gas/
[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, 2012, 2013
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 /* Check assumptions made in this file.  */
38 typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
39 typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
40
41 #ifdef DEBUG
42 #define DBG(x) printf x
43 #else
44 #define DBG(x)
45 #endif
46
47 /* Clean up namespace so we can include obj-elf.h too.  */
48 static int mips_output_flavor (void);
49 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
50 #undef OBJ_PROCESS_STAB
51 #undef OUTPUT_FLAVOR
52 #undef S_GET_ALIGN
53 #undef S_GET_SIZE
54 #undef S_SET_ALIGN
55 #undef S_SET_SIZE
56 #undef obj_frob_file
57 #undef obj_frob_file_after_relocs
58 #undef obj_frob_symbol
59 #undef obj_pop_insert
60 #undef obj_sec_sym_ok_for_reloc
61 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
62
63 #include "obj-elf.h"
64 /* Fix any of them that we actually care about.  */
65 #undef OUTPUT_FLAVOR
66 #define OUTPUT_FLAVOR mips_output_flavor()
67
68 #include "elf/mips.h"
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 static char *mips_regmask_frag;
89
90 #define ZERO 0
91 #define ATREG 1
92 #define S0  16
93 #define S7  23
94 #define TREG 24
95 #define PIC_CALL_REG 25
96 #define KT0 26
97 #define KT1 27
98 #define GP  28
99 #define SP  29
100 #define FP  30
101 #define RA  31
102
103 #define ILLEGAL_REG (32)
104
105 #define AT  mips_opts.at
106
107 extern int target_big_endian;
108
109 /* The name of the readonly data section.  */
110 #define RDATA_SECTION_NAME ".rodata"
111
112 /* Ways in which an instruction can be "appended" to the output.  */
113 enum append_method {
114   /* Just add it normally.  */
115   APPEND_ADD,
116
117   /* Add it normally and then add a nop.  */
118   APPEND_ADD_WITH_NOP,
119
120   /* Turn an instruction with a delay slot into a "compact" version.  */
121   APPEND_ADD_COMPACT,
122
123   /* Insert the instruction before the last one.  */
124   APPEND_SWAP
125 };
126
127 /* Information about an instruction, including its format, operands
128    and fixups.  */
129 struct mips_cl_insn
130 {
131   /* The opcode's entry in mips_opcodes or mips16_opcodes.  */
132   const struct mips_opcode *insn_mo;
133
134   /* The 16-bit or 32-bit bitstring of the instruction itself.  This is
135      a copy of INSN_MO->match with the operands filled in.  If we have
136      decided to use an extended MIPS16 instruction, this includes the
137      extension.  */
138   unsigned long insn_opcode;
139
140   /* The frag that contains the instruction.  */
141   struct frag *frag;
142
143   /* The offset into FRAG of the first instruction byte.  */
144   long where;
145
146   /* The relocs associated with the instruction, if any.  */
147   fixS *fixp[3];
148
149   /* True if this entry cannot be moved from its current position.  */
150   unsigned int fixed_p : 1;
151
152   /* True if this instruction occurred in a .set noreorder block.  */
153   unsigned int noreorder_p : 1;
154
155   /* True for mips16 instructions that jump to an absolute address.  */
156   unsigned int mips16_absolute_jump_p : 1;
157
158   /* True if this instruction is complete.  */
159   unsigned int complete_p : 1;
160
161   /* True if this instruction is cleared from history by unconditional
162      branch.  */
163   unsigned int cleared_p : 1;
164 };
165
166 /* The ABI to use.  */
167 enum mips_abi_level
168 {
169   NO_ABI = 0,
170   O32_ABI,
171   O64_ABI,
172   N32_ABI,
173   N64_ABI,
174   EABI_ABI
175 };
176
177 /* MIPS ABI we are using for this output file.  */
178 static enum mips_abi_level mips_abi = NO_ABI;
179
180 /* Whether or not we have code that can call pic code.  */
181 int mips_abicalls = FALSE;
182
183 /* Whether or not we have code which can be put into a shared
184    library.  */
185 static bfd_boolean mips_in_shared = TRUE;
186
187 /* This is the set of options which may be modified by the .set
188    pseudo-op.  We use a struct so that .set push and .set pop are more
189    reliable.  */
190
191 struct mips_set_options
192 {
193   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
194      if it has not been initialized.  Changed by `.set mipsN', and the
195      -mipsN command line option, and the default CPU.  */
196   int isa;
197   /* Enabled Application Specific Extensions (ASEs).  Changed by `.set
198      <asename>', by command line options, and based on the default
199      architecture.  */
200   int ase;
201   /* Whether we are assembling for the mips16 processor.  0 if we are
202      not, 1 if we are, and -1 if the value has not been initialized.
203      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
204      -nomips16 command line options, and the default CPU.  */
205   int mips16;
206   /* Whether we are assembling for the mipsMIPS ASE.  0 if we are not,
207      1 if we are, and -1 if the value has not been initialized.  Changed
208      by `.set micromips' and `.set nomicromips', and the -mmicromips
209      and -mno-micromips command line options, and the default CPU.  */
210   int micromips;
211   /* Non-zero if we should not reorder instructions.  Changed by `.set
212      reorder' and `.set noreorder'.  */
213   int noreorder;
214   /* Non-zero if we should not permit the register designated "assembler
215      temporary" to be used in instructions.  The value is the register
216      number, normally $at ($1).  Changed by `.set at=REG', `.set noat'
217      (same as `.set at=$0') and `.set at' (same as `.set at=$1').  */
218   unsigned int at;
219   /* Non-zero if we should warn when a macro instruction expands into
220      more than one machine instruction.  Changed by `.set nomacro' and
221      `.set macro'.  */
222   int warn_about_macros;
223   /* Non-zero if we should not move instructions.  Changed by `.set
224      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
225   int nomove;
226   /* Non-zero if we should not optimize branches by moving the target
227      of the branch into the delay slot.  Actually, we don't perform
228      this optimization anyhow.  Changed by `.set bopt' and `.set
229      nobopt'.  */
230   int nobopt;
231   /* Non-zero if we should not autoextend mips16 instructions.
232      Changed by `.set autoextend' and `.set noautoextend'.  */
233   int noautoextend;
234   /* True if we should only emit 32-bit microMIPS instructions.
235      Changed by `.set insn32' and `.set noinsn32', and the -minsn32
236      and -mno-insn32 command line options.  */
237   bfd_boolean insn32;
238   /* Restrict general purpose registers and floating point registers
239      to 32 bit.  This is initially determined when -mgp32 or -mfp32
240      is passed but can changed if the assembler code uses .set mipsN.  */
241   int gp32;
242   int fp32;
243   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
244      command line option, and the default CPU.  */
245   int arch;
246   /* True if ".set sym32" is in effect.  */
247   bfd_boolean sym32;
248   /* True if floating-point operations are not allowed.  Changed by .set
249      softfloat or .set hardfloat, by command line options -msoft-float or
250      -mhard-float.  The default is false.  */
251   bfd_boolean soft_float;
252
253   /* True if only single-precision floating-point operations are allowed.
254      Changed by .set singlefloat or .set doublefloat, command-line options
255      -msingle-float or -mdouble-float.  The default is false.  */
256   bfd_boolean single_float;
257 };
258
259 /* This is the struct we use to hold the current set of options.  Note
260    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
261    -1 to indicate that they have not been initialized.  */
262
263 /* True if -mgp32 was passed.  */
264 static int file_mips_gp32 = -1;
265
266 /* True if -mfp32 was passed.  */
267 static int file_mips_fp32 = -1;
268
269 /* 1 if -msoft-float, 0 if -mhard-float.  The default is 0.  */
270 static int file_mips_soft_float = 0;
271
272 /* 1 if -msingle-float, 0 if -mdouble-float.  The default is 0.   */
273 static int file_mips_single_float = 0;
274
275 static struct mips_set_options mips_opts =
276 {
277   /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
278   /* noreorder */ 0,  /* at */ ATREG, /* warn_about_macros */ 0,
279   /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
280   /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
281   /* soft_float */ FALSE, /* single_float */ FALSE
282 };
283
284 /* The set of ASEs that were selected on the command line, either
285    explicitly via ASE options or implicitly through things like -march.  */
286 static unsigned int file_ase;
287
288 /* Which bits of file_ase were explicitly set or cleared by ASE options.  */
289 static unsigned int file_ase_explicit;
290
291 /* These variables are filled in with the masks of registers used.
292    The object format code reads them and puts them in the appropriate
293    place.  */
294 unsigned long mips_gprmask;
295 unsigned long mips_cprmask[4];
296
297 /* MIPS ISA we are using for this output file.  */
298 static int file_mips_isa = ISA_UNKNOWN;
299
300 /* True if any MIPS16 code was produced.  */
301 static int file_ase_mips16;
302
303 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32               \
304                               || mips_opts.isa == ISA_MIPS32R2          \
305                               || mips_opts.isa == ISA_MIPS64            \
306                               || mips_opts.isa == ISA_MIPS64R2)
307
308 /* True if any microMIPS code was produced.  */
309 static int file_ase_micromips;
310
311 /* True if we want to create R_MIPS_JALR for jalr $25.  */
312 #ifdef TE_IRIX
313 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
314 #else
315 /* As a GNU extension, we use R_MIPS_JALR for o32 too.  However,
316    because there's no place for any addend, the only acceptable
317    expression is a bare symbol.  */
318 #define MIPS_JALR_HINT_P(EXPR) \
319   (!HAVE_IN_PLACE_ADDENDS \
320    || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
321 #endif
322
323 /* The argument of the -march= flag.  The architecture we are assembling.  */
324 static int file_mips_arch = CPU_UNKNOWN;
325 static const char *mips_arch_string;
326
327 /* The argument of the -mtune= flag.  The architecture for which we
328    are optimizing.  */
329 static int mips_tune = CPU_UNKNOWN;
330 static const char *mips_tune_string;
331
332 /* True when generating 32-bit code for a 64-bit processor.  */
333 static int mips_32bitmode = 0;
334
335 /* True if the given ABI requires 32-bit registers.  */
336 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
337
338 /* Likewise 64-bit registers.  */
339 #define ABI_NEEDS_64BIT_REGS(ABI)       \
340   ((ABI) == N32_ABI                     \
341    || (ABI) == N64_ABI                  \
342    || (ABI) == O64_ABI)
343
344 /*  Return true if ISA supports 64 bit wide gp registers.  */
345 #define ISA_HAS_64BIT_REGS(ISA)         \
346   ((ISA) == ISA_MIPS3                   \
347    || (ISA) == ISA_MIPS4                \
348    || (ISA) == ISA_MIPS5                \
349    || (ISA) == ISA_MIPS64               \
350    || (ISA) == ISA_MIPS64R2)
351
352 /*  Return true if ISA supports 64 bit wide float registers.  */
353 #define ISA_HAS_64BIT_FPRS(ISA)         \
354   ((ISA) == ISA_MIPS3                   \
355    || (ISA) == ISA_MIPS4                \
356    || (ISA) == ISA_MIPS5                \
357    || (ISA) == ISA_MIPS32R2             \
358    || (ISA) == ISA_MIPS64               \
359    || (ISA) == ISA_MIPS64R2)
360
361 /* Return true if ISA supports 64-bit right rotate (dror et al.)
362    instructions.  */
363 #define ISA_HAS_DROR(ISA)               \
364   ((ISA) == ISA_MIPS64R2                \
365    || (mips_opts.micromips              \
366        && ISA_HAS_64BIT_REGS (ISA))     \
367    )
368
369 /* Return true if ISA supports 32-bit right rotate (ror et al.)
370    instructions.  */
371 #define ISA_HAS_ROR(ISA)                \
372   ((ISA) == ISA_MIPS32R2                \
373    || (ISA) == ISA_MIPS64R2             \
374    || (mips_opts.ase & ASE_SMARTMIPS)   \
375    || mips_opts.micromips               \
376    )
377
378 /* Return true if ISA supports single-precision floats in odd registers.  */
379 #define ISA_HAS_ODD_SINGLE_FPR(ISA)     \
380   ((ISA) == ISA_MIPS32                  \
381    || (ISA) == ISA_MIPS32R2             \
382    || (ISA) == ISA_MIPS64               \
383    || (ISA) == ISA_MIPS64R2)
384
385 /* Return true if ISA supports move to/from high part of a 64-bit
386    floating-point register. */
387 #define ISA_HAS_MXHC1(ISA)              \
388   ((ISA) == ISA_MIPS32R2                \
389    || (ISA) == ISA_MIPS64R2)
390
391 #define HAVE_32BIT_GPRS                            \
392     (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
393
394 #define HAVE_32BIT_FPRS                            \
395     (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
396
397 #define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
398 #define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
399
400 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
401
402 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
403
404 /* True if relocations are stored in-place.  */
405 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
406
407 /* The ABI-derived address size.  */
408 #define HAVE_64BIT_ADDRESSES \
409   (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
410 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
411
412 /* The size of symbolic constants (i.e., expressions of the form
413    "SYMBOL" or "SYMBOL + OFFSET").  */
414 #define HAVE_32BIT_SYMBOLS \
415   (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
416 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
417
418 /* Addresses are loaded in different ways, depending on the address size
419    in use.  The n32 ABI Documentation also mandates the use of additions
420    with overflow checking, but existing implementations don't follow it.  */
421 #define ADDRESS_ADD_INSN                                                \
422    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
423
424 #define ADDRESS_ADDI_INSN                                               \
425    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
426
427 #define ADDRESS_LOAD_INSN                                               \
428    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
429
430 #define ADDRESS_STORE_INSN                                              \
431    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
432
433 /* Return true if the given CPU supports the MIPS16 ASE.  */
434 #define CPU_HAS_MIPS16(cpu)                                             \
435    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
436     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
437
438 /* Return true if the given CPU supports the microMIPS ASE.  */
439 #define CPU_HAS_MICROMIPS(cpu)  0
440
441 /* True if CPU has a dror instruction.  */
442 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
443
444 /* True if CPU has a ror instruction.  */
445 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
446
447 /* True if CPU is in the Octeon family */
448 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
449
450 /* True if CPU has seq/sne and seqi/snei instructions.  */
451 #define CPU_HAS_SEQ(CPU)        (CPU_IS_OCTEON (CPU))
452
453 /* True, if CPU has support for ldc1 and sdc1. */
454 #define CPU_HAS_LDC1_SDC1(CPU)  \
455    ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
456
457 /* True if mflo and mfhi can be immediately followed by instructions
458    which write to the HI and LO registers.
459
460    According to MIPS specifications, MIPS ISAs I, II, and III need
461    (at least) two instructions between the reads of HI/LO and
462    instructions which write them, and later ISAs do not.  Contradicting
463    the MIPS specifications, some MIPS IV processor user manuals (e.g.
464    the UM for the NEC Vr5000) document needing the instructions between
465    HI/LO reads and writes, as well.  Therefore, we declare only MIPS32,
466    MIPS64 and later ISAs to have the interlocks, plus any specific
467    earlier-ISA CPUs for which CPU documentation declares that the
468    instructions are really interlocked.  */
469 #define hilo_interlocks \
470   (mips_opts.isa == ISA_MIPS32                        \
471    || mips_opts.isa == ISA_MIPS32R2                   \
472    || mips_opts.isa == ISA_MIPS64                     \
473    || mips_opts.isa == ISA_MIPS64R2                   \
474    || mips_opts.arch == CPU_R4010                     \
475    || mips_opts.arch == CPU_R5900                     \
476    || mips_opts.arch == CPU_R10000                    \
477    || mips_opts.arch == CPU_R12000                    \
478    || mips_opts.arch == CPU_R14000                    \
479    || mips_opts.arch == CPU_R16000                    \
480    || mips_opts.arch == CPU_RM7000                    \
481    || mips_opts.arch == CPU_VR5500                    \
482    || mips_opts.micromips                             \
483    )
484
485 /* Whether the processor uses hardware interlocks to protect reads
486    from the GPRs after they are loaded from memory, and thus does not
487    require nops to be inserted.  This applies to instructions marked
488    INSN_LOAD_MEMORY_DELAY.  These nops are only required at MIPS ISA
489    level I and microMIPS mode instructions are always interlocked.  */
490 #define gpr_interlocks                                \
491   (mips_opts.isa != ISA_MIPS1                         \
492    || mips_opts.arch == CPU_R3900                     \
493    || mips_opts.arch == CPU_R5900                     \
494    || mips_opts.micromips                             \
495    )
496
497 /* Whether the processor uses hardware interlocks to avoid delays
498    required by coprocessor instructions, and thus does not require
499    nops to be inserted.  This applies to instructions marked
500    INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
501    between instructions marked INSN_WRITE_COND_CODE and ones marked
502    INSN_READ_COND_CODE.  These nops are only required at MIPS ISA
503    levels I, II, and III and microMIPS mode instructions are always
504    interlocked.  */
505 /* Itbl support may require additional care here.  */
506 #define cop_interlocks                                \
507   ((mips_opts.isa != ISA_MIPS1                        \
508     && mips_opts.isa != ISA_MIPS2                     \
509     && mips_opts.isa != ISA_MIPS3)                    \
510    || mips_opts.arch == CPU_R4300                     \
511    || mips_opts.micromips                             \
512    )
513
514 /* Whether the processor uses hardware interlocks to protect reads
515    from coprocessor registers after they are loaded from memory, and
516    thus does not require nops to be inserted.  This applies to
517    instructions marked INSN_COPROC_MEMORY_DELAY.  These nops are only
518    requires at MIPS ISA level I and microMIPS mode instructions are
519    always interlocked.  */
520 #define cop_mem_interlocks                            \
521   (mips_opts.isa != ISA_MIPS1                         \
522    || mips_opts.micromips                             \
523    )
524
525 /* Is this a mfhi or mflo instruction?  */
526 #define MF_HILO_INSN(PINFO) \
527   ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
528
529 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
530    has been selected.  This implies, in particular, that addresses of text
531    labels have their LSB set.  */
532 #define HAVE_CODE_COMPRESSION                                           \
533   ((mips_opts.mips16 | mips_opts.micromips) != 0)
534
535 /* The minimum and maximum signed values that can be stored in a GPR.  */
536 #define GPR_SMAX ((offsetT) (((valueT) 1 << (HAVE_64BIT_GPRS ? 63 : 31)) - 1))
537 #define GPR_SMIN (-GPR_SMAX - 1)
538
539 /* MIPS PIC level.  */
540
541 enum mips_pic_level mips_pic;
542
543 /* 1 if we should generate 32 bit offsets from the $gp register in
544    SVR4_PIC mode.  Currently has no meaning in other modes.  */
545 static int mips_big_got = 0;
546
547 /* 1 if trap instructions should used for overflow rather than break
548    instructions.  */
549 static int mips_trap = 0;
550
551 /* 1 if double width floating point constants should not be constructed
552    by assembling two single width halves into two single width floating
553    point registers which just happen to alias the double width destination
554    register.  On some architectures this aliasing can be disabled by a bit
555    in the status register, and the setting of this bit cannot be determined
556    automatically at assemble time.  */
557 static int mips_disable_float_construction;
558
559 /* Non-zero if any .set noreorder directives were used.  */
560
561 static int mips_any_noreorder;
562
563 /* Non-zero if nops should be inserted when the register referenced in
564    an mfhi/mflo instruction is read in the next two instructions.  */
565 static int mips_7000_hilo_fix;
566
567 /* The size of objects in the small data section.  */
568 static unsigned int g_switch_value = 8;
569 /* Whether the -G option was used.  */
570 static int g_switch_seen = 0;
571
572 #define N_RMASK 0xc4
573 #define N_VFP   0xd4
574
575 /* If we can determine in advance that GP optimization won't be
576    possible, we can skip the relaxation stuff that tries to produce
577    GP-relative references.  This makes delay slot optimization work
578    better.
579
580    This function can only provide a guess, but it seems to work for
581    gcc output.  It needs to guess right for gcc, otherwise gcc
582    will put what it thinks is a GP-relative instruction in a branch
583    delay slot.
584
585    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
586    fixed it for the non-PIC mode.  KR 95/04/07  */
587 static int nopic_need_relax (symbolS *, int);
588
589 /* handle of the OPCODE hash table */
590 static struct hash_control *op_hash = NULL;
591
592 /* The opcode hash table we use for the mips16.  */
593 static struct hash_control *mips16_op_hash = NULL;
594
595 /* The opcode hash table we use for the microMIPS ASE.  */
596 static struct hash_control *micromips_op_hash = NULL;
597
598 /* This array holds the chars that always start a comment.  If the
599     pre-processor is disabled, these aren't very useful */
600 const char comment_chars[] = "#";
601
602 /* This array holds the chars that only start a comment at the beginning of
603    a line.  If the line seems to have the form '# 123 filename'
604    .line and .file directives will appear in the pre-processed output */
605 /* Note that input_file.c hand checks for '#' at the beginning of the
606    first line of the input file.  This is because the compiler outputs
607    #NO_APP at the beginning of its output.  */
608 /* Also note that C style comments are always supported.  */
609 const char line_comment_chars[] = "#";
610
611 /* This array holds machine specific line separator characters.  */
612 const char line_separator_chars[] = ";";
613
614 /* Chars that can be used to separate mant from exp in floating point nums */
615 const char EXP_CHARS[] = "eE";
616
617 /* Chars that mean this number is a floating point constant */
618 /* As in 0f12.456 */
619 /* or    0d1.2345e12 */
620 const char FLT_CHARS[] = "rRsSfFdDxXpP";
621
622 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
623    changed in read.c .  Ideally it shouldn't have to know about it at all,
624    but nothing is ideal around here.
625  */
626
627 static char *insn_error;
628
629 static int auto_align = 1;
630
631 /* When outputting SVR4 PIC code, the assembler needs to know the
632    offset in the stack frame from which to restore the $gp register.
633    This is set by the .cprestore pseudo-op, and saved in this
634    variable.  */
635 static offsetT mips_cprestore_offset = -1;
636
637 /* Similar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
638    more optimizations, it can use a register value instead of a memory-saved
639    offset and even an other register than $gp as global pointer.  */
640 static offsetT mips_cpreturn_offset = -1;
641 static int mips_cpreturn_register = -1;
642 static int mips_gp_register = GP;
643 static int mips_gprel_offset = 0;
644
645 /* Whether mips_cprestore_offset has been set in the current function
646    (or whether it has already been warned about, if not).  */
647 static int mips_cprestore_valid = 0;
648
649 /* This is the register which holds the stack frame, as set by the
650    .frame pseudo-op.  This is needed to implement .cprestore.  */
651 static int mips_frame_reg = SP;
652
653 /* Whether mips_frame_reg has been set in the current function
654    (or whether it has already been warned about, if not).  */
655 static int mips_frame_reg_valid = 0;
656
657 /* To output NOP instructions correctly, we need to keep information
658    about the previous two instructions.  */
659
660 /* Whether we are optimizing.  The default value of 2 means to remove
661    unneeded NOPs and swap branch instructions when possible.  A value
662    of 1 means to not swap branches.  A value of 0 means to always
663    insert NOPs.  */
664 static int mips_optimize = 2;
665
666 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
667    equivalent to seeing no -g option at all.  */
668 static int mips_debug = 0;
669
670 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata.  */
671 #define MAX_VR4130_NOPS 4
672
673 /* The maximum number of NOPs needed to fill delay slots.  */
674 #define MAX_DELAY_NOPS 2
675
676 /* The maximum number of NOPs needed for any purpose.  */
677 #define MAX_NOPS 4
678
679 /* A list of previous instructions, with index 0 being the most recent.
680    We need to look back MAX_NOPS instructions when filling delay slots
681    or working around processor errata.  We need to look back one
682    instruction further if we're thinking about using history[0] to
683    fill a branch delay slot.  */
684 static struct mips_cl_insn history[1 + MAX_NOPS];
685
686 /* Nop instructions used by emit_nop.  */
687 static struct mips_cl_insn nop_insn;
688 static struct mips_cl_insn mips16_nop_insn;
689 static struct mips_cl_insn micromips_nop16_insn;
690 static struct mips_cl_insn micromips_nop32_insn;
691
692 /* The appropriate nop for the current mode.  */
693 #define NOP_INSN (mips_opts.mips16                                      \
694                   ? &mips16_nop_insn                                    \
695                   : (mips_opts.micromips                                \
696                      ? (mips_opts.insn32                                \
697                         ? &micromips_nop32_insn                         \
698                         : &micromips_nop16_insn)                        \
699                      : &nop_insn))
700
701 /* The size of NOP_INSN in bytes.  */
702 #define NOP_INSN_SIZE ((mips_opts.mips16                                \
703                         || (mips_opts.micromips && !mips_opts.insn32))  \
704                        ? 2 : 4)
705
706 /* If this is set, it points to a frag holding nop instructions which
707    were inserted before the start of a noreorder section.  If those
708    nops turn out to be unnecessary, the size of the frag can be
709    decreased.  */
710 static fragS *prev_nop_frag;
711
712 /* The number of nop instructions we created in prev_nop_frag.  */
713 static int prev_nop_frag_holds;
714
715 /* The number of nop instructions that we know we need in
716    prev_nop_frag.  */
717 static int prev_nop_frag_required;
718
719 /* The number of instructions we've seen since prev_nop_frag.  */
720 static int prev_nop_frag_since;
721
722 /* Relocations against symbols are sometimes done in two parts, with a HI
723    relocation and a LO relocation.  Each relocation has only 16 bits of
724    space to store an addend.  This means that in order for the linker to
725    handle carries correctly, it must be able to locate both the HI and
726    the LO relocation.  This means that the relocations must appear in
727    order in the relocation table.
728
729    In order to implement this, we keep track of each unmatched HI
730    relocation.  We then sort them so that they immediately precede the
731    corresponding LO relocation.  */
732
733 struct mips_hi_fixup
734 {
735   /* Next HI fixup.  */
736   struct mips_hi_fixup *next;
737   /* This fixup.  */
738   fixS *fixp;
739   /* The section this fixup is in.  */
740   segT seg;
741 };
742
743 /* The list of unmatched HI relocs.  */
744
745 static struct mips_hi_fixup *mips_hi_fixup_list;
746
747 /* The frag containing the last explicit relocation operator.
748    Null if explicit relocations have not been used.  */
749
750 static fragS *prev_reloc_op_frag;
751
752 /* Map normal MIPS register numbers to mips16 register numbers.  */
753
754 #define X ILLEGAL_REG
755 static const int mips32_to_16_reg_map[] =
756 {
757   X, X, 2, 3, 4, 5, 6, 7,
758   X, X, X, X, X, X, X, X,
759   0, 1, X, X, X, X, X, X,
760   X, X, X, X, X, X, X, X
761 };
762 #undef X
763
764 /* Map mips16 register numbers to normal MIPS register numbers.  */
765
766 static const unsigned int mips16_to_32_reg_map[] =
767 {
768   16, 17, 2, 3, 4, 5, 6, 7
769 };
770
771 /* Map normal MIPS register numbers to microMIPS register numbers.  */
772
773 #define mips32_to_micromips_reg_b_map   mips32_to_16_reg_map
774 #define mips32_to_micromips_reg_c_map   mips32_to_16_reg_map
775 #define mips32_to_micromips_reg_d_map   mips32_to_16_reg_map
776 #define mips32_to_micromips_reg_e_map   mips32_to_16_reg_map
777 #define mips32_to_micromips_reg_f_map   mips32_to_16_reg_map
778 #define mips32_to_micromips_reg_g_map   mips32_to_16_reg_map
779 #define mips32_to_micromips_reg_l_map   mips32_to_16_reg_map
780
781 #define X ILLEGAL_REG
782 /* reg type m: 0, 17, 2, 3, 16, 18, 19, 20.  */
783 static const int mips32_to_micromips_reg_m_map[] =
784 {
785   0, X, 2, 3, X, X, X, X,
786   X, X, X, X, X, X, X, X,
787   4, 1, 5, 6, 7, X, X, X,
788   X, X, X, X, X, X, X, X
789 };
790
791 /* reg type q: 0, 2-7. 17.  */
792 static const int mips32_to_micromips_reg_q_map[] =
793 {
794   0, X, 2, 3, 4, 5, 6, 7,
795   X, X, X, X, X, X, X, X,
796   X, 1, X, X, X, X, X, X,
797   X, X, X, X, X, X, X, X
798 };
799
800 #define mips32_to_micromips_reg_n_map  mips32_to_micromips_reg_m_map
801 #undef X
802
803 /* Map microMIPS register numbers to normal MIPS register numbers.  */
804
805 #define micromips_to_32_reg_b_map       mips16_to_32_reg_map
806 #define micromips_to_32_reg_c_map       mips16_to_32_reg_map
807 #define micromips_to_32_reg_d_map       mips16_to_32_reg_map
808 #define micromips_to_32_reg_e_map       mips16_to_32_reg_map
809 #define micromips_to_32_reg_f_map       mips16_to_32_reg_map
810 #define micromips_to_32_reg_g_map       mips16_to_32_reg_map
811
812 /* The microMIPS registers with type h.  */
813 static const unsigned int micromips_to_32_reg_h_map1[] =
814 {
815   5, 5, 6, 4, 4, 4, 4, 4
816 };
817 static const unsigned int micromips_to_32_reg_h_map2[] =
818 {
819   6, 7, 7, 21, 22, 5, 6, 7
820 };
821
822 #define micromips_to_32_reg_l_map       mips16_to_32_reg_map
823
824 /* The microMIPS registers with type m.  */
825 static const unsigned int micromips_to_32_reg_m_map[] =
826 {
827   0, 17, 2, 3, 16, 18, 19, 20
828 };
829
830 #define micromips_to_32_reg_n_map      micromips_to_32_reg_m_map
831
832 /* The microMIPS registers with type q.  */
833 static const unsigned int micromips_to_32_reg_q_map[] =
834 {
835   0, 17, 2, 3, 4, 5, 6, 7
836 };
837
838 /* microMIPS imm type B.  */
839 static const int micromips_imm_b_map[] =
840 {
841   1, 4, 8, 12, 16, 20, 24, -1
842 };
843
844 /* microMIPS imm type C.  */
845 static const int micromips_imm_c_map[] =
846 {
847   128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
848 };
849
850 /* Classifies the kind of instructions we're interested in when
851    implementing -mfix-vr4120.  */
852 enum fix_vr4120_class
853 {
854   FIX_VR4120_MACC,
855   FIX_VR4120_DMACC,
856   FIX_VR4120_MULT,
857   FIX_VR4120_DMULT,
858   FIX_VR4120_DIV,
859   FIX_VR4120_MTHILO,
860   NUM_FIX_VR4120_CLASSES
861 };
862
863 /* ...likewise -mfix-loongson2f-jump.  */
864 static bfd_boolean mips_fix_loongson2f_jump;
865
866 /* ...likewise -mfix-loongson2f-nop.  */
867 static bfd_boolean mips_fix_loongson2f_nop;
868
869 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed.  */
870 static bfd_boolean mips_fix_loongson2f;
871
872 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
873    there must be at least one other instruction between an instruction
874    of type X and an instruction of type Y.  */
875 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
876
877 /* True if -mfix-vr4120 is in force.  */
878 static int mips_fix_vr4120;
879
880 /* ...likewise -mfix-vr4130.  */
881 static int mips_fix_vr4130;
882
883 /* ...likewise -mfix-24k.  */
884 static int mips_fix_24k;
885
886 /* ...likewise -mfix-cn63xxp1 */
887 static bfd_boolean mips_fix_cn63xxp1;
888
889 /* We don't relax branches by default, since this causes us to expand
890    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
891    fail to compute the offset before expanding the macro to the most
892    efficient expansion.  */
893
894 static int mips_relax_branch;
895 \f
896 /* The expansion of many macros depends on the type of symbol that
897    they refer to.  For example, when generating position-dependent code,
898    a macro that refers to a symbol may have two different expansions,
899    one which uses GP-relative addresses and one which uses absolute
900    addresses.  When generating SVR4-style PIC, a macro may have
901    different expansions for local and global symbols.
902
903    We handle these situations by generating both sequences and putting
904    them in variant frags.  In position-dependent code, the first sequence
905    will be the GP-relative one and the second sequence will be the
906    absolute one.  In SVR4 PIC, the first sequence will be for global
907    symbols and the second will be for local symbols.
908
909    The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
910    SECOND are the lengths of the two sequences in bytes.  These fields
911    can be extracted using RELAX_FIRST() and RELAX_SECOND().  In addition,
912    the subtype has the following flags:
913
914    RELAX_USE_SECOND
915         Set if it has been decided that we should use the second
916         sequence instead of the first.
917
918    RELAX_SECOND_LONGER
919         Set in the first variant frag if the macro's second implementation
920         is longer than its first.  This refers to the macro as a whole,
921         not an individual relaxation.
922
923    RELAX_NOMACRO
924         Set in the first variant frag if the macro appeared in a .set nomacro
925         block and if one alternative requires a warning but the other does not.
926
927    RELAX_DELAY_SLOT
928         Like RELAX_NOMACRO, but indicates that the macro appears in a branch
929         delay slot.
930
931    RELAX_DELAY_SLOT_16BIT
932         Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
933         16-bit instruction.
934
935    RELAX_DELAY_SLOT_SIZE_FIRST
936         Like RELAX_DELAY_SLOT, but indicates that the first implementation of
937         the macro is of the wrong size for the branch delay slot.
938
939    RELAX_DELAY_SLOT_SIZE_SECOND
940         Like RELAX_DELAY_SLOT, but indicates that the second implementation of
941         the macro is of the wrong size for the branch delay slot.
942
943    The frag's "opcode" points to the first fixup for relaxable code.
944
945    Relaxable macros are generated using a sequence such as:
946
947       relax_start (SYMBOL);
948       ... generate first expansion ...
949       relax_switch ();
950       ... generate second expansion ...
951       relax_end ();
952
953    The code and fixups for the unwanted alternative are discarded
954    by md_convert_frag.  */
955 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
956
957 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
958 #define RELAX_SECOND(X) ((X) & 0xff)
959 #define RELAX_USE_SECOND 0x10000
960 #define RELAX_SECOND_LONGER 0x20000
961 #define RELAX_NOMACRO 0x40000
962 #define RELAX_DELAY_SLOT 0x80000
963 #define RELAX_DELAY_SLOT_16BIT 0x100000
964 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
965 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
966
967 /* Branch without likely bit.  If label is out of range, we turn:
968
969         beq reg1, reg2, label
970         delay slot
971
972    into
973
974         bne reg1, reg2, 0f
975         nop
976         j label
977      0: delay slot
978
979    with the following opcode replacements:
980
981         beq <-> bne
982         blez <-> bgtz
983         bltz <-> bgez
984         bc1f <-> bc1t
985
986         bltzal <-> bgezal  (with jal label instead of j label)
987
988    Even though keeping the delay slot instruction in the delay slot of
989    the branch would be more efficient, it would be very tricky to do
990    correctly, because we'd have to introduce a variable frag *after*
991    the delay slot instruction, and expand that instead.  Let's do it
992    the easy way for now, even if the branch-not-taken case now costs
993    one additional instruction.  Out-of-range branches are not supposed
994    to be common, anyway.
995
996    Branch likely.  If label is out of range, we turn:
997
998         beql reg1, reg2, label
999         delay slot (annulled if branch not taken)
1000
1001    into
1002
1003         beql reg1, reg2, 1f
1004         nop
1005         beql $0, $0, 2f
1006         nop
1007      1: j[al] label
1008         delay slot (executed only if branch taken)
1009      2:
1010
1011    It would be possible to generate a shorter sequence by losing the
1012    likely bit, generating something like:
1013
1014         bne reg1, reg2, 0f
1015         nop
1016         j[al] label
1017         delay slot (executed only if branch taken)
1018      0:
1019
1020         beql -> bne
1021         bnel -> beq
1022         blezl -> bgtz
1023         bgtzl -> blez
1024         bltzl -> bgez
1025         bgezl -> bltz
1026         bc1fl -> bc1t
1027         bc1tl -> bc1f
1028
1029         bltzall -> bgezal  (with jal label instead of j label)
1030         bgezall -> bltzal  (ditto)
1031
1032
1033    but it's not clear that it would actually improve performance.  */
1034 #define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar)   \
1035   ((relax_substateT)                                            \
1036    (0xc0000000                                                  \
1037     | ((at) & 0x1f)                                             \
1038     | ((toofar) ? 0x20 : 0)                                     \
1039     | ((link) ? 0x40 : 0)                                       \
1040     | ((likely) ? 0x80 : 0)                                     \
1041     | ((uncond) ? 0x100 : 0)))
1042 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1043 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1044 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1045 #define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1046 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1047 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1048
1049 /* For mips16 code, we use an entirely different form of relaxation.
1050    mips16 supports two versions of most instructions which take
1051    immediate values: a small one which takes some small value, and a
1052    larger one which takes a 16 bit value.  Since branches also follow
1053    this pattern, relaxing these values is required.
1054
1055    We can assemble both mips16 and normal MIPS code in a single
1056    object.  Therefore, we need to support this type of relaxation at
1057    the same time that we support the relaxation described above.  We
1058    use the high bit of the subtype field to distinguish these cases.
1059
1060    The information we store for this type of relaxation is the
1061    argument code found in the opcode file for this relocation, whether
1062    the user explicitly requested a small or extended form, and whether
1063    the relocation is in a jump or jal delay slot.  That tells us the
1064    size of the value, and how it should be stored.  We also store
1065    whether the fragment is considered to be extended or not.  We also
1066    store whether this is known to be a branch to a different section,
1067    whether we have tried to relax this frag yet, and whether we have
1068    ever extended a PC relative fragment because of a shift count.  */
1069 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1070   (0x80000000                                                   \
1071    | ((type) & 0xff)                                            \
1072    | ((small) ? 0x100 : 0)                                      \
1073    | ((ext) ? 0x200 : 0)                                        \
1074    | ((dslot) ? 0x400 : 0)                                      \
1075    | ((jal_dslot) ? 0x800 : 0))
1076 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1077 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1078 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1079 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1080 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1081 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1082 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1083 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1084 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1085 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1086 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1087 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
1088
1089 /* For microMIPS code, we use relaxation similar to one we use for
1090    MIPS16 code.  Some instructions that take immediate values support
1091    two encodings: a small one which takes some small value, and a
1092    larger one which takes a 16 bit value.  As some branches also follow
1093    this pattern, relaxing these values is required.
1094
1095    We can assemble both microMIPS and normal MIPS code in a single
1096    object.  Therefore, we need to support this type of relaxation at
1097    the same time that we support the relaxation described above.  We
1098    use one of the high bits of the subtype field to distinguish these
1099    cases.
1100
1101    The information we store for this type of relaxation is the argument
1102    code found in the opcode file for this relocation, the register
1103    selected as the assembler temporary, whether the branch is
1104    unconditional, whether it is compact, whether it stores the link
1105    address implicitly in $ra, whether relaxation of out-of-range 32-bit
1106    branches to a sequence of instructions is enabled, and whether the
1107    displacement of a branch is too large to fit as an immediate argument
1108    of a 16-bit and a 32-bit branch, respectively.  */
1109 #define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1110                                relax32, toofar16, toofar32)     \
1111   (0x40000000                                                   \
1112    | ((type) & 0xff)                                            \
1113    | (((at) & 0x1f) << 8)                                       \
1114    | ((uncond) ? 0x2000 : 0)                                    \
1115    | ((compact) ? 0x4000 : 0)                                   \
1116    | ((link) ? 0x8000 : 0)                                      \
1117    | ((relax32) ? 0x10000 : 0)                                  \
1118    | ((toofar16) ? 0x20000 : 0)                                 \
1119    | ((toofar32) ? 0x40000 : 0))
1120 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1121 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1122 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1123 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1124 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1125 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1126 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1127
1128 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1129 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1130 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1131 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1132 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1133 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
1134
1135 /* Sign-extend 16-bit value X.  */
1136 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1137
1138 /* Is the given value a sign-extended 32-bit value?  */
1139 #define IS_SEXT_32BIT_NUM(x)                                            \
1140   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
1141    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1142
1143 /* Is the given value a sign-extended 16-bit value?  */
1144 #define IS_SEXT_16BIT_NUM(x)                                            \
1145   (((x) &~ (offsetT) 0x7fff) == 0                                       \
1146    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1147
1148 /* Is the given value a sign-extended 12-bit value?  */
1149 #define IS_SEXT_12BIT_NUM(x)                                            \
1150   (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1151
1152 /* Is the given value a sign-extended 9-bit value?  */
1153 #define IS_SEXT_9BIT_NUM(x)                                             \
1154   (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1155
1156 /* Is the given value a zero-extended 32-bit value?  Or a negated one?  */
1157 #define IS_ZEXT_32BIT_NUM(x)                                            \
1158   (((x) &~ (offsetT) 0xffffffff) == 0                                   \
1159    || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1160
1161 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1162    VALUE << SHIFT.  VALUE is evaluated exactly once.  */
1163 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1164   (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1165               | (((VALUE) & (MASK)) << (SHIFT)))
1166
1167 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1168    SHIFT places.  */
1169 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1170   (((STRUCT) >> (SHIFT)) & (MASK))
1171
1172 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1173    INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1174
1175    include/opcode/mips.h specifies operand fields using the macros
1176    OP_MASK_<FIELD> and OP_SH_<FIELD>.  The MIPS16 equivalents start
1177    with "MIPS16OP" instead of "OP".  */
1178 #define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1179   do \
1180     if (!(MICROMIPS)) \
1181       INSERT_BITS ((INSN).insn_opcode, VALUE, \
1182                    OP_MASK_##FIELD, OP_SH_##FIELD); \
1183     else \
1184       INSERT_BITS ((INSN).insn_opcode, VALUE, \
1185                    MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1186   while (0)
1187 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1188   INSERT_BITS ((INSN).insn_opcode, VALUE, \
1189                 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1190
1191 /* Extract the operand given by FIELD from mips_cl_insn INSN.  */
1192 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1193   (!(MICROMIPS) \
1194    ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1195    : EXTRACT_BITS ((INSN).insn_opcode, \
1196                    MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1197 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1198   EXTRACT_BITS ((INSN).insn_opcode, \
1199                 MIPS16OP_MASK_##FIELD, \
1200                 MIPS16OP_SH_##FIELD)
1201
1202 /* The MIPS16 EXTEND opcode, shifted left 16 places.  */
1203 #define MIPS16_EXTEND (0xf000U << 16)
1204 \f
1205 /* Whether or not we are emitting a branch-likely macro.  */
1206 static bfd_boolean emit_branch_likely_macro = FALSE;
1207
1208 /* Global variables used when generating relaxable macros.  See the
1209    comment above RELAX_ENCODE for more details about how relaxation
1210    is used.  */
1211 static struct {
1212   /* 0 if we're not emitting a relaxable macro.
1213      1 if we're emitting the first of the two relaxation alternatives.
1214      2 if we're emitting the second alternative.  */
1215   int sequence;
1216
1217   /* The first relaxable fixup in the current frag.  (In other words,
1218      the first fixup that refers to relaxable code.)  */
1219   fixS *first_fixup;
1220
1221   /* sizes[0] says how many bytes of the first alternative are stored in
1222      the current frag.  Likewise sizes[1] for the second alternative.  */
1223   unsigned int sizes[2];
1224
1225   /* The symbol on which the choice of sequence depends.  */
1226   symbolS *symbol;
1227 } mips_relax;
1228 \f
1229 /* Global variables used to decide whether a macro needs a warning.  */
1230 static struct {
1231   /* True if the macro is in a branch delay slot.  */
1232   bfd_boolean delay_slot_p;
1233
1234   /* Set to the length in bytes required if the macro is in a delay slot
1235      that requires a specific length of instruction, otherwise zero.  */
1236   unsigned int delay_slot_length;
1237
1238   /* For relaxable macros, sizes[0] is the length of the first alternative
1239      in bytes and sizes[1] is the length of the second alternative.
1240      For non-relaxable macros, both elements give the length of the
1241      macro in bytes.  */
1242   unsigned int sizes[2];
1243
1244   /* For relaxable macros, first_insn_sizes[0] is the length of the first
1245      instruction of the first alternative in bytes and first_insn_sizes[1]
1246      is the length of the first instruction of the second alternative.
1247      For non-relaxable macros, both elements give the length of the first
1248      instruction in bytes.
1249
1250      Set to zero if we haven't yet seen the first instruction.  */
1251   unsigned int first_insn_sizes[2];
1252
1253   /* For relaxable macros, insns[0] is the number of instructions for the
1254      first alternative and insns[1] is the number of instructions for the
1255      second alternative.
1256
1257      For non-relaxable macros, both elements give the number of
1258      instructions for the macro.  */
1259   unsigned int insns[2];
1260
1261   /* The first variant frag for this macro.  */
1262   fragS *first_frag;
1263 } mips_macro_warning;
1264 \f
1265 /* Prototypes for static functions.  */
1266
1267 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1268
1269 static void append_insn
1270   (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1271    bfd_boolean expansionp);
1272 static void mips_no_prev_insn (void);
1273 static void macro_build (expressionS *, const char *, const char *, ...);
1274 static void mips16_macro_build
1275   (expressionS *, const char *, const char *, va_list *);
1276 static void load_register (int, expressionS *, int);
1277 static void macro_start (void);
1278 static void macro_end (void);
1279 static void macro (struct mips_cl_insn *ip, char *str);
1280 static void mips16_macro (struct mips_cl_insn * ip);
1281 static void mips_ip (char *str, struct mips_cl_insn * ip);
1282 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1283 static void mips16_immed
1284   (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1285    unsigned int, unsigned long *);
1286 static size_t my_getSmallExpression
1287   (expressionS *, bfd_reloc_code_real_type *, char *);
1288 static void my_getExpression (expressionS *, char *);
1289 static void s_align (int);
1290 static void s_change_sec (int);
1291 static void s_change_section (int);
1292 static void s_cons (int);
1293 static void s_float_cons (int);
1294 static void s_mips_globl (int);
1295 static void s_option (int);
1296 static void s_mipsset (int);
1297 static void s_abicalls (int);
1298 static void s_cpload (int);
1299 static void s_cpsetup (int);
1300 static void s_cplocal (int);
1301 static void s_cprestore (int);
1302 static void s_cpreturn (int);
1303 static void s_dtprelword (int);
1304 static void s_dtpreldword (int);
1305 static void s_tprelword (int);
1306 static void s_tpreldword (int);
1307 static void s_gpvalue (int);
1308 static void s_gpword (int);
1309 static void s_gpdword (int);
1310 static void s_ehword (int);
1311 static void s_cpadd (int);
1312 static void s_insn (int);
1313 static void md_obj_begin (void);
1314 static void md_obj_end (void);
1315 static void s_mips_ent (int);
1316 static void s_mips_end (int);
1317 static void s_mips_frame (int);
1318 static void s_mips_mask (int reg_type);
1319 static void s_mips_stab (int);
1320 static void s_mips_weakext (int);
1321 static void s_mips_file (int);
1322 static void s_mips_loc (int);
1323 static bfd_boolean pic_need_relax (symbolS *, asection *);
1324 static int relaxed_branch_length (fragS *, asection *, int);
1325 static int validate_mips_insn (const struct mips_opcode *);
1326 static int validate_micromips_insn (const struct mips_opcode *);
1327 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1328 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1329
1330 /* Table and functions used to map between CPU/ISA names, and
1331    ISA levels, and CPU numbers.  */
1332
1333 struct mips_cpu_info
1334 {
1335   const char *name;           /* CPU or ISA name.  */
1336   int flags;                  /* MIPS_CPU_* flags.  */
1337   int ase;                    /* Set of ASEs implemented by the CPU.  */
1338   int isa;                    /* ISA level.  */
1339   int cpu;                    /* CPU number (default CPU if ISA).  */
1340 };
1341
1342 #define MIPS_CPU_IS_ISA         0x0001  /* Is this an ISA?  (If 0, a CPU.) */
1343
1344 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1345 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1346 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1347 \f
1348 /* Command-line options.  */
1349 const char *md_shortopts = "O::g::G:";
1350
1351 enum options
1352   {
1353     OPTION_MARCH = OPTION_MD_BASE,
1354     OPTION_MTUNE,
1355     OPTION_MIPS1,
1356     OPTION_MIPS2,
1357     OPTION_MIPS3,
1358     OPTION_MIPS4,
1359     OPTION_MIPS5,
1360     OPTION_MIPS32,
1361     OPTION_MIPS64,
1362     OPTION_MIPS32R2,
1363     OPTION_MIPS64R2,
1364     OPTION_MIPS16,
1365     OPTION_NO_MIPS16,
1366     OPTION_MIPS3D,
1367     OPTION_NO_MIPS3D,
1368     OPTION_MDMX,
1369     OPTION_NO_MDMX,
1370     OPTION_DSP,
1371     OPTION_NO_DSP,
1372     OPTION_MT,
1373     OPTION_NO_MT,
1374     OPTION_VIRT,
1375     OPTION_NO_VIRT,
1376     OPTION_SMARTMIPS,
1377     OPTION_NO_SMARTMIPS,
1378     OPTION_DSPR2,
1379     OPTION_NO_DSPR2,
1380     OPTION_EVA,
1381     OPTION_NO_EVA,
1382     OPTION_MICROMIPS,
1383     OPTION_NO_MICROMIPS,
1384     OPTION_MCU,
1385     OPTION_NO_MCU,
1386     OPTION_COMPAT_ARCH_BASE,
1387     OPTION_M4650,
1388     OPTION_NO_M4650,
1389     OPTION_M4010,
1390     OPTION_NO_M4010,
1391     OPTION_M4100,
1392     OPTION_NO_M4100,
1393     OPTION_M3900,
1394     OPTION_NO_M3900,
1395     OPTION_M7000_HILO_FIX,
1396     OPTION_MNO_7000_HILO_FIX,
1397     OPTION_FIX_24K,
1398     OPTION_NO_FIX_24K,
1399     OPTION_FIX_LOONGSON2F_JUMP,
1400     OPTION_NO_FIX_LOONGSON2F_JUMP,
1401     OPTION_FIX_LOONGSON2F_NOP,
1402     OPTION_NO_FIX_LOONGSON2F_NOP,
1403     OPTION_FIX_VR4120,
1404     OPTION_NO_FIX_VR4120,
1405     OPTION_FIX_VR4130,
1406     OPTION_NO_FIX_VR4130,
1407     OPTION_FIX_CN63XXP1,
1408     OPTION_NO_FIX_CN63XXP1,
1409     OPTION_TRAP,
1410     OPTION_BREAK,
1411     OPTION_EB,
1412     OPTION_EL,
1413     OPTION_FP32,
1414     OPTION_GP32,
1415     OPTION_CONSTRUCT_FLOATS,
1416     OPTION_NO_CONSTRUCT_FLOATS,
1417     OPTION_FP64,
1418     OPTION_GP64,
1419     OPTION_RELAX_BRANCH,
1420     OPTION_NO_RELAX_BRANCH,
1421     OPTION_INSN32,
1422     OPTION_NO_INSN32,
1423     OPTION_MSHARED,
1424     OPTION_MNO_SHARED,
1425     OPTION_MSYM32,
1426     OPTION_MNO_SYM32,
1427     OPTION_SOFT_FLOAT,
1428     OPTION_HARD_FLOAT,
1429     OPTION_SINGLE_FLOAT,
1430     OPTION_DOUBLE_FLOAT,
1431     OPTION_32,
1432     OPTION_CALL_SHARED,
1433     OPTION_CALL_NONPIC,
1434     OPTION_NON_SHARED,
1435     OPTION_XGOT,
1436     OPTION_MABI,
1437     OPTION_N32,
1438     OPTION_64,
1439     OPTION_MDEBUG,
1440     OPTION_NO_MDEBUG,
1441     OPTION_PDR,
1442     OPTION_NO_PDR,
1443     OPTION_MVXWORKS_PIC,
1444     OPTION_END_OF_ENUM
1445   };
1446
1447 struct option md_longopts[] =
1448 {
1449   /* Options which specify architecture.  */
1450   {"march", required_argument, NULL, OPTION_MARCH},
1451   {"mtune", required_argument, NULL, OPTION_MTUNE},
1452   {"mips0", no_argument, NULL, OPTION_MIPS1},
1453   {"mips1", no_argument, NULL, OPTION_MIPS1},
1454   {"mips2", no_argument, NULL, OPTION_MIPS2},
1455   {"mips3", no_argument, NULL, OPTION_MIPS3},
1456   {"mips4", no_argument, NULL, OPTION_MIPS4},
1457   {"mips5", no_argument, NULL, OPTION_MIPS5},
1458   {"mips32", no_argument, NULL, OPTION_MIPS32},
1459   {"mips64", no_argument, NULL, OPTION_MIPS64},
1460   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1461   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1462
1463   /* Options which specify Application Specific Extensions (ASEs).  */
1464   {"mips16", no_argument, NULL, OPTION_MIPS16},
1465   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1466   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1467   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1468   {"mdmx", no_argument, NULL, OPTION_MDMX},
1469   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1470   {"mdsp", no_argument, NULL, OPTION_DSP},
1471   {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1472   {"mmt", no_argument, NULL, OPTION_MT},
1473   {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1474   {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1475   {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1476   {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1477   {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1478   {"meva", no_argument, NULL, OPTION_EVA},
1479   {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1480   {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1481   {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1482   {"mmcu", no_argument, NULL, OPTION_MCU},
1483   {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1484   {"mvirt", no_argument, NULL, OPTION_VIRT},
1485   {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1486
1487   /* Old-style architecture options.  Don't add more of these.  */
1488   {"m4650", no_argument, NULL, OPTION_M4650},
1489   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1490   {"m4010", no_argument, NULL, OPTION_M4010},
1491   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1492   {"m4100", no_argument, NULL, OPTION_M4100},
1493   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1494   {"m3900", no_argument, NULL, OPTION_M3900},
1495   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1496
1497   /* Options which enable bug fixes.  */
1498   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1499   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1500   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1501   {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1502   {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1503   {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1504   {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1505   {"mfix-vr4120",    no_argument, NULL, OPTION_FIX_VR4120},
1506   {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1507   {"mfix-vr4130",    no_argument, NULL, OPTION_FIX_VR4130},
1508   {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1509   {"mfix-24k",    no_argument, NULL, OPTION_FIX_24K},
1510   {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1511   {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1512   {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1513
1514   /* Miscellaneous options.  */
1515   {"trap", no_argument, NULL, OPTION_TRAP},
1516   {"no-break", no_argument, NULL, OPTION_TRAP},
1517   {"break", no_argument, NULL, OPTION_BREAK},
1518   {"no-trap", no_argument, NULL, OPTION_BREAK},
1519   {"EB", no_argument, NULL, OPTION_EB},
1520   {"EL", no_argument, NULL, OPTION_EL},
1521   {"mfp32", no_argument, NULL, OPTION_FP32},
1522   {"mgp32", no_argument, NULL, OPTION_GP32},
1523   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1524   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1525   {"mfp64", no_argument, NULL, OPTION_FP64},
1526   {"mgp64", no_argument, NULL, OPTION_GP64},
1527   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1528   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1529   {"minsn32", no_argument, NULL, OPTION_INSN32},
1530   {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1531   {"mshared", no_argument, NULL, OPTION_MSHARED},
1532   {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1533   {"msym32", no_argument, NULL, OPTION_MSYM32},
1534   {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1535   {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1536   {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1537   {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1538   {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1539
1540   /* Strictly speaking this next option is ELF specific,
1541      but we allow it for other ports as well in order to
1542      make testing easier.  */
1543   {"32", no_argument, NULL, OPTION_32},
1544
1545   /* ELF-specific options.  */
1546   {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1547   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1548   {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1549   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
1550   {"xgot", no_argument, NULL, OPTION_XGOT},
1551   {"mabi", required_argument, NULL, OPTION_MABI},
1552   {"n32", no_argument, NULL, OPTION_N32},
1553   {"64", no_argument, NULL, OPTION_64},
1554   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1555   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1556   {"mpdr", no_argument, NULL, OPTION_PDR},
1557   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1558   {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1559
1560   {NULL, no_argument, NULL, 0}
1561 };
1562 size_t md_longopts_size = sizeof (md_longopts);
1563 \f
1564 /* Information about either an Application Specific Extension or an
1565    optional architecture feature that, for simplicity, we treat in the
1566    same way as an ASE.  */
1567 struct mips_ase
1568 {
1569   /* The name of the ASE, used in both the command-line and .set options.  */
1570   const char *name;
1571
1572   /* The associated ASE_* flags.  If the ASE is available on both 32-bit
1573      and 64-bit architectures, the flags here refer to the subset that
1574      is available on both.  */
1575   unsigned int flags;
1576
1577   /* The ASE_* flag used for instructions that are available on 64-bit
1578      architectures but that are not included in FLAGS.  */
1579   unsigned int flags64;
1580
1581   /* The command-line options that turn the ASE on and off.  */
1582   int option_on;
1583   int option_off;
1584
1585   /* The minimum required architecture revisions for MIPS32, MIPS64,
1586      microMIPS32 and microMIPS64, or -1 if the extension isn't supported.  */
1587   int mips32_rev;
1588   int mips64_rev;
1589   int micromips32_rev;
1590   int micromips64_rev;
1591 };
1592
1593 /* A table of all supported ASEs.  */
1594 static const struct mips_ase mips_ases[] = {
1595   { "dsp", ASE_DSP, ASE_DSP64,
1596     OPTION_DSP, OPTION_NO_DSP,
1597     2, 2, 2, 2 },
1598
1599   { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1600     OPTION_DSPR2, OPTION_NO_DSPR2,
1601     2, 2, 2, 2 },
1602
1603   { "eva", ASE_EVA, 0,
1604     OPTION_EVA, OPTION_NO_EVA,
1605     2, 2, 2, 2 },
1606
1607   { "mcu", ASE_MCU, 0,
1608     OPTION_MCU, OPTION_NO_MCU,
1609     2, 2, 2, 2 },
1610
1611   /* Deprecated in MIPS64r5, but we don't implement that yet.  */
1612   { "mdmx", ASE_MDMX, 0,
1613     OPTION_MDMX, OPTION_NO_MDMX,
1614     -1, 1, -1, -1 },
1615
1616   /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2.  */
1617   { "mips3d", ASE_MIPS3D, 0,
1618     OPTION_MIPS3D, OPTION_NO_MIPS3D,
1619     2, 1, -1, -1 },
1620
1621   { "mt", ASE_MT, 0,
1622     OPTION_MT, OPTION_NO_MT,
1623     2, 2, -1, -1 },
1624
1625   { "smartmips", ASE_SMARTMIPS, 0,
1626     OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1627     1, -1, -1, -1 },
1628
1629   { "virt", ASE_VIRT, ASE_VIRT64,
1630     OPTION_VIRT, OPTION_NO_VIRT,
1631     2, 2, 2, 2 }
1632 };
1633
1634 /* The set of ASEs that require -mfp64.  */
1635 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX)
1636
1637 /* Groups of ASE_* flags that represent different revisions of an ASE.  */
1638 static const unsigned int mips_ase_groups[] = {
1639   ASE_DSP | ASE_DSPR2
1640 };
1641 \f
1642 /* Pseudo-op table.
1643
1644    The following pseudo-ops from the Kane and Heinrich MIPS book
1645    should be defined here, but are currently unsupported: .alias,
1646    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1647
1648    The following pseudo-ops from the Kane and Heinrich MIPS book are
1649    specific to the type of debugging information being generated, and
1650    should be defined by the object format: .aent, .begin, .bend,
1651    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1652    .vreg.
1653
1654    The following pseudo-ops from the Kane and Heinrich MIPS book are
1655    not MIPS CPU specific, but are also not specific to the object file
1656    format.  This file is probably the best place to define them, but
1657    they are not currently supported: .asm0, .endr, .lab, .struct.  */
1658
1659 static const pseudo_typeS mips_pseudo_table[] =
1660 {
1661   /* MIPS specific pseudo-ops.  */
1662   {"option", s_option, 0},
1663   {"set", s_mipsset, 0},
1664   {"rdata", s_change_sec, 'r'},
1665   {"sdata", s_change_sec, 's'},
1666   {"livereg", s_ignore, 0},
1667   {"abicalls", s_abicalls, 0},
1668   {"cpload", s_cpload, 0},
1669   {"cpsetup", s_cpsetup, 0},
1670   {"cplocal", s_cplocal, 0},
1671   {"cprestore", s_cprestore, 0},
1672   {"cpreturn", s_cpreturn, 0},
1673   {"dtprelword", s_dtprelword, 0},
1674   {"dtpreldword", s_dtpreldword, 0},
1675   {"tprelword", s_tprelword, 0},
1676   {"tpreldword", s_tpreldword, 0},
1677   {"gpvalue", s_gpvalue, 0},
1678   {"gpword", s_gpword, 0},
1679   {"gpdword", s_gpdword, 0},
1680   {"ehword", s_ehword, 0},
1681   {"cpadd", s_cpadd, 0},
1682   {"insn", s_insn, 0},
1683
1684   /* Relatively generic pseudo-ops that happen to be used on MIPS
1685      chips.  */
1686   {"asciiz", stringer, 8 + 1},
1687   {"bss", s_change_sec, 'b'},
1688   {"err", s_err, 0},
1689   {"half", s_cons, 1},
1690   {"dword", s_cons, 3},
1691   {"weakext", s_mips_weakext, 0},
1692   {"origin", s_org, 0},
1693   {"repeat", s_rept, 0},
1694
1695   /* For MIPS this is non-standard, but we define it for consistency.  */
1696   {"sbss", s_change_sec, 'B'},
1697
1698   /* These pseudo-ops are defined in read.c, but must be overridden
1699      here for one reason or another.  */
1700   {"align", s_align, 0},
1701   {"byte", s_cons, 0},
1702   {"data", s_change_sec, 'd'},
1703   {"double", s_float_cons, 'd'},
1704   {"float", s_float_cons, 'f'},
1705   {"globl", s_mips_globl, 0},
1706   {"global", s_mips_globl, 0},
1707   {"hword", s_cons, 1},
1708   {"int", s_cons, 2},
1709   {"long", s_cons, 2},
1710   {"octa", s_cons, 4},
1711   {"quad", s_cons, 3},
1712   {"section", s_change_section, 0},
1713   {"short", s_cons, 1},
1714   {"single", s_float_cons, 'f'},
1715   {"stabd", s_mips_stab, 'd'},
1716   {"stabn", s_mips_stab, 'n'},
1717   {"stabs", s_mips_stab, 's'},
1718   {"text", s_change_sec, 't'},
1719   {"word", s_cons, 2},
1720
1721   { "extern", ecoff_directive_extern, 0},
1722
1723   { NULL, NULL, 0 },
1724 };
1725
1726 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1727 {
1728   /* These pseudo-ops should be defined by the object file format.
1729      However, a.out doesn't support them, so we have versions here.  */
1730   {"aent", s_mips_ent, 1},
1731   {"bgnb", s_ignore, 0},
1732   {"end", s_mips_end, 0},
1733   {"endb", s_ignore, 0},
1734   {"ent", s_mips_ent, 0},
1735   {"file", s_mips_file, 0},
1736   {"fmask", s_mips_mask, 'F'},
1737   {"frame", s_mips_frame, 0},
1738   {"loc", s_mips_loc, 0},
1739   {"mask", s_mips_mask, 'R'},
1740   {"verstamp", s_ignore, 0},
1741   { NULL, NULL, 0 },
1742 };
1743
1744 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1745    purpose of the `.dc.a' internal pseudo-op.  */
1746
1747 int
1748 mips_address_bytes (void)
1749 {
1750   return HAVE_64BIT_ADDRESSES ? 8 : 4;
1751 }
1752
1753 extern void pop_insert (const pseudo_typeS *);
1754
1755 void
1756 mips_pop_insert (void)
1757 {
1758   pop_insert (mips_pseudo_table);
1759   if (! ECOFF_DEBUGGING)
1760     pop_insert (mips_nonecoff_pseudo_table);
1761 }
1762 \f
1763 /* Symbols labelling the current insn.  */
1764
1765 struct insn_label_list
1766 {
1767   struct insn_label_list *next;
1768   symbolS *label;
1769 };
1770
1771 static struct insn_label_list *free_insn_labels;
1772 #define label_list tc_segment_info_data.labels
1773
1774 static void mips_clear_insn_labels (void);
1775 static void mips_mark_labels (void);
1776 static void mips_compressed_mark_labels (void);
1777
1778 static inline void
1779 mips_clear_insn_labels (void)
1780 {
1781   register struct insn_label_list **pl;
1782   segment_info_type *si;
1783
1784   if (now_seg)
1785     {
1786       for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1787         ;
1788       
1789       si = seg_info (now_seg);
1790       *pl = si->label_list;
1791       si->label_list = NULL;
1792     }
1793 }
1794
1795 /* Mark instruction labels in MIPS16/microMIPS mode.  */
1796
1797 static inline void
1798 mips_mark_labels (void)
1799 {
1800   if (HAVE_CODE_COMPRESSION)
1801     mips_compressed_mark_labels ();
1802 }
1803 \f
1804 static char *expr_end;
1805
1806 /* Expressions which appear in instructions.  These are set by
1807    mips_ip.  */
1808
1809 static expressionS imm_expr;
1810 static expressionS imm2_expr;
1811 static expressionS offset_expr;
1812
1813 /* Relocs associated with imm_expr and offset_expr.  */
1814
1815 static bfd_reloc_code_real_type imm_reloc[3]
1816   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1817 static bfd_reloc_code_real_type offset_reloc[3]
1818   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1819
1820 /* This is set to the resulting size of the instruction to be produced
1821    by mips16_ip if an explicit extension is used or by mips_ip if an
1822    explicit size is supplied.  */
1823
1824 static unsigned int forced_insn_length;
1825
1826 /* True if we are assembling an instruction.  All dot symbols defined during
1827    this time should be treated as code labels.  */
1828
1829 static bfd_boolean mips_assembling_insn;
1830
1831 /* The pdr segment for per procedure frame/regmask info.  Not used for
1832    ECOFF debugging.  */
1833
1834 static segT pdr_seg;
1835
1836 /* The default target format to use.  */
1837
1838 #if defined (TE_FreeBSD)
1839 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1840 #elif defined (TE_TMIPS)
1841 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1842 #else
1843 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1844 #endif
1845
1846 const char *
1847 mips_target_format (void)
1848 {
1849   switch (OUTPUT_FLAVOR)
1850     {
1851     case bfd_target_elf_flavour:
1852 #ifdef TE_VXWORKS
1853       if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1854         return (target_big_endian
1855                 ? "elf32-bigmips-vxworks"
1856                 : "elf32-littlemips-vxworks");
1857 #endif
1858       return (target_big_endian
1859               ? (HAVE_64BIT_OBJECTS
1860                  ? ELF_TARGET ("elf64-", "big")
1861                  : (HAVE_NEWABI
1862                     ? ELF_TARGET ("elf32-n", "big")
1863                     : ELF_TARGET ("elf32-", "big")))
1864               : (HAVE_64BIT_OBJECTS
1865                  ? ELF_TARGET ("elf64-", "little")
1866                  : (HAVE_NEWABI
1867                     ? ELF_TARGET ("elf32-n", "little")
1868                     : ELF_TARGET ("elf32-", "little"))));
1869     default:
1870       abort ();
1871       return NULL;
1872     }
1873 }
1874
1875 /* Return the ISA revision that is currently in use, or 0 if we are
1876    generating code for MIPS V or below.  */
1877
1878 static int
1879 mips_isa_rev (void)
1880 {
1881   if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
1882     return 2;
1883
1884   /* microMIPS implies revision 2 or above.  */
1885   if (mips_opts.micromips)
1886     return 2;
1887
1888   if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
1889     return 1;
1890
1891   return 0;
1892 }
1893
1894 /* Return the mask of all ASEs that are revisions of those in FLAGS.  */
1895
1896 static unsigned int
1897 mips_ase_mask (unsigned int flags)
1898 {
1899   unsigned int i;
1900
1901   for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
1902     if (flags & mips_ase_groups[i])
1903       flags |= mips_ase_groups[i];
1904   return flags;
1905 }
1906
1907 /* Check whether the current ISA supports ASE.  Issue a warning if
1908    appropriate.  */
1909
1910 static void
1911 mips_check_isa_supports_ase (const struct mips_ase *ase)
1912 {
1913   const char *base;
1914   int min_rev, size;
1915   static unsigned int warned_isa;
1916   static unsigned int warned_fp32;
1917
1918   if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1919     min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
1920   else
1921     min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
1922   if ((min_rev < 0 || mips_isa_rev () < min_rev)
1923       && (warned_isa & ase->flags) != ase->flags)
1924     {
1925       warned_isa |= ase->flags;
1926       base = mips_opts.micromips ? "microMIPS" : "MIPS";
1927       size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
1928       if (min_rev < 0)
1929         as_warn (_("The %d-bit %s architecture does not support the"
1930                    " `%s' extension"), size, base, ase->name);
1931       else
1932         as_warn (_("The `%s' extension requires %s%d revision %d or greater"),
1933                  ase->name, base, size, min_rev);
1934     }
1935   if ((ase->flags & FP64_ASES)
1936       && mips_opts.fp32
1937       && (warned_fp32 & ase->flags) != ase->flags)
1938     {
1939       warned_fp32 |= ase->flags;
1940       as_warn (_("The `%s' extension requires 64-bit FPRs"), ase->name);
1941     }
1942 }
1943
1944 /* Check all enabled ASEs to see whether they are supported by the
1945    chosen architecture.  */
1946
1947 static void
1948 mips_check_isa_supports_ases (void)
1949 {
1950   unsigned int i, mask;
1951
1952   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1953     {
1954       mask = mips_ase_mask (mips_ases[i].flags);
1955       if ((mips_opts.ase & mask) == mips_ases[i].flags)
1956         mips_check_isa_supports_ase (&mips_ases[i]);
1957     }
1958 }
1959
1960 /* Set the state of ASE to ENABLED_P.  Return the mask of ASE_* flags
1961    that were affected.  */
1962
1963 static unsigned int
1964 mips_set_ase (const struct mips_ase *ase, bfd_boolean enabled_p)
1965 {
1966   unsigned int mask;
1967
1968   mask = mips_ase_mask (ase->flags);
1969   mips_opts.ase &= ~mask;
1970   if (enabled_p)
1971     mips_opts.ase |= ase->flags;
1972   return mask;
1973 }
1974
1975 /* Return the ASE called NAME, or null if none.  */
1976
1977 static const struct mips_ase *
1978 mips_lookup_ase (const char *name)
1979 {
1980   unsigned int i;
1981
1982   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1983     if (strcmp (name, mips_ases[i].name) == 0)
1984       return &mips_ases[i];
1985   return NULL;
1986 }
1987
1988 /* Return the length of a microMIPS instruction in bytes.  If bits of
1989    the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1990    Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1991    major opcode) will require further modifications to the opcode
1992    table.  */
1993
1994 static inline unsigned int
1995 micromips_insn_length (const struct mips_opcode *mo)
1996 {
1997   return (mo->mask >> 16) == 0 ? 2 : 4;
1998 }
1999
2000 /* Return the length of MIPS16 instruction OPCODE.  */
2001
2002 static inline unsigned int
2003 mips16_opcode_length (unsigned long opcode)
2004 {
2005   return (opcode >> 16) == 0 ? 2 : 4;
2006 }
2007
2008 /* Return the length of instruction INSN.  */
2009
2010 static inline unsigned int
2011 insn_length (const struct mips_cl_insn *insn)
2012 {
2013   if (mips_opts.micromips)
2014     return micromips_insn_length (insn->insn_mo);
2015   else if (mips_opts.mips16)
2016     return mips16_opcode_length (insn->insn_opcode);
2017   else
2018     return 4;
2019 }
2020
2021 /* Initialise INSN from opcode entry MO.  Leave its position unspecified.  */
2022
2023 static void
2024 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2025 {
2026   size_t i;
2027
2028   insn->insn_mo = mo;
2029   insn->insn_opcode = mo->match;
2030   insn->frag = NULL;
2031   insn->where = 0;
2032   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2033     insn->fixp[i] = NULL;
2034   insn->fixed_p = (mips_opts.noreorder > 0);
2035   insn->noreorder_p = (mips_opts.noreorder > 0);
2036   insn->mips16_absolute_jump_p = 0;
2037   insn->complete_p = 0;
2038   insn->cleared_p = 0;
2039 }
2040
2041 /* Record the current MIPS16/microMIPS mode in now_seg.  */
2042
2043 static void
2044 mips_record_compressed_mode (void)
2045 {
2046   segment_info_type *si;
2047
2048   si = seg_info (now_seg);
2049   if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2050     si->tc_segment_info_data.mips16 = mips_opts.mips16;
2051   if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2052     si->tc_segment_info_data.micromips = mips_opts.micromips;
2053 }
2054
2055 /* Read a standard MIPS instruction from BUF.  */
2056
2057 static unsigned long
2058 read_insn (char *buf)
2059 {
2060   if (target_big_endian)
2061     return bfd_getb32 ((bfd_byte *) buf);
2062   else
2063     return bfd_getl32 ((bfd_byte *) buf);
2064 }
2065
2066 /* Write standard MIPS instruction INSN to BUF.  Return a pointer to
2067    the next byte.  */
2068
2069 static char *
2070 write_insn (char *buf, unsigned int insn)
2071 {
2072   md_number_to_chars (buf, insn, 4);
2073   return buf + 4;
2074 }
2075
2076 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2077    has length LENGTH.  */
2078
2079 static unsigned long
2080 read_compressed_insn (char *buf, unsigned int length)
2081 {
2082   unsigned long insn;
2083   unsigned int i;
2084
2085   insn = 0;
2086   for (i = 0; i < length; i += 2)
2087     {
2088       insn <<= 16;
2089       if (target_big_endian)
2090         insn |= bfd_getb16 ((char *) buf);
2091       else
2092         insn |= bfd_getl16 ((char *) buf);
2093       buf += 2;
2094     }
2095   return insn;
2096 }
2097
2098 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2099    instruction is LENGTH bytes long.  Return a pointer to the next byte.  */
2100
2101 static char *
2102 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2103 {
2104   unsigned int i;
2105
2106   for (i = 0; i < length; i += 2)
2107     md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2108   return buf + length;
2109 }
2110
2111 /* Install INSN at the location specified by its "frag" and "where" fields.  */
2112
2113 static void
2114 install_insn (const struct mips_cl_insn *insn)
2115 {
2116   char *f = insn->frag->fr_literal + insn->where;
2117   if (HAVE_CODE_COMPRESSION)
2118     write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2119   else
2120     write_insn (f, insn->insn_opcode);
2121   mips_record_compressed_mode ();
2122 }
2123
2124 /* Move INSN to offset WHERE in FRAG.  Adjust the fixups accordingly
2125    and install the opcode in the new location.  */
2126
2127 static void
2128 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2129 {
2130   size_t i;
2131
2132   insn->frag = frag;
2133   insn->where = where;
2134   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2135     if (insn->fixp[i] != NULL)
2136       {
2137         insn->fixp[i]->fx_frag = frag;
2138         insn->fixp[i]->fx_where = where;
2139       }
2140   install_insn (insn);
2141 }
2142
2143 /* Add INSN to the end of the output.  */
2144
2145 static void
2146 add_fixed_insn (struct mips_cl_insn *insn)
2147 {
2148   char *f = frag_more (insn_length (insn));
2149   move_insn (insn, frag_now, f - frag_now->fr_literal);
2150 }
2151
2152 /* Start a variant frag and move INSN to the start of the variant part,
2153    marking it as fixed.  The other arguments are as for frag_var.  */
2154
2155 static void
2156 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2157                   relax_substateT subtype, symbolS *symbol, offsetT offset)
2158 {
2159   frag_grow (max_chars);
2160   move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2161   insn->fixed_p = 1;
2162   frag_var (rs_machine_dependent, max_chars, var,
2163             subtype, symbol, offset, NULL);
2164 }
2165
2166 /* Insert N copies of INSN into the history buffer, starting at
2167    position FIRST.  Neither FIRST nor N need to be clipped.  */
2168
2169 static void
2170 insert_into_history (unsigned int first, unsigned int n,
2171                      const struct mips_cl_insn *insn)
2172 {
2173   if (mips_relax.sequence != 2)
2174     {
2175       unsigned int i;
2176
2177       for (i = ARRAY_SIZE (history); i-- > first;)
2178         if (i >= first + n)
2179           history[i] = history[i - n];
2180         else
2181           history[i] = *insn;
2182     }
2183 }
2184
2185 /* Initialize vr4120_conflicts.  There is a bit of duplication here:
2186    the idea is to make it obvious at a glance that each errata is
2187    included.  */
2188
2189 static void
2190 init_vr4120_conflicts (void)
2191 {
2192 #define CONFLICT(FIRST, SECOND) \
2193     vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2194
2195   /* Errata 21 - [D]DIV[U] after [D]MACC */
2196   CONFLICT (MACC, DIV);
2197   CONFLICT (DMACC, DIV);
2198
2199   /* Errata 23 - Continuous DMULT[U]/DMACC instructions.  */
2200   CONFLICT (DMULT, DMULT);
2201   CONFLICT (DMULT, DMACC);
2202   CONFLICT (DMACC, DMULT);
2203   CONFLICT (DMACC, DMACC);
2204
2205   /* Errata 24 - MT{LO,HI} after [D]MACC */
2206   CONFLICT (MACC, MTHILO);
2207   CONFLICT (DMACC, MTHILO);
2208
2209   /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2210      instruction is executed immediately after a MACC or DMACC
2211      instruction, the result of [either instruction] is incorrect."  */
2212   CONFLICT (MACC, MULT);
2213   CONFLICT (MACC, DMULT);
2214   CONFLICT (DMACC, MULT);
2215   CONFLICT (DMACC, DMULT);
2216
2217   /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2218      executed immediately after a DMULT, DMULTU, DIV, DIVU,
2219      DDIV or DDIVU instruction, the result of the MACC or
2220      DMACC instruction is incorrect.".  */
2221   CONFLICT (DMULT, MACC);
2222   CONFLICT (DMULT, DMACC);
2223   CONFLICT (DIV, MACC);
2224   CONFLICT (DIV, DMACC);
2225
2226 #undef CONFLICT
2227 }
2228
2229 struct regname {
2230   const char *name;
2231   unsigned int num;
2232 };
2233
2234 #define RTYPE_MASK      0x1ff00
2235 #define RTYPE_NUM       0x00100
2236 #define RTYPE_FPU       0x00200
2237 #define RTYPE_FCC       0x00400
2238 #define RTYPE_VEC       0x00800
2239 #define RTYPE_GP        0x01000
2240 #define RTYPE_CP0       0x02000
2241 #define RTYPE_PC        0x04000
2242 #define RTYPE_ACC       0x08000
2243 #define RTYPE_CCC       0x10000
2244 #define RNUM_MASK       0x000ff
2245 #define RWARN           0x80000
2246
2247 #define GENERIC_REGISTER_NUMBERS \
2248     {"$0",      RTYPE_NUM | 0},  \
2249     {"$1",      RTYPE_NUM | 1},  \
2250     {"$2",      RTYPE_NUM | 2},  \
2251     {"$3",      RTYPE_NUM | 3},  \
2252     {"$4",      RTYPE_NUM | 4},  \
2253     {"$5",      RTYPE_NUM | 5},  \
2254     {"$6",      RTYPE_NUM | 6},  \
2255     {"$7",      RTYPE_NUM | 7},  \
2256     {"$8",      RTYPE_NUM | 8},  \
2257     {"$9",      RTYPE_NUM | 9},  \
2258     {"$10",     RTYPE_NUM | 10}, \
2259     {"$11",     RTYPE_NUM | 11}, \
2260     {"$12",     RTYPE_NUM | 12}, \
2261     {"$13",     RTYPE_NUM | 13}, \
2262     {"$14",     RTYPE_NUM | 14}, \
2263     {"$15",     RTYPE_NUM | 15}, \
2264     {"$16",     RTYPE_NUM | 16}, \
2265     {"$17",     RTYPE_NUM | 17}, \
2266     {"$18",     RTYPE_NUM | 18}, \
2267     {"$19",     RTYPE_NUM | 19}, \
2268     {"$20",     RTYPE_NUM | 20}, \
2269     {"$21",     RTYPE_NUM | 21}, \
2270     {"$22",     RTYPE_NUM | 22}, \
2271     {"$23",     RTYPE_NUM | 23}, \
2272     {"$24",     RTYPE_NUM | 24}, \
2273     {"$25",     RTYPE_NUM | 25}, \
2274     {"$26",     RTYPE_NUM | 26}, \
2275     {"$27",     RTYPE_NUM | 27}, \
2276     {"$28",     RTYPE_NUM | 28}, \
2277     {"$29",     RTYPE_NUM | 29}, \
2278     {"$30",     RTYPE_NUM | 30}, \
2279     {"$31",     RTYPE_NUM | 31} 
2280
2281 #define FPU_REGISTER_NAMES       \
2282     {"$f0",     RTYPE_FPU | 0},  \
2283     {"$f1",     RTYPE_FPU | 1},  \
2284     {"$f2",     RTYPE_FPU | 2},  \
2285     {"$f3",     RTYPE_FPU | 3},  \
2286     {"$f4",     RTYPE_FPU | 4},  \
2287     {"$f5",     RTYPE_FPU | 5},  \
2288     {"$f6",     RTYPE_FPU | 6},  \
2289     {"$f7",     RTYPE_FPU | 7},  \
2290     {"$f8",     RTYPE_FPU | 8},  \
2291     {"$f9",     RTYPE_FPU | 9},  \
2292     {"$f10",    RTYPE_FPU | 10}, \
2293     {"$f11",    RTYPE_FPU | 11}, \
2294     {"$f12",    RTYPE_FPU | 12}, \
2295     {"$f13",    RTYPE_FPU | 13}, \
2296     {"$f14",    RTYPE_FPU | 14}, \
2297     {"$f15",    RTYPE_FPU | 15}, \
2298     {"$f16",    RTYPE_FPU | 16}, \
2299     {"$f17",    RTYPE_FPU | 17}, \
2300     {"$f18",    RTYPE_FPU | 18}, \
2301     {"$f19",    RTYPE_FPU | 19}, \
2302     {"$f20",    RTYPE_FPU | 20}, \
2303     {"$f21",    RTYPE_FPU | 21}, \
2304     {"$f22",    RTYPE_FPU | 22}, \
2305     {"$f23",    RTYPE_FPU | 23}, \
2306     {"$f24",    RTYPE_FPU | 24}, \
2307     {"$f25",    RTYPE_FPU | 25}, \
2308     {"$f26",    RTYPE_FPU | 26}, \
2309     {"$f27",    RTYPE_FPU | 27}, \
2310     {"$f28",    RTYPE_FPU | 28}, \
2311     {"$f29",    RTYPE_FPU | 29}, \
2312     {"$f30",    RTYPE_FPU | 30}, \
2313     {"$f31",    RTYPE_FPU | 31}
2314
2315 #define FPU_CONDITION_CODE_NAMES \
2316     {"$fcc0",   RTYPE_FCC | 0},  \
2317     {"$fcc1",   RTYPE_FCC | 1},  \
2318     {"$fcc2",   RTYPE_FCC | 2},  \
2319     {"$fcc3",   RTYPE_FCC | 3},  \
2320     {"$fcc4",   RTYPE_FCC | 4},  \
2321     {"$fcc5",   RTYPE_FCC | 5},  \
2322     {"$fcc6",   RTYPE_FCC | 6},  \
2323     {"$fcc7",   RTYPE_FCC | 7}
2324
2325 #define COPROC_CONDITION_CODE_NAMES         \
2326     {"$cc0",    RTYPE_FCC | RTYPE_CCC | 0}, \
2327     {"$cc1",    RTYPE_FCC | RTYPE_CCC | 1}, \
2328     {"$cc2",    RTYPE_FCC | RTYPE_CCC | 2}, \
2329     {"$cc3",    RTYPE_FCC | RTYPE_CCC | 3}, \
2330     {"$cc4",    RTYPE_FCC | RTYPE_CCC | 4}, \
2331     {"$cc5",    RTYPE_FCC | RTYPE_CCC | 5}, \
2332     {"$cc6",    RTYPE_FCC | RTYPE_CCC | 6}, \
2333     {"$cc7",    RTYPE_FCC | RTYPE_CCC | 7}
2334
2335 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2336     {"$a4",     RTYPE_GP | 8},  \
2337     {"$a5",     RTYPE_GP | 9},  \
2338     {"$a6",     RTYPE_GP | 10}, \
2339     {"$a7",     RTYPE_GP | 11}, \
2340     {"$ta0",    RTYPE_GP | 8},  /* alias for $a4 */ \
2341     {"$ta1",    RTYPE_GP | 9},  /* alias for $a5 */ \
2342     {"$ta2",    RTYPE_GP | 10}, /* alias for $a6 */ \
2343     {"$ta3",    RTYPE_GP | 11}, /* alias for $a7 */ \
2344     {"$t0",     RTYPE_GP | 12}, \
2345     {"$t1",     RTYPE_GP | 13}, \
2346     {"$t2",     RTYPE_GP | 14}, \
2347     {"$t3",     RTYPE_GP | 15}
2348
2349 #define O32_SYMBOLIC_REGISTER_NAMES \
2350     {"$t0",     RTYPE_GP | 8},  \
2351     {"$t1",     RTYPE_GP | 9},  \
2352     {"$t2",     RTYPE_GP | 10}, \
2353     {"$t3",     RTYPE_GP | 11}, \
2354     {"$t4",     RTYPE_GP | 12}, \
2355     {"$t5",     RTYPE_GP | 13}, \
2356     {"$t6",     RTYPE_GP | 14}, \
2357     {"$t7",     RTYPE_GP | 15}, \
2358     {"$ta0",    RTYPE_GP | 12}, /* alias for $t4 */ \
2359     {"$ta1",    RTYPE_GP | 13}, /* alias for $t5 */ \
2360     {"$ta2",    RTYPE_GP | 14}, /* alias for $t6 */ \
2361     {"$ta3",    RTYPE_GP | 15}  /* alias for $t7 */ 
2362
2363 /* Remaining symbolic register names */
2364 #define SYMBOLIC_REGISTER_NAMES \
2365     {"$zero",   RTYPE_GP | 0},  \
2366     {"$at",     RTYPE_GP | 1},  \
2367     {"$AT",     RTYPE_GP | 1},  \
2368     {"$v0",     RTYPE_GP | 2},  \
2369     {"$v1",     RTYPE_GP | 3},  \
2370     {"$a0",     RTYPE_GP | 4},  \
2371     {"$a1",     RTYPE_GP | 5},  \
2372     {"$a2",     RTYPE_GP | 6},  \
2373     {"$a3",     RTYPE_GP | 7},  \
2374     {"$s0",     RTYPE_GP | 16}, \
2375     {"$s1",     RTYPE_GP | 17}, \
2376     {"$s2",     RTYPE_GP | 18}, \
2377     {"$s3",     RTYPE_GP | 19}, \
2378     {"$s4",     RTYPE_GP | 20}, \
2379     {"$s5",     RTYPE_GP | 21}, \
2380     {"$s6",     RTYPE_GP | 22}, \
2381     {"$s7",     RTYPE_GP | 23}, \
2382     {"$t8",     RTYPE_GP | 24}, \
2383     {"$t9",     RTYPE_GP | 25}, \
2384     {"$k0",     RTYPE_GP | 26}, \
2385     {"$kt0",    RTYPE_GP | 26}, \
2386     {"$k1",     RTYPE_GP | 27}, \
2387     {"$kt1",    RTYPE_GP | 27}, \
2388     {"$gp",     RTYPE_GP | 28}, \
2389     {"$sp",     RTYPE_GP | 29}, \
2390     {"$s8",     RTYPE_GP | 30}, \
2391     {"$fp",     RTYPE_GP | 30}, \
2392     {"$ra",     RTYPE_GP | 31}
2393
2394 #define MIPS16_SPECIAL_REGISTER_NAMES \
2395     {"$pc",     RTYPE_PC | 0}
2396
2397 #define MDMX_VECTOR_REGISTER_NAMES \
2398     /* {"$v0",  RTYPE_VEC | 0},  clash with REG 2 above */ \
2399     /* {"$v1",  RTYPE_VEC | 1},  clash with REG 3 above */ \
2400     {"$v2",     RTYPE_VEC | 2},  \
2401     {"$v3",     RTYPE_VEC | 3},  \
2402     {"$v4",     RTYPE_VEC | 4},  \
2403     {"$v5",     RTYPE_VEC | 5},  \
2404     {"$v6",     RTYPE_VEC | 6},  \
2405     {"$v7",     RTYPE_VEC | 7},  \
2406     {"$v8",     RTYPE_VEC | 8},  \
2407     {"$v9",     RTYPE_VEC | 9},  \
2408     {"$v10",    RTYPE_VEC | 10}, \
2409     {"$v11",    RTYPE_VEC | 11}, \
2410     {"$v12",    RTYPE_VEC | 12}, \
2411     {"$v13",    RTYPE_VEC | 13}, \
2412     {"$v14",    RTYPE_VEC | 14}, \
2413     {"$v15",    RTYPE_VEC | 15}, \
2414     {"$v16",    RTYPE_VEC | 16}, \
2415     {"$v17",    RTYPE_VEC | 17}, \
2416     {"$v18",    RTYPE_VEC | 18}, \
2417     {"$v19",    RTYPE_VEC | 19}, \
2418     {"$v20",    RTYPE_VEC | 20}, \
2419     {"$v21",    RTYPE_VEC | 21}, \
2420     {"$v22",    RTYPE_VEC | 22}, \
2421     {"$v23",    RTYPE_VEC | 23}, \
2422     {"$v24",    RTYPE_VEC | 24}, \
2423     {"$v25",    RTYPE_VEC | 25}, \
2424     {"$v26",    RTYPE_VEC | 26}, \
2425     {"$v27",    RTYPE_VEC | 27}, \
2426     {"$v28",    RTYPE_VEC | 28}, \
2427     {"$v29",    RTYPE_VEC | 29}, \
2428     {"$v30",    RTYPE_VEC | 30}, \
2429     {"$v31",    RTYPE_VEC | 31}
2430
2431 #define MIPS_DSP_ACCUMULATOR_NAMES \
2432     {"$ac0",    RTYPE_ACC | 0}, \
2433     {"$ac1",    RTYPE_ACC | 1}, \
2434     {"$ac2",    RTYPE_ACC | 2}, \
2435     {"$ac3",    RTYPE_ACC | 3}
2436
2437 static const struct regname reg_names[] = {
2438   GENERIC_REGISTER_NUMBERS,
2439   FPU_REGISTER_NAMES,
2440   FPU_CONDITION_CODE_NAMES,
2441   COPROC_CONDITION_CODE_NAMES,
2442
2443   /* The $txx registers depends on the abi,
2444      these will be added later into the symbol table from
2445      one of the tables below once mips_abi is set after 
2446      parsing of arguments from the command line. */
2447   SYMBOLIC_REGISTER_NAMES,
2448
2449   MIPS16_SPECIAL_REGISTER_NAMES,
2450   MDMX_VECTOR_REGISTER_NAMES,
2451   MIPS_DSP_ACCUMULATOR_NAMES,
2452   {0, 0}
2453 };
2454
2455 static const struct regname reg_names_o32[] = {
2456   O32_SYMBOLIC_REGISTER_NAMES,
2457   {0, 0}
2458 };
2459
2460 static const struct regname reg_names_n32n64[] = {
2461   N32N64_SYMBOLIC_REGISTER_NAMES,
2462   {0, 0}
2463 };
2464
2465 /* Check if S points at a valid register specifier according to TYPES.
2466    If so, then return 1, advance S to consume the specifier and store
2467    the register's number in REGNOP, otherwise return 0.  */
2468
2469 static int
2470 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2471 {
2472   symbolS *symbolP;
2473   char *e;
2474   char save_c;
2475   int reg = -1;
2476
2477   /* Find end of name.  */
2478   e = *s;
2479   if (is_name_beginner (*e))
2480     ++e;
2481   while (is_part_of_name (*e))
2482     ++e;
2483
2484   /* Terminate name.  */
2485   save_c = *e;
2486   *e = '\0';
2487
2488   /* Look for a register symbol.  */
2489   if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2490     {
2491       int r = S_GET_VALUE (symbolP);
2492       if (r & types)
2493         reg = r & RNUM_MASK;
2494       else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2495         /* Convert GP reg $v0/1 to MDMX reg $v0/1!  */
2496         reg = (r & RNUM_MASK) - 2;
2497     }
2498   /* Else see if this is a register defined in an itbl entry.  */
2499   else if ((types & RTYPE_GP) && itbl_have_entries)
2500     {
2501       char *n = *s;
2502       unsigned long r;
2503
2504       if (*n == '$')
2505         ++n;
2506       if (itbl_get_reg_val (n, &r))
2507         reg = r & RNUM_MASK;
2508     }
2509
2510   /* Advance to next token if a register was recognised.  */
2511   if (reg >= 0)
2512     *s = e;
2513   else if (types & RWARN)
2514     as_warn (_("Unrecognized register name `%s'"), *s);
2515
2516   *e = save_c;
2517   if (regnop)
2518     *regnop = reg;
2519   return reg >= 0;
2520 }
2521
2522 /* Check if S points at a valid register list according to TYPES.
2523    If so, then return 1, advance S to consume the list and store
2524    the registers present on the list as a bitmask of ones in REGLISTP,
2525    otherwise return 0.  A valid list comprises a comma-separated
2526    enumeration of valid single registers and/or dash-separated
2527    contiguous register ranges as determined by their numbers.
2528
2529    As a special exception if one of s0-s7 registers is specified as
2530    the range's lower delimiter and s8 (fp) is its upper one, then no
2531    registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2532    are selected; they have to be listed separately if needed.  */
2533
2534 static int
2535 reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2536 {
2537   unsigned int reglist = 0;
2538   unsigned int lastregno;
2539   bfd_boolean ok = TRUE;
2540   unsigned int regmask;
2541   char *s_endlist = *s;
2542   char *s_reset = *s;
2543   unsigned int regno;
2544
2545   while (reg_lookup (s, types, &regno))
2546     {
2547       lastregno = regno;
2548       if (**s == '-')
2549         {
2550           (*s)++;
2551           ok = reg_lookup (s, types, &lastregno);
2552           if (ok && lastregno < regno)
2553             ok = FALSE;
2554           if (!ok)
2555             break;
2556         }
2557
2558       if (lastregno == FP && regno >= S0 && regno <= S7)
2559         {
2560           lastregno = S7;
2561           reglist |= 1 << FP;
2562         }
2563       regmask = 1 << lastregno;
2564       regmask = (regmask << 1) - 1;
2565       regmask ^= (1 << regno) - 1;
2566       reglist |= regmask;
2567
2568       s_endlist = *s;
2569       if (**s != ',')
2570         break;
2571       (*s)++;
2572     }
2573
2574   if (ok)
2575     *s = s_endlist;
2576   else
2577     *s = s_reset;
2578   if (reglistp)
2579     *reglistp = reglist;
2580   return ok && reglist != 0;
2581 }
2582
2583 static unsigned int
2584 mips_lookup_reg_pair (unsigned int regno1, unsigned int regno2,
2585                       const unsigned int *map1, const unsigned int *map2,
2586                       unsigned int count)
2587 {
2588   unsigned int i;
2589
2590   for (i = 0; i < count; i++)
2591     if (map1[i] == regno1 && map2[i] == regno2)
2592       return i;
2593   return ILLEGAL_REG;
2594 }
2595
2596 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
2597    and architecture.  Use is_opcode_valid_16 for MIPS16 opcodes.  */
2598
2599 static bfd_boolean
2600 is_opcode_valid (const struct mips_opcode *mo)
2601 {
2602   int isa = mips_opts.isa;
2603   int ase = mips_opts.ase;
2604   int fp_s, fp_d;
2605   unsigned int i;
2606
2607   if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2608     for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2609       if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
2610         ase |= mips_ases[i].flags64;
2611
2612   if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
2613     return FALSE;
2614
2615   /* Check whether the instruction or macro requires single-precision or
2616      double-precision floating-point support.  Note that this information is
2617      stored differently in the opcode table for insns and macros.  */
2618   if (mo->pinfo == INSN_MACRO)
2619     {
2620       fp_s = mo->pinfo2 & INSN2_M_FP_S;
2621       fp_d = mo->pinfo2 & INSN2_M_FP_D;
2622     }
2623   else
2624     {
2625       fp_s = mo->pinfo & FP_S;
2626       fp_d = mo->pinfo & FP_D;
2627     }
2628
2629   if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2630     return FALSE;
2631
2632   if (fp_s && mips_opts.soft_float)
2633     return FALSE;
2634
2635   return TRUE;
2636 }
2637
2638 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
2639    selected ISA and architecture.  */
2640
2641 static bfd_boolean
2642 is_opcode_valid_16 (const struct mips_opcode *mo)
2643 {
2644   return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
2645 }
2646
2647 /* Return TRUE if the size of the microMIPS opcode MO matches one
2648    explicitly requested.  Always TRUE in the standard MIPS mode.  */
2649
2650 static bfd_boolean
2651 is_size_valid (const struct mips_opcode *mo)
2652 {
2653   if (!mips_opts.micromips)
2654     return TRUE;
2655
2656   if (mips_opts.insn32)
2657     {
2658       if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
2659         return FALSE;
2660       if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
2661         return FALSE;
2662     }
2663   if (!forced_insn_length)
2664     return TRUE;
2665   if (mo->pinfo == INSN_MACRO)
2666     return FALSE;
2667   return forced_insn_length == micromips_insn_length (mo);
2668 }
2669
2670 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
2671    of the preceding instruction.  Always TRUE in the standard MIPS mode.
2672
2673    We don't accept macros in 16-bit delay slots to avoid a case where
2674    a macro expansion fails because it relies on a preceding 32-bit real
2675    instruction to have matched and does not handle the operands correctly.
2676    The only macros that may expand to 16-bit instructions are JAL that
2677    cannot be placed in a delay slot anyway, and corner cases of BALIGN
2678    and BGT (that likewise cannot be placed in a delay slot) that decay to
2679    a NOP.  In all these cases the macros precede any corresponding real
2680    instruction definitions in the opcode table, so they will match in the
2681    second pass where the size of the delay slot is ignored and therefore
2682    produce correct code.  */
2683
2684 static bfd_boolean
2685 is_delay_slot_valid (const struct mips_opcode *mo)
2686 {
2687   if (!mips_opts.micromips)
2688     return TRUE;
2689
2690   if (mo->pinfo == INSN_MACRO)
2691     return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
2692   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2693       && micromips_insn_length (mo) != 4)
2694     return FALSE;
2695   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2696       && micromips_insn_length (mo) != 2)
2697     return FALSE;
2698
2699   return TRUE;
2700 }
2701
2702 /* This function is called once, at assembler startup time.  It should set up
2703    all the tables, etc. that the MD part of the assembler will need.  */
2704
2705 void
2706 md_begin (void)
2707 {
2708   const char *retval = NULL;
2709   int i = 0;
2710   int broken = 0;
2711
2712   if (mips_pic != NO_PIC)
2713     {
2714       if (g_switch_seen && g_switch_value != 0)
2715         as_bad (_("-G may not be used in position-independent code"));
2716       g_switch_value = 0;
2717     }
2718
2719   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
2720     as_warn (_("Could not set architecture and machine"));
2721
2722   op_hash = hash_new ();
2723
2724   for (i = 0; i < NUMOPCODES;)
2725     {
2726       const char *name = mips_opcodes[i].name;
2727
2728       retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
2729       if (retval != NULL)
2730         {
2731           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2732                    mips_opcodes[i].name, retval);
2733           /* Probably a memory allocation problem?  Give up now.  */
2734           as_fatal (_("Broken assembler.  No assembly attempted."));
2735         }
2736       do
2737         {
2738           if (mips_opcodes[i].pinfo != INSN_MACRO)
2739             {
2740               if (!validate_mips_insn (&mips_opcodes[i]))
2741                 broken = 1;
2742               if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2743                 {
2744                   create_insn (&nop_insn, mips_opcodes + i);
2745                   if (mips_fix_loongson2f_nop)
2746                     nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
2747                   nop_insn.fixed_p = 1;
2748                 }
2749             }
2750           ++i;
2751         }
2752       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2753     }
2754
2755   mips16_op_hash = hash_new ();
2756
2757   i = 0;
2758   while (i < bfd_mips16_num_opcodes)
2759     {
2760       const char *name = mips16_opcodes[i].name;
2761
2762       retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
2763       if (retval != NULL)
2764         as_fatal (_("internal: can't hash `%s': %s"),
2765                   mips16_opcodes[i].name, retval);
2766       do
2767         {
2768           if (mips16_opcodes[i].pinfo != INSN_MACRO
2769               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2770                   != mips16_opcodes[i].match))
2771             {
2772               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2773                        mips16_opcodes[i].name, mips16_opcodes[i].args);
2774               broken = 1;
2775             }
2776           if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2777             {
2778               create_insn (&mips16_nop_insn, mips16_opcodes + i);
2779               mips16_nop_insn.fixed_p = 1;
2780             }
2781           ++i;
2782         }
2783       while (i < bfd_mips16_num_opcodes
2784              && strcmp (mips16_opcodes[i].name, name) == 0);
2785     }
2786
2787   micromips_op_hash = hash_new ();
2788
2789   i = 0;
2790   while (i < bfd_micromips_num_opcodes)
2791     {
2792       const char *name = micromips_opcodes[i].name;
2793
2794       retval = hash_insert (micromips_op_hash, name,
2795                             (void *) &micromips_opcodes[i]);
2796       if (retval != NULL)
2797         as_fatal (_("internal: can't hash `%s': %s"),
2798                   micromips_opcodes[i].name, retval);
2799       do
2800         if (micromips_opcodes[i].pinfo != INSN_MACRO)
2801           {
2802             struct mips_cl_insn *micromips_nop_insn;
2803
2804             if (!validate_micromips_insn (&micromips_opcodes[i]))
2805               broken = 1;
2806
2807             if (micromips_insn_length (micromips_opcodes + i) == 2)
2808               micromips_nop_insn = &micromips_nop16_insn;
2809             else if (micromips_insn_length (micromips_opcodes + i) == 4)
2810               micromips_nop_insn = &micromips_nop32_insn;
2811             else
2812               continue;
2813
2814             if (micromips_nop_insn->insn_mo == NULL
2815                 && strcmp (name, "nop") == 0)
2816               {
2817                 create_insn (micromips_nop_insn, micromips_opcodes + i);
2818                 micromips_nop_insn->fixed_p = 1;
2819               }
2820           }
2821       while (++i < bfd_micromips_num_opcodes
2822              && strcmp (micromips_opcodes[i].name, name) == 0);
2823     }
2824
2825   if (broken)
2826     as_fatal (_("Broken assembler.  No assembly attempted."));
2827
2828   /* We add all the general register names to the symbol table.  This
2829      helps us detect invalid uses of them.  */
2830   for (i = 0; reg_names[i].name; i++) 
2831     symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
2832                                      reg_names[i].num, /* & RNUM_MASK, */
2833                                      &zero_address_frag));
2834   if (HAVE_NEWABI)
2835     for (i = 0; reg_names_n32n64[i].name; i++) 
2836       symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
2837                                        reg_names_n32n64[i].num, /* & RNUM_MASK, */
2838                                        &zero_address_frag));
2839   else
2840     for (i = 0; reg_names_o32[i].name; i++) 
2841       symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
2842                                        reg_names_o32[i].num, /* & RNUM_MASK, */
2843                                        &zero_address_frag));
2844
2845   mips_no_prev_insn ();
2846
2847   mips_gprmask = 0;
2848   mips_cprmask[0] = 0;
2849   mips_cprmask[1] = 0;
2850   mips_cprmask[2] = 0;
2851   mips_cprmask[3] = 0;
2852
2853   /* set the default alignment for the text section (2**2) */
2854   record_alignment (text_section, 2);
2855
2856   bfd_set_gp_size (stdoutput, g_switch_value);
2857
2858   /* On a native system other than VxWorks, sections must be aligned
2859      to 16 byte boundaries.  When configured for an embedded ELF
2860      target, we don't bother.  */
2861   if (strncmp (TARGET_OS, "elf", 3) != 0
2862       && strncmp (TARGET_OS, "vxworks", 7) != 0)
2863     {
2864       (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2865       (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2866       (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2867     }
2868
2869   /* Create a .reginfo section for register masks and a .mdebug
2870      section for debugging information.  */
2871   {
2872     segT seg;
2873     subsegT subseg;
2874     flagword flags;
2875     segT sec;
2876
2877     seg = now_seg;
2878     subseg = now_subseg;
2879
2880     /* The ABI says this section should be loaded so that the
2881        running program can access it.  However, we don't load it
2882        if we are configured for an embedded target */
2883     flags = SEC_READONLY | SEC_DATA;
2884     if (strncmp (TARGET_OS, "elf", 3) != 0)
2885       flags |= SEC_ALLOC | SEC_LOAD;
2886
2887     if (mips_abi != N64_ABI)
2888       {
2889         sec = subseg_new (".reginfo", (subsegT) 0);
2890
2891         bfd_set_section_flags (stdoutput, sec, flags);
2892         bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
2893
2894         mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
2895       }
2896     else
2897       {
2898         /* The 64-bit ABI uses a .MIPS.options section rather than
2899            .reginfo section.  */
2900         sec = subseg_new (".MIPS.options", (subsegT) 0);
2901         bfd_set_section_flags (stdoutput, sec, flags);
2902         bfd_set_section_alignment (stdoutput, sec, 3);
2903
2904         /* Set up the option header.  */
2905         {
2906           Elf_Internal_Options opthdr;
2907           char *f;
2908
2909           opthdr.kind = ODK_REGINFO;
2910           opthdr.size = (sizeof (Elf_External_Options)
2911                          + sizeof (Elf64_External_RegInfo));
2912           opthdr.section = 0;
2913           opthdr.info = 0;
2914           f = frag_more (sizeof (Elf_External_Options));
2915           bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2916                                          (Elf_External_Options *) f);
2917
2918           mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2919         }
2920       }
2921
2922     if (ECOFF_DEBUGGING)
2923       {
2924         sec = subseg_new (".mdebug", (subsegT) 0);
2925         (void) bfd_set_section_flags (stdoutput, sec,
2926                                       SEC_HAS_CONTENTS | SEC_READONLY);
2927         (void) bfd_set_section_alignment (stdoutput, sec, 2);
2928       }
2929     else if (mips_flag_pdr)
2930       {
2931         pdr_seg = subseg_new (".pdr", (subsegT) 0);
2932         (void) bfd_set_section_flags (stdoutput, pdr_seg,
2933                                       SEC_READONLY | SEC_RELOC
2934                                       | SEC_DEBUGGING);
2935         (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2936       }
2937
2938     subseg_set (seg, subseg);
2939   }
2940
2941   if (! ECOFF_DEBUGGING)
2942     md_obj_begin ();
2943
2944   if (mips_fix_vr4120)
2945     init_vr4120_conflicts ();
2946 }
2947
2948 void
2949 md_mips_end (void)
2950 {
2951   mips_emit_delays ();
2952   if (! ECOFF_DEBUGGING)
2953     md_obj_end ();
2954 }
2955
2956 void
2957 md_assemble (char *str)
2958 {
2959   struct mips_cl_insn insn;
2960   bfd_reloc_code_real_type unused_reloc[3]
2961     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2962
2963   imm_expr.X_op = O_absent;
2964   imm2_expr.X_op = O_absent;
2965   offset_expr.X_op = O_absent;
2966   imm_reloc[0] = BFD_RELOC_UNUSED;
2967   imm_reloc[1] = BFD_RELOC_UNUSED;
2968   imm_reloc[2] = BFD_RELOC_UNUSED;
2969   offset_reloc[0] = BFD_RELOC_UNUSED;
2970   offset_reloc[1] = BFD_RELOC_UNUSED;
2971   offset_reloc[2] = BFD_RELOC_UNUSED;
2972
2973   mips_mark_labels ();
2974   mips_assembling_insn = TRUE;
2975
2976   if (mips_opts.mips16)
2977     mips16_ip (str, &insn);
2978   else
2979     {
2980       mips_ip (str, &insn);
2981       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2982             str, insn.insn_opcode));
2983     }
2984
2985   if (insn_error)
2986     as_bad ("%s `%s'", insn_error, str);
2987   else if (insn.insn_mo->pinfo == INSN_MACRO)
2988     {
2989       macro_start ();
2990       if (mips_opts.mips16)
2991         mips16_macro (&insn);
2992       else
2993         macro (&insn, str);
2994       macro_end ();
2995     }
2996   else
2997     {
2998       if (imm_expr.X_op != O_absent)
2999         append_insn (&insn, &imm_expr, imm_reloc, FALSE);
3000       else if (offset_expr.X_op != O_absent)
3001         append_insn (&insn, &offset_expr, offset_reloc, FALSE);
3002       else
3003         append_insn (&insn, NULL, unused_reloc, FALSE);
3004     }
3005
3006   mips_assembling_insn = FALSE;
3007 }
3008
3009 /* Convenience functions for abstracting away the differences between
3010    MIPS16 and non-MIPS16 relocations.  */
3011
3012 static inline bfd_boolean
3013 mips16_reloc_p (bfd_reloc_code_real_type reloc)
3014 {
3015   switch (reloc)
3016     {
3017     case BFD_RELOC_MIPS16_JMP:
3018     case BFD_RELOC_MIPS16_GPREL:
3019     case BFD_RELOC_MIPS16_GOT16:
3020     case BFD_RELOC_MIPS16_CALL16:
3021     case BFD_RELOC_MIPS16_HI16_S:
3022     case BFD_RELOC_MIPS16_HI16:
3023     case BFD_RELOC_MIPS16_LO16:
3024       return TRUE;
3025
3026     default:
3027       return FALSE;
3028     }
3029 }
3030
3031 static inline bfd_boolean
3032 micromips_reloc_p (bfd_reloc_code_real_type reloc)
3033 {
3034   switch (reloc)
3035     {
3036     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3037     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3038     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3039     case BFD_RELOC_MICROMIPS_GPREL16:
3040     case BFD_RELOC_MICROMIPS_JMP:
3041     case BFD_RELOC_MICROMIPS_HI16:
3042     case BFD_RELOC_MICROMIPS_HI16_S:
3043     case BFD_RELOC_MICROMIPS_LO16:
3044     case BFD_RELOC_MICROMIPS_LITERAL:
3045     case BFD_RELOC_MICROMIPS_GOT16:
3046     case BFD_RELOC_MICROMIPS_CALL16:
3047     case BFD_RELOC_MICROMIPS_GOT_HI16:
3048     case BFD_RELOC_MICROMIPS_GOT_LO16:
3049     case BFD_RELOC_MICROMIPS_CALL_HI16:
3050     case BFD_RELOC_MICROMIPS_CALL_LO16:
3051     case BFD_RELOC_MICROMIPS_SUB:
3052     case BFD_RELOC_MICROMIPS_GOT_PAGE:
3053     case BFD_RELOC_MICROMIPS_GOT_OFST:
3054     case BFD_RELOC_MICROMIPS_GOT_DISP:
3055     case BFD_RELOC_MICROMIPS_HIGHEST:
3056     case BFD_RELOC_MICROMIPS_HIGHER:
3057     case BFD_RELOC_MICROMIPS_SCN_DISP:
3058     case BFD_RELOC_MICROMIPS_JALR:
3059       return TRUE;
3060
3061     default:
3062       return FALSE;
3063     }
3064 }
3065
3066 static inline bfd_boolean
3067 jmp_reloc_p (bfd_reloc_code_real_type reloc)
3068 {
3069   return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
3070 }
3071
3072 static inline bfd_boolean
3073 got16_reloc_p (bfd_reloc_code_real_type reloc)
3074 {
3075   return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
3076           || reloc == BFD_RELOC_MICROMIPS_GOT16);
3077 }
3078
3079 static inline bfd_boolean
3080 hi16_reloc_p (bfd_reloc_code_real_type reloc)
3081 {
3082   return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
3083           || reloc == BFD_RELOC_MICROMIPS_HI16_S);
3084 }
3085
3086 static inline bfd_boolean
3087 lo16_reloc_p (bfd_reloc_code_real_type reloc)
3088 {
3089   return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
3090           || reloc == BFD_RELOC_MICROMIPS_LO16);
3091 }
3092
3093 static inline bfd_boolean
3094 jalr_reloc_p (bfd_reloc_code_real_type reloc)
3095 {
3096   return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
3097 }
3098
3099 static inline bfd_boolean
3100 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
3101 {
3102   return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
3103           || reloc == BFD_RELOC_MICROMIPS_GPREL16);
3104 }
3105
3106 /* Return true if RELOC is a PC-relative relocation that does not have
3107    full address range.  */
3108
3109 static inline bfd_boolean
3110 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
3111 {
3112   switch (reloc)
3113     {
3114     case BFD_RELOC_16_PCREL_S2:
3115     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3116     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3117     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3118       return TRUE;
3119
3120     case BFD_RELOC_32_PCREL:
3121       return HAVE_64BIT_ADDRESSES;
3122
3123     default:
3124       return FALSE;
3125     }
3126 }
3127
3128 /* Return true if the given relocation might need a matching %lo().
3129    This is only "might" because SVR4 R_MIPS_GOT16 relocations only
3130    need a matching %lo() when applied to local symbols.  */
3131
3132 static inline bfd_boolean
3133 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
3134 {
3135   return (HAVE_IN_PLACE_ADDENDS
3136           && (hi16_reloc_p (reloc)
3137               /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
3138                  all GOT16 relocations evaluate to "G".  */
3139               || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
3140 }
3141
3142 /* Return the type of %lo() reloc needed by RELOC, given that
3143    reloc_needs_lo_p.  */
3144
3145 static inline bfd_reloc_code_real_type
3146 matching_lo_reloc (bfd_reloc_code_real_type reloc)
3147 {
3148   return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
3149           : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
3150              : BFD_RELOC_LO16));
3151 }
3152
3153 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
3154    relocation.  */
3155
3156 static inline bfd_boolean
3157 fixup_has_matching_lo_p (fixS *fixp)
3158 {
3159   return (fixp->fx_next != NULL
3160           && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
3161           && fixp->fx_addsy == fixp->fx_next->fx_addsy
3162           && fixp->fx_offset == fixp->fx_next->fx_offset);
3163 }
3164
3165 /* This function returns true if modifying a register requires a
3166    delay.  */
3167
3168 static int
3169 reg_needs_delay (unsigned int reg)
3170 {
3171   unsigned long prev_pinfo;
3172
3173   prev_pinfo = history[0].insn_mo->pinfo;
3174   if (! mips_opts.noreorder
3175       && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
3176            && ! gpr_interlocks)
3177           || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
3178               && ! cop_interlocks)))
3179     {
3180       /* A load from a coprocessor or from memory.  All load delays
3181          delay the use of general register rt for one instruction.  */
3182       /* Itbl support may require additional care here.  */
3183       know (prev_pinfo & INSN_WRITE_GPR_T);
3184       if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
3185         return 1;
3186     }
3187
3188   return 0;
3189 }
3190
3191 /* Move all labels in LABELS to the current insertion point.  TEXT_P
3192    says whether the labels refer to text or data.  */
3193
3194 static void
3195 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
3196 {
3197   struct insn_label_list *l;
3198   valueT val;
3199
3200   for (l = labels; l != NULL; l = l->next)
3201     {
3202       gas_assert (S_GET_SEGMENT (l->label) == now_seg);
3203       symbol_set_frag (l->label, frag_now);
3204       val = (valueT) frag_now_fix ();
3205       /* MIPS16/microMIPS text labels are stored as odd.  */
3206       if (text_p && HAVE_CODE_COMPRESSION)
3207         ++val;
3208       S_SET_VALUE (l->label, val);
3209     }
3210 }
3211
3212 /* Move all labels in insn_labels to the current insertion point
3213    and treat them as text labels.  */
3214
3215 static void
3216 mips_move_text_labels (void)
3217 {
3218   mips_move_labels (seg_info (now_seg)->label_list, TRUE);
3219 }
3220
3221 static bfd_boolean
3222 s_is_linkonce (symbolS *sym, segT from_seg)
3223 {
3224   bfd_boolean linkonce = FALSE;
3225   segT symseg = S_GET_SEGMENT (sym);
3226
3227   if (symseg != from_seg && !S_IS_LOCAL (sym))
3228     {
3229       if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
3230         linkonce = TRUE;
3231       /* The GNU toolchain uses an extension for ELF: a section
3232          beginning with the magic string .gnu.linkonce is a
3233          linkonce section.  */
3234       if (strncmp (segment_name (symseg), ".gnu.linkonce",
3235                    sizeof ".gnu.linkonce" - 1) == 0)
3236         linkonce = TRUE;
3237     }
3238   return linkonce;
3239 }
3240
3241 /* Mark MIPS16 or microMIPS instruction label LABEL.  This permits the
3242    linker to handle them specially, such as generating jalx instructions
3243    when needed.  We also make them odd for the duration of the assembly,
3244    in order to generate the right sort of code.  We will make them even
3245    in the adjust_symtab routine, while leaving them marked.  This is
3246    convenient for the debugger and the disassembler.  The linker knows
3247    to make them odd again.  */
3248
3249 static void
3250 mips_compressed_mark_label (symbolS *label)
3251 {
3252   gas_assert (HAVE_CODE_COMPRESSION);
3253
3254   if (mips_opts.mips16)
3255     S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
3256   else
3257     S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
3258   if ((S_GET_VALUE (label) & 1) == 0
3259       /* Don't adjust the address if the label is global or weak, or
3260          in a link-once section, since we'll be emitting symbol reloc
3261          references to it which will be patched up by the linker, and
3262          the final value of the symbol may or may not be MIPS16/microMIPS.  */
3263       && !S_IS_WEAK (label)
3264       && !S_IS_EXTERNAL (label)
3265       && !s_is_linkonce (label, now_seg))
3266     S_SET_VALUE (label, S_GET_VALUE (label) | 1);
3267 }
3268
3269 /* Mark preceding MIPS16 or microMIPS instruction labels.  */
3270
3271 static void
3272 mips_compressed_mark_labels (void)
3273 {
3274   struct insn_label_list *l;
3275
3276   for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
3277     mips_compressed_mark_label (l->label);
3278 }
3279
3280 /* End the current frag.  Make it a variant frag and record the
3281    relaxation info.  */
3282
3283 static void
3284 relax_close_frag (void)
3285 {
3286   mips_macro_warning.first_frag = frag_now;
3287   frag_var (rs_machine_dependent, 0, 0,
3288             RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
3289             mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
3290
3291   memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
3292   mips_relax.first_fixup = 0;
3293 }
3294
3295 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
3296    See the comment above RELAX_ENCODE for more details.  */
3297
3298 static void
3299 relax_start (symbolS *symbol)
3300 {
3301   gas_assert (mips_relax.sequence == 0);
3302   mips_relax.sequence = 1;
3303   mips_relax.symbol = symbol;
3304 }
3305
3306 /* Start generating the second version of a relaxable sequence.
3307    See the comment above RELAX_ENCODE for more details.  */
3308
3309 static void
3310 relax_switch (void)
3311 {
3312   gas_assert (mips_relax.sequence == 1);
3313   mips_relax.sequence = 2;
3314 }
3315
3316 /* End the current relaxable sequence.  */
3317
3318 static void
3319 relax_end (void)
3320 {
3321   gas_assert (mips_relax.sequence == 2);
3322   relax_close_frag ();
3323   mips_relax.sequence = 0;
3324 }
3325
3326 /* Return true if IP is a delayed branch or jump.  */
3327
3328 static inline bfd_boolean
3329 delayed_branch_p (const struct mips_cl_insn *ip)
3330 {
3331   return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
3332                                 | INSN_COND_BRANCH_DELAY
3333                                 | INSN_COND_BRANCH_LIKELY)) != 0;
3334 }
3335
3336 /* Return true if IP is a compact branch or jump.  */
3337
3338 static inline bfd_boolean
3339 compact_branch_p (const struct mips_cl_insn *ip)
3340 {
3341   if (mips_opts.mips16)
3342     return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
3343                                   | MIPS16_INSN_COND_BRANCH)) != 0;
3344   else
3345     return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
3346                                    | INSN2_COND_BRANCH)) != 0;
3347 }
3348
3349 /* Return true if IP is an unconditional branch or jump.  */
3350
3351 static inline bfd_boolean
3352 uncond_branch_p (const struct mips_cl_insn *ip)
3353 {
3354   return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
3355           || (mips_opts.mips16
3356               ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
3357               : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
3358 }
3359
3360 /* Return true if IP is a branch-likely instruction.  */
3361
3362 static inline bfd_boolean
3363 branch_likely_p (const struct mips_cl_insn *ip)
3364 {
3365   return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
3366 }
3367
3368 /* Return the type of nop that should be used to fill the delay slot
3369    of delayed branch IP.  */
3370
3371 static struct mips_cl_insn *
3372 get_delay_slot_nop (const struct mips_cl_insn *ip)
3373 {
3374   if (mips_opts.micromips
3375       && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3376     return &micromips_nop32_insn;
3377   return NOP_INSN;
3378 }
3379
3380 /* Return the mask of core registers that IP reads or writes.  */
3381
3382 static unsigned int
3383 gpr_mod_mask (const struct mips_cl_insn *ip)
3384 {
3385   unsigned long pinfo2;
3386   unsigned int mask;
3387
3388   mask = 0;
3389   pinfo2 = ip->insn_mo->pinfo2;
3390   if (mips_opts.micromips)
3391     {
3392       if (pinfo2 & INSN2_MOD_GPR_MD)
3393         mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
3394       if (pinfo2 & INSN2_MOD_GPR_MF)
3395         mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
3396       if (pinfo2 & INSN2_MOD_SP)
3397         mask |= 1 << SP;
3398     }
3399   return mask;
3400 }
3401
3402 /* Return the mask of core registers that IP reads.  */
3403
3404 static unsigned int
3405 gpr_read_mask (const struct mips_cl_insn *ip)
3406 {
3407   unsigned long pinfo, pinfo2;
3408   unsigned int mask;
3409
3410   mask = gpr_mod_mask (ip);
3411   pinfo = ip->insn_mo->pinfo;
3412   pinfo2 = ip->insn_mo->pinfo2;
3413   if (mips_opts.mips16)
3414     {
3415       if (pinfo & MIPS16_INSN_READ_X)
3416         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3417       if (pinfo & MIPS16_INSN_READ_Y)
3418         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3419       if (pinfo & MIPS16_INSN_READ_T)
3420         mask |= 1 << TREG;
3421       if (pinfo & MIPS16_INSN_READ_SP)
3422         mask |= 1 << SP;
3423       if (pinfo & MIPS16_INSN_READ_31)
3424         mask |= 1 << RA;
3425       if (pinfo & MIPS16_INSN_READ_Z)
3426         mask |= 1 << (mips16_to_32_reg_map
3427                       [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3428       if (pinfo & MIPS16_INSN_READ_GPR_X)
3429         mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3430     }
3431   else
3432     {
3433       if (pinfo2 & INSN2_READ_GPR_D)
3434         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3435       if (pinfo & INSN_READ_GPR_T)
3436         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3437       if (pinfo & INSN_READ_GPR_S)
3438         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3439       if (pinfo2 & INSN2_READ_GP)
3440         mask |= 1 << GP;
3441       if (pinfo2 & INSN2_READ_GPR_31)
3442         mask |= 1 << RA;
3443       if (pinfo2 & INSN2_READ_GPR_Z)
3444         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3445     }
3446   if (mips_opts.micromips)
3447     {
3448       if (pinfo2 & INSN2_READ_GPR_MC)
3449         mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3450       if (pinfo2 & INSN2_READ_GPR_ME)
3451         mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3452       if (pinfo2 & INSN2_READ_GPR_MG)
3453         mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3454       if (pinfo2 & INSN2_READ_GPR_MJ)
3455         mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3456       if (pinfo2 & INSN2_READ_GPR_MMN)
3457         {
3458           mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3459           mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3460         }
3461       if (pinfo2 & INSN2_READ_GPR_MP)
3462         mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3463       if (pinfo2 & INSN2_READ_GPR_MQ)
3464         mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3465     }
3466   /* Don't include register 0.  */
3467   return mask & ~1;
3468 }
3469
3470 /* Return the mask of core registers that IP writes.  */
3471
3472 static unsigned int
3473 gpr_write_mask (const struct mips_cl_insn *ip)
3474 {
3475   unsigned long pinfo, pinfo2;
3476   unsigned int mask;
3477
3478   mask = gpr_mod_mask (ip);
3479   pinfo = ip->insn_mo->pinfo;
3480   pinfo2 = ip->insn_mo->pinfo2;
3481   if (mips_opts.mips16)
3482     {
3483       if (pinfo & MIPS16_INSN_WRITE_X)
3484         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3485       if (pinfo & MIPS16_INSN_WRITE_Y)
3486         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3487       if (pinfo & MIPS16_INSN_WRITE_Z)
3488         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3489       if (pinfo & MIPS16_INSN_WRITE_T)
3490         mask |= 1 << TREG;
3491       if (pinfo & MIPS16_INSN_WRITE_SP)
3492         mask |= 1 << SP;
3493       if (pinfo & MIPS16_INSN_WRITE_31)
3494         mask |= 1 << RA;
3495       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3496         mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3497     }
3498   else
3499     {
3500       if (pinfo & INSN_WRITE_GPR_D)
3501         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3502       if (pinfo & INSN_WRITE_GPR_T)
3503         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3504       if (pinfo & INSN_WRITE_GPR_S)
3505         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3506       if (pinfo & INSN_WRITE_GPR_31)
3507         mask |= 1 << RA;
3508       if (pinfo2 & INSN2_WRITE_GPR_Z)
3509         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3510     }
3511   if (mips_opts.micromips)
3512     {
3513       if (pinfo2 & INSN2_WRITE_GPR_MB)
3514         mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3515       if (pinfo2 & INSN2_WRITE_GPR_MH)
3516         {
3517           mask |= 1 << micromips_to_32_reg_h_map1[EXTRACT_OPERAND (1, MH, *ip)];
3518           mask |= 1 << micromips_to_32_reg_h_map2[EXTRACT_OPERAND (1, MH, *ip)];
3519         }
3520       if (pinfo2 & INSN2_WRITE_GPR_MJ)
3521         mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3522       if (pinfo2 & INSN2_WRITE_GPR_MP)
3523         mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3524     }
3525   /* Don't include register 0.  */
3526   return mask & ~1;
3527 }
3528
3529 /* Return the mask of floating-point registers that IP reads.  */
3530
3531 static unsigned int
3532 fpr_read_mask (const struct mips_cl_insn *ip)
3533 {
3534   unsigned long pinfo, pinfo2;
3535   unsigned int mask;
3536
3537   mask = 0;
3538   pinfo = ip->insn_mo->pinfo;
3539   pinfo2 = ip->insn_mo->pinfo2;
3540   if (!mips_opts.mips16)
3541     {
3542       if (pinfo2 & INSN2_READ_FPR_D)
3543         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3544       if (pinfo & INSN_READ_FPR_S)
3545         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3546       if (pinfo & INSN_READ_FPR_T)
3547         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3548       if (pinfo & INSN_READ_FPR_R)
3549         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
3550       if (pinfo2 & INSN2_READ_FPR_Z)
3551         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3552     }
3553   /* Conservatively treat all operands to an FP_D instruction are doubles.
3554      (This is overly pessimistic for things like cvt.d.s.)  */
3555   if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3556     mask |= mask << 1;
3557   return mask;
3558 }
3559
3560 /* Return the mask of floating-point registers that IP writes.  */
3561
3562 static unsigned int
3563 fpr_write_mask (const struct mips_cl_insn *ip)
3564 {
3565   unsigned long pinfo, pinfo2;
3566   unsigned int mask;
3567
3568   mask = 0;
3569   pinfo = ip->insn_mo->pinfo;
3570   pinfo2 = ip->insn_mo->pinfo2;
3571   if (!mips_opts.mips16)
3572     {
3573       if (pinfo & INSN_WRITE_FPR_D)
3574         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3575       if (pinfo & INSN_WRITE_FPR_S)
3576         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3577       if (pinfo & INSN_WRITE_FPR_T)
3578         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3579       if (pinfo2 & INSN2_WRITE_FPR_Z)
3580         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3581     }
3582   /* Conservatively treat all operands to an FP_D instruction are doubles.
3583      (This is overly pessimistic for things like cvt.s.d.)  */
3584   if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3585     mask |= mask << 1;
3586   return mask;
3587 }
3588
3589 /* Classify an instruction according to the FIX_VR4120_* enumeration.
3590    Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3591    by VR4120 errata.  */
3592
3593 static unsigned int
3594 classify_vr4120_insn (const char *name)
3595 {
3596   if (strncmp (name, "macc", 4) == 0)
3597     return FIX_VR4120_MACC;
3598   if (strncmp (name, "dmacc", 5) == 0)
3599     return FIX_VR4120_DMACC;
3600   if (strncmp (name, "mult", 4) == 0)
3601     return FIX_VR4120_MULT;
3602   if (strncmp (name, "dmult", 5) == 0)
3603     return FIX_VR4120_DMULT;
3604   if (strstr (name, "div"))
3605     return FIX_VR4120_DIV;
3606   if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3607     return FIX_VR4120_MTHILO;
3608   return NUM_FIX_VR4120_CLASSES;
3609 }
3610
3611 #define INSN_ERET  0x42000018
3612 #define INSN_DERET 0x4200001f
3613
3614 /* Return the number of instructions that must separate INSN1 and INSN2,
3615    where INSN1 is the earlier instruction.  Return the worst-case value
3616    for any INSN2 if INSN2 is null.  */
3617
3618 static unsigned int
3619 insns_between (const struct mips_cl_insn *insn1,
3620                const struct mips_cl_insn *insn2)
3621 {
3622   unsigned long pinfo1, pinfo2;
3623   unsigned int mask;
3624
3625   /* This function needs to know which pinfo flags are set for INSN2
3626      and which registers INSN2 uses.  The former is stored in PINFO2 and
3627      the latter is tested via INSN2_USES_GPR.  If INSN2 is null, PINFO2
3628      will have every flag set and INSN2_USES_GPR will always return true.  */
3629   pinfo1 = insn1->insn_mo->pinfo;
3630   pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
3631
3632 #define INSN2_USES_GPR(REG) \
3633   (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
3634
3635   /* For most targets, write-after-read dependencies on the HI and LO
3636      registers must be separated by at least two instructions.  */
3637   if (!hilo_interlocks)
3638     {
3639       if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3640         return 2;
3641       if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3642         return 2;
3643     }
3644
3645   /* If we're working around r7000 errata, there must be two instructions
3646      between an mfhi or mflo and any instruction that uses the result.  */
3647   if (mips_7000_hilo_fix
3648       && !mips_opts.micromips
3649       && MF_HILO_INSN (pinfo1)
3650       && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
3651     return 2;
3652
3653   /* If we're working around 24K errata, one instruction is required
3654      if an ERET or DERET is followed by a branch instruction.  */
3655   if (mips_fix_24k && !mips_opts.micromips)
3656     {
3657       if (insn1->insn_opcode == INSN_ERET
3658           || insn1->insn_opcode == INSN_DERET)
3659         {
3660           if (insn2 == NULL
3661               || insn2->insn_opcode == INSN_ERET
3662               || insn2->insn_opcode == INSN_DERET
3663               || delayed_branch_p (insn2))
3664             return 1;
3665         }
3666     }
3667
3668   /* If working around VR4120 errata, check for combinations that need
3669      a single intervening instruction.  */
3670   if (mips_fix_vr4120 && !mips_opts.micromips)
3671     {
3672       unsigned int class1, class2;
3673
3674       class1 = classify_vr4120_insn (insn1->insn_mo->name);
3675       if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
3676         {
3677           if (insn2 == NULL)
3678             return 1;
3679           class2 = classify_vr4120_insn (insn2->insn_mo->name);
3680           if (vr4120_conflicts[class1] & (1 << class2))
3681             return 1;
3682         }
3683     }
3684
3685   if (!HAVE_CODE_COMPRESSION)
3686     {
3687       /* Check for GPR or coprocessor load delays.  All such delays
3688          are on the RT register.  */
3689       /* Itbl support may require additional care here.  */
3690       if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3691           || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
3692         {
3693           know (pinfo1 & INSN_WRITE_GPR_T);
3694           if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
3695             return 1;
3696         }
3697
3698       /* Check for generic coprocessor hazards.
3699
3700          This case is not handled very well.  There is no special
3701          knowledge of CP0 handling, and the coprocessors other than
3702          the floating point unit are not distinguished at all.  */
3703       /* Itbl support may require additional care here. FIXME!
3704          Need to modify this to include knowledge about
3705          user specified delays!  */
3706       else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3707                || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3708         {
3709           /* Handle cases where INSN1 writes to a known general coprocessor
3710              register.  There must be a one instruction delay before INSN2
3711              if INSN2 reads that register, otherwise no delay is needed.  */
3712           mask = fpr_write_mask (insn1);
3713           if (mask != 0)
3714             {
3715               if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
3716                 return 1;
3717             }
3718           else
3719             {
3720               /* Read-after-write dependencies on the control registers
3721                  require a two-instruction gap.  */
3722               if ((pinfo1 & INSN_WRITE_COND_CODE)
3723                   && (pinfo2 & INSN_READ_COND_CODE))
3724                 return 2;
3725
3726               /* We don't know exactly what INSN1 does.  If INSN2 is
3727                  also a coprocessor instruction, assume there must be
3728                  a one instruction gap.  */
3729               if (pinfo2 & INSN_COP)
3730                 return 1;
3731             }
3732         }
3733
3734       /* Check for read-after-write dependencies on the coprocessor
3735          control registers in cases where INSN1 does not need a general
3736          coprocessor delay.  This means that INSN1 is a floating point
3737          comparison instruction.  */
3738       /* Itbl support may require additional care here.  */
3739       else if (!cop_interlocks
3740                && (pinfo1 & INSN_WRITE_COND_CODE)
3741                && (pinfo2 & INSN_READ_COND_CODE))
3742         return 1;
3743     }
3744
3745 #undef INSN2_USES_GPR
3746
3747   return 0;
3748 }
3749
3750 /* Return the number of nops that would be needed to work around the
3751    VR4130 mflo/mfhi errata if instruction INSN immediately followed
3752    the MAX_VR4130_NOPS instructions described by HIST.  Ignore hazards
3753    that are contained within the first IGNORE instructions of HIST.  */
3754
3755 static int
3756 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
3757                  const struct mips_cl_insn *insn)
3758 {
3759   int i, j;
3760   unsigned int mask;
3761
3762   /* Check if the instruction writes to HI or LO.  MTHI and MTLO
3763      are not affected by the errata.  */
3764   if (insn != 0
3765       && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3766           || strcmp (insn->insn_mo->name, "mtlo") == 0
3767           || strcmp (insn->insn_mo->name, "mthi") == 0))
3768     return 0;
3769
3770   /* Search for the first MFLO or MFHI.  */
3771   for (i = 0; i < MAX_VR4130_NOPS; i++)
3772     if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
3773       {
3774         /* Extract the destination register.  */
3775         mask = gpr_write_mask (&hist[i]);
3776
3777         /* No nops are needed if INSN reads that register.  */
3778         if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
3779           return 0;
3780
3781         /* ...or if any of the intervening instructions do.  */
3782         for (j = 0; j < i; j++)
3783           if (gpr_read_mask (&hist[j]) & mask)
3784             return 0;
3785
3786         if (i >= ignore)
3787           return MAX_VR4130_NOPS - i;
3788       }
3789   return 0;
3790 }
3791
3792 #define BASE_REG_EQ(INSN1, INSN2)       \
3793   ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3794       == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3795
3796 /* Return the minimum alignment for this store instruction.  */
3797
3798 static int
3799 fix_24k_align_to (const struct mips_opcode *mo)
3800 {
3801   if (strcmp (mo->name, "sh") == 0)
3802     return 2;
3803
3804   if (strcmp (mo->name, "swc1") == 0
3805       || strcmp (mo->name, "swc2") == 0
3806       || strcmp (mo->name, "sw") == 0
3807       || strcmp (mo->name, "sc") == 0
3808       || strcmp (mo->name, "s.s") == 0)
3809     return 4;
3810
3811   if (strcmp (mo->name, "sdc1") == 0
3812       || strcmp (mo->name, "sdc2") == 0
3813       || strcmp (mo->name, "s.d") == 0)
3814     return 8;
3815
3816   /* sb, swl, swr */
3817   return 1;
3818 }
3819
3820 struct fix_24k_store_info
3821   {
3822     /* Immediate offset, if any, for this store instruction.  */
3823     short off;
3824     /* Alignment required by this store instruction.  */
3825     int align_to;
3826     /* True for register offsets.  */
3827     int register_offset;
3828   };
3829
3830 /* Comparison function used by qsort.  */
3831
3832 static int
3833 fix_24k_sort (const void *a, const void *b)
3834 {
3835   const struct fix_24k_store_info *pos1 = a;
3836   const struct fix_24k_store_info *pos2 = b;
3837
3838   return (pos1->off - pos2->off);
3839 }
3840
3841 /* INSN is a store instruction.  Try to record the store information
3842    in STINFO.  Return false if the information isn't known.  */
3843
3844 static bfd_boolean
3845 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
3846                            const struct mips_cl_insn *insn)
3847 {
3848   /* The instruction must have a known offset.  */
3849   if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3850     return FALSE;
3851
3852   stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3853   stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3854   return TRUE;
3855 }
3856
3857 /* Return the number of nops that would be needed to work around the 24k
3858    "lost data on stores during refill" errata if instruction INSN
3859    immediately followed the 2 instructions described by HIST.
3860    Ignore hazards that are contained within the first IGNORE
3861    instructions of HIST.
3862
3863    Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3864    for the data cache refills and store data. The following describes
3865    the scenario where the store data could be lost.
3866
3867    * A data cache miss, due to either a load or a store, causing fill
3868      data to be supplied by the memory subsystem
3869    * The first three doublewords of fill data are returned and written
3870      into the cache
3871    * A sequence of four stores occurs in consecutive cycles around the
3872      final doubleword of the fill:
3873    * Store A
3874    * Store B
3875    * Store C
3876    * Zero, One or more instructions
3877    * Store D
3878
3879    The four stores A-D must be to different doublewords of the line that
3880    is being filled. The fourth instruction in the sequence above permits
3881    the fill of the final doubleword to be transferred from the FSB into
3882    the cache. In the sequence above, the stores may be either integer
3883    (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3884    swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3885    different doublewords on the line. If the floating point unit is
3886    running in 1:2 mode, it is not possible to create the sequence above
3887    using only floating point store instructions.
3888
3889    In this case, the cache line being filled is incorrectly marked
3890    invalid, thereby losing the data from any store to the line that
3891    occurs between the original miss and the completion of the five
3892    cycle sequence shown above.
3893
3894    The workarounds are:
3895
3896    * Run the data cache in write-through mode.
3897    * Insert a non-store instruction between
3898      Store A and Store B or Store B and Store C.  */
3899   
3900 static int
3901 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
3902               const struct mips_cl_insn *insn)
3903 {
3904   struct fix_24k_store_info pos[3];
3905   int align, i, base_offset;
3906
3907   if (ignore >= 2)
3908     return 0;
3909
3910   /* If the previous instruction wasn't a store, there's nothing to
3911      worry about.  */
3912   if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3913     return 0;
3914
3915   /* If the instructions after the previous one are unknown, we have
3916      to assume the worst.  */
3917   if (!insn)
3918     return 1;
3919
3920   /* Check whether we are dealing with three consecutive stores.  */
3921   if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3922       || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3923     return 0;
3924
3925   /* If we don't know the relationship between the store addresses,
3926      assume the worst.  */
3927   if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
3928       || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3929     return 1;
3930
3931   if (!fix_24k_record_store_info (&pos[0], insn)
3932       || !fix_24k_record_store_info (&pos[1], &hist[0])
3933       || !fix_24k_record_store_info (&pos[2], &hist[1]))
3934     return 1;
3935
3936   qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3937
3938   /* Pick a value of ALIGN and X such that all offsets are adjusted by
3939      X bytes and such that the base register + X is known to be aligned
3940      to align bytes.  */
3941
3942   if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3943     align = 8;
3944   else
3945     {
3946       align = pos[0].align_to;
3947       base_offset = pos[0].off;
3948       for (i = 1; i < 3; i++)
3949         if (align < pos[i].align_to)
3950           {
3951             align = pos[i].align_to;
3952             base_offset = pos[i].off;
3953           }
3954       for (i = 0; i < 3; i++)
3955         pos[i].off -= base_offset;
3956     }
3957
3958   pos[0].off &= ~align + 1;
3959   pos[1].off &= ~align + 1;
3960   pos[2].off &= ~align + 1;
3961
3962   /* If any two stores write to the same chunk, they also write to the
3963      same doubleword.  The offsets are still sorted at this point.  */
3964   if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3965     return 0;
3966
3967   /* A range of at least 9 bytes is needed for the stores to be in
3968      non-overlapping doublewords.  */
3969   if (pos[2].off - pos[0].off <= 8)
3970     return 0;
3971
3972   if (pos[2].off - pos[1].off >= 24
3973       || pos[1].off - pos[0].off >= 24
3974       || pos[2].off - pos[0].off >= 32)
3975     return 0;
3976
3977   return 1;
3978 }
3979
3980 /* Return the number of nops that would be needed if instruction INSN
3981    immediately followed the MAX_NOPS instructions given by HIST,
3982    where HIST[0] is the most recent instruction.  Ignore hazards
3983    between INSN and the first IGNORE instructions in HIST.
3984
3985    If INSN is null, return the worse-case number of nops for any
3986    instruction.  */
3987
3988 static int
3989 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
3990                const struct mips_cl_insn *insn)
3991 {
3992   int i, nops, tmp_nops;
3993
3994   nops = 0;
3995   for (i = ignore; i < MAX_DELAY_NOPS; i++)
3996     {
3997       tmp_nops = insns_between (hist + i, insn) - i;
3998       if (tmp_nops > nops)
3999         nops = tmp_nops;
4000     }
4001
4002   if (mips_fix_vr4130 && !mips_opts.micromips)
4003     {
4004       tmp_nops = nops_for_vr4130 (ignore, hist, insn);
4005       if (tmp_nops > nops)
4006         nops = tmp_nops;
4007     }
4008
4009   if (mips_fix_24k && !mips_opts.micromips)
4010     {
4011       tmp_nops = nops_for_24k (ignore, hist, insn);
4012       if (tmp_nops > nops)
4013         nops = tmp_nops;
4014     }
4015
4016   return nops;
4017 }
4018
4019 /* The variable arguments provide NUM_INSNS extra instructions that
4020    might be added to HIST.  Return the largest number of nops that
4021    would be needed after the extended sequence, ignoring hazards
4022    in the first IGNORE instructions.  */
4023
4024 static int
4025 nops_for_sequence (int num_insns, int ignore,
4026                    const struct mips_cl_insn *hist, ...)
4027 {
4028   va_list args;
4029   struct mips_cl_insn buffer[MAX_NOPS];
4030   struct mips_cl_insn *cursor;
4031   int nops;
4032
4033   va_start (args, hist);
4034   cursor = buffer + num_insns;
4035   memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
4036   while (cursor > buffer)
4037     *--cursor = *va_arg (args, const struct mips_cl_insn *);
4038
4039   nops = nops_for_insn (ignore, buffer, NULL);
4040   va_end (args);
4041   return nops;
4042 }
4043
4044 /* Like nops_for_insn, but if INSN is a branch, take into account the
4045    worst-case delay for the branch target.  */
4046
4047 static int
4048 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
4049                          const struct mips_cl_insn *insn)
4050 {
4051   int nops, tmp_nops;
4052
4053   nops = nops_for_insn (ignore, hist, insn);
4054   if (delayed_branch_p (insn))
4055     {
4056       tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
4057                                     hist, insn, get_delay_slot_nop (insn));
4058       if (tmp_nops > nops)
4059         nops = tmp_nops;
4060     }
4061   else if (compact_branch_p (insn))
4062     {
4063       tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
4064       if (tmp_nops > nops)
4065         nops = tmp_nops;
4066     }
4067   return nops;
4068 }
4069
4070 /* Fix NOP issue: Replace nops by "or at,at,zero".  */
4071
4072 static void
4073 fix_loongson2f_nop (struct mips_cl_insn * ip)
4074 {
4075   gas_assert (!HAVE_CODE_COMPRESSION);
4076   if (strcmp (ip->insn_mo->name, "nop") == 0)
4077     ip->insn_opcode = LOONGSON2F_NOP_INSN;
4078 }
4079
4080 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
4081                    jr target pc &= 'hffff_ffff_cfff_ffff.  */
4082
4083 static void
4084 fix_loongson2f_jump (struct mips_cl_insn * ip)
4085 {
4086   gas_assert (!HAVE_CODE_COMPRESSION);
4087   if (strcmp (ip->insn_mo->name, "j") == 0
4088       || strcmp (ip->insn_mo->name, "jr") == 0
4089       || strcmp (ip->insn_mo->name, "jalr") == 0)
4090     {
4091       int sreg;
4092       expressionS ep;
4093
4094       if (! mips_opts.at)
4095         return;
4096
4097       sreg = EXTRACT_OPERAND (0, RS, *ip);
4098       if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
4099         return;
4100
4101       ep.X_op = O_constant;
4102       ep.X_add_number = 0xcfff0000;
4103       macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
4104       ep.X_add_number = 0xffff;
4105       macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
4106       macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
4107     }
4108 }
4109
4110 static void
4111 fix_loongson2f (struct mips_cl_insn * ip)
4112 {
4113   if (mips_fix_loongson2f_nop)
4114     fix_loongson2f_nop (ip);
4115
4116   if (mips_fix_loongson2f_jump)
4117     fix_loongson2f_jump (ip);
4118 }
4119
4120 /* IP is a branch that has a delay slot, and we need to fill it
4121    automatically.   Return true if we can do that by swapping IP
4122    with the previous instruction.
4123    ADDRESS_EXPR is an operand of the instruction to be used with
4124    RELOC_TYPE.  */
4125
4126 static bfd_boolean
4127 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
4128   bfd_reloc_code_real_type *reloc_type)
4129 {
4130   unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
4131   unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
4132
4133   /* -O2 and above is required for this optimization.  */
4134   if (mips_optimize < 2)
4135     return FALSE;
4136
4137   /* If we have seen .set volatile or .set nomove, don't optimize.  */
4138   if (mips_opts.nomove)
4139     return FALSE;
4140
4141   /* We can't swap if the previous instruction's position is fixed.  */
4142   if (history[0].fixed_p)
4143     return FALSE;
4144
4145   /* If the previous previous insn was in a .set noreorder, we can't
4146      swap.  Actually, the MIPS assembler will swap in this situation.
4147      However, gcc configured -with-gnu-as will generate code like
4148
4149         .set    noreorder
4150         lw      $4,XXX
4151         .set    reorder
4152         INSN
4153         bne     $4,$0,foo
4154
4155      in which we can not swap the bne and INSN.  If gcc is not configured
4156      -with-gnu-as, it does not output the .set pseudo-ops.  */
4157   if (history[1].noreorder_p)
4158     return FALSE;
4159
4160   /* If the previous instruction had a fixup in mips16 mode, we can not swap.
4161      This means that the previous instruction was a 4-byte one anyhow.  */
4162   if (mips_opts.mips16 && history[0].fixp[0])
4163     return FALSE;
4164
4165   /* If the branch is itself the target of a branch, we can not swap.
4166      We cheat on this; all we check for is whether there is a label on
4167      this instruction.  If there are any branches to anything other than
4168      a label, users must use .set noreorder.  */
4169   if (seg_info (now_seg)->label_list)
4170     return FALSE;
4171
4172   /* If the previous instruction is in a variant frag other than this
4173      branch's one, we cannot do the swap.  This does not apply to
4174      MIPS16 code, which uses variant frags for different purposes.  */
4175   if (!mips_opts.mips16
4176       && history[0].frag
4177       && history[0].frag->fr_type == rs_machine_dependent)
4178     return FALSE;
4179
4180   /* We do not swap with instructions that cannot architecturally
4181      be placed in a branch delay slot, such as SYNC or ERET.  We
4182      also refrain from swapping with a trap instruction, since it
4183      complicates trap handlers to have the trap instruction be in
4184      a delay slot.  */
4185   prev_pinfo = history[0].insn_mo->pinfo;
4186   if (prev_pinfo & INSN_NO_DELAY_SLOT)
4187     return FALSE;
4188
4189   /* Check for conflicts between the branch and the instructions
4190      before the candidate delay slot.  */
4191   if (nops_for_insn (0, history + 1, ip) > 0)
4192     return FALSE;
4193
4194   /* Check for conflicts between the swapped sequence and the
4195      target of the branch.  */
4196   if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
4197     return FALSE;
4198
4199   /* If the branch reads a register that the previous
4200      instruction sets, we can not swap.  */
4201   gpr_read = gpr_read_mask (ip);
4202   prev_gpr_write = gpr_write_mask (&history[0]);
4203   if (gpr_read & prev_gpr_write)
4204     return FALSE;
4205
4206   /* If the branch writes a register that the previous
4207      instruction sets, we can not swap.  */
4208   gpr_write = gpr_write_mask (ip);
4209   if (gpr_write & prev_gpr_write)
4210     return FALSE;
4211
4212   /* If the branch writes a register that the previous
4213      instruction reads, we can not swap.  */
4214   prev_gpr_read = gpr_read_mask (&history[0]);
4215   if (gpr_write & prev_gpr_read)
4216     return FALSE;
4217
4218   /* If one instruction sets a condition code and the
4219      other one uses a condition code, we can not swap.  */
4220   pinfo = ip->insn_mo->pinfo;
4221   if ((pinfo & INSN_READ_COND_CODE)
4222       && (prev_pinfo & INSN_WRITE_COND_CODE))
4223     return FALSE;
4224   if ((pinfo & INSN_WRITE_COND_CODE)
4225       && (prev_pinfo & INSN_READ_COND_CODE))
4226     return FALSE;
4227
4228   /* If the previous instruction uses the PC, we can not swap.  */
4229   prev_pinfo2 = history[0].insn_mo->pinfo2;
4230   if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
4231     return FALSE;
4232   if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
4233     return FALSE;
4234
4235   /* If the previous instruction has an incorrect size for a fixed
4236      branch delay slot in microMIPS mode, we cannot swap.  */
4237   pinfo2 = ip->insn_mo->pinfo2;
4238   if (mips_opts.micromips
4239       && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
4240       && insn_length (history) != 2)
4241     return FALSE;
4242   if (mips_opts.micromips
4243       && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
4244       && insn_length (history) != 4)
4245     return FALSE;
4246
4247   /* On R5900 short loops need to be fixed by inserting a nop in
4248      the branch delay slots.
4249      A short loop can be terminated too early.  */
4250   if (mips_opts.arch == CPU_R5900
4251       /* Check if instruction has a parameter, ignore "j $31". */
4252       && (address_expr != NULL)
4253       /* Parameter must be 16 bit. */
4254       && (*reloc_type == BFD_RELOC_16_PCREL_S2)
4255       /* Branch to same segment. */
4256       && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
4257       /* Branch to same code fragment. */
4258       && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
4259       /* Can only calculate branch offset if value is known. */
4260       && symbol_constant_p(address_expr->X_add_symbol)
4261       /* Check if branch is really conditional. */
4262       && !((ip->insn_opcode & 0xffff0000) == 0x10000000   /* beq $0,$0 */
4263         || (ip->insn_opcode & 0xffff0000) == 0x04010000   /* bgez $0 */
4264         || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
4265     {
4266       int distance;
4267       /* Check if loop is shorter than 6 instructions including
4268          branch and delay slot.  */
4269       distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
4270       if (distance <= 20)
4271         {
4272           int i;
4273           int rv;
4274
4275           rv = FALSE;
4276           /* When the loop includes branches or jumps,
4277              it is not a short loop. */
4278           for (i = 0; i < (distance / 4); i++)
4279             {
4280               if ((history[i].cleared_p)
4281                   || delayed_branch_p(&history[i]))
4282                 {
4283                   rv = TRUE;
4284                   break;
4285                 }
4286             }
4287           if (rv == FALSE)
4288             {
4289               /* Insert nop after branch to fix short loop. */
4290               return FALSE;
4291             }
4292         }
4293     }
4294
4295   return TRUE;
4296 }
4297
4298 /* Decide how we should add IP to the instruction stream.
4299    ADDRESS_EXPR is an operand of the instruction to be used with
4300    RELOC_TYPE.  */
4301
4302 static enum append_method
4303 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
4304   bfd_reloc_code_real_type *reloc_type)
4305 {
4306   unsigned long pinfo;
4307
4308   /* The relaxed version of a macro sequence must be inherently
4309      hazard-free.  */
4310   if (mips_relax.sequence == 2)
4311     return APPEND_ADD;
4312
4313   /* We must not dabble with instructions in a ".set norerorder" block.  */
4314   if (mips_opts.noreorder)
4315     return APPEND_ADD;
4316
4317   /* Otherwise, it's our responsibility to fill branch delay slots.  */
4318   if (delayed_branch_p (ip))
4319     {
4320       if (!branch_likely_p (ip)
4321           && can_swap_branch_p (ip, address_expr, reloc_type))
4322         return APPEND_SWAP;
4323
4324       pinfo = ip->insn_mo->pinfo;
4325       if (mips_opts.mips16
4326           && ISA_SUPPORTS_MIPS16E
4327           && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
4328         return APPEND_ADD_COMPACT;
4329
4330       return APPEND_ADD_WITH_NOP;
4331     }
4332
4333   return APPEND_ADD;
4334 }
4335
4336 /* IP is a MIPS16 instruction whose opcode we have just changed.
4337    Point IP->insn_mo to the new opcode's definition.  */
4338
4339 static void
4340 find_altered_mips16_opcode (struct mips_cl_insn *ip)
4341 {
4342   const struct mips_opcode *mo, *end;
4343
4344   end = &mips16_opcodes[bfd_mips16_num_opcodes];
4345   for (mo = ip->insn_mo; mo < end; mo++)
4346     if ((ip->insn_opcode & mo->mask) == mo->match)
4347       {
4348         ip->insn_mo = mo;
4349         return;
4350       }
4351   abort ();
4352 }
4353
4354 /* For microMIPS macros, we need to generate a local number label
4355    as the target of branches.  */
4356 #define MICROMIPS_LABEL_CHAR            '\037'
4357 static unsigned long micromips_target_label;
4358 static char micromips_target_name[32];
4359
4360 static char *
4361 micromips_label_name (void)
4362 {
4363   char *p = micromips_target_name;
4364   char symbol_name_temporary[24];
4365   unsigned long l;
4366   int i;
4367
4368   if (*p)
4369     return p;
4370
4371   i = 0;
4372   l = micromips_target_label;
4373 #ifdef LOCAL_LABEL_PREFIX
4374   *p++ = LOCAL_LABEL_PREFIX;
4375 #endif
4376   *p++ = 'L';
4377   *p++ = MICROMIPS_LABEL_CHAR;
4378   do
4379     {
4380       symbol_name_temporary[i++] = l % 10 + '0';
4381       l /= 10;
4382     }
4383   while (l != 0);
4384   while (i > 0)
4385     *p++ = symbol_name_temporary[--i];
4386   *p = '\0';
4387
4388   return micromips_target_name;
4389 }
4390
4391 static void
4392 micromips_label_expr (expressionS *label_expr)
4393 {
4394   label_expr->X_op = O_symbol;
4395   label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
4396   label_expr->X_add_number = 0;
4397 }
4398
4399 static void
4400 micromips_label_inc (void)
4401 {
4402   micromips_target_label++;
4403   *micromips_target_name = '\0';
4404 }
4405
4406 static void
4407 micromips_add_label (void)
4408 {
4409   symbolS *s;
4410
4411   s = colon (micromips_label_name ());
4412   micromips_label_inc ();
4413   S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
4414 }
4415
4416 /* If assembling microMIPS code, then return the microMIPS reloc
4417    corresponding to the requested one if any.  Otherwise return
4418    the reloc unchanged.  */
4419
4420 static bfd_reloc_code_real_type
4421 micromips_map_reloc (bfd_reloc_code_real_type reloc)
4422 {
4423   static const bfd_reloc_code_real_type relocs[][2] =
4424     {
4425       /* Keep sorted incrementally by the left-hand key.  */
4426       { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
4427       { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
4428       { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
4429       { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
4430       { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
4431       { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
4432       { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
4433       { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
4434       { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
4435       { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
4436       { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
4437       { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
4438       { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
4439       { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
4440       { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4441       { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4442       { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4443       { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4444       { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4445       { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4446       { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4447       { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4448       { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4449       { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4450       { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4451       { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4452       { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4453     };
4454   bfd_reloc_code_real_type r;
4455   size_t i;
4456
4457   if (!mips_opts.micromips)
4458     return reloc;
4459   for (i = 0; i < ARRAY_SIZE (relocs); i++)
4460     {
4461       r = relocs[i][0];
4462       if (r > reloc)
4463         return reloc;
4464       if (r == reloc)
4465         return relocs[i][1];
4466     }
4467   return reloc;
4468 }
4469
4470 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
4471    Return true on success, storing the resolved value in RESULT.  */
4472
4473 static bfd_boolean
4474 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
4475                  offsetT *result)
4476 {
4477   switch (reloc)
4478     {
4479     case BFD_RELOC_MIPS_HIGHEST:
4480     case BFD_RELOC_MICROMIPS_HIGHEST:
4481       *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
4482       return TRUE;
4483
4484     case BFD_RELOC_MIPS_HIGHER:
4485     case BFD_RELOC_MICROMIPS_HIGHER:
4486       *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
4487       return TRUE;
4488
4489     case BFD_RELOC_HI16_S:
4490     case BFD_RELOC_MICROMIPS_HI16_S:
4491     case BFD_RELOC_MIPS16_HI16_S:
4492       *result = ((operand + 0x8000) >> 16) & 0xffff;
4493       return TRUE;
4494
4495     case BFD_RELOC_HI16:
4496     case BFD_RELOC_MICROMIPS_HI16:
4497     case BFD_RELOC_MIPS16_HI16:
4498       *result = (operand >> 16) & 0xffff;
4499       return TRUE;
4500
4501     case BFD_RELOC_LO16:
4502     case BFD_RELOC_MICROMIPS_LO16:
4503     case BFD_RELOC_MIPS16_LO16:
4504       *result = operand & 0xffff;
4505       return TRUE;
4506
4507     case BFD_RELOC_UNUSED:
4508       *result = operand;
4509       return TRUE;
4510
4511     default:
4512       return FALSE;
4513     }
4514 }
4515
4516 /* Output an instruction.  IP is the instruction information.
4517    ADDRESS_EXPR is an operand of the instruction to be used with
4518    RELOC_TYPE.  EXPANSIONP is true if the instruction is part of
4519    a macro expansion.  */
4520
4521 static void
4522 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
4523              bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
4524 {
4525   unsigned long prev_pinfo2, pinfo;
4526   bfd_boolean relaxed_branch = FALSE;
4527   enum append_method method;
4528   bfd_boolean relax32;
4529   int branch_disp;
4530
4531   if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
4532     fix_loongson2f (ip);
4533
4534   file_ase_mips16 |= mips_opts.mips16;
4535   file_ase_micromips |= mips_opts.micromips;
4536
4537   prev_pinfo2 = history[0].insn_mo->pinfo2;
4538   pinfo = ip->insn_mo->pinfo;
4539
4540   if (mips_opts.micromips
4541       && !expansionp
4542       && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4543            && micromips_insn_length (ip->insn_mo) != 2)
4544           || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4545               && micromips_insn_length (ip->insn_mo) != 4)))
4546     as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4547              (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
4548
4549   if (address_expr == NULL)
4550     ip->complete_p = 1;
4551   else if (reloc_type[0] <= BFD_RELOC_UNUSED
4552            && reloc_type[1] == BFD_RELOC_UNUSED
4553            && reloc_type[2] == BFD_RELOC_UNUSED
4554            && address_expr->X_op == O_constant)
4555     {
4556       switch (*reloc_type)
4557         {
4558         case BFD_RELOC_MIPS_JMP:
4559           {
4560             int shift;
4561
4562             shift = mips_opts.micromips ? 1 : 2;
4563             if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4564               as_bad (_("jump to misaligned address (0x%lx)"),
4565                       (unsigned long) address_expr->X_add_number);
4566             ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4567                                 & 0x3ffffff);
4568             ip->complete_p = 1;
4569           }
4570           break;
4571
4572         case BFD_RELOC_MIPS16_JMP:
4573           if ((address_expr->X_add_number & 3) != 0)
4574             as_bad (_("jump to misaligned address (0x%lx)"),
4575                     (unsigned long) address_expr->X_add_number);
4576           ip->insn_opcode |=
4577             (((address_expr->X_add_number & 0x7c0000) << 3)
4578                | ((address_expr->X_add_number & 0xf800000) >> 7)
4579                | ((address_expr->X_add_number & 0x3fffc) >> 2));
4580           ip->complete_p = 1;
4581           break;
4582
4583         case BFD_RELOC_16_PCREL_S2:
4584           {
4585             int shift;
4586
4587             shift = mips_opts.micromips ? 1 : 2;
4588             if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4589               as_bad (_("branch to misaligned address (0x%lx)"),
4590                       (unsigned long) address_expr->X_add_number);
4591             if (!mips_relax_branch)
4592               {
4593                 if ((address_expr->X_add_number + (1 << (shift + 15)))
4594                     & ~((1 << (shift + 16)) - 1))
4595                   as_bad (_("branch address range overflow (0x%lx)"),
4596                           (unsigned long) address_expr->X_add_number);
4597                 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4598                                     & 0xffff);
4599               }
4600           }
4601           break;
4602
4603         default:
4604           {
4605             offsetT value;
4606
4607             if (calculate_reloc (*reloc_type, address_expr->X_add_number,
4608                                  &value))
4609               {
4610                 ip->insn_opcode |= value & 0xffff;
4611                 ip->complete_p = 1;
4612               }
4613           }
4614           break;
4615         }
4616     }
4617
4618   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4619     {
4620       /* There are a lot of optimizations we could do that we don't.
4621          In particular, we do not, in general, reorder instructions.
4622          If you use gcc with optimization, it will reorder
4623          instructions and generally do much more optimization then we
4624          do here; repeating all that work in the assembler would only
4625          benefit hand written assembly code, and does not seem worth
4626          it.  */
4627       int nops = (mips_optimize == 0
4628                   ? nops_for_insn (0, history, NULL)
4629                   : nops_for_insn_or_target (0, history, ip));
4630       if (nops > 0)
4631         {
4632           fragS *old_frag;
4633           unsigned long old_frag_offset;
4634           int i;
4635
4636           old_frag = frag_now;
4637           old_frag_offset = frag_now_fix ();
4638
4639           for (i = 0; i < nops; i++)
4640             add_fixed_insn (NOP_INSN);
4641           insert_into_history (0, nops, NOP_INSN);
4642
4643           if (listing)
4644             {
4645               listing_prev_line ();
4646               /* We may be at the start of a variant frag.  In case we
4647                  are, make sure there is enough space for the frag
4648                  after the frags created by listing_prev_line.  The
4649                  argument to frag_grow here must be at least as large
4650                  as the argument to all other calls to frag_grow in
4651                  this file.  We don't have to worry about being in the
4652                  middle of a variant frag, because the variants insert
4653                  all needed nop instructions themselves.  */
4654               frag_grow (40);
4655             }
4656
4657           mips_move_text_labels ();
4658
4659 #ifndef NO_ECOFF_DEBUGGING
4660           if (ECOFF_DEBUGGING)
4661             ecoff_fix_loc (old_frag, old_frag_offset);
4662 #endif
4663         }
4664     }
4665   else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4666     {
4667       int nops;
4668
4669       /* Work out how many nops in prev_nop_frag are needed by IP,
4670          ignoring hazards generated by the first prev_nop_frag_since
4671          instructions.  */
4672       nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
4673       gas_assert (nops <= prev_nop_frag_holds);
4674
4675       /* Enforce NOPS as a minimum.  */
4676       if (nops > prev_nop_frag_required)
4677         prev_nop_frag_required = nops;
4678
4679       if (prev_nop_frag_holds == prev_nop_frag_required)
4680         {
4681           /* Settle for the current number of nops.  Update the history
4682              accordingly (for the benefit of any future .set reorder code).  */
4683           prev_nop_frag = NULL;
4684           insert_into_history (prev_nop_frag_since,
4685                                prev_nop_frag_holds, NOP_INSN);
4686         }
4687       else
4688         {
4689           /* Allow this instruction to replace one of the nops that was
4690              tentatively added to prev_nop_frag.  */
4691           prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
4692           prev_nop_frag_holds--;
4693           prev_nop_frag_since++;
4694         }
4695     }
4696
4697   method = get_append_method (ip, address_expr, reloc_type);
4698   branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
4699
4700   dwarf2_emit_insn (0);
4701   /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
4702      so "move" the instruction address accordingly.
4703
4704      Also, it doesn't seem appropriate for the assembler to reorder .loc
4705      entries.  If this instruction is a branch that we are going to swap
4706      with the previous instruction, the two instructions should be
4707      treated as a unit, and the debug information for both instructions
4708      should refer to the start of the branch sequence.  Using the
4709      current position is certainly wrong when swapping a 32-bit branch
4710      and a 16-bit delay slot, since the current position would then be
4711      in the middle of a branch.  */
4712   dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
4713
4714   relax32 = (mips_relax_branch
4715              /* Don't try branch relaxation within .set nomacro, or within
4716                 .set noat if we use $at for PIC computations.  If it turns
4717                 out that the branch was out-of-range, we'll get an error.  */
4718              && !mips_opts.warn_about_macros
4719              && (mips_opts.at || mips_pic == NO_PIC)
4720              /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
4721                 as they have no complementing branches.  */
4722              && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
4723
4724   if (!HAVE_CODE_COMPRESSION
4725       && address_expr
4726       && relax32
4727       && *reloc_type == BFD_RELOC_16_PCREL_S2
4728       && delayed_branch_p (ip))
4729     {
4730       relaxed_branch = TRUE;
4731       add_relaxed_insn (ip, (relaxed_branch_length
4732                              (NULL, NULL,
4733                               uncond_branch_p (ip) ? -1
4734                               : branch_likely_p (ip) ? 1
4735                               : 0)), 4,
4736                         RELAX_BRANCH_ENCODE
4737                         (AT,
4738                          uncond_branch_p (ip),
4739                          branch_likely_p (ip),
4740                          pinfo & INSN_WRITE_GPR_31,
4741                          0),
4742                         address_expr->X_add_symbol,
4743                         address_expr->X_add_number);
4744       *reloc_type = BFD_RELOC_UNUSED;
4745     }
4746   else if (mips_opts.micromips
4747            && address_expr
4748            && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4749                || *reloc_type > BFD_RELOC_UNUSED)
4750            && (delayed_branch_p (ip) || compact_branch_p (ip))
4751            /* Don't try branch relaxation when users specify
4752               16-bit/32-bit instructions.  */
4753            && !forced_insn_length)
4754     {
4755       bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4756       int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
4757       int uncond = uncond_branch_p (ip) ? -1 : 0;
4758       int compact = compact_branch_p (ip);
4759       int al = pinfo & INSN_WRITE_GPR_31;
4760       int length32;
4761
4762       gas_assert (address_expr != NULL);
4763       gas_assert (!mips_relax.sequence);
4764
4765       relaxed_branch = TRUE;
4766       length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4767       add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
4768                         RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4769                                                 relax32, 0, 0),
4770                         address_expr->X_add_symbol,
4771                         address_expr->X_add_number);
4772       *reloc_type = BFD_RELOC_UNUSED;
4773     }
4774   else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
4775     {
4776       /* We need to set up a variant frag.  */
4777       gas_assert (address_expr != NULL);
4778       add_relaxed_insn (ip, 4, 0,
4779                         RELAX_MIPS16_ENCODE
4780                         (*reloc_type - BFD_RELOC_UNUSED,
4781                          forced_insn_length == 2, forced_insn_length == 4,
4782                          delayed_branch_p (&history[0]),
4783                          history[0].mips16_absolute_jump_p),
4784                         make_expr_symbol (address_expr), 0);
4785     }
4786   else if (mips_opts.mips16 && insn_length (ip) == 2)
4787     {
4788       if (!delayed_branch_p (ip))
4789         /* Make sure there is enough room to swap this instruction with
4790            a following jump instruction.  */
4791         frag_grow (6);
4792       add_fixed_insn (ip);
4793     }
4794   else
4795     {
4796       if (mips_opts.mips16
4797           && mips_opts.noreorder
4798           && delayed_branch_p (&history[0]))
4799         as_warn (_("extended instruction in delay slot"));
4800
4801       if (mips_relax.sequence)
4802         {
4803           /* If we've reached the end of this frag, turn it into a variant
4804              frag and record the information for the instructions we've
4805              written so far.  */
4806           if (frag_room () < 4)
4807             relax_close_frag ();
4808           mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4809         }
4810
4811       if (mips_relax.sequence != 2)
4812         {
4813           if (mips_macro_warning.first_insn_sizes[0] == 0)
4814             mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4815           mips_macro_warning.sizes[0] += insn_length (ip);
4816           mips_macro_warning.insns[0]++;
4817         }
4818       if (mips_relax.sequence != 1)
4819         {
4820           if (mips_macro_warning.first_insn_sizes[1] == 0)
4821             mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4822           mips_macro_warning.sizes[1] += insn_length (ip);
4823           mips_macro_warning.insns[1]++;
4824         }
4825
4826       if (mips_opts.mips16)
4827         {
4828           ip->fixed_p = 1;
4829           ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4830         }
4831       add_fixed_insn (ip);
4832     }
4833
4834   if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
4835     {
4836       bfd_reloc_code_real_type final_type[3];
4837       reloc_howto_type *howto0;
4838       reloc_howto_type *howto;
4839       int i;
4840
4841       /* Perform any necessary conversion to microMIPS relocations
4842          and find out how many relocations there actually are.  */
4843       for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4844         final_type[i] = micromips_map_reloc (reloc_type[i]);
4845
4846       /* In a compound relocation, it is the final (outermost)
4847          operator that determines the relocated field.  */
4848       howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4849       if (!howto)
4850         abort ();
4851
4852       if (i > 1)
4853         howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
4854       ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4855                                  bfd_get_reloc_size (howto),
4856                                  address_expr,
4857                                  howto0 && howto0->pc_relative,
4858                                  final_type[0]);
4859
4860       /* Tag symbols that have a R_MIPS16_26 relocation against them.  */
4861       if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
4862         *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4863
4864       /* These relocations can have an addend that won't fit in
4865          4 octets for 64bit assembly.  */
4866       if (HAVE_64BIT_GPRS
4867           && ! howto->partial_inplace
4868           && (reloc_type[0] == BFD_RELOC_16
4869               || reloc_type[0] == BFD_RELOC_32
4870               || reloc_type[0] == BFD_RELOC_MIPS_JMP
4871               || reloc_type[0] == BFD_RELOC_GPREL16
4872               || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4873               || reloc_type[0] == BFD_RELOC_GPREL32
4874               || reloc_type[0] == BFD_RELOC_64
4875               || reloc_type[0] == BFD_RELOC_CTOR
4876               || reloc_type[0] == BFD_RELOC_MIPS_SUB
4877               || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4878               || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4879               || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4880               || reloc_type[0] == BFD_RELOC_MIPS_REL16
4881               || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4882               || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4883               || hi16_reloc_p (reloc_type[0])
4884               || lo16_reloc_p (reloc_type[0])))
4885         ip->fixp[0]->fx_no_overflow = 1;
4886
4887       /* These relocations can have an addend that won't fit in 2 octets.  */
4888       if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
4889           || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
4890         ip->fixp[0]->fx_no_overflow = 1;
4891
4892       if (mips_relax.sequence)
4893         {
4894           if (mips_relax.first_fixup == 0)
4895             mips_relax.first_fixup = ip->fixp[0];
4896         }
4897       else if (reloc_needs_lo_p (*reloc_type))
4898         {
4899           struct mips_hi_fixup *hi_fixup;
4900
4901           /* Reuse the last entry if it already has a matching %lo.  */
4902           hi_fixup = mips_hi_fixup_list;
4903           if (hi_fixup == 0
4904               || !fixup_has_matching_lo_p (hi_fixup->fixp))
4905             {
4906               hi_fixup = ((struct mips_hi_fixup *)
4907                           xmalloc (sizeof (struct mips_hi_fixup)));
4908               hi_fixup->next = mips_hi_fixup_list;
4909               mips_hi_fixup_list = hi_fixup;
4910             }
4911           hi_fixup->fixp = ip->fixp[0];
4912           hi_fixup->seg = now_seg;
4913         }
4914
4915       /* Add fixups for the second and third relocations, if given.
4916          Note that the ABI allows the second relocation to be
4917          against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
4918          moment we only use RSS_UNDEF, but we could add support
4919          for the others if it ever becomes necessary.  */
4920       for (i = 1; i < 3; i++)
4921         if (reloc_type[i] != BFD_RELOC_UNUSED)
4922           {
4923             ip->fixp[i] = fix_new (ip->frag, ip->where,
4924                                    ip->fixp[0]->fx_size, NULL, 0,
4925                                    FALSE, final_type[i]);
4926
4927             /* Use fx_tcbit to mark compound relocs.  */
4928             ip->fixp[0]->fx_tcbit = 1;
4929             ip->fixp[i]->fx_tcbit = 1;
4930           }
4931     }
4932   install_insn (ip);
4933
4934   /* Update the register mask information.  */
4935   mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4936   mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
4937
4938   switch (method)
4939     {
4940     case APPEND_ADD:
4941       insert_into_history (0, 1, ip);
4942       break;
4943
4944     case APPEND_ADD_WITH_NOP:
4945       {
4946         struct mips_cl_insn *nop;
4947
4948         insert_into_history (0, 1, ip);
4949         nop = get_delay_slot_nop (ip);
4950         add_fixed_insn (nop);
4951         insert_into_history (0, 1, nop);
4952         if (mips_relax.sequence)
4953           mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4954       }
4955       break;
4956
4957     case APPEND_ADD_COMPACT:
4958       /* Convert MIPS16 jr/jalr into a "compact" jump.  */
4959       gas_assert (mips_opts.mips16);
4960       ip->insn_opcode |= 0x0080;
4961       find_altered_mips16_opcode (ip);
4962       install_insn (ip);
4963       insert_into_history (0, 1, ip);
4964       break;
4965
4966     case APPEND_SWAP:
4967       {
4968         struct mips_cl_insn delay = history[0];
4969         if (mips_opts.mips16)
4970           {
4971             know (delay.frag == ip->frag);
4972             move_insn (ip, delay.frag, delay.where);
4973             move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4974           }
4975         else if (relaxed_branch || delay.frag != ip->frag)
4976           {
4977             /* Add the delay slot instruction to the end of the
4978                current frag and shrink the fixed part of the
4979                original frag.  If the branch occupies the tail of
4980                the latter, move it backwards to cover the gap.  */
4981             delay.frag->fr_fix -= branch_disp;
4982             if (delay.frag == ip->frag)
4983               move_insn (ip, ip->frag, ip->where - branch_disp);
4984             add_fixed_insn (&delay);
4985           }
4986         else
4987           {
4988             move_insn (&delay, ip->frag,
4989                        ip->where - branch_disp + insn_length (ip));
4990             move_insn (ip, history[0].frag, history[0].where);
4991           }
4992         history[0] = *ip;
4993         delay.fixed_p = 1;
4994         insert_into_history (0, 1, &delay);
4995       }
4996       break;
4997     }
4998
4999   /* If we have just completed an unconditional branch, clear the history.  */
5000   if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
5001       || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
5002     {
5003       unsigned int i;
5004
5005       mips_no_prev_insn ();
5006
5007       for (i = 0; i < ARRAY_SIZE (history); i++)
5008         history[i].cleared_p = 1;
5009     }
5010
5011   /* We need to emit a label at the end of branch-likely macros.  */
5012   if (emit_branch_likely_macro)
5013     {
5014       emit_branch_likely_macro = FALSE;
5015       micromips_add_label ();
5016     }
5017
5018   /* We just output an insn, so the next one doesn't have a label.  */
5019   mips_clear_insn_labels ();
5020 }
5021
5022 /* Forget that there was any previous instruction or label.
5023    When BRANCH is true, the branch history is also flushed.  */
5024
5025 static void
5026 mips_no_prev_insn (void)
5027 {
5028   prev_nop_frag = NULL;
5029   insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
5030   mips_clear_insn_labels ();
5031 }
5032
5033 /* This function must be called before we emit something other than
5034    instructions.  It is like mips_no_prev_insn except that it inserts
5035    any NOPS that might be needed by previous instructions.  */
5036
5037 void
5038 mips_emit_delays (void)
5039 {
5040   if (! mips_opts.noreorder)
5041     {
5042       int nops = nops_for_insn (0, history, NULL);
5043       if (nops > 0)
5044         {
5045           while (nops-- > 0)
5046             add_fixed_insn (NOP_INSN);
5047           mips_move_text_labels ();
5048         }
5049     }
5050   mips_no_prev_insn ();
5051 }
5052
5053 /* Start a (possibly nested) noreorder block.  */
5054
5055 static void
5056 start_noreorder (void)
5057 {
5058   if (mips_opts.noreorder == 0)
5059     {
5060       unsigned int i;
5061       int nops;
5062
5063       /* None of the instructions before the .set noreorder can be moved.  */
5064       for (i = 0; i < ARRAY_SIZE (history); i++)
5065         history[i].fixed_p = 1;
5066
5067       /* Insert any nops that might be needed between the .set noreorder
5068          block and the previous instructions.  We will later remove any
5069          nops that turn out not to be needed.  */
5070       nops = nops_for_insn (0, history, NULL);
5071       if (nops > 0)
5072         {
5073           if (mips_optimize != 0)
5074             {
5075               /* Record the frag which holds the nop instructions, so
5076                  that we can remove them if we don't need them.  */
5077               frag_grow (nops * NOP_INSN_SIZE);
5078               prev_nop_frag = frag_now;
5079               prev_nop_frag_holds = nops;
5080               prev_nop_frag_required = 0;
5081               prev_nop_frag_since = 0;
5082             }
5083
5084           for (; nops > 0; --nops)
5085             add_fixed_insn (NOP_INSN);
5086
5087           /* Move on to a new frag, so that it is safe to simply
5088              decrease the size of prev_nop_frag.  */
5089           frag_wane (frag_now);
5090           frag_new (0);
5091           mips_move_text_labels ();
5092         }
5093       mips_mark_labels ();
5094       mips_clear_insn_labels ();
5095     }
5096   mips_opts.noreorder++;
5097   mips_any_noreorder = 1;
5098 }
5099
5100 /* End a nested noreorder block.  */
5101
5102 static void
5103 end_noreorder (void)
5104 {
5105   mips_opts.noreorder--;
5106   if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
5107     {
5108       /* Commit to inserting prev_nop_frag_required nops and go back to
5109          handling nop insertion the .set reorder way.  */
5110       prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
5111                                 * NOP_INSN_SIZE);
5112       insert_into_history (prev_nop_frag_since,
5113                            prev_nop_frag_required, NOP_INSN);
5114       prev_nop_frag = NULL;
5115     }
5116 }
5117
5118 /* Set up global variables for the start of a new macro.  */
5119
5120 static void
5121 macro_start (void)
5122 {
5123   memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
5124   memset (&mips_macro_warning.first_insn_sizes, 0,
5125           sizeof (mips_macro_warning.first_insn_sizes));
5126   memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
5127   mips_macro_warning.delay_slot_p = (mips_opts.noreorder
5128                                      && delayed_branch_p (&history[0]));
5129   switch (history[0].insn_mo->pinfo2
5130           & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
5131     {
5132     case INSN2_BRANCH_DELAY_32BIT:
5133       mips_macro_warning.delay_slot_length = 4;
5134       break;
5135     case INSN2_BRANCH_DELAY_16BIT:
5136       mips_macro_warning.delay_slot_length = 2;
5137       break;
5138     default:
5139       mips_macro_warning.delay_slot_length = 0;
5140       break;
5141     }
5142   mips_macro_warning.first_frag = NULL;
5143 }
5144
5145 /* Given that a macro is longer than one instruction or of the wrong size,
5146    return the appropriate warning for it.  Return null if no warning is
5147    needed.  SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
5148    RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
5149    and RELAX_NOMACRO.  */
5150
5151 static const char *
5152 macro_warning (relax_substateT subtype)
5153 {
5154   if (subtype & RELAX_DELAY_SLOT)
5155     return _("Macro instruction expanded into multiple instructions"
5156              " in a branch delay slot");
5157   else if (subtype & RELAX_NOMACRO)
5158     return _("Macro instruction expanded into multiple instructions");
5159   else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
5160                       | RELAX_DELAY_SLOT_SIZE_SECOND))
5161     return ((subtype & RELAX_DELAY_SLOT_16BIT)
5162             ? _("Macro instruction expanded into a wrong size instruction"
5163                 " in a 16-bit branch delay slot")
5164             : _("Macro instruction expanded into a wrong size instruction"
5165                 " in a 32-bit branch delay slot"));
5166   else
5167     return 0;
5168 }
5169
5170 /* Finish up a macro.  Emit warnings as appropriate.  */
5171
5172 static void
5173 macro_end (void)
5174 {
5175   /* Relaxation warning flags.  */
5176   relax_substateT subtype = 0;
5177
5178   /* Check delay slot size requirements.  */
5179   if (mips_macro_warning.delay_slot_length == 2)
5180     subtype |= RELAX_DELAY_SLOT_16BIT;
5181   if (mips_macro_warning.delay_slot_length != 0)
5182     {
5183       if (mips_macro_warning.delay_slot_length
5184           != mips_macro_warning.first_insn_sizes[0])
5185         subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
5186       if (mips_macro_warning.delay_slot_length
5187           != mips_macro_warning.first_insn_sizes[1])
5188         subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
5189     }
5190
5191   /* Check instruction count requirements.  */
5192   if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
5193     {
5194       if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
5195         subtype |= RELAX_SECOND_LONGER;
5196       if (mips_opts.warn_about_macros)
5197         subtype |= RELAX_NOMACRO;
5198       if (mips_macro_warning.delay_slot_p)
5199         subtype |= RELAX_DELAY_SLOT;
5200     }
5201
5202   /* If both alternatives fail to fill a delay slot correctly,
5203      emit the warning now.  */
5204   if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
5205       && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
5206     {
5207       relax_substateT s;
5208       const char *msg;
5209
5210       s = subtype & (RELAX_DELAY_SLOT_16BIT
5211                      | RELAX_DELAY_SLOT_SIZE_FIRST
5212                      | RELAX_DELAY_SLOT_SIZE_SECOND);
5213       msg = macro_warning (s);
5214       if (msg != NULL)
5215         as_warn ("%s", msg);
5216       subtype &= ~s;
5217     }
5218
5219   /* If both implementations are longer than 1 instruction, then emit the
5220      warning now.  */
5221   if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
5222     {
5223       relax_substateT s;
5224       const char *msg;
5225
5226       s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
5227       msg = macro_warning (s);
5228       if (msg != NULL)
5229         as_warn ("%s", msg);
5230       subtype &= ~s;
5231     }
5232
5233   /* If any flags still set, then one implementation might need a warning
5234      and the other either will need one of a different kind or none at all.
5235      Pass any remaining flags over to relaxation.  */
5236   if (mips_macro_warning.first_frag != NULL)
5237     mips_macro_warning.first_frag->fr_subtype |= subtype;
5238 }
5239
5240 /* Instruction operand formats used in macros that vary between
5241    standard MIPS and microMIPS code.  */
5242
5243 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
5244 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
5245 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
5246 static const char * const lui_fmt[2] = { "t,u", "s,u" };
5247 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
5248 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
5249 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
5250 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
5251
5252 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
5253 #define COP12_FMT (cop12_fmt[mips_opts.micromips])
5254 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
5255 #define LUI_FMT (lui_fmt[mips_opts.micromips])
5256 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
5257 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
5258 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
5259 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
5260
5261 /* Read a macro's relocation codes from *ARGS and store them in *R.
5262    The first argument in *ARGS will be either the code for a single
5263    relocation or -1 followed by the three codes that make up a
5264    composite relocation.  */
5265
5266 static void
5267 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
5268 {
5269   int i, next;
5270
5271   next = va_arg (*args, int);
5272   if (next >= 0)
5273     r[0] = (bfd_reloc_code_real_type) next;
5274   else
5275     {
5276       for (i = 0; i < 3; i++)
5277         r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
5278       /* This function is only used for 16-bit relocation fields.
5279          To make the macro code simpler, treat an unrelocated value
5280          in the same way as BFD_RELOC_LO16.  */
5281       if (r[0] == BFD_RELOC_UNUSED)
5282         r[0] = BFD_RELOC_LO16;
5283     }
5284 }
5285
5286 /* Build an instruction created by a macro expansion.  This is passed
5287    a pointer to the count of instructions created so far, an
5288    expression, the name of the instruction to build, an operand format
5289    string, and corresponding arguments.  */
5290
5291 static void
5292 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
5293 {
5294   const struct mips_opcode *mo = NULL;
5295   bfd_reloc_code_real_type r[3];
5296   const struct mips_opcode *amo;
5297   struct hash_control *hash;
5298   struct mips_cl_insn insn;
5299   va_list args;
5300
5301   va_start (args, fmt);
5302
5303   if (mips_opts.mips16)
5304     {
5305       mips16_macro_build (ep, name, fmt, &args);
5306       va_end (args);
5307       return;
5308     }
5309
5310   r[0] = BFD_RELOC_UNUSED;
5311   r[1] = BFD_RELOC_UNUSED;
5312   r[2] = BFD_RELOC_UNUSED;
5313   hash = mips_opts.micromips ? micromips_op_hash : op_hash;
5314   amo = (struct mips_opcode *) hash_find (hash, name);
5315   gas_assert (amo);
5316   gas_assert (strcmp (name, amo->name) == 0);
5317
5318   do
5319     {
5320       /* Search until we get a match for NAME.  It is assumed here that
5321          macros will never generate MDMX, MIPS-3D, or MT instructions.
5322          We try to match an instruction that fulfils the branch delay
5323          slot instruction length requirement (if any) of the previous
5324          instruction.  While doing this we record the first instruction
5325          seen that matches all the other conditions and use it anyway
5326          if the requirement cannot be met; we will issue an appropriate
5327          warning later on.  */
5328       if (strcmp (fmt, amo->args) == 0
5329           && amo->pinfo != INSN_MACRO
5330           && is_opcode_valid (amo)
5331           && is_size_valid (amo))
5332         {
5333           if (is_delay_slot_valid (amo))
5334             {
5335               mo = amo;
5336               break;
5337             }
5338           else if (!mo)
5339             mo = amo;
5340         }
5341
5342       ++amo;
5343       gas_assert (amo->name);
5344     }
5345   while (strcmp (name, amo->name) == 0);
5346
5347   gas_assert (mo);
5348   create_insn (&insn, mo);
5349   for (;;)
5350     {
5351       switch (*fmt++)
5352         {
5353         case '\0':
5354           break;
5355
5356         case ',':
5357         case '(':
5358         case ')':
5359           continue;
5360
5361         case '+':
5362           switch (*fmt++)
5363             {
5364             case 'A':
5365             case 'E':
5366               INSERT_OPERAND (mips_opts.micromips,
5367                               EXTLSB, insn, va_arg (args, int));
5368               continue;
5369
5370             case 'B':
5371             case 'F':
5372               /* Note that in the macro case, these arguments are already
5373                  in MSB form.  (When handling the instruction in the
5374                  non-macro case, these arguments are sizes from which
5375                  MSB values must be calculated.)  */
5376               INSERT_OPERAND (mips_opts.micromips,
5377                               INSMSB, insn, va_arg (args, int));
5378               continue;
5379
5380             case 'J':
5381               gas_assert (!mips_opts.micromips);
5382               INSERT_OPERAND (0, CODE10, insn, va_arg (args, int));
5383               continue;
5384
5385             case 'C':
5386             case 'G':
5387             case 'H':
5388               /* Note that in the macro case, these arguments are already
5389                  in MSBD form.  (When handling the instruction in the
5390                  non-macro case, these arguments are sizes from which
5391                  MSBD values must be calculated.)  */
5392               INSERT_OPERAND (mips_opts.micromips,
5393                               EXTMSBD, insn, va_arg (args, int));
5394               continue;
5395
5396             case 'Q':
5397               gas_assert (!mips_opts.micromips);
5398               INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
5399               continue;
5400
5401             case 'j':
5402               INSERT_OPERAND (mips_opts.micromips, EVAOFFSET, insn, va_arg (args, int));
5403               continue;
5404
5405             default:
5406               abort ();
5407             }
5408           continue;
5409
5410         case '2':
5411           INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
5412           continue;
5413
5414         case 'n':
5415           gas_assert (mips_opts.micromips);
5416         case 't':
5417         case 'w':
5418         case 'E':
5419           INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
5420           continue;
5421
5422         case 'c':
5423           INSERT_OPERAND (mips_opts.micromips, CODE, insn, va_arg (args, int));
5424           continue;
5425
5426         case 'W':
5427           gas_assert (!mips_opts.micromips);
5428         case 'T':
5429           INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
5430           continue;
5431
5432         case 'G':
5433           if (mips_opts.micromips)
5434             INSERT_OPERAND (1, RS, insn, va_arg (args, int));
5435           else
5436             INSERT_OPERAND (0, RD, insn, va_arg (args, int));
5437           continue;
5438
5439         case 'K':
5440           gas_assert (!mips_opts.micromips);
5441         case 'd':
5442           INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
5443           continue;
5444
5445         case 'U':
5446           gas_assert (!mips_opts.micromips);
5447           {
5448             int tmp = va_arg (args, int);
5449
5450             INSERT_OPERAND (0, RT, insn, tmp);
5451             INSERT_OPERAND (0, RD, insn, tmp);
5452           }
5453           continue;
5454
5455         case 'V':
5456         case 'S':
5457           gas_assert (!mips_opts.micromips);
5458           INSERT_OPERAND (0, FS, insn, va_arg (args, int));
5459           continue;
5460
5461         case 'z':
5462           continue;
5463
5464         case '<':
5465           INSERT_OPERAND (mips_opts.micromips,
5466                           SHAMT, insn, va_arg (args, int));
5467           continue;
5468
5469         case 'D':
5470           gas_assert (!mips_opts.micromips);
5471           INSERT_OPERAND (0, FD, insn, va_arg (args, int));
5472           continue;
5473
5474         case 'B':
5475           gas_assert (!mips_opts.micromips);
5476           INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
5477           continue;
5478
5479         case 'J':
5480           gas_assert (!mips_opts.micromips);
5481           INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
5482           continue;
5483
5484         case 'q':
5485           gas_assert (!mips_opts.micromips);
5486           INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
5487           continue;
5488
5489         case 'b':
5490         case 's':
5491         case 'r':
5492         case 'v':
5493           INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
5494           continue;
5495
5496         case 'i':
5497         case 'j':
5498           macro_read_relocs (&args, r);
5499           gas_assert (*r == BFD_RELOC_GPREL16
5500                       || *r == BFD_RELOC_MIPS_HIGHER
5501                       || *r == BFD_RELOC_HI16_S
5502                       || *r == BFD_RELOC_LO16
5503                       || *r == BFD_RELOC_MIPS_GOT_OFST);
5504           continue;
5505
5506         case 'o':
5507           macro_read_relocs (&args, r);
5508           continue;
5509
5510         case 'u':
5511           macro_read_relocs (&args, r);
5512           gas_assert (ep != NULL
5513                       && (ep->X_op == O_constant
5514                           || (ep->X_op == O_symbol
5515                               && (*r == BFD_RELOC_MIPS_HIGHEST
5516                                   || *r == BFD_RELOC_HI16_S
5517                                   || *r == BFD_RELOC_HI16
5518                                   || *r == BFD_RELOC_GPREL16
5519                                   || *r == BFD_RELOC_MIPS_GOT_HI16
5520                                   || *r == BFD_RELOC_MIPS_CALL_HI16))));
5521           continue;
5522
5523         case 'p':
5524           gas_assert (ep != NULL);
5525
5526           /*
5527            * This allows macro() to pass an immediate expression for
5528            * creating short branches without creating a symbol.
5529            *
5530            * We don't allow branch relaxation for these branches, as
5531            * they should only appear in ".set nomacro" anyway.
5532            */
5533           if (ep->X_op == O_constant)
5534             {
5535               /* For microMIPS we always use relocations for branches.
5536                  So we should not resolve immediate values.  */
5537               gas_assert (!mips_opts.micromips);
5538
5539               if ((ep->X_add_number & 3) != 0)
5540                 as_bad (_("branch to misaligned address (0x%lx)"),
5541                         (unsigned long) ep->X_add_number);
5542               if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5543                 as_bad (_("branch address range overflow (0x%lx)"),
5544                         (unsigned long) ep->X_add_number);
5545               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5546               ep = NULL;
5547             }
5548           else
5549             *r = BFD_RELOC_16_PCREL_S2;
5550           continue;
5551
5552         case 'a':
5553           gas_assert (ep != NULL);
5554           *r = BFD_RELOC_MIPS_JMP;
5555           continue;
5556
5557         case 'C':
5558           gas_assert (!mips_opts.micromips);
5559           INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
5560           continue;
5561
5562         case 'k':
5563           INSERT_OPERAND (mips_opts.micromips,
5564                           CACHE, insn, va_arg (args, unsigned long));
5565           continue;
5566
5567         case '|':
5568           gas_assert (mips_opts.micromips);
5569           INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5570           continue;
5571
5572         case '.':
5573           gas_assert (mips_opts.micromips);
5574           INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5575           continue;
5576
5577         case '\\':
5578           INSERT_OPERAND (mips_opts.micromips,
5579                           3BITPOS, insn, va_arg (args, unsigned int));
5580           continue;
5581
5582         case '~':
5583           INSERT_OPERAND (mips_opts.micromips,
5584                           OFFSET12, insn, va_arg (args, unsigned long));
5585           continue;
5586
5587         case 'N':
5588           gas_assert (mips_opts.micromips);
5589           INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5590           continue;
5591
5592         case 'm':       /* Opcode extension character.  */
5593           gas_assert (mips_opts.micromips);
5594           switch (*fmt++)
5595             {
5596             case 'j':
5597               INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5598               break;
5599
5600             case 'p':
5601               INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5602               break;
5603
5604             case 'F':
5605               INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5606               break;
5607
5608             default:
5609               abort ();
5610             }
5611           continue;
5612
5613         default:
5614           abort ();
5615         }
5616       break;
5617     }
5618   va_end (args);
5619   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5620
5621   append_insn (&insn, ep, r, TRUE);
5622 }
5623
5624 static void
5625 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
5626                     va_list *args)
5627 {
5628   struct mips_opcode *mo;
5629   struct mips_cl_insn insn;
5630   bfd_reloc_code_real_type r[3]
5631     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5632
5633   mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
5634   gas_assert (mo);
5635   gas_assert (strcmp (name, mo->name) == 0);
5636
5637   while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
5638     {
5639       ++mo;
5640       gas_assert (mo->name);
5641       gas_assert (strcmp (name, mo->name) == 0);
5642     }
5643
5644   create_insn (&insn, mo);
5645   for (;;)
5646     {
5647       int c;
5648
5649       c = *fmt++;
5650       switch (c)
5651         {
5652         case '\0':
5653           break;
5654
5655         case ',':
5656         case '(':
5657         case ')':
5658           continue;
5659
5660         case 'y':
5661         case 'w':
5662           MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
5663           continue;
5664
5665         case 'x':
5666         case 'v':
5667           MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
5668           continue;
5669
5670         case 'z':
5671           MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
5672           continue;
5673
5674         case 'Z':
5675           MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
5676           continue;
5677
5678         case '0':
5679         case 'S':
5680         case 'P':
5681         case 'R':
5682           continue;
5683
5684         case 'X':
5685           MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
5686           continue;
5687
5688         case 'Y':
5689           {
5690             int regno;
5691
5692             regno = va_arg (*args, int);
5693             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
5694             MIPS16_INSERT_OPERAND (REG32R, insn, regno);
5695           }
5696           continue;
5697
5698         case '<':
5699         case '>':
5700         case '4':
5701         case '5':
5702         case 'H':
5703         case 'W':
5704         case 'D':
5705         case 'j':
5706         case '8':
5707         case 'V':
5708         case 'C':
5709         case 'U':
5710         case 'k':
5711         case 'K':
5712         case 'p':
5713         case 'q':
5714           {
5715             offsetT value;
5716
5717             gas_assert (ep != NULL);
5718
5719             if (ep->X_op != O_constant)
5720               *r = (int) BFD_RELOC_UNUSED + c;
5721             else if (calculate_reloc (*r, ep->X_add_number, &value))
5722               {
5723                 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
5724                 ep = NULL;
5725                 *r = BFD_RELOC_UNUSED;
5726               }
5727           }
5728           continue;
5729
5730         case '6':
5731           MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
5732           continue;
5733         }
5734
5735       break;
5736     }
5737
5738   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5739
5740   append_insn (&insn, ep, r, TRUE);
5741 }
5742
5743 /*
5744  * Sign-extend 32-bit mode constants that have bit 31 set and all
5745  * higher bits unset.
5746  */
5747 static void
5748 normalize_constant_expr (expressionS *ex)
5749 {
5750   if (ex->X_op == O_constant
5751       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5752     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5753                         - 0x80000000);
5754 }
5755
5756 /*
5757  * Sign-extend 32-bit mode address offsets that have bit 31 set and
5758  * all higher bits unset.
5759  */
5760 static void
5761 normalize_address_expr (expressionS *ex)
5762 {
5763   if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5764         || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5765       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5766     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5767                         - 0x80000000);
5768 }
5769
5770 /*
5771  * Generate a "jalr" instruction with a relocation hint to the called
5772  * function.  This occurs in NewABI PIC code.
5773  */
5774 static void
5775 macro_build_jalr (expressionS *ep, int cprestore)
5776 {
5777   static const bfd_reloc_code_real_type jalr_relocs[2]
5778     = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5779   bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5780   const char *jalr;
5781   char *f = NULL;
5782
5783   if (MIPS_JALR_HINT_P (ep))
5784     {
5785       frag_grow (8);
5786       f = frag_more (0);
5787     }
5788   if (mips_opts.micromips)
5789     {
5790       jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
5791               ? "jalr" : "jalrs");
5792       if (MIPS_JALR_HINT_P (ep)
5793           || mips_opts.insn32
5794           || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
5795         macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5796       else
5797         macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5798     }
5799   else
5800     macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
5801   if (MIPS_JALR_HINT_P (ep))
5802     fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
5803 }
5804
5805 /*
5806  * Generate a "lui" instruction.
5807  */
5808 static void
5809 macro_build_lui (expressionS *ep, int regnum)
5810 {
5811   gas_assert (! mips_opts.mips16);
5812
5813   if (ep->X_op != O_constant)
5814     {
5815       gas_assert (ep->X_op == O_symbol);
5816       /* _gp_disp is a special case, used from s_cpload.
5817          __gnu_local_gp is used if mips_no_shared.  */
5818       gas_assert (mips_pic == NO_PIC
5819               || (! HAVE_NEWABI
5820                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5821               || (! mips_in_shared
5822                   && strcmp (S_GET_NAME (ep->X_add_symbol),
5823                              "__gnu_local_gp") == 0));
5824     }
5825
5826   macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
5827 }
5828
5829 /* Generate a sequence of instructions to do a load or store from a constant
5830    offset off of a base register (breg) into/from a target register (treg),
5831    using AT if necessary.  */
5832 static void
5833 macro_build_ldst_constoffset (expressionS *ep, const char *op,
5834                               int treg, int breg, int dbl)
5835 {
5836   gas_assert (ep->X_op == O_constant);
5837
5838   /* Sign-extending 32-bit constants makes their handling easier.  */
5839   if (!dbl)
5840     normalize_constant_expr (ep);
5841
5842   /* Right now, this routine can only handle signed 32-bit constants.  */
5843   if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
5844     as_warn (_("operand overflow"));
5845
5846   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5847     {
5848       /* Signed 16-bit offset will fit in the op.  Easy!  */
5849       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
5850     }
5851   else
5852     {
5853       /* 32-bit offset, need multiple instructions and AT, like:
5854            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
5855            addu     $tempreg,$tempreg,$breg
5856            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
5857          to handle the complete offset.  */
5858       macro_build_lui (ep, AT);
5859       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5860       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
5861
5862       if (!mips_opts.at)
5863         as_bad (_("Macro used $at after \".set noat\""));
5864     }
5865 }
5866
5867 /*                      set_at()
5868  * Generates code to set the $at register to true (one)
5869  * if reg is less than the immediate expression.
5870  */
5871 static void
5872 set_at (int reg, int unsignedp)
5873 {
5874   if (imm_expr.X_op == O_constant
5875       && imm_expr.X_add_number >= -0x8000
5876       && imm_expr.X_add_number < 0x8000)
5877     macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5878                  AT, reg, BFD_RELOC_LO16);
5879   else
5880     {
5881       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5882       macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
5883     }
5884 }
5885
5886 /* Warn if an expression is not a constant.  */
5887
5888 static void
5889 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
5890 {
5891   if (ex->X_op == O_big)
5892     as_bad (_("unsupported large constant"));
5893   else if (ex->X_op != O_constant)
5894     as_bad (_("Instruction %s requires absolute expression"),
5895             ip->insn_mo->name);
5896
5897   if (HAVE_32BIT_GPRS)
5898     normalize_constant_expr (ex);
5899 }
5900
5901 /* Count the leading zeroes by performing a binary chop. This is a
5902    bulky bit of source, but performance is a LOT better for the
5903    majority of values than a simple loop to count the bits:
5904        for (lcnt = 0; (lcnt < 32); lcnt++)
5905          if ((v) & (1 << (31 - lcnt)))
5906            break;
5907   However it is not code size friendly, and the gain will drop a bit
5908   on certain cached systems.
5909 */
5910 #define COUNT_TOP_ZEROES(v)             \
5911   (((v) & ~0xffff) == 0                 \
5912    ? ((v) & ~0xff) == 0                 \
5913      ? ((v) & ~0xf) == 0                \
5914        ? ((v) & ~0x3) == 0              \
5915          ? ((v) & ~0x1) == 0            \
5916            ? !(v)                       \
5917              ? 32                       \
5918              : 31                       \
5919            : 30                         \
5920          : ((v) & ~0x7) == 0            \
5921            ? 29                         \
5922            : 28                         \
5923        : ((v) & ~0x3f) == 0             \
5924          ? ((v) & ~0x1f) == 0           \
5925            ? 27                         \
5926            : 26                         \
5927          : ((v) & ~0x7f) == 0           \
5928            ? 25                         \
5929            : 24                         \
5930      : ((v) & ~0xfff) == 0              \
5931        ? ((v) & ~0x3ff) == 0            \
5932          ? ((v) & ~0x1ff) == 0          \
5933            ? 23                         \
5934            : 22                         \
5935          : ((v) & ~0x7ff) == 0          \
5936            ? 21                         \
5937            : 20                         \
5938        : ((v) & ~0x3fff) == 0           \
5939          ? ((v) & ~0x1fff) == 0         \
5940            ? 19                         \
5941            : 18                         \
5942          : ((v) & ~0x7fff) == 0         \
5943            ? 17                         \
5944            : 16                         \
5945    : ((v) & ~0xffffff) == 0             \
5946      ? ((v) & ~0xfffff) == 0            \
5947        ? ((v) & ~0x3ffff) == 0          \
5948          ? ((v) & ~0x1ffff) == 0        \
5949            ? 15                         \
5950            : 14                         \
5951          : ((v) & ~0x7ffff) == 0        \
5952            ? 13                         \
5953            : 12                         \
5954        : ((v) & ~0x3fffff) == 0         \
5955          ? ((v) & ~0x1fffff) == 0       \
5956            ? 11                         \
5957            : 10                         \
5958          : ((v) & ~0x7fffff) == 0       \
5959            ? 9                          \
5960            : 8                          \
5961      : ((v) & ~0xfffffff) == 0          \
5962        ? ((v) & ~0x3ffffff) == 0        \
5963          ? ((v) & ~0x1ffffff) == 0      \
5964            ? 7                          \
5965            : 6                          \
5966          : ((v) & ~0x7ffffff) == 0      \
5967            ? 5                          \
5968            : 4                          \
5969        : ((v) & ~0x3fffffff) == 0       \
5970          ? ((v) & ~0x1fffffff) == 0     \
5971            ? 3                          \
5972            : 2                          \
5973          : ((v) & ~0x7fffffff) == 0     \
5974            ? 1                          \
5975            : 0)
5976
5977 /*                      load_register()
5978  *  This routine generates the least number of instructions necessary to load
5979  *  an absolute expression value into a register.
5980  */
5981 static void
5982 load_register (int reg, expressionS *ep, int dbl)
5983 {
5984   int freg;
5985   expressionS hi32, lo32;
5986
5987   if (ep->X_op != O_big)
5988     {
5989       gas_assert (ep->X_op == O_constant);
5990
5991       /* Sign-extending 32-bit constants makes their handling easier.  */
5992       if (!dbl)
5993         normalize_constant_expr (ep);
5994
5995       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
5996         {
5997           /* We can handle 16 bit signed values with an addiu to
5998              $zero.  No need to ever use daddiu here, since $zero and
5999              the result are always correct in 32 bit mode.  */
6000           macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
6001           return;
6002         }
6003       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
6004         {
6005           /* We can handle 16 bit unsigned values with an ori to
6006              $zero.  */
6007           macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
6008           return;
6009         }
6010       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
6011         {
6012           /* 32 bit values require an lui.  */
6013           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
6014           if ((ep->X_add_number & 0xffff) != 0)
6015             macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
6016           return;
6017         }
6018     }
6019
6020   /* The value is larger than 32 bits.  */
6021
6022   if (!dbl || HAVE_32BIT_GPRS)
6023     {
6024       char value[32];
6025
6026       sprintf_vma (value, ep->X_add_number);
6027       as_bad (_("Number (0x%s) larger than 32 bits"), value);
6028       macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
6029       return;
6030     }
6031
6032   if (ep->X_op != O_big)
6033     {
6034       hi32 = *ep;
6035       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
6036       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
6037       hi32.X_add_number &= 0xffffffff;
6038       lo32 = *ep;
6039       lo32.X_add_number &= 0xffffffff;
6040     }
6041   else
6042     {
6043       gas_assert (ep->X_add_number > 2);
6044       if (ep->X_add_number == 3)
6045         generic_bignum[3] = 0;
6046       else if (ep->X_add_number > 4)
6047         as_bad (_("Number larger than 64 bits"));
6048       lo32.X_op = O_constant;
6049       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
6050       hi32.X_op = O_constant;
6051       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
6052     }
6053
6054   if (hi32.X_add_number == 0)
6055     freg = 0;
6056   else
6057     {
6058       int shift, bit;
6059       unsigned long hi, lo;
6060
6061       if (hi32.X_add_number == (offsetT) 0xffffffff)
6062         {
6063           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
6064             {
6065               macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
6066               return;
6067             }
6068           if (lo32.X_add_number & 0x80000000)
6069             {
6070               macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
6071               if (lo32.X_add_number & 0xffff)
6072                 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
6073               return;
6074             }
6075         }
6076
6077       /* Check for 16bit shifted constant.  We know that hi32 is
6078          non-zero, so start the mask on the first bit of the hi32
6079          value.  */
6080       shift = 17;
6081       do
6082         {
6083           unsigned long himask, lomask;
6084
6085           if (shift < 32)
6086             {
6087               himask = 0xffff >> (32 - shift);
6088               lomask = (0xffff << shift) & 0xffffffff;
6089             }
6090           else
6091             {
6092               himask = 0xffff << (shift - 32);
6093               lomask = 0;
6094             }
6095           if ((hi32.X_add_number & ~(offsetT) himask) == 0
6096               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
6097             {
6098               expressionS tmp;
6099
6100               tmp.X_op = O_constant;
6101               if (shift < 32)
6102                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
6103                                     | (lo32.X_add_number >> shift));
6104               else
6105                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
6106               macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
6107               macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
6108                            reg, reg, (shift >= 32) ? shift - 32 : shift);
6109               return;
6110             }
6111           ++shift;
6112         }
6113       while (shift <= (64 - 16));
6114
6115       /* Find the bit number of the lowest one bit, and store the
6116          shifted value in hi/lo.  */
6117       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
6118       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
6119       if (lo != 0)
6120         {
6121           bit = 0;
6122           while ((lo & 1) == 0)
6123             {
6124               lo >>= 1;
6125               ++bit;
6126             }
6127           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
6128           hi >>= bit;
6129         }
6130       else
6131         {
6132           bit = 32;
6133           while ((hi & 1) == 0)
6134             {
6135               hi >>= 1;
6136               ++bit;
6137             }
6138           lo = hi;
6139           hi = 0;
6140         }
6141
6142       /* Optimize if the shifted value is a (power of 2) - 1.  */
6143       if ((hi == 0 && ((lo + 1) & lo) == 0)
6144           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
6145         {
6146           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
6147           if (shift != 0)
6148             {
6149               expressionS tmp;
6150
6151               /* This instruction will set the register to be all
6152                  ones.  */
6153               tmp.X_op = O_constant;
6154               tmp.X_add_number = (offsetT) -1;
6155               macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
6156               if (bit != 0)
6157                 {
6158                   bit += shift;
6159                   macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
6160                                reg, reg, (bit >= 32) ? bit - 32 : bit);
6161                 }
6162               macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
6163                            reg, reg, (shift >= 32) ? shift - 32 : shift);
6164               return;
6165             }
6166         }
6167
6168       /* Sign extend hi32 before calling load_register, because we can
6169          generally get better code when we load a sign extended value.  */
6170       if ((hi32.X_add_number & 0x80000000) != 0)
6171         hi32.X_add_number |= ~(offsetT) 0xffffffff;
6172       load_register (reg, &hi32, 0);
6173       freg = reg;
6174     }
6175   if ((lo32.X_add_number & 0xffff0000) == 0)
6176     {
6177       if (freg != 0)
6178         {
6179           macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
6180           freg = reg;
6181         }
6182     }
6183   else
6184     {
6185       expressionS mid16;
6186
6187       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
6188         {
6189           macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
6190           macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
6191           return;
6192         }
6193
6194       if (freg != 0)
6195         {
6196           macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
6197           freg = reg;
6198         }
6199       mid16 = lo32;
6200       mid16.X_add_number >>= 16;
6201       macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
6202       macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
6203       freg = reg;
6204     }
6205   if ((lo32.X_add_number & 0xffff) != 0)
6206     macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
6207 }
6208
6209 static inline void
6210 load_delay_nop (void)
6211 {
6212   if (!gpr_interlocks)
6213     macro_build (NULL, "nop", "");
6214 }
6215
6216 /* Load an address into a register.  */
6217
6218 static void
6219 load_address (int reg, expressionS *ep, int *used_at)
6220 {
6221   if (ep->X_op != O_constant
6222       && ep->X_op != O_symbol)
6223     {
6224       as_bad (_("expression too complex"));
6225       ep->X_op = O_constant;
6226     }
6227
6228   if (ep->X_op == O_constant)
6229     {
6230       load_register (reg, ep, HAVE_64BIT_ADDRESSES);
6231       return;
6232     }
6233
6234   if (mips_pic == NO_PIC)
6235     {
6236       /* If this is a reference to a GP relative symbol, we want
6237            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
6238          Otherwise we want
6239            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
6240            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
6241          If we have an addend, we always use the latter form.
6242
6243          With 64bit address space and a usable $at we want
6244            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
6245            lui          $at,<sym>               (BFD_RELOC_HI16_S)
6246            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
6247            daddiu       $at,<sym>               (BFD_RELOC_LO16)
6248            dsll32       $reg,0
6249            daddu        $reg,$reg,$at
6250
6251          If $at is already in use, we use a path which is suboptimal
6252          on superscalar processors.
6253            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
6254            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
6255            dsll         $reg,16
6256            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
6257            dsll         $reg,16
6258            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
6259
6260          For GP relative symbols in 64bit address space we can use
6261          the same sequence as in 32bit address space.  */
6262       if (HAVE_64BIT_SYMBOLS)
6263         {
6264           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6265               && !nopic_need_relax (ep->X_add_symbol, 1))
6266             {
6267               relax_start (ep->X_add_symbol);
6268               macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
6269                            mips_gp_register, BFD_RELOC_GPREL16);
6270               relax_switch ();
6271             }
6272
6273           if (*used_at == 0 && mips_opts.at)
6274             {
6275               macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
6276               macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
6277               macro_build (ep, "daddiu", "t,r,j", reg, reg,
6278                            BFD_RELOC_MIPS_HIGHER);
6279               macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
6280               macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
6281               macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
6282               *used_at = 1;
6283             }
6284           else
6285             {
6286               macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
6287               macro_build (ep, "daddiu", "t,r,j", reg, reg,
6288                            BFD_RELOC_MIPS_HIGHER);
6289               macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
6290               macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
6291               macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
6292               macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
6293             }
6294
6295           if (mips_relax.sequence)
6296             relax_end ();
6297         }
6298       else
6299         {
6300           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6301               && !nopic_need_relax (ep->X_add_symbol, 1))
6302             {
6303               relax_start (ep->X_add_symbol);
6304               macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
6305                            mips_gp_register, BFD_RELOC_GPREL16);
6306               relax_switch ();
6307             }
6308           macro_build_lui (ep, reg);
6309           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
6310                        reg, reg, BFD_RELOC_LO16);
6311           if (mips_relax.sequence)
6312             relax_end ();
6313         }
6314     }
6315   else if (!mips_big_got)
6316     {
6317       expressionS ex;
6318
6319       /* If this is a reference to an external symbol, we want
6320            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
6321          Otherwise we want
6322            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
6323            nop
6324            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
6325          If there is a constant, it must be added in after.
6326
6327          If we have NewABI, we want
6328            lw           $reg,<sym+cst>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
6329          unless we're referencing a global symbol with a non-zero
6330          offset, in which case cst must be added separately.  */
6331       if (HAVE_NEWABI)
6332         {
6333           if (ep->X_add_number)
6334             {
6335               ex.X_add_number = ep->X_add_number;
6336               ep->X_add_number = 0;
6337               relax_start (ep->X_add_symbol);
6338               macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6339                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
6340               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6341                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6342               ex.X_op = O_constant;
6343               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
6344                            reg, reg, BFD_RELOC_LO16);
6345               ep->X_add_number = ex.X_add_number;
6346               relax_switch ();
6347             }
6348           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6349                        BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
6350           if (mips_relax.sequence)
6351             relax_end ();
6352         }
6353       else
6354         {
6355           ex.X_add_number = ep->X_add_number;
6356           ep->X_add_number = 0;
6357           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6358                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
6359           load_delay_nop ();
6360           relax_start (ep->X_add_symbol);
6361           relax_switch ();
6362           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6363                        BFD_RELOC_LO16);
6364           relax_end ();
6365
6366           if (ex.X_add_number != 0)
6367             {
6368               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6369                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6370               ex.X_op = O_constant;
6371               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
6372                            reg, reg, BFD_RELOC_LO16);
6373             }
6374         }
6375     }
6376   else if (mips_big_got)
6377     {
6378       expressionS ex;
6379
6380       /* This is the large GOT case.  If this is a reference to an
6381          external symbol, we want
6382            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
6383            addu         $reg,$reg,$gp
6384            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
6385
6386          Otherwise, for a reference to a local symbol in old ABI, we want
6387            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
6388            nop
6389            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
6390          If there is a constant, it must be added in after.
6391
6392          In the NewABI, for local symbols, with or without offsets, we want:
6393            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
6394            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
6395       */
6396       if (HAVE_NEWABI)
6397         {
6398           ex.X_add_number = ep->X_add_number;
6399           ep->X_add_number = 0;
6400           relax_start (ep->X_add_symbol);
6401           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
6402           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6403                        reg, reg, mips_gp_register);
6404           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6405                        reg, BFD_RELOC_MIPS_GOT_LO16, reg);
6406           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6407             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6408           else if (ex.X_add_number)
6409             {
6410               ex.X_op = O_constant;
6411               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6412                            BFD_RELOC_LO16);
6413             }
6414
6415           ep->X_add_number = ex.X_add_number;
6416           relax_switch ();
6417           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6418                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6419           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6420                        BFD_RELOC_MIPS_GOT_OFST);
6421           relax_end ();
6422         }
6423       else
6424         {
6425           ex.X_add_number = ep->X_add_number;
6426           ep->X_add_number = 0;
6427           relax_start (ep->X_add_symbol);
6428           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
6429           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6430                        reg, reg, mips_gp_register);
6431           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6432                        reg, BFD_RELOC_MIPS_GOT_LO16, reg);
6433           relax_switch ();
6434           if (reg_needs_delay (mips_gp_register))
6435             {
6436               /* We need a nop before loading from $gp.  This special
6437                  check is required because the lui which starts the main
6438                  instruction stream does not refer to $gp, and so will not
6439                  insert the nop which may be required.  */
6440               macro_build (NULL, "nop", "");
6441             }
6442           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6443                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
6444           load_delay_nop ();
6445           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6446                        BFD_RELOC_LO16);
6447           relax_end ();
6448
6449           if (ex.X_add_number != 0)
6450             {
6451               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6452                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6453               ex.X_op = O_constant;
6454               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6455                            BFD_RELOC_LO16);
6456             }
6457         }
6458     }
6459   else
6460     abort ();
6461
6462   if (!mips_opts.at && *used_at == 1)
6463     as_bad (_("Macro used $at after \".set noat\""));
6464 }
6465
6466 /* Move the contents of register SOURCE into register DEST.  */
6467
6468 static void
6469 move_register (int dest, int source)
6470 {
6471   /* Prefer to use a 16-bit microMIPS instruction unless the previous
6472      instruction specifically requires a 32-bit one.  */
6473   if (mips_opts.micromips
6474       && !mips_opts.insn32
6475       && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
6476     macro_build (NULL, "move", "mp,mj", dest, source);
6477   else
6478     macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6479                  dest, source, 0);
6480 }
6481
6482 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
6483    LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6484    The two alternatives are:
6485
6486    Global symbol                Local sybmol
6487    -------------                ------------
6488    lw DEST,%got(SYMBOL)         lw DEST,%got(SYMBOL + OFFSET)
6489    ...                          ...
6490    addiu DEST,DEST,OFFSET       addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6491
6492    load_got_offset emits the first instruction and add_got_offset
6493    emits the second for a 16-bit offset or add_got_offset_hilo emits
6494    a sequence to add a 32-bit offset using a scratch register.  */
6495
6496 static void
6497 load_got_offset (int dest, expressionS *local)
6498 {
6499   expressionS global;
6500
6501   global = *local;
6502   global.X_add_number = 0;
6503
6504   relax_start (local->X_add_symbol);
6505   macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6506                BFD_RELOC_MIPS_GOT16, mips_gp_register);
6507   relax_switch ();
6508   macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6509                BFD_RELOC_MIPS_GOT16, mips_gp_register);
6510   relax_end ();
6511 }
6512
6513 static void
6514 add_got_offset (int dest, expressionS *local)
6515 {
6516   expressionS global;
6517
6518   global.X_op = O_constant;
6519   global.X_op_symbol = NULL;
6520   global.X_add_symbol = NULL;
6521   global.X_add_number = local->X_add_number;
6522
6523   relax_start (local->X_add_symbol);
6524   macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
6525                dest, dest, BFD_RELOC_LO16);
6526   relax_switch ();
6527   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
6528   relax_end ();
6529 }
6530
6531 static void
6532 add_got_offset_hilo (int dest, expressionS *local, int tmp)
6533 {
6534   expressionS global;
6535   int hold_mips_optimize;
6536
6537   global.X_op = O_constant;
6538   global.X_op_symbol = NULL;
6539   global.X_add_symbol = NULL;
6540   global.X_add_number = local->X_add_number;
6541
6542   relax_start (local->X_add_symbol);
6543   load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6544   relax_switch ();
6545   /* Set mips_optimize around the lui instruction to avoid
6546      inserting an unnecessary nop after the lw.  */
6547   hold_mips_optimize = mips_optimize;
6548   mips_optimize = 2;
6549   macro_build_lui (&global, tmp);
6550   mips_optimize = hold_mips_optimize;
6551   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6552   relax_end ();
6553
6554   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6555 }
6556
6557 /* Emit a sequence of instructions to emulate a branch likely operation.
6558    BR is an ordinary branch corresponding to one to be emulated.  BRNEG
6559    is its complementing branch with the original condition negated.
6560    CALL is set if the original branch specified the link operation.
6561    EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6562
6563    Code like this is produced in the noreorder mode:
6564
6565         BRNEG   <args>, 1f
6566          nop
6567         b       <sym>
6568          delay slot (executed only if branch taken)
6569     1:
6570
6571    or, if CALL is set:
6572
6573         BRNEG   <args>, 1f
6574          nop
6575         bal     <sym>
6576          delay slot (executed only if branch taken)
6577     1:
6578
6579    In the reorder mode the delay slot would be filled with a nop anyway,
6580    so code produced is simply:
6581
6582         BR      <args>, <sym>
6583          nop
6584
6585    This function is used when producing code for the microMIPS ASE that
6586    does not implement branch likely instructions in hardware.  */
6587
6588 static void
6589 macro_build_branch_likely (const char *br, const char *brneg,
6590                            int call, expressionS *ep, const char *fmt,
6591                            unsigned int sreg, unsigned int treg)
6592 {
6593   int noreorder = mips_opts.noreorder;
6594   expressionS expr1;
6595
6596   gas_assert (mips_opts.micromips);
6597   start_noreorder ();
6598   if (noreorder)
6599     {
6600       micromips_label_expr (&expr1);
6601       macro_build (&expr1, brneg, fmt, sreg, treg);
6602       macro_build (NULL, "nop", "");
6603       macro_build (ep, call ? "bal" : "b", "p");
6604
6605       /* Set to true so that append_insn adds a label.  */
6606       emit_branch_likely_macro = TRUE;
6607     }
6608   else
6609     {
6610       macro_build (ep, br, fmt, sreg, treg);
6611       macro_build (NULL, "nop", "");
6612     }
6613   end_noreorder ();
6614 }
6615
6616 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6617    the condition code tested.  EP specifies the branch target.  */
6618
6619 static void
6620 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6621 {
6622   const int call = 0;
6623   const char *brneg;
6624   const char *br;
6625
6626   switch (type)
6627     {
6628     case M_BC1FL:
6629       br = "bc1f";
6630       brneg = "bc1t";
6631       break;
6632     case M_BC1TL:
6633       br = "bc1t";
6634       brneg = "bc1f";
6635       break;
6636     case M_BC2FL:
6637       br = "bc2f";
6638       brneg = "bc2t";
6639       break;
6640     case M_BC2TL:
6641       br = "bc2t";
6642       brneg = "bc2f";
6643       break;
6644     default:
6645       abort ();
6646     }
6647   macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6648 }
6649
6650 /* Emit a two-argument branch macro specified by TYPE, using SREG as
6651    the register tested.  EP specifies the branch target.  */
6652
6653 static void
6654 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6655 {
6656   const char *brneg = NULL;
6657   const char *br;
6658   int call = 0;
6659
6660   switch (type)
6661     {
6662     case M_BGEZ:
6663       br = "bgez";
6664       break;
6665     case M_BGEZL:
6666       br = mips_opts.micromips ? "bgez" : "bgezl";
6667       brneg = "bltz";
6668       break;
6669     case M_BGEZALL:
6670       gas_assert (mips_opts.micromips);
6671       br = mips_opts.insn32 ? "bgezal" : "bgezals";
6672       brneg = "bltz";
6673       call = 1;
6674       break;
6675     case M_BGTZ:
6676       br = "bgtz";
6677       break;
6678     case M_BGTZL:
6679       br = mips_opts.micromips ? "bgtz" : "bgtzl";
6680       brneg = "blez";
6681       break;
6682     case M_BLEZ:
6683       br = "blez";
6684       break;
6685     case M_BLEZL:
6686       br = mips_opts.micromips ? "blez" : "blezl";
6687       brneg = "bgtz";
6688       break;
6689     case M_BLTZ:
6690       br = "bltz";
6691       break;
6692     case M_BLTZL:
6693       br = mips_opts.micromips ? "bltz" : "bltzl";
6694       brneg = "bgez";
6695       break;
6696     case M_BLTZALL:
6697       gas_assert (mips_opts.micromips);
6698       br = mips_opts.insn32 ? "bltzal" : "bltzals";
6699       brneg = "bgez";
6700       call = 1;
6701       break;
6702     default:
6703       abort ();
6704     }
6705   if (mips_opts.micromips && brneg)
6706     macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6707   else
6708     macro_build (ep, br, "s,p", sreg);
6709 }
6710
6711 /* Emit a three-argument branch macro specified by TYPE, using SREG and
6712    TREG as the registers tested.  EP specifies the branch target.  */
6713
6714 static void
6715 macro_build_branch_rsrt (int type, expressionS *ep,
6716                          unsigned int sreg, unsigned int treg)
6717 {
6718   const char *brneg = NULL;
6719   const int call = 0;
6720   const char *br;
6721
6722   switch (type)
6723     {
6724     case M_BEQ:
6725     case M_BEQ_I:
6726       br = "beq";
6727       break;
6728     case M_BEQL:
6729     case M_BEQL_I:
6730       br = mips_opts.micromips ? "beq" : "beql";
6731       brneg = "bne";
6732       break;
6733     case M_BNE:
6734     case M_BNE_I:
6735       br = "bne";
6736       break;
6737     case M_BNEL:
6738     case M_BNEL_I:
6739       br = mips_opts.micromips ? "bne" : "bnel";
6740       brneg = "beq";
6741       break;
6742     default:
6743       abort ();
6744     }
6745   if (mips_opts.micromips && brneg)
6746     macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6747   else
6748     macro_build (ep, br, "s,t,p", sreg, treg);
6749 }
6750
6751 /* Return the high part that should be loaded in order to make the low
6752    part of VALUE accessible using an offset of OFFBITS bits.  */
6753
6754 static offsetT
6755 offset_high_part (offsetT value, unsigned int offbits)
6756 {
6757   offsetT bias;
6758   addressT low_mask;
6759
6760   if (offbits == 0)
6761     return value;
6762   bias = 1 << (offbits - 1);
6763   low_mask = bias * 2 - 1;
6764   return (value + bias) & ~low_mask;
6765 }
6766
6767 /* Return true if the value stored in offset_expr and offset_reloc
6768    fits into a signed offset of OFFBITS bits.  RANGE is the maximum
6769    amount that the caller wants to add without inducing overflow
6770    and ALIGN is the known alignment of the value in bytes.  */
6771
6772 static bfd_boolean
6773 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
6774 {
6775   if (offbits == 16)
6776     {
6777       /* Accept any relocation operator if overflow isn't a concern.  */
6778       if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
6779         return TRUE;
6780
6781       /* These relocations are guaranteed not to overflow in correct links.  */
6782       if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
6783           || gprel16_reloc_p (*offset_reloc))
6784         return TRUE;
6785     }
6786   if (offset_expr.X_op == O_constant
6787       && offset_high_part (offset_expr.X_add_number, offbits) == 0
6788       && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
6789     return TRUE;
6790   return FALSE;
6791 }
6792
6793 /*
6794  *                      Build macros
6795  *   This routine implements the seemingly endless macro or synthesized
6796  * instructions and addressing modes in the mips assembly language. Many
6797  * of these macros are simple and are similar to each other. These could
6798  * probably be handled by some kind of table or grammar approach instead of
6799  * this verbose method. Others are not simple macros but are more like
6800  * optimizing code generation.
6801  *   One interesting optimization is when several store macros appear
6802  * consecutively that would load AT with the upper half of the same address.
6803  * The ensuing load upper instructions are ommited. This implies some kind
6804  * of global optimization. We currently only optimize within a single macro.
6805  *   For many of the load and store macros if the address is specified as a
6806  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6807  * first load register 'at' with zero and use it as the base register. The
6808  * mips assembler simply uses register $zero. Just one tiny optimization
6809  * we're missing.
6810  */
6811 static void
6812 macro (struct mips_cl_insn *ip, char *str)
6813 {
6814   unsigned int treg, sreg, dreg, breg;
6815   unsigned int tempreg;
6816   int mask;
6817   int used_at = 0;
6818   expressionS label_expr;
6819   expressionS expr1;
6820   expressionS *ep;
6821   const char *s;
6822   const char *s2;
6823   const char *fmt;
6824   int likely = 0;
6825   int coproc = 0;
6826   int offbits = 16;
6827   int call = 0;
6828   int jals = 0;
6829   int dbl = 0;
6830   int imm = 0;
6831   int ust = 0;
6832   int lp = 0;
6833   bfd_boolean large_offset;
6834   int off;
6835   int hold_mips_optimize;
6836   unsigned int align;
6837
6838   gas_assert (! mips_opts.mips16);
6839
6840   treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6841   dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6842   sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
6843   mask = ip->insn_mo->mask;
6844
6845   label_expr.X_op = O_constant;
6846   label_expr.X_op_symbol = NULL;
6847   label_expr.X_add_symbol = NULL;
6848   label_expr.X_add_number = 0;
6849
6850   expr1.X_op = O_constant;
6851   expr1.X_op_symbol = NULL;
6852   expr1.X_add_symbol = NULL;
6853   expr1.X_add_number = 1;
6854   align = 1;
6855
6856   switch (mask)
6857     {
6858     case M_DABS:
6859       dbl = 1;
6860     case M_ABS:
6861       /*    bgez    $a0,1f
6862             move    v0,$a0
6863             sub     v0,$zero,$a0
6864          1:
6865        */
6866
6867       start_noreorder ();
6868
6869       if (mips_opts.micromips)
6870         micromips_label_expr (&label_expr);
6871       else
6872         label_expr.X_add_number = 8;
6873       macro_build (&label_expr, "bgez", "s,p", sreg);
6874       if (dreg == sreg)
6875         macro_build (NULL, "nop", "");
6876       else
6877         move_register (dreg, sreg);
6878       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
6879       if (mips_opts.micromips)
6880         micromips_add_label ();
6881
6882       end_noreorder ();
6883       break;
6884
6885     case M_ADD_I:
6886       s = "addi";
6887       s2 = "add";
6888       goto do_addi;
6889     case M_ADDU_I:
6890       s = "addiu";
6891       s2 = "addu";
6892       goto do_addi;
6893     case M_DADD_I:
6894       dbl = 1;
6895       s = "daddi";
6896       s2 = "dadd";
6897       if (!mips_opts.micromips)
6898         goto do_addi;
6899       if (imm_expr.X_op == O_constant
6900           && imm_expr.X_add_number >= -0x200
6901           && imm_expr.X_add_number < 0x200)
6902         {
6903           macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6904           break;
6905         }
6906       goto do_addi_i;
6907     case M_DADDU_I:
6908       dbl = 1;
6909       s = "daddiu";
6910       s2 = "daddu";
6911     do_addi:
6912       if (imm_expr.X_op == O_constant
6913           && imm_expr.X_add_number >= -0x8000
6914           && imm_expr.X_add_number < 0x8000)
6915         {
6916           macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
6917           break;
6918         }
6919     do_addi_i:
6920       used_at = 1;
6921       load_register (AT, &imm_expr, dbl);
6922       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6923       break;
6924
6925     case M_AND_I:
6926       s = "andi";
6927       s2 = "and";
6928       goto do_bit;
6929     case M_OR_I:
6930       s = "ori";
6931       s2 = "or";
6932       goto do_bit;
6933     case M_NOR_I:
6934       s = "";
6935       s2 = "nor";
6936       goto do_bit;
6937     case M_XOR_I:
6938       s = "xori";
6939       s2 = "xor";
6940     do_bit:
6941       if (imm_expr.X_op == O_constant
6942           && imm_expr.X_add_number >= 0
6943           && imm_expr.X_add_number < 0x10000)
6944         {
6945           if (mask != M_NOR_I)
6946             macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
6947           else
6948             {
6949               macro_build (&imm_expr, "ori", "t,r,i",
6950                            treg, sreg, BFD_RELOC_LO16);
6951               macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
6952             }
6953           break;
6954         }
6955
6956       used_at = 1;
6957       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6958       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6959       break;
6960
6961     case M_BALIGN:
6962       switch (imm_expr.X_add_number)
6963         {
6964         case 0:
6965           macro_build (NULL, "nop", "");
6966           break;
6967         case 2:
6968           macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6969           break;
6970         case 1:
6971         case 3:
6972           macro_build (NULL, "balign", "t,s,2", treg, sreg,
6973                        (int) imm_expr.X_add_number);
6974           break;
6975         default:
6976           as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6977                   (unsigned long) imm_expr.X_add_number);
6978           break;
6979         }
6980       break;
6981
6982     case M_BC1FL:
6983     case M_BC1TL:
6984     case M_BC2FL:
6985     case M_BC2TL:
6986       gas_assert (mips_opts.micromips);
6987       macro_build_branch_ccl (mask, &offset_expr,
6988                               EXTRACT_OPERAND (1, BCC, *ip));
6989       break;
6990
6991     case M_BEQ_I:
6992     case M_BEQL_I:
6993     case M_BNE_I:
6994     case M_BNEL_I:
6995       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6996         treg = 0;
6997       else
6998         {
6999           treg = AT;
7000           used_at = 1;
7001           load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
7002         }
7003       /* Fall through.  */
7004     case M_BEQL:
7005     case M_BNEL:
7006       macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
7007       break;
7008
7009     case M_BGEL:
7010       likely = 1;
7011     case M_BGE:
7012       if (treg == 0)
7013         macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
7014       else if (sreg == 0)
7015         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
7016       else
7017         {
7018           used_at = 1;
7019           macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
7020           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7021                                    &offset_expr, AT, ZERO);
7022         }
7023       break;
7024
7025     case M_BGEZL:
7026     case M_BGEZALL:
7027     case M_BGTZL:
7028     case M_BLEZL:
7029     case M_BLTZL:
7030     case M_BLTZALL:
7031       macro_build_branch_rs (mask, &offset_expr, sreg);
7032       break;
7033
7034     case M_BGTL_I:
7035       likely = 1;
7036     case M_BGT_I:
7037       /* Check for > max integer.  */
7038       if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
7039         {
7040         do_false:
7041           /* Result is always false.  */
7042           if (! likely)
7043             macro_build (NULL, "nop", "");
7044           else
7045             macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
7046           break;
7047         }
7048       if (imm_expr.X_op != O_constant)
7049         as_bad (_("Unsupported large constant"));
7050       ++imm_expr.X_add_number;
7051       /* FALLTHROUGH */
7052     case M_BGE_I:
7053     case M_BGEL_I:
7054       if (mask == M_BGEL_I)
7055         likely = 1;
7056       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7057         {
7058           macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
7059                                  &offset_expr, sreg);
7060           break;
7061         }
7062       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7063         {
7064           macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
7065                                  &offset_expr, sreg);
7066           break;
7067         }
7068       if (imm_expr.X_op == O_constant && imm_expr.X_add_number <= GPR_SMIN)
7069         {
7070         do_true:
7071           /* result is always true */
7072           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
7073           macro_build (&offset_expr, "b", "p");
7074           break;
7075         }
7076       used_at = 1;
7077       set_at (sreg, 0);
7078       macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7079                                &offset_expr, AT, ZERO);
7080       break;
7081
7082     case M_BGEUL:
7083       likely = 1;
7084     case M_BGEU:
7085       if (treg == 0)
7086         goto do_true;
7087       else if (sreg == 0)
7088         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7089                                  &offset_expr, ZERO, treg);
7090       else
7091         {
7092           used_at = 1;
7093           macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
7094           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7095                                    &offset_expr, AT, ZERO);
7096         }
7097       break;
7098
7099     case M_BGTUL_I:
7100       likely = 1;
7101     case M_BGTU_I:
7102       if (sreg == 0
7103           || (HAVE_32BIT_GPRS
7104               && imm_expr.X_op == O_constant
7105               && imm_expr.X_add_number == -1))
7106         goto do_false;
7107       if (imm_expr.X_op != O_constant)
7108         as_bad (_("Unsupported large constant"));
7109       ++imm_expr.X_add_number;
7110       /* FALLTHROUGH */
7111     case M_BGEU_I:
7112     case M_BGEUL_I:
7113       if (mask == M_BGEUL_I)
7114         likely = 1;
7115       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7116         goto do_true;
7117       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7118         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7119                                  &offset_expr, sreg, ZERO);
7120       else
7121         {
7122           used_at = 1;
7123           set_at (sreg, 1);
7124           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7125                                    &offset_expr, AT, ZERO);
7126         }
7127       break;
7128
7129     case M_BGTL:
7130       likely = 1;
7131     case M_BGT:
7132       if (treg == 0)
7133         macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
7134       else if (sreg == 0)
7135         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
7136       else
7137         {
7138           used_at = 1;
7139           macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
7140           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7141                                    &offset_expr, AT, ZERO);
7142         }
7143       break;
7144
7145     case M_BGTUL:
7146       likely = 1;
7147     case M_BGTU:
7148       if (treg == 0)
7149         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7150                                  &offset_expr, sreg, ZERO);
7151       else if (sreg == 0)
7152         goto do_false;
7153       else
7154         {
7155           used_at = 1;
7156           macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
7157           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7158                                    &offset_expr, AT, ZERO);
7159         }
7160       break;
7161
7162     case M_BLEL:
7163       likely = 1;
7164     case M_BLE:
7165       if (treg == 0)
7166         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
7167       else if (sreg == 0)
7168         macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
7169       else
7170         {
7171           used_at = 1;
7172           macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
7173           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7174                                    &offset_expr, AT, ZERO);
7175         }
7176       break;
7177
7178     case M_BLEL_I:
7179       likely = 1;
7180     case M_BLE_I:
7181       if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
7182         goto do_true;
7183       if (imm_expr.X_op != O_constant)
7184         as_bad (_("Unsupported large constant"));
7185       ++imm_expr.X_add_number;
7186       /* FALLTHROUGH */
7187     case M_BLT_I:
7188     case M_BLTL_I:
7189       if (mask == M_BLTL_I)
7190         likely = 1;
7191       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7192         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
7193       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7194         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
7195       else
7196         {
7197           used_at = 1;
7198           set_at (sreg, 0);
7199           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7200                                    &offset_expr, AT, ZERO);
7201         }
7202       break;
7203
7204     case M_BLEUL:
7205       likely = 1;
7206     case M_BLEU:
7207       if (treg == 0)
7208         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7209                                  &offset_expr, sreg, ZERO);
7210       else if (sreg == 0)
7211         goto do_true;
7212       else
7213         {
7214           used_at = 1;
7215           macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
7216           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7217                                    &offset_expr, AT, ZERO);
7218         }
7219       break;
7220
7221     case M_BLEUL_I:
7222       likely = 1;
7223     case M_BLEU_I:
7224       if (sreg == 0
7225           || (HAVE_32BIT_GPRS
7226               && imm_expr.X_op == O_constant
7227               && imm_expr.X_add_number == -1))
7228         goto do_true;
7229       if (imm_expr.X_op != O_constant)
7230         as_bad (_("Unsupported large constant"));
7231       ++imm_expr.X_add_number;
7232       /* FALLTHROUGH */
7233     case M_BLTU_I:
7234     case M_BLTUL_I:
7235       if (mask == M_BLTUL_I)
7236         likely = 1;
7237       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7238         goto do_false;
7239       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7240         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7241                                  &offset_expr, sreg, ZERO);
7242       else
7243         {
7244           used_at = 1;
7245           set_at (sreg, 1);
7246           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7247                                    &offset_expr, AT, ZERO);
7248         }
7249       break;
7250
7251     case M_BLTL:
7252       likely = 1;
7253     case M_BLT:
7254       if (treg == 0)
7255         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
7256       else if (sreg == 0)
7257         macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
7258       else
7259         {
7260           used_at = 1;
7261           macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
7262           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7263                                    &offset_expr, AT, ZERO);
7264         }
7265       break;
7266
7267     case M_BLTUL:
7268       likely = 1;
7269     case M_BLTU:
7270       if (treg == 0)
7271         goto do_false;
7272       else if (sreg == 0)
7273         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7274                                  &offset_expr, ZERO, treg);
7275       else
7276         {
7277           used_at = 1;
7278           macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
7279           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7280                                    &offset_expr, AT, ZERO);
7281         }
7282       break;
7283
7284     case M_DEXT:
7285       {
7286         /* Use unsigned arithmetic.  */
7287         addressT pos;
7288         addressT size;
7289
7290         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
7291           {
7292             as_bad (_("Unsupported large constant"));
7293             pos = size = 1;
7294           }
7295         else
7296           {
7297             pos = imm_expr.X_add_number;
7298             size = imm2_expr.X_add_number;
7299           }
7300
7301         if (pos > 63)
7302           {
7303             as_bad (_("Improper position (%lu)"), (unsigned long) pos);
7304             pos = 1;
7305           }
7306         if (size == 0 || size > 64 || (pos + size - 1) > 63)
7307           {
7308             as_bad (_("Improper extract size (%lu, position %lu)"),
7309                     (unsigned long) size, (unsigned long) pos);
7310             size = 1;
7311           }
7312
7313         if (size <= 32 && pos < 32)
7314           {
7315             s = "dext";
7316             fmt = "t,r,+A,+C";
7317           }
7318         else if (size <= 32)
7319           {
7320             s = "dextu";
7321             fmt = "t,r,+E,+H";
7322           }
7323         else
7324           {
7325             s = "dextm";
7326             fmt = "t,r,+A,+G";
7327           }
7328         macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
7329                      (int) (size - 1));
7330       }
7331       break;
7332
7333     case M_DINS:
7334       {
7335         /* Use unsigned arithmetic.  */
7336         addressT pos;
7337         addressT size;
7338
7339         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
7340           {
7341             as_bad (_("Unsupported large constant"));
7342             pos = size = 1;
7343           }
7344         else
7345           {
7346             pos = imm_expr.X_add_number;
7347             size = imm2_expr.X_add_number;
7348           }
7349
7350         if (pos > 63)
7351           {
7352             as_bad (_("Improper position (%lu)"), (unsigned long) pos);
7353             pos = 1;
7354           }
7355         if (size == 0 || size > 64 || (pos + size - 1) > 63)
7356           {
7357             as_bad (_("Improper insert size (%lu, position %lu)"),
7358                     (unsigned long) size, (unsigned long) pos);
7359             size = 1;
7360           }
7361
7362         if (pos < 32 && (pos + size - 1) < 32)
7363           {
7364             s = "dins";
7365             fmt = "t,r,+A,+B";
7366           }
7367         else if (pos >= 32)
7368           {
7369             s = "dinsu";
7370             fmt = "t,r,+E,+F";
7371           }
7372         else
7373           {
7374             s = "dinsm";
7375             fmt = "t,r,+A,+F";
7376           }
7377         macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
7378                      (int) (pos + size - 1));
7379       }
7380       break;
7381
7382     case M_DDIV_3:
7383       dbl = 1;
7384     case M_DIV_3:
7385       s = "mflo";
7386       goto do_div3;
7387     case M_DREM_3:
7388       dbl = 1;
7389     case M_REM_3:
7390       s = "mfhi";
7391     do_div3:
7392       if (treg == 0)
7393         {
7394           as_warn (_("Divide by zero."));
7395           if (mips_trap)
7396             macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
7397           else
7398             macro_build (NULL, "break", BRK_FMT, 7);
7399           break;
7400         }
7401
7402       start_noreorder ();
7403       if (mips_trap)
7404         {
7405           macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7406           macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
7407         }
7408       else
7409         {
7410           if (mips_opts.micromips)
7411             micromips_label_expr (&label_expr);
7412           else
7413             label_expr.X_add_number = 8;
7414           macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7415           macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
7416           macro_build (NULL, "break", BRK_FMT, 7);
7417           if (mips_opts.micromips)
7418             micromips_add_label ();
7419         }
7420       expr1.X_add_number = -1;
7421       used_at = 1;
7422       load_register (AT, &expr1, dbl);
7423       if (mips_opts.micromips)
7424         micromips_label_expr (&label_expr);
7425       else
7426         label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
7427       macro_build (&label_expr, "bne", "s,t,p", treg, AT);
7428       if (dbl)
7429         {
7430           expr1.X_add_number = 1;
7431           load_register (AT, &expr1, dbl);
7432           macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
7433         }
7434       else
7435         {
7436           expr1.X_add_number = 0x80000000;
7437           macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
7438         }
7439       if (mips_trap)
7440         {
7441           macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
7442           /* We want to close the noreorder block as soon as possible, so
7443              that later insns are available for delay slot filling.  */
7444           end_noreorder ();
7445         }
7446       else
7447         {
7448           if (mips_opts.micromips)
7449             micromips_label_expr (&label_expr);
7450           else
7451             label_expr.X_add_number = 8;
7452           macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
7453           macro_build (NULL, "nop", "");
7454
7455           /* We want to close the noreorder block as soon as possible, so
7456              that later insns are available for delay slot filling.  */
7457           end_noreorder ();
7458
7459           macro_build (NULL, "break", BRK_FMT, 6);
7460         }
7461       if (mips_opts.micromips)
7462         micromips_add_label ();
7463       macro_build (NULL, s, MFHL_FMT, dreg);
7464       break;
7465
7466     case M_DIV_3I:
7467       s = "div";
7468       s2 = "mflo";
7469       goto do_divi;
7470     case M_DIVU_3I:
7471       s = "divu";
7472       s2 = "mflo";
7473       goto do_divi;
7474     case M_REM_3I:
7475       s = "div";
7476       s2 = "mfhi";
7477       goto do_divi;
7478     case M_REMU_3I:
7479       s = "divu";
7480       s2 = "mfhi";
7481       goto do_divi;
7482     case M_DDIV_3I:
7483       dbl = 1;
7484       s = "ddiv";
7485       s2 = "mflo";
7486       goto do_divi;
7487     case M_DDIVU_3I:
7488       dbl = 1;
7489       s = "ddivu";
7490       s2 = "mflo";
7491       goto do_divi;
7492     case M_DREM_3I:
7493       dbl = 1;
7494       s = "ddiv";
7495       s2 = "mfhi";
7496       goto do_divi;
7497     case M_DREMU_3I:
7498       dbl = 1;
7499       s = "ddivu";
7500       s2 = "mfhi";
7501     do_divi:
7502       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7503         {
7504           as_warn (_("Divide by zero."));
7505           if (mips_trap)
7506             macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
7507           else
7508             macro_build (NULL, "break", BRK_FMT, 7);
7509           break;
7510         }
7511       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7512         {
7513           if (strcmp (s2, "mflo") == 0)
7514             move_register (dreg, sreg);
7515           else
7516             move_register (dreg, ZERO);
7517           break;
7518         }
7519       if (imm_expr.X_op == O_constant
7520           && imm_expr.X_add_number == -1
7521           && s[strlen (s) - 1] != 'u')
7522         {
7523           if (strcmp (s2, "mflo") == 0)
7524             {
7525               macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
7526             }
7527           else
7528             move_register (dreg, ZERO);
7529           break;
7530         }
7531
7532       used_at = 1;
7533       load_register (AT, &imm_expr, dbl);
7534       macro_build (NULL, s, "z,s,t", sreg, AT);
7535       macro_build (NULL, s2, MFHL_FMT, dreg);
7536       break;
7537
7538     case M_DIVU_3:
7539       s = "divu";
7540       s2 = "mflo";
7541       goto do_divu3;
7542     case M_REMU_3:
7543       s = "divu";
7544       s2 = "mfhi";
7545       goto do_divu3;
7546     case M_DDIVU_3:
7547       s = "ddivu";
7548       s2 = "mflo";
7549       goto do_divu3;
7550     case M_DREMU_3:
7551       s = "ddivu";
7552       s2 = "mfhi";
7553     do_divu3:
7554       start_noreorder ();
7555       if (mips_trap)
7556         {
7557           macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7558           macro_build (NULL, s, "z,s,t", sreg, treg);
7559           /* We want to close the noreorder block as soon as possible, so
7560              that later insns are available for delay slot filling.  */
7561           end_noreorder ();
7562         }
7563       else
7564         {
7565           if (mips_opts.micromips)
7566             micromips_label_expr (&label_expr);
7567           else
7568             label_expr.X_add_number = 8;
7569           macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7570           macro_build (NULL, s, "z,s,t", sreg, treg);
7571
7572           /* We want to close the noreorder block as soon as possible, so
7573              that later insns are available for delay slot filling.  */
7574           end_noreorder ();
7575           macro_build (NULL, "break", BRK_FMT, 7);
7576           if (mips_opts.micromips)
7577             micromips_add_label ();
7578         }
7579       macro_build (NULL, s2, MFHL_FMT, dreg);
7580       break;
7581
7582     case M_DLCA_AB:
7583       dbl = 1;
7584     case M_LCA_AB:
7585       call = 1;
7586       goto do_la;
7587     case M_DLA_AB:
7588       dbl = 1;
7589     case M_LA_AB:
7590     do_la:
7591       /* Load the address of a symbol into a register.  If breg is not
7592          zero, we then add a base register to it.  */
7593
7594       if (dbl && HAVE_32BIT_GPRS)
7595         as_warn (_("dla used to load 32-bit register"));
7596
7597       if (!dbl && HAVE_64BIT_OBJECTS)
7598         as_warn (_("la used to load 64-bit address"));
7599
7600       if (small_offset_p (0, align, 16))
7601         {
7602           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", treg, breg,
7603                        -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
7604           break;
7605         }
7606
7607       if (mips_opts.at && (treg == breg))
7608         {
7609           tempreg = AT;
7610           used_at = 1;
7611         }
7612       else
7613         {
7614           tempreg = treg;
7615         }
7616
7617       if (offset_expr.X_op != O_symbol
7618           && offset_expr.X_op != O_constant)
7619         {
7620           as_bad (_("Expression too complex"));
7621           offset_expr.X_op = O_constant;
7622         }
7623
7624       if (offset_expr.X_op == O_constant)
7625         load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
7626       else if (mips_pic == NO_PIC)
7627         {
7628           /* If this is a reference to a GP relative symbol, we want
7629                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
7630              Otherwise we want
7631                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
7632                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7633              If we have a constant, we need two instructions anyhow,
7634              so we may as well always use the latter form.
7635
7636              With 64bit address space and a usable $at we want
7637                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
7638                lui      $at,<sym>               (BFD_RELOC_HI16_S)
7639                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
7640                daddiu   $at,<sym>               (BFD_RELOC_LO16)
7641                dsll32   $tempreg,0
7642                daddu    $tempreg,$tempreg,$at
7643
7644              If $at is already in use, we use a path which is suboptimal
7645              on superscalar processors.
7646                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
7647                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
7648                dsll     $tempreg,16
7649                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
7650                dsll     $tempreg,16
7651                daddiu   $tempreg,<sym>          (BFD_RELOC_LO16)
7652
7653              For GP relative symbols in 64bit address space we can use
7654              the same sequence as in 32bit address space.  */
7655           if (HAVE_64BIT_SYMBOLS)
7656             {
7657               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7658                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7659                 {
7660                   relax_start (offset_expr.X_add_symbol);
7661                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7662                                tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7663                   relax_switch ();
7664                 }
7665
7666               if (used_at == 0 && mips_opts.at)
7667                 {
7668                   macro_build (&offset_expr, "lui", LUI_FMT,
7669                                tempreg, BFD_RELOC_MIPS_HIGHEST);
7670                   macro_build (&offset_expr, "lui", LUI_FMT,
7671                                AT, BFD_RELOC_HI16_S);
7672                   macro_build (&offset_expr, "daddiu", "t,r,j",
7673                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7674                   macro_build (&offset_expr, "daddiu", "t,r,j",
7675                                AT, AT, BFD_RELOC_LO16);
7676                   macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
7677                   macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
7678                   used_at = 1;
7679                 }
7680               else
7681                 {
7682                   macro_build (&offset_expr, "lui", LUI_FMT,
7683                                tempreg, BFD_RELOC_MIPS_HIGHEST);
7684                   macro_build (&offset_expr, "daddiu", "t,r,j",
7685                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7686                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7687                   macro_build (&offset_expr, "daddiu", "t,r,j",
7688                                tempreg, tempreg, BFD_RELOC_HI16_S);
7689                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7690                   macro_build (&offset_expr, "daddiu", "t,r,j",
7691                                tempreg, tempreg, BFD_RELOC_LO16);
7692                 }
7693
7694               if (mips_relax.sequence)
7695                 relax_end ();
7696             }
7697           else
7698             {
7699               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7700                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7701                 {
7702                   relax_start (offset_expr.X_add_symbol);
7703                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7704                                tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7705                   relax_switch ();
7706                 }
7707               if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
7708                 as_bad (_("Offset too large"));
7709               macro_build_lui (&offset_expr, tempreg);
7710               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7711                            tempreg, tempreg, BFD_RELOC_LO16);
7712               if (mips_relax.sequence)
7713                 relax_end ();
7714             }
7715         }
7716       else if (!mips_big_got && !HAVE_NEWABI)
7717         {
7718           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7719
7720           /* If this is a reference to an external symbol, and there
7721              is no constant, we want
7722                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7723              or for lca or if tempreg is PIC_CALL_REG
7724                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
7725              For a local symbol, we want
7726                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7727                nop
7728                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7729
7730              If we have a small constant, and this is a reference to
7731              an external symbol, we want
7732                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7733                nop
7734                addiu    $tempreg,$tempreg,<constant>
7735              For a local symbol, we want the same instruction
7736              sequence, but we output a BFD_RELOC_LO16 reloc on the
7737              addiu instruction.
7738
7739              If we have a large constant, and this is a reference to
7740              an external symbol, we want
7741                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7742                lui      $at,<hiconstant>
7743                addiu    $at,$at,<loconstant>
7744                addu     $tempreg,$tempreg,$at
7745              For a local symbol, we want the same instruction
7746              sequence, but we output a BFD_RELOC_LO16 reloc on the
7747              addiu instruction.
7748            */
7749
7750           if (offset_expr.X_add_number == 0)
7751             {
7752               if (mips_pic == SVR4_PIC
7753                   && breg == 0
7754                   && (call || tempreg == PIC_CALL_REG))
7755                 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7756
7757               relax_start (offset_expr.X_add_symbol);
7758               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7759                            lw_reloc_type, mips_gp_register);
7760               if (breg != 0)
7761                 {
7762                   /* We're going to put in an addu instruction using
7763                      tempreg, so we may as well insert the nop right
7764                      now.  */
7765                   load_delay_nop ();
7766                 }
7767               relax_switch ();
7768               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7769                            tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
7770               load_delay_nop ();
7771               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7772                            tempreg, tempreg, BFD_RELOC_LO16);
7773               relax_end ();
7774               /* FIXME: If breg == 0, and the next instruction uses
7775                  $tempreg, then if this variant case is used an extra
7776                  nop will be generated.  */
7777             }
7778           else if (offset_expr.X_add_number >= -0x8000
7779                    && offset_expr.X_add_number < 0x8000)
7780             {
7781               load_got_offset (tempreg, &offset_expr);
7782               load_delay_nop ();
7783               add_got_offset (tempreg, &offset_expr);
7784             }
7785           else
7786             {
7787               expr1.X_add_number = offset_expr.X_add_number;
7788               offset_expr.X_add_number =
7789                 SEXT_16BIT (offset_expr.X_add_number);
7790               load_got_offset (tempreg, &offset_expr);
7791               offset_expr.X_add_number = expr1.X_add_number;
7792               /* If we are going to add in a base register, and the
7793                  target register and the base register are the same,
7794                  then we are using AT as a temporary register.  Since
7795                  we want to load the constant into AT, we add our
7796                  current AT (from the global offset table) and the
7797                  register into the register now, and pretend we were
7798                  not using a base register.  */
7799               if (breg == treg)
7800                 {
7801                   load_delay_nop ();
7802                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7803                                treg, AT, breg);
7804                   breg = 0;
7805                   tempreg = treg;
7806                 }
7807               add_got_offset_hilo (tempreg, &offset_expr, AT);
7808               used_at = 1;
7809             }
7810         }
7811       else if (!mips_big_got && HAVE_NEWABI)
7812         {
7813           int add_breg_early = 0;
7814
7815           /* If this is a reference to an external, and there is no
7816              constant, or local symbol (*), with or without a
7817              constant, we want
7818                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
7819              or for lca or if tempreg is PIC_CALL_REG
7820                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
7821
7822              If we have a small constant, and this is a reference to
7823              an external symbol, we want
7824                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
7825                addiu    $tempreg,$tempreg,<constant>
7826
7827              If we have a large constant, and this is a reference to
7828              an external symbol, we want
7829                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
7830                lui      $at,<hiconstant>
7831                addiu    $at,$at,<loconstant>
7832                addu     $tempreg,$tempreg,$at
7833
7834              (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7835              local symbols, even though it introduces an additional
7836              instruction.  */
7837
7838           if (offset_expr.X_add_number)
7839             {
7840               expr1.X_add_number = offset_expr.X_add_number;
7841               offset_expr.X_add_number = 0;
7842
7843               relax_start (offset_expr.X_add_symbol);
7844               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7845                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7846
7847               if (expr1.X_add_number >= -0x8000
7848                   && expr1.X_add_number < 0x8000)
7849                 {
7850                   macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7851                                tempreg, tempreg, BFD_RELOC_LO16);
7852                 }
7853               else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7854                 {
7855                   /* If we are going to add in a base register, and the
7856                      target register and the base register are the same,
7857                      then we are using AT as a temporary register.  Since
7858                      we want to load the constant into AT, we add our
7859                      current AT (from the global offset table) and the
7860                      register into the register now, and pretend we were
7861                      not using a base register.  */
7862                   if (breg != treg)
7863                     dreg = tempreg;
7864                   else
7865                     {
7866                       gas_assert (tempreg == AT);
7867                       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7868                                    treg, AT, breg);
7869                       dreg = treg;
7870                       add_breg_early = 1;
7871                     }
7872
7873                   load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7874                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7875                                dreg, dreg, AT);
7876
7877                   used_at = 1;
7878                 }
7879               else
7880                 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7881
7882               relax_switch ();
7883               offset_expr.X_add_number = expr1.X_add_number;
7884
7885               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7886                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7887               if (add_breg_early)
7888                 {
7889                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7890                                treg, tempreg, breg);
7891                   breg = 0;
7892                   tempreg = treg;
7893                 }
7894               relax_end ();
7895             }
7896           else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
7897             {
7898               relax_start (offset_expr.X_add_symbol);
7899               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7900                            BFD_RELOC_MIPS_CALL16, mips_gp_register);
7901               relax_switch ();
7902               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7903                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7904               relax_end ();
7905             }
7906           else
7907             {
7908               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7909                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7910             }
7911         }
7912       else if (mips_big_got && !HAVE_NEWABI)
7913         {
7914           int gpdelay;
7915           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7916           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7917           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7918
7919           /* This is the large GOT case.  If this is a reference to an
7920              external symbol, and there is no constant, we want
7921                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7922                addu     $tempreg,$tempreg,$gp
7923                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7924              or for lca or if tempreg is PIC_CALL_REG
7925                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
7926                addu     $tempreg,$tempreg,$gp
7927                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7928              For a local symbol, we want
7929                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7930                nop
7931                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7932
7933              If we have a small constant, and this is a reference to
7934              an external symbol, we want
7935                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7936                addu     $tempreg,$tempreg,$gp
7937                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7938                nop
7939                addiu    $tempreg,$tempreg,<constant>
7940              For a local symbol, we want
7941                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7942                nop
7943                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7944
7945              If we have a large constant, and this is a reference to
7946              an external symbol, we want
7947                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7948                addu     $tempreg,$tempreg,$gp
7949                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7950                lui      $at,<hiconstant>
7951                addiu    $at,$at,<loconstant>
7952                addu     $tempreg,$tempreg,$at
7953              For a local symbol, we want
7954                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7955                lui      $at,<hiconstant>
7956                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
7957                addu     $tempreg,$tempreg,$at
7958           */
7959
7960           expr1.X_add_number = offset_expr.X_add_number;
7961           offset_expr.X_add_number = 0;
7962           relax_start (offset_expr.X_add_symbol);
7963           gpdelay = reg_needs_delay (mips_gp_register);
7964           if (expr1.X_add_number == 0 && breg == 0
7965               && (call || tempreg == PIC_CALL_REG))
7966             {
7967               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7968               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7969             }
7970           macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7971           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7972                        tempreg, tempreg, mips_gp_register);
7973           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7974                        tempreg, lw_reloc_type, tempreg);
7975           if (expr1.X_add_number == 0)
7976             {
7977               if (breg != 0)
7978                 {
7979                   /* We're going to put in an addu instruction using
7980                      tempreg, so we may as well insert the nop right
7981                      now.  */
7982                   load_delay_nop ();
7983                 }
7984             }
7985           else if (expr1.X_add_number >= -0x8000
7986                    && expr1.X_add_number < 0x8000)
7987             {
7988               load_delay_nop ();
7989               macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7990                            tempreg, tempreg, BFD_RELOC_LO16);
7991             }
7992           else
7993             {
7994               /* If we are going to add in a base register, and the
7995                  target register and the base register are the same,
7996                  then we are using AT as a temporary register.  Since
7997                  we want to load the constant into AT, we add our
7998                  current AT (from the global offset table) and the
7999                  register into the register now, and pretend we were
8000                  not using a base register.  */
8001               if (breg != treg)
8002                 dreg = tempreg;
8003               else
8004                 {
8005                   gas_assert (tempreg == AT);
8006                   load_delay_nop ();
8007                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8008                                treg, AT, breg);
8009                   dreg = treg;
8010                 }
8011
8012               load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
8013               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
8014
8015               used_at = 1;
8016             }
8017           offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
8018           relax_switch ();
8019
8020           if (gpdelay)
8021             {
8022               /* This is needed because this instruction uses $gp, but
8023                  the first instruction on the main stream does not.  */
8024               macro_build (NULL, "nop", "");
8025             }
8026
8027           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8028                        local_reloc_type, mips_gp_register);
8029           if (expr1.X_add_number >= -0x8000
8030               && expr1.X_add_number < 0x8000)
8031             {
8032               load_delay_nop ();
8033               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8034                            tempreg, tempreg, BFD_RELOC_LO16);
8035               /* FIXME: If add_number is 0, and there was no base
8036                  register, the external symbol case ended with a load,
8037                  so if the symbol turns out to not be external, and
8038                  the next instruction uses tempreg, an unnecessary nop
8039                  will be inserted.  */
8040             }
8041           else
8042             {
8043               if (breg == treg)
8044                 {
8045                   /* We must add in the base register now, as in the
8046                      external symbol case.  */
8047                   gas_assert (tempreg == AT);
8048                   load_delay_nop ();
8049                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8050                                treg, AT, breg);
8051                   tempreg = treg;
8052                   /* We set breg to 0 because we have arranged to add
8053                      it in in both cases.  */
8054                   breg = 0;
8055                 }
8056
8057               macro_build_lui (&expr1, AT);
8058               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8059                            AT, AT, BFD_RELOC_LO16);
8060               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8061                            tempreg, tempreg, AT);
8062               used_at = 1;
8063             }
8064           relax_end ();
8065         }
8066       else if (mips_big_got && HAVE_NEWABI)
8067         {
8068           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
8069           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
8070           int add_breg_early = 0;
8071
8072           /* This is the large GOT case.  If this is a reference to an
8073              external symbol, and there is no constant, we want
8074                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
8075                add      $tempreg,$tempreg,$gp
8076                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8077              or for lca or if tempreg is PIC_CALL_REG
8078                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
8079                add      $tempreg,$tempreg,$gp
8080                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
8081
8082              If we have a small constant, and this is a reference to
8083              an external symbol, we want
8084                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
8085                add      $tempreg,$tempreg,$gp
8086                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8087                addi     $tempreg,$tempreg,<constant>
8088
8089              If we have a large constant, and this is a reference to
8090              an external symbol, we want
8091                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
8092                addu     $tempreg,$tempreg,$gp
8093                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8094                lui      $at,<hiconstant>
8095                addi     $at,$at,<loconstant>
8096                add      $tempreg,$tempreg,$at
8097
8098              If we have NewABI, and we know it's a local symbol, we want
8099                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
8100                addiu    $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
8101              otherwise we have to resort to GOT_HI16/GOT_LO16.  */
8102
8103           relax_start (offset_expr.X_add_symbol);
8104
8105           expr1.X_add_number = offset_expr.X_add_number;
8106           offset_expr.X_add_number = 0;
8107
8108           if (expr1.X_add_number == 0 && breg == 0
8109               && (call || tempreg == PIC_CALL_REG))
8110             {
8111               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
8112               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
8113             }
8114           macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
8115           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8116                        tempreg, tempreg, mips_gp_register);
8117           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8118                        tempreg, lw_reloc_type, tempreg);
8119
8120           if (expr1.X_add_number == 0)
8121             ;
8122           else if (expr1.X_add_number >= -0x8000
8123                    && expr1.X_add_number < 0x8000)
8124             {
8125               macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
8126                            tempreg, tempreg, BFD_RELOC_LO16);
8127             }
8128           else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
8129             {
8130               /* If we are going to add in a base register, and the
8131                  target register and the base register are the same,
8132                  then we are using AT as a temporary register.  Since
8133                  we want to load the constant into AT, we add our
8134                  current AT (from the global offset table) and the
8135                  register into the register now, and pretend we were
8136                  not using a base register.  */
8137               if (breg != treg)
8138                 dreg = tempreg;
8139               else
8140                 {
8141                   gas_assert (tempreg == AT);
8142                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8143                                treg, AT, breg);
8144                   dreg = treg;
8145                   add_breg_early = 1;
8146                 }
8147
8148               load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
8149               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
8150
8151               used_at = 1;
8152             }
8153           else
8154             as_bad (_("PIC code offset overflow (max 32 signed bits)"));
8155
8156           relax_switch ();
8157           offset_expr.X_add_number = expr1.X_add_number;
8158           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8159                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8160           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8161                        tempreg, BFD_RELOC_MIPS_GOT_OFST);
8162           if (add_breg_early)
8163             {
8164               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8165                            treg, tempreg, breg);
8166               breg = 0;
8167               tempreg = treg;
8168             }
8169           relax_end ();
8170         }
8171       else
8172         abort ();
8173
8174       if (breg != 0)
8175         macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
8176       break;
8177
8178     case M_MSGSND:
8179       gas_assert (!mips_opts.micromips);
8180       {
8181         unsigned long temp = (treg << 16) | (0x01);
8182         macro_build (NULL, "c2", "C", temp);
8183       }
8184       break;
8185
8186     case M_MSGLD:
8187       gas_assert (!mips_opts.micromips);
8188       {
8189         unsigned long temp = (0x02);
8190         macro_build (NULL, "c2", "C", temp);
8191       }
8192       break;
8193
8194     case M_MSGLD_T:
8195       gas_assert (!mips_opts.micromips);
8196       {
8197         unsigned long temp = (treg << 16) | (0x02);
8198         macro_build (NULL, "c2", "C", temp);
8199       }
8200       break;
8201
8202     case M_MSGWAIT:
8203       gas_assert (!mips_opts.micromips);
8204       macro_build (NULL, "c2", "C", 3);
8205       break;
8206
8207     case M_MSGWAIT_T:
8208       gas_assert (!mips_opts.micromips);
8209       {
8210         unsigned long temp = (treg << 16) | 0x03;
8211         macro_build (NULL, "c2", "C", temp);
8212       }
8213       break;
8214
8215     case M_J_A:
8216       /* The j instruction may not be used in PIC code, since it
8217          requires an absolute address.  We convert it to a b
8218          instruction.  */
8219       if (mips_pic == NO_PIC)
8220         macro_build (&offset_expr, "j", "a");
8221       else
8222         macro_build (&offset_expr, "b", "p");
8223       break;
8224
8225       /* The jal instructions must be handled as macros because when
8226          generating PIC code they expand to multi-instruction
8227          sequences.  Normally they are simple instructions.  */
8228     case M_JALS_1:
8229       dreg = RA;
8230       /* Fall through.  */
8231     case M_JALS_2:
8232       gas_assert (mips_opts.micromips);
8233       if (mips_opts.insn32)
8234         {
8235           as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
8236           break;
8237         }
8238       jals = 1;
8239       goto jal;
8240     case M_JAL_1:
8241       dreg = RA;
8242       /* Fall through.  */
8243     case M_JAL_2:
8244     jal:
8245       if (mips_pic == NO_PIC)
8246         {
8247           s = jals ? "jalrs" : "jalr";
8248           if (mips_opts.micromips
8249               && !mips_opts.insn32
8250               && dreg == RA
8251               && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
8252             macro_build (NULL, s, "mj", sreg);
8253           else
8254             macro_build (NULL, s, JALR_FMT, dreg, sreg);
8255         }
8256       else
8257         {
8258           int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
8259                            && mips_cprestore_offset >= 0);
8260
8261           if (sreg != PIC_CALL_REG)
8262             as_warn (_("MIPS PIC call to register other than $25"));
8263
8264           s = ((mips_opts.micromips
8265                 && !mips_opts.insn32
8266                 && (!mips_opts.noreorder || cprestore))
8267                ? "jalrs" : "jalr");
8268           if (mips_opts.micromips
8269               && !mips_opts.insn32
8270               && dreg == RA
8271               && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
8272             macro_build (NULL, s, "mj", sreg);
8273           else
8274             macro_build (NULL, s, JALR_FMT, dreg, sreg);
8275           if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
8276             {
8277               if (mips_cprestore_offset < 0)
8278                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8279               else
8280                 {
8281                   if (!mips_frame_reg_valid)
8282                     {
8283                       as_warn (_("No .frame pseudo-op used in PIC code"));
8284                       /* Quiet this warning.  */
8285                       mips_frame_reg_valid = 1;
8286                     }
8287                   if (!mips_cprestore_valid)
8288                     {
8289                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
8290                       /* Quiet this warning.  */
8291                       mips_cprestore_valid = 1;
8292                     }
8293                   if (mips_opts.noreorder)
8294                     macro_build (NULL, "nop", "");
8295                   expr1.X_add_number = mips_cprestore_offset;
8296                   macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
8297                                                 mips_gp_register,
8298                                                 mips_frame_reg,
8299                                                 HAVE_64BIT_ADDRESSES);
8300                 }
8301             }
8302         }
8303
8304       break;
8305
8306     case M_JALS_A:
8307       gas_assert (mips_opts.micromips);
8308       if (mips_opts.insn32)
8309         {
8310           as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
8311           break;
8312         }
8313       jals = 1;
8314       /* Fall through.  */
8315     case M_JAL_A:
8316       if (mips_pic == NO_PIC)
8317         macro_build (&offset_expr, jals ? "jals" : "jal", "a");
8318       else if (mips_pic == SVR4_PIC)
8319         {
8320           /* If this is a reference to an external symbol, and we are
8321              using a small GOT, we want
8322                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
8323                nop
8324                jalr     $ra,$25
8325                nop
8326                lw       $gp,cprestore($sp)
8327              The cprestore value is set using the .cprestore
8328              pseudo-op.  If we are using a big GOT, we want
8329                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
8330                addu     $25,$25,$gp
8331                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
8332                nop
8333                jalr     $ra,$25
8334                nop
8335                lw       $gp,cprestore($sp)
8336              If the symbol is not external, we want
8337                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
8338                nop
8339                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
8340                jalr     $ra,$25
8341                nop
8342                lw $gp,cprestore($sp)
8343
8344              For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
8345              sequences above, minus nops, unless the symbol is local,
8346              which enables us to use GOT_PAGE/GOT_OFST (big got) or
8347              GOT_DISP.  */
8348           if (HAVE_NEWABI)
8349             {
8350               if (!mips_big_got)
8351                 {
8352                   relax_start (offset_expr.X_add_symbol);
8353                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8354                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
8355                                mips_gp_register);
8356                   relax_switch ();
8357                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8358                                PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
8359                                mips_gp_register);
8360                   relax_end ();
8361                 }
8362               else
8363                 {
8364                   relax_start (offset_expr.X_add_symbol);
8365                   macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
8366                                BFD_RELOC_MIPS_CALL_HI16);
8367                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8368                                PIC_CALL_REG, mips_gp_register);
8369                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8370                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8371                                PIC_CALL_REG);
8372                   relax_switch ();
8373                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8374                                PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
8375                                mips_gp_register);
8376                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8377                                PIC_CALL_REG, PIC_CALL_REG,
8378                                BFD_RELOC_MIPS_GOT_OFST);
8379                   relax_end ();
8380                 }
8381
8382               macro_build_jalr (&offset_expr, 0);
8383             }
8384           else
8385             {
8386               relax_start (offset_expr.X_add_symbol);
8387               if (!mips_big_got)
8388                 {
8389                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8390                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
8391                                mips_gp_register);
8392                   load_delay_nop ();
8393                   relax_switch ();
8394                 }
8395               else
8396                 {
8397                   int gpdelay;
8398
8399                   gpdelay = reg_needs_delay (mips_gp_register);
8400                   macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
8401                                BFD_RELOC_MIPS_CALL_HI16);
8402                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8403                                PIC_CALL_REG, mips_gp_register);
8404                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8405                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8406                                PIC_CALL_REG);
8407                   load_delay_nop ();
8408                   relax_switch ();
8409                   if (gpdelay)
8410                     macro_build (NULL, "nop", "");
8411                 }
8412               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8413                            PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
8414                            mips_gp_register);
8415               load_delay_nop ();
8416               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8417                            PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
8418               relax_end ();
8419               macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
8420
8421               if (mips_cprestore_offset < 0)
8422                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8423               else
8424                 {
8425                   if (!mips_frame_reg_valid)
8426                     {
8427                       as_warn (_("No .frame pseudo-op used in PIC code"));
8428                       /* Quiet this warning.  */
8429                       mips_frame_reg_valid = 1;
8430                     }
8431                   if (!mips_cprestore_valid)
8432                     {
8433                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
8434                       /* Quiet this warning.  */
8435                       mips_cprestore_valid = 1;
8436                     }
8437                   if (mips_opts.noreorder)
8438                     macro_build (NULL, "nop", "");
8439                   expr1.X_add_number = mips_cprestore_offset;
8440                   macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
8441                                                 mips_gp_register,
8442                                                 mips_frame_reg,
8443                                                 HAVE_64BIT_ADDRESSES);
8444                 }
8445             }
8446         }
8447       else if (mips_pic == VXWORKS_PIC)
8448         as_bad (_("Non-PIC jump used in PIC library"));
8449       else
8450         abort ();
8451
8452       break;
8453
8454     case M_LBUE_AB:
8455       s = "lbue";
8456       fmt = "t,+j(b)";
8457       offbits = 9;
8458       goto ld_st;
8459     case M_LHUE_AB:
8460       s = "lhue";
8461       fmt = "t,+j(b)";
8462       offbits = 9;
8463       goto ld_st;
8464     case M_LBE_AB:
8465       s = "lbe";
8466       fmt = "t,+j(b)";
8467       offbits = 9;
8468       goto ld_st;
8469     case M_LHE_AB:
8470       s = "lhe";
8471       fmt = "t,+j(b)";
8472       offbits = 9;
8473       goto ld_st;
8474     case M_LLE_AB:
8475       s = "lle";
8476       fmt = "t,+j(b)";
8477       offbits = 9;
8478       goto ld_st;
8479     case M_LWE_AB:
8480       s = "lwe";
8481       fmt = "t,+j(b)";
8482       offbits = 9;
8483       goto ld_st;
8484     case M_LWLE_AB:
8485       s = "lwle";
8486       fmt = "t,+j(b)";
8487       offbits = 9;
8488       goto ld_st;
8489     case M_LWRE_AB:
8490       s = "lwre";
8491       fmt = "t,+j(b)";
8492       offbits = 9;
8493       goto ld_st;
8494     case M_SBE_AB:
8495       s = "sbe";
8496       fmt = "t,+j(b)";
8497       offbits = 9;
8498       goto ld_st;
8499     case M_SCE_AB:
8500       s = "sce";
8501       fmt = "t,+j(b)";
8502       offbits = 9;
8503       goto ld_st;
8504     case M_SHE_AB:
8505       s = "she";
8506       fmt = "t,+j(b)";
8507       offbits = 9;
8508       goto ld_st;
8509     case M_SWE_AB:
8510       s = "swe";
8511       fmt = "t,+j(b)";
8512       offbits = 9;
8513       goto ld_st;
8514     case M_SWLE_AB:
8515       s = "swle";
8516       fmt = "t,+j(b)";
8517       offbits = 9;
8518       goto ld_st;
8519     case M_SWRE_AB:
8520       s = "swre";
8521       fmt = "t,+j(b)";
8522       offbits = 9;
8523       goto ld_st;
8524     case M_ACLR_AB:
8525       s = "aclr";
8526       treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8527       fmt = "\\,~(b)";
8528       offbits = 12;
8529       goto ld_st;
8530     case M_ASET_AB:
8531       s = "aset";
8532       treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8533       fmt = "\\,~(b)";
8534       offbits = 12;
8535       goto ld_st;
8536     case M_LB_AB:
8537       s = "lb";
8538       fmt = "t,o(b)";
8539       goto ld;
8540     case M_LBU_AB:
8541       s = "lbu";
8542       fmt = "t,o(b)";
8543       goto ld;
8544     case M_LH_AB:
8545       s = "lh";
8546       fmt = "t,o(b)";
8547       goto ld;
8548     case M_LHU_AB:
8549       s = "lhu";
8550       fmt = "t,o(b)";
8551       goto ld;
8552     case M_LW_AB:
8553       s = "lw";
8554       fmt = "t,o(b)";
8555       goto ld;
8556     case M_LWC0_AB:
8557       gas_assert (!mips_opts.micromips);
8558       s = "lwc0";
8559       fmt = "E,o(b)";
8560       /* Itbl support may require additional care here.  */
8561       coproc = 1;
8562       goto ld_st;
8563     case M_LWC1_AB:
8564       s = "lwc1";
8565       fmt = "T,o(b)";
8566       /* Itbl support may require additional care here.  */
8567       coproc = 1;
8568       goto ld_st;
8569     case M_LWC2_AB:
8570       s = "lwc2";
8571       fmt = COP12_FMT;
8572       offbits = (mips_opts.micromips ? 12 : 16);
8573       /* Itbl support may require additional care here.  */
8574       coproc = 1;
8575       goto ld_st;
8576     case M_LWC3_AB:
8577       gas_assert (!mips_opts.micromips);
8578       s = "lwc3";
8579       fmt = "E,o(b)";
8580       /* Itbl support may require additional care here.  */
8581       coproc = 1;
8582       goto ld_st;
8583     case M_LWL_AB:
8584       s = "lwl";
8585       fmt = MEM12_FMT;
8586       offbits = (mips_opts.micromips ? 12 : 16);
8587       goto ld_st;
8588     case M_LWR_AB:
8589       s = "lwr";
8590       fmt = MEM12_FMT;
8591       offbits = (mips_opts.micromips ? 12 : 16);
8592       goto ld_st;
8593     case M_LDC1_AB:
8594       s = "ldc1";
8595       fmt = "T,o(b)";
8596       /* Itbl support may require additional care here.  */
8597       coproc = 1;
8598       goto ld_st;
8599     case M_LDC2_AB:
8600       s = "ldc2";
8601       fmt = COP12_FMT;
8602       offbits = (mips_opts.micromips ? 12 : 16);
8603       /* Itbl support may require additional care here.  */
8604       coproc = 1;
8605       goto ld_st;
8606     case M_LQC2_AB:
8607       s = "lqc2";
8608       fmt = "E,o(b)";
8609       /* Itbl support may require additional care here.  */
8610       coproc = 1;
8611       goto ld_st;
8612     case M_LDC3_AB:
8613       s = "ldc3";
8614       fmt = "E,o(b)";
8615       /* Itbl support may require additional care here.  */
8616       coproc = 1;
8617       goto ld_st;
8618     case M_LDL_AB:
8619       s = "ldl";
8620       fmt = MEM12_FMT;
8621       offbits = (mips_opts.micromips ? 12 : 16);
8622       goto ld_st;
8623     case M_LDR_AB:
8624       s = "ldr";
8625       fmt = MEM12_FMT;
8626       offbits = (mips_opts.micromips ? 12 : 16);
8627       goto ld_st;
8628     case M_LL_AB:
8629       s = "ll";
8630       fmt = MEM12_FMT;
8631       offbits = (mips_opts.micromips ? 12 : 16);
8632       goto ld;
8633     case M_LLD_AB:
8634       s = "lld";
8635       fmt = MEM12_FMT;
8636       offbits = (mips_opts.micromips ? 12 : 16);
8637       goto ld;
8638     case M_LWU_AB:
8639       s = "lwu";
8640       fmt = MEM12_FMT;
8641       offbits = (mips_opts.micromips ? 12 : 16);
8642       goto ld;
8643     case M_LWP_AB:
8644       gas_assert (mips_opts.micromips);
8645       s = "lwp";
8646       fmt = "t,~(b)";
8647       offbits = 12;
8648       lp = 1;
8649       goto ld;
8650     case M_LDP_AB:
8651       gas_assert (mips_opts.micromips);
8652       s = "ldp";
8653       fmt = "t,~(b)";
8654       offbits = 12;
8655       lp = 1;
8656       goto ld;
8657     case M_LWM_AB:
8658       gas_assert (mips_opts.micromips);
8659       s = "lwm";
8660       fmt = "n,~(b)";
8661       offbits = 12;
8662       goto ld_st;
8663     case M_LDM_AB:
8664       gas_assert (mips_opts.micromips);
8665       s = "ldm";
8666       fmt = "n,~(b)";
8667       offbits = 12;
8668       goto ld_st;
8669
8670     ld:
8671       /* We don't want to use $0 as tempreg.  */
8672       if (breg == treg + lp || treg + lp == ZERO)
8673         goto ld_st;
8674       else
8675         tempreg = treg + lp;
8676       goto ld_noat;
8677
8678     case M_SB_AB:
8679       s = "sb";
8680       fmt = "t,o(b)";
8681       goto ld_st;
8682     case M_SH_AB:
8683       s = "sh";
8684       fmt = "t,o(b)";
8685       goto ld_st;
8686     case M_SW_AB:
8687       s = "sw";
8688       fmt = "t,o(b)";
8689       goto ld_st;
8690     case M_SWC0_AB:
8691       gas_assert (!mips_opts.micromips);
8692       s = "swc0";
8693       fmt = "E,o(b)";
8694       /* Itbl support may require additional care here.  */
8695       coproc = 1;
8696       goto ld_st;
8697     case M_SWC1_AB:
8698       s = "swc1";
8699       fmt = "T,o(b)";
8700       /* Itbl support may require additional care here.  */
8701       coproc = 1;
8702       goto ld_st;
8703     case M_SWC2_AB:
8704       s = "swc2";
8705       fmt = COP12_FMT;
8706       offbits = (mips_opts.micromips ? 12 : 16);
8707       /* Itbl support may require additional care here.  */
8708       coproc = 1;
8709       goto ld_st;
8710     case M_SWC3_AB:
8711       gas_assert (!mips_opts.micromips);
8712       s = "swc3";
8713       fmt = "E,o(b)";
8714       /* Itbl support may require additional care here.  */
8715       coproc = 1;
8716       goto ld_st;
8717     case M_SWL_AB:
8718       s = "swl";
8719       fmt = MEM12_FMT;
8720       offbits = (mips_opts.micromips ? 12 : 16);
8721       goto ld_st;
8722     case M_SWR_AB:
8723       s = "swr";
8724       fmt = MEM12_FMT;
8725       offbits = (mips_opts.micromips ? 12 : 16);
8726       goto ld_st;
8727     case M_SC_AB:
8728       s = "sc";
8729       fmt = MEM12_FMT;
8730       offbits = (mips_opts.micromips ? 12 : 16);
8731       goto ld_st;
8732     case M_SCD_AB:
8733       s = "scd";
8734       fmt = MEM12_FMT;
8735       offbits = (mips_opts.micromips ? 12 : 16);
8736       goto ld_st;
8737     case M_CACHE_AB:
8738       s = "cache";
8739       fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
8740       offbits = (mips_opts.micromips ? 12 : 16);
8741       goto ld_st;
8742     case M_CACHEE_AB:
8743       s = "cachee";
8744       fmt = "k,+j(b)";
8745       offbits = 9;
8746       goto ld_st;
8747     case M_PREF_AB:
8748       s = "pref";
8749       fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
8750       offbits = (mips_opts.micromips ? 12 : 16);
8751       goto ld_st;
8752     case M_PREFE_AB:
8753       s = "prefe";
8754       fmt = "k,+j(b)";
8755       offbits = 9;
8756       goto ld_st;
8757     case M_SDC1_AB:
8758       s = "sdc1";
8759       fmt = "T,o(b)";
8760       coproc = 1;
8761       /* Itbl support may require additional care here.  */
8762       goto ld_st;
8763     case M_SDC2_AB:
8764       s = "sdc2";
8765       fmt = COP12_FMT;
8766       offbits = (mips_opts.micromips ? 12 : 16);
8767       /* Itbl support may require additional care here.  */
8768       coproc = 1;
8769       goto ld_st;
8770     case M_SQC2_AB:
8771       s = "sqc2";
8772       fmt = "E,o(b)";
8773       /* Itbl support may require additional care here.  */
8774       coproc = 1;
8775       goto ld_st;
8776     case M_SDC3_AB:
8777       gas_assert (!mips_opts.micromips);
8778       s = "sdc3";
8779       fmt = "E,o(b)";
8780       /* Itbl support may require additional care here.  */
8781       coproc = 1;
8782       goto ld_st;
8783     case M_SDL_AB:
8784       s = "sdl";
8785       fmt = MEM12_FMT;
8786       offbits = (mips_opts.micromips ? 12 : 16);
8787       goto ld_st;
8788     case M_SDR_AB:
8789       s = "sdr";
8790       fmt = MEM12_FMT;
8791       offbits = (mips_opts.micromips ? 12 : 16);
8792       goto ld_st;
8793     case M_SWP_AB:
8794       gas_assert (mips_opts.micromips);
8795       s = "swp";
8796       fmt = "t,~(b)";
8797       offbits = 12;
8798       goto ld_st;
8799     case M_SDP_AB:
8800       gas_assert (mips_opts.micromips);
8801       s = "sdp";
8802       fmt = "t,~(b)";
8803       offbits = 12;
8804       goto ld_st;
8805     case M_SWM_AB:
8806       gas_assert (mips_opts.micromips);
8807       s = "swm";
8808       fmt = "n,~(b)";
8809       offbits = 12;
8810       goto ld_st;
8811     case M_SDM_AB:
8812       gas_assert (mips_opts.micromips);
8813       s = "sdm";
8814       fmt = "n,~(b)";
8815       offbits = 12;
8816
8817     ld_st:
8818       tempreg = AT;
8819     ld_noat:
8820       if (small_offset_p (0, align, 16))
8821         {
8822           /* The first case exists for M_LD_AB and M_SD_AB, which are
8823              macros for o32 but which should act like normal instructions
8824              otherwise.  */
8825           if (offbits == 16)
8826             macro_build (&offset_expr, s, fmt, treg, -1, offset_reloc[0],
8827                          offset_reloc[1], offset_reloc[2], breg);
8828           else if (small_offset_p (0, align, offbits))
8829             {
8830               if (offbits == 0)
8831                 macro_build (NULL, s, fmt, treg, breg);
8832               else
8833                 macro_build (NULL, s, fmt, treg,
8834                              (unsigned long) offset_expr.X_add_number, breg);
8835             }
8836           else
8837             {
8838               if (tempreg == AT)
8839                 used_at = 1;
8840               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8841                            tempreg, breg, -1, offset_reloc[0],
8842                            offset_reloc[1], offset_reloc[2]);
8843               if (offbits == 0)
8844                 macro_build (NULL, s, fmt, treg, tempreg);
8845               else
8846                 macro_build (NULL, s, fmt, treg, 0L, tempreg);
8847             }
8848           break;
8849         }
8850
8851       if (tempreg == AT)
8852         used_at = 1;
8853
8854       if (offset_expr.X_op != O_constant
8855           && offset_expr.X_op != O_symbol)
8856         {
8857           as_bad (_("Expression too complex"));
8858           offset_expr.X_op = O_constant;
8859         }
8860
8861       if (HAVE_32BIT_ADDRESSES
8862           && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
8863         {
8864           char value [32];
8865
8866           sprintf_vma (value, offset_expr.X_add_number);
8867           as_bad (_("Number (0x%s) larger than 32 bits"), value);
8868         }
8869
8870       /* A constant expression in PIC code can be handled just as it
8871          is in non PIC code.  */
8872       if (offset_expr.X_op == O_constant)
8873         {
8874           expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
8875                                                  offbits == 0 ? 16 : offbits);
8876           offset_expr.X_add_number -= expr1.X_add_number;
8877
8878           load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8879           if (breg != 0)
8880             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8881                          tempreg, tempreg, breg);
8882           if (offbits == 0)
8883             {
8884               if (offset_expr.X_add_number != 0)
8885                 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8886                              "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
8887               macro_build (NULL, s, fmt, treg, tempreg);
8888             }
8889           else if (offbits == 16)
8890             macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8891           else
8892             macro_build (NULL, s, fmt, treg,
8893                          (unsigned long) offset_expr.X_add_number, tempreg);
8894         }
8895       else if (offbits != 16)
8896         {
8897           /* The offset field is too narrow to be used for a low-part
8898              relocation, so load the whole address into the auxillary
8899              register.  */
8900           load_address (tempreg, &offset_expr, &used_at);
8901           if (breg != 0)
8902             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8903                          tempreg, tempreg, breg);
8904           if (offbits == 0)
8905             macro_build (NULL, s, fmt, treg, tempreg);
8906           else
8907             macro_build (NULL, s, fmt, treg, 0L, tempreg);
8908         }
8909       else if (mips_pic == NO_PIC)
8910         {
8911           /* If this is a reference to a GP relative symbol, and there
8912              is no base register, we want
8913                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
8914              Otherwise, if there is no base register, we want
8915                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
8916                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8917              If we have a constant, we need two instructions anyhow,
8918              so we always use the latter form.
8919
8920              If we have a base register, and this is a reference to a
8921              GP relative symbol, we want
8922                addu     $tempreg,$breg,$gp
8923                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
8924              Otherwise we want
8925                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
8926                addu     $tempreg,$tempreg,$breg
8927                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8928              With a constant we always use the latter case.
8929
8930              With 64bit address space and no base register and $at usable,
8931              we want
8932                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8933                lui      $at,<sym>               (BFD_RELOC_HI16_S)
8934                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8935                dsll32   $tempreg,0
8936                daddu    $tempreg,$at
8937                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8938              If we have a base register, we want
8939                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8940                lui      $at,<sym>               (BFD_RELOC_HI16_S)
8941                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8942                daddu    $at,$breg
8943                dsll32   $tempreg,0
8944                daddu    $tempreg,$at
8945                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8946
8947              Without $at we can't generate the optimal path for superscalar
8948              processors here since this would require two temporary registers.
8949                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8950                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8951                dsll     $tempreg,16
8952                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
8953                dsll     $tempreg,16
8954                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8955              If we have a base register, we want
8956                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8957                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8958                dsll     $tempreg,16
8959                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
8960                dsll     $tempreg,16
8961                daddu    $tempreg,$tempreg,$breg
8962                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8963
8964              For GP relative symbols in 64bit address space we can use
8965              the same sequence as in 32bit address space.  */
8966           if (HAVE_64BIT_SYMBOLS)
8967             {
8968               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8969                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8970                 {
8971                   relax_start (offset_expr.X_add_symbol);
8972                   if (breg == 0)
8973                     {
8974                       macro_build (&offset_expr, s, fmt, treg,
8975                                    BFD_RELOC_GPREL16, mips_gp_register);
8976                     }
8977                   else
8978                     {
8979                       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8980                                    tempreg, breg, mips_gp_register);
8981                       macro_build (&offset_expr, s, fmt, treg,
8982                                    BFD_RELOC_GPREL16, tempreg);
8983                     }
8984                   relax_switch ();
8985                 }
8986
8987               if (used_at == 0 && mips_opts.at)
8988                 {
8989                   macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8990                                BFD_RELOC_MIPS_HIGHEST);
8991                   macro_build (&offset_expr, "lui", LUI_FMT, AT,
8992                                BFD_RELOC_HI16_S);
8993                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8994                                tempreg, BFD_RELOC_MIPS_HIGHER);
8995                   if (breg != 0)
8996                     macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
8997                   macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
8998                   macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8999                   macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
9000                                tempreg);
9001                   used_at = 1;
9002                 }
9003               else
9004                 {
9005                   macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
9006                                BFD_RELOC_MIPS_HIGHEST);
9007                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
9008                                tempreg, BFD_RELOC_MIPS_HIGHER);
9009                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
9010                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
9011                                tempreg, BFD_RELOC_HI16_S);
9012                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
9013                   if (breg != 0)
9014                     macro_build (NULL, "daddu", "d,v,t",
9015                                  tempreg, tempreg, breg);
9016                   macro_build (&offset_expr, s, fmt, treg,
9017                                BFD_RELOC_LO16, tempreg);
9018                 }
9019
9020               if (mips_relax.sequence)
9021                 relax_end ();
9022               break;
9023             }
9024
9025           if (breg == 0)
9026             {
9027               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9028                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9029                 {
9030                   relax_start (offset_expr.X_add_symbol);
9031                   macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
9032                                mips_gp_register);
9033                   relax_switch ();
9034                 }
9035               macro_build_lui (&offset_expr, tempreg);
9036               macro_build (&offset_expr, s, fmt, treg,
9037                            BFD_RELOC_LO16, tempreg);
9038               if (mips_relax.sequence)
9039                 relax_end ();
9040             }
9041           else
9042             {
9043               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9044                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9045                 {
9046                   relax_start (offset_expr.X_add_symbol);
9047                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9048                                tempreg, breg, mips_gp_register);
9049                   macro_build (&offset_expr, s, fmt, treg,
9050                                BFD_RELOC_GPREL16, tempreg);
9051                   relax_switch ();
9052                 }
9053               macro_build_lui (&offset_expr, tempreg);
9054               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9055                            tempreg, tempreg, breg);
9056               macro_build (&offset_expr, s, fmt, treg,
9057                            BFD_RELOC_LO16, tempreg);
9058               if (mips_relax.sequence)
9059                 relax_end ();
9060             }
9061         }
9062       else if (!mips_big_got)
9063         {
9064           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
9065
9066           /* If this is a reference to an external symbol, we want
9067                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
9068                nop
9069                <op>     $treg,0($tempreg)
9070              Otherwise we want
9071                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
9072                nop
9073                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9074                <op>     $treg,0($tempreg)
9075
9076              For NewABI, we want
9077                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
9078                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
9079
9080              If there is a base register, we add it to $tempreg before
9081              the <op>.  If there is a constant, we stick it in the
9082              <op> instruction.  We don't handle constants larger than
9083              16 bits, because we have no way to load the upper 16 bits
9084              (actually, we could handle them for the subset of cases
9085              in which we are not using $at).  */
9086           gas_assert (offset_expr.X_op == O_symbol);
9087           if (HAVE_NEWABI)
9088             {
9089               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9090                            BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9091               if (breg != 0)
9092                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9093                              tempreg, tempreg, breg);
9094               macro_build (&offset_expr, s, fmt, treg,
9095                            BFD_RELOC_MIPS_GOT_OFST, tempreg);
9096               break;
9097             }
9098           expr1.X_add_number = offset_expr.X_add_number;
9099           offset_expr.X_add_number = 0;
9100           if (expr1.X_add_number < -0x8000
9101               || expr1.X_add_number >= 0x8000)
9102             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9103           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9104                        lw_reloc_type, mips_gp_register);
9105           load_delay_nop ();
9106           relax_start (offset_expr.X_add_symbol);
9107           relax_switch ();
9108           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
9109                        tempreg, BFD_RELOC_LO16);
9110           relax_end ();
9111           if (breg != 0)
9112             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9113                          tempreg, tempreg, breg);
9114           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
9115         }
9116       else if (mips_big_got && !HAVE_NEWABI)
9117         {
9118           int gpdelay;
9119
9120           /* If this is a reference to an external symbol, we want
9121                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
9122                addu     $tempreg,$tempreg,$gp
9123                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9124                <op>     $treg,0($tempreg)
9125              Otherwise we want
9126                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
9127                nop
9128                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9129                <op>     $treg,0($tempreg)
9130              If there is a base register, we add it to $tempreg before
9131              the <op>.  If there is a constant, we stick it in the
9132              <op> instruction.  We don't handle constants larger than
9133              16 bits, because we have no way to load the upper 16 bits
9134              (actually, we could handle them for the subset of cases
9135              in which we are not using $at).  */
9136           gas_assert (offset_expr.X_op == O_symbol);
9137           expr1.X_add_number = offset_expr.X_add_number;
9138           offset_expr.X_add_number = 0;
9139           if (expr1.X_add_number < -0x8000
9140               || expr1.X_add_number >= 0x8000)
9141             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9142           gpdelay = reg_needs_delay (mips_gp_register);
9143           relax_start (offset_expr.X_add_symbol);
9144           macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
9145                        BFD_RELOC_MIPS_GOT_HI16);
9146           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
9147                        mips_gp_register);
9148           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9149                        BFD_RELOC_MIPS_GOT_LO16, tempreg);
9150           relax_switch ();
9151           if (gpdelay)
9152             macro_build (NULL, "nop", "");
9153           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9154                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
9155           load_delay_nop ();
9156           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
9157                        tempreg, BFD_RELOC_LO16);
9158           relax_end ();
9159
9160           if (breg != 0)
9161             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9162                          tempreg, tempreg, breg);
9163           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
9164         }
9165       else if (mips_big_got && HAVE_NEWABI)
9166         {
9167           /* If this is a reference to an external symbol, we want
9168                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
9169                add      $tempreg,$tempreg,$gp
9170                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9171                <op>     $treg,<ofst>($tempreg)
9172              Otherwise, for local symbols, we want:
9173                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
9174                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
9175           gas_assert (offset_expr.X_op == O_symbol);
9176           expr1.X_add_number = offset_expr.X_add_number;
9177           offset_expr.X_add_number = 0;
9178           if (expr1.X_add_number < -0x8000
9179               || expr1.X_add_number >= 0x8000)
9180             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9181           relax_start (offset_expr.X_add_symbol);
9182           macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
9183                        BFD_RELOC_MIPS_GOT_HI16);
9184           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
9185                        mips_gp_register);
9186           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9187                        BFD_RELOC_MIPS_GOT_LO16, tempreg);
9188           if (breg != 0)
9189             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9190                          tempreg, tempreg, breg);
9191           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
9192
9193           relax_switch ();
9194           offset_expr.X_add_number = expr1.X_add_number;
9195           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9196                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9197           if (breg != 0)
9198             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9199                          tempreg, tempreg, breg);
9200           macro_build (&offset_expr, s, fmt, treg,
9201                        BFD_RELOC_MIPS_GOT_OFST, tempreg);
9202           relax_end ();
9203         }
9204       else
9205         abort ();
9206
9207       break;
9208
9209     case M_JRADDIUSP:
9210       gas_assert (mips_opts.micromips);
9211       gas_assert (mips_opts.insn32);
9212       start_noreorder ();
9213       macro_build (NULL, "jr", "s", RA);
9214       expr1.X_add_number = EXTRACT_OPERAND (1, IMMP, *ip) << 2;
9215       macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
9216       end_noreorder ();
9217       break;
9218
9219     case M_JRC:
9220       gas_assert (mips_opts.micromips);
9221       gas_assert (mips_opts.insn32);
9222       macro_build (NULL, "jr", "s", sreg);
9223       if (mips_opts.noreorder)
9224         macro_build (NULL, "nop", "");
9225       break;
9226
9227     case M_LI:
9228     case M_LI_S:
9229       load_register (treg, &imm_expr, 0);
9230       break;
9231
9232     case M_DLI:
9233       load_register (treg, &imm_expr, 1);
9234       break;
9235
9236     case M_LI_SS:
9237       if (imm_expr.X_op == O_constant)
9238         {
9239           used_at = 1;
9240           load_register (AT, &imm_expr, 0);
9241           macro_build (NULL, "mtc1", "t,G", AT, treg);
9242           break;
9243         }
9244       else
9245         {
9246           gas_assert (offset_expr.X_op == O_symbol
9247                       && strcmp (segment_name (S_GET_SEGMENT
9248                                                (offset_expr.X_add_symbol)),
9249                                  ".lit4") == 0
9250                       && offset_expr.X_add_number == 0);
9251           macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
9252                        BFD_RELOC_MIPS_LITERAL, mips_gp_register);
9253           break;
9254         }
9255
9256     case M_LI_D:
9257       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
9258          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
9259          order 32 bits of the value and the low order 32 bits are either
9260          zero or in OFFSET_EXPR.  */
9261       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
9262         {
9263           if (HAVE_64BIT_GPRS)
9264             load_register (treg, &imm_expr, 1);
9265           else
9266             {
9267               int hreg, lreg;
9268
9269               if (target_big_endian)
9270                 {
9271                   hreg = treg;
9272                   lreg = treg + 1;
9273                 }
9274               else
9275                 {
9276                   hreg = treg + 1;
9277                   lreg = treg;
9278                 }
9279
9280               if (hreg <= 31)
9281                 load_register (hreg, &imm_expr, 0);
9282               if (lreg <= 31)
9283                 {
9284                   if (offset_expr.X_op == O_absent)
9285                     move_register (lreg, 0);
9286                   else
9287                     {
9288                       gas_assert (offset_expr.X_op == O_constant);
9289                       load_register (lreg, &offset_expr, 0);
9290                     }
9291                 }
9292             }
9293           break;
9294         }
9295
9296       /* We know that sym is in the .rdata section.  First we get the
9297          upper 16 bits of the address.  */
9298       if (mips_pic == NO_PIC)
9299         {
9300           macro_build_lui (&offset_expr, AT);
9301           used_at = 1;
9302         }
9303       else
9304         {
9305           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9306                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
9307           used_at = 1;
9308         }
9309
9310       /* Now we load the register(s).  */
9311       if (HAVE_64BIT_GPRS)
9312         {
9313           used_at = 1;
9314           macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
9315         }
9316       else
9317         {
9318           used_at = 1;
9319           macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
9320           if (treg != RA)
9321             {
9322               /* FIXME: How in the world do we deal with the possible
9323                  overflow here?  */
9324               offset_expr.X_add_number += 4;
9325               macro_build (&offset_expr, "lw", "t,o(b)",
9326                            treg + 1, BFD_RELOC_LO16, AT);
9327             }
9328         }
9329       break;
9330
9331     case M_LI_DD:
9332       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
9333          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
9334          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
9335          the value and the low order 32 bits are either zero or in
9336          OFFSET_EXPR.  */
9337       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
9338         {
9339           used_at = 1;
9340           load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
9341           if (HAVE_64BIT_FPRS)
9342             {
9343               gas_assert (HAVE_64BIT_GPRS);
9344               macro_build (NULL, "dmtc1", "t,S", AT, treg);
9345             }
9346           else
9347             {
9348               macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
9349               if (offset_expr.X_op == O_absent)
9350                 macro_build (NULL, "mtc1", "t,G", 0, treg);
9351               else
9352                 {
9353                   gas_assert (offset_expr.X_op == O_constant);
9354                   load_register (AT, &offset_expr, 0);
9355                   macro_build (NULL, "mtc1", "t,G", AT, treg);
9356                 }
9357             }
9358           break;
9359         }
9360
9361       gas_assert (offset_expr.X_op == O_symbol
9362                   && offset_expr.X_add_number == 0);
9363       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
9364       if (strcmp (s, ".lit8") == 0)
9365         {
9366           breg = mips_gp_register;
9367           offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
9368           offset_reloc[1] = BFD_RELOC_UNUSED;
9369           offset_reloc[2] = BFD_RELOC_UNUSED;
9370         }
9371       else
9372         {
9373           gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
9374           used_at = 1;
9375           if (mips_pic != NO_PIC)
9376             macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9377                          BFD_RELOC_MIPS_GOT16, mips_gp_register);
9378           else
9379             {
9380               /* FIXME: This won't work for a 64 bit address.  */
9381               macro_build_lui (&offset_expr, AT);
9382             }
9383
9384           breg = AT;
9385           offset_reloc[0] = BFD_RELOC_LO16;
9386           offset_reloc[1] = BFD_RELOC_UNUSED;
9387           offset_reloc[2] = BFD_RELOC_UNUSED;
9388         }
9389       align = 8;
9390       /* Fall through */
9391
9392     case M_L_DAB:
9393       /*
9394        * The MIPS assembler seems to check for X_add_number not
9395        * being double aligned and generating:
9396        *        lui     at,%hi(foo+1)
9397        *        addu    at,at,v1
9398        *        addiu   at,at,%lo(foo+1)
9399        *        lwc1    f2,0(at)
9400        *        lwc1    f3,4(at)
9401        * But, the resulting address is the same after relocation so why
9402        * generate the extra instruction?
9403        */
9404       /* Itbl support may require additional care here.  */
9405       coproc = 1;
9406       fmt = "T,o(b)";
9407       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
9408         {
9409           s = "ldc1";
9410           goto ld_st;
9411         }
9412       s = "lwc1";
9413       goto ldd_std;
9414
9415     case M_S_DAB:
9416       gas_assert (!mips_opts.micromips);
9417       /* Itbl support may require additional care here.  */
9418       coproc = 1;
9419       fmt = "T,o(b)";
9420       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
9421         {
9422           s = "sdc1";
9423           goto ld_st;
9424         }
9425       s = "swc1";
9426       goto ldd_std;
9427
9428     case M_LQ_AB:
9429       fmt = "t,o(b)";
9430       s = "lq";
9431       goto ld;
9432
9433     case M_SQ_AB:
9434       fmt = "t,o(b)";
9435       s = "sq";
9436       goto ld_st;
9437
9438     case M_LD_AB:
9439       fmt = "t,o(b)";
9440       if (HAVE_64BIT_GPRS)
9441         {
9442           s = "ld";
9443           goto ld;
9444         }
9445       s = "lw";
9446       goto ldd_std;
9447
9448     case M_SD_AB:
9449       fmt = "t,o(b)";
9450       if (HAVE_64BIT_GPRS)
9451         {
9452           s = "sd";
9453           goto ld_st;
9454         }
9455       s = "sw";
9456
9457     ldd_std:
9458       /* Even on a big endian machine $fn comes before $fn+1.  We have
9459          to adjust when loading from memory.  We set coproc if we must
9460          load $fn+1 first.  */
9461       /* Itbl support may require additional care here.  */
9462       if (!target_big_endian)
9463         coproc = 0;
9464
9465       if (small_offset_p (0, align, 16))
9466         {
9467           ep = &offset_expr;
9468           if (!small_offset_p (4, align, 16))
9469             {
9470               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
9471                            -1, offset_reloc[0], offset_reloc[1],
9472                            offset_reloc[2]);
9473               expr1.X_add_number = 0;
9474               ep = &expr1;
9475               breg = AT;
9476               used_at = 1;
9477               offset_reloc[0] = BFD_RELOC_LO16;
9478               offset_reloc[1] = BFD_RELOC_UNUSED;
9479               offset_reloc[2] = BFD_RELOC_UNUSED;
9480             }
9481           if (strcmp (s, "lw") == 0 && treg == breg)
9482             {
9483               ep->X_add_number += 4;
9484               macro_build (ep, s, fmt, treg + 1, -1, offset_reloc[0],
9485                            offset_reloc[1], offset_reloc[2], breg);
9486               ep->X_add_number -= 4;
9487               macro_build (ep, s, fmt, treg, -1, offset_reloc[0],
9488                            offset_reloc[1], offset_reloc[2], breg);
9489             }
9490           else
9491             {
9492               macro_build (ep, s, fmt, coproc ? treg + 1 : treg, -1,
9493                            offset_reloc[0], offset_reloc[1], offset_reloc[2],
9494                            breg);
9495               ep->X_add_number += 4;
9496               macro_build (ep, s, fmt, coproc ? treg : treg + 1, -1,
9497                            offset_reloc[0], offset_reloc[1], offset_reloc[2],
9498                            breg);
9499             }
9500           break;
9501         }
9502
9503       if (offset_expr.X_op != O_symbol
9504           && offset_expr.X_op != O_constant)
9505         {
9506           as_bad (_("Expression too complex"));
9507           offset_expr.X_op = O_constant;
9508         }
9509
9510       if (HAVE_32BIT_ADDRESSES
9511           && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
9512         {
9513           char value [32];
9514
9515           sprintf_vma (value, offset_expr.X_add_number);
9516           as_bad (_("Number (0x%s) larger than 32 bits"), value);
9517         }
9518
9519       if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
9520         {
9521           /* If this is a reference to a GP relative symbol, we want
9522                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
9523                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
9524              If we have a base register, we use this
9525                addu     $at,$breg,$gp
9526                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
9527                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
9528              If this is not a GP relative symbol, we want
9529                lui      $at,<sym>               (BFD_RELOC_HI16_S)
9530                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
9531                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
9532              If there is a base register, we add it to $at after the
9533              lui instruction.  If there is a constant, we always use
9534              the last case.  */
9535           if (offset_expr.X_op == O_symbol
9536               && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9537               && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9538             {
9539               relax_start (offset_expr.X_add_symbol);
9540               if (breg == 0)
9541                 {
9542                   tempreg = mips_gp_register;
9543                 }
9544               else
9545                 {
9546                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9547                                AT, breg, mips_gp_register);
9548                   tempreg = AT;
9549                   used_at = 1;
9550                 }
9551
9552               /* Itbl support may require additional care here.  */
9553               macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9554                            BFD_RELOC_GPREL16, tempreg);
9555               offset_expr.X_add_number += 4;
9556
9557               /* Set mips_optimize to 2 to avoid inserting an
9558                  undesired nop.  */
9559               hold_mips_optimize = mips_optimize;
9560               mips_optimize = 2;
9561               /* Itbl support may require additional care here.  */
9562               macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9563                            BFD_RELOC_GPREL16, tempreg);
9564               mips_optimize = hold_mips_optimize;
9565
9566               relax_switch ();
9567
9568               offset_expr.X_add_number -= 4;
9569             }
9570           used_at = 1;
9571           if (offset_high_part (offset_expr.X_add_number, 16)
9572               != offset_high_part (offset_expr.X_add_number + 4, 16))
9573             {
9574               load_address (AT, &offset_expr, &used_at);
9575               offset_expr.X_op = O_constant;
9576               offset_expr.X_add_number = 0;
9577             }
9578           else
9579             macro_build_lui (&offset_expr, AT);
9580           if (breg != 0)
9581             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9582           /* Itbl support may require additional care here.  */
9583           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9584                        BFD_RELOC_LO16, AT);
9585           /* FIXME: How do we handle overflow here?  */
9586           offset_expr.X_add_number += 4;
9587           /* Itbl support may require additional care here.  */
9588           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9589                        BFD_RELOC_LO16, AT);
9590           if (mips_relax.sequence)
9591             relax_end ();
9592         }
9593       else if (!mips_big_got)
9594         {
9595           /* If this is a reference to an external symbol, we want
9596                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
9597                nop
9598                <op>     $treg,0($at)
9599                <op>     $treg+1,4($at)
9600              Otherwise we want
9601                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
9602                nop
9603                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
9604                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
9605              If there is a base register we add it to $at before the
9606              lwc1 instructions.  If there is a constant we include it
9607              in the lwc1 instructions.  */
9608           used_at = 1;
9609           expr1.X_add_number = offset_expr.X_add_number;
9610           if (expr1.X_add_number < -0x8000
9611               || expr1.X_add_number >= 0x8000 - 4)
9612             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9613           load_got_offset (AT, &offset_expr);
9614           load_delay_nop ();
9615           if (breg != 0)
9616             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9617
9618           /* Set mips_optimize to 2 to avoid inserting an undesired
9619              nop.  */
9620           hold_mips_optimize = mips_optimize;
9621           mips_optimize = 2;
9622
9623           /* Itbl support may require additional care here.  */
9624           relax_start (offset_expr.X_add_symbol);
9625           macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9626                        BFD_RELOC_LO16, AT);
9627           expr1.X_add_number += 4;
9628           macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9629                        BFD_RELOC_LO16, AT);
9630           relax_switch ();
9631           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9632                        BFD_RELOC_LO16, AT);
9633           offset_expr.X_add_number += 4;
9634           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9635                        BFD_RELOC_LO16, AT);
9636           relax_end ();
9637
9638           mips_optimize = hold_mips_optimize;
9639         }
9640       else if (mips_big_got)
9641         {
9642           int gpdelay;
9643
9644           /* If this is a reference to an external symbol, we want
9645                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
9646                addu     $at,$at,$gp
9647                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
9648                nop
9649                <op>     $treg,0($at)
9650                <op>     $treg+1,4($at)
9651              Otherwise we want
9652                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
9653                nop
9654                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
9655                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
9656              If there is a base register we add it to $at before the
9657              lwc1 instructions.  If there is a constant we include it
9658              in the lwc1 instructions.  */
9659           used_at = 1;
9660           expr1.X_add_number = offset_expr.X_add_number;
9661           offset_expr.X_add_number = 0;
9662           if (expr1.X_add_number < -0x8000
9663               || expr1.X_add_number >= 0x8000 - 4)
9664             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9665           gpdelay = reg_needs_delay (mips_gp_register);
9666           relax_start (offset_expr.X_add_symbol);
9667           macro_build (&offset_expr, "lui", LUI_FMT,
9668                        AT, BFD_RELOC_MIPS_GOT_HI16);
9669           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9670                        AT, AT, mips_gp_register);
9671           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9672                        AT, BFD_RELOC_MIPS_GOT_LO16, AT);
9673           load_delay_nop ();
9674           if (breg != 0)
9675             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9676           /* Itbl support may require additional care here.  */
9677           macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9678                        BFD_RELOC_LO16, AT);
9679           expr1.X_add_number += 4;
9680
9681           /* Set mips_optimize to 2 to avoid inserting an undesired
9682              nop.  */
9683           hold_mips_optimize = mips_optimize;
9684           mips_optimize = 2;
9685           /* Itbl support may require additional care here.  */
9686           macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9687                        BFD_RELOC_LO16, AT);
9688           mips_optimize = hold_mips_optimize;
9689           expr1.X_add_number -= 4;
9690
9691           relax_switch ();
9692           offset_expr.X_add_number = expr1.X_add_number;
9693           if (gpdelay)
9694             macro_build (NULL, "nop", "");
9695           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9696                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
9697           load_delay_nop ();
9698           if (breg != 0)
9699             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9700           /* Itbl support may require additional care here.  */
9701           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9702                        BFD_RELOC_LO16, AT);
9703           offset_expr.X_add_number += 4;
9704
9705           /* Set mips_optimize to 2 to avoid inserting an undesired
9706              nop.  */
9707           hold_mips_optimize = mips_optimize;
9708           mips_optimize = 2;
9709           /* Itbl support may require additional care here.  */
9710           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9711                        BFD_RELOC_LO16, AT);
9712           mips_optimize = hold_mips_optimize;
9713           relax_end ();
9714         }
9715       else
9716         abort ();
9717
9718       break;
9719         
9720     case M_SAA_AB:
9721       s = "saa";
9722       offbits = 0;
9723       fmt = "t,(b)";
9724       goto ld_st;
9725     case M_SAAD_AB:
9726       s = "saad";
9727       offbits = 0;
9728       fmt = "t,(b)";
9729       goto ld_st;
9730
9731    /* New code added to support COPZ instructions.
9732       This code builds table entries out of the macros in mip_opcodes.
9733       R4000 uses interlocks to handle coproc delays.
9734       Other chips (like the R3000) require nops to be inserted for delays.
9735
9736       FIXME: Currently, we require that the user handle delays.
9737       In order to fill delay slots for non-interlocked chips,
9738       we must have a way to specify delays based on the coprocessor.
9739       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9740       What are the side-effects of the cop instruction?
9741       What cache support might we have and what are its effects?
9742       Both coprocessor & memory require delays. how long???
9743       What registers are read/set/modified?
9744
9745       If an itbl is provided to interpret cop instructions,
9746       this knowledge can be encoded in the itbl spec.  */
9747
9748     case M_COP0:
9749       s = "c0";
9750       goto copz;
9751     case M_COP1:
9752       s = "c1";
9753       goto copz;
9754     case M_COP2:
9755       s = "c2";
9756       goto copz;
9757     case M_COP3:
9758       s = "c3";
9759     copz:
9760       gas_assert (!mips_opts.micromips);
9761       /* For now we just do C (same as Cz).  The parameter will be
9762          stored in insn_opcode by mips_ip.  */
9763       macro_build (NULL, s, "C", ip->insn_opcode);
9764       break;
9765
9766     case M_MOVE:
9767       move_register (dreg, sreg);
9768       break;
9769
9770     case M_MOVEP:
9771       gas_assert (mips_opts.micromips);
9772       gas_assert (mips_opts.insn32);
9773       dreg = micromips_to_32_reg_h_map1[EXTRACT_OPERAND (1, MH, *ip)];
9774       breg = micromips_to_32_reg_h_map2[EXTRACT_OPERAND (1, MH, *ip)];
9775       sreg = micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
9776       treg = micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
9777       move_register (dreg, sreg);
9778       move_register (breg, treg);
9779       break;
9780
9781     case M_DMUL:
9782       dbl = 1;
9783     case M_MUL:
9784       if (mips_opts.arch == CPU_R5900)
9785         {
9786           macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", dreg, sreg, treg);
9787         }
9788       else
9789         {
9790       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
9791       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9792         }
9793       break;
9794
9795     case M_DMUL_I:
9796       dbl = 1;
9797     case M_MUL_I:
9798       /* The MIPS assembler some times generates shifts and adds.  I'm
9799          not trying to be that fancy. GCC should do this for us
9800          anyway.  */
9801       used_at = 1;
9802       load_register (AT, &imm_expr, dbl);
9803       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
9804       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9805       break;
9806
9807     case M_DMULO_I:
9808       dbl = 1;
9809     case M_MULO_I:
9810       imm = 1;
9811       goto do_mulo;
9812
9813     case M_DMULO:
9814       dbl = 1;
9815     case M_MULO:
9816     do_mulo:
9817       start_noreorder ();
9818       used_at = 1;
9819       if (imm)
9820         load_register (AT, &imm_expr, dbl);
9821       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
9822       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9823       macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9824       macro_build (NULL, "mfhi", MFHL_FMT, AT);
9825       if (mips_trap)
9826         macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
9827       else
9828         {
9829           if (mips_opts.micromips)
9830             micromips_label_expr (&label_expr);
9831           else
9832             label_expr.X_add_number = 8;
9833           macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
9834           macro_build (NULL, "nop", "");
9835           macro_build (NULL, "break", BRK_FMT, 6);
9836           if (mips_opts.micromips)
9837             micromips_add_label ();
9838         }
9839       end_noreorder ();
9840       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9841       break;
9842
9843     case M_DMULOU_I:
9844       dbl = 1;
9845     case M_MULOU_I:
9846       imm = 1;
9847       goto do_mulou;
9848
9849     case M_DMULOU:
9850       dbl = 1;
9851     case M_MULOU:
9852     do_mulou:
9853       start_noreorder ();
9854       used_at = 1;
9855       if (imm)
9856         load_register (AT, &imm_expr, dbl);
9857       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
9858                    sreg, imm ? AT : treg);
9859       macro_build (NULL, "mfhi", MFHL_FMT, AT);
9860       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9861       if (mips_trap)
9862         macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
9863       else
9864         {
9865           if (mips_opts.micromips)
9866             micromips_label_expr (&label_expr);
9867           else
9868             label_expr.X_add_number = 8;
9869           macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
9870           macro_build (NULL, "nop", "");
9871           macro_build (NULL, "break", BRK_FMT, 6);
9872           if (mips_opts.micromips)
9873             micromips_add_label ();
9874         }
9875       end_noreorder ();
9876       break;
9877
9878     case M_DROL:
9879       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9880         {
9881           if (dreg == sreg)
9882             {
9883               tempreg = AT;
9884               used_at = 1;
9885             }
9886           else
9887             {
9888               tempreg = dreg;
9889             }
9890           macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9891           macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
9892           break;
9893         }
9894       used_at = 1;
9895       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9896       macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9897       macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9898       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9899       break;
9900
9901     case M_ROL:
9902       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9903         {
9904           if (dreg == sreg)
9905             {
9906               tempreg = AT;
9907               used_at = 1;
9908             }
9909           else
9910             {
9911               tempreg = dreg;
9912             }
9913           macro_build (NULL, "negu", "d,w", tempreg, treg);
9914           macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
9915           break;
9916         }
9917       used_at = 1;
9918       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9919       macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9920       macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9921       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9922       break;
9923
9924     case M_DROL_I:
9925       {
9926         unsigned int rot;
9927         char *l;
9928         char *rr;
9929
9930         if (imm_expr.X_op != O_constant)
9931           as_bad (_("Improper rotate count"));
9932         rot = imm_expr.X_add_number & 0x3f;
9933         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9934           {
9935             rot = (64 - rot) & 0x3f;
9936             if (rot >= 32)
9937               macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9938             else
9939               macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9940             break;
9941           }
9942         if (rot == 0)
9943           {
9944             macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9945             break;
9946           }
9947         l = (rot < 0x20) ? "dsll" : "dsll32";
9948         rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
9949         rot &= 0x1f;
9950         used_at = 1;
9951         macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9952         macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9953         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9954       }
9955       break;
9956
9957     case M_ROL_I:
9958       {
9959         unsigned int rot;
9960
9961         if (imm_expr.X_op != O_constant)
9962           as_bad (_("Improper rotate count"));
9963         rot = imm_expr.X_add_number & 0x1f;
9964         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9965           {
9966             macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
9967             break;
9968           }
9969         if (rot == 0)
9970           {
9971             macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9972             break;
9973           }
9974         used_at = 1;
9975         macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9976         macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9977         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9978       }
9979       break;
9980
9981     case M_DROR:
9982       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9983         {
9984           macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
9985           break;
9986         }
9987       used_at = 1;
9988       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9989       macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9990       macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9991       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9992       break;
9993
9994     case M_ROR:
9995       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9996         {
9997           macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
9998           break;
9999         }
10000       used_at = 1;
10001       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
10002       macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
10003       macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
10004       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
10005       break;
10006
10007     case M_DROR_I:
10008       {
10009         unsigned int rot;
10010         char *l;
10011         char *rr;
10012
10013         if (imm_expr.X_op != O_constant)
10014           as_bad (_("Improper rotate count"));
10015         rot = imm_expr.X_add_number & 0x3f;
10016         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
10017           {
10018             if (rot >= 32)
10019               macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
10020             else
10021               macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
10022             break;
10023           }
10024         if (rot == 0)
10025           {
10026             macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
10027             break;
10028           }
10029         rr = (rot < 0x20) ? "dsrl" : "dsrl32";
10030         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
10031         rot &= 0x1f;
10032         used_at = 1;
10033         macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
10034         macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
10035         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
10036       }
10037       break;
10038
10039     case M_ROR_I:
10040       {
10041         unsigned int rot;
10042
10043         if (imm_expr.X_op != O_constant)
10044           as_bad (_("Improper rotate count"));
10045         rot = imm_expr.X_add_number & 0x1f;
10046         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
10047           {
10048             macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
10049             break;
10050           }
10051         if (rot == 0)
10052           {
10053             macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
10054             break;
10055           }
10056         used_at = 1;
10057         macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
10058         macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
10059         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
10060       }
10061       break;
10062
10063     case M_SEQ:
10064       if (sreg == 0)
10065         macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
10066       else if (treg == 0)
10067         macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10068       else
10069         {
10070           macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
10071           macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
10072         }
10073       break;
10074
10075     case M_SEQ_I:
10076       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
10077         {
10078           macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10079           break;
10080         }
10081       if (sreg == 0)
10082         {
10083           as_warn (_("Instruction %s: result is always false"),
10084                    ip->insn_mo->name);
10085           move_register (dreg, 0);
10086           break;
10087         }
10088       if (CPU_HAS_SEQ (mips_opts.arch)
10089           && -512 <= imm_expr.X_add_number
10090           && imm_expr.X_add_number < 512)
10091         {
10092           macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
10093                        (int) imm_expr.X_add_number);
10094           break;
10095         }
10096       if (imm_expr.X_op == O_constant
10097           && imm_expr.X_add_number >= 0
10098           && imm_expr.X_add_number < 0x10000)
10099         {
10100           macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
10101         }
10102       else if (imm_expr.X_op == O_constant
10103                && imm_expr.X_add_number > -0x8000
10104                && imm_expr.X_add_number < 0)
10105         {
10106           imm_expr.X_add_number = -imm_expr.X_add_number;
10107           macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
10108                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10109         }
10110       else if (CPU_HAS_SEQ (mips_opts.arch))
10111         {
10112           used_at = 1;
10113           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10114           macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
10115           break;
10116         }
10117       else
10118         {
10119           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10120           macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
10121           used_at = 1;
10122         }
10123       macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
10124       break;
10125
10126     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
10127       s = "slt";
10128       goto sge;
10129     case M_SGEU:
10130       s = "sltu";
10131     sge:
10132       macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
10133       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
10134       break;
10135
10136     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
10137     case M_SGEU_I:
10138       if (imm_expr.X_op == O_constant
10139           && imm_expr.X_add_number >= -0x8000
10140           && imm_expr.X_add_number < 0x8000)
10141         {
10142           macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
10143                        dreg, sreg, BFD_RELOC_LO16);
10144         }
10145       else
10146         {
10147           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10148           macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
10149                        dreg, sreg, AT);
10150           used_at = 1;
10151         }
10152       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
10153       break;
10154
10155     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
10156       s = "slt";
10157       goto sgt;
10158     case M_SGTU:
10159       s = "sltu";
10160     sgt:
10161       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
10162       break;
10163
10164     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
10165       s = "slt";
10166       goto sgti;
10167     case M_SGTU_I:
10168       s = "sltu";
10169     sgti:
10170       used_at = 1;
10171       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10172       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
10173       break;
10174
10175     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
10176       s = "slt";
10177       goto sle;
10178     case M_SLEU:
10179       s = "sltu";
10180     sle:
10181       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
10182       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
10183       break;
10184
10185     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
10186       s = "slt";
10187       goto slei;
10188     case M_SLEU_I:
10189       s = "sltu";
10190     slei:
10191       used_at = 1;
10192       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10193       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
10194       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
10195       break;
10196
10197     case M_SLT_I:
10198       if (imm_expr.X_op == O_constant
10199           && imm_expr.X_add_number >= -0x8000
10200           && imm_expr.X_add_number < 0x8000)
10201         {
10202           macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10203           break;
10204         }
10205       used_at = 1;
10206       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10207       macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
10208       break;
10209
10210     case M_SLTU_I:
10211       if (imm_expr.X_op == O_constant
10212           && imm_expr.X_add_number >= -0x8000
10213           && imm_expr.X_add_number < 0x8000)
10214         {
10215           macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
10216                        BFD_RELOC_LO16);
10217           break;
10218         }
10219       used_at = 1;
10220       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10221       macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
10222       break;
10223
10224     case M_SNE:
10225       if (sreg == 0)
10226         macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
10227       else if (treg == 0)
10228         macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
10229       else
10230         {
10231           macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
10232           macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
10233         }
10234       break;
10235
10236     case M_SNE_I:
10237       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
10238         {
10239           macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
10240           break;
10241         }
10242       if (sreg == 0)
10243         {
10244           as_warn (_("Instruction %s: result is always true"),
10245                    ip->insn_mo->name);
10246           macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
10247                        dreg, 0, BFD_RELOC_LO16);
10248           break;
10249         }
10250       if (CPU_HAS_SEQ (mips_opts.arch)
10251           && -512 <= imm_expr.X_add_number
10252           && imm_expr.X_add_number < 512)
10253         {
10254           macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
10255                        (int) imm_expr.X_add_number);
10256           break;
10257         }
10258       if (imm_expr.X_op == O_constant
10259           && imm_expr.X_add_number >= 0
10260           && imm_expr.X_add_number < 0x10000)
10261         {
10262           macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
10263         }
10264       else if (imm_expr.X_op == O_constant
10265                && imm_expr.X_add_number > -0x8000
10266                && imm_expr.X_add_number < 0)
10267         {
10268           imm_expr.X_add_number = -imm_expr.X_add_number;
10269           macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
10270                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10271         }
10272       else if (CPU_HAS_SEQ (mips_opts.arch))
10273         {
10274           used_at = 1;
10275           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10276           macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
10277           break;
10278         }
10279       else
10280         {
10281           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10282           macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
10283           used_at = 1;
10284         }
10285       macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
10286       break;
10287
10288     case M_SUB_I:
10289       s = "addi";
10290       s2 = "sub";
10291       goto do_subi;
10292     case M_SUBU_I:
10293       s = "addiu";
10294       s2 = "subu";
10295       goto do_subi;
10296     case M_DSUB_I:
10297       dbl = 1;
10298       s = "daddi";
10299       s2 = "dsub";
10300       if (!mips_opts.micromips)
10301         goto do_subi;
10302       if (imm_expr.X_op == O_constant
10303           && imm_expr.X_add_number > -0x200
10304           && imm_expr.X_add_number <= 0x200)
10305         {
10306           macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
10307           break;
10308         }
10309       goto do_subi_i;
10310     case M_DSUBU_I:
10311       dbl = 1;
10312       s = "daddiu";
10313       s2 = "dsubu";
10314     do_subi:
10315       if (imm_expr.X_op == O_constant
10316           && imm_expr.X_add_number > -0x8000
10317           && imm_expr.X_add_number <= 0x8000)
10318         {
10319           imm_expr.X_add_number = -imm_expr.X_add_number;
10320           macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10321           break;
10322         }
10323     do_subi_i:
10324       used_at = 1;
10325       load_register (AT, &imm_expr, dbl);
10326       macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
10327       break;
10328
10329     case M_TEQ_I:
10330       s = "teq";
10331       goto trap;
10332     case M_TGE_I:
10333       s = "tge";
10334       goto trap;
10335     case M_TGEU_I:
10336       s = "tgeu";
10337       goto trap;
10338     case M_TLT_I:
10339       s = "tlt";
10340       goto trap;
10341     case M_TLTU_I:
10342       s = "tltu";
10343       goto trap;
10344     case M_TNE_I:
10345       s = "tne";
10346     trap:
10347       used_at = 1;
10348       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10349       macro_build (NULL, s, "s,t", sreg, AT);
10350       break;
10351
10352     case M_TRUNCWS:
10353     case M_TRUNCWD:
10354       gas_assert (!mips_opts.micromips);
10355       gas_assert (mips_opts.isa == ISA_MIPS1);
10356       used_at = 1;
10357       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
10358       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
10359
10360       /*
10361        * Is the double cfc1 instruction a bug in the mips assembler;
10362        * or is there a reason for it?
10363        */
10364       start_noreorder ();
10365       macro_build (NULL, "cfc1", "t,G", treg, RA);
10366       macro_build (NULL, "cfc1", "t,G", treg, RA);
10367       macro_build (NULL, "nop", "");
10368       expr1.X_add_number = 3;
10369       macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
10370       expr1.X_add_number = 2;
10371       macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
10372       macro_build (NULL, "ctc1", "t,G", AT, RA);
10373       macro_build (NULL, "nop", "");
10374       macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
10375                    dreg, sreg);
10376       macro_build (NULL, "ctc1", "t,G", treg, RA);
10377       macro_build (NULL, "nop", "");
10378       end_noreorder ();
10379       break;
10380
10381     case M_ULH_AB:
10382       s = "lb";
10383       s2 = "lbu";
10384       off = 1;
10385       goto uld_st;
10386     case M_ULHU_AB:
10387       s = "lbu";
10388       s2 = "lbu";
10389       off = 1;
10390       goto uld_st;
10391     case M_ULW_AB:
10392       s = "lwl";
10393       s2 = "lwr";
10394       offbits = (mips_opts.micromips ? 12 : 16);
10395       off = 3;
10396       goto uld_st;
10397     case M_ULD_AB:
10398       s = "ldl";
10399       s2 = "ldr";
10400       offbits = (mips_opts.micromips ? 12 : 16);
10401       off = 7;
10402       goto uld_st;
10403     case M_USH_AB:
10404       s = "sb";
10405       s2 = "sb";
10406       off = 1;
10407       ust = 1;
10408       goto uld_st;
10409     case M_USW_AB:
10410       s = "swl";
10411       s2 = "swr";
10412       offbits = (mips_opts.micromips ? 12 : 16);
10413       off = 3;
10414       ust = 1;
10415       goto uld_st;
10416     case M_USD_AB:
10417       s = "sdl";
10418       s2 = "sdr";
10419       offbits = (mips_opts.micromips ? 12 : 16);
10420       off = 7;
10421       ust = 1;
10422
10423     uld_st:
10424       large_offset = !small_offset_p (off, align, offbits);
10425       ep = &offset_expr;
10426       expr1.X_add_number = 0;
10427       if (large_offset)
10428         {
10429           used_at = 1;
10430           tempreg = AT;
10431           if (small_offset_p (0, align, 16))
10432             macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
10433                          offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10434           else
10435             {
10436               load_address (tempreg, ep, &used_at);
10437               if (breg != 0)
10438                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10439                              tempreg, tempreg, breg);
10440             }
10441           offset_reloc[0] = BFD_RELOC_LO16;
10442           offset_reloc[1] = BFD_RELOC_UNUSED;
10443           offset_reloc[2] = BFD_RELOC_UNUSED;
10444           breg = tempreg;
10445           tempreg = treg;
10446           ep = &expr1;
10447         }
10448       else if (!ust && treg == breg)
10449         {
10450           used_at = 1;
10451           tempreg = AT;
10452         }
10453       else
10454         tempreg = treg;
10455
10456       if (off == 1)
10457         goto ulh_sh;
10458
10459       if (!target_big_endian)
10460         ep->X_add_number += off;
10461       if (offbits == 12)
10462         macro_build (NULL, s, "t,~(b)",
10463                      tempreg, (unsigned long) ep->X_add_number, breg);
10464       else
10465         macro_build (ep, s, "t,o(b)", tempreg, -1,
10466                      offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
10467
10468       if (!target_big_endian)
10469         ep->X_add_number -= off;
10470       else
10471         ep->X_add_number += off;
10472       if (offbits == 12)
10473         macro_build (NULL, s2, "t,~(b)",
10474                      tempreg, (unsigned long) ep->X_add_number, breg);
10475       else
10476         macro_build (ep, s2, "t,o(b)", tempreg, -1,
10477                      offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
10478
10479       /* If necessary, move the result in tempreg to the final destination.  */
10480       if (!ust && treg != tempreg)
10481         {
10482           /* Protect second load's delay slot.  */
10483           load_delay_nop ();
10484           move_register (treg, tempreg);
10485         }
10486       break;
10487
10488     ulh_sh:
10489       used_at = 1;
10490       if (target_big_endian == ust)
10491         ep->X_add_number += off;
10492       tempreg = ust || large_offset ? treg : AT;
10493       macro_build (ep, s, "t,o(b)", tempreg, -1,
10494                    offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
10495
10496       /* For halfword transfers we need a temporary register to shuffle
10497          bytes.  Unfortunately for M_USH_A we have none available before
10498          the next store as AT holds the base address.  We deal with this
10499          case by clobbering TREG and then restoring it as with ULH.  */
10500       tempreg = ust == large_offset ? treg : AT;
10501       if (ust)
10502         macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10503
10504       if (target_big_endian == ust)
10505         ep->X_add_number -= off;
10506       else
10507         ep->X_add_number += off;
10508       macro_build (ep, s2, "t,o(b)", tempreg, -1,
10509                    offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
10510
10511       /* For M_USH_A re-retrieve the LSB.  */
10512       if (ust && large_offset)
10513         {
10514           if (target_big_endian)
10515             ep->X_add_number += off;
10516           else
10517             ep->X_add_number -= off;
10518           macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
10519                        offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
10520         }
10521       /* For ULH and M_USH_A OR the LSB in.  */
10522       if (!ust || large_offset)
10523         {
10524           tempreg = !large_offset ? AT : treg;
10525           macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10526           macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10527         }
10528       break;
10529
10530     default:
10531       /* FIXME: Check if this is one of the itbl macros, since they
10532          are added dynamically.  */
10533       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10534       break;
10535     }
10536   if (!mips_opts.at && used_at)
10537     as_bad (_("Macro used $at after \".set noat\""));
10538 }
10539
10540 /* Implement macros in mips16 mode.  */
10541
10542 static void
10543 mips16_macro (struct mips_cl_insn *ip)
10544 {
10545   int mask;
10546   int xreg, yreg, zreg, tmp;
10547   expressionS expr1;
10548   int dbl;
10549   const char *s, *s2, *s3;
10550
10551   mask = ip->insn_mo->mask;
10552
10553   xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10554   yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10555   zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
10556
10557   expr1.X_op = O_constant;
10558   expr1.X_op_symbol = NULL;
10559   expr1.X_add_symbol = NULL;
10560   expr1.X_add_number = 1;
10561
10562   dbl = 0;
10563
10564   switch (mask)
10565     {
10566     default:
10567       abort ();
10568
10569     case M_DDIV_3:
10570       dbl = 1;
10571     case M_DIV_3:
10572       s = "mflo";
10573       goto do_div3;
10574     case M_DREM_3:
10575       dbl = 1;
10576     case M_REM_3:
10577       s = "mfhi";
10578     do_div3:
10579       start_noreorder ();
10580       macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
10581       expr1.X_add_number = 2;
10582       macro_build (&expr1, "bnez", "x,p", yreg);
10583       macro_build (NULL, "break", "6", 7);
10584
10585       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10586          since that causes an overflow.  We should do that as well,
10587          but I don't see how to do the comparisons without a temporary
10588          register.  */
10589       end_noreorder ();
10590       macro_build (NULL, s, "x", zreg);
10591       break;
10592
10593     case M_DIVU_3:
10594       s = "divu";
10595       s2 = "mflo";
10596       goto do_divu3;
10597     case M_REMU_3:
10598       s = "divu";
10599       s2 = "mfhi";
10600       goto do_divu3;
10601     case M_DDIVU_3:
10602       s = "ddivu";
10603       s2 = "mflo";
10604       goto do_divu3;
10605     case M_DREMU_3:
10606       s = "ddivu";
10607       s2 = "mfhi";
10608     do_divu3:
10609       start_noreorder ();
10610       macro_build (NULL, s, "0,x,y", xreg, yreg);
10611       expr1.X_add_number = 2;
10612       macro_build (&expr1, "bnez", "x,p", yreg);
10613       macro_build (NULL, "break", "6", 7);
10614       end_noreorder ();
10615       macro_build (NULL, s2, "x", zreg);
10616       break;
10617
10618     case M_DMUL:
10619       dbl = 1;
10620     case M_MUL:
10621       macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10622       macro_build (NULL, "mflo", "x", zreg);
10623       break;
10624
10625     case M_DSUBU_I:
10626       dbl = 1;
10627       goto do_subu;
10628     case M_SUBU_I:
10629     do_subu:
10630       if (imm_expr.X_op != O_constant)
10631         as_bad (_("Unsupported large constant"));
10632       imm_expr.X_add_number = -imm_expr.X_add_number;
10633       macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
10634       break;
10635
10636     case M_SUBU_I_2:
10637       if (imm_expr.X_op != O_constant)
10638         as_bad (_("Unsupported large constant"));
10639       imm_expr.X_add_number = -imm_expr.X_add_number;
10640       macro_build (&imm_expr, "addiu", "x,k", xreg);
10641       break;
10642
10643     case M_DSUBU_I_2:
10644       if (imm_expr.X_op != O_constant)
10645         as_bad (_("Unsupported large constant"));
10646       imm_expr.X_add_number = -imm_expr.X_add_number;
10647       macro_build (&imm_expr, "daddiu", "y,j", yreg);
10648       break;
10649
10650     case M_BEQ:
10651       s = "cmp";
10652       s2 = "bteqz";
10653       goto do_branch;
10654     case M_BNE:
10655       s = "cmp";
10656       s2 = "btnez";
10657       goto do_branch;
10658     case M_BLT:
10659       s = "slt";
10660       s2 = "btnez";
10661       goto do_branch;
10662     case M_BLTU:
10663       s = "sltu";
10664       s2 = "btnez";
10665       goto do_branch;
10666     case M_BLE:
10667       s = "slt";
10668       s2 = "bteqz";
10669       goto do_reverse_branch;
10670     case M_BLEU:
10671       s = "sltu";
10672       s2 = "bteqz";
10673       goto do_reverse_branch;
10674     case M_BGE:
10675       s = "slt";
10676       s2 = "bteqz";
10677       goto do_branch;
10678     case M_BGEU:
10679       s = "sltu";
10680       s2 = "bteqz";
10681       goto do_branch;
10682     case M_BGT:
10683       s = "slt";
10684       s2 = "btnez";
10685       goto do_reverse_branch;
10686     case M_BGTU:
10687       s = "sltu";
10688       s2 = "btnez";
10689
10690     do_reverse_branch:
10691       tmp = xreg;
10692       xreg = yreg;
10693       yreg = tmp;
10694
10695     do_branch:
10696       macro_build (NULL, s, "x,y", xreg, yreg);
10697       macro_build (&offset_expr, s2, "p");
10698       break;
10699
10700     case M_BEQ_I:
10701       s = "cmpi";
10702       s2 = "bteqz";
10703       s3 = "x,U";
10704       goto do_branch_i;
10705     case M_BNE_I:
10706       s = "cmpi";
10707       s2 = "btnez";
10708       s3 = "x,U";
10709       goto do_branch_i;
10710     case M_BLT_I:
10711       s = "slti";
10712       s2 = "btnez";
10713       s3 = "x,8";
10714       goto do_branch_i;
10715     case M_BLTU_I:
10716       s = "sltiu";
10717       s2 = "btnez";
10718       s3 = "x,8";
10719       goto do_branch_i;
10720     case M_BLE_I:
10721       s = "slti";
10722       s2 = "btnez";
10723       s3 = "x,8";
10724       goto do_addone_branch_i;
10725     case M_BLEU_I:
10726       s = "sltiu";
10727       s2 = "btnez";
10728       s3 = "x,8";
10729       goto do_addone_branch_i;
10730     case M_BGE_I:
10731       s = "slti";
10732       s2 = "bteqz";
10733       s3 = "x,8";
10734       goto do_branch_i;
10735     case M_BGEU_I:
10736       s = "sltiu";
10737       s2 = "bteqz";
10738       s3 = "x,8";
10739       goto do_branch_i;
10740     case M_BGT_I:
10741       s = "slti";
10742       s2 = "bteqz";
10743       s3 = "x,8";
10744       goto do_addone_branch_i;
10745     case M_BGTU_I:
10746       s = "sltiu";
10747       s2 = "bteqz";
10748       s3 = "x,8";
10749
10750     do_addone_branch_i:
10751       if (imm_expr.X_op != O_constant)
10752         as_bad (_("Unsupported large constant"));
10753       ++imm_expr.X_add_number;
10754
10755     do_branch_i:
10756       macro_build (&imm_expr, s, s3, xreg);
10757       macro_build (&offset_expr, s2, "p");
10758       break;
10759
10760     case M_ABS:
10761       expr1.X_add_number = 0;
10762       macro_build (&expr1, "slti", "x,8", yreg);
10763       if (xreg != yreg)
10764         move_register (xreg, yreg);
10765       expr1.X_add_number = 2;
10766       macro_build (&expr1, "bteqz", "p");
10767       macro_build (NULL, "neg", "x,w", xreg, xreg);
10768     }
10769 }
10770
10771 /* For consistency checking, verify that all bits are specified either
10772    by the match/mask part of the instruction definition, or by the
10773    operand list.  */
10774 static int
10775 validate_mips_insn (const struct mips_opcode *opc)
10776 {
10777   const char *p = opc->args;
10778   char c;
10779   unsigned long used_bits = opc->mask;
10780
10781   if ((used_bits & opc->match) != opc->match)
10782     {
10783       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10784               opc->name, opc->args);
10785       return 0;
10786     }
10787 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
10788   while (*p)
10789     switch (c = *p++)
10790       {
10791       case ',': break;
10792       case '(': break;
10793       case ')': break;
10794       case '+':
10795         switch (c = *p++)
10796           {
10797           case '1': USE_BITS (OP_MASK_UDI1,     OP_SH_UDI1);    break;
10798           case '2': USE_BITS (OP_MASK_UDI2,     OP_SH_UDI2);    break;
10799           case '3': USE_BITS (OP_MASK_UDI3,     OP_SH_UDI3);    break;
10800           case '4': USE_BITS (OP_MASK_UDI4,     OP_SH_UDI4);    break;
10801           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
10802           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
10803           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
10804           case 'E': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
10805           case 'F': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
10806           case 'G': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
10807           case 'H': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
10808           case 'I': break;
10809           case 'J': USE_BITS (OP_MASK_CODE10,   OP_SH_CODE10);  break;
10810           case 't': USE_BITS (OP_MASK_RT,       OP_SH_RT);      break;
10811           case 'x': USE_BITS (OP_MASK_BBITIND,  OP_SH_BBITIND); break;
10812           case 'X': USE_BITS (OP_MASK_BBITIND,  OP_SH_BBITIND); break;
10813           case 'p': USE_BITS (OP_MASK_CINSPOS,  OP_SH_CINSPOS); break;
10814           case 'P': USE_BITS (OP_MASK_CINSPOS,  OP_SH_CINSPOS); break;
10815           case 'Q': USE_BITS (OP_MASK_SEQI,     OP_SH_SEQI);    break;
10816           case 's': USE_BITS (OP_MASK_CINSLM1,  OP_SH_CINSLM1); break;
10817           case 'S': USE_BITS (OP_MASK_CINSLM1,  OP_SH_CINSLM1); break;
10818           case 'z': USE_BITS (OP_MASK_RZ,       OP_SH_RZ);      break;
10819           case 'Z': USE_BITS (OP_MASK_FZ,       OP_SH_FZ);      break;
10820           case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10821           case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10822           case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
10823           case 'i': USE_BITS (OP_MASK_TARGET,   OP_SH_TARGET);  break;
10824           case 'j': USE_BITS (OP_MASK_EVAOFFSET, OP_SH_EVAOFFSET); break;
10825
10826           default:
10827             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10828                     c, opc->name, opc->args);
10829             return 0;
10830           }
10831         break;
10832       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
10833       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
10834       case 'A': break;
10835       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
10836       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
10837       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
10838       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10839       case 'F': break;
10840       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10841       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
10842       case 'I': break;
10843       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
10844       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10845       case 'L': break;
10846       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
10847       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
10848       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
10849       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
10850                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10851       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
10852       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
10853       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10854       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
10855       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10856       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
10857       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
10858       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10859       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
10860       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10861       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
10862       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10863       case 'f': break;
10864       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
10865       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
10866       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
10867       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
10868       case 'l': break;
10869       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
10870       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
10871       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
10872       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10873       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10874       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10875       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
10876       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10877       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10878       case 'x': break;
10879       case 'z': break;
10880       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
10881       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
10882                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10883       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
10884       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
10885       case '1': USE_BITS (OP_MASK_STYPE,        OP_SH_STYPE);   break;
10886       case '2': USE_BITS (OP_MASK_BP,           OP_SH_BP);      break;
10887       case '3': USE_BITS (OP_MASK_SA3,          OP_SH_SA3);     break;
10888       case '4': USE_BITS (OP_MASK_SA4,          OP_SH_SA4);     break;
10889       case '5': USE_BITS (OP_MASK_IMM8,         OP_SH_IMM8);    break;
10890       case '6': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10891       case '7': USE_BITS (OP_MASK_DSPACC,       OP_SH_DSPACC);  break;
10892       case '8': USE_BITS (OP_MASK_WRDSP,        OP_SH_WRDSP);   break;
10893       case '9': USE_BITS (OP_MASK_DSPACC_S,     OP_SH_DSPACC_S);break;
10894       case '0': USE_BITS (OP_MASK_DSPSFT,       OP_SH_DSPSFT);  break;
10895       case '\'': USE_BITS (OP_MASK_RDDSP,       OP_SH_RDDSP);   break;
10896       case ':': USE_BITS (OP_MASK_DSPSFT_7,     OP_SH_DSPSFT_7);break;
10897       case '@': USE_BITS (OP_MASK_IMM10,        OP_SH_IMM10);   break;
10898       case '!': USE_BITS (OP_MASK_MT_U,         OP_SH_MT_U);    break;
10899       case '$': USE_BITS (OP_MASK_MT_H,         OP_SH_MT_H);    break;
10900       case '*': USE_BITS (OP_MASK_MTACC_T,      OP_SH_MTACC_T); break;
10901       case '&': USE_BITS (OP_MASK_MTACC_D,      OP_SH_MTACC_D); break;
10902       case '\\': USE_BITS (OP_MASK_3BITPOS,     OP_SH_3BITPOS); break;
10903       case '~': USE_BITS (OP_MASK_OFFSET12,     OP_SH_OFFSET12); break;
10904       case 'g': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10905       default:
10906         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10907                 c, opc->name, opc->args);
10908         return 0;
10909       }
10910 #undef USE_BITS
10911   if (used_bits != 0xffffffff)
10912     {
10913       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10914               ~used_bits & 0xffffffff, opc->name, opc->args);
10915       return 0;
10916     }
10917   return 1;
10918 }
10919
10920 /* For consistency checking, verify that the length implied matches the
10921    major opcode and that all bits are specified either by the match/mask
10922    part of the instruction definition, or by the operand list.  */
10923
10924 static int
10925 validate_micromips_insn (const struct mips_opcode *opc)
10926 {
10927   unsigned long match = opc->match;
10928   unsigned long mask = opc->mask;
10929   const char *p = opc->args;
10930   unsigned long insn_bits;
10931   unsigned long used_bits;
10932   unsigned long major;
10933   unsigned int length;
10934   char e;
10935   char c;
10936
10937   if ((mask & match) != match)
10938     {
10939       as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10940               opc->name, opc->args);
10941       return 0;
10942     }
10943   length = micromips_insn_length (opc);
10944   if (length != 2 && length != 4)
10945     {
10946       as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10947                 "%s %s"), length, opc->name, opc->args);
10948       return 0;
10949     }
10950   major = match >> (10 + 8 * (length - 2));
10951   if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10952       || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10953     {
10954       as_bad (_("Internal error: bad microMIPS opcode "
10955                 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
10956       return 0;
10957     }
10958
10959   /* Shift piecewise to avoid an overflow where unsigned long is 32-bit.  */
10960   insn_bits = 1 << 4 * length;
10961   insn_bits <<= 4 * length;
10962   insn_bits -= 1;
10963   used_bits = mask;
10964 #define USE_BITS(field) \
10965   (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10966   while (*p)
10967     switch (c = *p++)
10968       {
10969       case ',': break;
10970       case '(': break;
10971       case ')': break;
10972       case '+':
10973         e = c;
10974         switch (c = *p++)
10975           {
10976           case 'A': USE_BITS (EXTLSB);  break;
10977           case 'B': USE_BITS (INSMSB);  break;
10978           case 'C': USE_BITS (EXTMSBD); break;
10979           case 'E': USE_BITS (EXTLSB);  break;
10980           case 'F': USE_BITS (INSMSB);  break;
10981           case 'G': USE_BITS (EXTMSBD); break;
10982           case 'H': USE_BITS (EXTMSBD); break;
10983           case 'i': USE_BITS (TARGET);  break;
10984           case 'j': USE_BITS (EVAOFFSET);       break;
10985           default:
10986             as_bad (_("Internal error: bad mips opcode "
10987                       "(unknown extension operand type `%c%c'): %s %s"),
10988                     e, c, opc->name, opc->args);
10989             return 0;
10990           }
10991         break;
10992       case 'm':
10993         e = c;
10994         switch (c = *p++)
10995           {
10996           case 'A': USE_BITS (IMMA);    break;
10997           case 'B': USE_BITS (IMMB);    break;
10998           case 'C': USE_BITS (IMMC);    break;
10999           case 'D': USE_BITS (IMMD);    break;
11000           case 'E': USE_BITS (IMME);    break;
11001           case 'F': USE_BITS (IMMF);    break;
11002           case 'G': USE_BITS (IMMG);    break;
11003           case 'H': USE_BITS (IMMH);    break;
11004           case 'I': USE_BITS (IMMI);    break;
11005           case 'J': USE_BITS (IMMJ);    break;
11006           case 'L': USE_BITS (IMML);    break;
11007           case 'M': USE_BITS (IMMM);    break;
11008           case 'N': USE_BITS (IMMN);    break;
11009           case 'O': USE_BITS (IMMO);    break;
11010           case 'P': USE_BITS (IMMP);    break;
11011           case 'Q': USE_BITS (IMMQ);    break;
11012           case 'U': USE_BITS (IMMU);    break;
11013           case 'W': USE_BITS (IMMW);    break;
11014           case 'X': USE_BITS (IMMX);    break;
11015           case 'Y': USE_BITS (IMMY);    break;
11016           case 'Z': break;
11017           case 'a': break;
11018           case 'b': USE_BITS (MB);      break;
11019           case 'c': USE_BITS (MC);      break;
11020           case 'd': USE_BITS (MD);      break;
11021           case 'e': USE_BITS (ME);      break;
11022           case 'f': USE_BITS (MF);      break;
11023           case 'g': USE_BITS (MG);      break;
11024           case 'h': USE_BITS (MH);      break;
11025           case 'j': USE_BITS (MJ);      break;
11026           case 'l': USE_BITS (ML);      break;
11027           case 'm': USE_BITS (MM);      break;
11028           case 'n': USE_BITS (MN);      break;
11029           case 'p': USE_BITS (MP);      break;
11030           case 'q': USE_BITS (MQ);      break;
11031           case 'r': break;
11032           case 's': break;
11033           case 't': break;
11034           case 'x': break;
11035           case 'y': break;
11036           case 'z': break;
11037           default:
11038             as_bad (_("Internal error: bad mips opcode "
11039                       "(unknown extension operand type `%c%c'): %s %s"),
11040                     e, c, opc->name, opc->args);
11041             return 0;
11042           }
11043         break;
11044       case '.': USE_BITS (OFFSET10);    break;
11045       case '1': USE_BITS (STYPE);       break;
11046       case '2': USE_BITS (BP);          break;
11047       case '3': USE_BITS (SA3);         break;
11048       case '4': USE_BITS (SA4);         break;
11049       case '5': USE_BITS (IMM8);        break;
11050       case '6': USE_BITS (RS);          break;
11051       case '7': USE_BITS (DSPACC);      break;
11052       case '8': USE_BITS (WRDSP);       break;
11053       case '0': USE_BITS (DSPSFT);      break;
11054       case '<': USE_BITS (SHAMT);       break;
11055       case '>': USE_BITS (SHAMT);       break;
11056       case '@': USE_BITS (IMM10);       break;
11057       case 'B': USE_BITS (CODE10);      break;
11058       case 'C': USE_BITS (COPZ);        break;
11059       case 'D': USE_BITS (FD);          break;
11060       case 'E': USE_BITS (RT);          break;
11061       case 'G': USE_BITS (RS);          break;
11062       case 'H': USE_BITS (SEL);         break;
11063       case 'K': USE_BITS (RS);          break;
11064       case 'M': USE_BITS (CCC);         break;
11065       case 'N': USE_BITS (BCC);         break;
11066       case 'R': USE_BITS (FR);          break;
11067       case 'S': USE_BITS (FS);          break;
11068       case 'T': USE_BITS (FT);          break;
11069       case 'V': USE_BITS (FS);          break;
11070       case '\\': USE_BITS (3BITPOS);    break;
11071       case '^': USE_BITS (RD);          break;
11072       case 'a': USE_BITS (TARGET);      break;
11073       case 'b': USE_BITS (RS);          break;
11074       case 'c': USE_BITS (CODE);        break;
11075       case 'd': USE_BITS (RD);          break;
11076       case 'h': USE_BITS (PREFX);       break;
11077       case 'i': USE_BITS (IMMEDIATE);   break;
11078       case 'j': USE_BITS (DELTA);       break;
11079       case 'k': USE_BITS (CACHE);       break;
11080       case 'n': USE_BITS (RT);          break;
11081       case 'o': USE_BITS (DELTA);       break;
11082       case 'p': USE_BITS (DELTA);       break;
11083       case 'q': USE_BITS (CODE2);       break;
11084       case 'r': USE_BITS (RS);          break;
11085       case 's': USE_BITS (RS);          break;
11086       case 't': USE_BITS (RT);          break;
11087       case 'u': USE_BITS (IMMEDIATE);   break;
11088       case 'v': USE_BITS (RS);          break;
11089       case 'w': USE_BITS (RT);          break;
11090       case 'y': USE_BITS (RS3);         break;
11091       case 'z': break;
11092       case '|': USE_BITS (TRAP);        break;
11093       case '~': USE_BITS (OFFSET12);    break;
11094       default:
11095         as_bad (_("Internal error: bad microMIPS opcode "
11096                   "(unknown operand type `%c'): %s %s"),
11097                 c, opc->name, opc->args);
11098         return 0;
11099       }
11100 #undef USE_BITS
11101   if (used_bits != insn_bits)
11102     {
11103       if (~used_bits & insn_bits)
11104         as_bad (_("Internal error: bad microMIPS opcode "
11105                   "(bits 0x%lx undefined): %s %s"),
11106                 ~used_bits & insn_bits, opc->name, opc->args);
11107       if (used_bits & ~insn_bits)
11108         as_bad (_("Internal error: bad microMIPS opcode "
11109                   "(bits 0x%lx defined): %s %s"),
11110                 used_bits & ~insn_bits, opc->name, opc->args);
11111       return 0;
11112     }
11113   return 1;
11114 }
11115
11116 /* UDI immediates.  */
11117 struct mips_immed {
11118   char          type;
11119   unsigned int  shift;
11120   unsigned long mask;
11121   const char *  desc;
11122 };
11123
11124 static const struct mips_immed mips_immed[] = {
11125   { '1',        OP_SH_UDI1,     OP_MASK_UDI1,           0},
11126   { '2',        OP_SH_UDI2,     OP_MASK_UDI2,           0},
11127   { '3',        OP_SH_UDI3,     OP_MASK_UDI3,           0},
11128   { '4',        OP_SH_UDI4,     OP_MASK_UDI4,           0},
11129   { 0,0,0,0 }
11130 };
11131
11132 /* Check whether an odd floating-point register is allowed.  */
11133 static int
11134 mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
11135 {
11136   const char *s = insn->name;
11137
11138   if (insn->pinfo == INSN_MACRO)
11139     /* Let a macro pass, we'll catch it later when it is expanded.  */
11140     return 1;
11141
11142   if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || (mips_opts.arch == CPU_R5900))
11143     {
11144       /* Allow odd registers for single-precision ops.  */
11145       switch (insn->pinfo & (FP_S | FP_D))
11146         {
11147         case FP_S:
11148         case 0:
11149           return 1;     /* both single precision - ok */
11150         case FP_D:
11151           return 0;     /* both double precision - fail */
11152         default:
11153           break;
11154         }
11155
11156       /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand.  */
11157       s = strchr (insn->name, '.');
11158       if (argnum == 2)
11159         s = s != NULL ? strchr (s + 1, '.') : NULL;
11160       return (s != NULL && (s[1] == 'w' || s[1] == 's'));
11161     } 
11162
11163   /* Single-precision coprocessor loads and moves are OK too.  */
11164   if ((insn->pinfo & FP_S)
11165       && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
11166                          | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
11167     return 1;
11168
11169   return 0;
11170 }
11171
11172 /* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
11173    taking bits from BIT up.  */
11174 static int
11175 expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
11176 {
11177   return (ep->X_op == O_constant
11178           && (ep->X_add_number & ((1 << bit) - 1)) == 0
11179           && ep->X_add_number >= min << bit
11180           && ep->X_add_number < max << bit);
11181 }
11182
11183 /* This routine assembles an instruction into its binary format.  As a
11184    side effect, it sets one of the global variables imm_reloc or
11185    offset_reloc to the type of relocation to do if one of the operands
11186    is an address expression.  */
11187
11188 static void
11189 mips_ip (char *str, struct mips_cl_insn *ip)
11190 {
11191   bfd_boolean wrong_delay_slot_insns = FALSE;
11192   bfd_boolean need_delay_slot_ok = TRUE;
11193   struct mips_opcode *firstinsn = NULL;
11194   const struct mips_opcode *past;
11195   struct hash_control *hash;
11196   char *s;
11197   const char *args;
11198   char c = 0;
11199   struct mips_opcode *insn;
11200   char *argsStart;
11201   unsigned int regno, regno2;
11202   unsigned int lastregno;
11203   unsigned int destregno = 0;
11204   unsigned int lastpos = 0;
11205   unsigned int limlo, limhi;
11206   int sizelo;
11207   char *s_reset;
11208   offsetT min_range, max_range;
11209   long opend;
11210   char *name;
11211   int argnum;
11212   unsigned int rtype;
11213   char *dot;
11214   long end;
11215
11216   insn_error = NULL;
11217
11218   if (mips_opts.micromips)
11219     {
11220       hash = micromips_op_hash;
11221       past = &micromips_opcodes[bfd_micromips_num_opcodes];
11222     }
11223   else
11224     {
11225       hash = op_hash;
11226       past = &mips_opcodes[NUMOPCODES];
11227     }
11228   forced_insn_length = 0;
11229   insn = NULL;
11230
11231   /* We first try to match an instruction up to a space or to the end.  */
11232   for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
11233     continue;
11234
11235   /* Make a copy of the instruction so that we can fiddle with it.  */
11236   name = alloca (end + 1);
11237   memcpy (name, str, end);
11238   name[end] = '\0';
11239
11240   for (;;)
11241     {
11242       insn = (struct mips_opcode *) hash_find (hash, name);
11243
11244       if (insn != NULL || !mips_opts.micromips)
11245         break;
11246       if (forced_insn_length)
11247         break;
11248
11249       /* See if there's an instruction size override suffix,
11250          either `16' or `32', at the end of the mnemonic proper,
11251          that defines the operation, i.e. before the first `.'
11252          character if any.  Strip it and retry.  */
11253       dot = strchr (name, '.');
11254       opend = dot != NULL ? dot - name : end;
11255       if (opend < 3)
11256         break;
11257       if (name[opend - 2] == '1' && name[opend - 1] == '6')
11258         forced_insn_length = 2;
11259       else if (name[opend - 2] == '3' && name[opend - 1] == '2')
11260         forced_insn_length = 4;
11261       else
11262         break;
11263       memcpy (name + opend - 2, name + opend, end - opend + 1);
11264     }
11265   if (insn == NULL)
11266     {
11267       insn_error = _("Unrecognized opcode");
11268       return;
11269     }
11270
11271   /* For microMIPS instructions placed in a fixed-length branch delay slot
11272      we make up to two passes over the relevant fragment of the opcode
11273      table.  First we try instructions that meet the delay slot's length
11274      requirement.  If none matched, then we retry with the remaining ones
11275      and if one matches, then we use it and then issue an appropriate
11276      warning later on.  */
11277   argsStart = s = str + end;
11278   for (;;)
11279     {
11280       bfd_boolean delay_slot_ok;
11281       bfd_boolean size_ok;
11282       bfd_boolean ok;
11283
11284       gas_assert (strcmp (insn->name, name) == 0);
11285
11286       ok = is_opcode_valid (insn);
11287       size_ok = is_size_valid (insn);
11288       delay_slot_ok = is_delay_slot_valid (insn);
11289       if (!delay_slot_ok && !wrong_delay_slot_insns)
11290         {
11291           firstinsn = insn;
11292           wrong_delay_slot_insns = TRUE;
11293         }
11294       if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
11295         {
11296           static char buf[256];
11297
11298           if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
11299             {
11300               ++insn;
11301               continue;
11302             }
11303           if (wrong_delay_slot_insns && need_delay_slot_ok)
11304             {
11305               gas_assert (firstinsn);
11306               need_delay_slot_ok = FALSE;
11307               past = insn + 1;
11308               insn = firstinsn;
11309               continue;
11310             }
11311
11312           if (insn_error)
11313             return;
11314
11315           if (!ok)
11316             sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
11317                      mips_cpu_info_from_arch (mips_opts.arch)->name,
11318                      mips_cpu_info_from_isa (mips_opts.isa)->name);
11319           else if (mips_opts.insn32)
11320             sprintf (buf, _("Opcode not supported in the `insn32' mode"));
11321           else
11322             sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
11323                      8 * forced_insn_length);
11324           insn_error = buf;
11325
11326           return;
11327         }
11328
11329       imm_expr.X_op = O_absent;
11330       imm2_expr.X_op = O_absent;
11331       offset_expr.X_op = O_absent;
11332       imm_reloc[0] = BFD_RELOC_UNUSED;
11333       imm_reloc[1] = BFD_RELOC_UNUSED;
11334       imm_reloc[2] = BFD_RELOC_UNUSED;
11335       offset_reloc[0] = BFD_RELOC_UNUSED;
11336       offset_reloc[1] = BFD_RELOC_UNUSED;
11337       offset_reloc[2] = BFD_RELOC_UNUSED;
11338
11339       create_insn (ip, insn);
11340       insn_error = NULL;
11341       argnum = 1;
11342       lastregno = 0xffffffff;
11343       for (args = insn->args;; ++args)
11344         {
11345           int is_mdmx;
11346
11347           s += strspn (s, " \t");
11348           is_mdmx = 0;
11349           switch (*args)
11350             {
11351             case '\0':          /* end of args */
11352               if (*s == '\0')
11353                 return;
11354               break;
11355
11356             case '2':
11357               /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
11358                  code) or 14 (for microMIPS code).  */
11359               my_getExpression (&imm_expr, s);
11360               check_absolute_expr (ip, &imm_expr);
11361               if ((unsigned long) imm_expr.X_add_number != 1
11362                   && (unsigned long) imm_expr.X_add_number != 3)
11363                 {
11364                   as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
11365                           (unsigned long) imm_expr.X_add_number);
11366                 }
11367               INSERT_OPERAND (mips_opts.micromips,
11368                               BP, *ip, imm_expr.X_add_number);
11369               imm_expr.X_op = O_absent;
11370               s = expr_end;
11371               continue;
11372
11373             case '3':
11374               /* DSP 3-bit unsigned immediate in bit 21 (for standard MIPS
11375                  code) or 13 (for microMIPS code).  */
11376               {
11377                 unsigned long mask = (mips_opts.micromips
11378                                       ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
11379
11380                 my_getExpression (&imm_expr, s);
11381                 check_absolute_expr (ip, &imm_expr);
11382                 if ((unsigned long) imm_expr.X_add_number > mask)
11383                   as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11384                           mask, (unsigned long) imm_expr.X_add_number);
11385                 INSERT_OPERAND (mips_opts.micromips,
11386                                 SA3, *ip, imm_expr.X_add_number);
11387                 imm_expr.X_op = O_absent;
11388                 s = expr_end;
11389               }
11390               continue;
11391
11392             case '4':
11393               /* DSP 4-bit unsigned immediate in bit 21 (for standard MIPS
11394                  code) or 12 (for microMIPS code).  */
11395               {
11396                 unsigned long mask = (mips_opts.micromips
11397                                       ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
11398
11399                 my_getExpression (&imm_expr, s);
11400                 check_absolute_expr (ip, &imm_expr);
11401                 if ((unsigned long) imm_expr.X_add_number > mask)
11402                   as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11403                           mask, (unsigned long) imm_expr.X_add_number);
11404                 INSERT_OPERAND (mips_opts.micromips,
11405                                 SA4, *ip, imm_expr.X_add_number);
11406                 imm_expr.X_op = O_absent;
11407                 s = expr_end;
11408               }
11409               continue;
11410
11411             case '5':
11412               /* DSP 8-bit unsigned immediate in bit 16 (for standard MIPS
11413                  code) or 13 (for microMIPS code).  */
11414               {
11415                 unsigned long mask = (mips_opts.micromips
11416                                       ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
11417
11418                 my_getExpression (&imm_expr, s);
11419                 check_absolute_expr (ip, &imm_expr);
11420                 if ((unsigned long) imm_expr.X_add_number > mask)
11421                   as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11422                           mask, (unsigned long) imm_expr.X_add_number);
11423                 INSERT_OPERAND (mips_opts.micromips,
11424                                 IMM8, *ip, imm_expr.X_add_number);
11425                 imm_expr.X_op = O_absent;
11426                 s = expr_end;
11427               }
11428               continue;
11429
11430             case '6':
11431               /* DSP 5-bit unsigned immediate in bit 21 (for standard MIPS
11432                  code) or 16 (for microMIPS code).  */
11433               {
11434                 unsigned long mask = (mips_opts.micromips
11435                                       ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
11436
11437                 my_getExpression (&imm_expr, s);
11438                 check_absolute_expr (ip, &imm_expr);
11439                 if ((unsigned long) imm_expr.X_add_number > mask)
11440                   as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11441                           mask, (unsigned long) imm_expr.X_add_number);
11442                 INSERT_OPERAND (mips_opts.micromips,
11443                                 RS, *ip, imm_expr.X_add_number);
11444                 imm_expr.X_op = O_absent;
11445                 s = expr_end;
11446               }
11447               continue;
11448
11449             case '7':
11450               /* Four DSP accumulators in bit 11 (for standard MIPS code)
11451                  or 14 (for microMIPS code).  */
11452               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11453                   && s[3] >= '0' && s[3] <= '3')
11454                 {
11455                   regno = s[3] - '0';
11456                   s += 4;
11457                   INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
11458                   continue;
11459                 }
11460               else
11461                 as_bad (_("Invalid dsp acc register"));
11462               break;
11463
11464             case '8':
11465               /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
11466                  code) or 14 (for microMIPS code).  */
11467               {
11468                 unsigned long mask = (mips_opts.micromips
11469                                       ? MICROMIPSOP_MASK_WRDSP
11470                                       : OP_MASK_WRDSP);
11471
11472                 my_getExpression (&imm_expr, s);
11473                 check_absolute_expr (ip, &imm_expr);
11474                 if ((unsigned long) imm_expr.X_add_number > mask)
11475                   as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11476                           mask, (unsigned long) imm_expr.X_add_number);
11477                 INSERT_OPERAND (mips_opts.micromips,
11478                                 WRDSP, *ip, imm_expr.X_add_number);
11479                 imm_expr.X_op = O_absent;
11480                 s = expr_end;
11481               }
11482               continue;
11483
11484             case '9': /* Four DSP accumulators in bits 21,22.  */
11485               gas_assert (!mips_opts.micromips);
11486               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11487                   && s[3] >= '0' && s[3] <= '3')
11488                 {
11489                   regno = s[3] - '0';
11490                   s += 4;
11491                   INSERT_OPERAND (0, DSPACC_S, *ip, regno);
11492                   continue;
11493                 }
11494               else
11495                 as_bad (_("Invalid dsp acc register"));
11496               break;
11497
11498             case '0':
11499               /* DSP 6-bit signed immediate in bit 20 (for standard MIPS
11500                  code) or 16 (for microMIPS code).  */
11501               {
11502                 long mask = (mips_opts.micromips
11503                              ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
11504
11505                 my_getExpression (&imm_expr, s);
11506                 check_absolute_expr (ip, &imm_expr);
11507                 min_range = -((mask + 1) >> 1);
11508                 max_range = ((mask + 1) >> 1) - 1;
11509                 if (imm_expr.X_add_number < min_range
11510                     || imm_expr.X_add_number > max_range)
11511                   as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11512                           (long) min_range, (long) max_range,
11513                           (long) imm_expr.X_add_number);
11514                 INSERT_OPERAND (mips_opts.micromips,
11515                                 DSPSFT, *ip, imm_expr.X_add_number);
11516                 imm_expr.X_op = O_absent;
11517                 s = expr_end;
11518               }
11519               continue;
11520
11521             case '\'': /* DSP 6-bit unsigned immediate in bit 16.  */
11522               gas_assert (!mips_opts.micromips);
11523               my_getExpression (&imm_expr, s);
11524               check_absolute_expr (ip, &imm_expr);
11525               if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11526                 {
11527                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11528                           OP_MASK_RDDSP,
11529                           (unsigned long) imm_expr.X_add_number);
11530                 }
11531               INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
11532               imm_expr.X_op = O_absent;
11533               s = expr_end;
11534               continue;
11535
11536             case ':': /* DSP 7-bit signed immediate in bit 19.  */
11537               gas_assert (!mips_opts.micromips);
11538               my_getExpression (&imm_expr, s);
11539               check_absolute_expr (ip, &imm_expr);
11540               min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11541               max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11542               if (imm_expr.X_add_number < min_range ||
11543                   imm_expr.X_add_number > max_range)
11544                 {
11545                   as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11546                           (long) min_range, (long) max_range,
11547                           (long) imm_expr.X_add_number);
11548                 }
11549               INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
11550               imm_expr.X_op = O_absent;
11551               s = expr_end;
11552               continue;
11553
11554             case '@': /* DSP 10-bit signed immediate in bit 16.  */
11555               {
11556                 long mask = (mips_opts.micromips
11557                              ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11558
11559                 my_getExpression (&imm_expr, s);
11560                 check_absolute_expr (ip, &imm_expr);
11561                 min_range = -((mask + 1) >> 1);
11562                 max_range = ((mask + 1) >> 1) - 1;
11563                 if (imm_expr.X_add_number < min_range
11564                     || imm_expr.X_add_number > max_range)
11565                   as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11566                           (long) min_range, (long) max_range,
11567                           (long) imm_expr.X_add_number);
11568                 INSERT_OPERAND (mips_opts.micromips,
11569                                 IMM10, *ip, imm_expr.X_add_number);
11570                 imm_expr.X_op = O_absent;
11571                 s = expr_end;
11572               }
11573               continue;
11574
11575             case '^': /* DSP 5-bit unsigned immediate in bit 11.  */
11576               gas_assert (mips_opts.micromips);
11577               my_getExpression (&imm_expr, s);
11578               check_absolute_expr (ip, &imm_expr);
11579               if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11580                 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11581                         MICROMIPSOP_MASK_RD,
11582                         (unsigned long) imm_expr.X_add_number);
11583               INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
11584               imm_expr.X_op = O_absent;
11585               s = expr_end;
11586               continue;
11587
11588             case '!': /* MT usermode flag bit.  */
11589               gas_assert (!mips_opts.micromips);
11590               my_getExpression (&imm_expr, s);
11591               check_absolute_expr (ip, &imm_expr);
11592               if (imm_expr.X_add_number & ~OP_MASK_MT_U)
11593                 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11594                         (unsigned long) imm_expr.X_add_number);
11595               INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
11596               imm_expr.X_op = O_absent;
11597               s = expr_end;
11598               continue;
11599
11600             case '$': /* MT load high flag bit.  */
11601               gas_assert (!mips_opts.micromips);
11602               my_getExpression (&imm_expr, s);
11603               check_absolute_expr (ip, &imm_expr);
11604               if (imm_expr.X_add_number & ~OP_MASK_MT_H)
11605                 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11606                         (unsigned long) imm_expr.X_add_number);
11607               INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
11608               imm_expr.X_op = O_absent;
11609               s = expr_end;
11610               continue;
11611
11612             case '*': /* Four DSP accumulators in bits 18,19.  */
11613               gas_assert (!mips_opts.micromips);
11614               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11615                   s[3] >= '0' && s[3] <= '3')
11616                 {
11617                   regno = s[3] - '0';
11618                   s += 4;
11619                   INSERT_OPERAND (0, MTACC_T, *ip, regno);
11620                   continue;
11621                 }
11622               else
11623                 as_bad (_("Invalid dsp/smartmips acc register"));
11624               break;
11625
11626             case '&': /* Four DSP accumulators in bits 13,14.  */
11627               gas_assert (!mips_opts.micromips);
11628               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11629                   s[3] >= '0' && s[3] <= '3')
11630                 {
11631                   regno = s[3] - '0';
11632                   s += 4;
11633                   INSERT_OPERAND (0, MTACC_D, *ip, regno);
11634                   continue;
11635                 }
11636               else
11637                 as_bad (_("Invalid dsp/smartmips acc register"));
11638               break;
11639
11640             case '\\':          /* 3-bit bit position.  */
11641               {
11642                 unsigned long mask = (mips_opts.micromips
11643                                       ? MICROMIPSOP_MASK_3BITPOS
11644                                       : OP_MASK_3BITPOS);
11645
11646                 my_getExpression (&imm_expr, s);
11647                 check_absolute_expr (ip, &imm_expr);
11648                 if ((unsigned long) imm_expr.X_add_number > mask)
11649                   as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11650                            ip->insn_mo->name,
11651                            mask, (unsigned long) imm_expr.X_add_number);
11652                 INSERT_OPERAND (mips_opts.micromips,
11653                                 3BITPOS, *ip, imm_expr.X_add_number);
11654                 imm_expr.X_op = O_absent;
11655                 s = expr_end;
11656               }
11657               continue;
11658
11659             case ',':
11660               ++argnum;
11661               if (*s++ == *args)
11662                 continue;
11663               s--;
11664               switch (*++args)
11665                 {
11666                 case 'r':
11667                 case 'v':
11668                   INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
11669                   continue;
11670
11671                 case 'w':
11672                   INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
11673                   continue;
11674
11675                 case 'W':
11676                   gas_assert (!mips_opts.micromips);
11677                   INSERT_OPERAND (0, FT, *ip, lastregno);
11678                   continue;
11679
11680                 case 'V':
11681                   INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
11682                   continue;
11683                 }
11684               break;
11685
11686             case '(':
11687               /* Handle optional base register.
11688                  Either the base register is omitted or
11689                  we must have a left paren.  */
11690               /* This is dependent on the next operand specifier
11691                  is a base register specification.  */
11692               gas_assert (args[1] == 'b'
11693                           || (mips_opts.micromips
11694                               && args[1] == 'm'
11695                               && (args[2] == 'l' || args[2] == 'n'
11696                                   || args[2] == 's' || args[2] == 'a')));
11697               if (*s == '\0' && args[1] == 'b')
11698                 return;
11699               /* Fall through.  */
11700
11701             case ')':           /* These must match exactly.  */
11702               if (*s++ == *args)
11703                 continue;
11704               break;
11705
11706             case '+':           /* Opcode extension character.  */
11707               switch (*++args)
11708                 {
11709                 case '1':       /* UDI immediates.  */
11710                 case '2':
11711                 case '3':
11712                 case '4':
11713                   gas_assert (!mips_opts.micromips);
11714                   {
11715                     const struct mips_immed *imm = mips_immed;
11716
11717                     while (imm->type && imm->type != *args)
11718                       ++imm;
11719                     if (! imm->type)
11720                       abort ();
11721                     my_getExpression (&imm_expr, s);
11722                     check_absolute_expr (ip, &imm_expr);
11723                     if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11724                       {
11725                         as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11726                                  imm->desc ? imm->desc : ip->insn_mo->name,
11727                                  (unsigned long) imm_expr.X_add_number,
11728                                  (unsigned long) imm_expr.X_add_number);
11729                         imm_expr.X_add_number &= imm->mask;
11730                       }
11731                     ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11732                                         << imm->shift);
11733                     imm_expr.X_op = O_absent;
11734                     s = expr_end;
11735                   }
11736                   continue;
11737
11738                 case 'J':               /* 10-bit hypcall code.  */
11739                   gas_assert (!mips_opts.micromips);
11740                   {
11741                     unsigned long mask = OP_MASK_CODE10;
11742
11743                     my_getExpression (&imm_expr, s);
11744                     check_absolute_expr (ip, &imm_expr);
11745                     if ((unsigned long) imm_expr.X_add_number > mask)
11746                       as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11747                                ip->insn_mo->name,
11748                                mask, (unsigned long) imm_expr.X_add_number);
11749                     INSERT_OPERAND (0, CODE10, *ip, imm_expr.X_add_number);
11750                     imm_expr.X_op = O_absent;
11751                     s = expr_end;
11752                   }
11753                   continue;
11754
11755                 case 'A':               /* ins/ext position, becomes LSB.  */
11756                   limlo = 0;
11757                   limhi = 31;
11758                   goto do_lsb;
11759                 case 'E':
11760                   limlo = 32;
11761                   limhi = 63;
11762                   goto do_lsb;
11763                 do_lsb:
11764                   my_getExpression (&imm_expr, s);
11765                   check_absolute_expr (ip, &imm_expr);
11766                   if ((unsigned long) imm_expr.X_add_number < limlo
11767                       || (unsigned long) imm_expr.X_add_number > limhi)
11768                     {
11769                       as_bad (_("Improper position (%lu)"),
11770                               (unsigned long) imm_expr.X_add_number);
11771                       imm_expr.X_add_number = limlo;
11772                     }
11773                   lastpos = imm_expr.X_add_number;
11774                   INSERT_OPERAND (mips_opts.micromips,
11775                                   EXTLSB, *ip, imm_expr.X_add_number);
11776                   imm_expr.X_op = O_absent;
11777                   s = expr_end;
11778                   continue;
11779
11780                 case 'B':               /* ins size, becomes MSB.  */
11781                   limlo = 1;
11782                   limhi = 32;
11783                   goto do_msb;
11784                 case 'F':
11785                   limlo = 33;
11786                   limhi = 64;
11787                   goto do_msb;
11788                 do_msb:
11789                   my_getExpression (&imm_expr, s);
11790                   check_absolute_expr (ip, &imm_expr);
11791                   /* Check for negative input so that small negative numbers
11792                      will not succeed incorrectly.  The checks against
11793                      (pos+size) transitively check "size" itself,
11794                      assuming that "pos" is reasonable.  */
11795                   if ((long) imm_expr.X_add_number < 0
11796                       || ((unsigned long) imm_expr.X_add_number
11797                           + lastpos) < limlo
11798                       || ((unsigned long) imm_expr.X_add_number
11799                           + lastpos) > limhi)
11800                     {
11801                       as_bad (_("Improper insert size (%lu, position %lu)"),
11802                               (unsigned long) imm_expr.X_add_number,
11803                               (unsigned long) lastpos);
11804                       imm_expr.X_add_number = limlo - lastpos;
11805                     }
11806                   INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11807                                   lastpos + imm_expr.X_add_number - 1);
11808                   imm_expr.X_op = O_absent;
11809                   s = expr_end;
11810                   continue;
11811
11812                 case 'C':               /* ext size, becomes MSBD.  */
11813                   limlo = 1;
11814                   limhi = 32;
11815                   sizelo = 1;
11816                   goto do_msbd;
11817                 case 'G':
11818                   limlo = 33;
11819                   limhi = 64;
11820                   sizelo = 33;
11821                   goto do_msbd;
11822                 case 'H':
11823                   limlo = 33;
11824                   limhi = 64;
11825                   sizelo = 1;
11826                   goto do_msbd;
11827                 do_msbd:
11828                   my_getExpression (&imm_expr, s);
11829                   check_absolute_expr (ip, &imm_expr);
11830                   /* The checks against (pos+size) don't transitively check
11831                      "size" itself, assuming that "pos" is reasonable.
11832                      We also need to check the lower bound of "size".  */
11833                   if ((long) imm_expr.X_add_number < sizelo
11834                       || ((unsigned long) imm_expr.X_add_number
11835                           + lastpos) < limlo
11836                       || ((unsigned long) imm_expr.X_add_number
11837                           + lastpos) > limhi)
11838                     {
11839                       as_bad (_("Improper extract size (%lu, position %lu)"),
11840                               (unsigned long) imm_expr.X_add_number,
11841                               (unsigned long) lastpos);
11842                       imm_expr.X_add_number = limlo - lastpos;
11843                     }
11844                   INSERT_OPERAND (mips_opts.micromips,
11845                                   EXTMSBD, *ip, imm_expr.X_add_number - 1);
11846                   imm_expr.X_op = O_absent;
11847                   s = expr_end;
11848                   continue;
11849
11850                 case 'I':
11851                   /* "+I" is like "I", except that imm2_expr is used.  */
11852                   my_getExpression (&imm2_expr, s);
11853                   if (imm2_expr.X_op != O_big
11854                       && imm2_expr.X_op != O_constant)
11855                   insn_error = _("absolute expression required");
11856                   if (HAVE_32BIT_GPRS)
11857                     normalize_constant_expr (&imm2_expr);
11858                   s = expr_end;
11859                   continue;
11860
11861                 case 't': /* Coprocessor register number.  */
11862                   gas_assert (!mips_opts.micromips);
11863                   if (s[0] == '$' && ISDIGIT (s[1]))
11864                     {
11865                       ++s;
11866                       regno = 0;
11867                       do
11868                         {
11869                           regno *= 10;
11870                           regno += *s - '0';
11871                           ++s;
11872                         }
11873                       while (ISDIGIT (*s));
11874                       if (regno > 31)
11875                         as_bad (_("Invalid register number (%d)"), regno);
11876                       else
11877                         {
11878                           INSERT_OPERAND (0, RT, *ip, regno);
11879                           continue;
11880                         }
11881                     }
11882                   else
11883                     as_bad (_("Invalid coprocessor 0 register number"));
11884                   break;
11885
11886                 case 'x':
11887                   /* bbit[01] and bbit[01]32 bit index.  Give error if index
11888                      is not in the valid range.  */
11889                   gas_assert (!mips_opts.micromips);
11890                   my_getExpression (&imm_expr, s);
11891                   check_absolute_expr (ip, &imm_expr);
11892                   if ((unsigned) imm_expr.X_add_number > 31)
11893                     {
11894                       as_bad (_("Improper bit index (%lu)"),
11895                               (unsigned long) imm_expr.X_add_number);
11896                       imm_expr.X_add_number = 0;
11897                     }
11898                   INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
11899                   imm_expr.X_op = O_absent;
11900                   s = expr_end;
11901                   continue;
11902
11903                 case 'X':
11904                   /* bbit[01] bit index when bbit is used but we generate
11905                      bbit[01]32 because the index is over 32.  Move to the
11906                      next candidate if index is not in the valid range.  */
11907                   gas_assert (!mips_opts.micromips);
11908                   my_getExpression (&imm_expr, s);
11909                   check_absolute_expr (ip, &imm_expr);
11910                   if ((unsigned) imm_expr.X_add_number < 32
11911                       || (unsigned) imm_expr.X_add_number > 63)
11912                     break;
11913                   INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
11914                   imm_expr.X_op = O_absent;
11915                   s = expr_end;
11916                   continue;
11917
11918                 case 'p':
11919                   /* cins, cins32, exts and exts32 position field.  Give error
11920                      if it's not in the valid range.  */
11921                   gas_assert (!mips_opts.micromips);
11922                   my_getExpression (&imm_expr, s);
11923                   check_absolute_expr (ip, &imm_expr);
11924                   if ((unsigned) imm_expr.X_add_number > 31)
11925                     {
11926                       as_bad (_("Improper position (%lu)"),
11927                               (unsigned long) imm_expr.X_add_number);
11928                       imm_expr.X_add_number = 0;
11929                     }
11930                   lastpos = imm_expr.X_add_number;
11931                   INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
11932                   imm_expr.X_op = O_absent;
11933                   s = expr_end;
11934                   continue;
11935
11936                 case 'P':
11937                   /* cins, cins32, exts and exts32 position field.  Move to
11938                      the next candidate if it's not in the valid range.  */
11939                   gas_assert (!mips_opts.micromips);
11940                   my_getExpression (&imm_expr, s);
11941                   check_absolute_expr (ip, &imm_expr);
11942                   if ((unsigned) imm_expr.X_add_number < 32
11943                       || (unsigned) imm_expr.X_add_number > 63)
11944                     break;
11945                   lastpos = imm_expr.X_add_number;
11946                   INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
11947                   imm_expr.X_op = O_absent;
11948                   s = expr_end;
11949                   continue;
11950
11951                 case 's':
11952                   /* cins32 and exts32 length-minus-one field.  */
11953                   gas_assert (!mips_opts.micromips);
11954                   my_getExpression (&imm_expr, s);
11955                   check_absolute_expr (ip, &imm_expr);
11956                   if ((unsigned long) imm_expr.X_add_number > 31
11957                       || (unsigned long) imm_expr.X_add_number + lastpos > 31)
11958                     {
11959                       as_bad (_("Improper size (%lu)"),
11960                               (unsigned long) imm_expr.X_add_number);
11961                       imm_expr.X_add_number = 0;
11962                     }
11963                   INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11964                   imm_expr.X_op = O_absent;
11965                   s = expr_end;
11966                   continue;
11967
11968                 case 'S':
11969                   /* cins/exts length-minus-one field.  */
11970                   gas_assert (!mips_opts.micromips);
11971                   my_getExpression (&imm_expr, s);
11972                   check_absolute_expr (ip, &imm_expr);
11973                   if ((unsigned long) imm_expr.X_add_number > 31
11974                       || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11975                     {
11976                       as_bad (_("Improper size (%lu)"),
11977                               (unsigned long) imm_expr.X_add_number);
11978                       imm_expr.X_add_number = 0;
11979                     }
11980                   INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11981                   imm_expr.X_op = O_absent;
11982                   s = expr_end;
11983                   continue;
11984
11985                 case 'Q':
11986                   /* seqi/snei immediate field.  */
11987                   gas_assert (!mips_opts.micromips);
11988                   my_getExpression (&imm_expr, s);
11989                   check_absolute_expr (ip, &imm_expr);
11990                   if ((long) imm_expr.X_add_number < -512
11991                       || (long) imm_expr.X_add_number >= 512)
11992                     {
11993                       as_bad (_("Improper immediate (%ld)"),
11994                                (long) imm_expr.X_add_number);
11995                       imm_expr.X_add_number = 0;
11996                     }
11997                   INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
11998                   imm_expr.X_op = O_absent;
11999                   s = expr_end;
12000                   continue;
12001
12002                 case 'a': /* 8-bit signed offset in bit 6 */
12003                   gas_assert (!mips_opts.micromips);
12004                   my_getExpression (&imm_expr, s);
12005                   check_absolute_expr (ip, &imm_expr);
12006                   min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
12007                   max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
12008                   if (imm_expr.X_add_number < min_range
12009                       || imm_expr.X_add_number > max_range)
12010                     {
12011                       as_bad (_("Offset not in range %ld..%ld (%ld)"),
12012                               (long) min_range, (long) max_range,
12013                               (long) imm_expr.X_add_number);
12014                     }
12015                   INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
12016                   imm_expr.X_op = O_absent;
12017                   s = expr_end;
12018                   continue;
12019
12020                 case 'b': /* 8-bit signed offset in bit 3 */
12021                   gas_assert (!mips_opts.micromips);
12022                   my_getExpression (&imm_expr, s);
12023                   check_absolute_expr (ip, &imm_expr);
12024                   min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
12025                   max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
12026                   if (imm_expr.X_add_number < min_range
12027                       || imm_expr.X_add_number > max_range)
12028                     {
12029                       as_bad (_("Offset not in range %ld..%ld (%ld)"),
12030                               (long) min_range, (long) max_range,
12031                               (long) imm_expr.X_add_number);
12032                     }
12033                   INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
12034                   imm_expr.X_op = O_absent;
12035                   s = expr_end;
12036                   continue;
12037
12038                 case 'c': /* 9-bit signed offset in bit 6 */
12039                   gas_assert (!mips_opts.micromips);
12040                   my_getExpression (&imm_expr, s);
12041                   check_absolute_expr (ip, &imm_expr);
12042                   min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
12043                   max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
12044                   /* We check the offset range before adjusted.  */
12045                   min_range <<= 4;
12046                   max_range <<= 4;
12047                   if (imm_expr.X_add_number < min_range
12048                       || imm_expr.X_add_number > max_range)
12049                     {
12050                       as_bad (_("Offset not in range %ld..%ld (%ld)"),
12051                               (long) min_range, (long) max_range,
12052                               (long) imm_expr.X_add_number);
12053                     }
12054                   if (imm_expr.X_add_number & 0xf)
12055                     {
12056                       as_bad (_("Offset not 16 bytes alignment (%ld)"),
12057                               (long) imm_expr.X_add_number);
12058                     }
12059                   /* Right shift 4 bits to adjust the offset operand.  */
12060                   INSERT_OPERAND (0, OFFSET_C, *ip,
12061                                   imm_expr.X_add_number >> 4);
12062                   imm_expr.X_op = O_absent;
12063                   s = expr_end;
12064                   continue;
12065
12066                 case 'z':
12067                   gas_assert (!mips_opts.micromips);
12068                   if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
12069                     break;
12070                   if (regno == AT && mips_opts.at)
12071                     {
12072                       if (mips_opts.at == ATREG)
12073                         as_warn (_("used $at without \".set noat\""));
12074                       else
12075                         as_warn (_("used $%u with \".set at=$%u\""),
12076                                  regno, mips_opts.at);
12077                     }
12078                   INSERT_OPERAND (0, RZ, *ip, regno);
12079                   continue;
12080
12081                 case 'Z':
12082                   gas_assert (!mips_opts.micromips);
12083                   if (!reg_lookup (&s, RTYPE_FPU, &regno))
12084                     break;
12085                   INSERT_OPERAND (0, FZ, *ip, regno);
12086                   continue;
12087
12088                 case 'i':
12089                   goto jump;
12090
12091                 case 'j':
12092                   {
12093                     int shift = 8;
12094                     size_t i;
12095                     /* Check whether there is only a single bracketed expression
12096                        left.  If so, it must be the base register and the
12097                        constant must be zero.  */
12098                     if (*s == '(' && strchr (s + 1, '(') == 0)
12099                       continue;
12100
12101                     /* If this value won't fit into the offset, then go find
12102                        a macro that will generate a 16- or 32-bit offset code
12103                        pattern.  */
12104                     i = my_getSmallExpression (&imm_expr, imm_reloc, s);
12105                     if ((i == 0 && (imm_expr.X_op != O_constant
12106                                     || imm_expr.X_add_number >= 1 << shift
12107                                     || imm_expr.X_add_number < -1 << shift))
12108                         || i > 0)
12109                       {
12110                         imm_expr.X_op = O_absent;
12111                         break;
12112                       }
12113                     INSERT_OPERAND (mips_opts.micromips, EVAOFFSET, *ip,
12114                                     imm_expr.X_add_number);
12115                     imm_expr.X_op = O_absent;
12116                     s = expr_end;
12117                   }
12118                   continue;
12119
12120                 default:
12121                   as_bad (_("Internal error: bad %s opcode "
12122                             "(unknown extension operand type `+%c'): %s %s"),
12123                           mips_opts.micromips ? "microMIPS" : "MIPS",
12124                           *args, insn->name, insn->args);
12125                   /* Further processing is fruitless.  */
12126                   return;
12127                 }
12128               break;
12129
12130             case '.':           /* 10-bit offset.  */
12131               gas_assert (mips_opts.micromips);
12132             case '~':           /* 12-bit offset.  */
12133               {
12134                 int shift = *args == '.' ? 9 : 11;
12135                 size_t i;
12136
12137                 /* Check whether there is only a single bracketed expression
12138                    left.  If so, it must be the base register and the
12139                    constant must be zero.  */
12140                 if (*s == '(' && strchr (s + 1, '(') == 0)
12141                   continue;
12142
12143                 /* If this value won't fit into the offset, then go find
12144                    a macro that will generate a 16- or 32-bit offset code
12145                    pattern.  */
12146                 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
12147                 if ((i == 0 && (imm_expr.X_op != O_constant
12148                                 || imm_expr.X_add_number >= 1 << shift
12149                                 || imm_expr.X_add_number < -1 << shift))
12150                     || i > 0)
12151                   {
12152                     imm_expr.X_op = O_absent;
12153                     break;
12154                   }
12155                 if (shift == 9)
12156                   INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
12157                 else
12158                   INSERT_OPERAND (mips_opts.micromips,
12159                                   OFFSET12, *ip, imm_expr.X_add_number);
12160                 imm_expr.X_op = O_absent;
12161                 s = expr_end;
12162               }
12163               continue;
12164
12165             case '<':           /* must be at least one digit */
12166               /*
12167                * According to the manual, if the shift amount is greater
12168                * than 31 or less than 0, then the shift amount should be
12169                * mod 32.  In reality the mips assembler issues an error.
12170                * We issue a warning and mask out all but the low 5 bits.
12171                */
12172               my_getExpression (&imm_expr, s);
12173               check_absolute_expr (ip, &imm_expr);
12174               if ((unsigned long) imm_expr.X_add_number > 31)
12175                 as_warn (_("Improper shift amount (%lu)"),
12176                          (unsigned long) imm_expr.X_add_number);
12177               INSERT_OPERAND (mips_opts.micromips,
12178                               SHAMT, *ip, imm_expr.X_add_number);
12179               imm_expr.X_op = O_absent;
12180               s = expr_end;
12181               continue;
12182
12183             case '>':           /* shift amount minus 32 */
12184               my_getExpression (&imm_expr, s);
12185               check_absolute_expr (ip, &imm_expr);
12186               if ((unsigned long) imm_expr.X_add_number < 32
12187                   || (unsigned long) imm_expr.X_add_number > 63)
12188                 break;
12189               INSERT_OPERAND (mips_opts.micromips,
12190                               SHAMT, *ip, imm_expr.X_add_number - 32);
12191               imm_expr.X_op = O_absent;
12192               s = expr_end;
12193               continue;
12194
12195             case 'k':           /* CACHE code.  */
12196             case 'h':           /* PREFX code.  */
12197             case '1':           /* SYNC type.  */
12198               my_getExpression (&imm_expr, s);
12199               check_absolute_expr (ip, &imm_expr);
12200               if ((unsigned long) imm_expr.X_add_number > 31)
12201                 as_warn (_("Invalid value for `%s' (%lu)"),
12202                          ip->insn_mo->name,
12203                          (unsigned long) imm_expr.X_add_number);
12204               switch (*args)
12205                 {
12206                 case 'k':
12207                   if (mips_fix_cn63xxp1
12208                       && !mips_opts.micromips
12209                       && strcmp ("pref", insn->name) == 0)
12210                     switch (imm_expr.X_add_number)
12211                       {
12212                       case 5:
12213                       case 25:
12214                       case 26:
12215                       case 27:
12216                       case 28:
12217                       case 29:
12218                       case 30:
12219                       case 31:  /* These are ok.  */
12220                         break;
12221
12222                       default:  /* The rest must be changed to 28.  */
12223                         imm_expr.X_add_number = 28;
12224                         break;
12225                       }
12226                   INSERT_OPERAND (mips_opts.micromips,
12227                                   CACHE, *ip, imm_expr.X_add_number);
12228                   break;
12229                 case 'h':
12230                   INSERT_OPERAND (mips_opts.micromips,
12231                                   PREFX, *ip, imm_expr.X_add_number);
12232                   break;
12233                 case '1':
12234                   INSERT_OPERAND (mips_opts.micromips,
12235                                   STYPE, *ip, imm_expr.X_add_number);
12236                   break;
12237                 }
12238               imm_expr.X_op = O_absent;
12239               s = expr_end;
12240               continue;
12241
12242             case 'c':           /* BREAK code.  */
12243               {
12244                 unsigned long mask = (mips_opts.micromips
12245                                       ? MICROMIPSOP_MASK_CODE
12246                                       : OP_MASK_CODE);
12247
12248                 my_getExpression (&imm_expr, s);
12249                 check_absolute_expr (ip, &imm_expr);
12250                 if ((unsigned long) imm_expr.X_add_number > mask)
12251                   as_warn (_("Code for %s not in range 0..%lu (%lu)"),
12252                            ip->insn_mo->name,
12253                            mask, (unsigned long) imm_expr.X_add_number);
12254                 INSERT_OPERAND (mips_opts.micromips,
12255                                 CODE, *ip, imm_expr.X_add_number);
12256                 imm_expr.X_op = O_absent;
12257                 s = expr_end;
12258               }
12259               continue;
12260
12261             case 'q':           /* Lower BREAK code.  */
12262               {
12263                 unsigned long mask = (mips_opts.micromips
12264                                       ? MICROMIPSOP_MASK_CODE2
12265                                       : OP_MASK_CODE2);
12266
12267                 my_getExpression (&imm_expr, s);
12268                 check_absolute_expr (ip, &imm_expr);
12269                 if ((unsigned long) imm_expr.X_add_number > mask)
12270                   as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
12271                            ip->insn_mo->name,
12272                            mask, (unsigned long) imm_expr.X_add_number);
12273                 INSERT_OPERAND (mips_opts.micromips,
12274                                 CODE2, *ip, imm_expr.X_add_number);
12275                 imm_expr.X_op = O_absent;
12276                 s = expr_end;
12277               }
12278               continue;
12279
12280             case 'B':           /* 20- or 10-bit syscall/break/wait code.  */
12281               {
12282                 unsigned long mask = (mips_opts.micromips
12283                                       ? MICROMIPSOP_MASK_CODE10
12284                                       : OP_MASK_CODE20);
12285
12286                 my_getExpression (&imm_expr, s);
12287                 check_absolute_expr (ip, &imm_expr);
12288                 if ((unsigned long) imm_expr.X_add_number > mask)
12289                   as_warn (_("Code for %s not in range 0..%lu (%lu)"),
12290                            ip->insn_mo->name,
12291                            mask, (unsigned long) imm_expr.X_add_number);
12292                 if (mips_opts.micromips)
12293                   INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
12294                 else
12295                   INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
12296                 imm_expr.X_op = O_absent;
12297                 s = expr_end;
12298               }
12299               continue;
12300
12301             case 'C':           /* 25- or 23-bit coprocessor code.  */
12302               {
12303                 unsigned long mask = (mips_opts.micromips
12304                                       ? MICROMIPSOP_MASK_COPZ
12305                                       : OP_MASK_COPZ);
12306
12307                 my_getExpression (&imm_expr, s);
12308                 check_absolute_expr (ip, &imm_expr);
12309                 if ((unsigned long) imm_expr.X_add_number > mask)
12310                   as_warn (_("Coproccesor code > %u bits (%lu)"),
12311                            mips_opts.micromips ? 23U : 25U,
12312                            (unsigned long) imm_expr.X_add_number);
12313                 INSERT_OPERAND (mips_opts.micromips,
12314                                 COPZ, *ip, imm_expr.X_add_number);
12315                 imm_expr.X_op = O_absent;
12316                 s = expr_end;
12317               }
12318               continue;
12319
12320             case 'J':           /* 19-bit WAIT code.  */
12321               gas_assert (!mips_opts.micromips);
12322               my_getExpression (&imm_expr, s);
12323               check_absolute_expr (ip, &imm_expr);
12324               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
12325                 {
12326                   as_warn (_("Illegal 19-bit code (%lu)"),
12327                            (unsigned long) imm_expr.X_add_number);
12328                   imm_expr.X_add_number &= OP_MASK_CODE19;
12329                 }
12330               INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
12331               imm_expr.X_op = O_absent;
12332               s = expr_end;
12333               continue;
12334
12335             case 'P':           /* Performance register.  */
12336               gas_assert (!mips_opts.micromips);
12337               my_getExpression (&imm_expr, s);
12338               check_absolute_expr (ip, &imm_expr);
12339               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
12340                 as_warn (_("Invalid performance register (%lu)"),
12341                          (unsigned long) imm_expr.X_add_number);
12342               if (imm_expr.X_add_number != 0 && mips_opts.arch == CPU_R5900
12343                 && (!strcmp(insn->name,"mfps") || !strcmp(insn->name,"mtps")))
12344                 as_warn (_("Invalid performance register (%lu)"),
12345                   (unsigned long) imm_expr.X_add_number);
12346               INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
12347               imm_expr.X_op = O_absent;
12348               s = expr_end;
12349               continue;
12350
12351             case 'G':           /* Coprocessor destination register.  */
12352               {
12353                 unsigned long opcode = ip->insn_opcode;
12354                 unsigned long mask;
12355                 unsigned int types;
12356                 int cop0;
12357
12358                 if (mips_opts.micromips)
12359                   {
12360                     mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
12361                              | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
12362                              | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
12363                     opcode &= mask;
12364                     switch (opcode)
12365                       {
12366                       case 0x000000fc:                          /* mfc0  */
12367                       case 0x000002fc:                          /* mtc0  */
12368                       case 0x580000fc:                          /* dmfc0 */
12369                       case 0x580002fc:                          /* dmtc0 */
12370                         cop0 = 1;
12371                         break;
12372                       default:
12373                         cop0 = 0;
12374                         break;
12375                       }
12376                   }
12377                 else
12378                   {
12379                     opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
12380                     cop0 = opcode == OP_OP_COP0;
12381                   }
12382                 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
12383                 ok = reg_lookup (&s, types, &regno);
12384                 if (mips_opts.micromips)
12385                   INSERT_OPERAND (1, RS, *ip, regno);
12386                 else
12387                   INSERT_OPERAND (0, RD, *ip, regno);
12388                 if (ok)
12389                   {
12390                     lastregno = regno;
12391                     continue;
12392                   }
12393               }
12394               break;
12395
12396             case 'y':           /* ALNV.PS source register.  */
12397               gas_assert (mips_opts.micromips);
12398               goto do_reg;
12399             case 'x':           /* Ignore register name.  */
12400             case 'U':           /* Destination register (CLO/CLZ).  */
12401             case 'g':           /* Coprocessor destination register.  */
12402               gas_assert (!mips_opts.micromips);
12403             case 'b':           /* Base register.  */
12404             case 'd':           /* Destination register.  */
12405             case 's':           /* Source register.  */
12406             case 't':           /* Target register.  */
12407             case 'r':           /* Both target and source.  */
12408             case 'v':           /* Both dest and source.  */
12409             case 'w':           /* Both dest and target.  */
12410             case 'E':           /* Coprocessor target register.  */
12411             case 'K':           /* RDHWR destination register.  */
12412             case 'z':           /* Must be zero register.  */
12413             do_reg:
12414               s_reset = s;
12415               if (*args == 'E' || *args == 'K')
12416                 ok = reg_lookup (&s, RTYPE_NUM, &regno);
12417               else
12418                 {
12419                   ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12420                   if (regno == AT && mips_opts.at)
12421                     {
12422                       if (mips_opts.at == ATREG)
12423                         as_warn (_("Used $at without \".set noat\""));
12424                       else
12425                         as_warn (_("Used $%u with \".set at=$%u\""),
12426                                  regno, mips_opts.at);
12427                     }
12428                 }
12429               if (ok)
12430                 {
12431                   c = *args;
12432                   if (*s == ' ')
12433                     ++s;
12434                   if (args[1] != *s)
12435                     {
12436                       if (c == 'r' || c == 'v' || c == 'w')
12437                         {
12438                           regno = lastregno;
12439                           s = s_reset;
12440                           ++args;
12441                         }
12442                     }
12443                   /* 'z' only matches $0.  */
12444                   if (c == 'z' && regno != 0)
12445                     break;
12446
12447                   if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
12448                     {
12449                       if (regno == lastregno)
12450                         {
12451                           insn_error
12452                             = _("Source and destination must be different");
12453                           continue;
12454                         }
12455                       if (regno == 31 && lastregno == 0xffffffff)
12456                         {
12457                           insn_error
12458                             = _("A destination register must be supplied");
12459                           continue;
12460                         }
12461                     }
12462                   /* Now that we have assembled one operand, we use the args
12463                      string to figure out where it goes in the instruction.  */
12464                   switch (c)
12465                     {
12466                     case 'r':
12467                     case 's':
12468                     case 'v':
12469                     case 'b':
12470                       INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
12471                       break;
12472
12473                     case 'K':
12474                       if (mips_opts.micromips)
12475                         INSERT_OPERAND (1, RS, *ip, regno);
12476                       else
12477                         INSERT_OPERAND (0, RD, *ip, regno);
12478                       break;
12479
12480                     case 'd':
12481                     case 'g':
12482                       INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
12483                       break;
12484
12485                     case 'U':
12486                       gas_assert (!mips_opts.micromips);
12487                       INSERT_OPERAND (0, RD, *ip, regno);
12488                       INSERT_OPERAND (0, RT, *ip, regno);
12489                       break;
12490
12491                     case 'w':
12492                     case 't':
12493                     case 'E':
12494                       INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
12495                       break;
12496
12497                     case 'y':
12498                       gas_assert (mips_opts.micromips);
12499                       INSERT_OPERAND (1, RS3, *ip, regno);
12500                       break;
12501
12502                     case 'x':
12503                       /* This case exists because on the r3000 trunc
12504                          expands into a macro which requires a gp
12505                          register.  On the r6000 or r4000 it is
12506                          assembled into a single instruction which
12507                          ignores the register.  Thus the insn version
12508                          is MIPS_ISA2 and uses 'x', and the macro
12509                          version is MIPS_ISA1 and uses 't'.  */
12510                       break;
12511
12512                     case 'z':
12513                       /* This case is for the div instruction, which
12514                          acts differently if the destination argument
12515                          is $0.  This only matches $0, and is checked
12516                          outside the switch.  */
12517                       break;
12518                     }
12519                   lastregno = regno;
12520                   continue;
12521                 }
12522               switch (*args++)
12523                 {
12524                 case 'r':
12525                 case 'v':
12526                   INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
12527                   continue;
12528
12529                 case 'w':
12530                   INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
12531                   continue;
12532                 }
12533               break;
12534
12535             case 'O':           /* MDMX alignment immediate constant.  */
12536               gas_assert (!mips_opts.micromips);
12537               my_getExpression (&imm_expr, s);
12538               check_absolute_expr (ip, &imm_expr);
12539               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
12540                 as_warn (_("Improper align amount (%ld), using low bits"),
12541                          (long) imm_expr.X_add_number);
12542               INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
12543               imm_expr.X_op = O_absent;
12544               s = expr_end;
12545               continue;
12546
12547             case 'Q':           /* MDMX vector, element sel, or const.  */
12548               if (s[0] != '$')
12549                 {
12550                   /* MDMX Immediate.  */
12551                   gas_assert (!mips_opts.micromips);
12552                   my_getExpression (&imm_expr, s);
12553                   check_absolute_expr (ip, &imm_expr);
12554                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
12555                     as_warn (_("Invalid MDMX Immediate (%ld)"),
12556                              (long) imm_expr.X_add_number);
12557                   INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
12558                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12559                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12560                   else
12561                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
12562                   imm_expr.X_op = O_absent;
12563                   s = expr_end;
12564                   continue;
12565                 }
12566               /* Not MDMX Immediate.  Fall through.  */
12567             case 'X':           /* MDMX destination register.  */
12568             case 'Y':           /* MDMX source register.  */
12569             case 'Z':           /* MDMX target register.  */
12570               is_mdmx = !(insn->membership & INSN_5400);
12571             case 'W':
12572               gas_assert (!mips_opts.micromips);
12573             case 'D':           /* Floating point destination register.  */
12574             case 'S':           /* Floating point source register.  */
12575             case 'T':           /* Floating point target register.  */
12576             case 'R':           /* Floating point source register.  */
12577             case 'V':
12578               rtype = RTYPE_FPU;
12579               if (is_mdmx
12580                   || ((mips_opts.ase & ASE_MDMX)
12581                       && (ip->insn_mo->pinfo & FP_D)
12582                       && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12583                                                 | INSN_COPROC_MEMORY_DELAY
12584                                                 | INSN_LOAD_COPROC_DELAY
12585                                                 | INSN_LOAD_MEMORY_DELAY
12586                                                 | INSN_STORE_MEMORY))))
12587                 rtype |= RTYPE_VEC;
12588               s_reset = s;
12589               if (reg_lookup (&s, rtype, &regno))
12590                 {
12591                   if ((regno & 1) != 0
12592                       && HAVE_32BIT_FPRS
12593                       && !mips_oddfpreg_ok (ip->insn_mo, argnum))
12594                     as_warn (_("Float register should be even, was %d"),
12595                              regno);
12596
12597                   c = *args;
12598                   if (*s == ' ')
12599                     ++s;
12600                   if (args[1] != *s)
12601                     {
12602                       if (c == 'V' || c == 'W')
12603                         {
12604                           regno = lastregno;
12605                           s = s_reset;
12606                           ++args;
12607                         }
12608                     }
12609                   switch (c)
12610                     {
12611                     case 'D':
12612                     case 'X':
12613                       INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
12614                       break;
12615
12616                     case 'V':
12617                     case 'S':
12618                     case 'Y':
12619                       INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
12620                       break;
12621
12622                     case 'Q':
12623                       /* This is like 'Z', but also needs to fix the MDMX
12624                          vector/scalar select bits.  Note that the
12625                          scalar immediate case is handled above.  */
12626                       if ((ip->insn_mo->membership & INSN_5400)
12627                           && strcmp (insn->name, "rzu.ob") == 0)
12628                         as_bad (_("Operand %d of `%s' must be an immediate"),
12629                                 argnum, ip->insn_mo->name);
12630
12631                       if (*s == '[')
12632                         {
12633                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12634                           int max_el = (is_qh ? 3 : 7);
12635                           s++;
12636                           my_getExpression(&imm_expr, s);
12637                           check_absolute_expr (ip, &imm_expr);
12638                           s = expr_end;
12639                           if (imm_expr.X_add_number > max_el)
12640                             as_bad (_("Bad element selector %ld"),
12641                                     (long) imm_expr.X_add_number);
12642                           imm_expr.X_add_number &= max_el;
12643                           ip->insn_opcode |= (imm_expr.X_add_number
12644                                               << (OP_SH_VSEL +
12645                                                   (is_qh ? 2 : 1)));
12646                           imm_expr.X_op = O_absent;
12647                           if (*s != ']')
12648                             as_warn (_("Expecting ']' found '%s'"), s);
12649                           else
12650                             s++;
12651                         }
12652                       else
12653                         {
12654                           if ((ip->insn_mo->membership & INSN_5400)
12655                               && (strcmp (insn->name, "sll.ob") == 0
12656                                   || strcmp (insn->name, "srl.ob") == 0))
12657                             as_bad (_("Operand %d of `%s' must be scalar"),
12658                                     argnum, ip->insn_mo->name);
12659
12660                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12661                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12662                                                 << OP_SH_VSEL);
12663                           else
12664                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12665                                                 OP_SH_VSEL);
12666                         }
12667                       /* Fall through.  */
12668                     case 'W':
12669                     case 'T':
12670                     case 'Z':
12671                       INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
12672                       break;
12673
12674                     case 'R':
12675                       INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
12676                       break;
12677                     }
12678                   lastregno = regno;
12679                   continue;
12680                 }
12681
12682               switch (*args++)
12683                 {
12684                 case 'V':
12685                   INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
12686                   continue;
12687
12688                 case 'W':
12689                   INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
12690                   continue;
12691                 }
12692               break;
12693
12694             case 'I':
12695               my_getExpression (&imm_expr, s);
12696               if (imm_expr.X_op != O_big
12697                   && imm_expr.X_op != O_constant)
12698                 insn_error = _("absolute expression required");
12699               if (HAVE_32BIT_GPRS)
12700                 normalize_constant_expr (&imm_expr);
12701               s = expr_end;
12702               continue;
12703
12704             case 'A':
12705               my_getSmallExpression (&offset_expr, offset_reloc, s);
12706               if (offset_expr.X_op == O_register)
12707                 {
12708                   /* Assume that the offset has been elided and that what
12709                      we saw was a base register.  The match will fail later
12710                      if that assumption turns out to be wrong.  */
12711                   offset_expr.X_op = O_constant;
12712                   offset_expr.X_add_number = 0;
12713                 }
12714               else
12715                 {
12716                   normalize_address_expr (&offset_expr);
12717                   s = expr_end;
12718                 }
12719               continue;
12720
12721             case 'F':
12722             case 'L':
12723             case 'f':
12724             case 'l':
12725               {
12726                 int f64;
12727                 int using_gprs;
12728                 char *save_in;
12729                 char *err;
12730                 unsigned char temp[8];
12731                 int len;
12732                 unsigned int length;
12733                 segT seg;
12734                 subsegT subseg;
12735                 char *p;
12736
12737                 /* These only appear as the last operand in an
12738                    instruction, and every instruction that accepts
12739                    them in any variant accepts them in all variants.
12740                    This means we don't have to worry about backing out
12741                    any changes if the instruction does not match.
12742
12743                    The difference between them is the size of the
12744                    floating point constant and where it goes.  For 'F'
12745                    and 'L' the constant is 64 bits; for 'f' and 'l' it
12746                    is 32 bits.  Where the constant is placed is based
12747                    on how the MIPS assembler does things:
12748                     F -- .rdata
12749                     L -- .lit8
12750                     f -- immediate value
12751                     l -- .lit4
12752
12753                     The .lit4 and .lit8 sections are only used if
12754                     permitted by the -G argument.
12755
12756                     The code below needs to know whether the target register
12757                     is 32 or 64 bits wide.  It relies on the fact 'f' and
12758                     'F' are used with GPR-based instructions and 'l' and
12759                     'L' are used with FPR-based instructions.  */
12760
12761                 f64 = *args == 'F' || *args == 'L';
12762                 using_gprs = *args == 'F' || *args == 'f';
12763
12764                 save_in = input_line_pointer;
12765                 input_line_pointer = s;
12766                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12767                 length = len;
12768                 s = input_line_pointer;
12769                 input_line_pointer = save_in;
12770                 if (err != NULL && *err != '\0')
12771                   {
12772                     as_bad (_("Bad floating point constant: %s"), err);
12773                     memset (temp, '\0', sizeof temp);
12774                     length = f64 ? 8 : 4;
12775                   }
12776
12777                 gas_assert (length == (unsigned) (f64 ? 8 : 4));
12778
12779                 if (*args == 'f'
12780                     || (*args == 'l'
12781                         && (g_switch_value < 4
12782                             || (temp[0] == 0 && temp[1] == 0)
12783                             || (temp[2] == 0 && temp[3] == 0))))
12784                   {
12785                     imm_expr.X_op = O_constant;
12786                     if (!target_big_endian)
12787                       imm_expr.X_add_number = bfd_getl32 (temp);
12788                     else
12789                       imm_expr.X_add_number = bfd_getb32 (temp);
12790                   }
12791                 else if (length > 4
12792                          && !mips_disable_float_construction
12793                          /* Constants can only be constructed in GPRs and
12794                             copied to FPRs if the GPRs are at least as wide
12795                             as the FPRs.  Force the constant into memory if
12796                             we are using 64-bit FPRs but the GPRs are only
12797                             32 bits wide.  */
12798                          && (using_gprs
12799                              || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
12800                          && ((temp[0] == 0 && temp[1] == 0)
12801                              || (temp[2] == 0 && temp[3] == 0))
12802                          && ((temp[4] == 0 && temp[5] == 0)
12803                              || (temp[6] == 0 && temp[7] == 0)))
12804                   {
12805                     /* The value is simple enough to load with a couple of
12806                        instructions.  If using 32-bit registers, set
12807                        imm_expr to the high order 32 bits and offset_expr to
12808                        the low order 32 bits.  Otherwise, set imm_expr to
12809                        the entire 64 bit constant.  */
12810                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
12811                       {
12812                         imm_expr.X_op = O_constant;
12813                         offset_expr.X_op = O_constant;
12814                         if (!target_big_endian)
12815                           {
12816                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
12817                             offset_expr.X_add_number = bfd_getl32 (temp);
12818                           }
12819                         else
12820                           {
12821                             imm_expr.X_add_number = bfd_getb32 (temp);
12822                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
12823                           }
12824                         if (offset_expr.X_add_number == 0)
12825                           offset_expr.X_op = O_absent;
12826                       }
12827                     else
12828                       {
12829                         imm_expr.X_op = O_constant;
12830                         if (!target_big_endian)
12831                           imm_expr.X_add_number = bfd_getl64 (temp);
12832                         else
12833                           imm_expr.X_add_number = bfd_getb64 (temp);
12834                       }
12835                   }
12836                 else
12837                   {
12838                     const char *newname;
12839                     segT new_seg;
12840
12841                     /* Switch to the right section.  */
12842                     seg = now_seg;
12843                     subseg = now_subseg;
12844                     switch (*args)
12845                       {
12846                       default: /* unused default case avoids warnings.  */
12847                       case 'L':
12848                         newname = RDATA_SECTION_NAME;
12849                         if (g_switch_value >= 8)
12850                           newname = ".lit8";
12851                         break;
12852                       case 'F':
12853                         newname = RDATA_SECTION_NAME;
12854                         break;
12855                       case 'l':
12856                         gas_assert (g_switch_value >= 4);
12857                         newname = ".lit4";
12858                         break;
12859                       }
12860                     new_seg = subseg_new (newname, (subsegT) 0);
12861                     bfd_set_section_flags (stdoutput, new_seg,
12862                                            (SEC_ALLOC
12863                                             | SEC_LOAD
12864                                             | SEC_READONLY
12865                                             | SEC_DATA));
12866                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
12867                     if (strncmp (TARGET_OS, "elf", 3) != 0)
12868                       record_alignment (new_seg, 4);
12869                     else
12870                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
12871                     if (seg == now_seg)
12872                       as_bad (_("Can't use floating point insn in this section"));
12873
12874                     /* Set the argument to the current address in the
12875                        section.  */
12876                     offset_expr.X_op = O_symbol;
12877                     offset_expr.X_add_symbol = symbol_temp_new_now ();
12878                     offset_expr.X_add_number = 0;
12879
12880                     /* Put the floating point number into the section.  */
12881                     p = frag_more ((int) length);
12882                     memcpy (p, temp, length);
12883
12884                     /* Switch back to the original section.  */
12885                     subseg_set (seg, subseg);
12886                   }
12887               }
12888               continue;
12889
12890             case 'i':           /* 16-bit unsigned immediate.  */
12891             case 'j':           /* 16-bit signed immediate.  */
12892               *imm_reloc = BFD_RELOC_LO16;
12893               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12894                 {
12895                   int more;
12896                   offsetT minval, maxval;
12897
12898                   more = (insn + 1 < past
12899                           && strcmp (insn->name, insn[1].name) == 0);
12900
12901                   /* For compatibility with older assemblers, we accept
12902                      0x8000-0xffff as signed 16-bit numbers when only
12903                      signed numbers are allowed.  */
12904                   if (*args == 'i')
12905                     minval = 0, maxval = 0xffff;
12906                   else if (more)
12907                     minval = -0x8000, maxval = 0x7fff;
12908                   else
12909                     minval = -0x8000, maxval = 0xffff;
12910
12911                   if (imm_expr.X_op != O_constant
12912                       || imm_expr.X_add_number < minval
12913                       || imm_expr.X_add_number > maxval)
12914                     {
12915                       if (more)
12916                         break;
12917                       if (imm_expr.X_op == O_constant
12918                           || imm_expr.X_op == O_big)
12919                         as_bad (_("Expression out of range"));
12920                     }
12921                 }
12922               s = expr_end;
12923               continue;
12924
12925             case 'o':           /* 16-bit offset.  */
12926               offset_reloc[0] = BFD_RELOC_LO16;
12927               offset_reloc[1] = BFD_RELOC_UNUSED;
12928               offset_reloc[2] = BFD_RELOC_UNUSED;
12929
12930               /* Check whether there is only a single bracketed expression
12931                  left.  If so, it must be the base register and the
12932                  constant must be zero.  */
12933               if (*s == '(' && strchr (s + 1, '(') == 0)
12934                 {
12935                   offset_expr.X_op = O_constant;
12936                   offset_expr.X_add_number = 0;
12937                   continue;
12938                 }
12939
12940               /* If this value won't fit into a 16 bit offset, then go
12941                  find a macro that will generate the 32 bit offset
12942                  code pattern.  */
12943               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12944                   && (offset_expr.X_op != O_constant
12945                       || offset_expr.X_add_number >= 0x8000
12946                       || offset_expr.X_add_number < -0x8000))
12947                 break;
12948
12949               s = expr_end;
12950               continue;
12951
12952             case 'p':           /* PC-relative offset.  */
12953               *offset_reloc = BFD_RELOC_16_PCREL_S2;
12954               my_getExpression (&offset_expr, s);
12955               s = expr_end;
12956               continue;
12957
12958             case 'u':           /* Upper 16 bits.  */
12959               *imm_reloc = BFD_RELOC_LO16;
12960               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12961                   && imm_expr.X_op == O_constant
12962                   && (imm_expr.X_add_number < 0
12963                       || imm_expr.X_add_number >= 0x10000))
12964                 as_bad (_("lui expression (%lu) not in range 0..65535"),
12965                         (unsigned long) imm_expr.X_add_number);
12966               s = expr_end;
12967               continue;
12968
12969             case 'a':           /* 26-bit address.  */
12970             jump:
12971               *offset_reloc = BFD_RELOC_MIPS_JMP;
12972               my_getExpression (&offset_expr, s);
12973               s = expr_end;
12974               continue;
12975
12976             case 'N':           /* 3-bit branch condition code.  */
12977             case 'M':           /* 3-bit compare condition code.  */
12978               rtype = RTYPE_CCC;
12979               if (ip->insn_mo->pinfo & (FP_D | FP_S))
12980                 rtype |= RTYPE_FCC;
12981               if (!reg_lookup (&s, rtype, &regno))
12982                 break;
12983               if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12984                    || strcmp (str + strlen (str) - 5, "any2f") == 0
12985                    || strcmp (str + strlen (str) - 5, "any2t") == 0)
12986                   && (regno & 1) != 0)
12987                 as_warn (_("Condition code register should be even for %s, "
12988                            "was %d"),
12989                          str, regno);
12990               if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12991                    || strcmp (str + strlen (str) - 5, "any4t") == 0)
12992                   && (regno & 3) != 0)
12993                 as_warn (_("Condition code register should be 0 or 4 for %s, "
12994                            "was %d"),
12995                          str, regno);
12996               if (*args == 'N')
12997                 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12998               else
12999                 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
13000               continue;
13001
13002             case 'H':
13003               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
13004                 s += 2;
13005               if (ISDIGIT (*s))
13006                 {
13007                   c = 0;
13008                   do
13009                     {
13010                       c *= 10;
13011                       c += *s - '0';
13012                       ++s;
13013                     }
13014                   while (ISDIGIT (*s));
13015                 }
13016               else
13017                 c = 8; /* Invalid sel value.  */
13018
13019               if (c > 7)
13020                 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
13021               INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
13022               continue;
13023
13024             case 'e':
13025               gas_assert (!mips_opts.micromips);
13026               /* Must be at least one digit.  */
13027               my_getExpression (&imm_expr, s);
13028               check_absolute_expr (ip, &imm_expr);
13029
13030               if ((unsigned long) imm_expr.X_add_number
13031                   > (unsigned long) OP_MASK_VECBYTE)
13032                 {
13033                   as_bad (_("bad byte vector index (%ld)"),
13034                            (long) imm_expr.X_add_number);
13035                   imm_expr.X_add_number = 0;
13036                 }
13037
13038               INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
13039               imm_expr.X_op = O_absent;
13040               s = expr_end;
13041               continue;
13042
13043             case '%':
13044               gas_assert (!mips_opts.micromips);
13045               my_getExpression (&imm_expr, s);
13046               check_absolute_expr (ip, &imm_expr);
13047
13048               if ((unsigned long) imm_expr.X_add_number
13049                   > (unsigned long) OP_MASK_VECALIGN)
13050                 {
13051                   as_bad (_("bad byte vector index (%ld)"),
13052                            (long) imm_expr.X_add_number);
13053                   imm_expr.X_add_number = 0;
13054                 }
13055
13056               INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
13057               imm_expr.X_op = O_absent;
13058               s = expr_end;
13059               continue;
13060
13061             case 'm':           /* Opcode extension character.  */
13062               gas_assert (mips_opts.micromips);
13063               c = *++args;
13064               switch (c)
13065                 {
13066                 case 'r':
13067                   if (strncmp (s, "$pc", 3) == 0)
13068                     {
13069                       s += 3;
13070                       continue;
13071                     }
13072                   break;
13073
13074                 case 'a':
13075                 case 'b':
13076                 case 'c':
13077                 case 'd':
13078                 case 'e':
13079                 case 'f':
13080                 case 'g':
13081                 case 'h':
13082                 case 'j':
13083                 case 'l':
13084                 case 'm':
13085                 case 'n':
13086                 case 'p':
13087                 case 'q':
13088                 case 's':
13089                 case 't':
13090                 case 'x':
13091                 case 'y':
13092                 case 'z':
13093                   s_reset = s;
13094                   ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
13095                   if (regno == AT && mips_opts.at)
13096                     {
13097                       if (mips_opts.at == ATREG)
13098                         as_warn (_("Used $at without \".set noat\""));
13099                       else
13100                         as_warn (_("Used $%u with \".set at=$%u\""),
13101                                  regno, mips_opts.at);
13102                     }
13103                   if (!ok)
13104                     {
13105                       if (c == 'c')
13106                         {
13107                           gas_assert (args[1] == ',');
13108                           regno = lastregno;
13109                           ++args;
13110                         }
13111                       else if (c == 't')
13112                         {
13113                           gas_assert (args[1] == ',');
13114                           ++args;
13115                           continue;                     /* Nothing to do.  */
13116                         }
13117                       else
13118                         break;
13119                     }
13120
13121                   if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
13122                     {
13123                       if (regno == lastregno)
13124                         {
13125                           insn_error
13126                             = _("Source and destination must be different");
13127                           continue;
13128                         }
13129                       if (regno == 31 && lastregno == 0xffffffff)
13130                         {
13131                           insn_error
13132                             = _("A destination register must be supplied");
13133                           continue;
13134                         }
13135                     }
13136
13137                   if (*s == ' ')
13138                     ++s;
13139                   if (args[1] != *s)
13140                     {
13141                       if (c == 'e')
13142                         {
13143                           gas_assert (args[1] == ',');
13144                           regno = lastregno;
13145                           s = s_reset;
13146                           ++args;
13147                         }
13148                       else if (c == 't')
13149                         {
13150                           gas_assert (args[1] == ',');
13151                           s = s_reset;
13152                           ++args;
13153                           continue;                     /* Nothing to do.  */
13154                         }
13155                     }
13156
13157                   /* Make sure regno is the same as lastregno.  */
13158                   if (c == 't' && regno != lastregno)
13159                     break;
13160
13161                   /* Make sure regno is the same as destregno.  */
13162                   if (c == 'x' && regno != destregno)
13163                     break;
13164
13165                   /* We need to save regno, before regno maps to the
13166                      microMIPS register encoding.  */
13167                   lastregno = regno;
13168
13169                   if (c == 'f')
13170                     destregno = regno;
13171
13172                   switch (c)
13173                     {
13174                       case 'a':
13175                         if (regno != GP)
13176                           regno = ILLEGAL_REG;
13177                         break;
13178
13179                       case 'b':
13180                         regno = mips32_to_micromips_reg_b_map[regno];
13181                         break;
13182
13183                       case 'c':
13184                         regno = mips32_to_micromips_reg_c_map[regno];
13185                         break;
13186
13187                       case 'd':
13188                         regno = mips32_to_micromips_reg_d_map[regno];
13189                         break;
13190
13191                       case 'e':
13192                         regno = mips32_to_micromips_reg_e_map[regno];
13193                         break;
13194
13195                       case 'f':
13196                         regno = mips32_to_micromips_reg_f_map[regno];
13197                         break;
13198
13199                       case 'g':
13200                         regno = mips32_to_micromips_reg_g_map[regno];
13201                         break;
13202
13203                       case 'h':
13204                         s += strspn (s, " \t");
13205                         if (*s != ',')
13206                           {
13207                             regno = ILLEGAL_REG;
13208                             break;
13209                           }
13210                         ++s;
13211                         s += strspn (s, " \t");
13212                         ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno2);
13213                         if (!ok)
13214                           {
13215                             regno = ILLEGAL_REG;
13216                             break;
13217                           }
13218                         if (regno2 == AT && mips_opts.at)
13219                           {
13220                             if (mips_opts.at == ATREG)
13221                               as_warn (_("Used $at without \".set noat\""));
13222                             else
13223                               as_warn (_("Used $%u with \".set at=$%u\""),
13224                                        regno2, mips_opts.at);
13225                           }
13226                         regno = (mips_lookup_reg_pair
13227                                  (regno, regno2,
13228                                   micromips_to_32_reg_h_map1,
13229                                   micromips_to_32_reg_h_map2, 8));
13230                         break;
13231
13232                       case 'l':
13233                         regno = mips32_to_micromips_reg_l_map[regno];
13234                         break;
13235
13236                       case 'm':
13237                         regno = mips32_to_micromips_reg_m_map[regno];
13238                         break;
13239
13240                       case 'n':
13241                         regno = mips32_to_micromips_reg_n_map[regno];
13242                         break;
13243
13244                       case 'q':
13245                         regno = mips32_to_micromips_reg_q_map[regno];
13246                         break;
13247
13248                       case 's':
13249                         if (regno != SP)
13250                           regno = ILLEGAL_REG;
13251                         break;
13252
13253                       case 'y':
13254                         if (regno != 31)
13255                           regno = ILLEGAL_REG;
13256                         break;
13257
13258                       case 'z':
13259                         if (regno != ZERO)
13260                           regno = ILLEGAL_REG;
13261                         break;
13262
13263                       case 'j': /* Do nothing.  */
13264                       case 'p':
13265                       case 't':
13266                       case 'x':
13267                         break;
13268
13269                       default:
13270                         abort ();
13271                     }
13272
13273                   if (regno == ILLEGAL_REG)
13274                     break;
13275
13276                   switch (c)
13277                     {
13278                       case 'b':
13279                         INSERT_OPERAND (1, MB, *ip, regno);
13280                         break;
13281
13282                       case 'c':
13283                         INSERT_OPERAND (1, MC, *ip, regno);
13284                         break;
13285
13286                       case 'd':
13287                         INSERT_OPERAND (1, MD, *ip, regno);
13288                         break;
13289
13290                       case 'e':
13291                         INSERT_OPERAND (1, ME, *ip, regno);
13292                         break;
13293
13294                       case 'f':
13295                         INSERT_OPERAND (1, MF, *ip, regno);
13296                         break;
13297
13298                       case 'g':
13299                         INSERT_OPERAND (1, MG, *ip, regno);
13300                         break;
13301
13302                       case 'h':
13303                         INSERT_OPERAND (1, MH, *ip, regno);
13304                         break;
13305
13306                       case 'j':
13307                         INSERT_OPERAND (1, MJ, *ip, regno);
13308                         break;
13309
13310                       case 'l':
13311                         INSERT_OPERAND (1, ML, *ip, regno);
13312                         break;
13313
13314                       case 'm':
13315                         INSERT_OPERAND (1, MM, *ip, regno);
13316                         break;
13317
13318                       case 'n':
13319                         INSERT_OPERAND (1, MN, *ip, regno);
13320                         break;
13321
13322                       case 'p':
13323                         INSERT_OPERAND (1, MP, *ip, regno);
13324                         break;
13325
13326                       case 'q':
13327                         INSERT_OPERAND (1, MQ, *ip, regno);
13328                         break;
13329
13330                       case 'a': /* Do nothing.  */
13331                       case 's': /* Do nothing.  */
13332                       case 't': /* Do nothing.  */
13333                       case 'x': /* Do nothing.  */
13334                       case 'y': /* Do nothing.  */
13335                       case 'z': /* Do nothing.  */
13336                         break;
13337
13338                       default:
13339                         abort ();
13340                     }
13341                   continue;
13342
13343                 case 'A':
13344                   {
13345                     bfd_reloc_code_real_type r[3];
13346                     expressionS ep;
13347                     int imm;
13348
13349                     /* Check whether there is only a single bracketed
13350                        expression left.  If so, it must be the base register
13351                        and the constant must be zero.  */
13352                     if (*s == '(' && strchr (s + 1, '(') == 0)
13353                       {
13354                         INSERT_OPERAND (1, IMMA, *ip, 0);
13355                         continue;
13356                       }
13357
13358                     if (my_getSmallExpression (&ep, r, s) > 0
13359                         || !expr_const_in_range (&ep, -64, 64, 2))
13360                       break;
13361
13362                     imm = ep.X_add_number >> 2;
13363                     INSERT_OPERAND (1, IMMA, *ip, imm);
13364                   }
13365                   s = expr_end;
13366                   continue;
13367
13368                 case 'B':
13369                   {
13370                     bfd_reloc_code_real_type r[3];
13371                     expressionS ep;
13372                     int imm;
13373
13374                     if (my_getSmallExpression (&ep, r, s) > 0
13375                         || ep.X_op != O_constant)
13376                       break;
13377
13378                     for (imm = 0; imm < 8; imm++)
13379                       if (micromips_imm_b_map[imm] == ep.X_add_number)
13380                         break;
13381                     if (imm >= 8)
13382                       break;
13383
13384                     INSERT_OPERAND (1, IMMB, *ip, imm);
13385                   }
13386                   s = expr_end;
13387                   continue;
13388
13389                 case 'C':
13390                   {
13391                     bfd_reloc_code_real_type r[3];
13392                     expressionS ep;
13393                     int imm;
13394
13395                     if (my_getSmallExpression (&ep, r, s) > 0
13396                         || ep.X_op != O_constant)
13397                       break;
13398
13399                     for (imm = 0; imm < 16; imm++)
13400                       if (micromips_imm_c_map[imm] == ep.X_add_number)
13401                         break;
13402                     if (imm >= 16)
13403                       break;
13404
13405                     INSERT_OPERAND (1, IMMC, *ip, imm);
13406                   }
13407                   s = expr_end;
13408                   continue;
13409
13410                 case 'D':       /* pc relative offset */
13411                 case 'E':       /* pc relative offset */
13412                   my_getExpression (&offset_expr, s);
13413                   if (offset_expr.X_op == O_register)
13414                     break;
13415
13416                   if (!forced_insn_length)
13417                     *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13418                   else if (c == 'D')
13419                     *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
13420                   else
13421                     *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
13422                   s = expr_end;
13423                   continue;
13424
13425                 case 'F':
13426                   {
13427                     bfd_reloc_code_real_type r[3];
13428                     expressionS ep;
13429                     int imm;
13430
13431                     if (my_getSmallExpression (&ep, r, s) > 0
13432                         || !expr_const_in_range (&ep, 0, 16, 0))
13433                       break;
13434
13435                     imm = ep.X_add_number;
13436                     INSERT_OPERAND (1, IMMF, *ip, imm);
13437                   }
13438                   s = expr_end;
13439                   continue;
13440
13441                 case 'G':
13442                   {
13443                     bfd_reloc_code_real_type r[3];
13444                     expressionS ep;
13445                     int imm;
13446
13447                     /* Check whether there is only a single bracketed
13448                        expression left.  If so, it must be the base register
13449                        and the constant must be zero.  */
13450                     if (*s == '(' && strchr (s + 1, '(') == 0)
13451                       {
13452                         INSERT_OPERAND (1, IMMG, *ip, 0);
13453                         continue;
13454                       }
13455
13456                     if (my_getSmallExpression (&ep, r, s) > 0
13457                         || !expr_const_in_range (&ep, -1, 15, 0))
13458                       break;
13459
13460                     imm = ep.X_add_number & 15;
13461                     INSERT_OPERAND (1, IMMG, *ip, imm);
13462                   }
13463                   s = expr_end;
13464                   continue;
13465
13466                 case 'H':
13467                   {
13468                     bfd_reloc_code_real_type r[3];
13469                     expressionS ep;
13470                     int imm;
13471
13472                     /* Check whether there is only a single bracketed
13473                        expression left.  If so, it must be the base register
13474                        and the constant must be zero.  */
13475                     if (*s == '(' && strchr (s + 1, '(') == 0)
13476                       {
13477                         INSERT_OPERAND (1, IMMH, *ip, 0);
13478                         continue;
13479                       }
13480
13481                     if (my_getSmallExpression (&ep, r, s) > 0
13482                         || !expr_const_in_range (&ep, 0, 16, 1))
13483                       break;
13484
13485                     imm = ep.X_add_number >> 1;
13486                     INSERT_OPERAND (1, IMMH, *ip, imm);
13487                   }
13488                   s = expr_end;
13489                   continue;
13490
13491                 case 'I':
13492                   {
13493                     bfd_reloc_code_real_type r[3];
13494                     expressionS ep;
13495                     int imm;
13496
13497                     if (my_getSmallExpression (&ep, r, s) > 0
13498                         || !expr_const_in_range (&ep, -1, 127, 0))
13499                       break;
13500
13501                     imm = ep.X_add_number & 127;
13502                     INSERT_OPERAND (1, IMMI, *ip, imm);
13503                   }
13504                   s = expr_end;
13505                   continue;
13506
13507                 case 'J':
13508                   {
13509                     bfd_reloc_code_real_type r[3];
13510                     expressionS ep;
13511                     int imm;
13512
13513                     /* Check whether there is only a single bracketed
13514                        expression left.  If so, it must be the base register
13515                        and the constant must be zero.  */
13516                     if (*s == '(' && strchr (s + 1, '(') == 0)
13517                       {
13518                         INSERT_OPERAND (1, IMMJ, *ip, 0);
13519                         continue;
13520                       }
13521
13522                     if (my_getSmallExpression (&ep, r, s) > 0
13523                         || !expr_const_in_range (&ep, 0, 16, 2))
13524                       break;
13525
13526                     imm = ep.X_add_number >> 2;
13527                     INSERT_OPERAND (1, IMMJ, *ip, imm);
13528                   }
13529                   s = expr_end;
13530                   continue;
13531
13532                 case 'L':
13533                   {
13534                     bfd_reloc_code_real_type r[3];
13535                     expressionS ep;
13536                     int imm;
13537
13538                     /* Check whether there is only a single bracketed
13539                        expression left.  If so, it must be the base register
13540                        and the constant must be zero.  */
13541                     if (*s == '(' && strchr (s + 1, '(') == 0)
13542                       {
13543                         INSERT_OPERAND (1, IMML, *ip, 0);
13544                         continue;
13545                       }
13546
13547                     if (my_getSmallExpression (&ep, r, s) > 0
13548                         || !expr_const_in_range (&ep, 0, 16, 0))
13549                       break;
13550
13551                     imm = ep.X_add_number;
13552                     INSERT_OPERAND (1, IMML, *ip, imm);
13553                   }
13554                   s = expr_end;
13555                   continue;
13556
13557                 case 'M':
13558                   {
13559                     bfd_reloc_code_real_type r[3];
13560                     expressionS ep;
13561                     int imm;
13562
13563                     if (my_getSmallExpression (&ep, r, s) > 0
13564                         || !expr_const_in_range (&ep, 1, 9, 0))
13565                       break;
13566
13567                     imm = ep.X_add_number & 7;
13568                     INSERT_OPERAND (1, IMMM, *ip, imm);
13569                   }
13570                   s = expr_end;
13571                   continue;
13572
13573                 case 'N':       /* Register list for lwm and swm.  */
13574                   {
13575                     /* A comma-separated list of registers and/or
13576                        dash-separated contiguous ranges including
13577                        both ra and a set of one or more registers
13578                        starting at s0 up to s3 which have to be
13579                        consecutive, e.g.:
13580
13581                        s0, ra
13582                        s0, s1, ra, s2, s3
13583                        s0-s2, ra
13584
13585                        and any permutations of these.  */
13586                     unsigned int reglist;
13587                     int imm;
13588
13589                     if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13590                       break;
13591
13592                     if ((reglist & 0xfff1ffff) != 0x80010000)
13593                       break;
13594
13595                     reglist = (reglist >> 17) & 7;
13596                     reglist += 1;
13597                     if ((reglist & -reglist) != reglist)
13598                       break;
13599
13600                     imm = ffs (reglist) - 1;
13601                     INSERT_OPERAND (1, IMMN, *ip, imm);
13602                   }
13603                   continue;
13604
13605                 case 'O':       /* sdbbp 4-bit code.  */
13606                   {
13607                     bfd_reloc_code_real_type r[3];
13608                     expressionS ep;
13609                     int imm;
13610
13611                     if (my_getSmallExpression (&ep, r, s) > 0
13612                         || !expr_const_in_range (&ep, 0, 16, 0))
13613                       break;
13614
13615                     imm = ep.X_add_number;
13616                     INSERT_OPERAND (1, IMMO, *ip, imm);
13617                   }
13618                   s = expr_end;
13619                   continue;
13620
13621                 case 'P':
13622                   {
13623                     bfd_reloc_code_real_type r[3];
13624                     expressionS ep;
13625                     int imm;
13626
13627                     if (my_getSmallExpression (&ep, r, s) > 0
13628                         || !expr_const_in_range (&ep, 0, 32, 2))
13629                       break;
13630
13631                     imm = ep.X_add_number >> 2;
13632                     INSERT_OPERAND (1, IMMP, *ip, imm);
13633                   }
13634                   s = expr_end;
13635                   continue;
13636
13637                 case 'Q':
13638                   {
13639                     bfd_reloc_code_real_type r[3];
13640                     expressionS ep;
13641                     int imm;
13642
13643                     if (my_getSmallExpression (&ep, r, s) > 0
13644                         || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13645                       break;
13646
13647                     imm = ep.X_add_number >> 2;
13648                     INSERT_OPERAND (1, IMMQ, *ip, imm);
13649                   }
13650                   s = expr_end;
13651                   continue;
13652
13653                 case 'U':
13654                   {
13655                     bfd_reloc_code_real_type r[3];
13656                     expressionS ep;
13657                     int imm;
13658
13659                     /* Check whether there is only a single bracketed
13660                        expression left.  If so, it must be the base register
13661                        and the constant must be zero.  */
13662                     if (*s == '(' && strchr (s + 1, '(') == 0)
13663                       {
13664                         INSERT_OPERAND (1, IMMU, *ip, 0);
13665                         continue;
13666                       }
13667
13668                     if (my_getSmallExpression (&ep, r, s) > 0
13669                         || !expr_const_in_range (&ep, 0, 32, 2))
13670                       break;
13671
13672                     imm = ep.X_add_number >> 2;
13673                     INSERT_OPERAND (1, IMMU, *ip, imm);
13674                   }
13675                   s = expr_end;
13676                   continue;
13677
13678                 case 'W':
13679                   {
13680                     bfd_reloc_code_real_type r[3];
13681                     expressionS ep;
13682                     int imm;
13683
13684                     if (my_getSmallExpression (&ep, r, s) > 0
13685                         || !expr_const_in_range (&ep, 0, 64, 2))
13686                       break;
13687
13688                     imm = ep.X_add_number >> 2;
13689                     INSERT_OPERAND (1, IMMW, *ip, imm);
13690                   }
13691                   s = expr_end;
13692                   continue;
13693
13694                 case 'X':
13695                   {
13696                     bfd_reloc_code_real_type r[3];
13697                     expressionS ep;
13698                     int imm;
13699
13700                     if (my_getSmallExpression (&ep, r, s) > 0
13701                         || !expr_const_in_range (&ep, -8, 8, 0))
13702                       break;
13703
13704                     imm = ep.X_add_number;
13705                     INSERT_OPERAND (1, IMMX, *ip, imm);
13706                   }
13707                   s = expr_end;
13708                   continue;
13709
13710                 case 'Y':
13711                   {
13712                     bfd_reloc_code_real_type r[3];
13713                     expressionS ep;
13714                     int imm;
13715
13716                     if (my_getSmallExpression (&ep, r, s) > 0
13717                         || expr_const_in_range (&ep, -2, 2, 2)
13718                         || !expr_const_in_range (&ep, -258, 258, 2))
13719                       break;
13720
13721                     imm = ep.X_add_number >> 2;
13722                     imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13723                     INSERT_OPERAND (1, IMMY, *ip, imm);
13724                   }
13725                   s = expr_end;
13726                   continue;
13727
13728                 case 'Z':
13729                   {
13730                     bfd_reloc_code_real_type r[3];
13731                     expressionS ep;
13732
13733                     if (my_getSmallExpression (&ep, r, s) > 0
13734                         || !expr_const_in_range (&ep, 0, 1, 0))
13735                       break;
13736                   }
13737                   s = expr_end;
13738                   continue;
13739
13740                 default:
13741                   as_bad (_("Internal error: bad microMIPS opcode "
13742                             "(unknown extension operand type `m%c'): %s %s"),
13743                           *args, insn->name, insn->args);
13744                   /* Further processing is fruitless.  */
13745                   return;
13746                 }
13747               break;
13748
13749             case 'n':           /* Register list for 32-bit lwm and swm.  */
13750               gas_assert (mips_opts.micromips);
13751               {
13752                 /* A comma-separated list of registers and/or
13753                    dash-separated contiguous ranges including
13754                    at least one of ra and a set of one or more
13755                    registers starting at s0 up to s7 and then
13756                    s8 which have to be consecutive, e.g.:
13757
13758                    ra
13759                    s0
13760                    ra, s0, s1, s2
13761                    s0-s8
13762                    s0-s5, ra
13763
13764                    and any permutations of these.  */
13765                 unsigned int reglist;
13766                 int imm;
13767                 int ra;
13768
13769                 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13770                   break;
13771
13772                 if ((reglist & 0x3f00ffff) != 0)
13773                   break;
13774
13775                 ra = (reglist >> 27) & 0x10;
13776                 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13777                 reglist += 1;
13778                 if ((reglist & -reglist) != reglist)
13779                   break;
13780
13781                 imm = (ffs (reglist) - 1) | ra;
13782                 INSERT_OPERAND (1, RT, *ip, imm);
13783                 imm_expr.X_op = O_absent;
13784               }
13785               continue;
13786
13787             case '|':           /* 4-bit trap code.  */
13788               gas_assert (mips_opts.micromips);
13789               my_getExpression (&imm_expr, s);
13790               check_absolute_expr (ip, &imm_expr);
13791               if ((unsigned long) imm_expr.X_add_number
13792                   > MICROMIPSOP_MASK_TRAP)
13793                 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13794                         (unsigned long) imm_expr.X_add_number,
13795                         ip->insn_mo->name);
13796               INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
13797               imm_expr.X_op = O_absent;
13798               s = expr_end;
13799               continue;
13800
13801             default:
13802               as_bad (_("Bad char = '%c'\n"), *args);
13803               abort ();
13804             }
13805           break;
13806         }
13807       /* Args don't match.  */
13808       s = argsStart;
13809       insn_error = _("Illegal operands");
13810       if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
13811         {
13812           ++insn;
13813           continue;
13814         }
13815       else if (wrong_delay_slot_insns && need_delay_slot_ok)
13816         {
13817           gas_assert (firstinsn);
13818           need_delay_slot_ok = FALSE;
13819           past = insn + 1;
13820           insn = firstinsn;
13821           continue;
13822         }
13823       return;
13824     }
13825 }
13826
13827 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13828
13829 /* This routine assembles an instruction into its binary format when
13830    assembling for the mips16.  As a side effect, it sets one of the
13831    global variables imm_reloc or offset_reloc to the type of relocation
13832    to do if one of the operands is an address expression.  It also sets
13833    forced_insn_length to the resulting instruction size in bytes if the
13834    user explicitly requested a small or extended instruction.  */
13835
13836 static void
13837 mips16_ip (char *str, struct mips_cl_insn *ip)
13838 {
13839   char *s;
13840   const char *args;
13841   struct mips_opcode *insn;
13842   char *argsstart;
13843   unsigned int regno;
13844   unsigned int lastregno = 0;
13845   char *s_reset;
13846   size_t i;
13847
13848   insn_error = NULL;
13849
13850   forced_insn_length = 0;
13851
13852   for (s = str; ISLOWER (*s); ++s)
13853     ;
13854   switch (*s)
13855     {
13856     case '\0':
13857       break;
13858
13859     case ' ':
13860       *s++ = '\0';
13861       break;
13862
13863     case '.':
13864       if (s[1] == 't' && s[2] == ' ')
13865         {
13866           *s = '\0';
13867           forced_insn_length = 2;
13868           s += 3;
13869           break;
13870         }
13871       else if (s[1] == 'e' && s[2] == ' ')
13872         {
13873           *s = '\0';
13874           forced_insn_length = 4;
13875           s += 3;
13876           break;
13877         }
13878       /* Fall through.  */
13879     default:
13880       insn_error = _("unknown opcode");
13881       return;
13882     }
13883
13884   if (mips_opts.noautoextend && !forced_insn_length)
13885     forced_insn_length = 2;
13886
13887   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13888     {
13889       insn_error = _("unrecognized opcode");
13890       return;
13891     }
13892
13893   argsstart = s;
13894   for (;;)
13895     {
13896       bfd_boolean ok;
13897
13898       gas_assert (strcmp (insn->name, str) == 0);
13899
13900       ok = is_opcode_valid_16 (insn);
13901       if (! ok)
13902         {
13903           if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13904               && strcmp (insn->name, insn[1].name) == 0)
13905             {
13906               ++insn;
13907               continue;
13908             }
13909           else
13910             {
13911               if (!insn_error)
13912                 {
13913                   static char buf[100];
13914                   sprintf (buf,
13915                            _("Opcode not supported on this processor: %s (%s)"),
13916                            mips_cpu_info_from_arch (mips_opts.arch)->name,
13917                            mips_cpu_info_from_isa (mips_opts.isa)->name);
13918                   insn_error = buf;
13919                 }
13920               return;
13921             }
13922         }
13923
13924       create_insn (ip, insn);
13925       imm_expr.X_op = O_absent;
13926       imm_reloc[0] = BFD_RELOC_UNUSED;
13927       imm_reloc[1] = BFD_RELOC_UNUSED;
13928       imm_reloc[2] = BFD_RELOC_UNUSED;
13929       imm2_expr.X_op = O_absent;
13930       offset_expr.X_op = O_absent;
13931       offset_reloc[0] = BFD_RELOC_UNUSED;
13932       offset_reloc[1] = BFD_RELOC_UNUSED;
13933       offset_reloc[2] = BFD_RELOC_UNUSED;
13934       for (args = insn->args; 1; ++args)
13935         {
13936           int c;
13937
13938           if (*s == ' ')
13939             ++s;
13940
13941           /* In this switch statement we call break if we did not find
13942              a match, continue if we did find a match, or return if we
13943              are done.  */
13944
13945           c = *args;
13946           switch (c)
13947             {
13948             case '\0':
13949               if (*s == '\0')
13950                 {
13951                   offsetT value;
13952
13953                   /* Stuff the immediate value in now, if we can.  */
13954                   if (imm_expr.X_op == O_constant
13955                       && *imm_reloc > BFD_RELOC_UNUSED
13956                       && insn->pinfo != INSN_MACRO
13957                       && calculate_reloc (*offset_reloc,
13958                                           imm_expr.X_add_number, &value))
13959                     {
13960                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
13961                                     *offset_reloc, value, forced_insn_length,
13962                                     &ip->insn_opcode);
13963                       imm_expr.X_op = O_absent;
13964                       *imm_reloc = BFD_RELOC_UNUSED;
13965                       *offset_reloc = BFD_RELOC_UNUSED;
13966                     }
13967
13968                   return;
13969                 }
13970               break;
13971
13972             case ',':
13973               if (*s++ == c)
13974                 continue;
13975               s--;
13976               switch (*++args)
13977                 {
13978                 case 'v':
13979                   MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13980                   continue;
13981                 case 'w':
13982                   MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13983                   continue;
13984                 }
13985               break;
13986
13987             case '(':
13988             case ')':
13989               if (*s++ == c)
13990                 continue;
13991               break;
13992
13993             case 'v':
13994             case 'w':
13995               if (s[0] != '$')
13996                 {
13997                   if (c == 'v')
13998                     MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13999                   else
14000                     MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
14001                   ++args;
14002                   continue;
14003                 }
14004               /* Fall through.  */
14005             case 'x':
14006             case 'y':
14007             case 'z':
14008             case 'Z':
14009             case '0':
14010             case 'S':
14011             case 'R':
14012             case 'X':
14013             case 'Y':
14014               s_reset = s;
14015               if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
14016                 {
14017                   if (c == 'v' || c == 'w')
14018                     {
14019                       if (c == 'v')
14020                         MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
14021                       else
14022                         MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
14023                       ++args;
14024                       continue;
14025                     }
14026                   break;
14027                 }
14028
14029               if (*s == ' ')
14030                 ++s;
14031               if (args[1] != *s)
14032                 {
14033                   if (c == 'v' || c == 'w')
14034                     {
14035                       regno = mips16_to_32_reg_map[lastregno];
14036                       s = s_reset;
14037                       ++args;
14038                     }
14039                 }
14040
14041               switch (c)
14042                 {
14043                 case 'x':
14044                 case 'y':
14045                 case 'z':
14046                 case 'v':
14047                 case 'w':
14048                 case 'Z':
14049                   regno = mips32_to_16_reg_map[regno];
14050                   break;
14051
14052                 case '0':
14053                   if (regno != 0)
14054                     regno = ILLEGAL_REG;
14055                   break;
14056
14057                 case 'S':
14058                   if (regno != SP)
14059                     regno = ILLEGAL_REG;
14060                   break;
14061
14062                 case 'R':
14063                   if (regno != RA)
14064                     regno = ILLEGAL_REG;
14065                   break;
14066
14067                 case 'X':
14068                 case 'Y':
14069                   if (regno == AT && mips_opts.at)
14070                     {
14071                       if (mips_opts.at == ATREG)
14072                         as_warn (_("used $at without \".set noat\""));
14073                       else
14074                         as_warn (_("used $%u with \".set at=$%u\""),
14075                                  regno, mips_opts.at);
14076                     }
14077                   break;
14078
14079                 default:
14080                   abort ();
14081                 }
14082
14083               if (regno == ILLEGAL_REG)
14084                 break;
14085
14086               switch (c)
14087                 {
14088                 case 'x':
14089                 case 'v':
14090                   MIPS16_INSERT_OPERAND (RX, *ip, regno);
14091                   break;
14092                 case 'y':
14093                 case 'w':
14094                   MIPS16_INSERT_OPERAND (RY, *ip, regno);
14095                   break;
14096                 case 'z':
14097                   MIPS16_INSERT_OPERAND (RZ, *ip, regno);
14098                   break;
14099                 case 'Z':
14100                   MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
14101                 case '0':
14102                 case 'S':
14103                 case 'R':
14104                   break;
14105                 case 'X':
14106                   MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
14107                   break;
14108                 case 'Y':
14109                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
14110                   MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
14111                   break;
14112                 default:
14113                   abort ();
14114                 }
14115
14116               lastregno = regno;
14117               continue;
14118
14119             case 'P':
14120               if (strncmp (s, "$pc", 3) == 0)
14121                 {
14122                   s += 3;
14123                   continue;
14124                 }
14125               break;
14126
14127             case '5':
14128             case 'H':
14129             case 'W':
14130             case 'D':
14131             case 'j':
14132             case 'V':
14133             case 'C':
14134             case 'U':
14135             case 'k':
14136             case 'K':
14137               i = my_getSmallExpression (&imm_expr, imm_reloc, s);
14138               if (i > 0)
14139                 {
14140                   if (imm_expr.X_op != O_constant)
14141                     {
14142                       forced_insn_length = 4;
14143                       ip->insn_opcode |= MIPS16_EXTEND;
14144                     }
14145                   else
14146                     {
14147                       /* We need to relax this instruction.  */
14148                       *offset_reloc = *imm_reloc;
14149                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
14150                     }
14151                   s = expr_end;
14152                   continue;
14153                 }
14154               *imm_reloc = BFD_RELOC_UNUSED;
14155               /* Fall through.  */
14156             case '<':
14157             case '>':
14158             case '[':
14159             case ']':
14160             case '4':
14161             case '8':
14162               my_getExpression (&imm_expr, s);
14163               if (imm_expr.X_op == O_register)
14164                 {
14165                   /* What we thought was an expression turned out to
14166                      be a register.  */
14167
14168                   if (s[0] == '(' && args[1] == '(')
14169                     {
14170                       /* It looks like the expression was omitted
14171                          before a register indirection, which means
14172                          that the expression is implicitly zero.  We
14173                          still set up imm_expr, so that we handle
14174                          explicit extensions correctly.  */
14175                       imm_expr.X_op = O_constant;
14176                       imm_expr.X_add_number = 0;
14177                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
14178                       continue;
14179                     }
14180
14181                   break;
14182                 }
14183
14184               /* We need to relax this instruction.  */
14185               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
14186               s = expr_end;
14187               continue;
14188
14189             case 'p':
14190             case 'q':
14191             case 'A':
14192             case 'B':
14193             case 'E':
14194               /* We use offset_reloc rather than imm_reloc for the PC
14195                  relative operands.  This lets macros with both
14196                  immediate and address operands work correctly.  */
14197               my_getExpression (&offset_expr, s);
14198
14199               if (offset_expr.X_op == O_register)
14200                 break;
14201
14202               /* We need to relax this instruction.  */
14203               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
14204               s = expr_end;
14205               continue;
14206
14207             case '6':           /* break code */
14208               my_getExpression (&imm_expr, s);
14209               check_absolute_expr (ip, &imm_expr);
14210               if ((unsigned long) imm_expr.X_add_number > 63)
14211                 as_warn (_("Invalid value for `%s' (%lu)"),
14212                          ip->insn_mo->name,
14213                          (unsigned long) imm_expr.X_add_number);
14214               MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
14215               imm_expr.X_op = O_absent;
14216               s = expr_end;
14217               continue;
14218
14219             case 'a':           /* 26 bit address */
14220             case 'i':
14221               my_getExpression (&offset_expr, s);
14222               s = expr_end;
14223               *offset_reloc = BFD_RELOC_MIPS16_JMP;
14224               ip->insn_opcode <<= 16;
14225               continue;
14226
14227             case 'l':           /* register list for entry macro */
14228             case 'L':           /* register list for exit macro */
14229               {
14230                 int mask;
14231
14232                 if (c == 'l')
14233                   mask = 0;
14234                 else
14235                   mask = 7 << 3;
14236                 while (*s != '\0')
14237                   {
14238                     unsigned int freg, reg1, reg2;
14239
14240                     while (*s == ' ' || *s == ',')
14241                       ++s;
14242                     if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
14243                       freg = 0;
14244                     else if (reg_lookup (&s, RTYPE_FPU, &reg1))
14245                       freg = 1;
14246                     else
14247                       {
14248                         as_bad (_("can't parse register list"));
14249                         break;
14250                       }
14251                     if (*s == ' ')
14252                       ++s;
14253                     if (*s != '-')
14254                       reg2 = reg1;
14255                     else
14256                       {
14257                         ++s;
14258                         if (!reg_lookup (&s, freg ? RTYPE_FPU 
14259                                          : (RTYPE_GP | RTYPE_NUM), &reg2))
14260                           {
14261                             as_bad (_("invalid register list"));
14262                             break;
14263                           }
14264                       }
14265                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
14266                       {
14267                         mask &= ~ (7 << 3);
14268                         mask |= 5 << 3;
14269                       }
14270                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
14271                       {
14272                         mask &= ~ (7 << 3);
14273                         mask |= 6 << 3;
14274                       }
14275                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
14276                       mask |= (reg2 - 3) << 3;
14277                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
14278                       mask |= (reg2 - 15) << 1;
14279                     else if (reg1 == RA && reg2 == RA)
14280                       mask |= 1;
14281                     else
14282                       {
14283                         as_bad (_("invalid register list"));
14284                         break;
14285                       }
14286                   }
14287                 /* The mask is filled in in the opcode table for the
14288                    benefit of the disassembler.  We remove it before
14289                    applying the actual mask.  */
14290                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
14291                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
14292               }
14293             continue;
14294
14295             case 'm':           /* Register list for save insn.  */
14296             case 'M':           /* Register list for restore insn.  */
14297               {
14298                 int opcode = ip->insn_opcode;
14299                 int framesz = 0, seen_framesz = 0;
14300                 int nargs = 0, statics = 0, sregs = 0;
14301
14302                 while (*s != '\0')
14303                   {
14304                     unsigned int reg1, reg2;
14305
14306                     SKIP_SPACE_TABS (s);
14307                     while (*s == ',')
14308                       ++s;
14309                     SKIP_SPACE_TABS (s);
14310
14311                     my_getExpression (&imm_expr, s);
14312                     if (imm_expr.X_op == O_constant)
14313                       {
14314                         /* Handle the frame size.  */
14315                         if (seen_framesz)
14316                           {
14317                             as_bad (_("more than one frame size in list"));
14318                             break;
14319                           }
14320                         seen_framesz = 1;
14321                         framesz = imm_expr.X_add_number;
14322                         imm_expr.X_op = O_absent;
14323                         s = expr_end;
14324                         continue;
14325                       }
14326
14327                     if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
14328                       {
14329                         as_bad (_("can't parse register list"));
14330                         break;
14331                       }
14332
14333                     while (*s == ' ')
14334                       ++s;
14335
14336                     if (*s != '-')
14337                       reg2 = reg1;
14338                     else
14339                       {
14340                         ++s;
14341                         if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
14342                             || reg2 < reg1)
14343                           {
14344                             as_bad (_("can't parse register list"));
14345                             break;
14346                           }
14347                       }
14348
14349                     while (reg1 <= reg2)
14350                       {
14351                         if (reg1 >= 4 && reg1 <= 7)
14352                           {
14353                             if (!seen_framesz)
14354                                 /* args $a0-$a3 */
14355                                 nargs |= 1 << (reg1 - 4);
14356                             else
14357                                 /* statics $a0-$a3 */
14358                                 statics |= 1 << (reg1 - 4);
14359                           }
14360                         else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
14361                           {
14362                             /* $s0-$s8 */
14363                             sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
14364                           }
14365                         else if (reg1 == 31)
14366                           {
14367                             /* Add $ra to insn.  */
14368                             opcode |= 0x40;
14369                           }
14370                         else
14371                           {
14372                             as_bad (_("unexpected register in list"));
14373                             break;
14374                           }
14375                         if (++reg1 == 24)
14376                           reg1 = 30;
14377                       }
14378                   }
14379
14380                 /* Encode args/statics combination.  */
14381                 if (nargs & statics)
14382                   as_bad (_("arg/static registers overlap"));
14383                 else if (nargs == 0xf)
14384                   /* All $a0-$a3 are args.  */
14385                   opcode |= MIPS16_ALL_ARGS << 16;
14386                 else if (statics == 0xf)
14387                   /* All $a0-$a3 are statics.  */
14388                   opcode |= MIPS16_ALL_STATICS << 16;
14389                 else 
14390                   {
14391                     int narg = 0, nstat = 0;
14392
14393                     /* Count arg registers.  */
14394                     while (nargs & 0x1)
14395                       {
14396                         nargs >>= 1;
14397                         narg++;
14398                       }
14399                     if (nargs != 0)
14400                       as_bad (_("invalid arg register list"));
14401
14402                     /* Count static registers.  */
14403                     while (statics & 0x8)
14404                       {
14405                         statics = (statics << 1) & 0xf;
14406                         nstat++;
14407                       }
14408                     if (statics != 0) 
14409                       as_bad (_("invalid static register list"));
14410
14411                     /* Encode args/statics.  */
14412                     opcode |= ((narg << 2) | nstat) << 16;
14413                   }
14414
14415                 /* Encode $s0/$s1.  */
14416                 if (sregs & (1 << 0))           /* $s0 */
14417                   opcode |= 0x20;
14418                 if (sregs & (1 << 1))           /* $s1 */
14419                   opcode |= 0x10;
14420                 sregs >>= 2;
14421
14422                 if (sregs != 0)
14423                   {
14424                     /* Count regs $s2-$s8.  */
14425                     int nsreg = 0;
14426                     while (sregs & 1)
14427                       {
14428                         sregs >>= 1;
14429                         nsreg++;
14430                       }
14431                     if (sregs != 0)
14432                       as_bad (_("invalid static register list"));
14433                     /* Encode $s2-$s8. */
14434                     opcode |= nsreg << 24;
14435                   }
14436
14437                 /* Encode frame size.  */
14438                 if (!seen_framesz)
14439                   as_bad (_("missing frame size"));
14440                 else if ((framesz & 7) != 0 || framesz < 0
14441                          || framesz > 0xff * 8)
14442                   as_bad (_("invalid frame size"));
14443                 else if (framesz != 128 || (opcode >> 16) != 0)
14444                   {
14445                     framesz /= 8;
14446                     opcode |= (((framesz & 0xf0) << 16)
14447                              | (framesz & 0x0f));
14448                   }
14449
14450                 /* Finally build the instruction.  */
14451                 if ((opcode >> 16) != 0 || framesz == 0)
14452                   opcode |= MIPS16_EXTEND;
14453                 ip->insn_opcode = opcode;
14454               }
14455             continue;
14456
14457             case 'e':           /* extend code */
14458               my_getExpression (&imm_expr, s);
14459               check_absolute_expr (ip, &imm_expr);
14460               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
14461                 {
14462                   as_warn (_("Invalid value for `%s' (%lu)"),
14463                            ip->insn_mo->name,
14464                            (unsigned long) imm_expr.X_add_number);
14465                   imm_expr.X_add_number &= 0x7ff;
14466                 }
14467               ip->insn_opcode |= imm_expr.X_add_number;
14468               imm_expr.X_op = O_absent;
14469               s = expr_end;
14470               continue;
14471
14472             default:
14473               abort ();
14474             }
14475           break;
14476         }
14477
14478       /* Args don't match.  */
14479       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14480           strcmp (insn->name, insn[1].name) == 0)
14481         {
14482           ++insn;
14483           s = argsstart;
14484           continue;
14485         }
14486
14487       insn_error = _("illegal operands");
14488
14489       return;
14490     }
14491 }
14492
14493 /* This structure holds information we know about a mips16 immediate
14494    argument type.  */
14495
14496 struct mips16_immed_operand
14497 {
14498   /* The type code used in the argument string in the opcode table.  */
14499   int type;
14500   /* The number of bits in the short form of the opcode.  */
14501   int nbits;
14502   /* The number of bits in the extended form of the opcode.  */
14503   int extbits;
14504   /* The amount by which the short form is shifted when it is used;
14505      for example, the sw instruction has a shift count of 2.  */
14506   int shift;
14507   /* The amount by which the short form is shifted when it is stored
14508      into the instruction code.  */
14509   int op_shift;
14510   /* Non-zero if the short form is unsigned.  */
14511   int unsp;
14512   /* Non-zero if the extended form is unsigned.  */
14513   int extu;
14514   /* Non-zero if the value is PC relative.  */
14515   int pcrel;
14516 };
14517
14518 /* The mips16 immediate operand types.  */
14519
14520 static const struct mips16_immed_operand mips16_immed_operands[] =
14521 {
14522   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
14523   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
14524   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
14525   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
14526   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14527   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14528   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14529   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14530   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14531   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14532   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14533   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14534   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14535   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14536   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14537   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14538   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14539   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14540   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14541   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14542   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14543 };
14544
14545 #define MIPS16_NUM_IMMED \
14546   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14547
14548 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14549    NBITS is the number of significant bits in VAL.  */
14550
14551 static unsigned long
14552 mips16_immed_extend (offsetT val, unsigned int nbits)
14553 {
14554   int extval;
14555   if (nbits == 16)
14556     {
14557       extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14558       val &= 0x1f;
14559     }
14560   else if (nbits == 15)
14561     {
14562       extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14563       val &= 0xf;
14564     }
14565   else
14566     {
14567       extval = ((val & 0x1f) << 6) | (val & 0x20);
14568       val = 0;
14569     }
14570   return (extval << 16) | val;
14571 }
14572
14573 /* Install immediate value VAL into MIPS16 instruction *INSN,
14574    extending it if necessary.  The instruction in *INSN may
14575    already be extended.
14576
14577    RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14578    if none.  In the former case, VAL is a 16-bit number with no
14579    defined signedness.
14580
14581    TYPE is the type of the immediate field.  USER_INSN_LENGTH
14582    is the length that the user requested, or 0 if none.  */
14583
14584 static void
14585 mips16_immed (char *file, unsigned int line, int type,
14586               bfd_reloc_code_real_type reloc, offsetT val,
14587               unsigned int user_insn_length, unsigned long *insn)
14588 {
14589   const struct mips16_immed_operand *op;
14590   int mintiny, maxtiny;
14591
14592   op = mips16_immed_operands;
14593   while (op->type != type)
14594     {
14595       ++op;
14596       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
14597     }
14598
14599   if (op->unsp)
14600     {
14601       if (type == '<' || type == '>' || type == '[' || type == ']')
14602         {
14603           mintiny = 1;
14604           maxtiny = 1 << op->nbits;
14605         }
14606       else
14607         {
14608           mintiny = 0;
14609           maxtiny = (1 << op->nbits) - 1;
14610         }
14611       if (reloc != BFD_RELOC_UNUSED)
14612         val &= 0xffff;
14613     }
14614   else
14615     {
14616       mintiny = - (1 << (op->nbits - 1));
14617       maxtiny = (1 << (op->nbits - 1)) - 1;
14618       if (reloc != BFD_RELOC_UNUSED)
14619         val = SEXT_16BIT (val);
14620     }
14621
14622   /* Branch offsets have an implicit 0 in the lowest bit.  */
14623   if (type == 'p' || type == 'q')
14624     val /= 2;
14625
14626   if ((val & ((1 << op->shift) - 1)) != 0
14627       || val < (mintiny << op->shift)
14628       || val > (maxtiny << op->shift))
14629     {
14630       /* We need an extended instruction.  */
14631       if (user_insn_length == 2)
14632         as_bad_where (file, line, _("invalid unextended operand value"));
14633       else
14634         *insn |= MIPS16_EXTEND;
14635     }
14636   else if (user_insn_length == 4)
14637     {
14638       /* The operand doesn't force an unextended instruction to be extended.
14639          Warn if the user wanted an extended instruction anyway.  */
14640       *insn |= MIPS16_EXTEND;
14641       as_warn_where (file, line,
14642                      _("extended operand requested but not required"));
14643     }
14644
14645   if (mips16_opcode_length (*insn) == 2)
14646     {
14647       int insnval;
14648
14649       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14650       insnval <<= op->op_shift;
14651       *insn |= insnval;
14652     }
14653   else
14654     {
14655       long minext, maxext;
14656
14657       if (reloc == BFD_RELOC_UNUSED)
14658         {
14659           if (op->extu)
14660             {
14661               minext = 0;
14662               maxext = (1 << op->extbits) - 1;
14663             }
14664           else
14665             {
14666               minext = - (1 << (op->extbits - 1));
14667               maxext = (1 << (op->extbits - 1)) - 1;
14668             }
14669           if (val < minext || val > maxext)
14670             as_bad_where (file, line,
14671                           _("operand value out of range for instruction"));
14672         }
14673
14674       *insn |= mips16_immed_extend (val, op->extbits);
14675     }
14676 }
14677 \f
14678 struct percent_op_match
14679 {
14680   const char *str;
14681   bfd_reloc_code_real_type reloc;
14682 };
14683
14684 static const struct percent_op_match mips_percent_op[] =
14685 {
14686   {"%lo", BFD_RELOC_LO16},
14687   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14688   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14689   {"%call16", BFD_RELOC_MIPS_CALL16},
14690   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14691   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14692   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14693   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14694   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14695   {"%got", BFD_RELOC_MIPS_GOT16},
14696   {"%gp_rel", BFD_RELOC_GPREL16},
14697   {"%half", BFD_RELOC_16},
14698   {"%highest", BFD_RELOC_MIPS_HIGHEST},
14699   {"%higher", BFD_RELOC_MIPS_HIGHER},
14700   {"%neg", BFD_RELOC_MIPS_SUB},
14701   {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14702   {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14703   {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14704   {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14705   {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14706   {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14707   {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14708   {"%hi", BFD_RELOC_HI16_S}
14709 };
14710
14711 static const struct percent_op_match mips16_percent_op[] =
14712 {
14713   {"%lo", BFD_RELOC_MIPS16_LO16},
14714   {"%gprel", BFD_RELOC_MIPS16_GPREL},
14715   {"%got", BFD_RELOC_MIPS16_GOT16},
14716   {"%call16", BFD_RELOC_MIPS16_CALL16},
14717   {"%hi", BFD_RELOC_MIPS16_HI16_S},
14718   {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14719   {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14720   {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14721   {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14722   {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14723   {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14724   {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14725 };
14726
14727
14728 /* Return true if *STR points to a relocation operator.  When returning true,
14729    move *STR over the operator and store its relocation code in *RELOC.
14730    Leave both *STR and *RELOC alone when returning false.  */
14731
14732 static bfd_boolean
14733 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14734 {
14735   const struct percent_op_match *percent_op;
14736   size_t limit, i;
14737
14738   if (mips_opts.mips16)
14739     {
14740       percent_op = mips16_percent_op;
14741       limit = ARRAY_SIZE (mips16_percent_op);
14742     }
14743   else
14744     {
14745       percent_op = mips_percent_op;
14746       limit = ARRAY_SIZE (mips_percent_op);
14747     }
14748
14749   for (i = 0; i < limit; i++)
14750     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14751       {
14752         int len = strlen (percent_op[i].str);
14753
14754         if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14755           continue;
14756
14757         *str += strlen (percent_op[i].str);
14758         *reloc = percent_op[i].reloc;
14759
14760         /* Check whether the output BFD supports this relocation.
14761            If not, issue an error and fall back on something safe.  */
14762         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14763           {
14764             as_bad (_("relocation %s isn't supported by the current ABI"),
14765                     percent_op[i].str);
14766             *reloc = BFD_RELOC_UNUSED;
14767           }
14768         return TRUE;
14769       }
14770   return FALSE;
14771 }
14772
14773
14774 /* Parse string STR as a 16-bit relocatable operand.  Store the
14775    expression in *EP and the relocations in the array starting
14776    at RELOC.  Return the number of relocation operators used.
14777
14778    On exit, EXPR_END points to the first character after the expression.  */
14779
14780 static size_t
14781 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14782                        char *str)
14783 {
14784   bfd_reloc_code_real_type reversed_reloc[3];
14785   size_t reloc_index, i;
14786   int crux_depth, str_depth;
14787   char *crux;
14788
14789   /* Search for the start of the main expression, recoding relocations
14790      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
14791      of the main expression and with CRUX_DEPTH containing the number
14792      of open brackets at that point.  */
14793   reloc_index = -1;
14794   str_depth = 0;
14795   do
14796     {
14797       reloc_index++;
14798       crux = str;
14799       crux_depth = str_depth;
14800
14801       /* Skip over whitespace and brackets, keeping count of the number
14802          of brackets.  */
14803       while (*str == ' ' || *str == '\t' || *str == '(')
14804         if (*str++ == '(')
14805           str_depth++;
14806     }
14807   while (*str == '%'
14808          && reloc_index < (HAVE_NEWABI ? 3 : 1)
14809          && parse_relocation (&str, &reversed_reloc[reloc_index]));
14810
14811   my_getExpression (ep, crux);
14812   str = expr_end;
14813
14814   /* Match every open bracket.  */
14815   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14816     if (*str++ == ')')
14817       crux_depth--;
14818
14819   if (crux_depth > 0)
14820     as_bad (_("unclosed '('"));
14821
14822   expr_end = str;
14823
14824   if (reloc_index != 0)
14825     {
14826       prev_reloc_op_frag = frag_now;
14827       for (i = 0; i < reloc_index; i++)
14828         reloc[i] = reversed_reloc[reloc_index - 1 - i];
14829     }
14830
14831   return reloc_index;
14832 }
14833
14834 static void
14835 my_getExpression (expressionS *ep, char *str)
14836 {
14837   char *save_in;
14838
14839   save_in = input_line_pointer;
14840   input_line_pointer = str;
14841   expression (ep);
14842   expr_end = input_line_pointer;
14843   input_line_pointer = save_in;
14844 }
14845
14846 char *
14847 md_atof (int type, char *litP, int *sizeP)
14848 {
14849   return ieee_md_atof (type, litP, sizeP, target_big_endian);
14850 }
14851
14852 void
14853 md_number_to_chars (char *buf, valueT val, int n)
14854 {
14855   if (target_big_endian)
14856     number_to_chars_bigendian (buf, val, n);
14857   else
14858     number_to_chars_littleendian (buf, val, n);
14859 }
14860 \f
14861 static int support_64bit_objects(void)
14862 {
14863   const char **list, **l;
14864   int yes;
14865
14866   list = bfd_target_list ();
14867   for (l = list; *l != NULL; l++)
14868     if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14869         || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14870       break;
14871   yes = (*l != NULL);
14872   free (list);
14873   return yes;
14874 }
14875
14876 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14877    NEW_VALUE.  Warn if another value was already specified.  Note:
14878    we have to defer parsing the -march and -mtune arguments in order
14879    to handle 'from-abi' correctly, since the ABI might be specified
14880    in a later argument.  */
14881
14882 static void
14883 mips_set_option_string (const char **string_ptr, const char *new_value)
14884 {
14885   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14886     as_warn (_("A different %s was already specified, is now %s"),
14887              string_ptr == &mips_arch_string ? "-march" : "-mtune",
14888              new_value);
14889
14890   *string_ptr = new_value;
14891 }
14892
14893 int
14894 md_parse_option (int c, char *arg)
14895 {
14896   unsigned int i;
14897
14898   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14899     if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14900       {
14901         file_ase_explicit |= mips_set_ase (&mips_ases[i],
14902                                            c == mips_ases[i].option_on);
14903         return 1;
14904       }
14905
14906   switch (c)
14907     {
14908     case OPTION_CONSTRUCT_FLOATS:
14909       mips_disable_float_construction = 0;
14910       break;
14911
14912     case OPTION_NO_CONSTRUCT_FLOATS:
14913       mips_disable_float_construction = 1;
14914       break;
14915
14916     case OPTION_TRAP:
14917       mips_trap = 1;
14918       break;
14919
14920     case OPTION_BREAK:
14921       mips_trap = 0;
14922       break;
14923
14924     case OPTION_EB:
14925       target_big_endian = 1;
14926       break;
14927
14928     case OPTION_EL:
14929       target_big_endian = 0;
14930       break;
14931
14932     case 'O':
14933       if (arg == NULL)
14934         mips_optimize = 1;
14935       else if (arg[0] == '0')
14936         mips_optimize = 0;
14937       else if (arg[0] == '1')
14938         mips_optimize = 1;
14939       else
14940         mips_optimize = 2;
14941       break;
14942
14943     case 'g':
14944       if (arg == NULL)
14945         mips_debug = 2;
14946       else
14947         mips_debug = atoi (arg);
14948       break;
14949
14950     case OPTION_MIPS1:
14951       file_mips_isa = ISA_MIPS1;
14952       break;
14953
14954     case OPTION_MIPS2:
14955       file_mips_isa = ISA_MIPS2;
14956       break;
14957
14958     case OPTION_MIPS3:
14959       file_mips_isa = ISA_MIPS3;
14960       break;
14961
14962     case OPTION_MIPS4:
14963       file_mips_isa = ISA_MIPS4;
14964       break;
14965
14966     case OPTION_MIPS5:
14967       file_mips_isa = ISA_MIPS5;
14968       break;
14969
14970     case OPTION_MIPS32:
14971       file_mips_isa = ISA_MIPS32;
14972       break;
14973
14974     case OPTION_MIPS32R2:
14975       file_mips_isa = ISA_MIPS32R2;
14976       break;
14977
14978     case OPTION_MIPS64R2:
14979       file_mips_isa = ISA_MIPS64R2;
14980       break;
14981
14982     case OPTION_MIPS64:
14983       file_mips_isa = ISA_MIPS64;
14984       break;
14985
14986     case OPTION_MTUNE:
14987       mips_set_option_string (&mips_tune_string, arg);
14988       break;
14989
14990     case OPTION_MARCH:
14991       mips_set_option_string (&mips_arch_string, arg);
14992       break;
14993
14994     case OPTION_M4650:
14995       mips_set_option_string (&mips_arch_string, "4650");
14996       mips_set_option_string (&mips_tune_string, "4650");
14997       break;
14998
14999     case OPTION_NO_M4650:
15000       break;
15001
15002     case OPTION_M4010:
15003       mips_set_option_string (&mips_arch_string, "4010");
15004       mips_set_option_string (&mips_tune_string, "4010");
15005       break;
15006
15007     case OPTION_NO_M4010:
15008       break;
15009
15010     case OPTION_M4100:
15011       mips_set_option_string (&mips_arch_string, "4100");
15012       mips_set_option_string (&mips_tune_string, "4100");
15013       break;
15014
15015     case OPTION_NO_M4100:
15016       break;
15017
15018     case OPTION_M3900:
15019       mips_set_option_string (&mips_arch_string, "3900");
15020       mips_set_option_string (&mips_tune_string, "3900");
15021       break;
15022
15023     case OPTION_NO_M3900:
15024       break;
15025
15026     case OPTION_MICROMIPS:
15027       if (mips_opts.mips16 == 1)
15028         {
15029           as_bad (_("-mmicromips cannot be used with -mips16"));
15030           return 0;
15031         }
15032       mips_opts.micromips = 1;
15033       mips_no_prev_insn ();
15034       break;
15035
15036     case OPTION_NO_MICROMIPS:
15037       mips_opts.micromips = 0;
15038       mips_no_prev_insn ();
15039       break;
15040
15041     case OPTION_MIPS16:
15042       if (mips_opts.micromips == 1)
15043         {
15044           as_bad (_("-mips16 cannot be used with -micromips"));
15045           return 0;
15046         }
15047       mips_opts.mips16 = 1;
15048       mips_no_prev_insn ();
15049       break;
15050
15051     case OPTION_NO_MIPS16:
15052       mips_opts.mips16 = 0;
15053       mips_no_prev_insn ();
15054       break;
15055
15056     case OPTION_FIX_24K:
15057       mips_fix_24k = 1;
15058       break;
15059
15060     case OPTION_NO_FIX_24K:
15061       mips_fix_24k = 0;
15062       break;
15063
15064     case OPTION_FIX_LOONGSON2F_JUMP:
15065       mips_fix_loongson2f_jump = TRUE;
15066       break;
15067
15068     case OPTION_NO_FIX_LOONGSON2F_JUMP:
15069       mips_fix_loongson2f_jump = FALSE;
15070       break;
15071
15072     case OPTION_FIX_LOONGSON2F_NOP:
15073       mips_fix_loongson2f_nop = TRUE;
15074       break;
15075
15076     case OPTION_NO_FIX_LOONGSON2F_NOP:
15077       mips_fix_loongson2f_nop = FALSE;
15078       break;
15079
15080     case OPTION_FIX_VR4120:
15081       mips_fix_vr4120 = 1;
15082       break;
15083
15084     case OPTION_NO_FIX_VR4120:
15085       mips_fix_vr4120 = 0;
15086       break;
15087
15088     case OPTION_FIX_VR4130:
15089       mips_fix_vr4130 = 1;
15090       break;
15091
15092     case OPTION_NO_FIX_VR4130:
15093       mips_fix_vr4130 = 0;
15094       break;
15095
15096     case OPTION_FIX_CN63XXP1:
15097       mips_fix_cn63xxp1 = TRUE;
15098       break;
15099
15100     case OPTION_NO_FIX_CN63XXP1:
15101       mips_fix_cn63xxp1 = FALSE;
15102       break;
15103
15104     case OPTION_RELAX_BRANCH:
15105       mips_relax_branch = 1;
15106       break;
15107
15108     case OPTION_NO_RELAX_BRANCH:
15109       mips_relax_branch = 0;
15110       break;
15111
15112     case OPTION_INSN32:
15113       mips_opts.insn32 = TRUE;
15114       break;
15115
15116     case OPTION_NO_INSN32:
15117       mips_opts.insn32 = FALSE;
15118       break;
15119
15120     case OPTION_MSHARED:
15121       mips_in_shared = TRUE;
15122       break;
15123
15124     case OPTION_MNO_SHARED:
15125       mips_in_shared = FALSE;
15126       break;
15127
15128     case OPTION_MSYM32:
15129       mips_opts.sym32 = TRUE;
15130       break;
15131
15132     case OPTION_MNO_SYM32:
15133       mips_opts.sym32 = FALSE;
15134       break;
15135
15136       /* When generating ELF code, we permit -KPIC and -call_shared to
15137          select SVR4_PIC, and -non_shared to select no PIC.  This is
15138          intended to be compatible with Irix 5.  */
15139     case OPTION_CALL_SHARED:
15140       mips_pic = SVR4_PIC;
15141       mips_abicalls = TRUE;
15142       break;
15143
15144     case OPTION_CALL_NONPIC:
15145       mips_pic = NO_PIC;
15146       mips_abicalls = TRUE;
15147       break;
15148
15149     case OPTION_NON_SHARED:
15150       mips_pic = NO_PIC;
15151       mips_abicalls = FALSE;
15152       break;
15153
15154       /* The -xgot option tells the assembler to use 32 bit offsets
15155          when accessing the got in SVR4_PIC mode.  It is for Irix
15156          compatibility.  */
15157     case OPTION_XGOT:
15158       mips_big_got = 1;
15159       break;
15160
15161     case 'G':
15162       g_switch_value = atoi (arg);
15163       g_switch_seen = 1;
15164       break;
15165
15166       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15167          and -mabi=64.  */
15168     case OPTION_32:
15169       mips_abi = O32_ABI;
15170       break;
15171
15172     case OPTION_N32:
15173       mips_abi = N32_ABI;
15174       break;
15175
15176     case OPTION_64:
15177       mips_abi = N64_ABI;
15178       if (!support_64bit_objects())
15179         as_fatal (_("No compiled in support for 64 bit object file format"));
15180       break;
15181
15182     case OPTION_GP32:
15183       file_mips_gp32 = 1;
15184       break;
15185
15186     case OPTION_GP64:
15187       file_mips_gp32 = 0;
15188       break;
15189
15190     case OPTION_FP32:
15191       file_mips_fp32 = 1;
15192       break;
15193
15194     case OPTION_FP64:
15195       file_mips_fp32 = 0;
15196       break;
15197
15198     case OPTION_SINGLE_FLOAT:
15199       file_mips_single_float = 1;
15200       break;
15201
15202     case OPTION_DOUBLE_FLOAT:
15203       file_mips_single_float = 0;
15204       break;
15205
15206     case OPTION_SOFT_FLOAT:
15207       file_mips_soft_float = 1;
15208       break;
15209
15210     case OPTION_HARD_FLOAT:
15211       file_mips_soft_float = 0;
15212       break;
15213
15214     case OPTION_MABI:
15215       if (strcmp (arg, "32") == 0)
15216         mips_abi = O32_ABI;
15217       else if (strcmp (arg, "o64") == 0)
15218         mips_abi = O64_ABI;
15219       else if (strcmp (arg, "n32") == 0)
15220         mips_abi = N32_ABI;
15221       else if (strcmp (arg, "64") == 0)
15222         {
15223           mips_abi = N64_ABI;
15224           if (! support_64bit_objects())
15225             as_fatal (_("No compiled in support for 64 bit object file "
15226                         "format"));
15227         }
15228       else if (strcmp (arg, "eabi") == 0)
15229         mips_abi = EABI_ABI;
15230       else
15231         {
15232           as_fatal (_("invalid abi -mabi=%s"), arg);
15233           return 0;
15234         }
15235       break;
15236
15237     case OPTION_M7000_HILO_FIX:
15238       mips_7000_hilo_fix = TRUE;
15239       break;
15240
15241     case OPTION_MNO_7000_HILO_FIX:
15242       mips_7000_hilo_fix = FALSE;
15243       break;
15244
15245     case OPTION_MDEBUG:
15246       mips_flag_mdebug = TRUE;
15247       break;
15248
15249     case OPTION_NO_MDEBUG:
15250       mips_flag_mdebug = FALSE;
15251       break;
15252
15253     case OPTION_PDR:
15254       mips_flag_pdr = TRUE;
15255       break;
15256
15257     case OPTION_NO_PDR:
15258       mips_flag_pdr = FALSE;
15259       break;
15260
15261     case OPTION_MVXWORKS_PIC:
15262       mips_pic = VXWORKS_PIC;
15263       break;
15264
15265     default:
15266       return 0;
15267     }
15268
15269     mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15270
15271   return 1;
15272 }
15273 \f
15274 /* Set up globals to generate code for the ISA or processor
15275    described by INFO.  */
15276
15277 static void
15278 mips_set_architecture (const struct mips_cpu_info *info)
15279 {
15280   if (info != 0)
15281     {
15282       file_mips_arch = info->cpu;
15283       mips_opts.arch = info->cpu;
15284       mips_opts.isa = info->isa;
15285     }
15286 }
15287
15288
15289 /* Likewise for tuning.  */
15290
15291 static void
15292 mips_set_tune (const struct mips_cpu_info *info)
15293 {
15294   if (info != 0)
15295     mips_tune = info->cpu;
15296 }
15297
15298
15299 void
15300 mips_after_parse_args (void)
15301 {
15302   const struct mips_cpu_info *arch_info = 0;
15303   const struct mips_cpu_info *tune_info = 0;
15304
15305   /* GP relative stuff not working for PE */
15306   if (strncmp (TARGET_OS, "pe", 2) == 0)
15307     {
15308       if (g_switch_seen && g_switch_value != 0)
15309         as_bad (_("-G not supported in this configuration."));
15310       g_switch_value = 0;
15311     }
15312
15313   if (mips_abi == NO_ABI)
15314     mips_abi = MIPS_DEFAULT_ABI;
15315
15316   /* The following code determines the architecture and register size.
15317      Similar code was added to GCC 3.3 (see override_options() in
15318      config/mips/mips.c).  The GAS and GCC code should be kept in sync
15319      as much as possible.  */
15320
15321   if (mips_arch_string != 0)
15322     arch_info = mips_parse_cpu ("-march", mips_arch_string);
15323
15324   if (file_mips_isa != ISA_UNKNOWN)
15325     {
15326       /* Handle -mipsN.  At this point, file_mips_isa contains the
15327          ISA level specified by -mipsN, while arch_info->isa contains
15328          the -march selection (if any).  */
15329       if (arch_info != 0)
15330         {
15331           /* -march takes precedence over -mipsN, since it is more descriptive.
15332              There's no harm in specifying both as long as the ISA levels
15333              are the same.  */
15334           if (file_mips_isa != arch_info->isa)
15335             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15336                     mips_cpu_info_from_isa (file_mips_isa)->name,
15337                     mips_cpu_info_from_isa (arch_info->isa)->name);
15338         }
15339       else
15340         arch_info = mips_cpu_info_from_isa (file_mips_isa);
15341     }
15342
15343   if (arch_info == 0)
15344     {
15345       arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15346       gas_assert (arch_info);
15347     }
15348
15349   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15350     as_bad (_("-march=%s is not compatible with the selected ABI"),
15351             arch_info->name);
15352
15353   mips_set_architecture (arch_info);
15354
15355   /* Optimize for file_mips_arch, unless -mtune selects a different processor.  */
15356   if (mips_tune_string != 0)
15357     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15358
15359   if (tune_info == 0)
15360     mips_set_tune (arch_info);
15361   else
15362     mips_set_tune (tune_info);
15363
15364   if (file_mips_gp32 >= 0)
15365     {
15366       /* The user specified the size of the integer registers.  Make sure
15367          it agrees with the ABI and ISA.  */
15368       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15369         as_bad (_("-mgp64 used with a 32-bit processor"));
15370       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15371         as_bad (_("-mgp32 used with a 64-bit ABI"));
15372       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15373         as_bad (_("-mgp64 used with a 32-bit ABI"));
15374     }
15375   else
15376     {
15377       /* Infer the integer register size from the ABI and processor.
15378          Restrict ourselves to 32-bit registers if that's all the
15379          processor has, or if the ABI cannot handle 64-bit registers.  */
15380       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15381                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
15382     }
15383
15384   switch (file_mips_fp32)
15385     {
15386     default:
15387     case -1:
15388       /* No user specified float register size.
15389          ??? GAS treats single-float processors as though they had 64-bit
15390          float registers (although it complains when double-precision
15391          instructions are used).  As things stand, saying they have 32-bit
15392          registers would lead to spurious "register must be even" messages.
15393          So here we assume float registers are never smaller than the
15394          integer ones.  */
15395       if (file_mips_gp32 == 0)
15396         /* 64-bit integer registers implies 64-bit float registers.  */
15397         file_mips_fp32 = 0;
15398       else if ((mips_opts.ase & FP64_ASES)
15399                && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15400         /* -mips3d and -mdmx imply 64-bit float registers, if possible.  */
15401         file_mips_fp32 = 0;
15402       else
15403         /* 32-bit float registers.  */
15404         file_mips_fp32 = 1;
15405       break;
15406
15407     /* The user specified the size of the float registers.  Check if it
15408        agrees with the ABI and ISA.  */
15409     case 0:
15410       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15411         as_bad (_("-mfp64 used with a 32-bit fpu"));
15412       else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15413                && !ISA_HAS_MXHC1 (mips_opts.isa))
15414         as_warn (_("-mfp64 used with a 32-bit ABI"));
15415       break;
15416     case 1:
15417       if (ABI_NEEDS_64BIT_REGS (mips_abi))
15418         as_warn (_("-mfp32 used with a 64-bit ABI"));
15419       break;
15420     }
15421
15422   /* End of GCC-shared inference code.  */
15423
15424   /* This flag is set when we have a 64-bit capable CPU but use only
15425      32-bit wide registers.  Note that EABI does not use it.  */
15426   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15427       && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15428           || mips_abi == O32_ABI))
15429     mips_32bitmode = 1;
15430
15431   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15432     as_bad (_("trap exception not supported at ISA 1"));
15433
15434   /* If the selected architecture includes support for ASEs, enable
15435      generation of code for them.  */
15436   if (mips_opts.mips16 == -1)
15437     mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
15438   if (mips_opts.micromips == -1)
15439     mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
15440
15441   /* MIPS3D and MDMX require 64-bit FPRs, so -mfp32 should stop those
15442      ASEs from being selected implicitly.  */
15443   if (file_mips_fp32 == 1)
15444     file_ase_explicit |= ASE_MIPS3D | ASE_MDMX;
15445
15446   /* If the user didn't explicitly select or deselect a particular ASE,
15447      use the default setting for the CPU.  */
15448   mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
15449
15450   file_mips_isa = mips_opts.isa;
15451   file_ase = mips_opts.ase;
15452   mips_opts.gp32 = file_mips_gp32;
15453   mips_opts.fp32 = file_mips_fp32;
15454   mips_opts.soft_float = file_mips_soft_float;
15455   mips_opts.single_float = file_mips_single_float;
15456
15457   mips_check_isa_supports_ases ();
15458
15459   if (mips_flag_mdebug < 0)
15460     mips_flag_mdebug = 0;
15461 }
15462 \f
15463 void
15464 mips_init_after_args (void)
15465 {
15466   /* initialize opcodes */
15467   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15468   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15469 }
15470
15471 long
15472 md_pcrel_from (fixS *fixP)
15473 {
15474   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15475   switch (fixP->fx_r_type)
15476     {
15477     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15478     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15479       /* Return the address of the delay slot.  */
15480       return addr + 2;
15481
15482     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15483     case BFD_RELOC_MICROMIPS_JMP:
15484     case BFD_RELOC_16_PCREL_S2:
15485     case BFD_RELOC_MIPS_JMP:
15486       /* Return the address of the delay slot.  */
15487       return addr + 4;
15488
15489     case BFD_RELOC_32_PCREL:
15490       return addr;
15491
15492     default:
15493       /* We have no relocation type for PC relative MIPS16 instructions.  */
15494       if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15495         as_bad_where (fixP->fx_file, fixP->fx_line,
15496                       _("PC relative MIPS16 instruction references a different section"));
15497       return addr;
15498     }
15499 }
15500
15501 /* This is called before the symbol table is processed.  In order to
15502    work with gcc when using mips-tfile, we must keep all local labels.
15503    However, in other cases, we want to discard them.  If we were
15504    called with -g, but we didn't see any debugging information, it may
15505    mean that gcc is smuggling debugging information through to
15506    mips-tfile, in which case we must generate all local labels.  */
15507
15508 void
15509 mips_frob_file_before_adjust (void)
15510 {
15511 #ifndef NO_ECOFF_DEBUGGING
15512   if (ECOFF_DEBUGGING
15513       && mips_debug != 0
15514       && ! ecoff_debugging_seen)
15515     flag_keep_locals = 1;
15516 #endif
15517 }
15518
15519 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15520    the corresponding LO16 reloc.  This is called before md_apply_fix and
15521    tc_gen_reloc.  Unmatched relocs can only be generated by use of explicit
15522    relocation operators.
15523
15524    For our purposes, a %lo() expression matches a %got() or %hi()
15525    expression if:
15526
15527       (a) it refers to the same symbol; and
15528       (b) the offset applied in the %lo() expression is no lower than
15529           the offset applied in the %got() or %hi().
15530
15531    (b) allows us to cope with code like:
15532
15533         lui     $4,%hi(foo)
15534         lh      $4,%lo(foo+2)($4)
15535
15536    ...which is legal on RELA targets, and has a well-defined behaviour
15537    if the user knows that adding 2 to "foo" will not induce a carry to
15538    the high 16 bits.
15539
15540    When several %lo()s match a particular %got() or %hi(), we use the
15541    following rules to distinguish them:
15542
15543      (1) %lo()s with smaller offsets are a better match than %lo()s with
15544          higher offsets.
15545
15546      (2) %lo()s with no matching %got() or %hi() are better than those
15547          that already have a matching %got() or %hi().
15548
15549      (3) later %lo()s are better than earlier %lo()s.
15550
15551    These rules are applied in order.
15552
15553    (1) means, among other things, that %lo()s with identical offsets are
15554    chosen if they exist.
15555
15556    (2) means that we won't associate several high-part relocations with
15557    the same low-part relocation unless there's no alternative.  Having
15558    several high parts for the same low part is a GNU extension; this rule
15559    allows careful users to avoid it.
15560
15561    (3) is purely cosmetic.  mips_hi_fixup_list is is in reverse order,
15562    with the last high-part relocation being at the front of the list.
15563    It therefore makes sense to choose the last matching low-part
15564    relocation, all other things being equal.  It's also easier
15565    to code that way.  */
15566
15567 void
15568 mips_frob_file (void)
15569 {
15570   struct mips_hi_fixup *l;
15571   bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15572
15573   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15574     {
15575       segment_info_type *seginfo;
15576       bfd_boolean matched_lo_p;
15577       fixS **hi_pos, **lo_pos, **pos;
15578
15579       gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15580
15581       /* If a GOT16 relocation turns out to be against a global symbol,
15582          there isn't supposed to be a matching LO.  Ignore %gots against
15583          constants; we'll report an error for those later.  */
15584       if (got16_reloc_p (l->fixp->fx_r_type)
15585           && !(l->fixp->fx_addsy
15586                && pic_need_relax (l->fixp->fx_addsy, l->seg)))
15587         continue;
15588
15589       /* Check quickly whether the next fixup happens to be a matching %lo.  */
15590       if (fixup_has_matching_lo_p (l->fixp))
15591         continue;
15592
15593       seginfo = seg_info (l->seg);
15594
15595       /* Set HI_POS to the position of this relocation in the chain.
15596          Set LO_POS to the position of the chosen low-part relocation.
15597          MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15598          relocation that matches an immediately-preceding high-part
15599          relocation.  */
15600       hi_pos = NULL;
15601       lo_pos = NULL;
15602       matched_lo_p = FALSE;
15603       looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15604
15605       for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15606         {
15607           if (*pos == l->fixp)
15608             hi_pos = pos;
15609
15610           if ((*pos)->fx_r_type == looking_for_rtype
15611               && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15612               && (*pos)->fx_offset >= l->fixp->fx_offset
15613               && (lo_pos == NULL
15614                   || (*pos)->fx_offset < (*lo_pos)->fx_offset
15615                   || (!matched_lo_p
15616                       && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15617             lo_pos = pos;
15618
15619           matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15620                           && fixup_has_matching_lo_p (*pos));
15621         }
15622
15623       /* If we found a match, remove the high-part relocation from its
15624          current position and insert it before the low-part relocation.
15625          Make the offsets match so that fixup_has_matching_lo_p()
15626          will return true.
15627
15628          We don't warn about unmatched high-part relocations since some
15629          versions of gcc have been known to emit dead "lui ...%hi(...)"
15630          instructions.  */
15631       if (lo_pos != NULL)
15632         {
15633           l->fixp->fx_offset = (*lo_pos)->fx_offset;
15634           if (l->fixp->fx_next != *lo_pos)
15635             {
15636               *hi_pos = l->fixp->fx_next;
15637               l->fixp->fx_next = *lo_pos;
15638               *lo_pos = l->fixp;
15639             }
15640         }
15641     }
15642 }
15643
15644 int
15645 mips_force_relocation (fixS *fixp)
15646 {
15647   if (generic_force_reloc (fixp))
15648     return 1;
15649
15650   /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15651      so that the linker relaxation can update targets.  */
15652   if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15653       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15654       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15655     return 1;
15656
15657   return 0;
15658 }
15659
15660 /* Read the instruction associated with RELOC from BUF.  */
15661
15662 static unsigned int
15663 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15664 {
15665   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15666     return read_compressed_insn (buf, 4);
15667   else
15668     return read_insn (buf);
15669 }
15670
15671 /* Write instruction INSN to BUF, given that it has been relocated
15672    by RELOC.  */
15673
15674 static void
15675 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15676                   unsigned long insn)
15677 {
15678   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15679     write_compressed_insn (buf, insn, 4);
15680   else
15681     write_insn (buf, insn);
15682 }
15683
15684 /* Apply a fixup to the object file.  */
15685
15686 void
15687 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15688 {
15689   char *buf;
15690   unsigned long insn;
15691   reloc_howto_type *howto;
15692
15693   /* We ignore generic BFD relocations we don't know about.  */
15694   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15695   if (! howto)
15696     return;
15697
15698   gas_assert (fixP->fx_size == 2
15699               || fixP->fx_size == 4
15700               || fixP->fx_r_type == BFD_RELOC_16
15701               || fixP->fx_r_type == BFD_RELOC_64
15702               || fixP->fx_r_type == BFD_RELOC_CTOR
15703               || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15704               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15705               || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15706               || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15707               || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
15708
15709   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15710
15711   gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15712               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15713               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15714               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
15715               || fixP->fx_r_type == BFD_RELOC_32_PCREL);
15716
15717   /* Don't treat parts of a composite relocation as done.  There are two
15718      reasons for this:
15719
15720      (1) The second and third parts will be against 0 (RSS_UNDEF) but
15721          should nevertheless be emitted if the first part is.
15722
15723      (2) In normal usage, composite relocations are never assembly-time
15724          constants.  The easiest way of dealing with the pathological
15725          exceptions is to generate a relocation against STN_UNDEF and
15726          leave everything up to the linker.  */
15727   if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15728     fixP->fx_done = 1;
15729
15730   switch (fixP->fx_r_type)
15731     {
15732     case BFD_RELOC_MIPS_TLS_GD:
15733     case BFD_RELOC_MIPS_TLS_LDM:
15734     case BFD_RELOC_MIPS_TLS_DTPREL32:
15735     case BFD_RELOC_MIPS_TLS_DTPREL64:
15736     case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15737     case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15738     case BFD_RELOC_MIPS_TLS_GOTTPREL:
15739     case BFD_RELOC_MIPS_TLS_TPREL32:
15740     case BFD_RELOC_MIPS_TLS_TPREL64:
15741     case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15742     case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15743     case BFD_RELOC_MICROMIPS_TLS_GD:
15744     case BFD_RELOC_MICROMIPS_TLS_LDM:
15745     case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15746     case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15747     case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15748     case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15749     case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15750     case BFD_RELOC_MIPS16_TLS_GD:
15751     case BFD_RELOC_MIPS16_TLS_LDM:
15752     case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15753     case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15754     case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15755     case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15756     case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15757       if (!fixP->fx_addsy)
15758         {
15759           as_bad_where (fixP->fx_file, fixP->fx_line,
15760                         _("TLS relocation against a constant"));
15761           break;
15762         }
15763       S_SET_THREAD_LOCAL (fixP->fx_addsy);
15764       /* fall through */
15765
15766     case BFD_RELOC_MIPS_JMP:
15767     case BFD_RELOC_MIPS_SHIFT5:
15768     case BFD_RELOC_MIPS_SHIFT6:
15769     case BFD_RELOC_MIPS_GOT_DISP:
15770     case BFD_RELOC_MIPS_GOT_PAGE:
15771     case BFD_RELOC_MIPS_GOT_OFST:
15772     case BFD_RELOC_MIPS_SUB:
15773     case BFD_RELOC_MIPS_INSERT_A:
15774     case BFD_RELOC_MIPS_INSERT_B:
15775     case BFD_RELOC_MIPS_DELETE:
15776     case BFD_RELOC_MIPS_HIGHEST:
15777     case BFD_RELOC_MIPS_HIGHER:
15778     case BFD_RELOC_MIPS_SCN_DISP:
15779     case BFD_RELOC_MIPS_REL16:
15780     case BFD_RELOC_MIPS_RELGOT:
15781     case BFD_RELOC_MIPS_JALR:
15782     case BFD_RELOC_HI16:
15783     case BFD_RELOC_HI16_S:
15784     case BFD_RELOC_LO16:
15785     case BFD_RELOC_GPREL16:
15786     case BFD_RELOC_MIPS_LITERAL:
15787     case BFD_RELOC_MIPS_CALL16:
15788     case BFD_RELOC_MIPS_GOT16:
15789     case BFD_RELOC_GPREL32:
15790     case BFD_RELOC_MIPS_GOT_HI16:
15791     case BFD_RELOC_MIPS_GOT_LO16:
15792     case BFD_RELOC_MIPS_CALL_HI16:
15793     case BFD_RELOC_MIPS_CALL_LO16:
15794     case BFD_RELOC_MIPS16_GPREL:
15795     case BFD_RELOC_MIPS16_GOT16:
15796     case BFD_RELOC_MIPS16_CALL16:
15797     case BFD_RELOC_MIPS16_HI16:
15798     case BFD_RELOC_MIPS16_HI16_S:
15799     case BFD_RELOC_MIPS16_LO16:
15800     case BFD_RELOC_MIPS16_JMP:
15801     case BFD_RELOC_MICROMIPS_JMP:
15802     case BFD_RELOC_MICROMIPS_GOT_DISP:
15803     case BFD_RELOC_MICROMIPS_GOT_PAGE:
15804     case BFD_RELOC_MICROMIPS_GOT_OFST:
15805     case BFD_RELOC_MICROMIPS_SUB:
15806     case BFD_RELOC_MICROMIPS_HIGHEST:
15807     case BFD_RELOC_MICROMIPS_HIGHER:
15808     case BFD_RELOC_MICROMIPS_SCN_DISP:
15809     case BFD_RELOC_MICROMIPS_JALR:
15810     case BFD_RELOC_MICROMIPS_HI16:
15811     case BFD_RELOC_MICROMIPS_HI16_S:
15812     case BFD_RELOC_MICROMIPS_LO16:
15813     case BFD_RELOC_MICROMIPS_GPREL16:
15814     case BFD_RELOC_MICROMIPS_LITERAL:
15815     case BFD_RELOC_MICROMIPS_CALL16:
15816     case BFD_RELOC_MICROMIPS_GOT16:
15817     case BFD_RELOC_MICROMIPS_GOT_HI16:
15818     case BFD_RELOC_MICROMIPS_GOT_LO16:
15819     case BFD_RELOC_MICROMIPS_CALL_HI16:
15820     case BFD_RELOC_MICROMIPS_CALL_LO16:
15821     case BFD_RELOC_MIPS_EH:
15822       if (fixP->fx_done)
15823         {
15824           offsetT value;
15825
15826           if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15827             {
15828               insn = read_reloc_insn (buf, fixP->fx_r_type);
15829               if (mips16_reloc_p (fixP->fx_r_type))
15830                 insn |= mips16_immed_extend (value, 16);
15831               else
15832                 insn |= (value & 0xffff);
15833               write_reloc_insn (buf, fixP->fx_r_type, insn);
15834             }
15835           else
15836             as_bad_where (fixP->fx_file, fixP->fx_line,
15837                           _("Unsupported constant in relocation"));
15838         }
15839       break;
15840
15841     case BFD_RELOC_64:
15842       /* This is handled like BFD_RELOC_32, but we output a sign
15843          extended value if we are only 32 bits.  */
15844       if (fixP->fx_done)
15845         {
15846           if (8 <= sizeof (valueT))
15847             md_number_to_chars (buf, *valP, 8);
15848           else
15849             {
15850               valueT hiv;
15851
15852               if ((*valP & 0x80000000) != 0)
15853                 hiv = 0xffffffff;
15854               else
15855                 hiv = 0;
15856               md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15857               md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
15858             }
15859         }
15860       break;
15861
15862     case BFD_RELOC_RVA:
15863     case BFD_RELOC_32:
15864     case BFD_RELOC_32_PCREL:
15865     case BFD_RELOC_16:
15866       /* If we are deleting this reloc entry, we must fill in the
15867          value now.  This can happen if we have a .word which is not
15868          resolved when it appears but is later defined.  */
15869       if (fixP->fx_done)
15870         md_number_to_chars (buf, *valP, fixP->fx_size);
15871       break;
15872
15873     case BFD_RELOC_16_PCREL_S2:
15874       if ((*valP & 0x3) != 0)
15875         as_bad_where (fixP->fx_file, fixP->fx_line,
15876                       _("Branch to misaligned address (%lx)"), (long) *valP);
15877
15878       /* We need to save the bits in the instruction since fixup_segment()
15879          might be deleting the relocation entry (i.e., a branch within
15880          the current segment).  */
15881       if (! fixP->fx_done)
15882         break;
15883
15884       /* Update old instruction data.  */
15885       insn = read_insn (buf);
15886
15887       if (*valP + 0x20000 <= 0x3ffff)
15888         {
15889           insn |= (*valP >> 2) & 0xffff;
15890           write_insn (buf, insn);
15891         }
15892       else if (mips_pic == NO_PIC
15893                && fixP->fx_done
15894                && fixP->fx_frag->fr_address >= text_section->vma
15895                && (fixP->fx_frag->fr_address
15896                    < text_section->vma + bfd_get_section_size (text_section))
15897                && ((insn & 0xffff0000) == 0x10000000     /* beq $0,$0 */
15898                    || (insn & 0xffff0000) == 0x04010000  /* bgez $0 */
15899                    || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15900         {
15901           /* The branch offset is too large.  If this is an
15902              unconditional branch, and we are not generating PIC code,
15903              we can convert it to an absolute jump instruction.  */
15904           if ((insn & 0xffff0000) == 0x04110000)         /* bgezal $0 */
15905             insn = 0x0c000000;  /* jal */
15906           else
15907             insn = 0x08000000;  /* j */
15908           fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15909           fixP->fx_done = 0;
15910           fixP->fx_addsy = section_symbol (text_section);
15911           *valP += md_pcrel_from (fixP);
15912           write_insn (buf, insn);
15913         }
15914       else
15915         {
15916           /* If we got here, we have branch-relaxation disabled,
15917              and there's nothing we can do to fix this instruction
15918              without turning it into a longer sequence.  */
15919           as_bad_where (fixP->fx_file, fixP->fx_line,
15920                         _("Branch out of range"));
15921         }
15922       break;
15923
15924     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15925     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15926     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15927       /* We adjust the offset back to even.  */
15928       if ((*valP & 0x1) != 0)
15929         --(*valP);
15930
15931       if (! fixP->fx_done)
15932         break;
15933
15934       /* Should never visit here, because we keep the relocation.  */
15935       abort ();
15936       break;
15937
15938     case BFD_RELOC_VTABLE_INHERIT:
15939       fixP->fx_done = 0;
15940       if (fixP->fx_addsy
15941           && !S_IS_DEFINED (fixP->fx_addsy)
15942           && !S_IS_WEAK (fixP->fx_addsy))
15943         S_SET_WEAK (fixP->fx_addsy);
15944       break;
15945
15946     case BFD_RELOC_VTABLE_ENTRY:
15947       fixP->fx_done = 0;
15948       break;
15949
15950     default:
15951       abort ();
15952     }
15953
15954   /* Remember value for tc_gen_reloc.  */
15955   fixP->fx_addnumber = *valP;
15956 }
15957
15958 static symbolS *
15959 get_symbol (void)
15960 {
15961   int c;
15962   char *name;
15963   symbolS *p;
15964
15965   name = input_line_pointer;
15966   c = get_symbol_end ();
15967   p = (symbolS *) symbol_find_or_make (name);
15968   *input_line_pointer = c;
15969   return p;
15970 }
15971
15972 /* Align the current frag to a given power of two.  If a particular
15973    fill byte should be used, FILL points to an integer that contains
15974    that byte, otherwise FILL is null.
15975
15976    This function used to have the comment:
15977
15978       The MIPS assembler also automatically adjusts any preceding label.
15979
15980    The implementation therefore applied the adjustment to a maximum of
15981    one label.  However, other label adjustments are applied to batches
15982    of labels, and adjusting just one caused problems when new labels
15983    were added for the sake of debugging or unwind information.
15984    We therefore adjust all preceding labels (given as LABELS) instead.  */
15985
15986 static void
15987 mips_align (int to, int *fill, struct insn_label_list *labels)
15988 {
15989   mips_emit_delays ();
15990   mips_record_compressed_mode ();
15991   if (fill == NULL && subseg_text_p (now_seg))
15992     frag_align_code (to, 0);
15993   else
15994     frag_align (to, fill ? *fill : 0, 0);
15995   record_alignment (now_seg, to);
15996   mips_move_labels (labels, FALSE);
15997 }
15998
15999 /* Align to a given power of two.  .align 0 turns off the automatic
16000    alignment used by the data creating pseudo-ops.  */
16001
16002 static void
16003 s_align (int x ATTRIBUTE_UNUSED)
16004 {
16005   int temp, fill_value, *fill_ptr;
16006   long max_alignment = 28;
16007
16008   /* o Note that the assembler pulls down any immediately preceding label
16009        to the aligned address.
16010      o It's not documented but auto alignment is reinstated by
16011        a .align pseudo instruction.
16012      o Note also that after auto alignment is turned off the mips assembler
16013        issues an error on attempt to assemble an improperly aligned data item.
16014        We don't.  */
16015
16016   temp = get_absolute_expression ();
16017   if (temp > max_alignment)
16018     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
16019   else if (temp < 0)
16020     {
16021       as_warn (_("Alignment negative: 0 assumed."));
16022       temp = 0;
16023     }
16024   if (*input_line_pointer == ',')
16025     {
16026       ++input_line_pointer;
16027       fill_value = get_absolute_expression ();
16028       fill_ptr = &fill_value;
16029     }
16030   else
16031     fill_ptr = 0;
16032   if (temp)
16033     {
16034       segment_info_type *si = seg_info (now_seg);
16035       struct insn_label_list *l = si->label_list;
16036       /* Auto alignment should be switched on by next section change.  */
16037       auto_align = 1;
16038       mips_align (temp, fill_ptr, l);
16039     }
16040   else
16041     {
16042       auto_align = 0;
16043     }
16044
16045   demand_empty_rest_of_line ();
16046 }
16047
16048 static void
16049 s_change_sec (int sec)
16050 {
16051   segT seg;
16052
16053   /* The ELF backend needs to know that we are changing sections, so
16054      that .previous works correctly.  We could do something like check
16055      for an obj_section_change_hook macro, but that might be confusing
16056      as it would not be appropriate to use it in the section changing
16057      functions in read.c, since obj-elf.c intercepts those.  FIXME:
16058      This should be cleaner, somehow.  */
16059   obj_elf_section_change_hook ();
16060
16061   mips_emit_delays ();
16062
16063   switch (sec)
16064     {
16065     case 't':
16066       s_text (0);
16067       break;
16068     case 'd':
16069       s_data (0);
16070       break;
16071     case 'b':
16072       subseg_set (bss_section, (subsegT) get_absolute_expression ());
16073       demand_empty_rest_of_line ();
16074       break;
16075
16076     case 'r':
16077       seg = subseg_new (RDATA_SECTION_NAME,
16078                         (subsegT) get_absolute_expression ());
16079       bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16080                                               | SEC_READONLY | SEC_RELOC
16081                                               | SEC_DATA));
16082       if (strncmp (TARGET_OS, "elf", 3) != 0)
16083         record_alignment (seg, 4);
16084       demand_empty_rest_of_line ();
16085       break;
16086
16087     case 's':
16088       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
16089       bfd_set_section_flags (stdoutput, seg,
16090                              SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16091       if (strncmp (TARGET_OS, "elf", 3) != 0)
16092         record_alignment (seg, 4);
16093       demand_empty_rest_of_line ();
16094       break;
16095
16096     case 'B':
16097       seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16098       bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16099       if (strncmp (TARGET_OS, "elf", 3) != 0)
16100         record_alignment (seg, 4);
16101       demand_empty_rest_of_line ();
16102       break;
16103     }
16104
16105   auto_align = 1;
16106 }
16107
16108 void
16109 s_change_section (int ignore ATTRIBUTE_UNUSED)
16110 {
16111   char *section_name;
16112   char c;
16113   char next_c = 0;
16114   int section_type;
16115   int section_flag;
16116   int section_entry_size;
16117   int section_alignment;
16118
16119   section_name = input_line_pointer;
16120   c = get_symbol_end ();
16121   if (c)
16122     next_c = *(input_line_pointer + 1);
16123
16124   /* Do we have .section Name<,"flags">?  */
16125   if (c != ',' || (c == ',' && next_c == '"'))
16126     {
16127       /* just after name is now '\0'.  */
16128       *input_line_pointer = c;
16129       input_line_pointer = section_name;
16130       obj_elf_section (ignore);
16131       return;
16132     }
16133   input_line_pointer++;
16134
16135   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
16136   if (c == ',')
16137     section_type = get_absolute_expression ();
16138   else
16139     section_type = 0;
16140   if (*input_line_pointer++ == ',')
16141     section_flag = get_absolute_expression ();
16142   else
16143     section_flag = 0;
16144   if (*input_line_pointer++ == ',')
16145     section_entry_size = get_absolute_expression ();
16146   else
16147     section_entry_size = 0;
16148   if (*input_line_pointer++ == ',')
16149     section_alignment = get_absolute_expression ();
16150   else
16151     section_alignment = 0;
16152   /* FIXME: really ignore?  */
16153   (void) section_alignment;
16154
16155   section_name = xstrdup (section_name);
16156
16157   /* When using the generic form of .section (as implemented by obj-elf.c),
16158      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
16159      traditionally had to fall back on the more common @progbits instead.
16160
16161      There's nothing really harmful in this, since bfd will correct
16162      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
16163      means that, for backwards compatibility, the special_section entries
16164      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16165
16166      Even so, we shouldn't force users of the MIPS .section syntax to
16167      incorrectly label the sections as SHT_PROGBITS.  The best compromise
16168      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16169      generic type-checking code.  */
16170   if (section_type == SHT_MIPS_DWARF)
16171     section_type = SHT_PROGBITS;
16172
16173   obj_elf_change_section (section_name, section_type, section_flag,
16174                           section_entry_size, 0, 0, 0);
16175
16176   if (now_seg->name != section_name)
16177     free (section_name);
16178 }
16179
16180 void
16181 mips_enable_auto_align (void)
16182 {
16183   auto_align = 1;
16184 }
16185
16186 static void
16187 s_cons (int log_size)
16188 {
16189   segment_info_type *si = seg_info (now_seg);
16190   struct insn_label_list *l = si->label_list;
16191
16192   mips_emit_delays ();
16193   if (log_size > 0 && auto_align)
16194     mips_align (log_size, 0, l);
16195   cons (1 << log_size);
16196   mips_clear_insn_labels ();
16197 }
16198
16199 static void
16200 s_float_cons (int type)
16201 {
16202   segment_info_type *si = seg_info (now_seg);
16203   struct insn_label_list *l = si->label_list;
16204
16205   mips_emit_delays ();
16206
16207   if (auto_align)
16208     {
16209       if (type == 'd')
16210         mips_align (3, 0, l);
16211       else
16212         mips_align (2, 0, l);
16213     }
16214
16215   float_cons (type);
16216   mips_clear_insn_labels ();
16217 }
16218
16219 /* Handle .globl.  We need to override it because on Irix 5 you are
16220    permitted to say
16221        .globl foo .text
16222    where foo is an undefined symbol, to mean that foo should be
16223    considered to be the address of a function.  */
16224
16225 static void
16226 s_mips_globl (int x ATTRIBUTE_UNUSED)
16227 {
16228   char *name;
16229   int c;
16230   symbolS *symbolP;
16231   flagword flag;
16232
16233   do
16234     {
16235       name = input_line_pointer;
16236       c = get_symbol_end ();
16237       symbolP = symbol_find_or_make (name);
16238       S_SET_EXTERNAL (symbolP);
16239
16240       *input_line_pointer = c;
16241       SKIP_WHITESPACE ();
16242
16243       /* On Irix 5, every global symbol that is not explicitly labelled as
16244          being a function is apparently labelled as being an object.  */
16245       flag = BSF_OBJECT;
16246
16247       if (!is_end_of_line[(unsigned char) *input_line_pointer]
16248           && (*input_line_pointer != ','))
16249         {
16250           char *secname;
16251           asection *sec;
16252
16253           secname = input_line_pointer;
16254           c = get_symbol_end ();
16255           sec = bfd_get_section_by_name (stdoutput, secname);
16256           if (sec == NULL)
16257             as_bad (_("%s: no such section"), secname);
16258           *input_line_pointer = c;
16259
16260           if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16261             flag = BSF_FUNCTION;
16262         }
16263
16264       symbol_get_bfdsym (symbolP)->flags |= flag;
16265
16266       c = *input_line_pointer;
16267       if (c == ',')
16268         {
16269           input_line_pointer++;
16270           SKIP_WHITESPACE ();
16271           if (is_end_of_line[(unsigned char) *input_line_pointer])
16272             c = '\n';
16273         }
16274     }
16275   while (c == ',');
16276
16277   demand_empty_rest_of_line ();
16278 }
16279
16280 static void
16281 s_option (int x ATTRIBUTE_UNUSED)
16282 {
16283   char *opt;
16284   char c;
16285
16286   opt = input_line_pointer;
16287   c = get_symbol_end ();
16288
16289   if (*opt == 'O')
16290     {
16291       /* FIXME: What does this mean?  */
16292     }
16293   else if (strncmp (opt, "pic", 3) == 0)
16294     {
16295       int i;
16296
16297       i = atoi (opt + 3);
16298       if (i == 0)
16299         mips_pic = NO_PIC;
16300       else if (i == 2)
16301         {
16302           mips_pic = SVR4_PIC;
16303           mips_abicalls = TRUE;
16304         }
16305       else
16306         as_bad (_(".option pic%d not supported"), i);
16307
16308       if (mips_pic == SVR4_PIC)
16309         {
16310           if (g_switch_seen && g_switch_value != 0)
16311             as_warn (_("-G may not be used with SVR4 PIC code"));
16312           g_switch_value = 0;
16313           bfd_set_gp_size (stdoutput, 0);
16314         }
16315     }
16316   else
16317     as_warn (_("Unrecognized option \"%s\""), opt);
16318
16319   *input_line_pointer = c;
16320   demand_empty_rest_of_line ();
16321 }
16322
16323 /* This structure is used to hold a stack of .set values.  */
16324
16325 struct mips_option_stack
16326 {
16327   struct mips_option_stack *next;
16328   struct mips_set_options options;
16329 };
16330
16331 static struct mips_option_stack *mips_opts_stack;
16332
16333 /* Handle the .set pseudo-op.  */
16334
16335 static void
16336 s_mipsset (int x ATTRIBUTE_UNUSED)
16337 {
16338   char *name = input_line_pointer, ch;
16339   const struct mips_ase *ase;
16340
16341   while (!is_end_of_line[(unsigned char) *input_line_pointer])
16342     ++input_line_pointer;
16343   ch = *input_line_pointer;
16344   *input_line_pointer = '\0';
16345
16346   if (strcmp (name, "reorder") == 0)
16347     {
16348       if (mips_opts.noreorder)
16349         end_noreorder ();
16350     }
16351   else if (strcmp (name, "noreorder") == 0)
16352     {
16353       if (!mips_opts.noreorder)
16354         start_noreorder ();
16355     }
16356   else if (strncmp (name, "at=", 3) == 0)
16357     {
16358       char *s = name + 3;
16359
16360       if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16361         as_bad (_("Unrecognized register name `%s'"), s);
16362     }
16363   else if (strcmp (name, "at") == 0)
16364     {
16365       mips_opts.at = ATREG;
16366     }
16367   else if (strcmp (name, "noat") == 0)
16368     {
16369       mips_opts.at = ZERO;
16370     }
16371   else if (strcmp (name, "macro") == 0)
16372     {
16373       mips_opts.warn_about_macros = 0;
16374     }
16375   else if (strcmp (name, "nomacro") == 0)
16376     {
16377       if (mips_opts.noreorder == 0)
16378         as_bad (_("`noreorder' must be set before `nomacro'"));
16379       mips_opts.warn_about_macros = 1;
16380     }
16381   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16382     {
16383       mips_opts.nomove = 0;
16384     }
16385   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16386     {
16387       mips_opts.nomove = 1;
16388     }
16389   else if (strcmp (name, "bopt") == 0)
16390     {
16391       mips_opts.nobopt = 0;
16392     }
16393   else if (strcmp (name, "nobopt") == 0)
16394     {
16395       mips_opts.nobopt = 1;
16396     }
16397   else if (strcmp (name, "gp=default") == 0)
16398     mips_opts.gp32 = file_mips_gp32;
16399   else if (strcmp (name, "gp=32") == 0)
16400     mips_opts.gp32 = 1;
16401   else if (strcmp (name, "gp=64") == 0)
16402     {
16403       if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
16404         as_warn (_("%s isa does not support 64-bit registers"),
16405                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16406       mips_opts.gp32 = 0;
16407     }
16408   else if (strcmp (name, "fp=default") == 0)
16409     mips_opts.fp32 = file_mips_fp32;
16410   else if (strcmp (name, "fp=32") == 0)
16411     mips_opts.fp32 = 1;
16412   else if (strcmp (name, "fp=64") == 0)
16413     {
16414       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
16415         as_warn (_("%s isa does not support 64-bit floating point registers"),
16416                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16417       mips_opts.fp32 = 0;
16418     }
16419   else if (strcmp (name, "softfloat") == 0)
16420     mips_opts.soft_float = 1;
16421   else if (strcmp (name, "hardfloat") == 0)
16422     mips_opts.soft_float = 0;
16423   else if (strcmp (name, "singlefloat") == 0)
16424     mips_opts.single_float = 1;
16425   else if (strcmp (name, "doublefloat") == 0)
16426     mips_opts.single_float = 0;
16427   else if (strcmp (name, "mips16") == 0
16428            || strcmp (name, "MIPS-16") == 0)
16429     {
16430       if (mips_opts.micromips == 1)
16431         as_fatal (_("`mips16' cannot be used with `micromips'"));
16432       mips_opts.mips16 = 1;
16433     }
16434   else if (strcmp (name, "nomips16") == 0
16435            || strcmp (name, "noMIPS-16") == 0)
16436     mips_opts.mips16 = 0;
16437   else if (strcmp (name, "micromips") == 0)
16438     {
16439       if (mips_opts.mips16 == 1)
16440         as_fatal (_("`micromips' cannot be used with `mips16'"));
16441       mips_opts.micromips = 1;
16442     }
16443   else if (strcmp (name, "nomicromips") == 0)
16444     mips_opts.micromips = 0;
16445   else if (name[0] == 'n'
16446            && name[1] == 'o'
16447            && (ase = mips_lookup_ase (name + 2)))
16448     mips_set_ase (ase, FALSE);
16449   else if ((ase = mips_lookup_ase (name)))
16450     mips_set_ase (ase, TRUE);
16451   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16452     {
16453       int reset = 0;
16454
16455       /* Permit the user to change the ISA and architecture on the fly.
16456          Needless to say, misuse can cause serious problems.  */
16457       if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16458         {
16459           reset = 1;
16460           mips_opts.isa = file_mips_isa;
16461           mips_opts.arch = file_mips_arch;
16462         }
16463       else if (strncmp (name, "arch=", 5) == 0)
16464         {
16465           const struct mips_cpu_info *p;
16466
16467           p = mips_parse_cpu("internal use", name + 5);
16468           if (!p)
16469             as_bad (_("unknown architecture %s"), name + 5);
16470           else
16471             {
16472               mips_opts.arch = p->cpu;
16473               mips_opts.isa = p->isa;
16474             }
16475         }
16476       else if (strncmp (name, "mips", 4) == 0)
16477         {
16478           const struct mips_cpu_info *p;
16479
16480           p = mips_parse_cpu("internal use", name);
16481           if (!p)
16482             as_bad (_("unknown ISA level %s"), name + 4);
16483           else
16484             {
16485               mips_opts.arch = p->cpu;
16486               mips_opts.isa = p->isa;
16487             }
16488         }
16489       else
16490         as_bad (_("unknown ISA or architecture %s"), name);
16491
16492       switch (mips_opts.isa)
16493         {
16494         case  0:
16495           break;
16496         case ISA_MIPS1:
16497         case ISA_MIPS2:
16498         case ISA_MIPS32:
16499         case ISA_MIPS32R2:
16500           mips_opts.gp32 = 1;
16501           mips_opts.fp32 = 1;
16502           break;
16503         case ISA_MIPS3:
16504         case ISA_MIPS4:
16505         case ISA_MIPS5:
16506         case ISA_MIPS64:
16507         case ISA_MIPS64R2:
16508           mips_opts.gp32 = 0;
16509           if (mips_opts.arch == CPU_R5900)
16510             {
16511                 mips_opts.fp32 = 1;
16512             }
16513           else
16514             {
16515           mips_opts.fp32 = 0;
16516             }
16517           break;
16518         default:
16519           as_bad (_("unknown ISA level %s"), name + 4);
16520           break;
16521         }
16522       if (reset)
16523         {
16524           mips_opts.gp32 = file_mips_gp32;
16525           mips_opts.fp32 = file_mips_fp32;
16526         }
16527     }
16528   else if (strcmp (name, "autoextend") == 0)
16529     mips_opts.noautoextend = 0;
16530   else if (strcmp (name, "noautoextend") == 0)
16531     mips_opts.noautoextend = 1;
16532   else if (strcmp (name, "insn32") == 0)
16533     mips_opts.insn32 = TRUE;
16534   else if (strcmp (name, "noinsn32") == 0)
16535     mips_opts.insn32 = FALSE;
16536   else if (strcmp (name, "push") == 0)
16537     {
16538       struct mips_option_stack *s;
16539
16540       s = (struct mips_option_stack *) xmalloc (sizeof *s);
16541       s->next = mips_opts_stack;
16542       s->options = mips_opts;
16543       mips_opts_stack = s;
16544     }
16545   else if (strcmp (name, "pop") == 0)
16546     {
16547       struct mips_option_stack *s;
16548
16549       s = mips_opts_stack;
16550       if (s == NULL)
16551         as_bad (_(".set pop with no .set push"));
16552       else
16553         {
16554           /* If we're changing the reorder mode we need to handle
16555              delay slots correctly.  */
16556           if (s->options.noreorder && ! mips_opts.noreorder)
16557             start_noreorder ();
16558           else if (! s->options.noreorder && mips_opts.noreorder)
16559             end_noreorder ();
16560
16561           mips_opts = s->options;
16562           mips_opts_stack = s->next;
16563           free (s);
16564         }
16565     }
16566   else if (strcmp (name, "sym32") == 0)
16567     mips_opts.sym32 = TRUE;
16568   else if (strcmp (name, "nosym32") == 0)
16569     mips_opts.sym32 = FALSE;
16570   else if (strchr (name, ','))
16571     {
16572       /* Generic ".set" directive; use the generic handler.  */
16573       *input_line_pointer = ch;
16574       input_line_pointer = name;
16575       s_set (0);
16576       return;
16577     }
16578   else
16579     {
16580       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16581     }
16582   mips_check_isa_supports_ases ();
16583   *input_line_pointer = ch;
16584   demand_empty_rest_of_line ();
16585 }
16586
16587 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
16588    .option pic2.  It means to generate SVR4 PIC calls.  */
16589
16590 static void
16591 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16592 {
16593   mips_pic = SVR4_PIC;
16594   mips_abicalls = TRUE;
16595
16596   if (g_switch_seen && g_switch_value != 0)
16597     as_warn (_("-G may not be used with SVR4 PIC code"));
16598   g_switch_value = 0;
16599
16600   bfd_set_gp_size (stdoutput, 0);
16601   demand_empty_rest_of_line ();
16602 }
16603
16604 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
16605    PIC code.  It sets the $gp register for the function based on the
16606    function address, which is in the register named in the argument.
16607    This uses a relocation against _gp_disp, which is handled specially
16608    by the linker.  The result is:
16609         lui     $gp,%hi(_gp_disp)
16610         addiu   $gp,$gp,%lo(_gp_disp)
16611         addu    $gp,$gp,.cpload argument
16612    The .cpload argument is normally $25 == $t9.
16613
16614    The -mno-shared option changes this to:
16615         lui     $gp,%hi(__gnu_local_gp)
16616         addiu   $gp,$gp,%lo(__gnu_local_gp)
16617    and the argument is ignored.  This saves an instruction, but the
16618    resulting code is not position independent; it uses an absolute
16619    address for __gnu_local_gp.  Thus code assembled with -mno-shared
16620    can go into an ordinary executable, but not into a shared library.  */
16621
16622 static void
16623 s_cpload (int ignore ATTRIBUTE_UNUSED)
16624 {
16625   expressionS ex;
16626   int reg;
16627   int in_shared;
16628
16629   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16630      .cpload is ignored.  */
16631   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16632     {
16633       s_ignore (0);
16634       return;
16635     }
16636
16637   if (mips_opts.mips16)
16638     {
16639       as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16640       ignore_rest_of_line ();
16641       return;
16642     }
16643
16644   /* .cpload should be in a .set noreorder section.  */
16645   if (mips_opts.noreorder == 0)
16646     as_warn (_(".cpload not in noreorder section"));
16647
16648   reg = tc_get_register (0);
16649
16650   /* If we need to produce a 64-bit address, we are better off using
16651      the default instruction sequence.  */
16652   in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16653
16654   ex.X_op = O_symbol;
16655   ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16656                                          "__gnu_local_gp");
16657   ex.X_op_symbol = NULL;
16658   ex.X_add_number = 0;
16659
16660   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16661   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16662
16663   mips_mark_labels ();
16664   mips_assembling_insn = TRUE;
16665
16666   macro_start ();
16667   macro_build_lui (&ex, mips_gp_register);
16668   macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16669                mips_gp_register, BFD_RELOC_LO16);
16670   if (in_shared)
16671     macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16672                  mips_gp_register, reg);
16673   macro_end ();
16674
16675   mips_assembling_insn = FALSE;
16676   demand_empty_rest_of_line ();
16677 }
16678
16679 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
16680      .cpsetup $reg1, offset|$reg2, label
16681
16682    If offset is given, this results in:
16683      sd         $gp, offset($sp)
16684      lui        $gp, %hi(%neg(%gp_rel(label)))
16685      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
16686      daddu      $gp, $gp, $reg1
16687
16688    If $reg2 is given, this results in:
16689      daddu      $reg2, $gp, $0
16690      lui        $gp, %hi(%neg(%gp_rel(label)))
16691      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
16692      daddu      $gp, $gp, $reg1
16693    $reg1 is normally $25 == $t9.
16694
16695    The -mno-shared option replaces the last three instructions with
16696         lui     $gp,%hi(_gp)
16697         addiu   $gp,$gp,%lo(_gp)  */
16698
16699 static void
16700 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16701 {
16702   expressionS ex_off;
16703   expressionS ex_sym;
16704   int reg1;
16705
16706   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16707      We also need NewABI support.  */
16708   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16709     {
16710       s_ignore (0);
16711       return;
16712     }
16713
16714   if (mips_opts.mips16)
16715     {
16716       as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16717       ignore_rest_of_line ();
16718       return;
16719     }
16720
16721   reg1 = tc_get_register (0);
16722   SKIP_WHITESPACE ();
16723   if (*input_line_pointer != ',')
16724     {
16725       as_bad (_("missing argument separator ',' for .cpsetup"));
16726       return;
16727     }
16728   else
16729     ++input_line_pointer;
16730   SKIP_WHITESPACE ();
16731   if (*input_line_pointer == '$')
16732     {
16733       mips_cpreturn_register = tc_get_register (0);
16734       mips_cpreturn_offset = -1;
16735     }
16736   else
16737     {
16738       mips_cpreturn_offset = get_absolute_expression ();
16739       mips_cpreturn_register = -1;
16740     }
16741   SKIP_WHITESPACE ();
16742   if (*input_line_pointer != ',')
16743     {
16744       as_bad (_("missing argument separator ',' for .cpsetup"));
16745       return;
16746     }
16747   else
16748     ++input_line_pointer;
16749   SKIP_WHITESPACE ();
16750   expression (&ex_sym);
16751
16752   mips_mark_labels ();
16753   mips_assembling_insn = TRUE;
16754
16755   macro_start ();
16756   if (mips_cpreturn_register == -1)
16757     {
16758       ex_off.X_op = O_constant;
16759       ex_off.X_add_symbol = NULL;
16760       ex_off.X_op_symbol = NULL;
16761       ex_off.X_add_number = mips_cpreturn_offset;
16762
16763       macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16764                    BFD_RELOC_LO16, SP);
16765     }
16766   else
16767     macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
16768                  mips_gp_register, 0);
16769
16770   if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16771     {
16772       macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16773                    -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16774                    BFD_RELOC_HI16_S);
16775
16776       macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16777                    mips_gp_register, -1, BFD_RELOC_GPREL16,
16778                    BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16779
16780       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16781                    mips_gp_register, reg1);
16782     }
16783   else
16784     {
16785       expressionS ex;
16786
16787       ex.X_op = O_symbol;
16788       ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16789       ex.X_op_symbol = NULL;
16790       ex.X_add_number = 0;
16791
16792       /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16793       symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16794
16795       macro_build_lui (&ex, mips_gp_register);
16796       macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16797                    mips_gp_register, BFD_RELOC_LO16);
16798     }
16799
16800   macro_end ();
16801
16802   mips_assembling_insn = FALSE;
16803   demand_empty_rest_of_line ();
16804 }
16805
16806 static void
16807 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16808 {
16809   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16810      .cplocal is ignored.  */
16811   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16812     {
16813       s_ignore (0);
16814       return;
16815     }
16816
16817   if (mips_opts.mips16)
16818     {
16819       as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16820       ignore_rest_of_line ();
16821       return;
16822     }
16823
16824   mips_gp_register = tc_get_register (0);
16825   demand_empty_rest_of_line ();
16826 }
16827
16828 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
16829    offset from $sp.  The offset is remembered, and after making a PIC
16830    call $gp is restored from that location.  */
16831
16832 static void
16833 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16834 {
16835   expressionS ex;
16836
16837   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16838      .cprestore is ignored.  */
16839   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16840     {
16841       s_ignore (0);
16842       return;
16843     }
16844
16845   if (mips_opts.mips16)
16846     {
16847       as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16848       ignore_rest_of_line ();
16849       return;
16850     }
16851
16852   mips_cprestore_offset = get_absolute_expression ();
16853   mips_cprestore_valid = 1;
16854
16855   ex.X_op = O_constant;
16856   ex.X_add_symbol = NULL;
16857   ex.X_op_symbol = NULL;
16858   ex.X_add_number = mips_cprestore_offset;
16859
16860   mips_mark_labels ();
16861   mips_assembling_insn = TRUE;
16862
16863   macro_start ();
16864   macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16865                                 SP, HAVE_64BIT_ADDRESSES);
16866   macro_end ();
16867
16868   mips_assembling_insn = FALSE;
16869   demand_empty_rest_of_line ();
16870 }
16871
16872 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16873    was given in the preceding .cpsetup, it results in:
16874      ld         $gp, offset($sp)
16875
16876    If a register $reg2 was given there, it results in:
16877      daddu      $gp, $reg2, $0  */
16878
16879 static void
16880 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16881 {
16882   expressionS ex;
16883
16884   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16885      We also need NewABI support.  */
16886   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16887     {
16888       s_ignore (0);
16889       return;
16890     }
16891
16892   if (mips_opts.mips16)
16893     {
16894       as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16895       ignore_rest_of_line ();
16896       return;
16897     }
16898
16899   mips_mark_labels ();
16900   mips_assembling_insn = TRUE;
16901
16902   macro_start ();
16903   if (mips_cpreturn_register == -1)
16904     {
16905       ex.X_op = O_constant;
16906       ex.X_add_symbol = NULL;
16907       ex.X_op_symbol = NULL;
16908       ex.X_add_number = mips_cpreturn_offset;
16909
16910       macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16911     }
16912   else
16913     macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
16914                  mips_cpreturn_register, 0);
16915   macro_end ();
16916
16917   mips_assembling_insn = FALSE;
16918   demand_empty_rest_of_line ();
16919 }
16920
16921 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16922    pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16923    DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16924    debug information or MIPS16 TLS.  */
16925
16926 static void
16927 s_tls_rel_directive (const size_t bytes, const char *dirstr,
16928                      bfd_reloc_code_real_type rtype)
16929 {
16930   expressionS ex;
16931   char *p;
16932
16933   expression (&ex);
16934
16935   if (ex.X_op != O_symbol)
16936     {
16937       as_bad (_("Unsupported use of %s"), dirstr);
16938       ignore_rest_of_line ();
16939     }
16940
16941   p = frag_more (bytes);
16942   md_number_to_chars (p, 0, bytes);
16943   fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
16944   demand_empty_rest_of_line ();
16945   mips_clear_insn_labels ();
16946 }
16947
16948 /* Handle .dtprelword.  */
16949
16950 static void
16951 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16952 {
16953   s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
16954 }
16955
16956 /* Handle .dtpreldword.  */
16957
16958 static void
16959 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16960 {
16961   s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16962 }
16963
16964 /* Handle .tprelword.  */
16965
16966 static void
16967 s_tprelword (int ignore ATTRIBUTE_UNUSED)
16968 {
16969   s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16970 }
16971
16972 /* Handle .tpreldword.  */
16973
16974 static void
16975 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16976 {
16977   s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
16978 }
16979
16980 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
16981    code.  It sets the offset to use in gp_rel relocations.  */
16982
16983 static void
16984 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
16985 {
16986   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16987      We also need NewABI support.  */
16988   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16989     {
16990       s_ignore (0);
16991       return;
16992     }
16993
16994   mips_gprel_offset = get_absolute_expression ();
16995
16996   demand_empty_rest_of_line ();
16997 }
16998
16999 /* Handle the .gpword pseudo-op.  This is used when generating PIC
17000    code.  It generates a 32 bit GP relative reloc.  */
17001
17002 static void
17003 s_gpword (int ignore ATTRIBUTE_UNUSED)
17004 {
17005   segment_info_type *si;
17006   struct insn_label_list *l;
17007   expressionS ex;
17008   char *p;
17009
17010   /* When not generating PIC code, this is treated as .word.  */
17011   if (mips_pic != SVR4_PIC)
17012     {
17013       s_cons (2);
17014       return;
17015     }
17016
17017   si = seg_info (now_seg);
17018   l = si->label_list;
17019   mips_emit_delays ();
17020   if (auto_align)
17021     mips_align (2, 0, l);
17022
17023   expression (&ex);
17024   mips_clear_insn_labels ();
17025
17026   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17027     {
17028       as_bad (_("Unsupported use of .gpword"));
17029       ignore_rest_of_line ();
17030     }
17031
17032   p = frag_more (4);
17033   md_number_to_chars (p, 0, 4);
17034   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17035                BFD_RELOC_GPREL32);
17036
17037   demand_empty_rest_of_line ();
17038 }
17039
17040 static void
17041 s_gpdword (int ignore ATTRIBUTE_UNUSED)
17042 {
17043   segment_info_type *si;
17044   struct insn_label_list *l;
17045   expressionS ex;
17046   char *p;
17047
17048   /* When not generating PIC code, this is treated as .dword.  */
17049   if (mips_pic != SVR4_PIC)
17050     {
17051       s_cons (3);
17052       return;
17053     }
17054
17055   si = seg_info (now_seg);
17056   l = si->label_list;
17057   mips_emit_delays ();
17058   if (auto_align)
17059     mips_align (3, 0, l);
17060
17061   expression (&ex);
17062   mips_clear_insn_labels ();
17063
17064   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17065     {
17066       as_bad (_("Unsupported use of .gpdword"));
17067       ignore_rest_of_line ();
17068     }
17069
17070   p = frag_more (8);
17071   md_number_to_chars (p, 0, 8);
17072   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17073                BFD_RELOC_GPREL32)->fx_tcbit = 1;
17074
17075   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
17076   fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17077            FALSE, BFD_RELOC_64)->fx_tcbit = 1;
17078
17079   demand_empty_rest_of_line ();
17080 }
17081
17082 /* Handle the .ehword pseudo-op.  This is used when generating unwinding
17083    tables.  It generates a R_MIPS_EH reloc.  */
17084
17085 static void
17086 s_ehword (int ignore ATTRIBUTE_UNUSED)
17087 {
17088   expressionS ex;
17089   char *p;
17090
17091   mips_emit_delays ();
17092
17093   expression (&ex);
17094   mips_clear_insn_labels ();
17095
17096   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17097     {
17098       as_bad (_("Unsupported use of .ehword"));
17099       ignore_rest_of_line ();
17100     }
17101
17102   p = frag_more (4);
17103   md_number_to_chars (p, 0, 4);
17104   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17105                BFD_RELOC_MIPS_EH);
17106
17107   demand_empty_rest_of_line ();
17108 }
17109
17110 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
17111    tables in SVR4 PIC code.  */
17112
17113 static void
17114 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17115 {
17116   int reg;
17117
17118   /* This is ignored when not generating SVR4 PIC code.  */
17119   if (mips_pic != SVR4_PIC)
17120     {
17121       s_ignore (0);
17122       return;
17123     }
17124
17125   mips_mark_labels ();
17126   mips_assembling_insn = TRUE;
17127
17128   /* Add $gp to the register named as an argument.  */
17129   macro_start ();
17130   reg = tc_get_register (0);
17131   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17132   macro_end ();
17133
17134   mips_assembling_insn = FALSE;
17135   demand_empty_rest_of_line ();
17136 }
17137
17138 /* Handle the .insn pseudo-op.  This marks instruction labels in
17139    mips16/micromips mode.  This permits the linker to handle them specially,
17140    such as generating jalx instructions when needed.  We also make
17141    them odd for the duration of the assembly, in order to generate the
17142    right sort of code.  We will make them even in the adjust_symtab
17143    routine, while leaving them marked.  This is convenient for the
17144    debugger and the disassembler.  The linker knows to make them odd
17145    again.  */
17146
17147 static void
17148 s_insn (int ignore ATTRIBUTE_UNUSED)
17149 {
17150   mips_mark_labels ();
17151
17152   demand_empty_rest_of_line ();
17153 }
17154
17155 /* Handle a .stab[snd] directive.  Ideally these directives would be
17156    implemented in a transparent way, so that removing them would not
17157    have any effect on the generated instructions.  However, s_stab
17158    internally changes the section, so in practice we need to decide
17159    now whether the preceding label marks compressed code.  We do not
17160    support changing the compression mode of a label after a .stab*
17161    directive, such as in:
17162
17163    foo:
17164         .stabs ...
17165         .set mips16
17166
17167    so the current mode wins.  */
17168
17169 static void
17170 s_mips_stab (int type)
17171 {
17172   mips_mark_labels ();
17173   s_stab (type);
17174 }
17175
17176 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.  */
17177
17178 static void
17179 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17180 {
17181   char *name;
17182   int c;
17183   symbolS *symbolP;
17184   expressionS exp;
17185
17186   name = input_line_pointer;
17187   c = get_symbol_end ();
17188   symbolP = symbol_find_or_make (name);
17189   S_SET_WEAK (symbolP);
17190   *input_line_pointer = c;
17191
17192   SKIP_WHITESPACE ();
17193
17194   if (! is_end_of_line[(unsigned char) *input_line_pointer])
17195     {
17196       if (S_IS_DEFINED (symbolP))
17197         {
17198           as_bad (_("ignoring attempt to redefine symbol %s"),
17199                   S_GET_NAME (symbolP));
17200           ignore_rest_of_line ();
17201           return;
17202         }
17203
17204       if (*input_line_pointer == ',')
17205         {
17206           ++input_line_pointer;
17207           SKIP_WHITESPACE ();
17208         }
17209
17210       expression (&exp);
17211       if (exp.X_op != O_symbol)
17212         {
17213           as_bad (_("bad .weakext directive"));
17214           ignore_rest_of_line ();
17215           return;
17216         }
17217       symbol_set_value_expression (symbolP, &exp);
17218     }
17219
17220   demand_empty_rest_of_line ();
17221 }
17222
17223 /* Parse a register string into a number.  Called from the ECOFF code
17224    to parse .frame.  The argument is non-zero if this is the frame
17225    register, so that we can record it in mips_frame_reg.  */
17226
17227 int
17228 tc_get_register (int frame)
17229 {
17230   unsigned int reg;
17231
17232   SKIP_WHITESPACE ();
17233   if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17234     reg = 0;
17235   if (frame)
17236     {
17237       mips_frame_reg = reg != 0 ? reg : SP;
17238       mips_frame_reg_valid = 1;
17239       mips_cprestore_valid = 0;
17240     }
17241   return reg;
17242 }
17243
17244 valueT
17245 md_section_align (asection *seg, valueT addr)
17246 {
17247   int align = bfd_get_section_alignment (stdoutput, seg);
17248
17249   /* We don't need to align ELF sections to the full alignment.
17250      However, Irix 5 may prefer that we align them at least to a 16
17251      byte boundary.  We don't bother to align the sections if we
17252      are targeted for an embedded system.  */
17253   if (strncmp (TARGET_OS, "elf", 3) == 0)
17254     return addr;
17255   if (align > 4)
17256     align = 4;
17257
17258   return ((addr + (1 << align) - 1) & (-1 << align));
17259 }
17260
17261 /* Utility routine, called from above as well.  If called while the
17262    input file is still being read, it's only an approximation.  (For
17263    example, a symbol may later become defined which appeared to be
17264    undefined earlier.)  */
17265
17266 static int
17267 nopic_need_relax (symbolS *sym, int before_relaxing)
17268 {
17269   if (sym == 0)
17270     return 0;
17271
17272   if (g_switch_value > 0)
17273     {
17274       const char *symname;
17275       int change;
17276
17277       /* Find out whether this symbol can be referenced off the $gp
17278          register.  It can be if it is smaller than the -G size or if
17279          it is in the .sdata or .sbss section.  Certain symbols can
17280          not be referenced off the $gp, although it appears as though
17281          they can.  */
17282       symname = S_GET_NAME (sym);
17283       if (symname != (const char *) NULL
17284           && (strcmp (symname, "eprol") == 0
17285               || strcmp (symname, "etext") == 0
17286               || strcmp (symname, "_gp") == 0
17287               || strcmp (symname, "edata") == 0
17288               || strcmp (symname, "_fbss") == 0
17289               || strcmp (symname, "_fdata") == 0
17290               || strcmp (symname, "_ftext") == 0
17291               || strcmp (symname, "end") == 0
17292               || strcmp (symname, "_gp_disp") == 0))
17293         change = 1;
17294       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17295                && (0
17296 #ifndef NO_ECOFF_DEBUGGING
17297                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
17298                        && (symbol_get_obj (sym)->ecoff_extern_size
17299                            <= g_switch_value))
17300 #endif
17301                    /* We must defer this decision until after the whole
17302                       file has been read, since there might be a .extern
17303                       after the first use of this symbol.  */
17304                    || (before_relaxing
17305 #ifndef NO_ECOFF_DEBUGGING
17306                        && symbol_get_obj (sym)->ecoff_extern_size == 0
17307 #endif
17308                        && S_GET_VALUE (sym) == 0)
17309                    || (S_GET_VALUE (sym) != 0
17310                        && S_GET_VALUE (sym) <= g_switch_value)))
17311         change = 0;
17312       else
17313         {
17314           const char *segname;
17315
17316           segname = segment_name (S_GET_SEGMENT (sym));
17317           gas_assert (strcmp (segname, ".lit8") != 0
17318                   && strcmp (segname, ".lit4") != 0);
17319           change = (strcmp (segname, ".sdata") != 0
17320                     && strcmp (segname, ".sbss") != 0
17321                     && strncmp (segname, ".sdata.", 7) != 0
17322                     && strncmp (segname, ".sbss.", 6) != 0
17323                     && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17324                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17325         }
17326       return change;
17327     }
17328   else
17329     /* We are not optimizing for the $gp register.  */
17330     return 1;
17331 }
17332
17333
17334 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
17335
17336 static bfd_boolean
17337 pic_need_relax (symbolS *sym, asection *segtype)
17338 {
17339   asection *symsec;
17340
17341   /* Handle the case of a symbol equated to another symbol.  */
17342   while (symbol_equated_reloc_p (sym))
17343     {
17344       symbolS *n;
17345
17346       /* It's possible to get a loop here in a badly written program.  */
17347       n = symbol_get_value_expression (sym)->X_add_symbol;
17348       if (n == sym)
17349         break;
17350       sym = n;
17351     }
17352
17353   if (symbol_section_p (sym))
17354     return TRUE;
17355
17356   symsec = S_GET_SEGMENT (sym);
17357
17358   /* This must duplicate the test in adjust_reloc_syms.  */
17359   return (!bfd_is_und_section (symsec)
17360           && !bfd_is_abs_section (symsec)
17361           && !bfd_is_com_section (symsec)
17362           && !s_is_linkonce (sym, segtype)
17363           /* A global or weak symbol is treated as external.  */
17364           && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
17365 }
17366
17367
17368 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17369    extended opcode.  SEC is the section the frag is in.  */
17370
17371 static int
17372 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17373 {
17374   int type;
17375   const struct mips16_immed_operand *op;
17376   offsetT val;
17377   int mintiny, maxtiny;
17378   segT symsec;
17379   fragS *sym_frag;
17380
17381   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17382     return 0;
17383   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17384     return 1;
17385
17386   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17387   op = mips16_immed_operands;
17388   while (op->type != type)
17389     {
17390       ++op;
17391       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
17392     }
17393
17394   if (op->unsp)
17395     {
17396       if (type == '<' || type == '>' || type == '[' || type == ']')
17397         {
17398           mintiny = 1;
17399           maxtiny = 1 << op->nbits;
17400         }
17401       else
17402         {
17403           mintiny = 0;
17404           maxtiny = (1 << op->nbits) - 1;
17405         }
17406     }
17407   else
17408     {
17409       mintiny = - (1 << (op->nbits - 1));
17410       maxtiny = (1 << (op->nbits - 1)) - 1;
17411     }
17412
17413   sym_frag = symbol_get_frag (fragp->fr_symbol);
17414   val = S_GET_VALUE (fragp->fr_symbol);
17415   symsec = S_GET_SEGMENT (fragp->fr_symbol);
17416
17417   if (op->pcrel)
17418     {
17419       addressT addr;
17420
17421       /* We won't have the section when we are called from
17422          mips_relax_frag.  However, we will always have been called
17423          from md_estimate_size_before_relax first.  If this is a
17424          branch to a different section, we mark it as such.  If SEC is
17425          NULL, and the frag is not marked, then it must be a branch to
17426          the same section.  */
17427       if (sec == NULL)
17428         {
17429           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17430             return 1;
17431         }
17432       else
17433         {
17434           /* Must have been called from md_estimate_size_before_relax.  */
17435           if (symsec != sec)
17436             {
17437               fragp->fr_subtype =
17438                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17439
17440               /* FIXME: We should support this, and let the linker
17441                  catch branches and loads that are out of range.  */
17442               as_bad_where (fragp->fr_file, fragp->fr_line,
17443                             _("unsupported PC relative reference to different section"));
17444
17445               return 1;
17446             }
17447           if (fragp != sym_frag && sym_frag->fr_address == 0)
17448             /* Assume non-extended on the first relaxation pass.
17449                The address we have calculated will be bogus if this is
17450                a forward branch to another frag, as the forward frag
17451                will have fr_address == 0.  */
17452             return 0;
17453         }
17454
17455       /* In this case, we know for sure that the symbol fragment is in
17456          the same section.  If the relax_marker of the symbol fragment
17457          differs from the relax_marker of this fragment, we have not
17458          yet adjusted the symbol fragment fr_address.  We want to add
17459          in STRETCH in order to get a better estimate of the address.
17460          This particularly matters because of the shift bits.  */
17461       if (stretch != 0
17462           && sym_frag->relax_marker != fragp->relax_marker)
17463         {
17464           fragS *f;
17465
17466           /* Adjust stretch for any alignment frag.  Note that if have
17467              been expanding the earlier code, the symbol may be
17468              defined in what appears to be an earlier frag.  FIXME:
17469              This doesn't handle the fr_subtype field, which specifies
17470              a maximum number of bytes to skip when doing an
17471              alignment.  */
17472           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17473             {
17474               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17475                 {
17476                   if (stretch < 0)
17477                     stretch = - ((- stretch)
17478                                  & ~ ((1 << (int) f->fr_offset) - 1));
17479                   else
17480                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17481                   if (stretch == 0)
17482                     break;
17483                 }
17484             }
17485           if (f != NULL)
17486             val += stretch;
17487         }
17488
17489       addr = fragp->fr_address + fragp->fr_fix;
17490
17491       /* The base address rules are complicated.  The base address of
17492          a branch is the following instruction.  The base address of a
17493          PC relative load or add is the instruction itself, but if it
17494          is in a delay slot (in which case it can not be extended) use
17495          the address of the instruction whose delay slot it is in.  */
17496       if (type == 'p' || type == 'q')
17497         {
17498           addr += 2;
17499
17500           /* If we are currently assuming that this frag should be
17501              extended, then, the current address is two bytes
17502              higher.  */
17503           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17504             addr += 2;
17505
17506           /* Ignore the low bit in the target, since it will be set
17507              for a text label.  */
17508           if ((val & 1) != 0)
17509             --val;
17510         }
17511       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17512         addr -= 4;
17513       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17514         addr -= 2;
17515
17516       val -= addr & ~ ((1 << op->shift) - 1);
17517
17518       /* Branch offsets have an implicit 0 in the lowest bit.  */
17519       if (type == 'p' || type == 'q')
17520         val /= 2;
17521
17522       /* If any of the shifted bits are set, we must use an extended
17523          opcode.  If the address depends on the size of this
17524          instruction, this can lead to a loop, so we arrange to always
17525          use an extended opcode.  We only check this when we are in
17526          the main relaxation loop, when SEC is NULL.  */
17527       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17528         {
17529           fragp->fr_subtype =
17530             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17531           return 1;
17532         }
17533
17534       /* If we are about to mark a frag as extended because the value
17535          is precisely maxtiny + 1, then there is a chance of an
17536          infinite loop as in the following code:
17537              la $4,foo
17538              .skip      1020
17539              .align     2
17540            foo:
17541          In this case when the la is extended, foo is 0x3fc bytes
17542          away, so the la can be shrunk, but then foo is 0x400 away, so
17543          the la must be extended.  To avoid this loop, we mark the
17544          frag as extended if it was small, and is about to become
17545          extended with a value of maxtiny + 1.  */
17546       if (val == ((maxtiny + 1) << op->shift)
17547           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17548           && sec == NULL)
17549         {
17550           fragp->fr_subtype =
17551             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17552           return 1;
17553         }
17554     }
17555   else if (symsec != absolute_section && sec != NULL)
17556     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17557
17558   if ((val & ((1 << op->shift) - 1)) != 0
17559       || val < (mintiny << op->shift)
17560       || val > (maxtiny << op->shift))
17561     return 1;
17562   else
17563     return 0;
17564 }
17565
17566 /* Compute the length of a branch sequence, and adjust the
17567    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
17568    worst-case length is computed, with UPDATE being used to indicate
17569    whether an unconditional (-1), branch-likely (+1) or regular (0)
17570    branch is to be computed.  */
17571 static int
17572 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17573 {
17574   bfd_boolean toofar;
17575   int length;
17576
17577   if (fragp
17578       && S_IS_DEFINED (fragp->fr_symbol)
17579       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17580     {
17581       addressT addr;
17582       offsetT val;
17583
17584       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17585
17586       addr = fragp->fr_address + fragp->fr_fix + 4;
17587
17588       val -= addr;
17589
17590       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17591     }
17592   else if (fragp)
17593     /* If the symbol is not defined or it's in a different segment,
17594        assume the user knows what's going on and emit a short
17595        branch.  */
17596     toofar = FALSE;
17597   else
17598     toofar = TRUE;
17599
17600   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17601     fragp->fr_subtype
17602       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17603                              RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17604                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17605                              RELAX_BRANCH_LINK (fragp->fr_subtype),
17606                              toofar);
17607
17608   length = 4;
17609   if (toofar)
17610     {
17611       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17612         length += 8;
17613
17614       if (mips_pic != NO_PIC)
17615         {
17616           /* Additional space for PIC loading of target address.  */
17617           length += 8;
17618           if (mips_opts.isa == ISA_MIPS1)
17619             /* Additional space for $at-stabilizing nop.  */
17620             length += 4;
17621         }
17622
17623       /* If branch is conditional.  */
17624       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17625         length += 8;
17626     }
17627
17628   return length;
17629 }
17630
17631 /* Compute the length of a branch sequence, and adjust the
17632    RELAX_MICROMIPS_TOOFAR32 bit accordingly.  If FRAGP is NULL, the
17633    worst-case length is computed, with UPDATE being used to indicate
17634    whether an unconditional (-1), or regular (0) branch is to be
17635    computed.  */
17636
17637 static int
17638 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17639 {
17640   bfd_boolean toofar;
17641   int length;
17642
17643   if (fragp
17644       && S_IS_DEFINED (fragp->fr_symbol)
17645       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17646     {
17647       addressT addr;
17648       offsetT val;
17649
17650       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17651       /* Ignore the low bit in the target, since it will be set
17652          for a text label.  */
17653       if ((val & 1) != 0)
17654         --val;
17655
17656       addr = fragp->fr_address + fragp->fr_fix + 4;
17657
17658       val -= addr;
17659
17660       toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17661     }
17662   else if (fragp)
17663     /* If the symbol is not defined or it's in a different segment,
17664        assume the user knows what's going on and emit a short
17665        branch.  */
17666     toofar = FALSE;
17667   else
17668     toofar = TRUE;
17669
17670   if (fragp && update
17671       && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17672     fragp->fr_subtype = (toofar
17673                          ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17674                          : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17675
17676   length = 4;
17677   if (toofar)
17678     {
17679       bfd_boolean compact_known = fragp != NULL;
17680       bfd_boolean compact = FALSE;
17681       bfd_boolean uncond;
17682
17683       if (compact_known)
17684         compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17685       if (fragp)
17686         uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17687       else
17688         uncond = update < 0;
17689
17690       /* If label is out of range, we turn branch <br>:
17691
17692                 <br>    label                   # 4 bytes
17693             0:
17694
17695          into:
17696
17697                 j       label                   # 4 bytes
17698                 nop                             # 2 bytes if compact && !PIC
17699             0:
17700        */
17701       if (mips_pic == NO_PIC && (!compact_known || compact))
17702         length += 2;
17703
17704       /* If assembling PIC code, we further turn:
17705
17706                         j       label                   # 4 bytes
17707
17708          into:
17709
17710                         lw/ld   at, %got(label)(gp)     # 4 bytes
17711                         d/addiu at, %lo(label)          # 4 bytes
17712                         jr/c    at                      # 2 bytes
17713        */
17714       if (mips_pic != NO_PIC)
17715         length += 6;
17716
17717       /* If branch <br> is conditional, we prepend negated branch <brneg>:
17718
17719                         <brneg> 0f                      # 4 bytes
17720                         nop                             # 2 bytes if !compact
17721        */
17722       if (!uncond)
17723         length += (compact_known && compact) ? 4 : 6;
17724     }
17725
17726   return length;
17727 }
17728
17729 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17730    bit accordingly.  */
17731
17732 static int
17733 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17734 {
17735   bfd_boolean toofar;
17736
17737   if (fragp
17738       && S_IS_DEFINED (fragp->fr_symbol)
17739       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17740     {
17741       addressT addr;
17742       offsetT val;
17743       int type;
17744
17745       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17746       /* Ignore the low bit in the target, since it will be set
17747          for a text label.  */
17748       if ((val & 1) != 0)
17749         --val;
17750
17751       /* Assume this is a 2-byte branch.  */
17752       addr = fragp->fr_address + fragp->fr_fix + 2;
17753
17754       /* We try to avoid the infinite loop by not adding 2 more bytes for
17755          long branches.  */
17756
17757       val -= addr;
17758
17759       type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17760       if (type == 'D')
17761         toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17762       else if (type == 'E')
17763         toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17764       else
17765         abort ();
17766     }
17767   else
17768     /* If the symbol is not defined or it's in a different segment,
17769        we emit a normal 32-bit branch.  */
17770     toofar = TRUE;
17771
17772   if (fragp && update
17773       && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17774     fragp->fr_subtype
17775       = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17776                : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17777
17778   if (toofar)
17779     return 4;
17780
17781   return 2;
17782 }
17783
17784 /* Estimate the size of a frag before relaxing.  Unless this is the
17785    mips16, we are not really relaxing here, and the final size is
17786    encoded in the subtype information.  For the mips16, we have to
17787    decide whether we are using an extended opcode or not.  */
17788
17789 int
17790 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17791 {
17792   int change;
17793
17794   if (RELAX_BRANCH_P (fragp->fr_subtype))
17795     {
17796
17797       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17798
17799       return fragp->fr_var;
17800     }
17801
17802   if (RELAX_MIPS16_P (fragp->fr_subtype))
17803     /* We don't want to modify the EXTENDED bit here; it might get us
17804        into infinite loops.  We change it only in mips_relax_frag().  */
17805     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
17806
17807   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17808     {
17809       int length = 4;
17810
17811       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17812         length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17813       if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17814         length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17815       fragp->fr_var = length;
17816
17817       return length;
17818     }
17819
17820   if (mips_pic == NO_PIC)
17821     change = nopic_need_relax (fragp->fr_symbol, 0);
17822   else if (mips_pic == SVR4_PIC)
17823     change = pic_need_relax (fragp->fr_symbol, segtype);
17824   else if (mips_pic == VXWORKS_PIC)
17825     /* For vxworks, GOT16 relocations never have a corresponding LO16.  */
17826     change = 0;
17827   else
17828     abort ();
17829
17830   if (change)
17831     {
17832       fragp->fr_subtype |= RELAX_USE_SECOND;
17833       return -RELAX_FIRST (fragp->fr_subtype);
17834     }
17835   else
17836     return -RELAX_SECOND (fragp->fr_subtype);
17837 }
17838
17839 /* This is called to see whether a reloc against a defined symbol
17840    should be converted into a reloc against a section.  */
17841
17842 int
17843 mips_fix_adjustable (fixS *fixp)
17844 {
17845   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17846       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17847     return 0;
17848
17849   if (fixp->fx_addsy == NULL)
17850     return 1;
17851
17852   /* If symbol SYM is in a mergeable section, relocations of the form
17853      SYM + 0 can usually be made section-relative.  The mergeable data
17854      is then identified by the section offset rather than by the symbol.
17855
17856      However, if we're generating REL LO16 relocations, the offset is split
17857      between the LO16 and parterning high part relocation.  The linker will
17858      need to recalculate the complete offset in order to correctly identify
17859      the merge data.
17860
17861      The linker has traditionally not looked for the parterning high part
17862      relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17863      placed anywhere.  Rather than break backwards compatibility by changing
17864      this, it seems better not to force the issue, and instead keep the
17865      original symbol.  This will work with either linker behavior.  */
17866   if ((lo16_reloc_p (fixp->fx_r_type)
17867        || reloc_needs_lo_p (fixp->fx_r_type))
17868       && HAVE_IN_PLACE_ADDENDS
17869       && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17870     return 0;
17871
17872   /* There is no place to store an in-place offset for JALR relocations.
17873      Likewise an in-range offset of limited PC-relative relocations may
17874      overflow the in-place relocatable field if recalculated against the
17875      start address of the symbol's containing section.  */
17876   if (HAVE_IN_PLACE_ADDENDS
17877       && (limited_pcrel_reloc_p (fixp->fx_r_type)
17878           || jalr_reloc_p (fixp->fx_r_type)))
17879     return 0;
17880
17881   /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17882      to a floating-point stub.  The same is true for non-R_MIPS16_26
17883      relocations against MIPS16 functions; in this case, the stub becomes
17884      the function's canonical address.
17885
17886      Floating-point stubs are stored in unique .mips16.call.* or
17887      .mips16.fn.* sections.  If a stub T for function F is in section S,
17888      the first relocation in section S must be against F; this is how the
17889      linker determines the target function.  All relocations that might
17890      resolve to T must also be against F.  We therefore have the following
17891      restrictions, which are given in an intentionally-redundant way:
17892
17893        1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17894           symbols.
17895
17896        2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17897           if that stub might be used.
17898
17899        3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17900           symbols.
17901
17902        4. We cannot reduce a stub's relocations against MIPS16 symbols if
17903           that stub might be used.
17904
17905      There is a further restriction:
17906
17907        5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17908           R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17909           targets with in-place addends; the relocation field cannot
17910           encode the low bit.
17911
17912      For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17913      against a MIPS16 symbol.  We deal with (5) by by not reducing any
17914      such relocations on REL targets.
17915
17916      We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17917      relocation against some symbol R, no relocation against R may be
17918      reduced.  (Note that this deals with (2) as well as (1) because
17919      relocations against global symbols will never be reduced on ELF
17920      targets.)  This approach is a little simpler than trying to detect
17921      stub sections, and gives the "all or nothing" per-symbol consistency
17922      that we have for MIPS16 symbols.  */
17923   if (fixp->fx_subsy == NULL
17924       && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
17925           || *symbol_get_tc (fixp->fx_addsy)
17926           || (HAVE_IN_PLACE_ADDENDS
17927               && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17928               && jmp_reloc_p (fixp->fx_r_type))))
17929     return 0;
17930
17931   return 1;
17932 }
17933
17934 /* Translate internal representation of relocation info to BFD target
17935    format.  */
17936
17937 arelent **
17938 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
17939 {
17940   static arelent *retval[4];
17941   arelent *reloc;
17942   bfd_reloc_code_real_type code;
17943
17944   memset (retval, 0, sizeof(retval));
17945   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
17946   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
17947   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
17948   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17949
17950   if (fixp->fx_pcrel)
17951     {
17952       gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17953                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17954                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17955                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
17956                   || fixp->fx_r_type == BFD_RELOC_32_PCREL);
17957
17958       /* At this point, fx_addnumber is "symbol offset - pcrel address".
17959          Relocations want only the symbol offset.  */
17960       reloc->addend = fixp->fx_addnumber + reloc->address;
17961     }
17962   else
17963     reloc->addend = fixp->fx_addnumber;
17964
17965   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17966      entry to be used in the relocation's section offset.  */
17967   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17968     {
17969       reloc->address = reloc->addend;
17970       reloc->addend = 0;
17971     }
17972
17973   code = fixp->fx_r_type;
17974
17975   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
17976   if (reloc->howto == NULL)
17977     {
17978       as_bad_where (fixp->fx_file, fixp->fx_line,
17979                     _("Can not represent %s relocation in this object file format"),
17980                     bfd_get_reloc_code_name (code));
17981       retval[0] = NULL;
17982     }
17983
17984   return retval;
17985 }
17986
17987 /* Relax a machine dependent frag.  This returns the amount by which
17988    the current size of the frag should change.  */
17989
17990 int
17991 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
17992 {
17993   if (RELAX_BRANCH_P (fragp->fr_subtype))
17994     {
17995       offsetT old_var = fragp->fr_var;
17996
17997       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
17998
17999       return fragp->fr_var - old_var;
18000     }
18001
18002   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18003     {
18004       offsetT old_var = fragp->fr_var;
18005       offsetT new_var = 4;
18006
18007       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18008         new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18009       if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18010         new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18011       fragp->fr_var = new_var;
18012
18013       return new_var - old_var;
18014     }
18015
18016   if (! RELAX_MIPS16_P (fragp->fr_subtype))
18017     return 0;
18018
18019   if (mips16_extended_frag (fragp, NULL, stretch))
18020     {
18021       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18022         return 0;
18023       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18024       return 2;
18025     }
18026   else
18027     {
18028       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18029         return 0;
18030       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18031       return -2;
18032     }
18033
18034   return 0;
18035 }
18036
18037 /* Convert a machine dependent frag.  */
18038
18039 void
18040 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
18041 {
18042   if (RELAX_BRANCH_P (fragp->fr_subtype))
18043     {
18044       char *buf;
18045       unsigned long insn;
18046       expressionS exp;
18047       fixS *fixp;
18048
18049       buf = fragp->fr_literal + fragp->fr_fix;
18050       insn = read_insn (buf);
18051
18052       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18053         {
18054           /* We generate a fixup instead of applying it right now
18055              because, if there are linker relaxations, we're going to
18056              need the relocations.  */
18057           exp.X_op = O_symbol;
18058           exp.X_add_symbol = fragp->fr_symbol;
18059           exp.X_add_number = fragp->fr_offset;
18060
18061           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18062                               BFD_RELOC_16_PCREL_S2);
18063           fixp->fx_file = fragp->fr_file;
18064           fixp->fx_line = fragp->fr_line;
18065
18066           buf = write_insn (buf, insn);
18067         }
18068       else
18069         {
18070           int i;
18071
18072           as_warn_where (fragp->fr_file, fragp->fr_line,
18073                          _("Relaxed out-of-range branch into a jump"));
18074
18075           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18076             goto uncond;
18077
18078           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18079             {
18080               /* Reverse the branch.  */
18081               switch ((insn >> 28) & 0xf)
18082                 {
18083                 case 4:
18084                   /* bc[0-3][tf]l? instructions can have the condition
18085                      reversed by tweaking a single TF bit, and their
18086                      opcodes all have 0x4???????.  */
18087                   gas_assert ((insn & 0xf3e00000) == 0x41000000);
18088                   insn ^= 0x00010000;
18089                   break;
18090
18091                 case 0:
18092                   /* bltz       0x04000000      bgez    0x04010000
18093                      bltzal     0x04100000      bgezal  0x04110000  */
18094                   gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18095                   insn ^= 0x00010000;
18096                   break;
18097
18098                 case 1:
18099                   /* beq        0x10000000      bne     0x14000000
18100                      blez       0x18000000      bgtz    0x1c000000  */
18101                   insn ^= 0x04000000;
18102                   break;
18103
18104                 default:
18105                   abort ();
18106                 }
18107             }
18108
18109           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18110             {
18111               /* Clear the and-link bit.  */
18112               gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18113
18114               /* bltzal         0x04100000      bgezal  0x04110000
18115                  bltzall        0x04120000      bgezall 0x04130000  */
18116               insn &= ~0x00100000;
18117             }
18118
18119           /* Branch over the branch (if the branch was likely) or the
18120              full jump (not likely case).  Compute the offset from the
18121              current instruction to branch to.  */
18122           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18123             i = 16;
18124           else
18125             {
18126               /* How many bytes in instructions we've already emitted?  */
18127               i = buf - fragp->fr_literal - fragp->fr_fix;
18128               /* How many bytes in instructions from here to the end?  */
18129               i = fragp->fr_var - i;
18130             }
18131           /* Convert to instruction count.  */
18132           i >>= 2;
18133           /* Branch counts from the next instruction.  */
18134           i--;
18135           insn |= i;
18136           /* Branch over the jump.  */
18137           buf = write_insn (buf, insn);
18138
18139           /* nop */
18140           buf = write_insn (buf, 0);
18141
18142           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18143             {
18144               /* beql $0, $0, 2f */
18145               insn = 0x50000000;
18146               /* Compute the PC offset from the current instruction to
18147                  the end of the variable frag.  */
18148               /* How many bytes in instructions we've already emitted?  */
18149               i = buf - fragp->fr_literal - fragp->fr_fix;
18150               /* How many bytes in instructions from here to the end?  */
18151               i = fragp->fr_var - i;
18152               /* Convert to instruction count.  */
18153               i >>= 2;
18154               /* Don't decrement i, because we want to branch over the
18155                  delay slot.  */
18156               insn |= i;
18157
18158               buf = write_insn (buf, insn);
18159               buf = write_insn (buf, 0);
18160             }
18161
18162         uncond:
18163           if (mips_pic == NO_PIC)
18164             {
18165               /* j or jal.  */
18166               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18167                       ? 0x0c000000 : 0x08000000);
18168               exp.X_op = O_symbol;
18169               exp.X_add_symbol = fragp->fr_symbol;
18170               exp.X_add_number = fragp->fr_offset;
18171
18172               fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18173                                   FALSE, BFD_RELOC_MIPS_JMP);
18174               fixp->fx_file = fragp->fr_file;
18175               fixp->fx_line = fragp->fr_line;
18176
18177               buf = write_insn (buf, insn);
18178             }
18179           else
18180             {
18181               unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18182
18183               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
18184               insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18185               insn |= at << OP_SH_RT;
18186               exp.X_op = O_symbol;
18187               exp.X_add_symbol = fragp->fr_symbol;
18188               exp.X_add_number = fragp->fr_offset;
18189
18190               if (fragp->fr_offset)
18191                 {
18192                   exp.X_add_symbol = make_expr_symbol (&exp);
18193                   exp.X_add_number = 0;
18194                 }
18195
18196               fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18197                                   FALSE, BFD_RELOC_MIPS_GOT16);
18198               fixp->fx_file = fragp->fr_file;
18199               fixp->fx_line = fragp->fr_line;
18200
18201               buf = write_insn (buf, insn);
18202
18203               if (mips_opts.isa == ISA_MIPS1)
18204                 /* nop */
18205                 buf = write_insn (buf, 0);
18206
18207               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
18208               insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18209               insn |= at << OP_SH_RS | at << OP_SH_RT;
18210
18211               fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18212                                   FALSE, BFD_RELOC_LO16);
18213               fixp->fx_file = fragp->fr_file;
18214               fixp->fx_line = fragp->fr_line;
18215
18216               buf = write_insn (buf, insn);
18217
18218               /* j(al)r $at.  */
18219               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18220                 insn = 0x0000f809;
18221               else
18222                 insn = 0x00000008;
18223               insn |= at << OP_SH_RS;
18224
18225               buf = write_insn (buf, insn);
18226             }
18227         }
18228
18229       fragp->fr_fix += fragp->fr_var;
18230       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18231       return;
18232     }
18233
18234   /* Relax microMIPS branches.  */
18235   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18236     {
18237       char *buf = fragp->fr_literal + fragp->fr_fix;
18238       bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18239       bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18240       int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18241       bfd_boolean short_ds;
18242       unsigned long insn;
18243       expressionS exp;
18244       fixS *fixp;
18245
18246       exp.X_op = O_symbol;
18247       exp.X_add_symbol = fragp->fr_symbol;
18248       exp.X_add_number = fragp->fr_offset;
18249
18250       fragp->fr_fix += fragp->fr_var;
18251
18252       /* Handle 16-bit branches that fit or are forced to fit.  */
18253       if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18254         {
18255           /* We generate a fixup instead of applying it right now,
18256              because if there is linker relaxation, we're going to
18257              need the relocations.  */
18258           if (type == 'D')
18259             fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18260                                 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18261           else if (type == 'E')
18262             fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18263                                 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18264           else
18265             abort ();
18266
18267           fixp->fx_file = fragp->fr_file;
18268           fixp->fx_line = fragp->fr_line;
18269
18270           /* These relocations can have an addend that won't fit in
18271              2 octets.  */
18272           fixp->fx_no_overflow = 1;
18273
18274           return;
18275         }
18276
18277       /* Handle 32-bit branches that fit or are forced to fit.  */
18278       if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18279           || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18280         {
18281           /* We generate a fixup instead of applying it right now,
18282              because if there is linker relaxation, we're going to
18283              need the relocations.  */
18284           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18285                               BFD_RELOC_MICROMIPS_16_PCREL_S1);
18286           fixp->fx_file = fragp->fr_file;
18287           fixp->fx_line = fragp->fr_line;
18288
18289           if (type == 0)
18290             return;
18291         }
18292
18293       /* Relax 16-bit branches to 32-bit branches.  */
18294       if (type != 0)
18295         {
18296           insn = read_compressed_insn (buf, 2);
18297
18298           if ((insn & 0xfc00) == 0xcc00)                /* b16  */
18299             insn = 0x94000000;                          /* beq  */
18300           else if ((insn & 0xdc00) == 0x8c00)           /* beqz16/bnez16  */
18301             {
18302               unsigned long regno;
18303
18304               regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18305               regno = micromips_to_32_reg_d_map [regno];
18306               insn = ((insn & 0x2000) << 16) | 0x94000000;      /* beq/bne  */
18307               insn |= regno << MICROMIPSOP_SH_RS;
18308             }
18309           else
18310             abort ();
18311
18312           /* Nothing else to do, just write it out.  */
18313           if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18314               || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18315             {
18316               buf = write_compressed_insn (buf, insn, 4);
18317               gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18318               return;
18319             }
18320         }
18321       else
18322         insn = read_compressed_insn (buf, 4);
18323
18324       /* Relax 32-bit branches to a sequence of instructions.  */
18325       as_warn_where (fragp->fr_file, fragp->fr_line,
18326                      _("Relaxed out-of-range branch into a jump"));
18327
18328       /* Set the short-delay-slot bit.  */
18329       short_ds = al && (insn & 0x02000000) != 0;
18330
18331       if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18332         {
18333           symbolS *l;
18334
18335           /* Reverse the branch.  */
18336           if ((insn & 0xfc000000) == 0x94000000                 /* beq  */
18337               || (insn & 0xfc000000) == 0xb4000000)             /* bne  */
18338             insn ^= 0x20000000;
18339           else if ((insn & 0xffe00000) == 0x40000000            /* bltz  */
18340                    || (insn & 0xffe00000) == 0x40400000         /* bgez  */
18341                    || (insn & 0xffe00000) == 0x40800000         /* blez  */
18342                    || (insn & 0xffe00000) == 0x40c00000         /* bgtz  */
18343                    || (insn & 0xffe00000) == 0x40a00000         /* bnezc  */
18344                    || (insn & 0xffe00000) == 0x40e00000         /* beqzc  */
18345                    || (insn & 0xffe00000) == 0x40200000         /* bltzal  */
18346                    || (insn & 0xffe00000) == 0x40600000         /* bgezal  */
18347                    || (insn & 0xffe00000) == 0x42200000         /* bltzals  */
18348                    || (insn & 0xffe00000) == 0x42600000)        /* bgezals  */
18349             insn ^= 0x00400000;
18350           else if ((insn & 0xffe30000) == 0x43800000            /* bc1f  */
18351                    || (insn & 0xffe30000) == 0x43a00000         /* bc1t  */
18352                    || (insn & 0xffe30000) == 0x42800000         /* bc2f  */
18353                    || (insn & 0xffe30000) == 0x42a00000)        /* bc2t  */
18354             insn ^= 0x00200000;
18355           else
18356             abort ();
18357
18358           if (al)
18359             {
18360               /* Clear the and-link and short-delay-slot bits.  */
18361               gas_assert ((insn & 0xfda00000) == 0x40200000);
18362
18363               /* bltzal  0x40200000     bgezal  0x40600000  */
18364               /* bltzals 0x42200000     bgezals 0x42600000  */
18365               insn &= ~0x02200000;
18366             }
18367
18368           /* Make a label at the end for use with the branch.  */
18369           l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18370           micromips_label_inc ();
18371           S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18372
18373           /* Refer to it.  */
18374           fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18375                           BFD_RELOC_MICROMIPS_16_PCREL_S1);
18376           fixp->fx_file = fragp->fr_file;
18377           fixp->fx_line = fragp->fr_line;
18378
18379           /* Branch over the jump.  */
18380           buf = write_compressed_insn (buf, insn, 4);
18381           if (!compact)
18382             /* nop */
18383             buf = write_compressed_insn (buf, 0x0c00, 2);
18384         }
18385
18386       if (mips_pic == NO_PIC)
18387         {
18388           unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s  */
18389
18390           /* j/jal/jals <sym>  R_MICROMIPS_26_S1  */
18391           insn = al ? jal : 0xd4000000;
18392
18393           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18394                               BFD_RELOC_MICROMIPS_JMP);
18395           fixp->fx_file = fragp->fr_file;
18396           fixp->fx_line = fragp->fr_line;
18397
18398           buf = write_compressed_insn (buf, insn, 4);
18399           if (compact)
18400             /* nop */
18401             buf = write_compressed_insn (buf, 0x0c00, 2);
18402         }
18403       else
18404         {
18405           unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18406           unsigned long jalr = short_ds ? 0x45e0 : 0x45c0;      /* jalr/s  */
18407           unsigned long jr = compact ? 0x45a0 : 0x4580;         /* jr/c  */
18408
18409           /* lw/ld $at, <sym>($gp)  R_MICROMIPS_GOT16  */
18410           insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18411           insn |= at << MICROMIPSOP_SH_RT;
18412
18413           if (exp.X_add_number)
18414             {
18415               exp.X_add_symbol = make_expr_symbol (&exp);
18416               exp.X_add_number = 0;
18417             }
18418
18419           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18420                               BFD_RELOC_MICROMIPS_GOT16);
18421           fixp->fx_file = fragp->fr_file;
18422           fixp->fx_line = fragp->fr_line;
18423
18424           buf = write_compressed_insn (buf, insn, 4);
18425
18426           /* d/addiu $at, $at, <sym>  R_MICROMIPS_LO16  */
18427           insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18428           insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18429
18430           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18431                               BFD_RELOC_MICROMIPS_LO16);
18432           fixp->fx_file = fragp->fr_file;
18433           fixp->fx_line = fragp->fr_line;
18434
18435           buf = write_compressed_insn (buf, insn, 4);
18436
18437           /* jr/jrc/jalr/jalrs $at  */
18438           insn = al ? jalr : jr;
18439           insn |= at << MICROMIPSOP_SH_MJ;
18440
18441           buf = write_compressed_insn (buf, insn, 2);
18442         }
18443
18444       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18445       return;
18446     }
18447
18448   if (RELAX_MIPS16_P (fragp->fr_subtype))
18449     {
18450       int type;
18451       const struct mips16_immed_operand *op;
18452       offsetT val;
18453       char *buf;
18454       unsigned int user_length, length;
18455       unsigned long insn;
18456       bfd_boolean ext;
18457
18458       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18459       op = mips16_immed_operands;
18460       while (op->type != type)
18461         ++op;
18462
18463       ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18464       val = resolve_symbol_value (fragp->fr_symbol);
18465       if (op->pcrel)
18466         {
18467           addressT addr;
18468
18469           addr = fragp->fr_address + fragp->fr_fix;
18470
18471           /* The rules for the base address of a PC relative reloc are
18472              complicated; see mips16_extended_frag.  */
18473           if (type == 'p' || type == 'q')
18474             {
18475               addr += 2;
18476               if (ext)
18477                 addr += 2;
18478               /* Ignore the low bit in the target, since it will be
18479                  set for a text label.  */
18480               if ((val & 1) != 0)
18481                 --val;
18482             }
18483           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18484             addr -= 4;
18485           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18486             addr -= 2;
18487
18488           addr &= ~ (addressT) ((1 << op->shift) - 1);
18489           val -= addr;
18490
18491           /* Make sure the section winds up with the alignment we have
18492              assumed.  */
18493           if (op->shift > 0)
18494             record_alignment (asec, op->shift);
18495         }
18496
18497       if (ext
18498           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18499               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18500         as_warn_where (fragp->fr_file, fragp->fr_line,
18501                        _("extended instruction in delay slot"));
18502
18503       buf = fragp->fr_literal + fragp->fr_fix;
18504
18505       insn = read_compressed_insn (buf, 2);
18506       if (ext)
18507         insn |= MIPS16_EXTEND;
18508
18509       if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18510         user_length = 4;
18511       else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18512         user_length = 2;
18513       else
18514         user_length = 0;
18515
18516       mips16_immed (fragp->fr_file, fragp->fr_line, type,
18517                     BFD_RELOC_UNUSED, val, user_length, &insn);
18518
18519       length = (ext ? 4 : 2);
18520       gas_assert (mips16_opcode_length (insn) == length);
18521       write_compressed_insn (buf, insn, length);
18522       fragp->fr_fix += length;
18523     }
18524   else
18525     {
18526       relax_substateT subtype = fragp->fr_subtype;
18527       bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18528       bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18529       int first, second;
18530       fixS *fixp;
18531
18532       first = RELAX_FIRST (subtype);
18533       second = RELAX_SECOND (subtype);
18534       fixp = (fixS *) fragp->fr_opcode;
18535
18536       /* If the delay slot chosen does not match the size of the instruction,
18537          then emit a warning.  */
18538       if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18539            || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18540         {
18541           relax_substateT s;
18542           const char *msg;
18543
18544           s = subtype & (RELAX_DELAY_SLOT_16BIT
18545                          | RELAX_DELAY_SLOT_SIZE_FIRST
18546                          | RELAX_DELAY_SLOT_SIZE_SECOND);
18547           msg = macro_warning (s);
18548           if (msg != NULL)
18549             as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18550           subtype &= ~s;
18551         }
18552
18553       /* Possibly emit a warning if we've chosen the longer option.  */
18554       if (use_second == second_longer)
18555         {
18556           relax_substateT s;
18557           const char *msg;
18558
18559           s = (subtype
18560                & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18561           msg = macro_warning (s);
18562           if (msg != NULL)
18563             as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18564           subtype &= ~s;
18565         }
18566
18567       /* Go through all the fixups for the first sequence.  Disable them
18568          (by marking them as done) if we're going to use the second
18569          sequence instead.  */
18570       while (fixp
18571              && fixp->fx_frag == fragp
18572              && fixp->fx_where < fragp->fr_fix - second)
18573         {
18574           if (subtype & RELAX_USE_SECOND)
18575             fixp->fx_done = 1;
18576           fixp = fixp->fx_next;
18577         }
18578
18579       /* Go through the fixups for the second sequence.  Disable them if
18580          we're going to use the first sequence, otherwise adjust their
18581          addresses to account for the relaxation.  */
18582       while (fixp && fixp->fx_frag == fragp)
18583         {
18584           if (subtype & RELAX_USE_SECOND)
18585             fixp->fx_where -= first;
18586           else
18587             fixp->fx_done = 1;
18588           fixp = fixp->fx_next;
18589         }
18590
18591       /* Now modify the frag contents.  */
18592       if (subtype & RELAX_USE_SECOND)
18593         {
18594           char *start;
18595
18596           start = fragp->fr_literal + fragp->fr_fix - first - second;
18597           memmove (start, start + first, second);
18598           fragp->fr_fix -= first;
18599         }
18600       else
18601         fragp->fr_fix -= second;
18602     }
18603 }
18604
18605 /* This function is called after the relocs have been generated.
18606    We've been storing mips16 text labels as odd.  Here we convert them
18607    back to even for the convenience of the debugger.  */
18608
18609 void
18610 mips_frob_file_after_relocs (void)
18611 {
18612   asymbol **syms;
18613   unsigned int count, i;
18614
18615   syms = bfd_get_outsymbols (stdoutput);
18616   count = bfd_get_symcount (stdoutput);
18617   for (i = 0; i < count; i++, syms++)
18618     if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18619         && ((*syms)->value & 1) != 0)
18620       {
18621         (*syms)->value &= ~1;
18622         /* If the symbol has an odd size, it was probably computed
18623            incorrectly, so adjust that as well.  */
18624         if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18625           ++elf_symbol (*syms)->internal_elf_sym.st_size;
18626       }
18627 }
18628
18629 /* This function is called whenever a label is defined, including fake
18630    labels instantiated off the dot special symbol.  It is used when
18631    handling branch delays; if a branch has a label, we assume we cannot
18632    move it.  This also bumps the value of the symbol by 1 in compressed
18633    code.  */
18634
18635 static void
18636 mips_record_label (symbolS *sym)
18637 {
18638   segment_info_type *si = seg_info (now_seg);
18639   struct insn_label_list *l;
18640
18641   if (free_insn_labels == NULL)
18642     l = (struct insn_label_list *) xmalloc (sizeof *l);
18643   else
18644     {
18645       l = free_insn_labels;
18646       free_insn_labels = l->next;
18647     }
18648
18649   l->label = sym;
18650   l->next = si->label_list;
18651   si->label_list = l;
18652 }
18653
18654 /* This function is called as tc_frob_label() whenever a label is defined
18655    and adds a DWARF-2 record we only want for true labels.  */
18656
18657 void
18658 mips_define_label (symbolS *sym)
18659 {
18660   mips_record_label (sym);
18661   dwarf2_emit_label (sym);
18662 }
18663
18664 /* This function is called by tc_new_dot_label whenever a new dot symbol
18665    is defined.  */
18666
18667 void
18668 mips_add_dot_label (symbolS *sym)
18669 {
18670   mips_record_label (sym);
18671   if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18672     mips_compressed_mark_label (sym);
18673 }
18674 \f
18675 /* Some special processing for a MIPS ELF file.  */
18676
18677 void
18678 mips_elf_final_processing (void)
18679 {
18680   /* Write out the register information.  */
18681   if (mips_abi != N64_ABI)
18682     {
18683       Elf32_RegInfo s;
18684
18685       s.ri_gprmask = mips_gprmask;
18686       s.ri_cprmask[0] = mips_cprmask[0];
18687       s.ri_cprmask[1] = mips_cprmask[1];
18688       s.ri_cprmask[2] = mips_cprmask[2];
18689       s.ri_cprmask[3] = mips_cprmask[3];
18690       /* The gp_value field is set by the MIPS ELF backend.  */
18691
18692       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18693                                        ((Elf32_External_RegInfo *)
18694                                         mips_regmask_frag));
18695     }
18696   else
18697     {
18698       Elf64_Internal_RegInfo s;
18699
18700       s.ri_gprmask = mips_gprmask;
18701       s.ri_pad = 0;
18702       s.ri_cprmask[0] = mips_cprmask[0];
18703       s.ri_cprmask[1] = mips_cprmask[1];
18704       s.ri_cprmask[2] = mips_cprmask[2];
18705       s.ri_cprmask[3] = mips_cprmask[3];
18706       /* The gp_value field is set by the MIPS ELF backend.  */
18707
18708       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18709                                        ((Elf64_External_RegInfo *)
18710                                         mips_regmask_frag));
18711     }
18712
18713   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
18714      sort of BFD interface for this.  */
18715   if (mips_any_noreorder)
18716     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18717   if (mips_pic != NO_PIC)
18718     {
18719       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
18720       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18721     }
18722   if (mips_abicalls)
18723     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18724
18725   /* Set MIPS ELF flags for ASEs.  Note that not all ASEs have flags
18726      defined at present; this might need to change in future.  */
18727   if (file_ase_mips16)
18728     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
18729   if (file_ase_micromips)
18730     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
18731   if (file_ase & ASE_MDMX)
18732     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
18733
18734   /* Set the MIPS ELF ABI flags.  */
18735   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
18736     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
18737   else if (mips_abi == O64_ABI)
18738     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
18739   else if (mips_abi == EABI_ABI)
18740     {
18741       if (!file_mips_gp32)
18742         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18743       else
18744         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18745     }
18746   else if (mips_abi == N32_ABI)
18747     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18748
18749   /* Nothing to do for N64_ABI.  */
18750
18751   if (mips_32bitmode)
18752     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
18753
18754 #if 0 /* XXX FIXME */
18755   /* 32 bit code with 64 bit FP registers.  */
18756   if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18757     elf_elfheader (stdoutput)->e_flags |= ???;
18758 #endif
18759 }
18760 \f
18761 typedef struct proc {
18762   symbolS *func_sym;
18763   symbolS *func_end_sym;
18764   unsigned long reg_mask;
18765   unsigned long reg_offset;
18766   unsigned long fpreg_mask;
18767   unsigned long fpreg_offset;
18768   unsigned long frame_offset;
18769   unsigned long frame_reg;
18770   unsigned long pc_reg;
18771 } procS;
18772
18773 static procS cur_proc;
18774 static procS *cur_proc_ptr;
18775 static int numprocs;
18776
18777 /* Implement NOP_OPCODE.  We encode a MIPS16 nop as "1", a microMIPS nop
18778    as "2", and a normal nop as "0".  */
18779
18780 #define NOP_OPCODE_MIPS         0
18781 #define NOP_OPCODE_MIPS16       1
18782 #define NOP_OPCODE_MICROMIPS    2
18783
18784 char
18785 mips_nop_opcode (void)
18786 {
18787   if (seg_info (now_seg)->tc_segment_info_data.micromips)
18788     return NOP_OPCODE_MICROMIPS;
18789   else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18790     return NOP_OPCODE_MIPS16;
18791   else
18792     return NOP_OPCODE_MIPS;
18793 }
18794
18795 /* Fill in an rs_align_code fragment.  Unlike elsewhere we want to use
18796    32-bit microMIPS NOPs here (if applicable).  */
18797
18798 void
18799 mips_handle_align (fragS *fragp)
18800 {
18801   char nop_opcode;
18802   char *p;
18803   int bytes, size, excess;
18804   valueT opcode;
18805
18806   if (fragp->fr_type != rs_align_code)
18807     return;
18808
18809   p = fragp->fr_literal + fragp->fr_fix;
18810   nop_opcode = *p;
18811   switch (nop_opcode)
18812     {
18813     case NOP_OPCODE_MICROMIPS:
18814       opcode = micromips_nop32_insn.insn_opcode;
18815       size = 4;
18816       break;
18817     case NOP_OPCODE_MIPS16:
18818       opcode = mips16_nop_insn.insn_opcode;
18819       size = 2;
18820       break;
18821     case NOP_OPCODE_MIPS:
18822     default:
18823       opcode = nop_insn.insn_opcode;
18824       size = 4;
18825       break;
18826     }
18827
18828   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18829   excess = bytes % size;
18830
18831   /* Handle the leading part if we're not inserting a whole number of
18832      instructions, and make it the end of the fixed part of the frag.
18833      Try to fit in a short microMIPS NOP if applicable and possible,
18834      and use zeroes otherwise.  */
18835   gas_assert (excess < 4);
18836   fragp->fr_fix += excess;
18837   switch (excess)
18838     {
18839     case 3:
18840       *p++ = '\0';
18841       /* Fall through.  */
18842     case 2:
18843       if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
18844         {
18845           p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
18846           break;
18847         }
18848       *p++ = '\0';
18849       /* Fall through.  */
18850     case 1:
18851       *p++ = '\0';
18852       /* Fall through.  */
18853     case 0:
18854       break;
18855     }
18856
18857   md_number_to_chars (p, opcode, size);
18858   fragp->fr_var = size;
18859 }
18860
18861 static void
18862 md_obj_begin (void)
18863 {
18864 }
18865
18866 static void
18867 md_obj_end (void)
18868 {
18869   /* Check for premature end, nesting errors, etc.  */
18870   if (cur_proc_ptr)
18871     as_warn (_("missing .end at end of assembly"));
18872 }
18873
18874 static long
18875 get_number (void)
18876 {
18877   int negative = 0;
18878   long val = 0;
18879
18880   if (*input_line_pointer == '-')
18881     {
18882       ++input_line_pointer;
18883       negative = 1;
18884     }
18885   if (!ISDIGIT (*input_line_pointer))
18886     as_bad (_("expected simple number"));
18887   if (input_line_pointer[0] == '0')
18888     {
18889       if (input_line_pointer[1] == 'x')
18890         {
18891           input_line_pointer += 2;
18892           while (ISXDIGIT (*input_line_pointer))
18893             {
18894               val <<= 4;
18895               val |= hex_value (*input_line_pointer++);
18896             }
18897           return negative ? -val : val;
18898         }
18899       else
18900         {
18901           ++input_line_pointer;
18902           while (ISDIGIT (*input_line_pointer))
18903             {
18904               val <<= 3;
18905               val |= *input_line_pointer++ - '0';
18906             }
18907           return negative ? -val : val;
18908         }
18909     }
18910   if (!ISDIGIT (*input_line_pointer))
18911     {
18912       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18913               *input_line_pointer, *input_line_pointer);
18914       as_warn (_("invalid number"));
18915       return -1;
18916     }
18917   while (ISDIGIT (*input_line_pointer))
18918     {
18919       val *= 10;
18920       val += *input_line_pointer++ - '0';
18921     }
18922   return negative ? -val : val;
18923 }
18924
18925 /* The .file directive; just like the usual .file directive, but there
18926    is an initial number which is the ECOFF file index.  In the non-ECOFF
18927    case .file implies DWARF-2.  */
18928
18929 static void
18930 s_mips_file (int x ATTRIBUTE_UNUSED)
18931 {
18932   static int first_file_directive = 0;
18933
18934   if (ECOFF_DEBUGGING)
18935     {
18936       get_number ();
18937       s_app_file (0);
18938     }
18939   else
18940     {
18941       char *filename;
18942
18943       filename = dwarf2_directive_file (0);
18944
18945       /* Versions of GCC up to 3.1 start files with a ".file"
18946          directive even for stabs output.  Make sure that this
18947          ".file" is handled.  Note that you need a version of GCC
18948          after 3.1 in order to support DWARF-2 on MIPS.  */
18949       if (filename != NULL && ! first_file_directive)
18950         {
18951           (void) new_logical_line (filename, -1);
18952           s_app_file_string (filename, 0);
18953         }
18954       first_file_directive = 1;
18955     }
18956 }
18957
18958 /* The .loc directive, implying DWARF-2.  */
18959
18960 static void
18961 s_mips_loc (int x ATTRIBUTE_UNUSED)
18962 {
18963   if (!ECOFF_DEBUGGING)
18964     dwarf2_directive_loc (0);
18965 }
18966
18967 /* The .end directive.  */
18968
18969 static void
18970 s_mips_end (int x ATTRIBUTE_UNUSED)
18971 {
18972   symbolS *p;
18973
18974   /* Following functions need their own .frame and .cprestore directives.  */
18975   mips_frame_reg_valid = 0;
18976   mips_cprestore_valid = 0;
18977
18978   if (!is_end_of_line[(unsigned char) *input_line_pointer])
18979     {
18980       p = get_symbol ();
18981       demand_empty_rest_of_line ();
18982     }
18983   else
18984     p = NULL;
18985
18986   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
18987     as_warn (_(".end not in text section"));
18988
18989   if (!cur_proc_ptr)
18990     {
18991       as_warn (_(".end directive without a preceding .ent directive."));
18992       demand_empty_rest_of_line ();
18993       return;
18994     }
18995
18996   if (p != NULL)
18997     {
18998       gas_assert (S_GET_NAME (p));
18999       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
19000         as_warn (_(".end symbol does not match .ent symbol."));
19001
19002       if (debug_type == DEBUG_STABS)
19003         stabs_generate_asm_endfunc (S_GET_NAME (p),
19004                                     S_GET_NAME (p));
19005     }
19006   else
19007     as_warn (_(".end directive missing or unknown symbol"));
19008
19009   /* Create an expression to calculate the size of the function.  */
19010   if (p && cur_proc_ptr)
19011     {
19012       OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19013       expressionS *exp = xmalloc (sizeof (expressionS));
19014
19015       obj->size = exp;
19016       exp->X_op = O_subtract;
19017       exp->X_add_symbol = symbol_temp_new_now ();
19018       exp->X_op_symbol = p;
19019       exp->X_add_number = 0;
19020
19021       cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19022     }
19023
19024   /* Generate a .pdr section.  */
19025   if (!ECOFF_DEBUGGING && mips_flag_pdr)
19026     {
19027       segT saved_seg = now_seg;
19028       subsegT saved_subseg = now_subseg;
19029       expressionS exp;
19030       char *fragp;
19031
19032 #ifdef md_flush_pending_output
19033       md_flush_pending_output ();
19034 #endif
19035
19036       gas_assert (pdr_seg);
19037       subseg_set (pdr_seg, 0);
19038
19039       /* Write the symbol.  */
19040       exp.X_op = O_symbol;
19041       exp.X_add_symbol = p;
19042       exp.X_add_number = 0;
19043       emit_expr (&exp, 4);
19044
19045       fragp = frag_more (7 * 4);
19046
19047       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19048       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19049       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19050       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19051       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19052       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19053       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19054
19055       subseg_set (saved_seg, saved_subseg);
19056     }
19057
19058   cur_proc_ptr = NULL;
19059 }
19060
19061 /* The .aent and .ent directives.  */
19062
19063 static void
19064 s_mips_ent (int aent)
19065 {
19066   symbolS *symbolP;
19067
19068   symbolP = get_symbol ();
19069   if (*input_line_pointer == ',')
19070     ++input_line_pointer;
19071   SKIP_WHITESPACE ();
19072   if (ISDIGIT (*input_line_pointer)
19073       || *input_line_pointer == '-')
19074     get_number ();
19075
19076   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19077     as_warn (_(".ent or .aent not in text section."));
19078
19079   if (!aent && cur_proc_ptr)
19080     as_warn (_("missing .end"));
19081
19082   if (!aent)
19083     {
19084       /* This function needs its own .frame and .cprestore directives.  */
19085       mips_frame_reg_valid = 0;
19086       mips_cprestore_valid = 0;
19087
19088       cur_proc_ptr = &cur_proc;
19089       memset (cur_proc_ptr, '\0', sizeof (procS));
19090
19091       cur_proc_ptr->func_sym = symbolP;
19092
19093       ++numprocs;
19094
19095       if (debug_type == DEBUG_STABS)
19096         stabs_generate_asm_func (S_GET_NAME (symbolP),
19097                                  S_GET_NAME (symbolP));
19098     }
19099
19100   symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19101
19102   demand_empty_rest_of_line ();
19103 }
19104
19105 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19106    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19107    s_mips_frame is used so that we can set the PDR information correctly.
19108    We can't use the ecoff routines because they make reference to the ecoff
19109    symbol table (in the mdebug section).  */
19110
19111 static void
19112 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19113 {
19114   if (ECOFF_DEBUGGING)
19115     s_ignore (ignore);
19116   else
19117     {
19118       long val;
19119
19120       if (cur_proc_ptr == (procS *) NULL)
19121         {
19122           as_warn (_(".frame outside of .ent"));
19123           demand_empty_rest_of_line ();
19124           return;
19125         }
19126
19127       cur_proc_ptr->frame_reg = tc_get_register (1);
19128
19129       SKIP_WHITESPACE ();
19130       if (*input_line_pointer++ != ','
19131           || get_absolute_expression_and_terminator (&val) != ',')
19132         {
19133           as_warn (_("Bad .frame directive"));
19134           --input_line_pointer;
19135           demand_empty_rest_of_line ();
19136           return;
19137         }
19138
19139       cur_proc_ptr->frame_offset = val;
19140       cur_proc_ptr->pc_reg = tc_get_register (0);
19141
19142       demand_empty_rest_of_line ();
19143     }
19144 }
19145
19146 /* The .fmask and .mask directives. If the mdebug section is present
19147    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19148    embedded targets, s_mips_mask is used so that we can set the PDR
19149    information correctly. We can't use the ecoff routines because they
19150    make reference to the ecoff symbol table (in the mdebug section).  */
19151
19152 static void
19153 s_mips_mask (int reg_type)
19154 {
19155   if (ECOFF_DEBUGGING)
19156     s_ignore (reg_type);
19157   else
19158     {
19159       long mask, off;
19160
19161       if (cur_proc_ptr == (procS *) NULL)
19162         {
19163           as_warn (_(".mask/.fmask outside of .ent"));
19164           demand_empty_rest_of_line ();
19165           return;
19166         }
19167
19168       if (get_absolute_expression_and_terminator (&mask) != ',')
19169         {
19170           as_warn (_("Bad .mask/.fmask directive"));
19171           --input_line_pointer;
19172           demand_empty_rest_of_line ();
19173           return;
19174         }
19175
19176       off = get_absolute_expression ();
19177
19178       if (reg_type == 'F')
19179         {
19180           cur_proc_ptr->fpreg_mask = mask;
19181           cur_proc_ptr->fpreg_offset = off;
19182         }
19183       else
19184         {
19185           cur_proc_ptr->reg_mask = mask;
19186           cur_proc_ptr->reg_offset = off;
19187         }
19188
19189       demand_empty_rest_of_line ();
19190     }
19191 }
19192
19193 /* A table describing all the processors gas knows about.  Names are
19194    matched in the order listed.
19195
19196    To ease comparison, please keep this table in the same order as
19197    gcc's mips_cpu_info_table[].  */
19198 static const struct mips_cpu_info mips_cpu_info_table[] =
19199 {
19200   /* Entries for generic ISAs */
19201   { "mips1",          MIPS_CPU_IS_ISA, 0,       ISA_MIPS1,    CPU_R3000 },
19202   { "mips2",          MIPS_CPU_IS_ISA, 0,       ISA_MIPS2,    CPU_R6000 },
19203   { "mips3",          MIPS_CPU_IS_ISA, 0,       ISA_MIPS3,    CPU_R4000 },
19204   { "mips4",          MIPS_CPU_IS_ISA, 0,       ISA_MIPS4,    CPU_R8000 },
19205   { "mips5",          MIPS_CPU_IS_ISA, 0,       ISA_MIPS5,    CPU_MIPS5 },
19206   { "mips32",         MIPS_CPU_IS_ISA, 0,       ISA_MIPS32,   CPU_MIPS32 },
19207   { "mips32r2",       MIPS_CPU_IS_ISA, 0,       ISA_MIPS32R2, CPU_MIPS32R2 },
19208   { "mips64",         MIPS_CPU_IS_ISA, 0,       ISA_MIPS64,   CPU_MIPS64 },
19209   { "mips64r2",       MIPS_CPU_IS_ISA, 0,       ISA_MIPS64R2, CPU_MIPS64R2 },
19210
19211   /* MIPS I */
19212   { "r3000",          0, 0,                     ISA_MIPS1,    CPU_R3000 },
19213   { "r2000",          0, 0,                     ISA_MIPS1,    CPU_R3000 },
19214   { "r3900",          0, 0,                     ISA_MIPS1,    CPU_R3900 },
19215
19216   /* MIPS II */
19217   { "r6000",          0, 0,                     ISA_MIPS2,    CPU_R6000 },
19218
19219   /* MIPS III */
19220   { "r4000",          0, 0,                     ISA_MIPS3,    CPU_R4000 },
19221   { "r4010",          0, 0,                     ISA_MIPS2,    CPU_R4010 },
19222   { "vr4100",         0, 0,                     ISA_MIPS3,    CPU_VR4100 },
19223   { "vr4111",         0, 0,                     ISA_MIPS3,    CPU_R4111 },
19224   { "vr4120",         0, 0,                     ISA_MIPS3,    CPU_VR4120 },
19225   { "vr4130",         0, 0,                     ISA_MIPS3,    CPU_VR4120 },
19226   { "vr4181",         0, 0,                     ISA_MIPS3,    CPU_R4111 },
19227   { "vr4300",         0, 0,                     ISA_MIPS3,    CPU_R4300 },
19228   { "r4400",          0, 0,                     ISA_MIPS3,    CPU_R4400 },
19229   { "r4600",          0, 0,                     ISA_MIPS3,    CPU_R4600 },
19230   { "orion",          0, 0,                     ISA_MIPS3,    CPU_R4600 },
19231   { "r4650",          0, 0,                     ISA_MIPS3,    CPU_R4650 },
19232   { "r5900",          0, 0,                     ISA_MIPS3,    CPU_R5900 },
19233   /* ST Microelectronics Loongson 2E and 2F cores */
19234   { "loongson2e",     0, 0,                     ISA_MIPS3,    CPU_LOONGSON_2E },
19235   { "loongson2f",     0, 0,                     ISA_MIPS3,    CPU_LOONGSON_2F },
19236
19237   /* MIPS IV */
19238   { "r8000",          0, 0,                     ISA_MIPS4,    CPU_R8000 },
19239   { "r10000",         0, 0,                     ISA_MIPS4,    CPU_R10000 },
19240   { "r12000",         0, 0,                     ISA_MIPS4,    CPU_R12000 },
19241   { "r14000",         0, 0,                     ISA_MIPS4,    CPU_R14000 },
19242   { "r16000",         0, 0,                     ISA_MIPS4,    CPU_R16000 },
19243   { "vr5000",         0, 0,                     ISA_MIPS4,    CPU_R5000 },
19244   { "vr5400",         0, 0,                     ISA_MIPS4,    CPU_VR5400 },
19245   { "vr5500",         0, 0,                     ISA_MIPS4,    CPU_VR5500 },
19246   { "rm5200",         0, 0,                     ISA_MIPS4,    CPU_R5000 },
19247   { "rm5230",         0, 0,                     ISA_MIPS4,    CPU_R5000 },
19248   { "rm5231",         0, 0,                     ISA_MIPS4,    CPU_R5000 },
19249   { "rm5261",         0, 0,                     ISA_MIPS4,    CPU_R5000 },
19250   { "rm5721",         0, 0,                     ISA_MIPS4,    CPU_R5000 },
19251   { "rm7000",         0, 0,                     ISA_MIPS4,    CPU_RM7000 },
19252   { "rm9000",         0, 0,                     ISA_MIPS4,    CPU_RM9000 },
19253
19254   /* MIPS 32 */
19255   { "4kc",            0, 0,                     ISA_MIPS32,   CPU_MIPS32 },
19256   { "4km",            0, 0,                     ISA_MIPS32,   CPU_MIPS32 },
19257   { "4kp",            0, 0,                     ISA_MIPS32,   CPU_MIPS32 },
19258   { "4ksc",           0, ASE_SMARTMIPS,         ISA_MIPS32,   CPU_MIPS32 },
19259
19260   /* MIPS 32 Release 2 */
19261   { "4kec",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
19262   { "4kem",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
19263   { "4kep",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
19264   { "4ksd",           0, ASE_SMARTMIPS,         ISA_MIPS32R2, CPU_MIPS32R2 },
19265   { "m4k",            0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
19266   { "m4kp",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
19267   { "m14k",           0, ASE_MCU,               ISA_MIPS32R2, CPU_MIPS32R2 },
19268   { "m14kc",          0, ASE_MCU,               ISA_MIPS32R2, CPU_MIPS32R2 },
19269   { "m14ke",          0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19270                                                 ISA_MIPS32R2, CPU_MIPS32R2 },
19271   { "m14kec",         0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19272                                                 ISA_MIPS32R2, CPU_MIPS32R2 },
19273   { "24kc",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
19274   { "24kf2_1",        0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
19275   { "24kf",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
19276   { "24kf1_1",        0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
19277   /* Deprecated forms of the above.  */
19278   { "24kfx",          0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
19279   { "24kx",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
19280   /* 24KE is a 24K with DSP ASE, other ASEs are optional.  */
19281   { "24kec",          0, ASE_DSP,               ISA_MIPS32R2, CPU_MIPS32R2 },
19282   { "24kef2_1",       0, ASE_DSP,               ISA_MIPS32R2, CPU_MIPS32R2 },
19283   { "24kef",          0, ASE_DSP,               ISA_MIPS32R2, CPU_MIPS32R2 },
19284   { "24kef1_1",       0, ASE_DSP,               ISA_MIPS32R2, CPU_MIPS32R2 },
19285   /* Deprecated forms of the above.  */
19286   { "24kefx",         0, ASE_DSP,               ISA_MIPS32R2, CPU_MIPS32R2 },
19287   { "24kex",          0, ASE_DSP,               ISA_MIPS32R2, CPU_MIPS32R2 },
19288   /* 34K is a 24K with DSP and MT ASE, other ASEs are optional.  */
19289   { "34kc",           0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
19290   { "34kf2_1",        0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
19291   { "34kf",           0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
19292   { "34kf1_1",        0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
19293   /* Deprecated forms of the above.  */
19294   { "34kfx",          0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
19295   { "34kx",           0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
19296   /* 34Kn is a 34kc without DSP.  */
19297   { "34kn",           0, ASE_MT,                ISA_MIPS32R2, CPU_MIPS32R2 },
19298   /* 74K with DSP and DSPR2 ASE, other ASEs are optional.  */
19299   { "74kc",           0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
19300   { "74kf2_1",        0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
19301   { "74kf",           0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
19302   { "74kf1_1",        0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
19303   { "74kf3_2",        0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
19304   /* Deprecated forms of the above.  */
19305   { "74kfx",          0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
19306   { "74kx",           0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
19307   /* 1004K cores are multiprocessor versions of the 34K.  */
19308   { "1004kc",         0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
19309   { "1004kf2_1",      0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
19310   { "1004kf",         0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
19311   { "1004kf1_1",      0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
19312
19313   /* MIPS 64 */
19314   { "5kc",            0, 0,                     ISA_MIPS64,   CPU_MIPS64 },
19315   { "5kf",            0, 0,                     ISA_MIPS64,   CPU_MIPS64 },
19316   { "20kc",           0, ASE_MIPS3D,            ISA_MIPS64,   CPU_MIPS64 },
19317   { "25kf",           0, ASE_MIPS3D,            ISA_MIPS64,   CPU_MIPS64 },
19318
19319   /* Broadcom SB-1 CPU core */
19320   { "sb1",            0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64,   CPU_SB1 },
19321   /* Broadcom SB-1A CPU core */
19322   { "sb1a",           0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64,   CPU_SB1 },
19323   
19324   { "loongson3a",     0, 0,                     ISA_MIPS64,   CPU_LOONGSON_3A },
19325
19326   /* MIPS 64 Release 2 */
19327
19328   /* Cavium Networks Octeon CPU core */
19329   { "octeon",         0, 0,                     ISA_MIPS64R2, CPU_OCTEON },
19330   { "octeon+",        0, 0,                     ISA_MIPS64R2, CPU_OCTEONP },
19331   { "octeon2",        0, 0,                     ISA_MIPS64R2, CPU_OCTEON2 },
19332
19333   /* RMI Xlr */
19334   { "xlr",            0, 0,                     ISA_MIPS64,   CPU_XLR },
19335
19336   /* Broadcom XLP.
19337      XLP is mostly like XLR, with the prominent exception that it is
19338      MIPS64R2 rather than MIPS64.  */
19339   { "xlp",            0, 0,                     ISA_MIPS64R2, CPU_XLR },
19340
19341   /* End marker */
19342   { NULL, 0, 0, 0, 0 }
19343 };
19344
19345
19346 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19347    with a final "000" replaced by "k".  Ignore case.
19348
19349    Note: this function is shared between GCC and GAS.  */
19350
19351 static bfd_boolean
19352 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19353 {
19354   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19355     given++, canonical++;
19356
19357   return ((*given == 0 && *canonical == 0)
19358           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19359 }
19360
19361
19362 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19363    CPU name.  We've traditionally allowed a lot of variation here.
19364
19365    Note: this function is shared between GCC and GAS.  */
19366
19367 static bfd_boolean
19368 mips_matching_cpu_name_p (const char *canonical, const char *given)
19369 {
19370   /* First see if the name matches exactly, or with a final "000"
19371      turned into "k".  */
19372   if (mips_strict_matching_cpu_name_p (canonical, given))
19373     return TRUE;
19374
19375   /* If not, try comparing based on numerical designation alone.
19376      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
19377   if (TOLOWER (*given) == 'r')
19378     given++;
19379   if (!ISDIGIT (*given))
19380     return FALSE;
19381
19382   /* Skip over some well-known prefixes in the canonical name,
19383      hoping to find a number there too.  */
19384   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19385     canonical += 2;
19386   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19387     canonical += 2;
19388   else if (TOLOWER (canonical[0]) == 'r')
19389     canonical += 1;
19390
19391   return mips_strict_matching_cpu_name_p (canonical, given);
19392 }
19393
19394
19395 /* Parse an option that takes the name of a processor as its argument.
19396    OPTION is the name of the option and CPU_STRING is the argument.
19397    Return the corresponding processor enumeration if the CPU_STRING is
19398    recognized, otherwise report an error and return null.
19399
19400    A similar function exists in GCC.  */
19401
19402 static const struct mips_cpu_info *
19403 mips_parse_cpu (const char *option, const char *cpu_string)
19404 {
19405   const struct mips_cpu_info *p;
19406
19407   /* 'from-abi' selects the most compatible architecture for the given
19408      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
19409      EABIs, we have to decide whether we're using the 32-bit or 64-bit
19410      version.  Look first at the -mgp options, if given, otherwise base
19411      the choice on MIPS_DEFAULT_64BIT.
19412
19413      Treat NO_ABI like the EABIs.  One reason to do this is that the
19414      plain 'mips' and 'mips64' configs have 'from-abi' as their default
19415      architecture.  This code picks MIPS I for 'mips' and MIPS III for
19416      'mips64', just as we did in the days before 'from-abi'.  */
19417   if (strcasecmp (cpu_string, "from-abi") == 0)
19418     {
19419       if (ABI_NEEDS_32BIT_REGS (mips_abi))
19420         return mips_cpu_info_from_isa (ISA_MIPS1);
19421
19422       if (ABI_NEEDS_64BIT_REGS (mips_abi))
19423         return mips_cpu_info_from_isa (ISA_MIPS3);
19424
19425       if (file_mips_gp32 >= 0)
19426         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19427
19428       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19429                                      ? ISA_MIPS3
19430                                      : ISA_MIPS1);
19431     }
19432
19433   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
19434   if (strcasecmp (cpu_string, "default") == 0)
19435     return 0;
19436
19437   for (p = mips_cpu_info_table; p->name != 0; p++)
19438     if (mips_matching_cpu_name_p (p->name, cpu_string))
19439       return p;
19440
19441   as_bad (_("Bad value (%s) for %s"), cpu_string, option);
19442   return 0;
19443 }
19444
19445 /* Return the canonical processor information for ISA (a member of the
19446    ISA_MIPS* enumeration).  */
19447
19448 static const struct mips_cpu_info *
19449 mips_cpu_info_from_isa (int isa)
19450 {
19451   int i;
19452
19453   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19454     if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19455         && isa == mips_cpu_info_table[i].isa)
19456       return (&mips_cpu_info_table[i]);
19457
19458   return NULL;
19459 }
19460
19461 static const struct mips_cpu_info *
19462 mips_cpu_info_from_arch (int arch)
19463 {
19464   int i;
19465
19466   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19467     if (arch == mips_cpu_info_table[i].cpu)
19468       return (&mips_cpu_info_table[i]);
19469
19470   return NULL;
19471 }
19472 \f
19473 static void
19474 show (FILE *stream, const char *string, int *col_p, int *first_p)
19475 {
19476   if (*first_p)
19477     {
19478       fprintf (stream, "%24s", "");
19479       *col_p = 24;
19480     }
19481   else
19482     {
19483       fprintf (stream, ", ");
19484       *col_p += 2;
19485     }
19486
19487   if (*col_p + strlen (string) > 72)
19488     {
19489       fprintf (stream, "\n%24s", "");
19490       *col_p = 24;
19491     }
19492
19493   fprintf (stream, "%s", string);
19494   *col_p += strlen (string);
19495
19496   *first_p = 0;
19497 }
19498
19499 void
19500 md_show_usage (FILE *stream)
19501 {
19502   int column, first;
19503   size_t i;
19504
19505   fprintf (stream, _("\
19506 MIPS options:\n\
19507 -EB                     generate big endian output\n\
19508 -EL                     generate little endian output\n\
19509 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
19510 -G NUM                  allow referencing objects up to NUM bytes\n\
19511                         implicitly with the gp register [default 8]\n"));
19512   fprintf (stream, _("\
19513 -mips1                  generate MIPS ISA I instructions\n\
19514 -mips2                  generate MIPS ISA II instructions\n\
19515 -mips3                  generate MIPS ISA III instructions\n\
19516 -mips4                  generate MIPS ISA IV instructions\n\
19517 -mips5                  generate MIPS ISA V instructions\n\
19518 -mips32                 generate MIPS32 ISA instructions\n\
19519 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
19520 -mips64                 generate MIPS64 ISA instructions\n\
19521 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
19522 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
19523
19524   first = 1;
19525
19526   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19527     show (stream, mips_cpu_info_table[i].name, &column, &first);
19528   show (stream, "from-abi", &column, &first);
19529   fputc ('\n', stream);
19530
19531   fprintf (stream, _("\
19532 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19533 -no-mCPU                don't generate code specific to CPU.\n\
19534                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
19535
19536   first = 1;
19537
19538   show (stream, "3900", &column, &first);
19539   show (stream, "4010", &column, &first);
19540   show (stream, "4100", &column, &first);
19541   show (stream, "4650", &column, &first);
19542   fputc ('\n', stream);
19543
19544   fprintf (stream, _("\
19545 -mips16                 generate mips16 instructions\n\
19546 -no-mips16              do not generate mips16 instructions\n"));
19547   fprintf (stream, _("\
19548 -mmicromips             generate microMIPS instructions\n\
19549 -mno-micromips          do not generate microMIPS instructions\n"));
19550   fprintf (stream, _("\
19551 -msmartmips             generate smartmips instructions\n\
19552 -mno-smartmips          do not generate smartmips instructions\n"));  
19553   fprintf (stream, _("\
19554 -mdsp                   generate DSP instructions\n\
19555 -mno-dsp                do not generate DSP instructions\n"));
19556   fprintf (stream, _("\
19557 -mdspr2                 generate DSP R2 instructions\n\
19558 -mno-dspr2              do not generate DSP R2 instructions\n"));
19559   fprintf (stream, _("\
19560 -mmt                    generate MT instructions\n\
19561 -mno-mt                 do not generate MT instructions\n"));
19562   fprintf (stream, _("\
19563 -mmcu                   generate MCU instructions\n\
19564 -mno-mcu                do not generate MCU instructions\n"));
19565   fprintf (stream, _("\
19566 -mvirt                  generate Virtualization instructions\n\
19567 -mno-virt               do not generate Virtualization instructions\n"));
19568   fprintf (stream, _("\
19569 -minsn32                only generate 32-bit microMIPS instructions\n\
19570 -mno-insn32             generate all microMIPS instructions\n"));
19571   fprintf (stream, _("\
19572 -mfix-loongson2f-jump   work around Loongson2F JUMP instructions\n\
19573 -mfix-loongson2f-nop    work around Loongson2F NOP errata\n\
19574 -mfix-vr4120            work around certain VR4120 errata\n\
19575 -mfix-vr4130            work around VR4130 mflo/mfhi errata\n\
19576 -mfix-24k               insert a nop after ERET and DERET instructions\n\
19577 -mfix-cn63xxp1          work around CN63XXP1 PREF errata\n\
19578 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
19579 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
19580 -msym32                 assume all symbols have 32-bit values\n\
19581 -O0                     remove unneeded NOPs, do not swap branches\n\
19582 -O                      remove unneeded NOPs and swap branches\n\
19583 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
19584 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
19585   fprintf (stream, _("\
19586 -mhard-float            allow floating-point instructions\n\
19587 -msoft-float            do not allow floating-point instructions\n\
19588 -msingle-float          only allow 32-bit floating-point operations\n\
19589 -mdouble-float          allow 32-bit and 64-bit floating-point operations\n\
19590 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
19591 --[no-]relax-branch     [dis]allow out-of-range branches to be relaxed\n"
19592                      ));
19593   fprintf (stream, _("\
19594 -KPIC, -call_shared     generate SVR4 position independent code\n\
19595 -call_nonpic            generate non-PIC code that can operate with DSOs\n\
19596 -mvxworks-pic           generate VxWorks position independent code\n\
19597 -non_shared             do not generate code that can operate with DSOs\n\
19598 -xgot                   assume a 32 bit GOT\n\
19599 -mpdr, -mno-pdr         enable/disable creation of .pdr sections\n\
19600 -mshared, -mno-shared   disable/enable .cpload optimization for\n\
19601                         position dependent (non shared) code\n\
19602 -mabi=ABI               create ABI conformant object file for:\n"));
19603
19604   first = 1;
19605
19606   show (stream, "32", &column, &first);
19607   show (stream, "o64", &column, &first);
19608   show (stream, "n32", &column, &first);
19609   show (stream, "64", &column, &first);
19610   show (stream, "eabi", &column, &first);
19611
19612   fputc ('\n', stream);
19613
19614   fprintf (stream, _("\
19615 -32                     create o32 ABI object file (default)\n\
19616 -n32                    create n32 ABI object file\n\
19617 -64                     create 64 ABI object file\n"));
19618 }
19619
19620 #ifdef TE_IRIX
19621 enum dwarf2_format
19622 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
19623 {
19624   if (HAVE_64BIT_SYMBOLS)
19625     return dwarf2_format_64bit_irix;
19626   else
19627     return dwarf2_format_32bit;
19628 }
19629 #endif
19630
19631 int
19632 mips_dwarf2_addr_size (void)
19633 {
19634   if (HAVE_64BIT_OBJECTS)
19635     return 8;
19636   else
19637     return 4;
19638 }
19639
19640 /* Standard calling conventions leave the CFA at SP on entry.  */
19641 void
19642 mips_cfi_frame_initial_instructions (void)
19643 {
19644   cfi_add_CFA_def_cfa_register (SP);
19645 }
19646
19647 int
19648 tc_mips_regname_to_dw2regnum (char *regname)
19649 {
19650   unsigned int regnum = -1;
19651   unsigned int reg;
19652
19653   if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19654     regnum = reg;
19655
19656   return regnum;
19657 }