1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright (C) 1993-2014 Free Software Foundation, Inc.
3    Contributed by the OSF and Ralph Campbell.
4    Written by Keith Knowles and Ralph Campbell, working independently.
5    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6    Support.
7 
8    This file is part of GAS.
9 
10    GAS is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3, or (at your option)
13    any later version.
14 
15    GAS is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with GAS; see the file COPYING.  If not, write to the Free
22    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23    02110-1301, USA.  */
24 
25 #include "as.h"
26 #include "config.h"
27 #include "subsegs.h"
28 #include "safe-ctype.h"
29 
30 #include "opcode/mips.h"
31 #include "itbl-ops.h"
32 #include "dwarf2dbg.h"
33 #include "dw2gencfi.h"
34 
35 /* Check assumptions made in this file.  */
36 typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
37 typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
38 
39 #ifdef DEBUG
40 #define DBG(x) printf x
41 #else
42 #define DBG(x)
43 #endif
44 
45 #define streq(a, b)           (strcmp (a, b) == 0)
46 
47 #define SKIP_SPACE_TABS(S) \
48   do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49 
50 /* Clean up namespace so we can include obj-elf.h too.  */
51 static int mips_output_flavor (void);
mips_output_flavor(void)52 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
53 #undef OBJ_PROCESS_STAB
54 #undef OUTPUT_FLAVOR
55 #undef S_GET_ALIGN
56 #undef S_GET_SIZE
57 #undef S_SET_ALIGN
58 #undef S_SET_SIZE
59 #undef obj_frob_file
60 #undef obj_frob_file_after_relocs
61 #undef obj_frob_symbol
62 #undef obj_pop_insert
63 #undef obj_sec_sym_ok_for_reloc
64 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
65 
66 #include "obj-elf.h"
67 /* Fix any of them that we actually care about.  */
68 #undef OUTPUT_FLAVOR
69 #define OUTPUT_FLAVOR mips_output_flavor()
70 
71 #include "elf/mips.h"
72 
73 #ifndef ECOFF_DEBUGGING
74 #define NO_ECOFF_DEBUGGING
75 #define ECOFF_DEBUGGING 0
76 #endif
77 
78 int mips_flag_mdebug = -1;
79 
80 /* Control generation of .pdr sections.  Off by default on IRIX: the native
81    linker doesn't know about and discards them, but relocations against them
82    remain, leading to rld crashes.  */
83 #ifdef TE_IRIX
84 int mips_flag_pdr = FALSE;
85 #else
86 int mips_flag_pdr = TRUE;
87 #endif
88 
89 #include "ecoff.h"
90 
91 static char *mips_regmask_frag;
92 static char *mips_flags_frag;
93 
94 #define ZERO 0
95 #define ATREG 1
96 #define S0  16
97 #define S7  23
98 #define TREG 24
99 #define PIC_CALL_REG 25
100 #define KT0 26
101 #define KT1 27
102 #define GP  28
103 #define SP  29
104 #define FP  30
105 #define RA  31
106 
107 #define ILLEGAL_REG (32)
108 
109 #define AT  mips_opts.at
110 
111 extern int target_big_endian;
112 
113 /* The name of the readonly data section.  */
114 #define RDATA_SECTION_NAME ".rodata"
115 
116 /* Ways in which an instruction can be "appended" to the output.  */
117 enum append_method {
118   /* Just add it normally.  */
119   APPEND_ADD,
120 
121   /* Add it normally and then add a nop.  */
122   APPEND_ADD_WITH_NOP,
123 
124   /* Turn an instruction with a delay slot into a "compact" version.  */
125   APPEND_ADD_COMPACT,
126 
127   /* Insert the instruction before the last one.  */
128   APPEND_SWAP
129 };
130 
131 /* Information about an instruction, including its format, operands
132    and fixups.  */
133 struct mips_cl_insn
134 {
135   /* The opcode's entry in mips_opcodes or mips16_opcodes.  */
136   const struct mips_opcode *insn_mo;
137 
138   /* The 16-bit or 32-bit bitstring of the instruction itself.  This is
139      a copy of INSN_MO->match with the operands filled in.  If we have
140      decided to use an extended MIPS16 instruction, this includes the
141      extension.  */
142   unsigned long insn_opcode;
143 
144   /* The frag that contains the instruction.  */
145   struct frag *frag;
146 
147   /* The offset into FRAG of the first instruction byte.  */
148   long where;
149 
150   /* The relocs associated with the instruction, if any.  */
151   fixS *fixp[3];
152 
153   /* True if this entry cannot be moved from its current position.  */
154   unsigned int fixed_p : 1;
155 
156   /* True if this instruction occurred in a .set noreorder block.  */
157   unsigned int noreorder_p : 1;
158 
159   /* True for mips16 instructions that jump to an absolute address.  */
160   unsigned int mips16_absolute_jump_p : 1;
161 
162   /* True if this instruction is complete.  */
163   unsigned int complete_p : 1;
164 
165   /* True if this instruction is cleared from history by unconditional
166      branch.  */
167   unsigned int cleared_p : 1;
168 };
169 
170 /* The ABI to use.  */
171 enum mips_abi_level
172 {
173   NO_ABI = 0,
174   O32_ABI,
175   O64_ABI,
176   N32_ABI,
177   N64_ABI,
178   EABI_ABI
179 };
180 
181 /* MIPS ABI we are using for this output file.  */
182 static enum mips_abi_level mips_abi = NO_ABI;
183 
184 /* Whether or not we have code that can call pic code.  */
185 int mips_abicalls = FALSE;
186 
187 /* Whether or not we have code which can be put into a shared
188    library.  */
189 static bfd_boolean mips_in_shared = TRUE;
190 
191 /* This is the set of options which may be modified by the .set
192    pseudo-op.  We use a struct so that .set push and .set pop are more
193    reliable.  */
194 
195 struct mips_set_options
196 {
197   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
198      if it has not been initialized.  Changed by `.set mipsN', and the
199      -mipsN command line option, and the default CPU.  */
200   int isa;
201   /* Enabled Application Specific Extensions (ASEs).  Changed by `.set
202      <asename>', by command line options, and based on the default
203      architecture.  */
204   int ase;
205   /* Whether we are assembling for the mips16 processor.  0 if we are
206      not, 1 if we are, and -1 if the value has not been initialized.
207      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
208      -nomips16 command line options, and the default CPU.  */
209   int mips16;
210   /* Whether we are assembling for the mipsMIPS ASE.  0 if we are not,
211      1 if we are, and -1 if the value has not been initialized.  Changed
212      by `.set micromips' and `.set nomicromips', and the -mmicromips
213      and -mno-micromips command line options, and the default CPU.  */
214   int micromips;
215   /* Non-zero if we should not reorder instructions.  Changed by `.set
216      reorder' and `.set noreorder'.  */
217   int noreorder;
218   /* Non-zero if we should not permit the register designated "assembler
219      temporary" to be used in instructions.  The value is the register
220      number, normally $at ($1).  Changed by `.set at=REG', `.set noat'
221      (same as `.set at=$0') and `.set at' (same as `.set at=$1').  */
222   unsigned int at;
223   /* Non-zero if we should warn when a macro instruction expands into
224      more than one machine instruction.  Changed by `.set nomacro' and
225      `.set macro'.  */
226   int warn_about_macros;
227   /* Non-zero if we should not move instructions.  Changed by `.set
228      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
229   int nomove;
230   /* Non-zero if we should not optimize branches by moving the target
231      of the branch into the delay slot.  Actually, we don't perform
232      this optimization anyhow.  Changed by `.set bopt' and `.set
233      nobopt'.  */
234   int nobopt;
235   /* Non-zero if we should not autoextend mips16 instructions.
236      Changed by `.set autoextend' and `.set noautoextend'.  */
237   int noautoextend;
238   /* True if we should only emit 32-bit microMIPS instructions.
239      Changed by `.set insn32' and `.set noinsn32', and the -minsn32
240      and -mno-insn32 command line options.  */
241   bfd_boolean insn32;
242   /* Restrict general purpose registers and floating point registers
243      to 32 bit.  This is initially determined when -mgp32 or -mfp32
244      is passed but can changed if the assembler code uses .set mipsN.  */
245   int gp;
246   int fp;
247   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
248      command line option, and the default CPU.  */
249   int arch;
250   /* True if ".set sym32" is in effect.  */
251   bfd_boolean sym32;
252   /* True if floating-point operations are not allowed.  Changed by .set
253      softfloat or .set hardfloat, by command line options -msoft-float or
254      -mhard-float.  The default is false.  */
255   bfd_boolean soft_float;
256 
257   /* True if only single-precision floating-point operations are allowed.
258      Changed by .set singlefloat or .set doublefloat, command-line options
259      -msingle-float or -mdouble-float.  The default is false.  */
260   bfd_boolean single_float;
261 
262   /* 1 if single-precision operations on odd-numbered registers are
263      allowed.  */
264   int oddspreg;
265 };
266 
267 /* Specifies whether module level options have been checked yet.  */
268 static bfd_boolean file_mips_opts_checked = FALSE;
269 
270 /* Do we support nan2008?  0 if we don't, 1 if we do, and -1 if the
271    value has not been initialized.  Changed by `.nan legacy' and
272    `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
273    options, and the default CPU.  */
274 static int mips_nan2008 = -1;
275 
276 /* This is the struct we use to hold the module level set of options.
277    Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
278    fp fields to -1 to indicate that they have not been initialized.  */
279 
280 static struct mips_set_options file_mips_opts =
281 {
282   /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
283   /* noreorder */ 0,  /* at */ ATREG, /* warn_about_macros */ 0,
284   /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
285   /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
286   /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
287 };
288 
289 /* This is similar to file_mips_opts, but for the current set of options.  */
290 
291 static struct mips_set_options mips_opts =
292 {
293   /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
294   /* noreorder */ 0,  /* at */ ATREG, /* warn_about_macros */ 0,
295   /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
296   /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
297   /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
298 };
299 
300 /* Which bits of file_ase were explicitly set or cleared by ASE options.  */
301 static unsigned int file_ase_explicit;
302 
303 /* These variables are filled in with the masks of registers used.
304    The object format code reads them and puts them in the appropriate
305    place.  */
306 unsigned long mips_gprmask;
307 unsigned long mips_cprmask[4];
308 
309 /* True if any MIPS16 code was produced.  */
310 static int file_ase_mips16;
311 
312 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32		\
313 			      || mips_opts.isa == ISA_MIPS32R2		\
314 			      || mips_opts.isa == ISA_MIPS32R3		\
315 			      || mips_opts.isa == ISA_MIPS32R5		\
316 			      || mips_opts.isa == ISA_MIPS64		\
317 			      || mips_opts.isa == ISA_MIPS64R2		\
318 			      || mips_opts.isa == ISA_MIPS64R3		\
319 			      || mips_opts.isa == ISA_MIPS64R5)
320 
321 /* True if any microMIPS code was produced.  */
322 static int file_ase_micromips;
323 
324 /* True if we want to create R_MIPS_JALR for jalr $25.  */
325 #ifdef TE_IRIX
326 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
327 #else
328 /* As a GNU extension, we use R_MIPS_JALR for o32 too.  However,
329    because there's no place for any addend, the only acceptable
330    expression is a bare symbol.  */
331 #define MIPS_JALR_HINT_P(EXPR) \
332   (!HAVE_IN_PLACE_ADDENDS \
333    || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
334 #endif
335 
336 /* The argument of the -march= flag.  The architecture we are assembling.  */
337 static const char *mips_arch_string;
338 
339 /* The argument of the -mtune= flag.  The architecture for which we
340    are optimizing.  */
341 static int mips_tune = CPU_UNKNOWN;
342 static const char *mips_tune_string;
343 
344 /* True when generating 32-bit code for a 64-bit processor.  */
345 static int mips_32bitmode = 0;
346 
347 /* True if the given ABI requires 32-bit registers.  */
348 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
349 
350 /* Likewise 64-bit registers.  */
351 #define ABI_NEEDS_64BIT_REGS(ABI)	\
352   ((ABI) == N32_ABI 			\
353    || (ABI) == N64_ABI			\
354    || (ABI) == O64_ABI)
355 
356 #define ISA_IS_R6(ISA)			\
357   ((ISA) == ISA_MIPS32R6		\
358    || (ISA) == ISA_MIPS64R6)
359 
360 /*  Return true if ISA supports 64 bit wide gp registers.  */
361 #define ISA_HAS_64BIT_REGS(ISA)		\
362   ((ISA) == ISA_MIPS3			\
363    || (ISA) == ISA_MIPS4		\
364    || (ISA) == ISA_MIPS5		\
365    || (ISA) == ISA_MIPS64		\
366    || (ISA) == ISA_MIPS64R2		\
367    || (ISA) == ISA_MIPS64R3		\
368    || (ISA) == ISA_MIPS64R5		\
369    || (ISA) == ISA_MIPS64R6)
370 
371 /*  Return true if ISA supports 64 bit wide float registers.  */
372 #define ISA_HAS_64BIT_FPRS(ISA)		\
373   ((ISA) == ISA_MIPS3			\
374    || (ISA) == ISA_MIPS4		\
375    || (ISA) == ISA_MIPS5		\
376    || (ISA) == ISA_MIPS32R2		\
377    || (ISA) == ISA_MIPS32R3		\
378    || (ISA) == ISA_MIPS32R5		\
379    || (ISA) == ISA_MIPS32R6		\
380    || (ISA) == ISA_MIPS64		\
381    || (ISA) == ISA_MIPS64R2		\
382    || (ISA) == ISA_MIPS64R3		\
383    || (ISA) == ISA_MIPS64R5		\
384    || (ISA) == ISA_MIPS64R6)
385 
386 /* Return true if ISA supports 64-bit right rotate (dror et al.)
387    instructions.  */
388 #define ISA_HAS_DROR(ISA)		\
389   ((ISA) == ISA_MIPS64R2		\
390    || (ISA) == ISA_MIPS64R3		\
391    || (ISA) == ISA_MIPS64R5		\
392    || (ISA) == ISA_MIPS64R6		\
393    || (mips_opts.micromips		\
394        && ISA_HAS_64BIT_REGS (ISA))	\
395    )
396 
397 /* Return true if ISA supports 32-bit right rotate (ror et al.)
398    instructions.  */
399 #define ISA_HAS_ROR(ISA)		\
400   ((ISA) == ISA_MIPS32R2		\
401    || (ISA) == ISA_MIPS32R3		\
402    || (ISA) == ISA_MIPS32R5		\
403    || (ISA) == ISA_MIPS32R6		\
404    || (ISA) == ISA_MIPS64R2		\
405    || (ISA) == ISA_MIPS64R3		\
406    || (ISA) == ISA_MIPS64R5		\
407    || (ISA) == ISA_MIPS64R6		\
408    || (mips_opts.ase & ASE_SMARTMIPS)	\
409    || mips_opts.micromips		\
410    )
411 
412 /* Return true if ISA supports single-precision floats in odd registers.  */
413 #define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
414   (((ISA) == ISA_MIPS32			\
415     || (ISA) == ISA_MIPS32R2		\
416     || (ISA) == ISA_MIPS32R3		\
417     || (ISA) == ISA_MIPS32R5		\
418     || (ISA) == ISA_MIPS32R6		\
419     || (ISA) == ISA_MIPS64		\
420     || (ISA) == ISA_MIPS64R2		\
421     || (ISA) == ISA_MIPS64R3		\
422     || (ISA) == ISA_MIPS64R5		\
423     || (ISA) == ISA_MIPS64R6		\
424     || (CPU) == CPU_R5900)		\
425    && (CPU) != CPU_LOONGSON_3A)
426 
427 /* Return true if ISA supports move to/from high part of a 64-bit
428    floating-point register. */
429 #define ISA_HAS_MXHC1(ISA)		\
430   ((ISA) == ISA_MIPS32R2		\
431    || (ISA) == ISA_MIPS32R3		\
432    || (ISA) == ISA_MIPS32R5		\
433    || (ISA) == ISA_MIPS32R6		\
434    || (ISA) == ISA_MIPS64R2		\
435    || (ISA) == ISA_MIPS64R3		\
436    || (ISA) == ISA_MIPS64R5		\
437    || (ISA) == ISA_MIPS64R6)
438 
439 /*  Return true if ISA supports legacy NAN.  */
440 #define ISA_HAS_LEGACY_NAN(ISA)		\
441   ((ISA) == ISA_MIPS1			\
442    || (ISA) == ISA_MIPS2		\
443    || (ISA) == ISA_MIPS3		\
444    || (ISA) == ISA_MIPS4		\
445    || (ISA) == ISA_MIPS5		\
446    || (ISA) == ISA_MIPS32		\
447    || (ISA) == ISA_MIPS32R2		\
448    || (ISA) == ISA_MIPS32R3		\
449    || (ISA) == ISA_MIPS32R5		\
450    || (ISA) == ISA_MIPS64		\
451    || (ISA) == ISA_MIPS64R2		\
452    || (ISA) == ISA_MIPS64R3		\
453    || (ISA) == ISA_MIPS64R5)
454 
455 #define GPR_SIZE \
456     (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
457      ? 32 \
458      : mips_opts.gp)
459 
460 #define FPR_SIZE \
461     (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
462      ? 32 \
463      : mips_opts.fp)
464 
465 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
466 
467 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
468 
469 /* True if relocations are stored in-place.  */
470 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
471 
472 /* The ABI-derived address size.  */
473 #define HAVE_64BIT_ADDRESSES \
474   (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
475 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
476 
477 /* The size of symbolic constants (i.e., expressions of the form
478    "SYMBOL" or "SYMBOL + OFFSET").  */
479 #define HAVE_32BIT_SYMBOLS \
480   (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
481 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
482 
483 /* Addresses are loaded in different ways, depending on the address size
484    in use.  The n32 ABI Documentation also mandates the use of additions
485    with overflow checking, but existing implementations don't follow it.  */
486 #define ADDRESS_ADD_INSN						\
487    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
488 
489 #define ADDRESS_ADDI_INSN						\
490    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
491 
492 #define ADDRESS_LOAD_INSN						\
493    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
494 
495 #define ADDRESS_STORE_INSN						\
496    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
497 
498 /* Return true if the given CPU supports the MIPS16 ASE.  */
499 #define CPU_HAS_MIPS16(cpu)						\
500    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0		\
501     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
502 
503 /* Return true if the given CPU supports the microMIPS ASE.  */
504 #define CPU_HAS_MICROMIPS(cpu)	0
505 
506 /* True if CPU has a dror instruction.  */
507 #define CPU_HAS_DROR(CPU)	((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
508 
509 /* True if CPU has a ror instruction.  */
510 #define CPU_HAS_ROR(CPU)	CPU_HAS_DROR (CPU)
511 
512 /* True if CPU is in the Octeon family */
513 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
514 			    || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
515 
516 /* True if CPU has seq/sne and seqi/snei instructions.  */
517 #define CPU_HAS_SEQ(CPU)	(CPU_IS_OCTEON (CPU))
518 
519 /* True, if CPU has support for ldc1 and sdc1. */
520 #define CPU_HAS_LDC1_SDC1(CPU)	\
521    ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
522 
523 /* True if mflo and mfhi can be immediately followed by instructions
524    which write to the HI and LO registers.
525 
526    According to MIPS specifications, MIPS ISAs I, II, and III need
527    (at least) two instructions between the reads of HI/LO and
528    instructions which write them, and later ISAs do not.  Contradicting
529    the MIPS specifications, some MIPS IV processor user manuals (e.g.
530    the UM for the NEC Vr5000) document needing the instructions between
531    HI/LO reads and writes, as well.  Therefore, we declare only MIPS32,
532    MIPS64 and later ISAs to have the interlocks, plus any specific
533    earlier-ISA CPUs for which CPU documentation declares that the
534    instructions are really interlocked.  */
535 #define hilo_interlocks \
536   (mips_opts.isa == ISA_MIPS32                        \
537    || mips_opts.isa == ISA_MIPS32R2                   \
538    || mips_opts.isa == ISA_MIPS32R3                   \
539    || mips_opts.isa == ISA_MIPS32R5                   \
540    || mips_opts.isa == ISA_MIPS32R6                   \
541    || mips_opts.isa == ISA_MIPS64                     \
542    || mips_opts.isa == ISA_MIPS64R2                   \
543    || mips_opts.isa == ISA_MIPS64R3                   \
544    || mips_opts.isa == ISA_MIPS64R5                   \
545    || mips_opts.isa == ISA_MIPS64R6                   \
546    || mips_opts.arch == CPU_R4010                     \
547    || mips_opts.arch == CPU_R5900                     \
548    || mips_opts.arch == CPU_R10000                    \
549    || mips_opts.arch == CPU_R12000                    \
550    || mips_opts.arch == CPU_R14000                    \
551    || mips_opts.arch == CPU_R16000                    \
552    || mips_opts.arch == CPU_RM7000                    \
553    || mips_opts.arch == CPU_VR5500                    \
554    || mips_opts.micromips                             \
555    )
556 
557 /* Whether the processor uses hardware interlocks to protect reads
558    from the GPRs after they are loaded from memory, and thus does not
559    require nops to be inserted.  This applies to instructions marked
560    INSN_LOAD_MEMORY.  These nops are only required at MIPS ISA
561    level I and microMIPS mode instructions are always interlocked.  */
562 #define gpr_interlocks                                \
563   (mips_opts.isa != ISA_MIPS1                         \
564    || mips_opts.arch == CPU_R3900                     \
565    || mips_opts.arch == CPU_R5900                     \
566    || mips_opts.micromips                             \
567    )
568 
569 /* Whether the processor uses hardware interlocks to avoid delays
570    required by coprocessor instructions, and thus does not require
571    nops to be inserted.  This applies to instructions marked
572    INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
573    instructions marked INSN_WRITE_COND_CODE and ones marked
574    INSN_READ_COND_CODE.  These nops are only required at MIPS ISA
575    levels I, II, and III and microMIPS mode instructions are always
576    interlocked.  */
577 /* Itbl support may require additional care here.  */
578 #define cop_interlocks                                \
579   ((mips_opts.isa != ISA_MIPS1                        \
580     && mips_opts.isa != ISA_MIPS2                     \
581     && mips_opts.isa != ISA_MIPS3)                    \
582    || mips_opts.arch == CPU_R4300                     \
583    || mips_opts.micromips                             \
584    )
585 
586 /* Whether the processor uses hardware interlocks to protect reads
587    from coprocessor registers after they are loaded from memory, and
588    thus does not require nops to be inserted.  This applies to
589    instructions marked INSN_COPROC_MEMORY_DELAY.  These nops are only
590    requires at MIPS ISA level I and microMIPS mode instructions are
591    always interlocked.  */
592 #define cop_mem_interlocks                            \
593   (mips_opts.isa != ISA_MIPS1                         \
594    || mips_opts.micromips                             \
595    )
596 
597 /* Is this a mfhi or mflo instruction?  */
598 #define MF_HILO_INSN(PINFO) \
599   ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
600 
601 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
602    has been selected.  This implies, in particular, that addresses of text
603    labels have their LSB set.  */
604 #define HAVE_CODE_COMPRESSION						\
605   ((mips_opts.mips16 | mips_opts.micromips) != 0)
606 
607 /* The minimum and maximum signed values that can be stored in a GPR.  */
608 #define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
609 #define GPR_SMIN (-GPR_SMAX - 1)
610 
611 /* MIPS PIC level.  */
612 
613 enum mips_pic_level mips_pic;
614 
615 /* 1 if we should generate 32 bit offsets from the $gp register in
616    SVR4_PIC mode.  Currently has no meaning in other modes.  */
617 static int mips_big_got = 0;
618 
619 /* 1 if trap instructions should used for overflow rather than break
620    instructions.  */
621 static int mips_trap = 0;
622 
623 /* 1 if double width floating point constants should not be constructed
624    by assembling two single width halves into two single width floating
625    point registers which just happen to alias the double width destination
626    register.  On some architectures this aliasing can be disabled by a bit
627    in the status register, and the setting of this bit cannot be determined
628    automatically at assemble time.  */
629 static int mips_disable_float_construction;
630 
631 /* Non-zero if any .set noreorder directives were used.  */
632 
633 static int mips_any_noreorder;
634 
635 /* Non-zero if nops should be inserted when the register referenced in
636    an mfhi/mflo instruction is read in the next two instructions.  */
637 static int mips_7000_hilo_fix;
638 
639 /* The size of objects in the small data section.  */
640 static unsigned int g_switch_value = 8;
641 /* Whether the -G option was used.  */
642 static int g_switch_seen = 0;
643 
644 #define N_RMASK 0xc4
645 #define N_VFP   0xd4
646 
647 /* If we can determine in advance that GP optimization won't be
648    possible, we can skip the relaxation stuff that tries to produce
649    GP-relative references.  This makes delay slot optimization work
650    better.
651 
652    This function can only provide a guess, but it seems to work for
653    gcc output.  It needs to guess right for gcc, otherwise gcc
654    will put what it thinks is a GP-relative instruction in a branch
655    delay slot.
656 
657    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
658    fixed it for the non-PIC mode.  KR 95/04/07  */
659 static int nopic_need_relax (symbolS *, int);
660 
661 /* handle of the OPCODE hash table */
662 static struct hash_control *op_hash = NULL;
663 
664 /* The opcode hash table we use for the mips16.  */
665 static struct hash_control *mips16_op_hash = NULL;
666 
667 /* The opcode hash table we use for the microMIPS ASE.  */
668 static struct hash_control *micromips_op_hash = NULL;
669 
670 /* This array holds the chars that always start a comment.  If the
671     pre-processor is disabled, these aren't very useful */
672 const char comment_chars[] = "#";
673 
674 /* This array holds the chars that only start a comment at the beginning of
675    a line.  If the line seems to have the form '# 123 filename'
676    .line and .file directives will appear in the pre-processed output */
677 /* Note that input_file.c hand checks for '#' at the beginning of the
678    first line of the input file.  This is because the compiler outputs
679    #NO_APP at the beginning of its output.  */
680 /* Also note that C style comments are always supported.  */
681 const char line_comment_chars[] = "#";
682 
683 /* This array holds machine specific line separator characters.  */
684 const char line_separator_chars[] = ";";
685 
686 /* Chars that can be used to separate mant from exp in floating point nums */
687 const char EXP_CHARS[] = "eE";
688 
689 /* Chars that mean this number is a floating point constant */
690 /* As in 0f12.456 */
691 /* or    0d1.2345e12 */
692 const char FLT_CHARS[] = "rRsSfFdDxXpP";
693 
694 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
695    changed in read.c .  Ideally it shouldn't have to know about it at all,
696    but nothing is ideal around here.
697  */
698 
699 /* Types of printf format used for instruction-related error messages.
700    "I" means int ("%d") and "S" means string ("%s"). */
701 enum mips_insn_error_format {
702   ERR_FMT_PLAIN,
703   ERR_FMT_I,
704   ERR_FMT_SS,
705 };
706 
707 /* Information about an error that was found while assembling the current
708    instruction.  */
709 struct mips_insn_error {
710   /* We sometimes need to match an instruction against more than one
711      opcode table entry.  Errors found during this matching are reported
712      against a particular syntactic argument rather than against the
713      instruction as a whole.  We grade these messages so that errors
714      against argument N have a greater priority than an error against
715      any argument < N, since the former implies that arguments up to N
716      were acceptable and that the opcode entry was therefore a closer match.
717      If several matches report an error against the same argument,
718      we only use that error if it is the same in all cases.
719 
720      min_argnum is the minimum argument number for which an error message
721      should be accepted.  It is 0 if MSG is against the instruction as
722      a whole.  */
723   int min_argnum;
724 
725   /* The printf()-style message, including its format and arguments.  */
726   enum mips_insn_error_format format;
727   const char *msg;
728   union {
729     int i;
730     const char *ss[2];
731   } u;
732 };
733 
734 /* The error that should be reported for the current instruction.  */
735 static struct mips_insn_error insn_error;
736 
737 static int auto_align = 1;
738 
739 /* When outputting SVR4 PIC code, the assembler needs to know the
740    offset in the stack frame from which to restore the $gp register.
741    This is set by the .cprestore pseudo-op, and saved in this
742    variable.  */
743 static offsetT mips_cprestore_offset = -1;
744 
745 /* Similar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
746    more optimizations, it can use a register value instead of a memory-saved
747    offset and even an other register than $gp as global pointer.  */
748 static offsetT mips_cpreturn_offset = -1;
749 static int mips_cpreturn_register = -1;
750 static int mips_gp_register = GP;
751 static int mips_gprel_offset = 0;
752 
753 /* Whether mips_cprestore_offset has been set in the current function
754    (or whether it has already been warned about, if not).  */
755 static int mips_cprestore_valid = 0;
756 
757 /* This is the register which holds the stack frame, as set by the
758    .frame pseudo-op.  This is needed to implement .cprestore.  */
759 static int mips_frame_reg = SP;
760 
761 /* Whether mips_frame_reg has been set in the current function
762    (or whether it has already been warned about, if not).  */
763 static int mips_frame_reg_valid = 0;
764 
765 /* To output NOP instructions correctly, we need to keep information
766    about the previous two instructions.  */
767 
768 /* Whether we are optimizing.  The default value of 2 means to remove
769    unneeded NOPs and swap branch instructions when possible.  A value
770    of 1 means to not swap branches.  A value of 0 means to always
771    insert NOPs.  */
772 static int mips_optimize = 2;
773 
774 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
775    equivalent to seeing no -g option at all.  */
776 static int mips_debug = 0;
777 
778 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata.  */
779 #define MAX_VR4130_NOPS 4
780 
781 /* The maximum number of NOPs needed to fill delay slots.  */
782 #define MAX_DELAY_NOPS 2
783 
784 /* The maximum number of NOPs needed for any purpose.  */
785 #define MAX_NOPS 4
786 
787 /* A list of previous instructions, with index 0 being the most recent.
788    We need to look back MAX_NOPS instructions when filling delay slots
789    or working around processor errata.  We need to look back one
790    instruction further if we're thinking about using history[0] to
791    fill a branch delay slot.  */
792 static struct mips_cl_insn history[1 + MAX_NOPS];
793 
794 /* Arrays of operands for each instruction.  */
795 #define MAX_OPERANDS 6
796 struct mips_operand_array {
797   const struct mips_operand *operand[MAX_OPERANDS];
798 };
799 static struct mips_operand_array *mips_operands;
800 static struct mips_operand_array *mips16_operands;
801 static struct mips_operand_array *micromips_operands;
802 
803 /* Nop instructions used by emit_nop.  */
804 static struct mips_cl_insn nop_insn;
805 static struct mips_cl_insn mips16_nop_insn;
806 static struct mips_cl_insn micromips_nop16_insn;
807 static struct mips_cl_insn micromips_nop32_insn;
808 
809 /* The appropriate nop for the current mode.  */
810 #define NOP_INSN (mips_opts.mips16					\
811 		  ? &mips16_nop_insn					\
812 		  : (mips_opts.micromips				\
813 		     ? (mips_opts.insn32				\
814 			? &micromips_nop32_insn				\
815 			: &micromips_nop16_insn)			\
816 		     : &nop_insn))
817 
818 /* The size of NOP_INSN in bytes.  */
819 #define NOP_INSN_SIZE ((mips_opts.mips16				\
820 			|| (mips_opts.micromips && !mips_opts.insn32))	\
821 		       ? 2 : 4)
822 
823 /* If this is set, it points to a frag holding nop instructions which
824    were inserted before the start of a noreorder section.  If those
825    nops turn out to be unnecessary, the size of the frag can be
826    decreased.  */
827 static fragS *prev_nop_frag;
828 
829 /* The number of nop instructions we created in prev_nop_frag.  */
830 static int prev_nop_frag_holds;
831 
832 /* The number of nop instructions that we know we need in
833    prev_nop_frag.  */
834 static int prev_nop_frag_required;
835 
836 /* The number of instructions we've seen since prev_nop_frag.  */
837 static int prev_nop_frag_since;
838 
839 /* Relocations against symbols are sometimes done in two parts, with a HI
840    relocation and a LO relocation.  Each relocation has only 16 bits of
841    space to store an addend.  This means that in order for the linker to
842    handle carries correctly, it must be able to locate both the HI and
843    the LO relocation.  This means that the relocations must appear in
844    order in the relocation table.
845 
846    In order to implement this, we keep track of each unmatched HI
847    relocation.  We then sort them so that they immediately precede the
848    corresponding LO relocation.  */
849 
850 struct mips_hi_fixup
851 {
852   /* Next HI fixup.  */
853   struct mips_hi_fixup *next;
854   /* This fixup.  */
855   fixS *fixp;
856   /* The section this fixup is in.  */
857   segT seg;
858 };
859 
860 /* The list of unmatched HI relocs.  */
861 
862 static struct mips_hi_fixup *mips_hi_fixup_list;
863 
864 /* The frag containing the last explicit relocation operator.
865    Null if explicit relocations have not been used.  */
866 
867 static fragS *prev_reloc_op_frag;
868 
869 /* Map mips16 register numbers to normal MIPS register numbers.  */
870 
871 static const unsigned int mips16_to_32_reg_map[] =
872 {
873   16, 17, 2, 3, 4, 5, 6, 7
874 };
875 
876 /* Map microMIPS register numbers to normal MIPS register numbers.  */
877 
878 #define micromips_to_32_reg_d_map	mips16_to_32_reg_map
879 
880 /* The microMIPS registers with type h.  */
881 static const unsigned int micromips_to_32_reg_h_map1[] =
882 {
883   5, 5, 6, 4, 4, 4, 4, 4
884 };
885 static const unsigned int micromips_to_32_reg_h_map2[] =
886 {
887   6, 7, 7, 21, 22, 5, 6, 7
888 };
889 
890 /* The microMIPS registers with type m.  */
891 static const unsigned int micromips_to_32_reg_m_map[] =
892 {
893   0, 17, 2, 3, 16, 18, 19, 20
894 };
895 
896 #define micromips_to_32_reg_n_map      micromips_to_32_reg_m_map
897 
898 /* Classifies the kind of instructions we're interested in when
899    implementing -mfix-vr4120.  */
900 enum fix_vr4120_class
901 {
902   FIX_VR4120_MACC,
903   FIX_VR4120_DMACC,
904   FIX_VR4120_MULT,
905   FIX_VR4120_DMULT,
906   FIX_VR4120_DIV,
907   FIX_VR4120_MTHILO,
908   NUM_FIX_VR4120_CLASSES
909 };
910 
911 /* ...likewise -mfix-loongson2f-jump.  */
912 static bfd_boolean mips_fix_loongson2f_jump;
913 
914 /* ...likewise -mfix-loongson2f-nop.  */
915 static bfd_boolean mips_fix_loongson2f_nop;
916 
917 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed.  */
918 static bfd_boolean mips_fix_loongson2f;
919 
920 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
921    there must be at least one other instruction between an instruction
922    of type X and an instruction of type Y.  */
923 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
924 
925 /* True if -mfix-vr4120 is in force.  */
926 static int mips_fix_vr4120;
927 
928 /* ...likewise -mfix-vr4130.  */
929 static int mips_fix_vr4130;
930 
931 /* ...likewise -mfix-24k.  */
932 static int mips_fix_24k;
933 
934 /* ...likewise -mfix-rm7000  */
935 static int mips_fix_rm7000;
936 
937 /* ...likewise -mfix-cn63xxp1 */
938 static bfd_boolean mips_fix_cn63xxp1;
939 
940 /* We don't relax branches by default, since this causes us to expand
941    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
942    fail to compute the offset before expanding the macro to the most
943    efficient expansion.  */
944 
945 static int mips_relax_branch;
946 
947 /* The expansion of many macros depends on the type of symbol that
948    they refer to.  For example, when generating position-dependent code,
949    a macro that refers to a symbol may have two different expansions,
950    one which uses GP-relative addresses and one which uses absolute
951    addresses.  When generating SVR4-style PIC, a macro may have
952    different expansions for local and global symbols.
953 
954    We handle these situations by generating both sequences and putting
955    them in variant frags.  In position-dependent code, the first sequence
956    will be the GP-relative one and the second sequence will be the
957    absolute one.  In SVR4 PIC, the first sequence will be for global
958    symbols and the second will be for local symbols.
959 
960    The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
961    SECOND are the lengths of the two sequences in bytes.  These fields
962    can be extracted using RELAX_FIRST() and RELAX_SECOND().  In addition,
963    the subtype has the following flags:
964 
965    RELAX_USE_SECOND
966 	Set if it has been decided that we should use the second
967 	sequence instead of the first.
968 
969    RELAX_SECOND_LONGER
970 	Set in the first variant frag if the macro's second implementation
971 	is longer than its first.  This refers to the macro as a whole,
972 	not an individual relaxation.
973 
974    RELAX_NOMACRO
975 	Set in the first variant frag if the macro appeared in a .set nomacro
976 	block and if one alternative requires a warning but the other does not.
977 
978    RELAX_DELAY_SLOT
979 	Like RELAX_NOMACRO, but indicates that the macro appears in a branch
980 	delay slot.
981 
982    RELAX_DELAY_SLOT_16BIT
983 	Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
984 	16-bit instruction.
985 
986    RELAX_DELAY_SLOT_SIZE_FIRST
987 	Like RELAX_DELAY_SLOT, but indicates that the first implementation of
988 	the macro is of the wrong size for the branch delay slot.
989 
990    RELAX_DELAY_SLOT_SIZE_SECOND
991 	Like RELAX_DELAY_SLOT, but indicates that the second implementation of
992 	the macro is of the wrong size for the branch delay slot.
993 
994    The frag's "opcode" points to the first fixup for relaxable code.
995 
996    Relaxable macros are generated using a sequence such as:
997 
998       relax_start (SYMBOL);
999       ... generate first expansion ...
1000       relax_switch ();
1001       ... generate second expansion ...
1002       relax_end ();
1003 
1004    The code and fixups for the unwanted alternative are discarded
1005    by md_convert_frag.  */
1006 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
1007 
1008 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1009 #define RELAX_SECOND(X) ((X) & 0xff)
1010 #define RELAX_USE_SECOND 0x10000
1011 #define RELAX_SECOND_LONGER 0x20000
1012 #define RELAX_NOMACRO 0x40000
1013 #define RELAX_DELAY_SLOT 0x80000
1014 #define RELAX_DELAY_SLOT_16BIT 0x100000
1015 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
1016 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
1017 
1018 /* Branch without likely bit.  If label is out of range, we turn:
1019 
1020  	beq reg1, reg2, label
1021 	delay slot
1022 
1023    into
1024 
1025         bne reg1, reg2, 0f
1026         nop
1027         j label
1028      0: delay slot
1029 
1030    with the following opcode replacements:
1031 
1032 	beq <-> bne
1033 	blez <-> bgtz
1034 	bltz <-> bgez
1035 	bc1f <-> bc1t
1036 
1037 	bltzal <-> bgezal  (with jal label instead of j label)
1038 
1039    Even though keeping the delay slot instruction in the delay slot of
1040    the branch would be more efficient, it would be very tricky to do
1041    correctly, because we'd have to introduce a variable frag *after*
1042    the delay slot instruction, and expand that instead.  Let's do it
1043    the easy way for now, even if the branch-not-taken case now costs
1044    one additional instruction.  Out-of-range branches are not supposed
1045    to be common, anyway.
1046 
1047    Branch likely.  If label is out of range, we turn:
1048 
1049 	beql reg1, reg2, label
1050 	delay slot (annulled if branch not taken)
1051 
1052    into
1053 
1054         beql reg1, reg2, 1f
1055         nop
1056         beql $0, $0, 2f
1057         nop
1058      1: j[al] label
1059         delay slot (executed only if branch taken)
1060      2:
1061 
1062    It would be possible to generate a shorter sequence by losing the
1063    likely bit, generating something like:
1064 
1065 	bne reg1, reg2, 0f
1066 	nop
1067 	j[al] label
1068 	delay slot (executed only if branch taken)
1069      0:
1070 
1071 	beql -> bne
1072 	bnel -> beq
1073 	blezl -> bgtz
1074 	bgtzl -> blez
1075 	bltzl -> bgez
1076 	bgezl -> bltz
1077 	bc1fl -> bc1t
1078 	bc1tl -> bc1f
1079 
1080 	bltzall -> bgezal  (with jal label instead of j label)
1081 	bgezall -> bltzal  (ditto)
1082 
1083 
1084    but it's not clear that it would actually improve performance.  */
1085 #define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar)	\
1086   ((relax_substateT)						\
1087    (0xc0000000							\
1088     | ((at) & 0x1f)						\
1089     | ((toofar) ? 0x20 : 0)					\
1090     | ((link) ? 0x40 : 0)					\
1091     | ((likely) ? 0x80 : 0)					\
1092     | ((uncond) ? 0x100 : 0)))
1093 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1094 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1095 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1096 #define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1097 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1098 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1099 
1100 /* For mips16 code, we use an entirely different form of relaxation.
1101    mips16 supports two versions of most instructions which take
1102    immediate values: a small one which takes some small value, and a
1103    larger one which takes a 16 bit value.  Since branches also follow
1104    this pattern, relaxing these values is required.
1105 
1106    We can assemble both mips16 and normal MIPS code in a single
1107    object.  Therefore, we need to support this type of relaxation at
1108    the same time that we support the relaxation described above.  We
1109    use the high bit of the subtype field to distinguish these cases.
1110 
1111    The information we store for this type of relaxation is the
1112    argument code found in the opcode file for this relocation, whether
1113    the user explicitly requested a small or extended form, and whether
1114    the relocation is in a jump or jal delay slot.  That tells us the
1115    size of the value, and how it should be stored.  We also store
1116    whether the fragment is considered to be extended or not.  We also
1117    store whether this is known to be a branch to a different section,
1118    whether we have tried to relax this frag yet, and whether we have
1119    ever extended a PC relative fragment because of a shift count.  */
1120 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot)	\
1121   (0x80000000							\
1122    | ((type) & 0xff)						\
1123    | ((small) ? 0x100 : 0)					\
1124    | ((ext) ? 0x200 : 0)					\
1125    | ((dslot) ? 0x400 : 0)					\
1126    | ((jal_dslot) ? 0x800 : 0))
1127 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1128 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1129 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1130 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1131 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1132 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1133 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1134 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1135 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1136 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1137 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1138 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
1139 
1140 /* For microMIPS code, we use relaxation similar to one we use for
1141    MIPS16 code.  Some instructions that take immediate values support
1142    two encodings: a small one which takes some small value, and a
1143    larger one which takes a 16 bit value.  As some branches also follow
1144    this pattern, relaxing these values is required.
1145 
1146    We can assemble both microMIPS and normal MIPS code in a single
1147    object.  Therefore, we need to support this type of relaxation at
1148    the same time that we support the relaxation described above.  We
1149    use one of the high bits of the subtype field to distinguish these
1150    cases.
1151 
1152    The information we store for this type of relaxation is the argument
1153    code found in the opcode file for this relocation, the register
1154    selected as the assembler temporary, whether the branch is
1155    unconditional, whether it is compact, whether it stores the link
1156    address implicitly in $ra, whether relaxation of out-of-range 32-bit
1157    branches to a sequence of instructions is enabled, and whether the
1158    displacement of a branch is too large to fit as an immediate argument
1159    of a 16-bit and a 32-bit branch, respectively.  */
1160 #define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link,	\
1161 			       relax32, toofar16, toofar32)	\
1162   (0x40000000							\
1163    | ((type) & 0xff)						\
1164    | (((at) & 0x1f) << 8)					\
1165    | ((uncond) ? 0x2000 : 0)					\
1166    | ((compact) ? 0x4000 : 0)					\
1167    | ((link) ? 0x8000 : 0)					\
1168    | ((relax32) ? 0x10000 : 0)					\
1169    | ((toofar16) ? 0x20000 : 0)					\
1170    | ((toofar32) ? 0x40000 : 0))
1171 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1172 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1173 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1174 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1175 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1176 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1177 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1178 
1179 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1180 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1181 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1182 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1183 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1184 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
1185 
1186 /* Sign-extend 16-bit value X.  */
1187 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1188 
1189 /* Is the given value a sign-extended 32-bit value?  */
1190 #define IS_SEXT_32BIT_NUM(x)						\
1191   (((x) &~ (offsetT) 0x7fffffff) == 0					\
1192    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1193 
1194 /* Is the given value a sign-extended 16-bit value?  */
1195 #define IS_SEXT_16BIT_NUM(x)						\
1196   (((x) &~ (offsetT) 0x7fff) == 0					\
1197    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1198 
1199 /* Is the given value a sign-extended 12-bit value?  */
1200 #define IS_SEXT_12BIT_NUM(x)						\
1201   (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1202 
1203 /* Is the given value a sign-extended 9-bit value?  */
1204 #define IS_SEXT_9BIT_NUM(x)						\
1205   (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1206 
1207 /* Is the given value a zero-extended 32-bit value?  Or a negated one?  */
1208 #define IS_ZEXT_32BIT_NUM(x)						\
1209   (((x) &~ (offsetT) 0xffffffff) == 0					\
1210    || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1211 
1212 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1213    SHIFT places.  */
1214 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1215   (((STRUCT) >> (SHIFT)) & (MASK))
1216 
1217 /* Extract the operand given by FIELD from mips_cl_insn INSN.  */
1218 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1219   (!(MICROMIPS) \
1220    ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1221    : EXTRACT_BITS ((INSN).insn_opcode, \
1222 		   MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1223 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1224   EXTRACT_BITS ((INSN).insn_opcode, \
1225 		MIPS16OP_MASK_##FIELD, \
1226 		MIPS16OP_SH_##FIELD)
1227 
1228 /* The MIPS16 EXTEND opcode, shifted left 16 places.  */
1229 #define MIPS16_EXTEND (0xf000U << 16)
1230 
1231 /* Whether or not we are emitting a branch-likely macro.  */
1232 static bfd_boolean emit_branch_likely_macro = FALSE;
1233 
1234 /* Global variables used when generating relaxable macros.  See the
1235    comment above RELAX_ENCODE for more details about how relaxation
1236    is used.  */
1237 static struct {
1238   /* 0 if we're not emitting a relaxable macro.
1239      1 if we're emitting the first of the two relaxation alternatives.
1240      2 if we're emitting the second alternative.  */
1241   int sequence;
1242 
1243   /* The first relaxable fixup in the current frag.  (In other words,
1244      the first fixup that refers to relaxable code.)  */
1245   fixS *first_fixup;
1246 
1247   /* sizes[0] says how many bytes of the first alternative are stored in
1248      the current frag.  Likewise sizes[1] for the second alternative.  */
1249   unsigned int sizes[2];
1250 
1251   /* The symbol on which the choice of sequence depends.  */
1252   symbolS *symbol;
1253 } mips_relax;
1254 
1255 /* Global variables used to decide whether a macro needs a warning.  */
1256 static struct {
1257   /* True if the macro is in a branch delay slot.  */
1258   bfd_boolean delay_slot_p;
1259 
1260   /* Set to the length in bytes required if the macro is in a delay slot
1261      that requires a specific length of instruction, otherwise zero.  */
1262   unsigned int delay_slot_length;
1263 
1264   /* For relaxable macros, sizes[0] is the length of the first alternative
1265      in bytes and sizes[1] is the length of the second alternative.
1266      For non-relaxable macros, both elements give the length of the
1267      macro in bytes.  */
1268   unsigned int sizes[2];
1269 
1270   /* For relaxable macros, first_insn_sizes[0] is the length of the first
1271      instruction of the first alternative in bytes and first_insn_sizes[1]
1272      is the length of the first instruction of the second alternative.
1273      For non-relaxable macros, both elements give the length of the first
1274      instruction in bytes.
1275 
1276      Set to zero if we haven't yet seen the first instruction.  */
1277   unsigned int first_insn_sizes[2];
1278 
1279   /* For relaxable macros, insns[0] is the number of instructions for the
1280      first alternative and insns[1] is the number of instructions for the
1281      second alternative.
1282 
1283      For non-relaxable macros, both elements give the number of
1284      instructions for the macro.  */
1285   unsigned int insns[2];
1286 
1287   /* The first variant frag for this macro.  */
1288   fragS *first_frag;
1289 } mips_macro_warning;
1290 
1291 /* Prototypes for static functions.  */
1292 
1293 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1294 
1295 static void append_insn
1296   (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1297    bfd_boolean expansionp);
1298 static void mips_no_prev_insn (void);
1299 static void macro_build (expressionS *, const char *, const char *, ...);
1300 static void mips16_macro_build
1301   (expressionS *, const char *, const char *, va_list *);
1302 static void load_register (int, expressionS *, int);
1303 static void macro_start (void);
1304 static void macro_end (void);
1305 static void macro (struct mips_cl_insn *ip, char *str);
1306 static void mips16_macro (struct mips_cl_insn * ip);
1307 static void mips_ip (char *str, struct mips_cl_insn * ip);
1308 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1309 static void mips16_immed
1310   (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1311    unsigned int, unsigned long *);
1312 static size_t my_getSmallExpression
1313   (expressionS *, bfd_reloc_code_real_type *, char *);
1314 static void my_getExpression (expressionS *, char *);
1315 static void s_align (int);
1316 static void s_change_sec (int);
1317 static void s_change_section (int);
1318 static void s_cons (int);
1319 static void s_float_cons (int);
1320 static void s_mips_globl (int);
1321 static void s_option (int);
1322 static void s_mipsset (int);
1323 static void s_abicalls (int);
1324 static void s_cpload (int);
1325 static void s_cpsetup (int);
1326 static void s_cplocal (int);
1327 static void s_cprestore (int);
1328 static void s_cpreturn (int);
1329 static void s_dtprelword (int);
1330 static void s_dtpreldword (int);
1331 static void s_tprelword (int);
1332 static void s_tpreldword (int);
1333 static void s_gpvalue (int);
1334 static void s_gpword (int);
1335 static void s_gpdword (int);
1336 static void s_ehword (int);
1337 static void s_cpadd (int);
1338 static void s_insn (int);
1339 static void s_nan (int);
1340 static void s_module (int);
1341 static void s_mips_ent (int);
1342 static void s_mips_end (int);
1343 static void s_mips_frame (int);
1344 static void s_mips_mask (int reg_type);
1345 static void s_mips_stab (int);
1346 static void s_mips_weakext (int);
1347 static void s_mips_file (int);
1348 static void s_mips_loc (int);
1349 static bfd_boolean pic_need_relax (symbolS *, asection *);
1350 static int relaxed_branch_length (fragS *, asection *, int);
1351 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1352 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1353 static void file_mips_check_options (void);
1354 
1355 /* Table and functions used to map between CPU/ISA names, and
1356    ISA levels, and CPU numbers.  */
1357 
1358 struct mips_cpu_info
1359 {
1360   const char *name;           /* CPU or ISA name.  */
1361   int flags;                  /* MIPS_CPU_* flags.  */
1362   int ase;                    /* Set of ASEs implemented by the CPU.  */
1363   int isa;                    /* ISA level.  */
1364   int cpu;                    /* CPU number (default CPU if ISA).  */
1365 };
1366 
1367 #define MIPS_CPU_IS_ISA		0x0001	/* Is this an ISA?  (If 0, a CPU.) */
1368 
1369 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1370 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1371 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1372 
1373 /* Command-line options.  */
1374 const char *md_shortopts = "O::g::G:";
1375 
1376 enum options
1377   {
1378     OPTION_MARCH = OPTION_MD_BASE,
1379     OPTION_MTUNE,
1380     OPTION_MIPS1,
1381     OPTION_MIPS2,
1382     OPTION_MIPS3,
1383     OPTION_MIPS4,
1384     OPTION_MIPS5,
1385     OPTION_MIPS32,
1386     OPTION_MIPS64,
1387     OPTION_MIPS32R2,
1388     OPTION_MIPS32R3,
1389     OPTION_MIPS32R5,
1390     OPTION_MIPS32R6,
1391     OPTION_MIPS64R2,
1392     OPTION_MIPS64R3,
1393     OPTION_MIPS64R5,
1394     OPTION_MIPS64R6,
1395     OPTION_MIPS16,
1396     OPTION_NO_MIPS16,
1397     OPTION_MIPS3D,
1398     OPTION_NO_MIPS3D,
1399     OPTION_MDMX,
1400     OPTION_NO_MDMX,
1401     OPTION_DSP,
1402     OPTION_NO_DSP,
1403     OPTION_MT,
1404     OPTION_NO_MT,
1405     OPTION_VIRT,
1406     OPTION_NO_VIRT,
1407     OPTION_MSA,
1408     OPTION_NO_MSA,
1409     OPTION_SMARTMIPS,
1410     OPTION_NO_SMARTMIPS,
1411     OPTION_DSPR2,
1412     OPTION_NO_DSPR2,
1413     OPTION_DSPR6,
1414     OPTION_NO_DSPR6,
1415     OPTION_EVA,
1416     OPTION_NO_EVA,
1417     OPTION_XPA,
1418     OPTION_NO_XPA,
1419     OPTION_MXU,
1420     OPTION_NO_MXU,
1421     OPTION_MICROMIPS,
1422     OPTION_NO_MICROMIPS,
1423     OPTION_MCU,
1424     OPTION_NO_MCU,
1425     OPTION_COMPAT_ARCH_BASE,
1426     OPTION_M4650,
1427     OPTION_NO_M4650,
1428     OPTION_M4010,
1429     OPTION_NO_M4010,
1430     OPTION_M4100,
1431     OPTION_NO_M4100,
1432     OPTION_M3900,
1433     OPTION_NO_M3900,
1434     OPTION_M7000_HILO_FIX,
1435     OPTION_MNO_7000_HILO_FIX,
1436     OPTION_FIX_24K,
1437     OPTION_NO_FIX_24K,
1438     OPTION_FIX_RM7000,
1439     OPTION_NO_FIX_RM7000,
1440     OPTION_FIX_LOONGSON2F_JUMP,
1441     OPTION_NO_FIX_LOONGSON2F_JUMP,
1442     OPTION_FIX_LOONGSON2F_NOP,
1443     OPTION_NO_FIX_LOONGSON2F_NOP,
1444     OPTION_FIX_VR4120,
1445     OPTION_NO_FIX_VR4120,
1446     OPTION_FIX_VR4130,
1447     OPTION_NO_FIX_VR4130,
1448     OPTION_FIX_CN63XXP1,
1449     OPTION_NO_FIX_CN63XXP1,
1450     OPTION_TRAP,
1451     OPTION_BREAK,
1452     OPTION_EB,
1453     OPTION_EL,
1454     OPTION_FP32,
1455     OPTION_GP32,
1456     OPTION_CONSTRUCT_FLOATS,
1457     OPTION_NO_CONSTRUCT_FLOATS,
1458     OPTION_FP64,
1459     OPTION_FPXX,
1460     OPTION_GP64,
1461     OPTION_RELAX_BRANCH,
1462     OPTION_NO_RELAX_BRANCH,
1463     OPTION_INSN32,
1464     OPTION_NO_INSN32,
1465     OPTION_MSHARED,
1466     OPTION_MNO_SHARED,
1467     OPTION_MSYM32,
1468     OPTION_MNO_SYM32,
1469     OPTION_SOFT_FLOAT,
1470     OPTION_HARD_FLOAT,
1471     OPTION_SINGLE_FLOAT,
1472     OPTION_DOUBLE_FLOAT,
1473     OPTION_32,
1474     OPTION_CALL_SHARED,
1475     OPTION_CALL_NONPIC,
1476     OPTION_NON_SHARED,
1477     OPTION_XGOT,
1478     OPTION_MABI,
1479     OPTION_N32,
1480     OPTION_64,
1481     OPTION_MDEBUG,
1482     OPTION_NO_MDEBUG,
1483     OPTION_PDR,
1484     OPTION_NO_PDR,
1485     OPTION_MVXWORKS_PIC,
1486     OPTION_NAN,
1487     OPTION_ODD_SPREG,
1488     OPTION_NO_ODD_SPREG,
1489     OPTION_END_OF_ENUM
1490   };
1491 
1492 struct option md_longopts[] =
1493 {
1494   /* Options which specify architecture.  */
1495   {"march", required_argument, NULL, OPTION_MARCH},
1496   {"mtune", required_argument, NULL, OPTION_MTUNE},
1497   {"mips0", no_argument, NULL, OPTION_MIPS1},
1498   {"mips1", no_argument, NULL, OPTION_MIPS1},
1499   {"mips2", no_argument, NULL, OPTION_MIPS2},
1500   {"mips3", no_argument, NULL, OPTION_MIPS3},
1501   {"mips4", no_argument, NULL, OPTION_MIPS4},
1502   {"mips5", no_argument, NULL, OPTION_MIPS5},
1503   {"mips32", no_argument, NULL, OPTION_MIPS32},
1504   {"mips64", no_argument, NULL, OPTION_MIPS64},
1505   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1506   {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1507   {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
1508   {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
1509   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1510   {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1511   {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
1512   {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
1513 
1514   /* Options which specify Application Specific Extensions (ASEs).  */
1515   {"mips16", no_argument, NULL, OPTION_MIPS16},
1516   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1517   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1518   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1519   {"mdmx", no_argument, NULL, OPTION_MDMX},
1520   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1521   {"mdsp", no_argument, NULL, OPTION_DSP},
1522   {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1523   {"mmt", no_argument, NULL, OPTION_MT},
1524   {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1525   {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1526   {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1527   {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1528   {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1529   {"mdspr6", no_argument, NULL, OPTION_DSPR6},
1530   {"mno-dspr6", no_argument, NULL, OPTION_NO_DSPR6},
1531   {"meva", no_argument, NULL, OPTION_EVA},
1532   {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1533   {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1534   {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1535   {"mmcu", no_argument, NULL, OPTION_MCU},
1536   {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1537   {"mvirt", no_argument, NULL, OPTION_VIRT},
1538   {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1539   {"mmsa", no_argument, NULL, OPTION_MSA},
1540   {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
1541   {"mxpa", no_argument, NULL, OPTION_XPA},
1542   {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
1543   {"mmxu", no_argument, NULL, OPTION_MXU},
1544   {"mno-mxu", no_argument, NULL, OPTION_NO_MXU},
1545 
1546   /* Old-style architecture options.  Don't add more of these.  */
1547   {"m4650", no_argument, NULL, OPTION_M4650},
1548   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1549   {"m4010", no_argument, NULL, OPTION_M4010},
1550   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1551   {"m4100", no_argument, NULL, OPTION_M4100},
1552   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1553   {"m3900", no_argument, NULL, OPTION_M3900},
1554   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1555 
1556   /* Options which enable bug fixes.  */
1557   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1558   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1559   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1560   {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1561   {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1562   {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1563   {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1564   {"mfix-vr4120",    no_argument, NULL, OPTION_FIX_VR4120},
1565   {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1566   {"mfix-vr4130",    no_argument, NULL, OPTION_FIX_VR4130},
1567   {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1568   {"mfix-24k",    no_argument, NULL, OPTION_FIX_24K},
1569   {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1570   {"mfix-rm7000",    no_argument, NULL, OPTION_FIX_RM7000},
1571   {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
1572   {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1573   {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1574 
1575   /* Miscellaneous options.  */
1576   {"trap", no_argument, NULL, OPTION_TRAP},
1577   {"no-break", no_argument, NULL, OPTION_TRAP},
1578   {"break", no_argument, NULL, OPTION_BREAK},
1579   {"no-trap", no_argument, NULL, OPTION_BREAK},
1580   {"EB", no_argument, NULL, OPTION_EB},
1581   {"EL", no_argument, NULL, OPTION_EL},
1582   {"mfp32", no_argument, NULL, OPTION_FP32},
1583   {"mgp32", no_argument, NULL, OPTION_GP32},
1584   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1585   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1586   {"mfp64", no_argument, NULL, OPTION_FP64},
1587   {"mfpxx", no_argument, NULL, OPTION_FPXX},
1588   {"mgp64", no_argument, NULL, OPTION_GP64},
1589   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1590   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1591   {"minsn32", no_argument, NULL, OPTION_INSN32},
1592   {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1593   {"mshared", no_argument, NULL, OPTION_MSHARED},
1594   {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1595   {"msym32", no_argument, NULL, OPTION_MSYM32},
1596   {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1597   {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1598   {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1599   {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1600   {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1601   {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1602   {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
1603 
1604   /* Strictly speaking this next option is ELF specific,
1605      but we allow it for other ports as well in order to
1606      make testing easier.  */
1607   {"32", no_argument, NULL, OPTION_32},
1608 
1609   /* ELF-specific options.  */
1610   {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1611   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1612   {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1613   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
1614   {"xgot", no_argument, NULL, OPTION_XGOT},
1615   {"mabi", required_argument, NULL, OPTION_MABI},
1616   {"n32", no_argument, NULL, OPTION_N32},
1617   {"64", no_argument, NULL, OPTION_64},
1618   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1619   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1620   {"mpdr", no_argument, NULL, OPTION_PDR},
1621   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1622   {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1623   {"mnan", required_argument, NULL, OPTION_NAN},
1624 
1625   {NULL, no_argument, NULL, 0}
1626 };
1627 size_t md_longopts_size = sizeof (md_longopts);
1628 
1629 /* Information about either an Application Specific Extension or an
1630    optional architecture feature that, for simplicity, we treat in the
1631    same way as an ASE.  */
1632 struct mips_ase
1633 {
1634   /* The name of the ASE, used in both the command-line and .set options.  */
1635   const char *name;
1636 
1637   /* The associated ASE_* flags.  If the ASE is available on both 32-bit
1638      and 64-bit architectures, the flags here refer to the subset that
1639      is available on both.  */
1640   unsigned int flags;
1641 
1642   /* The ASE_* flag used for instructions that are available on 64-bit
1643      architectures but that are not included in FLAGS.  */
1644   unsigned int flags64;
1645 
1646   /* The command-line options that turn the ASE on and off.  */
1647   int option_on;
1648   int option_off;
1649 
1650   /* The minimum required architecture revisions for MIPS32, MIPS64,
1651      microMIPS32 and microMIPS64, or -1 if the extension isn't supported.  */
1652   int mips32_rev;
1653   int mips64_rev;
1654   int micromips32_rev;
1655   int micromips64_rev;
1656 
1657   /* The architecture where the ASE was removed or -1 if the extension has not
1658      been removed.  */
1659   int rem_rev;
1660 };
1661 
1662 /* A table of all supported ASEs.  */
1663 static const struct mips_ase mips_ases[] = {
1664   { "dsp", ASE_DSP, ASE_DSP64,
1665     OPTION_DSP, OPTION_NO_DSP,
1666     2, 2, 2, 2,
1667     -1 },
1668 
1669   { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1670     OPTION_DSPR2, OPTION_NO_DSPR2,
1671     2, 2, 2, 2,
1672     -1 },
1673 
1674   { "dspr6", ASE_DSP | ASE_DSPR2 | ASE_DSPR6, 0,
1675     OPTION_DSPR6, OPTION_NO_DSPR6,
1676     6, 6, 6, 6,
1677     -1 },
1678 
1679   { "eva", ASE_EVA, 0,
1680     OPTION_EVA, OPTION_NO_EVA,
1681      2,  2,  2,  2,
1682     -1 },
1683 
1684   { "mcu", ASE_MCU, 0,
1685     OPTION_MCU, OPTION_NO_MCU,
1686      2,  2,  2,  2,
1687     -1 },
1688 
1689   /* Deprecated in MIPS64r5, but we don't implement that yet.  */
1690   { "mdmx", ASE_MDMX, 0,
1691     OPTION_MDMX, OPTION_NO_MDMX,
1692     -1, 1, -1, -1,
1693      6 },
1694 
1695   /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2.  */
1696   { "mips3d", ASE_MIPS3D, 0,
1697     OPTION_MIPS3D, OPTION_NO_MIPS3D,
1698     2, 1, -1, -1,
1699     6 },
1700 
1701   { "mt", ASE_MT, 0,
1702     OPTION_MT, OPTION_NO_MT,
1703      2,  2, -1, -1,
1704     -1 },
1705 
1706   { "smartmips", ASE_SMARTMIPS, 0,
1707     OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1708     1, -1, -1, -1,
1709     6 },
1710 
1711   { "virt", ASE_VIRT, ASE_VIRT64,
1712     OPTION_VIRT, OPTION_NO_VIRT,
1713      2,  2,  2,  2,
1714     -1 },
1715 
1716   { "msa", ASE_MSA, ASE_MSA64,
1717     OPTION_MSA, OPTION_NO_MSA,
1718      2,  2,  2,  2,
1719     -1 },
1720 
1721   { "xpa", ASE_XPA, 0,
1722     OPTION_XPA, OPTION_NO_XPA,
1723      2,  2, -1, -1,
1724     -1 },
1725 
1726   { "mxu", ASE_MXU, 0,
1727     OPTION_MXU, OPTION_NO_MXU,
1728      1,  1, -1, -1,
1729     -1 },
1730 };
1731 
1732 /* The set of ASEs that require -mfp64.  */
1733 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
1734 
1735 /* Groups of ASE_* flags that represent different revisions of an ASE.  */
1736 static const unsigned int mips_ase_groups[] = {
1737   ASE_DSP | ASE_DSPR2 | ASE_DSPR6
1738 };
1739 
1740 /* Pseudo-op table.
1741 
1742    The following pseudo-ops from the Kane and Heinrich MIPS book
1743    should be defined here, but are currently unsupported: .alias,
1744    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1745 
1746    The following pseudo-ops from the Kane and Heinrich MIPS book are
1747    specific to the type of debugging information being generated, and
1748    should be defined by the object format: .aent, .begin, .bend,
1749    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1750    .vreg.
1751 
1752    The following pseudo-ops from the Kane and Heinrich MIPS book are
1753    not MIPS CPU specific, but are also not specific to the object file
1754    format.  This file is probably the best place to define them, but
1755    they are not currently supported: .asm0, .endr, .lab, .struct.  */
1756 
1757 static const pseudo_typeS mips_pseudo_table[] =
1758 {
1759   /* MIPS specific pseudo-ops.  */
1760   {"option", s_option, 0},
1761   {"set", s_mipsset, 0},
1762   {"rdata", s_change_sec, 'r'},
1763   {"sdata", s_change_sec, 's'},
1764   {"livereg", s_ignore, 0},
1765   {"abicalls", s_abicalls, 0},
1766   {"cpload", s_cpload, 0},
1767   {"cpsetup", s_cpsetup, 0},
1768   {"cplocal", s_cplocal, 0},
1769   {"cprestore", s_cprestore, 0},
1770   {"cpreturn", s_cpreturn, 0},
1771   {"dtprelword", s_dtprelword, 0},
1772   {"dtpreldword", s_dtpreldword, 0},
1773   {"tprelword", s_tprelword, 0},
1774   {"tpreldword", s_tpreldword, 0},
1775   {"gpvalue", s_gpvalue, 0},
1776   {"gpword", s_gpword, 0},
1777   {"gpdword", s_gpdword, 0},
1778   {"ehword", s_ehword, 0},
1779   {"cpadd", s_cpadd, 0},
1780   {"insn", s_insn, 0},
1781   {"nan", s_nan, 0},
1782   {"module", s_module, 0},
1783 
1784   /* Relatively generic pseudo-ops that happen to be used on MIPS
1785      chips.  */
1786   {"asciiz", stringer, 8 + 1},
1787   {"bss", s_change_sec, 'b'},
1788   {"err", s_err, 0},
1789   {"half", s_cons, 1},
1790   {"dword", s_cons, 3},
1791   {"weakext", s_mips_weakext, 0},
1792   {"origin", s_org, 0},
1793   {"repeat", s_rept, 0},
1794 
1795   /* For MIPS this is non-standard, but we define it for consistency.  */
1796   {"sbss", s_change_sec, 'B'},
1797 
1798   /* These pseudo-ops are defined in read.c, but must be overridden
1799      here for one reason or another.  */
1800   {"align", s_align, 0},
1801   {"byte", s_cons, 0},
1802   {"data", s_change_sec, 'd'},
1803   {"double", s_float_cons, 'd'},
1804   {"float", s_float_cons, 'f'},
1805   {"globl", s_mips_globl, 0},
1806   {"global", s_mips_globl, 0},
1807   {"hword", s_cons, 1},
1808   {"int", s_cons, 2},
1809   {"long", s_cons, 2},
1810   {"octa", s_cons, 4},
1811   {"quad", s_cons, 3},
1812   {"section", s_change_section, 0},
1813   {"short", s_cons, 1},
1814   {"single", s_float_cons, 'f'},
1815   {"stabd", s_mips_stab, 'd'},
1816   {"stabn", s_mips_stab, 'n'},
1817   {"stabs", s_mips_stab, 's'},
1818   {"text", s_change_sec, 't'},
1819   {"word", s_cons, 2},
1820 
1821   { "extern", ecoff_directive_extern, 0},
1822 
1823   { NULL, NULL, 0 },
1824 };
1825 
1826 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1827 {
1828   /* These pseudo-ops should be defined by the object file format.
1829      However, a.out doesn't support them, so we have versions here.  */
1830   {"aent", s_mips_ent, 1},
1831   {"bgnb", s_ignore, 0},
1832   {"end", s_mips_end, 0},
1833   {"endb", s_ignore, 0},
1834   {"ent", s_mips_ent, 0},
1835   {"file", s_mips_file, 0},
1836   {"fmask", s_mips_mask, 'F'},
1837   {"frame", s_mips_frame, 0},
1838   {"loc", s_mips_loc, 0},
1839   {"mask", s_mips_mask, 'R'},
1840   {"verstamp", s_ignore, 0},
1841   { NULL, NULL, 0 },
1842 };
1843 
1844 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1845    purpose of the `.dc.a' internal pseudo-op.  */
1846 
1847 int
mips_address_bytes(void)1848 mips_address_bytes (void)
1849 {
1850   file_mips_check_options ();
1851   return HAVE_64BIT_ADDRESSES ? 8 : 4;
1852 }
1853 
1854 extern void pop_insert (const pseudo_typeS *);
1855 
1856 void
mips_pop_insert(void)1857 mips_pop_insert (void)
1858 {
1859   pop_insert (mips_pseudo_table);
1860   if (! ECOFF_DEBUGGING)
1861     pop_insert (mips_nonecoff_pseudo_table);
1862 }
1863 
1864 /* Symbols labelling the current insn.  */
1865 
1866 struct insn_label_list
1867 {
1868   struct insn_label_list *next;
1869   symbolS *label;
1870 };
1871 
1872 static struct insn_label_list *free_insn_labels;
1873 #define label_list tc_segment_info_data.labels
1874 
1875 static void mips_clear_insn_labels (void);
1876 static void mips_mark_labels (void);
1877 static void mips_compressed_mark_labels (void);
1878 
1879 static inline void
mips_clear_insn_labels(void)1880 mips_clear_insn_labels (void)
1881 {
1882   struct insn_label_list **pl;
1883   segment_info_type *si;
1884 
1885   if (now_seg)
1886     {
1887       for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1888 	;
1889 
1890       si = seg_info (now_seg);
1891       *pl = si->label_list;
1892       si->label_list = NULL;
1893     }
1894 }
1895 
1896 /* Mark instruction labels in MIPS16/microMIPS mode.  */
1897 
1898 static inline void
mips_mark_labels(void)1899 mips_mark_labels (void)
1900 {
1901   if (HAVE_CODE_COMPRESSION)
1902     mips_compressed_mark_labels ();
1903 }
1904 
1905 static char *expr_end;
1906 
1907 /* An expression in a macro instruction.  This is set by mips_ip and
1908    mips16_ip and when populated is always an O_constant.  */
1909 
1910 static expressionS imm_expr;
1911 
1912 /* The relocatable field in an instruction and the relocs associated
1913    with it.  These variables are used for instructions like LUI and
1914    JAL as well as true offsets.  They are also used for address
1915    operands in macros.  */
1916 
1917 static expressionS offset_expr;
1918 static bfd_reloc_code_real_type offset_reloc[3]
1919   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1920 
1921 /* This is set to the resulting size of the instruction to be produced
1922    by mips16_ip if an explicit extension is used or by mips_ip if an
1923    explicit size is supplied.  */
1924 
1925 static unsigned int forced_insn_length;
1926 
1927 /* True if we are assembling an instruction.  All dot symbols defined during
1928    this time should be treated as code labels.  */
1929 
1930 static bfd_boolean mips_assembling_insn;
1931 
1932 /* The pdr segment for per procedure frame/regmask info.  Not used for
1933    ECOFF debugging.  */
1934 
1935 static segT pdr_seg;
1936 
1937 /* The default target format to use.  */
1938 
1939 #if defined (TE_FreeBSD)
1940 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1941 #elif defined (TE_TMIPS)
1942 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1943 #else
1944 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1945 #endif
1946 
1947 const char *
mips_target_format(void)1948 mips_target_format (void)
1949 {
1950   switch (OUTPUT_FLAVOR)
1951     {
1952     case bfd_target_elf_flavour:
1953 #ifdef TE_VXWORKS
1954       if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1955 	return (target_big_endian
1956 		? "elf32-bigmips-vxworks"
1957 		: "elf32-littlemips-vxworks");
1958 #endif
1959       return (target_big_endian
1960 	      ? (HAVE_64BIT_OBJECTS
1961 		 ? ELF_TARGET ("elf64-", "big")
1962 		 : (HAVE_NEWABI
1963 		    ? ELF_TARGET ("elf32-n", "big")
1964 		    : ELF_TARGET ("elf32-", "big")))
1965 	      : (HAVE_64BIT_OBJECTS
1966 		 ? ELF_TARGET ("elf64-", "little")
1967 		 : (HAVE_NEWABI
1968 		    ? ELF_TARGET ("elf32-n", "little")
1969 		    : ELF_TARGET ("elf32-", "little"))));
1970     default:
1971       abort ();
1972       return NULL;
1973     }
1974 }
1975 
1976 /* Return the ISA revision that is currently in use, or 0 if we are
1977    generating code for MIPS V or below.  */
1978 
1979 static int
mips_isa_rev(void)1980 mips_isa_rev (void)
1981 {
1982   if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
1983     return 2;
1984 
1985   if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
1986     return 3;
1987 
1988   if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
1989     return 5;
1990 
1991   if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
1992     return 6;
1993 
1994   /* microMIPS implies revision 2 or above.  */
1995   if (mips_opts.micromips)
1996     return 2;
1997 
1998   if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
1999     return 1;
2000 
2001   return 0;
2002 }
2003 
2004 /* Return the mask of all ASEs that are revisions of those in FLAGS.  */
2005 
2006 static unsigned int
mips_ase_mask(unsigned int flags)2007 mips_ase_mask (unsigned int flags)
2008 {
2009   unsigned int i;
2010 
2011   for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2012     if (flags & mips_ase_groups[i])
2013       flags |= mips_ase_groups[i];
2014   return flags;
2015 }
2016 
2017 /* Check whether the current ISA supports ASE.  Issue a warning if
2018    appropriate.  */
2019 
2020 static void
mips_check_isa_supports_ase(const struct mips_ase * ase)2021 mips_check_isa_supports_ase (const struct mips_ase *ase)
2022 {
2023   const char *base;
2024   int min_rev, size;
2025   static unsigned int warned_isa;
2026   static unsigned int warned_fp32;
2027 
2028   if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2029     min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2030   else
2031     min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2032   if ((min_rev < 0 || mips_isa_rev () < min_rev)
2033       && (warned_isa & ase->flags) != ase->flags)
2034     {
2035       warned_isa |= ase->flags;
2036       base = mips_opts.micromips ? "microMIPS" : "MIPS";
2037       size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2038       if (min_rev < 0)
2039 	as_warn (_("the %d-bit %s architecture does not support the"
2040 		   " `%s' extension"), size, base, ase->name);
2041       else
2042 	as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
2043 		 ase->name, base, size, min_rev);
2044     }
2045   else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2046 	   && (warned_isa & ase->flags) != ase->flags)
2047     {
2048       warned_isa |= ase->flags;
2049       base = mips_opts.micromips ? "microMIPS" : "MIPS";
2050       size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2051       as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2052 	       ase->name, base, size, ase->rem_rev);
2053     }
2054 
2055   if ((ase->flags & FP64_ASES)
2056       && mips_opts.fp != 64
2057       && (warned_fp32 & ase->flags) != ase->flags)
2058     {
2059       warned_fp32 |= ase->flags;
2060       as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
2061     }
2062 }
2063 
2064 /* Check all enabled ASEs to see whether they are supported by the
2065    chosen architecture.  */
2066 
2067 static void
mips_check_isa_supports_ases(void)2068 mips_check_isa_supports_ases (void)
2069 {
2070   unsigned int i, mask;
2071 
2072   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2073     {
2074       mask = mips_ase_mask (mips_ases[i].flags);
2075       if ((mips_opts.ase & mask) == mips_ases[i].flags)
2076 	mips_check_isa_supports_ase (&mips_ases[i]);
2077     }
2078 }
2079 
2080 /* Set the state of ASE to ENABLED_P.  Return the mask of ASE_* flags
2081    that were affected.  */
2082 
2083 static unsigned int
mips_set_ase(const struct mips_ase * ase,struct mips_set_options * opts,bfd_boolean enabled_p)2084 mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2085 	      bfd_boolean enabled_p)
2086 {
2087   unsigned int mask;
2088 
2089   mask = mips_ase_mask (ase->flags);
2090   opts->ase &= ~mask;
2091   if (enabled_p)
2092     opts->ase |= ase->flags;
2093   return mask;
2094 }
2095 
2096 /* Return the ASE called NAME, or null if none.  */
2097 
2098 static const struct mips_ase *
mips_lookup_ase(const char * name)2099 mips_lookup_ase (const char *name)
2100 {
2101   unsigned int i;
2102 
2103   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2104     if (strcmp (name, mips_ases[i].name) == 0)
2105       return &mips_ases[i];
2106   return NULL;
2107 }
2108 
2109 /* Return the length of a microMIPS instruction in bytes.  If bits of
2110    the mask beyond the low 16 are 0, then it is a 16-bit instruction.
2111    Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
2112    major opcode) will require further modifications to the opcode
2113    table.  */
2114 
2115 static inline unsigned int
micromips_insn_length(const struct mips_opcode * mo)2116 micromips_insn_length (const struct mips_opcode *mo)
2117 {
2118   return (mo->mask >> 16) == 0 ? 2 : 4;
2119 }
2120 
2121 /* Return the length of MIPS16 instruction OPCODE.  */
2122 
2123 static inline unsigned int
mips16_opcode_length(unsigned long opcode)2124 mips16_opcode_length (unsigned long opcode)
2125 {
2126   return (opcode >> 16) == 0 ? 2 : 4;
2127 }
2128 
2129 /* Return the length of instruction INSN.  */
2130 
2131 static inline unsigned int
insn_length(const struct mips_cl_insn * insn)2132 insn_length (const struct mips_cl_insn *insn)
2133 {
2134   if (mips_opts.micromips)
2135     return micromips_insn_length (insn->insn_mo);
2136   else if (mips_opts.mips16)
2137     return mips16_opcode_length (insn->insn_opcode);
2138   else
2139     return 4;
2140 }
2141 
2142 /* Initialise INSN from opcode entry MO.  Leave its position unspecified.  */
2143 
2144 static void
create_insn(struct mips_cl_insn * insn,const struct mips_opcode * mo)2145 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2146 {
2147   size_t i;
2148 
2149   insn->insn_mo = mo;
2150   insn->insn_opcode = mo->match;
2151   insn->frag = NULL;
2152   insn->where = 0;
2153   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2154     insn->fixp[i] = NULL;
2155   insn->fixed_p = (mips_opts.noreorder > 0);
2156   insn->noreorder_p = (mips_opts.noreorder > 0);
2157   insn->mips16_absolute_jump_p = 0;
2158   insn->complete_p = 0;
2159   insn->cleared_p = 0;
2160 }
2161 
2162 /* Get a list of all the operands in INSN.  */
2163 
2164 static const struct mips_operand_array *
insn_operands(const struct mips_cl_insn * insn)2165 insn_operands (const struct mips_cl_insn *insn)
2166 {
2167   if (insn->insn_mo >= &mips_opcodes[0]
2168       && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2169     return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2170 
2171   if (insn->insn_mo >= &mips16_opcodes[0]
2172       && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2173     return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2174 
2175   if (insn->insn_mo >= &micromips_opcodes[0]
2176       && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2177     return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2178 
2179   abort ();
2180 }
2181 
2182 /* Get a description of operand OPNO of INSN.  */
2183 
2184 static const struct mips_operand *
insn_opno(const struct mips_cl_insn * insn,unsigned opno)2185 insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2186 {
2187   const struct mips_operand_array *operands;
2188 
2189   operands = insn_operands (insn);
2190   if (opno >= MAX_OPERANDS || !operands->operand[opno])
2191     abort ();
2192   return operands->operand[opno];
2193 }
2194 
2195 /* Install UVAL as the value of OPERAND in INSN.  */
2196 
2197 static inline void
insn_insert_operand(struct mips_cl_insn * insn,const struct mips_operand * operand,unsigned int uval)2198 insn_insert_operand (struct mips_cl_insn *insn,
2199 		     const struct mips_operand *operand, unsigned int uval)
2200 {
2201   insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2202 }
2203 
2204 /* Extract the value of OPERAND from INSN.  */
2205 
2206 static inline unsigned
insn_extract_operand(const struct mips_cl_insn * insn,const struct mips_operand * operand)2207 insn_extract_operand (const struct mips_cl_insn *insn,
2208 		      const struct mips_operand *operand)
2209 {
2210   return mips_extract_operand (operand, insn->insn_opcode);
2211 }
2212 
2213 /* Record the current MIPS16/microMIPS mode in now_seg.  */
2214 
2215 static void
mips_record_compressed_mode(void)2216 mips_record_compressed_mode (void)
2217 {
2218   segment_info_type *si;
2219 
2220   si = seg_info (now_seg);
2221   if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2222     si->tc_segment_info_data.mips16 = mips_opts.mips16;
2223   if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2224     si->tc_segment_info_data.micromips = mips_opts.micromips;
2225 }
2226 
2227 /* Read a standard MIPS instruction from BUF.  */
2228 
2229 static unsigned long
read_insn(char * buf)2230 read_insn (char *buf)
2231 {
2232   if (target_big_endian)
2233     return bfd_getb32 ((bfd_byte *) buf);
2234   else
2235     return bfd_getl32 ((bfd_byte *) buf);
2236 }
2237 
2238 /* Write standard MIPS instruction INSN to BUF.  Return a pointer to
2239    the next byte.  */
2240 
2241 static char *
write_insn(char * buf,unsigned int insn)2242 write_insn (char *buf, unsigned int insn)
2243 {
2244   md_number_to_chars (buf, insn, 4);
2245   return buf + 4;
2246 }
2247 
2248 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2249    has length LENGTH.  */
2250 
2251 static unsigned long
read_compressed_insn(char * buf,unsigned int length)2252 read_compressed_insn (char *buf, unsigned int length)
2253 {
2254   unsigned long insn;
2255   unsigned int i;
2256 
2257   insn = 0;
2258   for (i = 0; i < length; i += 2)
2259     {
2260       insn <<= 16;
2261       if (target_big_endian)
2262 	insn |= bfd_getb16 ((char *) buf);
2263       else
2264 	insn |= bfd_getl16 ((char *) buf);
2265       buf += 2;
2266     }
2267   return insn;
2268 }
2269 
2270 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2271    instruction is LENGTH bytes long.  Return a pointer to the next byte.  */
2272 
2273 static char *
write_compressed_insn(char * buf,unsigned int insn,unsigned int length)2274 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2275 {
2276   unsigned int i;
2277 
2278   for (i = 0; i < length; i += 2)
2279     md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2280   return buf + length;
2281 }
2282 
2283 /* Install INSN at the location specified by its "frag" and "where" fields.  */
2284 
2285 static void
install_insn(const struct mips_cl_insn * insn)2286 install_insn (const struct mips_cl_insn *insn)
2287 {
2288   char *f = insn->frag->fr_literal + insn->where;
2289   if (HAVE_CODE_COMPRESSION)
2290     write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2291   else
2292     write_insn (f, insn->insn_opcode);
2293   mips_record_compressed_mode ();
2294 }
2295 
2296 /* Move INSN to offset WHERE in FRAG.  Adjust the fixups accordingly
2297    and install the opcode in the new location.  */
2298 
2299 static void
move_insn(struct mips_cl_insn * insn,fragS * frag,long where)2300 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2301 {
2302   size_t i;
2303 
2304   insn->frag = frag;
2305   insn->where = where;
2306   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2307     if (insn->fixp[i] != NULL)
2308       {
2309 	insn->fixp[i]->fx_frag = frag;
2310 	insn->fixp[i]->fx_where = where;
2311       }
2312   install_insn (insn);
2313 }
2314 
2315 /* Add INSN to the end of the output.  */
2316 
2317 static void
add_fixed_insn(struct mips_cl_insn * insn)2318 add_fixed_insn (struct mips_cl_insn *insn)
2319 {
2320   char *f = frag_more (insn_length (insn));
2321   move_insn (insn, frag_now, f - frag_now->fr_literal);
2322 }
2323 
2324 /* Start a variant frag and move INSN to the start of the variant part,
2325    marking it as fixed.  The other arguments are as for frag_var.  */
2326 
2327 static void
add_relaxed_insn(struct mips_cl_insn * insn,int max_chars,int var,relax_substateT subtype,symbolS * symbol,offsetT offset)2328 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2329 		  relax_substateT subtype, symbolS *symbol, offsetT offset)
2330 {
2331   frag_grow (max_chars);
2332   move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2333   insn->fixed_p = 1;
2334   frag_var (rs_machine_dependent, max_chars, var,
2335 	    subtype, symbol, offset, NULL);
2336 }
2337 
2338 /* Insert N copies of INSN into the history buffer, starting at
2339    position FIRST.  Neither FIRST nor N need to be clipped.  */
2340 
2341 static void
insert_into_history(unsigned int first,unsigned int n,const struct mips_cl_insn * insn)2342 insert_into_history (unsigned int first, unsigned int n,
2343 		     const struct mips_cl_insn *insn)
2344 {
2345   if (mips_relax.sequence != 2)
2346     {
2347       unsigned int i;
2348 
2349       for (i = ARRAY_SIZE (history); i-- > first;)
2350 	if (i >= first + n)
2351 	  history[i] = history[i - n];
2352 	else
2353 	  history[i] = *insn;
2354     }
2355 }
2356 
2357 /* Clear the error in insn_error.  */
2358 
2359 static void
clear_insn_error(void)2360 clear_insn_error (void)
2361 {
2362   memset (&insn_error, 0, sizeof (insn_error));
2363 }
2364 
2365 /* Possibly record error message MSG for the current instruction.
2366    If the error is about a particular argument, ARGNUM is the 1-based
2367    number of that argument, otherwise it is 0.  FORMAT is the format
2368    of MSG.  Return true if MSG was used, false if the current message
2369    was kept.  */
2370 
2371 static bfd_boolean
set_insn_error_format(int argnum,enum mips_insn_error_format format,const char * msg)2372 set_insn_error_format (int argnum, enum mips_insn_error_format format,
2373 		       const char *msg)
2374 {
2375   if (argnum == 0)
2376     {
2377       /* Give priority to errors against specific arguments, and to
2378 	 the first whole-instruction message.  */
2379       if (insn_error.msg)
2380 	return FALSE;
2381     }
2382   else
2383     {
2384       /* Keep insn_error if it is against a later argument.  */
2385       if (argnum < insn_error.min_argnum)
2386 	return FALSE;
2387 
2388       /* If both errors are against the same argument but are different,
2389 	 give up on reporting a specific error for this argument.
2390 	 See the comment about mips_insn_error for details.  */
2391       if (argnum == insn_error.min_argnum
2392 	  && insn_error.msg
2393 	  && strcmp (insn_error.msg, msg) != 0)
2394 	{
2395 	  insn_error.msg = 0;
2396 	  insn_error.min_argnum += 1;
2397 	  return FALSE;
2398 	}
2399     }
2400   insn_error.min_argnum = argnum;
2401   insn_error.format = format;
2402   insn_error.msg = msg;
2403   return TRUE;
2404 }
2405 
2406 /* Record an instruction error with no % format fields.  ARGNUM and MSG are
2407    as for set_insn_error_format.  */
2408 
2409 static void
set_insn_error(int argnum,const char * msg)2410 set_insn_error (int argnum, const char *msg)
2411 {
2412   set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2413 }
2414 
2415 /* Record an instruction error with one %d field I.  ARGNUM and MSG are
2416    as for set_insn_error_format.  */
2417 
2418 static void
set_insn_error_i(int argnum,const char * msg,int i)2419 set_insn_error_i (int argnum, const char *msg, int i)
2420 {
2421   if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2422     insn_error.u.i = i;
2423 }
2424 
2425 /* Record an instruction error with two %s fields S1 and S2.  ARGNUM and MSG
2426    are as for set_insn_error_format.  */
2427 
2428 static void
set_insn_error_ss(int argnum,const char * msg,const char * s1,const char * s2)2429 set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2430 {
2431   if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2432     {
2433       insn_error.u.ss[0] = s1;
2434       insn_error.u.ss[1] = s2;
2435     }
2436 }
2437 
2438 /* Report the error in insn_error, which is against assembly code STR.  */
2439 
2440 static void
report_insn_error(const char * str)2441 report_insn_error (const char *str)
2442 {
2443   const char *msg;
2444 
2445   msg = ACONCAT ((insn_error.msg, " `%s'", NULL));
2446   switch (insn_error.format)
2447     {
2448     case ERR_FMT_PLAIN:
2449       as_bad (msg, str);
2450       break;
2451 
2452     case ERR_FMT_I:
2453       as_bad (msg, insn_error.u.i, str);
2454       break;
2455 
2456     case ERR_FMT_SS:
2457       as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2458       break;
2459     }
2460 }
2461 
2462 /* Initialize vr4120_conflicts.  There is a bit of duplication here:
2463    the idea is to make it obvious at a glance that each errata is
2464    included.  */
2465 
2466 static void
init_vr4120_conflicts(void)2467 init_vr4120_conflicts (void)
2468 {
2469 #define CONFLICT(FIRST, SECOND) \
2470     vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2471 
2472   /* Errata 21 - [D]DIV[U] after [D]MACC */
2473   CONFLICT (MACC, DIV);
2474   CONFLICT (DMACC, DIV);
2475 
2476   /* Errata 23 - Continuous DMULT[U]/DMACC instructions.  */
2477   CONFLICT (DMULT, DMULT);
2478   CONFLICT (DMULT, DMACC);
2479   CONFLICT (DMACC, DMULT);
2480   CONFLICT (DMACC, DMACC);
2481 
2482   /* Errata 24 - MT{LO,HI} after [D]MACC */
2483   CONFLICT (MACC, MTHILO);
2484   CONFLICT (DMACC, MTHILO);
2485 
2486   /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2487      instruction is executed immediately after a MACC or DMACC
2488      instruction, the result of [either instruction] is incorrect."  */
2489   CONFLICT (MACC, MULT);
2490   CONFLICT (MACC, DMULT);
2491   CONFLICT (DMACC, MULT);
2492   CONFLICT (DMACC, DMULT);
2493 
2494   /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2495      executed immediately after a DMULT, DMULTU, DIV, DIVU,
2496      DDIV or DDIVU instruction, the result of the MACC or
2497      DMACC instruction is incorrect.".  */
2498   CONFLICT (DMULT, MACC);
2499   CONFLICT (DMULT, DMACC);
2500   CONFLICT (DIV, MACC);
2501   CONFLICT (DIV, DMACC);
2502 
2503 #undef CONFLICT
2504 }
2505 
2506 struct regname {
2507   const char *name;
2508   unsigned int num;
2509 };
2510 
2511 #define RNUM_MASK	0x00000ff
2512 #define RTYPE_MASK	0x1ffff00
2513 #define RTYPE_NUM	0x0000100
2514 #define RTYPE_FPU	0x0000200
2515 #define RTYPE_FCC	0x0000400
2516 #define RTYPE_VEC	0x0000800
2517 #define RTYPE_GP	0x0001000
2518 #define RTYPE_CP0	0x0002000
2519 #define RTYPE_PC	0x0004000
2520 #define RTYPE_ACC	0x0008000
2521 #define RTYPE_CCC	0x0010000
2522 #define RTYPE_VI	0x0020000
2523 #define RTYPE_VF	0x0040000
2524 #define RTYPE_R5900_I	0x0080000
2525 #define RTYPE_R5900_Q	0x0100000
2526 #define RTYPE_R5900_R	0x0200000
2527 #define RTYPE_R5900_ACC	0x0400000
2528 #define RTYPE_MSA	0x0800000
2529 #define RTYPE_MXU	0x1000000
2530 #define RWARN		0x8000000
2531 
2532 #define GENERIC_REGISTER_NUMBERS \
2533     {"$0",	RTYPE_NUM | 0},  \
2534     {"$1",	RTYPE_NUM | 1},  \
2535     {"$2",	RTYPE_NUM | 2},  \
2536     {"$3",	RTYPE_NUM | 3},  \
2537     {"$4",	RTYPE_NUM | 4},  \
2538     {"$5",	RTYPE_NUM | 5},  \
2539     {"$6",	RTYPE_NUM | 6},  \
2540     {"$7",	RTYPE_NUM | 7},  \
2541     {"$8",	RTYPE_NUM | 8},  \
2542     {"$9",	RTYPE_NUM | 9},  \
2543     {"$10",	RTYPE_NUM | 10}, \
2544     {"$11",	RTYPE_NUM | 11}, \
2545     {"$12",	RTYPE_NUM | 12}, \
2546     {"$13",	RTYPE_NUM | 13}, \
2547     {"$14",	RTYPE_NUM | 14}, \
2548     {"$15",	RTYPE_NUM | 15}, \
2549     {"$16",	RTYPE_NUM | 16}, \
2550     {"$17",	RTYPE_NUM | 17}, \
2551     {"$18",	RTYPE_NUM | 18}, \
2552     {"$19",	RTYPE_NUM | 19}, \
2553     {"$20",	RTYPE_NUM | 20}, \
2554     {"$21",	RTYPE_NUM | 21}, \
2555     {"$22",	RTYPE_NUM | 22}, \
2556     {"$23",	RTYPE_NUM | 23}, \
2557     {"$24",	RTYPE_NUM | 24}, \
2558     {"$25",	RTYPE_NUM | 25}, \
2559     {"$26",	RTYPE_NUM | 26}, \
2560     {"$27",	RTYPE_NUM | 27}, \
2561     {"$28",	RTYPE_NUM | 28}, \
2562     {"$29",	RTYPE_NUM | 29}, \
2563     {"$30",	RTYPE_NUM | 30}, \
2564     {"$31",	RTYPE_NUM | 31}
2565 
2566 #define FPU_REGISTER_NAMES       \
2567     {"$f0",	RTYPE_FPU | 0},  \
2568     {"$f1",	RTYPE_FPU | 1},  \
2569     {"$f2",	RTYPE_FPU | 2},  \
2570     {"$f3",	RTYPE_FPU | 3},  \
2571     {"$f4",	RTYPE_FPU | 4},  \
2572     {"$f5",	RTYPE_FPU | 5},  \
2573     {"$f6",	RTYPE_FPU | 6},  \
2574     {"$f7",	RTYPE_FPU | 7},  \
2575     {"$f8",	RTYPE_FPU | 8},  \
2576     {"$f9",	RTYPE_FPU | 9},  \
2577     {"$f10",	RTYPE_FPU | 10}, \
2578     {"$f11",	RTYPE_FPU | 11}, \
2579     {"$f12",	RTYPE_FPU | 12}, \
2580     {"$f13",	RTYPE_FPU | 13}, \
2581     {"$f14",	RTYPE_FPU | 14}, \
2582     {"$f15",	RTYPE_FPU | 15}, \
2583     {"$f16",	RTYPE_FPU | 16}, \
2584     {"$f17",	RTYPE_FPU | 17}, \
2585     {"$f18",	RTYPE_FPU | 18}, \
2586     {"$f19",	RTYPE_FPU | 19}, \
2587     {"$f20",	RTYPE_FPU | 20}, \
2588     {"$f21",	RTYPE_FPU | 21}, \
2589     {"$f22",	RTYPE_FPU | 22}, \
2590     {"$f23",	RTYPE_FPU | 23}, \
2591     {"$f24",	RTYPE_FPU | 24}, \
2592     {"$f25",	RTYPE_FPU | 25}, \
2593     {"$f26",	RTYPE_FPU | 26}, \
2594     {"$f27",	RTYPE_FPU | 27}, \
2595     {"$f28",	RTYPE_FPU | 28}, \
2596     {"$f29",	RTYPE_FPU | 29}, \
2597     {"$f30",	RTYPE_FPU | 30}, \
2598     {"$f31",	RTYPE_FPU | 31}
2599 
2600 #define FPU_CONDITION_CODE_NAMES \
2601     {"$fcc0",	RTYPE_FCC | 0},  \
2602     {"$fcc1",	RTYPE_FCC | 1},  \
2603     {"$fcc2",	RTYPE_FCC | 2},  \
2604     {"$fcc3",	RTYPE_FCC | 3},  \
2605     {"$fcc4",	RTYPE_FCC | 4},  \
2606     {"$fcc5",	RTYPE_FCC | 5},  \
2607     {"$fcc6",	RTYPE_FCC | 6},  \
2608     {"$fcc7",	RTYPE_FCC | 7}
2609 
2610 #define COPROC_CONDITION_CODE_NAMES         \
2611     {"$cc0",	RTYPE_FCC | RTYPE_CCC | 0}, \
2612     {"$cc1",	RTYPE_FCC | RTYPE_CCC | 1}, \
2613     {"$cc2",	RTYPE_FCC | RTYPE_CCC | 2}, \
2614     {"$cc3",	RTYPE_FCC | RTYPE_CCC | 3}, \
2615     {"$cc4",	RTYPE_FCC | RTYPE_CCC | 4}, \
2616     {"$cc5",	RTYPE_FCC | RTYPE_CCC | 5}, \
2617     {"$cc6",	RTYPE_FCC | RTYPE_CCC | 6}, \
2618     {"$cc7",	RTYPE_FCC | RTYPE_CCC | 7}
2619 
2620 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2621     {"$a4",	RTYPE_GP | 8},  \
2622     {"$a5",	RTYPE_GP | 9},  \
2623     {"$a6",	RTYPE_GP | 10}, \
2624     {"$a7",	RTYPE_GP | 11}, \
2625     {"$ta0",	RTYPE_GP | 8},  /* alias for $a4 */ \
2626     {"$ta1",	RTYPE_GP | 9},  /* alias for $a5 */ \
2627     {"$ta2",	RTYPE_GP | 10}, /* alias for $a6 */ \
2628     {"$ta3",	RTYPE_GP | 11}, /* alias for $a7 */ \
2629     {"$t0",	RTYPE_GP | 12}, \
2630     {"$t1",	RTYPE_GP | 13}, \
2631     {"$t2",	RTYPE_GP | 14}, \
2632     {"$t3",	RTYPE_GP | 15}
2633 
2634 #define O32_SYMBOLIC_REGISTER_NAMES \
2635     {"$t0",	RTYPE_GP | 8},  \
2636     {"$t1",	RTYPE_GP | 9},  \
2637     {"$t2",	RTYPE_GP | 10}, \
2638     {"$t3",	RTYPE_GP | 11}, \
2639     {"$t4",	RTYPE_GP | 12}, \
2640     {"$t5",	RTYPE_GP | 13}, \
2641     {"$t6",	RTYPE_GP | 14}, \
2642     {"$t7",	RTYPE_GP | 15}, \
2643     {"$ta0",	RTYPE_GP | 12}, /* alias for $t4 */ \
2644     {"$ta1",	RTYPE_GP | 13}, /* alias for $t5 */ \
2645     {"$ta2",	RTYPE_GP | 14}, /* alias for $t6 */ \
2646     {"$ta3",	RTYPE_GP | 15}  /* alias for $t7 */
2647 
2648 /* Remaining symbolic register names */
2649 #define SYMBOLIC_REGISTER_NAMES \
2650     {"$zero",	RTYPE_GP | 0},  \
2651     {"$at",	RTYPE_GP | 1},  \
2652     {"$AT",	RTYPE_GP | 1},  \
2653     {"$v0",	RTYPE_GP | 2},  \
2654     {"$v1",	RTYPE_GP | 3},  \
2655     {"$a0",	RTYPE_GP | 4},  \
2656     {"$a1",	RTYPE_GP | 5},  \
2657     {"$a2",	RTYPE_GP | 6},  \
2658     {"$a3",	RTYPE_GP | 7},  \
2659     {"$s0",	RTYPE_GP | 16}, \
2660     {"$s1",	RTYPE_GP | 17}, \
2661     {"$s2",	RTYPE_GP | 18}, \
2662     {"$s3",	RTYPE_GP | 19}, \
2663     {"$s4",	RTYPE_GP | 20}, \
2664     {"$s5",	RTYPE_GP | 21}, \
2665     {"$s6",	RTYPE_GP | 22}, \
2666     {"$s7",	RTYPE_GP | 23}, \
2667     {"$t8",	RTYPE_GP | 24}, \
2668     {"$t9",	RTYPE_GP | 25}, \
2669     {"$k0",	RTYPE_GP | 26}, \
2670     {"$kt0",	RTYPE_GP | 26}, \
2671     {"$k1",	RTYPE_GP | 27}, \
2672     {"$kt1",	RTYPE_GP | 27}, \
2673     {"$gp",	RTYPE_GP | 28}, \
2674     {"$sp",	RTYPE_GP | 29}, \
2675     {"$s8",	RTYPE_GP | 30}, \
2676     {"$fp",	RTYPE_GP | 30}, \
2677     {"$ra",	RTYPE_GP | 31}
2678 
2679 #define MIPS16_SPECIAL_REGISTER_NAMES \
2680     {"$pc",	RTYPE_PC | 0}
2681 
2682 #define MDMX_VECTOR_REGISTER_NAMES \
2683     /* {"$v0",	RTYPE_VEC | 0},  clash with REG 2 above */ \
2684     /* {"$v1",	RTYPE_VEC | 1},  clash with REG 3 above */ \
2685     {"$v2",	RTYPE_VEC | 2},  \
2686     {"$v3",	RTYPE_VEC | 3},  \
2687     {"$v4",	RTYPE_VEC | 4},  \
2688     {"$v5",	RTYPE_VEC | 5},  \
2689     {"$v6",	RTYPE_VEC | 6},  \
2690     {"$v7",	RTYPE_VEC | 7},  \
2691     {"$v8",	RTYPE_VEC | 8},  \
2692     {"$v9",	RTYPE_VEC | 9},  \
2693     {"$v10",	RTYPE_VEC | 10}, \
2694     {"$v11",	RTYPE_VEC | 11}, \
2695     {"$v12",	RTYPE_VEC | 12}, \
2696     {"$v13",	RTYPE_VEC | 13}, \
2697     {"$v14",	RTYPE_VEC | 14}, \
2698     {"$v15",	RTYPE_VEC | 15}, \
2699     {"$v16",	RTYPE_VEC | 16}, \
2700     {"$v17",	RTYPE_VEC | 17}, \
2701     {"$v18",	RTYPE_VEC | 18}, \
2702     {"$v19",	RTYPE_VEC | 19}, \
2703     {"$v20",	RTYPE_VEC | 20}, \
2704     {"$v21",	RTYPE_VEC | 21}, \
2705     {"$v22",	RTYPE_VEC | 22}, \
2706     {"$v23",	RTYPE_VEC | 23}, \
2707     {"$v24",	RTYPE_VEC | 24}, \
2708     {"$v25",	RTYPE_VEC | 25}, \
2709     {"$v26",	RTYPE_VEC | 26}, \
2710     {"$v27",	RTYPE_VEC | 27}, \
2711     {"$v28",	RTYPE_VEC | 28}, \
2712     {"$v29",	RTYPE_VEC | 29}, \
2713     {"$v30",	RTYPE_VEC | 30}, \
2714     {"$v31",	RTYPE_VEC | 31}
2715 
2716 #define R5900_I_NAMES \
2717     {"$I",	RTYPE_R5900_I | 0}
2718 
2719 #define R5900_Q_NAMES \
2720     {"$Q",	RTYPE_R5900_Q | 0}
2721 
2722 #define R5900_R_NAMES \
2723     {"$R",	RTYPE_R5900_R | 0}
2724 
2725 #define R5900_ACC_NAMES \
2726     {"$ACC",	RTYPE_R5900_ACC | 0 }
2727 
2728 #define MIPS_DSP_ACCUMULATOR_NAMES \
2729     {"$ac0",	RTYPE_ACC | 0}, \
2730     {"$ac1",	RTYPE_ACC | 1}, \
2731     {"$ac2",	RTYPE_ACC | 2}, \
2732     {"$ac3",	RTYPE_ACC | 3}
2733 
2734 #define MXU_REGISTER_NAMES \
2735     {"xr0",    RTYPE_MXU | 0},  \
2736     {"xr1",    RTYPE_MXU | 1},  \
2737     {"xr2",    RTYPE_MXU | 2},  \
2738     {"xr3",    RTYPE_MXU | 3},  \
2739     {"xr4",    RTYPE_MXU | 4},  \
2740     {"xr5",    RTYPE_MXU | 5},  \
2741     {"xr6",    RTYPE_MXU | 6},  \
2742     {"xr7",    RTYPE_MXU | 7},  \
2743     {"xr8",    RTYPE_MXU | 8},  \
2744     {"xr9",    RTYPE_MXU | 9},  \
2745     {"xr10",   RTYPE_MXU | 10}, \
2746     {"xr11",   RTYPE_MXU | 11}, \
2747     {"xr12",   RTYPE_MXU | 12}, \
2748     {"xr13",   RTYPE_MXU | 13}, \
2749     {"xr14",   RTYPE_MXU | 14}, \
2750     {"xr15",   RTYPE_MXU | 15}, \
2751     {"xr16",   RTYPE_MXU | 16}, \
2752     {"mxu_cr", RTYPE_MXU | 16}
2753 
2754 static const struct regname reg_names[] = {
2755   GENERIC_REGISTER_NUMBERS,
2756   FPU_REGISTER_NAMES,
2757   FPU_CONDITION_CODE_NAMES,
2758   COPROC_CONDITION_CODE_NAMES,
2759 
2760   /* The $txx registers depends on the abi,
2761      these will be added later into the symbol table from
2762      one of the tables below once mips_abi is set after
2763      parsing of arguments from the command line. */
2764   SYMBOLIC_REGISTER_NAMES,
2765 
2766   MIPS16_SPECIAL_REGISTER_NAMES,
2767   MDMX_VECTOR_REGISTER_NAMES,
2768   R5900_I_NAMES,
2769   R5900_Q_NAMES,
2770   R5900_R_NAMES,
2771   R5900_ACC_NAMES,
2772   MIPS_DSP_ACCUMULATOR_NAMES,
2773   MXU_REGISTER_NAMES,
2774   {0, 0}
2775 };
2776 
2777 static const struct regname reg_names_o32[] = {
2778   O32_SYMBOLIC_REGISTER_NAMES,
2779   {0, 0}
2780 };
2781 
2782 static const struct regname reg_names_n32n64[] = {
2783   N32N64_SYMBOLIC_REGISTER_NAMES,
2784   {0, 0}
2785 };
2786 
2787 /* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2788    interpreted as vector registers 0 and 1.  If SYMVAL is the value of one
2789    of these register symbols, return the associated vector register,
2790    otherwise return SYMVAL itself.  */
2791 
2792 static unsigned int
mips_prefer_vec_regno(unsigned int symval)2793 mips_prefer_vec_regno (unsigned int symval)
2794 {
2795   if ((symval & -2) == (RTYPE_GP | 2))
2796     return RTYPE_VEC | (symval & 1);
2797   return symval;
2798 }
2799 
2800 /* Return true if string [S, E) is a valid register name, storing its
2801    symbol value in *SYMVAL_PTR if so.  */
2802 
2803 static bfd_boolean
mips_parse_register_1(char * s,char * e,unsigned int * symval_ptr)2804 mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
2805 {
2806   char save_c;
2807   symbolS *symbol;
2808 
2809   /* Terminate name.  */
2810   save_c = *e;
2811   *e = '\0';
2812 
2813   /* Look up the name.  */
2814   symbol = symbol_find (s);
2815   *e = save_c;
2816 
2817   if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2818     return FALSE;
2819 
2820   *symval_ptr = S_GET_VALUE (symbol);
2821   return TRUE;
2822 }
2823 
2824 /* Return true if the string at *SPTR is a valid register name.  Allow it
2825    to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2826    is nonnull.
2827 
2828    When returning true, move *SPTR past the register, store the
2829    register's symbol value in *SYMVAL_PTR and the channel mask in
2830    *CHANNELS_PTR (if nonnull).  The symbol value includes the register
2831    number (RNUM_MASK) and register type (RTYPE_MASK).  The channel mask
2832    is a 4-bit value of the form XYZW and is 0 if no suffix was given.  */
2833 
2834 static bfd_boolean
mips_parse_register(char ** sptr,unsigned int * symval_ptr,unsigned int * channels_ptr)2835 mips_parse_register (char **sptr, unsigned int *symval_ptr,
2836 		     unsigned int *channels_ptr)
2837 {
2838   char *s, *e, *m;
2839   const char *q;
2840   unsigned int channels, symval, bit;
2841 
2842   /* Find end of name.  */
2843   s = e = *sptr;
2844   if (is_name_beginner (*e))
2845     ++e;
2846   while (is_part_of_name (*e))
2847     ++e;
2848 
2849   channels = 0;
2850   if (!mips_parse_register_1 (s, e, &symval))
2851     {
2852       if (!channels_ptr)
2853 	return FALSE;
2854 
2855       /* Eat characters from the end of the string that are valid
2856 	 channel suffixes.  The preceding register must be $ACC or
2857 	 end with a digit, so there is no ambiguity.  */
2858       bit = 1;
2859       m = e;
2860       for (q = "wzyx"; *q; q++, bit <<= 1)
2861 	if (m > s && m[-1] == *q)
2862 	  {
2863 	    --m;
2864 	    channels |= bit;
2865 	  }
2866 
2867       if (channels == 0
2868 	  || !mips_parse_register_1 (s, m, &symval)
2869 	  || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2870 	return FALSE;
2871     }
2872 
2873   *sptr = e;
2874   *symval_ptr = symval;
2875   if (channels_ptr)
2876     *channels_ptr = channels;
2877   return TRUE;
2878 }
2879 
2880 /* Check if SPTR points at a valid register specifier according to TYPES.
2881    If so, then return 1, advance S to consume the specifier and store
2882    the register's number in REGNOP, otherwise return 0.  */
2883 
2884 static int
reg_lookup(char ** s,unsigned int types,unsigned int * regnop)2885 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2886 {
2887   unsigned int regno;
2888 
2889   if (mips_parse_register (s, &regno, NULL))
2890     {
2891       if (types & RTYPE_VEC)
2892 	regno = mips_prefer_vec_regno (regno);
2893       if (regno & types)
2894 	regno &= RNUM_MASK;
2895       else
2896 	regno = ~0;
2897     }
2898   else
2899     {
2900       if (types & RWARN)
2901 	as_warn (_("unrecognized register name `%s'"), *s);
2902       regno = ~0;
2903     }
2904   if (regnop)
2905     *regnop = regno;
2906   return regno <= RNUM_MASK;
2907 }
2908 
2909 /* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2910    mask in *CHANNELS.  Return a pointer to the first unconsumed character.  */
2911 
2912 static char *
mips_parse_vu0_channels(char * s,unsigned int * channels)2913 mips_parse_vu0_channels (char *s, unsigned int *channels)
2914 {
2915   unsigned int i;
2916 
2917   *channels = 0;
2918   for (i = 0; i < 4; i++)
2919     if (*s == "xyzw"[i])
2920       {
2921 	*channels |= 1 << (3 - i);
2922 	++s;
2923       }
2924   return s;
2925 }
2926 
2927 /* Token types for parsed operand lists.  */
2928 enum mips_operand_token_type {
2929   /* A plain register, e.g. $f2.  */
2930   OT_REG,
2931 
2932   /* A 4-bit XYZW channel mask.  */
2933   OT_CHANNELS,
2934 
2935   /* A constant vector index, e.g. [1].  */
2936   OT_INTEGER_INDEX,
2937 
2938   /* A register vector index, e.g. [$2].  */
2939   OT_REG_INDEX,
2940 
2941   /* A continuous range of registers, e.g. $s0-$s4.  */
2942   OT_REG_RANGE,
2943 
2944   /* A (possibly relocated) expression.  */
2945   OT_INTEGER,
2946 
2947   /* A floating-point value.  */
2948   OT_FLOAT,
2949 
2950   /* A single character.  This can be '(', ')' or ',', but '(' only appears
2951      before OT_REGs.  */
2952   OT_CHAR,
2953 
2954   /* A doubled character, either "--" or "++".  */
2955   OT_DOUBLE_CHAR,
2956 
2957   /* The end of the operand list.  */
2958   OT_END
2959 };
2960 
2961 /* A parsed operand token.  */
2962 struct mips_operand_token
2963 {
2964   /* The type of token.  */
2965   enum mips_operand_token_type type;
2966   union
2967   {
2968     /* The register symbol value for an OT_REG or OT_REG_INDEX.  */
2969     unsigned int regno;
2970 
2971     /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX.  */
2972     unsigned int channels;
2973 
2974     /* The integer value of an OT_INTEGER_INDEX.  */
2975     addressT index;
2976 
2977     /* The two register symbol values involved in an OT_REG_RANGE.  */
2978     struct {
2979       unsigned int regno1;
2980       unsigned int regno2;
2981     } reg_range;
2982 
2983     /* The value of an OT_INTEGER.  The value is represented as an
2984        expression and the relocation operators that were applied to
2985        that expression.  The reloc entries are BFD_RELOC_UNUSED if no
2986        relocation operators were used.  */
2987     struct {
2988       expressionS value;
2989       bfd_reloc_code_real_type relocs[3];
2990     } integer;
2991 
2992     /* The binary data for an OT_FLOAT constant, and the number of bytes
2993        in the constant.  */
2994     struct {
2995       unsigned char data[8];
2996       int length;
2997     } flt;
2998 
2999     /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR.  */
3000     char ch;
3001   } u;
3002 };
3003 
3004 /* An obstack used to construct lists of mips_operand_tokens.  */
3005 static struct obstack mips_operand_tokens;
3006 
3007 /* Give TOKEN type TYPE and add it to mips_operand_tokens.  */
3008 
3009 static void
mips_add_token(struct mips_operand_token * token,enum mips_operand_token_type type)3010 mips_add_token (struct mips_operand_token *token,
3011 		enum mips_operand_token_type type)
3012 {
3013   token->type = type;
3014   obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3015 }
3016 
3017 /* Check whether S is '(' followed by a register name.  Add OT_CHAR
3018    and OT_REG tokens for them if so, and return a pointer to the first
3019    unconsumed character.  Return null otherwise.  */
3020 
3021 static char *
mips_parse_base_start(char * s)3022 mips_parse_base_start (char *s)
3023 {
3024   struct mips_operand_token token;
3025   unsigned int regno, channels;
3026   bfd_boolean decrement_p;
3027 
3028   if (*s != '(')
3029     return 0;
3030 
3031   ++s;
3032   SKIP_SPACE_TABS (s);
3033 
3034   /* Only match "--" as part of a base expression.  In other contexts "--X"
3035      is a double negative.  */
3036   decrement_p = (s[0] == '-' && s[1] == '-');
3037   if (decrement_p)
3038     {
3039       s += 2;
3040       SKIP_SPACE_TABS (s);
3041     }
3042 
3043   /* Allow a channel specifier because that leads to better error messages
3044      than treating something like "$vf0x++" as an expression.  */
3045   if (!mips_parse_register (&s, &regno, &channels))
3046     return 0;
3047 
3048   token.u.ch = '(';
3049   mips_add_token (&token, OT_CHAR);
3050 
3051   if (decrement_p)
3052     {
3053       token.u.ch = '-';
3054       mips_add_token (&token, OT_DOUBLE_CHAR);
3055     }
3056 
3057   token.u.regno = regno;
3058   mips_add_token (&token, OT_REG);
3059 
3060   if (channels)
3061     {
3062       token.u.channels = channels;
3063       mips_add_token (&token, OT_CHANNELS);
3064     }
3065 
3066   /* For consistency, only match "++" as part of base expressions too.  */
3067   SKIP_SPACE_TABS (s);
3068   if (s[0] == '+' && s[1] == '+')
3069     {
3070       s += 2;
3071       token.u.ch = '+';
3072       mips_add_token (&token, OT_DOUBLE_CHAR);
3073     }
3074 
3075   return s;
3076 }
3077 
3078 /* Parse one or more tokens from S.  Return a pointer to the first
3079    unconsumed character on success.  Return null if an error was found
3080    and store the error text in insn_error.  FLOAT_FORMAT is as for
3081    mips_parse_arguments.  */
3082 
3083 static char *
mips_parse_argument_token(char * s,char float_format)3084 mips_parse_argument_token (char *s, char float_format)
3085 {
3086   char *end, *save_in, *err;
3087   unsigned int regno1, regno2, channels;
3088   struct mips_operand_token token;
3089 
3090   /* First look for "($reg", since we want to treat that as an
3091      OT_CHAR and OT_REG rather than an expression.  */
3092   end = mips_parse_base_start (s);
3093   if (end)
3094     return end;
3095 
3096   /* Handle other characters that end up as OT_CHARs.  */
3097   if (*s == ')' || *s == ',')
3098     {
3099       token.u.ch = *s;
3100       mips_add_token (&token, OT_CHAR);
3101       ++s;
3102       return s;
3103     }
3104 
3105   /* Handle tokens that start with a register.  */
3106   if (mips_parse_register (&s, &regno1, &channels))
3107     {
3108       if (channels)
3109 	{
3110 	  /* A register and a VU0 channel suffix.  */
3111 	  token.u.regno = regno1;
3112 	  mips_add_token (&token, OT_REG);
3113 
3114 	  token.u.channels = channels;
3115 	  mips_add_token (&token, OT_CHANNELS);
3116 	  return s;
3117 	}
3118 
3119       SKIP_SPACE_TABS (s);
3120       if (*s == '-')
3121 	{
3122 	  /* A register range.  */
3123 	  ++s;
3124 	  SKIP_SPACE_TABS (s);
3125 	  if (!mips_parse_register (&s, &regno2, NULL))
3126 	    {
3127 	      set_insn_error (0, _("invalid register range"));
3128 	      return 0;
3129 	    }
3130 
3131 	  token.u.reg_range.regno1 = regno1;
3132 	  token.u.reg_range.regno2 = regno2;
3133 	  mips_add_token (&token, OT_REG_RANGE);
3134 	  return s;
3135 	}
3136 
3137       /* Add the register itself.  */
3138       token.u.regno = regno1;
3139       mips_add_token (&token, OT_REG);
3140 
3141       /* Check for a vector index.  */
3142       if (*s == '[')
3143 	{
3144 	  ++s;
3145 	  SKIP_SPACE_TABS (s);
3146 	  if (mips_parse_register (&s, &token.u.regno, NULL))
3147 	    mips_add_token (&token, OT_REG_INDEX);
3148 	  else
3149 	    {
3150 	      expressionS element;
3151 
3152 	      my_getExpression (&element, s);
3153 	      if (element.X_op != O_constant)
3154 		{
3155 		  set_insn_error (0, _("vector element must be constant"));
3156 		  return 0;
3157 		}
3158 	      s = expr_end;
3159 	      token.u.index = element.X_add_number;
3160 	      mips_add_token (&token, OT_INTEGER_INDEX);
3161 	    }
3162 	  SKIP_SPACE_TABS (s);
3163 	  if (*s != ']')
3164 	    {
3165 	      set_insn_error (0, _("missing `]'"));
3166 	      return 0;
3167 	    }
3168 	  ++s;
3169 	}
3170       return s;
3171     }
3172 
3173   if (float_format)
3174     {
3175       /* First try to treat expressions as floats.  */
3176       save_in = input_line_pointer;
3177       input_line_pointer = s;
3178       err = md_atof (float_format, (char *) token.u.flt.data,
3179 		     &token.u.flt.length);
3180       end = input_line_pointer;
3181       input_line_pointer = save_in;
3182       if (err && *err)
3183 	{
3184 	  set_insn_error (0, err);
3185 	  return 0;
3186 	}
3187       if (s != end)
3188 	{
3189 	  mips_add_token (&token, OT_FLOAT);
3190 	  return end;
3191 	}
3192     }
3193 
3194   /* Treat everything else as an integer expression.  */
3195   token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3196   token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3197   token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3198   my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3199   s = expr_end;
3200   mips_add_token (&token, OT_INTEGER);
3201   return s;
3202 }
3203 
3204 /* S points to the operand list for an instruction.  FLOAT_FORMAT is 'f'
3205    if expressions should be treated as 32-bit floating-point constants,
3206    'd' if they should be treated as 64-bit floating-point constants,
3207    or 0 if they should be treated as integer expressions (the usual case).
3208 
3209    Return a list of tokens on success, otherwise return 0.  The caller
3210    must obstack_free the list after use.  */
3211 
3212 static struct mips_operand_token *
mips_parse_arguments(char * s,char float_format)3213 mips_parse_arguments (char *s, char float_format)
3214 {
3215   struct mips_operand_token token;
3216 
3217   SKIP_SPACE_TABS (s);
3218   while (*s)
3219     {
3220       s = mips_parse_argument_token (s, float_format);
3221       if (!s)
3222 	{
3223 	  obstack_free (&mips_operand_tokens,
3224 			obstack_finish (&mips_operand_tokens));
3225 	  return 0;
3226 	}
3227       SKIP_SPACE_TABS (s);
3228     }
3229   mips_add_token (&token, OT_END);
3230   return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
3231 }
3232 
3233 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3234    and architecture.  Use is_opcode_valid_16 for MIPS16 opcodes.  */
3235 
3236 static bfd_boolean
is_opcode_valid(const struct mips_opcode * mo)3237 is_opcode_valid (const struct mips_opcode *mo)
3238 {
3239   int isa = mips_opts.isa;
3240   int ase = mips_opts.ase;
3241   int fp_s, fp_d;
3242   unsigned int i;
3243 
3244   if (ISA_HAS_64BIT_REGS (mips_opts.isa))
3245     for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3246       if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3247 	ase |= mips_ases[i].flags64;
3248 
3249   if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
3250     return FALSE;
3251 
3252   /* Check whether the instruction or macro requires single-precision or
3253      double-precision floating-point support.  Note that this information is
3254      stored differently in the opcode table for insns and macros.  */
3255   if (mo->pinfo == INSN_MACRO)
3256     {
3257       fp_s = mo->pinfo2 & INSN2_M_FP_S;
3258       fp_d = mo->pinfo2 & INSN2_M_FP_D;
3259     }
3260   else
3261     {
3262       fp_s = mo->pinfo & FP_S;
3263       fp_d = mo->pinfo & FP_D;
3264     }
3265 
3266   if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3267     return FALSE;
3268 
3269   if (fp_s && mips_opts.soft_float)
3270     return FALSE;
3271 
3272   return TRUE;
3273 }
3274 
3275 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
3276    selected ISA and architecture.  */
3277 
3278 static bfd_boolean
is_opcode_valid_16(const struct mips_opcode * mo)3279 is_opcode_valid_16 (const struct mips_opcode *mo)
3280 {
3281   return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
3282 }
3283 
3284 /* Return TRUE if the size of the microMIPS opcode MO matches one
3285    explicitly requested.  Always TRUE in the standard MIPS mode.  */
3286 
3287 static bfd_boolean
is_size_valid(const struct mips_opcode * mo)3288 is_size_valid (const struct mips_opcode *mo)
3289 {
3290   if (!mips_opts.micromips)
3291     return TRUE;
3292 
3293   if (mips_opts.insn32)
3294     {
3295       if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3296 	return FALSE;
3297       if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3298 	return FALSE;
3299     }
3300   if (!forced_insn_length)
3301     return TRUE;
3302   if (mo->pinfo == INSN_MACRO)
3303     return FALSE;
3304   return forced_insn_length == micromips_insn_length (mo);
3305 }
3306 
3307 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
3308    of the preceding instruction.  Always TRUE in the standard MIPS mode.
3309 
3310    We don't accept macros in 16-bit delay slots to avoid a case where
3311    a macro expansion fails because it relies on a preceding 32-bit real
3312    instruction to have matched and does not handle the operands correctly.
3313    The only macros that may expand to 16-bit instructions are JAL that
3314    cannot be placed in a delay slot anyway, and corner cases of BALIGN
3315    and BGT (that likewise cannot be placed in a delay slot) that decay to
3316    a NOP.  In all these cases the macros precede any corresponding real
3317    instruction definitions in the opcode table, so they will match in the
3318    second pass where the size of the delay slot is ignored and therefore
3319    produce correct code.  */
3320 
3321 static bfd_boolean
is_delay_slot_valid(const struct mips_opcode * mo)3322 is_delay_slot_valid (const struct mips_opcode *mo)
3323 {
3324   if (!mips_opts.micromips)
3325     return TRUE;
3326 
3327   if (mo->pinfo == INSN_MACRO)
3328     return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
3329   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3330       && micromips_insn_length (mo) != 4)
3331     return FALSE;
3332   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3333       && micromips_insn_length (mo) != 2)
3334     return FALSE;
3335 
3336   return TRUE;
3337 }
3338 
3339 /* For consistency checking, verify that all bits of OPCODE are specified
3340    either by the match/mask part of the instruction definition, or by the
3341    operand list.  Also build up a list of operands in OPERANDS.
3342 
3343    INSN_BITS says which bits of the instruction are significant.
3344    If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3345    provides the mips_operand description of each operand.  DECODE_OPERAND
3346    is null for MIPS16 instructions.  */
3347 
3348 static int
validate_mips_insn(const struct mips_opcode * opcode,unsigned long insn_bits,const struct mips_operand * (* decode_operand)(const char *),struct mips_operand_array * operands)3349 validate_mips_insn (const struct mips_opcode *opcode,
3350 		    unsigned long insn_bits,
3351 		    const struct mips_operand *(*decode_operand) (const char *),
3352 		    struct mips_operand_array *operands)
3353 {
3354   const char *s;
3355   unsigned long used_bits, doubled, undefined, opno, mask;
3356   const struct mips_operand *operand;
3357 
3358   mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3359   if ((mask & opcode->match) != opcode->match)
3360     {
3361       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3362 	      opcode->name, opcode->args);
3363       return 0;
3364     }
3365   used_bits = 0;
3366   opno = 0;
3367   if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3368     used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
3369   for (s = opcode->args; *s; ++s)
3370     switch (*s)
3371       {
3372       case ',':
3373       case '(':
3374       case ')':
3375 	break;
3376 
3377       case '#':
3378 	s++;
3379 	break;
3380 
3381       default:
3382 	if (!decode_operand)
3383 	  operand = decode_mips16_operand (*s, FALSE);
3384 	else
3385 	  operand = decode_operand (s);
3386 	if (!operand && opcode->pinfo != INSN_MACRO)
3387 	  {
3388 	    as_bad (_("internal: unknown operand type: %s %s"),
3389 		    opcode->name, opcode->args);
3390 	    return 0;
3391 	  }
3392 	gas_assert (opno < MAX_OPERANDS);
3393 	operands->operand[opno] = operand;
3394 	if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
3395 	  {
3396 	    used_bits = mips_insert_operand (operand, used_bits, -1);
3397 	    if (operand->type == OP_MDMX_IMM_REG)
3398 	      /* Bit 5 is the format selector (OB vs QH).  The opcode table
3399 		 has separate entries for each format.  */
3400 	      used_bits &= ~(1 << (operand->lsb + 5));
3401 	    if (operand->type == OP_ENTRY_EXIT_LIST)
3402 	      used_bits &= ~(mask & 0x700);
3403 	  }
3404 	/* Skip prefix characters.  */
3405 	if (decode_operand && (*s == '+' || *s == 'm' || *s == '-' || *s == '`'))
3406 	  ++s;
3407 	opno += 1;
3408 	break;
3409       }
3410   doubled = used_bits & mask & insn_bits;
3411   if (doubled)
3412     {
3413       as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3414 		" %s %s"), doubled, opcode->name, opcode->args);
3415       return 0;
3416     }
3417   used_bits |= mask;
3418   undefined = ~used_bits & insn_bits;
3419   if (opcode->pinfo != INSN_MACRO && undefined)
3420     {
3421       as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3422 	      undefined, opcode->name, opcode->args);
3423       return 0;
3424     }
3425   used_bits &= ~insn_bits;
3426   if (used_bits)
3427     {
3428       as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3429 	      used_bits, opcode->name, opcode->args);
3430       return 0;
3431     }
3432   return 1;
3433 }
3434 
3435 /* The MIPS16 version of validate_mips_insn.  */
3436 
3437 static int
validate_mips16_insn(const struct mips_opcode * opcode,struct mips_operand_array * operands)3438 validate_mips16_insn (const struct mips_opcode *opcode,
3439 		      struct mips_operand_array *operands)
3440 {
3441   if (opcode->args[0] == 'a' || opcode->args[0] == 'i')
3442     {
3443       /* In this case OPCODE defines the first 16 bits in a 32-bit jump
3444 	 instruction.  Use TMP to describe the full instruction.  */
3445       struct mips_opcode tmp;
3446 
3447       tmp = *opcode;
3448       tmp.match <<= 16;
3449       tmp.mask <<= 16;
3450       return validate_mips_insn (&tmp, 0xffffffff, 0, operands);
3451     }
3452   return validate_mips_insn (opcode, 0xffff, 0, operands);
3453 }
3454 
3455 /* The microMIPS version of validate_mips_insn.  */
3456 
3457 static int
validate_micromips_insn(const struct mips_opcode * opc,struct mips_operand_array * operands)3458 validate_micromips_insn (const struct mips_opcode *opc,
3459 			 struct mips_operand_array *operands)
3460 {
3461   unsigned long insn_bits;
3462   unsigned long major;
3463   unsigned int length;
3464 
3465   if (opc->pinfo == INSN_MACRO)
3466     return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3467 			       operands);
3468 
3469   length = micromips_insn_length (opc);
3470   if (length != 2 && length != 4)
3471     {
3472       as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
3473 		"%s %s"), length, opc->name, opc->args);
3474       return 0;
3475     }
3476   major = opc->match >> (10 + 8 * (length - 2));
3477   if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3478       || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3479     {
3480       as_bad (_("internal error: bad microMIPS opcode "
3481 		"(opcode/length mismatch): %s %s"), opc->name, opc->args);
3482       return 0;
3483     }
3484 
3485   /* Shift piecewise to avoid an overflow where unsigned long is 32-bit.  */
3486   insn_bits = 1 << 4 * length;
3487   insn_bits <<= 4 * length;
3488   insn_bits -= 1;
3489   return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3490 			     operands);
3491 }
3492 
3493 /* This function is called once, at assembler startup time.  It should set up
3494    all the tables, etc. that the MD part of the assembler will need.  */
3495 
3496 void
md_begin(void)3497 md_begin (void)
3498 {
3499   const char *retval = NULL;
3500   int i = 0;
3501   int broken = 0;
3502 
3503   if (mips_pic != NO_PIC)
3504     {
3505       if (g_switch_seen && g_switch_value != 0)
3506 	as_bad (_("-G may not be used in position-independent code"));
3507       g_switch_value = 0;
3508     }
3509 
3510   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3511     as_warn (_("could not set architecture and machine"));
3512 
3513   op_hash = hash_new ();
3514 
3515   mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
3516   for (i = 0; i < NUMOPCODES;)
3517     {
3518       const char *name = mips_opcodes[i].name;
3519 
3520       retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
3521       if (retval != NULL)
3522 	{
3523 	  fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3524 		   mips_opcodes[i].name, retval);
3525 	  /* Probably a memory allocation problem?  Give up now.  */
3526 	  as_fatal (_("broken assembler, no assembly attempted"));
3527 	}
3528       do
3529 	{
3530 	  if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3531 				   decode_mips_operand, &mips_operands[i]))
3532 	    broken = 1;
3533 	  if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3534 	    {
3535 	      create_insn (&nop_insn, mips_opcodes + i);
3536 	      if (mips_fix_loongson2f_nop)
3537 		nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3538 	      nop_insn.fixed_p = 1;
3539 	    }
3540 	  ++i;
3541 	}
3542       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3543     }
3544 
3545   mips16_op_hash = hash_new ();
3546   mips16_operands = XCNEWVEC (struct mips_operand_array,
3547 			      bfd_mips16_num_opcodes);
3548 
3549   i = 0;
3550   while (i < bfd_mips16_num_opcodes)
3551     {
3552       const char *name = mips16_opcodes[i].name;
3553 
3554       retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
3555       if (retval != NULL)
3556 	as_fatal (_("internal: can't hash `%s': %s"),
3557 		  mips16_opcodes[i].name, retval);
3558       do
3559 	{
3560 	  if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3561 	    broken = 1;
3562 	  if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3563 	    {
3564 	      create_insn (&mips16_nop_insn, mips16_opcodes + i);
3565 	      mips16_nop_insn.fixed_p = 1;
3566 	    }
3567 	  ++i;
3568 	}
3569       while (i < bfd_mips16_num_opcodes
3570 	     && strcmp (mips16_opcodes[i].name, name) == 0);
3571     }
3572 
3573   micromips_op_hash = hash_new ();
3574   micromips_operands = XCNEWVEC (struct mips_operand_array,
3575 				 bfd_micromips_num_opcodes);
3576 
3577   i = 0;
3578   while (i < bfd_micromips_num_opcodes)
3579     {
3580       const char *name = micromips_opcodes[i].name;
3581 
3582       retval = hash_insert (micromips_op_hash, name,
3583 			    (void *) &micromips_opcodes[i]);
3584       if (retval != NULL)
3585 	as_fatal (_("internal: can't hash `%s': %s"),
3586 		  micromips_opcodes[i].name, retval);
3587       do
3588 	{
3589 	  struct mips_cl_insn *micromips_nop_insn;
3590 
3591 	  if (!validate_micromips_insn (&micromips_opcodes[i],
3592 					&micromips_operands[i]))
3593 	    broken = 1;
3594 
3595 	  if (micromips_opcodes[i].pinfo != INSN_MACRO)
3596 	    {
3597 	      if (micromips_insn_length (micromips_opcodes + i) == 2)
3598 		micromips_nop_insn = &micromips_nop16_insn;
3599 	      else if (micromips_insn_length (micromips_opcodes + i) == 4)
3600 		micromips_nop_insn = &micromips_nop32_insn;
3601 	      else
3602 		continue;
3603 
3604 	      if (micromips_nop_insn->insn_mo == NULL
3605 		  && strcmp (name, "nop") == 0)
3606 		{
3607 		  create_insn (micromips_nop_insn, micromips_opcodes + i);
3608 		  micromips_nop_insn->fixed_p = 1;
3609 		}
3610 	    }
3611 	}
3612       while (++i < bfd_micromips_num_opcodes
3613 	     && strcmp (micromips_opcodes[i].name, name) == 0);
3614     }
3615 
3616   if (broken)
3617     as_fatal (_("broken assembler, no assembly attempted"));
3618 
3619   /* We add all the general register names to the symbol table.  This
3620      helps us detect invalid uses of them.  */
3621   for (i = 0; reg_names[i].name; i++)
3622     symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
3623 				     reg_names[i].num, /* & RNUM_MASK, */
3624 				     &zero_address_frag));
3625   if (HAVE_NEWABI)
3626     for (i = 0; reg_names_n32n64[i].name; i++)
3627       symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
3628 				       reg_names_n32n64[i].num, /* & RNUM_MASK, */
3629 				       &zero_address_frag));
3630   else
3631     for (i = 0; reg_names_o32[i].name; i++)
3632       symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
3633 				       reg_names_o32[i].num, /* & RNUM_MASK, */
3634 				       &zero_address_frag));
3635 
3636   for (i = 0; i < 32; i++)
3637     {
3638       char regname[7];
3639 
3640       /* R5900 VU0 floating-point register.  */
3641       regname[sizeof (rename) - 1] = 0;
3642       snprintf (regname, sizeof (regname) - 1, "$vf%d", i);
3643       symbol_table_insert (symbol_new (regname, reg_section,
3644 				       RTYPE_VF | i, &zero_address_frag));
3645 
3646       /* R5900 VU0 integer register.  */
3647       snprintf (regname, sizeof (regname) - 1, "$vi%d", i);
3648       symbol_table_insert (symbol_new (regname, reg_section,
3649 				       RTYPE_VI | i, &zero_address_frag));
3650 
3651       /* MSA register.  */
3652       snprintf (regname, sizeof (regname) - 1, "$w%d", i);
3653       symbol_table_insert (symbol_new (regname, reg_section,
3654 				       RTYPE_MSA | i, &zero_address_frag));
3655     }
3656 
3657   obstack_init (&mips_operand_tokens);
3658 
3659   mips_no_prev_insn ();
3660 
3661   mips_gprmask = 0;
3662   mips_cprmask[0] = 0;
3663   mips_cprmask[1] = 0;
3664   mips_cprmask[2] = 0;
3665   mips_cprmask[3] = 0;
3666 
3667   /* set the default alignment for the text section (2**2) */
3668   record_alignment (text_section, 2);
3669 
3670   bfd_set_gp_size (stdoutput, g_switch_value);
3671 
3672   /* On a native system other than VxWorks, sections must be aligned
3673      to 16 byte boundaries.  When configured for an embedded ELF
3674      target, we don't bother.  */
3675   if (strncmp (TARGET_OS, "elf", 3) != 0
3676       && strncmp (TARGET_OS, "vxworks", 7) != 0)
3677     {
3678       (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3679       (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3680       (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3681     }
3682 
3683   /* Create a .reginfo section for register masks and a .mdebug
3684      section for debugging information.  */
3685   {
3686     segT seg;
3687     subsegT subseg;
3688     flagword flags;
3689     segT sec;
3690 
3691     seg = now_seg;
3692     subseg = now_subseg;
3693 
3694     /* The ABI says this section should be loaded so that the
3695        running program can access it.  However, we don't load it
3696        if we are configured for an embedded target */
3697     flags = SEC_READONLY | SEC_DATA;
3698     if (strncmp (TARGET_OS, "elf", 3) != 0)
3699       flags |= SEC_ALLOC | SEC_LOAD;
3700 
3701     if (mips_abi != N64_ABI)
3702       {
3703 	sec = subseg_new (".reginfo", (subsegT) 0);
3704 
3705 	bfd_set_section_flags (stdoutput, sec, flags);
3706 	bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
3707 
3708 	mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3709       }
3710     else
3711       {
3712 	/* The 64-bit ABI uses a .MIPS.options section rather than
3713 	   .reginfo section.  */
3714 	sec = subseg_new (".MIPS.options", (subsegT) 0);
3715 	bfd_set_section_flags (stdoutput, sec, flags);
3716 	bfd_set_section_alignment (stdoutput, sec, 3);
3717 
3718 	/* Set up the option header.  */
3719 	{
3720 	  Elf_Internal_Options opthdr;
3721 	  char *f;
3722 
3723 	  opthdr.kind = ODK_REGINFO;
3724 	  opthdr.size = (sizeof (Elf_External_Options)
3725 			 + sizeof (Elf64_External_RegInfo));
3726 	  opthdr.section = 0;
3727 	  opthdr.info = 0;
3728 	  f = frag_more (sizeof (Elf_External_Options));
3729 	  bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3730 					 (Elf_External_Options *) f);
3731 
3732 	  mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3733 	}
3734       }
3735 
3736     sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3737     bfd_set_section_flags (stdoutput, sec,
3738 			   SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3739     bfd_set_section_alignment (stdoutput, sec, 3);
3740     mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3741 
3742     if (ECOFF_DEBUGGING)
3743       {
3744 	sec = subseg_new (".mdebug", (subsegT) 0);
3745 	(void) bfd_set_section_flags (stdoutput, sec,
3746 				      SEC_HAS_CONTENTS | SEC_READONLY);
3747 	(void) bfd_set_section_alignment (stdoutput, sec, 2);
3748       }
3749     else if (mips_flag_pdr)
3750       {
3751 	pdr_seg = subseg_new (".pdr", (subsegT) 0);
3752 	(void) bfd_set_section_flags (stdoutput, pdr_seg,
3753 				      SEC_READONLY | SEC_RELOC
3754 				      | SEC_DEBUGGING);
3755 	(void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3756       }
3757 
3758     subseg_set (seg, subseg);
3759   }
3760 
3761   if (mips_fix_vr4120)
3762     init_vr4120_conflicts ();
3763 }
3764 
3765 static inline void
fpabi_incompatible_with(int fpabi,const char * what)3766 fpabi_incompatible_with (int fpabi, const char *what)
3767 {
3768   as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3769 	   Tag_GNU_MIPS_ABI_FP, fpabi, what);
3770 }
3771 
3772 static inline void
fpabi_requires(int fpabi,const char * what)3773 fpabi_requires (int fpabi, const char *what)
3774 {
3775   as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3776 	   Tag_GNU_MIPS_ABI_FP, fpabi, what);
3777 }
3778 
3779 /* Check -mabi and register sizes against the specified FP ABI.  */
3780 static void
check_fpabi(int fpabi)3781 check_fpabi (int fpabi)
3782 {
3783   switch (fpabi)
3784     {
3785     case Val_GNU_MIPS_ABI_FP_DOUBLE:
3786       if (file_mips_opts.soft_float)
3787 	fpabi_incompatible_with (fpabi, "softfloat");
3788       else if (file_mips_opts.single_float)
3789 	fpabi_incompatible_with (fpabi, "singlefloat");
3790       if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3791 	fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3792       else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3793 	fpabi_incompatible_with (fpabi, "gp=32 fp=64");
3794       break;
3795 
3796     case Val_GNU_MIPS_ABI_FP_XX:
3797       if (mips_abi != O32_ABI)
3798 	fpabi_requires (fpabi, "-mabi=32");
3799       else if (file_mips_opts.soft_float)
3800 	fpabi_incompatible_with (fpabi, "softfloat");
3801       else if (file_mips_opts.single_float)
3802 	fpabi_incompatible_with (fpabi, "singlefloat");
3803       else if (file_mips_opts.fp != 0)
3804 	fpabi_requires (fpabi, "fp=xx");
3805       break;
3806 
3807     case Val_GNU_MIPS_ABI_FP_64A:
3808     case Val_GNU_MIPS_ABI_FP_64:
3809       if (mips_abi != O32_ABI)
3810 	fpabi_requires (fpabi, "-mabi=32");
3811       else if (file_mips_opts.soft_float)
3812 	fpabi_incompatible_with (fpabi, "softfloat");
3813       else if (file_mips_opts.single_float)
3814 	fpabi_incompatible_with (fpabi, "singlefloat");
3815       else if (file_mips_opts.fp != 64)
3816 	fpabi_requires (fpabi, "fp=64");
3817       else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3818 	fpabi_incompatible_with (fpabi, "nooddspreg");
3819       else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3820 	fpabi_requires (fpabi, "nooddspreg");
3821       break;
3822 
3823     case Val_GNU_MIPS_ABI_FP_SINGLE:
3824       if (file_mips_opts.soft_float)
3825 	fpabi_incompatible_with (fpabi, "softfloat");
3826       else if (!file_mips_opts.single_float)
3827 	fpabi_requires (fpabi, "singlefloat");
3828       break;
3829 
3830     case Val_GNU_MIPS_ABI_FP_SOFT:
3831       if (!file_mips_opts.soft_float)
3832 	fpabi_requires (fpabi, "softfloat");
3833       break;
3834 
3835     case Val_GNU_MIPS_ABI_FP_OLD_64:
3836       as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3837 	       Tag_GNU_MIPS_ABI_FP, fpabi);
3838       break;
3839 
3840     default:
3841       as_warn (_(".gnu_attribute %d,%d is not a recognized"
3842 	         " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3843       break;
3844     }
3845 }
3846 
3847 /* Perform consistency checks on the current options.  */
3848 
3849 static void
mips_check_options(struct mips_set_options * opts,bfd_boolean abi_checks)3850 mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3851 {
3852   /* Check the size of integer registers agrees with the ABI and ISA.  */
3853   if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3854     as_bad (_("`gp=64' used with a 32-bit processor"));
3855   else if (abi_checks
3856 	   && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3857     as_bad (_("`gp=32' used with a 64-bit ABI"));
3858   else if (abi_checks
3859 	   && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3860     as_bad (_("`gp=64' used with a 32-bit ABI"));
3861 
3862   /* Check the size of the float registers agrees with the ABI and ISA.  */
3863   switch (opts->fp)
3864     {
3865     case 0:
3866       if (!CPU_HAS_LDC1_SDC1 (opts->arch))
3867 	as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
3868       else if (opts->single_float == 1)
3869 	as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
3870       break;
3871     case 64:
3872       if (!ISA_HAS_64BIT_FPRS (opts->isa))
3873 	as_bad (_("`fp=64' used with a 32-bit fpu"));
3874       else if (abi_checks
3875 	       && ABI_NEEDS_32BIT_REGS (mips_abi)
3876 	       && !ISA_HAS_MXHC1 (opts->isa))
3877 	as_warn (_("`fp=64' used with a 32-bit ABI"));
3878       break;
3879     case 32:
3880       if (abi_checks
3881 	  && ABI_NEEDS_64BIT_REGS (mips_abi))
3882 	as_warn (_("`fp=32' used with a 64-bit ABI"));
3883       if (ISA_IS_R6 (mips_opts.isa) && opts->single_float == 0)
3884 	as_bad (_("`fp=32' used with a MIPS R6 cpu"));
3885       break;
3886     default:
3887       as_bad (_("Unknown size of floating point registers"));
3888       break;
3889     }
3890 
3891   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
3892     as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
3893 
3894   if (opts->micromips == 1 && opts->mips16 == 1)
3895     as_bad (_("`mips16' cannot be used with `micromips'"));
3896   else if (ISA_IS_R6 (mips_opts.isa)
3897 	   && (opts->micromips == 1
3898 	       || opts->mips16 == 1))
3899     as_fatal (_("`%s' can not be used with `%s'"),
3900 	      opts->micromips ? "micromips" : "mips16",
3901 	      mips_cpu_info_from_isa (mips_opts.isa)->name);
3902 
3903   if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
3904     as_fatal (_("branch relaxation is not supported in `%s'"),
3905 	      mips_cpu_info_from_isa (opts->isa)->name);
3906 }
3907 
3908 /* Perform consistency checks on the module level options exactly once.
3909    This is a deferred check that happens:
3910      at the first .set directive
3911      or, at the first pseudo op that generates code (inc .dc.a)
3912      or, at the first instruction
3913      or, at the end.  */
3914 
3915 static void
file_mips_check_options(void)3916 file_mips_check_options (void)
3917 {
3918   const struct mips_cpu_info *arch_info = 0;
3919 
3920   if (file_mips_opts_checked)
3921     return;
3922 
3923   /* The following code determines the register size.
3924      Similar code was added to GCC 3.3 (see override_options() in
3925      config/mips/mips.c).  The GAS and GCC code should be kept in sync
3926      as much as possible.  */
3927 
3928   if (file_mips_opts.gp < 0)
3929     {
3930       /* Infer the integer register size from the ABI and processor.
3931 	 Restrict ourselves to 32-bit registers if that's all the
3932 	 processor has, or if the ABI cannot handle 64-bit registers.  */
3933       file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
3934 			   || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
3935 			  ? 32 : 64;
3936     }
3937 
3938   if (file_mips_opts.fp < 0)
3939     {
3940       /* No user specified float register size.
3941 	 ??? GAS treats single-float processors as though they had 64-bit
3942 	 float registers (although it complains when double-precision
3943 	 instructions are used).  As things stand, saying they have 32-bit
3944 	 registers would lead to spurious "register must be even" messages.
3945 	 So here we assume float registers are never smaller than the
3946 	 integer ones.  */
3947       if (file_mips_opts.gp == 64)
3948 	/* 64-bit integer registers implies 64-bit float registers.  */
3949 	file_mips_opts.fp = 64;
3950       else if ((file_mips_opts.ase & FP64_ASES)
3951 	       && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
3952 	/* Handle ASEs that require 64-bit float registers, if possible.  */
3953 	file_mips_opts.fp = 64;
3954       else if (ISA_IS_R6 (mips_opts.isa))
3955 	/* R6 implies 64-bit float registers.  */
3956 	file_mips_opts.fp = 64;
3957       else
3958 	/* 32-bit float registers.  */
3959 	file_mips_opts.fp = 32;
3960     }
3961 
3962   arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
3963 
3964   /* Disable operations on odd-numbered floating-point registers by default
3965      when using the FPXX ABI.  */
3966   if (file_mips_opts.oddspreg < 0)
3967     {
3968       if (file_mips_opts.fp == 0)
3969 	file_mips_opts.oddspreg = 0;
3970       else
3971 	file_mips_opts.oddspreg = 1;
3972     }
3973 
3974   /* End of GCC-shared inference code.  */
3975 
3976   /* This flag is set when we have a 64-bit capable CPU but use only
3977      32-bit wide registers.  Note that EABI does not use it.  */
3978   if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
3979       && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
3980 	  || mips_abi == O32_ABI))
3981     mips_32bitmode = 1;
3982 
3983   if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
3984     as_bad (_("trap exception not supported at ISA 1"));
3985 
3986   /* If the selected architecture includes support for ASEs, enable
3987      generation of code for them.  */
3988   if (file_mips_opts.mips16 == -1)
3989     file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
3990   if (file_mips_opts.micromips == -1)
3991     file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
3992 				? 1 : 0;
3993 
3994   if (mips_nan2008 == -1)
3995     mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
3996   else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
3997     as_fatal (_("`%s' does not support legacy NaN"),
3998 	      mips_cpu_info_from_arch (file_mips_opts.arch)->name);
3999 
4000   /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4001      being selected implicitly.  */
4002   if (file_mips_opts.fp != 64)
4003     file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4004 
4005   /* If the user didn't explicitly select or deselect a particular ASE,
4006      use the default setting for the CPU.  */
4007   file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4008 
4009   /* Set up the current options.  These may change throughout assembly.  */
4010   mips_opts = file_mips_opts;
4011 
4012   mips_check_isa_supports_ases ();
4013   mips_check_options (&file_mips_opts, TRUE);
4014   file_mips_opts_checked = TRUE;
4015 
4016   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4017     as_warn (_("could not set architecture and machine"));
4018 }
4019 
4020 void
md_assemble(char * str)4021 md_assemble (char *str)
4022 {
4023   struct mips_cl_insn insn;
4024   bfd_reloc_code_real_type unused_reloc[3]
4025     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
4026 
4027   file_mips_check_options ();
4028 
4029   imm_expr.X_op = O_absent;
4030   offset_expr.X_op = O_absent;
4031   offset_reloc[0] = BFD_RELOC_UNUSED;
4032   offset_reloc[1] = BFD_RELOC_UNUSED;
4033   offset_reloc[2] = BFD_RELOC_UNUSED;
4034 
4035   mips_mark_labels ();
4036   mips_assembling_insn = TRUE;
4037   clear_insn_error ();
4038 
4039   if (mips_opts.mips16)
4040     mips16_ip (str, &insn);
4041   else
4042     {
4043       mips_ip (str, &insn);
4044       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4045 	    str, insn.insn_opcode));
4046     }
4047 
4048   if (insn_error.msg)
4049     report_insn_error (str);
4050   else if (insn.insn_mo->pinfo == INSN_MACRO)
4051     {
4052       macro_start ();
4053       if (mips_opts.mips16)
4054 	mips16_macro (&insn);
4055       else
4056 	macro (&insn, str);
4057       macro_end ();
4058     }
4059   else
4060     {
4061       if (offset_expr.X_op != O_absent)
4062 	append_insn (&insn, &offset_expr, offset_reloc, FALSE);
4063       else
4064 	append_insn (&insn, NULL, unused_reloc, FALSE);
4065     }
4066 
4067   mips_assembling_insn = FALSE;
4068 }
4069 
4070 /* Convenience functions for abstracting away the differences between
4071    MIPS16 and non-MIPS16 relocations.  */
4072 
4073 static inline bfd_boolean
mips16_reloc_p(bfd_reloc_code_real_type reloc)4074 mips16_reloc_p (bfd_reloc_code_real_type reloc)
4075 {
4076   switch (reloc)
4077     {
4078     case BFD_RELOC_MIPS16_JMP:
4079     case BFD_RELOC_MIPS16_GPREL:
4080     case BFD_RELOC_MIPS16_GOT16:
4081     case BFD_RELOC_MIPS16_CALL16:
4082     case BFD_RELOC_MIPS16_HI16_S:
4083     case BFD_RELOC_MIPS16_HI16:
4084     case BFD_RELOC_MIPS16_LO16:
4085       return TRUE;
4086 
4087     default:
4088       return FALSE;
4089     }
4090 }
4091 
4092 static inline bfd_boolean
micromips_reloc_p(bfd_reloc_code_real_type reloc)4093 micromips_reloc_p (bfd_reloc_code_real_type reloc)
4094 {
4095   switch (reloc)
4096     {
4097     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4098     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4099     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4100     case BFD_RELOC_MICROMIPS_GPREL16:
4101     case BFD_RELOC_MICROMIPS_JMP:
4102     case BFD_RELOC_MICROMIPS_HI16:
4103     case BFD_RELOC_MICROMIPS_HI16_S:
4104     case BFD_RELOC_MICROMIPS_LO16:
4105     case BFD_RELOC_MICROMIPS_LITERAL:
4106     case BFD_RELOC_MICROMIPS_GOT16:
4107     case BFD_RELOC_MICROMIPS_CALL16:
4108     case BFD_RELOC_MICROMIPS_GOT_HI16:
4109     case BFD_RELOC_MICROMIPS_GOT_LO16:
4110     case BFD_RELOC_MICROMIPS_CALL_HI16:
4111     case BFD_RELOC_MICROMIPS_CALL_LO16:
4112     case BFD_RELOC_MICROMIPS_SUB:
4113     case BFD_RELOC_MICROMIPS_GOT_PAGE:
4114     case BFD_RELOC_MICROMIPS_GOT_OFST:
4115     case BFD_RELOC_MICROMIPS_GOT_DISP:
4116     case BFD_RELOC_MICROMIPS_HIGHEST:
4117     case BFD_RELOC_MICROMIPS_HIGHER:
4118     case BFD_RELOC_MICROMIPS_SCN_DISP:
4119     case BFD_RELOC_MICROMIPS_JALR:
4120       return TRUE;
4121 
4122     default:
4123       return FALSE;
4124     }
4125 }
4126 
4127 static inline bfd_boolean
jmp_reloc_p(bfd_reloc_code_real_type reloc)4128 jmp_reloc_p (bfd_reloc_code_real_type reloc)
4129 {
4130   return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4131 }
4132 
4133 static inline bfd_boolean
got16_reloc_p(bfd_reloc_code_real_type reloc)4134 got16_reloc_p (bfd_reloc_code_real_type reloc)
4135 {
4136   return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
4137 	  || reloc == BFD_RELOC_MICROMIPS_GOT16);
4138 }
4139 
4140 static inline bfd_boolean
hi16_reloc_p(bfd_reloc_code_real_type reloc)4141 hi16_reloc_p (bfd_reloc_code_real_type reloc)
4142 {
4143   return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
4144 	  || reloc == BFD_RELOC_MICROMIPS_HI16_S);
4145 }
4146 
4147 static inline bfd_boolean
lo16_reloc_p(bfd_reloc_code_real_type reloc)4148 lo16_reloc_p (bfd_reloc_code_real_type reloc)
4149 {
4150   return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
4151 	  || reloc == BFD_RELOC_MICROMIPS_LO16);
4152 }
4153 
4154 static inline bfd_boolean
jalr_reloc_p(bfd_reloc_code_real_type reloc)4155 jalr_reloc_p (bfd_reloc_code_real_type reloc)
4156 {
4157   return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
4158 }
4159 
4160 static inline bfd_boolean
gprel16_reloc_p(bfd_reloc_code_real_type reloc)4161 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4162 {
4163   return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4164 	  || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4165 }
4166 
4167 /* Return true if RELOC is a PC-relative relocation that does not have
4168    full address range.  */
4169 
4170 static inline bfd_boolean
limited_pcrel_reloc_p(bfd_reloc_code_real_type reloc)4171 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4172 {
4173   switch (reloc)
4174     {
4175     case BFD_RELOC_16_PCREL_S2:
4176     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4177     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4178     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4179     case BFD_RELOC_MIPS_21_PCREL_S2:
4180     case BFD_RELOC_MIPS_26_PCREL_S2:
4181     case BFD_RELOC_MIPS_18_PCREL_S3:
4182     case BFD_RELOC_MIPS_19_PCREL_S2:
4183       return TRUE;
4184 
4185     case BFD_RELOC_32_PCREL:
4186     case BFD_RELOC_HI16_S_PCREL:
4187     case BFD_RELOC_LO16_PCREL:
4188       return HAVE_64BIT_ADDRESSES;
4189 
4190     default:
4191       return FALSE;
4192     }
4193 }
4194 
4195 /* Return true if the given relocation might need a matching %lo().
4196    This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4197    need a matching %lo() when applied to local symbols.  */
4198 
4199 static inline bfd_boolean
reloc_needs_lo_p(bfd_reloc_code_real_type reloc)4200 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
4201 {
4202   return (HAVE_IN_PLACE_ADDENDS
4203 	  && (hi16_reloc_p (reloc)
4204 	      /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4205 		 all GOT16 relocations evaluate to "G".  */
4206 	      || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4207 }
4208 
4209 /* Return the type of %lo() reloc needed by RELOC, given that
4210    reloc_needs_lo_p.  */
4211 
4212 static inline bfd_reloc_code_real_type
matching_lo_reloc(bfd_reloc_code_real_type reloc)4213 matching_lo_reloc (bfd_reloc_code_real_type reloc)
4214 {
4215   return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4216 	  : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4217 	     : BFD_RELOC_LO16));
4218 }
4219 
4220 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
4221    relocation.  */
4222 
4223 static inline bfd_boolean
fixup_has_matching_lo_p(fixS * fixp)4224 fixup_has_matching_lo_p (fixS *fixp)
4225 {
4226   return (fixp->fx_next != NULL
4227 	  && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
4228 	  && fixp->fx_addsy == fixp->fx_next->fx_addsy
4229 	  && fixp->fx_offset == fixp->fx_next->fx_offset);
4230 }
4231 
4232 /* Move all labels in LABELS to the current insertion point.  TEXT_P
4233    says whether the labels refer to text or data.  */
4234 
4235 static void
mips_move_labels(struct insn_label_list * labels,bfd_boolean text_p)4236 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
4237 {
4238   struct insn_label_list *l;
4239   valueT val;
4240 
4241   for (l = labels; l != NULL; l = l->next)
4242     {
4243       gas_assert (S_GET_SEGMENT (l->label) == now_seg);
4244       symbol_set_frag (l->label, frag_now);
4245       val = (valueT) frag_now_fix ();
4246       /* MIPS16/microMIPS text labels are stored as odd.  */
4247       if (text_p && HAVE_CODE_COMPRESSION)
4248 	++val;
4249       S_SET_VALUE (l->label, val);
4250     }
4251 }
4252 
4253 /* Move all labels in insn_labels to the current insertion point
4254    and treat them as text labels.  */
4255 
4256 static void
mips_move_text_labels(void)4257 mips_move_text_labels (void)
4258 {
4259   mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4260 }
4261 
4262 static bfd_boolean
s_is_linkonce(symbolS * sym,segT from_seg)4263 s_is_linkonce (symbolS *sym, segT from_seg)
4264 {
4265   bfd_boolean linkonce = FALSE;
4266   segT symseg = S_GET_SEGMENT (sym);
4267 
4268   if (symseg != from_seg && !S_IS_LOCAL (sym))
4269     {
4270       if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4271 	linkonce = TRUE;
4272       /* The GNU toolchain uses an extension for ELF: a section
4273 	 beginning with the magic string .gnu.linkonce is a
4274 	 linkonce section.  */
4275       if (strncmp (segment_name (symseg), ".gnu.linkonce",
4276 		   sizeof ".gnu.linkonce" - 1) == 0)
4277 	linkonce = TRUE;
4278     }
4279   return linkonce;
4280 }
4281 
4282 /* Mark MIPS16 or microMIPS instruction label LABEL.  This permits the
4283    linker to handle them specially, such as generating jalx instructions
4284    when needed.  We also make them odd for the duration of the assembly,
4285    in order to generate the right sort of code.  We will make them even
4286    in the adjust_symtab routine, while leaving them marked.  This is
4287    convenient for the debugger and the disassembler.  The linker knows
4288    to make them odd again.  */
4289 
4290 static void
mips_compressed_mark_label(symbolS * label)4291 mips_compressed_mark_label (symbolS *label)
4292 {
4293   gas_assert (HAVE_CODE_COMPRESSION);
4294 
4295   if (mips_opts.mips16)
4296     S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4297   else
4298     S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
4299   if ((S_GET_VALUE (label) & 1) == 0
4300       /* Don't adjust the address if the label is global or weak, or
4301 	 in a link-once section, since we'll be emitting symbol reloc
4302 	 references to it which will be patched up by the linker, and
4303 	 the final value of the symbol may or may not be MIPS16/microMIPS.  */
4304       && !S_IS_WEAK (label)
4305       && !S_IS_EXTERNAL (label)
4306       && !s_is_linkonce (label, now_seg))
4307     S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4308 }
4309 
4310 /* Mark preceding MIPS16 or microMIPS instruction labels.  */
4311 
4312 static void
mips_compressed_mark_labels(void)4313 mips_compressed_mark_labels (void)
4314 {
4315   struct insn_label_list *l;
4316 
4317   for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4318     mips_compressed_mark_label (l->label);
4319 }
4320 
4321 /* End the current frag.  Make it a variant frag and record the
4322    relaxation info.  */
4323 
4324 static void
relax_close_frag(void)4325 relax_close_frag (void)
4326 {
4327   mips_macro_warning.first_frag = frag_now;
4328   frag_var (rs_machine_dependent, 0, 0,
4329 	    RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4330 	    mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4331 
4332   memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4333   mips_relax.first_fixup = 0;
4334 }
4335 
4336 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
4337    See the comment above RELAX_ENCODE for more details.  */
4338 
4339 static void
relax_start(symbolS * symbol)4340 relax_start (symbolS *symbol)
4341 {
4342   gas_assert (mips_relax.sequence == 0);
4343   mips_relax.sequence = 1;
4344   mips_relax.symbol = symbol;
4345 }
4346 
4347 /* Start generating the second version of a relaxable sequence.
4348    See the comment above RELAX_ENCODE for more details.  */
4349 
4350 static void
relax_switch(void)4351 relax_switch (void)
4352 {
4353   gas_assert (mips_relax.sequence == 1);
4354   mips_relax.sequence = 2;
4355 }
4356 
4357 /* End the current relaxable sequence.  */
4358 
4359 static void
relax_end(void)4360 relax_end (void)
4361 {
4362   gas_assert (mips_relax.sequence == 2);
4363   relax_close_frag ();
4364   mips_relax.sequence = 0;
4365 }
4366 
4367 /* Return true if IP is a delayed branch or jump.  */
4368 
4369 static inline bfd_boolean
delayed_branch_p(const struct mips_cl_insn * ip)4370 delayed_branch_p (const struct mips_cl_insn *ip)
4371 {
4372   return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4373 				| INSN_COND_BRANCH_DELAY
4374 				| INSN_COND_BRANCH_LIKELY)) != 0;
4375 }
4376 
4377 /* Return true if IP is a compact branch or jump.  */
4378 
4379 static inline bfd_boolean
compact_branch_p(const struct mips_cl_insn * ip)4380 compact_branch_p (const struct mips_cl_insn *ip)
4381 {
4382   return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4383 				 | INSN2_COND_BRANCH)) != 0;
4384 }
4385 
4386 /* Return true if IP is an unconditional branch or jump.  */
4387 
4388 static inline bfd_boolean
uncond_branch_p(const struct mips_cl_insn * ip)4389 uncond_branch_p (const struct mips_cl_insn *ip)
4390 {
4391   return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
4392 	  || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
4393 }
4394 
4395 /* Return true if IP is a branch-likely instruction.  */
4396 
4397 static inline bfd_boolean
branch_likely_p(const struct mips_cl_insn * ip)4398 branch_likely_p (const struct mips_cl_insn *ip)
4399 {
4400   return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4401 }
4402 
4403 /* Return the type of nop that should be used to fill the delay slot
4404    of delayed branch IP.  */
4405 
4406 static struct mips_cl_insn *
get_delay_slot_nop(const struct mips_cl_insn * ip)4407 get_delay_slot_nop (const struct mips_cl_insn *ip)
4408 {
4409   if (mips_opts.micromips
4410       && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4411     return &micromips_nop32_insn;
4412   return NOP_INSN;
4413 }
4414 
4415 /* Return a mask that has bit N set if OPCODE reads the register(s)
4416    in operand N.  */
4417 
4418 static unsigned int
insn_read_mask(const struct mips_opcode * opcode)4419 insn_read_mask (const struct mips_opcode *opcode)
4420 {
4421   return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4422 }
4423 
4424 /* Return a mask that has bit N set if OPCODE writes to the register(s)
4425    in operand N.  */
4426 
4427 static unsigned int
insn_write_mask(const struct mips_opcode * opcode)4428 insn_write_mask (const struct mips_opcode *opcode)
4429 {
4430   return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4431 }
4432 
4433 /* Return a mask of the registers specified by operand OPERAND of INSN.
4434    Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4435    is set.  */
4436 
4437 static unsigned int
operand_reg_mask(const struct mips_cl_insn * insn,const struct mips_operand * operand,unsigned int type_mask)4438 operand_reg_mask (const struct mips_cl_insn *insn,
4439 		  const struct mips_operand *operand,
4440 		  unsigned int type_mask)
4441 {
4442   unsigned int uval, vsel;
4443 
4444   switch (operand->type)
4445     {
4446     case OP_INT:
4447     case OP_MAPPED_INT:
4448     case OP_MSB:
4449     case OP_PCREL:
4450     case OP_PERF_REG:
4451     case OP_ADDIUSP_INT:
4452     case OP_ENTRY_EXIT_LIST:
4453     case OP_REPEAT_DEST_REG:
4454     case OP_REPEAT_PREV_REG:
4455     case OP_PC:
4456     case OP_VU0_SUFFIX:
4457     case OP_VU0_MATCH_SUFFIX:
4458     case OP_IMM_INDEX:
4459     case OP_MAPPED_STRING:
4460     case OP_MXU_STRIDE:
4461       abort ();
4462 
4463     case OP_REG:
4464     case OP_OPTIONAL_REG:
4465       {
4466 	const struct mips_reg_operand *reg_op;
4467 
4468 	reg_op = (const struct mips_reg_operand *) operand;
4469 	if (!(type_mask & (1 << reg_op->reg_type)))
4470 	  return 0;
4471 	uval = insn_extract_operand (insn, operand);
4472 	return 1 << mips_decode_reg_operand (reg_op, uval);
4473       }
4474 
4475     case OP_REG_PAIR:
4476       {
4477 	const struct mips_reg_pair_operand *pair_op;
4478 
4479 	pair_op = (const struct mips_reg_pair_operand *) operand;
4480 	if (!(type_mask & (1 << pair_op->reg_type)))
4481 	  return 0;
4482 	uval = insn_extract_operand (insn, operand);
4483 	return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4484       }
4485 
4486     case OP_CLO_CLZ_DEST:
4487       if (!(type_mask & (1 << OP_REG_GP)))
4488 	return 0;
4489       uval = insn_extract_operand (insn, operand);
4490       return (1 << (uval & 31)) | (1 << (uval >> 5));
4491 
4492     case OP_SAME_RS_RT:
4493       if (!(type_mask & (1 << OP_REG_GP)))
4494 	return 0;
4495       uval = insn_extract_operand (insn, operand);
4496       gas_assert ((uval & 31) == (uval >> 5));
4497       return 1 << (uval & 31);
4498 
4499     case OP_CHECK_PREV:
4500     case OP_NON_ZERO_REG:
4501       if (!(type_mask & (1 << OP_REG_GP)))
4502 	return 0;
4503       uval = insn_extract_operand (insn, operand);
4504       return 1 << (uval & 31);
4505 
4506     case OP_LWM_SWM_LIST:
4507       abort ();
4508 
4509     case OP_SAVE_RESTORE_LIST:
4510       abort ();
4511 
4512     case OP_MDMX_IMM_REG:
4513       if (!(type_mask & (1 << OP_REG_VEC)))
4514 	return 0;
4515       uval = insn_extract_operand (insn, operand);
4516       vsel = uval >> 5;
4517       if ((vsel & 0x18) == 0x18)
4518 	return 0;
4519       return 1 << (uval & 31);
4520 
4521     case OP_REG_INDEX:
4522       if (!(type_mask & (1 << OP_REG_GP)))
4523 	return 0;
4524       return 1 << insn_extract_operand (insn, operand);
4525     }
4526   abort ();
4527 }
4528 
4529 /* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4530    where bit N of OPNO_MASK is set if operand N should be included.
4531    Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4532    is set.  */
4533 
4534 static unsigned int
insn_reg_mask(const struct mips_cl_insn * insn,unsigned int type_mask,unsigned int opno_mask)4535 insn_reg_mask (const struct mips_cl_insn *insn,
4536 	       unsigned int type_mask, unsigned int opno_mask)
4537 {
4538   unsigned int opno, reg_mask;
4539 
4540   opno = 0;
4541   reg_mask = 0;
4542   while (opno_mask != 0)
4543     {
4544       if (opno_mask & 1)
4545 	reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4546       opno_mask >>= 1;
4547       opno += 1;
4548     }
4549   return reg_mask;
4550 }
4551 
4552 /* Return the mask of core registers that IP reads.  */
4553 
4554 static unsigned int
gpr_read_mask(const struct mips_cl_insn * ip)4555 gpr_read_mask (const struct mips_cl_insn *ip)
4556 {
4557   unsigned long pinfo, pinfo2;
4558   unsigned int mask;
4559 
4560   mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4561   pinfo = ip->insn_mo->pinfo;
4562   pinfo2 = ip->insn_mo->pinfo2;
4563   if (pinfo & INSN_UDI)
4564     {
4565       /* UDI instructions have traditionally been assumed to read RS
4566 	 and RT.  */
4567       mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4568       mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4569     }
4570   if (pinfo & INSN_READ_GPR_24)
4571     mask |= 1 << 24;
4572   if (pinfo2 & INSN2_READ_GPR_16)
4573     mask |= 1 << 16;
4574   if (pinfo2 & INSN2_READ_SP)
4575     mask |= 1 << SP;
4576   if (pinfo2 & INSN2_READ_GPR_31)
4577     mask |= 1 << 31;
4578   /* Don't include register 0.  */
4579   return mask & ~1;
4580 }
4581 
4582 /* Return the mask of core registers that IP writes.  */
4583 
4584 static unsigned int
gpr_write_mask(const struct mips_cl_insn * ip)4585 gpr_write_mask (const struct mips_cl_insn *ip)
4586 {
4587   unsigned long pinfo, pinfo2;
4588   unsigned int mask;
4589 
4590   mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4591   pinfo = ip->insn_mo->pinfo;
4592   pinfo2 = ip->insn_mo->pinfo2;
4593   if (pinfo & INSN_WRITE_GPR_24)
4594     mask |= 1 << 24;
4595   if (pinfo & INSN_WRITE_GPR_31)
4596     mask |= 1 << 31;
4597   if (pinfo & INSN_UDI)
4598     /* UDI instructions have traditionally been assumed to write to RD.  */
4599     mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4600   if (pinfo2 & INSN2_WRITE_SP)
4601     mask |= 1 << SP;
4602   /* Don't include register 0.  */
4603   return mask & ~1;
4604 }
4605 
4606 /* Return the mask of floating-point registers that IP reads.  */
4607 
4608 static unsigned int
fpr_read_mask(const struct mips_cl_insn * ip)4609 fpr_read_mask (const struct mips_cl_insn *ip)
4610 {
4611   unsigned long pinfo;
4612   unsigned int mask;
4613 
4614   mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4615 			     | (1 << OP_REG_MSA)),
4616 			insn_read_mask (ip->insn_mo));
4617   pinfo = ip->insn_mo->pinfo;
4618   /* Conservatively treat all operands to an FP_D instruction are doubles.
4619      (This is overly pessimistic for things like cvt.d.s.)  */
4620   if (FPR_SIZE != 64 && (pinfo & FP_D))
4621     mask |= mask << 1;
4622   return mask;
4623 }
4624 
4625 /* Return the mask of floating-point registers that IP writes.  */
4626 
4627 static unsigned int
fpr_write_mask(const struct mips_cl_insn * ip)4628 fpr_write_mask (const struct mips_cl_insn *ip)
4629 {
4630   unsigned long pinfo;
4631   unsigned int mask;
4632 
4633   mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4634 			     | (1 << OP_REG_MSA)),
4635 			insn_write_mask (ip->insn_mo));
4636   pinfo = ip->insn_mo->pinfo;
4637   /* Conservatively treat all operands to an FP_D instruction are doubles.
4638      (This is overly pessimistic for things like cvt.s.d.)  */
4639   if (FPR_SIZE != 64 && (pinfo & FP_D))
4640     mask |= mask << 1;
4641   return mask;
4642 }
4643 
4644 /* Operand OPNUM of INSN is an odd-numbered floating-point register.
4645    Check whether that is allowed.  */
4646 
4647 static bfd_boolean
mips_oddfpreg_ok(const struct mips_opcode * insn,int opnum)4648 mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4649 {
4650   const char *s = insn->name;
4651   bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4652 			  || FPR_SIZE == 64)
4653 			 && mips_opts.oddspreg;
4654 
4655   if (insn->pinfo == INSN_MACRO)
4656     /* Let a macro pass, we'll catch it later when it is expanded.  */
4657     return TRUE;
4658 
4659   /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4660      otherwise it depends on oddspreg.  */
4661   if ((insn->pinfo & FP_S)
4662       && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
4663 			 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
4664     return FPR_SIZE == 32 || oddspreg;
4665 
4666   /* Allow odd registers for single-precision ops and double-precision if the
4667      floating-point registers are 64-bit wide.  */
4668   switch (insn->pinfo & (FP_S | FP_D))
4669     {
4670     case FP_S:
4671     case 0:
4672       return oddspreg;
4673     case FP_D:
4674       return FPR_SIZE == 64;
4675     default:
4676       break;
4677     }
4678 
4679   /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand.  */
4680   s = strchr (insn->name, '.');
4681   if (s != NULL && opnum == 2)
4682     s = strchr (s + 1, '.');
4683   if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4684     return oddspreg;
4685 
4686   return FPR_SIZE == 64;
4687 }
4688 
4689 /* Information about an instruction argument that we're trying to match.  */
4690 struct mips_arg_info
4691 {
4692   /* The instruction so far.  */
4693   struct mips_cl_insn *insn;
4694 
4695   /* The first unconsumed operand token.  */
4696   struct mips_operand_token *token;
4697 
4698   /* The 1-based operand number, in terms of insn->insn_mo->args.  */
4699   int opnum;
4700 
4701   /* The 1-based argument number, for error reporting.  This does not
4702      count elided optional registers, etc..  */
4703   int argnum;
4704 
4705   /* The last OP_REG operand seen, or ILLEGAL_REG if none.  */
4706   unsigned int last_regno;
4707 
4708   /* If the first operand was an OP_REG, this is the register that it
4709      specified, otherwise it is ILLEGAL_REG.  */
4710   unsigned int dest_regno;
4711 
4712   /* The value of the last OP_INT operand.  Only used for OP_MSB,
4713      where it gives the lsb position.  */
4714   unsigned int last_op_int;
4715 
4716   /* If true, match routines should assume that no later instruction
4717      alternative matches and should therefore be as accomodating as
4718      possible.  Match routines should not report errors if something
4719      is only invalid for !LAX_MATCH.  */
4720   bfd_boolean lax_match;
4721 
4722   /* True if a reference to the current AT register was seen.  */
4723   bfd_boolean seen_at;
4724 };
4725 
4726 /* Record that the argument is out of range.  */
4727 
4728 static void
match_out_of_range(struct mips_arg_info * arg)4729 match_out_of_range (struct mips_arg_info *arg)
4730 {
4731   set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4732 }
4733 
4734 /* Record that the argument isn't constant but needs to be.  */
4735 
4736 static void
match_not_constant(struct mips_arg_info * arg)4737 match_not_constant (struct mips_arg_info *arg)
4738 {
4739   set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4740 		    arg->argnum);
4741 }
4742 
4743 /* Try to match an OT_CHAR token for character CH.  Consume the token
4744    and return true on success, otherwise return false.  */
4745 
4746 static bfd_boolean
match_char(struct mips_arg_info * arg,char ch)4747 match_char (struct mips_arg_info *arg, char ch)
4748 {
4749   if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4750     {
4751       ++arg->token;
4752       if (ch == ',')
4753 	arg->argnum += 1;
4754       return TRUE;
4755     }
4756   return FALSE;
4757 }
4758 
4759 /* Try to get an expression from the next tokens in ARG.  Consume the
4760    tokens and return true on success, storing the expression value in
4761    VALUE and relocation types in R.  */
4762 
4763 static bfd_boolean
match_expression(struct mips_arg_info * arg,expressionS * value,bfd_reloc_code_real_type * r)4764 match_expression (struct mips_arg_info *arg, expressionS *value,
4765 		  bfd_reloc_code_real_type *r)
4766 {
4767   /* If the next token is a '(' that was parsed as being part of a base
4768      expression, assume we have an elided offset.  The later match will fail
4769      if this turns out to be wrong.  */
4770   if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4771     {
4772       value->X_op = O_constant;
4773       value->X_add_number = 0;
4774       r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4775       return TRUE;
4776     }
4777 
4778   /* Reject register-based expressions such as "0+$2" and "(($2))".
4779      For plain registers the default error seems more appropriate.  */
4780   if (arg->token->type == OT_INTEGER
4781       && arg->token->u.integer.value.X_op == O_register)
4782     {
4783       set_insn_error (arg->argnum, _("register value used as expression"));
4784       return FALSE;
4785     }
4786 
4787   if (arg->token->type == OT_INTEGER)
4788     {
4789       *value = arg->token->u.integer.value;
4790       memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4791       ++arg->token;
4792       return TRUE;
4793     }
4794 
4795   set_insn_error_i
4796     (arg->argnum, _("operand %d must be an immediate expression"),
4797      arg->argnum);
4798   return FALSE;
4799 }
4800 
4801 /* Try to get a constant expression from the next tokens in ARG.  Consume
4802    the tokens and return return true on success, storing the constant value
4803    in *VALUE.  Use FALLBACK as the value if the match succeeded with an
4804    error.  */
4805 
4806 static bfd_boolean
match_const_int(struct mips_arg_info * arg,offsetT * value)4807 match_const_int (struct mips_arg_info *arg, offsetT *value)
4808 {
4809   expressionS ex;
4810   bfd_reloc_code_real_type r[3];
4811 
4812   if (!match_expression (arg, &ex, r))
4813     return FALSE;
4814 
4815   if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
4816     *value = ex.X_add_number;
4817   else
4818     {
4819       match_not_constant (arg);
4820       return FALSE;
4821     }
4822   return TRUE;
4823 }
4824 
4825 /* Return the RTYPE_* flags for a register operand of type TYPE that
4826    appears in instruction OPCODE.  */
4827 
4828 static unsigned int
convert_reg_type(const struct mips_opcode * opcode,enum mips_reg_operand_type type)4829 convert_reg_type (const struct mips_opcode *opcode,
4830 		  enum mips_reg_operand_type type)
4831 {
4832   switch (type)
4833     {
4834     case OP_REG_MXU:
4835       return RTYPE_NUM | RTYPE_MXU;
4836 
4837     case OP_REG_MXU_GP:
4838       return RTYPE_GP | RTYPE_MXU;
4839 
4840     case OP_REG_GP:
4841       return RTYPE_NUM | RTYPE_GP;
4842 
4843     case OP_REG_FP:
4844       /* Allow vector register names for MDMX if the instruction is a 64-bit
4845 	 FPR load, store or move (including moves to and from GPRs).  */
4846       if ((mips_opts.ase & ASE_MDMX)
4847 	  && (opcode->pinfo & FP_D)
4848 	  && (opcode->pinfo & (INSN_COPROC_MOVE
4849 			       | INSN_COPROC_MEMORY_DELAY
4850 			       | INSN_LOAD_COPROC
4851 			       | INSN_LOAD_MEMORY
4852 			       | INSN_STORE_MEMORY)))
4853 	return RTYPE_FPU | RTYPE_VEC;
4854       return RTYPE_FPU;
4855 
4856     case OP_REG_CCC:
4857       if (opcode->pinfo & (FP_D | FP_S))
4858 	return RTYPE_CCC | RTYPE_FCC;
4859       return RTYPE_CCC;
4860 
4861     case OP_REG_VEC:
4862       if (opcode->membership & INSN_5400)
4863 	return RTYPE_FPU;
4864       return RTYPE_FPU | RTYPE_VEC;
4865 
4866     case OP_REG_ACC:
4867       return RTYPE_ACC;
4868 
4869     case OP_REG_COPRO:
4870       if (opcode->name[strlen (opcode->name) - 1] == '0')
4871 	return RTYPE_NUM | RTYPE_CP0;
4872       return RTYPE_NUM;
4873 
4874     case OP_REG_HW:
4875       return RTYPE_NUM;
4876 
4877     case OP_REG_VI:
4878       return RTYPE_NUM | RTYPE_VI;
4879 
4880     case OP_REG_VF:
4881       return RTYPE_NUM | RTYPE_VF;
4882 
4883     case OP_REG_R5900_I:
4884       return RTYPE_R5900_I;
4885 
4886     case OP_REG_R5900_Q:
4887       return RTYPE_R5900_Q;
4888 
4889     case OP_REG_R5900_R:
4890       return RTYPE_R5900_R;
4891 
4892     case OP_REG_R5900_ACC:
4893       return RTYPE_R5900_ACC;
4894 
4895     case OP_REG_MSA:
4896       return RTYPE_MSA;
4897 
4898     case OP_REG_MSA_CTRL:
4899       return RTYPE_NUM;
4900     }
4901   abort ();
4902 }
4903 
4904 /* ARG is register REGNO, of type TYPE.  Warn about any dubious registers.  */
4905 
4906 static void
check_regno(struct mips_arg_info * arg,enum mips_reg_operand_type type,unsigned int regno)4907 check_regno (struct mips_arg_info *arg,
4908 	     enum mips_reg_operand_type type, unsigned int regno)
4909 {
4910   if (AT && type == OP_REG_GP && regno == AT)
4911     arg->seen_at = TRUE;
4912 
4913   if (type == OP_REG_FP
4914       && (regno & 1) != 0
4915       && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
4916     {
4917       /* This was a warning prior to introducing O32 FPXX and FP64 support
4918 	 so maintain a warning for FP32 but raise an error for the new
4919 	 cases.  */
4920       if (FPR_SIZE == 32)
4921 	as_warn (_("float register should be even, was %d"), regno);
4922       else
4923 	as_bad (_("float register should be even, was %d"), regno);
4924     }
4925 
4926   if (type == OP_REG_CCC)
4927     {
4928       const char *name;
4929       size_t length;
4930 
4931       name = arg->insn->insn_mo->name;
4932       length = strlen (name);
4933       if ((regno & 1) != 0
4934 	  && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4935 	      || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
4936 	as_warn (_("condition code register should be even for %s, was %d"),
4937 		 name, regno);
4938 
4939       if ((regno & 3) != 0
4940 	  && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
4941 	as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
4942 		 name, regno);
4943     }
4944 }
4945 
4946 /* ARG is a register with symbol value SYMVAL.  Try to interpret it as
4947    a register of type TYPE.  Return true on success, storing the register
4948    number in *REGNO and warning about any dubious uses.  */
4949 
4950 static bfd_boolean
match_regno(struct mips_arg_info * arg,enum mips_reg_operand_type type,unsigned int symval,unsigned int * regno)4951 match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4952 	     unsigned int symval, unsigned int *regno)
4953 {
4954   if (type == OP_REG_VEC)
4955     symval = mips_prefer_vec_regno (symval);
4956   if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4957     return FALSE;
4958 
4959   *regno = symval & RNUM_MASK;
4960   check_regno (arg, type, *regno);
4961   return TRUE;
4962 }
4963 
4964 /* Try to interpret the next token in ARG as a register of type TYPE.
4965    Consume the token and return true on success, storing the register
4966    number in *REGNO.  Return false on failure.  */
4967 
4968 static bfd_boolean
match_reg(struct mips_arg_info * arg,enum mips_reg_operand_type type,unsigned int * regno)4969 match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4970 	   unsigned int *regno)
4971 {
4972   if (arg->token->type == OT_REG
4973       && match_regno (arg, type, arg->token->u.regno, regno))
4974     {
4975       ++arg->token;
4976       return TRUE;
4977     }
4978   return FALSE;
4979 }
4980 
4981 /* Try to interpret the next token in ARG as a range of registers of type TYPE.
4982    Consume the token and return true on success, storing the register numbers
4983    in *REGNO1 and *REGNO2.  Return false on failure.  */
4984 
4985 static bfd_boolean
match_reg_range(struct mips_arg_info * arg,enum mips_reg_operand_type type,unsigned int * regno1,unsigned int * regno2)4986 match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4987 		 unsigned int *regno1, unsigned int *regno2)
4988 {
4989   if (match_reg (arg, type, regno1))
4990     {
4991       *regno2 = *regno1;
4992       return TRUE;
4993     }
4994   if (arg->token->type == OT_REG_RANGE
4995       && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
4996       && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
4997       && *regno1 <= *regno2)
4998     {
4999       ++arg->token;
5000       return TRUE;
5001     }
5002   return FALSE;
5003 }
5004 
5005 /* OP_INT matcher.  */
5006 
5007 static bfd_boolean
match_int_operand(struct mips_arg_info * arg,const struct mips_operand * operand_base)5008 match_int_operand (struct mips_arg_info *arg,
5009 		   const struct mips_operand *operand_base)
5010 {
5011   const struct mips_int_operand *operand;
5012   unsigned int uval;
5013   int min_val, max_val, factor;
5014   offsetT sval;
5015 
5016   operand = (const struct mips_int_operand *) operand_base;
5017   factor = 1 << operand->shift;
5018   min_val = mips_int_operand_min (operand);
5019   max_val = mips_int_operand_max (operand);
5020 
5021   if (operand_base->lsb == 0
5022       && operand_base->size == 16
5023       && operand->shift == 0
5024       && operand->bias == 0
5025       && (operand->max_val == 32767 || operand->max_val == 65535))
5026     {
5027       /* The operand can be relocated.  */
5028       if (!match_expression (arg, &offset_expr, offset_reloc))
5029 	return FALSE;
5030 
5031       if (offset_reloc[0] != BFD_RELOC_UNUSED)
5032 	/* Relocation operators were used.  Accept the arguent and
5033 	   leave the relocation value in offset_expr and offset_relocs
5034 	   for the caller to process.  */
5035 	return TRUE;
5036 
5037       if (offset_expr.X_op != O_constant)
5038 	{
5039 	  /* Accept non-constant operands if no later alternative matches,
5040 	     leaving it for the caller to process.  */
5041 	  if (!arg->lax_match)
5042 	    return FALSE;
5043 	  offset_reloc[0] = BFD_RELOC_LO16;
5044 	  return TRUE;
5045 	}
5046 
5047       /* Clear the global state; we're going to install the operand
5048 	 ourselves.  */
5049       sval = offset_expr.X_add_number;
5050       offset_expr.X_op = O_absent;
5051 
5052       /* For compatibility with older assemblers, we accept
5053 	 0x8000-0xffff as signed 16-bit numbers when only
5054 	 signed numbers are allowed.  */
5055       if (sval > max_val)
5056 	{
5057 	  max_val = ((1 << operand_base->size) - 1) << operand->shift;
5058 	  if (!arg->lax_match && sval <= max_val)
5059 	    return FALSE;
5060 	}
5061     }
5062   else
5063     {
5064       if (!match_const_int (arg, &sval))
5065 	return FALSE;
5066     }
5067 
5068   arg->last_op_int = sval;
5069 
5070   if (sval < min_val || sval > max_val || sval % factor)
5071     {
5072       match_out_of_range (arg);
5073       return FALSE;
5074     }
5075 
5076   uval = (unsigned int) sval >> operand->shift;
5077   uval -= operand->bias;
5078 
5079   /* Handle -mfix-cn63xxp1.  */
5080   if (arg->opnum == 1
5081       && mips_fix_cn63xxp1
5082       && !mips_opts.micromips
5083       && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5084     switch (uval)
5085       {
5086       case 5:
5087       case 25:
5088       case 26:
5089       case 27:
5090       case 28:
5091       case 29:
5092       case 30:
5093       case 31:
5094 	/* These are ok.  */
5095 	break;
5096 
5097       default:
5098 	/* The rest must be changed to 28.  */
5099 	uval = 28;
5100 	break;
5101       }
5102 
5103   insn_insert_operand (arg->insn, operand_base, uval);
5104   return TRUE;
5105 }
5106 
5107 /* OP_MAPPED_INT matcher.  */
5108 
5109 static bfd_boolean
match_mapped_int_operand(struct mips_arg_info * arg,const struct mips_operand * operand_base)5110 match_mapped_int_operand (struct mips_arg_info *arg,
5111 			  const struct mips_operand *operand_base)
5112 {
5113   const struct mips_mapped_int_operand *operand;
5114   unsigned int uval, num_vals;
5115   offsetT sval;
5116 
5117   operand = (const struct mips_mapped_int_operand *) operand_base;
5118   if (!match_const_int (arg, &sval))
5119     return FALSE;
5120 
5121   num_vals = 1 << operand_base->size;
5122   for (uval = 0; uval < num_vals; uval++)
5123     if (operand->int_map[uval] == sval)
5124       break;
5125   if (uval == num_vals)
5126     {
5127       match_out_of_range (arg);
5128       return FALSE;
5129     }
5130 
5131   insn_insert_operand (arg->insn, operand_base, uval);
5132   return TRUE;
5133 }
5134 
5135 /* OP_MSB matcher.  */
5136 
5137 static bfd_boolean
match_msb_operand(struct mips_arg_info * arg,const struct mips_operand * operand_base)5138 match_msb_operand (struct mips_arg_info *arg,
5139 		   const struct mips_operand *operand_base)
5140 {
5141   const struct mips_msb_operand *operand;
5142   int min_val, max_val, max_high;
5143   offsetT size, sval, high;
5144 
5145   operand = (const struct mips_msb_operand *) operand_base;
5146   min_val = operand->bias;
5147   max_val = min_val + (1 << operand_base->size) - 1;
5148   max_high = operand->opsize;
5149 
5150   if (!match_const_int (arg, &size))
5151     return FALSE;
5152 
5153   high = size + arg->last_op_int;
5154   sval = operand->add_lsb ? high : size;
5155 
5156   if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5157     {
5158       match_out_of_range (arg);
5159       return FALSE;
5160     }
5161   insn_insert_operand (arg->insn, operand_base, sval - min_val);
5162   return TRUE;
5163 }
5164 
5165 
5166 /* OP_MAPPED_STRING matcher.  */
5167 
5168 static bfd_boolean
match_string_operand(struct mips_arg_info * arg,const struct mips_operand * operand_base)5169 match_string_operand (struct mips_arg_info *arg,
5170 		      const struct mips_operand *operand_base)
5171 {
5172   const struct mips_mapped_string_operand *operand;
5173   expressionS ex;
5174   bfd_reloc_code_real_type r[3];
5175   int i;
5176   unsigned int store_val;
5177   const char * symbol_name;
5178   bfd_boolean match;
5179 
5180   operand = (const struct mips_mapped_string_operand *) operand_base;
5181 
5182   if (!match_expression (arg, &ex, r))
5183     return FALSE;
5184 
5185   if (operand->allow_constants && ex.X_op == O_constant
5186       && r[0] == BFD_RELOC_UNUSED)
5187     store_val = ex.X_add_number;
5188   else if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_symbol
5189 	   && ex.X_add_number == 0 && ex.X_op_symbol == NULL)
5190     {
5191       symbol_name = S_GET_NAME (ex.X_add_symbol);
5192       match = FALSE;
5193 
5194       for (i = 0 ; i < (1 << operand_base->size) ; i++)
5195 	{
5196 	  if (strcmp (operand->strings[i], symbol_name) == 0)
5197 	    {
5198 	      store_val = i;
5199 	      match = TRUE;
5200 	      break;
5201 	    }
5202 	}
5203 
5204       if (!match)
5205 	{
5206 	  set_insn_error (arg->argnum, _("Invalid string in operand"));
5207 	  return FALSE;
5208 	}
5209     }
5210   else
5211     return FALSE;
5212 
5213   if (store_val >= (unsigned int) (1 << operand_base->size))
5214     {
5215       match_out_of_range (arg);
5216       return FALSE;
5217     }
5218 
5219   insn_insert_operand (arg->insn, operand_base, store_val);
5220   return TRUE;
5221 }
5222 
5223 
5224 /* OP_REG matcher.  */
5225 
5226 static bfd_boolean
match_reg_operand(struct mips_arg_info * arg,const struct mips_operand * operand_base)5227 match_reg_operand (struct mips_arg_info *arg,
5228 		   const struct mips_operand *operand_base)
5229 {
5230   const struct mips_reg_operand *operand;
5231   unsigned int regno, uval, num_vals;
5232 
5233   operand = (const struct mips_reg_operand *) operand_base;
5234   if (!match_reg (arg, operand->reg_type, &regno))
5235     return FALSE;
5236 
5237   if (operand->reg_map)
5238     {
5239       num_vals = 1 << operand->root.size;
5240       for (uval = 0; uval < num_vals; uval++)
5241 	if (operand->reg_map[uval] == regno)
5242 	  break;
5243       if (num_vals == uval)
5244 	return FALSE;
5245     }
5246   else
5247     uval = regno;
5248 
5249   if (operand_base->size > 0
5250       && uval >= (unsigned int) (1 << operand_base->size))
5251     {
5252       match_out_of_range (arg);
5253       return FALSE;
5254     }
5255 
5256   arg->last_regno = regno;
5257   if (arg->opnum == 1)
5258     arg->dest_regno = regno;
5259   insn_insert_operand (arg->insn, operand_base, uval);
5260   return TRUE;
5261 }
5262 
5263 /* OP_REG_PAIR matcher.  */
5264 
5265 static bfd_boolean
match_reg_pair_operand(struct mips_arg_info * arg,const struct mips_operand * operand_base)5266 match_reg_pair_operand (struct mips_arg_info *arg,
5267 			const struct mips_operand *operand_base)
5268 {
5269   const struct mips_reg_pair_operand *operand;
5270   unsigned int regno1, regno2, uval, num_vals;
5271 
5272   operand = (const struct mips_reg_pair_operand *) operand_base;
5273   if (!match_reg (arg, operand->reg_type, &regno1)
5274       || !match_char (arg, ',')
5275       || !match_reg (arg, operand->reg_type, &regno2))
5276     return FALSE;
5277 
5278   num_vals = 1 << operand_base->size;
5279   for (uval = 0; uval < num_vals; uval++)
5280     if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5281       break;
5282   if (uval == num_vals)
5283     return FALSE;
5284 
5285   insn_insert_operand (arg->insn, operand_base, uval);
5286   return TRUE;
5287 }
5288 
5289 /* OP_PCREL matcher.  The caller chooses the relocation type.  */
5290 
5291 static bfd_boolean
match_pcrel_operand(struct mips_arg_info * arg)5292 match_pcrel_operand (struct mips_arg_info *arg)
5293 {
5294   bfd_reloc_code_real_type r[3];
5295 
5296   return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
5297 }
5298 
5299 /* OP_PERF_REG matcher.  */
5300 
5301 static bfd_boolean
match_perf_reg_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5302 match_perf_reg_operand (struct mips_arg_info *arg,
5303 			const struct mips_operand *operand)
5304 {
5305   offsetT sval;
5306 
5307   if (!match_const_int (arg, &sval))
5308     return FALSE;
5309 
5310   if (sval != 0
5311       && (sval != 1
5312 	  || (mips_opts.arch == CPU_R5900
5313 	      && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5314 		  || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5315     {
5316       set_insn_error (arg->argnum, _("invalid performance register"));
5317       return FALSE;
5318     }
5319 
5320   insn_insert_operand (arg->insn, operand, sval);
5321   return TRUE;
5322 }
5323 
5324 /* OP_ADDIUSP matcher.  */
5325 
5326 static bfd_boolean
match_addiusp_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5327 match_addiusp_operand (struct mips_arg_info *arg,
5328 		       const struct mips_operand *operand)
5329 {
5330   offsetT sval;
5331   unsigned int uval;
5332 
5333   if (!match_const_int (arg, &sval))
5334     return FALSE;
5335 
5336   if (sval % 4)
5337     {
5338       match_out_of_range (arg);
5339       return FALSE;
5340     }
5341 
5342   sval /= 4;
5343   if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
5344     {
5345       match_out_of_range (arg);
5346       return FALSE;
5347     }
5348 
5349   uval = (unsigned int) sval;
5350   uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5351   insn_insert_operand (arg->insn, operand, uval);
5352   return TRUE;
5353 }
5354 
5355 /* OP_CLO_CLZ_DEST matcher.  */
5356 
5357 static bfd_boolean
match_clo_clz_dest_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5358 match_clo_clz_dest_operand (struct mips_arg_info *arg,
5359 			    const struct mips_operand *operand)
5360 {
5361   unsigned int regno;
5362 
5363   if (!match_reg (arg, OP_REG_GP, &regno))
5364     return FALSE;
5365 
5366   insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5367   return TRUE;
5368 }
5369 
5370 /* OP_CHECK_PREV matcher.  */
5371 
5372 static bfd_boolean
match_check_prev_operand(struct mips_arg_info * arg,const struct mips_operand * operand_base)5373 match_check_prev_operand (struct mips_arg_info *arg,
5374 			  const struct mips_operand *operand_base)
5375 {
5376   const struct mips_check_prev_operand *operand;
5377   unsigned int regno;
5378 
5379   operand = (const struct mips_check_prev_operand *) operand_base;
5380 
5381   if (!match_reg (arg, OP_REG_GP, &regno))
5382     return FALSE;
5383 
5384   if (!operand->zero_ok && regno == 0)
5385     return FALSE;
5386 
5387   if ((operand->less_than_ok && regno < arg->last_regno)
5388       || (operand->greater_than_ok && regno > arg->last_regno)
5389       || (operand->equal_ok && regno == arg->last_regno))
5390     {
5391       arg->last_regno = regno;
5392       insn_insert_operand (arg->insn, operand_base, regno);
5393       return TRUE;
5394     }
5395 
5396   return FALSE;
5397 }
5398 
5399 /* OP_SAME_RS_RT matcher.  */
5400 
5401 static bfd_boolean
match_same_rs_rt_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5402 match_same_rs_rt_operand (struct mips_arg_info *arg,
5403 			  const struct mips_operand *operand)
5404 {
5405   unsigned int regno;
5406 
5407   if (!match_reg (arg, OP_REG_GP, &regno))
5408     return FALSE;
5409 
5410   if (regno == 0)
5411     {
5412       set_insn_error (arg->argnum, _("the source register must not be $0"));
5413       return FALSE;
5414     }
5415 
5416   arg->last_regno = regno;
5417 
5418   insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5419   return TRUE;
5420 }
5421 
5422 /* OP_LWM_SWM_LIST matcher.  */
5423 
5424 static bfd_boolean
match_lwm_swm_list_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5425 match_lwm_swm_list_operand (struct mips_arg_info *arg,
5426 			    const struct mips_operand *operand)
5427 {
5428   unsigned int reglist, sregs, ra, regno1, regno2;
5429   struct mips_arg_info reset;
5430 
5431   reglist = 0;
5432   if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5433     return FALSE;
5434   do
5435     {
5436       if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5437 	{
5438 	  reglist |= 1 << FP;
5439 	  regno2 = S7;
5440 	}
5441       reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5442       reset = *arg;
5443     }
5444   while (match_char (arg, ',')
5445 	 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5446   *arg = reset;
5447 
5448   if (operand->size == 2)
5449     {
5450       /* The list must include both ra and s0-sN, for 0 <= N <= 3.  E.g.:
5451 
5452 	 s0, ra
5453 	 s0, s1, ra, s2, s3
5454 	 s0-s2, ra
5455 
5456 	 and any permutations of these.  */
5457       if ((reglist & 0xfff1ffff) != 0x80010000)
5458 	return FALSE;
5459 
5460       sregs = (reglist >> 17) & 7;
5461       ra = 0;
5462     }
5463   else
5464     {
5465       /* The list must include at least one of ra and s0-sN,
5466 	 for 0 <= N <= 8.  (Note that there is a gap between s7 and s8,
5467 	 which are $23 and $30 respectively.)  E.g.:
5468 
5469 	 ra
5470 	 s0
5471 	 ra, s0, s1, s2
5472 	 s0-s8
5473 	 s0-s5, ra
5474 
5475 	 and any permutations of these.  */
5476       if ((reglist & 0x3f00ffff) != 0)
5477 	return FALSE;
5478 
5479       ra = (reglist >> 27) & 0x10;
5480       sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5481     }
5482   sregs += 1;
5483   if ((sregs & -sregs) != sregs)
5484     return FALSE;
5485 
5486   insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5487   return TRUE;
5488 }
5489 
5490 /* OP_ENTRY_EXIT_LIST matcher.  */
5491 
5492 static unsigned int
match_entry_exit_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5493 match_entry_exit_operand (struct mips_arg_info *arg,
5494 			  const struct mips_operand *operand)
5495 {
5496   unsigned int mask;
5497   bfd_boolean is_exit;
5498 
5499   /* The format is the same for both ENTRY and EXIT, but the constraints
5500      are different.  */
5501   is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5502   mask = (is_exit ? 7 << 3 : 0);
5503   do
5504     {
5505       unsigned int regno1, regno2;
5506       bfd_boolean is_freg;
5507 
5508       if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5509 	is_freg = FALSE;
5510       else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5511 	is_freg = TRUE;
5512       else
5513 	return FALSE;
5514 
5515       if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5516 	{
5517 	  mask &= ~(7 << 3);
5518 	  mask |= (5 + regno2) << 3;
5519 	}
5520       else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5521 	mask |= (regno2 - 3) << 3;
5522       else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5523 	mask |= (regno2 - 15) << 1;
5524       else if (regno1 == RA && regno2 == RA)
5525 	mask |= 1;
5526       else
5527 	return FALSE;
5528     }
5529   while (match_char (arg, ','));
5530 
5531   insn_insert_operand (arg->insn, operand, mask);
5532   return TRUE;
5533 }
5534 
5535 /* OP_SAVE_RESTORE_LIST matcher.  */
5536 
5537 static bfd_boolean
match_save_restore_list_operand(struct mips_arg_info * arg)5538 match_save_restore_list_operand (struct mips_arg_info *arg)
5539 {
5540   unsigned int opcode, args, statics, sregs;
5541   unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
5542   offsetT frame_size;
5543 
5544   opcode = arg->insn->insn_opcode;
5545   frame_size = 0;
5546   num_frame_sizes = 0;
5547   args = 0;
5548   statics = 0;
5549   sregs = 0;
5550   do
5551     {
5552       unsigned int regno1, regno2;
5553 
5554       if (arg->token->type == OT_INTEGER)
5555 	{
5556 	  /* Handle the frame size.  */
5557 	  if (!match_const_int (arg, &frame_size))
5558 	    return FALSE;
5559 	  num_frame_sizes += 1;
5560 	}
5561       else
5562 	{
5563 	  if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5564 	    return FALSE;
5565 
5566 	  while (regno1 <= regno2)
5567 	    {
5568 	      if (regno1 >= 4 && regno1 <= 7)
5569 		{
5570 		  if (num_frame_sizes == 0)
5571 		    /* args $a0-$a3 */
5572 		    args |= 1 << (regno1 - 4);
5573 		  else
5574 		    /* statics $a0-$a3 */
5575 		    statics |= 1 << (regno1 - 4);
5576 		}
5577 	      else if (regno1 >= 16 && regno1 <= 23)
5578 		/* $s0-$s7 */
5579 		sregs |= 1 << (regno1 - 16);
5580 	      else if (regno1 == 30)
5581 		/* $s8 */
5582 		sregs |= 1 << 8;
5583 	      else if (regno1 == 31)
5584 		/* Add $ra to insn.  */
5585 		opcode |= 0x40;
5586 	      else
5587 		return FALSE;
5588 	      regno1 += 1;
5589 	      if (regno1 == 24)
5590 		regno1 = 30;
5591 	    }
5592 	}
5593     }
5594   while (match_char (arg, ','));
5595 
5596   /* Encode args/statics combination.  */
5597   if (args & statics)
5598     return FALSE;
5599   else if (args == 0xf)
5600     /* All $a0-$a3 are args.  */
5601     opcode |= MIPS16_ALL_ARGS << 16;
5602   else if (statics == 0xf)
5603     /* All $a0-$a3 are statics.  */
5604     opcode |= MIPS16_ALL_STATICS << 16;
5605   else
5606     {
5607       /* Count arg registers.  */
5608       num_args = 0;
5609       while (args & 0x1)
5610 	{
5611 	  args >>= 1;
5612 	  num_args += 1;
5613 	}
5614       if (args != 0)
5615 	return FALSE;
5616 
5617       /* Count static registers.  */
5618       num_statics = 0;
5619       while (statics & 0x8)
5620 	{
5621 	  statics = (statics << 1) & 0xf;
5622 	  num_statics += 1;
5623 	}
5624       if (statics != 0)
5625 	return FALSE;
5626 
5627       /* Encode args/statics.  */
5628       opcode |= ((num_args << 2) | num_statics) << 16;
5629     }
5630 
5631   /* Encode $s0/$s1.  */
5632   if (sregs & (1 << 0))		/* $s0 */
5633     opcode |= 0x20;
5634   if (sregs & (1 << 1))		/* $s1 */
5635     opcode |= 0x10;
5636   sregs >>= 2;
5637 
5638   /* Encode $s2-$s8. */
5639   num_sregs = 0;
5640   while (sregs & 1)
5641     {
5642       sregs >>= 1;
5643       num_sregs += 1;
5644     }
5645   if (sregs != 0)
5646     return FALSE;
5647   opcode |= num_sregs << 24;
5648 
5649   /* Encode frame size.  */
5650   if (num_frame_sizes == 0)
5651     {
5652       set_insn_error (arg->argnum, _("missing frame size"));
5653       return FALSE;
5654     }
5655   if (num_frame_sizes > 1)
5656     {
5657       set_insn_error (arg->argnum, _("frame size specified twice"));
5658       return FALSE;
5659     }
5660   if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5661     {
5662       set_insn_error (arg->argnum, _("invalid frame size"));
5663       return FALSE;
5664     }
5665   if (frame_size != 128 || (opcode >> 16) != 0)
5666     {
5667       frame_size /= 8;
5668       opcode |= (((frame_size & 0xf0) << 16)
5669 		 | (frame_size & 0x0f));
5670     }
5671 
5672   /* Finally build the instruction.  */
5673   if ((opcode >> 16) != 0 || frame_size == 0)
5674     opcode |= MIPS16_EXTEND;
5675   arg->insn->insn_opcode = opcode;
5676   return TRUE;
5677 }
5678 
5679 /* OP_MDMX_IMM_REG matcher.  */
5680 
5681 static bfd_boolean
match_mdmx_imm_reg_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5682 match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
5683 			    const struct mips_operand *operand)
5684 {
5685   unsigned int regno, uval;
5686   bfd_boolean is_qh;
5687   const struct mips_opcode *opcode;
5688 
5689   /* The mips_opcode records whether this is an octobyte or quadhalf
5690      instruction.  Start out with that bit in place.  */
5691   opcode = arg->insn->insn_mo;
5692   uval = mips_extract_operand (operand, opcode->match);
5693   is_qh = (uval != 0);
5694 
5695   if (arg->token->type == OT_REG)
5696     {
5697       if ((opcode->membership & INSN_5400)
5698 	  && strcmp (opcode->name, "rzu.ob") == 0)
5699 	{
5700 	  set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5701 			    arg->argnum);
5702 	  return FALSE;
5703 	}
5704 
5705       if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5706 	return FALSE;
5707       ++arg->token;
5708 
5709       /* Check whether this is a vector register or a broadcast of
5710 	 a single element.  */
5711       if (arg->token->type == OT_INTEGER_INDEX)
5712 	{
5713 	  if (arg->token->u.index > (is_qh ? 3 : 7))
5714 	    {
5715 	      set_insn_error (arg->argnum, _("invalid element selector"));
5716 	      return FALSE;
5717 	    }
5718 	  uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5719 	  ++arg->token;
5720 	}
5721       else
5722 	{
5723 	  /* A full vector.  */
5724 	  if ((opcode->membership & INSN_5400)
5725 	      && (strcmp (opcode->name, "sll.ob") == 0
5726 		  || strcmp (opcode->name, "srl.ob") == 0))
5727 	    {
5728 	      set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5729 				arg->argnum);
5730 	      return FALSE;
5731 	    }
5732 
5733 	  if (is_qh)
5734 	    uval |= MDMX_FMTSEL_VEC_QH << 5;
5735 	  else
5736 	    uval |= MDMX_FMTSEL_VEC_OB << 5;
5737 	}
5738       uval |= regno;
5739     }
5740   else
5741     {
5742       offsetT sval;
5743 
5744       if (!match_const_int (arg, &sval))
5745 	return FALSE;
5746       if (sval < 0 || sval > 31)
5747 	{
5748 	  match_out_of_range (arg);
5749 	  return FALSE;
5750 	}
5751       uval |= (sval & 31);
5752       if (is_qh)
5753 	uval |= MDMX_FMTSEL_IMM_QH << 5;
5754       else
5755 	uval |= MDMX_FMTSEL_IMM_OB << 5;
5756     }
5757   insn_insert_operand (arg->insn, operand, uval);
5758   return TRUE;
5759 }
5760 
5761 /* OP_IMM_INDEX matcher.  */
5762 
5763 static bfd_boolean
match_imm_index_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5764 match_imm_index_operand (struct mips_arg_info *arg,
5765 			 const struct mips_operand *operand)
5766 {
5767   unsigned int max_val;
5768 
5769   if (arg->token->type != OT_INTEGER_INDEX)
5770     return FALSE;
5771 
5772   max_val = (1 << operand->size) - 1;
5773   if (arg->token->u.index > max_val)
5774     {
5775       match_out_of_range (arg);
5776       return FALSE;
5777     }
5778   insn_insert_operand (arg->insn, operand, arg->token->u.index);
5779   ++arg->token;
5780   return TRUE;
5781 }
5782 
5783 /* OP_MXU_STRIDE matcher.  */
5784 
5785 static bfd_boolean
match_mxu_stride_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5786 match_mxu_stride_operand (struct mips_arg_info *arg,
5787 			  const struct mips_operand *operand)
5788 {
5789   offsetT sval;
5790 
5791   if (!match_const_int (arg, &sval))
5792     return FALSE;
5793 
5794   if (sval < 0 || sval > 2)
5795     return FALSE;
5796 
5797   insn_insert_operand (arg->insn, operand, sval);
5798   return TRUE;
5799 }
5800 
5801 /* OP_REG_INDEX matcher.  */
5802 
5803 static bfd_boolean
match_reg_index_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5804 match_reg_index_operand (struct mips_arg_info *arg,
5805 			 const struct mips_operand *operand)
5806 {
5807   unsigned int regno;
5808 
5809   if (arg->token->type != OT_REG_INDEX)
5810     return FALSE;
5811 
5812   if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5813     return FALSE;
5814 
5815   insn_insert_operand (arg->insn, operand, regno);
5816   ++arg->token;
5817   return TRUE;
5818 }
5819 
5820 /* OP_PC matcher.  */
5821 
5822 static bfd_boolean
match_pc_operand(struct mips_arg_info * arg)5823 match_pc_operand (struct mips_arg_info *arg)
5824 {
5825   if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5826     {
5827       ++arg->token;
5828       return TRUE;
5829     }
5830   return FALSE;
5831 }
5832 
5833 /* OP_NON_ZERO_REG matcher.  */
5834 
5835 static bfd_boolean
match_non_zero_reg_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5836 match_non_zero_reg_operand (struct mips_arg_info *arg,
5837 			    const struct mips_operand *operand)
5838 {
5839   unsigned int regno;
5840 
5841   if (!match_reg (arg, OP_REG_GP, &regno))
5842     return FALSE;
5843 
5844   if (regno == 0)
5845     return FALSE;
5846 
5847   arg->last_regno = regno;
5848   insn_insert_operand (arg->insn, operand, regno);
5849   return TRUE;
5850 }
5851 
5852 /* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher.  OTHER_REGNO is the
5853    register that we need to match.  */
5854 
5855 static bfd_boolean
match_tied_reg_operand(struct mips_arg_info * arg,unsigned int other_regno)5856 match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
5857 {
5858   unsigned int regno;
5859 
5860   return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
5861 }
5862 
5863 /* Read a floating-point constant from S for LI.S or LI.D.  LENGTH is
5864    the length of the value in bytes (4 for float, 8 for double) and
5865    USING_GPRS says whether the destination is a GPR rather than an FPR.
5866 
5867    Return the constant in IMM and OFFSET as follows:
5868 
5869    - If the constant should be loaded via memory, set IMM to O_absent and
5870      OFFSET to the memory address.
5871 
5872    - Otherwise, if the constant should be loaded into two 32-bit registers,
5873      set IMM to the O_constant to load into the high register and OFFSET
5874      to the corresponding value for the low register.
5875 
5876    - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5877 
5878    These constants only appear as the last operand in an instruction,
5879    and every instruction that accepts them in any variant accepts them
5880    in all variants.  This means we don't have to worry about backing out
5881    any changes if the instruction does not match.  We just match
5882    unconditionally and report an error if the constant is invalid.  */
5883 
5884 static bfd_boolean
match_float_constant(struct mips_arg_info * arg,expressionS * imm,expressionS * offset,int length,bfd_boolean using_gprs)5885 match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5886 		      expressionS *offset, int length, bfd_boolean using_gprs)
5887 {
5888   char *p;
5889   segT seg, new_seg;
5890   subsegT subseg;
5891   const char *newname;
5892   unsigned char *data;
5893 
5894   /* Where the constant is placed is based on how the MIPS assembler
5895      does things:
5896 
5897      length == 4 && using_gprs  -- immediate value only
5898      length == 8 && using_gprs  -- .rdata or immediate value
5899      length == 4 && !using_gprs -- .lit4 or immediate value
5900      length == 8 && !using_gprs -- .lit8 or immediate value
5901 
5902      The .lit4 and .lit8 sections are only used if permitted by the
5903      -G argument.  */
5904   if (arg->token->type != OT_FLOAT)
5905     {
5906       set_insn_error (arg->argnum, _("floating-point expression required"));
5907       return FALSE;
5908     }
5909 
5910   gas_assert (arg->token->u.flt.length == length);
5911   data = arg->token->u.flt.data;
5912   ++arg->token;
5913 
5914   /* Handle 32-bit constants for which an immediate value is best.  */
5915   if (length == 4
5916       && (using_gprs
5917 	  || g_switch_value < 4
5918 	  || (data[0] == 0 && data[1] == 0)
5919 	  || (data[2] == 0 && data[3] == 0)))
5920     {
5921       imm->X_op = O_constant;
5922       if (!target_big_endian)
5923 	imm->X_add_number = bfd_getl32 (data);
5924       else
5925 	imm->X_add_number = bfd_getb32 (data);
5926       offset->X_op = O_absent;
5927       return TRUE;
5928     }
5929 
5930   /* Handle 64-bit constants for which an immediate value is best.  */
5931   if (length == 8
5932       && !mips_disable_float_construction
5933       /* Constants can only be constructed in GPRs and copied to FPRs if the
5934 	 GPRs are at least as wide as the FPRs or MTHC1 is available.
5935 	 Unlike most tests for 32-bit floating-point registers this check
5936 	 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
5937 	 permit 64-bit moves without MXHC1.
5938 	 Force the constant into memory otherwise.  */
5939       && (using_gprs
5940 	  || GPR_SIZE == 64
5941 	  || ISA_HAS_MXHC1 (mips_opts.isa)
5942 	  || FPR_SIZE == 32)
5943       && ((data[0] == 0 && data[1] == 0)
5944 	  || (data[2] == 0 && data[3] == 0))
5945       && ((data[4] == 0 && data[5] == 0)
5946 	  || (data[6] == 0 && data[7] == 0)))
5947     {
5948       /* The value is simple enough to load with a couple of instructions.
5949 	 If using 32-bit registers, set IMM to the high order 32 bits and
5950 	 OFFSET to the low order 32 bits.  Otherwise, set IMM to the entire
5951 	 64 bit constant.  */
5952       if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
5953 	{
5954 	  imm->X_op = O_constant;
5955 	  offset->X_op = O_constant;
5956 	  if (!target_big_endian)
5957 	    {
5958 	      imm->X_add_number = bfd_getl32 (data + 4);
5959 	      offset->X_add_number = bfd_getl32 (data);
5960 	    }
5961 	  else
5962 	    {
5963 	      imm->X_add_number = bfd_getb32 (data);
5964 	      offset->X_add_number = bfd_getb32 (data + 4);
5965 	    }
5966 	  if (offset->X_add_number == 0)
5967 	    offset->X_op = O_absent;
5968 	}
5969       else
5970 	{
5971 	  imm->X_op = O_constant;
5972 	  if (!target_big_endian)
5973 	    imm->X_add_number = bfd_getl64 (data);
5974 	  else
5975 	    imm->X_add_number = bfd_getb64 (data);
5976 	  offset->X_op = O_absent;
5977 	}
5978       return TRUE;
5979     }
5980 
5981   /* Switch to the right section.  */
5982   seg = now_seg;
5983   subseg = now_subseg;
5984   if (length == 4)
5985     {
5986       gas_assert (!using_gprs && g_switch_value >= 4);
5987       newname = ".lit4";
5988     }
5989   else
5990     {
5991       if (using_gprs || g_switch_value < 8)
5992 	newname = RDATA_SECTION_NAME;
5993       else
5994 	newname = ".lit8";
5995     }
5996 
5997   new_seg = subseg_new (newname, (subsegT) 0);
5998   bfd_set_section_flags (stdoutput, new_seg,
5999 			 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6000   frag_align (length == 4 ? 2 : 3, 0, 0);
6001   if (strncmp (TARGET_OS, "elf", 3) != 0)
6002     record_alignment (new_seg, 4);
6003   else
6004     record_alignment (new_seg, length == 4 ? 2 : 3);
6005   if (seg == now_seg)
6006     as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
6007 
6008   /* Set the argument to the current address in the section.  */
6009   imm->X_op = O_absent;
6010   offset->X_op = O_symbol;
6011   offset->X_add_symbol = symbol_temp_new_now ();
6012   offset->X_add_number = 0;
6013 
6014   /* Put the floating point number into the section.  */
6015   p = frag_more (length);
6016   memcpy (p, data, length);
6017 
6018   /* Switch back to the original section.  */
6019   subseg_set (seg, subseg);
6020   return TRUE;
6021 }
6022 
6023 /* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6024    them.  */
6025 
6026 static bfd_boolean
match_vu0_suffix_operand(struct mips_arg_info * arg,const struct mips_operand * operand,bfd_boolean match_p)6027 match_vu0_suffix_operand (struct mips_arg_info *arg,
6028 			  const struct mips_operand *operand,
6029 			  bfd_boolean match_p)
6030 {
6031   unsigned int uval;
6032 
6033   /* The operand can be an XYZW mask or a single 2-bit channel index
6034      (with X being 0).  */
6035   gas_assert (operand->size == 2 || operand->size == 4);
6036 
6037   /* The suffix can be omitted when it is already part of the opcode.  */
6038   if (arg->token->type != OT_CHANNELS)
6039     return match_p;
6040 
6041   uval = arg->token->u.channels;
6042   if (operand->size == 2)
6043     {
6044       /* Check that a single bit is set and convert it into a 2-bit index.  */
6045       if ((uval & -uval) != uval)
6046 	return FALSE;
6047       uval = 4 - ffs (uval);
6048     }
6049 
6050   if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6051     return FALSE;
6052 
6053   ++arg->token;
6054   if (!match_p)
6055     insn_insert_operand (arg->insn, operand, uval);
6056   return TRUE;
6057 }
6058 
6059 /* S is the text seen for ARG.  Match it against OPERAND.  Return the end
6060    of the argument text if the match is successful, otherwise return null.  */
6061 
6062 static bfd_boolean
match_operand(struct mips_arg_info * arg,const struct mips_operand * operand)6063 match_operand (struct mips_arg_info *arg,
6064 	       const struct mips_operand *operand)
6065 {
6066   switch (operand->type)
6067     {
6068     case OP_INT:
6069       return match_int_operand (arg, operand);
6070 
6071     case OP_MAPPED_INT:
6072       return match_mapped_int_operand (arg, operand);
6073 
6074     case OP_MSB:
6075       return match_msb_operand (arg, operand);
6076 
6077     case OP_MAPPED_STRING:
6078       return match_string_operand (arg, operand);
6079 
6080     case OP_REG:
6081     case OP_OPTIONAL_REG:
6082       return match_reg_operand (arg, operand);
6083 
6084     case OP_REG_PAIR:
6085       return match_reg_pair_operand (arg, operand);
6086 
6087     case OP_PCREL:
6088       return match_pcrel_operand (arg);
6089 
6090     case OP_PERF_REG:
6091       return match_perf_reg_operand (arg, operand);
6092 
6093     case OP_ADDIUSP_INT:
6094       return match_addiusp_operand (arg, operand);
6095 
6096     case OP_CLO_CLZ_DEST:
6097       return match_clo_clz_dest_operand (arg, operand);
6098 
6099     case OP_LWM_SWM_LIST:
6100       return match_lwm_swm_list_operand (arg, operand);
6101 
6102     case OP_ENTRY_EXIT_LIST:
6103       return match_entry_exit_operand (arg, operand);
6104 
6105     case OP_SAVE_RESTORE_LIST:
6106       return match_save_restore_list_operand (arg);
6107 
6108     case OP_MDMX_IMM_REG:
6109       return match_mdmx_imm_reg_operand (arg, operand);
6110 
6111     case OP_REPEAT_DEST_REG:
6112       return match_tied_reg_operand (arg, arg->dest_regno);
6113 
6114     case OP_REPEAT_PREV_REG:
6115       return match_tied_reg_operand (arg, arg->last_regno);
6116 
6117     case OP_PC:
6118       return match_pc_operand (arg);
6119 
6120     case OP_VU0_SUFFIX:
6121       return match_vu0_suffix_operand (arg, operand, FALSE);
6122 
6123     case OP_VU0_MATCH_SUFFIX:
6124       return match_vu0_suffix_operand (arg, operand, TRUE);
6125 
6126     case OP_IMM_INDEX:
6127       return match_imm_index_operand (arg, operand);
6128 
6129     case OP_REG_INDEX:
6130       return match_reg_index_operand (arg, operand);
6131 
6132     case OP_SAME_RS_RT:
6133       return match_same_rs_rt_operand (arg, operand);
6134 
6135     case OP_CHECK_PREV:
6136       return match_check_prev_operand (arg, operand);
6137 
6138     case OP_NON_ZERO_REG:
6139       return match_non_zero_reg_operand (arg, operand);
6140 
6141     case OP_MXU_STRIDE:
6142       return match_mxu_stride_operand (arg, operand);
6143     }
6144   abort ();
6145 }
6146 
6147 /* ARG is the state after successfully matching an instruction.
6148    Issue any queued-up warnings.  */
6149 
6150 static void
check_completed_insn(struct mips_arg_info * arg)6151 check_completed_insn (struct mips_arg_info *arg)
6152 {
6153   if (arg->seen_at)
6154     {
6155       if (AT == ATREG)
6156 	as_warn (_("used $at without \".set noat\""));
6157       else
6158 	as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
6159     }
6160 }
6161 
6162 /* Return true if modifying general-purpose register REG needs a delay.  */
6163 
6164 static bfd_boolean
reg_needs_delay(unsigned int reg)6165 reg_needs_delay (unsigned int reg)
6166 {
6167   unsigned long prev_pinfo;
6168 
6169   prev_pinfo = history[0].insn_mo->pinfo;
6170   if (!mips_opts.noreorder
6171       && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
6172 	  || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
6173       && (gpr_write_mask (&history[0]) & (1 << reg)))
6174     return TRUE;
6175 
6176   return FALSE;
6177 }
6178 
6179 /* Classify an instruction according to the FIX_VR4120_* enumeration.
6180    Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6181    by VR4120 errata.  */
6182 
6183 static unsigned int
classify_vr4120_insn(const char * name)6184 classify_vr4120_insn (const char *name)
6185 {
6186   if (strncmp (name, "macc", 4) == 0)
6187     return FIX_VR4120_MACC;
6188   if (strncmp (name, "dmacc", 5) == 0)
6189     return FIX_VR4120_DMACC;
6190   if (strncmp (name, "mult", 4) == 0)
6191     return FIX_VR4120_MULT;
6192   if (strncmp (name, "dmult", 5) == 0)
6193     return FIX_VR4120_DMULT;
6194   if (strstr (name, "div"))
6195     return FIX_VR4120_DIV;
6196   if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6197     return FIX_VR4120_MTHILO;
6198   return NUM_FIX_VR4120_CLASSES;
6199 }
6200 
6201 #define INSN_ERET	0x42000018
6202 #define INSN_DERET	0x4200001f
6203 #define INSN_DMULT	0x1c
6204 #define INSN_DMULTU	0x1d
6205 
6206 /* Return the number of instructions that must separate INSN1 and INSN2,
6207    where INSN1 is the earlier instruction.  Return the worst-case value
6208    for any INSN2 if INSN2 is null.  */
6209 
6210 static unsigned int
insns_between(const struct mips_cl_insn * insn1,const struct mips_cl_insn * insn2)6211 insns_between (const struct mips_cl_insn *insn1,
6212 	       const struct mips_cl_insn *insn2)
6213 {
6214   unsigned long pinfo1, pinfo2;
6215   unsigned int mask;
6216 
6217   /* If INFO2 is null, pessimistically assume that all flags are set for
6218      the second instruction.  */
6219   pinfo1 = insn1->insn_mo->pinfo;
6220   pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
6221 
6222   /* For most targets, write-after-read dependencies on the HI and LO
6223      registers must be separated by at least two instructions.  */
6224   if (!hilo_interlocks)
6225     {
6226       if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6227 	return 2;
6228       if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6229 	return 2;
6230     }
6231 
6232   /* If we're working around r7000 errata, there must be two instructions
6233      between an mfhi or mflo and any instruction that uses the result.  */
6234   if (mips_7000_hilo_fix
6235       && !mips_opts.micromips
6236       && MF_HILO_INSN (pinfo1)
6237       && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
6238     return 2;
6239 
6240   /* If we're working around 24K errata, one instruction is required
6241      if an ERET or DERET is followed by a branch instruction.  */
6242   if (mips_fix_24k && !mips_opts.micromips)
6243     {
6244       if (insn1->insn_opcode == INSN_ERET
6245 	  || insn1->insn_opcode == INSN_DERET)
6246 	{
6247 	  if (insn2 == NULL
6248 	      || insn2->insn_opcode == INSN_ERET
6249 	      || insn2->insn_opcode == INSN_DERET
6250 	      || delayed_branch_p (insn2))
6251 	    return 1;
6252 	}
6253     }
6254 
6255   /* If we're working around PMC RM7000 errata, there must be three
6256      nops between a dmult and a load instruction.  */
6257   if (mips_fix_rm7000 && !mips_opts.micromips)
6258     {
6259       if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6260 	  || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6261 	{
6262 	  if (pinfo2 & INSN_LOAD_MEMORY)
6263 	   return 3;
6264 	}
6265     }
6266 
6267   /* If working around VR4120 errata, check for combinations that need
6268      a single intervening instruction.  */
6269   if (mips_fix_vr4120 && !mips_opts.micromips)
6270     {
6271       unsigned int class1, class2;
6272 
6273       class1 = classify_vr4120_insn (insn1->insn_mo->name);
6274       if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
6275 	{
6276 	  if (insn2 == NULL)
6277 	    return 1;
6278 	  class2 = classify_vr4120_insn (insn2->insn_mo->name);
6279 	  if (vr4120_conflicts[class1] & (1 << class2))
6280 	    return 1;
6281 	}
6282     }
6283 
6284   if (!HAVE_CODE_COMPRESSION)
6285     {
6286       /* Check for GPR or coprocessor load delays.  All such delays
6287 	 are on the RT register.  */
6288       /* Itbl support may require additional care here.  */
6289       if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
6290 	  || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
6291 	{
6292 	  if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
6293 	    return 1;
6294 	}
6295 
6296       /* Check for generic coprocessor hazards.
6297 
6298 	 This case is not handled very well.  There is no special
6299 	 knowledge of CP0 handling, and the coprocessors other than
6300 	 the floating point unit are not distinguished at all.  */
6301       /* Itbl support may require additional care here. FIXME!
6302 	 Need to modify this to include knowledge about
6303 	 user specified delays!  */
6304       else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
6305 	       || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6306 	{
6307 	  /* Handle cases where INSN1 writes to a known general coprocessor
6308 	     register.  There must be a one instruction delay before INSN2
6309 	     if INSN2 reads that register, otherwise no delay is needed.  */
6310 	  mask = fpr_write_mask (insn1);
6311 	  if (mask != 0)
6312 	    {
6313 	      if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
6314 		return 1;
6315 	    }
6316 	  else
6317 	    {
6318 	      /* Read-after-write dependencies on the control registers
6319 		 require a two-instruction gap.  */
6320 	      if ((pinfo1 & INSN_WRITE_COND_CODE)
6321 		  && (pinfo2 & INSN_READ_COND_CODE))
6322 		return 2;
6323 
6324 	      /* We don't know exactly what INSN1 does.  If INSN2 is
6325 		 also a coprocessor instruction, assume there must be
6326 		 a one instruction gap.  */
6327 	      if (pinfo2 & INSN_COP)
6328 		return 1;
6329 	    }
6330 	}
6331 
6332       /* Check for read-after-write dependencies on the coprocessor
6333 	 control registers in cases where INSN1 does not need a general
6334 	 coprocessor delay.  This means that INSN1 is a floating point
6335 	 comparison instruction.  */
6336       /* Itbl support may require additional care here.  */
6337       else if (!cop_interlocks
6338 	       && (pinfo1 & INSN_WRITE_COND_CODE)
6339 	       && (pinfo2 & INSN_READ_COND_CODE))
6340 	return 1;
6341     }
6342 
6343   /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6344      CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6345      and pause.  */
6346   if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6347       && ((pinfo2 & INSN_NO_DELAY_SLOT)
6348 	  || (insn2 && delayed_branch_p (insn2))))
6349     return 1;
6350 
6351   return 0;
6352 }
6353 
6354 /* Return the number of nops that would be needed to work around the
6355    VR4130 mflo/mfhi errata if instruction INSN immediately followed
6356    the MAX_VR4130_NOPS instructions described by HIST.  Ignore hazards
6357    that are contained within the first IGNORE instructions of HIST.  */
6358 
6359 static int
nops_for_vr4130(int ignore,const struct mips_cl_insn * hist,const struct mips_cl_insn * insn)6360 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
6361 		 const struct mips_cl_insn *insn)
6362 {
6363   int i, j;
6364   unsigned int mask;
6365 
6366   /* Check if the instruction writes to HI or LO.  MTHI and MTLO
6367      are not affected by the errata.  */
6368   if (insn != 0
6369       && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6370 	  || strcmp (insn->insn_mo->name, "mtlo") == 0
6371 	  || strcmp (insn->insn_mo->name, "mthi") == 0))
6372     return 0;
6373 
6374   /* Search for the first MFLO or MFHI.  */
6375   for (i = 0; i < MAX_VR4130_NOPS; i++)
6376     if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
6377       {
6378 	/* Extract the destination register.  */
6379 	mask = gpr_write_mask (&hist[i]);
6380 
6381 	/* No nops are needed if INSN reads that register.  */
6382 	if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
6383 	  return 0;
6384 
6385 	/* ...or if any of the intervening instructions do.  */
6386 	for (j = 0; j < i; j++)
6387 	  if (gpr_read_mask (&hist[j]) & mask)
6388 	    return 0;
6389 
6390 	if (i >= ignore)
6391 	  return MAX_VR4130_NOPS - i;
6392       }
6393   return 0;
6394 }
6395 
6396 #define BASE_REG_EQ(INSN1, INSN2) 	\
6397   ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
6398       == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6399 
6400 /* Return the minimum alignment for this store instruction.  */
6401 
6402 static int
fix_24k_align_to(const struct mips_opcode * mo)6403 fix_24k_align_to (const struct mips_opcode *mo)
6404 {
6405   if (strcmp (mo->name, "sh") == 0)
6406     return 2;
6407 
6408   if (strcmp (mo->name, "swc1") == 0
6409       || strcmp (mo->name, "swc2") == 0
6410       || strcmp (mo->name, "sw") == 0
6411       || strcmp (mo->name, "sc") == 0
6412       || strcmp (mo->name, "s.s") == 0)
6413     return 4;
6414 
6415   if (strcmp (mo->name, "sdc1") == 0
6416       || strcmp (mo->name, "sdc2") == 0
6417       || strcmp (mo->name, "s.d") == 0)
6418     return 8;
6419 
6420   /* sb, swl, swr */
6421   return 1;
6422 }
6423 
6424 struct fix_24k_store_info
6425   {
6426     /* Immediate offset, if any, for this store instruction.  */
6427     short off;
6428     /* Alignment required by this store instruction.  */
6429     int align_to;
6430     /* True for register offsets.  */
6431     int register_offset;
6432   };
6433 
6434 /* Comparison function used by qsort.  */
6435 
6436 static int
fix_24k_sort(const void * a,const void * b)6437 fix_24k_sort (const void *a, const void *b)
6438 {
6439   const struct fix_24k_store_info *pos1 = a;
6440   const struct fix_24k_store_info *pos2 = b;
6441 
6442   return (pos1->off - pos2->off);
6443 }
6444 
6445 /* INSN is a store instruction.  Try to record the store information
6446    in STINFO.  Return false if the information isn't known.  */
6447 
6448 static bfd_boolean
fix_24k_record_store_info(struct fix_24k_store_info * stinfo,const struct mips_cl_insn * insn)6449 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
6450 			   const struct mips_cl_insn *insn)
6451 {
6452   /* The instruction must have a known offset.  */
6453   if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6454     return FALSE;
6455 
6456   stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6457   stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6458   return TRUE;
6459 }
6460 
6461 /* Return the number of nops that would be needed to work around the 24k
6462    "lost data on stores during refill" errata if instruction INSN
6463    immediately followed the 2 instructions described by HIST.
6464    Ignore hazards that are contained within the first IGNORE
6465    instructions of HIST.
6466 
6467    Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6468    for the data cache refills and store data. The following describes
6469    the scenario where the store data could be lost.
6470 
6471    * A data cache miss, due to either a load or a store, causing fill
6472      data to be supplied by the memory subsystem
6473    * The first three doublewords of fill data are returned and written
6474      into the cache
6475    * A sequence of four stores occurs in consecutive cycles around the
6476      final doubleword of the fill:
6477    * Store A
6478    * Store B
6479    * Store C
6480    * Zero, One or more instructions
6481    * Store D
6482 
6483    The four stores A-D must be to different doublewords of the line that
6484    is being filled. The fourth instruction in the sequence above permits
6485    the fill of the final doubleword to be transferred from the FSB into
6486    the cache. In the sequence above, the stores may be either integer
6487    (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6488    swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6489    different doublewords on the line. If the floating point unit is
6490    running in 1:2 mode, it is not possible to create the sequence above
6491    using only floating point store instructions.
6492 
6493    In this case, the cache line being filled is incorrectly marked
6494    invalid, thereby losing the data from any store to the line that
6495    occurs between the original miss and the completion of the five
6496    cycle sequence shown above.
6497 
6498    The workarounds are:
6499 
6500    * Run the data cache in write-through mode.
6501    * Insert a non-store instruction between
6502      Store A and Store B or Store B and Store C.  */
6503 
6504 static int
nops_for_24k(int ignore,const struct mips_cl_insn * hist,const struct mips_cl_insn * insn)6505 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
6506 	      const struct mips_cl_insn *insn)
6507 {
6508   struct fix_24k_store_info pos[3];
6509   int align, i, base_offset;
6510 
6511   if (ignore >= 2)
6512     return 0;
6513 
6514   /* If the previous instruction wasn't a store, there's nothing to
6515      worry about.  */
6516   if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6517     return 0;
6518 
6519   /* If the instructions after the previous one are unknown, we have
6520      to assume the worst.  */
6521   if (!insn)
6522     return 1;
6523 
6524   /* Check whether we are dealing with three consecutive stores.  */
6525   if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6526       || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6527     return 0;
6528 
6529   /* If we don't know the relationship between the store addresses,
6530      assume the worst.  */
6531   if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
6532       || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6533     return 1;
6534 
6535   if (!fix_24k_record_store_info (&pos[0], insn)
6536       || !fix_24k_record_store_info (&pos[1], &hist[0])
6537       || !fix_24k_record_store_info (&pos[2], &hist[1]))
6538     return 1;
6539 
6540   qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6541 
6542   /* Pick a value of ALIGN and X such that all offsets are adjusted by
6543      X bytes and such that the base register + X is known to be aligned
6544      to align bytes.  */
6545 
6546   if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6547     align = 8;
6548   else
6549     {
6550       align = pos[0].align_to;
6551       base_offset = pos[0].off;
6552       for (i = 1; i < 3; i++)
6553 	if (align < pos[i].align_to)
6554 	  {
6555 	    align = pos[i].align_to;
6556 	    base_offset = pos[i].off;
6557 	  }
6558       for (i = 0; i < 3; i++)
6559 	pos[i].off -= base_offset;
6560     }
6561 
6562   pos[0].off &= ~align + 1;
6563   pos[1].off &= ~align + 1;
6564   pos[2].off &= ~align + 1;
6565 
6566   /* If any two stores write to the same chunk, they also write to the
6567      same doubleword.  The offsets are still sorted at this point.  */
6568   if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6569     return 0;
6570 
6571   /* A range of at least 9 bytes is needed for the stores to be in
6572      non-overlapping doublewords.  */
6573   if (pos[2].off - pos[0].off <= 8)
6574     return 0;
6575 
6576   if (pos[2].off - pos[1].off >= 24
6577       || pos[1].off - pos[0].off >= 24
6578       || pos[2].off - pos[0].off >= 32)
6579     return 0;
6580 
6581   return 1;
6582 }
6583 
6584 /* Return the number of nops that would be needed if instruction INSN
6585    immediately followed the MAX_NOPS instructions given by HIST,
6586    where HIST[0] is the most recent instruction.  Ignore hazards
6587    between INSN and the first IGNORE instructions in HIST.
6588 
6589    If INSN is null, return the worse-case number of nops for any
6590    instruction.  */
6591 
6592 static int
nops_for_insn(int ignore,const struct mips_cl_insn * hist,const struct mips_cl_insn * insn)6593 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
6594 	       const struct mips_cl_insn *insn)
6595 {
6596   int i, nops, tmp_nops;
6597 
6598   nops = 0;
6599   for (i = ignore; i < MAX_DELAY_NOPS; i++)
6600     {
6601       tmp_nops = insns_between (hist + i, insn) - i;
6602       if (tmp_nops > nops)
6603 	nops = tmp_nops;
6604     }
6605 
6606   if (mips_fix_vr4130 && !mips_opts.micromips)
6607     {
6608       tmp_nops = nops_for_vr4130 (ignore, hist, insn);
6609       if (tmp_nops > nops)
6610 	nops = tmp_nops;
6611     }
6612 
6613   if (mips_fix_24k && !mips_opts.micromips)
6614     {
6615       tmp_nops = nops_for_24k (ignore, hist, insn);
6616       if (tmp_nops > nops)
6617 	nops = tmp_nops;
6618     }
6619 
6620   return nops;
6621 }
6622 
6623 /* The variable arguments provide NUM_INSNS extra instructions that
6624    might be added to HIST.  Return the largest number of nops that
6625    would be needed after the extended sequence, ignoring hazards
6626    in the first IGNORE instructions.  */
6627 
6628 static int
nops_for_sequence(int num_insns,int ignore,const struct mips_cl_insn * hist,...)6629 nops_for_sequence (int num_insns, int ignore,
6630 		   const struct mips_cl_insn *hist, ...)
6631 {
6632   va_list args;
6633   struct mips_cl_insn buffer[MAX_NOPS];
6634   struct mips_cl_insn *cursor;
6635   int nops;
6636 
6637   va_start (args, hist);
6638   cursor = buffer + num_insns;
6639   memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
6640   while (cursor > buffer)
6641     *--cursor = *va_arg (args, const struct mips_cl_insn *);
6642 
6643   nops = nops_for_insn (ignore, buffer, NULL);
6644   va_end (args);
6645   return nops;
6646 }
6647 
6648 /* Like nops_for_insn, but if INSN is a branch, take into account the
6649    worst-case delay for the branch target.  */
6650 
6651 static int
nops_for_insn_or_target(int ignore,const struct mips_cl_insn * hist,const struct mips_cl_insn * insn)6652 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
6653 			 const struct mips_cl_insn *insn)
6654 {
6655   int nops, tmp_nops;
6656 
6657   nops = nops_for_insn (ignore, hist, insn);
6658   if (delayed_branch_p (insn))
6659     {
6660       tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
6661 				    hist, insn, get_delay_slot_nop (insn));
6662       if (tmp_nops > nops)
6663 	nops = tmp_nops;
6664     }
6665   else if (compact_branch_p (insn))
6666     {
6667       tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
6668       if (tmp_nops > nops)
6669 	nops = tmp_nops;
6670     }
6671   return nops;
6672 }
6673 
6674 /* Fix NOP issue: Replace nops by "or at,at,zero".  */
6675 
6676 static void
fix_loongson2f_nop(struct mips_cl_insn * ip)6677 fix_loongson2f_nop (struct mips_cl_insn * ip)
6678 {
6679   gas_assert (!HAVE_CODE_COMPRESSION);
6680   if (strcmp (ip->insn_mo->name, "nop") == 0)
6681     ip->insn_opcode = LOONGSON2F_NOP_INSN;
6682 }
6683 
6684 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6685                    jr target pc &= 'hffff_ffff_cfff_ffff.  */
6686 
6687 static void
fix_loongson2f_jump(struct mips_cl_insn * ip)6688 fix_loongson2f_jump (struct mips_cl_insn * ip)
6689 {
6690   gas_assert (!HAVE_CODE_COMPRESSION);
6691   if (strcmp (ip->insn_mo->name, "j") == 0
6692       || strcmp (ip->insn_mo->name, "jr") == 0
6693       || strcmp (ip->insn_mo->name, "jalr") == 0)
6694     {
6695       int sreg;
6696       expressionS ep;
6697 
6698       if (! mips_opts.at)
6699         return;
6700 
6701       sreg = EXTRACT_OPERAND (0, RS, *ip);
6702       if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6703         return;
6704 
6705       ep.X_op = O_constant;
6706       ep.X_add_number = 0xcfff0000;
6707       macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6708       ep.X_add_number = 0xffff;
6709       macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6710       macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6711     }
6712 }
6713 
6714 static void
fix_loongson2f(struct mips_cl_insn * ip)6715 fix_loongson2f (struct mips_cl_insn * ip)
6716 {
6717   if (mips_fix_loongson2f_nop)
6718     fix_loongson2f_nop (ip);
6719 
6720   if (mips_fix_loongson2f_jump)
6721     fix_loongson2f_jump (ip);
6722 }
6723 
6724 /* IP is a branch that has a delay slot, and we need to fill it
6725    automatically.   Return true if we can do that by swapping IP
6726    with the previous instruction.
6727    ADDRESS_EXPR is an operand of the instruction to be used with
6728    RELOC_TYPE.  */
6729 
6730 static bfd_boolean
can_swap_branch_p(struct mips_cl_insn * ip,expressionS * address_expr,bfd_reloc_code_real_type * reloc_type)6731 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
6732 		   bfd_reloc_code_real_type *reloc_type)
6733 {
6734   unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
6735   unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
6736   unsigned int fpr_read, prev_fpr_write;
6737 
6738   /* -O2 and above is required for this optimization.  */
6739   if (mips_optimize < 2)
6740     return FALSE;
6741 
6742   /* If we have seen .set volatile or .set nomove, don't optimize.  */
6743   if (mips_opts.nomove)
6744     return FALSE;
6745 
6746   /* We can't swap if the previous instruction's position is fixed.  */
6747   if (history[0].fixed_p)
6748     return FALSE;
6749 
6750   /* If the previous previous insn was in a .set noreorder, we can't
6751      swap.  Actually, the MIPS assembler will swap in this situation.
6752      However, gcc configured -with-gnu-as will generate code like
6753 
6754 	.set	noreorder
6755 	lw	$4,XXX
6756 	.set	reorder
6757 	INSN
6758 	bne	$4,$0,foo
6759 
6760      in which we can not swap the bne and INSN.  If gcc is not configured
6761      -with-gnu-as, it does not output the .set pseudo-ops.  */
6762   if (history[1].noreorder_p)
6763     return FALSE;
6764 
6765   /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6766      This means that the previous instruction was a 4-byte one anyhow.  */
6767   if (mips_opts.mips16 && history[0].fixp[0])
6768     return FALSE;
6769 
6770   /* If the branch is itself the target of a branch, we can not swap.
6771      We cheat on this; all we check for is whether there is a label on
6772      this instruction.  If there are any branches to anything other than
6773      a label, users must use .set noreorder.  */
6774   if (seg_info (now_seg)->label_list)
6775     return FALSE;
6776 
6777   /* If the previous instruction is in a variant frag other than this
6778      branch's one, we cannot do the swap.  This does not apply to
6779      MIPS16 code, which uses variant frags for different purposes.  */
6780   if (!mips_opts.mips16
6781       && history[0].frag
6782       && history[0].frag->fr_type == rs_machine_dependent)
6783     return FALSE;
6784 
6785   /* We do not swap with instructions that cannot architecturally
6786      be placed in a branch delay slot, such as SYNC or ERET.  We
6787      also refrain from swapping with a trap instruction, since it
6788      complicates trap handlers to have the trap instruction be in
6789      a delay slot.  */
6790   prev_pinfo = history[0].insn_mo->pinfo;
6791   if (prev_pinfo & INSN_NO_DELAY_SLOT)
6792     return FALSE;
6793 
6794   /* Check for conflicts between the branch and the instructions
6795      before the candidate delay slot.  */
6796   if (nops_for_insn (0, history + 1, ip) > 0)
6797     return FALSE;
6798 
6799   /* Check for conflicts between the swapped sequence and the
6800      target of the branch.  */
6801   if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6802     return FALSE;
6803 
6804   /* If the branch reads a register that the previous
6805      instruction sets, we can not swap.  */
6806   gpr_read = gpr_read_mask (ip);
6807   prev_gpr_write = gpr_write_mask (&history[0]);
6808   if (gpr_read & prev_gpr_write)
6809     return FALSE;
6810 
6811   fpr_read = fpr_read_mask (ip);
6812   prev_fpr_write = fpr_write_mask (&history[0]);
6813   if (fpr_read & prev_fpr_write)
6814     return FALSE;
6815 
6816   /* If the branch writes a register that the previous
6817      instruction sets, we can not swap.  */
6818   gpr_write = gpr_write_mask (ip);
6819   if (gpr_write & prev_gpr_write)
6820     return FALSE;
6821 
6822   /* If the branch writes a register that the previous
6823      instruction reads, we can not swap.  */
6824   prev_gpr_read = gpr_read_mask (&history[0]);
6825   if (gpr_write & prev_gpr_read)
6826     return FALSE;
6827 
6828   /* If one instruction sets a condition code and the
6829      other one uses a condition code, we can not swap.  */
6830   pinfo = ip->insn_mo->pinfo;
6831   if ((pinfo & INSN_READ_COND_CODE)
6832       && (prev_pinfo & INSN_WRITE_COND_CODE))
6833     return FALSE;
6834   if ((pinfo & INSN_WRITE_COND_CODE)
6835       && (prev_pinfo & INSN_READ_COND_CODE))
6836     return FALSE;
6837 
6838   /* If the previous instruction uses the PC, we can not swap.  */
6839   prev_pinfo2 = history[0].insn_mo->pinfo2;
6840   if (prev_pinfo2 & INSN2_READ_PC)
6841     return FALSE;
6842 
6843   /* If the previous instruction has an incorrect size for a fixed
6844      branch delay slot in microMIPS mode, we cannot swap.  */
6845   pinfo2 = ip->insn_mo->pinfo2;
6846   if (mips_opts.micromips
6847       && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6848       && insn_length (history) != 2)
6849     return FALSE;
6850   if (mips_opts.micromips
6851       && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6852       && insn_length (history) != 4)
6853     return FALSE;
6854 
6855   /* On R5900 short loops need to be fixed by inserting a nop in
6856      the branch delay slots.
6857      A short loop can be terminated too early.  */
6858   if (mips_opts.arch == CPU_R5900
6859       /* Check if instruction has a parameter, ignore "j $31". */
6860       && (address_expr != NULL)
6861       /* Parameter must be 16 bit. */
6862       && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6863       /* Branch to same segment. */
6864       && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
6865       /* Branch to same code fragment. */
6866       && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
6867       /* Can only calculate branch offset if value is known. */
6868       && symbol_constant_p(address_expr->X_add_symbol)
6869       /* Check if branch is really conditional. */
6870       && !((ip->insn_opcode & 0xffff0000) == 0x10000000   /* beq $0,$0 */
6871 	|| (ip->insn_opcode & 0xffff0000) == 0x04010000   /* bgez $0 */
6872 	|| (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6873     {
6874       int distance;
6875       /* Check if loop is shorter than 6 instructions including
6876          branch and delay slot.  */
6877       distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
6878       if (distance <= 20)
6879         {
6880           int i;
6881           int rv;
6882 
6883           rv = FALSE;
6884           /* When the loop includes branches or jumps,
6885              it is not a short loop. */
6886           for (i = 0; i < (distance / 4); i++)
6887             {
6888               if ((history[i].cleared_p)
6889                   || delayed_branch_p(&history[i]))
6890                 {
6891                   rv = TRUE;
6892                   break;
6893                 }
6894             }
6895           if (rv == FALSE)
6896             {
6897               /* Insert nop after branch to fix short loop. */
6898               return FALSE;
6899             }
6900         }
6901     }
6902 
6903   return TRUE;
6904 }
6905 
6906 /* Decide how we should add IP to the instruction stream.
6907    ADDRESS_EXPR is an operand of the instruction to be used with
6908    RELOC_TYPE.  */
6909 
6910 static enum append_method
get_append_method(struct mips_cl_insn * ip,expressionS * address_expr,bfd_reloc_code_real_type * reloc_type)6911 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
6912 		   bfd_reloc_code_real_type *reloc_type)
6913 {
6914   /* The relaxed version of a macro sequence must be inherently
6915      hazard-free.  */
6916   if (mips_relax.sequence == 2)
6917     return APPEND_ADD;
6918 
6919   /* Convert a non-compact to compact branch/jump instruction.  */
6920   if (ISA_IS_R6 (mips_opts.isa)
6921       && (ip->insn_mo->pinfo2 & INSN2_CONVERTED_TO_COMPACT))
6922     return APPEND_ADD_COMPACT;
6923 
6924   /* We must not dabble with instructions in a ".set norerorder" block.  */
6925   if (mips_opts.noreorder)
6926     return APPEND_ADD;
6927 
6928   /* Otherwise, it's our responsibility to fill branch delay slots.  */
6929   if (delayed_branch_p (ip))
6930     {
6931       if (!branch_likely_p (ip)
6932 	  && can_swap_branch_p (ip, address_expr, reloc_type))
6933 	return APPEND_SWAP;
6934 
6935       if (mips_opts.mips16
6936 	  && ISA_SUPPORTS_MIPS16E
6937 	  && gpr_read_mask (ip) != 0)
6938 	return APPEND_ADD_COMPACT;
6939 
6940       return APPEND_ADD_WITH_NOP;
6941     }
6942 
6943   return APPEND_ADD;
6944 }
6945 
6946 /* IP is a MIPS16 instruction whose opcode we have just changed.
6947    Point IP->insn_mo to the new opcode's definition.  */
6948 
6949 static void
find_altered_mips16_opcode(struct mips_cl_insn * ip)6950 find_altered_mips16_opcode (struct mips_cl_insn *ip)
6951 {
6952   const struct mips_opcode *mo, *end;
6953 
6954   end = &mips16_opcodes[bfd_mips16_num_opcodes];
6955   for (mo = ip->insn_mo; mo < end; mo++)
6956     if ((ip->insn_opcode & mo->mask) == mo->match)
6957       {
6958 	ip->insn_mo = mo;
6959 	return;
6960       }
6961   abort ();
6962 }
6963 
6964 /* For microMIPS macros, we need to generate a local number label
6965    as the target of branches.  */
6966 #define MICROMIPS_LABEL_CHAR		'\037'
6967 static unsigned long micromips_target_label;
6968 static char micromips_target_name[32];
6969 
6970 static char *
micromips_label_name(void)6971 micromips_label_name (void)
6972 {
6973   char *p = micromips_target_name;
6974   char symbol_name_temporary[24];
6975   unsigned long l;
6976   int i;
6977 
6978   if (*p)
6979     return p;
6980 
6981   i = 0;
6982   l = micromips_target_label;
6983 #ifdef LOCAL_LABEL_PREFIX
6984   *p++ = LOCAL_LABEL_PREFIX;
6985 #endif
6986   *p++ = 'L';
6987   *p++ = MICROMIPS_LABEL_CHAR;
6988   do
6989     {
6990       symbol_name_temporary[i++] = l % 10 + '0';
6991       l /= 10;
6992     }
6993   while (l != 0);
6994   while (i > 0)
6995     *p++ = symbol_name_temporary[--i];
6996   *p = '\0';
6997 
6998   return micromips_target_name;
6999 }
7000 
7001 static void
micromips_label_expr(expressionS * label_expr)7002 micromips_label_expr (expressionS *label_expr)
7003 {
7004   label_expr->X_op = O_symbol;
7005   label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7006   label_expr->X_add_number = 0;
7007 }
7008 
7009 static void
micromips_label_inc(void)7010 micromips_label_inc (void)
7011 {
7012   micromips_target_label++;
7013   *micromips_target_name = '\0';
7014 }
7015 
7016 static void
micromips_add_label(void)7017 micromips_add_label (void)
7018 {
7019   symbolS *s;
7020 
7021   s = colon (micromips_label_name ());
7022   micromips_label_inc ();
7023   S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
7024 }
7025 
7026 /* If assembling microMIPS code, then return the microMIPS reloc
7027    corresponding to the requested one if any.  Otherwise return
7028    the reloc unchanged.  */
7029 
7030 static bfd_reloc_code_real_type
micromips_map_reloc(bfd_reloc_code_real_type reloc)7031 micromips_map_reloc (bfd_reloc_code_real_type reloc)
7032 {
7033   static const bfd_reloc_code_real_type relocs[][2] =
7034     {
7035       /* Keep sorted incrementally by the left-hand key.  */
7036       { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7037       { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7038       { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7039       { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7040       { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7041       { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7042       { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7043       { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7044       { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7045       { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7046       { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7047       { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7048       { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7049       { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7050       { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7051       { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7052       { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7053       { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7054       { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7055       { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7056       { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7057       { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7058       { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7059       { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7060       { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7061       { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7062       { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7063     };
7064   bfd_reloc_code_real_type r;
7065   size_t i;
7066 
7067   if (!mips_opts.micromips)
7068     return reloc;
7069   for (i = 0; i < ARRAY_SIZE (relocs); i++)
7070     {
7071       r = relocs[i][0];
7072       if (r > reloc)
7073 	return reloc;
7074       if (r == reloc)
7075 	return relocs[i][1];
7076     }
7077   return reloc;
7078 }
7079 
7080 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7081    Return true on success, storing the resolved value in RESULT.  */
7082 
7083 static bfd_boolean
calculate_reloc(bfd_reloc_code_real_type reloc,offsetT operand,offsetT * result)7084 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7085 		 offsetT *result)
7086 {
7087   switch (reloc)
7088     {
7089     case BFD_RELOC_MIPS_HIGHEST:
7090     case BFD_RELOC_MICROMIPS_HIGHEST:
7091       *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7092       return TRUE;
7093 
7094     case BFD_RELOC_MIPS_HIGHER:
7095     case BFD_RELOC_MICROMIPS_HIGHER:
7096       *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7097       return TRUE;
7098 
7099     case BFD_RELOC_HI16_S:
7100     case BFD_RELOC_MICROMIPS_HI16_S:
7101     case BFD_RELOC_MIPS16_HI16_S:
7102       *result = ((operand + 0x8000) >> 16) & 0xffff;
7103       return TRUE;
7104 
7105     case BFD_RELOC_HI16:
7106     case BFD_RELOC_MICROMIPS_HI16:
7107     case BFD_RELOC_MIPS16_HI16:
7108       *result = (operand >> 16) & 0xffff;
7109       return TRUE;
7110 
7111     case BFD_RELOC_LO16:
7112     case BFD_RELOC_MICROMIPS_LO16:
7113     case BFD_RELOC_MIPS16_LO16:
7114       *result = operand & 0xffff;
7115       return TRUE;
7116 
7117     case BFD_RELOC_UNUSED:
7118       *result = operand;
7119       return TRUE;
7120 
7121     default:
7122       return FALSE;
7123     }
7124 }
7125 
7126 /* Output an instruction.  IP is the instruction information.
7127    ADDRESS_EXPR is an operand of the instruction to be used with
7128    RELOC_TYPE.  EXPANSIONP is true if the instruction is part of
7129    a macro expansion.  */
7130 
7131 static void
append_insn(struct mips_cl_insn * ip,expressionS * address_expr,bfd_reloc_code_real_type * reloc_type,bfd_boolean expansionp)7132 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
7133 	     bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
7134 {
7135   unsigned long prev_pinfo2, pinfo;
7136   bfd_boolean relaxed_branch = FALSE;
7137   enum append_method method;
7138   bfd_boolean relax32;
7139   int branch_disp;
7140 
7141   if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
7142     fix_loongson2f (ip);
7143 
7144   file_ase_mips16 |= mips_opts.mips16;
7145   file_ase_micromips |= mips_opts.micromips;
7146 
7147   prev_pinfo2 = history[0].insn_mo->pinfo2;
7148   pinfo = ip->insn_mo->pinfo;
7149 
7150   if (mips_opts.micromips
7151       && !expansionp
7152       && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7153 	   && micromips_insn_length (ip->insn_mo) != 2)
7154 	  || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7155 	      && micromips_insn_length (ip->insn_mo) != 4)))
7156     as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
7157 	     (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
7158 
7159   if (address_expr == NULL)
7160     ip->complete_p = 1;
7161   else if (reloc_type[0] <= BFD_RELOC_UNUSED
7162 	   && reloc_type[1] == BFD_RELOC_UNUSED
7163 	   && reloc_type[2] == BFD_RELOC_UNUSED
7164 	   && address_expr->X_op == O_constant)
7165     {
7166       switch (*reloc_type)
7167 	{
7168 	case BFD_RELOC_MIPS_JMP:
7169 	  {
7170 	    int shift;
7171 
7172 	    shift = mips_opts.micromips ? 1 : 2;
7173 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7174 	      as_bad (_("jump to misaligned address (0x%lx)"),
7175 		      (unsigned long) address_expr->X_add_number);
7176 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7177 				& 0x3ffffff);
7178 	    ip->complete_p = 1;
7179 	  }
7180 	  break;
7181 
7182 	case BFD_RELOC_MIPS16_JMP:
7183 	  if ((address_expr->X_add_number & 3) != 0)
7184 	    as_bad (_("jump to misaligned address (0x%lx)"),
7185 	            (unsigned long) address_expr->X_add_number);
7186 	  ip->insn_opcode |=
7187 	    (((address_expr->X_add_number & 0x7c0000) << 3)
7188 	       | ((address_expr->X_add_number & 0xf800000) >> 7)
7189 	       | ((address_expr->X_add_number & 0x3fffc) >> 2));
7190 	  ip->complete_p = 1;
7191 	  break;
7192 
7193 	case BFD_RELOC_16_PCREL_S2:
7194 	  {
7195 	    int shift;
7196 
7197 	    shift = mips_opts.micromips ? 1 : 2;
7198 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7199 	      as_bad (_("branch to misaligned address (0x%lx)"),
7200 		      (unsigned long) address_expr->X_add_number);
7201 	    if (!mips_relax_branch)
7202 	      {
7203 		if ((address_expr->X_add_number + (1 << (shift + 15)))
7204 		    & ~((1 << (shift + 16)) - 1))
7205 		  as_bad (_("branch address range overflow (0x%lx)"),
7206 			  (unsigned long) address_expr->X_add_number);
7207 		ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7208 				    & 0xffff);
7209 	      }
7210 	  }
7211 	  break;
7212 
7213 	case BFD_RELOC_MIPS_21_PCREL_S2:
7214 	  {
7215 	    int shift;
7216 
7217 	    shift = 2;
7218 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7219 	      as_bad (_("branch to misaligned address (0x%lx)"),
7220 		      (unsigned long) address_expr->X_add_number);
7221 	    if ((address_expr->X_add_number + (1 << (shift + 20)))
7222 		& ~((1 << (shift + 21)) - 1))
7223 	      as_bad (_("branch address range overflow (0x%lx)"),
7224 		      (unsigned long) address_expr->X_add_number);
7225 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7226 				& 0x1fffff);
7227 	  }
7228 	  break;
7229 
7230 	case BFD_RELOC_MIPS_26_PCREL_S2:
7231 	  {
7232 	    int shift;
7233 
7234 	    shift = 2;
7235 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7236 	      as_bad (_("branch to misaligned address (0x%lx)"),
7237 		      (unsigned long) address_expr->X_add_number);
7238 	    if ((address_expr->X_add_number + (1 << (shift + 25)))
7239 		& ~((1 << (shift + 26)) - 1))
7240 	      as_bad (_("branch address range overflow (0x%lx)"),
7241 		      (unsigned long) address_expr->X_add_number);
7242 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7243 				& 0x3ffffff);
7244 	  }
7245 	  break;
7246 
7247 	default:
7248 	  {
7249 	    offsetT value;
7250 
7251 	    if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7252 				 &value))
7253 	      {
7254 		ip->insn_opcode |= value & 0xffff;
7255 		ip->complete_p = 1;
7256 	      }
7257 	  }
7258 	  break;
7259 	}
7260     }
7261 
7262   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7263     {
7264       /* There are a lot of optimizations we could do that we don't.
7265 	 In particular, we do not, in general, reorder instructions.
7266 	 If you use gcc with optimization, it will reorder
7267 	 instructions and generally do much more optimization then we
7268 	 do here; repeating all that work in the assembler would only
7269 	 benefit hand written assembly code, and does not seem worth
7270 	 it.  */
7271       int nops = (mips_optimize == 0
7272 		  ? nops_for_insn (0, history, NULL)
7273 		  : nops_for_insn_or_target (0, history, ip));
7274       if (nops > 0)
7275 	{
7276 	  fragS *old_frag;
7277 	  unsigned long old_frag_offset;
7278 	  int i;
7279 
7280 	  old_frag = frag_now;
7281 	  old_frag_offset = frag_now_fix ();
7282 
7283 	  for (i = 0; i < nops; i++)
7284 	    add_fixed_insn (NOP_INSN);
7285 	  insert_into_history (0, nops, NOP_INSN);
7286 
7287 	  if (listing)
7288 	    {
7289 	      listing_prev_line ();
7290 	      /* We may be at the start of a variant frag.  In case we
7291                  are, make sure there is enough space for the frag
7292                  after the frags created by listing_prev_line.  The
7293                  argument to frag_grow here must be at least as large
7294                  as the argument to all other calls to frag_grow in
7295                  this file.  We don't have to worry about being in the
7296                  middle of a variant frag, because the variants insert
7297                  all needed nop instructions themselves.  */
7298 	      frag_grow (40);
7299 	    }
7300 
7301 	  mips_move_text_labels ();
7302 
7303 #ifndef NO_ECOFF_DEBUGGING
7304 	  if (ECOFF_DEBUGGING)
7305 	    ecoff_fix_loc (old_frag, old_frag_offset);
7306 #endif
7307 	}
7308     }
7309   else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7310     {
7311       int nops;
7312 
7313       /* Work out how many nops in prev_nop_frag are needed by IP,
7314 	 ignoring hazards generated by the first prev_nop_frag_since
7315 	 instructions.  */
7316       nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
7317       gas_assert (nops <= prev_nop_frag_holds);
7318 
7319       /* Enforce NOPS as a minimum.  */
7320       if (nops > prev_nop_frag_required)
7321 	prev_nop_frag_required = nops;
7322 
7323       if (prev_nop_frag_holds == prev_nop_frag_required)
7324 	{
7325 	  /* Settle for the current number of nops.  Update the history
7326 	     accordingly (for the benefit of any future .set reorder code).  */
7327 	  prev_nop_frag = NULL;
7328 	  insert_into_history (prev_nop_frag_since,
7329 			       prev_nop_frag_holds, NOP_INSN);
7330 	}
7331       else
7332 	{
7333 	  /* Allow this instruction to replace one of the nops that was
7334 	     tentatively added to prev_nop_frag.  */
7335 	  prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
7336 	  prev_nop_frag_holds--;
7337 	  prev_nop_frag_since++;
7338 	}
7339     }
7340 
7341   method = get_append_method (ip, address_expr, reloc_type);
7342   branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
7343 
7344   dwarf2_emit_insn (0);
7345   /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7346      so "move" the instruction address accordingly.
7347 
7348      Also, it doesn't seem appropriate for the assembler to reorder .loc
7349      entries.  If this instruction is a branch that we are going to swap
7350      with the previous instruction, the two instructions should be
7351      treated as a unit, and the debug information for both instructions
7352      should refer to the start of the branch sequence.  Using the
7353      current position is certainly wrong when swapping a 32-bit branch
7354      and a 16-bit delay slot, since the current position would then be
7355      in the middle of a branch.  */
7356   dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
7357 
7358   relax32 = (mips_relax_branch
7359 	     /* Don't try branch relaxation within .set nomacro, or within
7360 	        .set noat if we use $at for PIC computations.  If it turns
7361 	        out that the branch was out-of-range, we'll get an error.  */
7362 	     && !mips_opts.warn_about_macros
7363 	     && (mips_opts.at || mips_pic == NO_PIC)
7364 	     /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7365 	        as they have no complementing branches.  */
7366 	     && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
7367 
7368   if (!HAVE_CODE_COMPRESSION
7369       && address_expr
7370       && relax32
7371       && *reloc_type == BFD_RELOC_16_PCREL_S2
7372       && delayed_branch_p (ip))
7373     {
7374       relaxed_branch = TRUE;
7375       add_relaxed_insn (ip, (relaxed_branch_length
7376 			     (NULL, NULL,
7377 			      uncond_branch_p (ip) ? -1
7378 			      : branch_likely_p (ip) ? 1
7379 			      : 0)), 4,
7380 			RELAX_BRANCH_ENCODE
7381 			(AT,
7382 			 uncond_branch_p (ip),
7383 			 branch_likely_p (ip),
7384 			 pinfo & INSN_WRITE_GPR_31,
7385 			 0),
7386 			address_expr->X_add_symbol,
7387 			address_expr->X_add_number);
7388       *reloc_type = BFD_RELOC_UNUSED;
7389     }
7390   else if (mips_opts.micromips
7391 	   && address_expr
7392 	   && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7393 	       || *reloc_type > BFD_RELOC_UNUSED)
7394 	   && (delayed_branch_p (ip) || compact_branch_p (ip))
7395 	   /* Don't try branch relaxation when users specify
7396 	      16-bit/32-bit instructions.  */
7397 	   && !forced_insn_length)
7398     {
7399       bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
7400       int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
7401       int uncond = uncond_branch_p (ip) ? -1 : 0;
7402       int compact = compact_branch_p (ip);
7403       int al = pinfo & INSN_WRITE_GPR_31;
7404       int length32;
7405 
7406       gas_assert (address_expr != NULL);
7407       gas_assert (!mips_relax.sequence);
7408 
7409       relaxed_branch = TRUE;
7410       length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7411       add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
7412 			RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
7413 						relax32, 0, 0),
7414 			address_expr->X_add_symbol,
7415 			address_expr->X_add_number);
7416       *reloc_type = BFD_RELOC_UNUSED;
7417     }
7418   else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
7419     {
7420       /* We need to set up a variant frag.  */
7421       gas_assert (address_expr != NULL);
7422       add_relaxed_insn (ip, 4, 0,
7423 			RELAX_MIPS16_ENCODE
7424 			(*reloc_type - BFD_RELOC_UNUSED,
7425 			 forced_insn_length == 2, forced_insn_length == 4,
7426 			 delayed_branch_p (&history[0]),
7427 			 history[0].mips16_absolute_jump_p),
7428 			make_expr_symbol (address_expr), 0);
7429     }
7430   else if (mips_opts.mips16 && insn_length (ip) == 2)
7431     {
7432       if (!delayed_branch_p (ip))
7433 	/* Make sure there is enough room to swap this instruction with
7434 	   a following jump instruction.  */
7435 	frag_grow (6);
7436       add_fixed_insn (ip);
7437     }
7438   else
7439     {
7440       if (mips_opts.mips16
7441 	  && mips_opts.noreorder
7442 	  && delayed_branch_p (&history[0]))
7443 	as_warn (_("extended instruction in delay slot"));
7444 
7445       if (mips_relax.sequence)
7446 	{
7447 	  /* If we've reached the end of this frag, turn it into a variant
7448 	     frag and record the information for the instructions we've
7449 	     written so far.  */
7450 	  if (frag_room () < 4)
7451 	    relax_close_frag ();
7452 	  mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
7453 	}
7454 
7455       if (mips_relax.sequence != 2)
7456 	{
7457 	  if (mips_macro_warning.first_insn_sizes[0] == 0)
7458 	    mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7459 	  mips_macro_warning.sizes[0] += insn_length (ip);
7460 	  mips_macro_warning.insns[0]++;
7461 	}
7462       if (mips_relax.sequence != 1)
7463 	{
7464 	  if (mips_macro_warning.first_insn_sizes[1] == 0)
7465 	    mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7466 	  mips_macro_warning.sizes[1] += insn_length (ip);
7467 	  mips_macro_warning.insns[1]++;
7468 	}
7469 
7470       if (mips_opts.mips16)
7471 	{
7472 	  ip->fixed_p = 1;
7473 	  ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7474 	}
7475       add_fixed_insn (ip);
7476     }
7477 
7478   if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
7479     {
7480       bfd_reloc_code_real_type final_type[3];
7481       reloc_howto_type *howto0;
7482       reloc_howto_type *howto;
7483       int i;
7484 
7485       /* Perform any necessary conversion to microMIPS relocations
7486 	 and find out how many relocations there actually are.  */
7487       for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7488 	final_type[i] = micromips_map_reloc (reloc_type[i]);
7489 
7490       /* In a compound relocation, it is the final (outermost)
7491 	 operator that determines the relocated field.  */
7492       howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
7493       if (!howto)
7494 	abort ();
7495 
7496       if (i > 1)
7497 	howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
7498       ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7499 				 bfd_get_reloc_size (howto),
7500 				 address_expr,
7501 				 howto0 && howto0->pc_relative,
7502 				 final_type[0]);
7503 
7504       /* Tag symbols that have a R_MIPS16_26 relocation against them.  */
7505       if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
7506 	*symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7507 
7508       /* These relocations can have an addend that won't fit in
7509 	 4 octets for 64bit assembly.  */
7510       if (GPR_SIZE == 64
7511 	  && ! howto->partial_inplace
7512 	  && (reloc_type[0] == BFD_RELOC_16
7513 	      || reloc_type[0] == BFD_RELOC_32
7514 	      || reloc_type[0] == BFD_RELOC_MIPS_JMP
7515 	      || reloc_type[0] == BFD_RELOC_GPREL16
7516 	      || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7517 	      || reloc_type[0] == BFD_RELOC_GPREL32
7518 	      || reloc_type[0] == BFD_RELOC_64
7519 	      || reloc_type[0] == BFD_RELOC_CTOR
7520 	      || reloc_type[0] == BFD_RELOC_MIPS_SUB
7521 	      || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7522 	      || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7523 	      || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7524 	      || reloc_type[0] == BFD_RELOC_MIPS_REL16
7525 	      || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7526 	      || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7527 	      || hi16_reloc_p (reloc_type[0])
7528 	      || lo16_reloc_p (reloc_type[0])))
7529 	ip->fixp[0]->fx_no_overflow = 1;
7530 
7531       /* These relocations can have an addend that won't fit in 2 octets.  */
7532       if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7533 	  || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7534 	ip->fixp[0]->fx_no_overflow = 1;
7535 
7536       if (mips_relax.sequence)
7537 	{
7538 	  if (mips_relax.first_fixup == 0)
7539 	    mips_relax.first_fixup = ip->fixp[0];
7540 	}
7541       else if (reloc_needs_lo_p (*reloc_type))
7542 	{
7543 	  struct mips_hi_fixup *hi_fixup;
7544 
7545 	  /* Reuse the last entry if it already has a matching %lo.  */
7546 	  hi_fixup = mips_hi_fixup_list;
7547 	  if (hi_fixup == 0
7548 	      || !fixup_has_matching_lo_p (hi_fixup->fixp))
7549 	    {
7550 	      hi_fixup = ((struct mips_hi_fixup *)
7551 			  xmalloc (sizeof (struct mips_hi_fixup)));
7552 	      hi_fixup->next = mips_hi_fixup_list;
7553 	      mips_hi_fixup_list = hi_fixup;
7554 	    }
7555 	  hi_fixup->fixp = ip->fixp[0];
7556 	  hi_fixup->seg = now_seg;
7557 	}
7558 
7559       /* Add fixups for the second and third relocations, if given.
7560 	 Note that the ABI allows the second relocation to be
7561 	 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
7562 	 moment we only use RSS_UNDEF, but we could add support
7563 	 for the others if it ever becomes necessary.  */
7564       for (i = 1; i < 3; i++)
7565 	if (reloc_type[i] != BFD_RELOC_UNUSED)
7566 	  {
7567 	    ip->fixp[i] = fix_new (ip->frag, ip->where,
7568 				   ip->fixp[0]->fx_size, NULL, 0,
7569 				   FALSE, final_type[i]);
7570 
7571 	    /* Use fx_tcbit to mark compound relocs.  */
7572 	    ip->fixp[0]->fx_tcbit = 1;
7573 	    ip->fixp[i]->fx_tcbit = 1;
7574 	  }
7575     }
7576   install_insn (ip);
7577 
7578   /* Update the register mask information.  */
7579   mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7580   mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
7581 
7582   switch (method)
7583     {
7584     case APPEND_ADD:
7585       insert_into_history (0, 1, ip);
7586       break;
7587 
7588     case APPEND_ADD_WITH_NOP:
7589       {
7590 	struct mips_cl_insn *nop;
7591 
7592 	insert_into_history (0, 1, ip);
7593 	nop = get_delay_slot_nop (ip);
7594 	add_fixed_insn (nop);
7595 	insert_into_history (0, 1, nop);
7596 	if (mips_relax.sequence)
7597 	  mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7598       }
7599       break;
7600 
7601     case APPEND_ADD_COMPACT:
7602       gas_assert(mips_opts.mips16 || ISA_IS_R6 (mips_opts.isa));
7603       if (mips_opts.mips16)
7604         {
7605           /* Convert MIPS16 jr/jalr into a "compact" jump.  */
7606           ip->insn_opcode |= 0x0080;
7607           find_altered_mips16_opcode (ip);
7608           install_insn (ip);
7609           insert_into_history (0, 1, ip);
7610         }
7611       else
7612         {
7613           install_insn (ip);
7614           insert_into_history (0, 1, ip);
7615         }
7616       break;
7617 
7618     case APPEND_SWAP:
7619       {
7620 	struct mips_cl_insn delay = history[0];
7621 	if (mips_opts.mips16)
7622 	  {
7623 	    know (delay.frag == ip->frag);
7624 	    move_insn (ip, delay.frag, delay.where);
7625 	    move_insn (&delay, ip->frag, ip->where + insn_length (ip));
7626 	  }
7627 	else if (relaxed_branch || delay.frag != ip->frag)
7628 	  {
7629 	    /* Add the delay slot instruction to the end of the
7630 	       current frag and shrink the fixed part of the
7631 	       original frag.  If the branch occupies the tail of
7632 	       the latter, move it backwards to cover the gap.  */
7633 	    delay.frag->fr_fix -= branch_disp;
7634 	    if (delay.frag == ip->frag)
7635 	      move_insn (ip, ip->frag, ip->where - branch_disp);
7636 	    add_fixed_insn (&delay);
7637 	  }
7638 	else
7639 	  {
7640 	    move_insn (&delay, ip->frag,
7641 		       ip->where - branch_disp + insn_length (ip));
7642 	    move_insn (ip, history[0].frag, history[0].where);
7643 	  }
7644 	history[0] = *ip;
7645 	delay.fixed_p = 1;
7646 	insert_into_history (0, 1, &delay);
7647       }
7648       break;
7649     }
7650 
7651   /* If we have just completed an unconditional branch, clear the history.  */
7652   if (((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7653       || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
7654       && !(history[0].insn_mo->pinfo2 & INSN2_CONVERTED_TO_COMPACT))
7655     {
7656       unsigned int i;
7657 
7658       mips_no_prev_insn ();
7659 
7660       for (i = 0; i < ARRAY_SIZE (history); i++)
7661 	history[i].cleared_p = 1;
7662     }
7663 
7664   /* We need to emit a label at the end of branch-likely macros.  */
7665   if (emit_branch_likely_macro)
7666     {
7667       emit_branch_likely_macro = FALSE;
7668       micromips_add_label ();
7669     }
7670 
7671   /* We just output an insn, so the next one doesn't have a label.  */
7672   mips_clear_insn_labels ();
7673 }
7674 
7675 /* Forget that there was any previous instruction or label.
7676    When BRANCH is true, the branch history is also flushed.  */
7677 
7678 static void
mips_no_prev_insn(void)7679 mips_no_prev_insn (void)
7680 {
7681   prev_nop_frag = NULL;
7682   insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
7683   mips_clear_insn_labels ();
7684 }
7685 
7686 /* This function must be called before we emit something other than
7687    instructions.  It is like mips_no_prev_insn except that it inserts
7688    any NOPS that might be needed by previous instructions.  */
7689 
7690 void
mips_emit_delays(void)7691 mips_emit_delays (void)
7692 {
7693   if (! mips_opts.noreorder)
7694     {
7695       int nops = nops_for_insn (0, history, NULL);
7696       if (nops > 0)
7697 	{
7698 	  while (nops-- > 0)
7699 	    add_fixed_insn (NOP_INSN);
7700 	  mips_move_text_labels ();
7701 	}
7702     }
7703   mips_no_prev_insn ();
7704 }
7705 
7706 /* Start a (possibly nested) noreorder block.  */
7707 
7708 static void
start_noreorder(void)7709 start_noreorder (void)
7710 {
7711   if (mips_opts.noreorder == 0)
7712     {
7713       unsigned int i;
7714       int nops;
7715 
7716       /* None of the instructions before the .set noreorder can be moved.  */
7717       for (i = 0; i < ARRAY_SIZE (history); i++)
7718 	history[i].fixed_p = 1;
7719 
7720       /* Insert any nops that might be needed between the .set noreorder
7721 	 block and the previous instructions.  We will later remove any
7722 	 nops that turn out not to be needed.  */
7723       nops = nops_for_insn (0, history, NULL);
7724       if (nops > 0)
7725 	{
7726 	  if (mips_optimize != 0)
7727 	    {
7728 	      /* Record the frag which holds the nop instructions, so
7729                  that we can remove them if we don't need them.  */
7730 	      frag_grow (nops * NOP_INSN_SIZE);
7731 	      prev_nop_frag = frag_now;
7732 	      prev_nop_frag_holds = nops;
7733 	      prev_nop_frag_required = 0;
7734 	      prev_nop_frag_since = 0;
7735 	    }
7736 
7737 	  for (; nops > 0; --nops)
7738 	    add_fixed_insn (NOP_INSN);
7739 
7740 	  /* Move on to a new frag, so that it is safe to simply
7741 	     decrease the size of prev_nop_frag.  */
7742 	  frag_wane (frag_now);
7743 	  frag_new (0);
7744 	  mips_move_text_labels ();
7745 	}
7746       mips_mark_labels ();
7747       mips_clear_insn_labels ();
7748     }
7749   mips_opts.noreorder++;
7750   mips_any_noreorder = 1;
7751 }
7752 
7753 /* End a nested noreorder block.  */
7754 
7755 static void
end_noreorder(void)7756 end_noreorder (void)
7757 {
7758   mips_opts.noreorder--;
7759   if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7760     {
7761       /* Commit to inserting prev_nop_frag_required nops and go back to
7762 	 handling nop insertion the .set reorder way.  */
7763       prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
7764 				* NOP_INSN_SIZE);
7765       insert_into_history (prev_nop_frag_since,
7766 			   prev_nop_frag_required, NOP_INSN);
7767       prev_nop_frag = NULL;
7768     }
7769 }
7770 
7771 /* Sign-extend 32-bit mode constants that have bit 31 set and all
7772    higher bits unset.  */
7773 
7774 static void
normalize_constant_expr(expressionS * ex)7775 normalize_constant_expr (expressionS *ex)
7776 {
7777   if (ex->X_op == O_constant
7778       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7779     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7780 			- 0x80000000);
7781 }
7782 
7783 /* Sign-extend 32-bit mode address offsets that have bit 31 set and
7784    all higher bits unset.  */
7785 
7786 static void
normalize_address_expr(expressionS * ex)7787 normalize_address_expr (expressionS *ex)
7788 {
7789   if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7790 	|| (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7791       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7792     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7793 			- 0x80000000);
7794 }
7795 
7796 /* Try to match TOKENS against OPCODE, storing the result in INSN.
7797    Return true if the match was successful.
7798 
7799    OPCODE_EXTRA is a value that should be ORed into the opcode
7800    (used for VU0 channel suffixes, etc.).  MORE_ALTS is true if
7801    there are more alternatives after OPCODE and SOFT_MATCH is
7802    as for mips_arg_info.  */
7803 
7804 static bfd_boolean
match_insn(struct mips_cl_insn * insn,const struct mips_opcode * opcode,struct mips_operand_token * tokens,unsigned int opcode_extra,bfd_boolean lax_match,bfd_boolean complete_p)7805 match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7806 	    struct mips_operand_token *tokens, unsigned int opcode_extra,
7807 	    bfd_boolean lax_match, bfd_boolean complete_p)
7808 {
7809   const char *args;
7810   struct mips_arg_info arg;
7811   const struct mips_operand *operand;
7812   char c;
7813 
7814   imm_expr.X_op = O_absent;
7815   offset_expr.X_op = O_absent;
7816   offset_reloc[0] = BFD_RELOC_UNUSED;
7817   offset_reloc[1] = BFD_RELOC_UNUSED;
7818   offset_reloc[2] = BFD_RELOC_UNUSED;
7819 
7820   create_insn (insn, opcode);
7821   /* When no opcode suffix is specified, assume ".xyzw". */
7822   if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
7823     insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
7824   else
7825     insn->insn_opcode |= opcode_extra;
7826   memset (&arg, 0, sizeof (arg));
7827   arg.insn = insn;
7828   arg.token = tokens;
7829   arg.argnum = 1;
7830   arg.last_regno = ILLEGAL_REG;
7831   arg.dest_regno = ILLEGAL_REG;
7832   arg.lax_match = lax_match;
7833   for (args = opcode->args;; ++args)
7834     {
7835       if (arg.token->type == OT_END)
7836 	{
7837 	  /* Handle unary instructions in which only one operand is given.
7838 	     The source is then the same as the destination.  */
7839 	  if (arg.opnum == 1 && *args == ',')
7840 	    {
7841 	      operand = (mips_opts.micromips
7842 			 ? decode_micromips_operand (args + 1)
7843 			 : decode_mips_operand (args + 1));
7844 	      if (operand && mips_optional_operand_p (operand))
7845 		{
7846 		  arg.token = tokens;
7847 		  arg.argnum = 1;
7848 		  continue;
7849 		}
7850 	    }
7851 
7852 	  /* Treat elided base registers as $0.  */
7853 	  if (strcmp (args, "(b)") == 0)
7854 	    args += 3;
7855 
7856 	  if (args[0] == '+')
7857 	    switch (args[1])
7858 	      {
7859 	      case 'K':
7860 	      case 'N':
7861 		/* The register suffix is optional. */
7862 		args += 2;
7863 		break;
7864 	      }
7865 
7866 	  /* Fail the match if there were too few operands.  */
7867 	  if (*args)
7868 	    return FALSE;
7869 
7870 	  /* Successful match.  */
7871 	  if (!complete_p)
7872 	    return TRUE;
7873 	  clear_insn_error ();
7874 	  if (arg.dest_regno == arg.last_regno
7875 	      && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
7876 	    {
7877 	      if (arg.opnum == 2)
7878 		set_insn_error
7879 		  (0, _("source and destination must be different"));
7880 	      else if (arg.last_regno == 31)
7881 		set_insn_error
7882 		  (0, _("a destination register must be supplied"));
7883 	    }
7884 	  else if (arg.last_regno == 31
7885 		   && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
7886 		       || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
7887 	    set_insn_error (0, _("the source register must not be $31"));
7888 	  check_completed_insn (&arg);
7889 	  return TRUE;
7890 	}
7891 
7892       /* Fail the match if the line has too many operands.   */
7893       if (*args == 0)
7894 	return FALSE;
7895 
7896       /* Handle characters that need to match exactly.  */
7897       if (*args == '(' || *args == ')' || *args == ',')
7898 	{
7899 	  if (match_char (&arg, *args))
7900 	    continue;
7901 	  return FALSE;
7902 	}
7903       if (*args == '#')
7904 	{
7905 	  ++args;
7906 	  if (arg.token->type == OT_DOUBLE_CHAR
7907 	      && arg.token->u.ch == *args)
7908 	    {
7909 	      ++arg.token;
7910 	      continue;
7911 	    }
7912 	  return FALSE;
7913 	}
7914 
7915       /* Handle special macro operands.  Work out the properties of
7916 	 other operands.  */
7917       arg.opnum += 1;
7918       switch (*args)
7919 	{
7920 	case '-':
7921 	  switch (args[1])
7922 	    {
7923 	    case 'A':
7924 	      *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
7925 	      break;
7926 
7927 	    case 'B':
7928 	      *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
7929 	      break;
7930 	    }
7931 	  break;
7932 
7933 	case '+':
7934 	  switch (args[1])
7935 	    {
7936 	    case 'i':
7937 	      *offset_reloc = BFD_RELOC_MIPS_JMP;
7938 	      break;
7939 
7940 	    case '\'':
7941 	      *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
7942 	      break;
7943 
7944 	    case '\"':
7945 	      *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
7946 	      break;
7947 	    }
7948 	  break;
7949 
7950 	case 'I':
7951 	  if (!match_const_int (&arg, &imm_expr.X_add_number))
7952 	    return FALSE;
7953 	  imm_expr.X_op = O_constant;
7954 	  if (GPR_SIZE == 32)
7955 	    normalize_constant_expr (&imm_expr);
7956 	  continue;
7957 
7958 	case 'A':
7959 	  if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
7960 	    {
7961 	      /* Assume that the offset has been elided and that what
7962 		 we saw was a base register.  The match will fail later
7963 		 if that assumption turns out to be wrong.  */
7964 	      offset_expr.X_op = O_constant;
7965 	      offset_expr.X_add_number = 0;
7966 	    }
7967 	  else
7968 	    {
7969 	      if (!match_expression (&arg, &offset_expr, offset_reloc))
7970 		return FALSE;
7971 	      normalize_address_expr (&offset_expr);
7972 	    }
7973 	  continue;
7974 
7975 	case 'F':
7976 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7977 				     8, TRUE))
7978 	    return FALSE;
7979 	  continue;
7980 
7981 	case 'L':
7982 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7983 				     8, FALSE))
7984 	    return FALSE;
7985 	  continue;
7986 
7987 	case 'f':
7988 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7989 				     4, TRUE))
7990 	    return FALSE;
7991 	  continue;
7992 
7993 	case 'l':
7994 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7995 				     4, FALSE))
7996 	    return FALSE;
7997 	  continue;
7998 
7999 	case 'p':
8000 	  *offset_reloc = BFD_RELOC_16_PCREL_S2;
8001 	  break;
8002 
8003 	case 'a':
8004 	  *offset_reloc = BFD_RELOC_MIPS_JMP;
8005 	  break;
8006 
8007 	case 'm':
8008 	  gas_assert (mips_opts.micromips);
8009 	  c = args[1];
8010 	  switch (c)
8011 	    {
8012 	    case 'D':
8013 	    case 'E':
8014 	      if (!forced_insn_length)
8015 		*offset_reloc = (int) BFD_RELOC_UNUSED + c;
8016 	      else if (c == 'D')
8017 		*offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8018 	      else
8019 		*offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8020 	      break;
8021 	    }
8022 	  break;
8023 	}
8024 
8025       operand = (mips_opts.micromips
8026 		 ? decode_micromips_operand (args)
8027 		 : decode_mips_operand (args));
8028       if (!operand)
8029 	abort ();
8030 
8031       /* Skip prefixes.  */
8032       if (*args == '+' || *args == 'm' || *args == '-' || *args == '`')
8033 	args++;
8034 
8035       if (mips_optional_operand_p (operand)
8036 	  && args[1] == ','
8037 	  && (arg.token[0].type != OT_REG
8038 	      || arg.token[1].type == OT_END))
8039 	{
8040 	  /* Assume that the register has been elided and is the
8041 	     same as the first operand.  */
8042 	  arg.token = tokens;
8043 	  arg.argnum = 1;
8044 	}
8045 
8046       if (!match_operand (&arg, operand))
8047 	return FALSE;
8048     }
8049 }
8050 
8051 /* Like match_insn, but for MIPS16.  */
8052 
8053 static bfd_boolean
match_mips16_insn(struct mips_cl_insn * insn,const struct mips_opcode * opcode,struct mips_operand_token * tokens)8054 match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8055 		   struct mips_operand_token *tokens)
8056 {
8057   const char *args;
8058   const struct mips_operand *operand;
8059   const struct mips_operand *ext_operand;
8060   struct mips_arg_info arg;
8061   int relax_char;
8062 
8063   create_insn (insn, opcode);
8064   imm_expr.X_op = O_absent;
8065   offset_expr.X_op = O_absent;
8066   offset_reloc[0] = BFD_RELOC_UNUSED;
8067   offset_reloc[1] = BFD_RELOC_UNUSED;
8068   offset_reloc[2] = BFD_RELOC_UNUSED;
8069   relax_char = 0;
8070 
8071   memset (&arg, 0, sizeof (arg));
8072   arg.insn = insn;
8073   arg.token = tokens;
8074   arg.argnum = 1;
8075   arg.last_regno = ILLEGAL_REG;
8076   arg.dest_regno = ILLEGAL_REG;
8077   relax_char = 0;
8078   for (args = opcode->args;; ++args)
8079     {
8080       int c;
8081 
8082       if (arg.token->type == OT_END)
8083 	{
8084 	  offsetT value;
8085 
8086 	  /* Handle unary instructions in which only one operand is given.
8087 	     The source is then the same as the destination.  */
8088 	  if (arg.opnum == 1 && *args == ',')
8089 	    {
8090 	      operand = decode_mips16_operand (args[1], FALSE);
8091 	      if (operand && mips_optional_operand_p (operand))
8092 		{
8093 		  arg.token = tokens;
8094 		  arg.argnum = 1;
8095 		  continue;
8096 		}
8097 	    }
8098 
8099 	  /* Fail the match if there were too few operands.  */
8100 	  if (*args)
8101 	    return FALSE;
8102 
8103 	  /* Successful match.  Stuff the immediate value in now, if
8104 	     we can.  */
8105 	  clear_insn_error ();
8106 	  if (opcode->pinfo == INSN_MACRO)
8107 	    {
8108 	      gas_assert (relax_char == 0 || relax_char == 'p');
8109 	      gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8110 	    }
8111 	  else if (relax_char
8112 		   && offset_expr.X_op == O_constant
8113 		   && calculate_reloc (*offset_reloc,
8114 				       offset_expr.X_add_number,
8115 				       &value))
8116 	    {
8117 	      mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
8118 			    forced_insn_length, &insn->insn_opcode);
8119 	      offset_expr.X_op = O_absent;
8120 	      *offset_reloc = BFD_RELOC_UNUSED;
8121 	    }
8122 	  else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8123 	    {
8124 	      if (forced_insn_length == 2)
8125 		set_insn_error (0, _("invalid unextended operand value"));
8126 	      forced_insn_length = 4;
8127 	      insn->insn_opcode |= MIPS16_EXTEND;
8128 	    }
8129 	  else if (relax_char)
8130 	    *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8131 
8132 	  check_completed_insn (&arg);
8133 	  return TRUE;
8134 	}
8135 
8136       /* Fail the match if the line has too many operands.   */
8137       if (*args == 0)
8138 	return FALSE;
8139 
8140       /* Handle characters that need to match exactly.  */
8141       if (*args == '(' || *args == ')' || *args == ',')
8142 	{
8143 	  if (match_char (&arg, *args))
8144 	    continue;
8145 	  return FALSE;
8146 	}
8147 
8148       arg.opnum += 1;
8149       c = *args;
8150       switch (c)
8151 	{
8152 	case 'p':
8153 	case 'q':
8154 	case 'A':
8155 	case 'B':
8156 	case 'E':
8157 	  relax_char = c;
8158 	  break;
8159 
8160 	case 'I':
8161 	  if (!match_const_int (&arg, &imm_expr.X_add_number))
8162 	    return FALSE;
8163 	  imm_expr.X_op = O_constant;
8164 	  if (GPR_SIZE == 32)
8165 	    normalize_constant_expr (&imm_expr);
8166 	  continue;
8167 
8168 	case 'a':
8169 	case 'i':
8170 	  *offset_reloc = BFD_RELOC_MIPS16_JMP;
8171 	  insn->insn_opcode <<= 16;
8172 	  break;
8173 	}
8174 
8175       operand = decode_mips16_operand (c, FALSE);
8176       if (!operand)
8177 	abort ();
8178 
8179       /* '6' is a special case.  It is used for BREAK and SDBBP,
8180 	 whose operands are only meaningful to the software that decodes
8181 	 them.  This means that there is no architectural reason why
8182 	 they cannot be prefixed by EXTEND, but in practice,
8183 	 exception handlers will only look at the instruction
8184 	 itself.  We therefore allow '6' to be extended when
8185 	 disassembling but not when assembling.  */
8186       if (operand->type != OP_PCREL && c != '6')
8187 	{
8188 	  ext_operand = decode_mips16_operand (c, TRUE);
8189 	  if (operand != ext_operand)
8190 	    {
8191 	      if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8192 		{
8193 		  offset_expr.X_op = O_constant;
8194 		  offset_expr.X_add_number = 0;
8195 		  relax_char = c;
8196 		  continue;
8197 		}
8198 
8199 	      /* We need the OT_INTEGER check because some MIPS16
8200 		 immediate variants are listed before the register ones.  */
8201 	      if (arg.token->type != OT_INTEGER
8202 		  || !match_expression (&arg, &offset_expr, offset_reloc))
8203 		return FALSE;
8204 
8205 	      /* '8' is used for SLTI(U) and has traditionally not
8206 		 been allowed to take relocation operators.  */
8207 	      if (offset_reloc[0] != BFD_RELOC_UNUSED
8208 		  && (ext_operand->size != 16 || c == '8'))
8209 		return FALSE;
8210 
8211 	      relax_char = c;
8212 	      continue;
8213 	    }
8214 	}
8215 
8216       if (mips_optional_operand_p (operand)
8217 	  && args[1] == ','
8218 	  && (arg.token[0].type != OT_REG
8219 	      || arg.token[1].type == OT_END))
8220 	{
8221 	  /* Assume that the register has been elided and is the
8222 	     same as the first operand.  */
8223 	  arg.token = tokens;
8224 	  arg.argnum = 1;
8225 	}
8226 
8227       if (!match_operand (&arg, operand))
8228 	return FALSE;
8229     }
8230 }
8231 
8232 /* Record that the current instruction is invalid for the current ISA.  */
8233 
8234 static void
match_invalid_for_isa(void)8235 match_invalid_for_isa (void)
8236 {
8237   set_insn_error_ss
8238     (0, _("opcode not supported on this processor: %s (%s)"),
8239      mips_cpu_info_from_arch (mips_opts.arch)->name,
8240      mips_cpu_info_from_isa (mips_opts.isa)->name);
8241 }
8242 
8243 /* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8244    Return true if a definite match or failure was found, storing any match
8245    in INSN.  OPCODE_EXTRA is a value that should be ORed into the opcode
8246    (to handle things like VU0 suffixes).  LAX_MATCH is true if we have already
8247    tried and failed to match under normal conditions and now want to try a
8248    more relaxed match.  */
8249 
8250 static bfd_boolean
match_insns(struct mips_cl_insn * insn,const struct mips_opcode * first,const struct mips_opcode * past,struct mips_operand_token * tokens,int opcode_extra,bfd_boolean lax_match)8251 match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8252 	     const struct mips_opcode *past, struct mips_operand_token *tokens,
8253 	     int opcode_extra, bfd_boolean lax_match)
8254 {
8255   const struct mips_opcode *opcode;
8256   const struct mips_opcode *invalid_delay_slot;
8257   bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8258 
8259   /* Search for a match, ignoring alternatives that don't satisfy the
8260      current ISA or forced_length.  */
8261   invalid_delay_slot = 0;
8262   seen_valid_for_isa = FALSE;
8263   seen_valid_for_size = FALSE;
8264   opcode = first;
8265   do
8266     {
8267       gas_assert (strcmp (opcode->name, first->name) == 0);
8268       if (is_opcode_valid (opcode))
8269 	{
8270 	  seen_valid_for_isa = TRUE;
8271 	  if (is_size_valid (opcode))
8272 	    {
8273 	      bfd_boolean delay_slot_ok;
8274 
8275 	      seen_valid_for_size = TRUE;
8276 	      delay_slot_ok = is_delay_slot_valid (opcode);
8277 	      if (match_insn (insn, opcode, tokens, opcode_extra,
8278 			      lax_match, delay_slot_ok))
8279 		{
8280 		  if (!delay_slot_ok)
8281 		    {
8282 		      if (!invalid_delay_slot)
8283 			invalid_delay_slot = opcode;
8284 		    }
8285 		  else
8286 		    return TRUE;
8287 		}
8288 	    }
8289 	}
8290       ++opcode;
8291     }
8292   while (opcode < past && strcmp (opcode->name, first->name) == 0);
8293 
8294   /* If the only matches we found had the wrong length for the delay slot,
8295      pick the first such match.  We'll issue an appropriate warning later.  */
8296   if (invalid_delay_slot)
8297     {
8298       if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8299 		      lax_match, TRUE))
8300 	return TRUE;
8301       abort ();
8302     }
8303 
8304   /* Handle the case where we didn't try to match an instruction because
8305      all the alternatives were incompatible with the current ISA.  */
8306   if (!seen_valid_for_isa)
8307     {
8308       match_invalid_for_isa ();
8309       return TRUE;
8310     }
8311 
8312   /* Handle the case where we didn't try to match an instruction because
8313      all the alternatives were of the wrong size.  */
8314   if (!seen_valid_for_size)
8315     {
8316       if (mips_opts.insn32)
8317 	set_insn_error (0, _("opcode not supported in the `insn32' mode"));
8318       else
8319 	set_insn_error_i
8320 	  (0, _("unrecognized %d-bit version of microMIPS opcode"),
8321 	   8 * forced_insn_length);
8322       return TRUE;
8323     }
8324 
8325   return FALSE;
8326 }
8327 
8328 /* Like match_insns, but for MIPS16.  */
8329 
8330 static bfd_boolean
match_mips16_insns(struct mips_cl_insn * insn,const struct mips_opcode * first,struct mips_operand_token * tokens)8331 match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8332 		    struct mips_operand_token *tokens)
8333 {
8334   const struct mips_opcode *opcode;
8335   bfd_boolean seen_valid_for_isa;
8336 
8337   /* Search for a match, ignoring alternatives that don't satisfy the
8338      current ISA.  There are no separate entries for extended forms so
8339      we deal with forced_length later.  */
8340   seen_valid_for_isa = FALSE;
8341   opcode = first;
8342   do
8343     {
8344       gas_assert (strcmp (opcode->name, first->name) == 0);
8345       if (is_opcode_valid_16 (opcode))
8346 	{
8347 	  seen_valid_for_isa = TRUE;
8348 	  if (match_mips16_insn (insn, opcode, tokens))
8349 	    return TRUE;
8350 	}
8351       ++opcode;
8352     }
8353   while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8354 	 && strcmp (opcode->name, first->name) == 0);
8355 
8356   /* Handle the case where we didn't try to match an instruction because
8357      all the alternatives were incompatible with the current ISA.  */
8358   if (!seen_valid_for_isa)
8359     {
8360       match_invalid_for_isa ();
8361       return TRUE;
8362     }
8363 
8364   return FALSE;
8365 }
8366 
8367 /* Set up global variables for the start of a new macro.  */
8368 
8369 static void
macro_start(void)8370 macro_start (void)
8371 {
8372   memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
8373   memset (&mips_macro_warning.first_insn_sizes, 0,
8374 	  sizeof (mips_macro_warning.first_insn_sizes));
8375   memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
8376   mips_macro_warning.delay_slot_p = (mips_opts.noreorder
8377 				     && delayed_branch_p (&history[0]));
8378   switch (history[0].insn_mo->pinfo2
8379 	  & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8380     {
8381     case INSN2_BRANCH_DELAY_32BIT:
8382       mips_macro_warning.delay_slot_length = 4;
8383       break;
8384     case INSN2_BRANCH_DELAY_16BIT:
8385       mips_macro_warning.delay_slot_length = 2;
8386       break;
8387     default:
8388       mips_macro_warning.delay_slot_length = 0;
8389       break;
8390     }
8391   mips_macro_warning.first_frag = NULL;
8392 }
8393 
8394 /* Given that a macro is longer than one instruction or of the wrong size,
8395    return the appropriate warning for it.  Return null if no warning is
8396    needed.  SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8397    RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8398    and RELAX_NOMACRO.  */
8399 
8400 static const char *
macro_warning(relax_substateT subtype)8401 macro_warning (relax_substateT subtype)
8402 {
8403   if (subtype & RELAX_DELAY_SLOT)
8404     return _("macro instruction expanded into multiple instructions"
8405 	     " in a branch delay slot");
8406   else if (subtype & RELAX_NOMACRO)
8407     return _("macro instruction expanded into multiple instructions");
8408   else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8409 		      | RELAX_DELAY_SLOT_SIZE_SECOND))
8410     return ((subtype & RELAX_DELAY_SLOT_16BIT)
8411 	    ? _("macro instruction expanded into a wrong size instruction"
8412 		" in a 16-bit branch delay slot")
8413 	    : _("macro instruction expanded into a wrong size instruction"
8414 		" in a 32-bit branch delay slot"));
8415   else
8416     return 0;
8417 }
8418 
8419 /* Finish up a macro.  Emit warnings as appropriate.  */
8420 
8421 static void
macro_end(void)8422 macro_end (void)
8423 {
8424   /* Relaxation warning flags.  */
8425   relax_substateT subtype = 0;
8426 
8427   /* Check delay slot size requirements.  */
8428   if (mips_macro_warning.delay_slot_length == 2)
8429     subtype |= RELAX_DELAY_SLOT_16BIT;
8430   if (mips_macro_warning.delay_slot_length != 0)
8431     {
8432       if (mips_macro_warning.delay_slot_length
8433 	  != mips_macro_warning.first_insn_sizes[0])
8434 	subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8435       if (mips_macro_warning.delay_slot_length
8436 	  != mips_macro_warning.first_insn_sizes[1])
8437 	subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8438     }
8439 
8440   /* Check instruction count requirements.  */
8441   if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8442     {
8443       if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
8444 	subtype |= RELAX_SECOND_LONGER;
8445       if (mips_opts.warn_about_macros)
8446 	subtype |= RELAX_NOMACRO;
8447       if (mips_macro_warning.delay_slot_p)
8448 	subtype |= RELAX_DELAY_SLOT;
8449     }
8450 
8451   /* If both alternatives fail to fill a delay slot correctly,
8452      emit the warning now.  */
8453   if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8454       && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8455     {
8456       relax_substateT s;
8457       const char *msg;
8458 
8459       s = subtype & (RELAX_DELAY_SLOT_16BIT
8460 		     | RELAX_DELAY_SLOT_SIZE_FIRST
8461 		     | RELAX_DELAY_SLOT_SIZE_SECOND);
8462       msg = macro_warning (s);
8463       if (msg != NULL)
8464 	as_warn ("%s", msg);
8465       subtype &= ~s;
8466     }
8467 
8468   /* If both implementations are longer than 1 instruction, then emit the
8469      warning now.  */
8470   if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8471     {
8472       relax_substateT s;
8473       const char *msg;
8474 
8475       s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8476       msg = macro_warning (s);
8477       if (msg != NULL)
8478 	as_warn ("%s", msg);
8479       subtype &= ~s;
8480     }
8481 
8482   /* If any flags still set, then one implementation might need a warning
8483      and the other either will need one of a different kind or none at all.
8484      Pass any remaining flags over to relaxation.  */
8485   if (mips_macro_warning.first_frag != NULL)
8486     mips_macro_warning.first_frag->fr_subtype |= subtype;
8487 }
8488 
8489 /* Instruction operand formats used in macros that vary between
8490    standard MIPS and microMIPS code.  */
8491 
8492 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
8493 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8494 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8495 static const char * const lui_fmt[2] = { "t,u", "s,u" };
8496 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
8497 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
8498 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8499 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8500 
8501 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
8502 #define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8503 					     : cop12_fmt[mips_opts.micromips])
8504 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
8505 #define LUI_FMT (lui_fmt[mips_opts.micromips])
8506 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
8507 #define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8508 					     : mem12_fmt[mips_opts.micromips])
8509 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
8510 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
8511 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
8512 
8513 /* Read a macro's relocation codes from *ARGS and store them in *R.
8514    The first argument in *ARGS will be either the code for a single
8515    relocation or -1 followed by the three codes that make up a
8516    composite relocation.  */
8517 
8518 static void
macro_read_relocs(va_list * args,bfd_reloc_code_real_type * r)8519 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8520 {
8521   int i, next;
8522 
8523   next = va_arg (*args, int);
8524   if (next >= 0)
8525     r[0] = (bfd_reloc_code_real_type) next;
8526   else
8527     {
8528       for (i = 0; i < 3; i++)
8529 	r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8530       /* This function is only used for 16-bit relocation fields.
8531 	 To make the macro code simpler, treat an unrelocated value
8532 	 in the same way as BFD_RELOC_LO16.  */
8533       if (r[0] == BFD_RELOC_UNUSED)
8534 	r[0] = BFD_RELOC_LO16;
8535     }
8536 }
8537 
8538 /* Build an instruction created by a macro expansion.  This is passed
8539    a pointer to the count of instructions created so far, an
8540    expression, the name of the instruction to build, an operand format
8541    string, and corresponding arguments.  */
8542 
8543 static void
macro_build(expressionS * ep,const char * name,const char * fmt,...)8544 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
8545 {
8546   const struct mips_opcode *mo = NULL;
8547   bfd_reloc_code_real_type r[3];
8548   const struct mips_opcode *amo;
8549   const struct mips_operand *operand;
8550   struct hash_control *hash;
8551   struct mips_cl_insn insn;
8552   va_list args;
8553   unsigned int uval;
8554 
8555   va_start (args, fmt);
8556 
8557   if (mips_opts.mips16)
8558     {
8559       mips16_macro_build (ep, name, fmt, &args);
8560       va_end (args);
8561       return;
8562     }
8563 
8564   r[0] = BFD_RELOC_UNUSED;
8565   r[1] = BFD_RELOC_UNUSED;
8566   r[2] = BFD_RELOC_UNUSED;
8567   hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8568   amo = (struct mips_opcode *) hash_find (hash, name);
8569   gas_assert (amo);
8570   gas_assert (strcmp (name, amo->name) == 0);
8571 
8572   do
8573     {
8574       /* Search until we get a match for NAME.  It is assumed here that
8575 	 macros will never generate MDMX, MIPS-3D, or MT instructions.
8576 	 We try to match an instruction that fulfils the branch delay
8577 	 slot instruction length requirement (if any) of the previous
8578 	 instruction.  While doing this we record the first instruction
8579 	 seen that matches all the other conditions and use it anyway
8580 	 if the requirement cannot be met; we will issue an appropriate
8581 	 warning later on.  */
8582       if (strcmp (fmt, amo->args) == 0
8583 	  && amo->pinfo != INSN_MACRO
8584 	  && is_opcode_valid (amo)
8585 	  && is_size_valid (amo))
8586 	{
8587 	  if (is_delay_slot_valid (amo))
8588 	    {
8589 	      mo = amo;
8590 	      break;
8591 	    }
8592 	  else if (!mo)
8593 	    mo = amo;
8594 	}
8595 
8596       ++amo;
8597       gas_assert (amo->name);
8598     }
8599   while (strcmp (name, amo->name) == 0);
8600 
8601   gas_assert (mo);
8602   create_insn (&insn, mo);
8603   for (; *fmt; ++fmt)
8604     {
8605       switch (*fmt)
8606 	{
8607 	case ',':
8608 	case '(':
8609 	case ')':
8610 	case 'z':
8611 	  break;
8612 
8613 	case 'i':
8614 	case 'j':
8615 	  macro_read_relocs (&args, r);
8616 	  gas_assert (*r == BFD_RELOC_GPREL16
8617 		      || *r == BFD_RELOC_MIPS_HIGHER
8618 		      || *r == BFD_RELOC_HI16_S
8619 		      || *r == BFD_RELOC_LO16
8620 		      || *r == BFD_RELOC_MIPS_GOT_OFST);
8621 	  break;
8622 
8623 	case 'o':
8624 	  macro_read_relocs (&args, r);
8625 	  break;
8626 
8627 	case 'u':
8628 	  macro_read_relocs (&args, r);
8629 	  gas_assert (ep != NULL
8630 		      && (ep->X_op == O_constant
8631 			  || (ep->X_op == O_symbol
8632 			      && (*r == BFD_RELOC_MIPS_HIGHEST
8633 				  || *r == BFD_RELOC_HI16_S
8634 				  || *r == BFD_RELOC_HI16
8635 				  || *r == BFD_RELOC_GPREL16
8636 				  || *r == BFD_RELOC_MIPS_GOT_HI16
8637 				  || *r == BFD_RELOC_MIPS_CALL_HI16))));
8638 	  break;
8639 
8640 	case 'p':
8641 	  gas_assert (ep != NULL);
8642 
8643 	  /*
8644 	   * This allows macro() to pass an immediate expression for
8645 	   * creating short branches without creating a symbol.
8646 	   *
8647 	   * We don't allow branch relaxation for these branches, as
8648 	   * they should only appear in ".set nomacro" anyway.
8649 	   */
8650 	  if (ep->X_op == O_constant)
8651 	    {
8652 	      /* For microMIPS we always use relocations for branches.
8653 	         So we should not resolve immediate values.  */
8654 	      gas_assert (!mips_opts.micromips);
8655 
8656 	      if ((ep->X_add_number & 3) != 0)
8657 		as_bad (_("branch to misaligned address (0x%lx)"),
8658 			(unsigned long) ep->X_add_number);
8659 	      if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8660 		as_bad (_("branch address range overflow (0x%lx)"),
8661 			(unsigned long) ep->X_add_number);
8662 	      insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8663 	      ep = NULL;
8664 	    }
8665 	  else
8666 	    *r = BFD_RELOC_16_PCREL_S2;
8667 	  break;
8668 
8669 	case 'a':
8670 	  gas_assert (ep != NULL);
8671 	  *r = BFD_RELOC_MIPS_JMP;
8672 	  break;
8673 
8674 	default:
8675 	  operand = (mips_opts.micromips
8676 		     ? decode_micromips_operand (fmt)
8677 		     : decode_mips_operand (fmt));
8678 	  if (!operand)
8679 	    abort ();
8680 
8681 	  uval = va_arg (args, int);
8682 	  if (operand->type == OP_CLO_CLZ_DEST)
8683 	    uval |= (uval << 5);
8684 	  insn_insert_operand (&insn, operand, uval);
8685 
8686 	  if (*fmt == '+' || *fmt == 'm' || *fmt == '-' || *fmt == '`')
8687 	    ++fmt;
8688 	  break;
8689 	}
8690     }
8691   va_end (args);
8692   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
8693 
8694   append_insn (&insn, ep, r, TRUE);
8695 }
8696 
8697 static void
mips16_macro_build(expressionS * ep,const char * name,const char * fmt,va_list * args)8698 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
8699 		    va_list *args)
8700 {
8701   struct mips_opcode *mo;
8702   struct mips_cl_insn insn;
8703   const struct mips_operand *operand;
8704   bfd_reloc_code_real_type r[3]
8705     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
8706 
8707   mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
8708   gas_assert (mo);
8709   gas_assert (strcmp (name, mo->name) == 0);
8710 
8711   while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
8712     {
8713       ++mo;
8714       gas_assert (mo->name);
8715       gas_assert (strcmp (name, mo->name) == 0);
8716     }
8717 
8718   create_insn (&insn, mo);
8719   for (; *fmt; ++fmt)
8720     {
8721       int c;
8722 
8723       c = *fmt;
8724       switch (c)
8725 	{
8726 	case ',':
8727 	case '(':
8728 	case ')':
8729 	  break;
8730 
8731 	case '0':
8732 	case 'S':
8733 	case 'P':
8734 	case 'R':
8735 	  break;
8736 
8737 	case '<':
8738 	case '>':
8739 	case '4':
8740 	case '5':
8741 	case 'H':
8742 	case 'W':
8743 	case 'D':
8744 	case 'j':
8745 	case '8':
8746 	case 'V':
8747 	case 'C':
8748 	case 'U':
8749 	case 'k':
8750 	case 'K':
8751 	case 'p':
8752 	case 'q':
8753 	  {
8754 	    offsetT value;
8755 
8756 	    gas_assert (ep != NULL);
8757 
8758 	    if (ep->X_op != O_constant)
8759 	      *r = (int) BFD_RELOC_UNUSED + c;
8760 	    else if (calculate_reloc (*r, ep->X_add_number, &value))
8761 	      {
8762 		mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
8763 		ep = NULL;
8764 		*r = BFD_RELOC_UNUSED;
8765 	      }
8766 	  }
8767 	  break;
8768 
8769 	default:
8770 	  operand = decode_mips16_operand (c, FALSE);
8771 	  if (!operand)
8772 	    abort ();
8773 
8774 	  insn_insert_operand (&insn, operand, va_arg (*args, int));
8775 	  break;
8776 	}
8777     }
8778 
8779   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
8780 
8781   append_insn (&insn, ep, r, TRUE);
8782 }
8783 
8784 /*
8785  * Generate a "jalr" instruction with a relocation hint to the called
8786  * function.  This occurs in NewABI PIC code.
8787  */
8788 static void
macro_build_jalr(expressionS * ep,int cprestore)8789 macro_build_jalr (expressionS *ep, int cprestore)
8790 {
8791   static const bfd_reloc_code_real_type jalr_relocs[2]
8792     = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
8793   bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
8794   const char *jalr;
8795   char *f = NULL;
8796 
8797   if (MIPS_JALR_HINT_P (ep))
8798     {
8799       frag_grow (8);
8800       f = frag_more (0);
8801     }
8802   if (mips_opts.micromips)
8803     {
8804       jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
8805 	      ? "jalr" : "jalrs");
8806       if (MIPS_JALR_HINT_P (ep)
8807 	  || mips_opts.insn32
8808 	  || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
8809 	macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
8810       else
8811 	macro_build (NULL, jalr, "mj", PIC_CALL_REG);
8812     }
8813   else
8814     macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
8815   if (MIPS_JALR_HINT_P (ep))
8816     fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
8817 }
8818 
8819 /*
8820  * Generate a "lui" instruction.
8821  */
8822 static void
macro_build_lui(expressionS * ep,int regnum)8823 macro_build_lui (expressionS *ep, int regnum)
8824 {
8825   gas_assert (! mips_opts.mips16);
8826 
8827   if (ep->X_op != O_constant)
8828     {
8829       gas_assert (ep->X_op == O_symbol);
8830       /* _gp_disp is a special case, used from s_cpload.
8831 	 __gnu_local_gp is used if mips_no_shared.  */
8832       gas_assert (mips_pic == NO_PIC
8833 	      || (! HAVE_NEWABI
8834 		  && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
8835 	      || (! mips_in_shared
8836 		  && strcmp (S_GET_NAME (ep->X_add_symbol),
8837                              "__gnu_local_gp") == 0));
8838     }
8839 
8840   macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
8841 }
8842 
8843 /* Generate a sequence of instructions to do a load or store from a constant
8844    offset off of a base register (breg) into/from a target register (treg),
8845    using AT if necessary.  */
8846 static void
macro_build_ldst_constoffset(expressionS * ep,const char * op,int treg,int breg,int dbl)8847 macro_build_ldst_constoffset (expressionS *ep, const char *op,
8848 			      int treg, int breg, int dbl)
8849 {
8850   gas_assert (ep->X_op == O_constant);
8851 
8852   /* Sign-extending 32-bit constants makes their handling easier.  */
8853   if (!dbl)
8854     normalize_constant_expr (ep);
8855 
8856   /* Right now, this routine can only handle signed 32-bit constants.  */
8857   if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
8858     as_warn (_("operand overflow"));
8859 
8860   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
8861     {
8862       /* Signed 16-bit offset will fit in the op.  Easy!  */
8863       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
8864     }
8865   else
8866     {
8867       /* 32-bit offset, need multiple instructions and AT, like:
8868 	   lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
8869 	   addu     $tempreg,$tempreg,$breg
8870            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
8871          to handle the complete offset.  */
8872       macro_build_lui (ep, AT);
8873       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
8874       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
8875 
8876       if (!mips_opts.at)
8877 	as_bad (_("macro used $at after \".set noat\""));
8878     }
8879 }
8880 
8881 /*			set_at()
8882  * Generates code to set the $at register to true (one)
8883  * if reg is less than the immediate expression.
8884  */
8885 static void
set_at(int reg,int unsignedp)8886 set_at (int reg, int unsignedp)
8887 {
8888   if (imm_expr.X_add_number >= -0x8000
8889       && imm_expr.X_add_number < 0x8000)
8890     macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
8891 		 AT, reg, BFD_RELOC_LO16);
8892   else
8893     {
8894       load_register (AT, &imm_expr, GPR_SIZE == 64);
8895       macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
8896     }
8897 }
8898 
8899 /* Count the leading zeroes by performing a binary chop. This is a
8900    bulky bit of source, but performance is a LOT better for the
8901    majority of values than a simple loop to count the bits:
8902        for (lcnt = 0; (lcnt < 32); lcnt++)
8903          if ((v) & (1 << (31 - lcnt)))
8904            break;
8905   However it is not code size friendly, and the gain will drop a bit
8906   on certain cached systems.
8907 */
8908 #define COUNT_TOP_ZEROES(v)             \
8909   (((v) & ~0xffff) == 0                 \
8910    ? ((v) & ~0xff) == 0                 \
8911      ? ((v) & ~0xf) == 0                \
8912        ? ((v) & ~0x3) == 0              \
8913          ? ((v) & ~0x1) == 0            \
8914            ? !(v)                       \
8915              ? 32                       \
8916              : 31                       \
8917            : 30                         \
8918          : ((v) & ~0x7) == 0            \
8919            ? 29                         \
8920            : 28                         \
8921        : ((v) & ~0x3f) == 0             \
8922          ? ((v) & ~0x1f) == 0           \
8923            ? 27                         \
8924            : 26                         \
8925          : ((v) & ~0x7f) == 0           \
8926            ? 25                         \
8927            : 24                         \
8928      : ((v) & ~0xfff) == 0              \
8929        ? ((v) & ~0x3ff) == 0            \
8930          ? ((v) & ~0x1ff) == 0          \
8931            ? 23                         \
8932            : 22                         \
8933          : ((v) & ~0x7ff) == 0          \
8934            ? 21                         \
8935            : 20                         \
8936        : ((v) & ~0x3fff) == 0           \
8937          ? ((v) & ~0x1fff) == 0         \
8938            ? 19                         \
8939            : 18                         \
8940          : ((v) & ~0x7fff) == 0         \
8941            ? 17                         \
8942            : 16                         \
8943    : ((v) & ~0xffffff) == 0             \
8944      ? ((v) & ~0xfffff) == 0            \
8945        ? ((v) & ~0x3ffff) == 0          \
8946          ? ((v) & ~0x1ffff) == 0        \
8947            ? 15                         \
8948            : 14                         \
8949          : ((v) & ~0x7ffff) == 0        \
8950            ? 13                         \
8951            : 12                         \
8952        : ((v) & ~0x3fffff) == 0         \
8953          ? ((v) & ~0x1fffff) == 0       \
8954            ? 11                         \
8955            : 10                         \
8956          : ((v) & ~0x7fffff) == 0       \
8957            ? 9                          \
8958            : 8                          \
8959      : ((v) & ~0xfffffff) == 0          \
8960        ? ((v) & ~0x3ffffff) == 0        \
8961          ? ((v) & ~0x1ffffff) == 0      \
8962            ? 7                          \
8963            : 6                          \
8964          : ((v) & ~0x7ffffff) == 0      \
8965            ? 5                          \
8966            : 4                          \
8967        : ((v) & ~0x3fffffff) == 0       \
8968          ? ((v) & ~0x1fffffff) == 0     \
8969            ? 3                          \
8970            : 2                          \
8971          : ((v) & ~0x7fffffff) == 0     \
8972            ? 1                          \
8973            : 0)
8974 
8975 /*			load_register()
8976  *  This routine generates the least number of instructions necessary to load
8977  *  an absolute expression value into a register.
8978  */
8979 static void
load_register(int reg,expressionS * ep,int dbl)8980 load_register (int reg, expressionS *ep, int dbl)
8981 {
8982   int freg;
8983   expressionS hi32, lo32;
8984 
8985   if (ep->X_op != O_big)
8986     {
8987       gas_assert (ep->X_op == O_constant);
8988 
8989       /* Sign-extending 32-bit constants makes their handling easier.  */
8990       if (!dbl)
8991 	normalize_constant_expr (ep);
8992 
8993       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
8994 	{
8995 	  /* We can handle 16 bit signed values with an addiu to
8996 	     $zero.  No need to ever use daddiu here, since $zero and
8997 	     the result are always correct in 32 bit mode.  */
8998 	  macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
8999 	  return;
9000 	}
9001       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9002 	{
9003 	  /* We can handle 16 bit unsigned values with an ori to
9004              $zero.  */
9005 	  macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9006 	  return;
9007 	}
9008       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
9009 	{
9010 	  /* 32 bit values require an lui.  */
9011 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9012 	  if ((ep->X_add_number & 0xffff) != 0)
9013 	    macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9014 	  return;
9015 	}
9016     }
9017 
9018   /* The value is larger than 32 bits.  */
9019 
9020   if (!dbl || GPR_SIZE == 32)
9021     {
9022       char value[32];
9023 
9024       sprintf_vma (value, ep->X_add_number);
9025       as_bad (_("number (0x%s) larger than 32 bits"), value);
9026       macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9027       return;
9028     }
9029 
9030   if (ep->X_op != O_big)
9031     {
9032       hi32 = *ep;
9033       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9034       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9035       hi32.X_add_number &= 0xffffffff;
9036       lo32 = *ep;
9037       lo32.X_add_number &= 0xffffffff;
9038     }
9039   else
9040     {
9041       gas_assert (ep->X_add_number > 2);
9042       if (ep->X_add_number == 3)
9043 	generic_bignum[3] = 0;
9044       else if (ep->X_add_number > 4)
9045 	as_bad (_("number larger than 64 bits"));
9046       lo32.X_op = O_constant;
9047       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9048       hi32.X_op = O_constant;
9049       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9050     }
9051 
9052   if (hi32.X_add_number == 0)
9053     freg = 0;
9054   else
9055     {
9056       int shift, bit;
9057       unsigned long hi, lo;
9058 
9059       if (hi32.X_add_number == (offsetT) 0xffffffff)
9060 	{
9061 	  if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9062 	    {
9063 	      macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9064 	      return;
9065 	    }
9066 	  if (lo32.X_add_number & 0x80000000)
9067 	    {
9068 	      macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9069 	      if (lo32.X_add_number & 0xffff)
9070 		macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9071 	      return;
9072 	    }
9073 	}
9074 
9075       /* Check for 16bit shifted constant.  We know that hi32 is
9076          non-zero, so start the mask on the first bit of the hi32
9077          value.  */
9078       shift = 17;
9079       do
9080 	{
9081 	  unsigned long himask, lomask;
9082 
9083 	  if (shift < 32)
9084 	    {
9085 	      himask = 0xffff >> (32 - shift);
9086 	      lomask = (0xffff << shift) & 0xffffffff;
9087 	    }
9088 	  else
9089 	    {
9090 	      himask = 0xffff << (shift - 32);
9091 	      lomask = 0;
9092 	    }
9093 	  if ((hi32.X_add_number & ~(offsetT) himask) == 0
9094 	      && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9095 	    {
9096 	      expressionS tmp;
9097 
9098 	      tmp.X_op = O_constant;
9099 	      if (shift < 32)
9100 		tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9101 				    | (lo32.X_add_number >> shift));
9102 	      else
9103 		tmp.X_add_number = hi32.X_add_number >> (shift - 32);
9104 	      macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9105 	      macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9106 			   reg, reg, (shift >= 32) ? shift - 32 : shift);
9107 	      return;
9108 	    }
9109 	  ++shift;
9110 	}
9111       while (shift <= (64 - 16));
9112 
9113       /* Find the bit number of the lowest one bit, and store the
9114          shifted value in hi/lo.  */
9115       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9116       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9117       if (lo != 0)
9118 	{
9119 	  bit = 0;
9120 	  while ((lo & 1) == 0)
9121 	    {
9122 	      lo >>= 1;
9123 	      ++bit;
9124 	    }
9125 	  lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9126 	  hi >>= bit;
9127 	}
9128       else
9129 	{
9130 	  bit = 32;
9131 	  while ((hi & 1) == 0)
9132 	    {
9133 	      hi >>= 1;
9134 	      ++bit;
9135 	    }
9136 	  lo = hi;
9137 	  hi = 0;
9138 	}
9139 
9140       /* Optimize if the shifted value is a (power of 2) - 1.  */
9141       if ((hi == 0 && ((lo + 1) & lo) == 0)
9142 	  || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
9143 	{
9144 	  shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
9145 	  if (shift != 0)
9146 	    {
9147 	      expressionS tmp;
9148 
9149 	      /* This instruction will set the register to be all
9150                  ones.  */
9151 	      tmp.X_op = O_constant;
9152 	      tmp.X_add_number = (offsetT) -1;
9153 	      macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9154 	      if (bit != 0)
9155 		{
9156 		  bit += shift;
9157 		  macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9158 			       reg, reg, (bit >= 32) ? bit - 32 : bit);
9159 		}
9160 	      macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
9161 			   reg, reg, (shift >= 32) ? shift - 32 : shift);
9162 	      return;
9163 	    }
9164 	}
9165 
9166       /* Sign extend hi32 before calling load_register, because we can
9167          generally get better code when we load a sign extended value.  */
9168       if ((hi32.X_add_number & 0x80000000) != 0)
9169 	hi32.X_add_number |= ~(offsetT) 0xffffffff;
9170       load_register (reg, &hi32, 0);
9171       freg = reg;
9172     }
9173   if ((lo32.X_add_number & 0xffff0000) == 0)
9174     {
9175       if (freg != 0)
9176 	{
9177 	  macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
9178 	  freg = reg;
9179 	}
9180     }
9181   else
9182     {
9183       expressionS mid16;
9184 
9185       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
9186 	{
9187 	  macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9188 	  macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
9189 	  return;
9190 	}
9191 
9192       if (freg != 0)
9193 	{
9194 	  macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
9195 	  freg = reg;
9196 	}
9197       mid16 = lo32;
9198       mid16.X_add_number >>= 16;
9199       macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9200       macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9201       freg = reg;
9202     }
9203   if ((lo32.X_add_number & 0xffff) != 0)
9204     macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9205 }
9206 
9207 static inline void
load_delay_nop(void)9208 load_delay_nop (void)
9209 {
9210   if (!gpr_interlocks)
9211     macro_build (NULL, "nop", "");
9212 }
9213 
9214 /* Load an address into a register.  */
9215 
9216 static void
load_address(int reg,expressionS * ep,int * used_at)9217 load_address (int reg, expressionS *ep, int *used_at)
9218 {
9219   if (ep->X_op != O_constant
9220       && ep->X_op != O_symbol)
9221     {
9222       as_bad (_("expression too complex"));
9223       ep->X_op = O_constant;
9224     }
9225 
9226   if (ep->X_op == O_constant)
9227     {
9228       load_register (reg, ep, HAVE_64BIT_ADDRESSES);
9229       return;
9230     }
9231 
9232   if (mips_pic == NO_PIC)
9233     {
9234       /* If this is a reference to a GP relative symbol, we want
9235 	   addiu	$reg,$gp,<sym>		(BFD_RELOC_GPREL16)
9236 	 Otherwise we want
9237 	   lui		$reg,<sym>		(BFD_RELOC_HI16_S)
9238 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
9239 	 If we have an addend, we always use the latter form.
9240 
9241 	 With 64bit address space and a usable $at we want
9242 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
9243 	   lui		$at,<sym>		(BFD_RELOC_HI16_S)
9244 	   daddiu	$reg,<sym>		(BFD_RELOC_MIPS_HIGHER)
9245 	   daddiu	$at,<sym>		(BFD_RELOC_LO16)
9246 	   dsll32	$reg,0
9247 	   daddu	$reg,$reg,$at
9248 
9249 	 If $at is already in use, we use a path which is suboptimal
9250 	 on superscalar processors.
9251 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
9252 	   daddiu	$reg,<sym>		(BFD_RELOC_MIPS_HIGHER)
9253 	   dsll		$reg,16
9254 	   daddiu	$reg,<sym>		(BFD_RELOC_HI16_S)
9255 	   dsll		$reg,16
9256 	   daddiu	$reg,<sym>		(BFD_RELOC_LO16)
9257 
9258 	 For GP relative symbols in 64bit address space we can use
9259 	 the same sequence as in 32bit address space.  */
9260       if (HAVE_64BIT_SYMBOLS)
9261 	{
9262 	  if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9263 	      && !nopic_need_relax (ep->X_add_symbol, 1))
9264 	    {
9265 	      relax_start (ep->X_add_symbol);
9266 	      macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9267 			   mips_gp_register, BFD_RELOC_GPREL16);
9268 	      relax_switch ();
9269 	    }
9270 
9271 	  if (*used_at == 0 && mips_opts.at)
9272 	    {
9273 	      macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9274 	      macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
9275 	      macro_build (ep, "daddiu", "t,r,j", reg, reg,
9276 			   BFD_RELOC_MIPS_HIGHER);
9277 	      macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
9278 	      macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
9279 	      macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
9280 	      *used_at = 1;
9281 	    }
9282 	  else
9283 	    {
9284 	      macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9285 	      macro_build (ep, "daddiu", "t,r,j", reg, reg,
9286 			   BFD_RELOC_MIPS_HIGHER);
9287 	      macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9288 	      macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
9289 	      macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9290 	      macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
9291 	    }
9292 
9293 	  if (mips_relax.sequence)
9294 	    relax_end ();
9295 	}
9296       else
9297 	{
9298 	  if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9299 	      && !nopic_need_relax (ep->X_add_symbol, 1))
9300 	    {
9301 	      relax_start (ep->X_add_symbol);
9302 	      macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9303 			   mips_gp_register, BFD_RELOC_GPREL16);
9304 	      relax_switch ();
9305 	    }
9306 	  macro_build_lui (ep, reg);
9307 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9308 		       reg, reg, BFD_RELOC_LO16);
9309 	  if (mips_relax.sequence)
9310 	    relax_end ();
9311 	}
9312     }
9313   else if (!mips_big_got)
9314     {
9315       expressionS ex;
9316 
9317       /* If this is a reference to an external symbol, we want
9318 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9319 	 Otherwise we want
9320 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9321 	   nop
9322 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
9323 	 If there is a constant, it must be added in after.
9324 
9325 	 If we have NewABI, we want
9326 	   lw		$reg,<sym+cst>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
9327          unless we're referencing a global symbol with a non-zero
9328          offset, in which case cst must be added separately.  */
9329       if (HAVE_NEWABI)
9330 	{
9331 	  if (ep->X_add_number)
9332 	    {
9333 	      ex.X_add_number = ep->X_add_number;
9334 	      ep->X_add_number = 0;
9335 	      relax_start (ep->X_add_symbol);
9336 	      macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9337 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9338 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9339 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9340 	      ex.X_op = O_constant;
9341 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9342 			   reg, reg, BFD_RELOC_LO16);
9343 	      ep->X_add_number = ex.X_add_number;
9344 	      relax_switch ();
9345 	    }
9346 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9347 		       BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9348 	  if (mips_relax.sequence)
9349 	    relax_end ();
9350 	}
9351       else
9352 	{
9353 	  ex.X_add_number = ep->X_add_number;
9354 	  ep->X_add_number = 0;
9355 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9356 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9357 	  load_delay_nop ();
9358 	  relax_start (ep->X_add_symbol);
9359 	  relax_switch ();
9360 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9361 		       BFD_RELOC_LO16);
9362 	  relax_end ();
9363 
9364 	  if (ex.X_add_number != 0)
9365 	    {
9366 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9367 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9368 	      ex.X_op = O_constant;
9369 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9370 			   reg, reg, BFD_RELOC_LO16);
9371 	    }
9372 	}
9373     }
9374   else if (mips_big_got)
9375     {
9376       expressionS ex;
9377 
9378       /* This is the large GOT case.  If this is a reference to an
9379 	 external symbol, we want
9380 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
9381 	   addu		$reg,$reg,$gp
9382 	   lw		$reg,<sym>($reg)	(BFD_RELOC_MIPS_GOT_LO16)
9383 
9384 	 Otherwise, for a reference to a local symbol in old ABI, we want
9385 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9386 	   nop
9387 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
9388 	 If there is a constant, it must be added in after.
9389 
9390 	 In the NewABI, for local symbols, with or without offsets, we want:
9391 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT_PAGE)
9392 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_MIPS_GOT_OFST)
9393       */
9394       if (HAVE_NEWABI)
9395 	{
9396 	  ex.X_add_number = ep->X_add_number;
9397 	  ep->X_add_number = 0;
9398 	  relax_start (ep->X_add_symbol);
9399 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9400 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9401 		       reg, reg, mips_gp_register);
9402 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9403 		       reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9404 	  if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9405 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9406 	  else if (ex.X_add_number)
9407 	    {
9408 	      ex.X_op = O_constant;
9409 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9410 			   BFD_RELOC_LO16);
9411 	    }
9412 
9413 	  ep->X_add_number = ex.X_add_number;
9414 	  relax_switch ();
9415 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9416 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9417 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9418 		       BFD_RELOC_MIPS_GOT_OFST);
9419 	  relax_end ();
9420 	}
9421       else
9422 	{
9423 	  ex.X_add_number = ep->X_add_number;
9424 	  ep->X_add_number = 0;
9425 	  relax_start (ep->X_add_symbol);
9426 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9427 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9428 		       reg, reg, mips_gp_register);
9429 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9430 		       reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9431 	  relax_switch ();
9432 	  if (reg_needs_delay (mips_gp_register))
9433 	    {
9434 	      /* We need a nop before loading from $gp.  This special
9435 		 check is required because the lui which starts the main
9436 		 instruction stream does not refer to $gp, and so will not
9437 		 insert the nop which may be required.  */
9438 	      macro_build (NULL, "nop", "");
9439 	    }
9440 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9441 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9442 	  load_delay_nop ();
9443 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9444 		       BFD_RELOC_LO16);
9445 	  relax_end ();
9446 
9447 	  if (ex.X_add_number != 0)
9448 	    {
9449 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9450 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9451 	      ex.X_op = O_constant;
9452 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9453 			   BFD_RELOC_LO16);
9454 	    }
9455 	}
9456     }
9457   else
9458     abort ();
9459 
9460   if (!mips_opts.at && *used_at == 1)
9461     as_bad (_("macro used $at after \".set noat\""));
9462 }
9463 
9464 /* Move the contents of register SOURCE into register DEST.  */
9465 
9466 static void
move_register(int dest,int source)9467 move_register (int dest, int source)
9468 {
9469   /* Prefer to use a 16-bit microMIPS instruction unless the previous
9470      instruction specifically requires a 32-bit one.  */
9471   if (mips_opts.micromips
9472       && !mips_opts.insn32
9473       && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9474     macro_build (NULL, "move", "mp,mj", dest, source);
9475   else
9476     macro_build (NULL, GPR_SIZE == 32 ? "addu" : "daddu", "d,v,t",
9477 		 dest, source, 0);
9478 }
9479 
9480 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
9481    LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9482    The two alternatives are:
9483 
9484    Global symbol		Local sybmol
9485    -------------		------------
9486    lw DEST,%got(SYMBOL)		lw DEST,%got(SYMBOL + OFFSET)
9487    ...				...
9488    addiu DEST,DEST,OFFSET	addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9489 
9490    load_got_offset emits the first instruction and add_got_offset
9491    emits the second for a 16-bit offset or add_got_offset_hilo emits
9492    a sequence to add a 32-bit offset using a scratch register.  */
9493 
9494 static void
load_got_offset(int dest,expressionS * local)9495 load_got_offset (int dest, expressionS *local)
9496 {
9497   expressionS global;
9498 
9499   global = *local;
9500   global.X_add_number = 0;
9501 
9502   relax_start (local->X_add_symbol);
9503   macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9504 	       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9505   relax_switch ();
9506   macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9507 	       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9508   relax_end ();
9509 }
9510 
9511 static void
add_got_offset(int dest,expressionS * local)9512 add_got_offset (int dest, expressionS *local)
9513 {
9514   expressionS global;
9515 
9516   global.X_op = O_constant;
9517   global.X_op_symbol = NULL;
9518   global.X_add_symbol = NULL;
9519   global.X_add_number = local->X_add_number;
9520 
9521   relax_start (local->X_add_symbol);
9522   macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
9523 	       dest, dest, BFD_RELOC_LO16);
9524   relax_switch ();
9525   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
9526   relax_end ();
9527 }
9528 
9529 static void
add_got_offset_hilo(int dest,expressionS * local,int tmp)9530 add_got_offset_hilo (int dest, expressionS *local, int tmp)
9531 {
9532   expressionS global;
9533   int hold_mips_optimize;
9534 
9535   global.X_op = O_constant;
9536   global.X_op_symbol = NULL;
9537   global.X_add_symbol = NULL;
9538   global.X_add_number = local->X_add_number;
9539 
9540   relax_start (local->X_add_symbol);
9541   load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9542   relax_switch ();
9543   /* Set mips_optimize around the lui instruction to avoid
9544      inserting an unnecessary nop after the lw.  */
9545   hold_mips_optimize = mips_optimize;
9546   mips_optimize = 2;
9547   macro_build_lui (&global, tmp);
9548   mips_optimize = hold_mips_optimize;
9549   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9550   relax_end ();
9551 
9552   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9553 }
9554 
9555 /* Emit a sequence of instructions to emulate a branch likely operation.
9556    BR is an ordinary branch corresponding to one to be emulated.  BRNEG
9557    is its complementing branch with the original condition negated.
9558    CALL is set if the original branch specified the link operation.
9559    EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9560 
9561    Code like this is produced in the noreorder mode:
9562 
9563 	BRNEG	<args>, 1f
9564 	 nop
9565 	b	<sym>
9566 	 delay slot (executed only if branch taken)
9567     1:
9568 
9569    or, if CALL is set:
9570 
9571 	BRNEG	<args>, 1f
9572 	 nop
9573 	bal	<sym>
9574 	 delay slot (executed only if branch taken)
9575     1:
9576 
9577    In the reorder mode the delay slot would be filled with a nop anyway,
9578    so code produced is simply:
9579 
9580 	BR	<args>, <sym>
9581 	 nop
9582 
9583    This function is used when producing code for the microMIPS ASE that
9584    does not implement branch likely instructions in hardware.  */
9585 
9586 static void
macro_build_branch_likely(const char * br,const char * brneg,int call,expressionS * ep,const char * fmt,unsigned int sreg,unsigned int treg)9587 macro_build_branch_likely (const char *br, const char *brneg,
9588 			   int call, expressionS *ep, const char *fmt,
9589 			   unsigned int sreg, unsigned int treg)
9590 {
9591   int noreorder = mips_opts.noreorder;
9592   expressionS expr1;
9593 
9594   gas_assert (mips_opts.micromips);
9595   start_noreorder ();
9596   if (noreorder)
9597     {
9598       micromips_label_expr (&expr1);
9599       macro_build (&expr1, brneg, fmt, sreg, treg);
9600       macro_build (NULL, "nop", "");
9601       macro_build (ep, call ? "bal" : "b", "p");
9602 
9603       /* Set to true so that append_insn adds a label.  */
9604       emit_branch_likely_macro = TRUE;
9605     }
9606   else
9607     {
9608       macro_build (ep, br, fmt, sreg, treg);
9609       macro_build (NULL, "nop", "");
9610     }
9611   end_noreorder ();
9612 }
9613 
9614 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9615    the condition code tested.  EP specifies the branch target.  */
9616 
9617 static void
macro_build_branch_ccl(int type,expressionS * ep,unsigned int cc)9618 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9619 {
9620   const int call = 0;
9621   const char *brneg;
9622   const char *br;
9623 
9624   switch (type)
9625     {
9626     case M_BC1FL:
9627       br = "bc1f";
9628       brneg = "bc1t";
9629       break;
9630     case M_BC1TL:
9631       br = "bc1t";
9632       brneg = "bc1f";
9633       break;
9634     case M_BC2FL:
9635       br = "bc2f";
9636       brneg = "bc2t";
9637       break;
9638     case M_BC2TL:
9639       br = "bc2t";
9640       brneg = "bc2f";
9641       break;
9642     default:
9643       abort ();
9644     }
9645   macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9646 }
9647 
9648 /* Emit a two-argument branch macro specified by TYPE, using SREG as
9649    the register tested.  EP specifies the branch target.  */
9650 
9651 static void
macro_build_branch_rs(int type,expressionS * ep,unsigned int sreg)9652 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9653 {
9654   const char *brneg = NULL;
9655   const char *br;
9656   int call = 0;
9657 
9658   switch (type)
9659     {
9660     case M_BGEZ:
9661       br = "bgez";
9662       break;
9663     case M_BGEZL:
9664       br = mips_opts.micromips ? "bgez" : "bgezl";
9665       brneg = "bltz";
9666       break;
9667     case M_BGEZALL:
9668       gas_assert (mips_opts.micromips);
9669       br = mips_opts.insn32 ? "bgezal" : "bgezals";
9670       brneg = "bltz";
9671       call = 1;
9672       break;
9673     case M_BGTZ:
9674       br = "bgtz";
9675       break;
9676     case M_BGTZL:
9677       br = mips_opts.micromips ? "bgtz" : "bgtzl";
9678       brneg = "blez";
9679       break;
9680     case M_BLEZ:
9681       br = "blez";
9682       break;
9683     case M_BLEZL:
9684       br = mips_opts.micromips ? "blez" : "blezl";
9685       brneg = "bgtz";
9686       break;
9687     case M_BLTZ:
9688       br = "bltz";
9689       break;
9690     case M_BLTZL:
9691       br = mips_opts.micromips ? "bltz" : "bltzl";
9692       brneg = "bgez";
9693       break;
9694     case M_BLTZALL:
9695       gas_assert (mips_opts.micromips);
9696       br = mips_opts.insn32 ? "bltzal" : "bltzals";
9697       brneg = "bgez";
9698       call = 1;
9699       break;
9700     default:
9701       abort ();
9702     }
9703   if (mips_opts.micromips && brneg)
9704     macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9705   else
9706     macro_build (ep, br, "s,p", sreg);
9707 }
9708 
9709 /* Emit a three-argument branch macro specified by TYPE, using SREG and
9710    TREG as the registers tested.  EP specifies the branch target.  */
9711 
9712 static void
macro_build_branch_rsrt(int type,expressionS * ep,unsigned int sreg,unsigned int treg)9713 macro_build_branch_rsrt (int type, expressionS *ep,
9714 			 unsigned int sreg, unsigned int treg)
9715 {
9716   const char *brneg = NULL;
9717   const int call = 0;
9718   const char *br;
9719 
9720   switch (type)
9721     {
9722     case M_BEQ:
9723     case M_BEQ_I:
9724       br = "beq";
9725       break;
9726     case M_BEQL:
9727     case M_BEQL_I:
9728       br = mips_opts.micromips ? "beq" : "beql";
9729       brneg = "bne";
9730       break;
9731     case M_BNE:
9732     case M_BNE_I:
9733       br = "bne";
9734       break;
9735     case M_BNEL:
9736     case M_BNEL_I:
9737       br = mips_opts.micromips ? "bne" : "bnel";
9738       brneg = "beq";
9739       break;
9740     default:
9741       abort ();
9742     }
9743   if (mips_opts.micromips && brneg)
9744     macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
9745   else
9746     macro_build (ep, br, "s,t,p", sreg, treg);
9747 }
9748 
9749 /* Return the high part that should be loaded in order to make the low
9750    part of VALUE accessible using an offset of OFFBITS bits.  */
9751 
9752 static offsetT
offset_high_part(offsetT value,unsigned int offbits)9753 offset_high_part (offsetT value, unsigned int offbits)
9754 {
9755   offsetT bias;
9756   addressT low_mask;
9757 
9758   if (offbits == 0)
9759     return value;
9760   bias = 1 << (offbits - 1);
9761   low_mask = bias * 2 - 1;
9762   return (value + bias) & ~low_mask;
9763 }
9764 
9765 /* Return true if the value stored in offset_expr and offset_reloc
9766    fits into a signed offset of OFFBITS bits.  RANGE is the maximum
9767    amount that the caller wants to add without inducing overflow
9768    and ALIGN is the known alignment of the value in bytes.  */
9769 
9770 static bfd_boolean
small_offset_p(unsigned int range,unsigned int align,unsigned int offbits)9771 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
9772 {
9773   if (offbits == 16)
9774     {
9775       /* Accept any relocation operator if overflow isn't a concern.  */
9776       if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
9777 	return TRUE;
9778 
9779       /* These relocations are guaranteed not to overflow in correct links.  */
9780       if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
9781 	  || gprel16_reloc_p (*offset_reloc))
9782 	return TRUE;
9783     }
9784   if (offset_expr.X_op == O_constant
9785       && offset_high_part (offset_expr.X_add_number, offbits) == 0
9786       && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
9787     return TRUE;
9788   return FALSE;
9789 }
9790 
9791 /*
9792  *			Build macros
9793  *   This routine implements the seemingly endless macro or synthesized
9794  * instructions and addressing modes in the mips assembly language. Many
9795  * of these macros are simple and are similar to each other. These could
9796  * probably be handled by some kind of table or grammar approach instead of
9797  * this verbose method. Others are not simple macros but are more like
9798  * optimizing code generation.
9799  *   One interesting optimization is when several store macros appear
9800  * consecutively that would load AT with the upper half of the same address.
9801  * The ensuing load upper instructions are ommited. This implies some kind
9802  * of global optimization. We currently only optimize within a single macro.
9803  *   For many of the load and store macros if the address is specified as a
9804  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
9805  * first load register 'at' with zero and use it as the base register. The
9806  * mips assembler simply uses register $zero. Just one tiny optimization
9807  * we're missing.
9808  */
9809 static void
macro(struct mips_cl_insn * ip,char * str)9810 macro (struct mips_cl_insn *ip, char *str)
9811 {
9812   const struct mips_operand_array *operands;
9813   unsigned int breg, i;
9814   unsigned int tempreg;
9815   int mask;
9816   int used_at = 0;
9817   expressionS label_expr;
9818   expressionS expr1;
9819   expressionS *ep;
9820   const char *s;
9821   const char *s2;
9822   const char *fmt;
9823   int likely = 0;
9824   int coproc = 0;
9825   int offbits = 16;
9826   int call = 0;
9827   int jals = 0;
9828   int dbl = 0;
9829   int imm = 0;
9830   int ust = 0;
9831   int lp = 0;
9832   bfd_boolean large_offset;
9833   int off;
9834   int hold_mips_optimize;
9835   unsigned int align;
9836   unsigned int op[MAX_OPERANDS];
9837 
9838   gas_assert (! mips_opts.mips16);
9839 
9840   operands = insn_operands (ip);
9841   for (i = 0; i < MAX_OPERANDS; i++)
9842     if (operands->operand[i])
9843       op[i] = insn_extract_operand (ip, operands->operand[i]);
9844     else
9845       op[i] = -1;
9846 
9847   mask = ip->insn_mo->mask;
9848 
9849   label_expr.X_op = O_constant;
9850   label_expr.X_op_symbol = NULL;
9851   label_expr.X_add_symbol = NULL;
9852   label_expr.X_add_number = 0;
9853 
9854   expr1.X_op = O_constant;
9855   expr1.X_op_symbol = NULL;
9856   expr1.X_add_symbol = NULL;
9857   expr1.X_add_number = 1;
9858   align = 1;
9859 
9860   switch (mask)
9861     {
9862     case M_DABS:
9863       dbl = 1;
9864     case M_ABS:
9865       /*    bgez    $a0,1f
9866 	    move    v0,$a0
9867 	    sub     v0,$zero,$a0
9868 	 1:
9869        */
9870 
9871       start_noreorder ();
9872 
9873       if (mips_opts.micromips)
9874 	micromips_label_expr (&label_expr);
9875       else
9876 	label_expr.X_add_number = 8;
9877       macro_build (&label_expr, "bgez", "s,p", op[1]);
9878       if (op[0] == op[1])
9879 	macro_build (NULL, "nop", "");
9880       else
9881 	move_register (op[0], op[1]);
9882       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
9883       if (mips_opts.micromips)
9884 	micromips_add_label ();
9885 
9886       end_noreorder ();
9887       break;
9888 
9889     case M_ADD_I:
9890       s = "addi";
9891       s2 = "add";
9892       goto do_addi;
9893     case M_ADDU_I:
9894       s = "addiu";
9895       s2 = "addu";
9896       goto do_addi;
9897     case M_DADD_I:
9898       dbl = 1;
9899       s = "daddi";
9900       s2 = "dadd";
9901       if (!mips_opts.micromips)
9902 	goto do_addi;
9903       if (imm_expr.X_add_number >= -0x200
9904 	  && imm_expr.X_add_number < 0x200)
9905 	{
9906 	  macro_build (NULL, s, "t,r,.", op[0], op[1],
9907 		       (int) imm_expr.X_add_number);
9908 	  break;
9909 	}
9910       goto do_addi_i;
9911     case M_DADDU_I:
9912       dbl = 1;
9913       s = "daddiu";
9914       s2 = "daddu";
9915     do_addi:
9916       if (imm_expr.X_add_number >= -0x8000
9917 	  && imm_expr.X_add_number < 0x8000)
9918 	{
9919 	  macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
9920 	  break;
9921 	}
9922     do_addi_i:
9923       used_at = 1;
9924       load_register (AT, &imm_expr, dbl);
9925       macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
9926       break;
9927 
9928     case M_AND_I:
9929       s = "andi";
9930       s2 = "and";
9931       goto do_bit;
9932     case M_OR_I:
9933       s = "ori";
9934       s2 = "or";
9935       goto do_bit;
9936     case M_NOR_I:
9937       s = "";
9938       s2 = "nor";
9939       goto do_bit;
9940     case M_XOR_I:
9941       s = "xori";
9942       s2 = "xor";
9943     do_bit:
9944       if (imm_expr.X_add_number >= 0
9945 	  && imm_expr.X_add_number < 0x10000)
9946 	{
9947 	  if (mask != M_NOR_I)
9948 	    macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
9949 	  else
9950 	    {
9951 	      macro_build (&imm_expr, "ori", "t,r,i",
9952 			   op[0], op[1], BFD_RELOC_LO16);
9953 	      macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
9954 	    }
9955 	  break;
9956 	}
9957 
9958       used_at = 1;
9959       load_register (AT, &imm_expr, GPR_SIZE == 64);
9960       macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
9961       break;
9962 
9963     case M_BALIGN:
9964       switch (imm_expr.X_add_number)
9965 	{
9966 	case 0:
9967 	  macro_build (NULL, "nop", "");
9968 	  break;
9969 	case 2:
9970 	  macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
9971 	  break;
9972 	case 1:
9973 	case 3:
9974 	  macro_build (NULL, "balign", "t,s,2", op[0], op[1],
9975 		       (int) imm_expr.X_add_number);
9976 	  break;
9977 	default:
9978 	  as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
9979 		  (unsigned long) imm_expr.X_add_number);
9980 	  break;
9981 	}
9982       break;
9983 
9984     case M_BC1FL:
9985     case M_BC1TL:
9986     case M_BC2FL:
9987     case M_BC2TL:
9988       gas_assert (mips_opts.micromips);
9989       macro_build_branch_ccl (mask, &offset_expr,
9990 			      EXTRACT_OPERAND (1, BCC, *ip));
9991       break;
9992 
9993     case M_BEQ_I:
9994     case M_BEQL_I:
9995     case M_BNE_I:
9996     case M_BNEL_I:
9997       if (imm_expr.X_add_number == 0)
9998 	op[1] = 0;
9999       else
10000 	{
10001 	  op[1] = AT;
10002 	  used_at = 1;
10003 	  load_register (op[1], &imm_expr, GPR_SIZE == 64);
10004 	}
10005       /* Fall through.  */
10006     case M_BEQL:
10007     case M_BNEL:
10008       macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
10009       break;
10010 
10011     case M_BGEL:
10012       likely = 1;
10013     case M_BGE:
10014       if (op[1] == 0)
10015 	macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10016       else if (op[0] == 0)
10017 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
10018       else
10019 	{
10020 	  used_at = 1;
10021 	  macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10022 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10023 				   &offset_expr, AT, ZERO);
10024 	}
10025       break;
10026 
10027     case M_BGEZL:
10028     case M_BGEZALL:
10029     case M_BGTZL:
10030     case M_BLEZL:
10031     case M_BLTZL:
10032     case M_BLTZALL:
10033       macro_build_branch_rs (mask, &offset_expr, op[0]);
10034       break;
10035 
10036     case M_BGTL_I:
10037       likely = 1;
10038     case M_BGT_I:
10039       /* Check for > max integer.  */
10040       if (imm_expr.X_add_number >= GPR_SMAX)
10041 	{
10042 	do_false:
10043 	  /* Result is always false.  */
10044 	  if (! likely)
10045 	    macro_build (NULL, "nop", "");
10046 	  else
10047 	    macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
10048 	  break;
10049 	}
10050       ++imm_expr.X_add_number;
10051       /* FALLTHROUGH */
10052     case M_BGE_I:
10053     case M_BGEL_I:
10054       if (mask == M_BGEL_I)
10055 	likely = 1;
10056       if (imm_expr.X_add_number == 0)
10057 	{
10058 	  macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
10059 				 &offset_expr, op[0]);
10060 	  break;
10061 	}
10062       if (imm_expr.X_add_number == 1)
10063 	{
10064 	  macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
10065 				 &offset_expr, op[0]);
10066 	  break;
10067 	}
10068       if (imm_expr.X_add_number <= GPR_SMIN)
10069 	{
10070 	do_true:
10071 	  /* result is always true */
10072 	  as_warn (_("branch %s is always true"), ip->insn_mo->name);
10073 	  macro_build (&offset_expr, "b", "p");
10074 	  break;
10075 	}
10076       used_at = 1;
10077       set_at (op[0], 0);
10078       macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10079 			       &offset_expr, AT, ZERO);
10080       break;
10081 
10082     case M_BGEUL:
10083       likely = 1;
10084     case M_BGEU:
10085       if (op[1] == 0)
10086 	goto do_true;
10087       else if (op[0] == 0)
10088 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10089 				 &offset_expr, ZERO, op[1]);
10090       else
10091 	{
10092 	  used_at = 1;
10093 	  macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10094 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10095 				   &offset_expr, AT, ZERO);
10096 	}
10097       break;
10098 
10099     case M_BGTUL_I:
10100       likely = 1;
10101     case M_BGTU_I:
10102       if (op[0] == 0
10103 	  || (GPR_SIZE == 32
10104 	      && imm_expr.X_add_number == -1))
10105 	goto do_false;
10106       ++imm_expr.X_add_number;
10107       /* FALLTHROUGH */
10108     case M_BGEU_I:
10109     case M_BGEUL_I:
10110       if (mask == M_BGEUL_I)
10111 	likely = 1;
10112       if (imm_expr.X_add_number == 0)
10113 	goto do_true;
10114       else if (imm_expr.X_add_number == 1)
10115 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10116 				 &offset_expr, op[0], ZERO);
10117       else
10118 	{
10119 	  used_at = 1;
10120 	  set_at (op[0], 1);
10121 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10122 				   &offset_expr, AT, ZERO);
10123 	}
10124       break;
10125 
10126     case M_BGTL:
10127       likely = 1;
10128     case M_BGT:
10129       if (op[1] == 0)
10130 	macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10131       else if (op[0] == 0)
10132 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
10133       else
10134 	{
10135 	  used_at = 1;
10136 	  macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10137 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10138 				   &offset_expr, AT, ZERO);
10139 	}
10140       break;
10141 
10142     case M_BGTUL:
10143       likely = 1;
10144     case M_BGTU:
10145       if (op[1] == 0)
10146 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10147 				 &offset_expr, op[0], ZERO);
10148       else if (op[0] == 0)
10149 	goto do_false;
10150       else
10151 	{
10152 	  used_at = 1;
10153 	  macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10154 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10155 				   &offset_expr, AT, ZERO);
10156 	}
10157       break;
10158 
10159     case M_BLEL:
10160       likely = 1;
10161     case M_BLE:
10162       if (op[1] == 0)
10163 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10164       else if (op[0] == 0)
10165 	macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
10166       else
10167 	{
10168 	  used_at = 1;
10169 	  macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10170 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10171 				   &offset_expr, AT, ZERO);
10172 	}
10173       break;
10174 
10175     case M_BLEL_I:
10176       likely = 1;
10177     case M_BLE_I:
10178       if (imm_expr.X_add_number >= GPR_SMAX)
10179 	goto do_true;
10180       ++imm_expr.X_add_number;
10181       /* FALLTHROUGH */
10182     case M_BLT_I:
10183     case M_BLTL_I:
10184       if (mask == M_BLTL_I)
10185 	likely = 1;
10186       if (imm_expr.X_add_number == 0)
10187 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10188       else if (imm_expr.X_add_number == 1)
10189 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10190       else
10191 	{
10192 	  used_at = 1;
10193 	  set_at (op[0], 0);
10194 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10195 				   &offset_expr, AT, ZERO);
10196 	}
10197       break;
10198 
10199     case M_BLEUL:
10200       likely = 1;
10201     case M_BLEU:
10202       if (op[1] == 0)
10203 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10204 				 &offset_expr, op[0], ZERO);
10205       else if (op[0] == 0)
10206 	goto do_true;
10207       else
10208 	{
10209 	  used_at = 1;
10210 	  macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10211 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10212 				   &offset_expr, AT, ZERO);
10213 	}
10214       break;
10215 
10216     case M_BLEUL_I:
10217       likely = 1;
10218     case M_BLEU_I:
10219       if (op[0] == 0
10220 	  || (GPR_SIZE == 32
10221 	      && imm_expr.X_add_number == -1))
10222 	goto do_true;
10223       ++imm_expr.X_add_number;
10224       /* FALLTHROUGH */
10225     case M_BLTU_I:
10226     case M_BLTUL_I:
10227       if (mask == M_BLTUL_I)
10228 	likely = 1;
10229       if (imm_expr.X_add_number == 0)
10230 	goto do_false;
10231       else if (imm_expr.X_add_number == 1)
10232 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10233 				 &offset_expr, op[0], ZERO);
10234       else
10235 	{
10236 	  used_at = 1;
10237 	  set_at (op[0], 1);
10238 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10239 				   &offset_expr, AT, ZERO);
10240 	}
10241       break;
10242 
10243     case M_BLTL:
10244       likely = 1;
10245     case M_BLT:
10246       if (op[1] == 0)
10247 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10248       else if (op[0] == 0)
10249 	macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
10250       else
10251 	{
10252 	  used_at = 1;
10253 	  macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10254 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10255 				   &offset_expr, AT, ZERO);
10256 	}
10257       break;
10258 
10259     case M_BLTUL:
10260       likely = 1;
10261     case M_BLTU:
10262       if (op[1] == 0)
10263 	goto do_false;
10264       else if (op[0] == 0)
10265 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10266 				 &offset_expr, ZERO, op[1]);
10267       else
10268 	{
10269 	  used_at = 1;
10270 	  macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10271 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10272 				   &offset_expr, AT, ZERO);
10273 	}
10274       break;
10275 
10276     case M_DDIV_3:
10277       dbl = 1;
10278     case M_DIV_3:
10279       s = "mflo";
10280       goto do_div3;
10281     case M_DREM_3:
10282       dbl = 1;
10283     case M_REM_3:
10284       s = "mfhi";
10285     do_div3:
10286       if (op[2] == 0)
10287 	{
10288 	  as_warn (_("divide by zero"));
10289 	  if (mips_trap)
10290 	    macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10291 	  else
10292 	    macro_build (NULL, "break", BRK_FMT, 7);
10293 	  break;
10294 	}
10295 
10296       start_noreorder ();
10297       if (mips_trap)
10298 	{
10299 	  macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10300 	  macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10301 	}
10302       else
10303 	{
10304 	  if (mips_opts.micromips)
10305 	    micromips_label_expr (&label_expr);
10306 	  else
10307 	    label_expr.X_add_number = 8;
10308 	  macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10309 	  macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10310 	  macro_build (NULL, "break", BRK_FMT, 7);
10311 	  if (mips_opts.micromips)
10312 	    micromips_add_label ();
10313 	}
10314       expr1.X_add_number = -1;
10315       used_at = 1;
10316       load_register (AT, &expr1, dbl);
10317       if (mips_opts.micromips)
10318 	micromips_label_expr (&label_expr);
10319       else
10320 	label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
10321       macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
10322       if (dbl)
10323 	{
10324 	  expr1.X_add_number = 1;
10325 	  load_register (AT, &expr1, dbl);
10326 	  macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
10327 	}
10328       else
10329 	{
10330 	  expr1.X_add_number = 0x80000000;
10331 	  macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
10332 	}
10333       if (mips_trap)
10334 	{
10335 	  macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
10336 	  /* We want to close the noreorder block as soon as possible, so
10337 	     that later insns are available for delay slot filling.  */
10338 	  end_noreorder ();
10339 	}
10340       else
10341 	{
10342 	  if (mips_opts.micromips)
10343 	    micromips_label_expr (&label_expr);
10344 	  else
10345 	    label_expr.X_add_number = 8;
10346 	  macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
10347 	  macro_build (NULL, "nop", "");
10348 
10349 	  /* We want to close the noreorder block as soon as possible, so
10350 	     that later insns are available for delay slot filling.  */
10351 	  end_noreorder ();
10352 
10353 	  macro_build (NULL, "break", BRK_FMT, 6);
10354 	}
10355       if (mips_opts.micromips)
10356 	micromips_add_label ();
10357       macro_build (NULL, s, MFHL_FMT, op[0]);
10358       break;
10359 
10360     case M_DIV_3I:
10361       s = "div";
10362       s2 = "mflo";
10363       goto do_divi;
10364     case M_DIVU_3I:
10365       s = "divu";
10366       s2 = "mflo";
10367       goto do_divi;
10368     case M_REM_3I:
10369       s = "div";
10370       s2 = "mfhi";
10371       goto do_divi;
10372     case M_REMU_3I:
10373       s = "divu";
10374       s2 = "mfhi";
10375       goto do_divi;
10376     case M_DDIV_3I:
10377       dbl = 1;
10378       s = "ddiv";
10379       s2 = "mflo";
10380       goto do_divi;
10381     case M_DDIVU_3I:
10382       dbl = 1;
10383       s = "ddivu";
10384       s2 = "mflo";
10385       goto do_divi;
10386     case M_DREM_3I:
10387       dbl = 1;
10388       s = "ddiv";
10389       s2 = "mfhi";
10390       goto do_divi;
10391     case M_DREMU_3I:
10392       dbl = 1;
10393       s = "ddivu";
10394       s2 = "mfhi";
10395     do_divi:
10396       if (imm_expr.X_add_number == 0)
10397 	{
10398 	  as_warn (_("divide by zero"));
10399 	  if (mips_trap)
10400 	    macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10401 	  else
10402 	    macro_build (NULL, "break", BRK_FMT, 7);
10403 	  break;
10404 	}
10405       if (imm_expr.X_add_number == 1)
10406 	{
10407 	  if (strcmp (s2, "mflo") == 0)
10408 	    move_register (op[0], op[1]);
10409 	  else
10410 	    move_register (op[0], ZERO);
10411 	  break;
10412 	}
10413       if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
10414 	{
10415 	  if (strcmp (s2, "mflo") == 0)
10416 	    macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
10417 	  else
10418 	    move_register (op[0], ZERO);
10419 	  break;
10420 	}
10421 
10422       used_at = 1;
10423       load_register (AT, &imm_expr, dbl);
10424       macro_build (NULL, s, "z,s,t", op[1], AT);
10425       macro_build (NULL, s2, MFHL_FMT, op[0]);
10426       break;
10427 
10428     case M_DIVU_3:
10429       s = "divu";
10430       s2 = "mflo";
10431       goto do_divu3;
10432     case M_REMU_3:
10433       s = "divu";
10434       s2 = "mfhi";
10435       goto do_divu3;
10436     case M_DDIVU_3:
10437       s = "ddivu";
10438       s2 = "mflo";
10439       goto do_divu3;
10440     case M_DREMU_3:
10441       s = "ddivu";
10442       s2 = "mfhi";
10443     do_divu3:
10444       start_noreorder ();
10445       if (mips_trap)
10446 	{
10447 	  macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10448 	  macro_build (NULL, s, "z,s,t", op[1], op[2]);
10449 	  /* We want to close the noreorder block as soon as possible, so
10450 	     that later insns are available for delay slot filling.  */
10451 	  end_noreorder ();
10452 	}
10453       else
10454 	{
10455 	  if (mips_opts.micromips)
10456 	    micromips_label_expr (&label_expr);
10457 	  else
10458 	    label_expr.X_add_number = 8;
10459 	  macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10460 	  macro_build (NULL, s, "z,s,t", op[1], op[2]);
10461 
10462 	  /* We want to close the noreorder block as soon as possible, so
10463 	     that later insns are available for delay slot filling.  */
10464 	  end_noreorder ();
10465 	  macro_build (NULL, "break", BRK_FMT, 7);
10466 	  if (mips_opts.micromips)
10467 	    micromips_add_label ();
10468 	}
10469       macro_build (NULL, s2, MFHL_FMT, op[0]);
10470       break;
10471 
10472     case M_DLCA_AB:
10473       dbl = 1;
10474     case M_LCA_AB:
10475       call = 1;
10476       goto do_la;
10477     case M_DLA_AB:
10478       dbl = 1;
10479     case M_LA_AB:
10480     do_la:
10481       /* Load the address of a symbol into a register.  If breg is not
10482 	 zero, we then add a base register to it.  */
10483 
10484       breg = op[2];
10485       if (dbl && GPR_SIZE == 32)
10486 	as_warn (_("dla used to load 32-bit register"));
10487 
10488       if (!dbl && HAVE_64BIT_OBJECTS)
10489 	as_warn (_("la used to load 64-bit address"));
10490 
10491       if (small_offset_p (0, align, 16))
10492 	{
10493 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
10494 		       -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10495 	  break;
10496 	}
10497 
10498       if (mips_opts.at && (op[0] == breg))
10499 	{
10500 	  tempreg = AT;
10501 	  used_at = 1;
10502 	}
10503       else
10504 	tempreg = op[0];
10505 
10506       if (offset_expr.X_op != O_symbol
10507 	  && offset_expr.X_op != O_constant)
10508 	{
10509 	  as_bad (_("expression too complex"));
10510 	  offset_expr.X_op = O_constant;
10511 	}
10512 
10513       if (offset_expr.X_op == O_constant)
10514 	load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
10515       else if (mips_pic == NO_PIC)
10516 	{
10517 	  /* If this is a reference to a GP relative symbol, we want
10518 	       addiu	$tempreg,$gp,<sym>	(BFD_RELOC_GPREL16)
10519 	     Otherwise we want
10520 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
10521 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
10522 	     If we have a constant, we need two instructions anyhow,
10523 	     so we may as well always use the latter form.
10524 
10525 	     With 64bit address space and a usable $at we want
10526 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
10527 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
10528 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
10529 	       daddiu	$at,<sym>		(BFD_RELOC_LO16)
10530 	       dsll32	$tempreg,0
10531 	       daddu	$tempreg,$tempreg,$at
10532 
10533 	     If $at is already in use, we use a path which is suboptimal
10534 	     on superscalar processors.
10535 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
10536 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
10537 	       dsll	$tempreg,16
10538 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
10539 	       dsll	$tempreg,16
10540 	       daddiu	$tempreg,<sym>		(BFD_RELOC_LO16)
10541 
10542 	     For GP relative symbols in 64bit address space we can use
10543 	     the same sequence as in 32bit address space.  */
10544 	  if (HAVE_64BIT_SYMBOLS)
10545 	    {
10546 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10547 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10548 		{
10549 		  relax_start (offset_expr.X_add_symbol);
10550 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10551 			       tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10552 		  relax_switch ();
10553 		}
10554 
10555 	      if (used_at == 0 && mips_opts.at)
10556 		{
10557 		  macro_build (&offset_expr, "lui", LUI_FMT,
10558 			       tempreg, BFD_RELOC_MIPS_HIGHEST);
10559 		  macro_build (&offset_expr, "lui", LUI_FMT,
10560 			       AT, BFD_RELOC_HI16_S);
10561 		  macro_build (&offset_expr, "daddiu", "t,r,j",
10562 			       tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10563 		  macro_build (&offset_expr, "daddiu", "t,r,j",
10564 			       AT, AT, BFD_RELOC_LO16);
10565 		  macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
10566 		  macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
10567 		  used_at = 1;
10568 		}
10569 	      else
10570 		{
10571 		  macro_build (&offset_expr, "lui", LUI_FMT,
10572 			       tempreg, BFD_RELOC_MIPS_HIGHEST);
10573 		  macro_build (&offset_expr, "daddiu", "t,r,j",
10574 			       tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10575 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10576 		  macro_build (&offset_expr, "daddiu", "t,r,j",
10577 			       tempreg, tempreg, BFD_RELOC_HI16_S);
10578 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10579 		  macro_build (&offset_expr, "daddiu", "t,r,j",
10580 			       tempreg, tempreg, BFD_RELOC_LO16);
10581 		}
10582 
10583 	      if (mips_relax.sequence)
10584 		relax_end ();
10585 	    }
10586 	  else
10587 	    {
10588 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10589 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10590 		{
10591 		  relax_start (offset_expr.X_add_symbol);
10592 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10593 			       tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10594 		  relax_switch ();
10595 		}
10596 	      if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
10597 		as_bad (_("offset too large"));
10598 	      macro_build_lui (&offset_expr, tempreg);
10599 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10600 			   tempreg, tempreg, BFD_RELOC_LO16);
10601 	      if (mips_relax.sequence)
10602 		relax_end ();
10603 	    }
10604 	}
10605       else if (!mips_big_got && !HAVE_NEWABI)
10606 	{
10607 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10608 
10609 	  /* If this is a reference to an external symbol, and there
10610 	     is no constant, we want
10611 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
10612 	     or for lca or if tempreg is PIC_CALL_REG
10613 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_CALL16)
10614 	     For a local symbol, we want
10615 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
10616 	       nop
10617 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
10618 
10619 	     If we have a small constant, and this is a reference to
10620 	     an external symbol, we want
10621 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
10622 	       nop
10623 	       addiu	$tempreg,$tempreg,<constant>
10624 	     For a local symbol, we want the same instruction
10625 	     sequence, but we output a BFD_RELOC_LO16 reloc on the
10626 	     addiu instruction.
10627 
10628 	     If we have a large constant, and this is a reference to
10629 	     an external symbol, we want
10630 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
10631 	       lui	$at,<hiconstant>
10632 	       addiu	$at,$at,<loconstant>
10633 	       addu	$tempreg,$tempreg,$at
10634 	     For a local symbol, we want the same instruction
10635 	     sequence, but we output a BFD_RELOC_LO16 reloc on the
10636 	     addiu instruction.
10637 	   */
10638 
10639 	  if (offset_expr.X_add_number == 0)
10640 	    {
10641 	      if (mips_pic == SVR4_PIC
10642 		  && breg == 0
10643 		  && (call || tempreg == PIC_CALL_REG))
10644 		lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10645 
10646 	      relax_start (offset_expr.X_add_symbol);
10647 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10648 			   lw_reloc_type, mips_gp_register);
10649 	      if (breg != 0)
10650 		{
10651 		  /* We're going to put in an addu instruction using
10652 		     tempreg, so we may as well insert the nop right
10653 		     now.  */
10654 		  load_delay_nop ();
10655 		}
10656 	      relax_switch ();
10657 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10658 			   tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
10659 	      load_delay_nop ();
10660 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10661 			   tempreg, tempreg, BFD_RELOC_LO16);
10662 	      relax_end ();
10663 	      /* FIXME: If breg == 0, and the next instruction uses
10664 		 $tempreg, then if this variant case is used an extra
10665 		 nop will be generated.  */
10666 	    }
10667 	  else if (offset_expr.X_add_number >= -0x8000
10668 		   && offset_expr.X_add_number < 0x8000)
10669 	    {
10670 	      load_got_offset (tempreg, &offset_expr);
10671 	      load_delay_nop ();
10672 	      add_got_offset (tempreg, &offset_expr);
10673 	    }
10674 	  else
10675 	    {
10676 	      expr1.X_add_number = offset_expr.X_add_number;
10677 	      offset_expr.X_add_number =
10678 		SEXT_16BIT (offset_expr.X_add_number);
10679 	      load_got_offset (tempreg, &offset_expr);
10680 	      offset_expr.X_add_number = expr1.X_add_number;
10681 	      /* If we are going to add in a base register, and the
10682 		 target register and the base register are the same,
10683 		 then we are using AT as a temporary register.  Since
10684 		 we want to load the constant into AT, we add our
10685 		 current AT (from the global offset table) and the
10686 		 register into the register now, and pretend we were
10687 		 not using a base register.  */
10688 	      if (breg == op[0])
10689 		{
10690 		  load_delay_nop ();
10691 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10692 			       op[0], AT, breg);
10693 		  breg = 0;
10694 		  tempreg = op[0];
10695 		}
10696 	      add_got_offset_hilo (tempreg, &offset_expr, AT);
10697 	      used_at = 1;
10698 	    }
10699 	}
10700       else if (!mips_big_got && HAVE_NEWABI)
10701 	{
10702 	  int add_breg_early = 0;
10703 
10704 	  /* If this is a reference to an external, and there is no
10705 	     constant, or local symbol (*), with or without a
10706 	     constant, we want
10707 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
10708 	     or for lca or if tempreg is PIC_CALL_REG
10709 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_CALL16)
10710 
10711 	     If we have a small constant, and this is a reference to
10712 	     an external symbol, we want
10713 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
10714 	       addiu	$tempreg,$tempreg,<constant>
10715 
10716 	     If we have a large constant, and this is a reference to
10717 	     an external symbol, we want
10718 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
10719 	       lui	$at,<hiconstant>
10720 	       addiu	$at,$at,<loconstant>
10721 	       addu	$tempreg,$tempreg,$at
10722 
10723 	     (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
10724 	     local symbols, even though it introduces an additional
10725 	     instruction.  */
10726 
10727 	  if (offset_expr.X_add_number)
10728 	    {
10729 	      expr1.X_add_number = offset_expr.X_add_number;
10730 	      offset_expr.X_add_number = 0;
10731 
10732 	      relax_start (offset_expr.X_add_symbol);
10733 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10734 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10735 
10736 	      if (expr1.X_add_number >= -0x8000
10737 		  && expr1.X_add_number < 0x8000)
10738 		{
10739 		  macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10740 			       tempreg, tempreg, BFD_RELOC_LO16);
10741 		}
10742 	      else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
10743 		{
10744 		  unsigned int dreg;
10745 
10746 		  /* If we are going to add in a base register, and the
10747 		     target register and the base register are the same,
10748 		     then we are using AT as a temporary register.  Since
10749 		     we want to load the constant into AT, we add our
10750 		     current AT (from the global offset table) and the
10751 		     register into the register now, and pretend we were
10752 		     not using a base register.  */
10753 		  if (breg != op[0])
10754 		    dreg = tempreg;
10755 		  else
10756 		    {
10757 		      gas_assert (tempreg == AT);
10758 		      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10759 				   op[0], AT, breg);
10760 		      dreg = op[0];
10761 		      add_breg_early = 1;
10762 		    }
10763 
10764 		  load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
10765 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10766 			       dreg, dreg, AT);
10767 
10768 		  used_at = 1;
10769 		}
10770 	      else
10771 		as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10772 
10773 	      relax_switch ();
10774 	      offset_expr.X_add_number = expr1.X_add_number;
10775 
10776 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10777 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10778 	      if (add_breg_early)
10779 		{
10780 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10781 			       op[0], tempreg, breg);
10782 		  breg = 0;
10783 		  tempreg = op[0];
10784 		}
10785 	      relax_end ();
10786 	    }
10787 	  else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
10788 	    {
10789 	      relax_start (offset_expr.X_add_symbol);
10790 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10791 			   BFD_RELOC_MIPS_CALL16, mips_gp_register);
10792 	      relax_switch ();
10793 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10794 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10795 	      relax_end ();
10796 	    }
10797 	  else
10798 	    {
10799 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10800 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10801 	    }
10802 	}
10803       else if (mips_big_got && !HAVE_NEWABI)
10804 	{
10805 	  int gpdelay;
10806 	  int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10807 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
10808 	  int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10809 
10810 	  /* This is the large GOT case.  If this is a reference to an
10811 	     external symbol, and there is no constant, we want
10812 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
10813 	       addu	$tempreg,$tempreg,$gp
10814 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10815 	     or for lca or if tempreg is PIC_CALL_REG
10816 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
10817 	       addu	$tempreg,$tempreg,$gp
10818 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
10819 	     For a local symbol, we want
10820 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
10821 	       nop
10822 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
10823 
10824 	     If we have a small constant, and this is a reference to
10825 	     an external symbol, we want
10826 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
10827 	       addu	$tempreg,$tempreg,$gp
10828 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10829 	       nop
10830 	       addiu	$tempreg,$tempreg,<constant>
10831 	     For a local symbol, we want
10832 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
10833 	       nop
10834 	       addiu	$tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
10835 
10836 	     If we have a large constant, and this is a reference to
10837 	     an external symbol, we want
10838 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
10839 	       addu	$tempreg,$tempreg,$gp
10840 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10841 	       lui	$at,<hiconstant>
10842 	       addiu	$at,$at,<loconstant>
10843 	       addu	$tempreg,$tempreg,$at
10844 	     For a local symbol, we want
10845 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
10846 	       lui	$at,<hiconstant>
10847 	       addiu	$at,$at,<loconstant>	(BFD_RELOC_LO16)
10848 	       addu	$tempreg,$tempreg,$at
10849 	  */
10850 
10851 	  expr1.X_add_number = offset_expr.X_add_number;
10852 	  offset_expr.X_add_number = 0;
10853 	  relax_start (offset_expr.X_add_symbol);
10854 	  gpdelay = reg_needs_delay (mips_gp_register);
10855 	  if (expr1.X_add_number == 0 && breg == 0
10856 	      && (call || tempreg == PIC_CALL_REG))
10857 	    {
10858 	      lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10859 	      lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10860 	    }
10861 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
10862 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10863 		       tempreg, tempreg, mips_gp_register);
10864 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10865 		       tempreg, lw_reloc_type, tempreg);
10866 	  if (expr1.X_add_number == 0)
10867 	    {
10868 	      if (breg != 0)
10869 		{
10870 		  /* We're going to put in an addu instruction using
10871 		     tempreg, so we may as well insert the nop right
10872 		     now.  */
10873 		  load_delay_nop ();
10874 		}
10875 	    }
10876 	  else if (expr1.X_add_number >= -0x8000
10877 		   && expr1.X_add_number < 0x8000)
10878 	    {
10879 	      load_delay_nop ();
10880 	      macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10881 			   tempreg, tempreg, BFD_RELOC_LO16);
10882 	    }
10883 	  else
10884 	    {
10885 	      unsigned int dreg;
10886 
10887 	      /* If we are going to add in a base register, and the
10888 		 target register and the base register are the same,
10889 		 then we are using AT as a temporary register.  Since
10890 		 we want to load the constant into AT, we add our
10891 		 current AT (from the global offset table) and the
10892 		 register into the register now, and pretend we were
10893 		 not using a base register.  */
10894 	      if (breg != op[0])
10895 		dreg = tempreg;
10896 	      else
10897 		{
10898 		  gas_assert (tempreg == AT);
10899 		  load_delay_nop ();
10900 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10901 			       op[0], AT, breg);
10902 		  dreg = op[0];
10903 		}
10904 
10905 	      load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
10906 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
10907 
10908 	      used_at = 1;
10909 	    }
10910 	  offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
10911 	  relax_switch ();
10912 
10913 	  if (gpdelay)
10914 	    {
10915 	      /* This is needed because this instruction uses $gp, but
10916 		 the first instruction on the main stream does not.  */
10917 	      macro_build (NULL, "nop", "");
10918 	    }
10919 
10920 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10921 		       local_reloc_type, mips_gp_register);
10922 	  if (expr1.X_add_number >= -0x8000
10923 	      && expr1.X_add_number < 0x8000)
10924 	    {
10925 	      load_delay_nop ();
10926 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10927 			   tempreg, tempreg, BFD_RELOC_LO16);
10928 	      /* FIXME: If add_number is 0, and there was no base
10929 		 register, the external symbol case ended with a load,
10930 		 so if the symbol turns out to not be external, and
10931 		 the next instruction uses tempreg, an unnecessary nop
10932 		 will be inserted.  */
10933 	    }
10934 	  else
10935 	    {
10936 	      if (breg == op[0])
10937 		{
10938 		  /* We must add in the base register now, as in the
10939 		     external symbol case.  */
10940 		  gas_assert (tempreg == AT);
10941 		  load_delay_nop ();
10942 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10943 			       op[0], AT, breg);
10944 		  tempreg = op[0];
10945 		  /* We set breg to 0 because we have arranged to add
10946 		     it in in both cases.  */
10947 		  breg = 0;
10948 		}
10949 
10950 	      macro_build_lui (&expr1, AT);
10951 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10952 			   AT, AT, BFD_RELOC_LO16);
10953 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10954 			   tempreg, tempreg, AT);
10955 	      used_at = 1;
10956 	    }
10957 	  relax_end ();
10958 	}
10959       else if (mips_big_got && HAVE_NEWABI)
10960 	{
10961 	  int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10962 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
10963 	  int add_breg_early = 0;
10964 
10965 	  /* This is the large GOT case.  If this is a reference to an
10966 	     external symbol, and there is no constant, we want
10967 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
10968 	       add	$tempreg,$tempreg,$gp
10969 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10970 	     or for lca or if tempreg is PIC_CALL_REG
10971 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
10972 	       add	$tempreg,$tempreg,$gp
10973 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
10974 
10975 	     If we have a small constant, and this is a reference to
10976 	     an external symbol, we want
10977 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
10978 	       add	$tempreg,$tempreg,$gp
10979 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10980 	       addi	$tempreg,$tempreg,<constant>
10981 
10982 	     If we have a large constant, and this is a reference to
10983 	     an external symbol, we want
10984 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
10985 	       addu	$tempreg,$tempreg,$gp
10986 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10987 	       lui	$at,<hiconstant>
10988 	       addi	$at,$at,<loconstant>
10989 	       add	$tempreg,$tempreg,$at
10990 
10991 	     If we have NewABI, and we know it's a local symbol, we want
10992 	       lw	$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT_PAGE)
10993 	       addiu	$reg,$reg,<sym>		(BFD_RELOC_MIPS_GOT_OFST)
10994 	     otherwise we have to resort to GOT_HI16/GOT_LO16.  */
10995 
10996 	  relax_start (offset_expr.X_add_symbol);
10997 
10998 	  expr1.X_add_number = offset_expr.X_add_number;
10999 	  offset_expr.X_add_number = 0;
11000 
11001 	  if (expr1.X_add_number == 0 && breg == 0
11002 	      && (call || tempreg == PIC_CALL_REG))
11003 	    {
11004 	      lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11005 	      lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11006 	    }
11007 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11008 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11009 		       tempreg, tempreg, mips_gp_register);
11010 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11011 		       tempreg, lw_reloc_type, tempreg);
11012 
11013 	  if (expr1.X_add_number == 0)
11014 	    ;
11015 	  else if (expr1.X_add_number >= -0x8000
11016 		   && expr1.X_add_number < 0x8000)
11017 	    {
11018 	      macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11019 			   tempreg, tempreg, BFD_RELOC_LO16);
11020 	    }
11021 	  else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11022 	    {
11023 	      unsigned int dreg;
11024 
11025 	      /* If we are going to add in a base register, and the
11026 		 target register and the base register are the same,
11027 		 then we are using AT as a temporary register.  Since
11028 		 we want to load the constant into AT, we add our
11029 		 current AT (from the global offset table) and the
11030 		 register into the register now, and pretend we were
11031 		 not using a base register.  */
11032 	      if (breg != op[0])
11033 		dreg = tempreg;
11034 	      else
11035 		{
11036 		  gas_assert (tempreg == AT);
11037 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11038 			       op[0], AT, breg);
11039 		  dreg = op[0];
11040 		  add_breg_early = 1;
11041 		}
11042 
11043 	      load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11044 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11045 
11046 	      used_at = 1;
11047 	    }
11048 	  else
11049 	    as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11050 
11051 	  relax_switch ();
11052 	  offset_expr.X_add_number = expr1.X_add_number;
11053 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11054 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11055 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11056 		       tempreg, BFD_RELOC_MIPS_GOT_OFST);
11057 	  if (add_breg_early)
11058 	    {
11059 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11060 			   op[0], tempreg, breg);
11061 	      breg = 0;
11062 	      tempreg = op[0];
11063 	    }
11064 	  relax_end ();
11065 	}
11066       else
11067 	abort ();
11068 
11069       if (breg != 0)
11070 	macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
11071       break;
11072 
11073     case M_MSGSND:
11074       gas_assert (!mips_opts.micromips);
11075       macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
11076       break;
11077 
11078     case M_MSGLD:
11079       gas_assert (!mips_opts.micromips);
11080       macro_build (NULL, "c2", "C", 0x02);
11081       break;
11082 
11083     case M_MSGLD_T:
11084       gas_assert (!mips_opts.micromips);
11085       macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
11086       break;
11087 
11088     case M_MSGWAIT:
11089       gas_assert (!mips_opts.micromips);
11090       macro_build (NULL, "c2", "C", 3);
11091       break;
11092 
11093     case M_MSGWAIT_T:
11094       gas_assert (!mips_opts.micromips);
11095       macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
11096       break;
11097 
11098     case M_J_A:
11099       /* The j instruction may not be used in PIC code, since it
11100 	 requires an absolute address.  We convert it to a b
11101 	 instruction.  */
11102       if (mips_pic == NO_PIC)
11103 	macro_build (&offset_expr, "j", "a");
11104       else
11105 	macro_build (&offset_expr, "b", "p");
11106       break;
11107 
11108       /* The jal instructions must be handled as macros because when
11109 	 generating PIC code they expand to multi-instruction
11110 	 sequences.  Normally they are simple instructions.  */
11111     case M_JALS_1:
11112       op[1] = op[0];
11113       op[0] = RA;
11114       /* Fall through.  */
11115     case M_JALS_2:
11116       gas_assert (mips_opts.micromips);
11117       if (mips_opts.insn32)
11118 	{
11119 	  as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11120 	  break;
11121 	}
11122       jals = 1;
11123       goto jal;
11124     case M_JAL_1:
11125       op[1] = op[0];
11126       op[0] = RA;
11127       /* Fall through.  */
11128     case M_JAL_2:
11129     jal:
11130       if (mips_pic == NO_PIC)
11131 	{
11132 	  s = jals ? "jalrs" : "jalr";
11133 	  if (mips_opts.micromips
11134 	      && !mips_opts.insn32
11135 	      && op[0] == RA
11136 	      && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11137 	    macro_build (NULL, s, "mj", op[1]);
11138 	  else
11139 	    macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11140 	}
11141       else
11142 	{
11143 	  int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11144 			   && mips_cprestore_offset >= 0);
11145 
11146 	  if (op[1] != PIC_CALL_REG)
11147 	    as_warn (_("MIPS PIC call to register other than $25"));
11148 
11149 	  s = ((mips_opts.micromips
11150 		&& !mips_opts.insn32
11151 		&& (!mips_opts.noreorder || cprestore))
11152 	       ? "jalrs" : "jalr");
11153 	  if (mips_opts.micromips
11154 	      && !mips_opts.insn32
11155 	      && op[0] == RA
11156 	      && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11157 	    macro_build (NULL, s, "mj", op[1]);
11158 	  else
11159 	    macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11160 	  if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
11161 	    {
11162 	      if (mips_cprestore_offset < 0)
11163 		as_warn (_("no .cprestore pseudo-op used in PIC code"));
11164 	      else
11165 		{
11166 		  if (!mips_frame_reg_valid)
11167 		    {
11168 		      as_warn (_("no .frame pseudo-op used in PIC code"));
11169 		      /* Quiet this warning.  */
11170 		      mips_frame_reg_valid = 1;
11171 		    }
11172 		  if (!mips_cprestore_valid)
11173 		    {
11174 		      as_warn (_("no .cprestore pseudo-op used in PIC code"));
11175 		      /* Quiet this warning.  */
11176 		      mips_cprestore_valid = 1;
11177 		    }
11178 		  if (mips_opts.noreorder)
11179 		    macro_build (NULL, "nop", "");
11180 		  expr1.X_add_number = mips_cprestore_offset;
11181   		  macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11182 						mips_gp_register,
11183 						mips_frame_reg,
11184 						HAVE_64BIT_ADDRESSES);
11185 		}
11186 	    }
11187 	}
11188 
11189       break;
11190 
11191     case M_JALS_A:
11192       gas_assert (mips_opts.micromips);
11193       if (mips_opts.insn32)
11194 	{
11195 	  as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11196 	  break;
11197 	}
11198       jals = 1;
11199       /* Fall through.  */
11200     case M_JAL_A:
11201       if (mips_pic == NO_PIC)
11202 	macro_build (&offset_expr, jals ? "jals" : "jal", "a");
11203       else if (mips_pic == SVR4_PIC)
11204 	{
11205 	  /* If this is a reference to an external symbol, and we are
11206 	     using a small GOT, we want
11207 	       lw	$25,<sym>($gp)		(BFD_RELOC_MIPS_CALL16)
11208 	       nop
11209 	       jalr	$ra,$25
11210 	       nop
11211 	       lw	$gp,cprestore($sp)
11212 	     The cprestore value is set using the .cprestore
11213 	     pseudo-op.  If we are using a big GOT, we want
11214 	       lui	$25,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
11215 	       addu	$25,$25,$gp
11216 	       lw	$25,<sym>($25)		(BFD_RELOC_MIPS_CALL_LO16)
11217 	       nop
11218 	       jalr	$ra,$25
11219 	       nop
11220 	       lw	$gp,cprestore($sp)
11221 	     If the symbol is not external, we want
11222 	       lw	$25,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
11223 	       nop
11224 	       addiu	$25,$25,<sym>		(BFD_RELOC_LO16)
11225 	       jalr	$ra,$25
11226 	       nop
11227 	       lw $gp,cprestore($sp)
11228 
11229 	     For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11230 	     sequences above, minus nops, unless the symbol is local,
11231 	     which enables us to use GOT_PAGE/GOT_OFST (big got) or
11232 	     GOT_DISP.  */
11233 	  if (HAVE_NEWABI)
11234 	    {
11235 	      if (!mips_big_got)
11236 		{
11237 		  relax_start (offset_expr.X_add_symbol);
11238 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11239 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11240 			       mips_gp_register);
11241 		  relax_switch ();
11242 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11243 			       PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
11244 			       mips_gp_register);
11245 		  relax_end ();
11246 		}
11247 	      else
11248 		{
11249 		  relax_start (offset_expr.X_add_symbol);
11250 		  macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11251 			       BFD_RELOC_MIPS_CALL_HI16);
11252 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11253 			       PIC_CALL_REG, mips_gp_register);
11254 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11255 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11256 			       PIC_CALL_REG);
11257 		  relax_switch ();
11258 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11259 			       PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11260 			       mips_gp_register);
11261 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11262 			       PIC_CALL_REG, PIC_CALL_REG,
11263 			       BFD_RELOC_MIPS_GOT_OFST);
11264 		  relax_end ();
11265 		}
11266 
11267 	      macro_build_jalr (&offset_expr, 0);
11268 	    }
11269 	  else
11270 	    {
11271 	      relax_start (offset_expr.X_add_symbol);
11272 	      if (!mips_big_got)
11273 		{
11274 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11275 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11276 			       mips_gp_register);
11277 		  load_delay_nop ();
11278 		  relax_switch ();
11279 		}
11280 	      else
11281 		{
11282 		  int gpdelay;
11283 
11284 		  gpdelay = reg_needs_delay (mips_gp_register);
11285 		  macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11286 			       BFD_RELOC_MIPS_CALL_HI16);
11287 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11288 			       PIC_CALL_REG, mips_gp_register);
11289 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11290 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11291 			       PIC_CALL_REG);
11292 		  load_delay_nop ();
11293 		  relax_switch ();
11294 		  if (gpdelay)
11295 		    macro_build (NULL, "nop", "");
11296 		}
11297 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11298 			   PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
11299 			   mips_gp_register);
11300 	      load_delay_nop ();
11301 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11302 			   PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
11303 	      relax_end ();
11304 	      macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
11305 
11306 	      if (mips_cprestore_offset < 0)
11307 		as_warn (_("no .cprestore pseudo-op used in PIC code"));
11308 	      else
11309 		{
11310 		  if (!mips_frame_reg_valid)
11311 		    {
11312 		      as_warn (_("no .frame pseudo-op used in PIC code"));
11313 		      /* Quiet this warning.  */
11314 		      mips_frame_reg_valid = 1;
11315 		    }
11316 		  if (!mips_cprestore_valid)
11317 		    {
11318 		      as_warn (_("no .cprestore pseudo-op used in PIC code"));
11319 		      /* Quiet this warning.  */
11320 		      mips_cprestore_valid = 1;
11321 		    }
11322 		  if (mips_opts.noreorder)
11323 		    macro_build (NULL, "nop", "");
11324 		  expr1.X_add_number = mips_cprestore_offset;
11325   		  macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11326 						mips_gp_register,
11327 						mips_frame_reg,
11328 						HAVE_64BIT_ADDRESSES);
11329 		}
11330 	    }
11331 	}
11332       else if (mips_pic == VXWORKS_PIC)
11333 	as_bad (_("non-PIC jump used in PIC library"));
11334       else
11335 	abort ();
11336 
11337       break;
11338 
11339     case M_LBUE_AB:
11340       s = "lbue";
11341       fmt = "t,+j(b)";
11342       offbits = 9;
11343       goto ld_st;
11344     case M_LHUE_AB:
11345       s = "lhue";
11346       fmt = "t,+j(b)";
11347       offbits = 9;
11348       goto ld_st;
11349     case M_LBE_AB:
11350       s = "lbe";
11351       fmt = "t,+j(b)";
11352       offbits = 9;
11353       goto ld_st;
11354     case M_LHE_AB:
11355       s = "lhe";
11356       fmt = "t,+j(b)";
11357       offbits = 9;
11358       goto ld_st;
11359     case M_LLE_AB:
11360       s = "lle";
11361       fmt = "t,+j(b)";
11362       offbits = 9;
11363       goto ld_st;
11364     case M_LWE_AB:
11365       s = "lwe";
11366       fmt = "t,+j(b)";
11367       offbits = 9;
11368       goto ld_st;
11369     case M_LWLE_AB:
11370       s = "lwle";
11371       fmt = "t,+j(b)";
11372       offbits = 9;
11373       goto ld_st;
11374     case M_LWRE_AB:
11375       s = "lwre";
11376       fmt = "t,+j(b)";
11377       offbits = 9;
11378       goto ld_st;
11379     case M_SBE_AB:
11380       s = "sbe";
11381       fmt = "t,+j(b)";
11382       offbits = 9;
11383       goto ld_st;
11384     case M_SCE_AB:
11385       s = "sce";
11386       fmt = "t,+j(b)";
11387       offbits = 9;
11388       goto ld_st;
11389     case M_SHE_AB:
11390       s = "she";
11391       fmt = "t,+j(b)";
11392       offbits = 9;
11393       goto ld_st;
11394     case M_SWE_AB:
11395       s = "swe";
11396       fmt = "t,+j(b)";
11397       offbits = 9;
11398       goto ld_st;
11399     case M_SWLE_AB:
11400       s = "swle";
11401       fmt = "t,+j(b)";
11402       offbits = 9;
11403       goto ld_st;
11404     case M_SWRE_AB:
11405       s = "swre";
11406       fmt = "t,+j(b)";
11407       offbits = 9;
11408       goto ld_st;
11409     case M_ACLR_AB:
11410       s = "aclr";
11411       fmt = "\\,~(b)";
11412       offbits = 12;
11413       goto ld_st;
11414     case M_ASET_AB:
11415       s = "aset";
11416       fmt = "\\,~(b)";
11417       offbits = 12;
11418       goto ld_st;
11419     case M_LB_AB:
11420       s = "lb";
11421       fmt = "t,o(b)";
11422       goto ld;
11423     case M_LBU_AB:
11424       s = "lbu";
11425       fmt = "t,o(b)";
11426       goto ld;
11427     case M_LH_AB:
11428       s = "lh";
11429       fmt = "t,o(b)";
11430       goto ld;
11431     case M_LHU_AB:
11432       s = "lhu";
11433       fmt = "t,o(b)";
11434       goto ld;
11435     case M_LW_AB:
11436       s = "lw";
11437       fmt = "t,o(b)";
11438       goto ld;
11439     case M_LWC0_AB:
11440       gas_assert (!mips_opts.micromips);
11441       s = "lwc0";
11442       fmt = "E,o(b)";
11443       /* Itbl support may require additional care here.  */
11444       coproc = 1;
11445       goto ld_st;
11446     case M_LWC1_AB:
11447       s = "lwc1";
11448       fmt = "T,o(b)";
11449       /* Itbl support may require additional care here.  */
11450       coproc = 1;
11451       goto ld_st;
11452     case M_LWC2_AB:
11453       s = "lwc2";
11454       fmt = COP12_FMT;
11455       offbits = (mips_opts.micromips ? 12
11456 		 : ISA_IS_R6 (mips_opts.isa) ? 11
11457 		 : 16);
11458       /* Itbl support may require additional care here.  */
11459       coproc = 1;
11460       goto ld_st;
11461     case M_LWC3_AB:
11462       gas_assert (!mips_opts.micromips);
11463       s = "lwc3";
11464       fmt = "E,o(b)";
11465       /* Itbl support may require additional care here.  */
11466       coproc = 1;
11467       goto ld_st;
11468     case M_LWL_AB:
11469       s = "lwl";
11470       fmt = MEM12_FMT;
11471       offbits = (mips_opts.micromips ? 12 : 16);
11472       goto ld_st;
11473     case M_LWR_AB:
11474       s = "lwr";
11475       fmt = MEM12_FMT;
11476       offbits = (mips_opts.micromips ? 12 : 16);
11477       goto ld_st;
11478     case M_LDC1_AB:
11479       s = "ldc1";
11480       fmt = "T,o(b)";
11481       /* Itbl support may require additional care here.  */
11482       coproc = 1;
11483       goto ld_st;
11484     case M_LDC2_AB:
11485       s = "ldc2";
11486       fmt = COP12_FMT;
11487       offbits = (mips_opts.micromips ? 12
11488 		 : ISA_IS_R6 (mips_opts.isa) ? 11
11489 		 : 16);
11490       /* Itbl support may require additional care here.  */
11491       coproc = 1;
11492       goto ld_st;
11493     case M_LQC2_AB:
11494       s = "lqc2";
11495       fmt = "+7,o(b)";
11496       /* Itbl support may require additional care here.  */
11497       coproc = 1;
11498       goto ld_st;
11499     case M_LDC3_AB:
11500       s = "ldc3";
11501       fmt = "E,o(b)";
11502       /* Itbl support may require additional care here.  */
11503       coproc = 1;
11504       goto ld_st;
11505     case M_LDL_AB:
11506       s = "ldl";
11507       fmt = MEM12_FMT;
11508       offbits = (mips_opts.micromips ? 12 : 16);
11509       goto ld_st;
11510     case M_LDR_AB:
11511       s = "ldr";
11512       fmt = MEM12_FMT;
11513       offbits = (mips_opts.micromips ? 12 : 16);
11514       goto ld_st;
11515     case M_LL_AB:
11516       s = "ll";
11517       fmt = LL_SC_FMT;
11518       offbits = (mips_opts.micromips ? 12
11519 		 : ISA_IS_R6 (mips_opts.isa) ? 9
11520 		 : 16);
11521       goto ld;
11522     case M_LLD_AB:
11523       s = "lld";
11524       fmt = LL_SC_FMT;
11525       offbits = (mips_opts.micromips ? 12
11526 		 : ISA_IS_R6 (mips_opts.isa) ? 9
11527 		 : 16);
11528       goto ld;
11529     case M_LWU_AB:
11530       s = "lwu";
11531       fmt = MEM12_FMT;
11532       offbits = (mips_opts.micromips ? 12 : 16);
11533       goto ld;
11534     case M_LWP_AB:
11535       gas_assert (mips_opts.micromips);
11536       s = "lwp";
11537       fmt = "t,~(b)";
11538       offbits = 12;
11539       lp = 1;
11540       goto ld;
11541     case M_LDP_AB:
11542       gas_assert (mips_opts.micromips);
11543       s = "ldp";
11544       fmt = "t,~(b)";
11545       offbits = 12;
11546       lp = 1;
11547       goto ld;
11548     case M_LWM_AB:
11549       gas_assert (mips_opts.micromips);
11550       s = "lwm";
11551       fmt = "n,~(b)";
11552       offbits = 12;
11553       goto ld_st;
11554     case M_LDM_AB:
11555       gas_assert (mips_opts.micromips);
11556       s = "ldm";
11557       fmt = "n,~(b)";
11558       offbits = 12;
11559       goto ld_st;
11560 
11561     ld:
11562       /* We don't want to use $0 as tempreg.  */
11563       if (op[2] == op[0] + lp || op[0] + lp == ZERO)
11564 	goto ld_st;
11565       else
11566 	tempreg = op[0] + lp;
11567       goto ld_noat;
11568 
11569     case M_SB_AB:
11570       s = "sb";
11571       fmt = "t,o(b)";
11572       goto ld_st;
11573     case M_SH_AB:
11574       s = "sh";
11575       fmt = "t,o(b)";
11576       goto ld_st;
11577     case M_SW_AB:
11578       s = "sw";
11579       fmt = "t,o(b)";
11580       goto ld_st;
11581     case M_SWC0_AB:
11582       gas_assert (!mips_opts.micromips);
11583       s = "swc0";
11584       fmt = "E,o(b)";
11585       /* Itbl support may require additional care here.  */
11586       coproc = 1;
11587       goto ld_st;
11588     case M_SWC1_AB:
11589       s = "swc1";
11590       fmt = "T,o(b)";
11591       /* Itbl support may require additional care here.  */
11592       coproc = 1;
11593       goto ld_st;
11594     case M_SWC2_AB:
11595       s = "swc2";
11596       fmt = COP12_FMT;
11597       offbits = (mips_opts.micromips ? 12
11598 		 : ISA_IS_R6 (mips_opts.isa) ? 11
11599 		 : 16);
11600       /* Itbl support may require additional care here.  */
11601       coproc = 1;
11602       goto ld_st;
11603     case M_SWC3_AB:
11604       gas_assert (!mips_opts.micromips);
11605       s = "swc3";
11606       fmt = "E,o(b)";
11607       /* Itbl support may require additional care here.  */
11608       coproc = 1;
11609       goto ld_st;
11610     case M_SWL_AB:
11611       s = "swl";
11612       fmt = MEM12_FMT;
11613       offbits = (mips_opts.micromips ? 12 : 16);
11614       goto ld_st;
11615     case M_SWR_AB:
11616       s = "swr";
11617       fmt = MEM12_FMT;
11618       offbits = (mips_opts.micromips ? 12 : 16);
11619       goto ld_st;
11620     case M_SC_AB:
11621       s = "sc";
11622       fmt = LL_SC_FMT;
11623       offbits = (mips_opts.micromips ? 12
11624 		 : ISA_IS_R6 (mips_opts.isa) ? 9
11625 		 : 16);
11626       goto ld_st;
11627     case M_SCD_AB:
11628       s = "scd";
11629       fmt = LL_SC_FMT;
11630       offbits = (mips_opts.micromips ? 12
11631 		 : ISA_IS_R6 (mips_opts.isa) ? 9
11632 		 : 16);
11633       goto ld_st;
11634     case M_CACHE_AB:
11635       s = "cache";
11636       fmt = (mips_opts.micromips ? "k,~(b)"
11637 	     : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11638 	     : "k,o(b)");
11639       offbits = (mips_opts.micromips ? 12
11640 		 : ISA_IS_R6 (mips_opts.isa) ? 9
11641 		 : 16);
11642       goto ld_st;
11643     case M_CACHEE_AB:
11644       s = "cachee";
11645       fmt = "k,+j(b)";
11646       offbits = 9;
11647       goto ld_st;
11648     case M_PREF_AB:
11649       s = "pref";
11650       fmt = (mips_opts.micromips ? "k,~(b)"
11651 	     : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11652 	     : "k,o(b)");
11653       offbits = (mips_opts.micromips ? 12
11654 		 : ISA_IS_R6 (mips_opts.isa) ? 9
11655 		 : 16);
11656       goto ld_st;
11657     case M_PREFE_AB:
11658       s = "prefe";
11659       fmt = "k,+j(b)";
11660       offbits = 9;
11661       goto ld_st;
11662     case M_SDC1_AB:
11663       s = "sdc1";
11664       fmt = "T,o(b)";
11665       coproc = 1;
11666       /* Itbl support may require additional care here.  */
11667       goto ld_st;
11668     case M_SDC2_AB:
11669       s = "sdc2";
11670       fmt = COP12_FMT;
11671       offbits = (mips_opts.micromips ? 12
11672 		 : ISA_IS_R6 (mips_opts.isa) ? 11
11673 		 : 16);
11674       /* Itbl support may require additional care here.  */
11675       coproc = 1;
11676       goto ld_st;
11677     case M_SQC2_AB:
11678       s = "sqc2";
11679       fmt = "+7,o(b)";
11680       /* Itbl support may require additional care here.  */
11681       coproc = 1;
11682       goto ld_st;
11683     case M_SDC3_AB:
11684       gas_assert (!mips_opts.micromips);
11685       s = "sdc3";
11686       fmt = "E,o(b)";
11687       /* Itbl support may require additional care here.  */
11688       coproc = 1;
11689       goto ld_st;
11690     case M_SDL_AB:
11691       s = "sdl";
11692       fmt = MEM12_FMT;
11693       offbits = (mips_opts.micromips ? 12 : 16);
11694       goto ld_st;
11695     case M_SDR_AB:
11696       s = "sdr";
11697       fmt = MEM12_FMT;
11698       offbits = (mips_opts.micromips ? 12 : 16);
11699       goto ld_st;
11700     case M_SWP_AB:
11701       gas_assert (mips_opts.micromips);
11702       s = "swp";
11703       fmt = "t,~(b)";
11704       offbits = 12;
11705       goto ld_st;
11706     case M_SDP_AB:
11707       gas_assert (mips_opts.micromips);
11708       s = "sdp";
11709       fmt = "t,~(b)";
11710       offbits = 12;
11711       goto ld_st;
11712     case M_SWM_AB:
11713       gas_assert (mips_opts.micromips);
11714       s = "swm";
11715       fmt = "n,~(b)";
11716       offbits = 12;
11717       goto ld_st;
11718     case M_SDM_AB:
11719       gas_assert (mips_opts.micromips);
11720       s = "sdm";
11721       fmt = "n,~(b)";
11722       offbits = 12;
11723 
11724     ld_st:
11725       tempreg = AT;
11726     ld_noat:
11727       breg = op[2];
11728       if (small_offset_p (0, align, 16))
11729 	{
11730 	  /* The first case exists for M_LD_AB and M_SD_AB, which are
11731 	     macros for o32 but which should act like normal instructions
11732 	     otherwise.  */
11733 	  if (offbits == 16)
11734 	    macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
11735 			 offset_reloc[1], offset_reloc[2], breg);
11736 	  else if (small_offset_p (0, align, offbits))
11737 	    {
11738 	      if (offbits == 0)
11739 		macro_build (NULL, s, fmt, op[0], breg);
11740 	      else
11741 		macro_build (NULL, s, fmt, op[0],
11742 			     (int) offset_expr.X_add_number, breg);
11743 	    }
11744 	  else
11745 	    {
11746 	      if (tempreg == AT)
11747 		used_at = 1;
11748 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11749 			   tempreg, breg, -1, offset_reloc[0],
11750 			   offset_reloc[1], offset_reloc[2]);
11751 	      if (offbits == 0)
11752 		macro_build (NULL, s, fmt, op[0], tempreg);
11753 	      else
11754 		macro_build (NULL, s, fmt, op[0], 0, tempreg);
11755 	    }
11756 	  break;
11757 	}
11758 
11759       if (tempreg == AT)
11760 	used_at = 1;
11761 
11762       if (offset_expr.X_op != O_constant
11763 	  && offset_expr.X_op != O_symbol)
11764 	{
11765 	  as_bad (_("expression too complex"));
11766 	  offset_expr.X_op = O_constant;
11767 	}
11768 
11769       if (HAVE_32BIT_ADDRESSES
11770 	  && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
11771 	{
11772 	  char value [32];
11773 
11774 	  sprintf_vma (value, offset_expr.X_add_number);
11775 	  as_bad (_("number (0x%s) larger than 32 bits"), value);
11776 	}
11777 
11778       /* A constant expression in PIC code can be handled just as it
11779 	 is in non PIC code.  */
11780       if (offset_expr.X_op == O_constant)
11781 	{
11782 	  expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
11783 						 offbits == 0 ? 16 : offbits);
11784 	  offset_expr.X_add_number -= expr1.X_add_number;
11785 
11786 	  load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
11787 	  if (breg != 0)
11788 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11789 			 tempreg, tempreg, breg);
11790 	  if (offbits == 0)
11791 	    {
11792 	      if (offset_expr.X_add_number != 0)
11793 		macro_build (&offset_expr, ADDRESS_ADDI_INSN,
11794 			     "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
11795 	      macro_build (NULL, s, fmt, op[0], tempreg);
11796 	    }
11797 	  else if (offbits == 16)
11798 	    macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
11799 	  else
11800 	    macro_build (NULL, s, fmt, op[0],
11801 			 (int) offset_expr.X_add_number, tempreg);
11802 	}
11803       else if (offbits != 16)
11804 	{
11805 	  /* The offset field is too narrow to be used for a low-part
11806 	     relocation, so load the whole address into the auxillary
11807 	     register.  */
11808 	  load_address (tempreg, &offset_expr, &used_at);
11809 	  if (breg != 0)
11810 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11811 			 tempreg, tempreg, breg);
11812 	  if (offbits == 0)
11813 	    macro_build (NULL, s, fmt, op[0], tempreg);
11814 	  else
11815 	    macro_build (NULL, s, fmt, op[0], 0, tempreg);
11816 	}
11817       else if (mips_pic == NO_PIC)
11818 	{
11819 	  /* If this is a reference to a GP relative symbol, and there
11820 	     is no base register, we want
11821 	       <op>	op[0],<sym>($gp)	(BFD_RELOC_GPREL16)
11822 	     Otherwise, if there is no base register, we want
11823 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
11824 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
11825 	     If we have a constant, we need two instructions anyhow,
11826 	     so we always use the latter form.
11827 
11828 	     If we have a base register, and this is a reference to a
11829 	     GP relative symbol, we want
11830 	       addu	$tempreg,$breg,$gp
11831 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_GPREL16)
11832 	     Otherwise we want
11833 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
11834 	       addu	$tempreg,$tempreg,$breg
11835 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
11836 	     With a constant we always use the latter case.
11837 
11838 	     With 64bit address space and no base register and $at usable,
11839 	     we want
11840 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
11841 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
11842 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
11843 	       dsll32	$tempreg,0
11844 	       daddu	$tempreg,$at
11845 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
11846 	     If we have a base register, we want
11847 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
11848 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
11849 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
11850 	       daddu	$at,$breg
11851 	       dsll32	$tempreg,0
11852 	       daddu	$tempreg,$at
11853 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
11854 
11855 	     Without $at we can't generate the optimal path for superscalar
11856 	     processors here since this would require two temporary registers.
11857 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
11858 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
11859 	       dsll	$tempreg,16
11860 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
11861 	       dsll	$tempreg,16
11862 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
11863 	     If we have a base register, we want
11864 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
11865 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
11866 	       dsll	$tempreg,16
11867 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
11868 	       dsll	$tempreg,16
11869 	       daddu	$tempreg,$tempreg,$breg
11870 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
11871 
11872 	     For GP relative symbols in 64bit address space we can use
11873 	     the same sequence as in 32bit address space.  */
11874 	  if (HAVE_64BIT_SYMBOLS)
11875 	    {
11876 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11877 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11878 		{
11879 		  relax_start (offset_expr.X_add_symbol);
11880 		  if (breg == 0)
11881 		    {
11882 		      macro_build (&offset_expr, s, fmt, op[0],
11883 				   BFD_RELOC_GPREL16, mips_gp_register);
11884 		    }
11885 		  else
11886 		    {
11887 		      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11888 				   tempreg, breg, mips_gp_register);
11889 		      macro_build (&offset_expr, s, fmt, op[0],
11890 				   BFD_RELOC_GPREL16, tempreg);
11891 		    }
11892 		  relax_switch ();
11893 		}
11894 
11895 	      if (used_at == 0 && mips_opts.at)
11896 		{
11897 		  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
11898 			       BFD_RELOC_MIPS_HIGHEST);
11899 		  macro_build (&offset_expr, "lui", LUI_FMT, AT,
11900 			       BFD_RELOC_HI16_S);
11901 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11902 			       tempreg, BFD_RELOC_MIPS_HIGHER);
11903 		  if (breg != 0)
11904 		    macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
11905 		  macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
11906 		  macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
11907 		  macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
11908 			       tempreg);
11909 		  used_at = 1;
11910 		}
11911 	      else
11912 		{
11913 		  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
11914 			       BFD_RELOC_MIPS_HIGHEST);
11915 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11916 			       tempreg, BFD_RELOC_MIPS_HIGHER);
11917 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11918 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11919 			       tempreg, BFD_RELOC_HI16_S);
11920 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11921 		  if (breg != 0)
11922 		    macro_build (NULL, "daddu", "d,v,t",
11923 				 tempreg, tempreg, breg);
11924 		  macro_build (&offset_expr, s, fmt, op[0],
11925 			       BFD_RELOC_LO16, tempreg);
11926 		}
11927 
11928 	      if (mips_relax.sequence)
11929 		relax_end ();
11930 	      break;
11931 	    }
11932 
11933 	  if (breg == 0)
11934 	    {
11935 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11936 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11937 		{
11938 		  relax_start (offset_expr.X_add_symbol);
11939 		  macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
11940 			       mips_gp_register);
11941 		  relax_switch ();
11942 		}
11943 	      macro_build_lui (&offset_expr, tempreg);
11944 	      macro_build (&offset_expr, s, fmt, op[0],
11945 			   BFD_RELOC_LO16, tempreg);
11946 	      if (mips_relax.sequence)
11947 		relax_end ();
11948 	    }
11949 	  else
11950 	    {
11951 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11952 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11953 		{
11954 		  relax_start (offset_expr.X_add_symbol);
11955 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11956 			       tempreg, breg, mips_gp_register);
11957 		  macro_build (&offset_expr, s, fmt, op[0],
11958 			       BFD_RELOC_GPREL16, tempreg);
11959 		  relax_switch ();
11960 		}
11961 	      macro_build_lui (&offset_expr, tempreg);
11962 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11963 			   tempreg, tempreg, breg);
11964 	      macro_build (&offset_expr, s, fmt, op[0],
11965 			   BFD_RELOC_LO16, tempreg);
11966 	      if (mips_relax.sequence)
11967 		relax_end ();
11968 	    }
11969 	}
11970       else if (!mips_big_got)
11971 	{
11972 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11973 
11974 	  /* If this is a reference to an external symbol, we want
11975 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11976 	       nop
11977 	       <op>	op[0],0($tempreg)
11978 	     Otherwise we want
11979 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11980 	       nop
11981 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
11982 	       <op>	op[0],0($tempreg)
11983 
11984 	     For NewABI, we want
11985 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_PAGE)
11986 	       <op>	op[0],<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
11987 
11988 	     If there is a base register, we add it to $tempreg before
11989 	     the <op>.  If there is a constant, we stick it in the
11990 	     <op> instruction.  We don't handle constants larger than
11991 	     16 bits, because we have no way to load the upper 16 bits
11992 	     (actually, we could handle them for the subset of cases
11993 	     in which we are not using $at).  */
11994 	  gas_assert (offset_expr.X_op == O_symbol);
11995 	  if (HAVE_NEWABI)
11996 	    {
11997 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11998 			   BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11999 	      if (breg != 0)
12000 		macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12001 			     tempreg, tempreg, breg);
12002 	      macro_build (&offset_expr, s, fmt, op[0],
12003 			   BFD_RELOC_MIPS_GOT_OFST, tempreg);
12004 	      break;
12005 	    }
12006 	  expr1.X_add_number = offset_expr.X_add_number;
12007 	  offset_expr.X_add_number = 0;
12008 	  if (expr1.X_add_number < -0x8000
12009 	      || expr1.X_add_number >= 0x8000)
12010 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12011 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12012 		       lw_reloc_type, mips_gp_register);
12013 	  load_delay_nop ();
12014 	  relax_start (offset_expr.X_add_symbol);
12015 	  relax_switch ();
12016 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12017 		       tempreg, BFD_RELOC_LO16);
12018 	  relax_end ();
12019 	  if (breg != 0)
12020 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12021 			 tempreg, tempreg, breg);
12022 	  macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12023 	}
12024       else if (mips_big_got && !HAVE_NEWABI)
12025 	{
12026 	  int gpdelay;
12027 
12028 	  /* If this is a reference to an external symbol, we want
12029 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
12030 	       addu	$tempreg,$tempreg,$gp
12031 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12032 	       <op>	op[0],0($tempreg)
12033 	     Otherwise we want
12034 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
12035 	       nop
12036 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
12037 	       <op>	op[0],0($tempreg)
12038 	     If there is a base register, we add it to $tempreg before
12039 	     the <op>.  If there is a constant, we stick it in the
12040 	     <op> instruction.  We don't handle constants larger than
12041 	     16 bits, because we have no way to load the upper 16 bits
12042 	     (actually, we could handle them for the subset of cases
12043 	     in which we are not using $at).  */
12044 	  gas_assert (offset_expr.X_op == O_symbol);
12045 	  expr1.X_add_number = offset_expr.X_add_number;
12046 	  offset_expr.X_add_number = 0;
12047 	  if (expr1.X_add_number < -0x8000
12048 	      || expr1.X_add_number >= 0x8000)
12049 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12050 	  gpdelay = reg_needs_delay (mips_gp_register);
12051 	  relax_start (offset_expr.X_add_symbol);
12052 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12053 		       BFD_RELOC_MIPS_GOT_HI16);
12054 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12055 		       mips_gp_register);
12056 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12057 		       BFD_RELOC_MIPS_GOT_LO16, tempreg);
12058 	  relax_switch ();
12059 	  if (gpdelay)
12060 	    macro_build (NULL, "nop", "");
12061 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12062 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
12063 	  load_delay_nop ();
12064 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12065 		       tempreg, BFD_RELOC_LO16);
12066 	  relax_end ();
12067 
12068 	  if (breg != 0)
12069 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12070 			 tempreg, tempreg, breg);
12071 	  macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12072 	}
12073       else if (mips_big_got && HAVE_NEWABI)
12074 	{
12075 	  /* If this is a reference to an external symbol, we want
12076 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
12077 	       add	$tempreg,$tempreg,$gp
12078 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12079 	       <op>	op[0],<ofst>($tempreg)
12080 	     Otherwise, for local symbols, we want:
12081 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_PAGE)
12082 	       <op>	op[0],<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
12083 	  gas_assert (offset_expr.X_op == O_symbol);
12084 	  expr1.X_add_number = offset_expr.X_add_number;
12085 	  offset_expr.X_add_number = 0;
12086 	  if (expr1.X_add_number < -0x8000
12087 	      || expr1.X_add_number >= 0x8000)
12088 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12089 	  relax_start (offset_expr.X_add_symbol);
12090 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12091 		       BFD_RELOC_MIPS_GOT_HI16);
12092 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12093 		       mips_gp_register);
12094 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12095 		       BFD_RELOC_MIPS_GOT_LO16, tempreg);
12096 	  if (breg != 0)
12097 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12098 			 tempreg, tempreg, breg);
12099 	  macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12100 
12101 	  relax_switch ();
12102 	  offset_expr.X_add_number = expr1.X_add_number;
12103 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12104 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12105 	  if (breg != 0)
12106 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12107 			 tempreg, tempreg, breg);
12108 	  macro_build (&offset_expr, s, fmt, op[0],
12109 		       BFD_RELOC_MIPS_GOT_OFST, tempreg);
12110 	  relax_end ();
12111 	}
12112       else
12113 	abort ();
12114 
12115       break;
12116 
12117     case M_JRADDIUSP:
12118       gas_assert (mips_opts.micromips);
12119       gas_assert (mips_opts.insn32);
12120       start_noreorder ();
12121       macro_build (NULL, "jr", "s", RA);
12122       expr1.X_add_number = op[0] << 2;
12123       macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12124       end_noreorder ();
12125       break;
12126 
12127     case M_JRC:
12128       gas_assert (mips_opts.micromips);
12129       gas_assert (mips_opts.insn32);
12130       macro_build (NULL, "jr", "s", op[0]);
12131       if (mips_opts.noreorder)
12132 	macro_build (NULL, "nop", "");
12133       break;
12134 
12135     case M_LI:
12136     case M_LI_S:
12137       load_register (op[0], &imm_expr, 0);
12138       break;
12139 
12140     case M_DLI:
12141       load_register (op[0], &imm_expr, 1);
12142       break;
12143 
12144     case M_LI_SS:
12145       if (imm_expr.X_op == O_constant)
12146 	{
12147 	  used_at = 1;
12148 	  load_register (AT, &imm_expr, 0);
12149 	  macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12150 	  break;
12151 	}
12152       else
12153 	{
12154 	  gas_assert (imm_expr.X_op == O_absent
12155 		      && offset_expr.X_op == O_symbol
12156 		      && strcmp (segment_name (S_GET_SEGMENT
12157 					       (offset_expr.X_add_symbol)),
12158 				 ".lit4") == 0
12159 		      && offset_expr.X_add_number == 0);
12160 	  macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
12161 		       BFD_RELOC_MIPS_LITERAL, mips_gp_register);
12162 	  break;
12163 	}
12164 
12165     case M_LI_D:
12166       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
12167          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
12168          order 32 bits of the value and the low order 32 bits are either
12169          zero or in OFFSET_EXPR.  */
12170       if (imm_expr.X_op == O_constant)
12171 	{
12172 	  if (GPR_SIZE == 64)
12173 	    load_register (op[0], &imm_expr, 1);
12174 	  else
12175 	    {
12176 	      int hreg, lreg;
12177 
12178 	      if (target_big_endian)
12179 		{
12180 		  hreg = op[0];
12181 		  lreg = op[0] + 1;
12182 		}
12183 	      else
12184 		{
12185 		  hreg = op[0] + 1;
12186 		  lreg = op[0];
12187 		}
12188 
12189 	      if (hreg <= 31)
12190 		load_register (hreg, &imm_expr, 0);
12191 	      if (lreg <= 31)
12192 		{
12193 		  if (offset_expr.X_op == O_absent)
12194 		    move_register (lreg, 0);
12195 		  else
12196 		    {
12197 		      gas_assert (offset_expr.X_op == O_constant);
12198 		      load_register (lreg, &offset_expr, 0);
12199 		    }
12200 		}
12201 	    }
12202 	  break;
12203 	}
12204       gas_assert (imm_expr.X_op == O_absent);
12205 
12206       /* We know that sym is in the .rdata section.  First we get the
12207 	 upper 16 bits of the address.  */
12208       if (mips_pic == NO_PIC)
12209 	{
12210 	  macro_build_lui (&offset_expr, AT);
12211 	  used_at = 1;
12212 	}
12213       else
12214 	{
12215 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12216 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
12217 	  used_at = 1;
12218 	}
12219 
12220       /* Now we load the register(s).  */
12221       if (GPR_SIZE == 64)
12222 	{
12223 	  used_at = 1;
12224 	  macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12225 		       BFD_RELOC_LO16, AT);
12226 	}
12227       else
12228 	{
12229 	  used_at = 1;
12230 	  macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12231 		       BFD_RELOC_LO16, AT);
12232 	  if (op[0] != RA)
12233 	    {
12234 	      /* FIXME: How in the world do we deal with the possible
12235 		 overflow here?  */
12236 	      offset_expr.X_add_number += 4;
12237 	      macro_build (&offset_expr, "lw", "t,o(b)",
12238 			   op[0] + 1, BFD_RELOC_LO16, AT);
12239 	    }
12240 	}
12241       break;
12242 
12243     case M_LI_DD:
12244       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
12245          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12246          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
12247          the value and the low order 32 bits are either zero or in
12248          OFFSET_EXPR.  */
12249       if (imm_expr.X_op == O_constant)
12250 	{
12251 	  used_at = 1;
12252 	  load_register (AT, &imm_expr, FPR_SIZE == 64);
12253 	  if (FPR_SIZE == 64 && GPR_SIZE == 64)
12254 	    macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
12255 	  else
12256 	    {
12257 	      if (ISA_HAS_MXHC1 (mips_opts.isa))
12258 	        macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12259 	      else if (FPR_SIZE != 32)
12260 		as_bad (_("Unable to generate `%s' compliant code "
12261 			  "without mthc1"),
12262 			(FPR_SIZE == 64) ? "fp64" : "fpxx");
12263 	      else
12264 		macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
12265 	      if (offset_expr.X_op == O_absent)
12266 		macro_build (NULL, "mtc1", "t,G", 0, op[0]);
12267 	      else
12268 		{
12269 		  gas_assert (offset_expr.X_op == O_constant);
12270 		  load_register (AT, &offset_expr, 0);
12271 		  macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12272 		}
12273 	    }
12274 	  break;
12275 	}
12276 
12277       gas_assert (imm_expr.X_op == O_absent
12278 		  && offset_expr.X_op == O_symbol
12279 		  && offset_expr.X_add_number == 0);
12280       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12281       if (strcmp (s, ".lit8") == 0)
12282  	{
12283  	  op[2] = mips_gp_register;
12284 	  offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12285 	  offset_reloc[1] = BFD_RELOC_UNUSED;
12286 	  offset_reloc[2] = BFD_RELOC_UNUSED;
12287 	}
12288       else
12289 	{
12290 	  gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
12291 	  used_at = 1;
12292 	  if (mips_pic != NO_PIC)
12293 	    macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12294 			 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12295 	  else
12296 	    {
12297 	      /* FIXME: This won't work for a 64 bit address.  */
12298 	      macro_build_lui (&offset_expr, AT);
12299 	    }
12300 
12301 	  op[2] = AT;
12302 	  offset_reloc[0] = BFD_RELOC_LO16;
12303 	  offset_reloc[1] = BFD_RELOC_UNUSED;
12304 	  offset_reloc[2] = BFD_RELOC_UNUSED;
12305  	}
12306       align = 8;
12307       /* Fall through */
12308 
12309     case M_L_DAB:
12310       /*
12311        * The MIPS assembler seems to check for X_add_number not
12312        * being double aligned and generating:
12313        *	lui	at,%hi(foo+1)
12314        *	addu	at,at,v1
12315        *	addiu	at,at,%lo(foo+1)
12316        *	lwc1	f2,0(at)
12317        *	lwc1	f3,4(at)
12318        * But, the resulting address is the same after relocation so why
12319        * generate the extra instruction?
12320        */
12321       /* Itbl support may require additional care here.  */
12322       coproc = 1;
12323       fmt = "T,o(b)";
12324       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12325 	{
12326 	  s = "ldc1";
12327 	  goto ld_st;
12328 	}
12329       s = "lwc1";
12330       goto ldd_std;
12331 
12332     case M_S_DAB:
12333       gas_assert (!mips_opts.micromips);
12334       /* Itbl support may require additional care here.  */
12335       coproc = 1;
12336       fmt = "T,o(b)";
12337       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12338 	{
12339 	  s = "sdc1";
12340 	  goto ld_st;
12341 	}
12342       s = "swc1";
12343       goto ldd_std;
12344 
12345     case M_LQ_AB:
12346       fmt = "t,o(b)";
12347       s = "lq";
12348       goto ld;
12349 
12350     case M_SQ_AB:
12351       fmt = "t,o(b)";
12352       s = "sq";
12353       goto ld_st;
12354 
12355     case M_LD_AB:
12356       fmt = "t,o(b)";
12357       if (GPR_SIZE == 64)
12358 	{
12359 	  s = "ld";
12360 	  goto ld;
12361 	}
12362       s = "lw";
12363       goto ldd_std;
12364 
12365     case M_SD_AB:
12366       fmt = "t,o(b)";
12367       if (GPR_SIZE == 64)
12368 	{
12369 	  s = "sd";
12370 	  goto ld_st;
12371 	}
12372       s = "sw";
12373 
12374     ldd_std:
12375       /* Even on a big endian machine $fn comes before $fn+1.  We have
12376 	 to adjust when loading from memory.  We set coproc if we must
12377 	 load $fn+1 first.  */
12378       /* Itbl support may require additional care here.  */
12379       if (!target_big_endian)
12380 	coproc = 0;
12381 
12382       breg = op[2];
12383       if (small_offset_p (0, align, 16))
12384 	{
12385 	  ep = &offset_expr;
12386 	  if (!small_offset_p (4, align, 16))
12387 	    {
12388 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12389 			   -1, offset_reloc[0], offset_reloc[1],
12390 			   offset_reloc[2]);
12391 	      expr1.X_add_number = 0;
12392 	      ep = &expr1;
12393 	      breg = AT;
12394 	      used_at = 1;
12395 	      offset_reloc[0] = BFD_RELOC_LO16;
12396 	      offset_reloc[1] = BFD_RELOC_UNUSED;
12397 	      offset_reloc[2] = BFD_RELOC_UNUSED;
12398 	    }
12399 	  if (strcmp (s, "lw") == 0 && op[0] == breg)
12400 	    {
12401 	      ep->X_add_number += 4;
12402 	      macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
12403 			   offset_reloc[1], offset_reloc[2], breg);
12404 	      ep->X_add_number -= 4;
12405 	      macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
12406 			   offset_reloc[1], offset_reloc[2], breg);
12407 	    }
12408 	  else
12409 	    {
12410 	      macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
12411 			   offset_reloc[0], offset_reloc[1], offset_reloc[2],
12412 			   breg);
12413 	      ep->X_add_number += 4;
12414 	      macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
12415 			   offset_reloc[0], offset_reloc[1], offset_reloc[2],
12416 			   breg);
12417 	    }
12418 	  break;
12419 	}
12420 
12421       if (offset_expr.X_op != O_symbol
12422 	  && offset_expr.X_op != O_constant)
12423 	{
12424 	  as_bad (_("expression too complex"));
12425 	  offset_expr.X_op = O_constant;
12426 	}
12427 
12428       if (HAVE_32BIT_ADDRESSES
12429 	  && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12430 	{
12431 	  char value [32];
12432 
12433 	  sprintf_vma (value, offset_expr.X_add_number);
12434 	  as_bad (_("number (0x%s) larger than 32 bits"), value);
12435 	}
12436 
12437       if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
12438 	{
12439 	  /* If this is a reference to a GP relative symbol, we want
12440 	       <op>	op[0],<sym>($gp)	(BFD_RELOC_GPREL16)
12441 	       <op>	op[0]+1,<sym>+4($gp)	(BFD_RELOC_GPREL16)
12442 	     If we have a base register, we use this
12443 	       addu	$at,$breg,$gp
12444 	       <op>	op[0],<sym>($at)	(BFD_RELOC_GPREL16)
12445 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_GPREL16)
12446 	     If this is not a GP relative symbol, we want
12447 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
12448 	       <op>	op[0],<sym>($at)	(BFD_RELOC_LO16)
12449 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_LO16)
12450 	     If there is a base register, we add it to $at after the
12451 	     lui instruction.  If there is a constant, we always use
12452 	     the last case.  */
12453 	  if (offset_expr.X_op == O_symbol
12454 	      && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12455 	      && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12456 	    {
12457 	      relax_start (offset_expr.X_add_symbol);
12458 	      if (breg == 0)
12459 		{
12460 		  tempreg = mips_gp_register;
12461 		}
12462 	      else
12463 		{
12464 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12465 			       AT, breg, mips_gp_register);
12466 		  tempreg = AT;
12467 		  used_at = 1;
12468 		}
12469 
12470 	      /* Itbl support may require additional care here.  */
12471 	      macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12472 			   BFD_RELOC_GPREL16, tempreg);
12473 	      offset_expr.X_add_number += 4;
12474 
12475 	      /* Set mips_optimize to 2 to avoid inserting an
12476                  undesired nop.  */
12477 	      hold_mips_optimize = mips_optimize;
12478 	      mips_optimize = 2;
12479 	      /* Itbl support may require additional care here.  */
12480 	      macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12481 			   BFD_RELOC_GPREL16, tempreg);
12482 	      mips_optimize = hold_mips_optimize;
12483 
12484 	      relax_switch ();
12485 
12486 	      offset_expr.X_add_number -= 4;
12487 	    }
12488 	  used_at = 1;
12489 	  if (offset_high_part (offset_expr.X_add_number, 16)
12490 	      != offset_high_part (offset_expr.X_add_number + 4, 16))
12491 	    {
12492 	      load_address (AT, &offset_expr, &used_at);
12493 	      offset_expr.X_op = O_constant;
12494 	      offset_expr.X_add_number = 0;
12495 	    }
12496 	  else
12497 	    macro_build_lui (&offset_expr, AT);
12498 	  if (breg != 0)
12499 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12500 	  /* Itbl support may require additional care here.  */
12501 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12502 		       BFD_RELOC_LO16, AT);
12503 	  /* FIXME: How do we handle overflow here?  */
12504 	  offset_expr.X_add_number += 4;
12505 	  /* Itbl support may require additional care here.  */
12506 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12507 		       BFD_RELOC_LO16, AT);
12508 	  if (mips_relax.sequence)
12509 	    relax_end ();
12510 	}
12511       else if (!mips_big_got)
12512 	{
12513 	  /* If this is a reference to an external symbol, we want
12514 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
12515 	       nop
12516 	       <op>	op[0],0($at)
12517 	       <op>	op[0]+1,4($at)
12518 	     Otherwise we want
12519 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
12520 	       nop
12521 	       <op>	op[0],<sym>($at)	(BFD_RELOC_LO16)
12522 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_LO16)
12523 	     If there is a base register we add it to $at before the
12524 	     lwc1 instructions.  If there is a constant we include it
12525 	     in the lwc1 instructions.  */
12526 	  used_at = 1;
12527 	  expr1.X_add_number = offset_expr.X_add_number;
12528 	  if (expr1.X_add_number < -0x8000
12529 	      || expr1.X_add_number >= 0x8000 - 4)
12530 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12531 	  load_got_offset (AT, &offset_expr);
12532 	  load_delay_nop ();
12533 	  if (breg != 0)
12534 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12535 
12536 	  /* Set mips_optimize to 2 to avoid inserting an undesired
12537              nop.  */
12538 	  hold_mips_optimize = mips_optimize;
12539 	  mips_optimize = 2;
12540 
12541 	  /* Itbl support may require additional care here.  */
12542 	  relax_start (offset_expr.X_add_symbol);
12543 	  macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12544 		       BFD_RELOC_LO16, AT);
12545 	  expr1.X_add_number += 4;
12546 	  macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12547 		       BFD_RELOC_LO16, AT);
12548 	  relax_switch ();
12549 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12550 		       BFD_RELOC_LO16, AT);
12551 	  offset_expr.X_add_number += 4;
12552 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12553 		       BFD_RELOC_LO16, AT);
12554 	  relax_end ();
12555 
12556 	  mips_optimize = hold_mips_optimize;
12557 	}
12558       else if (mips_big_got)
12559 	{
12560 	  int gpdelay;
12561 
12562 	  /* If this is a reference to an external symbol, we want
12563 	       lui	$at,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
12564 	       addu	$at,$at,$gp
12565 	       lw	$at,<sym>($at)		(BFD_RELOC_MIPS_GOT_LO16)
12566 	       nop
12567 	       <op>	op[0],0($at)
12568 	       <op>	op[0]+1,4($at)
12569 	     Otherwise we want
12570 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
12571 	       nop
12572 	       <op>	op[0],<sym>($at)	(BFD_RELOC_LO16)
12573 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_LO16)
12574 	     If there is a base register we add it to $at before the
12575 	     lwc1 instructions.  If there is a constant we include it
12576 	     in the lwc1 instructions.  */
12577 	  used_at = 1;
12578 	  expr1.X_add_number = offset_expr.X_add_number;
12579 	  offset_expr.X_add_number = 0;
12580 	  if (expr1.X_add_number < -0x8000
12581 	      || expr1.X_add_number >= 0x8000 - 4)
12582 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12583 	  gpdelay = reg_needs_delay (mips_gp_register);
12584 	  relax_start (offset_expr.X_add_symbol);
12585 	  macro_build (&offset_expr, "lui", LUI_FMT,
12586 		       AT, BFD_RELOC_MIPS_GOT_HI16);
12587 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12588 		       AT, AT, mips_gp_register);
12589 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
12590 		       AT, BFD_RELOC_MIPS_GOT_LO16, AT);
12591 	  load_delay_nop ();
12592 	  if (breg != 0)
12593 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12594 	  /* Itbl support may require additional care here.  */
12595 	  macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12596 		       BFD_RELOC_LO16, AT);
12597 	  expr1.X_add_number += 4;
12598 
12599 	  /* Set mips_optimize to 2 to avoid inserting an undesired
12600              nop.  */
12601 	  hold_mips_optimize = mips_optimize;
12602 	  mips_optimize = 2;
12603 	  /* Itbl support may require additional care here.  */
12604 	  macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12605 		       BFD_RELOC_LO16, AT);
12606 	  mips_optimize = hold_mips_optimize;
12607 	  expr1.X_add_number -= 4;
12608 
12609 	  relax_switch ();
12610 	  offset_expr.X_add_number = expr1.X_add_number;
12611 	  if (gpdelay)
12612 	    macro_build (NULL, "nop", "");
12613 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12614 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
12615 	  load_delay_nop ();
12616 	  if (breg != 0)
12617 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12618 	  /* Itbl support may require additional care here.  */
12619 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12620 		       BFD_RELOC_LO16, AT);
12621 	  offset_expr.X_add_number += 4;
12622 
12623 	  /* Set mips_optimize to 2 to avoid inserting an undesired
12624              nop.  */
12625 	  hold_mips_optimize = mips_optimize;
12626 	  mips_optimize = 2;
12627 	  /* Itbl support may require additional care here.  */
12628 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12629 		       BFD_RELOC_LO16, AT);
12630 	  mips_optimize = hold_mips_optimize;
12631 	  relax_end ();
12632 	}
12633       else
12634 	abort ();
12635 
12636       break;
12637 
12638     case M_SAA_AB:
12639       s = "saa";
12640       goto saa_saad;
12641     case M_SAAD_AB:
12642       s = "saad";
12643     saa_saad:
12644       gas_assert (!mips_opts.micromips);
12645       offbits = 0;
12646       fmt = "t,(b)";
12647       goto ld_st;
12648 
12649    /* New code added to support COPZ instructions.
12650       This code builds table entries out of the macros in mip_opcodes.
12651       R4000 uses interlocks to handle coproc delays.
12652       Other chips (like the R3000) require nops to be inserted for delays.
12653 
12654       FIXME: Currently, we require that the user handle delays.
12655       In order to fill delay slots for non-interlocked chips,
12656       we must have a way to specify delays based on the coprocessor.
12657       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12658       What are the side-effects of the cop instruction?
12659       What cache support might we have and what are its effects?
12660       Both coprocessor & memory require delays. how long???
12661       What registers are read/set/modified?
12662 
12663       If an itbl is provided to interpret cop instructions,
12664       this knowledge can be encoded in the itbl spec.  */
12665 
12666     case M_COP0:
12667       s = "c0";
12668       goto copz;
12669     case M_COP1:
12670       s = "c1";
12671       goto copz;
12672     case M_COP2:
12673       s = "c2";
12674       goto copz;
12675     case M_COP3:
12676       s = "c3";
12677     copz:
12678       gas_assert (!mips_opts.micromips);
12679       /* For now we just do C (same as Cz).  The parameter will be
12680          stored in insn_opcode by mips_ip.  */
12681       macro_build (NULL, s, "C", (int) ip->insn_opcode);
12682       break;
12683 
12684     case M_MOVE:
12685       move_register (op[0], op[1]);
12686       break;
12687 
12688     case M_MOVEP:
12689       gas_assert (mips_opts.micromips);
12690       gas_assert (mips_opts.insn32);
12691       move_register (micromips_to_32_reg_h_map1[op[0]],
12692 		     micromips_to_32_reg_m_map[op[1]]);
12693       move_register (micromips_to_32_reg_h_map2[op[0]],
12694 		     micromips_to_32_reg_n_map[op[2]]);
12695       break;
12696 
12697     case M_DMUL:
12698       dbl = 1;
12699     case M_MUL:
12700       if (mips_opts.arch == CPU_R5900)
12701 	macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12702 		     op[2]);
12703       else
12704         {
12705 	  macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12706 	  macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12707         }
12708       break;
12709 
12710     case M_DMUL_I:
12711       dbl = 1;
12712     case M_MUL_I:
12713       /* The MIPS assembler some times generates shifts and adds.  I'm
12714 	 not trying to be that fancy. GCC should do this for us
12715 	 anyway.  */
12716       used_at = 1;
12717       load_register (AT, &imm_expr, dbl);
12718       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
12719       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12720       break;
12721 
12722     case M_DMULO_I:
12723       dbl = 1;
12724     case M_MULO_I:
12725       imm = 1;
12726       goto do_mulo;
12727 
12728     case M_DMULO:
12729       dbl = 1;
12730     case M_MULO:
12731     do_mulo:
12732       start_noreorder ();
12733       used_at = 1;
12734       if (imm)
12735 	load_register (AT, &imm_expr, dbl);
12736       macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
12737 		   op[1], imm ? AT : op[2]);
12738       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12739       macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
12740       macro_build (NULL, "mfhi", MFHL_FMT, AT);
12741       if (mips_trap)
12742 	macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
12743       else
12744 	{
12745 	  if (mips_opts.micromips)
12746 	    micromips_label_expr (&label_expr);
12747 	  else
12748 	    label_expr.X_add_number = 8;
12749 	  macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
12750 	  macro_build (NULL, "nop", "");
12751 	  macro_build (NULL, "break", BRK_FMT, 6);
12752 	  if (mips_opts.micromips)
12753 	    micromips_add_label ();
12754 	}
12755       end_noreorder ();
12756       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12757       break;
12758 
12759     case M_DMULOU_I:
12760       dbl = 1;
12761     case M_MULOU_I:
12762       imm = 1;
12763       goto do_mulou;
12764 
12765     case M_DMULOU:
12766       dbl = 1;
12767     case M_MULOU:
12768     do_mulou:
12769       start_noreorder ();
12770       used_at = 1;
12771       if (imm)
12772 	load_register (AT, &imm_expr, dbl);
12773       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
12774 		   op[1], imm ? AT : op[2]);
12775       macro_build (NULL, "mfhi", MFHL_FMT, AT);
12776       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12777       if (mips_trap)
12778 	macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
12779       else
12780 	{
12781 	  if (mips_opts.micromips)
12782 	    micromips_label_expr (&label_expr);
12783 	  else
12784 	    label_expr.X_add_number = 8;
12785 	  macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
12786 	  macro_build (NULL, "nop", "");
12787 	  macro_build (NULL, "break", BRK_FMT, 6);
12788 	  if (mips_opts.micromips)
12789 	    micromips_add_label ();
12790 	}
12791       end_noreorder ();
12792       break;
12793 
12794     case M_DROL:
12795       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
12796 	{
12797 	  if (op[0] == op[1])
12798 	    {
12799 	      tempreg = AT;
12800 	      used_at = 1;
12801 	    }
12802 	  else
12803 	    tempreg = op[0];
12804 	  macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
12805 	  macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
12806 	  break;
12807 	}
12808       used_at = 1;
12809       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12810       macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
12811       macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
12812       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12813       break;
12814 
12815     case M_ROL:
12816       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
12817 	{
12818 	  if (op[0] == op[1])
12819 	    {
12820 	      tempreg = AT;
12821 	      used_at = 1;
12822 	    }
12823 	  else
12824 	    tempreg = op[0];
12825 	  macro_build (NULL, "negu", "d,w", tempreg, op[2]);
12826 	  macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
12827 	  break;
12828 	}
12829       used_at = 1;
12830       macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12831       macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
12832       macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
12833       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12834       break;
12835 
12836     case M_DROL_I:
12837       {
12838 	unsigned int rot;
12839 	char *l;
12840 	char *rr;
12841 
12842 	rot = imm_expr.X_add_number & 0x3f;
12843 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
12844 	  {
12845 	    rot = (64 - rot) & 0x3f;
12846 	    if (rot >= 32)
12847 	      macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
12848 	    else
12849 	      macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
12850 	    break;
12851 	  }
12852 	if (rot == 0)
12853 	  {
12854 	    macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
12855 	    break;
12856 	  }
12857 	l = (rot < 0x20) ? "dsll" : "dsll32";
12858 	rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
12859 	rot &= 0x1f;
12860 	used_at = 1;
12861 	macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
12862 	macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12863 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12864       }
12865       break;
12866 
12867     case M_ROL_I:
12868       {
12869 	unsigned int rot;
12870 
12871 	rot = imm_expr.X_add_number & 0x1f;
12872 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
12873 	  {
12874 	    macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
12875 			 (32 - rot) & 0x1f);
12876 	    break;
12877 	  }
12878 	if (rot == 0)
12879 	  {
12880 	    macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
12881 	    break;
12882 	  }
12883 	used_at = 1;
12884 	macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
12885 	macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12886 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12887       }
12888       break;
12889 
12890     case M_DROR:
12891       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
12892 	{
12893 	  macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
12894 	  break;
12895 	}
12896       used_at = 1;
12897       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12898       macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
12899       macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
12900       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12901       break;
12902 
12903     case M_ROR:
12904       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
12905 	{
12906 	  macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
12907 	  break;
12908 	}
12909       used_at = 1;
12910       macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12911       macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
12912       macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
12913       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12914       break;
12915 
12916     case M_DROR_I:
12917       {
12918 	unsigned int rot;
12919 	char *l;
12920 	char *rr;
12921 
12922 	rot = imm_expr.X_add_number & 0x3f;
12923 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
12924 	  {
12925 	    if (rot >= 32)
12926 	      macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
12927 	    else
12928 	      macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
12929 	    break;
12930 	  }
12931 	if (rot == 0)
12932 	  {
12933 	    macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
12934 	    break;
12935 	  }
12936 	rr = (rot < 0x20) ? "dsrl" : "dsrl32";
12937 	l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
12938 	rot &= 0x1f;
12939 	used_at = 1;
12940 	macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
12941 	macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12942 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12943       }
12944       break;
12945 
12946     case M_ROR_I:
12947       {
12948 	unsigned int rot;
12949 
12950 	rot = imm_expr.X_add_number & 0x1f;
12951 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
12952 	  {
12953 	    macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
12954 	    break;
12955 	  }
12956 	if (rot == 0)
12957 	  {
12958 	    macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
12959 	    break;
12960 	  }
12961 	used_at = 1;
12962 	macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
12963 	macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12964 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12965       }
12966       break;
12967 
12968     case M_SEQ:
12969       if (op[1] == 0)
12970 	macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
12971       else if (op[2] == 0)
12972 	macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
12973       else
12974 	{
12975 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
12976 	  macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
12977 	}
12978       break;
12979 
12980     case M_SEQ_I:
12981       if (imm_expr.X_add_number == 0)
12982 	{
12983 	  macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
12984 	  break;
12985 	}
12986       if (op[1] == 0)
12987 	{
12988 	  as_warn (_("instruction %s: result is always false"),
12989 		   ip->insn_mo->name);
12990 	  move_register (op[0], 0);
12991 	  break;
12992 	}
12993       if (CPU_HAS_SEQ (mips_opts.arch)
12994 	  && -512 <= imm_expr.X_add_number
12995 	  && imm_expr.X_add_number < 512)
12996 	{
12997 	  macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
12998 		       (int) imm_expr.X_add_number);
12999 	  break;
13000 	}
13001       if (imm_expr.X_add_number >= 0
13002 	  && imm_expr.X_add_number < 0x10000)
13003 	macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
13004       else if (imm_expr.X_add_number > -0x8000
13005 	       && imm_expr.X_add_number < 0)
13006 	{
13007 	  imm_expr.X_add_number = -imm_expr.X_add_number;
13008 	  macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13009 		       "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13010 	}
13011       else if (CPU_HAS_SEQ (mips_opts.arch))
13012 	{
13013 	  used_at = 1;
13014 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13015 	  macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
13016 	  break;
13017 	}
13018       else
13019 	{
13020 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13021 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13022 	  used_at = 1;
13023 	}
13024       macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13025       break;
13026 
13027     case M_SGE:		/* X >= Y  <==>  not (X < Y) */
13028       s = "slt";
13029       goto sge;
13030     case M_SGEU:
13031       s = "sltu";
13032     sge:
13033       macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13034       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13035       break;
13036 
13037     case M_SGE_I:	/* X >= I  <==>  not (X < I) */
13038     case M_SGEU_I:
13039       if (imm_expr.X_add_number >= -0x8000
13040 	  && imm_expr.X_add_number < 0x8000)
13041 	macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13042 		     op[0], op[1], BFD_RELOC_LO16);
13043       else
13044 	{
13045 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13046 	  macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
13047 		       op[0], op[1], AT);
13048 	  used_at = 1;
13049 	}
13050       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13051       break;
13052 
13053     case M_SGT:		/* X > Y  <==>  Y < X */
13054       s = "slt";
13055       goto sgt;
13056     case M_SGTU:
13057       s = "sltu";
13058     sgt:
13059       macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13060       break;
13061 
13062     case M_SGT_I:	/* X > I  <==>  I < X */
13063       s = "slt";
13064       goto sgti;
13065     case M_SGTU_I:
13066       s = "sltu";
13067     sgti:
13068       used_at = 1;
13069       load_register (AT, &imm_expr, GPR_SIZE == 64);
13070       macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13071       break;
13072 
13073     case M_SLE:		/* X <= Y  <==>  Y >= X  <==>  not (Y < X) */
13074       s = "slt";
13075       goto sle;
13076     case M_SLEU:
13077       s = "sltu";
13078     sle:
13079       macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13080       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13081       break;
13082 
13083     case M_SLE_I:	/* X <= I  <==>  I >= X  <==>  not (I < X) */
13084       s = "slt";
13085       goto slei;
13086     case M_SLEU_I:
13087       s = "sltu";
13088     slei:
13089       used_at = 1;
13090       load_register (AT, &imm_expr, GPR_SIZE == 64);
13091       macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13092       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13093       break;
13094 
13095     case M_SLT_I:
13096       if (imm_expr.X_add_number >= -0x8000
13097 	  && imm_expr.X_add_number < 0x8000)
13098 	{
13099 	  macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13100 		       BFD_RELOC_LO16);
13101 	  break;
13102 	}
13103       used_at = 1;
13104       load_register (AT, &imm_expr, GPR_SIZE == 64);
13105       macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
13106       break;
13107 
13108     case M_SLTU_I:
13109       if (imm_expr.X_add_number >= -0x8000
13110 	  && imm_expr.X_add_number < 0x8000)
13111 	{
13112 	  macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
13113 		       BFD_RELOC_LO16);
13114 	  break;
13115 	}
13116       used_at = 1;
13117       load_register (AT, &imm_expr, GPR_SIZE == 64);
13118       macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
13119       break;
13120 
13121     case M_SNE:
13122       if (op[1] == 0)
13123 	macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13124       else if (op[2] == 0)
13125 	macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13126       else
13127 	{
13128 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13129 	  macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13130 	}
13131       break;
13132 
13133     case M_SNE_I:
13134       if (imm_expr.X_add_number == 0)
13135 	{
13136 	  macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13137 	  break;
13138 	}
13139       if (op[1] == 0)
13140 	{
13141 	  as_warn (_("instruction %s: result is always true"),
13142 		   ip->insn_mo->name);
13143 	  macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
13144 		       op[0], 0, BFD_RELOC_LO16);
13145 	  break;
13146 	}
13147       if (CPU_HAS_SEQ (mips_opts.arch)
13148 	  && -512 <= imm_expr.X_add_number
13149 	  && imm_expr.X_add_number < 512)
13150 	{
13151 	  macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
13152 		       (int) imm_expr.X_add_number);
13153 	  break;
13154 	}
13155       if (imm_expr.X_add_number >= 0
13156 	  && imm_expr.X_add_number < 0x10000)
13157 	{
13158 	  macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13159 		       BFD_RELOC_LO16);
13160 	}
13161       else if (imm_expr.X_add_number > -0x8000
13162 	       && imm_expr.X_add_number < 0)
13163 	{
13164 	  imm_expr.X_add_number = -imm_expr.X_add_number;
13165 	  macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13166 		       "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13167 	}
13168       else if (CPU_HAS_SEQ (mips_opts.arch))
13169 	{
13170 	  used_at = 1;
13171 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13172 	  macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
13173 	  break;
13174 	}
13175       else
13176 	{
13177 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13178 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13179 	  used_at = 1;
13180 	}
13181       macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13182       break;
13183 
13184     case M_SUB_I:
13185       s = "addi";
13186       s2 = "sub";
13187       goto do_subi;
13188     case M_SUBU_I:
13189       s = "addiu";
13190       s2 = "subu";
13191       goto do_subi;
13192     case M_DSUB_I:
13193       dbl = 1;
13194       s = "daddi";
13195       s2 = "dsub";
13196       if (!mips_opts.micromips)
13197 	goto do_subi;
13198       if (imm_expr.X_add_number > -0x200
13199 	  && imm_expr.X_add_number <= 0x200)
13200 	{
13201 	  macro_build (NULL, s, "t,r,.", op[0], op[1],
13202 		       (int) -imm_expr.X_add_number);
13203 	  break;
13204 	}
13205       goto do_subi_i;
13206     case M_DSUBU_I:
13207       dbl = 1;
13208       s = "daddiu";
13209       s2 = "dsubu";
13210     do_subi:
13211       if (imm_expr.X_add_number > -0x8000
13212 	  && imm_expr.X_add_number <= 0x8000)
13213 	{
13214 	  imm_expr.X_add_number = -imm_expr.X_add_number;
13215 	  macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13216 	  break;
13217 	}
13218     do_subi_i:
13219       used_at = 1;
13220       load_register (AT, &imm_expr, dbl);
13221       macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
13222       break;
13223 
13224     case M_TEQ_I:
13225       s = "teq";
13226       goto trap;
13227     case M_TGE_I:
13228       s = "tge";
13229       goto trap;
13230     case M_TGEU_I:
13231       s = "tgeu";
13232       goto trap;
13233     case M_TLT_I:
13234       s = "tlt";
13235       goto trap;
13236     case M_TLTU_I:
13237       s = "tltu";
13238       goto trap;
13239     case M_TNE_I:
13240       s = "tne";
13241     trap:
13242       used_at = 1;
13243       load_register (AT, &imm_expr, GPR_SIZE == 64);
13244       macro_build (NULL, s, "s,t", op[0], AT);
13245       break;
13246 
13247     case M_TRUNCWS:
13248     case M_TRUNCWD:
13249       gas_assert (!mips_opts.micromips);
13250       gas_assert (mips_opts.isa == ISA_MIPS1);
13251       used_at = 1;
13252 
13253       /*
13254        * Is the double cfc1 instruction a bug in the mips assembler;
13255        * or is there a reason for it?
13256        */
13257       start_noreorder ();
13258       macro_build (NULL, "cfc1", "t,G", op[2], RA);
13259       macro_build (NULL, "cfc1", "t,G", op[2], RA);
13260       macro_build (NULL, "nop", "");
13261       expr1.X_add_number = 3;
13262       macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
13263       expr1.X_add_number = 2;
13264       macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13265       macro_build (NULL, "ctc1", "t,G", AT, RA);
13266       macro_build (NULL, "nop", "");
13267       macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
13268 		   op[0], op[1]);
13269       macro_build (NULL, "ctc1", "t,G", op[2], RA);
13270       macro_build (NULL, "nop", "");
13271       end_noreorder ();
13272       break;
13273 
13274     case M_ULH_AB:
13275       s = "lb";
13276       s2 = "lbu";
13277       off = 1;
13278       goto uld_st;
13279     case M_ULHU_AB:
13280       s = "lbu";
13281       s2 = "lbu";
13282       off = 1;
13283       goto uld_st;
13284     case M_ULW_AB:
13285       s = "lwl";
13286       s2 = "lwr";
13287       offbits = (mips_opts.micromips ? 12 : 16);
13288       off = 3;
13289       goto uld_st;
13290     case M_ULD_AB:
13291       s = "ldl";
13292       s2 = "ldr";
13293       offbits = (mips_opts.micromips ? 12 : 16);
13294       off = 7;
13295       goto uld_st;
13296     case M_USH_AB:
13297       s = "sb";
13298       s2 = "sb";
13299       off = 1;
13300       ust = 1;
13301       goto uld_st;
13302     case M_USW_AB:
13303       s = "swl";
13304       s2 = "swr";
13305       offbits = (mips_opts.micromips ? 12 : 16);
13306       off = 3;
13307       ust = 1;
13308       goto uld_st;
13309     case M_USD_AB:
13310       s = "sdl";
13311       s2 = "sdr";
13312       offbits = (mips_opts.micromips ? 12 : 16);
13313       off = 7;
13314       ust = 1;
13315 
13316     uld_st:
13317       breg = op[2];
13318       large_offset = !small_offset_p (off, align, offbits);
13319       ep = &offset_expr;
13320       expr1.X_add_number = 0;
13321       if (large_offset)
13322 	{
13323 	  used_at = 1;
13324 	  tempreg = AT;
13325 	  if (small_offset_p (0, align, 16))
13326 	    macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13327 			 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13328 	  else
13329 	    {
13330 	      load_address (tempreg, ep, &used_at);
13331 	      if (breg != 0)
13332 		macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13333 			     tempreg, tempreg, breg);
13334 	    }
13335 	  offset_reloc[0] = BFD_RELOC_LO16;
13336 	  offset_reloc[1] = BFD_RELOC_UNUSED;
13337 	  offset_reloc[2] = BFD_RELOC_UNUSED;
13338 	  breg = tempreg;
13339 	  tempreg = op[0];
13340 	  ep = &expr1;
13341 	}
13342       else if (!ust && op[0] == breg)
13343 	{
13344 	  used_at = 1;
13345 	  tempreg = AT;
13346 	}
13347       else
13348 	tempreg = op[0];
13349 
13350       if (off == 1)
13351 	goto ulh_sh;
13352 
13353       if (!target_big_endian)
13354 	ep->X_add_number += off;
13355       if (offbits == 12)
13356 	macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
13357       else
13358 	macro_build (ep, s, "t,o(b)", tempreg, -1,
13359 		     offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13360 
13361       if (!target_big_endian)
13362 	ep->X_add_number -= off;
13363       else
13364 	ep->X_add_number += off;
13365       if (offbits == 12)
13366 	macro_build (NULL, s2, "t,~(b)",
13367 		     tempreg, (int) ep->X_add_number, breg);
13368       else
13369 	macro_build (ep, s2, "t,o(b)", tempreg, -1,
13370 		     offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13371 
13372       /* If necessary, move the result in tempreg to the final destination.  */
13373       if (!ust && op[0] != tempreg)
13374         {
13375 	  /* Protect second load's delay slot.  */
13376 	  load_delay_nop ();
13377 	  move_register (op[0], tempreg);
13378 	}
13379       break;
13380 
13381     ulh_sh:
13382       used_at = 1;
13383       if (target_big_endian == ust)
13384 	ep->X_add_number += off;
13385       tempreg = ust || large_offset ? op[0] : AT;
13386       macro_build (ep, s, "t,o(b)", tempreg, -1,
13387 		   offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13388 
13389       /* For halfword transfers we need a temporary register to shuffle
13390          bytes.  Unfortunately for M_USH_A we have none available before
13391          the next store as AT holds the base address.  We deal with this
13392          case by clobbering TREG and then restoring it as with ULH.  */
13393       tempreg = ust == large_offset ? op[0] : AT;
13394       if (ust)
13395 	macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
13396 
13397       if (target_big_endian == ust)
13398 	ep->X_add_number -= off;
13399       else
13400 	ep->X_add_number += off;
13401       macro_build (ep, s2, "t,o(b)", tempreg, -1,
13402 		   offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13403 
13404       /* For M_USH_A re-retrieve the LSB.  */
13405       if (ust && large_offset)
13406 	{
13407 	  if (target_big_endian)
13408 	    ep->X_add_number += off;
13409 	  else
13410 	    ep->X_add_number -= off;
13411 	  macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13412 		       offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
13413 	}
13414       /* For ULH and M_USH_A OR the LSB in.  */
13415       if (!ust || large_offset)
13416 	{
13417 	  tempreg = !large_offset ? AT : op[0];
13418 	  macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
13419 	  macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13420 	}
13421       break;
13422 
13423     default:
13424       /* FIXME: Check if this is one of the itbl macros, since they
13425 	 are added dynamically.  */
13426       as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
13427       break;
13428     }
13429   if (!mips_opts.at && used_at)
13430     as_bad (_("macro used $at after \".set noat\""));
13431 }
13432 
13433 /* Implement macros in mips16 mode.  */
13434 
13435 static void
mips16_macro(struct mips_cl_insn * ip)13436 mips16_macro (struct mips_cl_insn *ip)
13437 {
13438   const struct mips_operand_array *operands;
13439   int mask;
13440   int tmp;
13441   expressionS expr1;
13442   int dbl;
13443   const char *s, *s2, *s3;
13444   unsigned int op[MAX_OPERANDS];
13445   unsigned int i;
13446 
13447   mask = ip->insn_mo->mask;
13448 
13449   operands = insn_operands (ip);
13450   for (i = 0; i < MAX_OPERANDS; i++)
13451     if (operands->operand[i])
13452       op[i] = insn_extract_operand (ip, operands->operand[i]);
13453     else
13454       op[i] = -1;
13455 
13456   expr1.X_op = O_constant;
13457   expr1.X_op_symbol = NULL;
13458   expr1.X_add_symbol = NULL;
13459   expr1.X_add_number = 1;
13460 
13461   dbl = 0;
13462 
13463   switch (mask)
13464     {
13465     default:
13466       abort ();
13467 
13468     case M_DDIV_3:
13469       dbl = 1;
13470     case M_DIV_3:
13471       s = "mflo";
13472       goto do_div3;
13473     case M_DREM_3:
13474       dbl = 1;
13475     case M_REM_3:
13476       s = "mfhi";
13477     do_div3:
13478       start_noreorder ();
13479       macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", op[1], op[2]);
13480       expr1.X_add_number = 2;
13481       macro_build (&expr1, "bnez", "x,p", op[2]);
13482       macro_build (NULL, "break", "6", 7);
13483 
13484       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13485          since that causes an overflow.  We should do that as well,
13486          but I don't see how to do the comparisons without a temporary
13487          register.  */
13488       end_noreorder ();
13489       macro_build (NULL, s, "x", op[0]);
13490       break;
13491 
13492     case M_DIVU_3:
13493       s = "divu";
13494       s2 = "mflo";
13495       goto do_divu3;
13496     case M_REMU_3:
13497       s = "divu";
13498       s2 = "mfhi";
13499       goto do_divu3;
13500     case M_DDIVU_3:
13501       s = "ddivu";
13502       s2 = "mflo";
13503       goto do_divu3;
13504     case M_DREMU_3:
13505       s = "ddivu";
13506       s2 = "mfhi";
13507     do_divu3:
13508       start_noreorder ();
13509       macro_build (NULL, s, "0,x,y", op[1], op[2]);
13510       expr1.X_add_number = 2;
13511       macro_build (&expr1, "bnez", "x,p", op[2]);
13512       macro_build (NULL, "break", "6", 7);
13513       end_noreorder ();
13514       macro_build (NULL, s2, "x", op[0]);
13515       break;
13516 
13517     case M_DMUL:
13518       dbl = 1;
13519     case M_MUL:
13520       macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13521       macro_build (NULL, "mflo", "x", op[0]);
13522       break;
13523 
13524     case M_DSUBU_I:
13525       dbl = 1;
13526       goto do_subu;
13527     case M_SUBU_I:
13528     do_subu:
13529       imm_expr.X_add_number = -imm_expr.X_add_number;
13530       macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", op[0], op[1]);
13531       break;
13532 
13533     case M_SUBU_I_2:
13534       imm_expr.X_add_number = -imm_expr.X_add_number;
13535       macro_build (&imm_expr, "addiu", "x,k", op[0]);
13536       break;
13537 
13538     case M_DSUBU_I_2:
13539       imm_expr.X_add_number = -imm_expr.X_add_number;
13540       macro_build (&imm_expr, "daddiu", "y,j", op[0]);
13541       break;
13542 
13543     case M_BEQ:
13544       s = "cmp";
13545       s2 = "bteqz";
13546       goto do_branch;
13547     case M_BNE:
13548       s = "cmp";
13549       s2 = "btnez";
13550       goto do_branch;
13551     case M_BLT:
13552       s = "slt";
13553       s2 = "btnez";
13554       goto do_branch;
13555     case M_BLTU:
13556       s = "sltu";
13557       s2 = "btnez";
13558       goto do_branch;
13559     case M_BLE:
13560       s = "slt";
13561       s2 = "bteqz";
13562       goto do_reverse_branch;
13563     case M_BLEU:
13564       s = "sltu";
13565       s2 = "bteqz";
13566       goto do_reverse_branch;
13567     case M_BGE:
13568       s = "slt";
13569       s2 = "bteqz";
13570       goto do_branch;
13571     case M_BGEU:
13572       s = "sltu";
13573       s2 = "bteqz";
13574       goto do_branch;
13575     case M_BGT:
13576       s = "slt";
13577       s2 = "btnez";
13578       goto do_reverse_branch;
13579     case M_BGTU:
13580       s = "sltu";
13581       s2 = "btnez";
13582 
13583     do_reverse_branch:
13584       tmp = op[1];
13585       op[1] = op[0];
13586       op[0] = tmp;
13587 
13588     do_branch:
13589       macro_build (NULL, s, "x,y", op[0], op[1]);
13590       macro_build (&offset_expr, s2, "p");
13591       break;
13592 
13593     case M_BEQ_I:
13594       s = "cmpi";
13595       s2 = "bteqz";
13596       s3 = "x,U";
13597       goto do_branch_i;
13598     case M_BNE_I:
13599       s = "cmpi";
13600       s2 = "btnez";
13601       s3 = "x,U";
13602       goto do_branch_i;
13603     case M_BLT_I:
13604       s = "slti";
13605       s2 = "btnez";
13606       s3 = "x,8";
13607       goto do_branch_i;
13608     case M_BLTU_I:
13609       s = "sltiu";
13610       s2 = "btnez";
13611       s3 = "x,8";
13612       goto do_branch_i;
13613     case M_BLE_I:
13614       s = "slti";
13615       s2 = "btnez";
13616       s3 = "x,8";
13617       goto do_addone_branch_i;
13618     case M_BLEU_I:
13619       s = "sltiu";
13620       s2 = "btnez";
13621       s3 = "x,8";
13622       goto do_addone_branch_i;
13623     case M_BGE_I:
13624       s = "slti";
13625       s2 = "bteqz";
13626       s3 = "x,8";
13627       goto do_branch_i;
13628     case M_BGEU_I:
13629       s = "sltiu";
13630       s2 = "bteqz";
13631       s3 = "x,8";
13632       goto do_branch_i;
13633     case M_BGT_I:
13634       s = "slti";
13635       s2 = "bteqz";
13636       s3 = "x,8";
13637       goto do_addone_branch_i;
13638     case M_BGTU_I:
13639       s = "sltiu";
13640       s2 = "bteqz";
13641       s3 = "x,8";
13642 
13643     do_addone_branch_i:
13644       ++imm_expr.X_add_number;
13645 
13646     do_branch_i:
13647       macro_build (&imm_expr, s, s3, op[0]);
13648       macro_build (&offset_expr, s2, "p");
13649       break;
13650 
13651     case M_ABS:
13652       expr1.X_add_number = 0;
13653       macro_build (&expr1, "slti", "x,8", op[1]);
13654       if (op[0] != op[1])
13655 	macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
13656       expr1.X_add_number = 2;
13657       macro_build (&expr1, "bteqz", "p");
13658       macro_build (NULL, "neg", "x,w", op[0], op[0]);
13659       break;
13660     }
13661 }
13662 
13663 /* Look up instruction [START, START + LENGTH) in HASH.  Record any extra
13664    opcode bits in *OPCODE_EXTRA.  */
13665 
13666 static struct mips_opcode *
mips_lookup_insn(struct hash_control * hash,const char * start,ssize_t length,unsigned int * opcode_extra)13667 mips_lookup_insn (struct hash_control *hash, const char *start,
13668 		  ssize_t length, unsigned int *opcode_extra)
13669 {
13670   char *name, *dot, *p;
13671   unsigned int mask, suffix;
13672   ssize_t opend;
13673   struct mips_opcode *insn;
13674 
13675   /* Make a copy of the instruction so that we can fiddle with it.  */
13676   name = alloca (length + 1);
13677   memcpy (name, start, length);
13678   name[length] = '\0';
13679 
13680   /* Look up the instruction as-is.  */
13681   insn = (struct mips_opcode *) hash_find (hash, name);
13682   if (insn)
13683     return insn;
13684 
13685   dot = strchr (name, '.');
13686   if (dot && dot[1])
13687     {
13688       /* Try to interpret the text after the dot as a VU0 channel suffix.  */
13689       p = mips_parse_vu0_channels (dot + 1, &mask);
13690       if (*p == 0 && mask != 0)
13691 	{
13692 	  *dot = 0;
13693 	  insn = (struct mips_opcode *) hash_find (hash, name);
13694 	  *dot = '.';
13695 	  if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13696 	    {
13697 	      *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
13698 	      return insn;
13699 	    }
13700 	}
13701     }
13702 
13703   if (mips_opts.micromips)
13704     {
13705       /* See if there's an instruction size override suffix,
13706 	 either `16' or `32', at the end of the mnemonic proper,
13707 	 that defines the operation, i.e. before the first `.'
13708 	 character if any.  Strip it and retry.  */
13709       opend = dot != NULL ? dot - name : length;
13710       if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13711 	suffix = 2;
13712       else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13713 	suffix = 4;
13714       else
13715 	suffix = 0;
13716       if (suffix)
13717 	{
13718 	  memcpy (name + opend - 2, name + opend, length - opend + 1);
13719 	  insn = (struct mips_opcode *) hash_find (hash, name);
13720 	  if (insn)
13721 	    {
13722 	      forced_insn_length = suffix;
13723 	      return insn;
13724 	    }
13725 	}
13726     }
13727 
13728   return NULL;
13729 }
13730 
13731 /* Assemble an instruction into its binary format.  If the instruction
13732    is a macro, set imm_expr and offset_expr to the values associated
13733    with "I" and "A" operands respectively.  Otherwise store the value
13734    of the relocatable field (if any) in offset_expr.  In both cases
13735    set offset_reloc to the relocation operators applied to offset_expr.  */
13736 
13737 static void
mips_ip(char * str,struct mips_cl_insn * insn)13738 mips_ip (char *str, struct mips_cl_insn *insn)
13739 {
13740   const struct mips_opcode *first, *past;
13741   struct hash_control *hash;
13742   char format;
13743   size_t end;
13744   struct mips_operand_token *tokens;
13745   unsigned int opcode_extra;
13746 
13747   if (mips_opts.micromips)
13748     {
13749       hash = micromips_op_hash;
13750       past = &micromips_opcodes[bfd_micromips_num_opcodes];
13751     }
13752   else
13753     {
13754       hash = op_hash;
13755       past = &mips_opcodes[NUMOPCODES];
13756     }
13757   forced_insn_length = 0;
13758   opcode_extra = 0;
13759 
13760   /* We first try to match an instruction up to a space or to the end.  */
13761   for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
13762     continue;
13763 
13764   first = mips_lookup_insn (hash, str, end, &opcode_extra);
13765   if (first == NULL)
13766     {
13767       set_insn_error (0, _("unrecognized opcode"));
13768       return;
13769     }
13770 
13771   if (strcmp (first->name, "li.s") == 0)
13772     format = 'f';
13773   else if (strcmp (first->name, "li.d") == 0)
13774     format = 'd';
13775   else
13776     format = 0;
13777   tokens = mips_parse_arguments (str + end, format);
13778   if (!tokens)
13779     return;
13780 
13781   if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
13782       && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
13783     set_insn_error (0, _("invalid operands"));
13784 
13785   obstack_free (&mips_operand_tokens, tokens);
13786 }
13787 
13788 /* As for mips_ip, but used when assembling MIPS16 code.
13789    Also set forced_insn_length to the resulting instruction size in
13790    bytes if the user explicitly requested a small or extended instruction.  */
13791 
13792 static void
mips16_ip(char * str,struct mips_cl_insn * insn)13793 mips16_ip (char *str, struct mips_cl_insn *insn)
13794 {
13795   char *end, *s, c;
13796   struct mips_opcode *first;
13797   struct mips_operand_token *tokens;
13798 
13799   forced_insn_length = 0;
13800 
13801   for (s = str; ISLOWER (*s); ++s)
13802     ;
13803   end = s;
13804   c = *end;
13805   switch (c)
13806     {
13807     case '\0':
13808       break;
13809 
13810     case ' ':
13811       s++;
13812       break;
13813 
13814     case '.':
13815       if (s[1] == 't' && s[2] == ' ')
13816 	{
13817 	  forced_insn_length = 2;
13818 	  s += 3;
13819 	  break;
13820 	}
13821       else if (s[1] == 'e' && s[2] == ' ')
13822 	{
13823 	  forced_insn_length = 4;
13824 	  s += 3;
13825 	  break;
13826 	}
13827       /* Fall through.  */
13828     default:
13829       set_insn_error (0, _("unrecognized opcode"));
13830       return;
13831     }
13832 
13833   if (mips_opts.noautoextend && !forced_insn_length)
13834     forced_insn_length = 2;
13835 
13836   *end = 0;
13837   first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
13838   *end = c;
13839 
13840   if (!first)
13841     {
13842       set_insn_error (0, _("unrecognized opcode"));
13843       return;
13844     }
13845 
13846   tokens = mips_parse_arguments (s, 0);
13847   if (!tokens)
13848     return;
13849 
13850   if (!match_mips16_insns (insn, first, tokens))
13851     set_insn_error (0, _("invalid operands"));
13852 
13853   obstack_free (&mips_operand_tokens, tokens);
13854 }
13855 
13856 /* Marshal immediate value VAL for an extended MIPS16 instruction.
13857    NBITS is the number of significant bits in VAL.  */
13858 
13859 static unsigned long
mips16_immed_extend(offsetT val,unsigned int nbits)13860 mips16_immed_extend (offsetT val, unsigned int nbits)
13861 {
13862   int extval;
13863   if (nbits == 16)
13864     {
13865       extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
13866       val &= 0x1f;
13867     }
13868   else if (nbits == 15)
13869     {
13870       extval = ((val >> 11) & 0xf) | (val & 0x7f0);
13871       val &= 0xf;
13872     }
13873   else
13874     {
13875       extval = ((val & 0x1f) << 6) | (val & 0x20);
13876       val = 0;
13877     }
13878   return (extval << 16) | val;
13879 }
13880 
13881 /* Like decode_mips16_operand, but require the operand to be defined and
13882    require it to be an integer.  */
13883 
13884 static const struct mips_int_operand *
mips16_immed_operand(int type,bfd_boolean extended_p)13885 mips16_immed_operand (int type, bfd_boolean extended_p)
13886 {
13887   const struct mips_operand *operand;
13888 
13889   operand = decode_mips16_operand (type, extended_p);
13890   if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
13891     abort ();
13892   return (const struct mips_int_operand *) operand;
13893 }
13894 
13895 /* Return true if SVAL fits OPERAND.  RELOC is as for mips16_immed.  */
13896 
13897 static bfd_boolean
mips16_immed_in_range_p(const struct mips_int_operand * operand,bfd_reloc_code_real_type reloc,offsetT sval)13898 mips16_immed_in_range_p (const struct mips_int_operand *operand,
13899 			 bfd_reloc_code_real_type reloc, offsetT sval)
13900 {
13901   int min_val, max_val;
13902 
13903   min_val = mips_int_operand_min (operand);
13904   max_val = mips_int_operand_max (operand);
13905   if (reloc != BFD_RELOC_UNUSED)
13906     {
13907       if (min_val < 0)
13908 	sval = SEXT_16BIT (sval);
13909       else
13910 	sval &= 0xffff;
13911     }
13912 
13913   return (sval >= min_val
13914 	  && sval <= max_val
13915 	  && (sval & ((1 << operand->shift) - 1)) == 0);
13916 }
13917 
13918 /* Install immediate value VAL into MIPS16 instruction *INSN,
13919    extending it if necessary.  The instruction in *INSN may
13920    already be extended.
13921 
13922    RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
13923    if none.  In the former case, VAL is a 16-bit number with no
13924    defined signedness.
13925 
13926    TYPE is the type of the immediate field.  USER_INSN_LENGTH
13927    is the length that the user requested, or 0 if none.  */
13928 
13929 static void
mips16_immed(char * file,unsigned int line,int type,bfd_reloc_code_real_type reloc,offsetT val,unsigned int user_insn_length,unsigned long * insn)13930 mips16_immed (char *file, unsigned int line, int type,
13931 	      bfd_reloc_code_real_type reloc, offsetT val,
13932 	      unsigned int user_insn_length, unsigned long *insn)
13933 {
13934   const struct mips_int_operand *operand;
13935   unsigned int uval, length;
13936 
13937   operand = mips16_immed_operand (type, FALSE);
13938   if (!mips16_immed_in_range_p (operand, reloc, val))
13939     {
13940       /* We need an extended instruction.  */
13941       if (user_insn_length == 2)
13942 	as_bad_where (file, line, _("invalid unextended operand value"));
13943       else
13944 	*insn |= MIPS16_EXTEND;
13945     }
13946   else if (user_insn_length == 4)
13947     {
13948       /* The operand doesn't force an unextended instruction to be extended.
13949 	 Warn if the user wanted an extended instruction anyway.  */
13950       *insn |= MIPS16_EXTEND;
13951       as_warn_where (file, line,
13952 		     _("extended operand requested but not required"));
13953     }
13954 
13955   length = mips16_opcode_length (*insn);
13956   if (length == 4)
13957     {
13958       operand = mips16_immed_operand (type, TRUE);
13959       if (!mips16_immed_in_range_p (operand, reloc, val))
13960 	as_bad_where (file, line,
13961 		      _("operand value out of range for instruction"));
13962     }
13963   uval = ((unsigned int) val >> operand->shift) - operand->bias;
13964   if (length == 2)
13965     *insn = mips_insert_operand (&operand->root, *insn, uval);
13966   else
13967     *insn |= mips16_immed_extend (uval, operand->root.size);
13968 }
13969 
13970 struct percent_op_match
13971 {
13972   const char *str;
13973   bfd_reloc_code_real_type reloc;
13974 };
13975 
13976 static const struct percent_op_match mips_percent_op[] =
13977 {
13978   {"%lo", BFD_RELOC_LO16},
13979   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
13980   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
13981   {"%call16", BFD_RELOC_MIPS_CALL16},
13982   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
13983   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
13984   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
13985   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
13986   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
13987   {"%got", BFD_RELOC_MIPS_GOT16},
13988   {"%gp_rel", BFD_RELOC_GPREL16},
13989   {"%half", BFD_RELOC_16},
13990   {"%highest", BFD_RELOC_MIPS_HIGHEST},
13991   {"%higher", BFD_RELOC_MIPS_HIGHER},
13992   {"%neg", BFD_RELOC_MIPS_SUB},
13993   {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
13994   {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
13995   {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
13996   {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
13997   {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
13998   {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
13999   {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14000   {"%hi", BFD_RELOC_HI16_S},
14001   {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14002   {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
14003 };
14004 
14005 static const struct percent_op_match mips16_percent_op[] =
14006 {
14007   {"%lo", BFD_RELOC_MIPS16_LO16},
14008   {"%gprel", BFD_RELOC_MIPS16_GPREL},
14009   {"%got", BFD_RELOC_MIPS16_GOT16},
14010   {"%call16", BFD_RELOC_MIPS16_CALL16},
14011   {"%hi", BFD_RELOC_MIPS16_HI16_S},
14012   {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14013   {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14014   {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14015   {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14016   {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14017   {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14018   {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14019 };
14020 
14021 
14022 /* Return true if *STR points to a relocation operator.  When returning true,
14023    move *STR over the operator and store its relocation code in *RELOC.
14024    Leave both *STR and *RELOC alone when returning false.  */
14025 
14026 static bfd_boolean
parse_relocation(char ** str,bfd_reloc_code_real_type * reloc)14027 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14028 {
14029   const struct percent_op_match *percent_op;
14030   size_t limit, i;
14031 
14032   if (mips_opts.mips16)
14033     {
14034       percent_op = mips16_percent_op;
14035       limit = ARRAY_SIZE (mips16_percent_op);
14036     }
14037   else
14038     {
14039       percent_op = mips_percent_op;
14040       limit = ARRAY_SIZE (mips_percent_op);
14041     }
14042 
14043   for (i = 0; i < limit; i++)
14044     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14045       {
14046 	int len = strlen (percent_op[i].str);
14047 
14048 	if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14049 	  continue;
14050 
14051 	*str += strlen (percent_op[i].str);
14052 	*reloc = percent_op[i].reloc;
14053 
14054 	/* Check whether the output BFD supports this relocation.
14055 	   If not, issue an error and fall back on something safe.  */
14056 	if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14057 	  {
14058 	    as_bad (_("relocation %s isn't supported by the current ABI"),
14059 		    percent_op[i].str);
14060 	    *reloc = BFD_RELOC_UNUSED;
14061 	  }
14062 	return TRUE;
14063       }
14064   return FALSE;
14065 }
14066 
14067 
14068 /* Parse string STR as a 16-bit relocatable operand.  Store the
14069    expression in *EP and the relocations in the array starting
14070    at RELOC.  Return the number of relocation operators used.
14071 
14072    On exit, EXPR_END points to the first character after the expression.  */
14073 
14074 static size_t
my_getSmallExpression(expressionS * ep,bfd_reloc_code_real_type * reloc,char * str)14075 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14076 		       char *str)
14077 {
14078   bfd_reloc_code_real_type reversed_reloc[3];
14079   size_t reloc_index, i;
14080   int crux_depth, str_depth;
14081   char *crux;
14082 
14083   /* Search for the start of the main expression, recoding relocations
14084      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
14085      of the main expression and with CRUX_DEPTH containing the number
14086      of open brackets at that point.  */
14087   reloc_index = -1;
14088   str_depth = 0;
14089   do
14090     {
14091       reloc_index++;
14092       crux = str;
14093       crux_depth = str_depth;
14094 
14095       /* Skip over whitespace and brackets, keeping count of the number
14096 	 of brackets.  */
14097       while (*str == ' ' || *str == '\t' || *str == '(')
14098 	if (*str++ == '(')
14099 	  str_depth++;
14100     }
14101   while (*str == '%'
14102 	 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14103 	 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14104 
14105   my_getExpression (ep, crux);
14106   str = expr_end;
14107 
14108   /* Match every open bracket.  */
14109   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14110     if (*str++ == ')')
14111       crux_depth--;
14112 
14113   if (crux_depth > 0)
14114     as_bad (_("unclosed '('"));
14115 
14116   expr_end = str;
14117 
14118   if (reloc_index != 0)
14119     {
14120       prev_reloc_op_frag = frag_now;
14121       for (i = 0; i < reloc_index; i++)
14122 	reloc[i] = reversed_reloc[reloc_index - 1 - i];
14123     }
14124 
14125   return reloc_index;
14126 }
14127 
14128 static void
my_getExpression(expressionS * ep,char * str)14129 my_getExpression (expressionS *ep, char *str)
14130 {
14131   char *save_in;
14132 
14133   save_in = input_line_pointer;
14134   input_line_pointer = str;
14135   expression (ep);
14136   expr_end = input_line_pointer;
14137   input_line_pointer = save_in;
14138 }
14139 
14140 char *
md_atof(int type,char * litP,int * sizeP)14141 md_atof (int type, char *litP, int *sizeP)
14142 {
14143   return ieee_md_atof (type, litP, sizeP, target_big_endian);
14144 }
14145 
14146 void
md_number_to_chars(char * buf,valueT val,int n)14147 md_number_to_chars (char *buf, valueT val, int n)
14148 {
14149   if (target_big_endian)
14150     number_to_chars_bigendian (buf, val, n);
14151   else
14152     number_to_chars_littleendian (buf, val, n);
14153 }
14154 
support_64bit_objects(void)14155 static int support_64bit_objects(void)
14156 {
14157   const char **list, **l;
14158   int yes;
14159 
14160   list = bfd_target_list ();
14161   for (l = list; *l != NULL; l++)
14162     if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14163 	|| strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14164       break;
14165   yes = (*l != NULL);
14166   free (list);
14167   return yes;
14168 }
14169 
14170 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14171    NEW_VALUE.  Warn if another value was already specified.  Note:
14172    we have to defer parsing the -march and -mtune arguments in order
14173    to handle 'from-abi' correctly, since the ABI might be specified
14174    in a later argument.  */
14175 
14176 static void
mips_set_option_string(const char ** string_ptr,const char * new_value)14177 mips_set_option_string (const char **string_ptr, const char *new_value)
14178 {
14179   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14180     as_warn (_("a different %s was already specified, is now %s"),
14181 	     string_ptr == &mips_arch_string ? "-march" : "-mtune",
14182 	     new_value);
14183 
14184   *string_ptr = new_value;
14185 }
14186 
14187 int
md_parse_option(int c,char * arg)14188 md_parse_option (int c, char *arg)
14189 {
14190   unsigned int i;
14191 
14192   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14193     if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14194       {
14195 	file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
14196 					   c == mips_ases[i].option_on);
14197 	return 1;
14198       }
14199 
14200   switch (c)
14201     {
14202     case OPTION_CONSTRUCT_FLOATS:
14203       mips_disable_float_construction = 0;
14204       break;
14205 
14206     case OPTION_NO_CONSTRUCT_FLOATS:
14207       mips_disable_float_construction = 1;
14208       break;
14209 
14210     case OPTION_TRAP:
14211       mips_trap = 1;
14212       break;
14213 
14214     case OPTION_BREAK:
14215       mips_trap = 0;
14216       break;
14217 
14218     case OPTION_EB:
14219       target_big_endian = 1;
14220       break;
14221 
14222     case OPTION_EL:
14223       target_big_endian = 0;
14224       break;
14225 
14226     case 'O':
14227       if (arg == NULL)
14228 	mips_optimize = 1;
14229       else if (arg[0] == '0')
14230 	mips_optimize = 0;
14231       else if (arg[0] == '1')
14232 	mips_optimize = 1;
14233       else
14234 	mips_optimize = 2;
14235       break;
14236 
14237     case 'g':
14238       if (arg == NULL)
14239 	mips_debug = 2;
14240       else
14241 	mips_debug = atoi (arg);
14242       break;
14243 
14244     case OPTION_MIPS1:
14245       file_mips_opts.isa = ISA_MIPS1;
14246       break;
14247 
14248     case OPTION_MIPS2:
14249       file_mips_opts.isa = ISA_MIPS2;
14250       break;
14251 
14252     case OPTION_MIPS3:
14253       file_mips_opts.isa = ISA_MIPS3;
14254       break;
14255 
14256     case OPTION_MIPS4:
14257       file_mips_opts.isa = ISA_MIPS4;
14258       break;
14259 
14260     case OPTION_MIPS5:
14261       file_mips_opts.isa = ISA_MIPS5;
14262       break;
14263 
14264     case OPTION_MIPS32:
14265       file_mips_opts.isa = ISA_MIPS32;
14266       break;
14267 
14268     case OPTION_MIPS32R2:
14269       file_mips_opts.isa = ISA_MIPS32R2;
14270       break;
14271 
14272     case OPTION_MIPS32R3:
14273       file_mips_opts.isa = ISA_MIPS32R3;
14274       break;
14275 
14276     case OPTION_MIPS32R5:
14277       file_mips_opts.isa = ISA_MIPS32R5;
14278       break;
14279 
14280     case OPTION_MIPS32R6:
14281       file_mips_opts.isa = ISA_MIPS32R6;
14282       break;
14283 
14284     case OPTION_MIPS64R2:
14285       file_mips_opts.isa = ISA_MIPS64R2;
14286       break;
14287 
14288     case OPTION_MIPS64R3:
14289       file_mips_opts.isa = ISA_MIPS64R3;
14290       break;
14291 
14292     case OPTION_MIPS64R5:
14293       file_mips_opts.isa = ISA_MIPS64R5;
14294       break;
14295 
14296     case OPTION_MIPS64R6:
14297       file_mips_opts.isa = ISA_MIPS64R6;
14298       break;
14299 
14300     case OPTION_MIPS64:
14301       file_mips_opts.isa = ISA_MIPS64;
14302       break;
14303 
14304     case OPTION_MTUNE:
14305       mips_set_option_string (&mips_tune_string, arg);
14306       break;
14307 
14308     case OPTION_MARCH:
14309       mips_set_option_string (&mips_arch_string, arg);
14310       break;
14311 
14312     case OPTION_M4650:
14313       mips_set_option_string (&mips_arch_string, "4650");
14314       mips_set_option_string (&mips_tune_string, "4650");
14315       break;
14316 
14317     case OPTION_NO_M4650:
14318       break;
14319 
14320     case OPTION_M4010:
14321       mips_set_option_string (&mips_arch_string, "4010");
14322       mips_set_option_string (&mips_tune_string, "4010");
14323       break;
14324 
14325     case OPTION_NO_M4010:
14326       break;
14327 
14328     case OPTION_M4100:
14329       mips_set_option_string (&mips_arch_string, "4100");
14330       mips_set_option_string (&mips_tune_string, "4100");
14331       break;
14332 
14333     case OPTION_NO_M4100:
14334       break;
14335 
14336     case OPTION_M3900:
14337       mips_set_option_string (&mips_arch_string, "3900");
14338       mips_set_option_string (&mips_tune_string, "3900");
14339       break;
14340 
14341     case OPTION_NO_M3900:
14342       break;
14343 
14344     case OPTION_MICROMIPS:
14345       if (file_mips_opts.mips16 == 1)
14346 	{
14347 	  as_bad (_("-mmicromips cannot be used with -mips16"));
14348 	  return 0;
14349 	}
14350       file_mips_opts.micromips = 1;
14351       mips_no_prev_insn ();
14352       break;
14353 
14354     case OPTION_NO_MICROMIPS:
14355       file_mips_opts.micromips = 0;
14356       mips_no_prev_insn ();
14357       break;
14358 
14359     case OPTION_MIPS16:
14360       if (file_mips_opts.micromips == 1)
14361 	{
14362 	  as_bad (_("-mips16 cannot be used with -micromips"));
14363 	  return 0;
14364 	}
14365       file_mips_opts.mips16 = 1;
14366       mips_no_prev_insn ();
14367       break;
14368 
14369     case OPTION_NO_MIPS16:
14370       file_mips_opts.mips16 = 0;
14371       mips_no_prev_insn ();
14372       break;
14373 
14374     case OPTION_FIX_24K:
14375       mips_fix_24k = 1;
14376       break;
14377 
14378     case OPTION_NO_FIX_24K:
14379       mips_fix_24k = 0;
14380       break;
14381 
14382     case OPTION_FIX_RM7000:
14383       mips_fix_rm7000 = 1;
14384       break;
14385 
14386     case OPTION_NO_FIX_RM7000:
14387       mips_fix_rm7000 = 0;
14388       break;
14389 
14390     case OPTION_FIX_LOONGSON2F_JUMP:
14391       mips_fix_loongson2f_jump = TRUE;
14392       break;
14393 
14394     case OPTION_NO_FIX_LOONGSON2F_JUMP:
14395       mips_fix_loongson2f_jump = FALSE;
14396       break;
14397 
14398     case OPTION_FIX_LOONGSON2F_NOP:
14399       mips_fix_loongson2f_nop = TRUE;
14400       break;
14401 
14402     case OPTION_NO_FIX_LOONGSON2F_NOP:
14403       mips_fix_loongson2f_nop = FALSE;
14404       break;
14405 
14406     case OPTION_FIX_VR4120:
14407       mips_fix_vr4120 = 1;
14408       break;
14409 
14410     case OPTION_NO_FIX_VR4120:
14411       mips_fix_vr4120 = 0;
14412       break;
14413 
14414     case OPTION_FIX_VR4130:
14415       mips_fix_vr4130 = 1;
14416       break;
14417 
14418     case OPTION_NO_FIX_VR4130:
14419       mips_fix_vr4130 = 0;
14420       break;
14421 
14422     case OPTION_FIX_CN63XXP1:
14423       mips_fix_cn63xxp1 = TRUE;
14424       break;
14425 
14426     case OPTION_NO_FIX_CN63XXP1:
14427       mips_fix_cn63xxp1 = FALSE;
14428       break;
14429 
14430     case OPTION_RELAX_BRANCH:
14431       mips_relax_branch = 1;
14432       break;
14433 
14434     case OPTION_NO_RELAX_BRANCH:
14435       mips_relax_branch = 0;
14436       break;
14437 
14438     case OPTION_INSN32:
14439       file_mips_opts.insn32 = TRUE;
14440       break;
14441 
14442     case OPTION_NO_INSN32:
14443       file_mips_opts.insn32 = FALSE;
14444       break;
14445 
14446     case OPTION_MSHARED:
14447       mips_in_shared = TRUE;
14448       break;
14449 
14450     case OPTION_MNO_SHARED:
14451       mips_in_shared = FALSE;
14452       break;
14453 
14454     case OPTION_MSYM32:
14455       file_mips_opts.sym32 = TRUE;
14456       break;
14457 
14458     case OPTION_MNO_SYM32:
14459       file_mips_opts.sym32 = FALSE;
14460       break;
14461 
14462       /* When generating ELF code, we permit -KPIC and -call_shared to
14463 	 select SVR4_PIC, and -non_shared to select no PIC.  This is
14464 	 intended to be compatible with Irix 5.  */
14465     case OPTION_CALL_SHARED:
14466       mips_pic = SVR4_PIC;
14467       mips_abicalls = TRUE;
14468       break;
14469 
14470     case OPTION_CALL_NONPIC:
14471       mips_pic = NO_PIC;
14472       mips_abicalls = TRUE;
14473       break;
14474 
14475     case OPTION_NON_SHARED:
14476       mips_pic = NO_PIC;
14477       mips_abicalls = FALSE;
14478       break;
14479 
14480       /* The -xgot option tells the assembler to use 32 bit offsets
14481          when accessing the got in SVR4_PIC mode.  It is for Irix
14482          compatibility.  */
14483     case OPTION_XGOT:
14484       mips_big_got = 1;
14485       break;
14486 
14487     case 'G':
14488       g_switch_value = atoi (arg);
14489       g_switch_seen = 1;
14490       break;
14491 
14492       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14493 	 and -mabi=64.  */
14494     case OPTION_32:
14495       mips_abi = O32_ABI;
14496       break;
14497 
14498     case OPTION_N32:
14499       mips_abi = N32_ABI;
14500       break;
14501 
14502     case OPTION_64:
14503       mips_abi = N64_ABI;
14504       if (!support_64bit_objects())
14505 	as_fatal (_("no compiled in support for 64 bit object file format"));
14506       break;
14507 
14508     case OPTION_GP32:
14509       file_mips_opts.gp = 32;
14510       break;
14511 
14512     case OPTION_GP64:
14513       file_mips_opts.gp = 64;
14514       break;
14515 
14516     case OPTION_FP32:
14517       file_mips_opts.fp = 32;
14518       break;
14519 
14520     case OPTION_FPXX:
14521       file_mips_opts.fp = 0;
14522       break;
14523 
14524     case OPTION_FP64:
14525       file_mips_opts.fp = 64;
14526       break;
14527 
14528     case OPTION_ODD_SPREG:
14529       file_mips_opts.oddspreg = 1;
14530       break;
14531 
14532     case OPTION_NO_ODD_SPREG:
14533       file_mips_opts.oddspreg = 0;
14534       break;
14535 
14536     case OPTION_SINGLE_FLOAT:
14537       file_mips_opts.single_float = 1;
14538       break;
14539 
14540     case OPTION_DOUBLE_FLOAT:
14541       file_mips_opts.single_float = 0;
14542       break;
14543 
14544     case OPTION_SOFT_FLOAT:
14545       file_mips_opts.soft_float = 1;
14546       break;
14547 
14548     case OPTION_HARD_FLOAT:
14549       file_mips_opts.soft_float = 0;
14550       break;
14551 
14552     case OPTION_MABI:
14553       if (strcmp (arg, "32") == 0)
14554 	mips_abi = O32_ABI;
14555       else if (strcmp (arg, "o64") == 0)
14556 	mips_abi = O64_ABI;
14557       else if (strcmp (arg, "n32") == 0)
14558 	mips_abi = N32_ABI;
14559       else if (strcmp (arg, "64") == 0)
14560 	{
14561 	  mips_abi = N64_ABI;
14562 	  if (! support_64bit_objects())
14563 	    as_fatal (_("no compiled in support for 64 bit object file "
14564 			"format"));
14565 	}
14566       else if (strcmp (arg, "eabi") == 0)
14567 	mips_abi = EABI_ABI;
14568       else
14569 	{
14570 	  as_fatal (_("invalid abi -mabi=%s"), arg);
14571 	  return 0;
14572 	}
14573       break;
14574 
14575     case OPTION_M7000_HILO_FIX:
14576       mips_7000_hilo_fix = TRUE;
14577       break;
14578 
14579     case OPTION_MNO_7000_HILO_FIX:
14580       mips_7000_hilo_fix = FALSE;
14581       break;
14582 
14583     case OPTION_MDEBUG:
14584       mips_flag_mdebug = TRUE;
14585       break;
14586 
14587     case OPTION_NO_MDEBUG:
14588       mips_flag_mdebug = FALSE;
14589       break;
14590 
14591     case OPTION_PDR:
14592       mips_flag_pdr = TRUE;
14593       break;
14594 
14595     case OPTION_NO_PDR:
14596       mips_flag_pdr = FALSE;
14597       break;
14598 
14599     case OPTION_MVXWORKS_PIC:
14600       mips_pic = VXWORKS_PIC;
14601       break;
14602 
14603     case OPTION_NAN:
14604       if (strcmp (arg, "2008") == 0)
14605 	mips_nan2008 = 1;
14606       else if (strcmp (arg, "legacy") == 0)
14607 	mips_nan2008 = 0;
14608       else
14609 	{
14610 	  as_fatal (_("invalid NaN setting -mnan=%s"), arg);
14611 	  return 0;
14612 	}
14613       break;
14614 
14615     default:
14616       return 0;
14617     }
14618 
14619     mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14620 
14621   return 1;
14622 }
14623 
14624 /* Set up globals to tune for the ISA or processor described by INFO.  */
14625 
14626 static void
mips_set_tune(const struct mips_cpu_info * info)14627 mips_set_tune (const struct mips_cpu_info *info)
14628 {
14629   if (info != 0)
14630     mips_tune = info->cpu;
14631 }
14632 
14633 
14634 void
mips_after_parse_args(void)14635 mips_after_parse_args (void)
14636 {
14637   const struct mips_cpu_info *arch_info = 0;
14638   const struct mips_cpu_info *tune_info = 0;
14639 
14640   /* GP relative stuff not working for PE */
14641   if (strncmp (TARGET_OS, "pe", 2) == 0)
14642     {
14643       if (g_switch_seen && g_switch_value != 0)
14644 	as_bad (_("-G not supported in this configuration"));
14645       g_switch_value = 0;
14646     }
14647 
14648   if (mips_abi == NO_ABI)
14649     mips_abi = MIPS_DEFAULT_ABI;
14650 
14651   /* The following code determines the architecture.
14652      Similar code was added to GCC 3.3 (see override_options() in
14653      config/mips/mips.c).  The GAS and GCC code should be kept in sync
14654      as much as possible.  */
14655 
14656   if (mips_arch_string != 0)
14657     arch_info = mips_parse_cpu ("-march", mips_arch_string);
14658 
14659   if (file_mips_opts.isa != ISA_UNKNOWN)
14660     {
14661       /* Handle -mipsN.  At this point, file_mips_opts.isa contains the
14662 	 ISA level specified by -mipsN, while arch_info->isa contains
14663 	 the -march selection (if any).  */
14664       if (arch_info != 0)
14665 	{
14666 	  /* -march takes precedence over -mipsN, since it is more descriptive.
14667 	     There's no harm in specifying both as long as the ISA levels
14668 	     are the same.  */
14669 	  if (file_mips_opts.isa != arch_info->isa)
14670 	    as_bad (_("-%s conflicts with the other architecture options,"
14671 		      " which imply -%s"),
14672 		    mips_cpu_info_from_isa (file_mips_opts.isa)->name,
14673 		    mips_cpu_info_from_isa (arch_info->isa)->name);
14674 	}
14675       else
14676 	arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
14677     }
14678 
14679   if (arch_info == 0)
14680     {
14681       arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
14682       gas_assert (arch_info);
14683     }
14684 
14685   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
14686     as_bad (_("-march=%s is not compatible with the selected ABI"),
14687 	    arch_info->name);
14688 
14689   file_mips_opts.arch = arch_info->cpu;
14690   file_mips_opts.isa = arch_info->isa;
14691 
14692   /* Set up initial mips_opts state.  */
14693   mips_opts = file_mips_opts;
14694 
14695   /* The register size inference code is now placed in
14696      file_mips_check_options.  */
14697 
14698   /* Optimize for file_mips_opts.arch, unless -mtune selects a different
14699      processor.  */
14700   if (mips_tune_string != 0)
14701     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
14702 
14703   if (tune_info == 0)
14704     mips_set_tune (arch_info);
14705   else
14706     mips_set_tune (tune_info);
14707 
14708   if (mips_flag_mdebug < 0)
14709     mips_flag_mdebug = 0;
14710 }
14711 
14712 void
mips_init_after_args(void)14713 mips_init_after_args (void)
14714 {
14715   /* initialize opcodes */
14716   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
14717   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
14718 }
14719 
14720 long
md_pcrel_from(fixS * fixP)14721 md_pcrel_from (fixS *fixP)
14722 {
14723   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14724   switch (fixP->fx_r_type)
14725     {
14726     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14727     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14728       /* Return the address of the delay slot.  */
14729       return addr + 2;
14730 
14731     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14732     case BFD_RELOC_MICROMIPS_JMP:
14733     case BFD_RELOC_16_PCREL_S2:
14734     case BFD_RELOC_MIPS_21_PCREL_S2:
14735     case BFD_RELOC_MIPS_26_PCREL_S2:
14736     case BFD_RELOC_MIPS_JMP:
14737       /* Return the address of the delay slot.  */
14738       return addr + 4;
14739 
14740     default:
14741       return addr;
14742     }
14743 }
14744 
14745 /* This is called before the symbol table is processed.  In order to
14746    work with gcc when using mips-tfile, we must keep all local labels.
14747    However, in other cases, we want to discard them.  If we were
14748    called with -g, but we didn't see any debugging information, it may
14749    mean that gcc is smuggling debugging information through to
14750    mips-tfile, in which case we must generate all local labels.  */
14751 
14752 void
mips_frob_file_before_adjust(void)14753 mips_frob_file_before_adjust (void)
14754 {
14755 #ifndef NO_ECOFF_DEBUGGING
14756   if (ECOFF_DEBUGGING
14757       && mips_debug != 0
14758       && ! ecoff_debugging_seen)
14759     flag_keep_locals = 1;
14760 #endif
14761 }
14762 
14763 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
14764    the corresponding LO16 reloc.  This is called before md_apply_fix and
14765    tc_gen_reloc.  Unmatched relocs can only be generated by use of explicit
14766    relocation operators.
14767 
14768    For our purposes, a %lo() expression matches a %got() or %hi()
14769    expression if:
14770 
14771       (a) it refers to the same symbol; and
14772       (b) the offset applied in the %lo() expression is no lower than
14773 	  the offset applied in the %got() or %hi().
14774 
14775    (b) allows us to cope with code like:
14776 
14777 	lui	$4,%hi(foo)
14778 	lh	$4,%lo(foo+2)($4)
14779 
14780    ...which is legal on RELA targets, and has a well-defined behaviour
14781    if the user knows that adding 2 to "foo" will not induce a carry to
14782    the high 16 bits.
14783 
14784    When several %lo()s match a particular %got() or %hi(), we use the
14785    following rules to distinguish them:
14786 
14787      (1) %lo()s with smaller offsets are a better match than %lo()s with
14788          higher offsets.
14789 
14790      (2) %lo()s with no matching %got() or %hi() are better than those
14791          that already have a matching %got() or %hi().
14792 
14793      (3) later %lo()s are better than earlier %lo()s.
14794 
14795    These rules are applied in order.
14796 
14797    (1) means, among other things, that %lo()s with identical offsets are
14798    chosen if they exist.
14799 
14800    (2) means that we won't associate several high-part relocations with
14801    the same low-part relocation unless there's no alternative.  Having
14802    several high parts for the same low part is a GNU extension; this rule
14803    allows careful users to avoid it.
14804 
14805    (3) is purely cosmetic.  mips_hi_fixup_list is is in reverse order,
14806    with the last high-part relocation being at the front of the list.
14807    It therefore makes sense to choose the last matching low-part
14808    relocation, all other things being equal.  It's also easier
14809    to code that way.  */
14810 
14811 void
mips_frob_file(void)14812 mips_frob_file (void)
14813 {
14814   struct mips_hi_fixup *l;
14815   bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
14816 
14817   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
14818     {
14819       segment_info_type *seginfo;
14820       bfd_boolean matched_lo_p;
14821       fixS **hi_pos, **lo_pos, **pos;
14822 
14823       gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
14824 
14825       /* If a GOT16 relocation turns out to be against a global symbol,
14826 	 there isn't supposed to be a matching LO.  Ignore %gots against
14827 	 constants; we'll report an error for those later.  */
14828       if (got16_reloc_p (l->fixp->fx_r_type)
14829 	  && !(l->fixp->fx_addsy
14830 	       && pic_need_relax (l->fixp->fx_addsy, l->seg)))
14831 	continue;
14832 
14833       /* Check quickly whether the next fixup happens to be a matching %lo.  */
14834       if (fixup_has_matching_lo_p (l->fixp))
14835 	continue;
14836 
14837       seginfo = seg_info (l->seg);
14838 
14839       /* Set HI_POS to the position of this relocation in the chain.
14840 	 Set LO_POS to the position of the chosen low-part relocation.
14841 	 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
14842 	 relocation that matches an immediately-preceding high-part
14843 	 relocation.  */
14844       hi_pos = NULL;
14845       lo_pos = NULL;
14846       matched_lo_p = FALSE;
14847       looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
14848 
14849       for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
14850 	{
14851 	  if (*pos == l->fixp)
14852 	    hi_pos = pos;
14853 
14854 	  if ((*pos)->fx_r_type == looking_for_rtype
14855 	      && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
14856 	      && (*pos)->fx_offset >= l->fixp->fx_offset
14857 	      && (lo_pos == NULL
14858 		  || (*pos)->fx_offset < (*lo_pos)->fx_offset
14859 		  || (!matched_lo_p
14860 		      && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
14861 	    lo_pos = pos;
14862 
14863 	  matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
14864 			  && fixup_has_matching_lo_p (*pos));
14865 	}
14866 
14867       /* If we found a match, remove the high-part relocation from its
14868 	 current position and insert it before the low-part relocation.
14869 	 Make the offsets match so that fixup_has_matching_lo_p()
14870 	 will return true.
14871 
14872 	 We don't warn about unmatched high-part relocations since some
14873 	 versions of gcc have been known to emit dead "lui ...%hi(...)"
14874 	 instructions.  */
14875       if (lo_pos != NULL)
14876 	{
14877 	  l->fixp->fx_offset = (*lo_pos)->fx_offset;
14878 	  if (l->fixp->fx_next != *lo_pos)
14879 	    {
14880 	      *hi_pos = l->fixp->fx_next;
14881 	      l->fixp->fx_next = *lo_pos;
14882 	      *lo_pos = l->fixp;
14883 	    }
14884 	}
14885     }
14886 }
14887 
14888 int
mips_force_relocation(fixS * fixp)14889 mips_force_relocation (fixS *fixp)
14890 {
14891   if (generic_force_reloc (fixp))
14892     return 1;
14893 
14894   /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
14895      so that the linker relaxation can update targets.  */
14896   if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
14897       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
14898       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
14899     return 1;
14900 
14901   /* We want all PC-relative relocations to be kept for R6 relaxation.  */
14902   if (ISA_IS_R6 (mips_opts.isa)
14903       && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
14904 	  || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
14905 	  || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
14906 	  || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
14907 	  || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
14908 	  || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
14909 	  || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
14910     return 1;
14911 
14912   return 0;
14913 }
14914 
14915 /* Read the instruction associated with RELOC from BUF.  */
14916 
14917 static unsigned int
read_reloc_insn(char * buf,bfd_reloc_code_real_type reloc)14918 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
14919 {
14920   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14921     return read_compressed_insn (buf, 4);
14922   else
14923     return read_insn (buf);
14924 }
14925 
14926 /* Write instruction INSN to BUF, given that it has been relocated
14927    by RELOC.  */
14928 
14929 static void
write_reloc_insn(char * buf,bfd_reloc_code_real_type reloc,unsigned long insn)14930 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
14931 		  unsigned long insn)
14932 {
14933   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14934     write_compressed_insn (buf, insn, 4);
14935   else
14936     write_insn (buf, insn);
14937 }
14938 
14939 /* Apply a fixup to the object file.  */
14940 
14941 void
md_apply_fix(fixS * fixP,valueT * valP,segT seg ATTRIBUTE_UNUSED)14942 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
14943 {
14944   char *buf;
14945   unsigned long insn;
14946   reloc_howto_type *howto;
14947 
14948   if (fixP->fx_pcrel)
14949     switch (fixP->fx_r_type)
14950       {
14951       case BFD_RELOC_16_PCREL_S2:
14952       case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14953       case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14954       case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14955       case BFD_RELOC_32_PCREL:
14956       case BFD_RELOC_MIPS_21_PCREL_S2:
14957       case BFD_RELOC_MIPS_26_PCREL_S2:
14958       case BFD_RELOC_MIPS_18_PCREL_S3:
14959       case BFD_RELOC_MIPS_19_PCREL_S2:
14960       case BFD_RELOC_HI16_S_PCREL:
14961       case BFD_RELOC_LO16_PCREL:
14962 	break;
14963 
14964       case BFD_RELOC_32:
14965 	fixP->fx_r_type = BFD_RELOC_32_PCREL;
14966 	break;
14967 
14968       default:
14969 	as_bad_where (fixP->fx_file, fixP->fx_line,
14970 		      _("PC-relative reference to a different section"));
14971 	break;
14972       }
14973 
14974   /* Handle BFD_RELOC_8, since it's easy.  Punt on other bfd relocations
14975      that have no MIPS ELF equivalent.  */
14976   if (fixP->fx_r_type != BFD_RELOC_8)
14977     {
14978       howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
14979       if (!howto)
14980 	return;
14981     }
14982 
14983   gas_assert (fixP->fx_size == 2
14984 	      || fixP->fx_size == 4
14985 	      || fixP->fx_r_type == BFD_RELOC_8
14986 	      || fixP->fx_r_type == BFD_RELOC_16
14987 	      || fixP->fx_r_type == BFD_RELOC_64
14988 	      || fixP->fx_r_type == BFD_RELOC_CTOR
14989 	      || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
14990 	      || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
14991 	      || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
14992 	      || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
14993 	      || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
14994 
14995   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
14996 
14997   /* Don't treat parts of a composite relocation as done.  There are two
14998      reasons for this:
14999 
15000      (1) The second and third parts will be against 0 (RSS_UNDEF) but
15001 	 should nevertheless be emitted if the first part is.
15002 
15003      (2) In normal usage, composite relocations are never assembly-time
15004 	 constants.  The easiest way of dealing with the pathological
15005 	 exceptions is to generate a relocation against STN_UNDEF and
15006 	 leave everything up to the linker.  */
15007   if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15008     fixP->fx_done = 1;
15009 
15010   switch (fixP->fx_r_type)
15011     {
15012     case BFD_RELOC_MIPS_TLS_GD:
15013     case BFD_RELOC_MIPS_TLS_LDM:
15014     case BFD_RELOC_MIPS_TLS_DTPREL32:
15015     case BFD_RELOC_MIPS_TLS_DTPREL64:
15016     case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15017     case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15018     case BFD_RELOC_MIPS_TLS_GOTTPREL:
15019     case BFD_RELOC_MIPS_TLS_TPREL32:
15020     case BFD_RELOC_MIPS_TLS_TPREL64:
15021     case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15022     case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15023     case BFD_RELOC_MICROMIPS_TLS_GD:
15024     case BFD_RELOC_MICROMIPS_TLS_LDM:
15025     case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15026     case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15027     case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15028     case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15029     case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15030     case BFD_RELOC_MIPS16_TLS_GD:
15031     case BFD_RELOC_MIPS16_TLS_LDM:
15032     case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15033     case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15034     case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15035     case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15036     case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15037       if (!fixP->fx_addsy)
15038 	{
15039 	  as_bad_where (fixP->fx_file, fixP->fx_line,
15040 			_("TLS relocation against a constant"));
15041 	  break;
15042 	}
15043       S_SET_THREAD_LOCAL (fixP->fx_addsy);
15044       /* fall through */
15045 
15046     case BFD_RELOC_MIPS_JMP:
15047     case BFD_RELOC_MIPS_SHIFT5:
15048     case BFD_RELOC_MIPS_SHIFT6:
15049     case BFD_RELOC_MIPS_GOT_DISP:
15050     case BFD_RELOC_MIPS_GOT_PAGE:
15051     case BFD_RELOC_MIPS_GOT_OFST:
15052     case BFD_RELOC_MIPS_SUB:
15053     case BFD_RELOC_MIPS_INSERT_A:
15054     case BFD_RELOC_MIPS_INSERT_B:
15055     case BFD_RELOC_MIPS_DELETE:
15056     case BFD_RELOC_MIPS_HIGHEST:
15057     case BFD_RELOC_MIPS_HIGHER:
15058     case BFD_RELOC_MIPS_SCN_DISP:
15059     case BFD_RELOC_MIPS_REL16:
15060     case BFD_RELOC_MIPS_RELGOT:
15061     case BFD_RELOC_MIPS_JALR:
15062     case BFD_RELOC_HI16:
15063     case BFD_RELOC_HI16_S:
15064     case BFD_RELOC_LO16:
15065     case BFD_RELOC_GPREL16:
15066     case BFD_RELOC_MIPS_LITERAL:
15067     case BFD_RELOC_MIPS_CALL16:
15068     case BFD_RELOC_MIPS_GOT16:
15069     case BFD_RELOC_GPREL32:
15070     case BFD_RELOC_MIPS_GOT_HI16:
15071     case BFD_RELOC_MIPS_GOT_LO16:
15072     case BFD_RELOC_MIPS_CALL_HI16:
15073     case BFD_RELOC_MIPS_CALL_LO16:
15074     case BFD_RELOC_MIPS16_GPREL:
15075     case BFD_RELOC_MIPS16_GOT16:
15076     case BFD_RELOC_MIPS16_CALL16:
15077     case BFD_RELOC_MIPS16_HI16:
15078     case BFD_RELOC_MIPS16_HI16_S:
15079     case BFD_RELOC_MIPS16_LO16:
15080     case BFD_RELOC_MIPS16_JMP:
15081     case BFD_RELOC_MICROMIPS_JMP:
15082     case BFD_RELOC_MICROMIPS_GOT_DISP:
15083     case BFD_RELOC_MICROMIPS_GOT_PAGE:
15084     case BFD_RELOC_MICROMIPS_GOT_OFST:
15085     case BFD_RELOC_MICROMIPS_SUB:
15086     case BFD_RELOC_MICROMIPS_HIGHEST:
15087     case BFD_RELOC_MICROMIPS_HIGHER:
15088     case BFD_RELOC_MICROMIPS_SCN_DISP:
15089     case BFD_RELOC_MICROMIPS_JALR:
15090     case BFD_RELOC_MICROMIPS_HI16:
15091     case BFD_RELOC_MICROMIPS_HI16_S:
15092     case BFD_RELOC_MICROMIPS_LO16:
15093     case BFD_RELOC_MICROMIPS_GPREL16:
15094     case BFD_RELOC_MICROMIPS_LITERAL:
15095     case BFD_RELOC_MICROMIPS_CALL16:
15096     case BFD_RELOC_MICROMIPS_GOT16:
15097     case BFD_RELOC_MICROMIPS_GOT_HI16:
15098     case BFD_RELOC_MICROMIPS_GOT_LO16:
15099     case BFD_RELOC_MICROMIPS_CALL_HI16:
15100     case BFD_RELOC_MICROMIPS_CALL_LO16:
15101     case BFD_RELOC_MIPS_EH:
15102       if (fixP->fx_done)
15103 	{
15104 	  offsetT value;
15105 
15106 	  if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15107 	    {
15108 	      insn = read_reloc_insn (buf, fixP->fx_r_type);
15109 	      if (mips16_reloc_p (fixP->fx_r_type))
15110 		insn |= mips16_immed_extend (value, 16);
15111 	      else
15112 		insn |= (value & 0xffff);
15113 	      write_reloc_insn (buf, fixP->fx_r_type, insn);
15114 	    }
15115 	  else
15116 	    as_bad_where (fixP->fx_file, fixP->fx_line,
15117 			  _("unsupported constant in relocation"));
15118 	}
15119       break;
15120 
15121     case BFD_RELOC_64:
15122       /* This is handled like BFD_RELOC_32, but we output a sign
15123          extended value if we are only 32 bits.  */
15124       if (fixP->fx_done)
15125 	{
15126 	  if (8 <= sizeof (valueT))
15127 	    md_number_to_chars (buf, *valP, 8);
15128 	  else
15129 	    {
15130 	      valueT hiv;
15131 
15132 	      if ((*valP & 0x80000000) != 0)
15133 		hiv = 0xffffffff;
15134 	      else
15135 		hiv = 0;
15136 	      md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15137 	      md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
15138 	    }
15139 	}
15140       break;
15141 
15142     case BFD_RELOC_RVA:
15143     case BFD_RELOC_32:
15144     case BFD_RELOC_32_PCREL:
15145     case BFD_RELOC_16:
15146     case BFD_RELOC_8:
15147       /* If we are deleting this reloc entry, we must fill in the
15148 	 value now.  This can happen if we have a .word which is not
15149 	 resolved when it appears but is later defined.  */
15150       if (fixP->fx_done)
15151 	md_number_to_chars (buf, *valP, fixP->fx_size);
15152       break;
15153 
15154     case BFD_RELOC_MIPS_21_PCREL_S2:
15155     case BFD_RELOC_MIPS_26_PCREL_S2:
15156       if ((*valP & 0x3) != 0)
15157 	as_bad_where (fixP->fx_file, fixP->fx_line,
15158 		      _("branch to misaligned address (%lx)"), (long) *valP);
15159 
15160       gas_assert (!fixP->fx_done);
15161       break;
15162 
15163     case BFD_RELOC_MIPS_18_PCREL_S3:
15164       if ((*valP & 0x7) != 0)
15165 	as_bad_where (fixP->fx_file, fixP->fx_line,
15166 		      _("PC-relative access to misaligned address (%lx)"),
15167 		      (long) *valP);
15168 
15169       gas_assert (!fixP->fx_done);
15170       break;
15171 
15172     case BFD_RELOC_MIPS_19_PCREL_S2:
15173       if ((*valP & 0x3) != 0)
15174 	as_bad_where (fixP->fx_file, fixP->fx_line,
15175 		      _("PC-relative access to misaligned address (%lx)"),
15176 		      (long) *valP);
15177 
15178       gas_assert (!fixP->fx_done);
15179       break;
15180 
15181     case BFD_RELOC_HI16_S_PCREL:
15182     case BFD_RELOC_LO16_PCREL:
15183       gas_assert (!fixP->fx_done);
15184       break;
15185 
15186     case BFD_RELOC_16_PCREL_S2:
15187       if ((*valP & 0x3) != 0)
15188 	as_bad_where (fixP->fx_file, fixP->fx_line,
15189 		      _("branch to misaligned address (%lx)"), (long) *valP);
15190 
15191       /* We need to save the bits in the instruction since fixup_segment()
15192 	 might be deleting the relocation entry (i.e., a branch within
15193 	 the current segment).  */
15194       if (! fixP->fx_done)
15195 	break;
15196 
15197       /* Update old instruction data.  */
15198       insn = read_insn (buf);
15199 
15200       if (*valP + 0x20000 <= 0x3ffff)
15201 	{
15202 	  insn |= (*valP >> 2) & 0xffff;
15203 	  write_insn (buf, insn);
15204 	}
15205       else if (mips_pic == NO_PIC
15206 	       && fixP->fx_done
15207 	       && fixP->fx_frag->fr_address >= text_section->vma
15208 	       && (fixP->fx_frag->fr_address
15209 		   < text_section->vma + bfd_get_section_size (text_section))
15210 	       && ((insn & 0xffff0000) == 0x10000000	 /* beq $0,$0 */
15211 		   || (insn & 0xffff0000) == 0x04010000	 /* bgez $0 */
15212 		   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15213 	{
15214 	  /* The branch offset is too large.  If this is an
15215              unconditional branch, and we are not generating PIC code,
15216              we can convert it to an absolute jump instruction.  */
15217 	  if ((insn & 0xffff0000) == 0x04110000)	 /* bgezal $0 */
15218 	    insn = 0x0c000000;	/* jal */
15219 	  else
15220 	    insn = 0x08000000;	/* j */
15221 	  fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15222 	  fixP->fx_done = 0;
15223 	  fixP->fx_addsy = section_symbol (text_section);
15224 	  *valP += md_pcrel_from (fixP);
15225 	  write_insn (buf, insn);
15226 	}
15227       else
15228 	{
15229 	  /* If we got here, we have branch-relaxation disabled,
15230 	     and there's nothing we can do to fix this instruction
15231 	     without turning it into a longer sequence.  */
15232 	  as_bad_where (fixP->fx_file, fixP->fx_line,
15233 			_("branch out of range"));
15234 	}
15235       break;
15236 
15237     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15238     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15239     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15240       /* We adjust the offset back to even.  */
15241       if ((*valP & 0x1) != 0)
15242 	--(*valP);
15243 
15244       if (! fixP->fx_done)
15245 	break;
15246 
15247       /* Should never visit here, because we keep the relocation.  */
15248       abort ();
15249       break;
15250 
15251     case BFD_RELOC_VTABLE_INHERIT:
15252       fixP->fx_done = 0;
15253       if (fixP->fx_addsy
15254           && !S_IS_DEFINED (fixP->fx_addsy)
15255           && !S_IS_WEAK (fixP->fx_addsy))
15256         S_SET_WEAK (fixP->fx_addsy);
15257       break;
15258 
15259     case BFD_RELOC_VTABLE_ENTRY:
15260       fixP->fx_done = 0;
15261       break;
15262 
15263     default:
15264       abort ();
15265     }
15266 
15267   /* Remember value for tc_gen_reloc.  */
15268   fixP->fx_addnumber = *valP;
15269 }
15270 
15271 static symbolS *
get_symbol(void)15272 get_symbol (void)
15273 {
15274   int c;
15275   char *name;
15276   symbolS *p;
15277 
15278   name = input_line_pointer;
15279   c = get_symbol_end ();
15280   p = (symbolS *) symbol_find_or_make (name);
15281   *input_line_pointer = c;
15282   return p;
15283 }
15284 
15285 /* Align the current frag to a given power of two.  If a particular
15286    fill byte should be used, FILL points to an integer that contains
15287    that byte, otherwise FILL is null.
15288 
15289    This function used to have the comment:
15290 
15291       The MIPS assembler also automatically adjusts any preceding label.
15292 
15293    The implementation therefore applied the adjustment to a maximum of
15294    one label.  However, other label adjustments are applied to batches
15295    of labels, and adjusting just one caused problems when new labels
15296    were added for the sake of debugging or unwind information.
15297    We therefore adjust all preceding labels (given as LABELS) instead.  */
15298 
15299 static void
mips_align(int to,int * fill,struct insn_label_list * labels)15300 mips_align (int to, int *fill, struct insn_label_list *labels)
15301 {
15302   mips_emit_delays ();
15303   mips_record_compressed_mode ();
15304   if (fill == NULL && subseg_text_p (now_seg))
15305     frag_align_code (to, 0);
15306   else
15307     frag_align (to, fill ? *fill : 0, 0);
15308   record_alignment (now_seg, to);
15309   mips_move_labels (labels, FALSE);
15310 }
15311 
15312 /* Align to a given power of two.  .align 0 turns off the automatic
15313    alignment used by the data creating pseudo-ops.  */
15314 
15315 static void
s_align(int x ATTRIBUTE_UNUSED)15316 s_align (int x ATTRIBUTE_UNUSED)
15317 {
15318   int temp, fill_value, *fill_ptr;
15319   long max_alignment = 28;
15320 
15321   /* o Note that the assembler pulls down any immediately preceding label
15322        to the aligned address.
15323      o It's not documented but auto alignment is reinstated by
15324        a .align pseudo instruction.
15325      o Note also that after auto alignment is turned off the mips assembler
15326        issues an error on attempt to assemble an improperly aligned data item.
15327        We don't.  */
15328 
15329   temp = get_absolute_expression ();
15330   if (temp > max_alignment)
15331     as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
15332   else if (temp < 0)
15333     {
15334       as_warn (_("alignment negative, 0 assumed"));
15335       temp = 0;
15336     }
15337   if (*input_line_pointer == ',')
15338     {
15339       ++input_line_pointer;
15340       fill_value = get_absolute_expression ();
15341       fill_ptr = &fill_value;
15342     }
15343   else
15344     fill_ptr = 0;
15345   if (temp)
15346     {
15347       segment_info_type *si = seg_info (now_seg);
15348       struct insn_label_list *l = si->label_list;
15349       /* Auto alignment should be switched on by next section change.  */
15350       auto_align = 1;
15351       mips_align (temp, fill_ptr, l);
15352     }
15353   else
15354     {
15355       auto_align = 0;
15356     }
15357 
15358   demand_empty_rest_of_line ();
15359 }
15360 
15361 static void
s_change_sec(int sec)15362 s_change_sec (int sec)
15363 {
15364   segT seg;
15365 
15366   /* The ELF backend needs to know that we are changing sections, so
15367      that .previous works correctly.  We could do something like check
15368      for an obj_section_change_hook macro, but that might be confusing
15369      as it would not be appropriate to use it in the section changing
15370      functions in read.c, since obj-elf.c intercepts those.  FIXME:
15371      This should be cleaner, somehow.  */
15372   obj_elf_section_change_hook ();
15373 
15374   mips_emit_delays ();
15375 
15376   switch (sec)
15377     {
15378     case 't':
15379       s_text (0);
15380       break;
15381     case 'd':
15382       s_data (0);
15383       break;
15384     case 'b':
15385       subseg_set (bss_section, (subsegT) get_absolute_expression ());
15386       demand_empty_rest_of_line ();
15387       break;
15388 
15389     case 'r':
15390       seg = subseg_new (RDATA_SECTION_NAME,
15391 			(subsegT) get_absolute_expression ());
15392       bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15393 					      | SEC_READONLY | SEC_RELOC
15394 					      | SEC_DATA));
15395       if (strncmp (TARGET_OS, "elf", 3) != 0)
15396 	record_alignment (seg, 4);
15397       demand_empty_rest_of_line ();
15398       break;
15399 
15400     case 's':
15401       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
15402       bfd_set_section_flags (stdoutput, seg,
15403 			     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
15404       if (strncmp (TARGET_OS, "elf", 3) != 0)
15405 	record_alignment (seg, 4);
15406       demand_empty_rest_of_line ();
15407       break;
15408 
15409     case 'B':
15410       seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
15411       bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15412       if (strncmp (TARGET_OS, "elf", 3) != 0)
15413 	record_alignment (seg, 4);
15414       demand_empty_rest_of_line ();
15415       break;
15416     }
15417 
15418   auto_align = 1;
15419 }
15420 
15421 void
s_change_section(int ignore ATTRIBUTE_UNUSED)15422 s_change_section (int ignore ATTRIBUTE_UNUSED)
15423 {
15424   char *section_name;
15425   char c;
15426   char next_c = 0;
15427   int section_type;
15428   int section_flag;
15429   int section_entry_size;
15430   int section_alignment;
15431 
15432   section_name = input_line_pointer;
15433   c = get_symbol_end ();
15434   if (c)
15435     next_c = *(input_line_pointer + 1);
15436 
15437   /* Do we have .section Name<,"flags">?  */
15438   if (c != ',' || (c == ',' && next_c == '"'))
15439     {
15440       /* just after name is now '\0'.  */
15441       *input_line_pointer = c;
15442       input_line_pointer = section_name;
15443       obj_elf_section (ignore);
15444       return;
15445     }
15446   input_line_pointer++;
15447 
15448   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
15449   if (c == ',')
15450     section_type = get_absolute_expression ();
15451   else
15452     section_type = 0;
15453   if (*input_line_pointer++ == ',')
15454     section_flag = get_absolute_expression ();
15455   else
15456     section_flag = 0;
15457   if (*input_line_pointer++ == ',')
15458     section_entry_size = get_absolute_expression ();
15459   else
15460     section_entry_size = 0;
15461   if (*input_line_pointer++ == ',')
15462     section_alignment = get_absolute_expression ();
15463   else
15464     section_alignment = 0;
15465   /* FIXME: really ignore?  */
15466   (void) section_alignment;
15467 
15468   section_name = xstrdup (section_name);
15469 
15470   /* When using the generic form of .section (as implemented by obj-elf.c),
15471      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
15472      traditionally had to fall back on the more common @progbits instead.
15473 
15474      There's nothing really harmful in this, since bfd will correct
15475      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
15476      means that, for backwards compatibility, the special_section entries
15477      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
15478 
15479      Even so, we shouldn't force users of the MIPS .section syntax to
15480      incorrectly label the sections as SHT_PROGBITS.  The best compromise
15481      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
15482      generic type-checking code.  */
15483   if (section_type == SHT_MIPS_DWARF)
15484     section_type = SHT_PROGBITS;
15485 
15486   obj_elf_change_section (section_name, section_type, section_flag,
15487 			  section_entry_size, 0, 0, 0);
15488 
15489   if (now_seg->name != section_name)
15490     free (section_name);
15491 }
15492 
15493 void
mips_enable_auto_align(void)15494 mips_enable_auto_align (void)
15495 {
15496   auto_align = 1;
15497 }
15498 
15499 static void
s_cons(int log_size)15500 s_cons (int log_size)
15501 {
15502   segment_info_type *si = seg_info (now_seg);
15503   struct insn_label_list *l = si->label_list;
15504 
15505   mips_emit_delays ();
15506   if (log_size > 0 && auto_align)
15507     mips_align (log_size, 0, l);
15508   cons (1 << log_size);
15509   mips_clear_insn_labels ();
15510 }
15511 
15512 static void
s_float_cons(int type)15513 s_float_cons (int type)
15514 {
15515   segment_info_type *si = seg_info (now_seg);
15516   struct insn_label_list *l = si->label_list;
15517 
15518   mips_emit_delays ();
15519 
15520   if (auto_align)
15521     {
15522       if (type == 'd')
15523 	mips_align (3, 0, l);
15524       else
15525 	mips_align (2, 0, l);
15526     }
15527 
15528   float_cons (type);
15529   mips_clear_insn_labels ();
15530 }
15531 
15532 /* Handle .globl.  We need to override it because on Irix 5 you are
15533    permitted to say
15534        .globl foo .text
15535    where foo is an undefined symbol, to mean that foo should be
15536    considered to be the address of a function.  */
15537 
15538 static void
s_mips_globl(int x ATTRIBUTE_UNUSED)15539 s_mips_globl (int x ATTRIBUTE_UNUSED)
15540 {
15541   char *name;
15542   int c;
15543   symbolS *symbolP;
15544   flagword flag;
15545 
15546   do
15547     {
15548       name = input_line_pointer;
15549       c = get_symbol_end ();
15550       symbolP = symbol_find_or_make (name);
15551       S_SET_EXTERNAL (symbolP);
15552 
15553       *input_line_pointer = c;
15554       SKIP_WHITESPACE ();
15555 
15556       /* On Irix 5, every global symbol that is not explicitly labelled as
15557          being a function is apparently labelled as being an object.  */
15558       flag = BSF_OBJECT;
15559 
15560       if (!is_end_of_line[(unsigned char) *input_line_pointer]
15561 	  && (*input_line_pointer != ','))
15562 	{
15563 	  char *secname;
15564 	  asection *sec;
15565 
15566 	  secname = input_line_pointer;
15567 	  c = get_symbol_end ();
15568 	  sec = bfd_get_section_by_name (stdoutput, secname);
15569 	  if (sec == NULL)
15570 	    as_bad (_("%s: no such section"), secname);
15571 	  *input_line_pointer = c;
15572 
15573 	  if (sec != NULL && (sec->flags & SEC_CODE) != 0)
15574 	    flag = BSF_FUNCTION;
15575 	}
15576 
15577       symbol_get_bfdsym (symbolP)->flags |= flag;
15578 
15579       c = *input_line_pointer;
15580       if (c == ',')
15581 	{
15582 	  input_line_pointer++;
15583 	  SKIP_WHITESPACE ();
15584 	  if (is_end_of_line[(unsigned char) *input_line_pointer])
15585 	    c = '\n';
15586 	}
15587     }
15588   while (c == ',');
15589 
15590   demand_empty_rest_of_line ();
15591 }
15592 
15593 static void
s_option(int x ATTRIBUTE_UNUSED)15594 s_option (int x ATTRIBUTE_UNUSED)
15595 {
15596   char *opt;
15597   char c;
15598 
15599   opt = input_line_pointer;
15600   c = get_symbol_end ();
15601 
15602   if (*opt == 'O')
15603     {
15604       /* FIXME: What does this mean?  */
15605     }
15606   else if (strncmp (opt, "pic", 3) == 0)
15607     {
15608       int i;
15609 
15610       i = atoi (opt + 3);
15611       if (i == 0)
15612 	mips_pic = NO_PIC;
15613       else if (i == 2)
15614 	{
15615 	  mips_pic = SVR4_PIC;
15616 	  mips_abicalls = TRUE;
15617 	}
15618       else
15619 	as_bad (_(".option pic%d not supported"), i);
15620 
15621       if (mips_pic == SVR4_PIC)
15622 	{
15623 	  if (g_switch_seen && g_switch_value != 0)
15624 	    as_warn (_("-G may not be used with SVR4 PIC code"));
15625 	  g_switch_value = 0;
15626 	  bfd_set_gp_size (stdoutput, 0);
15627 	}
15628     }
15629   else
15630     as_warn (_("unrecognized option \"%s\""), opt);
15631 
15632   *input_line_pointer = c;
15633   demand_empty_rest_of_line ();
15634 }
15635 
15636 /* This structure is used to hold a stack of .set values.  */
15637 
15638 struct mips_option_stack
15639 {
15640   struct mips_option_stack *next;
15641   struct mips_set_options options;
15642 };
15643 
15644 static struct mips_option_stack *mips_opts_stack;
15645 
15646 static bfd_boolean
parse_code_option(char * name)15647 parse_code_option (char * name)
15648 {
15649   const struct mips_ase *ase;
15650   if (strncmp (name, "at=", 3) == 0)
15651     {
15652       char *s = name + 3;
15653 
15654       if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
15655 	as_bad (_("unrecognized register name `%s'"), s);
15656     }
15657   else if (strcmp (name, "at") == 0)
15658     mips_opts.at = ATREG;
15659   else if (strcmp (name, "noat") == 0)
15660     mips_opts.at = ZERO;
15661   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
15662     mips_opts.nomove = 0;
15663   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
15664     mips_opts.nomove = 1;
15665   else if (strcmp (name, "bopt") == 0)
15666     mips_opts.nobopt = 0;
15667   else if (strcmp (name, "nobopt") == 0)
15668     mips_opts.nobopt = 1;
15669   else if (strcmp (name, "gp=32") == 0)
15670     mips_opts.gp = 32;
15671   else if (strcmp (name, "gp=64") == 0)
15672     mips_opts.gp = 64;
15673   else if (strcmp (name, "fp=32") == 0)
15674     mips_opts.fp = 32;
15675   else if (strcmp (name, "fp=xx") == 0)
15676     mips_opts.fp = 0;
15677   else if (strcmp (name, "fp=64") == 0)
15678     mips_opts.fp = 64;
15679   else if (strcmp (name, "softfloat") == 0)
15680     mips_opts.soft_float = 1;
15681   else if (strcmp (name, "hardfloat") == 0)
15682     mips_opts.soft_float = 0;
15683   else if (strcmp (name, "singlefloat") == 0)
15684     mips_opts.single_float = 1;
15685   else if (strcmp (name, "doublefloat") == 0)
15686     mips_opts.single_float = 0;
15687   else if (strcmp (name, "nooddspreg") == 0)
15688     mips_opts.oddspreg = 0;
15689   else if (strcmp (name, "oddspreg") == 0)
15690     mips_opts.oddspreg = 1;
15691   else if (strcmp (name, "mips16") == 0
15692 	   || strcmp (name, "MIPS-16") == 0)
15693     mips_opts.mips16 = 1;
15694   else if (strcmp (name, "nomips16") == 0
15695 	   || strcmp (name, "noMIPS-16") == 0)
15696     mips_opts.mips16 = 0;
15697   else if (strcmp (name, "micromips") == 0)
15698     mips_opts.micromips = 1;
15699   else if (strcmp (name, "nomicromips") == 0)
15700     mips_opts.micromips = 0;
15701   else if (name[0] == 'n'
15702 	   && name[1] == 'o'
15703 	   && (ase = mips_lookup_ase (name + 2)))
15704     mips_set_ase (ase, &mips_opts, FALSE);
15705   else if ((ase = mips_lookup_ase (name)))
15706     mips_set_ase (ase, &mips_opts, TRUE);
15707   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
15708     {
15709       /* Permit the user to change the ISA and architecture on the fly.
15710 	 Needless to say, misuse can cause serious problems.  */
15711       if (strncmp (name, "arch=", 5) == 0)
15712 	{
15713 	  const struct mips_cpu_info *p;
15714 
15715 	  p = mips_parse_cpu ("internal use", name + 5);
15716 	  if (!p)
15717 	    as_bad (_("unknown architecture %s"), name + 5);
15718 	  else
15719 	    {
15720 	      mips_opts.arch = p->cpu;
15721 	      mips_opts.isa = p->isa;
15722 	    }
15723 	}
15724       else if (strncmp (name, "mips", 4) == 0)
15725 	{
15726 	  const struct mips_cpu_info *p;
15727 
15728 	  p = mips_parse_cpu ("internal use", name);
15729 	  if (!p)
15730 	    as_bad (_("unknown ISA level %s"), name + 4);
15731 	  else
15732 	    {
15733 	      mips_opts.arch = p->cpu;
15734 	      mips_opts.isa = p->isa;
15735 	    }
15736 	}
15737       else
15738 	as_bad (_("unknown ISA or architecture %s"), name);
15739     }
15740   else if (strcmp (name, "autoextend") == 0)
15741     mips_opts.noautoextend = 0;
15742   else if (strcmp (name, "noautoextend") == 0)
15743     mips_opts.noautoextend = 1;
15744   else if (strcmp (name, "insn32") == 0)
15745     mips_opts.insn32 = TRUE;
15746   else if (strcmp (name, "noinsn32") == 0)
15747     mips_opts.insn32 = FALSE;
15748   else if (strcmp (name, "sym32") == 0)
15749     mips_opts.sym32 = TRUE;
15750   else if (strcmp (name, "nosym32") == 0)
15751     mips_opts.sym32 = FALSE;
15752   else
15753     return FALSE;
15754   return TRUE;
15755 }
15756 
15757 /* Handle the .set pseudo-op.  */
15758 
15759 static void
s_mipsset(int x ATTRIBUTE_UNUSED)15760 s_mipsset (int x ATTRIBUTE_UNUSED)
15761 {
15762   char *name = input_line_pointer, ch;
15763   int prev_isa = mips_opts.isa;
15764 
15765   file_mips_check_options ();
15766 
15767   while (!is_end_of_line[(unsigned char) *input_line_pointer])
15768     ++input_line_pointer;
15769   ch = *input_line_pointer;
15770   *input_line_pointer = '\0';
15771 
15772   if (strchr (name, ','))
15773     {
15774       /* Generic ".set" directive; use the generic handler.  */
15775       *input_line_pointer = ch;
15776       input_line_pointer = name;
15777       s_set (0);
15778       return;
15779     }
15780 
15781   if (strcmp (name, "reorder") == 0)
15782     {
15783       if (mips_opts.noreorder)
15784 	end_noreorder ();
15785     }
15786   else if (strcmp (name, "noreorder") == 0)
15787     {
15788       if (!mips_opts.noreorder)
15789 	start_noreorder ();
15790     }
15791   else if (strcmp (name, "macro") == 0)
15792     mips_opts.warn_about_macros = 0;
15793   else if (strcmp (name, "nomacro") == 0)
15794     {
15795       if (mips_opts.noreorder == 0)
15796 	as_bad (_("`noreorder' must be set before `nomacro'"));
15797       mips_opts.warn_about_macros = 1;
15798     }
15799   else if (strcmp (name, "gp=default") == 0)
15800     mips_opts.gp = file_mips_opts.gp;
15801   else if (strcmp (name, "fp=default") == 0)
15802     mips_opts.fp = file_mips_opts.fp;
15803   else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
15804     {
15805       mips_opts.isa = file_mips_opts.isa;
15806       mips_opts.arch = file_mips_opts.arch;
15807       mips_opts.gp = file_mips_opts.gp;
15808       mips_opts.fp = file_mips_opts.fp;
15809     }
15810   else if (strcmp (name, "push") == 0)
15811     {
15812       struct mips_option_stack *s;
15813 
15814       s = (struct mips_option_stack *) xmalloc (sizeof *s);
15815       s->next = mips_opts_stack;
15816       s->options = mips_opts;
15817       mips_opts_stack = s;
15818     }
15819   else if (strcmp (name, "pop") == 0)
15820     {
15821       struct mips_option_stack *s;
15822 
15823       s = mips_opts_stack;
15824       if (s == NULL)
15825 	as_bad (_(".set pop with no .set push"));
15826       else
15827 	{
15828 	  /* If we're changing the reorder mode we need to handle
15829              delay slots correctly.  */
15830 	  if (s->options.noreorder && ! mips_opts.noreorder)
15831 	    start_noreorder ();
15832 	  else if (! s->options.noreorder && mips_opts.noreorder)
15833 	    end_noreorder ();
15834 
15835 	  mips_opts = s->options;
15836 	  mips_opts_stack = s->next;
15837 	  free (s);
15838 	}
15839     }
15840   else if (!parse_code_option (name))
15841     as_warn (_("tried to set unrecognized symbol: %s\n"), name);
15842 
15843   /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
15844      registers based on what is supported by the arch/cpu.  */
15845   if (mips_opts.isa != prev_isa)
15846     {
15847       switch (mips_opts.isa)
15848 	{
15849 	case 0:
15850 	  break;
15851 	case ISA_MIPS1:
15852 	  /* MIPS I cannot support FPXX.  */
15853 	  mips_opts.fp = 32;
15854 	  /* fall-through.  */
15855 	case ISA_MIPS2:
15856 	case ISA_MIPS32:
15857 	case ISA_MIPS32R2:
15858 	case ISA_MIPS32R3:
15859 	case ISA_MIPS32R5:
15860 	  mips_opts.gp = 32;
15861 	  if (mips_opts.fp != 0)
15862 	    mips_opts.fp = 32;
15863 	  break;
15864 	case ISA_MIPS32R6:
15865 	  mips_opts.gp = 32;
15866 	  mips_opts.fp = 64;
15867 	  break;
15868 	case ISA_MIPS3:
15869 	case ISA_MIPS4:
15870 	case ISA_MIPS5:
15871 	case ISA_MIPS64:
15872 	case ISA_MIPS64R2:
15873 	case ISA_MIPS64R3:
15874 	case ISA_MIPS64R5:
15875 	case ISA_MIPS64R6:
15876 	  mips_opts.gp = 64;
15877 	  if (mips_opts.fp != 0)
15878 	    {
15879 	      if (mips_opts.arch == CPU_R5900)
15880 		mips_opts.fp = 32;
15881 	      else
15882 		mips_opts.fp = 64;
15883 	    }
15884 	  break;
15885 	default:
15886 	  as_bad (_("unknown ISA level %s"), name + 4);
15887 	  break;
15888 	}
15889     }
15890 
15891   mips_check_options (&mips_opts, FALSE);
15892 
15893   mips_check_isa_supports_ases ();
15894   *input_line_pointer = ch;
15895   demand_empty_rest_of_line ();
15896 }
15897 
15898 /* Handle the .module pseudo-op.  */
15899 
15900 static void
s_module(int ignore ATTRIBUTE_UNUSED)15901 s_module (int ignore ATTRIBUTE_UNUSED)
15902 {
15903   char *name = input_line_pointer, ch;
15904 
15905   while (!is_end_of_line[(unsigned char) *input_line_pointer])
15906     ++input_line_pointer;
15907   ch = *input_line_pointer;
15908   *input_line_pointer = '\0';
15909 
15910   if (!file_mips_opts_checked)
15911     {
15912       if (!parse_code_option (name))
15913 	as_bad (_(".module used with unrecognized symbol: %s\n"), name);
15914 
15915       /* Update module level settings from mips_opts.  */
15916       file_mips_opts = mips_opts;
15917     }
15918   else
15919     as_bad (_(".module is not permitted after generating code"));
15920 
15921   *input_line_pointer = ch;
15922   demand_empty_rest_of_line ();
15923 }
15924 
15925 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
15926    .option pic2.  It means to generate SVR4 PIC calls.  */
15927 
15928 static void
s_abicalls(int ignore ATTRIBUTE_UNUSED)15929 s_abicalls (int ignore ATTRIBUTE_UNUSED)
15930 {
15931   mips_pic = SVR4_PIC;
15932   mips_abicalls = TRUE;
15933 
15934   if (g_switch_seen && g_switch_value != 0)
15935     as_warn (_("-G may not be used with SVR4 PIC code"));
15936   g_switch_value = 0;
15937 
15938   bfd_set_gp_size (stdoutput, 0);
15939   demand_empty_rest_of_line ();
15940 }
15941 
15942 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
15943    PIC code.  It sets the $gp register for the function based on the
15944    function address, which is in the register named in the argument.
15945    This uses a relocation against _gp_disp, which is handled specially
15946    by the linker.  The result is:
15947 	lui	$gp,%hi(_gp_disp)
15948 	addiu	$gp,$gp,%lo(_gp_disp)
15949 	addu	$gp,$gp,.cpload argument
15950    The .cpload argument is normally $25 == $t9.
15951 
15952    The -mno-shared option changes this to:
15953 	lui	$gp,%hi(__gnu_local_gp)
15954 	addiu	$gp,$gp,%lo(__gnu_local_gp)
15955    and the argument is ignored.  This saves an instruction, but the
15956    resulting code is not position independent; it uses an absolute
15957    address for __gnu_local_gp.  Thus code assembled with -mno-shared
15958    can go into an ordinary executable, but not into a shared library.  */
15959 
15960 static void
s_cpload(int ignore ATTRIBUTE_UNUSED)15961 s_cpload (int ignore ATTRIBUTE_UNUSED)
15962 {
15963   expressionS ex;
15964   int reg;
15965   int in_shared;
15966 
15967   file_mips_check_options ();
15968 
15969   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
15970      .cpload is ignored.  */
15971   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
15972     {
15973       s_ignore (0);
15974       return;
15975     }
15976 
15977   if (mips_opts.mips16)
15978     {
15979       as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
15980       ignore_rest_of_line ();
15981       return;
15982     }
15983 
15984   /* .cpload should be in a .set noreorder section.  */
15985   if (mips_opts.noreorder == 0)
15986     as_warn (_(".cpload not in noreorder section"));
15987 
15988   reg = tc_get_register (0);
15989 
15990   /* If we need to produce a 64-bit address, we are better off using
15991      the default instruction sequence.  */
15992   in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
15993 
15994   ex.X_op = O_symbol;
15995   ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
15996                                          "__gnu_local_gp");
15997   ex.X_op_symbol = NULL;
15998   ex.X_add_number = 0;
15999 
16000   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16001   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16002 
16003   mips_mark_labels ();
16004   mips_assembling_insn = TRUE;
16005 
16006   macro_start ();
16007   macro_build_lui (&ex, mips_gp_register);
16008   macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16009 	       mips_gp_register, BFD_RELOC_LO16);
16010   if (in_shared)
16011     macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16012 		 mips_gp_register, reg);
16013   macro_end ();
16014 
16015   mips_assembling_insn = FALSE;
16016   demand_empty_rest_of_line ();
16017 }
16018 
16019 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
16020      .cpsetup $reg1, offset|$reg2, label
16021 
16022    If offset is given, this results in:
16023      sd		$gp, offset($sp)
16024      lui	$gp, %hi(%neg(%gp_rel(label)))
16025      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
16026      daddu	$gp, $gp, $reg1
16027 
16028    If $reg2 is given, this results in:
16029      daddu	$reg2, $gp, $0
16030      lui	$gp, %hi(%neg(%gp_rel(label)))
16031      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
16032      daddu	$gp, $gp, $reg1
16033    $reg1 is normally $25 == $t9.
16034 
16035    The -mno-shared option replaces the last three instructions with
16036 	lui	$gp,%hi(_gp)
16037 	addiu	$gp,$gp,%lo(_gp)  */
16038 
16039 static void
s_cpsetup(int ignore ATTRIBUTE_UNUSED)16040 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16041 {
16042   expressionS ex_off;
16043   expressionS ex_sym;
16044   int reg1;
16045 
16046   file_mips_check_options ();
16047 
16048   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16049      We also need NewABI support.  */
16050   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16051     {
16052       s_ignore (0);
16053       return;
16054     }
16055 
16056   if (mips_opts.mips16)
16057     {
16058       as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16059       ignore_rest_of_line ();
16060       return;
16061     }
16062 
16063   reg1 = tc_get_register (0);
16064   SKIP_WHITESPACE ();
16065   if (*input_line_pointer != ',')
16066     {
16067       as_bad (_("missing argument separator ',' for .cpsetup"));
16068       return;
16069     }
16070   else
16071     ++input_line_pointer;
16072   SKIP_WHITESPACE ();
16073   if (*input_line_pointer == '$')
16074     {
16075       mips_cpreturn_register = tc_get_register (0);
16076       mips_cpreturn_offset = -1;
16077     }
16078   else
16079     {
16080       mips_cpreturn_offset = get_absolute_expression ();
16081       mips_cpreturn_register = -1;
16082     }
16083   SKIP_WHITESPACE ();
16084   if (*input_line_pointer != ',')
16085     {
16086       as_bad (_("missing argument separator ',' for .cpsetup"));
16087       return;
16088     }
16089   else
16090     ++input_line_pointer;
16091   SKIP_WHITESPACE ();
16092   expression (&ex_sym);
16093 
16094   mips_mark_labels ();
16095   mips_assembling_insn = TRUE;
16096 
16097   macro_start ();
16098   if (mips_cpreturn_register == -1)
16099     {
16100       ex_off.X_op = O_constant;
16101       ex_off.X_add_symbol = NULL;
16102       ex_off.X_op_symbol = NULL;
16103       ex_off.X_add_number = mips_cpreturn_offset;
16104 
16105       macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16106 		   BFD_RELOC_LO16, SP);
16107     }
16108   else
16109     macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
16110 		 mips_gp_register, 0);
16111 
16112   if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16113     {
16114       macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16115 		   -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16116 		   BFD_RELOC_HI16_S);
16117 
16118       macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16119 		   mips_gp_register, -1, BFD_RELOC_GPREL16,
16120 		   BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16121 
16122       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16123 		   mips_gp_register, reg1);
16124     }
16125   else
16126     {
16127       expressionS ex;
16128 
16129       ex.X_op = O_symbol;
16130       ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16131       ex.X_op_symbol = NULL;
16132       ex.X_add_number = 0;
16133 
16134       /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16135       symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16136 
16137       macro_build_lui (&ex, mips_gp_register);
16138       macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16139 		   mips_gp_register, BFD_RELOC_LO16);
16140     }
16141 
16142   macro_end ();
16143 
16144   mips_assembling_insn = FALSE;
16145   demand_empty_rest_of_line ();
16146 }
16147 
16148 static void
s_cplocal(int ignore ATTRIBUTE_UNUSED)16149 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16150 {
16151   file_mips_check_options ();
16152 
16153   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16154      .cplocal is ignored.  */
16155   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16156     {
16157       s_ignore (0);
16158       return;
16159     }
16160 
16161   if (mips_opts.mips16)
16162     {
16163       as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16164       ignore_rest_of_line ();
16165       return;
16166     }
16167 
16168   mips_gp_register = tc_get_register (0);
16169   demand_empty_rest_of_line ();
16170 }
16171 
16172 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
16173    offset from $sp.  The offset is remembered, and after making a PIC
16174    call $gp is restored from that location.  */
16175 
16176 static void
s_cprestore(int ignore ATTRIBUTE_UNUSED)16177 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16178 {
16179   expressionS ex;
16180 
16181   file_mips_check_options ();
16182 
16183   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16184      .cprestore is ignored.  */
16185   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16186     {
16187       s_ignore (0);
16188       return;
16189     }
16190 
16191   if (mips_opts.mips16)
16192     {
16193       as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16194       ignore_rest_of_line ();
16195       return;
16196     }
16197 
16198   mips_cprestore_offset = get_absolute_expression ();
16199   mips_cprestore_valid = 1;
16200 
16201   ex.X_op = O_constant;
16202   ex.X_add_symbol = NULL;
16203   ex.X_op_symbol = NULL;
16204   ex.X_add_number = mips_cprestore_offset;
16205 
16206   mips_mark_labels ();
16207   mips_assembling_insn = TRUE;
16208 
16209   macro_start ();
16210   macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16211 				SP, HAVE_64BIT_ADDRESSES);
16212   macro_end ();
16213 
16214   mips_assembling_insn = FALSE;
16215   demand_empty_rest_of_line ();
16216 }
16217 
16218 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16219    was given in the preceding .cpsetup, it results in:
16220      ld		$gp, offset($sp)
16221 
16222    If a register $reg2 was given there, it results in:
16223      daddu	$gp, $reg2, $0  */
16224 
16225 static void
s_cpreturn(int ignore ATTRIBUTE_UNUSED)16226 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16227 {
16228   expressionS ex;
16229 
16230   file_mips_check_options ();
16231 
16232   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16233      We also need NewABI support.  */
16234   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16235     {
16236       s_ignore (0);
16237       return;
16238     }
16239 
16240   if (mips_opts.mips16)
16241     {
16242       as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16243       ignore_rest_of_line ();
16244       return;
16245     }
16246 
16247   mips_mark_labels ();
16248   mips_assembling_insn = TRUE;
16249 
16250   macro_start ();
16251   if (mips_cpreturn_register == -1)
16252     {
16253       ex.X_op = O_constant;
16254       ex.X_add_symbol = NULL;
16255       ex.X_op_symbol = NULL;
16256       ex.X_add_number = mips_cpreturn_offset;
16257 
16258       macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16259     }
16260   else
16261     macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
16262 		 mips_cpreturn_register, 0);
16263   macro_end ();
16264 
16265   mips_assembling_insn = FALSE;
16266   demand_empty_rest_of_line ();
16267 }
16268 
16269 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16270    pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16271    DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16272    debug information or MIPS16 TLS.  */
16273 
16274 static void
s_tls_rel_directive(const size_t bytes,const char * dirstr,bfd_reloc_code_real_type rtype)16275 s_tls_rel_directive (const size_t bytes, const char *dirstr,
16276 		     bfd_reloc_code_real_type rtype)
16277 {
16278   expressionS ex;
16279   char *p;
16280 
16281   expression (&ex);
16282 
16283   if (ex.X_op != O_symbol)
16284     {
16285       as_bad (_("unsupported use of %s"), dirstr);
16286       ignore_rest_of_line ();
16287     }
16288 
16289   p = frag_more (bytes);
16290   md_number_to_chars (p, 0, bytes);
16291   fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
16292   demand_empty_rest_of_line ();
16293   mips_clear_insn_labels ();
16294 }
16295 
16296 /* Handle .dtprelword.  */
16297 
16298 static void
s_dtprelword(int ignore ATTRIBUTE_UNUSED)16299 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16300 {
16301   s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
16302 }
16303 
16304 /* Handle .dtpreldword.  */
16305 
16306 static void
s_dtpreldword(int ignore ATTRIBUTE_UNUSED)16307 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16308 {
16309   s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16310 }
16311 
16312 /* Handle .tprelword.  */
16313 
16314 static void
s_tprelword(int ignore ATTRIBUTE_UNUSED)16315 s_tprelword (int ignore ATTRIBUTE_UNUSED)
16316 {
16317   s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16318 }
16319 
16320 /* Handle .tpreldword.  */
16321 
16322 static void
s_tpreldword(int ignore ATTRIBUTE_UNUSED)16323 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16324 {
16325   s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
16326 }
16327 
16328 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
16329    code.  It sets the offset to use in gp_rel relocations.  */
16330 
16331 static void
s_gpvalue(int ignore ATTRIBUTE_UNUSED)16332 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
16333 {
16334   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16335      We also need NewABI support.  */
16336   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16337     {
16338       s_ignore (0);
16339       return;
16340     }
16341 
16342   mips_gprel_offset = get_absolute_expression ();
16343 
16344   demand_empty_rest_of_line ();
16345 }
16346 
16347 /* Handle the .gpword pseudo-op.  This is used when generating PIC
16348    code.  It generates a 32 bit GP relative reloc.  */
16349 
16350 static void
s_gpword(int ignore ATTRIBUTE_UNUSED)16351 s_gpword (int ignore ATTRIBUTE_UNUSED)
16352 {
16353   segment_info_type *si;
16354   struct insn_label_list *l;
16355   expressionS ex;
16356   char *p;
16357 
16358   /* When not generating PIC code, this is treated as .word.  */
16359   if (mips_pic != SVR4_PIC)
16360     {
16361       s_cons (2);
16362       return;
16363     }
16364 
16365   si = seg_info (now_seg);
16366   l = si->label_list;
16367   mips_emit_delays ();
16368   if (auto_align)
16369     mips_align (2, 0, l);
16370 
16371   expression (&ex);
16372   mips_clear_insn_labels ();
16373 
16374   if (ex.X_op != O_symbol || ex.X_add_number != 0)
16375     {
16376       as_bad (_("unsupported use of .gpword"));
16377       ignore_rest_of_line ();
16378     }
16379 
16380   p = frag_more (4);
16381   md_number_to_chars (p, 0, 4);
16382   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16383 	       BFD_RELOC_GPREL32);
16384 
16385   demand_empty_rest_of_line ();
16386 }
16387 
16388 static void
s_gpdword(int ignore ATTRIBUTE_UNUSED)16389 s_gpdword (int ignore ATTRIBUTE_UNUSED)
16390 {
16391   segment_info_type *si;
16392   struct insn_label_list *l;
16393   expressionS ex;
16394   char *p;
16395 
16396   /* When not generating PIC code, this is treated as .dword.  */
16397   if (mips_pic != SVR4_PIC)
16398     {
16399       s_cons (3);
16400       return;
16401     }
16402 
16403   si = seg_info (now_seg);
16404   l = si->label_list;
16405   mips_emit_delays ();
16406   if (auto_align)
16407     mips_align (3, 0, l);
16408 
16409   expression (&ex);
16410   mips_clear_insn_labels ();
16411 
16412   if (ex.X_op != O_symbol || ex.X_add_number != 0)
16413     {
16414       as_bad (_("unsupported use of .gpdword"));
16415       ignore_rest_of_line ();
16416     }
16417 
16418   p = frag_more (8);
16419   md_number_to_chars (p, 0, 8);
16420   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16421 	       BFD_RELOC_GPREL32)->fx_tcbit = 1;
16422 
16423   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
16424   fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
16425 	   FALSE, BFD_RELOC_64)->fx_tcbit = 1;
16426 
16427   demand_empty_rest_of_line ();
16428 }
16429 
16430 /* Handle the .ehword pseudo-op.  This is used when generating unwinding
16431    tables.  It generates a R_MIPS_EH reloc.  */
16432 
16433 static void
s_ehword(int ignore ATTRIBUTE_UNUSED)16434 s_ehword (int ignore ATTRIBUTE_UNUSED)
16435 {
16436   expressionS ex;
16437   char *p;
16438 
16439   mips_emit_delays ();
16440 
16441   expression (&ex);
16442   mips_clear_insn_labels ();
16443 
16444   if (ex.X_op != O_symbol || ex.X_add_number != 0)
16445     {
16446       as_bad (_("unsupported use of .ehword"));
16447       ignore_rest_of_line ();
16448     }
16449 
16450   p = frag_more (4);
16451   md_number_to_chars (p, 0, 4);
16452   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16453 	       BFD_RELOC_MIPS_EH);
16454 
16455   demand_empty_rest_of_line ();
16456 }
16457 
16458 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
16459    tables in SVR4 PIC code.  */
16460 
16461 static void
s_cpadd(int ignore ATTRIBUTE_UNUSED)16462 s_cpadd (int ignore ATTRIBUTE_UNUSED)
16463 {
16464   int reg;
16465 
16466   file_mips_check_options ();
16467 
16468   /* This is ignored when not generating SVR4 PIC code.  */
16469   if (mips_pic != SVR4_PIC)
16470     {
16471       s_ignore (0);
16472       return;
16473     }
16474 
16475   mips_mark_labels ();
16476   mips_assembling_insn = TRUE;
16477 
16478   /* Add $gp to the register named as an argument.  */
16479   macro_start ();
16480   reg = tc_get_register (0);
16481   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
16482   macro_end ();
16483 
16484   mips_assembling_insn = FALSE;
16485   demand_empty_rest_of_line ();
16486 }
16487 
16488 /* Handle the .insn pseudo-op.  This marks instruction labels in
16489    mips16/micromips mode.  This permits the linker to handle them specially,
16490    such as generating jalx instructions when needed.  We also make
16491    them odd for the duration of the assembly, in order to generate the
16492    right sort of code.  We will make them even in the adjust_symtab
16493    routine, while leaving them marked.  This is convenient for the
16494    debugger and the disassembler.  The linker knows to make them odd
16495    again.  */
16496 
16497 static void
s_insn(int ignore ATTRIBUTE_UNUSED)16498 s_insn (int ignore ATTRIBUTE_UNUSED)
16499 {
16500   file_mips_check_options ();
16501   file_ase_mips16 |= mips_opts.mips16;
16502   file_ase_micromips |= mips_opts.micromips;
16503 
16504   mips_mark_labels ();
16505 
16506   demand_empty_rest_of_line ();
16507 }
16508 
16509 /* Handle the .nan pseudo-op.  */
16510 
16511 static void
s_nan(int ignore ATTRIBUTE_UNUSED)16512 s_nan (int ignore ATTRIBUTE_UNUSED)
16513 {
16514   static const char str_legacy[] = "legacy";
16515   static const char str_2008[] = "2008";
16516   size_t i;
16517 
16518   for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
16519 
16520   if (i == sizeof (str_2008) - 1
16521       && memcmp (input_line_pointer, str_2008, i) == 0)
16522     mips_nan2008 = 1;
16523   else if (i == sizeof (str_legacy) - 1
16524 	   && memcmp (input_line_pointer, str_legacy, i) == 0)
16525     {
16526       if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
16527 	mips_nan2008 = 0;
16528       else
16529 	as_bad (_("`%s' does not support legacy NaN"),
16530 	          mips_cpu_info_from_isa (file_mips_opts.isa)->name);
16531     }
16532   else
16533     as_bad (_("bad .nan directive"));
16534 
16535   input_line_pointer += i;
16536   demand_empty_rest_of_line ();
16537 }
16538 
16539 /* Handle a .stab[snd] directive.  Ideally these directives would be
16540    implemented in a transparent way, so that removing them would not
16541    have any effect on the generated instructions.  However, s_stab
16542    internally changes the section, so in practice we need to decide
16543    now whether the preceding label marks compressed code.  We do not
16544    support changing the compression mode of a label after a .stab*
16545    directive, such as in:
16546 
16547    foo:
16548    	.stabs ...
16549 	.set mips16
16550 
16551    so the current mode wins.  */
16552 
16553 static void
s_mips_stab(int type)16554 s_mips_stab (int type)
16555 {
16556   mips_mark_labels ();
16557   s_stab (type);
16558 }
16559 
16560 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.  */
16561 
16562 static void
s_mips_weakext(int ignore ATTRIBUTE_UNUSED)16563 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
16564 {
16565   char *name;
16566   int c;
16567   symbolS *symbolP;
16568   expressionS exp;
16569 
16570   name = input_line_pointer;
16571   c = get_symbol_end ();
16572   symbolP = symbol_find_or_make (name);
16573   S_SET_WEAK (symbolP);
16574   *input_line_pointer = c;
16575 
16576   SKIP_WHITESPACE ();
16577 
16578   if (! is_end_of_line[(unsigned char) *input_line_pointer])
16579     {
16580       if (S_IS_DEFINED (symbolP))
16581 	{
16582 	  as_bad (_("ignoring attempt to redefine symbol %s"),
16583 		  S_GET_NAME (symbolP));
16584 	  ignore_rest_of_line ();
16585 	  return;
16586 	}
16587 
16588       if (*input_line_pointer == ',')
16589 	{
16590 	  ++input_line_pointer;
16591 	  SKIP_WHITESPACE ();
16592 	}
16593 
16594       expression (&exp);
16595       if (exp.X_op != O_symbol)
16596 	{
16597 	  as_bad (_("bad .weakext directive"));
16598 	  ignore_rest_of_line ();
16599 	  return;
16600 	}
16601       symbol_set_value_expression (symbolP, &exp);
16602     }
16603 
16604   demand_empty_rest_of_line ();
16605 }
16606 
16607 /* Parse a register string into a number.  Called from the ECOFF code
16608    to parse .frame.  The argument is non-zero if this is the frame
16609    register, so that we can record it in mips_frame_reg.  */
16610 
16611 int
tc_get_register(int frame)16612 tc_get_register (int frame)
16613 {
16614   unsigned int reg;
16615 
16616   SKIP_WHITESPACE ();
16617   if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
16618     reg = 0;
16619   if (frame)
16620     {
16621       mips_frame_reg = reg != 0 ? reg : SP;
16622       mips_frame_reg_valid = 1;
16623       mips_cprestore_valid = 0;
16624     }
16625   return reg;
16626 }
16627 
16628 valueT
md_section_align(asection * seg,valueT addr)16629 md_section_align (asection *seg, valueT addr)
16630 {
16631   int align = bfd_get_section_alignment (stdoutput, seg);
16632 
16633   /* We don't need to align ELF sections to the full alignment.
16634      However, Irix 5 may prefer that we align them at least to a 16
16635      byte boundary.  We don't bother to align the sections if we
16636      are targeted for an embedded system.  */
16637   if (strncmp (TARGET_OS, "elf", 3) == 0)
16638     return addr;
16639   if (align > 4)
16640     align = 4;
16641 
16642   return ((addr + (1 << align) - 1) & (-1 << align));
16643 }
16644 
16645 /* Utility routine, called from above as well.  If called while the
16646    input file is still being read, it's only an approximation.  (For
16647    example, a symbol may later become defined which appeared to be
16648    undefined earlier.)  */
16649 
16650 static int
nopic_need_relax(symbolS * sym,int before_relaxing)16651 nopic_need_relax (symbolS *sym, int before_relaxing)
16652 {
16653   if (sym == 0)
16654     return 0;
16655 
16656   if (g_switch_value > 0)
16657     {
16658       const char *symname;
16659       int change;
16660 
16661       /* Find out whether this symbol can be referenced off the $gp
16662 	 register.  It can be if it is smaller than the -G size or if
16663 	 it is in the .sdata or .sbss section.  Certain symbols can
16664 	 not be referenced off the $gp, although it appears as though
16665 	 they can.  */
16666       symname = S_GET_NAME (sym);
16667       if (symname != (const char *) NULL
16668 	  && (strcmp (symname, "eprol") == 0
16669 	      || strcmp (symname, "etext") == 0
16670 	      || strcmp (symname, "_gp") == 0
16671 	      || strcmp (symname, "edata") == 0
16672 	      || strcmp (symname, "_fbss") == 0
16673 	      || strcmp (symname, "_fdata") == 0
16674 	      || strcmp (symname, "_ftext") == 0
16675 	      || strcmp (symname, "end") == 0
16676 	      || strcmp (symname, "_gp_disp") == 0))
16677 	change = 1;
16678       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
16679 	       && (0
16680 #ifndef NO_ECOFF_DEBUGGING
16681 		   || (symbol_get_obj (sym)->ecoff_extern_size != 0
16682 		       && (symbol_get_obj (sym)->ecoff_extern_size
16683 			   <= g_switch_value))
16684 #endif
16685 		   /* We must defer this decision until after the whole
16686 		      file has been read, since there might be a .extern
16687 		      after the first use of this symbol.  */
16688 		   || (before_relaxing
16689 #ifndef NO_ECOFF_DEBUGGING
16690 		       && symbol_get_obj (sym)->ecoff_extern_size == 0
16691 #endif
16692 		       && S_GET_VALUE (sym) == 0)
16693 		   || (S_GET_VALUE (sym) != 0
16694 		       && S_GET_VALUE (sym) <= g_switch_value)))
16695 	change = 0;
16696       else
16697 	{
16698 	  const char *segname;
16699 
16700 	  segname = segment_name (S_GET_SEGMENT (sym));
16701 	  gas_assert (strcmp (segname, ".lit8") != 0
16702 		  && strcmp (segname, ".lit4") != 0);
16703 	  change = (strcmp (segname, ".sdata") != 0
16704 		    && strcmp (segname, ".sbss") != 0
16705 		    && strncmp (segname, ".sdata.", 7) != 0
16706 		    && strncmp (segname, ".sbss.", 6) != 0
16707 		    && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
16708 		    && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
16709 	}
16710       return change;
16711     }
16712   else
16713     /* We are not optimizing for the $gp register.  */
16714     return 1;
16715 }
16716 
16717 
16718 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
16719 
16720 static bfd_boolean
pic_need_relax(symbolS * sym,asection * segtype)16721 pic_need_relax (symbolS *sym, asection *segtype)
16722 {
16723   asection *symsec;
16724 
16725   /* Handle the case of a symbol equated to another symbol.  */
16726   while (symbol_equated_reloc_p (sym))
16727     {
16728       symbolS *n;
16729 
16730       /* It's possible to get a loop here in a badly written program.  */
16731       n = symbol_get_value_expression (sym)->X_add_symbol;
16732       if (n == sym)
16733 	break;
16734       sym = n;
16735     }
16736 
16737   if (symbol_section_p (sym))
16738     return TRUE;
16739 
16740   symsec = S_GET_SEGMENT (sym);
16741 
16742   /* This must duplicate the test in adjust_reloc_syms.  */
16743   return (!bfd_is_und_section (symsec)
16744 	  && !bfd_is_abs_section (symsec)
16745 	  && !bfd_is_com_section (symsec)
16746 	  && !s_is_linkonce (sym, segtype)
16747 	  /* A global or weak symbol is treated as external.  */
16748 	  && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
16749 }
16750 
16751 
16752 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
16753    extended opcode.  SEC is the section the frag is in.  */
16754 
16755 static int
mips16_extended_frag(fragS * fragp,asection * sec,long stretch)16756 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
16757 {
16758   int type;
16759   const struct mips_int_operand *operand;
16760   offsetT val;
16761   segT symsec;
16762   fragS *sym_frag;
16763 
16764   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
16765     return 0;
16766   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
16767     return 1;
16768 
16769   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
16770   operand = mips16_immed_operand (type, FALSE);
16771 
16772   sym_frag = symbol_get_frag (fragp->fr_symbol);
16773   val = S_GET_VALUE (fragp->fr_symbol);
16774   symsec = S_GET_SEGMENT (fragp->fr_symbol);
16775 
16776   if (operand->root.type == OP_PCREL)
16777     {
16778       const struct mips_pcrel_operand *pcrel_op;
16779       addressT addr;
16780       offsetT maxtiny;
16781 
16782       /* We won't have the section when we are called from
16783          mips_relax_frag.  However, we will always have been called
16784          from md_estimate_size_before_relax first.  If this is a
16785          branch to a different section, we mark it as such.  If SEC is
16786          NULL, and the frag is not marked, then it must be a branch to
16787          the same section.  */
16788       pcrel_op = (const struct mips_pcrel_operand *) operand;
16789       if (sec == NULL)
16790 	{
16791 	  if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
16792 	    return 1;
16793 	}
16794       else
16795 	{
16796 	  /* Must have been called from md_estimate_size_before_relax.  */
16797 	  if (symsec != sec)
16798 	    {
16799 	      fragp->fr_subtype =
16800 		RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16801 
16802 	      /* FIXME: We should support this, and let the linker
16803                  catch branches and loads that are out of range.  */
16804 	      as_bad_where (fragp->fr_file, fragp->fr_line,
16805 			    _("unsupported PC relative reference to different section"));
16806 
16807 	      return 1;
16808 	    }
16809 	  if (fragp != sym_frag && sym_frag->fr_address == 0)
16810 	    /* Assume non-extended on the first relaxation pass.
16811 	       The address we have calculated will be bogus if this is
16812 	       a forward branch to another frag, as the forward frag
16813 	       will have fr_address == 0.  */
16814 	    return 0;
16815 	}
16816 
16817       /* In this case, we know for sure that the symbol fragment is in
16818 	 the same section.  If the relax_marker of the symbol fragment
16819 	 differs from the relax_marker of this fragment, we have not
16820 	 yet adjusted the symbol fragment fr_address.  We want to add
16821 	 in STRETCH in order to get a better estimate of the address.
16822 	 This particularly matters because of the shift bits.  */
16823       if (stretch != 0
16824 	  && sym_frag->relax_marker != fragp->relax_marker)
16825 	{
16826 	  fragS *f;
16827 
16828 	  /* Adjust stretch for any alignment frag.  Note that if have
16829              been expanding the earlier code, the symbol may be
16830              defined in what appears to be an earlier frag.  FIXME:
16831              This doesn't handle the fr_subtype field, which specifies
16832              a maximum number of bytes to skip when doing an
16833              alignment.  */
16834 	  for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
16835 	    {
16836 	      if (f->fr_type == rs_align || f->fr_type == rs_align_code)
16837 		{
16838 		  if (stretch < 0)
16839 		    stretch = - ((- stretch)
16840 				 & ~ ((1 << (int) f->fr_offset) - 1));
16841 		  else
16842 		    stretch &= ~ ((1 << (int) f->fr_offset) - 1);
16843 		  if (stretch == 0)
16844 		    break;
16845 		}
16846 	    }
16847 	  if (f != NULL)
16848 	    val += stretch;
16849 	}
16850 
16851       addr = fragp->fr_address + fragp->fr_fix;
16852 
16853       /* The base address rules are complicated.  The base address of
16854          a branch is the following instruction.  The base address of a
16855          PC relative load or add is the instruction itself, but if it
16856          is in a delay slot (in which case it can not be extended) use
16857          the address of the instruction whose delay slot it is in.  */
16858       if (pcrel_op->include_isa_bit)
16859 	{
16860 	  addr += 2;
16861 
16862 	  /* If we are currently assuming that this frag should be
16863 	     extended, then, the current address is two bytes
16864 	     higher.  */
16865 	  if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16866 	    addr += 2;
16867 
16868 	  /* Ignore the low bit in the target, since it will be set
16869              for a text label.  */
16870 	  val &= -2;
16871 	}
16872       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
16873 	addr -= 4;
16874       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
16875 	addr -= 2;
16876 
16877       val -= addr & -(1 << pcrel_op->align_log2);
16878 
16879       /* If any of the shifted bits are set, we must use an extended
16880          opcode.  If the address depends on the size of this
16881          instruction, this can lead to a loop, so we arrange to always
16882          use an extended opcode.  We only check this when we are in
16883          the main relaxation loop, when SEC is NULL.  */
16884       if ((val & ((1 << operand->shift) - 1)) != 0 && sec == NULL)
16885 	{
16886 	  fragp->fr_subtype =
16887 	    RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16888 	  return 1;
16889 	}
16890 
16891       /* If we are about to mark a frag as extended because the value
16892          is precisely the next value above maxtiny, then there is a
16893          chance of an infinite loop as in the following code:
16894 	     la	$4,foo
16895 	     .skip	1020
16896 	     .align	2
16897 	   foo:
16898 	 In this case when the la is extended, foo is 0x3fc bytes
16899 	 away, so the la can be shrunk, but then foo is 0x400 away, so
16900 	 the la must be extended.  To avoid this loop, we mark the
16901 	 frag as extended if it was small, and is about to become
16902 	 extended with the next value above maxtiny.  */
16903       maxtiny = mips_int_operand_max (operand);
16904       if (val == maxtiny + (1 << operand->shift)
16905 	  && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
16906 	  && sec == NULL)
16907 	{
16908 	  fragp->fr_subtype =
16909 	    RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16910 	  return 1;
16911 	}
16912     }
16913   else if (symsec != absolute_section && sec != NULL)
16914     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
16915 
16916   return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
16917 }
16918 
16919 /* Compute the length of a branch sequence, and adjust the
16920    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
16921    worst-case length is computed, with UPDATE being used to indicate
16922    whether an unconditional (-1), branch-likely (+1) or regular (0)
16923    branch is to be computed.  */
16924 static int
relaxed_branch_length(fragS * fragp,asection * sec,int update)16925 relaxed_branch_length (fragS *fragp, asection *sec, int update)
16926 {
16927   bfd_boolean toofar;
16928   int length;
16929 
16930   if (fragp
16931       && S_IS_DEFINED (fragp->fr_symbol)
16932       && sec == S_GET_SEGMENT (fragp->fr_symbol))
16933     {
16934       addressT addr;
16935       offsetT val;
16936 
16937       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16938 
16939       addr = fragp->fr_address + fragp->fr_fix + 4;
16940 
16941       val -= addr;
16942 
16943       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
16944     }
16945   else if (fragp)
16946     /* If the symbol is not defined or it's in a different segment,
16947        assume the user knows what's going on and emit a short
16948        branch.  */
16949     toofar = FALSE;
16950   else
16951     toofar = TRUE;
16952 
16953   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16954     fragp->fr_subtype
16955       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
16956 			     RELAX_BRANCH_UNCOND (fragp->fr_subtype),
16957 			     RELAX_BRANCH_LIKELY (fragp->fr_subtype),
16958 			     RELAX_BRANCH_LINK (fragp->fr_subtype),
16959 			     toofar);
16960 
16961   length = 4;
16962   if (toofar)
16963     {
16964       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
16965 	length += 8;
16966 
16967       if (mips_pic != NO_PIC)
16968 	{
16969 	  /* Additional space for PIC loading of target address.  */
16970 	  length += 8;
16971 	  if (mips_opts.isa == ISA_MIPS1)
16972 	    /* Additional space for $at-stabilizing nop.  */
16973 	    length += 4;
16974 	}
16975 
16976       /* If branch is conditional.  */
16977       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
16978 	length += 8;
16979     }
16980 
16981   return length;
16982 }
16983 
16984 /* Compute the length of a branch sequence, and adjust the
16985    RELAX_MICROMIPS_TOOFAR32 bit accordingly.  If FRAGP is NULL, the
16986    worst-case length is computed, with UPDATE being used to indicate
16987    whether an unconditional (-1), or regular (0) branch is to be
16988    computed.  */
16989 
16990 static int
relaxed_micromips_32bit_branch_length(fragS * fragp,asection * sec,int update)16991 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
16992 {
16993   bfd_boolean toofar;
16994   int length;
16995 
16996   if (fragp
16997       && S_IS_DEFINED (fragp->fr_symbol)
16998       && sec == S_GET_SEGMENT (fragp->fr_symbol))
16999     {
17000       addressT addr;
17001       offsetT val;
17002 
17003       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17004       /* Ignore the low bit in the target, since it will be set
17005 	 for a text label.  */
17006       if ((val & 1) != 0)
17007 	--val;
17008 
17009       addr = fragp->fr_address + fragp->fr_fix + 4;
17010 
17011       val -= addr;
17012 
17013       toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17014     }
17015   else if (fragp)
17016     /* If the symbol is not defined or it's in a different segment,
17017        assume the user knows what's going on and emit a short
17018        branch.  */
17019     toofar = FALSE;
17020   else
17021     toofar = TRUE;
17022 
17023   if (fragp && update
17024       && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17025     fragp->fr_subtype = (toofar
17026 			 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17027 			 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17028 
17029   length = 4;
17030   if (toofar)
17031     {
17032       bfd_boolean compact_known = fragp != NULL;
17033       bfd_boolean compact = FALSE;
17034       bfd_boolean uncond;
17035 
17036       if (compact_known)
17037 	compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17038       if (fragp)
17039 	uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17040       else
17041 	uncond = update < 0;
17042 
17043       /* If label is out of range, we turn branch <br>:
17044 
17045 		<br>	label			# 4 bytes
17046 	    0:
17047 
17048          into:
17049 
17050 		j	label			# 4 bytes
17051 		nop				# 2 bytes if compact && !PIC
17052 	    0:
17053        */
17054       if (mips_pic == NO_PIC && (!compact_known || compact))
17055 	length += 2;
17056 
17057       /* If assembling PIC code, we further turn:
17058 
17059 			j	label			# 4 bytes
17060 
17061          into:
17062 
17063 			lw/ld	at, %got(label)(gp)	# 4 bytes
17064 			d/addiu	at, %lo(label)		# 4 bytes
17065 			jr/c	at			# 2 bytes
17066        */
17067       if (mips_pic != NO_PIC)
17068 	length += 6;
17069 
17070       /* If branch <br> is conditional, we prepend negated branch <brneg>:
17071 
17072 			<brneg>	0f			# 4 bytes
17073 			nop				# 2 bytes if !compact
17074        */
17075       if (!uncond)
17076 	length += (compact_known && compact) ? 4 : 6;
17077     }
17078 
17079   return length;
17080 }
17081 
17082 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17083    bit accordingly.  */
17084 
17085 static int
relaxed_micromips_16bit_branch_length(fragS * fragp,asection * sec,int update)17086 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17087 {
17088   bfd_boolean toofar;
17089 
17090   if (fragp
17091       && S_IS_DEFINED (fragp->fr_symbol)
17092       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17093     {
17094       addressT addr;
17095       offsetT val;
17096       int type;
17097 
17098       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17099       /* Ignore the low bit in the target, since it will be set
17100 	 for a text label.  */
17101       if ((val & 1) != 0)
17102 	--val;
17103 
17104       /* Assume this is a 2-byte branch.  */
17105       addr = fragp->fr_address + fragp->fr_fix + 2;
17106 
17107       /* We try to avoid the infinite loop by not adding 2 more bytes for
17108 	 long branches.  */
17109 
17110       val -= addr;
17111 
17112       type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17113       if (type == 'D')
17114 	toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17115       else if (type == 'E')
17116 	toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17117       else
17118 	abort ();
17119     }
17120   else
17121     /* If the symbol is not defined or it's in a different segment,
17122        we emit a normal 32-bit branch.  */
17123     toofar = TRUE;
17124 
17125   if (fragp && update
17126       && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17127     fragp->fr_subtype
17128       = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17129 	       : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17130 
17131   if (toofar)
17132     return 4;
17133 
17134   return 2;
17135 }
17136 
17137 /* Estimate the size of a frag before relaxing.  Unless this is the
17138    mips16, we are not really relaxing here, and the final size is
17139    encoded in the subtype information.  For the mips16, we have to
17140    decide whether we are using an extended opcode or not.  */
17141 
17142 int
md_estimate_size_before_relax(fragS * fragp,asection * segtype)17143 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17144 {
17145   int change;
17146 
17147   if (RELAX_BRANCH_P (fragp->fr_subtype))
17148     {
17149 
17150       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17151 
17152       return fragp->fr_var;
17153     }
17154 
17155   if (RELAX_MIPS16_P (fragp->fr_subtype))
17156     /* We don't want to modify the EXTENDED bit here; it might get us
17157        into infinite loops.  We change it only in mips_relax_frag().  */
17158     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
17159 
17160   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17161     {
17162       int length = 4;
17163 
17164       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17165 	length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17166       if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17167 	length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17168       fragp->fr_var = length;
17169 
17170       return length;
17171     }
17172 
17173   if (mips_pic == NO_PIC)
17174     change = nopic_need_relax (fragp->fr_symbol, 0);
17175   else if (mips_pic == SVR4_PIC)
17176     change = pic_need_relax (fragp->fr_symbol, segtype);
17177   else if (mips_pic == VXWORKS_PIC)
17178     /* For vxworks, GOT16 relocations never have a corresponding LO16.  */
17179     change = 0;
17180   else
17181     abort ();
17182 
17183   if (change)
17184     {
17185       fragp->fr_subtype |= RELAX_USE_SECOND;
17186       return -RELAX_FIRST (fragp->fr_subtype);
17187     }
17188   else
17189     return -RELAX_SECOND (fragp->fr_subtype);
17190 }
17191 
17192 /* This is called to see whether a reloc against a defined symbol
17193    should be converted into a reloc against a section.  */
17194 
17195 int
mips_fix_adjustable(fixS * fixp)17196 mips_fix_adjustable (fixS *fixp)
17197 {
17198   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17199       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17200     return 0;
17201 
17202   if (fixp->fx_addsy == NULL)
17203     return 1;
17204 
17205   /* If symbol SYM is in a mergeable section, relocations of the form
17206      SYM + 0 can usually be made section-relative.  The mergeable data
17207      is then identified by the section offset rather than by the symbol.
17208 
17209      However, if we're generating REL LO16 relocations, the offset is split
17210      between the LO16 and parterning high part relocation.  The linker will
17211      need to recalculate the complete offset in order to correctly identify
17212      the merge data.
17213 
17214      The linker has traditionally not looked for the parterning high part
17215      relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17216      placed anywhere.  Rather than break backwards compatibility by changing
17217      this, it seems better not to force the issue, and instead keep the
17218      original symbol.  This will work with either linker behavior.  */
17219   if ((lo16_reloc_p (fixp->fx_r_type)
17220        || reloc_needs_lo_p (fixp->fx_r_type))
17221       && HAVE_IN_PLACE_ADDENDS
17222       && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17223     return 0;
17224 
17225   /* There is no place to store an in-place offset for JALR relocations.
17226      Likewise an in-range offset of limited PC-relative relocations may
17227      overflow the in-place relocatable field if recalculated against the
17228      start address of the symbol's containing section.
17229 
17230      Also, PC relative relocations for MIPS R6 need to be symbol rather than
17231      section relative to allow linker relaxations to be performed later on.  */
17232   if ((HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (mips_opts.isa))
17233       && (limited_pcrel_reloc_p (fixp->fx_r_type)
17234 	  || jalr_reloc_p (fixp->fx_r_type)))
17235     return 0;
17236 
17237   /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17238      to a floating-point stub.  The same is true for non-R_MIPS16_26
17239      relocations against MIPS16 functions; in this case, the stub becomes
17240      the function's canonical address.
17241 
17242      Floating-point stubs are stored in unique .mips16.call.* or
17243      .mips16.fn.* sections.  If a stub T for function F is in section S,
17244      the first relocation in section S must be against F; this is how the
17245      linker determines the target function.  All relocations that might
17246      resolve to T must also be against F.  We therefore have the following
17247      restrictions, which are given in an intentionally-redundant way:
17248 
17249        1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17250 	  symbols.
17251 
17252        2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17253 	  if that stub might be used.
17254 
17255        3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17256 	  symbols.
17257 
17258        4. We cannot reduce a stub's relocations against MIPS16 symbols if
17259 	  that stub might be used.
17260 
17261      There is a further restriction:
17262 
17263        5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17264 	  R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17265 	  targets with in-place addends; the relocation field cannot
17266 	  encode the low bit.
17267 
17268      For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17269      against a MIPS16 symbol.  We deal with (5) by by not reducing any
17270      such relocations on REL targets.
17271 
17272      We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17273      relocation against some symbol R, no relocation against R may be
17274      reduced.  (Note that this deals with (2) as well as (1) because
17275      relocations against global symbols will never be reduced on ELF
17276      targets.)  This approach is a little simpler than trying to detect
17277      stub sections, and gives the "all or nothing" per-symbol consistency
17278      that we have for MIPS16 symbols.  */
17279   if (fixp->fx_subsy == NULL
17280       && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
17281 	  || *symbol_get_tc (fixp->fx_addsy)
17282 	  || (HAVE_IN_PLACE_ADDENDS
17283 	      && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17284 	      && jmp_reloc_p (fixp->fx_r_type))))
17285     return 0;
17286 
17287   return 1;
17288 }
17289 
17290 /* Translate internal representation of relocation info to BFD target
17291    format.  */
17292 
17293 arelent **
tc_gen_reloc(asection * section ATTRIBUTE_UNUSED,fixS * fixp)17294 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
17295 {
17296   static arelent *retval[4];
17297   arelent *reloc;
17298   bfd_reloc_code_real_type code;
17299 
17300   memset (retval, 0, sizeof(retval));
17301   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
17302   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
17303   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
17304   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17305 
17306   if (fixp->fx_pcrel)
17307     {
17308       gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17309 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17310 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17311 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
17312 		  || fixp->fx_r_type == BFD_RELOC_32_PCREL
17313 		  || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
17314 		  || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
17315 		  || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
17316 		  || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
17317 		  || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
17318 		  || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
17319 
17320       /* At this point, fx_addnumber is "symbol offset - pcrel address".
17321 	 Relocations want only the symbol offset.  */
17322       reloc->addend = fixp->fx_addnumber + reloc->address;
17323     }
17324   else
17325     reloc->addend = fixp->fx_addnumber;
17326 
17327   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17328      entry to be used in the relocation's section offset.  */
17329   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17330     {
17331       reloc->address = reloc->addend;
17332       reloc->addend = 0;
17333     }
17334 
17335   code = fixp->fx_r_type;
17336 
17337   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
17338   if (reloc->howto == NULL)
17339     {
17340       as_bad_where (fixp->fx_file, fixp->fx_line,
17341 		    _("cannot represent %s relocation in this object file"
17342 		      " format"),
17343 		    bfd_get_reloc_code_name (code));
17344       retval[0] = NULL;
17345     }
17346 
17347   return retval;
17348 }
17349 
17350 /* Relax a machine dependent frag.  This returns the amount by which
17351    the current size of the frag should change.  */
17352 
17353 int
mips_relax_frag(asection * sec,fragS * fragp,long stretch)17354 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
17355 {
17356   if (RELAX_BRANCH_P (fragp->fr_subtype))
17357     {
17358       offsetT old_var = fragp->fr_var;
17359 
17360       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
17361 
17362       return fragp->fr_var - old_var;
17363     }
17364 
17365   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17366     {
17367       offsetT old_var = fragp->fr_var;
17368       offsetT new_var = 4;
17369 
17370       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17371 	new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17372       if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17373 	new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17374       fragp->fr_var = new_var;
17375 
17376       return new_var - old_var;
17377     }
17378 
17379   if (! RELAX_MIPS16_P (fragp->fr_subtype))
17380     return 0;
17381 
17382   if (mips16_extended_frag (fragp, NULL, stretch))
17383     {
17384       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17385 	return 0;
17386       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17387       return 2;
17388     }
17389   else
17390     {
17391       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17392 	return 0;
17393       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17394       return -2;
17395     }
17396 
17397   return 0;
17398 }
17399 
17400 /* Convert a machine dependent frag.  */
17401 
17402 void
md_convert_frag(bfd * abfd ATTRIBUTE_UNUSED,segT asec,fragS * fragp)17403 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
17404 {
17405   if (RELAX_BRANCH_P (fragp->fr_subtype))
17406     {
17407       char *buf;
17408       unsigned long insn;
17409       expressionS exp;
17410       fixS *fixp;
17411 
17412       buf = fragp->fr_literal + fragp->fr_fix;
17413       insn = read_insn (buf);
17414 
17415       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17416 	{
17417 	  /* We generate a fixup instead of applying it right now
17418 	     because, if there are linker relaxations, we're going to
17419 	     need the relocations.  */
17420 	  exp.X_op = O_symbol;
17421 	  exp.X_add_symbol = fragp->fr_symbol;
17422 	  exp.X_add_number = fragp->fr_offset;
17423 
17424 	  fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
17425 			      BFD_RELOC_16_PCREL_S2);
17426 	  fixp->fx_file = fragp->fr_file;
17427 	  fixp->fx_line = fragp->fr_line;
17428 
17429 	  buf = write_insn (buf, insn);
17430 	}
17431       else
17432 	{
17433 	  int i;
17434 
17435 	  as_warn_where (fragp->fr_file, fragp->fr_line,
17436 			 _("relaxed out-of-range branch into a jump"));
17437 
17438 	  if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
17439 	    goto uncond;
17440 
17441 	  if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17442 	    {
17443 	      /* Reverse the branch.  */
17444 	      switch ((insn >> 28) & 0xf)
17445 		{
17446 		case 4:
17447 		  if ((insn & 0xff000000) == 0x47000000
17448 		      || (insn & 0xff600000) == 0x45600000)
17449 		    {
17450 		      /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
17451 			 reversed by tweaking bit 23.  */
17452 		      insn ^= 0x00800000;
17453 		    }
17454 		  else
17455 		    {
17456 		      /* bc[0-3][tf]l? instructions can have the condition
17457 			 reversed by tweaking a single TF bit, and their
17458 			 opcodes all have 0x4???????.  */
17459 		      gas_assert ((insn & 0xf3e00000) == 0x41000000);
17460 		      insn ^= 0x00010000;
17461 		    }
17462 		  break;
17463 
17464 		case 0:
17465 		  /* bltz	0x04000000	bgez	0x04010000
17466 		     bltzal	0x04100000	bgezal	0x04110000  */
17467 		  gas_assert ((insn & 0xfc0e0000) == 0x04000000);
17468 		  insn ^= 0x00010000;
17469 		  break;
17470 
17471 		case 1:
17472 		  /* beq	0x10000000	bne	0x14000000
17473 		     blez	0x18000000	bgtz	0x1c000000  */
17474 		  insn ^= 0x04000000;
17475 		  break;
17476 
17477 		default:
17478 		  abort ();
17479 		}
17480 	    }
17481 
17482 	  if (RELAX_BRANCH_LINK (fragp->fr_subtype))
17483 	    {
17484 	      /* Clear the and-link bit.  */
17485 	      gas_assert ((insn & 0xfc1c0000) == 0x04100000);
17486 
17487 	      /* bltzal		0x04100000	bgezal	0x04110000
17488 		 bltzall	0x04120000	bgezall	0x04130000  */
17489 	      insn &= ~0x00100000;
17490 	    }
17491 
17492 	  /* Branch over the branch (if the branch was likely) or the
17493 	     full jump (not likely case).  Compute the offset from the
17494 	     current instruction to branch to.  */
17495 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17496 	    i = 16;
17497 	  else
17498 	    {
17499 	      /* How many bytes in instructions we've already emitted?  */
17500 	      i = buf - fragp->fr_literal - fragp->fr_fix;
17501 	      /* How many bytes in instructions from here to the end?  */
17502 	      i = fragp->fr_var - i;
17503 	    }
17504 	  /* Convert to instruction count.  */
17505 	  i >>= 2;
17506 	  /* Branch counts from the next instruction.  */
17507 	  i--;
17508 	  insn |= i;
17509 	  /* Branch over the jump.  */
17510 	  buf = write_insn (buf, insn);
17511 
17512 	  /* nop */
17513 	  buf = write_insn (buf, 0);
17514 
17515 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17516 	    {
17517 	      /* beql $0, $0, 2f */
17518 	      insn = 0x50000000;
17519 	      /* Compute the PC offset from the current instruction to
17520 		 the end of the variable frag.  */
17521 	      /* How many bytes in instructions we've already emitted?  */
17522 	      i = buf - fragp->fr_literal - fragp->fr_fix;
17523 	      /* How many bytes in instructions from here to the end?  */
17524 	      i = fragp->fr_var - i;
17525 	      /* Convert to instruction count.  */
17526 	      i >>= 2;
17527 	      /* Don't decrement i, because we want to branch over the
17528 		 delay slot.  */
17529 	      insn |= i;
17530 
17531 	      buf = write_insn (buf, insn);
17532 	      buf = write_insn (buf, 0);
17533 	    }
17534 
17535 	uncond:
17536 	  if (mips_pic == NO_PIC)
17537 	    {
17538 	      /* j or jal.  */
17539 	      insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
17540 		      ? 0x0c000000 : 0x08000000);
17541 	      exp.X_op = O_symbol;
17542 	      exp.X_add_symbol = fragp->fr_symbol;
17543 	      exp.X_add_number = fragp->fr_offset;
17544 
17545 	      fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
17546 				  FALSE, BFD_RELOC_MIPS_JMP);
17547 	      fixp->fx_file = fragp->fr_file;
17548 	      fixp->fx_line = fragp->fr_line;
17549 
17550 	      buf = write_insn (buf, insn);
17551 	    }
17552 	  else
17553 	    {
17554 	      unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
17555 
17556 	      /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
17557 	      insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
17558 	      insn |= at << OP_SH_RT;
17559 	      exp.X_op = O_symbol;
17560 	      exp.X_add_symbol = fragp->fr_symbol;
17561 	      exp.X_add_number = fragp->fr_offset;
17562 
17563 	      if (fragp->fr_offset)
17564 		{
17565 		  exp.X_add_symbol = make_expr_symbol (&exp);
17566 		  exp.X_add_number = 0;
17567 		}
17568 
17569 	      fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
17570 				  FALSE, BFD_RELOC_MIPS_GOT16);
17571 	      fixp->fx_file = fragp->fr_file;
17572 	      fixp->fx_line = fragp->fr_line;
17573 
17574 	      buf = write_insn (buf, insn);
17575 
17576 	      if (mips_opts.isa == ISA_MIPS1)
17577 		/* nop */
17578 		buf = write_insn (buf, 0);
17579 
17580 	      /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
17581 	      insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
17582 	      insn |= at << OP_SH_RS | at << OP_SH_RT;
17583 
17584 	      fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
17585 				  FALSE, BFD_RELOC_LO16);
17586 	      fixp->fx_file = fragp->fr_file;
17587 	      fixp->fx_line = fragp->fr_line;
17588 
17589 	      buf = write_insn (buf, insn);
17590 
17591 	      /* j(al)r $at.  */
17592 	      if (RELAX_BRANCH_LINK (fragp->fr_subtype))
17593 		insn = 0x0000f809;
17594 	      else
17595 		insn = 0x00000008;
17596 	      insn |= at << OP_SH_RS;
17597 
17598 	      buf = write_insn (buf, insn);
17599 	    }
17600 	}
17601 
17602       fragp->fr_fix += fragp->fr_var;
17603       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
17604       return;
17605     }
17606 
17607   /* Relax microMIPS branches.  */
17608   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17609     {
17610       char *buf = fragp->fr_literal + fragp->fr_fix;
17611       bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17612       bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17613       int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17614       bfd_boolean short_ds;
17615       unsigned long insn;
17616       expressionS exp;
17617       fixS *fixp;
17618 
17619       exp.X_op = O_symbol;
17620       exp.X_add_symbol = fragp->fr_symbol;
17621       exp.X_add_number = fragp->fr_offset;
17622 
17623       fragp->fr_fix += fragp->fr_var;
17624 
17625       /* Handle 16-bit branches that fit or are forced to fit.  */
17626       if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17627 	{
17628 	  /* We generate a fixup instead of applying it right now,
17629 	     because if there is linker relaxation, we're going to
17630 	     need the relocations.  */
17631 	  if (type == 'D')
17632 	    fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
17633 				BFD_RELOC_MICROMIPS_10_PCREL_S1);
17634 	  else if (type == 'E')
17635 	    fixp = fix_new_exp (fragp, buf - fragp->fr_literal,	2, &exp, TRUE,
17636 				BFD_RELOC_MICROMIPS_7_PCREL_S1);
17637 	  else
17638 	    abort ();
17639 
17640 	  fixp->fx_file = fragp->fr_file;
17641 	  fixp->fx_line = fragp->fr_line;
17642 
17643 	  /* These relocations can have an addend that won't fit in
17644 	     2 octets.  */
17645 	  fixp->fx_no_overflow = 1;
17646 
17647 	  return;
17648 	}
17649 
17650       /* Handle 32-bit branches that fit or are forced to fit.  */
17651       if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
17652 	  || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17653 	{
17654 	  /* We generate a fixup instead of applying it right now,
17655 	     because if there is linker relaxation, we're going to
17656 	     need the relocations.  */
17657 	  fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
17658 			      BFD_RELOC_MICROMIPS_16_PCREL_S1);
17659 	  fixp->fx_file = fragp->fr_file;
17660 	  fixp->fx_line = fragp->fr_line;
17661 
17662 	  if (type == 0)
17663 	    return;
17664 	}
17665 
17666       /* Relax 16-bit branches to 32-bit branches.  */
17667       if (type != 0)
17668 	{
17669 	  insn = read_compressed_insn (buf, 2);
17670 
17671 	  if ((insn & 0xfc00) == 0xcc00)		/* b16  */
17672 	    insn = 0x94000000;				/* beq  */
17673 	  else if ((insn & 0xdc00) == 0x8c00)		/* beqz16/bnez16  */
17674 	    {
17675 	      unsigned long regno;
17676 
17677 	      regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
17678 	      regno = micromips_to_32_reg_d_map [regno];
17679 	      insn = ((insn & 0x2000) << 16) | 0x94000000;	/* beq/bne  */
17680 	      insn |= regno << MICROMIPSOP_SH_RS;
17681 	    }
17682 	  else
17683 	    abort ();
17684 
17685 	  /* Nothing else to do, just write it out.  */
17686 	  if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
17687 	      || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17688 	    {
17689 	      buf = write_compressed_insn (buf, insn, 4);
17690 	      gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
17691 	      return;
17692 	    }
17693 	}
17694       else
17695 	insn = read_compressed_insn (buf, 4);
17696 
17697       /* Relax 32-bit branches to a sequence of instructions.  */
17698       as_warn_where (fragp->fr_file, fragp->fr_line,
17699 		     _("relaxed out-of-range branch into a jump"));
17700 
17701       /* Set the short-delay-slot bit.  */
17702       short_ds = al && (insn & 0x02000000) != 0;
17703 
17704       if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
17705 	{
17706 	  symbolS *l;
17707 
17708 	  /* Reverse the branch.  */
17709 	  if ((insn & 0xfc000000) == 0x94000000			/* beq  */
17710 	      || (insn & 0xfc000000) == 0xb4000000)		/* bne  */
17711 	    insn ^= 0x20000000;
17712 	  else if ((insn & 0xffe00000) == 0x40000000		/* bltz  */
17713 		   || (insn & 0xffe00000) == 0x40400000		/* bgez  */
17714 		   || (insn & 0xffe00000) == 0x40800000		/* blez  */
17715 		   || (insn & 0xffe00000) == 0x40c00000		/* bgtz  */
17716 		   || (insn & 0xffe00000) == 0x40a00000		/* bnezc  */
17717 		   || (insn & 0xffe00000) == 0x40e00000		/* beqzc  */
17718 		   || (insn & 0xffe00000) == 0x40200000		/* bltzal  */
17719 		   || (insn & 0xffe00000) == 0x40600000		/* bgezal  */
17720 		   || (insn & 0xffe00000) == 0x42200000		/* bltzals  */
17721 		   || (insn & 0xffe00000) == 0x42600000)	/* bgezals  */
17722 	    insn ^= 0x00400000;
17723 	  else if ((insn & 0xffe30000) == 0x43800000		/* bc1f  */
17724 		   || (insn & 0xffe30000) == 0x43a00000		/* bc1t  */
17725 		   || (insn & 0xffe30000) == 0x42800000		/* bc2f  */
17726 		   || (insn & 0xffe30000) == 0x42a00000)	/* bc2t  */
17727 	    insn ^= 0x00200000;
17728 	  else if ((insn & 0xff000000) == 0x83000000		/* BZ.df
17729 								   BNZ.df  */
17730 		    || (insn & 0xff600000) == 0x81600000)	/* BZ.V
17731 								   BNZ.V */
17732 	    insn ^= 0x00800000;
17733 	  else
17734 	    abort ();
17735 
17736 	  if (al)
17737 	    {
17738 	      /* Clear the and-link and short-delay-slot bits.  */
17739 	      gas_assert ((insn & 0xfda00000) == 0x40200000);
17740 
17741 	      /* bltzal  0x40200000	bgezal  0x40600000  */
17742 	      /* bltzals 0x42200000	bgezals 0x42600000  */
17743 	      insn &= ~0x02200000;
17744 	    }
17745 
17746 	  /* Make a label at the end for use with the branch.  */
17747 	  l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
17748 	  micromips_label_inc ();
17749 	  S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
17750 
17751 	  /* Refer to it.  */
17752 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
17753 			  BFD_RELOC_MICROMIPS_16_PCREL_S1);
17754 	  fixp->fx_file = fragp->fr_file;
17755 	  fixp->fx_line = fragp->fr_line;
17756 
17757 	  /* Branch over the jump.  */
17758 	  buf = write_compressed_insn (buf, insn, 4);
17759 	  if (!compact)
17760 	    /* nop */
17761 	    buf = write_compressed_insn (buf, 0x0c00, 2);
17762 	}
17763 
17764       if (mips_pic == NO_PIC)
17765 	{
17766 	  unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s  */
17767 
17768 	  /* j/jal/jals <sym>  R_MICROMIPS_26_S1  */
17769 	  insn = al ? jal : 0xd4000000;
17770 
17771 	  fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
17772 			      BFD_RELOC_MICROMIPS_JMP);
17773 	  fixp->fx_file = fragp->fr_file;
17774 	  fixp->fx_line = fragp->fr_line;
17775 
17776 	  buf = write_compressed_insn (buf, insn, 4);
17777 	  if (compact)
17778 	    /* nop */
17779 	    buf = write_compressed_insn (buf, 0x0c00, 2);
17780 	}
17781       else
17782 	{
17783 	  unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
17784 	  unsigned long jalr = short_ds ? 0x45e0 : 0x45c0;	/* jalr/s  */
17785 	  unsigned long jr = compact ? 0x45a0 : 0x4580;		/* jr/c  */
17786 
17787 	  /* lw/ld $at, <sym>($gp)  R_MICROMIPS_GOT16  */
17788 	  insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
17789 	  insn |= at << MICROMIPSOP_SH_RT;
17790 
17791 	  if (exp.X_add_number)
17792 	    {
17793 	      exp.X_add_symbol = make_expr_symbol (&exp);
17794 	      exp.X_add_number = 0;
17795 	    }
17796 
17797 	  fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
17798 			      BFD_RELOC_MICROMIPS_GOT16);
17799 	  fixp->fx_file = fragp->fr_file;
17800 	  fixp->fx_line = fragp->fr_line;
17801 
17802 	  buf = write_compressed_insn (buf, insn, 4);
17803 
17804 	  /* d/addiu $at, $at, <sym>  R_MICROMIPS_LO16  */
17805 	  insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
17806 	  insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
17807 
17808 	  fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
17809 			      BFD_RELOC_MICROMIPS_LO16);
17810 	  fixp->fx_file = fragp->fr_file;
17811 	  fixp->fx_line = fragp->fr_line;
17812 
17813 	  buf = write_compressed_insn (buf, insn, 4);
17814 
17815 	  /* jr/jrc/jalr/jalrs $at  */
17816 	  insn = al ? jalr : jr;
17817 	  insn |= at << MICROMIPSOP_SH_MJ;
17818 
17819 	  buf = write_compressed_insn (buf, insn, 2);
17820 	}
17821 
17822       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
17823       return;
17824     }
17825 
17826   if (RELAX_MIPS16_P (fragp->fr_subtype))
17827     {
17828       int type;
17829       const struct mips_int_operand *operand;
17830       offsetT val;
17831       char *buf;
17832       unsigned int user_length, length;
17833       unsigned long insn;
17834       bfd_boolean ext;
17835 
17836       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17837       operand = mips16_immed_operand (type, FALSE);
17838 
17839       ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
17840       val = resolve_symbol_value (fragp->fr_symbol);
17841       if (operand->root.type == OP_PCREL)
17842 	{
17843 	  const struct mips_pcrel_operand *pcrel_op;
17844 	  addressT addr;
17845 
17846 	  pcrel_op = (const struct mips_pcrel_operand *) operand;
17847 	  addr = fragp->fr_address + fragp->fr_fix;
17848 
17849 	  /* The rules for the base address of a PC relative reloc are
17850              complicated; see mips16_extended_frag.  */
17851 	  if (pcrel_op->include_isa_bit)
17852 	    {
17853 	      addr += 2;
17854 	      if (ext)
17855 		addr += 2;
17856 	      /* Ignore the low bit in the target, since it will be
17857                  set for a text label.  */
17858 	      val &= -2;
17859 	    }
17860 	  else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17861 	    addr -= 4;
17862 	  else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17863 	    addr -= 2;
17864 
17865 	  addr &= -(1 << pcrel_op->align_log2);
17866 	  val -= addr;
17867 
17868 	  /* Make sure the section winds up with the alignment we have
17869              assumed.  */
17870 	  if (operand->shift > 0)
17871 	    record_alignment (asec, operand->shift);
17872 	}
17873 
17874       if (ext
17875 	  && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
17876 	      || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
17877 	as_warn_where (fragp->fr_file, fragp->fr_line,
17878 		       _("extended instruction in delay slot"));
17879 
17880       buf = fragp->fr_literal + fragp->fr_fix;
17881 
17882       insn = read_compressed_insn (buf, 2);
17883       if (ext)
17884 	insn |= MIPS16_EXTEND;
17885 
17886       if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17887 	user_length = 4;
17888       else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17889 	user_length = 2;
17890       else
17891 	user_length = 0;
17892 
17893       mips16_immed (fragp->fr_file, fragp->fr_line, type,
17894 		    BFD_RELOC_UNUSED, val, user_length, &insn);
17895 
17896       length = (ext ? 4 : 2);
17897       gas_assert (mips16_opcode_length (insn) == length);
17898       write_compressed_insn (buf, insn, length);
17899       fragp->fr_fix += length;
17900     }
17901   else
17902     {
17903       relax_substateT subtype = fragp->fr_subtype;
17904       bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
17905       bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
17906       int first, second;
17907       fixS *fixp;
17908 
17909       first = RELAX_FIRST (subtype);
17910       second = RELAX_SECOND (subtype);
17911       fixp = (fixS *) fragp->fr_opcode;
17912 
17913       /* If the delay slot chosen does not match the size of the instruction,
17914          then emit a warning.  */
17915       if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
17916 	   || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
17917 	{
17918 	  relax_substateT s;
17919 	  const char *msg;
17920 
17921 	  s = subtype & (RELAX_DELAY_SLOT_16BIT
17922 			 | RELAX_DELAY_SLOT_SIZE_FIRST
17923 			 | RELAX_DELAY_SLOT_SIZE_SECOND);
17924 	  msg = macro_warning (s);
17925 	  if (msg != NULL)
17926 	    as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
17927 	  subtype &= ~s;
17928 	}
17929 
17930       /* Possibly emit a warning if we've chosen the longer option.  */
17931       if (use_second == second_longer)
17932 	{
17933 	  relax_substateT s;
17934 	  const char *msg;
17935 
17936 	  s = (subtype
17937 	       & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
17938 	  msg = macro_warning (s);
17939 	  if (msg != NULL)
17940 	    as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
17941 	  subtype &= ~s;
17942 	}
17943 
17944       /* Go through all the fixups for the first sequence.  Disable them
17945 	 (by marking them as done) if we're going to use the second
17946 	 sequence instead.  */
17947       while (fixp
17948 	     && fixp->fx_frag == fragp
17949 	     && fixp->fx_where < fragp->fr_fix - second)
17950 	{
17951 	  if (subtype & RELAX_USE_SECOND)
17952 	    fixp->fx_done = 1;
17953 	  fixp = fixp->fx_next;
17954 	}
17955 
17956       /* Go through the fixups for the second sequence.  Disable them if
17957 	 we're going to use the first sequence, otherwise adjust their
17958 	 addresses to account for the relaxation.  */
17959       while (fixp && fixp->fx_frag == fragp)
17960 	{
17961 	  if (subtype & RELAX_USE_SECOND)
17962 	    fixp->fx_where -= first;
17963 	  else
17964 	    fixp->fx_done = 1;
17965 	  fixp = fixp->fx_next;
17966 	}
17967 
17968       /* Now modify the frag contents.  */
17969       if (subtype & RELAX_USE_SECOND)
17970 	{
17971 	  char *start;
17972 
17973 	  start = fragp->fr_literal + fragp->fr_fix - first - second;
17974 	  memmove (start, start + first, second);
17975 	  fragp->fr_fix -= first;
17976 	}
17977       else
17978 	fragp->fr_fix -= second;
17979     }
17980 }
17981 
17982 /* This function is called after the relocs have been generated.
17983    We've been storing mips16 text labels as odd.  Here we convert them
17984    back to even for the convenience of the debugger.  */
17985 
17986 void
mips_frob_file_after_relocs(void)17987 mips_frob_file_after_relocs (void)
17988 {
17989   asymbol **syms;
17990   unsigned int count, i;
17991 
17992   syms = bfd_get_outsymbols (stdoutput);
17993   count = bfd_get_symcount (stdoutput);
17994   for (i = 0; i < count; i++, syms++)
17995     if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
17996 	&& ((*syms)->value & 1) != 0)
17997       {
17998 	(*syms)->value &= ~1;
17999 	/* If the symbol has an odd size, it was probably computed
18000 	   incorrectly, so adjust that as well.  */
18001 	if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18002 	  ++elf_symbol (*syms)->internal_elf_sym.st_size;
18003       }
18004 }
18005 
18006 /* This function is called whenever a label is defined, including fake
18007    labels instantiated off the dot special symbol.  It is used when
18008    handling branch delays; if a branch has a label, we assume we cannot
18009    move it.  This also bumps the value of the symbol by 1 in compressed
18010    code.  */
18011 
18012 static void
mips_record_label(symbolS * sym)18013 mips_record_label (symbolS *sym)
18014 {
18015   segment_info_type *si = seg_info (now_seg);
18016   struct insn_label_list *l;
18017 
18018   if (free_insn_labels == NULL)
18019     l = (struct insn_label_list *) xmalloc (sizeof *l);
18020   else
18021     {
18022       l = free_insn_labels;
18023       free_insn_labels = l->next;
18024     }
18025 
18026   l->label = sym;
18027   l->next = si->label_list;
18028   si->label_list = l;
18029 }
18030 
18031 /* This function is called as tc_frob_label() whenever a label is defined
18032    and adds a DWARF-2 record we only want for true labels.  */
18033 
18034 void
mips_define_label(symbolS * sym)18035 mips_define_label (symbolS *sym)
18036 {
18037   mips_record_label (sym);
18038   dwarf2_emit_label (sym);
18039 }
18040 
18041 /* This function is called by tc_new_dot_label whenever a new dot symbol
18042    is defined.  */
18043 
18044 void
mips_add_dot_label(symbolS * sym)18045 mips_add_dot_label (symbolS *sym)
18046 {
18047   mips_record_label (sym);
18048   if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18049     mips_compressed_mark_label (sym);
18050 }
18051 
18052 /* Converting ASE flags from internal to .MIPS.abiflags values.  */
18053 static unsigned int
mips_convert_ase_flags(int ase)18054 mips_convert_ase_flags (int ase)
18055 {
18056   unsigned int ext_ases = 0;
18057 
18058   if (ase & ASE_DSP)
18059     ext_ases |= AFL_ASE_DSP;
18060   if (ase & ASE_DSPR2)
18061     ext_ases |= AFL_ASE_DSPR2;
18062   if (ase & ASE_DSPR6)
18063     ext_ases |= AFL_ASE_DSPR6;
18064   if (ase & ASE_EVA)
18065     ext_ases |= AFL_ASE_EVA;
18066   if (ase & ASE_MCU)
18067     ext_ases |= AFL_ASE_MCU;
18068   if (ase & ASE_MDMX)
18069     ext_ases |= AFL_ASE_MDMX;
18070   if (ase & ASE_MIPS3D)
18071     ext_ases |= AFL_ASE_MIPS3D;
18072   if (ase & ASE_MT)
18073     ext_ases |= AFL_ASE_MT;
18074   if (ase & ASE_SMARTMIPS)
18075     ext_ases |= AFL_ASE_SMARTMIPS;
18076   if (ase & ASE_VIRT)
18077     ext_ases |= AFL_ASE_VIRT;
18078   if (ase & ASE_MSA)
18079     ext_ases |= AFL_ASE_MSA;
18080   if (ase & ASE_XPA)
18081     ext_ases |= AFL_ASE_XPA;
18082 
18083   return ext_ases;
18084 }
18085 /* Some special processing for a MIPS ELF file.  */
18086 
18087 void
mips_elf_final_processing(void)18088 mips_elf_final_processing (void)
18089 {
18090   int fpabi;
18091   Elf_Internal_ABIFlags_v0 flags;
18092 
18093   flags.version = 0;
18094   flags.isa_rev = 0;
18095   switch (file_mips_opts.isa)
18096     {
18097     case INSN_ISA1:
18098       flags.isa_level = 1;
18099       break;
18100     case INSN_ISA2:
18101       flags.isa_level = 2;
18102       break;
18103     case INSN_ISA3:
18104       flags.isa_level = 3;
18105       break;
18106     case INSN_ISA4:
18107       flags.isa_level = 4;
18108       break;
18109     case INSN_ISA5:
18110       flags.isa_level = 5;
18111       break;
18112     case INSN_ISA32:
18113       flags.isa_level = 32;
18114       flags.isa_rev = 1;
18115       break;
18116     case INSN_ISA32R2:
18117       flags.isa_level = 32;
18118       flags.isa_rev = 2;
18119       break;
18120     case INSN_ISA32R3:
18121       flags.isa_level = 32;
18122       flags.isa_rev = 3;
18123       break;
18124     case INSN_ISA32R5:
18125       flags.isa_level = 32;
18126       flags.isa_rev = 5;
18127       break;
18128     case INSN_ISA32R6:
18129       flags.isa_level = 32;
18130       flags.isa_rev = 6;
18131       break;
18132     case INSN_ISA64:
18133       flags.isa_level = 64;
18134       flags.isa_rev = 1;
18135       break;
18136     case INSN_ISA64R2:
18137       flags.isa_level = 64;
18138       flags.isa_rev = 2;
18139       break;
18140     case INSN_ISA64R3:
18141       flags.isa_level = 64;
18142       flags.isa_rev = 3;
18143       break;
18144     case INSN_ISA64R5:
18145       flags.isa_level = 64;
18146       flags.isa_rev = 5;
18147       break;
18148     case INSN_ISA64R6:
18149       flags.isa_level = 64;
18150       flags.isa_rev = 6;
18151       break;
18152     }
18153 
18154   flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
18155   flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
18156 		    : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
18157 		    : (file_mips_opts.fp == 64) ? AFL_REG_64
18158 		    : AFL_REG_32;
18159   flags.cpr2_size = AFL_REG_NONE;
18160   flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
18161                                            Tag_GNU_MIPS_ABI_FP);
18162   flags.isa_ext = bfd_mips_isa_ext (stdoutput);
18163   flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
18164   if (file_ase_mips16)
18165     flags.ases |= AFL_ASE_MIPS16;
18166   if (file_ase_micromips)
18167     flags.ases |= AFL_ASE_MICROMIPS;
18168   flags.flags1 = 0;
18169   if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
18170        || file_mips_opts.fp == 64)
18171       && file_mips_opts.oddspreg)
18172     flags.flags1 |= AFL_FLAGS1_ODDSPREG;
18173   flags.flags2 = 0;
18174 
18175   bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
18176 				     ((Elf_External_ABIFlags_v0 *)
18177 				     mips_flags_frag));
18178 
18179   /* Write out the register information.  */
18180   if (mips_abi != N64_ABI)
18181     {
18182       Elf32_RegInfo s;
18183 
18184       s.ri_gprmask = mips_gprmask;
18185       s.ri_cprmask[0] = mips_cprmask[0];
18186       s.ri_cprmask[1] = mips_cprmask[1];
18187       s.ri_cprmask[2] = mips_cprmask[2];
18188       s.ri_cprmask[3] = mips_cprmask[3];
18189       /* The gp_value field is set by the MIPS ELF backend.  */
18190 
18191       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18192 				       ((Elf32_External_RegInfo *)
18193 					mips_regmask_frag));
18194     }
18195   else
18196     {
18197       Elf64_Internal_RegInfo s;
18198 
18199       s.ri_gprmask = mips_gprmask;
18200       s.ri_pad = 0;
18201       s.ri_cprmask[0] = mips_cprmask[0];
18202       s.ri_cprmask[1] = mips_cprmask[1];
18203       s.ri_cprmask[2] = mips_cprmask[2];
18204       s.ri_cprmask[3] = mips_cprmask[3];
18205       /* The gp_value field is set by the MIPS ELF backend.  */
18206 
18207       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18208 				       ((Elf64_External_RegInfo *)
18209 					mips_regmask_frag));
18210     }
18211 
18212   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
18213      sort of BFD interface for this.  */
18214   if (mips_any_noreorder)
18215     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18216   if (mips_pic != NO_PIC)
18217     {
18218       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
18219       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18220     }
18221   if (mips_abicalls)
18222     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18223 
18224   /* Set MIPS ELF flags for ASEs.  Note that not all ASEs have flags
18225      defined at present; this might need to change in future.  */
18226   if (file_ase_mips16)
18227     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
18228   if (file_ase_micromips)
18229     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
18230   if (file_mips_opts.ase & ASE_MDMX)
18231     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
18232 
18233   /* Set the MIPS ELF ABI flags.  */
18234   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
18235     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
18236   else if (mips_abi == O64_ABI)
18237     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
18238   else if (mips_abi == EABI_ABI)
18239     {
18240       if (file_mips_opts.gp == 64)
18241 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18242       else
18243 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18244     }
18245   else if (mips_abi == N32_ABI)
18246     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18247 
18248   /* Nothing to do for N64_ABI.  */
18249 
18250   if (mips_32bitmode)
18251     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
18252 
18253   if (mips_nan2008 == 1)
18254     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
18255 
18256   /* 32 bit code with 64 bit FP registers.  */
18257   fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
18258 				    Tag_GNU_MIPS_ABI_FP);
18259   if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
18260     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
18261 }
18262 
18263 typedef struct proc {
18264   symbolS *func_sym;
18265   symbolS *func_end_sym;
18266   unsigned long reg_mask;
18267   unsigned long reg_offset;
18268   unsigned long fpreg_mask;
18269   unsigned long fpreg_offset;
18270   unsigned long frame_offset;
18271   unsigned long frame_reg;
18272   unsigned long pc_reg;
18273 } procS;
18274 
18275 static procS cur_proc;
18276 static procS *cur_proc_ptr;
18277 static int numprocs;
18278 
18279 /* Implement NOP_OPCODE.  We encode a MIPS16 nop as "1", a microMIPS nop
18280    as "2", and a normal nop as "0".  */
18281 
18282 #define NOP_OPCODE_MIPS		0
18283 #define NOP_OPCODE_MIPS16	1
18284 #define NOP_OPCODE_MICROMIPS	2
18285 
18286 char
mips_nop_opcode(void)18287 mips_nop_opcode (void)
18288 {
18289   if (seg_info (now_seg)->tc_segment_info_data.micromips)
18290     return NOP_OPCODE_MICROMIPS;
18291   else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18292     return NOP_OPCODE_MIPS16;
18293   else
18294     return NOP_OPCODE_MIPS;
18295 }
18296 
18297 /* Fill in an rs_align_code fragment.  Unlike elsewhere we want to use
18298    32-bit microMIPS NOPs here (if applicable).  */
18299 
18300 void
mips_handle_align(fragS * fragp)18301 mips_handle_align (fragS *fragp)
18302 {
18303   char nop_opcode;
18304   char *p;
18305   int bytes, size, excess;
18306   valueT opcode;
18307 
18308   if (fragp->fr_type != rs_align_code)
18309     return;
18310 
18311   p = fragp->fr_literal + fragp->fr_fix;
18312   nop_opcode = *p;
18313   switch (nop_opcode)
18314     {
18315     case NOP_OPCODE_MICROMIPS:
18316       opcode = micromips_nop32_insn.insn_opcode;
18317       size = 4;
18318       break;
18319     case NOP_OPCODE_MIPS16:
18320       opcode = mips16_nop_insn.insn_opcode;
18321       size = 2;
18322       break;
18323     case NOP_OPCODE_MIPS:
18324     default:
18325       opcode = nop_insn.insn_opcode;
18326       size = 4;
18327       break;
18328     }
18329 
18330   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18331   excess = bytes % size;
18332 
18333   /* Handle the leading part if we're not inserting a whole number of
18334      instructions, and make it the end of the fixed part of the frag.
18335      Try to fit in a short microMIPS NOP if applicable and possible,
18336      and use zeroes otherwise.  */
18337   gas_assert (excess < 4);
18338   fragp->fr_fix += excess;
18339   switch (excess)
18340     {
18341     case 3:
18342       *p++ = '\0';
18343       /* Fall through.  */
18344     case 2:
18345       if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
18346 	{
18347 	  p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
18348 	  break;
18349 	}
18350       *p++ = '\0';
18351       /* Fall through.  */
18352     case 1:
18353       *p++ = '\0';
18354       /* Fall through.  */
18355     case 0:
18356       break;
18357     }
18358 
18359   md_number_to_chars (p, opcode, size);
18360   fragp->fr_var = size;
18361 }
18362 
18363 static long
get_number(void)18364 get_number (void)
18365 {
18366   int negative = 0;
18367   long val = 0;
18368 
18369   if (*input_line_pointer == '-')
18370     {
18371       ++input_line_pointer;
18372       negative = 1;
18373     }
18374   if (!ISDIGIT (*input_line_pointer))
18375     as_bad (_("expected simple number"));
18376   if (input_line_pointer[0] == '0')
18377     {
18378       if (input_line_pointer[1] == 'x')
18379 	{
18380 	  input_line_pointer += 2;
18381 	  while (ISXDIGIT (*input_line_pointer))
18382 	    {
18383 	      val <<= 4;
18384 	      val |= hex_value (*input_line_pointer++);
18385 	    }
18386 	  return negative ? -val : val;
18387 	}
18388       else
18389 	{
18390 	  ++input_line_pointer;
18391 	  while (ISDIGIT (*input_line_pointer))
18392 	    {
18393 	      val <<= 3;
18394 	      val |= *input_line_pointer++ - '0';
18395 	    }
18396 	  return negative ? -val : val;
18397 	}
18398     }
18399   if (!ISDIGIT (*input_line_pointer))
18400     {
18401       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18402 	      *input_line_pointer, *input_line_pointer);
18403       as_warn (_("invalid number"));
18404       return -1;
18405     }
18406   while (ISDIGIT (*input_line_pointer))
18407     {
18408       val *= 10;
18409       val += *input_line_pointer++ - '0';
18410     }
18411   return negative ? -val : val;
18412 }
18413 
18414 /* The .file directive; just like the usual .file directive, but there
18415    is an initial number which is the ECOFF file index.  In the non-ECOFF
18416    case .file implies DWARF-2.  */
18417 
18418 static void
s_mips_file(int x ATTRIBUTE_UNUSED)18419 s_mips_file (int x ATTRIBUTE_UNUSED)
18420 {
18421   static int first_file_directive = 0;
18422 
18423   if (ECOFF_DEBUGGING)
18424     {
18425       get_number ();
18426       s_app_file (0);
18427     }
18428   else
18429     {
18430       char *filename;
18431 
18432       filename = dwarf2_directive_file (0);
18433 
18434       /* Versions of GCC up to 3.1 start files with a ".file"
18435 	 directive even for stabs output.  Make sure that this
18436 	 ".file" is handled.  Note that you need a version of GCC
18437          after 3.1 in order to support DWARF-2 on MIPS.  */
18438       if (filename != NULL && ! first_file_directive)
18439 	{
18440 	  (void) new_logical_line (filename, -1);
18441 	  s_app_file_string (filename, 0);
18442 	}
18443       first_file_directive = 1;
18444     }
18445 }
18446 
18447 /* The .loc directive, implying DWARF-2.  */
18448 
18449 static void
s_mips_loc(int x ATTRIBUTE_UNUSED)18450 s_mips_loc (int x ATTRIBUTE_UNUSED)
18451 {
18452   if (!ECOFF_DEBUGGING)
18453     dwarf2_directive_loc (0);
18454 }
18455 
18456 /* The .end directive.  */
18457 
18458 static void
s_mips_end(int x ATTRIBUTE_UNUSED)18459 s_mips_end (int x ATTRIBUTE_UNUSED)
18460 {
18461   symbolS *p;
18462 
18463   /* Following functions need their own .frame and .cprestore directives.  */
18464   mips_frame_reg_valid = 0;
18465   mips_cprestore_valid = 0;
18466 
18467   if (!is_end_of_line[(unsigned char) *input_line_pointer])
18468     {
18469       p = get_symbol ();
18470       demand_empty_rest_of_line ();
18471     }
18472   else
18473     p = NULL;
18474 
18475   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
18476     as_warn (_(".end not in text section"));
18477 
18478   if (!cur_proc_ptr)
18479     {
18480       as_warn (_(".end directive without a preceding .ent directive"));
18481       demand_empty_rest_of_line ();
18482       return;
18483     }
18484 
18485   if (p != NULL)
18486     {
18487       gas_assert (S_GET_NAME (p));
18488       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
18489 	as_warn (_(".end symbol does not match .ent symbol"));
18490 
18491       if (debug_type == DEBUG_STABS)
18492 	stabs_generate_asm_endfunc (S_GET_NAME (p),
18493 				    S_GET_NAME (p));
18494     }
18495   else
18496     as_warn (_(".end directive missing or unknown symbol"));
18497 
18498   /* Create an expression to calculate the size of the function.  */
18499   if (p && cur_proc_ptr)
18500     {
18501       OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
18502       expressionS *exp = xmalloc (sizeof (expressionS));
18503 
18504       obj->size = exp;
18505       exp->X_op = O_subtract;
18506       exp->X_add_symbol = symbol_temp_new_now ();
18507       exp->X_op_symbol = p;
18508       exp->X_add_number = 0;
18509 
18510       cur_proc_ptr->func_end_sym = exp->X_add_symbol;
18511     }
18512 
18513   /* Generate a .pdr section.  */
18514   if (!ECOFF_DEBUGGING && mips_flag_pdr)
18515     {
18516       segT saved_seg = now_seg;
18517       subsegT saved_subseg = now_subseg;
18518       expressionS exp;
18519       char *fragp;
18520 
18521 #ifdef md_flush_pending_output
18522       md_flush_pending_output ();
18523 #endif
18524 
18525       gas_assert (pdr_seg);
18526       subseg_set (pdr_seg, 0);
18527 
18528       /* Write the symbol.  */
18529       exp.X_op = O_symbol;
18530       exp.X_add_symbol = p;
18531       exp.X_add_number = 0;
18532       emit_expr (&exp, 4);
18533 
18534       fragp = frag_more (7 * 4);
18535 
18536       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
18537       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
18538       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
18539       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
18540       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
18541       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
18542       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
18543 
18544       subseg_set (saved_seg, saved_subseg);
18545     }
18546 
18547   cur_proc_ptr = NULL;
18548 }
18549 
18550 /* The .aent and .ent directives.  */
18551 
18552 static void
s_mips_ent(int aent)18553 s_mips_ent (int aent)
18554 {
18555   symbolS *symbolP;
18556 
18557   symbolP = get_symbol ();
18558   if (*input_line_pointer == ',')
18559     ++input_line_pointer;
18560   SKIP_WHITESPACE ();
18561   if (ISDIGIT (*input_line_pointer)
18562       || *input_line_pointer == '-')
18563     get_number ();
18564 
18565   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
18566     as_warn (_(".ent or .aent not in text section"));
18567 
18568   if (!aent && cur_proc_ptr)
18569     as_warn (_("missing .end"));
18570 
18571   if (!aent)
18572     {
18573       /* This function needs its own .frame and .cprestore directives.  */
18574       mips_frame_reg_valid = 0;
18575       mips_cprestore_valid = 0;
18576 
18577       cur_proc_ptr = &cur_proc;
18578       memset (cur_proc_ptr, '\0', sizeof (procS));
18579 
18580       cur_proc_ptr->func_sym = symbolP;
18581 
18582       ++numprocs;
18583 
18584       if (debug_type == DEBUG_STABS)
18585         stabs_generate_asm_func (S_GET_NAME (symbolP),
18586 				 S_GET_NAME (symbolP));
18587     }
18588 
18589   symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
18590 
18591   demand_empty_rest_of_line ();
18592 }
18593 
18594 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
18595    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
18596    s_mips_frame is used so that we can set the PDR information correctly.
18597    We can't use the ecoff routines because they make reference to the ecoff
18598    symbol table (in the mdebug section).  */
18599 
18600 static void
s_mips_frame(int ignore ATTRIBUTE_UNUSED)18601 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
18602 {
18603   if (ECOFF_DEBUGGING)
18604     s_ignore (ignore);
18605   else
18606     {
18607       long val;
18608 
18609       if (cur_proc_ptr == (procS *) NULL)
18610 	{
18611 	  as_warn (_(".frame outside of .ent"));
18612 	  demand_empty_rest_of_line ();
18613 	  return;
18614 	}
18615 
18616       cur_proc_ptr->frame_reg = tc_get_register (1);
18617 
18618       SKIP_WHITESPACE ();
18619       if (*input_line_pointer++ != ','
18620 	  || get_absolute_expression_and_terminator (&val) != ',')
18621 	{
18622 	  as_warn (_("bad .frame directive"));
18623 	  --input_line_pointer;
18624 	  demand_empty_rest_of_line ();
18625 	  return;
18626 	}
18627 
18628       cur_proc_ptr->frame_offset = val;
18629       cur_proc_ptr->pc_reg = tc_get_register (0);
18630 
18631       demand_empty_rest_of_line ();
18632     }
18633 }
18634 
18635 /* The .fmask and .mask directives. If the mdebug section is present
18636    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
18637    embedded targets, s_mips_mask is used so that we can set the PDR
18638    information correctly. We can't use the ecoff routines because they
18639    make reference to the ecoff symbol table (in the mdebug section).  */
18640 
18641 static void
s_mips_mask(int reg_type)18642 s_mips_mask (int reg_type)
18643 {
18644   if (ECOFF_DEBUGGING)
18645     s_ignore (reg_type);
18646   else
18647     {
18648       long mask, off;
18649 
18650       if (cur_proc_ptr == (procS *) NULL)
18651 	{
18652 	  as_warn (_(".mask/.fmask outside of .ent"));
18653 	  demand_empty_rest_of_line ();
18654 	  return;
18655 	}
18656 
18657       if (get_absolute_expression_and_terminator (&mask) != ',')
18658 	{
18659 	  as_warn (_("bad .mask/.fmask directive"));
18660 	  --input_line_pointer;
18661 	  demand_empty_rest_of_line ();
18662 	  return;
18663 	}
18664 
18665       off = get_absolute_expression ();
18666 
18667       if (reg_type == 'F')
18668 	{
18669 	  cur_proc_ptr->fpreg_mask = mask;
18670 	  cur_proc_ptr->fpreg_offset = off;
18671 	}
18672       else
18673 	{
18674 	  cur_proc_ptr->reg_mask = mask;
18675 	  cur_proc_ptr->reg_offset = off;
18676 	}
18677 
18678       demand_empty_rest_of_line ();
18679     }
18680 }
18681 
18682 /* A table describing all the processors gas knows about.  Names are
18683    matched in the order listed.
18684 
18685    To ease comparison, please keep this table in the same order as
18686    gcc's mips_cpu_info_table[].  */
18687 static const struct mips_cpu_info mips_cpu_info_table[] =
18688 {
18689   /* Entries for generic ISAs */
18690   { "mips1",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS1,    CPU_R3000 },
18691   { "mips2",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS2,    CPU_R6000 },
18692   { "mips3",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS3,    CPU_R4000 },
18693   { "mips4",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS4,    CPU_R8000 },
18694   { "mips5",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS5,    CPU_MIPS5 },
18695   { "mips32",         MIPS_CPU_IS_ISA, 0,	ISA_MIPS32,   CPU_MIPS32 },
18696   { "mips32r2",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R2, CPU_MIPS32R2 },
18697   { "mips32r3",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R3, CPU_MIPS32R3 },
18698   { "mips32r5",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R5, CPU_MIPS32R5 },
18699   { "mips32r6",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R6, CPU_MIPS32R6 },
18700   { "mips64",         MIPS_CPU_IS_ISA, 0,	ISA_MIPS64,   CPU_MIPS64 },
18701   { "mips64r2",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R2, CPU_MIPS64R2 },
18702   { "mips64r3",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R3, CPU_MIPS64R3 },
18703   { "mips64r5",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R5, CPU_MIPS64R5 },
18704   { "mips64r6",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R6, CPU_MIPS64R6 },
18705 
18706   /* MIPS I */
18707   { "r3000",          0, 0,			ISA_MIPS1,    CPU_R3000 },
18708   { "r2000",          0, 0,			ISA_MIPS1,    CPU_R3000 },
18709   { "r3900",          0, 0,			ISA_MIPS1,    CPU_R3900 },
18710 
18711   /* MIPS II */
18712   { "r6000",          0, 0,			ISA_MIPS2,    CPU_R6000 },
18713 
18714   /* MIPS III */
18715   { "r4000",          0, 0,			ISA_MIPS3,    CPU_R4000 },
18716   { "r4010",          0, 0,			ISA_MIPS2,    CPU_R4010 },
18717   { "vr4100",         0, 0,			ISA_MIPS3,    CPU_VR4100 },
18718   { "vr4111",         0, 0,			ISA_MIPS3,    CPU_R4111 },
18719   { "vr4120",         0, 0,			ISA_MIPS3,    CPU_VR4120 },
18720   { "vr4130",         0, 0,			ISA_MIPS3,    CPU_VR4120 },
18721   { "vr4181",         0, 0,			ISA_MIPS3,    CPU_R4111 },
18722   { "vr4300",         0, 0,			ISA_MIPS3,    CPU_R4300 },
18723   { "r4400",          0, 0,			ISA_MIPS3,    CPU_R4400 },
18724   { "r4600",          0, 0,			ISA_MIPS3,    CPU_R4600 },
18725   { "orion",          0, 0,			ISA_MIPS3,    CPU_R4600 },
18726   { "r4650",          0, 0,			ISA_MIPS3,    CPU_R4650 },
18727   { "r5900",          0, 0,			ISA_MIPS3,    CPU_R5900 },
18728   /* ST Microelectronics Loongson 2E and 2F cores */
18729   { "loongson2e",     0, 0,			ISA_MIPS3,    CPU_LOONGSON_2E },
18730   { "loongson2f",     0, 0,			ISA_MIPS3,    CPU_LOONGSON_2F },
18731 
18732   /* MIPS IV */
18733   { "r8000",          0, 0,			ISA_MIPS4,    CPU_R8000 },
18734   { "r10000",         0, 0,			ISA_MIPS4,    CPU_R10000 },
18735   { "r12000",         0, 0,			ISA_MIPS4,    CPU_R12000 },
18736   { "r14000",         0, 0,			ISA_MIPS4,    CPU_R14000 },
18737   { "r16000",         0, 0,			ISA_MIPS4,    CPU_R16000 },
18738   { "vr5000",         0, 0,			ISA_MIPS4,    CPU_R5000 },
18739   { "vr5400",         0, 0,			ISA_MIPS4,    CPU_VR5400 },
18740   { "vr5500",         0, 0,			ISA_MIPS4,    CPU_VR5500 },
18741   { "rm5200",         0, 0,			ISA_MIPS4,    CPU_R5000 },
18742   { "rm5230",         0, 0,			ISA_MIPS4,    CPU_R5000 },
18743   { "rm5231",         0, 0,			ISA_MIPS4,    CPU_R5000 },
18744   { "rm5261",         0, 0,			ISA_MIPS4,    CPU_R5000 },
18745   { "rm5721",         0, 0,			ISA_MIPS4,    CPU_R5000 },
18746   { "rm7000",         0, 0,			ISA_MIPS4,    CPU_RM7000 },
18747   { "rm9000",         0, 0,			ISA_MIPS4,    CPU_RM9000 },
18748 
18749   /* MIPS 32 */
18750   { "4kc",            0, 0,			ISA_MIPS32,   CPU_MIPS32 },
18751   { "4km",            0, 0,			ISA_MIPS32,   CPU_MIPS32 },
18752   { "4kp",            0, 0,			ISA_MIPS32,   CPU_MIPS32 },
18753   { "4ksc",           0, ASE_SMARTMIPS,		ISA_MIPS32,   CPU_MIPS32 },
18754 
18755   /* MIPS 32 Release 2 */
18756   { "4kec",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
18757   { "4kem",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
18758   { "4kep",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
18759   { "4ksd",           0, ASE_SMARTMIPS,		ISA_MIPS32R2, CPU_MIPS32R2 },
18760   { "m4k",            0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
18761   { "m4kp",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
18762   { "m14k",           0, ASE_MCU,		ISA_MIPS32R2, CPU_MIPS32R2 },
18763   { "m14kc",          0, ASE_MCU,		ISA_MIPS32R2, CPU_MIPS32R2 },
18764   { "m14ke",          0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
18765 						ISA_MIPS32R2, CPU_MIPS32R2 },
18766   { "m14kec",         0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
18767 						ISA_MIPS32R2, CPU_MIPS32R2 },
18768   { "24kc",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
18769   { "24kf2_1",        0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
18770   { "24kf",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
18771   { "24kf1_1",        0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
18772   /* Deprecated forms of the above.  */
18773   { "24kfx",          0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
18774   { "24kx",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
18775   /* 24KE is a 24K with DSP ASE, other ASEs are optional.  */
18776   { "24kec",          0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
18777   { "24kef2_1",       0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
18778   { "24kef",          0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
18779   { "24kef1_1",       0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
18780   /* Deprecated forms of the above.  */
18781   { "24kefx",         0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
18782   { "24kex",          0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
18783   /* 34K is a 24K with DSP and MT ASE, other ASEs are optional.  */
18784   { "34kc",           0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
18785   { "34kf2_1",        0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
18786   { "34kf",           0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
18787   { "34kf1_1",        0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
18788   /* Deprecated forms of the above.  */
18789   { "34kfx",          0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
18790   { "34kx",           0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
18791   /* 34Kn is a 34kc without DSP.  */
18792   { "34kn",           0, ASE_MT,		ISA_MIPS32R2, CPU_MIPS32R2 },
18793   /* 74K with DSP and DSPR2 ASE, other ASEs are optional.  */
18794   { "74kc",           0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
18795   { "74kf2_1",        0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
18796   { "74kf",           0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
18797   { "74kf1_1",        0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
18798   { "74kf3_2",        0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
18799   /* Deprecated forms of the above.  */
18800   { "74kfx",          0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
18801   { "74kx",           0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
18802   /* 1004K cores are multiprocessor versions of the 34K.  */
18803   { "1004kc",         0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
18804   { "1004kf2_1",      0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
18805   { "1004kf",         0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
18806   { "1004kf1_1",      0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
18807   /* P5600 with EVA and Virtualization ASEs, other ASEs are optional.  */
18808   { "p5600",          0, ASE_VIRT | ASE_EVA | ASE_XPA, 	ISA_MIPS32R5, CPU_MIPS32R5 },
18809 
18810   /* MIPS 64 */
18811   { "5kc",            0, 0,			ISA_MIPS64,   CPU_MIPS64 },
18812   { "5kf",            0, 0,			ISA_MIPS64,   CPU_MIPS64 },
18813   { "20kc",           0, ASE_MIPS3D,		ISA_MIPS64,   CPU_MIPS64 },
18814   { "25kf",           0, ASE_MIPS3D,		ISA_MIPS64,   CPU_MIPS64 },
18815 
18816   /* Broadcom SB-1 CPU core */
18817   { "sb1",            0, ASE_MIPS3D | ASE_MDMX,	ISA_MIPS64,   CPU_SB1 },
18818   /* Broadcom SB-1A CPU core */
18819   { "sb1a",           0, ASE_MIPS3D | ASE_MDMX,	ISA_MIPS64,   CPU_SB1 },
18820 
18821   { "loongson3a",     0, 0,			ISA_MIPS64R2, CPU_LOONGSON_3A },
18822 
18823   /* MIPS 64 Release 2 */
18824 
18825   /* Cavium Networks Octeon CPU core */
18826   { "octeon",	      0, 0,			ISA_MIPS64R2, CPU_OCTEON },
18827   { "octeon+",	      0, 0,			ISA_MIPS64R2, CPU_OCTEONP },
18828   { "octeon2",	      0, 0,			ISA_MIPS64R2, CPU_OCTEON2 },
18829   { "octeon3",	      0, ASE_VIRT | ASE_VIRT64,	ISA_MIPS64R5, CPU_OCTEON3 },
18830 
18831   /* RMI Xlr */
18832   { "xlr",	      0, 0,			ISA_MIPS64,   CPU_XLR },
18833 
18834   /* Broadcom XLP.
18835      XLP is mostly like XLR, with the prominent exception that it is
18836      MIPS64R2 rather than MIPS64.  */
18837   { "xlp",	      0, 0,			ISA_MIPS64R2, CPU_XLR },
18838 
18839   /* i6400.  */
18840   { "i6400",	      0, ASE_MSA,		ISA_MIPS64R6, CPU_MIPS64R6},
18841 
18842   /* End marker */
18843   { NULL, 0, 0, 0, 0 }
18844 };
18845 
18846 
18847 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
18848    with a final "000" replaced by "k".  Ignore case.
18849 
18850    Note: this function is shared between GCC and GAS.  */
18851 
18852 static bfd_boolean
mips_strict_matching_cpu_name_p(const char * canonical,const char * given)18853 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
18854 {
18855   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
18856     given++, canonical++;
18857 
18858   return ((*given == 0 && *canonical == 0)
18859 	  || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
18860 }
18861 
18862 
18863 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
18864    CPU name.  We've traditionally allowed a lot of variation here.
18865 
18866    Note: this function is shared between GCC and GAS.  */
18867 
18868 static bfd_boolean
mips_matching_cpu_name_p(const char * canonical,const char * given)18869 mips_matching_cpu_name_p (const char *canonical, const char *given)
18870 {
18871   /* First see if the name matches exactly, or with a final "000"
18872      turned into "k".  */
18873   if (mips_strict_matching_cpu_name_p (canonical, given))
18874     return TRUE;
18875 
18876   /* If not, try comparing based on numerical designation alone.
18877      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
18878   if (TOLOWER (*given) == 'r')
18879     given++;
18880   if (!ISDIGIT (*given))
18881     return FALSE;
18882 
18883   /* Skip over some well-known prefixes in the canonical name,
18884      hoping to find a number there too.  */
18885   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
18886     canonical += 2;
18887   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
18888     canonical += 2;
18889   else if (TOLOWER (canonical[0]) == 'r')
18890     canonical += 1;
18891 
18892   return mips_strict_matching_cpu_name_p (canonical, given);
18893 }
18894 
18895 
18896 /* Parse an option that takes the name of a processor as its argument.
18897    OPTION is the name of the option and CPU_STRING is the argument.
18898    Return the corresponding processor enumeration if the CPU_STRING is
18899    recognized, otherwise report an error and return null.
18900 
18901    A similar function exists in GCC.  */
18902 
18903 static const struct mips_cpu_info *
mips_parse_cpu(const char * option,const char * cpu_string)18904 mips_parse_cpu (const char *option, const char *cpu_string)
18905 {
18906   const struct mips_cpu_info *p;
18907 
18908   /* 'from-abi' selects the most compatible architecture for the given
18909      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
18910      EABIs, we have to decide whether we're using the 32-bit or 64-bit
18911      version.  Look first at the -mgp options, if given, otherwise base
18912      the choice on MIPS_DEFAULT_64BIT.
18913 
18914      Treat NO_ABI like the EABIs.  One reason to do this is that the
18915      plain 'mips' and 'mips64' configs have 'from-abi' as their default
18916      architecture.  This code picks MIPS I for 'mips' and MIPS III for
18917      'mips64', just as we did in the days before 'from-abi'.  */
18918   if (strcasecmp (cpu_string, "from-abi") == 0)
18919     {
18920       if (ABI_NEEDS_32BIT_REGS (mips_abi))
18921 	return mips_cpu_info_from_isa (ISA_MIPS1);
18922 
18923       if (ABI_NEEDS_64BIT_REGS (mips_abi))
18924 	return mips_cpu_info_from_isa (ISA_MIPS3);
18925 
18926       if (file_mips_opts.gp >= 0)
18927 	return mips_cpu_info_from_isa (file_mips_opts.gp == 32
18928 				       ? ISA_MIPS1 : ISA_MIPS3);
18929 
18930       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
18931 				     ? ISA_MIPS3
18932 				     : ISA_MIPS1);
18933     }
18934 
18935   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
18936   if (strcasecmp (cpu_string, "default") == 0)
18937     return 0;
18938 
18939   for (p = mips_cpu_info_table; p->name != 0; p++)
18940     if (mips_matching_cpu_name_p (p->name, cpu_string))
18941       return p;
18942 
18943   as_bad (_("bad value (%s) for %s"), cpu_string, option);
18944   return 0;
18945 }
18946 
18947 /* Return the canonical processor information for ISA (a member of the
18948    ISA_MIPS* enumeration).  */
18949 
18950 static const struct mips_cpu_info *
mips_cpu_info_from_isa(int isa)18951 mips_cpu_info_from_isa (int isa)
18952 {
18953   int i;
18954 
18955   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
18956     if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
18957 	&& isa == mips_cpu_info_table[i].isa)
18958       return (&mips_cpu_info_table[i]);
18959 
18960   return NULL;
18961 }
18962 
18963 static const struct mips_cpu_info *
mips_cpu_info_from_arch(int arch)18964 mips_cpu_info_from_arch (int arch)
18965 {
18966   int i;
18967 
18968   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
18969     if (arch == mips_cpu_info_table[i].cpu)
18970       return (&mips_cpu_info_table[i]);
18971 
18972   return NULL;
18973 }
18974 
18975 static void
show(FILE * stream,const char * string,int * col_p,int * first_p)18976 show (FILE *stream, const char *string, int *col_p, int *first_p)
18977 {
18978   if (*first_p)
18979     {
18980       fprintf (stream, "%24s", "");
18981       *col_p = 24;
18982     }
18983   else
18984     {
18985       fprintf (stream, ", ");
18986       *col_p += 2;
18987     }
18988 
18989   if (*col_p + strlen (string) > 72)
18990     {
18991       fprintf (stream, "\n%24s", "");
18992       *col_p = 24;
18993     }
18994 
18995   fprintf (stream, "%s", string);
18996   *col_p += strlen (string);
18997 
18998   *first_p = 0;
18999 }
19000 
19001 void
md_show_usage(FILE * stream)19002 md_show_usage (FILE *stream)
19003 {
19004   int column, first;
19005   size_t i;
19006 
19007   fprintf (stream, _("\
19008 MIPS options:\n\
19009 -EB			generate big endian output\n\
19010 -EL			generate little endian output\n\
19011 -g, -g2			do not remove unneeded NOPs or swap branches\n\
19012 -G NUM			allow referencing objects up to NUM bytes\n\
19013 			implicitly with the gp register [default 8]\n"));
19014   fprintf (stream, _("\
19015 -mips1			generate MIPS ISA I instructions\n\
19016 -mips2			generate MIPS ISA II instructions\n\
19017 -mips3			generate MIPS ISA III instructions\n\
19018 -mips4			generate MIPS ISA IV instructions\n\
19019 -mips5                  generate MIPS ISA V instructions\n\
19020 -mips32                 generate MIPS32 ISA instructions\n\
19021 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
19022 -mips32r3               generate MIPS32 release 3 ISA instructions\n\
19023 -mips32r5               generate MIPS32 release 5 ISA instructions\n\
19024 -mips32r6               generate MIPS32 release 6 ISA instructions\n\
19025 -mips64                 generate MIPS64 ISA instructions\n\
19026 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
19027 -mips64r3               generate MIPS64 release 3 ISA instructions\n\
19028 -mips64r5               generate MIPS64 release 5 ISA instructions\n\
19029 -mips64r6               generate MIPS64 release 6 ISA instructions\n\
19030 -march=CPU/-mtune=CPU	generate code/schedule for CPU, where CPU is one of:\n"));
19031 
19032   first = 1;
19033 
19034   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19035     show (stream, mips_cpu_info_table[i].name, &column, &first);
19036   show (stream, "from-abi", &column, &first);
19037   fputc ('\n', stream);
19038 
19039   fprintf (stream, _("\
19040 -mCPU			equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19041 -no-mCPU		don't generate code specific to CPU.\n\
19042 			For -mCPU and -no-mCPU, CPU must be one of:\n"));
19043 
19044   first = 1;
19045 
19046   show (stream, "3900", &column, &first);
19047   show (stream, "4010", &column, &first);
19048   show (stream, "4100", &column, &first);
19049   show (stream, "4650", &column, &first);
19050   fputc ('\n', stream);
19051 
19052   fprintf (stream, _("\
19053 -mips16			generate mips16 instructions\n\
19054 -no-mips16		do not generate mips16 instructions\n"));
19055   fprintf (stream, _("\
19056 -mmicromips		generate microMIPS instructions\n\
19057 -mno-micromips		do not generate microMIPS instructions\n"));
19058   fprintf (stream, _("\
19059 -msmartmips		generate smartmips instructions\n\
19060 -mno-smartmips		do not generate smartmips instructions\n"));
19061   fprintf (stream, _("\
19062 -mdsp			generate DSP instructions\n\
19063 -mno-dsp		do not generate DSP instructions\n"));
19064   fprintf (stream, _("\
19065 -mdspr2			generate DSP R2 instructions\n\
19066 -mno-dspr2		do not generate DSP R2 instructions\n"));
19067   fprintf (stream, _("\
19068 -mmt			generate MT instructions\n\
19069 -mno-mt			do not generate MT instructions\n"));
19070   fprintf (stream, _("\
19071 -mmcu			generate MCU instructions\n\
19072 -mno-mcu		do not generate MCU instructions\n"));
19073   fprintf (stream, _("\
19074 -mmsa			generate MSA instructions\n\
19075 -mno-msa		do not generate MSA instructions\n"));
19076   fprintf (stream, _("\
19077 -mxpa			generate eXtended Physical Address (XPA) instructions\n\
19078 -mno-xpa		do not generate eXtended Physical Address (XPA) instructions\n"));
19079   fprintf (stream, _("\
19080 -mmxu			generate MXU instructions\n\
19081 -mno-mxu		do not generate MXU instructions\n"));
19082   fprintf (stream, _("\
19083 -mvirt			generate Virtualization instructions\n\
19084 -mno-virt		do not generate Virtualization instructions\n"));
19085   fprintf (stream, _("\
19086 -minsn32		only generate 32-bit microMIPS instructions\n\
19087 -mno-insn32		generate all microMIPS instructions\n"));
19088   fprintf (stream, _("\
19089 -mfix-loongson2f-jump	work around Loongson2F JUMP instructions\n\
19090 -mfix-loongson2f-nop	work around Loongson2F NOP errata\n\
19091 -mfix-vr4120		work around certain VR4120 errata\n\
19092 -mfix-vr4130		work around VR4130 mflo/mfhi errata\n\
19093 -mfix-24k		insert a nop after ERET and DERET instructions\n\
19094 -mfix-cn63xxp1		work around CN63XXP1 PREF errata\n\
19095 -mgp32			use 32-bit GPRs, regardless of the chosen ISA\n\
19096 -mfp32			use 32-bit FPRs, regardless of the chosen ISA\n\
19097 -msym32			assume all symbols have 32-bit values\n\
19098 -O0			remove unneeded NOPs, do not swap branches\n\
19099 -O			remove unneeded NOPs and swap branches\n\
19100 --trap, --no-break	trap exception on div by 0 and mult overflow\n\
19101 --break, --no-trap	break exception on div by 0 and mult overflow\n"));
19102   fprintf (stream, _("\
19103 -mhard-float		allow floating-point instructions\n\
19104 -msoft-float		do not allow floating-point instructions\n\
19105 -msingle-float		only allow 32-bit floating-point operations\n\
19106 -mdouble-float		allow 32-bit and 64-bit floating-point operations\n\
19107 --[no-]construct-floats	[dis]allow floating point values to be constructed\n\
19108 --[no-]relax-branch	[dis]allow out-of-range branches to be relaxed\n\
19109 -mnan=ENCODING		select an IEEE 754 NaN encoding convention, either of:\n"));
19110 
19111   first = 1;
19112 
19113   show (stream, "legacy", &column, &first);
19114   show (stream, "2008", &column, &first);
19115 
19116   fputc ('\n', stream);
19117 
19118   fprintf (stream, _("\
19119 -KPIC, -call_shared	generate SVR4 position independent code\n\
19120 -call_nonpic		generate non-PIC code that can operate with DSOs\n\
19121 -mvxworks-pic		generate VxWorks position independent code\n\
19122 -non_shared		do not generate code that can operate with DSOs\n\
19123 -xgot			assume a 32 bit GOT\n\
19124 -mpdr, -mno-pdr		enable/disable creation of .pdr sections\n\
19125 -mshared, -mno-shared   disable/enable .cpload optimization for\n\
19126                         position dependent (non shared) code\n\
19127 -mabi=ABI		create ABI conformant object file for:\n"));
19128 
19129   first = 1;
19130 
19131   show (stream, "32", &column, &first);
19132   show (stream, "o64", &column, &first);
19133   show (stream, "n32", &column, &first);
19134   show (stream, "64", &column, &first);
19135   show (stream, "eabi", &column, &first);
19136 
19137   fputc ('\n', stream);
19138 
19139   fprintf (stream, _("\
19140 -32			create o32 ABI object file (default)\n\
19141 -n32			create n32 ABI object file\n\
19142 -64			create 64 ABI object file\n"));
19143 }
19144 
19145 #ifdef TE_IRIX
19146 enum dwarf2_format
mips_dwarf2_format(asection * sec ATTRIBUTE_UNUSED)19147 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
19148 {
19149   if (HAVE_64BIT_SYMBOLS)
19150     return dwarf2_format_64bit_irix;
19151   else
19152     return dwarf2_format_32bit;
19153 }
19154 #endif
19155 
19156 int
mips_dwarf2_addr_size(void)19157 mips_dwarf2_addr_size (void)
19158 {
19159   if (HAVE_64BIT_OBJECTS)
19160     return 8;
19161   else
19162     return 4;
19163 }
19164 
19165 /* Standard calling conventions leave the CFA at SP on entry.  */
19166 void
mips_cfi_frame_initial_instructions(void)19167 mips_cfi_frame_initial_instructions (void)
19168 {
19169   cfi_add_CFA_def_cfa_register (SP);
19170 }
19171 
19172 int
tc_mips_regname_to_dw2regnum(char * regname)19173 tc_mips_regname_to_dw2regnum (char *regname)
19174 {
19175   unsigned int regnum = -1;
19176   unsigned int reg;
19177 
19178   if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19179     regnum = reg;
19180 
19181   return regnum;
19182 }
19183 
19184 /* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
19185    Given a symbolic attribute NAME, return the proper integer value.
19186    Returns -1 if the attribute is not known.  */
19187 
19188 int
mips_convert_symbolic_attribute(const char * name)19189 mips_convert_symbolic_attribute (const char *name)
19190 {
19191   static const struct
19192   {
19193     const char * name;
19194     const int    tag;
19195   }
19196   attribute_table[] =
19197     {
19198 #define T(tag) {#tag, tag}
19199       T (Tag_GNU_MIPS_ABI_FP),
19200       T (Tag_GNU_MIPS_ABI_MSA),
19201 #undef T
19202     };
19203   unsigned int i;
19204 
19205   if (name == NULL)
19206     return -1;
19207 
19208   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
19209     if (streq (name, attribute_table[i].name))
19210       return attribute_table[i].tag;
19211 
19212   return -1;
19213 }
19214 
19215 void
md_mips_end(void)19216 md_mips_end (void)
19217 {
19218   int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
19219 
19220   mips_emit_delays ();
19221   if (cur_proc_ptr)
19222     as_warn (_("missing .end at end of assembly"));
19223 
19224   /* Just in case no code was emitted, do the consistency check.  */
19225   file_mips_check_options ();
19226 
19227   /* Set a floating-point ABI if the user did not.  */
19228   if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
19229     {
19230       /* Perform consistency checks on the floating-point ABI.  */
19231       fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19232 					Tag_GNU_MIPS_ABI_FP);
19233       if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
19234 	check_fpabi (fpabi);
19235     }
19236   else
19237     {
19238       /* Soft-float gets precedence over single-float, the two options should
19239          not be used together so this should not matter.  */
19240       if (file_mips_opts.soft_float == 1)
19241 	fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
19242       /* Single-float gets precedence over all double_float cases.  */
19243       else if (file_mips_opts.single_float == 1)
19244 	fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
19245       else
19246 	{
19247 	  switch (file_mips_opts.fp)
19248 	    {
19249 	    case 32:
19250 	      if (file_mips_opts.gp == 32)
19251 		fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
19252 	      break;
19253 	    case 0:
19254 	      fpabi = Val_GNU_MIPS_ABI_FP_XX;
19255 	      break;
19256 	    case 64:
19257 	      if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
19258 		fpabi = Val_GNU_MIPS_ABI_FP_64A;
19259 	      else if (file_mips_opts.gp == 32)
19260 		fpabi = Val_GNU_MIPS_ABI_FP_64;
19261 	      else
19262 		fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
19263 	      break;
19264 	    }
19265 	}
19266 
19267       bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19268 				Tag_GNU_MIPS_ABI_FP, fpabi);
19269     }
19270 }
19271