1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright (C) 1993-2021 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 FCSR 31
108 
109 #define ILLEGAL_REG (32)
110 
111 #define AT  mips_opts.at
112 
113 extern int target_big_endian;
114 
115 /* The name of the readonly data section.  */
116 #define RDATA_SECTION_NAME ".rodata"
117 
118 /* Ways in which an instruction can be "appended" to the output.  */
119 enum append_method {
120   /* Just add it normally.  */
121   APPEND_ADD,
122 
123   /* Add it normally and then add a nop.  */
124   APPEND_ADD_WITH_NOP,
125 
126   /* Turn an instruction with a delay slot into a "compact" version.  */
127   APPEND_ADD_COMPACT,
128 
129   /* Insert the instruction before the last one.  */
130   APPEND_SWAP
131 };
132 
133 /* Information about an instruction, including its format, operands
134    and fixups.  */
135 struct mips_cl_insn
136 {
137   /* The opcode's entry in mips_opcodes or mips16_opcodes.  */
138   const struct mips_opcode *insn_mo;
139 
140   /* The 16-bit or 32-bit bitstring of the instruction itself.  This is
141      a copy of INSN_MO->match with the operands filled in.  If we have
142      decided to use an extended MIPS16 instruction, this includes the
143      extension.  */
144   unsigned long insn_opcode;
145 
146   /* The name if this is an label.  */
147   char label[16];
148 
149   /* The target label name if this is an branch.  */
150   char target[16];
151 
152   /* The frag that contains the instruction.  */
153   struct frag *frag;
154 
155   /* The offset into FRAG of the first instruction byte.  */
156   long where;
157 
158   /* The relocs associated with the instruction, if any.  */
159   fixS *fixp[3];
160 
161   /* True if this entry cannot be moved from its current position.  */
162   unsigned int fixed_p : 1;
163 
164   /* True if this instruction occurred in a .set noreorder block.  */
165   unsigned int noreorder_p : 1;
166 
167   /* True for mips16 instructions that jump to an absolute address.  */
168   unsigned int mips16_absolute_jump_p : 1;
169 
170   /* True if this instruction is complete.  */
171   unsigned int complete_p : 1;
172 
173   /* True if this instruction is cleared from history by unconditional
174      branch.  */
175   unsigned int cleared_p : 1;
176 };
177 
178 /* The ABI to use.  */
179 enum mips_abi_level
180 {
181   NO_ABI = 0,
182   O32_ABI,
183   O64_ABI,
184   N32_ABI,
185   N64_ABI,
186   EABI_ABI
187 };
188 
189 /* MIPS ABI we are using for this output file.  */
190 static enum mips_abi_level mips_abi = NO_ABI;
191 
192 /* Whether or not we have code that can call pic code.  */
193 int mips_abicalls = false;
194 
195 /* Whether or not we have code which can be put into a shared
196    library.  */
197 static bool mips_in_shared = true;
198 
199 /* This is the set of options which may be modified by the .set
200    pseudo-op.  We use a struct so that .set push and .set pop are more
201    reliable.  */
202 
203 struct mips_set_options
204 {
205   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
206      if it has not been initialized.  Changed by `.set mipsN', and the
207      -mipsN command line option, and the default CPU.  */
208   int isa;
209   /* Enabled Application Specific Extensions (ASEs).  Changed by `.set
210      <asename>', by command line options, and based on the default
211      architecture.  */
212   int ase;
213   /* Whether we are assembling for the mips16 processor.  0 if we are
214      not, 1 if we are, and -1 if the value has not been initialized.
215      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
216      -nomips16 command line options, and the default CPU.  */
217   int mips16;
218   /* Whether we are assembling for the mipsMIPS ASE.  0 if we are not,
219      1 if we are, and -1 if the value has not been initialized.  Changed
220      by `.set micromips' and `.set nomicromips', and the -mmicromips
221      and -mno-micromips command line options, and the default CPU.  */
222   int micromips;
223   /* Non-zero if we should not reorder instructions.  Changed by `.set
224      reorder' and `.set noreorder'.  */
225   int noreorder;
226   /* Non-zero if we should not permit the register designated "assembler
227      temporary" to be used in instructions.  The value is the register
228      number, normally $at ($1).  Changed by `.set at=REG', `.set noat'
229      (same as `.set at=$0') and `.set at' (same as `.set at=$1').  */
230   unsigned int at;
231   /* Non-zero if we should warn when a macro instruction expands into
232      more than one machine instruction.  Changed by `.set nomacro' and
233      `.set macro'.  */
234   int warn_about_macros;
235   /* Non-zero if we should not move instructions.  Changed by `.set
236      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
237   int nomove;
238   /* Non-zero if we should not optimize branches by moving the target
239      of the branch into the delay slot.  Actually, we don't perform
240      this optimization anyhow.  Changed by `.set bopt' and `.set
241      nobopt'.  */
242   int nobopt;
243   /* Non-zero if we should not autoextend mips16 instructions.
244      Changed by `.set autoextend' and `.set noautoextend'.  */
245   int noautoextend;
246   /* True if we should only emit 32-bit microMIPS instructions.
247      Changed by `.set insn32' and `.set noinsn32', and the -minsn32
248      and -mno-insn32 command line options.  */
249   bool insn32;
250   /* Restrict general purpose registers and floating point registers
251      to 32 bit.  This is initially determined when -mgp32 or -mfp32
252      is passed but can changed if the assembler code uses .set mipsN.  */
253   int gp;
254   int fp;
255   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
256      command line option, and the default CPU.  */
257   int arch;
258   /* True if ".set sym32" is in effect.  */
259   bool sym32;
260   /* True if floating-point operations are not allowed.  Changed by .set
261      softfloat or .set hardfloat, by command line options -msoft-float or
262      -mhard-float.  The default is false.  */
263   bool soft_float;
264 
265   /* True if only single-precision floating-point operations are allowed.
266      Changed by .set singlefloat or .set doublefloat, command-line options
267      -msingle-float or -mdouble-float.  The default is false.  */
268   bool single_float;
269 
270   /* 1 if single-precision operations on odd-numbered registers are
271      allowed.  */
272   int oddspreg;
273 
274   /* The set of ASEs that should be enabled for the user specified
275      architecture.  This cannot be inferred from 'arch' for all cores
276      as processors only have a unique 'arch' if they add architecture
277      specific instructions (UDI).  */
278   int init_ase;
279 };
280 
281 /* Specifies whether module level options have been checked yet.  */
282 static bool file_mips_opts_checked = false;
283 
284 /* Do we support nan2008?  0 if we don't, 1 if we do, and -1 if the
285    value has not been initialized.  Changed by `.nan legacy' and
286    `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
287    options, and the default CPU.  */
288 static int mips_nan2008 = -1;
289 
290 /* This is the struct we use to hold the module level set of options.
291    Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
292    fp fields to -1 to indicate that they have not been initialized.  */
293 
294 static struct mips_set_options file_mips_opts =
295 {
296   /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
297   /* noreorder */ 0,  /* at */ ATREG, /* warn_about_macros */ 0,
298   /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ false,
299   /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ false,
300   /* soft_float */ false, /* single_float */ false, /* oddspreg */ -1,
301   /* init_ase */ 0
302 };
303 
304 /* This is similar to file_mips_opts, but for the current set of options.  */
305 
306 static struct mips_set_options mips_opts =
307 {
308   /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
309   /* noreorder */ 0,  /* at */ ATREG, /* warn_about_macros */ 0,
310   /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ false,
311   /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ false,
312   /* soft_float */ false, /* single_float */ false, /* oddspreg */ -1,
313   /* init_ase */ 0
314 };
315 
316 /* Which bits of file_ase were explicitly set or cleared by ASE options.  */
317 static unsigned int file_ase_explicit;
318 
319 /* These variables are filled in with the masks of registers used.
320    The object format code reads them and puts them in the appropriate
321    place.  */
322 unsigned long mips_gprmask;
323 unsigned long mips_cprmask[4];
324 
325 /* True if any MIPS16 code was produced.  */
326 static int file_ase_mips16;
327 
328 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32		\
329 			      || mips_opts.isa == ISA_MIPS32R2		\
330 			      || mips_opts.isa == ISA_MIPS32R3		\
331 			      || mips_opts.isa == ISA_MIPS32R5		\
332 			      || mips_opts.isa == ISA_MIPS64		\
333 			      || mips_opts.isa == ISA_MIPS64R2		\
334 			      || mips_opts.isa == ISA_MIPS64R3		\
335 			      || mips_opts.isa == ISA_MIPS64R5)
336 
337 /* True if any microMIPS code was produced.  */
338 static int file_ase_micromips;
339 
340 /* True if we want to create R_MIPS_JALR for jalr $25.  */
341 #ifdef TE_IRIX
342 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
343 #else
344 /* As a GNU extension, we use R_MIPS_JALR for o32 too.  However,
345    because there's no place for any addend, the only acceptable
346    expression is a bare symbol.  */
347 #define MIPS_JALR_HINT_P(EXPR) \
348   (!HAVE_IN_PLACE_ADDENDS \
349    || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
350 #endif
351 
352 /* The argument of the -march= flag.  The architecture we are assembling.  */
353 static const char *mips_arch_string;
354 
355 /* The argument of the -mtune= flag.  The architecture for which we
356    are optimizing.  */
357 static int mips_tune = CPU_UNKNOWN;
358 static const char *mips_tune_string;
359 
360 /* True when generating 32-bit code for a 64-bit processor.  */
361 static int mips_32bitmode = 0;
362 
363 /* True if the given ABI requires 32-bit registers.  */
364 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
365 
366 /* Likewise 64-bit registers.  */
367 #define ABI_NEEDS_64BIT_REGS(ABI)	\
368   ((ABI) == N32_ABI			\
369    || (ABI) == N64_ABI			\
370    || (ABI) == O64_ABI)
371 
372 #define ISA_IS_R6(ISA)			\
373   ((ISA) == ISA_MIPS32R6		\
374    || (ISA) == ISA_MIPS64R6)
375 
376 /*  Return true if ISA supports 64 bit wide gp registers.  */
377 #define ISA_HAS_64BIT_REGS(ISA)		\
378   ((ISA) == ISA_MIPS3			\
379    || (ISA) == ISA_MIPS4		\
380    || (ISA) == ISA_MIPS5		\
381    || (ISA) == ISA_MIPS64		\
382    || (ISA) == ISA_MIPS64R2		\
383    || (ISA) == ISA_MIPS64R3		\
384    || (ISA) == ISA_MIPS64R5		\
385    || (ISA) == ISA_MIPS64R6)
386 
387 /*  Return true if ISA supports 64 bit wide float registers.  */
388 #define ISA_HAS_64BIT_FPRS(ISA)		\
389   ((ISA) == ISA_MIPS3			\
390    || (ISA) == ISA_MIPS4		\
391    || (ISA) == ISA_MIPS5		\
392    || (ISA) == ISA_MIPS32R2		\
393    || (ISA) == ISA_MIPS32R3		\
394    || (ISA) == ISA_MIPS32R5		\
395    || (ISA) == ISA_MIPS32R6		\
396    || (ISA) == ISA_MIPS64		\
397    || (ISA) == ISA_MIPS64R2		\
398    || (ISA) == ISA_MIPS64R3		\
399    || (ISA) == ISA_MIPS64R5		\
400    || (ISA) == ISA_MIPS64R6)
401 
402 /* Return true if ISA supports 64-bit right rotate (dror et al.)
403    instructions.  */
404 #define ISA_HAS_DROR(ISA)		\
405   ((ISA) == ISA_MIPS64R2		\
406    || (ISA) == ISA_MIPS64R3		\
407    || (ISA) == ISA_MIPS64R5		\
408    || (ISA) == ISA_MIPS64R6		\
409    || (mips_opts.micromips		\
410        && ISA_HAS_64BIT_REGS (ISA))	\
411    )
412 
413 /* Return true if ISA supports 32-bit right rotate (ror et al.)
414    instructions.  */
415 #define ISA_HAS_ROR(ISA)		\
416   ((ISA) == ISA_MIPS32R2		\
417    || (ISA) == ISA_MIPS32R3		\
418    || (ISA) == ISA_MIPS32R5		\
419    || (ISA) == ISA_MIPS32R6		\
420    || (ISA) == ISA_MIPS64R2		\
421    || (ISA) == ISA_MIPS64R3		\
422    || (ISA) == ISA_MIPS64R5		\
423    || (ISA) == ISA_MIPS64R6		\
424    || (mips_opts.ase & ASE_SMARTMIPS)	\
425    || mips_opts.micromips		\
426    )
427 
428 /* Return true if ISA supports single-precision floats in odd registers.  */
429 #define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
430   (((ISA) == ISA_MIPS32			\
431     || (ISA) == ISA_MIPS32R2		\
432     || (ISA) == ISA_MIPS32R3		\
433     || (ISA) == ISA_MIPS32R5		\
434     || (ISA) == ISA_MIPS32R6		\
435     || (ISA) == ISA_MIPS64		\
436     || (ISA) == ISA_MIPS64R2		\
437     || (ISA) == ISA_MIPS64R3		\
438     || (ISA) == ISA_MIPS64R5		\
439     || (ISA) == ISA_MIPS64R6		\
440     || (CPU) == CPU_R5900)		\
441    && ((CPU) != CPU_GS464		\
442     || (CPU) != CPU_GS464E		\
443     || (CPU) != CPU_GS264E))
444 
445 /* Return true if ISA supports move to/from high part of a 64-bit
446    floating-point register. */
447 #define ISA_HAS_MXHC1(ISA)		\
448   ((ISA) == ISA_MIPS32R2		\
449    || (ISA) == ISA_MIPS32R3		\
450    || (ISA) == ISA_MIPS32R5		\
451    || (ISA) == ISA_MIPS32R6		\
452    || (ISA) == ISA_MIPS64R2		\
453    || (ISA) == ISA_MIPS64R3		\
454    || (ISA) == ISA_MIPS64R5		\
455    || (ISA) == ISA_MIPS64R6)
456 
457 /*  Return true if ISA supports legacy NAN.  */
458 #define ISA_HAS_LEGACY_NAN(ISA)		\
459   ((ISA) == ISA_MIPS1			\
460    || (ISA) == ISA_MIPS2		\
461    || (ISA) == ISA_MIPS3		\
462    || (ISA) == ISA_MIPS4		\
463    || (ISA) == ISA_MIPS5		\
464    || (ISA) == ISA_MIPS32		\
465    || (ISA) == ISA_MIPS32R2		\
466    || (ISA) == ISA_MIPS32R3		\
467    || (ISA) == ISA_MIPS32R5		\
468    || (ISA) == ISA_MIPS64		\
469    || (ISA) == ISA_MIPS64R2		\
470    || (ISA) == ISA_MIPS64R3		\
471    || (ISA) == ISA_MIPS64R5)
472 
473 #define GPR_SIZE \
474     (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
475      ? 32 \
476      : mips_opts.gp)
477 
478 #define FPR_SIZE \
479     (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
480      ? 32 \
481      : mips_opts.fp)
482 
483 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
484 
485 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
486 
487 /* True if relocations are stored in-place.  */
488 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
489 
490 /* The ABI-derived address size.  */
491 #define HAVE_64BIT_ADDRESSES \
492   (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
493 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
494 
495 /* The size of symbolic constants (i.e., expressions of the form
496    "SYMBOL" or "SYMBOL + OFFSET").  */
497 #define HAVE_32BIT_SYMBOLS \
498   (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
499 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
500 
501 /* Addresses are loaded in different ways, depending on the address size
502    in use.  The n32 ABI Documentation also mandates the use of additions
503    with overflow checking, but existing implementations don't follow it.  */
504 #define ADDRESS_ADD_INSN						\
505    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
506 
507 #define ADDRESS_ADDI_INSN						\
508    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
509 
510 #define ADDRESS_LOAD_INSN						\
511    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
512 
513 #define ADDRESS_STORE_INSN						\
514    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
515 
516 /* Return true if the given CPU supports the MIPS16 ASE.  */
517 #define CPU_HAS_MIPS16(cpu)						\
518    (startswith (TARGET_CPU, "mips16")					\
519     || startswith (TARGET_CANONICAL, "mips-lsi-elf"))
520 
521 /* Return true if the given CPU supports the microMIPS ASE.  */
522 #define CPU_HAS_MICROMIPS(cpu)	0
523 
524 /* True if CPU has a dror instruction.  */
525 #define CPU_HAS_DROR(CPU)	((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
526 
527 /* True if CPU has a ror instruction.  */
528 #define CPU_HAS_ROR(CPU)	CPU_HAS_DROR (CPU)
529 
530 /* True if CPU is in the Octeon family.  */
531 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
532 			    || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
533 
534 /* True if CPU has seq/sne and seqi/snei instructions.  */
535 #define CPU_HAS_SEQ(CPU)	(CPU_IS_OCTEON (CPU))
536 
537 /* True, if CPU has support for ldc1 and sdc1. */
538 #define CPU_HAS_LDC1_SDC1(CPU)	\
539    ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
540 
541 /* True if mflo and mfhi can be immediately followed by instructions
542    which write to the HI and LO registers.
543 
544    According to MIPS specifications, MIPS ISAs I, II, and III need
545    (at least) two instructions between the reads of HI/LO and
546    instructions which write them, and later ISAs do not.  Contradicting
547    the MIPS specifications, some MIPS IV processor user manuals (e.g.
548    the UM for the NEC Vr5000) document needing the instructions between
549    HI/LO reads and writes, as well.  Therefore, we declare only MIPS32,
550    MIPS64 and later ISAs to have the interlocks, plus any specific
551    earlier-ISA CPUs for which CPU documentation declares that the
552    instructions are really interlocked.  */
553 #define hilo_interlocks \
554   (mips_opts.isa == ISA_MIPS32                        \
555    || mips_opts.isa == ISA_MIPS32R2                   \
556    || mips_opts.isa == ISA_MIPS32R3                   \
557    || mips_opts.isa == ISA_MIPS32R5                   \
558    || mips_opts.isa == ISA_MIPS32R6                   \
559    || mips_opts.isa == ISA_MIPS64                     \
560    || mips_opts.isa == ISA_MIPS64R2                   \
561    || mips_opts.isa == ISA_MIPS64R3                   \
562    || mips_opts.isa == ISA_MIPS64R5                   \
563    || mips_opts.isa == ISA_MIPS64R6                   \
564    || mips_opts.arch == CPU_R4010                     \
565    || mips_opts.arch == CPU_R5900                     \
566    || mips_opts.arch == CPU_R10000                    \
567    || mips_opts.arch == CPU_R12000                    \
568    || mips_opts.arch == CPU_R14000                    \
569    || mips_opts.arch == CPU_R16000                    \
570    || mips_opts.arch == CPU_RM7000                    \
571    || mips_opts.arch == CPU_VR5500                    \
572    || mips_opts.micromips                             \
573    )
574 
575 /* Whether the processor uses hardware interlocks to protect reads
576    from the GPRs after they are loaded from memory, and thus does not
577    require nops to be inserted.  This applies to instructions marked
578    INSN_LOAD_MEMORY.  These nops are only required at MIPS ISA
579    level I and microMIPS mode instructions are always interlocked.  */
580 #define gpr_interlocks                                \
581   (mips_opts.isa != ISA_MIPS1                         \
582    || mips_opts.arch == CPU_R3900                     \
583    || mips_opts.arch == CPU_R5900                     \
584    || mips_opts.micromips                             \
585    )
586 
587 /* Whether the processor uses hardware interlocks to avoid delays
588    required by coprocessor instructions, and thus does not require
589    nops to be inserted.  This applies to instructions marked
590    INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
591    instructions marked INSN_WRITE_COND_CODE and ones marked
592    INSN_READ_COND_CODE.  These nops are only required at MIPS ISA
593    levels I, II, and III and microMIPS mode instructions are always
594    interlocked.  */
595 /* Itbl support may require additional care here.  */
596 #define cop_interlocks                                \
597   ((mips_opts.isa != ISA_MIPS1                        \
598     && mips_opts.isa != ISA_MIPS2                     \
599     && mips_opts.isa != ISA_MIPS3)                    \
600    || mips_opts.arch == CPU_R4300                     \
601    || mips_opts.micromips                             \
602    )
603 
604 /* Whether the processor uses hardware interlocks to protect reads
605    from coprocessor registers after they are loaded from memory, and
606    thus does not require nops to be inserted.  This applies to
607    instructions marked INSN_COPROC_MEMORY_DELAY.  These nops are only
608    requires at MIPS ISA level I and microMIPS mode instructions are
609    always interlocked.  */
610 #define cop_mem_interlocks                            \
611   (mips_opts.isa != ISA_MIPS1                         \
612    || mips_opts.micromips                             \
613    )
614 
615 /* Is this a mfhi or mflo instruction?  */
616 #define MF_HILO_INSN(PINFO) \
617   ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
618 
619 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
620    has been selected.  This implies, in particular, that addresses of text
621    labels have their LSB set.  */
622 #define HAVE_CODE_COMPRESSION						\
623   ((mips_opts.mips16 | mips_opts.micromips) != 0)
624 
625 /* The minimum and maximum signed values that can be stored in a GPR.  */
626 #define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
627 #define GPR_SMIN (-GPR_SMAX - 1)
628 
629 /* MIPS PIC level.  */
630 
631 enum mips_pic_level mips_pic;
632 
633 /* 1 if we should generate 32 bit offsets from the $gp register in
634    SVR4_PIC mode.  Currently has no meaning in other modes.  */
635 static int mips_big_got = 0;
636 
637 /* 1 if trap instructions should used for overflow rather than break
638    instructions.  */
639 static int mips_trap = 0;
640 
641 /* 1 if double width floating point constants should not be constructed
642    by assembling two single width halves into two single width floating
643    point registers which just happen to alias the double width destination
644    register.  On some architectures this aliasing can be disabled by a bit
645    in the status register, and the setting of this bit cannot be determined
646    automatically at assemble time.  */
647 static int mips_disable_float_construction;
648 
649 /* Non-zero if any .set noreorder directives were used.  */
650 
651 static int mips_any_noreorder;
652 
653 /* Non-zero if nops should be inserted when the register referenced in
654    an mfhi/mflo instruction is read in the next two instructions.  */
655 static int mips_7000_hilo_fix;
656 
657 /* The size of objects in the small data section.  */
658 static unsigned int g_switch_value = 8;
659 /* Whether the -G option was used.  */
660 static int g_switch_seen = 0;
661 
662 #define N_RMASK 0xc4
663 #define N_VFP   0xd4
664 
665 /* If we can determine in advance that GP optimization won't be
666    possible, we can skip the relaxation stuff that tries to produce
667    GP-relative references.  This makes delay slot optimization work
668    better.
669 
670    This function can only provide a guess, but it seems to work for
671    gcc output.  It needs to guess right for gcc, otherwise gcc
672    will put what it thinks is a GP-relative instruction in a branch
673    delay slot.
674 
675    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
676    fixed it for the non-PIC mode.  KR 95/04/07  */
677 static int nopic_need_relax (symbolS *, int);
678 
679 /* Handle of the OPCODE hash table.  */
680 static htab_t op_hash = NULL;
681 
682 /* The opcode hash table we use for the mips16.  */
683 static htab_t mips16_op_hash = NULL;
684 
685 /* The opcode hash table we use for the microMIPS ASE.  */
686 static htab_t micromips_op_hash = NULL;
687 
688 /* This array holds the chars that always start a comment.  If the
689     pre-processor is disabled, these aren't very useful.  */
690 const char comment_chars[] = "#";
691 
692 /* This array holds the chars that only start a comment at the beginning of
693    a line.  If the line seems to have the form '# 123 filename'
694    .line and .file directives will appear in the pre-processed output.  */
695 /* Note that input_file.c hand checks for '#' at the beginning of the
696    first line of the input file.  This is because the compiler outputs
697    #NO_APP at the beginning of its output.  */
698 /* Also note that C style comments are always supported.  */
699 const char line_comment_chars[] = "#";
700 
701 /* This array holds machine specific line separator characters.  */
702 const char line_separator_chars[] = ";";
703 
704 /* Chars that can be used to separate mant from exp in floating point nums.  */
705 const char EXP_CHARS[] = "eE";
706 
707 /* Chars that mean this number is a floating point constant.
708    As in 0f12.456
709    or    0d1.2345e12.  */
710 const char FLT_CHARS[] = "rRsSfFdDxXpP";
711 
712 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
713    changed in read.c .  Ideally it shouldn't have to know about it at all,
714    but nothing is ideal around here.  */
715 
716 /* Types of printf format used for instruction-related error messages.
717    "I" means int ("%d") and "S" means string ("%s").  */
718 enum mips_insn_error_format
719 {
720   ERR_FMT_PLAIN,
721   ERR_FMT_I,
722   ERR_FMT_SS,
723 };
724 
725 /* Information about an error that was found while assembling the current
726    instruction.  */
727 struct mips_insn_error
728 {
729   /* We sometimes need to match an instruction against more than one
730      opcode table entry.  Errors found during this matching are reported
731      against a particular syntactic argument rather than against the
732      instruction as a whole.  We grade these messages so that errors
733      against argument N have a greater priority than an error against
734      any argument < N, since the former implies that arguments up to N
735      were acceptable and that the opcode entry was therefore a closer match.
736      If several matches report an error against the same argument,
737      we only use that error if it is the same in all cases.
738 
739      min_argnum is the minimum argument number for which an error message
740      should be accepted.  It is 0 if MSG is against the instruction as
741      a whole.  */
742   int min_argnum;
743 
744   /* The printf()-style message, including its format and arguments.  */
745   enum mips_insn_error_format format;
746   const char *msg;
747   union
748   {
749     int i;
750     const char *ss[2];
751   } u;
752 };
753 
754 /* The error that should be reported for the current instruction.  */
755 static struct mips_insn_error insn_error;
756 
757 static int auto_align = 1;
758 
759 /* When outputting SVR4 PIC code, the assembler needs to know the
760    offset in the stack frame from which to restore the $gp register.
761    This is set by the .cprestore pseudo-op, and saved in this
762    variable.  */
763 static offsetT mips_cprestore_offset = -1;
764 
765 /* Similar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
766    more optimizations, it can use a register value instead of a memory-saved
767    offset and even an other register than $gp as global pointer.  */
768 static offsetT mips_cpreturn_offset = -1;
769 static int mips_cpreturn_register = -1;
770 static int mips_gp_register = GP;
771 static int mips_gprel_offset = 0;
772 
773 /* Whether mips_cprestore_offset has been set in the current function
774    (or whether it has already been warned about, if not).  */
775 static int mips_cprestore_valid = 0;
776 
777 /* This is the register which holds the stack frame, as set by the
778    .frame pseudo-op.  This is needed to implement .cprestore.  */
779 static int mips_frame_reg = SP;
780 
781 /* Whether mips_frame_reg has been set in the current function
782    (or whether it has already been warned about, if not).  */
783 static int mips_frame_reg_valid = 0;
784 
785 /* To output NOP instructions correctly, we need to keep information
786    about the previous two instructions.  */
787 
788 /* Whether we are optimizing.  The default value of 2 means to remove
789    unneeded NOPs and swap branch instructions when possible.  A value
790    of 1 means to not swap branches.  A value of 0 means to always
791    insert NOPs.  */
792 static int mips_optimize = 2;
793 
794 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
795    equivalent to seeing no -g option at all.  */
796 static int mips_debug = 0;
797 
798 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata.  */
799 #define MAX_VR4130_NOPS 4
800 
801 /* The maximum number of NOPs needed to fill delay slots.  */
802 #define MAX_DELAY_NOPS 2
803 
804 /* The maximum number of NOPs needed for any purpose.  */
805 #define MAX_NOPS 4
806 
807 /* The maximum range of context length of ll/sc.  */
808 #define MAX_LLSC_RANGE 20
809 
810 /* A list of previous instructions, with index 0 being the most recent.
811    We need to look back MAX_NOPS instructions when filling delay slots
812    or working around processor errata.  We need to look back one
813    instruction further if we're thinking about using history[0] to
814    fill a branch delay slot.  */
815 static struct mips_cl_insn history[1 + MAX_NOPS + MAX_LLSC_RANGE];
816 
817 /* The maximum number of LABELS detect for the same address.  */
818 #define MAX_LABELS_SAME 10
819 
820 /* Arrays of operands for each instruction.  */
821 #define MAX_OPERANDS 6
822 struct mips_operand_array
823 {
824   const struct mips_operand *operand[MAX_OPERANDS];
825 };
826 static struct mips_operand_array *mips_operands;
827 static struct mips_operand_array *mips16_operands;
828 static struct mips_operand_array *micromips_operands;
829 
830 /* Nop instructions used by emit_nop.  */
831 static struct mips_cl_insn nop_insn;
832 static struct mips_cl_insn mips16_nop_insn;
833 static struct mips_cl_insn micromips_nop16_insn;
834 static struct mips_cl_insn micromips_nop32_insn;
835 
836 /* Sync instructions used by insert sync.  */
837 static struct mips_cl_insn sync_insn;
838 
839 /* The appropriate nop for the current mode.  */
840 #define NOP_INSN (mips_opts.mips16					\
841 		  ? &mips16_nop_insn					\
842 		  : (mips_opts.micromips				\
843 		     ? (mips_opts.insn32				\
844 			? &micromips_nop32_insn				\
845 			: &micromips_nop16_insn)			\
846 		     : &nop_insn))
847 
848 /* The size of NOP_INSN in bytes.  */
849 #define NOP_INSN_SIZE ((mips_opts.mips16				\
850 			|| (mips_opts.micromips && !mips_opts.insn32))	\
851 		       ? 2 : 4)
852 
853 /* If this is set, it points to a frag holding nop instructions which
854    were inserted before the start of a noreorder section.  If those
855    nops turn out to be unnecessary, the size of the frag can be
856    decreased.  */
857 static fragS *prev_nop_frag;
858 
859 /* The number of nop instructions we created in prev_nop_frag.  */
860 static int prev_nop_frag_holds;
861 
862 /* The number of nop instructions that we know we need in
863    prev_nop_frag.  */
864 static int prev_nop_frag_required;
865 
866 /* The number of instructions we've seen since prev_nop_frag.  */
867 static int prev_nop_frag_since;
868 
869 /* Relocations against symbols are sometimes done in two parts, with a HI
870    relocation and a LO relocation.  Each relocation has only 16 bits of
871    space to store an addend.  This means that in order for the linker to
872    handle carries correctly, it must be able to locate both the HI and
873    the LO relocation.  This means that the relocations must appear in
874    order in the relocation table.
875 
876    In order to implement this, we keep track of each unmatched HI
877    relocation.  We then sort them so that they immediately precede the
878    corresponding LO relocation.  */
879 
880 struct mips_hi_fixup
881 {
882   /* Next HI fixup.  */
883   struct mips_hi_fixup *next;
884   /* This fixup.  */
885   fixS *fixp;
886   /* The section this fixup is in.  */
887   segT seg;
888 };
889 
890 /* The list of unmatched HI relocs.  */
891 
892 static struct mips_hi_fixup *mips_hi_fixup_list;
893 
894 /* Map mips16 register numbers to normal MIPS register numbers.  */
895 
896 static const unsigned int mips16_to_32_reg_map[] =
897 {
898   16, 17, 2, 3, 4, 5, 6, 7
899 };
900 
901 /* Map microMIPS register numbers to normal MIPS register numbers.  */
902 
903 #define micromips_to_32_reg_d_map	mips16_to_32_reg_map
904 
905 /* The microMIPS registers with type h.  */
906 static const unsigned int micromips_to_32_reg_h_map1[] =
907 {
908   5, 5, 6, 4, 4, 4, 4, 4
909 };
910 static const unsigned int micromips_to_32_reg_h_map2[] =
911 {
912   6, 7, 7, 21, 22, 5, 6, 7
913 };
914 
915 /* The microMIPS registers with type m.  */
916 static const unsigned int micromips_to_32_reg_m_map[] =
917 {
918   0, 17, 2, 3, 16, 18, 19, 20
919 };
920 
921 #define micromips_to_32_reg_n_map      micromips_to_32_reg_m_map
922 
923 /* Classifies the kind of instructions we're interested in when
924    implementing -mfix-vr4120.  */
925 enum fix_vr4120_class
926 {
927   FIX_VR4120_MACC,
928   FIX_VR4120_DMACC,
929   FIX_VR4120_MULT,
930   FIX_VR4120_DMULT,
931   FIX_VR4120_DIV,
932   FIX_VR4120_MTHILO,
933   NUM_FIX_VR4120_CLASSES
934 };
935 
936 /* ...likewise -mfix-loongson2f-jump.  */
937 static bool mips_fix_loongson2f_jump;
938 
939 /* ...likewise -mfix-loongson2f-nop.  */
940 static bool mips_fix_loongson2f_nop;
941 
942 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed.  */
943 static bool mips_fix_loongson2f;
944 
945 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
946    there must be at least one other instruction between an instruction
947    of type X and an instruction of type Y.  */
948 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
949 
950 /* True if -mfix-vr4120 is in force.  */
951 static int mips_fix_vr4120;
952 
953 /* ...likewise -mfix-vr4130.  */
954 static int mips_fix_vr4130;
955 
956 /* ...likewise -mfix-24k.  */
957 static int mips_fix_24k;
958 
959 /* ...likewise -mfix-rm7000  */
960 static int mips_fix_rm7000;
961 
962 /* ...likewise -mfix-cn63xxp1 */
963 static bool mips_fix_cn63xxp1;
964 
965 /* ...likewise -mfix-r5900 */
966 static bool mips_fix_r5900;
967 static bool mips_fix_r5900_explicit;
968 
969 /* ...likewise -mfix-loongson3-llsc.  */
970 static bool mips_fix_loongson3_llsc = DEFAULT_MIPS_FIX_LOONGSON3_LLSC;
971 
972 /* We don't relax branches by default, since this causes us to expand
973    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
974    fail to compute the offset before expanding the macro to the most
975    efficient expansion.  */
976 
977 static int mips_relax_branch;
978 
979 /* TRUE if checks are suppressed for invalid branches between ISA modes.
980    Needed for broken assembly produced by some GCC versions and some
981    sloppy code out there, where branches to data labels are present.  */
982 static bool mips_ignore_branch_isa;
983 
984 /* The expansion of many macros depends on the type of symbol that
985    they refer to.  For example, when generating position-dependent code,
986    a macro that refers to a symbol may have two different expansions,
987    one which uses GP-relative addresses and one which uses absolute
988    addresses.  When generating SVR4-style PIC, a macro may have
989    different expansions for local and global symbols.
990 
991    We handle these situations by generating both sequences and putting
992    them in variant frags.  In position-dependent code, the first sequence
993    will be the GP-relative one and the second sequence will be the
994    absolute one.  In SVR4 PIC, the first sequence will be for global
995    symbols and the second will be for local symbols.
996 
997    The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
998    SECOND are the lengths of the two sequences in bytes.  These fields
999    can be extracted using RELAX_FIRST() and RELAX_SECOND().  In addition,
1000    the subtype has the following flags:
1001 
1002    RELAX_PIC
1003 	Set if generating PIC code.
1004 
1005    RELAX_USE_SECOND
1006 	Set if it has been decided that we should use the second
1007 	sequence instead of the first.
1008 
1009    RELAX_SECOND_LONGER
1010 	Set in the first variant frag if the macro's second implementation
1011 	is longer than its first.  This refers to the macro as a whole,
1012 	not an individual relaxation.
1013 
1014    RELAX_NOMACRO
1015 	Set in the first variant frag if the macro appeared in a .set nomacro
1016 	block and if one alternative requires a warning but the other does not.
1017 
1018    RELAX_DELAY_SLOT
1019 	Like RELAX_NOMACRO, but indicates that the macro appears in a branch
1020 	delay slot.
1021 
1022    RELAX_DELAY_SLOT_16BIT
1023 	Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
1024 	16-bit instruction.
1025 
1026    RELAX_DELAY_SLOT_SIZE_FIRST
1027 	Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1028 	the macro is of the wrong size for the branch delay slot.
1029 
1030    RELAX_DELAY_SLOT_SIZE_SECOND
1031 	Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1032 	the macro is of the wrong size for the branch delay slot.
1033 
1034    The frag's "opcode" points to the first fixup for relaxable code.
1035 
1036    Relaxable macros are generated using a sequence such as:
1037 
1038       relax_start (SYMBOL);
1039       ... generate first expansion ...
1040       relax_switch ();
1041       ... generate second expansion ...
1042       relax_end ();
1043 
1044    The code and fixups for the unwanted alternative are discarded
1045    by md_convert_frag.  */
1046 #define RELAX_ENCODE(FIRST, SECOND, PIC)			\
1047   (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
1048 
1049 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1050 #define RELAX_SECOND(X) ((X) & 0xff)
1051 #define RELAX_PIC(X) (((X) & 0x10000) != 0)
1052 #define RELAX_USE_SECOND 0x20000
1053 #define RELAX_SECOND_LONGER 0x40000
1054 #define RELAX_NOMACRO 0x80000
1055 #define RELAX_DELAY_SLOT 0x100000
1056 #define RELAX_DELAY_SLOT_16BIT 0x200000
1057 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1058 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
1059 
1060 /* Branch without likely bit.  If label is out of range, we turn:
1061 
1062 	beq reg1, reg2, label
1063 	delay slot
1064 
1065    into
1066 
1067         bne reg1, reg2, 0f
1068         nop
1069         j label
1070      0: delay slot
1071 
1072    with the following opcode replacements:
1073 
1074 	beq <-> bne
1075 	blez <-> bgtz
1076 	bltz <-> bgez
1077 	bc1f <-> bc1t
1078 
1079 	bltzal <-> bgezal  (with jal label instead of j label)
1080 
1081    Even though keeping the delay slot instruction in the delay slot of
1082    the branch would be more efficient, it would be very tricky to do
1083    correctly, because we'd have to introduce a variable frag *after*
1084    the delay slot instruction, and expand that instead.  Let's do it
1085    the easy way for now, even if the branch-not-taken case now costs
1086    one additional instruction.  Out-of-range branches are not supposed
1087    to be common, anyway.
1088 
1089    Branch likely.  If label is out of range, we turn:
1090 
1091 	beql reg1, reg2, label
1092 	delay slot (annulled if branch not taken)
1093 
1094    into
1095 
1096         beql reg1, reg2, 1f
1097         nop
1098         beql $0, $0, 2f
1099         nop
1100      1: j[al] label
1101         delay slot (executed only if branch taken)
1102      2:
1103 
1104    It would be possible to generate a shorter sequence by losing the
1105    likely bit, generating something like:
1106 
1107 	bne reg1, reg2, 0f
1108 	nop
1109 	j[al] label
1110 	delay slot (executed only if branch taken)
1111      0:
1112 
1113 	beql -> bne
1114 	bnel -> beq
1115 	blezl -> bgtz
1116 	bgtzl -> blez
1117 	bltzl -> bgez
1118 	bgezl -> bltz
1119 	bc1fl -> bc1t
1120 	bc1tl -> bc1f
1121 
1122 	bltzall -> bgezal  (with jal label instead of j label)
1123 	bgezall -> bltzal  (ditto)
1124 
1125 
1126    but it's not clear that it would actually improve performance.  */
1127 #define RELAX_BRANCH_ENCODE(at, pic,				\
1128 			    uncond, likely, link, toofar)	\
1129   ((relax_substateT)						\
1130    (0xc0000000							\
1131     | ((at) & 0x1f)						\
1132     | ((pic) ? 0x20 : 0)					\
1133     | ((toofar) ? 0x40 : 0)					\
1134     | ((link) ? 0x80 : 0)					\
1135     | ((likely) ? 0x100 : 0)					\
1136     | ((uncond) ? 0x200 : 0)))
1137 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1138 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1139 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1140 #define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1141 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1142 #define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
1143 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1144 
1145 /* For mips16 code, we use an entirely different form of relaxation.
1146    mips16 supports two versions of most instructions which take
1147    immediate values: a small one which takes some small value, and a
1148    larger one which takes a 16 bit value.  Since branches also follow
1149    this pattern, relaxing these values is required.
1150 
1151    We can assemble both mips16 and normal MIPS code in a single
1152    object.  Therefore, we need to support this type of relaxation at
1153    the same time that we support the relaxation described above.  We
1154    use the high bit of the subtype field to distinguish these cases.
1155 
1156    The information we store for this type of relaxation is the
1157    argument code found in the opcode file for this relocation, whether
1158    the user explicitly requested a small or extended form, and whether
1159    the relocation is in a jump or jal delay slot.  That tells us the
1160    size of the value, and how it should be stored.  We also store
1161    whether the fragment is considered to be extended or not.  We also
1162    store whether this is known to be a branch to a different section,
1163    whether we have tried to relax this frag yet, and whether we have
1164    ever extended a PC relative fragment because of a shift count.  */
1165 #define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro,	\
1166 			    small, ext,				\
1167 			    dslot, jal_dslot)			\
1168   (0x80000000							\
1169    | ((type) & 0xff)						\
1170    | ((e2) ? 0x100 : 0)						\
1171    | ((pic) ? 0x200 : 0)					\
1172    | ((sym32) ? 0x400 : 0)					\
1173    | ((nomacro) ? 0x800 : 0)					\
1174    | ((small) ? 0x1000 : 0)					\
1175    | ((ext) ? 0x2000 : 0)					\
1176    | ((dslot) ? 0x4000 : 0)					\
1177    | ((jal_dslot) ? 0x8000 : 0))
1178 
1179 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1180 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1181 #define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1182 #define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1183 #define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1184 #define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1185 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1186 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1187 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1188 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1189 
1190 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1191 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1192 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1193 #define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1194 #define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1195 #define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1196 #define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1197 #define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1198 #define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
1199 
1200 /* For microMIPS code, we use relaxation similar to one we use for
1201    MIPS16 code.  Some instructions that take immediate values support
1202    two encodings: a small one which takes some small value, and a
1203    larger one which takes a 16 bit value.  As some branches also follow
1204    this pattern, relaxing these values is required.
1205 
1206    We can assemble both microMIPS and normal MIPS code in a single
1207    object.  Therefore, we need to support this type of relaxation at
1208    the same time that we support the relaxation described above.  We
1209    use one of the high bits of the subtype field to distinguish these
1210    cases.
1211 
1212    The information we store for this type of relaxation is the argument
1213    code found in the opcode file for this relocation, the register
1214    selected as the assembler temporary, whether in the 32-bit
1215    instruction mode, whether the branch is unconditional, whether it is
1216    compact, whether there is no delay-slot instruction available to fill
1217    in, whether it stores the link address implicitly in $ra, whether
1218    relaxation of out-of-range 32-bit branches to a sequence of
1219    instructions is enabled, and whether the displacement of a branch is
1220    too large to fit as an immediate argument of a 16-bit and a 32-bit
1221    branch, respectively.  */
1222 #define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic,		\
1223 			       uncond, compact, link, nods,	\
1224 			       relax32, toofar16, toofar32)	\
1225   (0x40000000							\
1226    | ((type) & 0xff)						\
1227    | (((at) & 0x1f) << 8)					\
1228    | ((insn32) ? 0x2000 : 0)					\
1229    | ((pic) ? 0x4000 : 0)					\
1230    | ((uncond) ? 0x8000 : 0)					\
1231    | ((compact) ? 0x10000 : 0)					\
1232    | ((link) ? 0x20000 : 0)					\
1233    | ((nods) ? 0x40000 : 0)					\
1234    | ((relax32) ? 0x80000 : 0)					\
1235    | ((toofar16) ? 0x100000 : 0)				\
1236    | ((toofar32) ? 0x200000 : 0))
1237 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1238 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1239 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1240 #define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
1241 #define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1242 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1243 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1244 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1245 #define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1246 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1247 
1248 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1249 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1250 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1251 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1252 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1253 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
1254 
1255 /* Sign-extend 16-bit value X.  */
1256 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1257 
1258 /* Is the given value a sign-extended 32-bit value?  */
1259 #define IS_SEXT_32BIT_NUM(x)						\
1260   (((x) &~ (offsetT) 0x7fffffff) == 0					\
1261    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1262 
1263 /* Is the given value a sign-extended 16-bit value?  */
1264 #define IS_SEXT_16BIT_NUM(x)						\
1265   (((x) &~ (offsetT) 0x7fff) == 0					\
1266    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1267 
1268 /* Is the given value a sign-extended 12-bit value?  */
1269 #define IS_SEXT_12BIT_NUM(x)						\
1270   (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1271 
1272 /* Is the given value a sign-extended 9-bit value?  */
1273 #define IS_SEXT_9BIT_NUM(x)						\
1274   (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1275 
1276 /* Is the given value a zero-extended 32-bit value?  Or a negated one?  */
1277 #define IS_ZEXT_32BIT_NUM(x)						\
1278   (((x) &~ (offsetT) 0xffffffff) == 0					\
1279    || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1280 
1281 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1282    SHIFT places.  */
1283 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1284   (((STRUCT) >> (SHIFT)) & (MASK))
1285 
1286 /* Extract the operand given by FIELD from mips_cl_insn INSN.  */
1287 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1288   (!(MICROMIPS) \
1289    ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1290    : EXTRACT_BITS ((INSN).insn_opcode, \
1291 		   MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1292 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1293   EXTRACT_BITS ((INSN).insn_opcode, \
1294 		MIPS16OP_MASK_##FIELD, \
1295 		MIPS16OP_SH_##FIELD)
1296 
1297 /* The MIPS16 EXTEND opcode, shifted left 16 places.  */
1298 #define MIPS16_EXTEND (0xf000U << 16)
1299 
1300 /* Whether or not we are emitting a branch-likely macro.  */
1301 static bool emit_branch_likely_macro = false;
1302 
1303 /* Global variables used when generating relaxable macros.  See the
1304    comment above RELAX_ENCODE for more details about how relaxation
1305    is used.  */
1306 static struct {
1307   /* 0 if we're not emitting a relaxable macro.
1308      1 if we're emitting the first of the two relaxation alternatives.
1309      2 if we're emitting the second alternative.  */
1310   int sequence;
1311 
1312   /* The first relaxable fixup in the current frag.  (In other words,
1313      the first fixup that refers to relaxable code.)  */
1314   fixS *first_fixup;
1315 
1316   /* sizes[0] says how many bytes of the first alternative are stored in
1317      the current frag.  Likewise sizes[1] for the second alternative.  */
1318   unsigned int sizes[2];
1319 
1320   /* The symbol on which the choice of sequence depends.  */
1321   symbolS *symbol;
1322 } mips_relax;
1323 
1324 /* Global variables used to decide whether a macro needs a warning.  */
1325 static struct {
1326   /* True if the macro is in a branch delay slot.  */
1327   bool delay_slot_p;
1328 
1329   /* Set to the length in bytes required if the macro is in a delay slot
1330      that requires a specific length of instruction, otherwise zero.  */
1331   unsigned int delay_slot_length;
1332 
1333   /* For relaxable macros, sizes[0] is the length of the first alternative
1334      in bytes and sizes[1] is the length of the second alternative.
1335      For non-relaxable macros, both elements give the length of the
1336      macro in bytes.  */
1337   unsigned int sizes[2];
1338 
1339   /* For relaxable macros, first_insn_sizes[0] is the length of the first
1340      instruction of the first alternative in bytes and first_insn_sizes[1]
1341      is the length of the first instruction of the second alternative.
1342      For non-relaxable macros, both elements give the length of the first
1343      instruction in bytes.
1344 
1345      Set to zero if we haven't yet seen the first instruction.  */
1346   unsigned int first_insn_sizes[2];
1347 
1348   /* For relaxable macros, insns[0] is the number of instructions for the
1349      first alternative and insns[1] is the number of instructions for the
1350      second alternative.
1351 
1352      For non-relaxable macros, both elements give the number of
1353      instructions for the macro.  */
1354   unsigned int insns[2];
1355 
1356   /* The first variant frag for this macro.  */
1357   fragS *first_frag;
1358 } mips_macro_warning;
1359 
1360 /* Prototypes for static functions.  */
1361 
1362 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1363 
1364 static void append_insn
1365   (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1366    bool expansionp);
1367 static void mips_no_prev_insn (void);
1368 static void macro_build (expressionS *, const char *, const char *, ...);
1369 static void mips16_macro_build
1370   (expressionS *, const char *, const char *, va_list *);
1371 static void load_register (int, expressionS *, int);
1372 static void macro_start (void);
1373 static void macro_end (void);
1374 static void macro (struct mips_cl_insn *ip, char *str);
1375 static void mips16_macro (struct mips_cl_insn * ip);
1376 static void mips_ip (char *str, struct mips_cl_insn * ip);
1377 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1378 static unsigned long mips16_immed_extend (offsetT, unsigned int);
1379 static void mips16_immed
1380   (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1381    unsigned int, unsigned long *);
1382 static size_t my_getSmallExpression
1383   (expressionS *, bfd_reloc_code_real_type *, char *);
1384 static void my_getExpression (expressionS *, char *);
1385 static void s_align (int);
1386 static void s_change_sec (int);
1387 static void s_change_section (int);
1388 static void s_cons (int);
1389 static void s_float_cons (int);
1390 static void s_mips_globl (int);
1391 static void s_option (int);
1392 static void s_mipsset (int);
1393 static void s_abicalls (int);
1394 static void s_cpload (int);
1395 static void s_cpsetup (int);
1396 static void s_cplocal (int);
1397 static void s_cprestore (int);
1398 static void s_cpreturn (int);
1399 static void s_dtprelword (int);
1400 static void s_dtpreldword (int);
1401 static void s_tprelword (int);
1402 static void s_tpreldword (int);
1403 static void s_gpvalue (int);
1404 static void s_gpword (int);
1405 static void s_gpdword (int);
1406 static void s_ehword (int);
1407 static void s_cpadd (int);
1408 static void s_insn (int);
1409 static void s_nan (int);
1410 static void s_module (int);
1411 static void s_mips_ent (int);
1412 static void s_mips_end (int);
1413 static void s_mips_frame (int);
1414 static void s_mips_mask (int reg_type);
1415 static void s_mips_stab (int);
1416 static void s_mips_weakext (int);
1417 static void s_mips_file (int);
1418 static void s_mips_loc (int);
1419 static bool pic_need_relax (symbolS *);
1420 static int relaxed_branch_length (fragS *, asection *, int);
1421 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1422 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1423 static void file_mips_check_options (void);
1424 
1425 /* Table and functions used to map between CPU/ISA names, and
1426    ISA levels, and CPU numbers.  */
1427 
1428 struct mips_cpu_info
1429 {
1430   const char *name;           /* CPU or ISA name.  */
1431   int flags;                  /* MIPS_CPU_* flags.  */
1432   int ase;                    /* Set of ASEs implemented by the CPU.  */
1433   int isa;                    /* ISA level.  */
1434   int cpu;                    /* CPU number (default CPU if ISA).  */
1435 };
1436 
1437 #define MIPS_CPU_IS_ISA		0x0001	/* Is this an ISA?  (If 0, a CPU.) */
1438 
1439 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1440 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1441 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1442 
1443 /* Command-line options.  */
1444 const char *md_shortopts = "O::g::G:";
1445 
1446 enum options
1447   {
1448     OPTION_MARCH = OPTION_MD_BASE,
1449     OPTION_MTUNE,
1450     OPTION_MIPS1,
1451     OPTION_MIPS2,
1452     OPTION_MIPS3,
1453     OPTION_MIPS4,
1454     OPTION_MIPS5,
1455     OPTION_MIPS32,
1456     OPTION_MIPS64,
1457     OPTION_MIPS32R2,
1458     OPTION_MIPS32R3,
1459     OPTION_MIPS32R5,
1460     OPTION_MIPS32R6,
1461     OPTION_MIPS64R2,
1462     OPTION_MIPS64R3,
1463     OPTION_MIPS64R5,
1464     OPTION_MIPS64R6,
1465     OPTION_MIPS16,
1466     OPTION_NO_MIPS16,
1467     OPTION_MIPS3D,
1468     OPTION_NO_MIPS3D,
1469     OPTION_MDMX,
1470     OPTION_NO_MDMX,
1471     OPTION_DSP,
1472     OPTION_NO_DSP,
1473     OPTION_MT,
1474     OPTION_NO_MT,
1475     OPTION_VIRT,
1476     OPTION_NO_VIRT,
1477     OPTION_MSA,
1478     OPTION_NO_MSA,
1479     OPTION_SMARTMIPS,
1480     OPTION_NO_SMARTMIPS,
1481     OPTION_DSPR2,
1482     OPTION_NO_DSPR2,
1483     OPTION_DSPR3,
1484     OPTION_NO_DSPR3,
1485     OPTION_EVA,
1486     OPTION_NO_EVA,
1487     OPTION_XPA,
1488     OPTION_NO_XPA,
1489     OPTION_MICROMIPS,
1490     OPTION_NO_MICROMIPS,
1491     OPTION_MCU,
1492     OPTION_NO_MCU,
1493     OPTION_MIPS16E2,
1494     OPTION_NO_MIPS16E2,
1495     OPTION_CRC,
1496     OPTION_NO_CRC,
1497     OPTION_M4650,
1498     OPTION_NO_M4650,
1499     OPTION_M4010,
1500     OPTION_NO_M4010,
1501     OPTION_M4100,
1502     OPTION_NO_M4100,
1503     OPTION_M3900,
1504     OPTION_NO_M3900,
1505     OPTION_M7000_HILO_FIX,
1506     OPTION_MNO_7000_HILO_FIX,
1507     OPTION_FIX_24K,
1508     OPTION_NO_FIX_24K,
1509     OPTION_FIX_RM7000,
1510     OPTION_NO_FIX_RM7000,
1511     OPTION_FIX_LOONGSON3_LLSC,
1512     OPTION_NO_FIX_LOONGSON3_LLSC,
1513     OPTION_FIX_LOONGSON2F_JUMP,
1514     OPTION_NO_FIX_LOONGSON2F_JUMP,
1515     OPTION_FIX_LOONGSON2F_NOP,
1516     OPTION_NO_FIX_LOONGSON2F_NOP,
1517     OPTION_FIX_VR4120,
1518     OPTION_NO_FIX_VR4120,
1519     OPTION_FIX_VR4130,
1520     OPTION_NO_FIX_VR4130,
1521     OPTION_FIX_CN63XXP1,
1522     OPTION_NO_FIX_CN63XXP1,
1523     OPTION_FIX_R5900,
1524     OPTION_NO_FIX_R5900,
1525     OPTION_TRAP,
1526     OPTION_BREAK,
1527     OPTION_EB,
1528     OPTION_EL,
1529     OPTION_FP32,
1530     OPTION_GP32,
1531     OPTION_CONSTRUCT_FLOATS,
1532     OPTION_NO_CONSTRUCT_FLOATS,
1533     OPTION_FP64,
1534     OPTION_FPXX,
1535     OPTION_GP64,
1536     OPTION_RELAX_BRANCH,
1537     OPTION_NO_RELAX_BRANCH,
1538     OPTION_IGNORE_BRANCH_ISA,
1539     OPTION_NO_IGNORE_BRANCH_ISA,
1540     OPTION_INSN32,
1541     OPTION_NO_INSN32,
1542     OPTION_MSHARED,
1543     OPTION_MNO_SHARED,
1544     OPTION_MSYM32,
1545     OPTION_MNO_SYM32,
1546     OPTION_SOFT_FLOAT,
1547     OPTION_HARD_FLOAT,
1548     OPTION_SINGLE_FLOAT,
1549     OPTION_DOUBLE_FLOAT,
1550     OPTION_32,
1551     OPTION_CALL_SHARED,
1552     OPTION_CALL_NONPIC,
1553     OPTION_NON_SHARED,
1554     OPTION_XGOT,
1555     OPTION_MABI,
1556     OPTION_N32,
1557     OPTION_64,
1558     OPTION_MDEBUG,
1559     OPTION_NO_MDEBUG,
1560     OPTION_PDR,
1561     OPTION_NO_PDR,
1562     OPTION_MVXWORKS_PIC,
1563     OPTION_NAN,
1564     OPTION_ODD_SPREG,
1565     OPTION_NO_ODD_SPREG,
1566     OPTION_GINV,
1567     OPTION_NO_GINV,
1568     OPTION_LOONGSON_MMI,
1569     OPTION_NO_LOONGSON_MMI,
1570     OPTION_LOONGSON_CAM,
1571     OPTION_NO_LOONGSON_CAM,
1572     OPTION_LOONGSON_EXT,
1573     OPTION_NO_LOONGSON_EXT,
1574     OPTION_LOONGSON_EXT2,
1575     OPTION_NO_LOONGSON_EXT2,
1576     OPTION_END_OF_ENUM
1577   };
1578 
1579 struct option md_longopts[] =
1580 {
1581   /* Options which specify architecture.  */
1582   {"march", required_argument, NULL, OPTION_MARCH},
1583   {"mtune", required_argument, NULL, OPTION_MTUNE},
1584   {"mips0", no_argument, NULL, OPTION_MIPS1},
1585   {"mips1", no_argument, NULL, OPTION_MIPS1},
1586   {"mips2", no_argument, NULL, OPTION_MIPS2},
1587   {"mips3", no_argument, NULL, OPTION_MIPS3},
1588   {"mips4", no_argument, NULL, OPTION_MIPS4},
1589   {"mips5", no_argument, NULL, OPTION_MIPS5},
1590   {"mips32", no_argument, NULL, OPTION_MIPS32},
1591   {"mips64", no_argument, NULL, OPTION_MIPS64},
1592   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1593   {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1594   {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
1595   {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
1596   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1597   {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1598   {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
1599   {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
1600 
1601   /* Options which specify Application Specific Extensions (ASEs).  */
1602   {"mips16", no_argument, NULL, OPTION_MIPS16},
1603   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1604   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1605   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1606   {"mdmx", no_argument, NULL, OPTION_MDMX},
1607   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1608   {"mdsp", no_argument, NULL, OPTION_DSP},
1609   {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1610   {"mmt", no_argument, NULL, OPTION_MT},
1611   {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1612   {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1613   {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1614   {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1615   {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1616   {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1617   {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
1618   {"meva", no_argument, NULL, OPTION_EVA},
1619   {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1620   {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1621   {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1622   {"mmcu", no_argument, NULL, OPTION_MCU},
1623   {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1624   {"mvirt", no_argument, NULL, OPTION_VIRT},
1625   {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1626   {"mmsa", no_argument, NULL, OPTION_MSA},
1627   {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
1628   {"mxpa", no_argument, NULL, OPTION_XPA},
1629   {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
1630   {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1631   {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
1632   {"mcrc", no_argument, NULL, OPTION_CRC},
1633   {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
1634   {"mginv", no_argument, NULL, OPTION_GINV},
1635   {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
1636   {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1637   {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
1638   {"mloongson-cam", no_argument, NULL, OPTION_LOONGSON_CAM},
1639   {"mno-loongson-cam", no_argument, NULL, OPTION_NO_LOONGSON_CAM},
1640   {"mloongson-ext", no_argument, NULL, OPTION_LOONGSON_EXT},
1641   {"mno-loongson-ext", no_argument, NULL, OPTION_NO_LOONGSON_EXT},
1642   {"mloongson-ext2", no_argument, NULL, OPTION_LOONGSON_EXT2},
1643   {"mno-loongson-ext2", no_argument, NULL, OPTION_NO_LOONGSON_EXT2},
1644 
1645   /* Old-style architecture options.  Don't add more of these.  */
1646   {"m4650", no_argument, NULL, OPTION_M4650},
1647   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1648   {"m4010", no_argument, NULL, OPTION_M4010},
1649   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1650   {"m4100", no_argument, NULL, OPTION_M4100},
1651   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1652   {"m3900", no_argument, NULL, OPTION_M3900},
1653   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1654 
1655   /* Options which enable bug fixes.  */
1656   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1657   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1658   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1659   {"mfix-loongson3-llsc",   no_argument, NULL, OPTION_FIX_LOONGSON3_LLSC},
1660   {"mno-fix-loongson3-llsc", no_argument, NULL, OPTION_NO_FIX_LOONGSON3_LLSC},
1661   {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1662   {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1663   {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1664   {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1665   {"mfix-vr4120",    no_argument, NULL, OPTION_FIX_VR4120},
1666   {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1667   {"mfix-vr4130",    no_argument, NULL, OPTION_FIX_VR4130},
1668   {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1669   {"mfix-24k",    no_argument, NULL, OPTION_FIX_24K},
1670   {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1671   {"mfix-rm7000",    no_argument, NULL, OPTION_FIX_RM7000},
1672   {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
1673   {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1674   {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1675   {"mfix-r5900", no_argument, NULL, OPTION_FIX_R5900},
1676   {"mno-fix-r5900", no_argument, NULL, OPTION_NO_FIX_R5900},
1677 
1678   /* Miscellaneous options.  */
1679   {"trap", no_argument, NULL, OPTION_TRAP},
1680   {"no-break", no_argument, NULL, OPTION_TRAP},
1681   {"break", no_argument, NULL, OPTION_BREAK},
1682   {"no-trap", no_argument, NULL, OPTION_BREAK},
1683   {"EB", no_argument, NULL, OPTION_EB},
1684   {"EL", no_argument, NULL, OPTION_EL},
1685   {"mfp32", no_argument, NULL, OPTION_FP32},
1686   {"mgp32", no_argument, NULL, OPTION_GP32},
1687   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1688   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1689   {"mfp64", no_argument, NULL, OPTION_FP64},
1690   {"mfpxx", no_argument, NULL, OPTION_FPXX},
1691   {"mgp64", no_argument, NULL, OPTION_GP64},
1692   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1693   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1694   {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1695   {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
1696   {"minsn32", no_argument, NULL, OPTION_INSN32},
1697   {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1698   {"mshared", no_argument, NULL, OPTION_MSHARED},
1699   {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1700   {"msym32", no_argument, NULL, OPTION_MSYM32},
1701   {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1702   {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1703   {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1704   {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1705   {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1706   {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1707   {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
1708 
1709   /* Strictly speaking this next option is ELF specific,
1710      but we allow it for other ports as well in order to
1711      make testing easier.  */
1712   {"32", no_argument, NULL, OPTION_32},
1713 
1714   /* ELF-specific options.  */
1715   {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1716   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1717   {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1718   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
1719   {"xgot", no_argument, NULL, OPTION_XGOT},
1720   {"mabi", required_argument, NULL, OPTION_MABI},
1721   {"n32", no_argument, NULL, OPTION_N32},
1722   {"64", no_argument, NULL, OPTION_64},
1723   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1724   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1725   {"mpdr", no_argument, NULL, OPTION_PDR},
1726   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1727   {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1728   {"mnan", required_argument, NULL, OPTION_NAN},
1729 
1730   {NULL, no_argument, NULL, 0}
1731 };
1732 size_t md_longopts_size = sizeof (md_longopts);
1733 
1734 /* Information about either an Application Specific Extension or an
1735    optional architecture feature that, for simplicity, we treat in the
1736    same way as an ASE.  */
1737 struct mips_ase
1738 {
1739   /* The name of the ASE, used in both the command-line and .set options.  */
1740   const char *name;
1741 
1742   /* The associated ASE_* flags.  If the ASE is available on both 32-bit
1743      and 64-bit architectures, the flags here refer to the subset that
1744      is available on both.  */
1745   unsigned int flags;
1746 
1747   /* The ASE_* flag used for instructions that are available on 64-bit
1748      architectures but that are not included in FLAGS.  */
1749   unsigned int flags64;
1750 
1751   /* The command-line options that turn the ASE on and off.  */
1752   int option_on;
1753   int option_off;
1754 
1755   /* The minimum required architecture revisions for MIPS32, MIPS64,
1756      microMIPS32 and microMIPS64, or -1 if the extension isn't supported.  */
1757   int mips32_rev;
1758   int mips64_rev;
1759   int micromips32_rev;
1760   int micromips64_rev;
1761 
1762   /* The architecture where the ASE was removed or -1 if the extension has not
1763      been removed.  */
1764   int rem_rev;
1765 };
1766 
1767 /* A table of all supported ASEs.  */
1768 static const struct mips_ase mips_ases[] = {
1769   { "dsp", ASE_DSP, ASE_DSP64,
1770     OPTION_DSP, OPTION_NO_DSP,
1771     2, 2, 2, 2,
1772     -1 },
1773 
1774   { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1775     OPTION_DSPR2, OPTION_NO_DSPR2,
1776     2, 2, 2, 2,
1777     -1 },
1778 
1779   { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1780     OPTION_DSPR3, OPTION_NO_DSPR3,
1781     6, 6, -1, -1,
1782     -1 },
1783 
1784   { "eva", ASE_EVA, 0,
1785     OPTION_EVA, OPTION_NO_EVA,
1786      2,  2,  2,  2,
1787     -1 },
1788 
1789   { "mcu", ASE_MCU, 0,
1790     OPTION_MCU, OPTION_NO_MCU,
1791      2,  2,  2,  2,
1792     -1 },
1793 
1794   /* Deprecated in MIPS64r5, but we don't implement that yet.  */
1795   { "mdmx", ASE_MDMX, 0,
1796     OPTION_MDMX, OPTION_NO_MDMX,
1797     -1, 1, -1, -1,
1798      6 },
1799 
1800   /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2.  */
1801   { "mips3d", ASE_MIPS3D, 0,
1802     OPTION_MIPS3D, OPTION_NO_MIPS3D,
1803     2, 1, -1, -1,
1804     6 },
1805 
1806   { "mt", ASE_MT, 0,
1807     OPTION_MT, OPTION_NO_MT,
1808      2,  2, -1, -1,
1809     -1 },
1810 
1811   { "smartmips", ASE_SMARTMIPS, 0,
1812     OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1813     1, -1, -1, -1,
1814     6 },
1815 
1816   { "virt", ASE_VIRT, ASE_VIRT64,
1817     OPTION_VIRT, OPTION_NO_VIRT,
1818      2,  2,  2,  2,
1819     -1 },
1820 
1821   { "msa", ASE_MSA, ASE_MSA64,
1822     OPTION_MSA, OPTION_NO_MSA,
1823      2,  2,  2,  2,
1824     -1 },
1825 
1826   { "xpa", ASE_XPA, 0,
1827     OPTION_XPA, OPTION_NO_XPA,
1828     2, 2, 2, 2,
1829     -1 },
1830 
1831   { "mips16e2", ASE_MIPS16E2, 0,
1832     OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1833     2,  2, -1, -1,
1834     6 },
1835 
1836   { "crc", ASE_CRC, ASE_CRC64,
1837     OPTION_CRC, OPTION_NO_CRC,
1838     6,  6, -1, -1,
1839     -1 },
1840 
1841   { "ginv", ASE_GINV, 0,
1842     OPTION_GINV, OPTION_NO_GINV,
1843     6,  6, 6, 6,
1844     -1 },
1845 
1846   { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1847     OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1848     0, 0, -1, -1,
1849     -1 },
1850 
1851   { "loongson-cam", ASE_LOONGSON_CAM, 0,
1852     OPTION_LOONGSON_CAM, OPTION_NO_LOONGSON_CAM,
1853     0, 0, -1, -1,
1854     -1 },
1855 
1856   { "loongson-ext", ASE_LOONGSON_EXT, 0,
1857     OPTION_LOONGSON_EXT, OPTION_NO_LOONGSON_EXT,
1858     0, 0, -1, -1,
1859     -1 },
1860 
1861   { "loongson-ext2", ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2, 0,
1862     OPTION_LOONGSON_EXT2, OPTION_NO_LOONGSON_EXT2,
1863     0, 0, -1, -1,
1864     -1 },
1865 };
1866 
1867 /* The set of ASEs that require -mfp64.  */
1868 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
1869 
1870 /* Groups of ASE_* flags that represent different revisions of an ASE.  */
1871 static const unsigned int mips_ase_groups[] = {
1872   ASE_DSP | ASE_DSPR2 | ASE_DSPR3,
1873   ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2
1874 };
1875 
1876 /* Pseudo-op table.
1877 
1878    The following pseudo-ops from the Kane and Heinrich MIPS book
1879    should be defined here, but are currently unsupported: .alias,
1880    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1881 
1882    The following pseudo-ops from the Kane and Heinrich MIPS book are
1883    specific to the type of debugging information being generated, and
1884    should be defined by the object format: .aent, .begin, .bend,
1885    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1886    .vreg.
1887 
1888    The following pseudo-ops from the Kane and Heinrich MIPS book are
1889    not MIPS CPU specific, but are also not specific to the object file
1890    format.  This file is probably the best place to define them, but
1891    they are not currently supported: .asm0, .endr, .lab, .struct.  */
1892 
1893 static const pseudo_typeS mips_pseudo_table[] =
1894 {
1895   /* MIPS specific pseudo-ops.  */
1896   {"option", s_option, 0},
1897   {"set", s_mipsset, 0},
1898   {"rdata", s_change_sec, 'r'},
1899   {"sdata", s_change_sec, 's'},
1900   {"livereg", s_ignore, 0},
1901   {"abicalls", s_abicalls, 0},
1902   {"cpload", s_cpload, 0},
1903   {"cpsetup", s_cpsetup, 0},
1904   {"cplocal", s_cplocal, 0},
1905   {"cprestore", s_cprestore, 0},
1906   {"cpreturn", s_cpreturn, 0},
1907   {"dtprelword", s_dtprelword, 0},
1908   {"dtpreldword", s_dtpreldword, 0},
1909   {"tprelword", s_tprelword, 0},
1910   {"tpreldword", s_tpreldword, 0},
1911   {"gpvalue", s_gpvalue, 0},
1912   {"gpword", s_gpword, 0},
1913   {"gpdword", s_gpdword, 0},
1914   {"ehword", s_ehword, 0},
1915   {"cpadd", s_cpadd, 0},
1916   {"insn", s_insn, 0},
1917   {"nan", s_nan, 0},
1918   {"module", s_module, 0},
1919 
1920   /* Relatively generic pseudo-ops that happen to be used on MIPS
1921      chips.  */
1922   {"asciiz", stringer, 8 + 1},
1923   {"bss", s_change_sec, 'b'},
1924   {"err", s_err, 0},
1925   {"half", s_cons, 1},
1926   {"dword", s_cons, 3},
1927   {"weakext", s_mips_weakext, 0},
1928   {"origin", s_org, 0},
1929   {"repeat", s_rept, 0},
1930 
1931   /* For MIPS this is non-standard, but we define it for consistency.  */
1932   {"sbss", s_change_sec, 'B'},
1933 
1934   /* These pseudo-ops are defined in read.c, but must be overridden
1935      here for one reason or another.  */
1936   {"align", s_align, 0},
1937   {"byte", s_cons, 0},
1938   {"data", s_change_sec, 'd'},
1939   {"double", s_float_cons, 'd'},
1940   {"float", s_float_cons, 'f'},
1941   {"globl", s_mips_globl, 0},
1942   {"global", s_mips_globl, 0},
1943   {"hword", s_cons, 1},
1944   {"int", s_cons, 2},
1945   {"long", s_cons, 2},
1946   {"octa", s_cons, 4},
1947   {"quad", s_cons, 3},
1948   {"section", s_change_section, 0},
1949   {"short", s_cons, 1},
1950   {"single", s_float_cons, 'f'},
1951   {"stabd", s_mips_stab, 'd'},
1952   {"stabn", s_mips_stab, 'n'},
1953   {"stabs", s_mips_stab, 's'},
1954   {"text", s_change_sec, 't'},
1955   {"word", s_cons, 2},
1956 
1957   { "extern", ecoff_directive_extern, 0},
1958 
1959   { NULL, NULL, 0 },
1960 };
1961 
1962 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1963 {
1964   /* These pseudo-ops should be defined by the object file format.
1965      However, a.out doesn't support them, so we have versions here.  */
1966   {"aent", s_mips_ent, 1},
1967   {"bgnb", s_ignore, 0},
1968   {"end", s_mips_end, 0},
1969   {"endb", s_ignore, 0},
1970   {"ent", s_mips_ent, 0},
1971   {"file", s_mips_file, 0},
1972   {"fmask", s_mips_mask, 'F'},
1973   {"frame", s_mips_frame, 0},
1974   {"loc", s_mips_loc, 0},
1975   {"mask", s_mips_mask, 'R'},
1976   {"verstamp", s_ignore, 0},
1977   { NULL, NULL, 0 },
1978 };
1979 
1980 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1981    purpose of the `.dc.a' internal pseudo-op.  */
1982 
1983 int
mips_address_bytes(void)1984 mips_address_bytes (void)
1985 {
1986   file_mips_check_options ();
1987   return HAVE_64BIT_ADDRESSES ? 8 : 4;
1988 }
1989 
1990 extern void pop_insert (const pseudo_typeS *);
1991 
1992 void
mips_pop_insert(void)1993 mips_pop_insert (void)
1994 {
1995   pop_insert (mips_pseudo_table);
1996   if (! ECOFF_DEBUGGING)
1997     pop_insert (mips_nonecoff_pseudo_table);
1998 }
1999 
2000 /* Symbols labelling the current insn.  */
2001 
2002 struct insn_label_list
2003 {
2004   struct insn_label_list *next;
2005   symbolS *label;
2006 };
2007 
2008 static struct insn_label_list *free_insn_labels;
2009 #define label_list tc_segment_info_data.labels
2010 
2011 static void mips_clear_insn_labels (void);
2012 static void mips_mark_labels (void);
2013 static void mips_compressed_mark_labels (void);
2014 
2015 static inline void
mips_clear_insn_labels(void)2016 mips_clear_insn_labels (void)
2017 {
2018   struct insn_label_list **pl;
2019   segment_info_type *si;
2020 
2021   if (now_seg)
2022     {
2023       for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
2024 	;
2025 
2026       si = seg_info (now_seg);
2027       *pl = si->label_list;
2028       si->label_list = NULL;
2029     }
2030 }
2031 
2032 /* Mark instruction labels in MIPS16/microMIPS mode.  */
2033 
2034 static inline void
mips_mark_labels(void)2035 mips_mark_labels (void)
2036 {
2037   if (HAVE_CODE_COMPRESSION)
2038     mips_compressed_mark_labels ();
2039 }
2040 
2041 static char *expr_end;
2042 
2043 /* An expression in a macro instruction.  This is set by mips_ip and
2044    mips16_ip and when populated is always an O_constant.  */
2045 
2046 static expressionS imm_expr;
2047 
2048 /* The relocatable field in an instruction and the relocs associated
2049    with it.  These variables are used for instructions like LUI and
2050    JAL as well as true offsets.  They are also used for address
2051    operands in macros.  */
2052 
2053 static expressionS offset_expr;
2054 static bfd_reloc_code_real_type offset_reloc[3]
2055   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2056 
2057 /* This is set to the resulting size of the instruction to be produced
2058    by mips16_ip if an explicit extension is used or by mips_ip if an
2059    explicit size is supplied.  */
2060 
2061 static unsigned int forced_insn_length;
2062 
2063 /* True if we are assembling an instruction.  All dot symbols defined during
2064    this time should be treated as code labels.  */
2065 
2066 static bool mips_assembling_insn;
2067 
2068 /* The pdr segment for per procedure frame/regmask info.  Not used for
2069    ECOFF debugging.  */
2070 
2071 static segT pdr_seg;
2072 
2073 /* The default target format to use.  */
2074 
2075 #if defined (TE_FreeBSD)
2076 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2077 #elif defined (TE_TMIPS)
2078 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2079 #else
2080 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2081 #endif
2082 
2083 const char *
mips_target_format(void)2084 mips_target_format (void)
2085 {
2086   switch (OUTPUT_FLAVOR)
2087     {
2088     case bfd_target_elf_flavour:
2089 #ifdef TE_VXWORKS
2090       if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2091 	return (target_big_endian
2092 		? "elf32-bigmips-vxworks"
2093 		: "elf32-littlemips-vxworks");
2094 #endif
2095       return (target_big_endian
2096 	      ? (HAVE_64BIT_OBJECTS
2097 		 ? ELF_TARGET ("elf64-", "big")
2098 		 : (HAVE_NEWABI
2099 		    ? ELF_TARGET ("elf32-n", "big")
2100 		    : ELF_TARGET ("elf32-", "big")))
2101 	      : (HAVE_64BIT_OBJECTS
2102 		 ? ELF_TARGET ("elf64-", "little")
2103 		 : (HAVE_NEWABI
2104 		    ? ELF_TARGET ("elf32-n", "little")
2105 		    : ELF_TARGET ("elf32-", "little"))));
2106     default:
2107       abort ();
2108       return NULL;
2109     }
2110 }
2111 
2112 /* Return the ISA revision that is currently in use, or 0 if we are
2113    generating code for MIPS V or below.  */
2114 
2115 static int
mips_isa_rev(void)2116 mips_isa_rev (void)
2117 {
2118   if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2119     return 2;
2120 
2121   if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2122     return 3;
2123 
2124   if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2125     return 5;
2126 
2127   if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2128     return 6;
2129 
2130   /* microMIPS implies revision 2 or above.  */
2131   if (mips_opts.micromips)
2132     return 2;
2133 
2134   if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2135     return 1;
2136 
2137   return 0;
2138 }
2139 
2140 /* Return the mask of all ASEs that are revisions of those in FLAGS.  */
2141 
2142 static unsigned int
mips_ase_mask(unsigned int flags)2143 mips_ase_mask (unsigned int flags)
2144 {
2145   unsigned int i;
2146 
2147   for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2148     if (flags & mips_ase_groups[i])
2149       flags |= mips_ase_groups[i];
2150   return flags;
2151 }
2152 
2153 /* Check whether the current ISA supports ASE.  Issue a warning if
2154    appropriate.  */
2155 
2156 static void
mips_check_isa_supports_ase(const struct mips_ase * ase)2157 mips_check_isa_supports_ase (const struct mips_ase *ase)
2158 {
2159   const char *base;
2160   int min_rev, size;
2161   static unsigned int warned_isa;
2162   static unsigned int warned_fp32;
2163 
2164   if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2165     min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2166   else
2167     min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2168   if ((min_rev < 0 || mips_isa_rev () < min_rev)
2169       && (warned_isa & ase->flags) != ase->flags)
2170     {
2171       warned_isa |= ase->flags;
2172       base = mips_opts.micromips ? "microMIPS" : "MIPS";
2173       size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2174       if (min_rev < 0)
2175 	as_warn (_("the %d-bit %s architecture does not support the"
2176 		   " `%s' extension"), size, base, ase->name);
2177       else
2178 	as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
2179 		 ase->name, base, size, min_rev);
2180     }
2181   else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2182 	   && (warned_isa & ase->flags) != ase->flags)
2183     {
2184       warned_isa |= ase->flags;
2185       base = mips_opts.micromips ? "microMIPS" : "MIPS";
2186       size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2187       as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2188 	       ase->name, base, size, ase->rem_rev);
2189     }
2190 
2191   if ((ase->flags & FP64_ASES)
2192       && mips_opts.fp != 64
2193       && (warned_fp32 & ase->flags) != ase->flags)
2194     {
2195       warned_fp32 |= ase->flags;
2196       as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
2197     }
2198 }
2199 
2200 /* Check all enabled ASEs to see whether they are supported by the
2201    chosen architecture.  */
2202 
2203 static void
mips_check_isa_supports_ases(void)2204 mips_check_isa_supports_ases (void)
2205 {
2206   unsigned int i, mask;
2207 
2208   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2209     {
2210       mask = mips_ase_mask (mips_ases[i].flags);
2211       if ((mips_opts.ase & mask) == mips_ases[i].flags)
2212 	mips_check_isa_supports_ase (&mips_ases[i]);
2213     }
2214 }
2215 
2216 /* Set the state of ASE to ENABLED_P.  Return the mask of ASE_* flags
2217    that were affected.  */
2218 
2219 static unsigned int
mips_set_ase(const struct mips_ase * ase,struct mips_set_options * opts,bool enabled_p)2220 mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2221 	      bool enabled_p)
2222 {
2223   unsigned int mask;
2224 
2225   mask = mips_ase_mask (ase->flags);
2226   opts->ase &= ~mask;
2227 
2228   /* Clear combination ASE flags, which need to be recalculated based on
2229      updated regular ASE settings.  */
2230   opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT | ASE_EVA_R6);
2231 
2232   if (enabled_p)
2233     opts->ase |= ase->flags;
2234 
2235   /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2236      instructions which are only valid when both ASEs are enabled.
2237      This sets the ASE_XPA_VIRT flag when both ASEs are present.  */
2238   if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2239     {
2240       opts->ase |= ASE_XPA_VIRT;
2241       mask |= ASE_XPA_VIRT;
2242     }
2243   if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2244     {
2245       opts->ase |= ASE_MIPS16E2_MT;
2246       mask |= ASE_MIPS16E2_MT;
2247     }
2248 
2249   /* The EVA Extension has instructions which are only valid when the R6 ISA
2250      is enabled.  This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
2251      present.  */
2252   if (((opts->ase & ASE_EVA) != 0) && ISA_IS_R6 (opts->isa))
2253     {
2254       opts->ase |= ASE_EVA_R6;
2255       mask |= ASE_EVA_R6;
2256     }
2257 
2258   return mask;
2259 }
2260 
2261 /* Return the ASE called NAME, or null if none.  */
2262 
2263 static const struct mips_ase *
mips_lookup_ase(const char * name)2264 mips_lookup_ase (const char *name)
2265 {
2266   unsigned int i;
2267 
2268   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2269     if (strcmp (name, mips_ases[i].name) == 0)
2270       return &mips_ases[i];
2271   return NULL;
2272 }
2273 
2274 /* Return the length of a microMIPS instruction in bytes.  If bits of
2275    the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2276    otherwise it is a 32-bit instruction.  */
2277 
2278 static inline unsigned int
micromips_insn_length(const struct mips_opcode * mo)2279 micromips_insn_length (const struct mips_opcode *mo)
2280 {
2281   return mips_opcode_32bit_p (mo) ? 4 : 2;
2282 }
2283 
2284 /* Return the length of MIPS16 instruction OPCODE.  */
2285 
2286 static inline unsigned int
mips16_opcode_length(unsigned long opcode)2287 mips16_opcode_length (unsigned long opcode)
2288 {
2289   return (opcode >> 16) == 0 ? 2 : 4;
2290 }
2291 
2292 /* Return the length of instruction INSN.  */
2293 
2294 static inline unsigned int
insn_length(const struct mips_cl_insn * insn)2295 insn_length (const struct mips_cl_insn *insn)
2296 {
2297   if (mips_opts.micromips)
2298     return micromips_insn_length (insn->insn_mo);
2299   else if (mips_opts.mips16)
2300     return mips16_opcode_length (insn->insn_opcode);
2301   else
2302     return 4;
2303 }
2304 
2305 /* Initialise INSN from opcode entry MO.  Leave its position unspecified.  */
2306 
2307 static void
create_insn(struct mips_cl_insn * insn,const struct mips_opcode * mo)2308 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2309 {
2310   size_t i;
2311 
2312   insn->insn_mo = mo;
2313   insn->insn_opcode = mo->match;
2314   insn->frag = NULL;
2315   insn->where = 0;
2316   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2317     insn->fixp[i] = NULL;
2318   insn->fixed_p = (mips_opts.noreorder > 0);
2319   insn->noreorder_p = (mips_opts.noreorder > 0);
2320   insn->mips16_absolute_jump_p = 0;
2321   insn->complete_p = 0;
2322   insn->cleared_p = 0;
2323 }
2324 
2325 /* Get a list of all the operands in INSN.  */
2326 
2327 static const struct mips_operand_array *
insn_operands(const struct mips_cl_insn * insn)2328 insn_operands (const struct mips_cl_insn *insn)
2329 {
2330   if (insn->insn_mo >= &mips_opcodes[0]
2331       && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2332     return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2333 
2334   if (insn->insn_mo >= &mips16_opcodes[0]
2335       && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2336     return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2337 
2338   if (insn->insn_mo >= &micromips_opcodes[0]
2339       && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2340     return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2341 
2342   abort ();
2343 }
2344 
2345 /* Get a description of operand OPNO of INSN.  */
2346 
2347 static const struct mips_operand *
insn_opno(const struct mips_cl_insn * insn,unsigned opno)2348 insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2349 {
2350   const struct mips_operand_array *operands;
2351 
2352   operands = insn_operands (insn);
2353   if (opno >= MAX_OPERANDS || !operands->operand[opno])
2354     abort ();
2355   return operands->operand[opno];
2356 }
2357 
2358 /* Install UVAL as the value of OPERAND in INSN.  */
2359 
2360 static inline void
insn_insert_operand(struct mips_cl_insn * insn,const struct mips_operand * operand,unsigned int uval)2361 insn_insert_operand (struct mips_cl_insn *insn,
2362 		     const struct mips_operand *operand, unsigned int uval)
2363 {
2364   if (mips_opts.mips16
2365       && operand->type == OP_INT && operand->lsb == 0
2366       && mips_opcode_32bit_p (insn->insn_mo))
2367     insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2368   else
2369     insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2370 }
2371 
2372 /* Extract the value of OPERAND from INSN.  */
2373 
2374 static inline unsigned
insn_extract_operand(const struct mips_cl_insn * insn,const struct mips_operand * operand)2375 insn_extract_operand (const struct mips_cl_insn *insn,
2376 		      const struct mips_operand *operand)
2377 {
2378   return mips_extract_operand (operand, insn->insn_opcode);
2379 }
2380 
2381 /* Record the current MIPS16/microMIPS mode in now_seg.  */
2382 
2383 static void
mips_record_compressed_mode(void)2384 mips_record_compressed_mode (void)
2385 {
2386   segment_info_type *si;
2387 
2388   si = seg_info (now_seg);
2389   if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2390     si->tc_segment_info_data.mips16 = mips_opts.mips16;
2391   if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2392     si->tc_segment_info_data.micromips = mips_opts.micromips;
2393 }
2394 
2395 /* Read a standard MIPS instruction from BUF.  */
2396 
2397 static unsigned long
read_insn(char * buf)2398 read_insn (char *buf)
2399 {
2400   if (target_big_endian)
2401     return bfd_getb32 ((bfd_byte *) buf);
2402   else
2403     return bfd_getl32 ((bfd_byte *) buf);
2404 }
2405 
2406 /* Write standard MIPS instruction INSN to BUF.  Return a pointer to
2407    the next byte.  */
2408 
2409 static char *
write_insn(char * buf,unsigned int insn)2410 write_insn (char *buf, unsigned int insn)
2411 {
2412   md_number_to_chars (buf, insn, 4);
2413   return buf + 4;
2414 }
2415 
2416 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2417    has length LENGTH.  */
2418 
2419 static unsigned long
read_compressed_insn(char * buf,unsigned int length)2420 read_compressed_insn (char *buf, unsigned int length)
2421 {
2422   unsigned long insn;
2423   unsigned int i;
2424 
2425   insn = 0;
2426   for (i = 0; i < length; i += 2)
2427     {
2428       insn <<= 16;
2429       if (target_big_endian)
2430 	insn |= bfd_getb16 ((char *) buf);
2431       else
2432 	insn |= bfd_getl16 ((char *) buf);
2433       buf += 2;
2434     }
2435   return insn;
2436 }
2437 
2438 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2439    instruction is LENGTH bytes long.  Return a pointer to the next byte.  */
2440 
2441 static char *
write_compressed_insn(char * buf,unsigned int insn,unsigned int length)2442 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2443 {
2444   unsigned int i;
2445 
2446   for (i = 0; i < length; i += 2)
2447     md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2448   return buf + length;
2449 }
2450 
2451 /* Install INSN at the location specified by its "frag" and "where" fields.  */
2452 
2453 static void
install_insn(const struct mips_cl_insn * insn)2454 install_insn (const struct mips_cl_insn *insn)
2455 {
2456   char *f = insn->frag->fr_literal + insn->where;
2457   if (HAVE_CODE_COMPRESSION)
2458     write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2459   else
2460     write_insn (f, insn->insn_opcode);
2461   mips_record_compressed_mode ();
2462 }
2463 
2464 /* Move INSN to offset WHERE in FRAG.  Adjust the fixups accordingly
2465    and install the opcode in the new location.  */
2466 
2467 static void
move_insn(struct mips_cl_insn * insn,fragS * frag,long where)2468 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2469 {
2470   size_t i;
2471 
2472   insn->frag = frag;
2473   insn->where = where;
2474   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2475     if (insn->fixp[i] != NULL)
2476       {
2477 	insn->fixp[i]->fx_frag = frag;
2478 	insn->fixp[i]->fx_where = where;
2479       }
2480   install_insn (insn);
2481 }
2482 
2483 /* Add INSN to the end of the output.  */
2484 
2485 static void
add_fixed_insn(struct mips_cl_insn * insn)2486 add_fixed_insn (struct mips_cl_insn *insn)
2487 {
2488   char *f = frag_more (insn_length (insn));
2489   move_insn (insn, frag_now, f - frag_now->fr_literal);
2490 }
2491 
2492 /* Start a variant frag and move INSN to the start of the variant part,
2493    marking it as fixed.  The other arguments are as for frag_var.  */
2494 
2495 static void
add_relaxed_insn(struct mips_cl_insn * insn,int max_chars,int var,relax_substateT subtype,symbolS * symbol,offsetT offset)2496 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2497 		  relax_substateT subtype, symbolS *symbol, offsetT offset)
2498 {
2499   frag_grow (max_chars);
2500   move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2501   insn->fixed_p = 1;
2502   frag_var (rs_machine_dependent, max_chars, var,
2503 	    subtype, symbol, offset, NULL);
2504 }
2505 
2506 /* Insert N copies of INSN into the history buffer, starting at
2507    position FIRST.  Neither FIRST nor N need to be clipped.  */
2508 
2509 static void
insert_into_history(unsigned int first,unsigned int n,const struct mips_cl_insn * insn)2510 insert_into_history (unsigned int first, unsigned int n,
2511 		     const struct mips_cl_insn *insn)
2512 {
2513   if (mips_relax.sequence != 2)
2514     {
2515       unsigned int i;
2516 
2517       for (i = ARRAY_SIZE (history); i-- > first;)
2518 	if (i >= first + n)
2519 	  history[i] = history[i - n];
2520 	else
2521 	  history[i] = *insn;
2522     }
2523 }
2524 
2525 /* Clear the error in insn_error.  */
2526 
2527 static void
clear_insn_error(void)2528 clear_insn_error (void)
2529 {
2530   memset (&insn_error, 0, sizeof (insn_error));
2531 }
2532 
2533 /* Possibly record error message MSG for the current instruction.
2534    If the error is about a particular argument, ARGNUM is the 1-based
2535    number of that argument, otherwise it is 0.  FORMAT is the format
2536    of MSG.  Return true if MSG was used, false if the current message
2537    was kept.  */
2538 
2539 static bool
set_insn_error_format(int argnum,enum mips_insn_error_format format,const char * msg)2540 set_insn_error_format (int argnum, enum mips_insn_error_format format,
2541 		       const char *msg)
2542 {
2543   if (argnum == 0)
2544     {
2545       /* Give priority to errors against specific arguments, and to
2546 	 the first whole-instruction message.  */
2547       if (insn_error.msg)
2548 	return false;
2549     }
2550   else
2551     {
2552       /* Keep insn_error if it is against a later argument.  */
2553       if (argnum < insn_error.min_argnum)
2554 	return false;
2555 
2556       /* If both errors are against the same argument but are different,
2557 	 give up on reporting a specific error for this argument.
2558 	 See the comment about mips_insn_error for details.  */
2559       if (argnum == insn_error.min_argnum
2560 	  && insn_error.msg
2561 	  && strcmp (insn_error.msg, msg) != 0)
2562 	{
2563 	  insn_error.msg = 0;
2564 	  insn_error.min_argnum += 1;
2565 	  return false;
2566 	}
2567     }
2568   insn_error.min_argnum = argnum;
2569   insn_error.format = format;
2570   insn_error.msg = msg;
2571   return true;
2572 }
2573 
2574 /* Record an instruction error with no % format fields.  ARGNUM and MSG are
2575    as for set_insn_error_format.  */
2576 
2577 static void
set_insn_error(int argnum,const char * msg)2578 set_insn_error (int argnum, const char *msg)
2579 {
2580   set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2581 }
2582 
2583 /* Record an instruction error with one %d field I.  ARGNUM and MSG are
2584    as for set_insn_error_format.  */
2585 
2586 static void
set_insn_error_i(int argnum,const char * msg,int i)2587 set_insn_error_i (int argnum, const char *msg, int i)
2588 {
2589   if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2590     insn_error.u.i = i;
2591 }
2592 
2593 /* Record an instruction error with two %s fields S1 and S2.  ARGNUM and MSG
2594    are as for set_insn_error_format.  */
2595 
2596 static void
set_insn_error_ss(int argnum,const char * msg,const char * s1,const char * s2)2597 set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2598 {
2599   if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2600     {
2601       insn_error.u.ss[0] = s1;
2602       insn_error.u.ss[1] = s2;
2603     }
2604 }
2605 
2606 /* Report the error in insn_error, which is against assembly code STR.  */
2607 
2608 static void
report_insn_error(const char * str)2609 report_insn_error (const char *str)
2610 {
2611   const char *msg = concat (insn_error.msg, " `%s'", NULL);
2612 
2613   switch (insn_error.format)
2614     {
2615     case ERR_FMT_PLAIN:
2616       as_bad (msg, str);
2617       break;
2618 
2619     case ERR_FMT_I:
2620       as_bad (msg, insn_error.u.i, str);
2621       break;
2622 
2623     case ERR_FMT_SS:
2624       as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2625       break;
2626     }
2627 
2628   free ((char *) msg);
2629 }
2630 
2631 /* Initialize vr4120_conflicts.  There is a bit of duplication here:
2632    the idea is to make it obvious at a glance that each errata is
2633    included.  */
2634 
2635 static void
init_vr4120_conflicts(void)2636 init_vr4120_conflicts (void)
2637 {
2638 #define CONFLICT(FIRST, SECOND) \
2639     vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2640 
2641   /* Errata 21 - [D]DIV[U] after [D]MACC */
2642   CONFLICT (MACC, DIV);
2643   CONFLICT (DMACC, DIV);
2644 
2645   /* Errata 23 - Continuous DMULT[U]/DMACC instructions.  */
2646   CONFLICT (DMULT, DMULT);
2647   CONFLICT (DMULT, DMACC);
2648   CONFLICT (DMACC, DMULT);
2649   CONFLICT (DMACC, DMACC);
2650 
2651   /* Errata 24 - MT{LO,HI} after [D]MACC */
2652   CONFLICT (MACC, MTHILO);
2653   CONFLICT (DMACC, MTHILO);
2654 
2655   /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2656      instruction is executed immediately after a MACC or DMACC
2657      instruction, the result of [either instruction] is incorrect."  */
2658   CONFLICT (MACC, MULT);
2659   CONFLICT (MACC, DMULT);
2660   CONFLICT (DMACC, MULT);
2661   CONFLICT (DMACC, DMULT);
2662 
2663   /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2664      executed immediately after a DMULT, DMULTU, DIV, DIVU,
2665      DDIV or DDIVU instruction, the result of the MACC or
2666      DMACC instruction is incorrect.".  */
2667   CONFLICT (DMULT, MACC);
2668   CONFLICT (DMULT, DMACC);
2669   CONFLICT (DIV, MACC);
2670   CONFLICT (DIV, DMACC);
2671 
2672 #undef CONFLICT
2673 }
2674 
2675 struct regname {
2676   const char *name;
2677   unsigned int num;
2678 };
2679 
2680 #define RNUM_MASK	0x00000ff
2681 #define RTYPE_MASK	0x0ffff00
2682 #define RTYPE_NUM	0x0000100
2683 #define RTYPE_FPU	0x0000200
2684 #define RTYPE_FCC	0x0000400
2685 #define RTYPE_VEC	0x0000800
2686 #define RTYPE_GP	0x0001000
2687 #define RTYPE_CP0	0x0002000
2688 #define RTYPE_PC	0x0004000
2689 #define RTYPE_ACC	0x0008000
2690 #define RTYPE_CCC	0x0010000
2691 #define RTYPE_VI	0x0020000
2692 #define RTYPE_VF	0x0040000
2693 #define RTYPE_R5900_I	0x0080000
2694 #define RTYPE_R5900_Q	0x0100000
2695 #define RTYPE_R5900_R	0x0200000
2696 #define RTYPE_R5900_ACC	0x0400000
2697 #define RTYPE_MSA	0x0800000
2698 #define RWARN		0x8000000
2699 
2700 #define GENERIC_REGISTER_NUMBERS \
2701     {"$0",	RTYPE_NUM | 0},  \
2702     {"$1",	RTYPE_NUM | 1},  \
2703     {"$2",	RTYPE_NUM | 2},  \
2704     {"$3",	RTYPE_NUM | 3},  \
2705     {"$4",	RTYPE_NUM | 4},  \
2706     {"$5",	RTYPE_NUM | 5},  \
2707     {"$6",	RTYPE_NUM | 6},  \
2708     {"$7",	RTYPE_NUM | 7},  \
2709     {"$8",	RTYPE_NUM | 8},  \
2710     {"$9",	RTYPE_NUM | 9},  \
2711     {"$10",	RTYPE_NUM | 10}, \
2712     {"$11",	RTYPE_NUM | 11}, \
2713     {"$12",	RTYPE_NUM | 12}, \
2714     {"$13",	RTYPE_NUM | 13}, \
2715     {"$14",	RTYPE_NUM | 14}, \
2716     {"$15",	RTYPE_NUM | 15}, \
2717     {"$16",	RTYPE_NUM | 16}, \
2718     {"$17",	RTYPE_NUM | 17}, \
2719     {"$18",	RTYPE_NUM | 18}, \
2720     {"$19",	RTYPE_NUM | 19}, \
2721     {"$20",	RTYPE_NUM | 20}, \
2722     {"$21",	RTYPE_NUM | 21}, \
2723     {"$22",	RTYPE_NUM | 22}, \
2724     {"$23",	RTYPE_NUM | 23}, \
2725     {"$24",	RTYPE_NUM | 24}, \
2726     {"$25",	RTYPE_NUM | 25}, \
2727     {"$26",	RTYPE_NUM | 26}, \
2728     {"$27",	RTYPE_NUM | 27}, \
2729     {"$28",	RTYPE_NUM | 28}, \
2730     {"$29",	RTYPE_NUM | 29}, \
2731     {"$30",	RTYPE_NUM | 30}, \
2732     {"$31",	RTYPE_NUM | 31}
2733 
2734 #define FPU_REGISTER_NAMES       \
2735     {"$f0",	RTYPE_FPU | 0},  \
2736     {"$f1",	RTYPE_FPU | 1},  \
2737     {"$f2",	RTYPE_FPU | 2},  \
2738     {"$f3",	RTYPE_FPU | 3},  \
2739     {"$f4",	RTYPE_FPU | 4},  \
2740     {"$f5",	RTYPE_FPU | 5},  \
2741     {"$f6",	RTYPE_FPU | 6},  \
2742     {"$f7",	RTYPE_FPU | 7},  \
2743     {"$f8",	RTYPE_FPU | 8},  \
2744     {"$f9",	RTYPE_FPU | 9},  \
2745     {"$f10",	RTYPE_FPU | 10}, \
2746     {"$f11",	RTYPE_FPU | 11}, \
2747     {"$f12",	RTYPE_FPU | 12}, \
2748     {"$f13",	RTYPE_FPU | 13}, \
2749     {"$f14",	RTYPE_FPU | 14}, \
2750     {"$f15",	RTYPE_FPU | 15}, \
2751     {"$f16",	RTYPE_FPU | 16}, \
2752     {"$f17",	RTYPE_FPU | 17}, \
2753     {"$f18",	RTYPE_FPU | 18}, \
2754     {"$f19",	RTYPE_FPU | 19}, \
2755     {"$f20",	RTYPE_FPU | 20}, \
2756     {"$f21",	RTYPE_FPU | 21}, \
2757     {"$f22",	RTYPE_FPU | 22}, \
2758     {"$f23",	RTYPE_FPU | 23}, \
2759     {"$f24",	RTYPE_FPU | 24}, \
2760     {"$f25",	RTYPE_FPU | 25}, \
2761     {"$f26",	RTYPE_FPU | 26}, \
2762     {"$f27",	RTYPE_FPU | 27}, \
2763     {"$f28",	RTYPE_FPU | 28}, \
2764     {"$f29",	RTYPE_FPU | 29}, \
2765     {"$f30",	RTYPE_FPU | 30}, \
2766     {"$f31",	RTYPE_FPU | 31}
2767 
2768 #define FPU_CONDITION_CODE_NAMES \
2769     {"$fcc0",	RTYPE_FCC | 0},  \
2770     {"$fcc1",	RTYPE_FCC | 1},  \
2771     {"$fcc2",	RTYPE_FCC | 2},  \
2772     {"$fcc3",	RTYPE_FCC | 3},  \
2773     {"$fcc4",	RTYPE_FCC | 4},  \
2774     {"$fcc5",	RTYPE_FCC | 5},  \
2775     {"$fcc6",	RTYPE_FCC | 6},  \
2776     {"$fcc7",	RTYPE_FCC | 7}
2777 
2778 #define COPROC_CONDITION_CODE_NAMES         \
2779     {"$cc0",	RTYPE_FCC | RTYPE_CCC | 0}, \
2780     {"$cc1",	RTYPE_FCC | RTYPE_CCC | 1}, \
2781     {"$cc2",	RTYPE_FCC | RTYPE_CCC | 2}, \
2782     {"$cc3",	RTYPE_FCC | RTYPE_CCC | 3}, \
2783     {"$cc4",	RTYPE_FCC | RTYPE_CCC | 4}, \
2784     {"$cc5",	RTYPE_FCC | RTYPE_CCC | 5}, \
2785     {"$cc6",	RTYPE_FCC | RTYPE_CCC | 6}, \
2786     {"$cc7",	RTYPE_FCC | RTYPE_CCC | 7}
2787 
2788 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2789     {"$a4",	RTYPE_GP | 8},  \
2790     {"$a5",	RTYPE_GP | 9},  \
2791     {"$a6",	RTYPE_GP | 10}, \
2792     {"$a7",	RTYPE_GP | 11}, \
2793     {"$ta0",	RTYPE_GP | 8},  /* alias for $a4 */ \
2794     {"$ta1",	RTYPE_GP | 9},  /* alias for $a5 */ \
2795     {"$ta2",	RTYPE_GP | 10}, /* alias for $a6 */ \
2796     {"$ta3",	RTYPE_GP | 11}, /* alias for $a7 */ \
2797     {"$t0",	RTYPE_GP | 12}, \
2798     {"$t1",	RTYPE_GP | 13}, \
2799     {"$t2",	RTYPE_GP | 14}, \
2800     {"$t3",	RTYPE_GP | 15}
2801 
2802 #define O32_SYMBOLIC_REGISTER_NAMES \
2803     {"$t0",	RTYPE_GP | 8},  \
2804     {"$t1",	RTYPE_GP | 9},  \
2805     {"$t2",	RTYPE_GP | 10}, \
2806     {"$t3",	RTYPE_GP | 11}, \
2807     {"$t4",	RTYPE_GP | 12}, \
2808     {"$t5",	RTYPE_GP | 13}, \
2809     {"$t6",	RTYPE_GP | 14}, \
2810     {"$t7",	RTYPE_GP | 15}, \
2811     {"$ta0",	RTYPE_GP | 12}, /* alias for $t4 */ \
2812     {"$ta1",	RTYPE_GP | 13}, /* alias for $t5 */ \
2813     {"$ta2",	RTYPE_GP | 14}, /* alias for $t6 */ \
2814     {"$ta3",	RTYPE_GP | 15}  /* alias for $t7 */
2815 
2816 /* Remaining symbolic register names.  */
2817 #define SYMBOLIC_REGISTER_NAMES \
2818     {"$zero",	RTYPE_GP | 0},  \
2819     {"$at",	RTYPE_GP | 1},  \
2820     {"$AT",	RTYPE_GP | 1},  \
2821     {"$v0",	RTYPE_GP | 2},  \
2822     {"$v1",	RTYPE_GP | 3},  \
2823     {"$a0",	RTYPE_GP | 4},  \
2824     {"$a1",	RTYPE_GP | 5},  \
2825     {"$a2",	RTYPE_GP | 6},  \
2826     {"$a3",	RTYPE_GP | 7},  \
2827     {"$s0",	RTYPE_GP | 16}, \
2828     {"$s1",	RTYPE_GP | 17}, \
2829     {"$s2",	RTYPE_GP | 18}, \
2830     {"$s3",	RTYPE_GP | 19}, \
2831     {"$s4",	RTYPE_GP | 20}, \
2832     {"$s5",	RTYPE_GP | 21}, \
2833     {"$s6",	RTYPE_GP | 22}, \
2834     {"$s7",	RTYPE_GP | 23}, \
2835     {"$t8",	RTYPE_GP | 24}, \
2836     {"$t9",	RTYPE_GP | 25}, \
2837     {"$k0",	RTYPE_GP | 26}, \
2838     {"$kt0",	RTYPE_GP | 26}, \
2839     {"$k1",	RTYPE_GP | 27}, \
2840     {"$kt1",	RTYPE_GP | 27}, \
2841     {"$gp",	RTYPE_GP | 28}, \
2842     {"$sp",	RTYPE_GP | 29}, \
2843     {"$s8",	RTYPE_GP | 30}, \
2844     {"$fp",	RTYPE_GP | 30}, \
2845     {"$ra",	RTYPE_GP | 31}
2846 
2847 #define MIPS16_SPECIAL_REGISTER_NAMES \
2848     {"$pc",	RTYPE_PC | 0}
2849 
2850 #define MDMX_VECTOR_REGISTER_NAMES \
2851     /* {"$v0",	RTYPE_VEC | 0},  Clash with REG 2 above.  */ \
2852     /* {"$v1",	RTYPE_VEC | 1},  Clash with REG 3 above.  */ \
2853     {"$v2",	RTYPE_VEC | 2},  \
2854     {"$v3",	RTYPE_VEC | 3},  \
2855     {"$v4",	RTYPE_VEC | 4},  \
2856     {"$v5",	RTYPE_VEC | 5},  \
2857     {"$v6",	RTYPE_VEC | 6},  \
2858     {"$v7",	RTYPE_VEC | 7},  \
2859     {"$v8",	RTYPE_VEC | 8},  \
2860     {"$v9",	RTYPE_VEC | 9},  \
2861     {"$v10",	RTYPE_VEC | 10}, \
2862     {"$v11",	RTYPE_VEC | 11}, \
2863     {"$v12",	RTYPE_VEC | 12}, \
2864     {"$v13",	RTYPE_VEC | 13}, \
2865     {"$v14",	RTYPE_VEC | 14}, \
2866     {"$v15",	RTYPE_VEC | 15}, \
2867     {"$v16",	RTYPE_VEC | 16}, \
2868     {"$v17",	RTYPE_VEC | 17}, \
2869     {"$v18",	RTYPE_VEC | 18}, \
2870     {"$v19",	RTYPE_VEC | 19}, \
2871     {"$v20",	RTYPE_VEC | 20}, \
2872     {"$v21",	RTYPE_VEC | 21}, \
2873     {"$v22",	RTYPE_VEC | 22}, \
2874     {"$v23",	RTYPE_VEC | 23}, \
2875     {"$v24",	RTYPE_VEC | 24}, \
2876     {"$v25",	RTYPE_VEC | 25}, \
2877     {"$v26",	RTYPE_VEC | 26}, \
2878     {"$v27",	RTYPE_VEC | 27}, \
2879     {"$v28",	RTYPE_VEC | 28}, \
2880     {"$v29",	RTYPE_VEC | 29}, \
2881     {"$v30",	RTYPE_VEC | 30}, \
2882     {"$v31",	RTYPE_VEC | 31}
2883 
2884 #define R5900_I_NAMES \
2885     {"$I",	RTYPE_R5900_I | 0}
2886 
2887 #define R5900_Q_NAMES \
2888     {"$Q",	RTYPE_R5900_Q | 0}
2889 
2890 #define R5900_R_NAMES \
2891     {"$R",	RTYPE_R5900_R | 0}
2892 
2893 #define R5900_ACC_NAMES \
2894     {"$ACC",	RTYPE_R5900_ACC | 0 }
2895 
2896 #define MIPS_DSP_ACCUMULATOR_NAMES \
2897     {"$ac0",	RTYPE_ACC | 0}, \
2898     {"$ac1",	RTYPE_ACC | 1}, \
2899     {"$ac2",	RTYPE_ACC | 2}, \
2900     {"$ac3",	RTYPE_ACC | 3}
2901 
2902 static const struct regname reg_names[] = {
2903   GENERIC_REGISTER_NUMBERS,
2904   FPU_REGISTER_NAMES,
2905   FPU_CONDITION_CODE_NAMES,
2906   COPROC_CONDITION_CODE_NAMES,
2907 
2908   /* The $txx registers depends on the abi,
2909      these will be added later into the symbol table from
2910      one of the tables below once mips_abi is set after
2911      parsing of arguments from the command line. */
2912   SYMBOLIC_REGISTER_NAMES,
2913 
2914   MIPS16_SPECIAL_REGISTER_NAMES,
2915   MDMX_VECTOR_REGISTER_NAMES,
2916   R5900_I_NAMES,
2917   R5900_Q_NAMES,
2918   R5900_R_NAMES,
2919   R5900_ACC_NAMES,
2920   MIPS_DSP_ACCUMULATOR_NAMES,
2921   {0, 0}
2922 };
2923 
2924 static const struct regname reg_names_o32[] = {
2925   O32_SYMBOLIC_REGISTER_NAMES,
2926   {0, 0}
2927 };
2928 
2929 static const struct regname reg_names_n32n64[] = {
2930   N32N64_SYMBOLIC_REGISTER_NAMES,
2931   {0, 0}
2932 };
2933 
2934 /* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2935    interpreted as vector registers 0 and 1.  If SYMVAL is the value of one
2936    of these register symbols, return the associated vector register,
2937    otherwise return SYMVAL itself.  */
2938 
2939 static unsigned int
mips_prefer_vec_regno(unsigned int symval)2940 mips_prefer_vec_regno (unsigned int symval)
2941 {
2942   if ((symval & -2) == (RTYPE_GP | 2))
2943     return RTYPE_VEC | (symval & 1);
2944   return symval;
2945 }
2946 
2947 /* Return true if string [S, E) is a valid register name, storing its
2948    symbol value in *SYMVAL_PTR if so.  */
2949 
2950 static bool
mips_parse_register_1(char * s,char * e,unsigned int * symval_ptr)2951 mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
2952 {
2953   char save_c;
2954   symbolS *symbol;
2955 
2956   /* Terminate name.  */
2957   save_c = *e;
2958   *e = '\0';
2959 
2960   /* Look up the name.  */
2961   symbol = symbol_find (s);
2962   *e = save_c;
2963 
2964   if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2965     return false;
2966 
2967   *symval_ptr = S_GET_VALUE (symbol);
2968   return true;
2969 }
2970 
2971 /* Return true if the string at *SPTR is a valid register name.  Allow it
2972    to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2973    is nonnull.
2974 
2975    When returning true, move *SPTR past the register, store the
2976    register's symbol value in *SYMVAL_PTR and the channel mask in
2977    *CHANNELS_PTR (if nonnull).  The symbol value includes the register
2978    number (RNUM_MASK) and register type (RTYPE_MASK).  The channel mask
2979    is a 4-bit value of the form XYZW and is 0 if no suffix was given.  */
2980 
2981 static bool
mips_parse_register(char ** sptr,unsigned int * symval_ptr,unsigned int * channels_ptr)2982 mips_parse_register (char **sptr, unsigned int *symval_ptr,
2983 		     unsigned int *channels_ptr)
2984 {
2985   char *s, *e, *m;
2986   const char *q;
2987   unsigned int channels, symval, bit;
2988 
2989   /* Find end of name.  */
2990   s = e = *sptr;
2991   if (is_name_beginner (*e))
2992     ++e;
2993   while (is_part_of_name (*e))
2994     ++e;
2995 
2996   channels = 0;
2997   if (!mips_parse_register_1 (s, e, &symval))
2998     {
2999       if (!channels_ptr)
3000 	return false;
3001 
3002       /* Eat characters from the end of the string that are valid
3003 	 channel suffixes.  The preceding register must be $ACC or
3004 	 end with a digit, so there is no ambiguity.  */
3005       bit = 1;
3006       m = e;
3007       for (q = "wzyx"; *q; q++, bit <<= 1)
3008 	if (m > s && m[-1] == *q)
3009 	  {
3010 	    --m;
3011 	    channels |= bit;
3012 	  }
3013 
3014       if (channels == 0
3015 	  || !mips_parse_register_1 (s, m, &symval)
3016 	  || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
3017 	return false;
3018     }
3019 
3020   *sptr = e;
3021   *symval_ptr = symval;
3022   if (channels_ptr)
3023     *channels_ptr = channels;
3024   return true;
3025 }
3026 
3027 /* Check if SPTR points at a valid register specifier according to TYPES.
3028    If so, then return 1, advance S to consume the specifier and store
3029    the register's number in REGNOP, otherwise return 0.  */
3030 
3031 static int
reg_lookup(char ** s,unsigned int types,unsigned int * regnop)3032 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
3033 {
3034   unsigned int regno;
3035 
3036   if (mips_parse_register (s, &regno, NULL))
3037     {
3038       if (types & RTYPE_VEC)
3039 	regno = mips_prefer_vec_regno (regno);
3040       if (regno & types)
3041 	regno &= RNUM_MASK;
3042       else
3043 	regno = ~0;
3044     }
3045   else
3046     {
3047       if (types & RWARN)
3048 	as_warn (_("unrecognized register name `%s'"), *s);
3049       regno = ~0;
3050     }
3051   if (regnop)
3052     *regnop = regno;
3053   return regno <= RNUM_MASK;
3054 }
3055 
3056 /* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
3057    mask in *CHANNELS.  Return a pointer to the first unconsumed character.  */
3058 
3059 static char *
mips_parse_vu0_channels(char * s,unsigned int * channels)3060 mips_parse_vu0_channels (char *s, unsigned int *channels)
3061 {
3062   unsigned int i;
3063 
3064   *channels = 0;
3065   for (i = 0; i < 4; i++)
3066     if (*s == "xyzw"[i])
3067       {
3068 	*channels |= 1 << (3 - i);
3069 	++s;
3070       }
3071   return s;
3072 }
3073 
3074 /* Token types for parsed operand lists.  */
3075 enum mips_operand_token_type {
3076   /* A plain register, e.g. $f2.  */
3077   OT_REG,
3078 
3079   /* A 4-bit XYZW channel mask.  */
3080   OT_CHANNELS,
3081 
3082   /* A constant vector index, e.g. [1].  */
3083   OT_INTEGER_INDEX,
3084 
3085   /* A register vector index, e.g. [$2].  */
3086   OT_REG_INDEX,
3087 
3088   /* A continuous range of registers, e.g. $s0-$s4.  */
3089   OT_REG_RANGE,
3090 
3091   /* A (possibly relocated) expression.  */
3092   OT_INTEGER,
3093 
3094   /* A floating-point value.  */
3095   OT_FLOAT,
3096 
3097   /* A single character.  This can be '(', ')' or ',', but '(' only appears
3098      before OT_REGs.  */
3099   OT_CHAR,
3100 
3101   /* A doubled character, either "--" or "++".  */
3102   OT_DOUBLE_CHAR,
3103 
3104   /* The end of the operand list.  */
3105   OT_END
3106 };
3107 
3108 /* A parsed operand token.  */
3109 struct mips_operand_token
3110 {
3111   /* The type of token.  */
3112   enum mips_operand_token_type type;
3113   union
3114   {
3115     /* The register symbol value for an OT_REG or OT_REG_INDEX.  */
3116     unsigned int regno;
3117 
3118     /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX.  */
3119     unsigned int channels;
3120 
3121     /* The integer value of an OT_INTEGER_INDEX.  */
3122     addressT index;
3123 
3124     /* The two register symbol values involved in an OT_REG_RANGE.  */
3125     struct {
3126       unsigned int regno1;
3127       unsigned int regno2;
3128     } reg_range;
3129 
3130     /* The value of an OT_INTEGER.  The value is represented as an
3131        expression and the relocation operators that were applied to
3132        that expression.  The reloc entries are BFD_RELOC_UNUSED if no
3133        relocation operators were used.  */
3134     struct {
3135       expressionS value;
3136       bfd_reloc_code_real_type relocs[3];
3137     } integer;
3138 
3139     /* The binary data for an OT_FLOAT constant, and the number of bytes
3140        in the constant.  */
3141     struct {
3142       unsigned char data[8];
3143       int length;
3144     } flt;
3145 
3146     /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR.  */
3147     char ch;
3148   } u;
3149 };
3150 
3151 /* An obstack used to construct lists of mips_operand_tokens.  */
3152 static struct obstack mips_operand_tokens;
3153 
3154 /* Give TOKEN type TYPE and add it to mips_operand_tokens.  */
3155 
3156 static void
mips_add_token(struct mips_operand_token * token,enum mips_operand_token_type type)3157 mips_add_token (struct mips_operand_token *token,
3158 		enum mips_operand_token_type type)
3159 {
3160   token->type = type;
3161   obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3162 }
3163 
3164 /* Check whether S is '(' followed by a register name.  Add OT_CHAR
3165    and OT_REG tokens for them if so, and return a pointer to the first
3166    unconsumed character.  Return null otherwise.  */
3167 
3168 static char *
mips_parse_base_start(char * s)3169 mips_parse_base_start (char *s)
3170 {
3171   struct mips_operand_token token;
3172   unsigned int regno, channels;
3173   bool decrement_p;
3174 
3175   if (*s != '(')
3176     return 0;
3177 
3178   ++s;
3179   SKIP_SPACE_TABS (s);
3180 
3181   /* Only match "--" as part of a base expression.  In other contexts "--X"
3182      is a double negative.  */
3183   decrement_p = (s[0] == '-' && s[1] == '-');
3184   if (decrement_p)
3185     {
3186       s += 2;
3187       SKIP_SPACE_TABS (s);
3188     }
3189 
3190   /* Allow a channel specifier because that leads to better error messages
3191      than treating something like "$vf0x++" as an expression.  */
3192   if (!mips_parse_register (&s, &regno, &channels))
3193     return 0;
3194 
3195   token.u.ch = '(';
3196   mips_add_token (&token, OT_CHAR);
3197 
3198   if (decrement_p)
3199     {
3200       token.u.ch = '-';
3201       mips_add_token (&token, OT_DOUBLE_CHAR);
3202     }
3203 
3204   token.u.regno = regno;
3205   mips_add_token (&token, OT_REG);
3206 
3207   if (channels)
3208     {
3209       token.u.channels = channels;
3210       mips_add_token (&token, OT_CHANNELS);
3211     }
3212 
3213   /* For consistency, only match "++" as part of base expressions too.  */
3214   SKIP_SPACE_TABS (s);
3215   if (s[0] == '+' && s[1] == '+')
3216     {
3217       s += 2;
3218       token.u.ch = '+';
3219       mips_add_token (&token, OT_DOUBLE_CHAR);
3220     }
3221 
3222   return s;
3223 }
3224 
3225 /* Parse one or more tokens from S.  Return a pointer to the first
3226    unconsumed character on success.  Return null if an error was found
3227    and store the error text in insn_error.  FLOAT_FORMAT is as for
3228    mips_parse_arguments.  */
3229 
3230 static char *
mips_parse_argument_token(char * s,char float_format)3231 mips_parse_argument_token (char *s, char float_format)
3232 {
3233   char *end, *save_in;
3234   const char *err;
3235   unsigned int regno1, regno2, channels;
3236   struct mips_operand_token token;
3237 
3238   /* First look for "($reg", since we want to treat that as an
3239      OT_CHAR and OT_REG rather than an expression.  */
3240   end = mips_parse_base_start (s);
3241   if (end)
3242     return end;
3243 
3244   /* Handle other characters that end up as OT_CHARs.  */
3245   if (*s == ')' || *s == ',')
3246     {
3247       token.u.ch = *s;
3248       mips_add_token (&token, OT_CHAR);
3249       ++s;
3250       return s;
3251     }
3252 
3253   /* Handle tokens that start with a register.  */
3254   if (mips_parse_register (&s, &regno1, &channels))
3255     {
3256       if (channels)
3257 	{
3258 	  /* A register and a VU0 channel suffix.  */
3259 	  token.u.regno = regno1;
3260 	  mips_add_token (&token, OT_REG);
3261 
3262 	  token.u.channels = channels;
3263 	  mips_add_token (&token, OT_CHANNELS);
3264 	  return s;
3265 	}
3266 
3267       SKIP_SPACE_TABS (s);
3268       if (*s == '-')
3269 	{
3270 	  /* A register range.  */
3271 	  ++s;
3272 	  SKIP_SPACE_TABS (s);
3273 	  if (!mips_parse_register (&s, &regno2, NULL))
3274 	    {
3275 	      set_insn_error (0, _("invalid register range"));
3276 	      return 0;
3277 	    }
3278 
3279 	  token.u.reg_range.regno1 = regno1;
3280 	  token.u.reg_range.regno2 = regno2;
3281 	  mips_add_token (&token, OT_REG_RANGE);
3282 	  return s;
3283 	}
3284 
3285       /* Add the register itself.  */
3286       token.u.regno = regno1;
3287       mips_add_token (&token, OT_REG);
3288 
3289       /* Check for a vector index.  */
3290       if (*s == '[')
3291 	{
3292 	  ++s;
3293 	  SKIP_SPACE_TABS (s);
3294 	  if (mips_parse_register (&s, &token.u.regno, NULL))
3295 	    mips_add_token (&token, OT_REG_INDEX);
3296 	  else
3297 	    {
3298 	      expressionS element;
3299 
3300 	      my_getExpression (&element, s);
3301 	      if (element.X_op != O_constant)
3302 		{
3303 		  set_insn_error (0, _("vector element must be constant"));
3304 		  return 0;
3305 		}
3306 	      s = expr_end;
3307 	      token.u.index = element.X_add_number;
3308 	      mips_add_token (&token, OT_INTEGER_INDEX);
3309 	    }
3310 	  SKIP_SPACE_TABS (s);
3311 	  if (*s != ']')
3312 	    {
3313 	      set_insn_error (0, _("missing `]'"));
3314 	      return 0;
3315 	    }
3316 	  ++s;
3317 	}
3318       return s;
3319     }
3320 
3321   if (float_format)
3322     {
3323       /* First try to treat expressions as floats.  */
3324       save_in = input_line_pointer;
3325       input_line_pointer = s;
3326       err = md_atof (float_format, (char *) token.u.flt.data,
3327 		     &token.u.flt.length);
3328       end = input_line_pointer;
3329       input_line_pointer = save_in;
3330       if (err && *err)
3331 	{
3332 	  set_insn_error (0, err);
3333 	  return 0;
3334 	}
3335       if (s != end)
3336 	{
3337 	  mips_add_token (&token, OT_FLOAT);
3338 	  return end;
3339 	}
3340     }
3341 
3342   /* Treat everything else as an integer expression.  */
3343   token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3344   token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3345   token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3346   my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3347   s = expr_end;
3348   mips_add_token (&token, OT_INTEGER);
3349   return s;
3350 }
3351 
3352 /* S points to the operand list for an instruction.  FLOAT_FORMAT is 'f'
3353    if expressions should be treated as 32-bit floating-point constants,
3354    'd' if they should be treated as 64-bit floating-point constants,
3355    or 0 if they should be treated as integer expressions (the usual case).
3356 
3357    Return a list of tokens on success, otherwise return 0.  The caller
3358    must obstack_free the list after use.  */
3359 
3360 static struct mips_operand_token *
mips_parse_arguments(char * s,char float_format)3361 mips_parse_arguments (char *s, char float_format)
3362 {
3363   struct mips_operand_token token;
3364 
3365   SKIP_SPACE_TABS (s);
3366   while (*s)
3367     {
3368       s = mips_parse_argument_token (s, float_format);
3369       if (!s)
3370 	{
3371 	  obstack_free (&mips_operand_tokens,
3372 			obstack_finish (&mips_operand_tokens));
3373 	  return 0;
3374 	}
3375       SKIP_SPACE_TABS (s);
3376     }
3377   mips_add_token (&token, OT_END);
3378   return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
3379 }
3380 
3381 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3382    and architecture.  Use is_opcode_valid_16 for MIPS16 opcodes.  */
3383 
3384 static bool
is_opcode_valid(const struct mips_opcode * mo)3385 is_opcode_valid (const struct mips_opcode *mo)
3386 {
3387   int isa = mips_opts.isa;
3388   int ase = mips_opts.ase;
3389   int fp_s, fp_d;
3390   unsigned int i;
3391 
3392   if (ISA_HAS_64BIT_REGS (isa))
3393     for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3394       if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3395 	ase |= mips_ases[i].flags64;
3396 
3397   if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
3398     return false;
3399 
3400   /* Check whether the instruction or macro requires single-precision or
3401      double-precision floating-point support.  Note that this information is
3402      stored differently in the opcode table for insns and macros.  */
3403   if (mo->pinfo == INSN_MACRO)
3404     {
3405       fp_s = mo->pinfo2 & INSN2_M_FP_S;
3406       fp_d = mo->pinfo2 & INSN2_M_FP_D;
3407     }
3408   else
3409     {
3410       fp_s = mo->pinfo & FP_S;
3411       fp_d = mo->pinfo & FP_D;
3412     }
3413 
3414   if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3415     return false;
3416 
3417   if (fp_s && mips_opts.soft_float)
3418     return false;
3419 
3420   return true;
3421 }
3422 
3423 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
3424    selected ISA and architecture.  */
3425 
3426 static bool
is_opcode_valid_16(const struct mips_opcode * mo)3427 is_opcode_valid_16 (const struct mips_opcode *mo)
3428 {
3429   int isa = mips_opts.isa;
3430   int ase = mips_opts.ase;
3431   unsigned int i;
3432 
3433   if (ISA_HAS_64BIT_REGS (isa))
3434     for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3435       if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3436 	ase |= mips_ases[i].flags64;
3437 
3438   return opcode_is_member (mo, isa, ase, mips_opts.arch);
3439 }
3440 
3441 /* Return TRUE if the size of the microMIPS opcode MO matches one
3442    explicitly requested.  Always TRUE in the standard MIPS mode.
3443    Use is_size_valid_16 for MIPS16 opcodes.  */
3444 
3445 static bool
is_size_valid(const struct mips_opcode * mo)3446 is_size_valid (const struct mips_opcode *mo)
3447 {
3448   if (!mips_opts.micromips)
3449     return true;
3450 
3451   if (mips_opts.insn32)
3452     {
3453       if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3454 	return false;
3455       if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3456 	return false;
3457     }
3458   if (!forced_insn_length)
3459     return true;
3460   if (mo->pinfo == INSN_MACRO)
3461     return false;
3462   return forced_insn_length == micromips_insn_length (mo);
3463 }
3464 
3465 /* Return TRUE if the size of the MIPS16 opcode MO matches one
3466    explicitly requested.  */
3467 
3468 static bool
is_size_valid_16(const struct mips_opcode * mo)3469 is_size_valid_16 (const struct mips_opcode *mo)
3470 {
3471   if (!forced_insn_length)
3472     return true;
3473   if (mo->pinfo == INSN_MACRO)
3474     return false;
3475   if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3476     return false;
3477   if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3478     return false;
3479   return true;
3480 }
3481 
3482 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
3483    of the preceding instruction.  Always TRUE in the standard MIPS mode.
3484 
3485    We don't accept macros in 16-bit delay slots to avoid a case where
3486    a macro expansion fails because it relies on a preceding 32-bit real
3487    instruction to have matched and does not handle the operands correctly.
3488    The only macros that may expand to 16-bit instructions are JAL that
3489    cannot be placed in a delay slot anyway, and corner cases of BALIGN
3490    and BGT (that likewise cannot be placed in a delay slot) that decay to
3491    a NOP.  In all these cases the macros precede any corresponding real
3492    instruction definitions in the opcode table, so they will match in the
3493    second pass where the size of the delay slot is ignored and therefore
3494    produce correct code.  */
3495 
3496 static bool
is_delay_slot_valid(const struct mips_opcode * mo)3497 is_delay_slot_valid (const struct mips_opcode *mo)
3498 {
3499   if (!mips_opts.micromips)
3500     return true;
3501 
3502   if (mo->pinfo == INSN_MACRO)
3503     return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
3504   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3505       && micromips_insn_length (mo) != 4)
3506     return false;
3507   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3508       && micromips_insn_length (mo) != 2)
3509     return false;
3510 
3511   return true;
3512 }
3513 
3514 /* For consistency checking, verify that all bits of OPCODE are specified
3515    either by the match/mask part of the instruction definition, or by the
3516    operand list.  Also build up a list of operands in OPERANDS.
3517 
3518    INSN_BITS says which bits of the instruction are significant.
3519    If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3520    provides the mips_operand description of each operand.  DECODE_OPERAND
3521    is null for MIPS16 instructions.  */
3522 
3523 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)3524 validate_mips_insn (const struct mips_opcode *opcode,
3525 		    unsigned long insn_bits,
3526 		    const struct mips_operand *(*decode_operand) (const char *),
3527 		    struct mips_operand_array *operands)
3528 {
3529   const char *s;
3530   unsigned long used_bits, doubled, undefined, opno, mask;
3531   const struct mips_operand *operand;
3532 
3533   mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3534   if ((mask & opcode->match) != opcode->match)
3535     {
3536       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3537 	      opcode->name, opcode->args);
3538       return 0;
3539     }
3540   used_bits = 0;
3541   opno = 0;
3542   if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3543     used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
3544   for (s = opcode->args; *s; ++s)
3545     switch (*s)
3546       {
3547       case ',':
3548       case '(':
3549       case ')':
3550 	break;
3551 
3552       case '#':
3553 	s++;
3554 	break;
3555 
3556       default:
3557 	if (!decode_operand)
3558 	  operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
3559 	else
3560 	  operand = decode_operand (s);
3561 	if (!operand && opcode->pinfo != INSN_MACRO)
3562 	  {
3563 	    as_bad (_("internal: unknown operand type: %s %s"),
3564 		    opcode->name, opcode->args);
3565 	    return 0;
3566 	  }
3567 	gas_assert (opno < MAX_OPERANDS);
3568 	operands->operand[opno] = operand;
3569 	if (!decode_operand && operand
3570 	    && operand->type == OP_INT && operand->lsb == 0
3571 	    && mips_opcode_32bit_p (opcode))
3572 	  used_bits |= mips16_immed_extend (-1, operand->size);
3573 	else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
3574 	  {
3575 	    used_bits = mips_insert_operand (operand, used_bits, -1);
3576 	    if (operand->type == OP_MDMX_IMM_REG)
3577 	      /* Bit 5 is the format selector (OB vs QH).  The opcode table
3578 		 has separate entries for each format.  */
3579 	      used_bits &= ~(1 << (operand->lsb + 5));
3580 	    if (operand->type == OP_ENTRY_EXIT_LIST)
3581 	      used_bits &= ~(mask & 0x700);
3582 	    /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3583 	       operand field that cannot be fully described with LSB/SIZE.  */
3584 	    if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3585 	      used_bits &= ~0x6000;
3586 	  }
3587 	/* Skip prefix characters.  */
3588 	if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
3589 	  ++s;
3590 	opno += 1;
3591 	break;
3592       }
3593   doubled = used_bits & mask & insn_bits;
3594   if (doubled)
3595     {
3596       as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3597 		" %s %s"), doubled, opcode->name, opcode->args);
3598       return 0;
3599     }
3600   used_bits |= mask;
3601   undefined = ~used_bits & insn_bits;
3602   if (opcode->pinfo != INSN_MACRO && undefined)
3603     {
3604       as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3605 	      undefined, opcode->name, opcode->args);
3606       return 0;
3607     }
3608   used_bits &= ~insn_bits;
3609   if (used_bits)
3610     {
3611       as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3612 	      used_bits, opcode->name, opcode->args);
3613       return 0;
3614     }
3615   return 1;
3616 }
3617 
3618 /* The MIPS16 version of validate_mips_insn.  */
3619 
3620 static int
validate_mips16_insn(const struct mips_opcode * opcode,struct mips_operand_array * operands)3621 validate_mips16_insn (const struct mips_opcode *opcode,
3622 		      struct mips_operand_array *operands)
3623 {
3624   unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
3625 
3626   return validate_mips_insn (opcode, insn_bits, 0, operands);
3627 }
3628 
3629 /* The microMIPS version of validate_mips_insn.  */
3630 
3631 static int
validate_micromips_insn(const struct mips_opcode * opc,struct mips_operand_array * operands)3632 validate_micromips_insn (const struct mips_opcode *opc,
3633 			 struct mips_operand_array *operands)
3634 {
3635   unsigned long insn_bits;
3636   unsigned long major;
3637   unsigned int length;
3638 
3639   if (opc->pinfo == INSN_MACRO)
3640     return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3641 			       operands);
3642 
3643   length = micromips_insn_length (opc);
3644   if (length != 2 && length != 4)
3645     {
3646       as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
3647 		"%s %s"), length, opc->name, opc->args);
3648       return 0;
3649     }
3650   major = opc->match >> (10 + 8 * (length - 2));
3651   if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3652       || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3653     {
3654       as_bad (_("internal error: bad microMIPS opcode "
3655 		"(opcode/length mismatch): %s %s"), opc->name, opc->args);
3656       return 0;
3657     }
3658 
3659   /* Shift piecewise to avoid an overflow where unsigned long is 32-bit.  */
3660   insn_bits = 1 << 4 * length;
3661   insn_bits <<= 4 * length;
3662   insn_bits -= 1;
3663   return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3664 			     operands);
3665 }
3666 
3667 /* This function is called once, at assembler startup time.  It should set up
3668    all the tables, etc. that the MD part of the assembler will need.  */
3669 
3670 void
md_begin(void)3671 md_begin (void)
3672 {
3673   int i = 0;
3674   int broken = 0;
3675 
3676   if (mips_pic != NO_PIC)
3677     {
3678       if (g_switch_seen && g_switch_value != 0)
3679 	as_bad (_("-G may not be used in position-independent code"));
3680       g_switch_value = 0;
3681     }
3682   else if (mips_abicalls)
3683     {
3684       if (g_switch_seen && g_switch_value != 0)
3685 	as_bad (_("-G may not be used with abicalls"));
3686       g_switch_value = 0;
3687     }
3688 
3689   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3690     as_warn (_("could not set architecture and machine"));
3691 
3692   op_hash = str_htab_create ();
3693 
3694   mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
3695   for (i = 0; i < NUMOPCODES;)
3696     {
3697       const char *name = mips_opcodes[i].name;
3698 
3699       if (str_hash_insert (op_hash, name, &mips_opcodes[i], 0) != NULL)
3700 	as_fatal (_("duplicate %s"), name);
3701       do
3702 	{
3703 	  if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3704 				   decode_mips_operand, &mips_operands[i]))
3705 	    broken = 1;
3706 
3707 	  if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3708 	    {
3709 	      create_insn (&nop_insn, mips_opcodes + i);
3710 	      if (mips_fix_loongson2f_nop)
3711 		nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3712 	      nop_insn.fixed_p = 1;
3713 	    }
3714 
3715           if (sync_insn.insn_mo == NULL && strcmp (name, "sync") == 0)
3716 	    create_insn (&sync_insn, mips_opcodes + i);
3717 
3718 	  ++i;
3719 	}
3720       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3721     }
3722 
3723   mips16_op_hash = str_htab_create ();
3724   mips16_operands = XCNEWVEC (struct mips_operand_array,
3725 			      bfd_mips16_num_opcodes);
3726 
3727   i = 0;
3728   while (i < bfd_mips16_num_opcodes)
3729     {
3730       const char *name = mips16_opcodes[i].name;
3731 
3732       if (str_hash_insert (mips16_op_hash, name, &mips16_opcodes[i], 0))
3733 	as_fatal (_("duplicate %s"), name);
3734       do
3735 	{
3736 	  if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3737 	    broken = 1;
3738 	  if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3739 	    {
3740 	      create_insn (&mips16_nop_insn, mips16_opcodes + i);
3741 	      mips16_nop_insn.fixed_p = 1;
3742 	    }
3743 	  ++i;
3744 	}
3745       while (i < bfd_mips16_num_opcodes
3746 	     && strcmp (mips16_opcodes[i].name, name) == 0);
3747     }
3748 
3749   micromips_op_hash = str_htab_create ();
3750   micromips_operands = XCNEWVEC (struct mips_operand_array,
3751 				 bfd_micromips_num_opcodes);
3752 
3753   i = 0;
3754   while (i < bfd_micromips_num_opcodes)
3755     {
3756       const char *name = micromips_opcodes[i].name;
3757 
3758       if (str_hash_insert (micromips_op_hash, name, &micromips_opcodes[i], 0))
3759 	as_fatal (_("duplicate %s"), name);
3760       do
3761 	{
3762 	  struct mips_cl_insn *micromips_nop_insn;
3763 
3764 	  if (!validate_micromips_insn (&micromips_opcodes[i],
3765 					&micromips_operands[i]))
3766 	    broken = 1;
3767 
3768 	  if (micromips_opcodes[i].pinfo != INSN_MACRO)
3769 	    {
3770 	      if (micromips_insn_length (micromips_opcodes + i) == 2)
3771 		micromips_nop_insn = &micromips_nop16_insn;
3772 	      else if (micromips_insn_length (micromips_opcodes + i) == 4)
3773 		micromips_nop_insn = &micromips_nop32_insn;
3774 	      else
3775 		continue;
3776 
3777 	      if (micromips_nop_insn->insn_mo == NULL
3778 		  && strcmp (name, "nop") == 0)
3779 		{
3780 		  create_insn (micromips_nop_insn, micromips_opcodes + i);
3781 		  micromips_nop_insn->fixed_p = 1;
3782 		}
3783 	    }
3784 	}
3785       while (++i < bfd_micromips_num_opcodes
3786 	     && strcmp (micromips_opcodes[i].name, name) == 0);
3787     }
3788 
3789   if (broken)
3790     as_fatal (_("broken assembler, no assembly attempted"));
3791 
3792   /* We add all the general register names to the symbol table.  This
3793      helps us detect invalid uses of them.  */
3794   for (i = 0; reg_names[i].name; i++)
3795     symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
3796 				     &zero_address_frag,
3797 				     reg_names[i].num));
3798   if (HAVE_NEWABI)
3799     for (i = 0; reg_names_n32n64[i].name; i++)
3800       symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
3801 				       &zero_address_frag,
3802 				       reg_names_n32n64[i].num));
3803   else
3804     for (i = 0; reg_names_o32[i].name; i++)
3805       symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
3806 				       &zero_address_frag,
3807 				       reg_names_o32[i].num));
3808 
3809   for (i = 0; i < 32; i++)
3810     {
3811       char regname[16];
3812 
3813       /* R5900 VU0 floating-point register.  */
3814       sprintf (regname, "$vf%d", i);
3815       symbol_table_insert (symbol_new (regname, reg_section,
3816 				       &zero_address_frag, RTYPE_VF | i));
3817 
3818       /* R5900 VU0 integer register.  */
3819       sprintf (regname, "$vi%d", i);
3820       symbol_table_insert (symbol_new (regname, reg_section,
3821 				       &zero_address_frag, RTYPE_VI | i));
3822 
3823       /* MSA register.  */
3824       sprintf (regname, "$w%d", i);
3825       symbol_table_insert (symbol_new (regname, reg_section,
3826 				       &zero_address_frag, RTYPE_MSA | i));
3827     }
3828 
3829   obstack_init (&mips_operand_tokens);
3830 
3831   mips_no_prev_insn ();
3832 
3833   mips_gprmask = 0;
3834   mips_cprmask[0] = 0;
3835   mips_cprmask[1] = 0;
3836   mips_cprmask[2] = 0;
3837   mips_cprmask[3] = 0;
3838 
3839   /* set the default alignment for the text section (2**2) */
3840   record_alignment (text_section, 2);
3841 
3842   bfd_set_gp_size (stdoutput, g_switch_value);
3843 
3844   /* On a native system other than VxWorks, sections must be aligned
3845      to 16 byte boundaries.  When configured for an embedded ELF
3846      target, we don't bother.  */
3847   if (!startswith (TARGET_OS, "elf")
3848       && !startswith (TARGET_OS, "vxworks"))
3849     {
3850       bfd_set_section_alignment (text_section, 4);
3851       bfd_set_section_alignment (data_section, 4);
3852       bfd_set_section_alignment (bss_section, 4);
3853     }
3854 
3855   /* Create a .reginfo section for register masks and a .mdebug
3856      section for debugging information.  */
3857   {
3858     segT seg;
3859     subsegT subseg;
3860     flagword flags;
3861     segT sec;
3862 
3863     seg = now_seg;
3864     subseg = now_subseg;
3865 
3866     /* The ABI says this section should be loaded so that the
3867        running program can access it.  However, we don't load it
3868        if we are configured for an embedded target.  */
3869     flags = SEC_READONLY | SEC_DATA;
3870     if (!startswith (TARGET_OS, "elf"))
3871       flags |= SEC_ALLOC | SEC_LOAD;
3872 
3873     if (mips_abi != N64_ABI)
3874       {
3875 	sec = subseg_new (".reginfo", (subsegT) 0);
3876 
3877 	bfd_set_section_flags (sec, flags);
3878 	bfd_set_section_alignment (sec, HAVE_NEWABI ? 3 : 2);
3879 
3880 	mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3881       }
3882     else
3883       {
3884 	/* The 64-bit ABI uses a .MIPS.options section rather than
3885 	   .reginfo section.  */
3886 	sec = subseg_new (".MIPS.options", (subsegT) 0);
3887 	bfd_set_section_flags (sec, flags);
3888 	bfd_set_section_alignment (sec, 3);
3889 
3890 	/* Set up the option header.  */
3891 	{
3892 	  Elf_Internal_Options opthdr;
3893 	  char *f;
3894 
3895 	  opthdr.kind = ODK_REGINFO;
3896 	  opthdr.size = (sizeof (Elf_External_Options)
3897 			 + sizeof (Elf64_External_RegInfo));
3898 	  opthdr.section = 0;
3899 	  opthdr.info = 0;
3900 	  f = frag_more (sizeof (Elf_External_Options));
3901 	  bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3902 					 (Elf_External_Options *) f);
3903 
3904 	  mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3905 	}
3906       }
3907 
3908     sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3909     bfd_set_section_flags (sec,
3910 			   SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3911     bfd_set_section_alignment (sec, 3);
3912     mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3913 
3914     if (ECOFF_DEBUGGING)
3915       {
3916 	sec = subseg_new (".mdebug", (subsegT) 0);
3917 	bfd_set_section_flags (sec, SEC_HAS_CONTENTS | SEC_READONLY);
3918 	bfd_set_section_alignment (sec, 2);
3919       }
3920     else if (mips_flag_pdr)
3921       {
3922 	pdr_seg = subseg_new (".pdr", (subsegT) 0);
3923 	bfd_set_section_flags (pdr_seg,
3924 			       SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
3925 	bfd_set_section_alignment (pdr_seg, 2);
3926       }
3927 
3928     subseg_set (seg, subseg);
3929   }
3930 
3931   if (mips_fix_vr4120)
3932     init_vr4120_conflicts ();
3933 }
3934 
3935 static inline void
fpabi_incompatible_with(int fpabi,const char * what)3936 fpabi_incompatible_with (int fpabi, const char *what)
3937 {
3938   as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3939 	   Tag_GNU_MIPS_ABI_FP, fpabi, what);
3940 }
3941 
3942 static inline void
fpabi_requires(int fpabi,const char * what)3943 fpabi_requires (int fpabi, const char *what)
3944 {
3945   as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3946 	   Tag_GNU_MIPS_ABI_FP, fpabi, what);
3947 }
3948 
3949 /* Check -mabi and register sizes against the specified FP ABI.  */
3950 static void
check_fpabi(int fpabi)3951 check_fpabi (int fpabi)
3952 {
3953   switch (fpabi)
3954     {
3955     case Val_GNU_MIPS_ABI_FP_DOUBLE:
3956       if (file_mips_opts.soft_float)
3957 	fpabi_incompatible_with (fpabi, "softfloat");
3958       else if (file_mips_opts.single_float)
3959 	fpabi_incompatible_with (fpabi, "singlefloat");
3960       if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3961 	fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3962       else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3963 	fpabi_incompatible_with (fpabi, "gp=32 fp=64");
3964       break;
3965 
3966     case Val_GNU_MIPS_ABI_FP_XX:
3967       if (mips_abi != O32_ABI)
3968 	fpabi_requires (fpabi, "-mabi=32");
3969       else if (file_mips_opts.soft_float)
3970 	fpabi_incompatible_with (fpabi, "softfloat");
3971       else if (file_mips_opts.single_float)
3972 	fpabi_incompatible_with (fpabi, "singlefloat");
3973       else if (file_mips_opts.fp != 0)
3974 	fpabi_requires (fpabi, "fp=xx");
3975       break;
3976 
3977     case Val_GNU_MIPS_ABI_FP_64A:
3978     case Val_GNU_MIPS_ABI_FP_64:
3979       if (mips_abi != O32_ABI)
3980 	fpabi_requires (fpabi, "-mabi=32");
3981       else if (file_mips_opts.soft_float)
3982 	fpabi_incompatible_with (fpabi, "softfloat");
3983       else if (file_mips_opts.single_float)
3984 	fpabi_incompatible_with (fpabi, "singlefloat");
3985       else if (file_mips_opts.fp != 64)
3986 	fpabi_requires (fpabi, "fp=64");
3987       else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3988 	fpabi_incompatible_with (fpabi, "nooddspreg");
3989       else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3990 	fpabi_requires (fpabi, "nooddspreg");
3991       break;
3992 
3993     case Val_GNU_MIPS_ABI_FP_SINGLE:
3994       if (file_mips_opts.soft_float)
3995 	fpabi_incompatible_with (fpabi, "softfloat");
3996       else if (!file_mips_opts.single_float)
3997 	fpabi_requires (fpabi, "singlefloat");
3998       break;
3999 
4000     case Val_GNU_MIPS_ABI_FP_SOFT:
4001       if (!file_mips_opts.soft_float)
4002 	fpabi_requires (fpabi, "softfloat");
4003       break;
4004 
4005     case Val_GNU_MIPS_ABI_FP_OLD_64:
4006       as_warn (_(".gnu_attribute %d,%d is no longer supported"),
4007 	       Tag_GNU_MIPS_ABI_FP, fpabi);
4008       break;
4009 
4010     case Val_GNU_MIPS_ABI_FP_NAN2008:
4011       /* Silently ignore compatibility value.  */
4012       break;
4013 
4014     default:
4015       as_warn (_(".gnu_attribute %d,%d is not a recognized"
4016 	         " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
4017       break;
4018     }
4019 }
4020 
4021 /* Perform consistency checks on the current options.  */
4022 
4023 static void
mips_check_options(struct mips_set_options * opts,bool abi_checks)4024 mips_check_options (struct mips_set_options *opts, bool abi_checks)
4025 {
4026   /* Check the size of integer registers agrees with the ABI and ISA.  */
4027   if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
4028     as_bad (_("`gp=64' used with a 32-bit processor"));
4029   else if (abi_checks
4030 	   && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
4031     as_bad (_("`gp=32' used with a 64-bit ABI"));
4032   else if (abi_checks
4033 	   && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
4034     as_bad (_("`gp=64' used with a 32-bit ABI"));
4035 
4036   /* Check the size of the float registers agrees with the ABI and ISA.  */
4037   switch (opts->fp)
4038     {
4039     case 0:
4040       if (!CPU_HAS_LDC1_SDC1 (opts->arch))
4041 	as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
4042       else if (opts->single_float == 1)
4043 	as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
4044       break;
4045     case 64:
4046       if (!ISA_HAS_64BIT_FPRS (opts->isa))
4047 	as_bad (_("`fp=64' used with a 32-bit fpu"));
4048       else if (abi_checks
4049 	       && ABI_NEEDS_32BIT_REGS (mips_abi)
4050 	       && !ISA_HAS_MXHC1 (opts->isa))
4051 	as_warn (_("`fp=64' used with a 32-bit ABI"));
4052       break;
4053     case 32:
4054       if (abi_checks
4055 	  && ABI_NEEDS_64BIT_REGS (mips_abi))
4056 	as_warn (_("`fp=32' used with a 64-bit ABI"));
4057       if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
4058 	as_bad (_("`fp=32' used with a MIPS R6 cpu"));
4059       break;
4060     default:
4061       as_bad (_("Unknown size of floating point registers"));
4062       break;
4063     }
4064 
4065   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
4066     as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4067 
4068   if (opts->micromips == 1 && opts->mips16 == 1)
4069     as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
4070   else if (ISA_IS_R6 (opts->isa)
4071 	   && (opts->micromips == 1
4072 	       || opts->mips16 == 1))
4073     as_fatal (_("`%s' cannot be used with `%s'"),
4074 	      opts->micromips ? "micromips" : "mips16",
4075 	      mips_cpu_info_from_isa (opts->isa)->name);
4076 
4077   if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4078     as_fatal (_("branch relaxation is not supported in `%s'"),
4079 	      mips_cpu_info_from_isa (opts->isa)->name);
4080 }
4081 
4082 /* Perform consistency checks on the module level options exactly once.
4083    This is a deferred check that happens:
4084      at the first .set directive
4085      or, at the first pseudo op that generates code (inc .dc.a)
4086      or, at the first instruction
4087      or, at the end.  */
4088 
4089 static void
file_mips_check_options(void)4090 file_mips_check_options (void)
4091 {
4092   if (file_mips_opts_checked)
4093     return;
4094 
4095   /* The following code determines the register size.
4096      Similar code was added to GCC 3.3 (see override_options() in
4097      config/mips/mips.c).  The GAS and GCC code should be kept in sync
4098      as much as possible.  */
4099 
4100   if (file_mips_opts.gp < 0)
4101     {
4102       /* Infer the integer register size from the ABI and processor.
4103 	 Restrict ourselves to 32-bit registers if that's all the
4104 	 processor has, or if the ABI cannot handle 64-bit registers.  */
4105       file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4106 			   || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4107 			  ? 32 : 64;
4108     }
4109 
4110   if (file_mips_opts.fp < 0)
4111     {
4112       /* No user specified float register size.
4113 	 ??? GAS treats single-float processors as though they had 64-bit
4114 	 float registers (although it complains when double-precision
4115 	 instructions are used).  As things stand, saying they have 32-bit
4116 	 registers would lead to spurious "register must be even" messages.
4117 	 So here we assume float registers are never smaller than the
4118 	 integer ones.  */
4119       if (file_mips_opts.gp == 64)
4120 	/* 64-bit integer registers implies 64-bit float registers.  */
4121 	file_mips_opts.fp = 64;
4122       else if ((file_mips_opts.ase & FP64_ASES)
4123 	       && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4124 	/* Handle ASEs that require 64-bit float registers, if possible.  */
4125 	file_mips_opts.fp = 64;
4126       else if (ISA_IS_R6 (mips_opts.isa))
4127 	/* R6 implies 64-bit float registers.  */
4128 	file_mips_opts.fp = 64;
4129       else
4130 	/* 32-bit float registers.  */
4131 	file_mips_opts.fp = 32;
4132     }
4133 
4134   /* Disable operations on odd-numbered floating-point registers by default
4135      when using the FPXX ABI.  */
4136   if (file_mips_opts.oddspreg < 0)
4137     {
4138       if (file_mips_opts.fp == 0)
4139 	file_mips_opts.oddspreg = 0;
4140       else
4141 	file_mips_opts.oddspreg = 1;
4142     }
4143 
4144   /* End of GCC-shared inference code.  */
4145 
4146   /* This flag is set when we have a 64-bit capable CPU but use only
4147      32-bit wide registers.  Note that EABI does not use it.  */
4148   if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4149       && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4150 	  || mips_abi == O32_ABI))
4151     mips_32bitmode = 1;
4152 
4153   if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4154     as_bad (_("trap exception not supported at ISA 1"));
4155 
4156   /* If the selected architecture includes support for ASEs, enable
4157      generation of code for them.  */
4158   if (file_mips_opts.mips16 == -1)
4159     file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4160   if (file_mips_opts.micromips == -1)
4161     file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4162 				? 1 : 0;
4163 
4164   if (mips_nan2008 == -1)
4165     mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4166   else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4167     as_fatal (_("`%s' does not support legacy NaN"),
4168 	      mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4169 
4170   /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4171      being selected implicitly.  */
4172   if (file_mips_opts.fp != 64)
4173     file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4174 
4175   /* If the user didn't explicitly select or deselect a particular ASE,
4176      use the default setting for the CPU.  */
4177   file_mips_opts.ase |= (file_mips_opts.init_ase & ~file_ase_explicit);
4178 
4179   /* Set up the current options.  These may change throughout assembly.  */
4180   mips_opts = file_mips_opts;
4181 
4182   mips_check_isa_supports_ases ();
4183   mips_check_options (&file_mips_opts, true);
4184   file_mips_opts_checked = true;
4185 
4186   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4187     as_warn (_("could not set architecture and machine"));
4188 }
4189 
4190 void
md_assemble(char * str)4191 md_assemble (char *str)
4192 {
4193   struct mips_cl_insn insn;
4194   bfd_reloc_code_real_type unused_reloc[3]
4195     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
4196 
4197   file_mips_check_options ();
4198 
4199   imm_expr.X_op = O_absent;
4200   offset_expr.X_op = O_absent;
4201   offset_reloc[0] = BFD_RELOC_UNUSED;
4202   offset_reloc[1] = BFD_RELOC_UNUSED;
4203   offset_reloc[2] = BFD_RELOC_UNUSED;
4204 
4205   mips_mark_labels ();
4206   mips_assembling_insn = true;
4207   clear_insn_error ();
4208 
4209   if (mips_opts.mips16)
4210     mips16_ip (str, &insn);
4211   else
4212     {
4213       mips_ip (str, &insn);
4214       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4215 	    str, insn.insn_opcode));
4216     }
4217 
4218   if (insn_error.msg)
4219     report_insn_error (str);
4220   else if (insn.insn_mo->pinfo == INSN_MACRO)
4221     {
4222       macro_start ();
4223       if (mips_opts.mips16)
4224 	mips16_macro (&insn);
4225       else
4226 	macro (&insn, str);
4227       macro_end ();
4228     }
4229   else
4230     {
4231       if (offset_expr.X_op != O_absent)
4232 	append_insn (&insn, &offset_expr, offset_reloc, false);
4233       else
4234 	append_insn (&insn, NULL, unused_reloc, false);
4235     }
4236 
4237   mips_assembling_insn = false;
4238 }
4239 
4240 /* Convenience functions for abstracting away the differences between
4241    MIPS16 and non-MIPS16 relocations.  */
4242 
4243 static inline bool
mips16_reloc_p(bfd_reloc_code_real_type reloc)4244 mips16_reloc_p (bfd_reloc_code_real_type reloc)
4245 {
4246   switch (reloc)
4247     {
4248     case BFD_RELOC_MIPS16_JMP:
4249     case BFD_RELOC_MIPS16_GPREL:
4250     case BFD_RELOC_MIPS16_GOT16:
4251     case BFD_RELOC_MIPS16_CALL16:
4252     case BFD_RELOC_MIPS16_HI16_S:
4253     case BFD_RELOC_MIPS16_HI16:
4254     case BFD_RELOC_MIPS16_LO16:
4255     case BFD_RELOC_MIPS16_16_PCREL_S1:
4256       return true;
4257 
4258     default:
4259       return false;
4260     }
4261 }
4262 
4263 static inline bool
micromips_reloc_p(bfd_reloc_code_real_type reloc)4264 micromips_reloc_p (bfd_reloc_code_real_type reloc)
4265 {
4266   switch (reloc)
4267     {
4268     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4269     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4270     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4271     case BFD_RELOC_MICROMIPS_GPREL16:
4272     case BFD_RELOC_MICROMIPS_JMP:
4273     case BFD_RELOC_MICROMIPS_HI16:
4274     case BFD_RELOC_MICROMIPS_HI16_S:
4275     case BFD_RELOC_MICROMIPS_LO16:
4276     case BFD_RELOC_MICROMIPS_LITERAL:
4277     case BFD_RELOC_MICROMIPS_GOT16:
4278     case BFD_RELOC_MICROMIPS_CALL16:
4279     case BFD_RELOC_MICROMIPS_GOT_HI16:
4280     case BFD_RELOC_MICROMIPS_GOT_LO16:
4281     case BFD_RELOC_MICROMIPS_CALL_HI16:
4282     case BFD_RELOC_MICROMIPS_CALL_LO16:
4283     case BFD_RELOC_MICROMIPS_SUB:
4284     case BFD_RELOC_MICROMIPS_GOT_PAGE:
4285     case BFD_RELOC_MICROMIPS_GOT_OFST:
4286     case BFD_RELOC_MICROMIPS_GOT_DISP:
4287     case BFD_RELOC_MICROMIPS_HIGHEST:
4288     case BFD_RELOC_MICROMIPS_HIGHER:
4289     case BFD_RELOC_MICROMIPS_SCN_DISP:
4290     case BFD_RELOC_MICROMIPS_JALR:
4291       return true;
4292 
4293     default:
4294       return false;
4295     }
4296 }
4297 
4298 static inline bool
jmp_reloc_p(bfd_reloc_code_real_type reloc)4299 jmp_reloc_p (bfd_reloc_code_real_type reloc)
4300 {
4301   return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4302 }
4303 
4304 static inline bool
b_reloc_p(bfd_reloc_code_real_type reloc)4305 b_reloc_p (bfd_reloc_code_real_type reloc)
4306 {
4307   return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4308 	  || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4309 	  || reloc == BFD_RELOC_16_PCREL_S2
4310 	  || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
4311 	  || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4312 	  || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4313 	  || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4314 }
4315 
4316 static inline bool
got16_reloc_p(bfd_reloc_code_real_type reloc)4317 got16_reloc_p (bfd_reloc_code_real_type reloc)
4318 {
4319   return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
4320 	  || reloc == BFD_RELOC_MICROMIPS_GOT16);
4321 }
4322 
4323 static inline bool
hi16_reloc_p(bfd_reloc_code_real_type reloc)4324 hi16_reloc_p (bfd_reloc_code_real_type reloc)
4325 {
4326   return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
4327 	  || reloc == BFD_RELOC_MICROMIPS_HI16_S);
4328 }
4329 
4330 static inline bool
lo16_reloc_p(bfd_reloc_code_real_type reloc)4331 lo16_reloc_p (bfd_reloc_code_real_type reloc)
4332 {
4333   return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
4334 	  || reloc == BFD_RELOC_MICROMIPS_LO16);
4335 }
4336 
4337 static inline bool
jalr_reloc_p(bfd_reloc_code_real_type reloc)4338 jalr_reloc_p (bfd_reloc_code_real_type reloc)
4339 {
4340   return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
4341 }
4342 
4343 static inline bool
gprel16_reloc_p(bfd_reloc_code_real_type reloc)4344 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4345 {
4346   return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4347 	  || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4348 }
4349 
4350 /* Return true if RELOC is a PC-relative relocation that does not have
4351    full address range.  */
4352 
4353 static inline bool
limited_pcrel_reloc_p(bfd_reloc_code_real_type reloc)4354 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4355 {
4356   switch (reloc)
4357     {
4358     case BFD_RELOC_16_PCREL_S2:
4359     case BFD_RELOC_MIPS16_16_PCREL_S1:
4360     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4361     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4362     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4363     case BFD_RELOC_MIPS_21_PCREL_S2:
4364     case BFD_RELOC_MIPS_26_PCREL_S2:
4365     case BFD_RELOC_MIPS_18_PCREL_S3:
4366     case BFD_RELOC_MIPS_19_PCREL_S2:
4367       return true;
4368 
4369     case BFD_RELOC_32_PCREL:
4370     case BFD_RELOC_HI16_S_PCREL:
4371     case BFD_RELOC_LO16_PCREL:
4372       return HAVE_64BIT_ADDRESSES;
4373 
4374     default:
4375       return false;
4376     }
4377 }
4378 
4379 /* Return true if the given relocation might need a matching %lo().
4380    This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4381    need a matching %lo() when applied to local symbols.  */
4382 
4383 static inline bool
reloc_needs_lo_p(bfd_reloc_code_real_type reloc)4384 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
4385 {
4386   return (HAVE_IN_PLACE_ADDENDS
4387 	  && (hi16_reloc_p (reloc)
4388 	      /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4389 		 all GOT16 relocations evaluate to "G".  */
4390 	      || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4391 }
4392 
4393 /* Return the type of %lo() reloc needed by RELOC, given that
4394    reloc_needs_lo_p.  */
4395 
4396 static inline bfd_reloc_code_real_type
matching_lo_reloc(bfd_reloc_code_real_type reloc)4397 matching_lo_reloc (bfd_reloc_code_real_type reloc)
4398 {
4399   return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4400 	  : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4401 	     : BFD_RELOC_LO16));
4402 }
4403 
4404 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
4405    relocation.  */
4406 
4407 static inline bool
fixup_has_matching_lo_p(fixS * fixp)4408 fixup_has_matching_lo_p (fixS *fixp)
4409 {
4410   return (fixp->fx_next != NULL
4411 	  && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
4412 	  && fixp->fx_addsy == fixp->fx_next->fx_addsy
4413 	  && fixp->fx_offset == fixp->fx_next->fx_offset);
4414 }
4415 
4416 /* Move all labels in LABELS to the current insertion point.  TEXT_P
4417    says whether the labels refer to text or data.  */
4418 
4419 static void
mips_move_labels(struct insn_label_list * labels,bool text_p)4420 mips_move_labels (struct insn_label_list *labels, bool text_p)
4421 {
4422   struct insn_label_list *l;
4423   valueT val;
4424 
4425   for (l = labels; l != NULL; l = l->next)
4426     {
4427       gas_assert (S_GET_SEGMENT (l->label) == now_seg);
4428       symbol_set_frag (l->label, frag_now);
4429       val = (valueT) frag_now_fix ();
4430       /* MIPS16/microMIPS text labels are stored as odd.
4431 	 We just carry the ISA mode bit forward.  */
4432       if (text_p && HAVE_CODE_COMPRESSION)
4433 	val |= (S_GET_VALUE (l->label) & 0x1);
4434       S_SET_VALUE (l->label, val);
4435     }
4436 }
4437 
4438 /* Move all labels in insn_labels to the current insertion point
4439    and treat them as text labels.  */
4440 
4441 static void
mips_move_text_labels(void)4442 mips_move_text_labels (void)
4443 {
4444   mips_move_labels (seg_info (now_seg)->label_list, true);
4445 }
4446 
4447 /* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'.  */
4448 
4449 static bool
s_is_linkonce(symbolS * sym,segT from_seg)4450 s_is_linkonce (symbolS *sym, segT from_seg)
4451 {
4452   bool linkonce = false;
4453   segT symseg = S_GET_SEGMENT (sym);
4454 
4455   if (symseg != from_seg && !S_IS_LOCAL (sym))
4456     {
4457       if ((bfd_section_flags (symseg) & SEC_LINK_ONCE))
4458 	linkonce = true;
4459       /* The GNU toolchain uses an extension for ELF: a section
4460 	 beginning with the magic string .gnu.linkonce is a
4461 	 linkonce section.  */
4462       if (startswith (segment_name (symseg), ".gnu.linkonce"))
4463 	linkonce = true;
4464     }
4465   return linkonce;
4466 }
4467 
4468 /* Mark MIPS16 or microMIPS instruction label LABEL.  This permits the
4469    linker to handle them specially, such as generating jalx instructions
4470    when needed.  We also make them odd for the duration of the assembly,
4471    in order to generate the right sort of code.  We will make them even
4472    in the adjust_symtab routine, while leaving them marked.  This is
4473    convenient for the debugger and the disassembler.  The linker knows
4474    to make them odd again.  */
4475 
4476 static void
mips_compressed_mark_label(symbolS * label)4477 mips_compressed_mark_label (symbolS *label)
4478 {
4479   gas_assert (HAVE_CODE_COMPRESSION);
4480 
4481   if (mips_opts.mips16)
4482     S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4483   else
4484     S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
4485   if ((S_GET_VALUE (label) & 1) == 0
4486       /* Don't adjust the address if the label is global or weak, or
4487 	 in a link-once section, since we'll be emitting symbol reloc
4488 	 references to it which will be patched up by the linker, and
4489 	 the final value of the symbol may or may not be MIPS16/microMIPS.  */
4490       && !S_IS_WEAK (label)
4491       && !S_IS_EXTERNAL (label)
4492       && !s_is_linkonce (label, now_seg))
4493     S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4494 }
4495 
4496 /* Mark preceding MIPS16 or microMIPS instruction labels.  */
4497 
4498 static void
mips_compressed_mark_labels(void)4499 mips_compressed_mark_labels (void)
4500 {
4501   struct insn_label_list *l;
4502 
4503   for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4504     mips_compressed_mark_label (l->label);
4505 }
4506 
4507 /* End the current frag.  Make it a variant frag and record the
4508    relaxation info.  */
4509 
4510 static void
relax_close_frag(void)4511 relax_close_frag (void)
4512 {
4513   mips_macro_warning.first_frag = frag_now;
4514   frag_var (rs_machine_dependent, 0, 0,
4515 	    RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4516 			  mips_pic != NO_PIC),
4517 	    mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4518 
4519   memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4520   mips_relax.first_fixup = 0;
4521 }
4522 
4523 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
4524    See the comment above RELAX_ENCODE for more details.  */
4525 
4526 static void
relax_start(symbolS * symbol)4527 relax_start (symbolS *symbol)
4528 {
4529   gas_assert (mips_relax.sequence == 0);
4530   mips_relax.sequence = 1;
4531   mips_relax.symbol = symbol;
4532 }
4533 
4534 /* Start generating the second version of a relaxable sequence.
4535    See the comment above RELAX_ENCODE for more details.  */
4536 
4537 static void
relax_switch(void)4538 relax_switch (void)
4539 {
4540   gas_assert (mips_relax.sequence == 1);
4541   mips_relax.sequence = 2;
4542 }
4543 
4544 /* End the current relaxable sequence.  */
4545 
4546 static void
relax_end(void)4547 relax_end (void)
4548 {
4549   gas_assert (mips_relax.sequence == 2);
4550   relax_close_frag ();
4551   mips_relax.sequence = 0;
4552 }
4553 
4554 /* Return true if IP is a delayed branch or jump.  */
4555 
4556 static inline bool
delayed_branch_p(const struct mips_cl_insn * ip)4557 delayed_branch_p (const struct mips_cl_insn *ip)
4558 {
4559   return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4560 				| INSN_COND_BRANCH_DELAY
4561 				| INSN_COND_BRANCH_LIKELY)) != 0;
4562 }
4563 
4564 /* Return true if IP is a compact branch or jump.  */
4565 
4566 static inline bool
compact_branch_p(const struct mips_cl_insn * ip)4567 compact_branch_p (const struct mips_cl_insn *ip)
4568 {
4569   return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4570 				 | INSN2_COND_BRANCH)) != 0;
4571 }
4572 
4573 /* Return true if IP is an unconditional branch or jump.  */
4574 
4575 static inline bool
uncond_branch_p(const struct mips_cl_insn * ip)4576 uncond_branch_p (const struct mips_cl_insn *ip)
4577 {
4578   return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
4579 	  || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
4580 }
4581 
4582 /* Return true if IP is a branch-likely instruction.  */
4583 
4584 static inline bool
branch_likely_p(const struct mips_cl_insn * ip)4585 branch_likely_p (const struct mips_cl_insn *ip)
4586 {
4587   return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4588 }
4589 
4590 /* Return the type of nop that should be used to fill the delay slot
4591    of delayed branch IP.  */
4592 
4593 static struct mips_cl_insn *
get_delay_slot_nop(const struct mips_cl_insn * ip)4594 get_delay_slot_nop (const struct mips_cl_insn *ip)
4595 {
4596   if (mips_opts.micromips
4597       && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4598     return &micromips_nop32_insn;
4599   return NOP_INSN;
4600 }
4601 
4602 /* Return a mask that has bit N set if OPCODE reads the register(s)
4603    in operand N.  */
4604 
4605 static unsigned int
insn_read_mask(const struct mips_opcode * opcode)4606 insn_read_mask (const struct mips_opcode *opcode)
4607 {
4608   return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4609 }
4610 
4611 /* Return a mask that has bit N set if OPCODE writes to the register(s)
4612    in operand N.  */
4613 
4614 static unsigned int
insn_write_mask(const struct mips_opcode * opcode)4615 insn_write_mask (const struct mips_opcode *opcode)
4616 {
4617   return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4618 }
4619 
4620 /* Return a mask of the registers specified by operand OPERAND of INSN.
4621    Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4622    is set.  */
4623 
4624 static unsigned int
operand_reg_mask(const struct mips_cl_insn * insn,const struct mips_operand * operand,unsigned int type_mask)4625 operand_reg_mask (const struct mips_cl_insn *insn,
4626 		  const struct mips_operand *operand,
4627 		  unsigned int type_mask)
4628 {
4629   unsigned int uval, vsel;
4630 
4631   switch (operand->type)
4632     {
4633     case OP_INT:
4634     case OP_MAPPED_INT:
4635     case OP_MSB:
4636     case OP_PCREL:
4637     case OP_PERF_REG:
4638     case OP_ADDIUSP_INT:
4639     case OP_ENTRY_EXIT_LIST:
4640     case OP_REPEAT_DEST_REG:
4641     case OP_REPEAT_PREV_REG:
4642     case OP_PC:
4643     case OP_VU0_SUFFIX:
4644     case OP_VU0_MATCH_SUFFIX:
4645     case OP_IMM_INDEX:
4646       abort ();
4647 
4648     case OP_REG28:
4649       return 1 << 28;
4650 
4651     case OP_REG:
4652     case OP_OPTIONAL_REG:
4653       {
4654 	const struct mips_reg_operand *reg_op;
4655 
4656 	reg_op = (const struct mips_reg_operand *) operand;
4657 	if (!(type_mask & (1 << reg_op->reg_type)))
4658 	  return 0;
4659 	uval = insn_extract_operand (insn, operand);
4660 	return 1u << mips_decode_reg_operand (reg_op, uval);
4661       }
4662 
4663     case OP_REG_PAIR:
4664       {
4665 	const struct mips_reg_pair_operand *pair_op;
4666 
4667 	pair_op = (const struct mips_reg_pair_operand *) operand;
4668 	if (!(type_mask & (1 << pair_op->reg_type)))
4669 	  return 0;
4670 	uval = insn_extract_operand (insn, operand);
4671 	return (1u << pair_op->reg1_map[uval]) | (1u << pair_op->reg2_map[uval]);
4672       }
4673 
4674     case OP_CLO_CLZ_DEST:
4675       if (!(type_mask & (1 << OP_REG_GP)))
4676 	return 0;
4677       uval = insn_extract_operand (insn, operand);
4678       return (1u << (uval & 31)) | (1u << (uval >> 5));
4679 
4680     case OP_SAME_RS_RT:
4681       if (!(type_mask & (1 << OP_REG_GP)))
4682 	return 0;
4683       uval = insn_extract_operand (insn, operand);
4684       gas_assert ((uval & 31) == (uval >> 5));
4685       return 1u << (uval & 31);
4686 
4687     case OP_CHECK_PREV:
4688     case OP_NON_ZERO_REG:
4689       if (!(type_mask & (1 << OP_REG_GP)))
4690 	return 0;
4691       uval = insn_extract_operand (insn, operand);
4692       return 1u << (uval & 31);
4693 
4694     case OP_LWM_SWM_LIST:
4695       abort ();
4696 
4697     case OP_SAVE_RESTORE_LIST:
4698       abort ();
4699 
4700     case OP_MDMX_IMM_REG:
4701       if (!(type_mask & (1 << OP_REG_VEC)))
4702 	return 0;
4703       uval = insn_extract_operand (insn, operand);
4704       vsel = uval >> 5;
4705       if ((vsel & 0x18) == 0x18)
4706 	return 0;
4707       return 1u << (uval & 31);
4708 
4709     case OP_REG_INDEX:
4710       if (!(type_mask & (1 << OP_REG_GP)))
4711 	return 0;
4712       return 1u << insn_extract_operand (insn, operand);
4713     }
4714   abort ();
4715 }
4716 
4717 /* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4718    where bit N of OPNO_MASK is set if operand N should be included.
4719    Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4720    is set.  */
4721 
4722 static unsigned int
insn_reg_mask(const struct mips_cl_insn * insn,unsigned int type_mask,unsigned int opno_mask)4723 insn_reg_mask (const struct mips_cl_insn *insn,
4724 	       unsigned int type_mask, unsigned int opno_mask)
4725 {
4726   unsigned int opno, reg_mask;
4727 
4728   opno = 0;
4729   reg_mask = 0;
4730   while (opno_mask != 0)
4731     {
4732       if (opno_mask & 1)
4733 	reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4734       opno_mask >>= 1;
4735       opno += 1;
4736     }
4737   return reg_mask;
4738 }
4739 
4740 /* Return the mask of core registers that IP reads.  */
4741 
4742 static unsigned int
gpr_read_mask(const struct mips_cl_insn * ip)4743 gpr_read_mask (const struct mips_cl_insn *ip)
4744 {
4745   unsigned long pinfo, pinfo2;
4746   unsigned int mask;
4747 
4748   mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4749   pinfo = ip->insn_mo->pinfo;
4750   pinfo2 = ip->insn_mo->pinfo2;
4751   if (pinfo & INSN_UDI)
4752     {
4753       /* UDI instructions have traditionally been assumed to read RS
4754 	 and RT.  */
4755       mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4756       mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4757     }
4758   if (pinfo & INSN_READ_GPR_24)
4759     mask |= 1 << 24;
4760   if (pinfo2 & INSN2_READ_GPR_16)
4761     mask |= 1 << 16;
4762   if (pinfo2 & INSN2_READ_SP)
4763     mask |= 1 << SP;
4764   if (pinfo2 & INSN2_READ_GPR_31)
4765     mask |= 1u << 31;
4766   /* Don't include register 0.  */
4767   return mask & ~1;
4768 }
4769 
4770 /* Return the mask of core registers that IP writes.  */
4771 
4772 static unsigned int
gpr_write_mask(const struct mips_cl_insn * ip)4773 gpr_write_mask (const struct mips_cl_insn *ip)
4774 {
4775   unsigned long pinfo, pinfo2;
4776   unsigned int mask;
4777 
4778   mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4779   pinfo = ip->insn_mo->pinfo;
4780   pinfo2 = ip->insn_mo->pinfo2;
4781   if (pinfo & INSN_WRITE_GPR_24)
4782     mask |= 1 << 24;
4783   if (pinfo & INSN_WRITE_GPR_31)
4784     mask |= 1u << 31;
4785   if (pinfo & INSN_UDI)
4786     /* UDI instructions have traditionally been assumed to write to RD.  */
4787     mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4788   if (pinfo2 & INSN2_WRITE_SP)
4789     mask |= 1 << SP;
4790   /* Don't include register 0.  */
4791   return mask & ~1;
4792 }
4793 
4794 /* Return the mask of floating-point registers that IP reads.  */
4795 
4796 static unsigned int
fpr_read_mask(const struct mips_cl_insn * ip)4797 fpr_read_mask (const struct mips_cl_insn *ip)
4798 {
4799   unsigned long pinfo;
4800   unsigned int mask;
4801 
4802   mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4803 			     | (1 << OP_REG_MSA)),
4804 			insn_read_mask (ip->insn_mo));
4805   pinfo = ip->insn_mo->pinfo;
4806   /* Conservatively treat all operands to an FP_D instruction are doubles.
4807      (This is overly pessimistic for things like cvt.d.s.)  */
4808   if (FPR_SIZE != 64 && (pinfo & FP_D))
4809     mask |= mask << 1;
4810   return mask;
4811 }
4812 
4813 /* Return the mask of floating-point registers that IP writes.  */
4814 
4815 static unsigned int
fpr_write_mask(const struct mips_cl_insn * ip)4816 fpr_write_mask (const struct mips_cl_insn *ip)
4817 {
4818   unsigned long pinfo;
4819   unsigned int mask;
4820 
4821   mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4822 			     | (1 << OP_REG_MSA)),
4823 			insn_write_mask (ip->insn_mo));
4824   pinfo = ip->insn_mo->pinfo;
4825   /* Conservatively treat all operands to an FP_D instruction are doubles.
4826      (This is overly pessimistic for things like cvt.s.d.)  */
4827   if (FPR_SIZE != 64 && (pinfo & FP_D))
4828     mask |= mask << 1;
4829   return mask;
4830 }
4831 
4832 /* Operand OPNUM of INSN is an odd-numbered floating-point register.
4833    Check whether that is allowed.  */
4834 
4835 static bool
mips_oddfpreg_ok(const struct mips_opcode * insn,int opnum)4836 mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4837 {
4838   const char *s = insn->name;
4839   bool oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4840 		   || FPR_SIZE == 64) && mips_opts.oddspreg;
4841 
4842   if (insn->pinfo == INSN_MACRO)
4843     /* Let a macro pass, we'll catch it later when it is expanded.  */
4844     return true;
4845 
4846   /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4847      otherwise it depends on oddspreg.  */
4848   if ((insn->pinfo & FP_S)
4849       && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
4850 			 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
4851     return FPR_SIZE == 32 || oddspreg;
4852 
4853   /* Allow odd registers for single-precision ops and double-precision if the
4854      floating-point registers are 64-bit wide.  */
4855   switch (insn->pinfo & (FP_S | FP_D))
4856     {
4857     case FP_S:
4858     case 0:
4859       return oddspreg;
4860     case FP_D:
4861       return FPR_SIZE == 64;
4862     default:
4863       break;
4864     }
4865 
4866   /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand.  */
4867   s = strchr (insn->name, '.');
4868   if (s != NULL && opnum == 2)
4869     s = strchr (s + 1, '.');
4870   if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4871     return oddspreg;
4872 
4873   return FPR_SIZE == 64;
4874 }
4875 
4876 /* Information about an instruction argument that we're trying to match.  */
4877 struct mips_arg_info
4878 {
4879   /* The instruction so far.  */
4880   struct mips_cl_insn *insn;
4881 
4882   /* The first unconsumed operand token.  */
4883   struct mips_operand_token *token;
4884 
4885   /* The 1-based operand number, in terms of insn->insn_mo->args.  */
4886   int opnum;
4887 
4888   /* The 1-based argument number, for error reporting.  This does not
4889      count elided optional registers, etc..  */
4890   int argnum;
4891 
4892   /* The last OP_REG operand seen, or ILLEGAL_REG if none.  */
4893   unsigned int last_regno;
4894 
4895   /* If the first operand was an OP_REG, this is the register that it
4896      specified, otherwise it is ILLEGAL_REG.  */
4897   unsigned int dest_regno;
4898 
4899   /* The value of the last OP_INT operand.  Only used for OP_MSB,
4900      where it gives the lsb position.  */
4901   unsigned int last_op_int;
4902 
4903   /* If true, match routines should assume that no later instruction
4904      alternative matches and should therefore be as accommodating as
4905      possible.  Match routines should not report errors if something
4906      is only invalid for !LAX_MATCH.  */
4907   bool lax_match;
4908 
4909   /* True if a reference to the current AT register was seen.  */
4910   bool seen_at;
4911 };
4912 
4913 /* Record that the argument is out of range.  */
4914 
4915 static void
match_out_of_range(struct mips_arg_info * arg)4916 match_out_of_range (struct mips_arg_info *arg)
4917 {
4918   set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4919 }
4920 
4921 /* Record that the argument isn't constant but needs to be.  */
4922 
4923 static void
match_not_constant(struct mips_arg_info * arg)4924 match_not_constant (struct mips_arg_info *arg)
4925 {
4926   set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4927 		    arg->argnum);
4928 }
4929 
4930 /* Try to match an OT_CHAR token for character CH.  Consume the token
4931    and return true on success, otherwise return false.  */
4932 
4933 static bool
match_char(struct mips_arg_info * arg,char ch)4934 match_char (struct mips_arg_info *arg, char ch)
4935 {
4936   if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4937     {
4938       ++arg->token;
4939       if (ch == ',')
4940 	arg->argnum += 1;
4941       return true;
4942     }
4943   return false;
4944 }
4945 
4946 /* Try to get an expression from the next tokens in ARG.  Consume the
4947    tokens and return true on success, storing the expression value in
4948    VALUE and relocation types in R.  */
4949 
4950 static bool
match_expression(struct mips_arg_info * arg,expressionS * value,bfd_reloc_code_real_type * r)4951 match_expression (struct mips_arg_info *arg, expressionS *value,
4952 		  bfd_reloc_code_real_type *r)
4953 {
4954   /* If the next token is a '(' that was parsed as being part of a base
4955      expression, assume we have an elided offset.  The later match will fail
4956      if this turns out to be wrong.  */
4957   if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4958     {
4959       value->X_op = O_constant;
4960       value->X_add_number = 0;
4961       r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4962       return true;
4963     }
4964 
4965   /* Reject register-based expressions such as "0+$2" and "(($2))".
4966      For plain registers the default error seems more appropriate.  */
4967   if (arg->token->type == OT_INTEGER
4968       && arg->token->u.integer.value.X_op == O_register)
4969     {
4970       set_insn_error (arg->argnum, _("register value used as expression"));
4971       return false;
4972     }
4973 
4974   if (arg->token->type == OT_INTEGER)
4975     {
4976       *value = arg->token->u.integer.value;
4977       memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4978       ++arg->token;
4979       return true;
4980     }
4981 
4982   set_insn_error_i
4983     (arg->argnum, _("operand %d must be an immediate expression"),
4984      arg->argnum);
4985   return false;
4986 }
4987 
4988 /* Try to get a constant expression from the next tokens in ARG.  Consume
4989    the tokens and return true on success, storing the constant value
4990    in *VALUE.  */
4991 
4992 static bool
match_const_int(struct mips_arg_info * arg,offsetT * value)4993 match_const_int (struct mips_arg_info *arg, offsetT *value)
4994 {
4995   expressionS ex;
4996   bfd_reloc_code_real_type r[3];
4997 
4998   if (!match_expression (arg, &ex, r))
4999     return false;
5000 
5001   if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
5002     *value = ex.X_add_number;
5003   else
5004     {
5005       if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
5006 	match_out_of_range (arg);
5007       else
5008 	match_not_constant (arg);
5009       return false;
5010     }
5011   return true;
5012 }
5013 
5014 /* Return the RTYPE_* flags for a register operand of type TYPE that
5015    appears in instruction OPCODE.  */
5016 
5017 static unsigned int
convert_reg_type(const struct mips_opcode * opcode,enum mips_reg_operand_type type)5018 convert_reg_type (const struct mips_opcode *opcode,
5019 		  enum mips_reg_operand_type type)
5020 {
5021   switch (type)
5022     {
5023     case OP_REG_GP:
5024       return RTYPE_NUM | RTYPE_GP;
5025 
5026     case OP_REG_FP:
5027       /* Allow vector register names for MDMX if the instruction is a 64-bit
5028 	 FPR load, store or move (including moves to and from GPRs).  */
5029       if ((mips_opts.ase & ASE_MDMX)
5030 	  && (opcode->pinfo & FP_D)
5031 	  && (opcode->pinfo & (INSN_COPROC_MOVE
5032 			       | INSN_COPROC_MEMORY_DELAY
5033 			       | INSN_LOAD_COPROC
5034 			       | INSN_LOAD_MEMORY
5035 			       | INSN_STORE_MEMORY)))
5036 	return RTYPE_FPU | RTYPE_VEC;
5037       return RTYPE_FPU;
5038 
5039     case OP_REG_CCC:
5040       if (opcode->pinfo & (FP_D | FP_S))
5041 	return RTYPE_CCC | RTYPE_FCC;
5042       return RTYPE_CCC;
5043 
5044     case OP_REG_VEC:
5045       if (opcode->membership & INSN_5400)
5046 	return RTYPE_FPU;
5047       return RTYPE_FPU | RTYPE_VEC;
5048 
5049     case OP_REG_ACC:
5050       return RTYPE_ACC;
5051 
5052     case OP_REG_COPRO:
5053     case OP_REG_CONTROL:
5054       if (opcode->name[strlen (opcode->name) - 1] == '0')
5055 	return RTYPE_NUM | RTYPE_CP0;
5056       return RTYPE_NUM;
5057 
5058     case OP_REG_HW:
5059       return RTYPE_NUM;
5060 
5061     case OP_REG_VI:
5062       return RTYPE_NUM | RTYPE_VI;
5063 
5064     case OP_REG_VF:
5065       return RTYPE_NUM | RTYPE_VF;
5066 
5067     case OP_REG_R5900_I:
5068       return RTYPE_R5900_I;
5069 
5070     case OP_REG_R5900_Q:
5071       return RTYPE_R5900_Q;
5072 
5073     case OP_REG_R5900_R:
5074       return RTYPE_R5900_R;
5075 
5076     case OP_REG_R5900_ACC:
5077       return RTYPE_R5900_ACC;
5078 
5079     case OP_REG_MSA:
5080       return RTYPE_MSA;
5081 
5082     case OP_REG_MSA_CTRL:
5083       return RTYPE_NUM;
5084     }
5085   abort ();
5086 }
5087 
5088 /* ARG is register REGNO, of type TYPE.  Warn about any dubious registers.  */
5089 
5090 static void
check_regno(struct mips_arg_info * arg,enum mips_reg_operand_type type,unsigned int regno)5091 check_regno (struct mips_arg_info *arg,
5092 	     enum mips_reg_operand_type type, unsigned int regno)
5093 {
5094   if (AT && type == OP_REG_GP && regno == AT)
5095     arg->seen_at = true;
5096 
5097   if (type == OP_REG_FP
5098       && (regno & 1) != 0
5099       && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
5100     {
5101       /* This was a warning prior to introducing O32 FPXX and FP64 support
5102 	 so maintain a warning for FP32 but raise an error for the new
5103 	 cases.  */
5104       if (FPR_SIZE == 32)
5105 	as_warn (_("float register should be even, was %d"), regno);
5106       else
5107 	as_bad (_("float register should be even, was %d"), regno);
5108     }
5109 
5110   if (type == OP_REG_CCC)
5111     {
5112       const char *name;
5113       size_t length;
5114 
5115       name = arg->insn->insn_mo->name;
5116       length = strlen (name);
5117       if ((regno & 1) != 0
5118 	  && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5119 	      || (length >= 5 && startswith (name + length - 5, "any2"))))
5120 	as_warn (_("condition code register should be even for %s, was %d"),
5121 		 name, regno);
5122 
5123       if ((regno & 3) != 0
5124 	  && (length >= 5 && startswith (name + length - 5, "any4")))
5125 	as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
5126 		 name, regno);
5127     }
5128 }
5129 
5130 /* ARG is a register with symbol value SYMVAL.  Try to interpret it as
5131    a register of type TYPE.  Return true on success, storing the register
5132    number in *REGNO and warning about any dubious uses.  */
5133 
5134 static bool
match_regno(struct mips_arg_info * arg,enum mips_reg_operand_type type,unsigned int symval,unsigned int * regno)5135 match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5136 	     unsigned int symval, unsigned int *regno)
5137 {
5138   if (type == OP_REG_VEC)
5139     symval = mips_prefer_vec_regno (symval);
5140   if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5141     return false;
5142 
5143   *regno = symval & RNUM_MASK;
5144   check_regno (arg, type, *regno);
5145   return true;
5146 }
5147 
5148 /* Try to interpret the next token in ARG as a register of type TYPE.
5149    Consume the token and return true on success, storing the register
5150    number in *REGNO.  Return false on failure.  */
5151 
5152 static bool
match_reg(struct mips_arg_info * arg,enum mips_reg_operand_type type,unsigned int * regno)5153 match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5154 	   unsigned int *regno)
5155 {
5156   if (arg->token->type == OT_REG
5157       && match_regno (arg, type, arg->token->u.regno, regno))
5158     {
5159       ++arg->token;
5160       return true;
5161     }
5162   return false;
5163 }
5164 
5165 /* Try to interpret the next token in ARG as a range of registers of type TYPE.
5166    Consume the token and return true on success, storing the register numbers
5167    in *REGNO1 and *REGNO2.  Return false on failure.  */
5168 
5169 static bool
match_reg_range(struct mips_arg_info * arg,enum mips_reg_operand_type type,unsigned int * regno1,unsigned int * regno2)5170 match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5171 		 unsigned int *regno1, unsigned int *regno2)
5172 {
5173   if (match_reg (arg, type, regno1))
5174     {
5175       *regno2 = *regno1;
5176       return true;
5177     }
5178   if (arg->token->type == OT_REG_RANGE
5179       && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5180       && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5181       && *regno1 <= *regno2)
5182     {
5183       ++arg->token;
5184       return true;
5185     }
5186   return false;
5187 }
5188 
5189 /* OP_INT matcher.  */
5190 
5191 static bool
match_int_operand(struct mips_arg_info * arg,const struct mips_operand * operand_base)5192 match_int_operand (struct mips_arg_info *arg,
5193 		   const struct mips_operand *operand_base)
5194 {
5195   const struct mips_int_operand *operand;
5196   unsigned int uval;
5197   int min_val, max_val, factor;
5198   offsetT sval;
5199 
5200   operand = (const struct mips_int_operand *) operand_base;
5201   factor = 1 << operand->shift;
5202   min_val = mips_int_operand_min (operand);
5203   max_val = mips_int_operand_max (operand);
5204 
5205   if (operand_base->lsb == 0
5206       && operand_base->size == 16
5207       && operand->shift == 0
5208       && operand->bias == 0
5209       && (operand->max_val == 32767 || operand->max_val == 65535))
5210     {
5211       /* The operand can be relocated.  */
5212       if (!match_expression (arg, &offset_expr, offset_reloc))
5213 	return false;
5214 
5215       if (offset_expr.X_op == O_big)
5216 	{
5217 	  match_out_of_range (arg);
5218 	  return false;
5219 	}
5220 
5221       if (offset_reloc[0] != BFD_RELOC_UNUSED)
5222 	/* Relocation operators were used.  Accept the argument and
5223 	   leave the relocation value in offset_expr and offset_relocs
5224 	   for the caller to process.  */
5225 	return true;
5226 
5227       if (offset_expr.X_op != O_constant)
5228 	{
5229 	  /* Accept non-constant operands if no later alternative matches,
5230 	     leaving it for the caller to process.  */
5231 	  if (!arg->lax_match)
5232 	    {
5233 	      match_not_constant (arg);
5234 	      return false;
5235 	    }
5236 	  offset_reloc[0] = BFD_RELOC_LO16;
5237 	  return true;
5238 	}
5239 
5240       /* Clear the global state; we're going to install the operand
5241 	 ourselves.  */
5242       sval = offset_expr.X_add_number;
5243       offset_expr.X_op = O_absent;
5244 
5245       /* For compatibility with older assemblers, we accept
5246 	 0x8000-0xffff as signed 16-bit numbers when only
5247 	 signed numbers are allowed.  */
5248       if (sval > max_val)
5249 	{
5250 	  max_val = ((1 << operand_base->size) - 1) << operand->shift;
5251 	  if (!arg->lax_match && sval <= max_val)
5252 	    {
5253 	      match_out_of_range (arg);
5254 	      return false;
5255 	    }
5256 	}
5257     }
5258   else
5259     {
5260       if (!match_const_int (arg, &sval))
5261 	return false;
5262     }
5263 
5264   arg->last_op_int = sval;
5265 
5266   if (sval < min_val || sval > max_val || sval % factor)
5267     {
5268       match_out_of_range (arg);
5269       return false;
5270     }
5271 
5272   uval = (unsigned int) sval >> operand->shift;
5273   uval -= operand->bias;
5274 
5275   /* Handle -mfix-cn63xxp1.  */
5276   if (arg->opnum == 1
5277       && mips_fix_cn63xxp1
5278       && !mips_opts.micromips
5279       && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5280     switch (uval)
5281       {
5282       case 5:
5283       case 25:
5284       case 26:
5285       case 27:
5286       case 28:
5287       case 29:
5288       case 30:
5289       case 31:
5290 	/* These are ok.  */
5291 	break;
5292 
5293       default:
5294 	/* The rest must be changed to 28.  */
5295 	uval = 28;
5296 	break;
5297       }
5298 
5299   insn_insert_operand (arg->insn, operand_base, uval);
5300   return true;
5301 }
5302 
5303 /* OP_MAPPED_INT matcher.  */
5304 
5305 static bool
match_mapped_int_operand(struct mips_arg_info * arg,const struct mips_operand * operand_base)5306 match_mapped_int_operand (struct mips_arg_info *arg,
5307 			  const struct mips_operand *operand_base)
5308 {
5309   const struct mips_mapped_int_operand *operand;
5310   unsigned int uval, num_vals;
5311   offsetT sval;
5312 
5313   operand = (const struct mips_mapped_int_operand *) operand_base;
5314   if (!match_const_int (arg, &sval))
5315     return false;
5316 
5317   num_vals = 1 << operand_base->size;
5318   for (uval = 0; uval < num_vals; uval++)
5319     if (operand->int_map[uval] == sval)
5320       break;
5321   if (uval == num_vals)
5322     {
5323       match_out_of_range (arg);
5324       return false;
5325     }
5326 
5327   insn_insert_operand (arg->insn, operand_base, uval);
5328   return true;
5329 }
5330 
5331 /* OP_MSB matcher.  */
5332 
5333 static bool
match_msb_operand(struct mips_arg_info * arg,const struct mips_operand * operand_base)5334 match_msb_operand (struct mips_arg_info *arg,
5335 		   const struct mips_operand *operand_base)
5336 {
5337   const struct mips_msb_operand *operand;
5338   int min_val, max_val, max_high;
5339   offsetT size, sval, high;
5340 
5341   operand = (const struct mips_msb_operand *) operand_base;
5342   min_val = operand->bias;
5343   max_val = min_val + (1 << operand_base->size) - 1;
5344   max_high = operand->opsize;
5345 
5346   if (!match_const_int (arg, &size))
5347     return false;
5348 
5349   high = size + arg->last_op_int;
5350   sval = operand->add_lsb ? high : size;
5351 
5352   if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5353     {
5354       match_out_of_range (arg);
5355       return false;
5356     }
5357   insn_insert_operand (arg->insn, operand_base, sval - min_val);
5358   return true;
5359 }
5360 
5361 /* OP_REG matcher.  */
5362 
5363 static bool
match_reg_operand(struct mips_arg_info * arg,const struct mips_operand * operand_base)5364 match_reg_operand (struct mips_arg_info *arg,
5365 		   const struct mips_operand *operand_base)
5366 {
5367   const struct mips_reg_operand *operand;
5368   unsigned int regno, uval, num_vals;
5369 
5370   operand = (const struct mips_reg_operand *) operand_base;
5371   if (!match_reg (arg, operand->reg_type, &regno))
5372     return false;
5373 
5374   if (operand->reg_map)
5375     {
5376       num_vals = 1 << operand->root.size;
5377       for (uval = 0; uval < num_vals; uval++)
5378 	if (operand->reg_map[uval] == regno)
5379 	  break;
5380       if (num_vals == uval)
5381 	return false;
5382     }
5383   else
5384     uval = regno;
5385 
5386   arg->last_regno = regno;
5387   if (arg->opnum == 1)
5388     arg->dest_regno = regno;
5389   insn_insert_operand (arg->insn, operand_base, uval);
5390   return true;
5391 }
5392 
5393 /* OP_REG_PAIR matcher.  */
5394 
5395 static bool
match_reg_pair_operand(struct mips_arg_info * arg,const struct mips_operand * operand_base)5396 match_reg_pair_operand (struct mips_arg_info *arg,
5397 			const struct mips_operand *operand_base)
5398 {
5399   const struct mips_reg_pair_operand *operand;
5400   unsigned int regno1, regno2, uval, num_vals;
5401 
5402   operand = (const struct mips_reg_pair_operand *) operand_base;
5403   if (!match_reg (arg, operand->reg_type, &regno1)
5404       || !match_char (arg, ',')
5405       || !match_reg (arg, operand->reg_type, &regno2))
5406     return false;
5407 
5408   num_vals = 1 << operand_base->size;
5409   for (uval = 0; uval < num_vals; uval++)
5410     if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5411       break;
5412   if (uval == num_vals)
5413     return false;
5414 
5415   insn_insert_operand (arg->insn, operand_base, uval);
5416   return true;
5417 }
5418 
5419 /* OP_PCREL matcher.  The caller chooses the relocation type.  */
5420 
5421 static bool
match_pcrel_operand(struct mips_arg_info * arg)5422 match_pcrel_operand (struct mips_arg_info *arg)
5423 {
5424   bfd_reloc_code_real_type r[3];
5425 
5426   return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
5427 }
5428 
5429 /* OP_PERF_REG matcher.  */
5430 
5431 static bool
match_perf_reg_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5432 match_perf_reg_operand (struct mips_arg_info *arg,
5433 			const struct mips_operand *operand)
5434 {
5435   offsetT sval;
5436 
5437   if (!match_const_int (arg, &sval))
5438     return false;
5439 
5440   if (sval != 0
5441       && (sval != 1
5442 	  || (mips_opts.arch == CPU_R5900
5443 	      && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5444 		  || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5445     {
5446       set_insn_error (arg->argnum, _("invalid performance register"));
5447       return false;
5448     }
5449 
5450   insn_insert_operand (arg->insn, operand, sval);
5451   return true;
5452 }
5453 
5454 /* OP_ADDIUSP matcher.  */
5455 
5456 static bool
match_addiusp_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5457 match_addiusp_operand (struct mips_arg_info *arg,
5458 		       const struct mips_operand *operand)
5459 {
5460   offsetT sval;
5461   unsigned int uval;
5462 
5463   if (!match_const_int (arg, &sval))
5464     return false;
5465 
5466   if (sval % 4)
5467     {
5468       match_out_of_range (arg);
5469       return false;
5470     }
5471 
5472   sval /= 4;
5473   if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
5474     {
5475       match_out_of_range (arg);
5476       return false;
5477     }
5478 
5479   uval = (unsigned int) sval;
5480   uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5481   insn_insert_operand (arg->insn, operand, uval);
5482   return true;
5483 }
5484 
5485 /* OP_CLO_CLZ_DEST matcher.  */
5486 
5487 static bool
match_clo_clz_dest_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5488 match_clo_clz_dest_operand (struct mips_arg_info *arg,
5489 			    const struct mips_operand *operand)
5490 {
5491   unsigned int regno;
5492 
5493   if (!match_reg (arg, OP_REG_GP, &regno))
5494     return false;
5495 
5496   insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5497   return true;
5498 }
5499 
5500 /* OP_CHECK_PREV matcher.  */
5501 
5502 static bool
match_check_prev_operand(struct mips_arg_info * arg,const struct mips_operand * operand_base)5503 match_check_prev_operand (struct mips_arg_info *arg,
5504 			  const struct mips_operand *operand_base)
5505 {
5506   const struct mips_check_prev_operand *operand;
5507   unsigned int regno;
5508 
5509   operand = (const struct mips_check_prev_operand *) operand_base;
5510 
5511   if (!match_reg (arg, OP_REG_GP, &regno))
5512     return false;
5513 
5514   if (!operand->zero_ok && regno == 0)
5515     return false;
5516 
5517   if ((operand->less_than_ok && regno < arg->last_regno)
5518       || (operand->greater_than_ok && regno > arg->last_regno)
5519       || (operand->equal_ok && regno == arg->last_regno))
5520     {
5521       arg->last_regno = regno;
5522       insn_insert_operand (arg->insn, operand_base, regno);
5523       return true;
5524     }
5525 
5526   return false;
5527 }
5528 
5529 /* OP_SAME_RS_RT matcher.  */
5530 
5531 static bool
match_same_rs_rt_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5532 match_same_rs_rt_operand (struct mips_arg_info *arg,
5533 			  const struct mips_operand *operand)
5534 {
5535   unsigned int regno;
5536 
5537   if (!match_reg (arg, OP_REG_GP, &regno))
5538     return false;
5539 
5540   if (regno == 0)
5541     {
5542       set_insn_error (arg->argnum, _("the source register must not be $0"));
5543       return false;
5544     }
5545 
5546   arg->last_regno = regno;
5547 
5548   insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5549   return true;
5550 }
5551 
5552 /* OP_LWM_SWM_LIST matcher.  */
5553 
5554 static bool
match_lwm_swm_list_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5555 match_lwm_swm_list_operand (struct mips_arg_info *arg,
5556 			    const struct mips_operand *operand)
5557 {
5558   unsigned int reglist, sregs, ra, regno1, regno2;
5559   struct mips_arg_info reset;
5560 
5561   reglist = 0;
5562   if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5563     return false;
5564   do
5565     {
5566       if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5567 	{
5568 	  reglist |= 1 << FP;
5569 	  regno2 = S7;
5570 	}
5571       reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5572       reset = *arg;
5573     }
5574   while (match_char (arg, ',')
5575 	 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5576   *arg = reset;
5577 
5578   if (operand->size == 2)
5579     {
5580       /* The list must include both ra and s0-sN, for 0 <= N <= 3.  E.g.:
5581 
5582 	 s0, ra
5583 	 s0, s1, ra, s2, s3
5584 	 s0-s2, ra
5585 
5586 	 and any permutations of these.  */
5587       if ((reglist & 0xfff1ffff) != 0x80010000)
5588 	return false;
5589 
5590       sregs = (reglist >> 17) & 7;
5591       ra = 0;
5592     }
5593   else
5594     {
5595       /* The list must include at least one of ra and s0-sN,
5596 	 for 0 <= N <= 8.  (Note that there is a gap between s7 and s8,
5597 	 which are $23 and $30 respectively.)  E.g.:
5598 
5599 	 ra
5600 	 s0
5601 	 ra, s0, s1, s2
5602 	 s0-s8
5603 	 s0-s5, ra
5604 
5605 	 and any permutations of these.  */
5606       if ((reglist & 0x3f00ffff) != 0)
5607 	return false;
5608 
5609       ra = (reglist >> 27) & 0x10;
5610       sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5611     }
5612   sregs += 1;
5613   if ((sregs & -sregs) != sregs)
5614     return false;
5615 
5616   insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5617   return true;
5618 }
5619 
5620 /* OP_ENTRY_EXIT_LIST matcher.  */
5621 
5622 static unsigned int
match_entry_exit_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5623 match_entry_exit_operand (struct mips_arg_info *arg,
5624 			  const struct mips_operand *operand)
5625 {
5626   unsigned int mask;
5627   bool is_exit;
5628 
5629   /* The format is the same for both ENTRY and EXIT, but the constraints
5630      are different.  */
5631   is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5632   mask = (is_exit ? 7 << 3 : 0);
5633   do
5634     {
5635       unsigned int regno1, regno2;
5636       bool is_freg;
5637 
5638       if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5639 	is_freg = false;
5640       else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5641 	is_freg = true;
5642       else
5643 	return false;
5644 
5645       if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5646 	{
5647 	  mask &= ~(7 << 3);
5648 	  mask |= (5 + regno2) << 3;
5649 	}
5650       else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5651 	mask |= (regno2 - 3) << 3;
5652       else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5653 	mask |= (regno2 - 15) << 1;
5654       else if (regno1 == RA && regno2 == RA)
5655 	mask |= 1;
5656       else
5657 	return false;
5658     }
5659   while (match_char (arg, ','));
5660 
5661   insn_insert_operand (arg->insn, operand, mask);
5662   return true;
5663 }
5664 
5665 /* Encode regular MIPS SAVE/RESTORE instruction operands according to
5666    the argument register mask AMASK, the number of static registers
5667    saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5668    respectively, and the frame size FRAME_SIZE.  */
5669 
5670 static unsigned int
mips_encode_save_restore(unsigned int amask,unsigned int nsreg,unsigned int ra,unsigned int s0,unsigned int s1,unsigned int frame_size)5671 mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5672 			  unsigned int ra, unsigned int s0, unsigned int s1,
5673 			  unsigned int frame_size)
5674 {
5675   return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5676 	  | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5677 }
5678 
5679 /* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5680    argument register mask AMASK, the number of static registers saved
5681    NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5682    respectively, and the frame size FRAME_SIZE.  */
5683 
5684 static unsigned int
mips16_encode_save_restore(unsigned int amask,unsigned int nsreg,unsigned int ra,unsigned int s0,unsigned int s1,unsigned int frame_size)5685 mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5686 			    unsigned int ra, unsigned int s0, unsigned int s1,
5687 			    unsigned int frame_size)
5688 {
5689   unsigned int args;
5690 
5691   args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5692   if (nsreg || amask || frame_size == 0 || frame_size > 16)
5693     args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5694 	     | ((frame_size & 0xf0) << 16));
5695   return args;
5696 }
5697 
5698 /* OP_SAVE_RESTORE_LIST matcher.  */
5699 
5700 static bool
match_save_restore_list_operand(struct mips_arg_info * arg)5701 match_save_restore_list_operand (struct mips_arg_info *arg)
5702 {
5703   unsigned int opcode, args, statics, sregs;
5704   unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
5705   unsigned int arg_mask, ra, s0, s1;
5706   offsetT frame_size;
5707 
5708   opcode = arg->insn->insn_opcode;
5709   frame_size = 0;
5710   num_frame_sizes = 0;
5711   args = 0;
5712   statics = 0;
5713   sregs = 0;
5714   ra = 0;
5715   s0 = 0;
5716   s1 = 0;
5717   do
5718     {
5719       unsigned int regno1, regno2;
5720 
5721       if (arg->token->type == OT_INTEGER)
5722 	{
5723 	  /* Handle the frame size.  */
5724 	  if (!match_const_int (arg, &frame_size))
5725 	    return false;
5726 	  num_frame_sizes += 1;
5727 	}
5728       else
5729 	{
5730 	  if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5731 	    return false;
5732 
5733 	  while (regno1 <= regno2)
5734 	    {
5735 	      if (regno1 >= 4 && regno1 <= 7)
5736 		{
5737 		  if (num_frame_sizes == 0)
5738 		    /* args $a0-$a3 */
5739 		    args |= 1 << (regno1 - 4);
5740 		  else
5741 		    /* statics $a0-$a3 */
5742 		    statics |= 1 << (regno1 - 4);
5743 		}
5744 	      else if (regno1 >= 16 && regno1 <= 23)
5745 		/* $s0-$s7 */
5746 		sregs |= 1 << (regno1 - 16);
5747 	      else if (regno1 == 30)
5748 		/* $s8 */
5749 		sregs |= 1 << 8;
5750 	      else if (regno1 == 31)
5751 		/* Add $ra to insn.  */
5752 		ra = 1;
5753 	      else
5754 		return false;
5755 	      regno1 += 1;
5756 	      if (regno1 == 24)
5757 		regno1 = 30;
5758 	    }
5759 	}
5760     }
5761   while (match_char (arg, ','));
5762 
5763   /* Encode args/statics combination.  */
5764   if (args & statics)
5765     return false;
5766   else if (args == 0xf)
5767     /* All $a0-$a3 are args.  */
5768     arg_mask = MIPS_SVRS_ALL_ARGS;
5769   else if (statics == 0xf)
5770     /* All $a0-$a3 are statics.  */
5771     arg_mask = MIPS_SVRS_ALL_STATICS;
5772   else
5773     {
5774       /* Count arg registers.  */
5775       num_args = 0;
5776       while (args & 0x1)
5777 	{
5778 	  args >>= 1;
5779 	  num_args += 1;
5780 	}
5781       if (args != 0)
5782 	return false;
5783 
5784       /* Count static registers.  */
5785       num_statics = 0;
5786       while (statics & 0x8)
5787 	{
5788 	  statics = (statics << 1) & 0xf;
5789 	  num_statics += 1;
5790 	}
5791       if (statics != 0)
5792 	return false;
5793 
5794       /* Encode args/statics.  */
5795       arg_mask = (num_args << 2) | num_statics;
5796     }
5797 
5798   /* Encode $s0/$s1.  */
5799   if (sregs & (1 << 0))		/* $s0 */
5800     s0 = 1;
5801   if (sregs & (1 << 1))		/* $s1 */
5802     s1 = 1;
5803   sregs >>= 2;
5804 
5805   /* Encode $s2-$s8. */
5806   num_sregs = 0;
5807   while (sregs & 1)
5808     {
5809       sregs >>= 1;
5810       num_sregs += 1;
5811     }
5812   if (sregs != 0)
5813     return false;
5814 
5815   /* Encode frame size.  */
5816   if (num_frame_sizes == 0)
5817     {
5818       set_insn_error (arg->argnum, _("missing frame size"));
5819       return false;
5820     }
5821   if (num_frame_sizes > 1)
5822     {
5823       set_insn_error (arg->argnum, _("frame size specified twice"));
5824       return false;
5825     }
5826   if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5827     {
5828       set_insn_error (arg->argnum, _("invalid frame size"));
5829       return false;
5830     }
5831   frame_size /= 8;
5832 
5833   /* Finally build the instruction.  */
5834   if (mips_opts.mips16)
5835     opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5836 					  frame_size);
5837   else if (!mips_opts.micromips)
5838     opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5839 					frame_size);
5840   else
5841     abort ();
5842 
5843   arg->insn->insn_opcode = opcode;
5844   return true;
5845 }
5846 
5847 /* OP_MDMX_IMM_REG matcher.  */
5848 
5849 static bool
match_mdmx_imm_reg_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5850 match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
5851 			    const struct mips_operand *operand)
5852 {
5853   unsigned int regno, uval;
5854   bool is_qh;
5855   const struct mips_opcode *opcode;
5856 
5857   /* The mips_opcode records whether this is an octobyte or quadhalf
5858      instruction.  Start out with that bit in place.  */
5859   opcode = arg->insn->insn_mo;
5860   uval = mips_extract_operand (operand, opcode->match);
5861   is_qh = (uval != 0);
5862 
5863   if (arg->token->type == OT_REG)
5864     {
5865       if ((opcode->membership & INSN_5400)
5866 	  && strcmp (opcode->name, "rzu.ob") == 0)
5867 	{
5868 	  set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5869 			    arg->argnum);
5870 	  return false;
5871 	}
5872 
5873       if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5874 	return false;
5875       ++arg->token;
5876 
5877       /* Check whether this is a vector register or a broadcast of
5878 	 a single element.  */
5879       if (arg->token->type == OT_INTEGER_INDEX)
5880 	{
5881 	  if (arg->token->u.index > (is_qh ? 3 : 7))
5882 	    {
5883 	      set_insn_error (arg->argnum, _("invalid element selector"));
5884 	      return false;
5885 	    }
5886 	  uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5887 	  ++arg->token;
5888 	}
5889       else
5890 	{
5891 	  /* A full vector.  */
5892 	  if ((opcode->membership & INSN_5400)
5893 	      && (strcmp (opcode->name, "sll.ob") == 0
5894 		  || strcmp (opcode->name, "srl.ob") == 0))
5895 	    {
5896 	      set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5897 				arg->argnum);
5898 	      return false;
5899 	    }
5900 
5901 	  if (is_qh)
5902 	    uval |= MDMX_FMTSEL_VEC_QH << 5;
5903 	  else
5904 	    uval |= MDMX_FMTSEL_VEC_OB << 5;
5905 	}
5906       uval |= regno;
5907     }
5908   else
5909     {
5910       offsetT sval;
5911 
5912       if (!match_const_int (arg, &sval))
5913 	return false;
5914       if (sval < 0 || sval > 31)
5915 	{
5916 	  match_out_of_range (arg);
5917 	  return false;
5918 	}
5919       uval |= (sval & 31);
5920       if (is_qh)
5921 	uval |= MDMX_FMTSEL_IMM_QH << 5;
5922       else
5923 	uval |= MDMX_FMTSEL_IMM_OB << 5;
5924     }
5925   insn_insert_operand (arg->insn, operand, uval);
5926   return true;
5927 }
5928 
5929 /* OP_IMM_INDEX matcher.  */
5930 
5931 static bool
match_imm_index_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5932 match_imm_index_operand (struct mips_arg_info *arg,
5933 			 const struct mips_operand *operand)
5934 {
5935   unsigned int max_val;
5936 
5937   if (arg->token->type != OT_INTEGER_INDEX)
5938     return false;
5939 
5940   max_val = (1 << operand->size) - 1;
5941   if (arg->token->u.index > max_val)
5942     {
5943       match_out_of_range (arg);
5944       return false;
5945     }
5946   insn_insert_operand (arg->insn, operand, arg->token->u.index);
5947   ++arg->token;
5948   return true;
5949 }
5950 
5951 /* OP_REG_INDEX matcher.  */
5952 
5953 static bool
match_reg_index_operand(struct mips_arg_info * arg,const struct mips_operand * operand)5954 match_reg_index_operand (struct mips_arg_info *arg,
5955 			 const struct mips_operand *operand)
5956 {
5957   unsigned int regno;
5958 
5959   if (arg->token->type != OT_REG_INDEX)
5960     return false;
5961 
5962   if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5963     return false;
5964 
5965   insn_insert_operand (arg->insn, operand, regno);
5966   ++arg->token;
5967   return true;
5968 }
5969 
5970 /* OP_PC matcher.  */
5971 
5972 static bool
match_pc_operand(struct mips_arg_info * arg)5973 match_pc_operand (struct mips_arg_info *arg)
5974 {
5975   if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5976     {
5977       ++arg->token;
5978       return true;
5979     }
5980   return false;
5981 }
5982 
5983 /* OP_REG28 matcher.  */
5984 
5985 static bool
match_reg28_operand(struct mips_arg_info * arg)5986 match_reg28_operand (struct mips_arg_info *arg)
5987 {
5988   unsigned int regno;
5989 
5990   if (arg->token->type == OT_REG
5991       && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5992       && regno == GP)
5993     {
5994       ++arg->token;
5995       return true;
5996     }
5997   return false;
5998 }
5999 
6000 /* OP_NON_ZERO_REG matcher.  */
6001 
6002 static bool
match_non_zero_reg_operand(struct mips_arg_info * arg,const struct mips_operand * operand)6003 match_non_zero_reg_operand (struct mips_arg_info *arg,
6004 			    const struct mips_operand *operand)
6005 {
6006   unsigned int regno;
6007 
6008   if (!match_reg (arg, OP_REG_GP, &regno))
6009     return false;
6010 
6011   if (regno == 0)
6012     {
6013       set_insn_error (arg->argnum, _("the source register must not be $0"));
6014       return false;
6015     }
6016 
6017   arg->last_regno = regno;
6018   insn_insert_operand (arg->insn, operand, regno);
6019   return true;
6020 }
6021 
6022 /* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher.  OTHER_REGNO is the
6023    register that we need to match.  */
6024 
6025 static bool
match_tied_reg_operand(struct mips_arg_info * arg,unsigned int other_regno)6026 match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
6027 {
6028   unsigned int regno;
6029 
6030   return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
6031 }
6032 
6033 /* Try to match a floating-point constant from ARG for LI.S or LI.D.
6034    LENGTH is the length of the value in bytes (4 for float, 8 for double)
6035    and USING_GPRS says whether the destination is a GPR rather than an FPR.
6036 
6037    Return the constant in IMM and OFFSET as follows:
6038 
6039    - If the constant should be loaded via memory, set IMM to O_absent and
6040      OFFSET to the memory address.
6041 
6042    - Otherwise, if the constant should be loaded into two 32-bit registers,
6043      set IMM to the O_constant to load into the high register and OFFSET
6044      to the corresponding value for the low register.
6045 
6046    - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6047 
6048    These constants only appear as the last operand in an instruction,
6049    and every instruction that accepts them in any variant accepts them
6050    in all variants.  This means we don't have to worry about backing out
6051    any changes if the instruction does not match.  We just match
6052    unconditionally and report an error if the constant is invalid.  */
6053 
6054 static bool
match_float_constant(struct mips_arg_info * arg,expressionS * imm,expressionS * offset,int length,bool using_gprs)6055 match_float_constant (struct mips_arg_info *arg, expressionS *imm,
6056 		      expressionS *offset, int length, bool using_gprs)
6057 {
6058   char *p;
6059   segT seg, new_seg;
6060   subsegT subseg;
6061   const char *newname;
6062   unsigned char *data;
6063 
6064   /* Where the constant is placed is based on how the MIPS assembler
6065      does things:
6066 
6067      length == 4 && using_gprs  -- immediate value only
6068      length == 8 && using_gprs  -- .rdata or immediate value
6069      length == 4 && !using_gprs -- .lit4 or immediate value
6070      length == 8 && !using_gprs -- .lit8 or immediate value
6071 
6072      The .lit4 and .lit8 sections are only used if permitted by the
6073      -G argument.  */
6074   if (arg->token->type != OT_FLOAT)
6075     {
6076       set_insn_error (arg->argnum, _("floating-point expression required"));
6077       return false;
6078     }
6079 
6080   gas_assert (arg->token->u.flt.length == length);
6081   data = arg->token->u.flt.data;
6082   ++arg->token;
6083 
6084   /* Handle 32-bit constants for which an immediate value is best.  */
6085   if (length == 4
6086       && (using_gprs
6087 	  || g_switch_value < 4
6088 	  || (data[0] == 0 && data[1] == 0)
6089 	  || (data[2] == 0 && data[3] == 0)))
6090     {
6091       imm->X_op = O_constant;
6092       if (!target_big_endian)
6093 	imm->X_add_number = bfd_getl32 (data);
6094       else
6095 	imm->X_add_number = bfd_getb32 (data);
6096       offset->X_op = O_absent;
6097       return true;
6098     }
6099 
6100   /* Handle 64-bit constants for which an immediate value is best.  */
6101   if (length == 8
6102       && !mips_disable_float_construction
6103       /* Constants can only be constructed in GPRs and copied to FPRs if the
6104 	 GPRs are at least as wide as the FPRs or MTHC1 is available.
6105 	 Unlike most tests for 32-bit floating-point registers this check
6106 	 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6107 	 permit 64-bit moves without MXHC1.
6108 	 Force the constant into memory otherwise.  */
6109       && (using_gprs
6110 	  || GPR_SIZE == 64
6111 	  || ISA_HAS_MXHC1 (mips_opts.isa)
6112 	  || FPR_SIZE == 32)
6113       && ((data[0] == 0 && data[1] == 0)
6114 	  || (data[2] == 0 && data[3] == 0))
6115       && ((data[4] == 0 && data[5] == 0)
6116 	  || (data[6] == 0 && data[7] == 0)))
6117     {
6118       /* The value is simple enough to load with a couple of instructions.
6119 	 If using 32-bit registers, set IMM to the high order 32 bits and
6120 	 OFFSET to the low order 32 bits.  Otherwise, set IMM to the entire
6121 	 64 bit constant.  */
6122       if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
6123 	{
6124 	  imm->X_op = O_constant;
6125 	  offset->X_op = O_constant;
6126 	  if (!target_big_endian)
6127 	    {
6128 	      imm->X_add_number = bfd_getl32 (data + 4);
6129 	      offset->X_add_number = bfd_getl32 (data);
6130 	    }
6131 	  else
6132 	    {
6133 	      imm->X_add_number = bfd_getb32 (data);
6134 	      offset->X_add_number = bfd_getb32 (data + 4);
6135 	    }
6136 	  if (offset->X_add_number == 0)
6137 	    offset->X_op = O_absent;
6138 	}
6139       else
6140 	{
6141 	  imm->X_op = O_constant;
6142 	  if (!target_big_endian)
6143 	    imm->X_add_number = bfd_getl64 (data);
6144 	  else
6145 	    imm->X_add_number = bfd_getb64 (data);
6146 	  offset->X_op = O_absent;
6147 	}
6148       return true;
6149     }
6150 
6151   /* Switch to the right section.  */
6152   seg = now_seg;
6153   subseg = now_subseg;
6154   if (length == 4)
6155     {
6156       gas_assert (!using_gprs && g_switch_value >= 4);
6157       newname = ".lit4";
6158     }
6159   else
6160     {
6161       if (using_gprs || g_switch_value < 8)
6162 	newname = RDATA_SECTION_NAME;
6163       else
6164 	newname = ".lit8";
6165     }
6166 
6167   new_seg = subseg_new (newname, (subsegT) 0);
6168   bfd_set_section_flags (new_seg,
6169 			 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6170   frag_align (length == 4 ? 2 : 3, 0, 0);
6171   if (!startswith (TARGET_OS, "elf"))
6172     record_alignment (new_seg, 4);
6173   else
6174     record_alignment (new_seg, length == 4 ? 2 : 3);
6175   if (seg == now_seg)
6176     as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
6177 
6178   /* Set the argument to the current address in the section.  */
6179   imm->X_op = O_absent;
6180   offset->X_op = O_symbol;
6181   offset->X_add_symbol = symbol_temp_new_now ();
6182   offset->X_add_number = 0;
6183 
6184   /* Put the floating point number into the section.  */
6185   p = frag_more (length);
6186   memcpy (p, data, length);
6187 
6188   /* Switch back to the original section.  */
6189   subseg_set (seg, subseg);
6190   return true;
6191 }
6192 
6193 /* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6194    them.  */
6195 
6196 static bool
match_vu0_suffix_operand(struct mips_arg_info * arg,const struct mips_operand * operand,bool match_p)6197 match_vu0_suffix_operand (struct mips_arg_info *arg,
6198 			  const struct mips_operand *operand,
6199 			  bool match_p)
6200 {
6201   unsigned int uval;
6202 
6203   /* The operand can be an XYZW mask or a single 2-bit channel index
6204      (with X being 0).  */
6205   gas_assert (operand->size == 2 || operand->size == 4);
6206 
6207   /* The suffix can be omitted when it is already part of the opcode.  */
6208   if (arg->token->type != OT_CHANNELS)
6209     return match_p;
6210 
6211   uval = arg->token->u.channels;
6212   if (operand->size == 2)
6213     {
6214       /* Check that a single bit is set and convert it into a 2-bit index.  */
6215       if ((uval & -uval) != uval)
6216 	return false;
6217       uval = 4 - ffs (uval);
6218     }
6219 
6220   if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6221     return false;
6222 
6223   ++arg->token;
6224   if (!match_p)
6225     insn_insert_operand (arg->insn, operand, uval);
6226   return true;
6227 }
6228 
6229 /* Try to match a token from ARG against OPERAND.  Consume the token
6230    and return true on success, otherwise return false.  */
6231 
6232 static bool
match_operand(struct mips_arg_info * arg,const struct mips_operand * operand)6233 match_operand (struct mips_arg_info *arg,
6234 	       const struct mips_operand *operand)
6235 {
6236   switch (operand->type)
6237     {
6238     case OP_INT:
6239       return match_int_operand (arg, operand);
6240 
6241     case OP_MAPPED_INT:
6242       return match_mapped_int_operand (arg, operand);
6243 
6244     case OP_MSB:
6245       return match_msb_operand (arg, operand);
6246 
6247     case OP_REG:
6248     case OP_OPTIONAL_REG:
6249       return match_reg_operand (arg, operand);
6250 
6251     case OP_REG_PAIR:
6252       return match_reg_pair_operand (arg, operand);
6253 
6254     case OP_PCREL:
6255       return match_pcrel_operand (arg);
6256 
6257     case OP_PERF_REG:
6258       return match_perf_reg_operand (arg, operand);
6259 
6260     case OP_ADDIUSP_INT:
6261       return match_addiusp_operand (arg, operand);
6262 
6263     case OP_CLO_CLZ_DEST:
6264       return match_clo_clz_dest_operand (arg, operand);
6265 
6266     case OP_LWM_SWM_LIST:
6267       return match_lwm_swm_list_operand (arg, operand);
6268 
6269     case OP_ENTRY_EXIT_LIST:
6270       return match_entry_exit_operand (arg, operand);
6271 
6272     case OP_SAVE_RESTORE_LIST:
6273       return match_save_restore_list_operand (arg);
6274 
6275     case OP_MDMX_IMM_REG:
6276       return match_mdmx_imm_reg_operand (arg, operand);
6277 
6278     case OP_REPEAT_DEST_REG:
6279       return match_tied_reg_operand (arg, arg->dest_regno);
6280 
6281     case OP_REPEAT_PREV_REG:
6282       return match_tied_reg_operand (arg, arg->last_regno);
6283 
6284     case OP_PC:
6285       return match_pc_operand (arg);
6286 
6287     case OP_REG28:
6288       return match_reg28_operand (arg);
6289 
6290     case OP_VU0_SUFFIX:
6291       return match_vu0_suffix_operand (arg, operand, false);
6292 
6293     case OP_VU0_MATCH_SUFFIX:
6294       return match_vu0_suffix_operand (arg, operand, true);
6295 
6296     case OP_IMM_INDEX:
6297       return match_imm_index_operand (arg, operand);
6298 
6299     case OP_REG_INDEX:
6300       return match_reg_index_operand (arg, operand);
6301 
6302     case OP_SAME_RS_RT:
6303       return match_same_rs_rt_operand (arg, operand);
6304 
6305     case OP_CHECK_PREV:
6306       return match_check_prev_operand (arg, operand);
6307 
6308     case OP_NON_ZERO_REG:
6309       return match_non_zero_reg_operand (arg, operand);
6310     }
6311   abort ();
6312 }
6313 
6314 /* ARG is the state after successfully matching an instruction.
6315    Issue any queued-up warnings.  */
6316 
6317 static void
check_completed_insn(struct mips_arg_info * arg)6318 check_completed_insn (struct mips_arg_info *arg)
6319 {
6320   if (arg->seen_at)
6321     {
6322       if (AT == ATREG)
6323 	as_warn (_("used $at without \".set noat\""));
6324       else
6325 	as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
6326     }
6327 }
6328 
6329 /* Return true if modifying general-purpose register REG needs a delay.  */
6330 
6331 static bool
reg_needs_delay(unsigned int reg)6332 reg_needs_delay (unsigned int reg)
6333 {
6334   unsigned long prev_pinfo;
6335 
6336   prev_pinfo = history[0].insn_mo->pinfo;
6337   if (!mips_opts.noreorder
6338       && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
6339 	  || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
6340       && (gpr_write_mask (&history[0]) & (1 << reg)))
6341     return true;
6342 
6343   return false;
6344 }
6345 
6346 /* Classify an instruction according to the FIX_VR4120_* enumeration.
6347    Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6348    by VR4120 errata.  */
6349 
6350 static unsigned int
classify_vr4120_insn(const char * name)6351 classify_vr4120_insn (const char *name)
6352 {
6353   if (startswith (name, "macc"))
6354     return FIX_VR4120_MACC;
6355   if (startswith (name, "dmacc"))
6356     return FIX_VR4120_DMACC;
6357   if (startswith (name, "mult"))
6358     return FIX_VR4120_MULT;
6359   if (startswith (name, "dmult"))
6360     return FIX_VR4120_DMULT;
6361   if (strstr (name, "div"))
6362     return FIX_VR4120_DIV;
6363   if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6364     return FIX_VR4120_MTHILO;
6365   return NUM_FIX_VR4120_CLASSES;
6366 }
6367 
6368 #define INSN_ERET	0x42000018
6369 #define INSN_DERET	0x4200001f
6370 #define INSN_DMULT	0x1c
6371 #define INSN_DMULTU	0x1d
6372 
6373 /* Return the number of instructions that must separate INSN1 and INSN2,
6374    where INSN1 is the earlier instruction.  Return the worst-case value
6375    for any INSN2 if INSN2 is null.  */
6376 
6377 static unsigned int
insns_between(const struct mips_cl_insn * insn1,const struct mips_cl_insn * insn2)6378 insns_between (const struct mips_cl_insn *insn1,
6379 	       const struct mips_cl_insn *insn2)
6380 {
6381   unsigned long pinfo1, pinfo2;
6382   unsigned int mask;
6383 
6384   /* If INFO2 is null, pessimistically assume that all flags are set for
6385      the second instruction.  */
6386   pinfo1 = insn1->insn_mo->pinfo;
6387   pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
6388 
6389   /* For most targets, write-after-read dependencies on the HI and LO
6390      registers must be separated by at least two instructions.  */
6391   if (!hilo_interlocks)
6392     {
6393       if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6394 	return 2;
6395       if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6396 	return 2;
6397     }
6398 
6399   /* If we're working around r7000 errata, there must be two instructions
6400      between an mfhi or mflo and any instruction that uses the result.  */
6401   if (mips_7000_hilo_fix
6402       && !mips_opts.micromips
6403       && MF_HILO_INSN (pinfo1)
6404       && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
6405     return 2;
6406 
6407   /* If we're working around 24K errata, one instruction is required
6408      if an ERET or DERET is followed by a branch instruction.  */
6409   if (mips_fix_24k && !mips_opts.micromips)
6410     {
6411       if (insn1->insn_opcode == INSN_ERET
6412 	  || insn1->insn_opcode == INSN_DERET)
6413 	{
6414 	  if (insn2 == NULL
6415 	      || insn2->insn_opcode == INSN_ERET
6416 	      || insn2->insn_opcode == INSN_DERET
6417 	      || delayed_branch_p (insn2))
6418 	    return 1;
6419 	}
6420     }
6421 
6422   /* If we're working around PMC RM7000 errata, there must be three
6423      nops between a dmult and a load instruction.  */
6424   if (mips_fix_rm7000 && !mips_opts.micromips)
6425     {
6426       if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6427 	  || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6428 	{
6429 	  if (pinfo2 & INSN_LOAD_MEMORY)
6430 	   return 3;
6431 	}
6432     }
6433 
6434   /* If working around VR4120 errata, check for combinations that need
6435      a single intervening instruction.  */
6436   if (mips_fix_vr4120 && !mips_opts.micromips)
6437     {
6438       unsigned int class1, class2;
6439 
6440       class1 = classify_vr4120_insn (insn1->insn_mo->name);
6441       if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
6442 	{
6443 	  if (insn2 == NULL)
6444 	    return 1;
6445 	  class2 = classify_vr4120_insn (insn2->insn_mo->name);
6446 	  if (vr4120_conflicts[class1] & (1 << class2))
6447 	    return 1;
6448 	}
6449     }
6450 
6451   if (!HAVE_CODE_COMPRESSION)
6452     {
6453       /* Check for GPR or coprocessor load delays.  All such delays
6454 	 are on the RT register.  */
6455       /* Itbl support may require additional care here.  */
6456       if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
6457 	  || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
6458 	{
6459 	  if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
6460 	    return 1;
6461 	}
6462 
6463       /* Check for generic coprocessor hazards.
6464 
6465 	 This case is not handled very well.  There is no special
6466 	 knowledge of CP0 handling, and the coprocessors other than
6467 	 the floating point unit are not distinguished at all.  */
6468       /* Itbl support may require additional care here. FIXME!
6469 	 Need to modify this to include knowledge about
6470 	 user specified delays!  */
6471       else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
6472 	       || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6473 	{
6474 	  /* Handle cases where INSN1 writes to a known general coprocessor
6475 	     register.  There must be a one instruction delay before INSN2
6476 	     if INSN2 reads that register, otherwise no delay is needed.  */
6477 	  mask = fpr_write_mask (insn1);
6478 	  if (mask != 0)
6479 	    {
6480 	      if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
6481 		return 1;
6482 	    }
6483 	  else
6484 	    {
6485 	      /* Read-after-write dependencies on the control registers
6486 		 require a two-instruction gap.  */
6487 	      if ((pinfo1 & INSN_WRITE_COND_CODE)
6488 		  && (pinfo2 & INSN_READ_COND_CODE))
6489 		return 2;
6490 
6491 	      /* We don't know exactly what INSN1 does.  If INSN2 is
6492 		 also a coprocessor instruction, assume there must be
6493 		 a one instruction gap.  */
6494 	      if (pinfo2 & INSN_COP)
6495 		return 1;
6496 	    }
6497 	}
6498 
6499       /* Check for read-after-write dependencies on the coprocessor
6500 	 control registers in cases where INSN1 does not need a general
6501 	 coprocessor delay.  This means that INSN1 is a floating point
6502 	 comparison instruction.  */
6503       /* Itbl support may require additional care here.  */
6504       else if (!cop_interlocks
6505 	       && (pinfo1 & INSN_WRITE_COND_CODE)
6506 	       && (pinfo2 & INSN_READ_COND_CODE))
6507 	return 1;
6508     }
6509 
6510   /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6511      CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6512      and pause.  */
6513   if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6514       && ((pinfo2 & INSN_NO_DELAY_SLOT)
6515 	  || (insn2 && delayed_branch_p (insn2))))
6516     return 1;
6517 
6518   return 0;
6519 }
6520 
6521 /* Return the number of nops that would be needed to work around the
6522    VR4130 mflo/mfhi errata if instruction INSN immediately followed
6523    the MAX_VR4130_NOPS instructions described by HIST.  Ignore hazards
6524    that are contained within the first IGNORE instructions of HIST.  */
6525 
6526 static int
nops_for_vr4130(int ignore,const struct mips_cl_insn * hist,const struct mips_cl_insn * insn)6527 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
6528 		 const struct mips_cl_insn *insn)
6529 {
6530   int i, j;
6531   unsigned int mask;
6532 
6533   /* Check if the instruction writes to HI or LO.  MTHI and MTLO
6534      are not affected by the errata.  */
6535   if (insn != 0
6536       && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6537 	  || strcmp (insn->insn_mo->name, "mtlo") == 0
6538 	  || strcmp (insn->insn_mo->name, "mthi") == 0))
6539     return 0;
6540 
6541   /* Search for the first MFLO or MFHI.  */
6542   for (i = 0; i < MAX_VR4130_NOPS; i++)
6543     if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
6544       {
6545 	/* Extract the destination register.  */
6546 	mask = gpr_write_mask (&hist[i]);
6547 
6548 	/* No nops are needed if INSN reads that register.  */
6549 	if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
6550 	  return 0;
6551 
6552 	/* ...or if any of the intervening instructions do.  */
6553 	for (j = 0; j < i; j++)
6554 	  if (gpr_read_mask (&hist[j]) & mask)
6555 	    return 0;
6556 
6557 	if (i >= ignore)
6558 	  return MAX_VR4130_NOPS - i;
6559       }
6560   return 0;
6561 }
6562 
6563 #define BASE_REG_EQ(INSN1, INSN2)	\
6564   ((((INSN1) >> OP_SH_RS) & OP_MASK_RS)	\
6565       == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6566 
6567 /* Return the minimum alignment for this store instruction.  */
6568 
6569 static int
fix_24k_align_to(const struct mips_opcode * mo)6570 fix_24k_align_to (const struct mips_opcode *mo)
6571 {
6572   if (strcmp (mo->name, "sh") == 0)
6573     return 2;
6574 
6575   if (strcmp (mo->name, "swc1") == 0
6576       || strcmp (mo->name, "swc2") == 0
6577       || strcmp (mo->name, "sw") == 0
6578       || strcmp (mo->name, "sc") == 0
6579       || strcmp (mo->name, "s.s") == 0)
6580     return 4;
6581 
6582   if (strcmp (mo->name, "sdc1") == 0
6583       || strcmp (mo->name, "sdc2") == 0
6584       || strcmp (mo->name, "s.d") == 0)
6585     return 8;
6586 
6587   /* sb, swl, swr */
6588   return 1;
6589 }
6590 
6591 struct fix_24k_store_info
6592   {
6593     /* Immediate offset, if any, for this store instruction.  */
6594     short off;
6595     /* Alignment required by this store instruction.  */
6596     int align_to;
6597     /* True for register offsets.  */
6598     int register_offset;
6599   };
6600 
6601 /* Comparison function used by qsort.  */
6602 
6603 static int
fix_24k_sort(const void * a,const void * b)6604 fix_24k_sort (const void *a, const void *b)
6605 {
6606   const struct fix_24k_store_info *pos1 = a;
6607   const struct fix_24k_store_info *pos2 = b;
6608 
6609   return (pos1->off - pos2->off);
6610 }
6611 
6612 /* INSN is a store instruction.  Try to record the store information
6613    in STINFO.  Return false if the information isn't known.  */
6614 
6615 static bool
fix_24k_record_store_info(struct fix_24k_store_info * stinfo,const struct mips_cl_insn * insn)6616 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
6617 			   const struct mips_cl_insn *insn)
6618 {
6619   /* The instruction must have a known offset.  */
6620   if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6621     return false;
6622 
6623   stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6624   stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6625   return true;
6626 }
6627 
6628 /* Return the number of nops that would be needed to work around the 24k
6629    "lost data on stores during refill" errata if instruction INSN
6630    immediately followed the 2 instructions described by HIST.
6631    Ignore hazards that are contained within the first IGNORE
6632    instructions of HIST.
6633 
6634    Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6635    for the data cache refills and store data. The following describes
6636    the scenario where the store data could be lost.
6637 
6638    * A data cache miss, due to either a load or a store, causing fill
6639      data to be supplied by the memory subsystem
6640    * The first three doublewords of fill data are returned and written
6641      into the cache
6642    * A sequence of four stores occurs in consecutive cycles around the
6643      final doubleword of the fill:
6644    * Store A
6645    * Store B
6646    * Store C
6647    * Zero, One or more instructions
6648    * Store D
6649 
6650    The four stores A-D must be to different doublewords of the line that
6651    is being filled. The fourth instruction in the sequence above permits
6652    the fill of the final doubleword to be transferred from the FSB into
6653    the cache. In the sequence above, the stores may be either integer
6654    (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6655    swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6656    different doublewords on the line. If the floating point unit is
6657    running in 1:2 mode, it is not possible to create the sequence above
6658    using only floating point store instructions.
6659 
6660    In this case, the cache line being filled is incorrectly marked
6661    invalid, thereby losing the data from any store to the line that
6662    occurs between the original miss and the completion of the five
6663    cycle sequence shown above.
6664 
6665    The workarounds are:
6666 
6667    * Run the data cache in write-through mode.
6668    * Insert a non-store instruction between
6669      Store A and Store B or Store B and Store C.  */
6670 
6671 static int
nops_for_24k(int ignore,const struct mips_cl_insn * hist,const struct mips_cl_insn * insn)6672 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
6673 	      const struct mips_cl_insn *insn)
6674 {
6675   struct fix_24k_store_info pos[3];
6676   int align, i, base_offset;
6677 
6678   if (ignore >= 2)
6679     return 0;
6680 
6681   /* If the previous instruction wasn't a store, there's nothing to
6682      worry about.  */
6683   if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6684     return 0;
6685 
6686   /* If the instructions after the previous one are unknown, we have
6687      to assume the worst.  */
6688   if (!insn)
6689     return 1;
6690 
6691   /* Check whether we are dealing with three consecutive stores.  */
6692   if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6693       || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6694     return 0;
6695 
6696   /* If we don't know the relationship between the store addresses,
6697      assume the worst.  */
6698   if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
6699       || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6700     return 1;
6701 
6702   if (!fix_24k_record_store_info (&pos[0], insn)
6703       || !fix_24k_record_store_info (&pos[1], &hist[0])
6704       || !fix_24k_record_store_info (&pos[2], &hist[1]))
6705     return 1;
6706 
6707   qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6708 
6709   /* Pick a value of ALIGN and X such that all offsets are adjusted by
6710      X bytes and such that the base register + X is known to be aligned
6711      to align bytes.  */
6712 
6713   if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6714     align = 8;
6715   else
6716     {
6717       align = pos[0].align_to;
6718       base_offset = pos[0].off;
6719       for (i = 1; i < 3; i++)
6720 	if (align < pos[i].align_to)
6721 	  {
6722 	    align = pos[i].align_to;
6723 	    base_offset = pos[i].off;
6724 	  }
6725       for (i = 0; i < 3; i++)
6726 	pos[i].off -= base_offset;
6727     }
6728 
6729   pos[0].off &= ~align + 1;
6730   pos[1].off &= ~align + 1;
6731   pos[2].off &= ~align + 1;
6732 
6733   /* If any two stores write to the same chunk, they also write to the
6734      same doubleword.  The offsets are still sorted at this point.  */
6735   if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6736     return 0;
6737 
6738   /* A range of at least 9 bytes is needed for the stores to be in
6739      non-overlapping doublewords.  */
6740   if (pos[2].off - pos[0].off <= 8)
6741     return 0;
6742 
6743   if (pos[2].off - pos[1].off >= 24
6744       || pos[1].off - pos[0].off >= 24
6745       || pos[2].off - pos[0].off >= 32)
6746     return 0;
6747 
6748   return 1;
6749 }
6750 
6751 /* Return the number of nops that would be needed if instruction INSN
6752    immediately followed the MAX_NOPS instructions given by HIST,
6753    where HIST[0] is the most recent instruction.  Ignore hazards
6754    between INSN and the first IGNORE instructions in HIST.
6755 
6756    If INSN is null, return the worse-case number of nops for any
6757    instruction.  */
6758 
6759 static int
nops_for_insn(int ignore,const struct mips_cl_insn * hist,const struct mips_cl_insn * insn)6760 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
6761 	       const struct mips_cl_insn *insn)
6762 {
6763   int i, nops, tmp_nops;
6764 
6765   nops = 0;
6766   for (i = ignore; i < MAX_DELAY_NOPS; i++)
6767     {
6768       tmp_nops = insns_between (hist + i, insn) - i;
6769       if (tmp_nops > nops)
6770 	nops = tmp_nops;
6771     }
6772 
6773   if (mips_fix_vr4130 && !mips_opts.micromips)
6774     {
6775       tmp_nops = nops_for_vr4130 (ignore, hist, insn);
6776       if (tmp_nops > nops)
6777 	nops = tmp_nops;
6778     }
6779 
6780   if (mips_fix_24k && !mips_opts.micromips)
6781     {
6782       tmp_nops = nops_for_24k (ignore, hist, insn);
6783       if (tmp_nops > nops)
6784 	nops = tmp_nops;
6785     }
6786 
6787   return nops;
6788 }
6789 
6790 /* The variable arguments provide NUM_INSNS extra instructions that
6791    might be added to HIST.  Return the largest number of nops that
6792    would be needed after the extended sequence, ignoring hazards
6793    in the first IGNORE instructions.  */
6794 
6795 static int
nops_for_sequence(int num_insns,int ignore,const struct mips_cl_insn * hist,...)6796 nops_for_sequence (int num_insns, int ignore,
6797 		   const struct mips_cl_insn *hist, ...)
6798 {
6799   va_list args;
6800   struct mips_cl_insn buffer[MAX_NOPS];
6801   struct mips_cl_insn *cursor;
6802   int nops;
6803 
6804   va_start (args, hist);
6805   cursor = buffer + num_insns;
6806   memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
6807   while (cursor > buffer)
6808     *--cursor = *va_arg (args, const struct mips_cl_insn *);
6809 
6810   nops = nops_for_insn (ignore, buffer, NULL);
6811   va_end (args);
6812   return nops;
6813 }
6814 
6815 /* Like nops_for_insn, but if INSN is a branch, take into account the
6816    worst-case delay for the branch target.  */
6817 
6818 static int
nops_for_insn_or_target(int ignore,const struct mips_cl_insn * hist,const struct mips_cl_insn * insn)6819 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
6820 			 const struct mips_cl_insn *insn)
6821 {
6822   int nops, tmp_nops;
6823 
6824   nops = nops_for_insn (ignore, hist, insn);
6825   if (delayed_branch_p (insn))
6826     {
6827       tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
6828 				    hist, insn, get_delay_slot_nop (insn));
6829       if (tmp_nops > nops)
6830 	nops = tmp_nops;
6831     }
6832   else if (compact_branch_p (insn))
6833     {
6834       tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
6835       if (tmp_nops > nops)
6836 	nops = tmp_nops;
6837     }
6838   return nops;
6839 }
6840 
6841 /* Fix NOP issue: Replace nops by "or at,at,zero".  */
6842 
6843 static void
fix_loongson2f_nop(struct mips_cl_insn * ip)6844 fix_loongson2f_nop (struct mips_cl_insn * ip)
6845 {
6846   gas_assert (!HAVE_CODE_COMPRESSION);
6847   if (strcmp (ip->insn_mo->name, "nop") == 0)
6848     ip->insn_opcode = LOONGSON2F_NOP_INSN;
6849 }
6850 
6851 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6852                    jr target pc &= 'hffff_ffff_cfff_ffff.  */
6853 
6854 static void
fix_loongson2f_jump(struct mips_cl_insn * ip)6855 fix_loongson2f_jump (struct mips_cl_insn * ip)
6856 {
6857   gas_assert (!HAVE_CODE_COMPRESSION);
6858   if (strcmp (ip->insn_mo->name, "j") == 0
6859       || strcmp (ip->insn_mo->name, "jr") == 0
6860       || strcmp (ip->insn_mo->name, "jalr") == 0)
6861     {
6862       int sreg;
6863       expressionS ep;
6864 
6865       if (! mips_opts.at)
6866         return;
6867 
6868       sreg = EXTRACT_OPERAND (0, RS, *ip);
6869       if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6870         return;
6871 
6872       ep.X_op = O_constant;
6873       ep.X_add_number = 0xcfff0000;
6874       macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6875       ep.X_add_number = 0xffff;
6876       macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6877       macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6878     }
6879 }
6880 
6881 static void
fix_loongson2f(struct mips_cl_insn * ip)6882 fix_loongson2f (struct mips_cl_insn * ip)
6883 {
6884   if (mips_fix_loongson2f_nop)
6885     fix_loongson2f_nop (ip);
6886 
6887   if (mips_fix_loongson2f_jump)
6888     fix_loongson2f_jump (ip);
6889 }
6890 
6891 static bool
has_label_name(const char * arr[],size_t len,const char * s)6892 has_label_name (const char *arr[], size_t len ,const char *s)
6893 {
6894   unsigned long i;
6895   for (i = 0; i < len; i++)
6896     {
6897       if (!arr[i])
6898 	return false;
6899       if (streq (arr[i], s))
6900 	return true;
6901     }
6902   return false;
6903 }
6904 
6905 /* Fix loongson3 llsc errata: Insert sync before ll/lld.  */
6906 
6907 static void
fix_loongson3_llsc(struct mips_cl_insn * ip)6908 fix_loongson3_llsc (struct mips_cl_insn * ip)
6909 {
6910   gas_assert (!HAVE_CODE_COMPRESSION);
6911 
6912   /* If is an local label and the insn is not sync,
6913      look forward that whether an branch between ll/sc jump to here
6914      if so, insert a sync.  */
6915   if (seg_info (now_seg)->label_list
6916       && S_IS_LOCAL (seg_info (now_seg)->label_list->label)
6917       && (strcmp (ip->insn_mo->name, "sync") != 0))
6918     {
6919       unsigned long i;
6920       valueT label_value;
6921       const char *label_names[MAX_LABELS_SAME];
6922       const char *label_name;
6923 
6924       label_name = S_GET_NAME (seg_info (now_seg)->label_list->label);
6925       label_names[0] = label_name;
6926       struct insn_label_list *llist = seg_info (now_seg)->label_list;
6927       label_value = S_GET_VALUE (llist->label);
6928 
6929       for (i = 1; i < MAX_LABELS_SAME; i++)
6930 	{
6931 	  llist = llist->next;
6932 	  if (!llist)
6933 	    break;
6934 	  if (S_GET_VALUE (llist->label) == label_value)
6935 	    label_names[i] = S_GET_NAME (llist->label);
6936 	  else
6937 	    break;
6938 	}
6939       for (; i < MAX_LABELS_SAME; i++)
6940 	label_names[i] = NULL;
6941 
6942       unsigned long lookback = ARRAY_SIZE (history);
6943       for (i = 0; i < lookback; i++)
6944 	{
6945 	  if (streq (history[i].insn_mo->name, "ll")
6946 	      || streq (history[i].insn_mo->name, "lld"))
6947 	    break;
6948 
6949 	  if (streq (history[i].insn_mo->name, "sc")
6950 	      || streq (history[i].insn_mo->name, "scd"))
6951 	    {
6952 	      unsigned long j;
6953 
6954 	      for (j = i + 1; j < lookback; j++)
6955 		{
6956 		  if (streq (history[i].insn_mo->name, "ll")
6957 		      || streq (history[i].insn_mo->name, "lld"))
6958 		    break;
6959 
6960 		  if (delayed_branch_p (&history[j]))
6961 		    {
6962 		      if (has_label_name (label_names,
6963 					  MAX_LABELS_SAME,
6964 					  history[j].target))
6965 			{
6966 			  add_fixed_insn (&sync_insn);
6967 			  insert_into_history (0, 1, &sync_insn);
6968 			  i = lookback;
6969 			  break;
6970 			}
6971 		    }
6972 		}
6973 	    }
6974 	}
6975     }
6976   /* If we find a sc, we look forward to look for an branch insn,
6977      and see whether it jump back and out of ll/sc.  */
6978   else if (streq (ip->insn_mo->name, "sc") || streq (ip->insn_mo->name, "scd"))
6979     {
6980       unsigned long lookback = ARRAY_SIZE (history) - 1;
6981       unsigned long i;
6982 
6983       for (i = 0; i < lookback; i++)
6984 	{
6985 	  if (streq (history[i].insn_mo->name, "ll")
6986 	      || streq (history[i].insn_mo->name, "lld"))
6987 	    break;
6988 
6989 	  if (delayed_branch_p (&history[i]))
6990 	    {
6991 	      unsigned long j;
6992 
6993 	      for (j = i + 1; j < lookback; j++)
6994 		{
6995 		  if (streq (history[j].insn_mo->name, "ll")
6996 		      || streq (history[i].insn_mo->name, "lld"))
6997 		    break;
6998 		}
6999 
7000 	      for (; j < lookback; j++)
7001 		{
7002 		  if (history[j].label[0] != '\0'
7003 		      && streq (history[j].label, history[i].target)
7004 		      && strcmp (history[j+1].insn_mo->name, "sync") != 0)
7005 		    {
7006 		      add_fixed_insn (&sync_insn);
7007 		      insert_into_history (++j, 1, &sync_insn);
7008 		    }
7009 		}
7010 	    }
7011 	}
7012     }
7013 
7014   /* Skip if there is a sync before ll/lld.  */
7015   if ((strcmp (ip->insn_mo->name, "ll") == 0
7016        || strcmp (ip->insn_mo->name, "lld") == 0)
7017       && (strcmp (history[0].insn_mo->name, "sync") != 0))
7018     {
7019       add_fixed_insn (&sync_insn);
7020       insert_into_history (0, 1, &sync_insn);
7021     }
7022 }
7023 
7024 /* IP is a branch that has a delay slot, and we need to fill it
7025    automatically.   Return true if we can do that by swapping IP
7026    with the previous instruction.
7027    ADDRESS_EXPR is an operand of the instruction to be used with
7028    RELOC_TYPE.  */
7029 
7030 static bool
can_swap_branch_p(struct mips_cl_insn * ip,expressionS * address_expr,bfd_reloc_code_real_type * reloc_type)7031 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
7032 		   bfd_reloc_code_real_type *reloc_type)
7033 {
7034   unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
7035   unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
7036   unsigned int fpr_read, prev_fpr_write;
7037 
7038   /* -O2 and above is required for this optimization.  */
7039   if (mips_optimize < 2)
7040     return false;
7041 
7042   /* If we have seen .set volatile or .set nomove, don't optimize.  */
7043   if (mips_opts.nomove)
7044     return false;
7045 
7046   /* We can't swap if the previous instruction's position is fixed.  */
7047   if (history[0].fixed_p)
7048     return false;
7049 
7050   /* If the previous previous insn was in a .set noreorder, we can't
7051      swap.  Actually, the MIPS assembler will swap in this situation.
7052      However, gcc configured -with-gnu-as will generate code like
7053 
7054 	.set	noreorder
7055 	lw	$4,XXX
7056 	.set	reorder
7057 	INSN
7058 	bne	$4,$0,foo
7059 
7060      in which we can not swap the bne and INSN.  If gcc is not configured
7061      -with-gnu-as, it does not output the .set pseudo-ops.  */
7062   if (history[1].noreorder_p)
7063     return false;
7064 
7065   /* If the previous instruction had a fixup in mips16 mode, we can not swap.
7066      This means that the previous instruction was a 4-byte one anyhow.  */
7067   if (mips_opts.mips16 && history[0].fixp[0])
7068     return false;
7069 
7070   /* If the branch is itself the target of a branch, we can not swap.
7071      We cheat on this; all we check for is whether there is a label on
7072      this instruction.  If there are any branches to anything other than
7073      a label, users must use .set noreorder.  */
7074   if (seg_info (now_seg)->label_list)
7075     return false;
7076 
7077   /* If the previous instruction is in a variant frag other than this
7078      branch's one, we cannot do the swap.  This does not apply to
7079      MIPS16 code, which uses variant frags for different purposes.  */
7080   if (!mips_opts.mips16
7081       && history[0].frag
7082       && history[0].frag->fr_type == rs_machine_dependent)
7083     return false;
7084 
7085   /* We do not swap with instructions that cannot architecturally
7086      be placed in a branch delay slot, such as SYNC or ERET.  We
7087      also refrain from swapping with a trap instruction, since it
7088      complicates trap handlers to have the trap instruction be in
7089      a delay slot.  */
7090   prev_pinfo = history[0].insn_mo->pinfo;
7091   if (prev_pinfo & INSN_NO_DELAY_SLOT)
7092     return false;
7093 
7094   /* Check for conflicts between the branch and the instructions
7095      before the candidate delay slot.  */
7096   if (nops_for_insn (0, history + 1, ip) > 0)
7097     return false;
7098 
7099   /* Check for conflicts between the swapped sequence and the
7100      target of the branch.  */
7101   if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
7102     return false;
7103 
7104   /* If the branch reads a register that the previous
7105      instruction sets, we can not swap.  */
7106   gpr_read = gpr_read_mask (ip);
7107   prev_gpr_write = gpr_write_mask (&history[0]);
7108   if (gpr_read & prev_gpr_write)
7109     return false;
7110 
7111   fpr_read = fpr_read_mask (ip);
7112   prev_fpr_write = fpr_write_mask (&history[0]);
7113   if (fpr_read & prev_fpr_write)
7114     return false;
7115 
7116   /* If the branch writes a register that the previous
7117      instruction sets, we can not swap.  */
7118   gpr_write = gpr_write_mask (ip);
7119   if (gpr_write & prev_gpr_write)
7120     return false;
7121 
7122   /* If the branch writes a register that the previous
7123      instruction reads, we can not swap.  */
7124   prev_gpr_read = gpr_read_mask (&history[0]);
7125   if (gpr_write & prev_gpr_read)
7126     return false;
7127 
7128   /* If one instruction sets a condition code and the
7129      other one uses a condition code, we can not swap.  */
7130   pinfo = ip->insn_mo->pinfo;
7131   if ((pinfo & INSN_READ_COND_CODE)
7132       && (prev_pinfo & INSN_WRITE_COND_CODE))
7133     return false;
7134   if ((pinfo & INSN_WRITE_COND_CODE)
7135       && (prev_pinfo & INSN_READ_COND_CODE))
7136     return false;
7137 
7138   /* If the previous instruction uses the PC, we can not swap.  */
7139   prev_pinfo2 = history[0].insn_mo->pinfo2;
7140   if (prev_pinfo2 & INSN2_READ_PC)
7141     return false;
7142 
7143   /* If the previous instruction has an incorrect size for a fixed
7144      branch delay slot in microMIPS mode, we cannot swap.  */
7145   pinfo2 = ip->insn_mo->pinfo2;
7146   if (mips_opts.micromips
7147       && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
7148       && insn_length (history) != 2)
7149     return false;
7150   if (mips_opts.micromips
7151       && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
7152       && insn_length (history) != 4)
7153     return false;
7154 
7155   /* On the R5900 short loops need to be fixed by inserting a NOP in the
7156      branch delay slot.
7157 
7158      The short loop bug under certain conditions causes loops to execute
7159      only once or twice.  We must ensure that the assembler never
7160      generates loops that satisfy all of the following conditions:
7161 
7162      - a loop consists of less than or equal to six instructions
7163        (including the branch delay slot);
7164      - a loop contains only one conditional branch instruction at the end
7165        of the loop;
7166      - a loop does not contain any other branch or jump instructions;
7167      - a branch delay slot of the loop is not NOP (EE 2.9 or later).
7168 
7169      We need to do this because of a hardware bug in the R5900 chip.  */
7170   if (mips_fix_r5900
7171       /* Check if instruction has a parameter, ignore "j $31". */
7172       && (address_expr != NULL)
7173       /* Parameter must be 16 bit. */
7174       && (*reloc_type == BFD_RELOC_16_PCREL_S2)
7175       /* Branch to same segment. */
7176       && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
7177       /* Branch to same code fragment. */
7178       && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
7179       /* Can only calculate branch offset if value is known. */
7180       && symbol_constant_p (address_expr->X_add_symbol)
7181       /* Check if branch is really conditional. */
7182       && !((ip->insn_opcode & 0xffff0000) == 0x10000000   /* beq $0,$0 */
7183 	|| (ip->insn_opcode & 0xffff0000) == 0x04010000   /* bgez $0 */
7184 	|| (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7185     {
7186       int distance;
7187       /* Check if loop is shorter than or equal to 6 instructions
7188          including branch and delay slot.  */
7189       distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
7190       if (distance <= 20)
7191         {
7192           int i;
7193           int rv;
7194 
7195           rv = false;
7196           /* When the loop includes branches or jumps,
7197              it is not a short loop. */
7198           for (i = 0; i < (distance / 4); i++)
7199             {
7200               if ((history[i].cleared_p)
7201                   || delayed_branch_p (&history[i]))
7202                 {
7203                   rv = true;
7204                   break;
7205                 }
7206             }
7207           if (!rv)
7208             {
7209               /* Insert nop after branch to fix short loop. */
7210               return false;
7211             }
7212         }
7213     }
7214 
7215   return true;
7216 }
7217 
7218 /* Decide how we should add IP to the instruction stream.
7219    ADDRESS_EXPR is an operand of the instruction to be used with
7220    RELOC_TYPE.  */
7221 
7222 static enum append_method
get_append_method(struct mips_cl_insn * ip,expressionS * address_expr,bfd_reloc_code_real_type * reloc_type)7223 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
7224 		   bfd_reloc_code_real_type *reloc_type)
7225 {
7226   /* The relaxed version of a macro sequence must be inherently
7227      hazard-free.  */
7228   if (mips_relax.sequence == 2)
7229     return APPEND_ADD;
7230 
7231   /* We must not dabble with instructions in a ".set noreorder" block.  */
7232   if (mips_opts.noreorder)
7233     return APPEND_ADD;
7234 
7235   /* Otherwise, it's our responsibility to fill branch delay slots.  */
7236   if (delayed_branch_p (ip))
7237     {
7238       if (!branch_likely_p (ip)
7239 	  && can_swap_branch_p (ip, address_expr, reloc_type))
7240 	return APPEND_SWAP;
7241 
7242       if (mips_opts.mips16
7243 	  && ISA_SUPPORTS_MIPS16E
7244 	  && gpr_read_mask (ip) != 0)
7245 	return APPEND_ADD_COMPACT;
7246 
7247       if (mips_opts.micromips
7248 	  && ((ip->insn_opcode & 0xffe0) == 0x4580
7249 	      || (!forced_insn_length
7250 		  && ((ip->insn_opcode & 0xfc00) == 0xcc00
7251 		      || (ip->insn_opcode & 0xdc00) == 0x8c00))
7252 	      || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7253 	      || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7254 	return APPEND_ADD_COMPACT;
7255 
7256       return APPEND_ADD_WITH_NOP;
7257     }
7258 
7259   return APPEND_ADD;
7260 }
7261 
7262 /* IP is an instruction whose opcode we have just changed, END points
7263    to the end of the opcode table processed.  Point IP->insn_mo to the
7264    new opcode's definition.  */
7265 
7266 static void
find_altered_opcode(struct mips_cl_insn * ip,const struct mips_opcode * end)7267 find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
7268 {
7269   const struct mips_opcode *mo;
7270 
7271   for (mo = ip->insn_mo; mo < end; mo++)
7272     if (mo->pinfo != INSN_MACRO
7273 	&& (ip->insn_opcode & mo->mask) == mo->match)
7274       {
7275 	ip->insn_mo = mo;
7276 	return;
7277       }
7278   abort ();
7279 }
7280 
7281 /* IP is a MIPS16 instruction whose opcode we have just changed.
7282    Point IP->insn_mo to the new opcode's definition.  */
7283 
7284 static void
find_altered_mips16_opcode(struct mips_cl_insn * ip)7285 find_altered_mips16_opcode (struct mips_cl_insn *ip)
7286 {
7287   find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7288 }
7289 
7290 /* IP is a microMIPS instruction whose opcode we have just changed.
7291    Point IP->insn_mo to the new opcode's definition.  */
7292 
7293 static void
find_altered_micromips_opcode(struct mips_cl_insn * ip)7294 find_altered_micromips_opcode (struct mips_cl_insn *ip)
7295 {
7296   find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7297 }
7298 
7299 /* For microMIPS macros, we need to generate a local number label
7300    as the target of branches.  */
7301 #define MICROMIPS_LABEL_CHAR		'\037'
7302 static unsigned long micromips_target_label;
7303 static char micromips_target_name[32];
7304 
7305 static char *
micromips_label_name(void)7306 micromips_label_name (void)
7307 {
7308   char *p = micromips_target_name;
7309   char symbol_name_temporary[24];
7310   unsigned long l;
7311   int i;
7312 
7313   if (*p)
7314     return p;
7315 
7316   i = 0;
7317   l = micromips_target_label;
7318 #ifdef LOCAL_LABEL_PREFIX
7319   *p++ = LOCAL_LABEL_PREFIX;
7320 #endif
7321   *p++ = 'L';
7322   *p++ = MICROMIPS_LABEL_CHAR;
7323   do
7324     {
7325       symbol_name_temporary[i++] = l % 10 + '0';
7326       l /= 10;
7327     }
7328   while (l != 0);
7329   while (i > 0)
7330     *p++ = symbol_name_temporary[--i];
7331   *p = '\0';
7332 
7333   return micromips_target_name;
7334 }
7335 
7336 static void
micromips_label_expr(expressionS * label_expr)7337 micromips_label_expr (expressionS *label_expr)
7338 {
7339   label_expr->X_op = O_symbol;
7340   label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7341   label_expr->X_add_number = 0;
7342 }
7343 
7344 static void
micromips_label_inc(void)7345 micromips_label_inc (void)
7346 {
7347   micromips_target_label++;
7348   *micromips_target_name = '\0';
7349 }
7350 
7351 static void
micromips_add_label(void)7352 micromips_add_label (void)
7353 {
7354   symbolS *s;
7355 
7356   s = colon (micromips_label_name ());
7357   micromips_label_inc ();
7358   S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
7359 }
7360 
7361 /* If assembling microMIPS code, then return the microMIPS reloc
7362    corresponding to the requested one if any.  Otherwise return
7363    the reloc unchanged.  */
7364 
7365 static bfd_reloc_code_real_type
micromips_map_reloc(bfd_reloc_code_real_type reloc)7366 micromips_map_reloc (bfd_reloc_code_real_type reloc)
7367 {
7368   static const bfd_reloc_code_real_type relocs[][2] =
7369     {
7370       /* Keep sorted incrementally by the left-hand key.  */
7371       { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7372       { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7373       { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7374       { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7375       { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7376       { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7377       { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7378       { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7379       { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7380       { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7381       { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7382       { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7383       { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7384       { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7385       { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7386       { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7387       { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7388       { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7389       { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7390       { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7391       { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7392       { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7393       { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7394       { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7395       { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7396       { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7397       { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7398     };
7399   bfd_reloc_code_real_type r;
7400   size_t i;
7401 
7402   if (!mips_opts.micromips)
7403     return reloc;
7404   for (i = 0; i < ARRAY_SIZE (relocs); i++)
7405     {
7406       r = relocs[i][0];
7407       if (r > reloc)
7408 	return reloc;
7409       if (r == reloc)
7410 	return relocs[i][1];
7411     }
7412   return reloc;
7413 }
7414 
7415 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7416    Return true on success, storing the resolved value in RESULT.  */
7417 
7418 static bool
calculate_reloc(bfd_reloc_code_real_type reloc,offsetT operand,offsetT * result)7419 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7420 		 offsetT *result)
7421 {
7422   switch (reloc)
7423     {
7424     case BFD_RELOC_MIPS_HIGHEST:
7425     case BFD_RELOC_MICROMIPS_HIGHEST:
7426       *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7427       return true;
7428 
7429     case BFD_RELOC_MIPS_HIGHER:
7430     case BFD_RELOC_MICROMIPS_HIGHER:
7431       *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7432       return true;
7433 
7434     case BFD_RELOC_HI16_S:
7435     case BFD_RELOC_HI16_S_PCREL:
7436     case BFD_RELOC_MICROMIPS_HI16_S:
7437     case BFD_RELOC_MIPS16_HI16_S:
7438       *result = ((operand + 0x8000) >> 16) & 0xffff;
7439       return true;
7440 
7441     case BFD_RELOC_HI16:
7442     case BFD_RELOC_MICROMIPS_HI16:
7443     case BFD_RELOC_MIPS16_HI16:
7444       *result = (operand >> 16) & 0xffff;
7445       return true;
7446 
7447     case BFD_RELOC_LO16:
7448     case BFD_RELOC_LO16_PCREL:
7449     case BFD_RELOC_MICROMIPS_LO16:
7450     case BFD_RELOC_MIPS16_LO16:
7451       *result = operand & 0xffff;
7452       return true;
7453 
7454     case BFD_RELOC_UNUSED:
7455       *result = operand;
7456       return true;
7457 
7458     default:
7459       return false;
7460     }
7461 }
7462 
7463 /* Output an instruction.  IP is the instruction information.
7464    ADDRESS_EXPR is an operand of the instruction to be used with
7465    RELOC_TYPE.  EXPANSIONP is true if the instruction is part of
7466    a macro expansion.  */
7467 
7468 static void
append_insn(struct mips_cl_insn * ip,expressionS * address_expr,bfd_reloc_code_real_type * reloc_type,bool expansionp)7469 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
7470 	     bfd_reloc_code_real_type *reloc_type, bool expansionp)
7471 {
7472   unsigned long prev_pinfo2, pinfo;
7473   bool relaxed_branch = false;
7474   enum append_method method;
7475   bool relax32;
7476   int branch_disp;
7477 
7478   if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
7479     fix_loongson2f (ip);
7480 
7481   ip->target[0] = '\0';
7482   if (offset_expr.X_op == O_symbol)
7483     strncpy (ip->target, S_GET_NAME (offset_expr.X_add_symbol), 15);
7484   ip->label[0] = '\0';
7485   if (seg_info (now_seg)->label_list)
7486     strncpy (ip->label, S_GET_NAME (seg_info (now_seg)->label_list->label), 15);
7487   if (mips_fix_loongson3_llsc && !HAVE_CODE_COMPRESSION)
7488     fix_loongson3_llsc (ip);
7489 
7490   file_ase_mips16 |= mips_opts.mips16;
7491   file_ase_micromips |= mips_opts.micromips;
7492 
7493   prev_pinfo2 = history[0].insn_mo->pinfo2;
7494   pinfo = ip->insn_mo->pinfo;
7495 
7496   /* Don't raise alarm about `nods' frags as they'll fill in the right
7497      kind of nop in relaxation if required.  */
7498   if (mips_opts.micromips
7499       && !expansionp
7500       && !(history[0].frag
7501 	   && history[0].frag->fr_type == rs_machine_dependent
7502 	   && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7503 	   && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
7504       && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7505 	   && micromips_insn_length (ip->insn_mo) != 2)
7506 	  || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7507 	      && micromips_insn_length (ip->insn_mo) != 4)))
7508     as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
7509 	     (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
7510 
7511   if (address_expr == NULL)
7512     ip->complete_p = 1;
7513   else if (reloc_type[0] <= BFD_RELOC_UNUSED
7514 	   && reloc_type[1] == BFD_RELOC_UNUSED
7515 	   && reloc_type[2] == BFD_RELOC_UNUSED
7516 	   && address_expr->X_op == O_constant)
7517     {
7518       switch (*reloc_type)
7519 	{
7520 	case BFD_RELOC_MIPS_JMP:
7521 	  {
7522 	    int shift;
7523 
7524 	    /* Shift is 2, unusually, for microMIPS JALX.  */
7525 	    shift = (mips_opts.micromips
7526 		     && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
7527 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7528 	      as_bad (_("jump to misaligned address (0x%lx)"),
7529 		      (unsigned long) address_expr->X_add_number);
7530 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7531 				& 0x3ffffff);
7532 	    ip->complete_p = 1;
7533 	  }
7534 	  break;
7535 
7536 	case BFD_RELOC_MIPS16_JMP:
7537 	  if ((address_expr->X_add_number & 3) != 0)
7538 	    as_bad (_("jump to misaligned address (0x%lx)"),
7539 	            (unsigned long) address_expr->X_add_number);
7540 	  ip->insn_opcode |=
7541 	    (((address_expr->X_add_number & 0x7c0000) << 3)
7542 	       | ((address_expr->X_add_number & 0xf800000) >> 7)
7543 	       | ((address_expr->X_add_number & 0x3fffc) >> 2));
7544 	  ip->complete_p = 1;
7545 	  break;
7546 
7547 	case BFD_RELOC_16_PCREL_S2:
7548 	  {
7549 	    int shift;
7550 
7551 	    shift = mips_opts.micromips ? 1 : 2;
7552 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7553 	      as_bad (_("branch to misaligned address (0x%lx)"),
7554 		      (unsigned long) address_expr->X_add_number);
7555 	    if (!mips_relax_branch)
7556 	      {
7557 		if ((address_expr->X_add_number + (1 << (shift + 15)))
7558 		    & ~((1 << (shift + 16)) - 1))
7559 		  as_bad (_("branch address range overflow (0x%lx)"),
7560 			  (unsigned long) address_expr->X_add_number);
7561 		ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7562 				    & 0xffff);
7563 	      }
7564 	  }
7565 	  break;
7566 
7567 	case BFD_RELOC_MIPS_21_PCREL_S2:
7568 	  {
7569 	    int shift;
7570 
7571 	    shift = 2;
7572 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7573 	      as_bad (_("branch to misaligned address (0x%lx)"),
7574 		      (unsigned long) address_expr->X_add_number);
7575 	    if ((address_expr->X_add_number + (1 << (shift + 20)))
7576 		& ~((1 << (shift + 21)) - 1))
7577 	      as_bad (_("branch address range overflow (0x%lx)"),
7578 		      (unsigned long) address_expr->X_add_number);
7579 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7580 				& 0x1fffff);
7581 	  }
7582 	  break;
7583 
7584 	case BFD_RELOC_MIPS_26_PCREL_S2:
7585 	  {
7586 	    int shift;
7587 
7588 	    shift = 2;
7589 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7590 	      as_bad (_("branch to misaligned address (0x%lx)"),
7591 		      (unsigned long) address_expr->X_add_number);
7592 	    if ((address_expr->X_add_number + (1 << (shift + 25)))
7593 		& ~((1 << (shift + 26)) - 1))
7594 	      as_bad (_("branch address range overflow (0x%lx)"),
7595 		      (unsigned long) address_expr->X_add_number);
7596 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7597 				& 0x3ffffff);
7598 	  }
7599 	  break;
7600 
7601 	default:
7602 	  {
7603 	    offsetT value;
7604 
7605 	    if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7606 				 &value))
7607 	      {
7608 		ip->insn_opcode |= value & 0xffff;
7609 		ip->complete_p = 1;
7610 	      }
7611 	  }
7612 	  break;
7613 	}
7614     }
7615 
7616   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7617     {
7618       /* There are a lot of optimizations we could do that we don't.
7619 	 In particular, we do not, in general, reorder instructions.
7620 	 If you use gcc with optimization, it will reorder
7621 	 instructions and generally do much more optimization then we
7622 	 do here; repeating all that work in the assembler would only
7623 	 benefit hand written assembly code, and does not seem worth
7624 	 it.  */
7625       int nops = (mips_optimize == 0
7626 		  ? nops_for_insn (0, history, NULL)
7627 		  : nops_for_insn_or_target (0, history, ip));
7628       if (nops > 0)
7629 	{
7630 	  fragS *old_frag;
7631 	  unsigned long old_frag_offset;
7632 	  int i;
7633 
7634 	  old_frag = frag_now;
7635 	  old_frag_offset = frag_now_fix ();
7636 
7637 	  for (i = 0; i < nops; i++)
7638 	    add_fixed_insn (NOP_INSN);
7639 	  insert_into_history (0, nops, NOP_INSN);
7640 
7641 	  if (listing)
7642 	    {
7643 	      listing_prev_line ();
7644 	      /* We may be at the start of a variant frag.  In case we
7645                  are, make sure there is enough space for the frag
7646                  after the frags created by listing_prev_line.  The
7647                  argument to frag_grow here must be at least as large
7648                  as the argument to all other calls to frag_grow in
7649                  this file.  We don't have to worry about being in the
7650                  middle of a variant frag, because the variants insert
7651                  all needed nop instructions themselves.  */
7652 	      frag_grow (40);
7653 	    }
7654 
7655 	  mips_move_text_labels ();
7656 
7657 #ifndef NO_ECOFF_DEBUGGING
7658 	  if (ECOFF_DEBUGGING)
7659 	    ecoff_fix_loc (old_frag, old_frag_offset);
7660 #endif
7661 	}
7662     }
7663   else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7664     {
7665       int nops;
7666 
7667       /* Work out how many nops in prev_nop_frag are needed by IP,
7668 	 ignoring hazards generated by the first prev_nop_frag_since
7669 	 instructions.  */
7670       nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
7671       gas_assert (nops <= prev_nop_frag_holds);
7672 
7673       /* Enforce NOPS as a minimum.  */
7674       if (nops > prev_nop_frag_required)
7675 	prev_nop_frag_required = nops;
7676 
7677       if (prev_nop_frag_holds == prev_nop_frag_required)
7678 	{
7679 	  /* Settle for the current number of nops.  Update the history
7680 	     accordingly (for the benefit of any future .set reorder code).  */
7681 	  prev_nop_frag = NULL;
7682 	  insert_into_history (prev_nop_frag_since,
7683 			       prev_nop_frag_holds, NOP_INSN);
7684 	}
7685       else
7686 	{
7687 	  /* Allow this instruction to replace one of the nops that was
7688 	     tentatively added to prev_nop_frag.  */
7689 	  prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
7690 	  prev_nop_frag_holds--;
7691 	  prev_nop_frag_since++;
7692 	}
7693     }
7694 
7695   method = get_append_method (ip, address_expr, reloc_type);
7696   branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
7697 
7698   dwarf2_emit_insn (0);
7699   /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7700      so "move" the instruction address accordingly.
7701 
7702      Also, it doesn't seem appropriate for the assembler to reorder .loc
7703      entries.  If this instruction is a branch that we are going to swap
7704      with the previous instruction, the two instructions should be
7705      treated as a unit, and the debug information for both instructions
7706      should refer to the start of the branch sequence.  Using the
7707      current position is certainly wrong when swapping a 32-bit branch
7708      and a 16-bit delay slot, since the current position would then be
7709      in the middle of a branch.  */
7710   dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
7711 
7712   relax32 = (mips_relax_branch
7713 	     /* Don't try branch relaxation within .set nomacro, or within
7714 	        .set noat if we use $at for PIC computations.  If it turns
7715 	        out that the branch was out-of-range, we'll get an error.  */
7716 	     && !mips_opts.warn_about_macros
7717 	     && (mips_opts.at || mips_pic == NO_PIC)
7718 	     /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7719 	        as they have no complementing branches.  */
7720 	     && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
7721 
7722   if (!HAVE_CODE_COMPRESSION
7723       && address_expr
7724       && relax32
7725       && *reloc_type == BFD_RELOC_16_PCREL_S2
7726       && delayed_branch_p (ip))
7727     {
7728       relaxed_branch = true;
7729       add_relaxed_insn (ip, (relaxed_branch_length
7730 			     (NULL, NULL,
7731 			      uncond_branch_p (ip) ? -1
7732 			      : branch_likely_p (ip) ? 1
7733 			      : 0)), 4,
7734 			RELAX_BRANCH_ENCODE
7735 			(AT, mips_pic != NO_PIC,
7736 			 uncond_branch_p (ip),
7737 			 branch_likely_p (ip),
7738 			 pinfo & INSN_WRITE_GPR_31,
7739 			 0),
7740 			address_expr->X_add_symbol,
7741 			address_expr->X_add_number);
7742       *reloc_type = BFD_RELOC_UNUSED;
7743     }
7744   else if (mips_opts.micromips
7745 	   && address_expr
7746 	   && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7747 	       || *reloc_type > BFD_RELOC_UNUSED)
7748 	   && (delayed_branch_p (ip) || compact_branch_p (ip))
7749 	   /* Don't try branch relaxation when users specify
7750 	      16-bit/32-bit instructions.  */
7751 	   && !forced_insn_length)
7752     {
7753       bool relax16 = (method != APPEND_ADD_COMPACT
7754 		      && *reloc_type > BFD_RELOC_UNUSED);
7755       int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
7756       int uncond = uncond_branch_p (ip) ? -1 : 0;
7757       int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7758       int nods = method == APPEND_ADD_WITH_NOP;
7759       int al = pinfo & INSN_WRITE_GPR_31;
7760       int length32 = nods ? 8 : 4;
7761 
7762       gas_assert (address_expr != NULL);
7763       gas_assert (!mips_relax.sequence);
7764 
7765       relaxed_branch = true;
7766       if (nods)
7767 	method = APPEND_ADD;
7768       if (relax32)
7769 	length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7770       add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
7771 			RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
7772 						mips_pic != NO_PIC,
7773 						uncond, compact, al, nods,
7774 						relax32, 0, 0),
7775 			address_expr->X_add_symbol,
7776 			address_expr->X_add_number);
7777       *reloc_type = BFD_RELOC_UNUSED;
7778     }
7779   else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
7780     {
7781       bool require_unextended;
7782       bool require_extended;
7783       symbolS *symbol;
7784       offsetT offset;
7785 
7786       if (forced_insn_length != 0)
7787 	{
7788 	  require_unextended = forced_insn_length == 2;
7789 	  require_extended = forced_insn_length == 4;
7790 	}
7791       else
7792 	{
7793 	  require_unextended = (mips_opts.noautoextend
7794 				&& !mips_opcode_32bit_p (ip->insn_mo));
7795 	  require_extended = 0;
7796 	}
7797 
7798       /* We need to set up a variant frag.  */
7799       gas_assert (address_expr != NULL);
7800       /* Pass any `O_symbol' expression unchanged as an `expr_section'
7801          symbol created by `make_expr_symbol' may not get a necessary
7802          external relocation produced.  */
7803       if (address_expr->X_op == O_symbol)
7804 	{
7805 	  symbol = address_expr->X_add_symbol;
7806 	  offset = address_expr->X_add_number;
7807 	}
7808       else
7809 	{
7810 	  symbol = make_expr_symbol (address_expr);
7811 	  symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
7812 	  offset = 0;
7813 	}
7814       add_relaxed_insn (ip, 12, 0,
7815 			RELAX_MIPS16_ENCODE
7816 			(*reloc_type - BFD_RELOC_UNUSED,
7817 			 mips_opts.ase & ASE_MIPS16E2,
7818 			 mips_pic != NO_PIC,
7819 			 HAVE_32BIT_SYMBOLS,
7820 			 mips_opts.warn_about_macros,
7821 			 require_unextended, require_extended,
7822 			 delayed_branch_p (&history[0]),
7823 			 history[0].mips16_absolute_jump_p),
7824 			symbol, offset);
7825     }
7826   else if (mips_opts.mips16 && insn_length (ip) == 2)
7827     {
7828       if (!delayed_branch_p (ip))
7829 	/* Make sure there is enough room to swap this instruction with
7830 	   a following jump instruction.  */
7831 	frag_grow (6);
7832       add_fixed_insn (ip);
7833     }
7834   else
7835     {
7836       if (mips_opts.mips16
7837 	  && mips_opts.noreorder
7838 	  && delayed_branch_p (&history[0]))
7839 	as_warn (_("extended instruction in delay slot"));
7840 
7841       if (mips_relax.sequence)
7842 	{
7843 	  /* If we've reached the end of this frag, turn it into a variant
7844 	     frag and record the information for the instructions we've
7845 	     written so far.  */
7846 	  if (frag_room () < 4)
7847 	    relax_close_frag ();
7848 	  mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
7849 	}
7850 
7851       if (mips_relax.sequence != 2)
7852 	{
7853 	  if (mips_macro_warning.first_insn_sizes[0] == 0)
7854 	    mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7855 	  mips_macro_warning.sizes[0] += insn_length (ip);
7856 	  mips_macro_warning.insns[0]++;
7857 	}
7858       if (mips_relax.sequence != 1)
7859 	{
7860 	  if (mips_macro_warning.first_insn_sizes[1] == 0)
7861 	    mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7862 	  mips_macro_warning.sizes[1] += insn_length (ip);
7863 	  mips_macro_warning.insns[1]++;
7864 	}
7865 
7866       if (mips_opts.mips16)
7867 	{
7868 	  ip->fixed_p = 1;
7869 	  ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7870 	}
7871       add_fixed_insn (ip);
7872     }
7873 
7874   if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
7875     {
7876       bfd_reloc_code_real_type final_type[3];
7877       reloc_howto_type *howto0;
7878       reloc_howto_type *howto;
7879       int i;
7880 
7881       /* Perform any necessary conversion to microMIPS relocations
7882 	 and find out how many relocations there actually are.  */
7883       for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7884 	final_type[i] = micromips_map_reloc (reloc_type[i]);
7885 
7886       /* In a compound relocation, it is the final (outermost)
7887 	 operator that determines the relocated field.  */
7888       howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
7889       if (!howto)
7890 	abort ();
7891 
7892       if (i > 1)
7893 	howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
7894       ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7895 				 bfd_get_reloc_size (howto),
7896 				 address_expr,
7897 				 howto0 && howto0->pc_relative,
7898 				 final_type[0]);
7899       /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'.  */
7900       ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
7901 
7902       /* Tag symbols that have a R_MIPS16_26 relocation against them.  */
7903       if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
7904 	*symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7905 
7906       /* These relocations can have an addend that won't fit in
7907 	 4 octets for 64bit assembly.  */
7908       if (GPR_SIZE == 64
7909 	  && ! howto->partial_inplace
7910 	  && (reloc_type[0] == BFD_RELOC_16
7911 	      || reloc_type[0] == BFD_RELOC_32
7912 	      || reloc_type[0] == BFD_RELOC_MIPS_JMP
7913 	      || reloc_type[0] == BFD_RELOC_GPREL16
7914 	      || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7915 	      || reloc_type[0] == BFD_RELOC_GPREL32
7916 	      || reloc_type[0] == BFD_RELOC_64
7917 	      || reloc_type[0] == BFD_RELOC_CTOR
7918 	      || reloc_type[0] == BFD_RELOC_MIPS_SUB
7919 	      || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7920 	      || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7921 	      || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7922 	      || reloc_type[0] == BFD_RELOC_MIPS_REL16
7923 	      || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7924 	      || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7925 	      || hi16_reloc_p (reloc_type[0])
7926 	      || lo16_reloc_p (reloc_type[0])))
7927 	ip->fixp[0]->fx_no_overflow = 1;
7928 
7929       /* These relocations can have an addend that won't fit in 2 octets.  */
7930       if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7931 	  || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7932 	ip->fixp[0]->fx_no_overflow = 1;
7933 
7934       if (mips_relax.sequence)
7935 	{
7936 	  if (mips_relax.first_fixup == 0)
7937 	    mips_relax.first_fixup = ip->fixp[0];
7938 	}
7939       else if (reloc_needs_lo_p (*reloc_type))
7940 	{
7941 	  struct mips_hi_fixup *hi_fixup;
7942 
7943 	  /* Reuse the last entry if it already has a matching %lo.  */
7944 	  hi_fixup = mips_hi_fixup_list;
7945 	  if (hi_fixup == 0
7946 	      || !fixup_has_matching_lo_p (hi_fixup->fixp))
7947 	    {
7948 	      hi_fixup = XNEW (struct mips_hi_fixup);
7949 	      hi_fixup->next = mips_hi_fixup_list;
7950 	      mips_hi_fixup_list = hi_fixup;
7951 	    }
7952 	  hi_fixup->fixp = ip->fixp[0];
7953 	  hi_fixup->seg = now_seg;
7954 	}
7955 
7956       /* Add fixups for the second and third relocations, if given.
7957 	 Note that the ABI allows the second relocation to be
7958 	 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
7959 	 moment we only use RSS_UNDEF, but we could add support
7960 	 for the others if it ever becomes necessary.  */
7961       for (i = 1; i < 3; i++)
7962 	if (reloc_type[i] != BFD_RELOC_UNUSED)
7963 	  {
7964 	    ip->fixp[i] = fix_new (ip->frag, ip->where,
7965 				   ip->fixp[0]->fx_size, NULL, 0,
7966 				   false, final_type[i]);
7967 
7968 	    /* Use fx_tcbit to mark compound relocs.  */
7969 	    ip->fixp[0]->fx_tcbit = 1;
7970 	    ip->fixp[i]->fx_tcbit = 1;
7971 	  }
7972     }
7973 
7974   /* Update the register mask information.  */
7975   mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7976   mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
7977 
7978   switch (method)
7979     {
7980     case APPEND_ADD:
7981       insert_into_history (0, 1, ip);
7982       break;
7983 
7984     case APPEND_ADD_WITH_NOP:
7985       {
7986 	struct mips_cl_insn *nop;
7987 
7988 	insert_into_history (0, 1, ip);
7989 	nop = get_delay_slot_nop (ip);
7990 	add_fixed_insn (nop);
7991 	insert_into_history (0, 1, nop);
7992 	if (mips_relax.sequence)
7993 	  mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7994       }
7995       break;
7996 
7997     case APPEND_ADD_COMPACT:
7998       /* Convert MIPS16 jr/jalr into a "compact" jump.  */
7999       if (mips_opts.mips16)
8000 	{
8001 	  ip->insn_opcode |= 0x0080;
8002 	  find_altered_mips16_opcode (ip);
8003 	}
8004       /* Convert microMIPS instructions.  */
8005       else if (mips_opts.micromips)
8006 	{
8007 	  /* jr16->jrc */
8008 	  if ((ip->insn_opcode & 0xffe0) == 0x4580)
8009 	    ip->insn_opcode |= 0x0020;
8010 	  /* b16->bc */
8011 	  else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
8012 	    ip->insn_opcode = 0x40e00000;
8013 	  /* beqz16->beqzc, bnez16->bnezc */
8014 	  else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
8015 	    {
8016 	      unsigned long regno;
8017 
8018 	      regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
8019 	      regno &= MICROMIPSOP_MASK_MD;
8020 	      regno = micromips_to_32_reg_d_map[regno];
8021 	      ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
8022 				 | (regno << MICROMIPSOP_SH_RS)
8023 				 | 0x40a00000) ^ 0x00400000;
8024 	    }
8025 	  /* beqz->beqzc, bnez->bnezc */
8026 	  else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
8027 	    ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
8028 			       | ((ip->insn_opcode >> 7) & 0x00400000)
8029 			       | 0x40a00000) ^ 0x00400000;
8030 	  /* beq $0->beqzc, bne $0->bnezc */
8031 	  else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
8032 	    ip->insn_opcode = (((ip->insn_opcode >>
8033 				 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
8034 				& (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
8035 			       | ((ip->insn_opcode >> 7) & 0x00400000)
8036 			       | 0x40a00000) ^ 0x00400000;
8037 	  else
8038 	    abort ();
8039 	  find_altered_micromips_opcode (ip);
8040 	}
8041       else
8042 	abort ();
8043       install_insn (ip);
8044       insert_into_history (0, 1, ip);
8045       break;
8046 
8047     case APPEND_SWAP:
8048       {
8049 	struct mips_cl_insn delay = history[0];
8050 
8051 	if (relaxed_branch || delay.frag != ip->frag)
8052 	  {
8053 	    /* Add the delay slot instruction to the end of the
8054 	       current frag and shrink the fixed part of the
8055 	       original frag.  If the branch occupies the tail of
8056 	       the latter, move it backwards to cover the gap.  */
8057 	    delay.frag->fr_fix -= branch_disp;
8058 	    if (delay.frag == ip->frag)
8059 	      move_insn (ip, ip->frag, ip->where - branch_disp);
8060 	    add_fixed_insn (&delay);
8061 	  }
8062 	else
8063 	  {
8064 	    /* If this is not a relaxed branch and we are in the
8065 	       same frag, then just swap the instructions.  */
8066 	    move_insn (ip, delay.frag, delay.where);
8067 	    move_insn (&delay, ip->frag, ip->where + insn_length (ip));
8068 	  }
8069 	history[0] = *ip;
8070 	delay.fixed_p = 1;
8071 	insert_into_history (0, 1, &delay);
8072       }
8073       break;
8074     }
8075 
8076   /* If we have just completed an unconditional branch, clear the history.  */
8077   if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
8078       || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
8079     {
8080       unsigned int i;
8081 
8082       mips_no_prev_insn ();
8083 
8084       for (i = 0; i < ARRAY_SIZE (history); i++)
8085 	history[i].cleared_p = 1;
8086     }
8087 
8088   /* We need to emit a label at the end of branch-likely macros.  */
8089   if (emit_branch_likely_macro)
8090     {
8091       emit_branch_likely_macro = false;
8092       micromips_add_label ();
8093     }
8094 
8095   /* We just output an insn, so the next one doesn't have a label.  */
8096   mips_clear_insn_labels ();
8097 }
8098 
8099 /* Forget that there was any previous instruction or label.
8100    When BRANCH is true, the branch history is also flushed.  */
8101 
8102 static void
mips_no_prev_insn(void)8103 mips_no_prev_insn (void)
8104 {
8105   prev_nop_frag = NULL;
8106   insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
8107   mips_clear_insn_labels ();
8108 }
8109 
8110 /* This function must be called before we emit something other than
8111    instructions.  It is like mips_no_prev_insn except that it inserts
8112    any NOPS that might be needed by previous instructions.  */
8113 
8114 void
mips_emit_delays(void)8115 mips_emit_delays (void)
8116 {
8117   if (! mips_opts.noreorder)
8118     {
8119       int nops = nops_for_insn (0, history, NULL);
8120       if (nops > 0)
8121 	{
8122 	  while (nops-- > 0)
8123 	    add_fixed_insn (NOP_INSN);
8124 	  mips_move_text_labels ();
8125 	}
8126     }
8127   mips_no_prev_insn ();
8128 }
8129 
8130 /* Start a (possibly nested) noreorder block.  */
8131 
8132 static void
start_noreorder(void)8133 start_noreorder (void)
8134 {
8135   if (mips_opts.noreorder == 0)
8136     {
8137       unsigned int i;
8138       int nops;
8139 
8140       /* None of the instructions before the .set noreorder can be moved.  */
8141       for (i = 0; i < ARRAY_SIZE (history); i++)
8142 	history[i].fixed_p = 1;
8143 
8144       /* Insert any nops that might be needed between the .set noreorder
8145 	 block and the previous instructions.  We will later remove any
8146 	 nops that turn out not to be needed.  */
8147       nops = nops_for_insn (0, history, NULL);
8148       if (nops > 0)
8149 	{
8150 	  if (mips_optimize != 0)
8151 	    {
8152 	      /* Record the frag which holds the nop instructions, so
8153                  that we can remove them if we don't need them.  */
8154 	      frag_grow (nops * NOP_INSN_SIZE);
8155 	      prev_nop_frag = frag_now;
8156 	      prev_nop_frag_holds = nops;
8157 	      prev_nop_frag_required = 0;
8158 	      prev_nop_frag_since = 0;
8159 	    }
8160 
8161 	  for (; nops > 0; --nops)
8162 	    add_fixed_insn (NOP_INSN);
8163 
8164 	  /* Move on to a new frag, so that it is safe to simply
8165 	     decrease the size of prev_nop_frag.  */
8166 	  frag_wane (frag_now);
8167 	  frag_new (0);
8168 	  mips_move_text_labels ();
8169 	}
8170       mips_mark_labels ();
8171       mips_clear_insn_labels ();
8172     }
8173   mips_opts.noreorder++;
8174   mips_any_noreorder = 1;
8175 }
8176 
8177 /* End a nested noreorder block.  */
8178 
8179 static void
end_noreorder(void)8180 end_noreorder (void)
8181 {
8182   mips_opts.noreorder--;
8183   if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
8184     {
8185       /* Commit to inserting prev_nop_frag_required nops and go back to
8186 	 handling nop insertion the .set reorder way.  */
8187       prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
8188 				* NOP_INSN_SIZE);
8189       insert_into_history (prev_nop_frag_since,
8190 			   prev_nop_frag_required, NOP_INSN);
8191       prev_nop_frag = NULL;
8192     }
8193 }
8194 
8195 /* Sign-extend 32-bit mode constants that have bit 31 set and all
8196    higher bits unset.  */
8197 
8198 static void
normalize_constant_expr(expressionS * ex)8199 normalize_constant_expr (expressionS *ex)
8200 {
8201   if (ex->X_op == O_constant
8202       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8203     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8204 			- 0x80000000);
8205 }
8206 
8207 /* Sign-extend 32-bit mode address offsets that have bit 31 set and
8208    all higher bits unset.  */
8209 
8210 static void
normalize_address_expr(expressionS * ex)8211 normalize_address_expr (expressionS *ex)
8212 {
8213   if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8214 	|| (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8215       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8216     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8217 			- 0x80000000);
8218 }
8219 
8220 /* Try to match TOKENS against OPCODE, storing the result in INSN.
8221    Return true if the match was successful.
8222 
8223    OPCODE_EXTRA is a value that should be ORed into the opcode
8224    (used for VU0 channel suffixes, etc.).  MORE_ALTS is true if
8225    there are more alternatives after OPCODE and SOFT_MATCH is
8226    as for mips_arg_info.  */
8227 
8228 static bool
match_insn(struct mips_cl_insn * insn,const struct mips_opcode * opcode,struct mips_operand_token * tokens,unsigned int opcode_extra,bool lax_match,bool complete_p)8229 match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8230 	    struct mips_operand_token *tokens, unsigned int opcode_extra,
8231 	    bool lax_match, bool complete_p)
8232 {
8233   const char *args;
8234   struct mips_arg_info arg;
8235   const struct mips_operand *operand;
8236   char c;
8237 
8238   imm_expr.X_op = O_absent;
8239   offset_expr.X_op = O_absent;
8240   offset_reloc[0] = BFD_RELOC_UNUSED;
8241   offset_reloc[1] = BFD_RELOC_UNUSED;
8242   offset_reloc[2] = BFD_RELOC_UNUSED;
8243 
8244   create_insn (insn, opcode);
8245   /* When no opcode suffix is specified, assume ".xyzw". */
8246   if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8247     insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8248   else
8249     insn->insn_opcode |= opcode_extra;
8250   memset (&arg, 0, sizeof (arg));
8251   arg.insn = insn;
8252   arg.token = tokens;
8253   arg.argnum = 1;
8254   arg.last_regno = ILLEGAL_REG;
8255   arg.dest_regno = ILLEGAL_REG;
8256   arg.lax_match = lax_match;
8257   for (args = opcode->args;; ++args)
8258     {
8259       if (arg.token->type == OT_END)
8260 	{
8261 	  /* Handle unary instructions in which only one operand is given.
8262 	     The source is then the same as the destination.  */
8263 	  if (arg.opnum == 1 && *args == ',')
8264 	    {
8265 	      operand = (mips_opts.micromips
8266 			 ? decode_micromips_operand (args + 1)
8267 			 : decode_mips_operand (args + 1));
8268 	      if (operand && mips_optional_operand_p (operand))
8269 		{
8270 		  arg.token = tokens;
8271 		  arg.argnum = 1;
8272 		  continue;
8273 		}
8274 	    }
8275 
8276 	  /* Treat elided base registers as $0.  */
8277 	  if (strcmp (args, "(b)") == 0)
8278 	    args += 3;
8279 
8280 	  if (args[0] == '+')
8281 	    switch (args[1])
8282 	      {
8283 	      case 'K':
8284 	      case 'N':
8285 		/* The register suffix is optional. */
8286 		args += 2;
8287 		break;
8288 	      }
8289 
8290 	  /* Fail the match if there were too few operands.  */
8291 	  if (*args)
8292 	    return false;
8293 
8294 	  /* Successful match.  */
8295 	  if (!complete_p)
8296 	    return true;
8297 	  clear_insn_error ();
8298 	  if (arg.dest_regno == arg.last_regno
8299 	      && startswith (insn->insn_mo->name, "jalr"))
8300 	    {
8301 	      if (arg.opnum == 2)
8302 		set_insn_error
8303 		  (0, _("source and destination must be different"));
8304 	      else if (arg.last_regno == 31)
8305 		set_insn_error
8306 		  (0, _("a destination register must be supplied"));
8307 	    }
8308 	  else if (arg.last_regno == 31
8309 		   && (startswith (insn->insn_mo->name, "bltzal")
8310 		       || startswith (insn->insn_mo->name, "bgezal")))
8311 	    set_insn_error (0, _("the source register must not be $31"));
8312 	  check_completed_insn (&arg);
8313 	  return true;
8314 	}
8315 
8316       /* Fail the match if the line has too many operands.   */
8317       if (*args == 0)
8318 	return false;
8319 
8320       /* Handle characters that need to match exactly.  */
8321       if (*args == '(' || *args == ')' || *args == ',')
8322 	{
8323 	  if (match_char (&arg, *args))
8324 	    continue;
8325 	  return false;
8326 	}
8327       if (*args == '#')
8328 	{
8329 	  ++args;
8330 	  if (arg.token->type == OT_DOUBLE_CHAR
8331 	      && arg.token->u.ch == *args)
8332 	    {
8333 	      ++arg.token;
8334 	      continue;
8335 	    }
8336 	  return false;
8337 	}
8338 
8339       /* Handle special macro operands.  Work out the properties of
8340 	 other operands.  */
8341       arg.opnum += 1;
8342       switch (*args)
8343 	{
8344 	case '-':
8345 	  switch (args[1])
8346 	    {
8347 	    case 'A':
8348 	      *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8349 	      break;
8350 
8351 	    case 'B':
8352 	      *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8353 	      break;
8354 	    }
8355 	  break;
8356 
8357 	case '+':
8358 	  switch (args[1])
8359 	    {
8360 	    case 'i':
8361 	      *offset_reloc = BFD_RELOC_MIPS_JMP;
8362 	      break;
8363 
8364 	    case '\'':
8365 	      *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8366 	      break;
8367 
8368 	    case '\"':
8369 	      *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8370 	      break;
8371 	    }
8372 	  break;
8373 
8374 	case 'I':
8375 	  if (!match_const_int (&arg, &imm_expr.X_add_number))
8376 	    return false;
8377 	  imm_expr.X_op = O_constant;
8378 	  if (GPR_SIZE == 32)
8379 	    normalize_constant_expr (&imm_expr);
8380 	  continue;
8381 
8382 	case 'A':
8383 	  if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8384 	    {
8385 	      /* Assume that the offset has been elided and that what
8386 		 we saw was a base register.  The match will fail later
8387 		 if that assumption turns out to be wrong.  */
8388 	      offset_expr.X_op = O_constant;
8389 	      offset_expr.X_add_number = 0;
8390 	    }
8391 	  else
8392 	    {
8393 	      if (!match_expression (&arg, &offset_expr, offset_reloc))
8394 		return false;
8395 	      normalize_address_expr (&offset_expr);
8396 	    }
8397 	  continue;
8398 
8399 	case 'F':
8400 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8401 				     8, true))
8402 	    return false;
8403 	  continue;
8404 
8405 	case 'L':
8406 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8407 				     8, false))
8408 	    return false;
8409 	  continue;
8410 
8411 	case 'f':
8412 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8413 				     4, true))
8414 	    return false;
8415 	  continue;
8416 
8417 	case 'l':
8418 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8419 				     4, false))
8420 	    return false;
8421 	  continue;
8422 
8423 	case 'p':
8424 	  *offset_reloc = BFD_RELOC_16_PCREL_S2;
8425 	  break;
8426 
8427 	case 'a':
8428 	  *offset_reloc = BFD_RELOC_MIPS_JMP;
8429 	  break;
8430 
8431 	case 'm':
8432 	  gas_assert (mips_opts.micromips);
8433 	  c = args[1];
8434 	  switch (c)
8435 	    {
8436 	    case 'D':
8437 	    case 'E':
8438 	      if (!forced_insn_length)
8439 		*offset_reloc = (int) BFD_RELOC_UNUSED + c;
8440 	      else if (c == 'D')
8441 		*offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8442 	      else
8443 		*offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8444 	      break;
8445 	    }
8446 	  break;
8447 	}
8448 
8449       operand = (mips_opts.micromips
8450 		 ? decode_micromips_operand (args)
8451 		 : decode_mips_operand (args));
8452       if (!operand)
8453 	abort ();
8454 
8455       /* Skip prefixes.  */
8456       if (*args == '+' || *args == 'm' || *args == '-')
8457 	args++;
8458 
8459       if (mips_optional_operand_p (operand)
8460 	  && args[1] == ','
8461 	  && (arg.token[0].type != OT_REG
8462 	      || arg.token[1].type == OT_END))
8463 	{
8464 	  /* Assume that the register has been elided and is the
8465 	     same as the first operand.  */
8466 	  arg.token = tokens;
8467 	  arg.argnum = 1;
8468 	}
8469 
8470       if (!match_operand (&arg, operand))
8471 	return false;
8472     }
8473 }
8474 
8475 /* Like match_insn, but for MIPS16.  */
8476 
8477 static bool
match_mips16_insn(struct mips_cl_insn * insn,const struct mips_opcode * opcode,struct mips_operand_token * tokens)8478 match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8479 		   struct mips_operand_token *tokens)
8480 {
8481   const char *args;
8482   const struct mips_operand *operand;
8483   const struct mips_operand *ext_operand;
8484   bool pcrel = false;
8485   int required_insn_length;
8486   struct mips_arg_info arg;
8487   int relax_char;
8488 
8489   if (forced_insn_length)
8490     required_insn_length = forced_insn_length;
8491   else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8492     required_insn_length = 2;
8493   else
8494     required_insn_length = 0;
8495 
8496   create_insn (insn, opcode);
8497   imm_expr.X_op = O_absent;
8498   offset_expr.X_op = O_absent;
8499   offset_reloc[0] = BFD_RELOC_UNUSED;
8500   offset_reloc[1] = BFD_RELOC_UNUSED;
8501   offset_reloc[2] = BFD_RELOC_UNUSED;
8502   relax_char = 0;
8503 
8504   memset (&arg, 0, sizeof (arg));
8505   arg.insn = insn;
8506   arg.token = tokens;
8507   arg.argnum = 1;
8508   arg.last_regno = ILLEGAL_REG;
8509   arg.dest_regno = ILLEGAL_REG;
8510   relax_char = 0;
8511   for (args = opcode->args;; ++args)
8512     {
8513       int c;
8514 
8515       if (arg.token->type == OT_END)
8516 	{
8517 	  offsetT value;
8518 
8519 	  /* Handle unary instructions in which only one operand is given.
8520 	     The source is then the same as the destination.  */
8521 	  if (arg.opnum == 1 && *args == ',')
8522 	    {
8523 	      operand = decode_mips16_operand (args[1], false);
8524 	      if (operand && mips_optional_operand_p (operand))
8525 		{
8526 		  arg.token = tokens;
8527 		  arg.argnum = 1;
8528 		  continue;
8529 		}
8530 	    }
8531 
8532 	  /* Fail the match if there were too few operands.  */
8533 	  if (*args)
8534 	    return false;
8535 
8536 	  /* Successful match.  Stuff the immediate value in now, if
8537 	     we can.  */
8538 	  clear_insn_error ();
8539 	  if (opcode->pinfo == INSN_MACRO)
8540 	    {
8541 	      gas_assert (relax_char == 0 || relax_char == 'p');
8542 	      gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8543 	    }
8544 	  else if (relax_char
8545 		   && offset_expr.X_op == O_constant
8546 		   && !pcrel
8547 		   && calculate_reloc (*offset_reloc,
8548 				       offset_expr.X_add_number,
8549 				       &value))
8550 	    {
8551 	      mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
8552 			    required_insn_length, &insn->insn_opcode);
8553 	      offset_expr.X_op = O_absent;
8554 	      *offset_reloc = BFD_RELOC_UNUSED;
8555 	    }
8556 	  else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8557 	    {
8558 	      if (required_insn_length == 2)
8559 		set_insn_error (0, _("invalid unextended operand value"));
8560 	      else if (!mips_opcode_32bit_p (opcode))
8561 		{
8562 		  forced_insn_length = 4;
8563 		  insn->insn_opcode |= MIPS16_EXTEND;
8564 		}
8565 	    }
8566 	  else if (relax_char)
8567 	    *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8568 
8569 	  check_completed_insn (&arg);
8570 	  return true;
8571 	}
8572 
8573       /* Fail the match if the line has too many operands.   */
8574       if (*args == 0)
8575 	return false;
8576 
8577       /* Handle characters that need to match exactly.  */
8578       if (*args == '(' || *args == ')' || *args == ',')
8579 	{
8580 	  if (match_char (&arg, *args))
8581 	    continue;
8582 	  return false;
8583 	}
8584 
8585       arg.opnum += 1;
8586       c = *args;
8587       switch (c)
8588 	{
8589 	case 'p':
8590 	case 'q':
8591 	case 'A':
8592 	case 'B':
8593 	case 'E':
8594 	case 'V':
8595 	case 'u':
8596 	  relax_char = c;
8597 	  break;
8598 
8599 	case 'I':
8600 	  if (!match_const_int (&arg, &imm_expr.X_add_number))
8601 	    return false;
8602 	  imm_expr.X_op = O_constant;
8603 	  if (GPR_SIZE == 32)
8604 	    normalize_constant_expr (&imm_expr);
8605 	  continue;
8606 
8607 	case 'a':
8608 	case 'i':
8609 	  *offset_reloc = BFD_RELOC_MIPS16_JMP;
8610 	  break;
8611 	}
8612 
8613       operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
8614       if (!operand)
8615 	abort ();
8616 
8617       if (operand->type == OP_PCREL)
8618 	pcrel = true;
8619       else
8620 	{
8621 	  ext_operand = decode_mips16_operand (c, true);
8622 	  if (operand != ext_operand)
8623 	    {
8624 	      if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8625 		{
8626 		  offset_expr.X_op = O_constant;
8627 		  offset_expr.X_add_number = 0;
8628 		  relax_char = c;
8629 		  continue;
8630 		}
8631 
8632 	      if (!match_expression (&arg, &offset_expr, offset_reloc))
8633 		return false;
8634 
8635 	      /* '8' is used for SLTI(U) and has traditionally not
8636 		 been allowed to take relocation operators.  */
8637 	      if (offset_reloc[0] != BFD_RELOC_UNUSED
8638 		  && (ext_operand->size != 16 || c == '8'))
8639 		{
8640 		  match_not_constant (&arg);
8641 		  return false;
8642 		}
8643 
8644 	      if (offset_expr.X_op == O_big)
8645 		{
8646 		  match_out_of_range (&arg);
8647 		  return false;
8648 		}
8649 
8650 	      relax_char = c;
8651 	      continue;
8652 	    }
8653 	}
8654 
8655       if (mips_optional_operand_p (operand)
8656 	  && args[1] == ','
8657 	  && (arg.token[0].type != OT_REG
8658 	      || arg.token[1].type == OT_END))
8659 	{
8660 	  /* Assume that the register has been elided and is the
8661 	     same as the first operand.  */
8662 	  arg.token = tokens;
8663 	  arg.argnum = 1;
8664 	}
8665 
8666       if (!match_operand (&arg, operand))
8667 	return false;
8668     }
8669 }
8670 
8671 /* Record that the current instruction is invalid for the current ISA.  */
8672 
8673 static void
match_invalid_for_isa(void)8674 match_invalid_for_isa (void)
8675 {
8676   set_insn_error_ss
8677     (0, _("opcode not supported on this processor: %s (%s)"),
8678      mips_cpu_info_from_arch (mips_opts.arch)->name,
8679      mips_cpu_info_from_isa (mips_opts.isa)->name);
8680 }
8681 
8682 /* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8683    Return true if a definite match or failure was found, storing any match
8684    in INSN.  OPCODE_EXTRA is a value that should be ORed into the opcode
8685    (to handle things like VU0 suffixes).  LAX_MATCH is true if we have already
8686    tried and failed to match under normal conditions and now want to try a
8687    more relaxed match.  */
8688 
8689 static bool
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,bool lax_match)8690 match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8691 	     const struct mips_opcode *past, struct mips_operand_token *tokens,
8692 	     int opcode_extra, bool lax_match)
8693 {
8694   const struct mips_opcode *opcode;
8695   const struct mips_opcode *invalid_delay_slot;
8696   bool seen_valid_for_isa, seen_valid_for_size;
8697 
8698   /* Search for a match, ignoring alternatives that don't satisfy the
8699      current ISA or forced_length.  */
8700   invalid_delay_slot = 0;
8701   seen_valid_for_isa = false;
8702   seen_valid_for_size = false;
8703   opcode = first;
8704   do
8705     {
8706       gas_assert (strcmp (opcode->name, first->name) == 0);
8707       if (is_opcode_valid (opcode))
8708 	{
8709 	  seen_valid_for_isa = true;
8710 	  if (is_size_valid (opcode))
8711 	    {
8712 	      bool delay_slot_ok;
8713 
8714 	      seen_valid_for_size = true;
8715 	      delay_slot_ok = is_delay_slot_valid (opcode);
8716 	      if (match_insn (insn, opcode, tokens, opcode_extra,
8717 			      lax_match, delay_slot_ok))
8718 		{
8719 		  if (!delay_slot_ok)
8720 		    {
8721 		      if (!invalid_delay_slot)
8722 			invalid_delay_slot = opcode;
8723 		    }
8724 		  else
8725 		    return true;
8726 		}
8727 	    }
8728 	}
8729       ++opcode;
8730     }
8731   while (opcode < past && strcmp (opcode->name, first->name) == 0);
8732 
8733   /* If the only matches we found had the wrong length for the delay slot,
8734      pick the first such match.  We'll issue an appropriate warning later.  */
8735   if (invalid_delay_slot)
8736     {
8737       if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8738 		      lax_match, true))
8739 	return true;
8740       abort ();
8741     }
8742 
8743   /* Handle the case where we didn't try to match an instruction because
8744      all the alternatives were incompatible with the current ISA.  */
8745   if (!seen_valid_for_isa)
8746     {
8747       match_invalid_for_isa ();
8748       return true;
8749     }
8750 
8751   /* Handle the case where we didn't try to match an instruction because
8752      all the alternatives were of the wrong size.  */
8753   if (!seen_valid_for_size)
8754     {
8755       if (mips_opts.insn32)
8756 	set_insn_error (0, _("opcode not supported in the `insn32' mode"));
8757       else
8758 	set_insn_error_i
8759 	  (0, _("unrecognized %d-bit version of microMIPS opcode"),
8760 	   8 * forced_insn_length);
8761       return true;
8762     }
8763 
8764   return false;
8765 }
8766 
8767 /* Like match_insns, but for MIPS16.  */
8768 
8769 static bool
match_mips16_insns(struct mips_cl_insn * insn,const struct mips_opcode * first,struct mips_operand_token * tokens)8770 match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8771 		    struct mips_operand_token *tokens)
8772 {
8773   const struct mips_opcode *opcode;
8774   bool seen_valid_for_isa;
8775   bool seen_valid_for_size;
8776 
8777   /* Search for a match, ignoring alternatives that don't satisfy the
8778      current ISA.  There are no separate entries for extended forms so
8779      we deal with forced_length later.  */
8780   seen_valid_for_isa = false;
8781   seen_valid_for_size = false;
8782   opcode = first;
8783   do
8784     {
8785       gas_assert (strcmp (opcode->name, first->name) == 0);
8786       if (is_opcode_valid_16 (opcode))
8787 	{
8788 	  seen_valid_for_isa = true;
8789 	  if (is_size_valid_16 (opcode))
8790 	    {
8791 	      seen_valid_for_size = true;
8792 	      if (match_mips16_insn (insn, opcode, tokens))
8793 		return true;
8794 	    }
8795 	}
8796       ++opcode;
8797     }
8798   while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8799 	 && strcmp (opcode->name, first->name) == 0);
8800 
8801   /* Handle the case where we didn't try to match an instruction because
8802      all the alternatives were incompatible with the current ISA.  */
8803   if (!seen_valid_for_isa)
8804     {
8805       match_invalid_for_isa ();
8806       return true;
8807     }
8808 
8809   /* Handle the case where we didn't try to match an instruction because
8810      all the alternatives were of the wrong size.  */
8811   if (!seen_valid_for_size)
8812     {
8813       if (forced_insn_length == 2)
8814 	set_insn_error
8815 	  (0, _("unrecognized unextended version of MIPS16 opcode"));
8816       else
8817 	set_insn_error
8818 	  (0, _("unrecognized extended version of MIPS16 opcode"));
8819       return true;
8820     }
8821 
8822   return false;
8823 }
8824 
8825 /* Set up global variables for the start of a new macro.  */
8826 
8827 static void
macro_start(void)8828 macro_start (void)
8829 {
8830   memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
8831   memset (&mips_macro_warning.first_insn_sizes, 0,
8832 	  sizeof (mips_macro_warning.first_insn_sizes));
8833   memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
8834   mips_macro_warning.delay_slot_p = (mips_opts.noreorder
8835 				     && delayed_branch_p (&history[0]));
8836   if (history[0].frag
8837       && history[0].frag->fr_type == rs_machine_dependent
8838       && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8839       && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8840     mips_macro_warning.delay_slot_length = 0;
8841   else
8842     switch (history[0].insn_mo->pinfo2
8843 	    & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8844       {
8845       case INSN2_BRANCH_DELAY_32BIT:
8846 	mips_macro_warning.delay_slot_length = 4;
8847 	break;
8848       case INSN2_BRANCH_DELAY_16BIT:
8849 	mips_macro_warning.delay_slot_length = 2;
8850 	break;
8851       default:
8852 	mips_macro_warning.delay_slot_length = 0;
8853 	break;
8854       }
8855   mips_macro_warning.first_frag = NULL;
8856 }
8857 
8858 /* Given that a macro is longer than one instruction or of the wrong size,
8859    return the appropriate warning for it.  Return null if no warning is
8860    needed.  SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8861    RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8862    and RELAX_NOMACRO.  */
8863 
8864 static const char *
macro_warning(relax_substateT subtype)8865 macro_warning (relax_substateT subtype)
8866 {
8867   if (subtype & RELAX_DELAY_SLOT)
8868     return _("macro instruction expanded into multiple instructions"
8869 	     " in a branch delay slot");
8870   else if (subtype & RELAX_NOMACRO)
8871     return _("macro instruction expanded into multiple instructions");
8872   else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8873 		      | RELAX_DELAY_SLOT_SIZE_SECOND))
8874     return ((subtype & RELAX_DELAY_SLOT_16BIT)
8875 	    ? _("macro instruction expanded into a wrong size instruction"
8876 		" in a 16-bit branch delay slot")
8877 	    : _("macro instruction expanded into a wrong size instruction"
8878 		" in a 32-bit branch delay slot"));
8879   else
8880     return 0;
8881 }
8882 
8883 /* Finish up a macro.  Emit warnings as appropriate.  */
8884 
8885 static void
macro_end(void)8886 macro_end (void)
8887 {
8888   /* Relaxation warning flags.  */
8889   relax_substateT subtype = 0;
8890 
8891   /* Check delay slot size requirements.  */
8892   if (mips_macro_warning.delay_slot_length == 2)
8893     subtype |= RELAX_DELAY_SLOT_16BIT;
8894   if (mips_macro_warning.delay_slot_length != 0)
8895     {
8896       if (mips_macro_warning.delay_slot_length
8897 	  != mips_macro_warning.first_insn_sizes[0])
8898 	subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8899       if (mips_macro_warning.delay_slot_length
8900 	  != mips_macro_warning.first_insn_sizes[1])
8901 	subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8902     }
8903 
8904   /* Check instruction count requirements.  */
8905   if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8906     {
8907       if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
8908 	subtype |= RELAX_SECOND_LONGER;
8909       if (mips_opts.warn_about_macros)
8910 	subtype |= RELAX_NOMACRO;
8911       if (mips_macro_warning.delay_slot_p)
8912 	subtype |= RELAX_DELAY_SLOT;
8913     }
8914 
8915   /* If both alternatives fail to fill a delay slot correctly,
8916      emit the warning now.  */
8917   if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8918       && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8919     {
8920       relax_substateT s;
8921       const char *msg;
8922 
8923       s = subtype & (RELAX_DELAY_SLOT_16BIT
8924 		     | RELAX_DELAY_SLOT_SIZE_FIRST
8925 		     | RELAX_DELAY_SLOT_SIZE_SECOND);
8926       msg = macro_warning (s);
8927       if (msg != NULL)
8928 	as_warn ("%s", msg);
8929       subtype &= ~s;
8930     }
8931 
8932   /* If both implementations are longer than 1 instruction, then emit the
8933      warning now.  */
8934   if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8935     {
8936       relax_substateT s;
8937       const char *msg;
8938 
8939       s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8940       msg = macro_warning (s);
8941       if (msg != NULL)
8942 	as_warn ("%s", msg);
8943       subtype &= ~s;
8944     }
8945 
8946   /* If any flags still set, then one implementation might need a warning
8947      and the other either will need one of a different kind or none at all.
8948      Pass any remaining flags over to relaxation.  */
8949   if (mips_macro_warning.first_frag != NULL)
8950     mips_macro_warning.first_frag->fr_subtype |= subtype;
8951 }
8952 
8953 /* Instruction operand formats used in macros that vary between
8954    standard MIPS and microMIPS code.  */
8955 
8956 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
8957 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8958 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8959 static const char * const lui_fmt[2] = { "t,u", "s,u" };
8960 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
8961 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
8962 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8963 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8964 
8965 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
8966 #define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8967 					     : cop12_fmt[mips_opts.micromips])
8968 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
8969 #define LUI_FMT (lui_fmt[mips_opts.micromips])
8970 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
8971 #define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8972 					     : mem12_fmt[mips_opts.micromips])
8973 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
8974 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
8975 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
8976 
8977 /* Read a macro's relocation codes from *ARGS and store them in *R.
8978    The first argument in *ARGS will be either the code for a single
8979    relocation or -1 followed by the three codes that make up a
8980    composite relocation.  */
8981 
8982 static void
macro_read_relocs(va_list * args,bfd_reloc_code_real_type * r)8983 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8984 {
8985   int i, next;
8986 
8987   next = va_arg (*args, int);
8988   if (next >= 0)
8989     r[0] = (bfd_reloc_code_real_type) next;
8990   else
8991     {
8992       for (i = 0; i < 3; i++)
8993 	r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8994       /* This function is only used for 16-bit relocation fields.
8995 	 To make the macro code simpler, treat an unrelocated value
8996 	 in the same way as BFD_RELOC_LO16.  */
8997       if (r[0] == BFD_RELOC_UNUSED)
8998 	r[0] = BFD_RELOC_LO16;
8999     }
9000 }
9001 
9002 /* Build an instruction created by a macro expansion.  This is passed
9003    a pointer to the count of instructions created so far, an
9004    expression, the name of the instruction to build, an operand format
9005    string, and corresponding arguments.  */
9006 
9007 static void
macro_build(expressionS * ep,const char * name,const char * fmt,...)9008 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
9009 {
9010   const struct mips_opcode *mo = NULL;
9011   bfd_reloc_code_real_type r[3];
9012   const struct mips_opcode *amo;
9013   const struct mips_operand *operand;
9014   htab_t hash;
9015   struct mips_cl_insn insn;
9016   va_list args;
9017   unsigned int uval;
9018 
9019   va_start (args, fmt);
9020 
9021   if (mips_opts.mips16)
9022     {
9023       mips16_macro_build (ep, name, fmt, &args);
9024       va_end (args);
9025       return;
9026     }
9027 
9028   r[0] = BFD_RELOC_UNUSED;
9029   r[1] = BFD_RELOC_UNUSED;
9030   r[2] = BFD_RELOC_UNUSED;
9031   hash = mips_opts.micromips ? micromips_op_hash : op_hash;
9032   amo = (struct mips_opcode *) str_hash_find (hash, name);
9033   gas_assert (amo);
9034   gas_assert (strcmp (name, amo->name) == 0);
9035 
9036   do
9037     {
9038       /* Search until we get a match for NAME.  It is assumed here that
9039 	 macros will never generate MDMX, MIPS-3D, or MT instructions.
9040 	 We try to match an instruction that fulfills the branch delay
9041 	 slot instruction length requirement (if any) of the previous
9042 	 instruction.  While doing this we record the first instruction
9043 	 seen that matches all the other conditions and use it anyway
9044 	 if the requirement cannot be met; we will issue an appropriate
9045 	 warning later on.  */
9046       if (strcmp (fmt, amo->args) == 0
9047 	  && amo->pinfo != INSN_MACRO
9048 	  && is_opcode_valid (amo)
9049 	  && is_size_valid (amo))
9050 	{
9051 	  if (is_delay_slot_valid (amo))
9052 	    {
9053 	      mo = amo;
9054 	      break;
9055 	    }
9056 	  else if (!mo)
9057 	    mo = amo;
9058 	}
9059 
9060       ++amo;
9061       gas_assert (amo->name);
9062     }
9063   while (strcmp (name, amo->name) == 0);
9064 
9065   gas_assert (mo);
9066   create_insn (&insn, mo);
9067   for (; *fmt; ++fmt)
9068     {
9069       switch (*fmt)
9070 	{
9071 	case ',':
9072 	case '(':
9073 	case ')':
9074 	case 'z':
9075 	  break;
9076 
9077 	case 'i':
9078 	case 'j':
9079 	  macro_read_relocs (&args, r);
9080 	  gas_assert (*r == BFD_RELOC_GPREL16
9081 		      || *r == BFD_RELOC_MIPS_HIGHER
9082 		      || *r == BFD_RELOC_HI16_S
9083 		      || *r == BFD_RELOC_LO16
9084 		      || *r == BFD_RELOC_MIPS_GOT_OFST
9085 		      || (mips_opts.micromips
9086 			  && (*r == BFD_RELOC_16
9087 			      || *r == BFD_RELOC_MIPS_GOT16
9088 			      || *r == BFD_RELOC_MIPS_CALL16
9089 			      || *r == BFD_RELOC_MIPS_GOT_HI16
9090 			      || *r == BFD_RELOC_MIPS_GOT_LO16
9091 			      || *r == BFD_RELOC_MIPS_CALL_HI16
9092 			      || *r == BFD_RELOC_MIPS_CALL_LO16
9093 			      || *r == BFD_RELOC_MIPS_SUB
9094 			      || *r == BFD_RELOC_MIPS_GOT_PAGE
9095 			      || *r == BFD_RELOC_MIPS_HIGHEST
9096 			      || *r == BFD_RELOC_MIPS_GOT_DISP
9097 			      || *r == BFD_RELOC_MIPS_TLS_GD
9098 			      || *r == BFD_RELOC_MIPS_TLS_LDM
9099 			      || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
9100 			      || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
9101 			      || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
9102 			      || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
9103 			      || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
9104 	  break;
9105 
9106 	case 'o':
9107 	  macro_read_relocs (&args, r);
9108 	  break;
9109 
9110 	case 'u':
9111 	  macro_read_relocs (&args, r);
9112 	  gas_assert (ep != NULL
9113 		      && (ep->X_op == O_constant
9114 			  || (ep->X_op == O_symbol
9115 			      && (*r == BFD_RELOC_MIPS_HIGHEST
9116 				  || *r == BFD_RELOC_HI16_S
9117 				  || *r == BFD_RELOC_HI16
9118 				  || *r == BFD_RELOC_GPREL16
9119 				  || *r == BFD_RELOC_MIPS_GOT_HI16
9120 				  || *r == BFD_RELOC_MIPS_CALL_HI16))));
9121 	  break;
9122 
9123 	case 'p':
9124 	  gas_assert (ep != NULL);
9125 
9126 	  /*
9127 	   * This allows macro() to pass an immediate expression for
9128 	   * creating short branches without creating a symbol.
9129 	   *
9130 	   * We don't allow branch relaxation for these branches, as
9131 	   * they should only appear in ".set nomacro" anyway.
9132 	   */
9133 	  if (ep->X_op == O_constant)
9134 	    {
9135 	      /* For microMIPS we always use relocations for branches.
9136 	         So we should not resolve immediate values.  */
9137 	      gas_assert (!mips_opts.micromips);
9138 
9139 	      if ((ep->X_add_number & 3) != 0)
9140 		as_bad (_("branch to misaligned address (0x%lx)"),
9141 			(unsigned long) ep->X_add_number);
9142 	      if ((ep->X_add_number + 0x20000) & ~0x3ffff)
9143 		as_bad (_("branch address range overflow (0x%lx)"),
9144 			(unsigned long) ep->X_add_number);
9145 	      insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
9146 	      ep = NULL;
9147 	    }
9148 	  else
9149 	    *r = BFD_RELOC_16_PCREL_S2;
9150 	  break;
9151 
9152 	case 'a':
9153 	  gas_assert (ep != NULL);
9154 	  *r = BFD_RELOC_MIPS_JMP;
9155 	  break;
9156 
9157 	default:
9158 	  operand = (mips_opts.micromips
9159 		     ? decode_micromips_operand (fmt)
9160 		     : decode_mips_operand (fmt));
9161 	  if (!operand)
9162 	    abort ();
9163 
9164 	  uval = va_arg (args, int);
9165 	  if (operand->type == OP_CLO_CLZ_DEST)
9166 	    uval |= (uval << 5);
9167 	  insn_insert_operand (&insn, operand, uval);
9168 
9169 	  if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
9170 	    ++fmt;
9171 	  break;
9172 	}
9173     }
9174   va_end (args);
9175   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
9176 
9177   append_insn (&insn, ep, r, true);
9178 }
9179 
9180 static void
mips16_macro_build(expressionS * ep,const char * name,const char * fmt,va_list * args)9181 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
9182 		    va_list *args)
9183 {
9184   struct mips_opcode *mo;
9185   struct mips_cl_insn insn;
9186   const struct mips_operand *operand;
9187   bfd_reloc_code_real_type r[3]
9188     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
9189 
9190   mo = (struct mips_opcode *) str_hash_find (mips16_op_hash, name);
9191   gas_assert (mo);
9192   gas_assert (strcmp (name, mo->name) == 0);
9193 
9194   while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
9195     {
9196       ++mo;
9197       gas_assert (mo->name);
9198       gas_assert (strcmp (name, mo->name) == 0);
9199     }
9200 
9201   create_insn (&insn, mo);
9202   for (; *fmt; ++fmt)
9203     {
9204       int c;
9205 
9206       c = *fmt;
9207       switch (c)
9208 	{
9209 	case ',':
9210 	case '(':
9211 	case ')':
9212 	  break;
9213 
9214 	case '.':
9215 	case 'S':
9216 	case 'P':
9217 	case 'R':
9218 	  break;
9219 
9220 	case '<':
9221 	case '5':
9222 	case 'F':
9223 	case 'H':
9224 	case 'W':
9225 	case 'D':
9226 	case 'j':
9227 	case '8':
9228 	case 'V':
9229 	case 'C':
9230 	case 'U':
9231 	case 'k':
9232 	case 'K':
9233 	case 'p':
9234 	case 'q':
9235 	  {
9236 	    offsetT value;
9237 
9238 	    gas_assert (ep != NULL);
9239 
9240 	    if (ep->X_op != O_constant)
9241 	      *r = (int) BFD_RELOC_UNUSED + c;
9242 	    else if (calculate_reloc (*r, ep->X_add_number, &value))
9243 	      {
9244 		mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
9245 		ep = NULL;
9246 		*r = BFD_RELOC_UNUSED;
9247 	      }
9248 	  }
9249 	  break;
9250 
9251 	default:
9252 	  operand = decode_mips16_operand (c, false);
9253 	  if (!operand)
9254 	    abort ();
9255 
9256 	  insn_insert_operand (&insn, operand, va_arg (*args, int));
9257 	  break;
9258 	}
9259     }
9260 
9261   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
9262 
9263   append_insn (&insn, ep, r, true);
9264 }
9265 
9266 /*
9267  * Generate a "jalr" instruction with a relocation hint to the called
9268  * function.  This occurs in NewABI PIC code.
9269  */
9270 static void
macro_build_jalr(expressionS * ep,int cprestore)9271 macro_build_jalr (expressionS *ep, int cprestore)
9272 {
9273   static const bfd_reloc_code_real_type jalr_relocs[2]
9274     = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9275   bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9276   const char *jalr;
9277   char *f = NULL;
9278 
9279   if (MIPS_JALR_HINT_P (ep))
9280     {
9281       frag_grow (8);
9282       f = frag_more (0);
9283     }
9284   if (mips_opts.micromips)
9285     {
9286       jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9287 	      ? "jalr" : "jalrs");
9288       if (MIPS_JALR_HINT_P (ep)
9289 	  || mips_opts.insn32
9290 	  || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9291 	macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9292       else
9293 	macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9294     }
9295   else
9296     macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
9297   if (MIPS_JALR_HINT_P (ep))
9298     fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, false, jalr_reloc);
9299 }
9300 
9301 /*
9302  * Generate a "lui" instruction.
9303  */
9304 static void
macro_build_lui(expressionS * ep,int regnum)9305 macro_build_lui (expressionS *ep, int regnum)
9306 {
9307   gas_assert (! mips_opts.mips16);
9308 
9309   if (ep->X_op != O_constant)
9310     {
9311       gas_assert (ep->X_op == O_symbol);
9312       /* _gp_disp is a special case, used from s_cpload.
9313 	 __gnu_local_gp is used if mips_no_shared.  */
9314       gas_assert (mips_pic == NO_PIC
9315 	      || (! HAVE_NEWABI
9316 		  && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9317 	      || (! mips_in_shared
9318 		  && strcmp (S_GET_NAME (ep->X_add_symbol),
9319                              "__gnu_local_gp") == 0));
9320     }
9321 
9322   macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
9323 }
9324 
9325 /* Generate a sequence of instructions to do a load or store from a constant
9326    offset off of a base register (breg) into/from a target register (treg),
9327    using AT if necessary.  */
9328 static void
macro_build_ldst_constoffset(expressionS * ep,const char * op,int treg,int breg,int dbl)9329 macro_build_ldst_constoffset (expressionS *ep, const char *op,
9330 			      int treg, int breg, int dbl)
9331 {
9332   gas_assert (ep->X_op == O_constant);
9333 
9334   /* Sign-extending 32-bit constants makes their handling easier.  */
9335   if (!dbl)
9336     normalize_constant_expr (ep);
9337 
9338   /* Right now, this routine can only handle signed 32-bit constants.  */
9339   if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
9340     as_warn (_("operand overflow"));
9341 
9342   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9343     {
9344       /* Signed 16-bit offset will fit in the op.  Easy!  */
9345       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
9346     }
9347   else
9348     {
9349       /* 32-bit offset, need multiple instructions and AT, like:
9350 	   lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
9351 	   addu     $tempreg,$tempreg,$breg
9352            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
9353          to handle the complete offset.  */
9354       macro_build_lui (ep, AT);
9355       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9356       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
9357 
9358       if (!mips_opts.at)
9359 	as_bad (_("macro used $at after \".set noat\""));
9360     }
9361 }
9362 
9363 /*			set_at()
9364  * Generates code to set the $at register to true (one)
9365  * if reg is less than the immediate expression.
9366  */
9367 static void
set_at(int reg,int unsignedp)9368 set_at (int reg, int unsignedp)
9369 {
9370   if (imm_expr.X_add_number >= -0x8000
9371       && imm_expr.X_add_number < 0x8000)
9372     macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9373 		 AT, reg, BFD_RELOC_LO16);
9374   else
9375     {
9376       load_register (AT, &imm_expr, GPR_SIZE == 64);
9377       macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
9378     }
9379 }
9380 
9381 /* Count the leading zeroes by performing a binary chop. This is a
9382    bulky bit of source, but performance is a LOT better for the
9383    majority of values than a simple loop to count the bits:
9384        for (lcnt = 0; (lcnt < 32); lcnt++)
9385          if ((v) & (1 << (31 - lcnt)))
9386            break;
9387   However it is not code size friendly, and the gain will drop a bit
9388   on certain cached systems.
9389 */
9390 #define COUNT_TOP_ZEROES(v)             \
9391   (((v) & ~0xffff) == 0                 \
9392    ? ((v) & ~0xff) == 0                 \
9393      ? ((v) & ~0xf) == 0                \
9394        ? ((v) & ~0x3) == 0              \
9395          ? ((v) & ~0x1) == 0            \
9396            ? !(v)                       \
9397              ? 32                       \
9398              : 31                       \
9399            : 30                         \
9400          : ((v) & ~0x7) == 0            \
9401            ? 29                         \
9402            : 28                         \
9403        : ((v) & ~0x3f) == 0             \
9404          ? ((v) & ~0x1f) == 0           \
9405            ? 27                         \
9406            : 26                         \
9407          : ((v) & ~0x7f) == 0           \
9408            ? 25                         \
9409            : 24                         \
9410      : ((v) & ~0xfff) == 0              \
9411        ? ((v) & ~0x3ff) == 0            \
9412          ? ((v) & ~0x1ff) == 0          \
9413            ? 23                         \
9414            : 22                         \
9415          : ((v) & ~0x7ff) == 0          \
9416            ? 21                         \
9417            : 20                         \
9418        : ((v) & ~0x3fff) == 0           \
9419          ? ((v) & ~0x1fff) == 0         \
9420            ? 19                         \
9421            : 18                         \
9422          : ((v) & ~0x7fff) == 0         \
9423            ? 17                         \
9424            : 16                         \
9425    : ((v) & ~0xffffff) == 0             \
9426      ? ((v) & ~0xfffff) == 0            \
9427        ? ((v) & ~0x3ffff) == 0          \
9428          ? ((v) & ~0x1ffff) == 0        \
9429            ? 15                         \
9430            : 14                         \
9431          : ((v) & ~0x7ffff) == 0        \
9432            ? 13                         \
9433            : 12                         \
9434        : ((v) & ~0x3fffff) == 0         \
9435          ? ((v) & ~0x1fffff) == 0       \
9436            ? 11                         \
9437            : 10                         \
9438          : ((v) & ~0x7fffff) == 0       \
9439            ? 9                          \
9440            : 8                          \
9441      : ((v) & ~0xfffffff) == 0          \
9442        ? ((v) & ~0x3ffffff) == 0        \
9443          ? ((v) & ~0x1ffffff) == 0      \
9444            ? 7                          \
9445            : 6                          \
9446          : ((v) & ~0x7ffffff) == 0      \
9447            ? 5                          \
9448            : 4                          \
9449        : ((v) & ~0x3fffffff) == 0       \
9450          ? ((v) & ~0x1fffffff) == 0     \
9451            ? 3                          \
9452            : 2                          \
9453          : ((v) & ~0x7fffffff) == 0     \
9454            ? 1                          \
9455            : 0)
9456 
9457 /*			load_register()
9458  *  This routine generates the least number of instructions necessary to load
9459  *  an absolute expression value into a register.
9460  */
9461 static void
load_register(int reg,expressionS * ep,int dbl)9462 load_register (int reg, expressionS *ep, int dbl)
9463 {
9464   int freg;
9465   expressionS hi32, lo32;
9466 
9467   if (ep->X_op != O_big)
9468     {
9469       gas_assert (ep->X_op == O_constant);
9470 
9471       /* Sign-extending 32-bit constants makes their handling easier.  */
9472       if (!dbl)
9473 	normalize_constant_expr (ep);
9474 
9475       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
9476 	{
9477 	  /* We can handle 16 bit signed values with an addiu to
9478 	     $zero.  No need to ever use daddiu here, since $zero and
9479 	     the result are always correct in 32 bit mode.  */
9480 	  macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9481 	  return;
9482 	}
9483       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9484 	{
9485 	  /* We can handle 16 bit unsigned values with an ori to
9486              $zero.  */
9487 	  macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9488 	  return;
9489 	}
9490       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
9491 	{
9492 	  /* 32 bit values require an lui.  */
9493 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9494 	  if ((ep->X_add_number & 0xffff) != 0)
9495 	    macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9496 	  return;
9497 	}
9498     }
9499 
9500   /* The value is larger than 32 bits.  */
9501 
9502   if (!dbl || GPR_SIZE == 32)
9503     {
9504       char value[32];
9505 
9506       sprintf_vma (value, ep->X_add_number);
9507       as_bad (_("number (0x%s) larger than 32 bits"), value);
9508       macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9509       return;
9510     }
9511 
9512   if (ep->X_op != O_big)
9513     {
9514       hi32 = *ep;
9515       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9516       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9517       hi32.X_add_number &= 0xffffffff;
9518       lo32 = *ep;
9519       lo32.X_add_number &= 0xffffffff;
9520     }
9521   else
9522     {
9523       gas_assert (ep->X_add_number > 2);
9524       if (ep->X_add_number == 3)
9525 	generic_bignum[3] = 0;
9526       else if (ep->X_add_number > 4)
9527 	as_bad (_("number larger than 64 bits"));
9528       lo32.X_op = O_constant;
9529       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9530       hi32.X_op = O_constant;
9531       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9532     }
9533 
9534   if (hi32.X_add_number == 0)
9535     freg = 0;
9536   else
9537     {
9538       int shift, bit;
9539       unsigned long hi, lo;
9540 
9541       if (hi32.X_add_number == (offsetT) 0xffffffff)
9542 	{
9543 	  if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9544 	    {
9545 	      macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9546 	      return;
9547 	    }
9548 	  if (lo32.X_add_number & 0x80000000)
9549 	    {
9550 	      macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9551 	      if (lo32.X_add_number & 0xffff)
9552 		macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9553 	      return;
9554 	    }
9555 	}
9556 
9557       /* Check for 16bit shifted constant.  We know that hi32 is
9558          non-zero, so start the mask on the first bit of the hi32
9559          value.  */
9560       shift = 17;
9561       do
9562 	{
9563 	  unsigned long himask, lomask;
9564 
9565 	  if (shift < 32)
9566 	    {
9567 	      himask = 0xffff >> (32 - shift);
9568 	      lomask = (0xffffU << shift) & 0xffffffff;
9569 	    }
9570 	  else
9571 	    {
9572 	      himask = 0xffffU << (shift - 32);
9573 	      lomask = 0;
9574 	    }
9575 	  if ((hi32.X_add_number & ~(offsetT) himask) == 0
9576 	      && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9577 	    {
9578 	      expressionS tmp;
9579 
9580 	      tmp.X_op = O_constant;
9581 	      if (shift < 32)
9582 		tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9583 				    | (lo32.X_add_number >> shift));
9584 	      else
9585 		tmp.X_add_number = hi32.X_add_number >> (shift - 32);
9586 	      macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9587 	      macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9588 			   reg, reg, (shift >= 32) ? shift - 32 : shift);
9589 	      return;
9590 	    }
9591 	  ++shift;
9592 	}
9593       while (shift <= (64 - 16));
9594 
9595       /* Find the bit number of the lowest one bit, and store the
9596          shifted value in hi/lo.  */
9597       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9598       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9599       if (lo != 0)
9600 	{
9601 	  bit = 0;
9602 	  while ((lo & 1) == 0)
9603 	    {
9604 	      lo >>= 1;
9605 	      ++bit;
9606 	    }
9607 	  if (bit != 0)
9608 	    {
9609 	      lo |= (hi & ((2UL << (bit - 1)) - 1)) << (32 - bit);
9610 	      hi >>= bit;
9611 	    }
9612 	}
9613       else
9614 	{
9615 	  bit = 32;
9616 	  while ((hi & 1) == 0)
9617 	    {
9618 	      hi >>= 1;
9619 	      ++bit;
9620 	    }
9621 	  lo = hi;
9622 	  hi = 0;
9623 	}
9624 
9625       /* Optimize if the shifted value is a (power of 2) - 1.  */
9626       if ((hi == 0 && ((lo + 1) & lo) == 0)
9627 	  || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
9628 	{
9629 	  shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
9630 	  if (shift != 0)
9631 	    {
9632 	      expressionS tmp;
9633 
9634 	      /* This instruction will set the register to be all
9635                  ones.  */
9636 	      tmp.X_op = O_constant;
9637 	      tmp.X_add_number = (offsetT) -1;
9638 	      macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9639 	      if (bit != 0)
9640 		{
9641 		  bit += shift;
9642 		  macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9643 			       reg, reg, (bit >= 32) ? bit - 32 : bit);
9644 		}
9645 	      macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
9646 			   reg, reg, (shift >= 32) ? shift - 32 : shift);
9647 	      return;
9648 	    }
9649 	}
9650 
9651       /* Sign extend hi32 before calling load_register, because we can
9652          generally get better code when we load a sign extended value.  */
9653       if ((hi32.X_add_number & 0x80000000) != 0)
9654 	hi32.X_add_number |= ~(offsetT) 0xffffffff;
9655       load_register (reg, &hi32, 0);
9656       freg = reg;
9657     }
9658   if ((lo32.X_add_number & 0xffff0000) == 0)
9659     {
9660       if (freg != 0)
9661 	{
9662 	  macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
9663 	  freg = reg;
9664 	}
9665     }
9666   else
9667     {
9668       expressionS mid16;
9669 
9670       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
9671 	{
9672 	  macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9673 	  macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
9674 	  return;
9675 	}
9676 
9677       if (freg != 0)
9678 	{
9679 	  macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
9680 	  freg = reg;
9681 	}
9682       mid16 = lo32;
9683       mid16.X_add_number >>= 16;
9684       macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9685       macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9686       freg = reg;
9687     }
9688   if ((lo32.X_add_number & 0xffff) != 0)
9689     macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9690 }
9691 
9692 static inline void
load_delay_nop(void)9693 load_delay_nop (void)
9694 {
9695   if (!gpr_interlocks)
9696     macro_build (NULL, "nop", "");
9697 }
9698 
9699 /* Load an address into a register.  */
9700 
9701 static void
load_address(int reg,expressionS * ep,int * used_at)9702 load_address (int reg, expressionS *ep, int *used_at)
9703 {
9704   if (ep->X_op != O_constant
9705       && ep->X_op != O_symbol)
9706     {
9707       as_bad (_("expression too complex"));
9708       ep->X_op = O_constant;
9709     }
9710 
9711   if (ep->X_op == O_constant)
9712     {
9713       load_register (reg, ep, HAVE_64BIT_ADDRESSES);
9714       return;
9715     }
9716 
9717   if (mips_pic == NO_PIC)
9718     {
9719       /* If this is a reference to a GP relative symbol, we want
9720 	   addiu	$reg,$gp,<sym>		(BFD_RELOC_GPREL16)
9721 	 Otherwise we want
9722 	   lui		$reg,<sym>		(BFD_RELOC_HI16_S)
9723 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
9724 	 If we have an addend, we always use the latter form.
9725 
9726 	 With 64bit address space and a usable $at we want
9727 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
9728 	   lui		$at,<sym>		(BFD_RELOC_HI16_S)
9729 	   daddiu	$reg,<sym>		(BFD_RELOC_MIPS_HIGHER)
9730 	   daddiu	$at,<sym>		(BFD_RELOC_LO16)
9731 	   dsll32	$reg,0
9732 	   daddu	$reg,$reg,$at
9733 
9734 	 If $at is already in use, we use a path which is suboptimal
9735 	 on superscalar processors.
9736 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
9737 	   daddiu	$reg,<sym>		(BFD_RELOC_MIPS_HIGHER)
9738 	   dsll		$reg,16
9739 	   daddiu	$reg,<sym>		(BFD_RELOC_HI16_S)
9740 	   dsll		$reg,16
9741 	   daddiu	$reg,<sym>		(BFD_RELOC_LO16)
9742 
9743 	 For GP relative symbols in 64bit address space we can use
9744 	 the same sequence as in 32bit address space.  */
9745       if (HAVE_64BIT_SYMBOLS)
9746 	{
9747 	  if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9748 	      && !nopic_need_relax (ep->X_add_symbol, 1))
9749 	    {
9750 	      relax_start (ep->X_add_symbol);
9751 	      macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9752 			   mips_gp_register, BFD_RELOC_GPREL16);
9753 	      relax_switch ();
9754 	    }
9755 
9756 	  if (*used_at == 0 && mips_opts.at)
9757 	    {
9758 	      macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9759 	      macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
9760 	      macro_build (ep, "daddiu", "t,r,j", reg, reg,
9761 			   BFD_RELOC_MIPS_HIGHER);
9762 	      macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
9763 	      macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
9764 	      macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
9765 	      *used_at = 1;
9766 	    }
9767 	  else
9768 	    {
9769 	      macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9770 	      macro_build (ep, "daddiu", "t,r,j", reg, reg,
9771 			   BFD_RELOC_MIPS_HIGHER);
9772 	      macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9773 	      macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
9774 	      macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9775 	      macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
9776 	    }
9777 
9778 	  if (mips_relax.sequence)
9779 	    relax_end ();
9780 	}
9781       else
9782 	{
9783 	  if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9784 	      && !nopic_need_relax (ep->X_add_symbol, 1))
9785 	    {
9786 	      relax_start (ep->X_add_symbol);
9787 	      macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9788 			   mips_gp_register, BFD_RELOC_GPREL16);
9789 	      relax_switch ();
9790 	    }
9791 	  macro_build_lui (ep, reg);
9792 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9793 		       reg, reg, BFD_RELOC_LO16);
9794 	  if (mips_relax.sequence)
9795 	    relax_end ();
9796 	}
9797     }
9798   else if (!mips_big_got)
9799     {
9800       expressionS ex;
9801 
9802       /* If this is a reference to an external symbol, we want
9803 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9804 	 Otherwise we want
9805 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9806 	   nop
9807 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
9808 	 If there is a constant, it must be added in after.
9809 
9810 	 If we have NewABI, we want
9811 	   lw		$reg,<sym+cst>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
9812          unless we're referencing a global symbol with a non-zero
9813          offset, in which case cst must be added separately.  */
9814       if (HAVE_NEWABI)
9815 	{
9816 	  if (ep->X_add_number)
9817 	    {
9818 	      ex.X_add_number = ep->X_add_number;
9819 	      ep->X_add_number = 0;
9820 	      relax_start (ep->X_add_symbol);
9821 	      macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9822 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9823 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9824 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9825 	      ex.X_op = O_constant;
9826 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9827 			   reg, reg, BFD_RELOC_LO16);
9828 	      ep->X_add_number = ex.X_add_number;
9829 	      relax_switch ();
9830 	    }
9831 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9832 		       BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9833 	  if (mips_relax.sequence)
9834 	    relax_end ();
9835 	}
9836       else
9837 	{
9838 	  ex.X_add_number = ep->X_add_number;
9839 	  ep->X_add_number = 0;
9840 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9841 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9842 	  load_delay_nop ();
9843 	  relax_start (ep->X_add_symbol);
9844 	  relax_switch ();
9845 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9846 		       BFD_RELOC_LO16);
9847 	  relax_end ();
9848 
9849 	  if (ex.X_add_number != 0)
9850 	    {
9851 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9852 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9853 	      ex.X_op = O_constant;
9854 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9855 			   reg, reg, BFD_RELOC_LO16);
9856 	    }
9857 	}
9858     }
9859   else if (mips_big_got)
9860     {
9861       expressionS ex;
9862 
9863       /* This is the large GOT case.  If this is a reference to an
9864 	 external symbol, we want
9865 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
9866 	   addu		$reg,$reg,$gp
9867 	   lw		$reg,<sym>($reg)	(BFD_RELOC_MIPS_GOT_LO16)
9868 
9869 	 Otherwise, for a reference to a local symbol in old ABI, we want
9870 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9871 	   nop
9872 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
9873 	 If there is a constant, it must be added in after.
9874 
9875 	 In the NewABI, for local symbols, with or without offsets, we want:
9876 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT_PAGE)
9877 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_MIPS_GOT_OFST)
9878       */
9879       if (HAVE_NEWABI)
9880 	{
9881 	  ex.X_add_number = ep->X_add_number;
9882 	  ep->X_add_number = 0;
9883 	  relax_start (ep->X_add_symbol);
9884 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9885 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9886 		       reg, reg, mips_gp_register);
9887 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9888 		       reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9889 	  if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9890 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9891 	  else if (ex.X_add_number)
9892 	    {
9893 	      ex.X_op = O_constant;
9894 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9895 			   BFD_RELOC_LO16);
9896 	    }
9897 
9898 	  ep->X_add_number = ex.X_add_number;
9899 	  relax_switch ();
9900 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9901 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9902 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9903 		       BFD_RELOC_MIPS_GOT_OFST);
9904 	  relax_end ();
9905 	}
9906       else
9907 	{
9908 	  ex.X_add_number = ep->X_add_number;
9909 	  ep->X_add_number = 0;
9910 	  relax_start (ep->X_add_symbol);
9911 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9912 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9913 		       reg, reg, mips_gp_register);
9914 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9915 		       reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9916 	  relax_switch ();
9917 	  if (reg_needs_delay (mips_gp_register))
9918 	    {
9919 	      /* We need a nop before loading from $gp.  This special
9920 		 check is required because the lui which starts the main
9921 		 instruction stream does not refer to $gp, and so will not
9922 		 insert the nop which may be required.  */
9923 	      macro_build (NULL, "nop", "");
9924 	    }
9925 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9926 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9927 	  load_delay_nop ();
9928 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9929 		       BFD_RELOC_LO16);
9930 	  relax_end ();
9931 
9932 	  if (ex.X_add_number != 0)
9933 	    {
9934 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9935 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9936 	      ex.X_op = O_constant;
9937 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9938 			   BFD_RELOC_LO16);
9939 	    }
9940 	}
9941     }
9942   else
9943     abort ();
9944 
9945   if (!mips_opts.at && *used_at == 1)
9946     as_bad (_("macro used $at after \".set noat\""));
9947 }
9948 
9949 /* Move the contents of register SOURCE into register DEST.  */
9950 
9951 static void
move_register(int dest,int source)9952 move_register (int dest, int source)
9953 {
9954   /* Prefer to use a 16-bit microMIPS instruction unless the previous
9955      instruction specifically requires a 32-bit one.  */
9956   if (mips_opts.micromips
9957       && !mips_opts.insn32
9958       && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9959     macro_build (NULL, "move", "mp,mj", dest, source);
9960   else
9961     macro_build (NULL, "or", "d,v,t", dest, source, 0);
9962 }
9963 
9964 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
9965    LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9966    The two alternatives are:
9967 
9968    Global symbol		Local symbol
9969    -------------		------------
9970    lw DEST,%got(SYMBOL)		lw DEST,%got(SYMBOL + OFFSET)
9971    ...				...
9972    addiu DEST,DEST,OFFSET	addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9973 
9974    load_got_offset emits the first instruction and add_got_offset
9975    emits the second for a 16-bit offset or add_got_offset_hilo emits
9976    a sequence to add a 32-bit offset using a scratch register.  */
9977 
9978 static void
load_got_offset(int dest,expressionS * local)9979 load_got_offset (int dest, expressionS *local)
9980 {
9981   expressionS global;
9982 
9983   global = *local;
9984   global.X_add_number = 0;
9985 
9986   relax_start (local->X_add_symbol);
9987   macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9988 	       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9989   relax_switch ();
9990   macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9991 	       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9992   relax_end ();
9993 }
9994 
9995 static void
add_got_offset(int dest,expressionS * local)9996 add_got_offset (int dest, expressionS *local)
9997 {
9998   expressionS global;
9999 
10000   global.X_op = O_constant;
10001   global.X_op_symbol = NULL;
10002   global.X_add_symbol = NULL;
10003   global.X_add_number = local->X_add_number;
10004 
10005   relax_start (local->X_add_symbol);
10006   macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
10007 	       dest, dest, BFD_RELOC_LO16);
10008   relax_switch ();
10009   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
10010   relax_end ();
10011 }
10012 
10013 static void
add_got_offset_hilo(int dest,expressionS * local,int tmp)10014 add_got_offset_hilo (int dest, expressionS *local, int tmp)
10015 {
10016   expressionS global;
10017   int hold_mips_optimize;
10018 
10019   global.X_op = O_constant;
10020   global.X_op_symbol = NULL;
10021   global.X_add_symbol = NULL;
10022   global.X_add_number = local->X_add_number;
10023 
10024   relax_start (local->X_add_symbol);
10025   load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
10026   relax_switch ();
10027   /* Set mips_optimize around the lui instruction to avoid
10028      inserting an unnecessary nop after the lw.  */
10029   hold_mips_optimize = mips_optimize;
10030   mips_optimize = 2;
10031   macro_build_lui (&global, tmp);
10032   mips_optimize = hold_mips_optimize;
10033   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
10034   relax_end ();
10035 
10036   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
10037 }
10038 
10039 /* Emit a sequence of instructions to emulate a branch likely operation.
10040    BR is an ordinary branch corresponding to one to be emulated.  BRNEG
10041    is its complementing branch with the original condition negated.
10042    CALL is set if the original branch specified the link operation.
10043    EP, FMT, SREG and TREG specify the usual macro_build() parameters.
10044 
10045    Code like this is produced in the noreorder mode:
10046 
10047 	BRNEG	<args>, 1f
10048 	 nop
10049 	b	<sym>
10050 	 delay slot (executed only if branch taken)
10051     1:
10052 
10053    or, if CALL is set:
10054 
10055 	BRNEG	<args>, 1f
10056 	 nop
10057 	bal	<sym>
10058 	 delay slot (executed only if branch taken)
10059     1:
10060 
10061    In the reorder mode the delay slot would be filled with a nop anyway,
10062    so code produced is simply:
10063 
10064 	BR	<args>, <sym>
10065 	 nop
10066 
10067    This function is used when producing code for the microMIPS ASE that
10068    does not implement branch likely instructions in hardware.  */
10069 
10070 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)10071 macro_build_branch_likely (const char *br, const char *brneg,
10072 			   int call, expressionS *ep, const char *fmt,
10073 			   unsigned int sreg, unsigned int treg)
10074 {
10075   int noreorder = mips_opts.noreorder;
10076   expressionS expr1;
10077 
10078   gas_assert (mips_opts.micromips);
10079   start_noreorder ();
10080   if (noreorder)
10081     {
10082       micromips_label_expr (&expr1);
10083       macro_build (&expr1, brneg, fmt, sreg, treg);
10084       macro_build (NULL, "nop", "");
10085       macro_build (ep, call ? "bal" : "b", "p");
10086 
10087       /* Set to true so that append_insn adds a label.  */
10088       emit_branch_likely_macro = true;
10089     }
10090   else
10091     {
10092       macro_build (ep, br, fmt, sreg, treg);
10093       macro_build (NULL, "nop", "");
10094     }
10095   end_noreorder ();
10096 }
10097 
10098 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
10099    the condition code tested.  EP specifies the branch target.  */
10100 
10101 static void
macro_build_branch_ccl(int type,expressionS * ep,unsigned int cc)10102 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
10103 {
10104   const int call = 0;
10105   const char *brneg;
10106   const char *br;
10107 
10108   switch (type)
10109     {
10110     case M_BC1FL:
10111       br = "bc1f";
10112       brneg = "bc1t";
10113       break;
10114     case M_BC1TL:
10115       br = "bc1t";
10116       brneg = "bc1f";
10117       break;
10118     case M_BC2FL:
10119       br = "bc2f";
10120       brneg = "bc2t";
10121       break;
10122     case M_BC2TL:
10123       br = "bc2t";
10124       brneg = "bc2f";
10125       break;
10126     default:
10127       abort ();
10128     }
10129   macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
10130 }
10131 
10132 /* Emit a two-argument branch macro specified by TYPE, using SREG as
10133    the register tested.  EP specifies the branch target.  */
10134 
10135 static void
macro_build_branch_rs(int type,expressionS * ep,unsigned int sreg)10136 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
10137 {
10138   const char *brneg = NULL;
10139   const char *br;
10140   int call = 0;
10141 
10142   switch (type)
10143     {
10144     case M_BGEZ:
10145       br = "bgez";
10146       break;
10147     case M_BGEZL:
10148       br = mips_opts.micromips ? "bgez" : "bgezl";
10149       brneg = "bltz";
10150       break;
10151     case M_BGEZALL:
10152       gas_assert (mips_opts.micromips);
10153       br = mips_opts.insn32 ? "bgezal" : "bgezals";
10154       brneg = "bltz";
10155       call = 1;
10156       break;
10157     case M_BGTZ:
10158       br = "bgtz";
10159       break;
10160     case M_BGTZL:
10161       br = mips_opts.micromips ? "bgtz" : "bgtzl";
10162       brneg = "blez";
10163       break;
10164     case M_BLEZ:
10165       br = "blez";
10166       break;
10167     case M_BLEZL:
10168       br = mips_opts.micromips ? "blez" : "blezl";
10169       brneg = "bgtz";
10170       break;
10171     case M_BLTZ:
10172       br = "bltz";
10173       break;
10174     case M_BLTZL:
10175       br = mips_opts.micromips ? "bltz" : "bltzl";
10176       brneg = "bgez";
10177       break;
10178     case M_BLTZALL:
10179       gas_assert (mips_opts.micromips);
10180       br = mips_opts.insn32 ? "bltzal" : "bltzals";
10181       brneg = "bgez";
10182       call = 1;
10183       break;
10184     default:
10185       abort ();
10186     }
10187   if (mips_opts.micromips && brneg)
10188     macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
10189   else
10190     macro_build (ep, br, "s,p", sreg);
10191 }
10192 
10193 /* Emit a three-argument branch macro specified by TYPE, using SREG and
10194    TREG as the registers tested.  EP specifies the branch target.  */
10195 
10196 static void
macro_build_branch_rsrt(int type,expressionS * ep,unsigned int sreg,unsigned int treg)10197 macro_build_branch_rsrt (int type, expressionS *ep,
10198 			 unsigned int sreg, unsigned int treg)
10199 {
10200   const char *brneg = NULL;
10201   const int call = 0;
10202   const char *br;
10203 
10204   switch (type)
10205     {
10206     case M_BEQ:
10207     case M_BEQ_I:
10208       br = "beq";
10209       break;
10210     case M_BEQL:
10211     case M_BEQL_I:
10212       br = mips_opts.micromips ? "beq" : "beql";
10213       brneg = "bne";
10214       break;
10215     case M_BNE:
10216     case M_BNE_I:
10217       br = "bne";
10218       break;
10219     case M_BNEL:
10220     case M_BNEL_I:
10221       br = mips_opts.micromips ? "bne" : "bnel";
10222       brneg = "beq";
10223       break;
10224     default:
10225       abort ();
10226     }
10227   if (mips_opts.micromips && brneg)
10228     macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10229   else
10230     macro_build (ep, br, "s,t,p", sreg, treg);
10231 }
10232 
10233 /* Return the high part that should be loaded in order to make the low
10234    part of VALUE accessible using an offset of OFFBITS bits.  */
10235 
10236 static offsetT
offset_high_part(offsetT value,unsigned int offbits)10237 offset_high_part (offsetT value, unsigned int offbits)
10238 {
10239   offsetT bias;
10240   addressT low_mask;
10241 
10242   if (offbits == 0)
10243     return value;
10244   bias = 1 << (offbits - 1);
10245   low_mask = bias * 2 - 1;
10246   return (value + bias) & ~low_mask;
10247 }
10248 
10249 /* Return true if the value stored in offset_expr and offset_reloc
10250    fits into a signed offset of OFFBITS bits.  RANGE is the maximum
10251    amount that the caller wants to add without inducing overflow
10252    and ALIGN is the known alignment of the value in bytes.  */
10253 
10254 static bool
small_offset_p(unsigned int range,unsigned int align,unsigned int offbits)10255 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10256 {
10257   if (offbits == 16)
10258     {
10259       /* Accept any relocation operator if overflow isn't a concern.  */
10260       if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10261 	return true;
10262 
10263       /* These relocations are guaranteed not to overflow in correct links.  */
10264       if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10265 	  || gprel16_reloc_p (*offset_reloc))
10266 	return true;
10267     }
10268   if (offset_expr.X_op == O_constant
10269       && offset_high_part (offset_expr.X_add_number, offbits) == 0
10270       && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10271     return true;
10272   return false;
10273 }
10274 
10275 /*
10276  *			Build macros
10277  *   This routine implements the seemingly endless macro or synthesized
10278  * instructions and addressing modes in the mips assembly language. Many
10279  * of these macros are simple and are similar to each other. These could
10280  * probably be handled by some kind of table or grammar approach instead of
10281  * this verbose method. Others are not simple macros but are more like
10282  * optimizing code generation.
10283  *   One interesting optimization is when several store macros appear
10284  * consecutively that would load AT with the upper half of the same address.
10285  * The ensuing load upper instructions are omitted. This implies some kind
10286  * of global optimization. We currently only optimize within a single macro.
10287  *   For many of the load and store macros if the address is specified as a
10288  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10289  * first load register 'at' with zero and use it as the base register. The
10290  * mips assembler simply uses register $zero. Just one tiny optimization
10291  * we're missing.
10292  */
10293 static void
macro(struct mips_cl_insn * ip,char * str)10294 macro (struct mips_cl_insn *ip, char *str)
10295 {
10296   const struct mips_operand_array *operands;
10297   unsigned int breg, i;
10298   unsigned int tempreg;
10299   int mask;
10300   int used_at = 0;
10301   expressionS label_expr;
10302   expressionS expr1;
10303   expressionS *ep;
10304   const char *s;
10305   const char *s2;
10306   const char *fmt;
10307   int likely = 0;
10308   int coproc = 0;
10309   int offbits = 16;
10310   int call = 0;
10311   int jals = 0;
10312   int dbl = 0;
10313   int imm = 0;
10314   int ust = 0;
10315   int lp = 0;
10316   int ll_sc_paired = 0;
10317   bool large_offset;
10318   int off;
10319   int hold_mips_optimize;
10320   unsigned int align;
10321   unsigned int op[MAX_OPERANDS];
10322 
10323   gas_assert (! mips_opts.mips16);
10324 
10325   operands = insn_operands (ip);
10326   for (i = 0; i < MAX_OPERANDS; i++)
10327     if (operands->operand[i])
10328       op[i] = insn_extract_operand (ip, operands->operand[i]);
10329     else
10330       op[i] = -1;
10331 
10332   mask = ip->insn_mo->mask;
10333 
10334   label_expr.X_op = O_constant;
10335   label_expr.X_op_symbol = NULL;
10336   label_expr.X_add_symbol = NULL;
10337   label_expr.X_add_number = 0;
10338 
10339   expr1.X_op = O_constant;
10340   expr1.X_op_symbol = NULL;
10341   expr1.X_add_symbol = NULL;
10342   expr1.X_add_number = 1;
10343   align = 1;
10344 
10345   switch (mask)
10346     {
10347     case M_DABS:
10348       dbl = 1;
10349       /* Fall through.  */
10350     case M_ABS:
10351       /*    bgez    $a0,1f
10352 	    move    v0,$a0
10353 	    sub     v0,$zero,$a0
10354 	 1:
10355        */
10356 
10357       start_noreorder ();
10358 
10359       if (mips_opts.micromips)
10360 	micromips_label_expr (&label_expr);
10361       else
10362 	label_expr.X_add_number = 8;
10363       macro_build (&label_expr, "bgez", "s,p", op[1]);
10364       if (op[0] == op[1])
10365 	macro_build (NULL, "nop", "");
10366       else
10367 	move_register (op[0], op[1]);
10368       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
10369       if (mips_opts.micromips)
10370 	micromips_add_label ();
10371 
10372       end_noreorder ();
10373       break;
10374 
10375     case M_ADD_I:
10376       s = "addi";
10377       s2 = "add";
10378       if (ISA_IS_R6 (mips_opts.isa))
10379 	goto do_addi_i;
10380       else
10381 	goto do_addi;
10382     case M_ADDU_I:
10383       s = "addiu";
10384       s2 = "addu";
10385       goto do_addi;
10386     case M_DADD_I:
10387       dbl = 1;
10388       s = "daddi";
10389       s2 = "dadd";
10390       if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
10391 	goto do_addi;
10392       if (imm_expr.X_add_number >= -0x200
10393 	  && imm_expr.X_add_number < 0x200
10394 	  && !ISA_IS_R6 (mips_opts.isa))
10395 	{
10396 	  macro_build (NULL, s, "t,r,.", op[0], op[1],
10397 		       (int) imm_expr.X_add_number);
10398 	  break;
10399 	}
10400       goto do_addi_i;
10401     case M_DADDU_I:
10402       dbl = 1;
10403       s = "daddiu";
10404       s2 = "daddu";
10405     do_addi:
10406       if (imm_expr.X_add_number >= -0x8000
10407 	  && imm_expr.X_add_number < 0x8000)
10408 	{
10409 	  macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
10410 	  break;
10411 	}
10412     do_addi_i:
10413       used_at = 1;
10414       load_register (AT, &imm_expr, dbl);
10415       macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10416       break;
10417 
10418     case M_AND_I:
10419       s = "andi";
10420       s2 = "and";
10421       goto do_bit;
10422     case M_OR_I:
10423       s = "ori";
10424       s2 = "or";
10425       goto do_bit;
10426     case M_NOR_I:
10427       s = "";
10428       s2 = "nor";
10429       goto do_bit;
10430     case M_XOR_I:
10431       s = "xori";
10432       s2 = "xor";
10433     do_bit:
10434       if (imm_expr.X_add_number >= 0
10435 	  && imm_expr.X_add_number < 0x10000)
10436 	{
10437 	  if (mask != M_NOR_I)
10438 	    macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
10439 	  else
10440 	    {
10441 	      macro_build (&imm_expr, "ori", "t,r,i",
10442 			   op[0], op[1], BFD_RELOC_LO16);
10443 	      macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
10444 	    }
10445 	  break;
10446 	}
10447 
10448       used_at = 1;
10449       load_register (AT, &imm_expr, GPR_SIZE == 64);
10450       macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10451       break;
10452 
10453     case M_BALIGN:
10454       switch (imm_expr.X_add_number)
10455 	{
10456 	case 0:
10457 	  macro_build (NULL, "nop", "");
10458 	  break;
10459 	case 2:
10460 	  macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
10461 	  break;
10462 	case 1:
10463 	case 3:
10464 	  macro_build (NULL, "balign", "t,s,2", op[0], op[1],
10465 		       (int) imm_expr.X_add_number);
10466 	  break;
10467 	default:
10468 	  as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10469 		  (unsigned long) imm_expr.X_add_number);
10470 	  break;
10471 	}
10472       break;
10473 
10474     case M_BC1FL:
10475     case M_BC1TL:
10476     case M_BC2FL:
10477     case M_BC2TL:
10478       gas_assert (mips_opts.micromips);
10479       macro_build_branch_ccl (mask, &offset_expr,
10480 			      EXTRACT_OPERAND (1, BCC, *ip));
10481       break;
10482 
10483     case M_BEQ_I:
10484     case M_BEQL_I:
10485     case M_BNE_I:
10486     case M_BNEL_I:
10487       if (imm_expr.X_add_number == 0)
10488 	op[1] = 0;
10489       else
10490 	{
10491 	  op[1] = AT;
10492 	  used_at = 1;
10493 	  load_register (op[1], &imm_expr, GPR_SIZE == 64);
10494 	}
10495       /* Fall through.  */
10496     case M_BEQL:
10497     case M_BNEL:
10498       macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
10499       break;
10500 
10501     case M_BGEL:
10502       likely = 1;
10503       /* Fall through.  */
10504     case M_BGE:
10505       if (op[1] == 0)
10506 	macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10507       else if (op[0] == 0)
10508 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
10509       else
10510 	{
10511 	  used_at = 1;
10512 	  macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10513 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10514 				   &offset_expr, AT, ZERO);
10515 	}
10516       break;
10517 
10518     case M_BGEZL:
10519     case M_BGEZALL:
10520     case M_BGTZL:
10521     case M_BLEZL:
10522     case M_BLTZL:
10523     case M_BLTZALL:
10524       macro_build_branch_rs (mask, &offset_expr, op[0]);
10525       break;
10526 
10527     case M_BGTL_I:
10528       likely = 1;
10529       /* Fall through.  */
10530     case M_BGT_I:
10531       /* Check for > max integer.  */
10532       if (imm_expr.X_add_number >= GPR_SMAX)
10533 	{
10534 	do_false:
10535 	  /* Result is always false.  */
10536 	  if (! likely)
10537 	    macro_build (NULL, "nop", "");
10538 	  else
10539 	    macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
10540 	  break;
10541 	}
10542       ++imm_expr.X_add_number;
10543       /* Fall through.  */
10544     case M_BGE_I:
10545     case M_BGEL_I:
10546       if (mask == M_BGEL_I)
10547 	likely = 1;
10548       if (imm_expr.X_add_number == 0)
10549 	{
10550 	  macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
10551 				 &offset_expr, op[0]);
10552 	  break;
10553 	}
10554       if (imm_expr.X_add_number == 1)
10555 	{
10556 	  macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
10557 				 &offset_expr, op[0]);
10558 	  break;
10559 	}
10560       if (imm_expr.X_add_number <= GPR_SMIN)
10561 	{
10562 	do_true:
10563 	  /* Result is always true.  */
10564 	  as_warn (_("branch %s is always true"), ip->insn_mo->name);
10565 	  macro_build (&offset_expr, "b", "p");
10566 	  break;
10567 	}
10568       used_at = 1;
10569       set_at (op[0], 0);
10570       macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10571 			       &offset_expr, AT, ZERO);
10572       break;
10573 
10574     case M_BGEUL:
10575       likely = 1;
10576       /* Fall through.  */
10577     case M_BGEU:
10578       if (op[1] == 0)
10579 	goto do_true;
10580       else if (op[0] == 0)
10581 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10582 				 &offset_expr, ZERO, op[1]);
10583       else
10584 	{
10585 	  used_at = 1;
10586 	  macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10587 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10588 				   &offset_expr, AT, ZERO);
10589 	}
10590       break;
10591 
10592     case M_BGTUL_I:
10593       likely = 1;
10594       /* Fall through.  */
10595     case M_BGTU_I:
10596       if (op[0] == 0
10597 	  || (GPR_SIZE == 32
10598 	      && imm_expr.X_add_number == -1))
10599 	goto do_false;
10600       ++imm_expr.X_add_number;
10601       /* Fall through.  */
10602     case M_BGEU_I:
10603     case M_BGEUL_I:
10604       if (mask == M_BGEUL_I)
10605 	likely = 1;
10606       if (imm_expr.X_add_number == 0)
10607 	goto do_true;
10608       else if (imm_expr.X_add_number == 1)
10609 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10610 				 &offset_expr, op[0], ZERO);
10611       else
10612 	{
10613 	  used_at = 1;
10614 	  set_at (op[0], 1);
10615 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10616 				   &offset_expr, AT, ZERO);
10617 	}
10618       break;
10619 
10620     case M_BGTL:
10621       likely = 1;
10622       /* Fall through.  */
10623     case M_BGT:
10624       if (op[1] == 0)
10625 	macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10626       else if (op[0] == 0)
10627 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
10628       else
10629 	{
10630 	  used_at = 1;
10631 	  macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10632 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10633 				   &offset_expr, AT, ZERO);
10634 	}
10635       break;
10636 
10637     case M_BGTUL:
10638       likely = 1;
10639       /* Fall through.  */
10640     case M_BGTU:
10641       if (op[1] == 0)
10642 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10643 				 &offset_expr, op[0], ZERO);
10644       else if (op[0] == 0)
10645 	goto do_false;
10646       else
10647 	{
10648 	  used_at = 1;
10649 	  macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10650 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10651 				   &offset_expr, AT, ZERO);
10652 	}
10653       break;
10654 
10655     case M_BLEL:
10656       likely = 1;
10657       /* Fall through.  */
10658     case M_BLE:
10659       if (op[1] == 0)
10660 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10661       else if (op[0] == 0)
10662 	macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
10663       else
10664 	{
10665 	  used_at = 1;
10666 	  macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10667 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10668 				   &offset_expr, AT, ZERO);
10669 	}
10670       break;
10671 
10672     case M_BLEL_I:
10673       likely = 1;
10674       /* Fall through.  */
10675     case M_BLE_I:
10676       if (imm_expr.X_add_number >= GPR_SMAX)
10677 	goto do_true;
10678       ++imm_expr.X_add_number;
10679       /* Fall through.  */
10680     case M_BLT_I:
10681     case M_BLTL_I:
10682       if (mask == M_BLTL_I)
10683 	likely = 1;
10684       if (imm_expr.X_add_number == 0)
10685 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10686       else if (imm_expr.X_add_number == 1)
10687 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10688       else
10689 	{
10690 	  used_at = 1;
10691 	  set_at (op[0], 0);
10692 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10693 				   &offset_expr, AT, ZERO);
10694 	}
10695       break;
10696 
10697     case M_BLEUL:
10698       likely = 1;
10699       /* Fall through.  */
10700     case M_BLEU:
10701       if (op[1] == 0)
10702 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10703 				 &offset_expr, op[0], ZERO);
10704       else if (op[0] == 0)
10705 	goto do_true;
10706       else
10707 	{
10708 	  used_at = 1;
10709 	  macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10710 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10711 				   &offset_expr, AT, ZERO);
10712 	}
10713       break;
10714 
10715     case M_BLEUL_I:
10716       likely = 1;
10717       /* Fall through.  */
10718     case M_BLEU_I:
10719       if (op[0] == 0
10720 	  || (GPR_SIZE == 32
10721 	      && imm_expr.X_add_number == -1))
10722 	goto do_true;
10723       ++imm_expr.X_add_number;
10724       /* Fall through.  */
10725     case M_BLTU_I:
10726     case M_BLTUL_I:
10727       if (mask == M_BLTUL_I)
10728 	likely = 1;
10729       if (imm_expr.X_add_number == 0)
10730 	goto do_false;
10731       else if (imm_expr.X_add_number == 1)
10732 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10733 				 &offset_expr, op[0], ZERO);
10734       else
10735 	{
10736 	  used_at = 1;
10737 	  set_at (op[0], 1);
10738 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10739 				   &offset_expr, AT, ZERO);
10740 	}
10741       break;
10742 
10743     case M_BLTL:
10744       likely = 1;
10745       /* Fall through.  */
10746     case M_BLT:
10747       if (op[1] == 0)
10748 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10749       else if (op[0] == 0)
10750 	macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
10751       else
10752 	{
10753 	  used_at = 1;
10754 	  macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10755 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10756 				   &offset_expr, AT, ZERO);
10757 	}
10758       break;
10759 
10760     case M_BLTUL:
10761       likely = 1;
10762       /* Fall through.  */
10763     case M_BLTU:
10764       if (op[1] == 0)
10765 	goto do_false;
10766       else if (op[0] == 0)
10767 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10768 				 &offset_expr, ZERO, op[1]);
10769       else
10770 	{
10771 	  used_at = 1;
10772 	  macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10773 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10774 				   &offset_expr, AT, ZERO);
10775 	}
10776       break;
10777 
10778     case M_DDIV_3:
10779       dbl = 1;
10780       /* Fall through.  */
10781     case M_DIV_3:
10782       s = "mflo";
10783       goto do_div3;
10784     case M_DREM_3:
10785       dbl = 1;
10786       /* Fall through.  */
10787     case M_REM_3:
10788       s = "mfhi";
10789     do_div3:
10790       if (op[2] == 0)
10791 	{
10792 	  as_warn (_("divide by zero"));
10793 	  if (mips_trap)
10794 	    macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10795 	  else
10796 	    macro_build (NULL, "break", BRK_FMT, 7);
10797 	  break;
10798 	}
10799 
10800       start_noreorder ();
10801       if (mips_trap)
10802 	{
10803 	  macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10804 	  macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10805 	}
10806       else
10807 	{
10808 	  if (mips_opts.micromips)
10809 	    micromips_label_expr (&label_expr);
10810 	  else
10811 	    label_expr.X_add_number = 8;
10812 	  macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10813 	  macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10814 	  macro_build (NULL, "break", BRK_FMT, 7);
10815 	  if (mips_opts.micromips)
10816 	    micromips_add_label ();
10817 	}
10818       expr1.X_add_number = -1;
10819       used_at = 1;
10820       load_register (AT, &expr1, dbl);
10821       if (mips_opts.micromips)
10822 	micromips_label_expr (&label_expr);
10823       else
10824 	label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
10825       macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
10826       if (dbl)
10827 	{
10828 	  expr1.X_add_number = 1;
10829 	  load_register (AT, &expr1, dbl);
10830 	  macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
10831 	}
10832       else
10833 	{
10834 	  expr1.X_add_number = 0x80000000;
10835 	  macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
10836 	}
10837       if (mips_trap)
10838 	{
10839 	  macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
10840 	  /* We want to close the noreorder block as soon as possible, so
10841 	     that later insns are available for delay slot filling.  */
10842 	  end_noreorder ();
10843 	}
10844       else
10845 	{
10846 	  if (mips_opts.micromips)
10847 	    micromips_label_expr (&label_expr);
10848 	  else
10849 	    label_expr.X_add_number = 8;
10850 	  macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
10851 	  macro_build (NULL, "nop", "");
10852 
10853 	  /* We want to close the noreorder block as soon as possible, so
10854 	     that later insns are available for delay slot filling.  */
10855 	  end_noreorder ();
10856 
10857 	  macro_build (NULL, "break", BRK_FMT, 6);
10858 	}
10859       if (mips_opts.micromips)
10860 	micromips_add_label ();
10861       macro_build (NULL, s, MFHL_FMT, op[0]);
10862       break;
10863 
10864     case M_DIV_3I:
10865       s = "div";
10866       s2 = "mflo";
10867       goto do_divi;
10868     case M_DIVU_3I:
10869       s = "divu";
10870       s2 = "mflo";
10871       goto do_divi;
10872     case M_REM_3I:
10873       s = "div";
10874       s2 = "mfhi";
10875       goto do_divi;
10876     case M_REMU_3I:
10877       s = "divu";
10878       s2 = "mfhi";
10879       goto do_divi;
10880     case M_DDIV_3I:
10881       dbl = 1;
10882       s = "ddiv";
10883       s2 = "mflo";
10884       goto do_divi;
10885     case M_DDIVU_3I:
10886       dbl = 1;
10887       s = "ddivu";
10888       s2 = "mflo";
10889       goto do_divi;
10890     case M_DREM_3I:
10891       dbl = 1;
10892       s = "ddiv";
10893       s2 = "mfhi";
10894       goto do_divi;
10895     case M_DREMU_3I:
10896       dbl = 1;
10897       s = "ddivu";
10898       s2 = "mfhi";
10899     do_divi:
10900       if (imm_expr.X_add_number == 0)
10901 	{
10902 	  as_warn (_("divide by zero"));
10903 	  if (mips_trap)
10904 	    macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10905 	  else
10906 	    macro_build (NULL, "break", BRK_FMT, 7);
10907 	  break;
10908 	}
10909       if (imm_expr.X_add_number == 1)
10910 	{
10911 	  if (strcmp (s2, "mflo") == 0)
10912 	    move_register (op[0], op[1]);
10913 	  else
10914 	    move_register (op[0], ZERO);
10915 	  break;
10916 	}
10917       if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
10918 	{
10919 	  if (strcmp (s2, "mflo") == 0)
10920 	    macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
10921 	  else
10922 	    move_register (op[0], ZERO);
10923 	  break;
10924 	}
10925 
10926       used_at = 1;
10927       load_register (AT, &imm_expr, dbl);
10928       macro_build (NULL, s, "z,s,t", op[1], AT);
10929       macro_build (NULL, s2, MFHL_FMT, op[0]);
10930       break;
10931 
10932     case M_DIVU_3:
10933       s = "divu";
10934       s2 = "mflo";
10935       goto do_divu3;
10936     case M_REMU_3:
10937       s = "divu";
10938       s2 = "mfhi";
10939       goto do_divu3;
10940     case M_DDIVU_3:
10941       s = "ddivu";
10942       s2 = "mflo";
10943       goto do_divu3;
10944     case M_DREMU_3:
10945       s = "ddivu";
10946       s2 = "mfhi";
10947     do_divu3:
10948       start_noreorder ();
10949       if (mips_trap)
10950 	{
10951 	  macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10952 	  macro_build (NULL, s, "z,s,t", op[1], op[2]);
10953 	  /* We want to close the noreorder block as soon as possible, so
10954 	     that later insns are available for delay slot filling.  */
10955 	  end_noreorder ();
10956 	}
10957       else
10958 	{
10959 	  if (mips_opts.micromips)
10960 	    micromips_label_expr (&label_expr);
10961 	  else
10962 	    label_expr.X_add_number = 8;
10963 	  macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10964 	  macro_build (NULL, s, "z,s,t", op[1], op[2]);
10965 
10966 	  /* We want to close the noreorder block as soon as possible, so
10967 	     that later insns are available for delay slot filling.  */
10968 	  end_noreorder ();
10969 	  macro_build (NULL, "break", BRK_FMT, 7);
10970 	  if (mips_opts.micromips)
10971 	    micromips_add_label ();
10972 	}
10973       macro_build (NULL, s2, MFHL_FMT, op[0]);
10974       break;
10975 
10976     case M_DLCA_AB:
10977       dbl = 1;
10978       /* Fall through.  */
10979     case M_LCA_AB:
10980       call = 1;
10981       goto do_la;
10982     case M_DLA_AB:
10983       dbl = 1;
10984       /* Fall through.  */
10985     case M_LA_AB:
10986     do_la:
10987       /* Load the address of a symbol into a register.  If breg is not
10988 	 zero, we then add a base register to it.  */
10989 
10990       breg = op[2];
10991       if (dbl && GPR_SIZE == 32)
10992 	as_warn (_("dla used to load 32-bit register; recommend using la "
10993 		   "instead"));
10994 
10995       if (!dbl && HAVE_64BIT_OBJECTS)
10996 	as_warn (_("la used to load 64-bit address; recommend using dla "
10997 		   "instead"));
10998 
10999       if (small_offset_p (0, align, 16))
11000 	{
11001 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
11002 		       -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
11003 	  break;
11004 	}
11005 
11006       if (mips_opts.at && (op[0] == breg))
11007 	{
11008 	  tempreg = AT;
11009 	  used_at = 1;
11010 	}
11011       else
11012 	tempreg = op[0];
11013 
11014       if (offset_expr.X_op != O_symbol
11015 	  && offset_expr.X_op != O_constant)
11016 	{
11017 	  as_bad (_("expression too complex"));
11018 	  offset_expr.X_op = O_constant;
11019 	}
11020 
11021       if (offset_expr.X_op == O_constant)
11022 	load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
11023       else if (mips_pic == NO_PIC)
11024 	{
11025 	  /* If this is a reference to a GP relative symbol, we want
11026 	       addiu	$tempreg,$gp,<sym>	(BFD_RELOC_GPREL16)
11027 	     Otherwise we want
11028 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
11029 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
11030 	     If we have a constant, we need two instructions anyhow,
11031 	     so we may as well always use the latter form.
11032 
11033 	     With 64bit address space and a usable $at we want
11034 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
11035 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
11036 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
11037 	       daddiu	$at,<sym>		(BFD_RELOC_LO16)
11038 	       dsll32	$tempreg,0
11039 	       daddu	$tempreg,$tempreg,$at
11040 
11041 	     If $at is already in use, we use a path which is suboptimal
11042 	     on superscalar processors.
11043 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
11044 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
11045 	       dsll	$tempreg,16
11046 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
11047 	       dsll	$tempreg,16
11048 	       daddiu	$tempreg,<sym>		(BFD_RELOC_LO16)
11049 
11050 	     For GP relative symbols in 64bit address space we can use
11051 	     the same sequence as in 32bit address space.  */
11052 	  if (HAVE_64BIT_SYMBOLS)
11053 	    {
11054 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11055 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11056 		{
11057 		  relax_start (offset_expr.X_add_symbol);
11058 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11059 			       tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11060 		  relax_switch ();
11061 		}
11062 
11063 	      if (used_at == 0 && mips_opts.at)
11064 		{
11065 		  macro_build (&offset_expr, "lui", LUI_FMT,
11066 			       tempreg, BFD_RELOC_MIPS_HIGHEST);
11067 		  macro_build (&offset_expr, "lui", LUI_FMT,
11068 			       AT, BFD_RELOC_HI16_S);
11069 		  macro_build (&offset_expr, "daddiu", "t,r,j",
11070 			       tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
11071 		  macro_build (&offset_expr, "daddiu", "t,r,j",
11072 			       AT, AT, BFD_RELOC_LO16);
11073 		  macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
11074 		  macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
11075 		  used_at = 1;
11076 		}
11077 	      else
11078 		{
11079 		  macro_build (&offset_expr, "lui", LUI_FMT,
11080 			       tempreg, BFD_RELOC_MIPS_HIGHEST);
11081 		  macro_build (&offset_expr, "daddiu", "t,r,j",
11082 			       tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
11083 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11084 		  macro_build (&offset_expr, "daddiu", "t,r,j",
11085 			       tempreg, tempreg, BFD_RELOC_HI16_S);
11086 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11087 		  macro_build (&offset_expr, "daddiu", "t,r,j",
11088 			       tempreg, tempreg, BFD_RELOC_LO16);
11089 		}
11090 
11091 	      if (mips_relax.sequence)
11092 		relax_end ();
11093 	    }
11094 	  else
11095 	    {
11096 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11097 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11098 		{
11099 		  relax_start (offset_expr.X_add_symbol);
11100 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11101 			       tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11102 		  relax_switch ();
11103 		}
11104 	      if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
11105 		as_bad (_("offset too large"));
11106 	      macro_build_lui (&offset_expr, tempreg);
11107 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11108 			   tempreg, tempreg, BFD_RELOC_LO16);
11109 	      if (mips_relax.sequence)
11110 		relax_end ();
11111 	    }
11112 	}
11113       else if (!mips_big_got && !HAVE_NEWABI)
11114 	{
11115 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11116 
11117 	  /* If this is a reference to an external symbol, and there
11118 	     is no constant, we want
11119 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11120 	     or for lca or if tempreg is PIC_CALL_REG
11121 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_CALL16)
11122 	     For a local symbol, we want
11123 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11124 	       nop
11125 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
11126 
11127 	     If we have a small constant, and this is a reference to
11128 	     an external symbol, we want
11129 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11130 	       nop
11131 	       addiu	$tempreg,$tempreg,<constant>
11132 	     For a local symbol, we want the same instruction
11133 	     sequence, but we output a BFD_RELOC_LO16 reloc on the
11134 	     addiu instruction.
11135 
11136 	     If we have a large constant, and this is a reference to
11137 	     an external symbol, we want
11138 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11139 	       lui	$at,<hiconstant>
11140 	       addiu	$at,$at,<loconstant>
11141 	       addu	$tempreg,$tempreg,$at
11142 	     For a local symbol, we want the same instruction
11143 	     sequence, but we output a BFD_RELOC_LO16 reloc on the
11144 	     addiu instruction.
11145 	   */
11146 
11147 	  if (offset_expr.X_add_number == 0)
11148 	    {
11149 	      if (mips_pic == SVR4_PIC
11150 		  && breg == 0
11151 		  && (call || tempreg == PIC_CALL_REG))
11152 		lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
11153 
11154 	      relax_start (offset_expr.X_add_symbol);
11155 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11156 			   lw_reloc_type, mips_gp_register);
11157 	      if (breg != 0)
11158 		{
11159 		  /* We're going to put in an addu instruction using
11160 		     tempreg, so we may as well insert the nop right
11161 		     now.  */
11162 		  load_delay_nop ();
11163 		}
11164 	      relax_switch ();
11165 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11166 			   tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
11167 	      load_delay_nop ();
11168 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11169 			   tempreg, tempreg, BFD_RELOC_LO16);
11170 	      relax_end ();
11171 	      /* FIXME: If breg == 0, and the next instruction uses
11172 		 $tempreg, then if this variant case is used an extra
11173 		 nop will be generated.  */
11174 	    }
11175 	  else if (offset_expr.X_add_number >= -0x8000
11176 		   && offset_expr.X_add_number < 0x8000)
11177 	    {
11178 	      load_got_offset (tempreg, &offset_expr);
11179 	      load_delay_nop ();
11180 	      add_got_offset (tempreg, &offset_expr);
11181 	    }
11182 	  else
11183 	    {
11184 	      expr1.X_add_number = offset_expr.X_add_number;
11185 	      offset_expr.X_add_number =
11186 		SEXT_16BIT (offset_expr.X_add_number);
11187 	      load_got_offset (tempreg, &offset_expr);
11188 	      offset_expr.X_add_number = expr1.X_add_number;
11189 	      /* If we are going to add in a base register, and the
11190 		 target register and the base register are the same,
11191 		 then we are using AT as a temporary register.  Since
11192 		 we want to load the constant into AT, we add our
11193 		 current AT (from the global offset table) and the
11194 		 register into the register now, and pretend we were
11195 		 not using a base register.  */
11196 	      if (breg == op[0])
11197 		{
11198 		  load_delay_nop ();
11199 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11200 			       op[0], AT, breg);
11201 		  breg = 0;
11202 		  tempreg = op[0];
11203 		}
11204 	      add_got_offset_hilo (tempreg, &offset_expr, AT);
11205 	      used_at = 1;
11206 	    }
11207 	}
11208       else if (!mips_big_got && HAVE_NEWABI)
11209 	{
11210 	  int add_breg_early = 0;
11211 
11212 	  /* If this is a reference to an external, and there is no
11213 	     constant, or local symbol (*), with or without a
11214 	     constant, we want
11215 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
11216 	     or for lca or if tempreg is PIC_CALL_REG
11217 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_CALL16)
11218 
11219 	     If we have a small constant, and this is a reference to
11220 	     an external symbol, we want
11221 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
11222 	       addiu	$tempreg,$tempreg,<constant>
11223 
11224 	     If we have a large constant, and this is a reference to
11225 	     an external symbol, we want
11226 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
11227 	       lui	$at,<hiconstant>
11228 	       addiu	$at,$at,<loconstant>
11229 	       addu	$tempreg,$tempreg,$at
11230 
11231 	     (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11232 	     local symbols, even though it introduces an additional
11233 	     instruction.  */
11234 
11235 	  if (offset_expr.X_add_number)
11236 	    {
11237 	      expr1.X_add_number = offset_expr.X_add_number;
11238 	      offset_expr.X_add_number = 0;
11239 
11240 	      relax_start (offset_expr.X_add_symbol);
11241 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11242 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11243 
11244 	      if (expr1.X_add_number >= -0x8000
11245 		  && expr1.X_add_number < 0x8000)
11246 		{
11247 		  macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11248 			       tempreg, tempreg, BFD_RELOC_LO16);
11249 		}
11250 	      else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11251 		{
11252 		  unsigned int dreg;
11253 
11254 		  /* If we are going to add in a base register, and the
11255 		     target register and the base register are the same,
11256 		     then we are using AT as a temporary register.  Since
11257 		     we want to load the constant into AT, we add our
11258 		     current AT (from the global offset table) and the
11259 		     register into the register now, and pretend we were
11260 		     not using a base register.  */
11261 		  if (breg != op[0])
11262 		    dreg = tempreg;
11263 		  else
11264 		    {
11265 		      gas_assert (tempreg == AT);
11266 		      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11267 				   op[0], AT, breg);
11268 		      dreg = op[0];
11269 		      add_breg_early = 1;
11270 		    }
11271 
11272 		  load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11273 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11274 			       dreg, dreg, AT);
11275 
11276 		  used_at = 1;
11277 		}
11278 	      else
11279 		as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11280 
11281 	      relax_switch ();
11282 	      offset_expr.X_add_number = expr1.X_add_number;
11283 
11284 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11285 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11286 	      if (add_breg_early)
11287 		{
11288 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11289 			       op[0], tempreg, breg);
11290 		  breg = 0;
11291 		  tempreg = op[0];
11292 		}
11293 	      relax_end ();
11294 	    }
11295 	  else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
11296 	    {
11297 	      relax_start (offset_expr.X_add_symbol);
11298 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11299 			   BFD_RELOC_MIPS_CALL16, mips_gp_register);
11300 	      relax_switch ();
11301 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11302 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11303 	      relax_end ();
11304 	    }
11305 	  else
11306 	    {
11307 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11308 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11309 	    }
11310 	}
11311       else if (mips_big_got && !HAVE_NEWABI)
11312 	{
11313 	  int gpdelay;
11314 	  int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11315 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11316 	  int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11317 
11318 	  /* This is the large GOT case.  If this is a reference to an
11319 	     external symbol, and there is no constant, we want
11320 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11321 	       addu	$tempreg,$tempreg,$gp
11322 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11323 	     or for lca or if tempreg is PIC_CALL_REG
11324 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
11325 	       addu	$tempreg,$tempreg,$gp
11326 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11327 	     For a local symbol, we want
11328 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11329 	       nop
11330 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
11331 
11332 	     If we have a small constant, and this is a reference to
11333 	     an external symbol, we want
11334 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11335 	       addu	$tempreg,$tempreg,$gp
11336 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11337 	       nop
11338 	       addiu	$tempreg,$tempreg,<constant>
11339 	     For a local symbol, we want
11340 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11341 	       nop
11342 	       addiu	$tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11343 
11344 	     If we have a large constant, and this is a reference to
11345 	     an external symbol, we want
11346 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11347 	       addu	$tempreg,$tempreg,$gp
11348 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11349 	       lui	$at,<hiconstant>
11350 	       addiu	$at,$at,<loconstant>
11351 	       addu	$tempreg,$tempreg,$at
11352 	     For a local symbol, we want
11353 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11354 	       lui	$at,<hiconstant>
11355 	       addiu	$at,$at,<loconstant>	(BFD_RELOC_LO16)
11356 	       addu	$tempreg,$tempreg,$at
11357 	  */
11358 
11359 	  expr1.X_add_number = offset_expr.X_add_number;
11360 	  offset_expr.X_add_number = 0;
11361 	  relax_start (offset_expr.X_add_symbol);
11362 	  gpdelay = reg_needs_delay (mips_gp_register);
11363 	  if (expr1.X_add_number == 0 && breg == 0
11364 	      && (call || tempreg == PIC_CALL_REG))
11365 	    {
11366 	      lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11367 	      lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11368 	    }
11369 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11370 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11371 		       tempreg, tempreg, mips_gp_register);
11372 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11373 		       tempreg, lw_reloc_type, tempreg);
11374 	  if (expr1.X_add_number == 0)
11375 	    {
11376 	      if (breg != 0)
11377 		{
11378 		  /* We're going to put in an addu instruction using
11379 		     tempreg, so we may as well insert the nop right
11380 		     now.  */
11381 		  load_delay_nop ();
11382 		}
11383 	    }
11384 	  else if (expr1.X_add_number >= -0x8000
11385 		   && expr1.X_add_number < 0x8000)
11386 	    {
11387 	      load_delay_nop ();
11388 	      macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11389 			   tempreg, tempreg, BFD_RELOC_LO16);
11390 	    }
11391 	  else
11392 	    {
11393 	      unsigned int dreg;
11394 
11395 	      /* If we are going to add in a base register, and the
11396 		 target register and the base register are the same,
11397 		 then we are using AT as a temporary register.  Since
11398 		 we want to load the constant into AT, we add our
11399 		 current AT (from the global offset table) and the
11400 		 register into the register now, and pretend we were
11401 		 not using a base register.  */
11402 	      if (breg != op[0])
11403 		dreg = tempreg;
11404 	      else
11405 		{
11406 		  gas_assert (tempreg == AT);
11407 		  load_delay_nop ();
11408 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11409 			       op[0], AT, breg);
11410 		  dreg = op[0];
11411 		}
11412 
11413 	      load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11414 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11415 
11416 	      used_at = 1;
11417 	    }
11418 	  offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
11419 	  relax_switch ();
11420 
11421 	  if (gpdelay)
11422 	    {
11423 	      /* This is needed because this instruction uses $gp, but
11424 		 the first instruction on the main stream does not.  */
11425 	      macro_build (NULL, "nop", "");
11426 	    }
11427 
11428 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11429 		       local_reloc_type, mips_gp_register);
11430 	  if (expr1.X_add_number >= -0x8000
11431 	      && expr1.X_add_number < 0x8000)
11432 	    {
11433 	      load_delay_nop ();
11434 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11435 			   tempreg, tempreg, BFD_RELOC_LO16);
11436 	      /* FIXME: If add_number is 0, and there was no base
11437 		 register, the external symbol case ended with a load,
11438 		 so if the symbol turns out to not be external, and
11439 		 the next instruction uses tempreg, an unnecessary nop
11440 		 will be inserted.  */
11441 	    }
11442 	  else
11443 	    {
11444 	      if (breg == op[0])
11445 		{
11446 		  /* We must add in the base register now, as in the
11447 		     external symbol case.  */
11448 		  gas_assert (tempreg == AT);
11449 		  load_delay_nop ();
11450 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11451 			       op[0], AT, breg);
11452 		  tempreg = op[0];
11453 		  /* We set breg to 0 because we have arranged to add
11454 		     it in in both cases.  */
11455 		  breg = 0;
11456 		}
11457 
11458 	      macro_build_lui (&expr1, AT);
11459 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11460 			   AT, AT, BFD_RELOC_LO16);
11461 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11462 			   tempreg, tempreg, AT);
11463 	      used_at = 1;
11464 	    }
11465 	  relax_end ();
11466 	}
11467       else if (mips_big_got && HAVE_NEWABI)
11468 	{
11469 	  int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11470 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11471 	  int add_breg_early = 0;
11472 
11473 	  /* This is the large GOT case.  If this is a reference to an
11474 	     external symbol, and there is no constant, we want
11475 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11476 	       add	$tempreg,$tempreg,$gp
11477 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11478 	     or for lca or if tempreg is PIC_CALL_REG
11479 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
11480 	       add	$tempreg,$tempreg,$gp
11481 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11482 
11483 	     If we have a small constant, and this is a reference to
11484 	     an external symbol, we want
11485 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11486 	       add	$tempreg,$tempreg,$gp
11487 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11488 	       addi	$tempreg,$tempreg,<constant>
11489 
11490 	     If we have a large constant, and this is a reference to
11491 	     an external symbol, we want
11492 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11493 	       addu	$tempreg,$tempreg,$gp
11494 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11495 	       lui	$at,<hiconstant>
11496 	       addi	$at,$at,<loconstant>
11497 	       add	$tempreg,$tempreg,$at
11498 
11499 	     If we have NewABI, and we know it's a local symbol, we want
11500 	       lw	$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT_PAGE)
11501 	       addiu	$reg,$reg,<sym>		(BFD_RELOC_MIPS_GOT_OFST)
11502 	     otherwise we have to resort to GOT_HI16/GOT_LO16.  */
11503 
11504 	  relax_start (offset_expr.X_add_symbol);
11505 
11506 	  expr1.X_add_number = offset_expr.X_add_number;
11507 	  offset_expr.X_add_number = 0;
11508 
11509 	  if (expr1.X_add_number == 0 && breg == 0
11510 	      && (call || tempreg == PIC_CALL_REG))
11511 	    {
11512 	      lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11513 	      lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11514 	    }
11515 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11516 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11517 		       tempreg, tempreg, mips_gp_register);
11518 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11519 		       tempreg, lw_reloc_type, tempreg);
11520 
11521 	  if (expr1.X_add_number == 0)
11522 	    ;
11523 	  else if (expr1.X_add_number >= -0x8000
11524 		   && expr1.X_add_number < 0x8000)
11525 	    {
11526 	      macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11527 			   tempreg, tempreg, BFD_RELOC_LO16);
11528 	    }
11529 	  else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11530 	    {
11531 	      unsigned int dreg;
11532 
11533 	      /* If we are going to add in a base register, and the
11534 		 target register and the base register are the same,
11535 		 then we are using AT as a temporary register.  Since
11536 		 we want to load the constant into AT, we add our
11537 		 current AT (from the global offset table) and the
11538 		 register into the register now, and pretend we were
11539 		 not using a base register.  */
11540 	      if (breg != op[0])
11541 		dreg = tempreg;
11542 	      else
11543 		{
11544 		  gas_assert (tempreg == AT);
11545 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11546 			       op[0], AT, breg);
11547 		  dreg = op[0];
11548 		  add_breg_early = 1;
11549 		}
11550 
11551 	      load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11552 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11553 
11554 	      used_at = 1;
11555 	    }
11556 	  else
11557 	    as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11558 
11559 	  relax_switch ();
11560 	  offset_expr.X_add_number = expr1.X_add_number;
11561 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11562 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11563 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11564 		       tempreg, BFD_RELOC_MIPS_GOT_OFST);
11565 	  if (add_breg_early)
11566 	    {
11567 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11568 			   op[0], tempreg, breg);
11569 	      breg = 0;
11570 	      tempreg = op[0];
11571 	    }
11572 	  relax_end ();
11573 	}
11574       else
11575 	abort ();
11576 
11577       if (breg != 0)
11578 	macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
11579       break;
11580 
11581     case M_MSGSND:
11582       gas_assert (!mips_opts.micromips);
11583       macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
11584       break;
11585 
11586     case M_MSGLD:
11587       gas_assert (!mips_opts.micromips);
11588       macro_build (NULL, "c2", "C", 0x02);
11589       break;
11590 
11591     case M_MSGLD_T:
11592       gas_assert (!mips_opts.micromips);
11593       macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
11594       break;
11595 
11596     case M_MSGWAIT:
11597       gas_assert (!mips_opts.micromips);
11598       macro_build (NULL, "c2", "C", 3);
11599       break;
11600 
11601     case M_MSGWAIT_T:
11602       gas_assert (!mips_opts.micromips);
11603       macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
11604       break;
11605 
11606     case M_J_A:
11607       /* The j instruction may not be used in PIC code, since it
11608 	 requires an absolute address.  We convert it to a b
11609 	 instruction.  */
11610       if (mips_pic == NO_PIC)
11611 	macro_build (&offset_expr, "j", "a");
11612       else
11613 	macro_build (&offset_expr, "b", "p");
11614       break;
11615 
11616       /* The jal instructions must be handled as macros because when
11617 	 generating PIC code they expand to multi-instruction
11618 	 sequences.  Normally they are simple instructions.  */
11619     case M_JALS_1:
11620       op[1] = op[0];
11621       op[0] = RA;
11622       /* Fall through.  */
11623     case M_JALS_2:
11624       gas_assert (mips_opts.micromips);
11625       if (mips_opts.insn32)
11626 	{
11627 	  as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11628 	  break;
11629 	}
11630       jals = 1;
11631       goto jal;
11632     case M_JAL_1:
11633       op[1] = op[0];
11634       op[0] = RA;
11635       /* Fall through.  */
11636     case M_JAL_2:
11637     jal:
11638       if (mips_pic == NO_PIC)
11639 	{
11640 	  s = jals ? "jalrs" : "jalr";
11641 	  if (mips_opts.micromips
11642 	      && !mips_opts.insn32
11643 	      && op[0] == RA
11644 	      && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11645 	    macro_build (NULL, s, "mj", op[1]);
11646 	  else
11647 	    macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11648 	}
11649       else
11650 	{
11651 	  int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11652 			   && mips_cprestore_offset >= 0);
11653 
11654 	  if (op[1] != PIC_CALL_REG)
11655 	    as_warn (_("MIPS PIC call to register other than $25"));
11656 
11657 	  s = ((mips_opts.micromips
11658 		&& !mips_opts.insn32
11659 		&& (!mips_opts.noreorder || cprestore))
11660 	       ? "jalrs" : "jalr");
11661 	  if (mips_opts.micromips
11662 	      && !mips_opts.insn32
11663 	      && op[0] == RA
11664 	      && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11665 	    macro_build (NULL, s, "mj", op[1]);
11666 	  else
11667 	    macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11668 	  if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
11669 	    {
11670 	      if (mips_cprestore_offset < 0)
11671 		as_warn (_("no .cprestore pseudo-op used in PIC code"));
11672 	      else
11673 		{
11674 		  if (!mips_frame_reg_valid)
11675 		    {
11676 		      as_warn (_("no .frame pseudo-op used in PIC code"));
11677 		      /* Quiet this warning.  */
11678 		      mips_frame_reg_valid = 1;
11679 		    }
11680 		  if (!mips_cprestore_valid)
11681 		    {
11682 		      as_warn (_("no .cprestore pseudo-op used in PIC code"));
11683 		      /* Quiet this warning.  */
11684 		      mips_cprestore_valid = 1;
11685 		    }
11686 		  if (mips_opts.noreorder)
11687 		    macro_build (NULL, "nop", "");
11688 		  expr1.X_add_number = mips_cprestore_offset;
11689 		  macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11690 						mips_gp_register,
11691 						mips_frame_reg,
11692 						HAVE_64BIT_ADDRESSES);
11693 		}
11694 	    }
11695 	}
11696 
11697       break;
11698 
11699     case M_JALS_A:
11700       gas_assert (mips_opts.micromips);
11701       if (mips_opts.insn32)
11702 	{
11703 	  as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11704 	  break;
11705 	}
11706       jals = 1;
11707       /* Fall through.  */
11708     case M_JAL_A:
11709       if (mips_pic == NO_PIC)
11710 	macro_build (&offset_expr, jals ? "jals" : "jal", "a");
11711       else if (mips_pic == SVR4_PIC)
11712 	{
11713 	  /* If this is a reference to an external symbol, and we are
11714 	     using a small GOT, we want
11715 	       lw	$25,<sym>($gp)		(BFD_RELOC_MIPS_CALL16)
11716 	       nop
11717 	       jalr	$ra,$25
11718 	       nop
11719 	       lw	$gp,cprestore($sp)
11720 	     The cprestore value is set using the .cprestore
11721 	     pseudo-op.  If we are using a big GOT, we want
11722 	       lui	$25,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
11723 	       addu	$25,$25,$gp
11724 	       lw	$25,<sym>($25)		(BFD_RELOC_MIPS_CALL_LO16)
11725 	       nop
11726 	       jalr	$ra,$25
11727 	       nop
11728 	       lw	$gp,cprestore($sp)
11729 	     If the symbol is not external, we want
11730 	       lw	$25,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
11731 	       nop
11732 	       addiu	$25,$25,<sym>		(BFD_RELOC_LO16)
11733 	       jalr	$ra,$25
11734 	       nop
11735 	       lw $gp,cprestore($sp)
11736 
11737 	     For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11738 	     sequences above, minus nops, unless the symbol is local,
11739 	     which enables us to use GOT_PAGE/GOT_OFST (big got) or
11740 	     GOT_DISP.  */
11741 	  if (HAVE_NEWABI)
11742 	    {
11743 	      if (!mips_big_got)
11744 		{
11745 		  relax_start (offset_expr.X_add_symbol);
11746 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11747 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11748 			       mips_gp_register);
11749 		  relax_switch ();
11750 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11751 			       PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
11752 			       mips_gp_register);
11753 		  relax_end ();
11754 		}
11755 	      else
11756 		{
11757 		  relax_start (offset_expr.X_add_symbol);
11758 		  macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11759 			       BFD_RELOC_MIPS_CALL_HI16);
11760 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11761 			       PIC_CALL_REG, mips_gp_register);
11762 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11763 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11764 			       PIC_CALL_REG);
11765 		  relax_switch ();
11766 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11767 			       PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11768 			       mips_gp_register);
11769 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11770 			       PIC_CALL_REG, PIC_CALL_REG,
11771 			       BFD_RELOC_MIPS_GOT_OFST);
11772 		  relax_end ();
11773 		}
11774 
11775 	      macro_build_jalr (&offset_expr, 0);
11776 	    }
11777 	  else
11778 	    {
11779 	      relax_start (offset_expr.X_add_symbol);
11780 	      if (!mips_big_got)
11781 		{
11782 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11783 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11784 			       mips_gp_register);
11785 		  load_delay_nop ();
11786 		  relax_switch ();
11787 		}
11788 	      else
11789 		{
11790 		  int gpdelay;
11791 
11792 		  gpdelay = reg_needs_delay (mips_gp_register);
11793 		  macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11794 			       BFD_RELOC_MIPS_CALL_HI16);
11795 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11796 			       PIC_CALL_REG, mips_gp_register);
11797 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11798 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11799 			       PIC_CALL_REG);
11800 		  load_delay_nop ();
11801 		  relax_switch ();
11802 		  if (gpdelay)
11803 		    macro_build (NULL, "nop", "");
11804 		}
11805 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11806 			   PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
11807 			   mips_gp_register);
11808 	      load_delay_nop ();
11809 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11810 			   PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
11811 	      relax_end ();
11812 	      macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
11813 
11814 	      if (mips_cprestore_offset < 0)
11815 		as_warn (_("no .cprestore pseudo-op used in PIC code"));
11816 	      else
11817 		{
11818 		  if (!mips_frame_reg_valid)
11819 		    {
11820 		      as_warn (_("no .frame pseudo-op used in PIC code"));
11821 		      /* Quiet this warning.  */
11822 		      mips_frame_reg_valid = 1;
11823 		    }
11824 		  if (!mips_cprestore_valid)
11825 		    {
11826 		      as_warn (_("no .cprestore pseudo-op used in PIC code"));
11827 		      /* Quiet this warning.  */
11828 		      mips_cprestore_valid = 1;
11829 		    }
11830 		  if (mips_opts.noreorder)
11831 		    macro_build (NULL, "nop", "");
11832 		  expr1.X_add_number = mips_cprestore_offset;
11833 		  macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11834 						mips_gp_register,
11835 						mips_frame_reg,
11836 						HAVE_64BIT_ADDRESSES);
11837 		}
11838 	    }
11839 	}
11840       else if (mips_pic == VXWORKS_PIC)
11841 	as_bad (_("non-PIC jump used in PIC library"));
11842       else
11843 	abort ();
11844 
11845       break;
11846 
11847     case M_LBUE_AB:
11848       s = "lbue";
11849       fmt = "t,+j(b)";
11850       offbits = 9;
11851       goto ld_st;
11852     case M_LHUE_AB:
11853       s = "lhue";
11854       fmt = "t,+j(b)";
11855       offbits = 9;
11856       goto ld_st;
11857     case M_LBE_AB:
11858       s = "lbe";
11859       fmt = "t,+j(b)";
11860       offbits = 9;
11861       goto ld_st;
11862     case M_LHE_AB:
11863       s = "lhe";
11864       fmt = "t,+j(b)";
11865       offbits = 9;
11866       goto ld_st;
11867     case M_LLE_AB:
11868       s = "lle";
11869       fmt = "t,+j(b)";
11870       offbits = 9;
11871       goto ld_st;
11872     case M_LWE_AB:
11873       s = "lwe";
11874       fmt = "t,+j(b)";
11875       offbits = 9;
11876       goto ld_st;
11877     case M_LWLE_AB:
11878       s = "lwle";
11879       fmt = "t,+j(b)";
11880       offbits = 9;
11881       goto ld_st;
11882     case M_LWRE_AB:
11883       s = "lwre";
11884       fmt = "t,+j(b)";
11885       offbits = 9;
11886       goto ld_st;
11887     case M_SBE_AB:
11888       s = "sbe";
11889       fmt = "t,+j(b)";
11890       offbits = 9;
11891       goto ld_st;
11892     case M_SCE_AB:
11893       s = "sce";
11894       fmt = "t,+j(b)";
11895       offbits = 9;
11896       goto ld_st;
11897     case M_SHE_AB:
11898       s = "she";
11899       fmt = "t,+j(b)";
11900       offbits = 9;
11901       goto ld_st;
11902     case M_SWE_AB:
11903       s = "swe";
11904       fmt = "t,+j(b)";
11905       offbits = 9;
11906       goto ld_st;
11907     case M_SWLE_AB:
11908       s = "swle";
11909       fmt = "t,+j(b)";
11910       offbits = 9;
11911       goto ld_st;
11912     case M_SWRE_AB:
11913       s = "swre";
11914       fmt = "t,+j(b)";
11915       offbits = 9;
11916       goto ld_st;
11917     case M_ACLR_AB:
11918       s = "aclr";
11919       fmt = "\\,~(b)";
11920       offbits = 12;
11921       goto ld_st;
11922     case M_ASET_AB:
11923       s = "aset";
11924       fmt = "\\,~(b)";
11925       offbits = 12;
11926       goto ld_st;
11927     case M_LB_AB:
11928       s = "lb";
11929       fmt = "t,o(b)";
11930       goto ld;
11931     case M_LBU_AB:
11932       s = "lbu";
11933       fmt = "t,o(b)";
11934       goto ld;
11935     case M_LH_AB:
11936       s = "lh";
11937       fmt = "t,o(b)";
11938       goto ld;
11939     case M_LHU_AB:
11940       s = "lhu";
11941       fmt = "t,o(b)";
11942       goto ld;
11943     case M_LW_AB:
11944       s = "lw";
11945       fmt = "t,o(b)";
11946       goto ld;
11947     case M_LWC0_AB:
11948       gas_assert (!mips_opts.micromips);
11949       s = "lwc0";
11950       fmt = "E,o(b)";
11951       /* Itbl support may require additional care here.  */
11952       coproc = 1;
11953       goto ld_st;
11954     case M_LWC1_AB:
11955       s = "lwc1";
11956       fmt = "T,o(b)";
11957       /* Itbl support may require additional care here.  */
11958       coproc = 1;
11959       goto ld_st;
11960     case M_LWC2_AB:
11961       s = "lwc2";
11962       fmt = COP12_FMT;
11963       offbits = (mips_opts.micromips ? 12
11964 		 : ISA_IS_R6 (mips_opts.isa) ? 11
11965 		 : 16);
11966       /* Itbl support may require additional care here.  */
11967       coproc = 1;
11968       goto ld_st;
11969     case M_LWC3_AB:
11970       gas_assert (!mips_opts.micromips);
11971       s = "lwc3";
11972       fmt = "E,o(b)";
11973       /* Itbl support may require additional care here.  */
11974       coproc = 1;
11975       goto ld_st;
11976     case M_LWL_AB:
11977       s = "lwl";
11978       fmt = MEM12_FMT;
11979       offbits = (mips_opts.micromips ? 12 : 16);
11980       goto ld_st;
11981     case M_LWR_AB:
11982       s = "lwr";
11983       fmt = MEM12_FMT;
11984       offbits = (mips_opts.micromips ? 12 : 16);
11985       goto ld_st;
11986     case M_LDC1_AB:
11987       s = "ldc1";
11988       fmt = "T,o(b)";
11989       /* Itbl support may require additional care here.  */
11990       coproc = 1;
11991       goto ld_st;
11992     case M_LDC2_AB:
11993       s = "ldc2";
11994       fmt = COP12_FMT;
11995       offbits = (mips_opts.micromips ? 12
11996 		 : ISA_IS_R6 (mips_opts.isa) ? 11
11997 		 : 16);
11998       /* Itbl support may require additional care here.  */
11999       coproc = 1;
12000       goto ld_st;
12001     case M_LQC2_AB:
12002       s = "lqc2";
12003       fmt = "+7,o(b)";
12004       /* Itbl support may require additional care here.  */
12005       coproc = 1;
12006       goto ld_st;
12007     case M_LDC3_AB:
12008       s = "ldc3";
12009       fmt = "E,o(b)";
12010       /* Itbl support may require additional care here.  */
12011       coproc = 1;
12012       goto ld_st;
12013     case M_LDL_AB:
12014       s = "ldl";
12015       fmt = MEM12_FMT;
12016       offbits = (mips_opts.micromips ? 12 : 16);
12017       goto ld_st;
12018     case M_LDR_AB:
12019       s = "ldr";
12020       fmt = MEM12_FMT;
12021       offbits = (mips_opts.micromips ? 12 : 16);
12022       goto ld_st;
12023     case M_LL_AB:
12024       s = "ll";
12025       fmt = LL_SC_FMT;
12026       offbits = (mips_opts.micromips ? 12
12027 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12028 		 : 16);
12029       goto ld;
12030     case M_LLD_AB:
12031       s = "lld";
12032       fmt = LL_SC_FMT;
12033       offbits = (mips_opts.micromips ? 12
12034 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12035 		 : 16);
12036       goto ld;
12037     case M_LWU_AB:
12038       s = "lwu";
12039       fmt = MEM12_FMT;
12040       offbits = (mips_opts.micromips ? 12 : 16);
12041       goto ld;
12042     case M_LWP_AB:
12043       gas_assert (mips_opts.micromips);
12044       s = "lwp";
12045       fmt = "t,~(b)";
12046       offbits = 12;
12047       lp = 1;
12048       goto ld;
12049     case M_LDP_AB:
12050       gas_assert (mips_opts.micromips);
12051       s = "ldp";
12052       fmt = "t,~(b)";
12053       offbits = 12;
12054       lp = 1;
12055       goto ld;
12056     case M_LLDP_AB:
12057     case M_LLWP_AB:
12058     case M_LLWPE_AB:
12059       s = ip->insn_mo->name;
12060       fmt = "t,d,s";
12061       ll_sc_paired = 1;
12062       offbits = 0;
12063       goto ld;
12064     case M_LWM_AB:
12065       gas_assert (mips_opts.micromips);
12066       s = "lwm";
12067       fmt = "n,~(b)";
12068       offbits = 12;
12069       goto ld_st;
12070     case M_LDM_AB:
12071       gas_assert (mips_opts.micromips);
12072       s = "ldm";
12073       fmt = "n,~(b)";
12074       offbits = 12;
12075       goto ld_st;
12076 
12077     ld:
12078       /* Try to use one the the load registers to compute the base address.
12079 	 We don't want to use $0 as tempreg.  */
12080       if (ll_sc_paired)
12081 	{
12082 	  if ((op[0] == ZERO && op[3] == op[1])
12083 	      || (op[1] == ZERO && op[3] == op[0])
12084 	      || (op[0] == ZERO && op[1] == ZERO))
12085 	    goto ld_st;
12086 	  else if (op[0] != op[3] && op[0] != ZERO)
12087 	    tempreg = op[0];
12088 	  else
12089 	    tempreg = op[1];
12090 	}
12091       else
12092         {
12093 	  if (op[2] == op[0] + lp || op[0] + lp == ZERO)
12094 	    goto ld_st;
12095 	  else
12096 	    tempreg = op[0] + lp;
12097 	}
12098       goto ld_noat;
12099 
12100     case M_SB_AB:
12101       s = "sb";
12102       fmt = "t,o(b)";
12103       goto ld_st;
12104     case M_SH_AB:
12105       s = "sh";
12106       fmt = "t,o(b)";
12107       goto ld_st;
12108     case M_SW_AB:
12109       s = "sw";
12110       fmt = "t,o(b)";
12111       goto ld_st;
12112     case M_SWC0_AB:
12113       gas_assert (!mips_opts.micromips);
12114       s = "swc0";
12115       fmt = "E,o(b)";
12116       /* Itbl support may require additional care here.  */
12117       coproc = 1;
12118       goto ld_st;
12119     case M_SWC1_AB:
12120       s = "swc1";
12121       fmt = "T,o(b)";
12122       /* Itbl support may require additional care here.  */
12123       coproc = 1;
12124       goto ld_st;
12125     case M_SWC2_AB:
12126       s = "swc2";
12127       fmt = COP12_FMT;
12128       offbits = (mips_opts.micromips ? 12
12129 		 : ISA_IS_R6 (mips_opts.isa) ? 11
12130 		 : 16);
12131       /* Itbl support may require additional care here.  */
12132       coproc = 1;
12133       goto ld_st;
12134     case M_SWC3_AB:
12135       gas_assert (!mips_opts.micromips);
12136       s = "swc3";
12137       fmt = "E,o(b)";
12138       /* Itbl support may require additional care here.  */
12139       coproc = 1;
12140       goto ld_st;
12141     case M_SWL_AB:
12142       s = "swl";
12143       fmt = MEM12_FMT;
12144       offbits = (mips_opts.micromips ? 12 : 16);
12145       goto ld_st;
12146     case M_SWR_AB:
12147       s = "swr";
12148       fmt = MEM12_FMT;
12149       offbits = (mips_opts.micromips ? 12 : 16);
12150       goto ld_st;
12151     case M_SC_AB:
12152       s = "sc";
12153       fmt = LL_SC_FMT;
12154       offbits = (mips_opts.micromips ? 12
12155 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12156 		 : 16);
12157       goto ld_st;
12158     case M_SCD_AB:
12159       s = "scd";
12160       fmt = LL_SC_FMT;
12161       offbits = (mips_opts.micromips ? 12
12162 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12163 		 : 16);
12164       goto ld_st;
12165     case M_SCDP_AB:
12166     case M_SCWP_AB:
12167     case M_SCWPE_AB:
12168       s = ip->insn_mo->name;
12169       fmt = "t,d,s";
12170       ll_sc_paired = 1;
12171       offbits = 0;
12172       goto ld_st;
12173     case M_CACHE_AB:
12174       s = "cache";
12175       fmt = (mips_opts.micromips ? "k,~(b)"
12176 	     : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12177 	     : "k,o(b)");
12178       offbits = (mips_opts.micromips ? 12
12179 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12180 		 : 16);
12181       goto ld_st;
12182     case M_CACHEE_AB:
12183       s = "cachee";
12184       fmt = "k,+j(b)";
12185       offbits = 9;
12186       goto ld_st;
12187     case M_PREF_AB:
12188       s = "pref";
12189       fmt = (mips_opts.micromips ? "k,~(b)"
12190 	     : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12191 	     : "k,o(b)");
12192       offbits = (mips_opts.micromips ? 12
12193 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12194 		 : 16);
12195       goto ld_st;
12196     case M_PREFE_AB:
12197       s = "prefe";
12198       fmt = "k,+j(b)";
12199       offbits = 9;
12200       goto ld_st;
12201     case M_SDC1_AB:
12202       s = "sdc1";
12203       fmt = "T,o(b)";
12204       coproc = 1;
12205       /* Itbl support may require additional care here.  */
12206       goto ld_st;
12207     case M_SDC2_AB:
12208       s = "sdc2";
12209       fmt = COP12_FMT;
12210       offbits = (mips_opts.micromips ? 12
12211 		 : ISA_IS_R6 (mips_opts.isa) ? 11
12212 		 : 16);
12213       /* Itbl support may require additional care here.  */
12214       coproc = 1;
12215       goto ld_st;
12216     case M_SQC2_AB:
12217       s = "sqc2";
12218       fmt = "+7,o(b)";
12219       /* Itbl support may require additional care here.  */
12220       coproc = 1;
12221       goto ld_st;
12222     case M_SDC3_AB:
12223       gas_assert (!mips_opts.micromips);
12224       s = "sdc3";
12225       fmt = "E,o(b)";
12226       /* Itbl support may require additional care here.  */
12227       coproc = 1;
12228       goto ld_st;
12229     case M_SDL_AB:
12230       s = "sdl";
12231       fmt = MEM12_FMT;
12232       offbits = (mips_opts.micromips ? 12 : 16);
12233       goto ld_st;
12234     case M_SDR_AB:
12235       s = "sdr";
12236       fmt = MEM12_FMT;
12237       offbits = (mips_opts.micromips ? 12 : 16);
12238       goto ld_st;
12239     case M_SWP_AB:
12240       gas_assert (mips_opts.micromips);
12241       s = "swp";
12242       fmt = "t,~(b)";
12243       offbits = 12;
12244       goto ld_st;
12245     case M_SDP_AB:
12246       gas_assert (mips_opts.micromips);
12247       s = "sdp";
12248       fmt = "t,~(b)";
12249       offbits = 12;
12250       goto ld_st;
12251     case M_SWM_AB:
12252       gas_assert (mips_opts.micromips);
12253       s = "swm";
12254       fmt = "n,~(b)";
12255       offbits = 12;
12256       goto ld_st;
12257     case M_SDM_AB:
12258       gas_assert (mips_opts.micromips);
12259       s = "sdm";
12260       fmt = "n,~(b)";
12261       offbits = 12;
12262 
12263     ld_st:
12264       tempreg = AT;
12265     ld_noat:
12266       breg = ll_sc_paired ? op[3] : op[2];
12267       if (small_offset_p (0, align, 16))
12268 	{
12269 	  /* The first case exists for M_LD_AB and M_SD_AB, which are
12270 	     macros for o32 but which should act like normal instructions
12271 	     otherwise.  */
12272 	  if (offbits == 16)
12273 	    macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
12274 			 offset_reloc[1], offset_reloc[2], breg);
12275 	  else if (small_offset_p (0, align, offbits))
12276 	    {
12277 	      if (offbits == 0)
12278 		{
12279 		  if (ll_sc_paired)
12280 		   macro_build (NULL, s, fmt, op[0], op[1], breg);
12281 		  else
12282 		   macro_build (NULL, s, fmt, op[0], breg);
12283 		}
12284 	      else
12285 		macro_build (NULL, s, fmt, op[0],
12286 			     (int) offset_expr.X_add_number, breg);
12287 	    }
12288 	  else
12289 	    {
12290 	      if (tempreg == AT)
12291 		used_at = 1;
12292 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12293 			   tempreg, breg, -1, offset_reloc[0],
12294 			   offset_reloc[1], offset_reloc[2]);
12295 	      if (offbits == 0)
12296 		{
12297 		  if (ll_sc_paired)
12298 		    macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12299 		  else
12300 		    macro_build (NULL, s, fmt, op[0], tempreg);
12301 		}
12302 	      else
12303 		macro_build (NULL, s, fmt, op[0], 0, tempreg);
12304 	    }
12305 	  break;
12306 	}
12307 
12308       if (tempreg == AT)
12309 	used_at = 1;
12310 
12311       if (offset_expr.X_op != O_constant
12312 	  && offset_expr.X_op != O_symbol)
12313 	{
12314 	  as_bad (_("expression too complex"));
12315 	  offset_expr.X_op = O_constant;
12316 	}
12317 
12318       if (HAVE_32BIT_ADDRESSES
12319 	  && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12320 	{
12321 	  char value [32];
12322 
12323 	  sprintf_vma (value, offset_expr.X_add_number);
12324 	  as_bad (_("number (0x%s) larger than 32 bits"), value);
12325 	}
12326 
12327       /* A constant expression in PIC code can be handled just as it
12328 	 is in non PIC code.  */
12329       if (offset_expr.X_op == O_constant)
12330 	{
12331 	  expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12332 						 offbits == 0 ? 16 : offbits);
12333 	  offset_expr.X_add_number -= expr1.X_add_number;
12334 
12335 	  load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12336 	  if (breg != 0)
12337 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12338 			 tempreg, tempreg, breg);
12339 	  if (offbits == 0)
12340 	    {
12341 	      if (offset_expr.X_add_number != 0)
12342 		macro_build (&offset_expr, ADDRESS_ADDI_INSN,
12343 			     "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
12344 	      if (ll_sc_paired)
12345 		macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12346 	      else
12347 		macro_build (NULL, s, fmt, op[0], tempreg);
12348 	    }
12349 	  else if (offbits == 16)
12350 	    macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12351 	  else
12352 	    macro_build (NULL, s, fmt, op[0],
12353 			 (int) offset_expr.X_add_number, tempreg);
12354 	}
12355       else if (offbits != 16)
12356 	{
12357 	  /* The offset field is too narrow to be used for a low-part
12358 	     relocation, so load the whole address into the auxiliary
12359 	     register.  */
12360 	  load_address (tempreg, &offset_expr, &used_at);
12361 	  if (breg != 0)
12362 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12363 			 tempreg, tempreg, breg);
12364 	  if (offbits == 0)
12365 	    {
12366 	      if (ll_sc_paired)
12367 		macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12368 	      else
12369 		macro_build (NULL, s, fmt, op[0], tempreg);
12370 	    }
12371 	  else
12372 	    macro_build (NULL, s, fmt, op[0], 0, tempreg);
12373 	}
12374       else if (mips_pic == NO_PIC)
12375 	{
12376 	  /* If this is a reference to a GP relative symbol, and there
12377 	     is no base register, we want
12378 	       <op>	op[0],<sym>($gp)	(BFD_RELOC_GPREL16)
12379 	     Otherwise, if there is no base register, we want
12380 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
12381 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12382 	     If we have a constant, we need two instructions anyhow,
12383 	     so we always use the latter form.
12384 
12385 	     If we have a base register, and this is a reference to a
12386 	     GP relative symbol, we want
12387 	       addu	$tempreg,$breg,$gp
12388 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_GPREL16)
12389 	     Otherwise we want
12390 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
12391 	       addu	$tempreg,$tempreg,$breg
12392 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12393 	     With a constant we always use the latter case.
12394 
12395 	     With 64bit address space and no base register and $at usable,
12396 	     we want
12397 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
12398 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
12399 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
12400 	       dsll32	$tempreg,0
12401 	       daddu	$tempreg,$at
12402 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12403 	     If we have a base register, we want
12404 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
12405 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
12406 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
12407 	       daddu	$at,$breg
12408 	       dsll32	$tempreg,0
12409 	       daddu	$tempreg,$at
12410 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12411 
12412 	     Without $at we can't generate the optimal path for superscalar
12413 	     processors here since this would require two temporary registers.
12414 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
12415 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
12416 	       dsll	$tempreg,16
12417 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
12418 	       dsll	$tempreg,16
12419 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12420 	     If we have a base register, we want
12421 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
12422 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
12423 	       dsll	$tempreg,16
12424 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
12425 	       dsll	$tempreg,16
12426 	       daddu	$tempreg,$tempreg,$breg
12427 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12428 
12429 	     For GP relative symbols in 64bit address space we can use
12430 	     the same sequence as in 32bit address space.  */
12431 	  if (HAVE_64BIT_SYMBOLS)
12432 	    {
12433 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12434 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12435 		{
12436 		  relax_start (offset_expr.X_add_symbol);
12437 		  if (breg == 0)
12438 		    {
12439 		      macro_build (&offset_expr, s, fmt, op[0],
12440 				   BFD_RELOC_GPREL16, mips_gp_register);
12441 		    }
12442 		  else
12443 		    {
12444 		      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12445 				   tempreg, breg, mips_gp_register);
12446 		      macro_build (&offset_expr, s, fmt, op[0],
12447 				   BFD_RELOC_GPREL16, tempreg);
12448 		    }
12449 		  relax_switch ();
12450 		}
12451 
12452 	      if (used_at == 0 && mips_opts.at)
12453 		{
12454 		  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12455 			       BFD_RELOC_MIPS_HIGHEST);
12456 		  macro_build (&offset_expr, "lui", LUI_FMT, AT,
12457 			       BFD_RELOC_HI16_S);
12458 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12459 			       tempreg, BFD_RELOC_MIPS_HIGHER);
12460 		  if (breg != 0)
12461 		    macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
12462 		  macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
12463 		  macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
12464 		  macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
12465 			       tempreg);
12466 		  used_at = 1;
12467 		}
12468 	      else
12469 		{
12470 		  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12471 			       BFD_RELOC_MIPS_HIGHEST);
12472 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12473 			       tempreg, BFD_RELOC_MIPS_HIGHER);
12474 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12475 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12476 			       tempreg, BFD_RELOC_HI16_S);
12477 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12478 		  if (breg != 0)
12479 		    macro_build (NULL, "daddu", "d,v,t",
12480 				 tempreg, tempreg, breg);
12481 		  macro_build (&offset_expr, s, fmt, op[0],
12482 			       BFD_RELOC_LO16, tempreg);
12483 		}
12484 
12485 	      if (mips_relax.sequence)
12486 		relax_end ();
12487 	      break;
12488 	    }
12489 
12490 	  if (breg == 0)
12491 	    {
12492 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12493 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12494 		{
12495 		  relax_start (offset_expr.X_add_symbol);
12496 		  macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
12497 			       mips_gp_register);
12498 		  relax_switch ();
12499 		}
12500 	      macro_build_lui (&offset_expr, tempreg);
12501 	      macro_build (&offset_expr, s, fmt, op[0],
12502 			   BFD_RELOC_LO16, tempreg);
12503 	      if (mips_relax.sequence)
12504 		relax_end ();
12505 	    }
12506 	  else
12507 	    {
12508 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12509 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12510 		{
12511 		  relax_start (offset_expr.X_add_symbol);
12512 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12513 			       tempreg, breg, mips_gp_register);
12514 		  macro_build (&offset_expr, s, fmt, op[0],
12515 			       BFD_RELOC_GPREL16, tempreg);
12516 		  relax_switch ();
12517 		}
12518 	      macro_build_lui (&offset_expr, tempreg);
12519 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12520 			   tempreg, tempreg, breg);
12521 	      macro_build (&offset_expr, s, fmt, op[0],
12522 			   BFD_RELOC_LO16, tempreg);
12523 	      if (mips_relax.sequence)
12524 		relax_end ();
12525 	    }
12526 	}
12527       else if (!mips_big_got)
12528 	{
12529 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
12530 
12531 	  /* If this is a reference to an external symbol, we want
12532 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
12533 	       nop
12534 	       <op>	op[0],0($tempreg)
12535 	     Otherwise we want
12536 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
12537 	       nop
12538 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
12539 	       <op>	op[0],0($tempreg)
12540 
12541 	     For NewABI, we want
12542 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_PAGE)
12543 	       <op>	op[0],<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
12544 
12545 	     If there is a base register, we add it to $tempreg before
12546 	     the <op>.  If there is a constant, we stick it in the
12547 	     <op> instruction.  We don't handle constants larger than
12548 	     16 bits, because we have no way to load the upper 16 bits
12549 	     (actually, we could handle them for the subset of cases
12550 	     in which we are not using $at).  */
12551 	  gas_assert (offset_expr.X_op == O_symbol);
12552 	  if (HAVE_NEWABI)
12553 	    {
12554 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12555 			   BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12556 	      if (breg != 0)
12557 		macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12558 			     tempreg, tempreg, breg);
12559 	      macro_build (&offset_expr, s, fmt, op[0],
12560 			   BFD_RELOC_MIPS_GOT_OFST, tempreg);
12561 	      break;
12562 	    }
12563 	  expr1.X_add_number = offset_expr.X_add_number;
12564 	  offset_expr.X_add_number = 0;
12565 	  if (expr1.X_add_number < -0x8000
12566 	      || expr1.X_add_number >= 0x8000)
12567 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12568 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12569 		       lw_reloc_type, mips_gp_register);
12570 	  load_delay_nop ();
12571 	  relax_start (offset_expr.X_add_symbol);
12572 	  relax_switch ();
12573 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12574 		       tempreg, BFD_RELOC_LO16);
12575 	  relax_end ();
12576 	  if (breg != 0)
12577 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12578 			 tempreg, tempreg, breg);
12579 	  macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12580 	}
12581       else if (mips_big_got && !HAVE_NEWABI)
12582 	{
12583 	  int gpdelay;
12584 
12585 	  /* If this is a reference to an external symbol, we want
12586 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
12587 	       addu	$tempreg,$tempreg,$gp
12588 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12589 	       <op>	op[0],0($tempreg)
12590 	     Otherwise we want
12591 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
12592 	       nop
12593 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
12594 	       <op>	op[0],0($tempreg)
12595 	     If there is a base register, we add it to $tempreg before
12596 	     the <op>.  If there is a constant, we stick it in the
12597 	     <op> instruction.  We don't handle constants larger than
12598 	     16 bits, because we have no way to load the upper 16 bits
12599 	     (actually, we could handle them for the subset of cases
12600 	     in which we are not using $at).  */
12601 	  gas_assert (offset_expr.X_op == O_symbol);
12602 	  expr1.X_add_number = offset_expr.X_add_number;
12603 	  offset_expr.X_add_number = 0;
12604 	  if (expr1.X_add_number < -0x8000
12605 	      || expr1.X_add_number >= 0x8000)
12606 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12607 	  gpdelay = reg_needs_delay (mips_gp_register);
12608 	  relax_start (offset_expr.X_add_symbol);
12609 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12610 		       BFD_RELOC_MIPS_GOT_HI16);
12611 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12612 		       mips_gp_register);
12613 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12614 		       BFD_RELOC_MIPS_GOT_LO16, tempreg);
12615 	  relax_switch ();
12616 	  if (gpdelay)
12617 	    macro_build (NULL, "nop", "");
12618 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12619 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
12620 	  load_delay_nop ();
12621 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12622 		       tempreg, BFD_RELOC_LO16);
12623 	  relax_end ();
12624 
12625 	  if (breg != 0)
12626 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12627 			 tempreg, tempreg, breg);
12628 	  macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12629 	}
12630       else if (mips_big_got && HAVE_NEWABI)
12631 	{
12632 	  /* If this is a reference to an external symbol, we want
12633 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
12634 	       add	$tempreg,$tempreg,$gp
12635 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12636 	       <op>	op[0],<ofst>($tempreg)
12637 	     Otherwise, for local symbols, we want:
12638 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_PAGE)
12639 	       <op>	op[0],<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
12640 	  gas_assert (offset_expr.X_op == O_symbol);
12641 	  expr1.X_add_number = offset_expr.X_add_number;
12642 	  offset_expr.X_add_number = 0;
12643 	  if (expr1.X_add_number < -0x8000
12644 	      || expr1.X_add_number >= 0x8000)
12645 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12646 	  relax_start (offset_expr.X_add_symbol);
12647 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12648 		       BFD_RELOC_MIPS_GOT_HI16);
12649 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12650 		       mips_gp_register);
12651 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12652 		       BFD_RELOC_MIPS_GOT_LO16, tempreg);
12653 	  if (breg != 0)
12654 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12655 			 tempreg, tempreg, breg);
12656 	  macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12657 
12658 	  relax_switch ();
12659 	  offset_expr.X_add_number = expr1.X_add_number;
12660 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12661 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12662 	  if (breg != 0)
12663 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12664 			 tempreg, tempreg, breg);
12665 	  macro_build (&offset_expr, s, fmt, op[0],
12666 		       BFD_RELOC_MIPS_GOT_OFST, tempreg);
12667 	  relax_end ();
12668 	}
12669       else
12670 	abort ();
12671 
12672       break;
12673 
12674     case M_JRADDIUSP:
12675       gas_assert (mips_opts.micromips);
12676       gas_assert (mips_opts.insn32);
12677       start_noreorder ();
12678       macro_build (NULL, "jr", "s", RA);
12679       expr1.X_add_number = op[0] << 2;
12680       macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12681       end_noreorder ();
12682       break;
12683 
12684     case M_JRC:
12685       gas_assert (mips_opts.micromips);
12686       gas_assert (mips_opts.insn32);
12687       macro_build (NULL, "jr", "s", op[0]);
12688       if (mips_opts.noreorder)
12689 	macro_build (NULL, "nop", "");
12690       break;
12691 
12692     case M_LI:
12693     case M_LI_S:
12694       load_register (op[0], &imm_expr, 0);
12695       break;
12696 
12697     case M_DLI:
12698       load_register (op[0], &imm_expr, 1);
12699       break;
12700 
12701     case M_LI_SS:
12702       if (imm_expr.X_op == O_constant)
12703 	{
12704 	  used_at = 1;
12705 	  load_register (AT, &imm_expr, 0);
12706 	  macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12707 	  break;
12708 	}
12709       else
12710 	{
12711 	  gas_assert (imm_expr.X_op == O_absent
12712 		      && offset_expr.X_op == O_symbol
12713 		      && strcmp (segment_name (S_GET_SEGMENT
12714 					       (offset_expr.X_add_symbol)),
12715 				 ".lit4") == 0
12716 		      && offset_expr.X_add_number == 0);
12717 	  macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
12718 		       BFD_RELOC_MIPS_LITERAL, mips_gp_register);
12719 	  break;
12720 	}
12721 
12722     case M_LI_D:
12723       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
12724          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
12725          order 32 bits of the value and the low order 32 bits are either
12726          zero or in OFFSET_EXPR.  */
12727       if (imm_expr.X_op == O_constant)
12728 	{
12729 	  if (GPR_SIZE == 64)
12730 	    load_register (op[0], &imm_expr, 1);
12731 	  else
12732 	    {
12733 	      int hreg, lreg;
12734 
12735 	      if (target_big_endian)
12736 		{
12737 		  hreg = op[0];
12738 		  lreg = op[0] + 1;
12739 		}
12740 	      else
12741 		{
12742 		  hreg = op[0] + 1;
12743 		  lreg = op[0];
12744 		}
12745 
12746 	      if (hreg <= 31)
12747 		load_register (hreg, &imm_expr, 0);
12748 	      if (lreg <= 31)
12749 		{
12750 		  if (offset_expr.X_op == O_absent)
12751 		    move_register (lreg, 0);
12752 		  else
12753 		    {
12754 		      gas_assert (offset_expr.X_op == O_constant);
12755 		      load_register (lreg, &offset_expr, 0);
12756 		    }
12757 		}
12758 	    }
12759 	  break;
12760 	}
12761       gas_assert (imm_expr.X_op == O_absent);
12762 
12763       /* We know that sym is in the .rdata section.  First we get the
12764 	 upper 16 bits of the address.  */
12765       if (mips_pic == NO_PIC)
12766 	{
12767 	  macro_build_lui (&offset_expr, AT);
12768 	  used_at = 1;
12769 	}
12770       else
12771 	{
12772 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12773 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
12774 	  used_at = 1;
12775 	}
12776 
12777       /* Now we load the register(s).  */
12778       if (GPR_SIZE == 64)
12779 	{
12780 	  used_at = 1;
12781 	  macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12782 		       BFD_RELOC_LO16, AT);
12783 	}
12784       else
12785 	{
12786 	  used_at = 1;
12787 	  macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12788 		       BFD_RELOC_LO16, AT);
12789 	  if (op[0] != RA)
12790 	    {
12791 	      /* FIXME: How in the world do we deal with the possible
12792 		 overflow here?  */
12793 	      offset_expr.X_add_number += 4;
12794 	      macro_build (&offset_expr, "lw", "t,o(b)",
12795 			   op[0] + 1, BFD_RELOC_LO16, AT);
12796 	    }
12797 	}
12798       break;
12799 
12800     case M_LI_DD:
12801       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
12802          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12803          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
12804          the value and the low order 32 bits are either zero or in
12805          OFFSET_EXPR.  */
12806       if (imm_expr.X_op == O_constant)
12807 	{
12808 	  tempreg = ZERO;
12809 	  if (((FPR_SIZE == 64 && GPR_SIZE == 64)
12810 	       || !ISA_HAS_MXHC1 (mips_opts.isa))
12811 	      && imm_expr.X_add_number != 0)
12812 	    {
12813 	      used_at = 1;
12814 	      tempreg = AT;
12815 	      load_register (AT, &imm_expr, FPR_SIZE == 64);
12816 	    }
12817 	  if (FPR_SIZE == 64 && GPR_SIZE == 64)
12818 	    macro_build (NULL, "dmtc1", "t,S", tempreg, op[0]);
12819 	  else
12820 	    {
12821 	      if (!ISA_HAS_MXHC1 (mips_opts.isa))
12822 		{
12823 		  if (FPR_SIZE != 32)
12824 		    as_bad (_("Unable to generate `%s' compliant code "
12825 			      "without mthc1"),
12826 			    (FPR_SIZE == 64) ? "fp64" : "fpxx");
12827 		  else
12828 		    macro_build (NULL, "mtc1", "t,G", tempreg, op[0] + 1);
12829 		}
12830 	      if (offset_expr.X_op == O_absent)
12831 		macro_build (NULL, "mtc1", "t,G", 0, op[0]);
12832 	      else
12833 		{
12834 		  gas_assert (offset_expr.X_op == O_constant);
12835 		  load_register (AT, &offset_expr, 0);
12836 		  macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12837 		}
12838 	      if (ISA_HAS_MXHC1 (mips_opts.isa))
12839 		{
12840 		  if (imm_expr.X_add_number != 0)
12841 		    {
12842 		      used_at = 1;
12843 		      tempreg = AT;
12844 		      load_register (AT, &imm_expr, 0);
12845 		    }
12846 		  macro_build (NULL, "mthc1", "t,G", tempreg, op[0]);
12847 		}
12848 	    }
12849 	  break;
12850 	}
12851 
12852       gas_assert (imm_expr.X_op == O_absent
12853 		  && offset_expr.X_op == O_symbol
12854 		  && offset_expr.X_add_number == 0);
12855       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12856       if (strcmp (s, ".lit8") == 0)
12857 	{
12858 	  op[2] = mips_gp_register;
12859 	  offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12860 	  offset_reloc[1] = BFD_RELOC_UNUSED;
12861 	  offset_reloc[2] = BFD_RELOC_UNUSED;
12862 	}
12863       else
12864 	{
12865 	  gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
12866 	  used_at = 1;
12867 	  if (mips_pic != NO_PIC)
12868 	    macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12869 			 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12870 	  else
12871 	    {
12872 	      /* FIXME: This won't work for a 64 bit address.  */
12873 	      macro_build_lui (&offset_expr, AT);
12874 	    }
12875 
12876 	  op[2] = AT;
12877 	  offset_reloc[0] = BFD_RELOC_LO16;
12878 	  offset_reloc[1] = BFD_RELOC_UNUSED;
12879 	  offset_reloc[2] = BFD_RELOC_UNUSED;
12880 	}
12881       align = 8;
12882       /* Fall through.  */
12883 
12884     case M_L_DAB:
12885       /* The MIPS assembler seems to check for X_add_number not
12886          being double aligned and generating:
12887         	lui	at,%hi(foo+1)
12888         	addu	at,at,v1
12889         	addiu	at,at,%lo(foo+1)
12890         	lwc1	f2,0(at)
12891         	lwc1	f3,4(at)
12892          But, the resulting address is the same after relocation so why
12893          generate the extra instruction?  */
12894       /* Itbl support may require additional care here.  */
12895       coproc = 1;
12896       fmt = "T,o(b)";
12897       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12898 	{
12899 	  s = "ldc1";
12900 	  goto ld_st;
12901 	}
12902       s = "lwc1";
12903       goto ldd_std;
12904 
12905     case M_S_DAB:
12906       gas_assert (!mips_opts.micromips);
12907       /* Itbl support may require additional care here.  */
12908       coproc = 1;
12909       fmt = "T,o(b)";
12910       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12911 	{
12912 	  s = "sdc1";
12913 	  goto ld_st;
12914 	}
12915       s = "swc1";
12916       goto ldd_std;
12917 
12918     case M_LQ_AB:
12919       fmt = "t,o(b)";
12920       s = "lq";
12921       goto ld;
12922 
12923     case M_SQ_AB:
12924       fmt = "t,o(b)";
12925       s = "sq";
12926       goto ld_st;
12927 
12928     case M_LD_AB:
12929       fmt = "t,o(b)";
12930       if (GPR_SIZE == 64)
12931 	{
12932 	  s = "ld";
12933 	  goto ld;
12934 	}
12935       s = "lw";
12936       goto ldd_std;
12937 
12938     case M_SD_AB:
12939       fmt = "t,o(b)";
12940       if (GPR_SIZE == 64)
12941 	{
12942 	  s = "sd";
12943 	  goto ld_st;
12944 	}
12945       s = "sw";
12946 
12947     ldd_std:
12948       /* Even on a big endian machine $fn comes before $fn+1.  We have
12949 	 to adjust when loading from memory.  We set coproc if we must
12950 	 load $fn+1 first.  */
12951       /* Itbl support may require additional care here.  */
12952       if (!target_big_endian)
12953 	coproc = 0;
12954 
12955       breg = op[2];
12956       if (small_offset_p (0, align, 16))
12957 	{
12958 	  ep = &offset_expr;
12959 	  if (!small_offset_p (4, align, 16))
12960 	    {
12961 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12962 			   -1, offset_reloc[0], offset_reloc[1],
12963 			   offset_reloc[2]);
12964 	      expr1.X_add_number = 0;
12965 	      ep = &expr1;
12966 	      breg = AT;
12967 	      used_at = 1;
12968 	      offset_reloc[0] = BFD_RELOC_LO16;
12969 	      offset_reloc[1] = BFD_RELOC_UNUSED;
12970 	      offset_reloc[2] = BFD_RELOC_UNUSED;
12971 	    }
12972 	  if (strcmp (s, "lw") == 0 && op[0] == breg)
12973 	    {
12974 	      ep->X_add_number += 4;
12975 	      macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
12976 			   offset_reloc[1], offset_reloc[2], breg);
12977 	      ep->X_add_number -= 4;
12978 	      macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
12979 			   offset_reloc[1], offset_reloc[2], breg);
12980 	    }
12981 	  else
12982 	    {
12983 	      macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
12984 			   offset_reloc[0], offset_reloc[1], offset_reloc[2],
12985 			   breg);
12986 	      ep->X_add_number += 4;
12987 	      macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
12988 			   offset_reloc[0], offset_reloc[1], offset_reloc[2],
12989 			   breg);
12990 	    }
12991 	  break;
12992 	}
12993 
12994       if (offset_expr.X_op != O_symbol
12995 	  && offset_expr.X_op != O_constant)
12996 	{
12997 	  as_bad (_("expression too complex"));
12998 	  offset_expr.X_op = O_constant;
12999 	}
13000 
13001       if (HAVE_32BIT_ADDRESSES
13002 	  && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
13003 	{
13004 	  char value [32];
13005 
13006 	  sprintf_vma (value, offset_expr.X_add_number);
13007 	  as_bad (_("number (0x%s) larger than 32 bits"), value);
13008 	}
13009 
13010       if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
13011 	{
13012 	  /* If this is a reference to a GP relative symbol, we want
13013 	       <op>	op[0],<sym>($gp)	(BFD_RELOC_GPREL16)
13014 	       <op>	op[0]+1,<sym>+4($gp)	(BFD_RELOC_GPREL16)
13015 	     If we have a base register, we use this
13016 	       addu	$at,$breg,$gp
13017 	       <op>	op[0],<sym>($at)	(BFD_RELOC_GPREL16)
13018 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_GPREL16)
13019 	     If this is not a GP relative symbol, we want
13020 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
13021 	       <op>	op[0],<sym>($at)	(BFD_RELOC_LO16)
13022 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_LO16)
13023 	     If there is a base register, we add it to $at after the
13024 	     lui instruction.  If there is a constant, we always use
13025 	     the last case.  */
13026 	  if (offset_expr.X_op == O_symbol
13027 	      && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
13028 	      && !nopic_need_relax (offset_expr.X_add_symbol, 1))
13029 	    {
13030 	      relax_start (offset_expr.X_add_symbol);
13031 	      if (breg == 0)
13032 		{
13033 		  tempreg = mips_gp_register;
13034 		}
13035 	      else
13036 		{
13037 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13038 			       AT, breg, mips_gp_register);
13039 		  tempreg = AT;
13040 		  used_at = 1;
13041 		}
13042 
13043 	      /* Itbl support may require additional care here.  */
13044 	      macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13045 			   BFD_RELOC_GPREL16, tempreg);
13046 	      offset_expr.X_add_number += 4;
13047 
13048 	      /* Set mips_optimize to 2 to avoid inserting an
13049                  undesired nop.  */
13050 	      hold_mips_optimize = mips_optimize;
13051 	      mips_optimize = 2;
13052 	      /* Itbl support may require additional care here.  */
13053 	      macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13054 			   BFD_RELOC_GPREL16, tempreg);
13055 	      mips_optimize = hold_mips_optimize;
13056 
13057 	      relax_switch ();
13058 
13059 	      offset_expr.X_add_number -= 4;
13060 	    }
13061 	  used_at = 1;
13062 	  if (offset_high_part (offset_expr.X_add_number, 16)
13063 	      != offset_high_part (offset_expr.X_add_number + 4, 16))
13064 	    {
13065 	      load_address (AT, &offset_expr, &used_at);
13066 	      offset_expr.X_op = O_constant;
13067 	      offset_expr.X_add_number = 0;
13068 	    }
13069 	  else
13070 	    macro_build_lui (&offset_expr, AT);
13071 	  if (breg != 0)
13072 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13073 	  /* Itbl support may require additional care here.  */
13074 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13075 		       BFD_RELOC_LO16, AT);
13076 	  /* FIXME: How do we handle overflow here?  */
13077 	  offset_expr.X_add_number += 4;
13078 	  /* Itbl support may require additional care here.  */
13079 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13080 		       BFD_RELOC_LO16, AT);
13081 	  if (mips_relax.sequence)
13082 	    relax_end ();
13083 	}
13084       else if (!mips_big_got)
13085 	{
13086 	  /* If this is a reference to an external symbol, we want
13087 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
13088 	       nop
13089 	       <op>	op[0],0($at)
13090 	       <op>	op[0]+1,4($at)
13091 	     Otherwise we want
13092 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
13093 	       nop
13094 	       <op>	op[0],<sym>($at)	(BFD_RELOC_LO16)
13095 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_LO16)
13096 	     If there is a base register we add it to $at before the
13097 	     lwc1 instructions.  If there is a constant we include it
13098 	     in the lwc1 instructions.  */
13099 	  used_at = 1;
13100 	  expr1.X_add_number = offset_expr.X_add_number;
13101 	  if (expr1.X_add_number < -0x8000
13102 	      || expr1.X_add_number >= 0x8000 - 4)
13103 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
13104 	  load_got_offset (AT, &offset_expr);
13105 	  load_delay_nop ();
13106 	  if (breg != 0)
13107 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13108 
13109 	  /* Set mips_optimize to 2 to avoid inserting an undesired
13110              nop.  */
13111 	  hold_mips_optimize = mips_optimize;
13112 	  mips_optimize = 2;
13113 
13114 	  /* Itbl support may require additional care here.  */
13115 	  relax_start (offset_expr.X_add_symbol);
13116 	  macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
13117 		       BFD_RELOC_LO16, AT);
13118 	  expr1.X_add_number += 4;
13119 	  macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
13120 		       BFD_RELOC_LO16, AT);
13121 	  relax_switch ();
13122 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13123 		       BFD_RELOC_LO16, AT);
13124 	  offset_expr.X_add_number += 4;
13125 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13126 		       BFD_RELOC_LO16, AT);
13127 	  relax_end ();
13128 
13129 	  mips_optimize = hold_mips_optimize;
13130 	}
13131       else if (mips_big_got)
13132 	{
13133 	  int gpdelay;
13134 
13135 	  /* If this is a reference to an external symbol, we want
13136 	       lui	$at,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
13137 	       addu	$at,$at,$gp
13138 	       lw	$at,<sym>($at)		(BFD_RELOC_MIPS_GOT_LO16)
13139 	       nop
13140 	       <op>	op[0],0($at)
13141 	       <op>	op[0]+1,4($at)
13142 	     Otherwise we want
13143 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
13144 	       nop
13145 	       <op>	op[0],<sym>($at)	(BFD_RELOC_LO16)
13146 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_LO16)
13147 	     If there is a base register we add it to $at before the
13148 	     lwc1 instructions.  If there is a constant we include it
13149 	     in the lwc1 instructions.  */
13150 	  used_at = 1;
13151 	  expr1.X_add_number = offset_expr.X_add_number;
13152 	  offset_expr.X_add_number = 0;
13153 	  if (expr1.X_add_number < -0x8000
13154 	      || expr1.X_add_number >= 0x8000 - 4)
13155 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
13156 	  gpdelay = reg_needs_delay (mips_gp_register);
13157 	  relax_start (offset_expr.X_add_symbol);
13158 	  macro_build (&offset_expr, "lui", LUI_FMT,
13159 		       AT, BFD_RELOC_MIPS_GOT_HI16);
13160 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13161 		       AT, AT, mips_gp_register);
13162 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
13163 		       AT, BFD_RELOC_MIPS_GOT_LO16, AT);
13164 	  load_delay_nop ();
13165 	  if (breg != 0)
13166 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13167 	  /* Itbl support may require additional care here.  */
13168 	  macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
13169 		       BFD_RELOC_LO16, AT);
13170 	  expr1.X_add_number += 4;
13171 
13172 	  /* Set mips_optimize to 2 to avoid inserting an undesired
13173              nop.  */
13174 	  hold_mips_optimize = mips_optimize;
13175 	  mips_optimize = 2;
13176 	  /* Itbl support may require additional care here.  */
13177 	  macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
13178 		       BFD_RELOC_LO16, AT);
13179 	  mips_optimize = hold_mips_optimize;
13180 	  expr1.X_add_number -= 4;
13181 
13182 	  relax_switch ();
13183 	  offset_expr.X_add_number = expr1.X_add_number;
13184 	  if (gpdelay)
13185 	    macro_build (NULL, "nop", "");
13186 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
13187 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
13188 	  load_delay_nop ();
13189 	  if (breg != 0)
13190 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13191 	  /* Itbl support may require additional care here.  */
13192 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13193 		       BFD_RELOC_LO16, AT);
13194 	  offset_expr.X_add_number += 4;
13195 
13196 	  /* Set mips_optimize to 2 to avoid inserting an undesired
13197              nop.  */
13198 	  hold_mips_optimize = mips_optimize;
13199 	  mips_optimize = 2;
13200 	  /* Itbl support may require additional care here.  */
13201 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13202 		       BFD_RELOC_LO16, AT);
13203 	  mips_optimize = hold_mips_optimize;
13204 	  relax_end ();
13205 	}
13206       else
13207 	abort ();
13208 
13209       break;
13210 
13211     case M_SAA_AB:
13212       s = "saa";
13213       goto saa_saad;
13214     case M_SAAD_AB:
13215       s = "saad";
13216     saa_saad:
13217       gas_assert (!mips_opts.micromips);
13218       offbits = 0;
13219       fmt = "t,(b)";
13220       goto ld_st;
13221 
13222    /* New code added to support COPZ instructions.
13223       This code builds table entries out of the macros in mip_opcodes.
13224       R4000 uses interlocks to handle coproc delays.
13225       Other chips (like the R3000) require nops to be inserted for delays.
13226 
13227       FIXME: Currently, we require that the user handle delays.
13228       In order to fill delay slots for non-interlocked chips,
13229       we must have a way to specify delays based on the coprocessor.
13230       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
13231       What are the side-effects of the cop instruction?
13232       What cache support might we have and what are its effects?
13233       Both coprocessor & memory require delays. how long???
13234       What registers are read/set/modified?
13235 
13236       If an itbl is provided to interpret cop instructions,
13237       this knowledge can be encoded in the itbl spec.  */
13238 
13239     case M_COP0:
13240       s = "c0";
13241       goto copz;
13242     case M_COP1:
13243       s = "c1";
13244       goto copz;
13245     case M_COP2:
13246       s = "c2";
13247       goto copz;
13248     case M_COP3:
13249       s = "c3";
13250     copz:
13251       gas_assert (!mips_opts.micromips);
13252       /* For now we just do C (same as Cz).  The parameter will be
13253          stored in insn_opcode by mips_ip.  */
13254       macro_build (NULL, s, "C", (int) ip->insn_opcode);
13255       break;
13256 
13257     case M_MOVE:
13258       move_register (op[0], op[1]);
13259       break;
13260 
13261     case M_MOVEP:
13262       gas_assert (mips_opts.micromips);
13263       gas_assert (mips_opts.insn32);
13264       move_register (micromips_to_32_reg_h_map1[op[0]],
13265 		     micromips_to_32_reg_m_map[op[1]]);
13266       move_register (micromips_to_32_reg_h_map2[op[0]],
13267 		     micromips_to_32_reg_n_map[op[2]]);
13268       break;
13269 
13270     case M_DMUL:
13271       dbl = 1;
13272       /* Fall through.  */
13273     case M_MUL:
13274       if (mips_opts.arch == CPU_R5900)
13275 	macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13276 		     op[2]);
13277       else
13278         {
13279 	  macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13280 	  macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13281         }
13282       break;
13283 
13284     case M_DMUL_I:
13285       dbl = 1;
13286       /* Fall through.  */
13287     case M_MUL_I:
13288       /* The MIPS assembler some times generates shifts and adds.  I'm
13289 	 not trying to be that fancy. GCC should do this for us
13290 	 anyway.  */
13291       used_at = 1;
13292       load_register (AT, &imm_expr, dbl);
13293       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13294       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13295       break;
13296 
13297     case M_DMULO_I:
13298       dbl = 1;
13299       /* Fall through.  */
13300     case M_MULO_I:
13301       imm = 1;
13302       goto do_mulo;
13303 
13304     case M_DMULO:
13305       dbl = 1;
13306       /* Fall through.  */
13307     case M_MULO:
13308     do_mulo:
13309       start_noreorder ();
13310       used_at = 1;
13311       if (imm)
13312 	load_register (AT, &imm_expr, dbl);
13313       macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13314 		   op[1], imm ? AT : op[2]);
13315       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13316       macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
13317       macro_build (NULL, "mfhi", MFHL_FMT, AT);
13318       if (mips_trap)
13319 	macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
13320       else
13321 	{
13322 	  if (mips_opts.micromips)
13323 	    micromips_label_expr (&label_expr);
13324 	  else
13325 	    label_expr.X_add_number = 8;
13326 	  macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
13327 	  macro_build (NULL, "nop", "");
13328 	  macro_build (NULL, "break", BRK_FMT, 6);
13329 	  if (mips_opts.micromips)
13330 	    micromips_add_label ();
13331 	}
13332       end_noreorder ();
13333       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13334       break;
13335 
13336     case M_DMULOU_I:
13337       dbl = 1;
13338       /* Fall through.  */
13339     case M_MULOU_I:
13340       imm = 1;
13341       goto do_mulou;
13342 
13343     case M_DMULOU:
13344       dbl = 1;
13345       /* Fall through.  */
13346     case M_MULOU:
13347     do_mulou:
13348       start_noreorder ();
13349       used_at = 1;
13350       if (imm)
13351 	load_register (AT, &imm_expr, dbl);
13352       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
13353 		   op[1], imm ? AT : op[2]);
13354       macro_build (NULL, "mfhi", MFHL_FMT, AT);
13355       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13356       if (mips_trap)
13357 	macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
13358       else
13359 	{
13360 	  if (mips_opts.micromips)
13361 	    micromips_label_expr (&label_expr);
13362 	  else
13363 	    label_expr.X_add_number = 8;
13364 	  macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
13365 	  macro_build (NULL, "nop", "");
13366 	  macro_build (NULL, "break", BRK_FMT, 6);
13367 	  if (mips_opts.micromips)
13368 	    micromips_add_label ();
13369 	}
13370       end_noreorder ();
13371       break;
13372 
13373     case M_DROL:
13374       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13375 	{
13376 	  if (op[0] == op[1])
13377 	    {
13378 	      tempreg = AT;
13379 	      used_at = 1;
13380 	    }
13381 	  else
13382 	    tempreg = op[0];
13383 	  macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13384 	  macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
13385 	  break;
13386 	}
13387       used_at = 1;
13388       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13389       macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13390       macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13391       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13392       break;
13393 
13394     case M_ROL:
13395       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13396 	{
13397 	  if (op[0] == op[1])
13398 	    {
13399 	      tempreg = AT;
13400 	      used_at = 1;
13401 	    }
13402 	  else
13403 	    tempreg = op[0];
13404 	  macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13405 	  macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
13406 	  break;
13407 	}
13408       used_at = 1;
13409       macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13410       macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13411       macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13412       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13413       break;
13414 
13415     case M_DROL_I:
13416       {
13417 	unsigned int rot;
13418 	const char *l;
13419 	const char *rr;
13420 
13421 	rot = imm_expr.X_add_number & 0x3f;
13422 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13423 	  {
13424 	    rot = (64 - rot) & 0x3f;
13425 	    if (rot >= 32)
13426 	      macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13427 	    else
13428 	      macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13429 	    break;
13430 	  }
13431 	if (rot == 0)
13432 	  {
13433 	    macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13434 	    break;
13435 	  }
13436 	l = (rot < 0x20) ? "dsll" : "dsll32";
13437 	rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
13438 	rot &= 0x1f;
13439 	used_at = 1;
13440 	macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13441 	macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13442 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13443       }
13444       break;
13445 
13446     case M_ROL_I:
13447       {
13448 	unsigned int rot;
13449 
13450 	rot = imm_expr.X_add_number & 0x1f;
13451 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13452 	  {
13453 	    macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13454 			 (32 - rot) & 0x1f);
13455 	    break;
13456 	  }
13457 	if (rot == 0)
13458 	  {
13459 	    macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13460 	    break;
13461 	  }
13462 	used_at = 1;
13463 	macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13464 	macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13465 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13466       }
13467       break;
13468 
13469     case M_DROR:
13470       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13471 	{
13472 	  macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
13473 	  break;
13474 	}
13475       used_at = 1;
13476       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13477       macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13478       macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13479       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13480       break;
13481 
13482     case M_ROR:
13483       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13484 	{
13485 	  macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
13486 	  break;
13487 	}
13488       used_at = 1;
13489       macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13490       macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13491       macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13492       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13493       break;
13494 
13495     case M_DROR_I:
13496       {
13497 	unsigned int rot;
13498 	const char *l;
13499 	const char *rr;
13500 
13501 	rot = imm_expr.X_add_number & 0x3f;
13502 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13503 	  {
13504 	    if (rot >= 32)
13505 	      macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13506 	    else
13507 	      macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13508 	    break;
13509 	  }
13510 	if (rot == 0)
13511 	  {
13512 	    macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13513 	    break;
13514 	  }
13515 	rr = (rot < 0x20) ? "dsrl" : "dsrl32";
13516 	l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13517 	rot &= 0x1f;
13518 	used_at = 1;
13519 	macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13520 	macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13521 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13522       }
13523       break;
13524 
13525     case M_ROR_I:
13526       {
13527 	unsigned int rot;
13528 
13529 	rot = imm_expr.X_add_number & 0x1f;
13530 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13531 	  {
13532 	    macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
13533 	    break;
13534 	  }
13535 	if (rot == 0)
13536 	  {
13537 	    macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13538 	    break;
13539 	  }
13540 	used_at = 1;
13541 	macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13542 	macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13543 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13544       }
13545       break;
13546 
13547     case M_SEQ:
13548       if (op[1] == 0)
13549 	macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13550       else if (op[2] == 0)
13551 	macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13552       else
13553 	{
13554 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13555 	  macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13556 	}
13557       break;
13558 
13559     case M_SEQ_I:
13560       if (imm_expr.X_add_number == 0)
13561 	{
13562 	  macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13563 	  break;
13564 	}
13565       if (op[1] == 0)
13566 	{
13567 	  as_warn (_("instruction %s: result is always false"),
13568 		   ip->insn_mo->name);
13569 	  move_register (op[0], 0);
13570 	  break;
13571 	}
13572       if (CPU_HAS_SEQ (mips_opts.arch)
13573 	  && -512 <= imm_expr.X_add_number
13574 	  && imm_expr.X_add_number < 512)
13575 	{
13576 	  macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
13577 		       (int) imm_expr.X_add_number);
13578 	  break;
13579 	}
13580       if (imm_expr.X_add_number >= 0
13581 	  && imm_expr.X_add_number < 0x10000)
13582 	macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
13583       else if (imm_expr.X_add_number > -0x8000
13584 	       && imm_expr.X_add_number < 0)
13585 	{
13586 	  imm_expr.X_add_number = -imm_expr.X_add_number;
13587 	  macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13588 		       "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13589 	}
13590       else if (CPU_HAS_SEQ (mips_opts.arch))
13591 	{
13592 	  used_at = 1;
13593 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13594 	  macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
13595 	  break;
13596 	}
13597       else
13598 	{
13599 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13600 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13601 	  used_at = 1;
13602 	}
13603       macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13604       break;
13605 
13606     case M_SGE:		/* X >= Y  <==>  not (X < Y) */
13607       s = "slt";
13608       goto sge;
13609     case M_SGEU:
13610       s = "sltu";
13611     sge:
13612       macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13613       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13614       break;
13615 
13616     case M_SGE_I:	/* X >= I  <==>  not (X < I).  */
13617     case M_SGEU_I:
13618       if (imm_expr.X_add_number >= -0x8000
13619 	  && imm_expr.X_add_number < 0x8000)
13620 	macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13621 		     op[0], op[1], BFD_RELOC_LO16);
13622       else
13623 	{
13624 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13625 	  macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
13626 		       op[0], op[1], AT);
13627 	  used_at = 1;
13628 	}
13629       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13630       break;
13631 
13632     case M_SGT:		/* X > Y  <==>  Y < X.  */
13633       s = "slt";
13634       goto sgt;
13635     case M_SGTU:
13636       s = "sltu";
13637     sgt:
13638       macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13639       break;
13640 
13641     case M_SGT_I:	/* X > I  <==>  I < X.  */
13642       s = "slt";
13643       goto sgti;
13644     case M_SGTU_I:
13645       s = "sltu";
13646     sgti:
13647       used_at = 1;
13648       load_register (AT, &imm_expr, GPR_SIZE == 64);
13649       macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13650       break;
13651 
13652     case M_SLE:		/* X <= Y  <==>  Y >= X  <==>  not (Y < X).  */
13653       s = "slt";
13654       goto sle;
13655     case M_SLEU:
13656       s = "sltu";
13657     sle:
13658       macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13659       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13660       break;
13661 
13662     case M_SLE_I:	/* X <= I  <==>  I >= X  <==>  not (I < X) */
13663       s = "slt";
13664       goto slei;
13665     case M_SLEU_I:
13666       s = "sltu";
13667     slei:
13668       used_at = 1;
13669       load_register (AT, &imm_expr, GPR_SIZE == 64);
13670       macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13671       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13672       break;
13673 
13674     case M_SLT_I:
13675       if (imm_expr.X_add_number >= -0x8000
13676 	  && imm_expr.X_add_number < 0x8000)
13677 	{
13678 	  macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13679 		       BFD_RELOC_LO16);
13680 	  break;
13681 	}
13682       used_at = 1;
13683       load_register (AT, &imm_expr, GPR_SIZE == 64);
13684       macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
13685       break;
13686 
13687     case M_SLTU_I:
13688       if (imm_expr.X_add_number >= -0x8000
13689 	  && imm_expr.X_add_number < 0x8000)
13690 	{
13691 	  macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
13692 		       BFD_RELOC_LO16);
13693 	  break;
13694 	}
13695       used_at = 1;
13696       load_register (AT, &imm_expr, GPR_SIZE == 64);
13697       macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
13698       break;
13699 
13700     case M_SNE:
13701       if (op[1] == 0)
13702 	macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13703       else if (op[2] == 0)
13704 	macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13705       else
13706 	{
13707 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13708 	  macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13709 	}
13710       break;
13711 
13712     case M_SNE_I:
13713       if (imm_expr.X_add_number == 0)
13714 	{
13715 	  macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13716 	  break;
13717 	}
13718       if (op[1] == 0)
13719 	{
13720 	  as_warn (_("instruction %s: result is always true"),
13721 		   ip->insn_mo->name);
13722 	  macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
13723 		       op[0], 0, BFD_RELOC_LO16);
13724 	  break;
13725 	}
13726       if (CPU_HAS_SEQ (mips_opts.arch)
13727 	  && -512 <= imm_expr.X_add_number
13728 	  && imm_expr.X_add_number < 512)
13729 	{
13730 	  macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
13731 		       (int) imm_expr.X_add_number);
13732 	  break;
13733 	}
13734       if (imm_expr.X_add_number >= 0
13735 	  && imm_expr.X_add_number < 0x10000)
13736 	{
13737 	  macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13738 		       BFD_RELOC_LO16);
13739 	}
13740       else if (imm_expr.X_add_number > -0x8000
13741 	       && imm_expr.X_add_number < 0)
13742 	{
13743 	  imm_expr.X_add_number = -imm_expr.X_add_number;
13744 	  macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13745 		       "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13746 	}
13747       else if (CPU_HAS_SEQ (mips_opts.arch))
13748 	{
13749 	  used_at = 1;
13750 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13751 	  macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
13752 	  break;
13753 	}
13754       else
13755 	{
13756 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13757 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13758 	  used_at = 1;
13759 	}
13760       macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13761       break;
13762 
13763     case M_SUB_I:
13764       s = "addi";
13765       s2 = "sub";
13766       if (ISA_IS_R6 (mips_opts.isa))
13767 	goto do_subi_i;
13768       else
13769 	goto do_subi;
13770     case M_SUBU_I:
13771       s = "addiu";
13772       s2 = "subu";
13773       goto do_subi;
13774     case M_DSUB_I:
13775       dbl = 1;
13776       s = "daddi";
13777       s2 = "dsub";
13778       if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
13779 	goto do_subi;
13780       if (imm_expr.X_add_number > -0x200
13781 	  && imm_expr.X_add_number <= 0x200
13782 	  && !ISA_IS_R6 (mips_opts.isa))
13783 	{
13784 	  macro_build (NULL, s, "t,r,.", op[0], op[1],
13785 		       (int) -imm_expr.X_add_number);
13786 	  break;
13787 	}
13788       goto do_subi_i;
13789     case M_DSUBU_I:
13790       dbl = 1;
13791       s = "daddiu";
13792       s2 = "dsubu";
13793     do_subi:
13794       if (imm_expr.X_add_number > -0x8000
13795 	  && imm_expr.X_add_number <= 0x8000)
13796 	{
13797 	  imm_expr.X_add_number = -imm_expr.X_add_number;
13798 	  macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13799 	  break;
13800 	}
13801     do_subi_i:
13802       used_at = 1;
13803       load_register (AT, &imm_expr, dbl);
13804       macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
13805       break;
13806 
13807     case M_TEQ_I:
13808       s = "teq";
13809       goto trap;
13810     case M_TGE_I:
13811       s = "tge";
13812       goto trap;
13813     case M_TGEU_I:
13814       s = "tgeu";
13815       goto trap;
13816     case M_TLT_I:
13817       s = "tlt";
13818       goto trap;
13819     case M_TLTU_I:
13820       s = "tltu";
13821       goto trap;
13822     case M_TNE_I:
13823       s = "tne";
13824     trap:
13825       used_at = 1;
13826       load_register (AT, &imm_expr, GPR_SIZE == 64);
13827       macro_build (NULL, s, "s,t", op[0], AT);
13828       break;
13829 
13830     case M_TRUNCWS:
13831     case M_TRUNCWD:
13832       gas_assert (!mips_opts.micromips);
13833       gas_assert (mips_opts.isa == ISA_MIPS1);
13834       used_at = 1;
13835 
13836       /*
13837        * Is the double cfc1 instruction a bug in the mips assembler;
13838        * or is there a reason for it?
13839        */
13840       start_noreorder ();
13841       macro_build (NULL, "cfc1", "t,g", op[2], FCSR);
13842       macro_build (NULL, "cfc1", "t,g", op[2], FCSR);
13843       macro_build (NULL, "nop", "");
13844       expr1.X_add_number = 3;
13845       macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
13846       expr1.X_add_number = 2;
13847       macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13848       macro_build (NULL, "ctc1", "t,g", AT, FCSR);
13849       macro_build (NULL, "nop", "");
13850       macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
13851 		   op[0], op[1]);
13852       macro_build (NULL, "ctc1", "t,g", op[2], FCSR);
13853       macro_build (NULL, "nop", "");
13854       end_noreorder ();
13855       break;
13856 
13857     case M_ULH_AB:
13858       s = "lb";
13859       s2 = "lbu";
13860       off = 1;
13861       goto uld_st;
13862     case M_ULHU_AB:
13863       s = "lbu";
13864       s2 = "lbu";
13865       off = 1;
13866       goto uld_st;
13867     case M_ULW_AB:
13868       s = "lwl";
13869       s2 = "lwr";
13870       offbits = (mips_opts.micromips ? 12 : 16);
13871       off = 3;
13872       goto uld_st;
13873     case M_ULD_AB:
13874       s = "ldl";
13875       s2 = "ldr";
13876       offbits = (mips_opts.micromips ? 12 : 16);
13877       off = 7;
13878       goto uld_st;
13879     case M_USH_AB:
13880       s = "sb";
13881       s2 = "sb";
13882       off = 1;
13883       ust = 1;
13884       goto uld_st;
13885     case M_USW_AB:
13886       s = "swl";
13887       s2 = "swr";
13888       offbits = (mips_opts.micromips ? 12 : 16);
13889       off = 3;
13890       ust = 1;
13891       goto uld_st;
13892     case M_USD_AB:
13893       s = "sdl";
13894       s2 = "sdr";
13895       offbits = (mips_opts.micromips ? 12 : 16);
13896       off = 7;
13897       ust = 1;
13898 
13899     uld_st:
13900       breg = op[2];
13901       large_offset = !small_offset_p (off, align, offbits);
13902       ep = &offset_expr;
13903       expr1.X_add_number = 0;
13904       if (large_offset)
13905 	{
13906 	  used_at = 1;
13907 	  tempreg = AT;
13908 	  if (small_offset_p (0, align, 16))
13909 	    macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13910 			 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13911 	  else
13912 	    {
13913 	      load_address (tempreg, ep, &used_at);
13914 	      if (breg != 0)
13915 		macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13916 			     tempreg, tempreg, breg);
13917 	    }
13918 	  offset_reloc[0] = BFD_RELOC_LO16;
13919 	  offset_reloc[1] = BFD_RELOC_UNUSED;
13920 	  offset_reloc[2] = BFD_RELOC_UNUSED;
13921 	  breg = tempreg;
13922 	  tempreg = op[0];
13923 	  ep = &expr1;
13924 	}
13925       else if (!ust && op[0] == breg)
13926 	{
13927 	  used_at = 1;
13928 	  tempreg = AT;
13929 	}
13930       else
13931 	tempreg = op[0];
13932 
13933       if (off == 1)
13934 	goto ulh_sh;
13935 
13936       if (!target_big_endian)
13937 	ep->X_add_number += off;
13938       if (offbits == 12)
13939 	macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
13940       else
13941 	macro_build (ep, s, "t,o(b)", tempreg, -1,
13942 		     offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13943 
13944       if (!target_big_endian)
13945 	ep->X_add_number -= off;
13946       else
13947 	ep->X_add_number += off;
13948       if (offbits == 12)
13949 	macro_build (NULL, s2, "t,~(b)",
13950 		     tempreg, (int) ep->X_add_number, breg);
13951       else
13952 	macro_build (ep, s2, "t,o(b)", tempreg, -1,
13953 		     offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13954 
13955       /* If necessary, move the result in tempreg to the final destination.  */
13956       if (!ust && op[0] != tempreg)
13957         {
13958 	  /* Protect second load's delay slot.  */
13959 	  load_delay_nop ();
13960 	  move_register (op[0], tempreg);
13961 	}
13962       break;
13963 
13964     ulh_sh:
13965       used_at = 1;
13966       if (target_big_endian == ust)
13967 	ep->X_add_number += off;
13968       tempreg = ust || large_offset ? op[0] : AT;
13969       macro_build (ep, s, "t,o(b)", tempreg, -1,
13970 		   offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13971 
13972       /* For halfword transfers we need a temporary register to shuffle
13973          bytes.  Unfortunately for M_USH_A we have none available before
13974          the next store as AT holds the base address.  We deal with this
13975          case by clobbering TREG and then restoring it as with ULH.  */
13976       tempreg = ust == large_offset ? op[0] : AT;
13977       if (ust)
13978 	macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
13979 
13980       if (target_big_endian == ust)
13981 	ep->X_add_number -= off;
13982       else
13983 	ep->X_add_number += off;
13984       macro_build (ep, s2, "t,o(b)", tempreg, -1,
13985 		   offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13986 
13987       /* For M_USH_A re-retrieve the LSB.  */
13988       if (ust && large_offset)
13989 	{
13990 	  if (target_big_endian)
13991 	    ep->X_add_number += off;
13992 	  else
13993 	    ep->X_add_number -= off;
13994 	  macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13995 		       offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
13996 	}
13997       /* For ULH and M_USH_A OR the LSB in.  */
13998       if (!ust || large_offset)
13999 	{
14000 	  tempreg = !large_offset ? AT : op[0];
14001 	  macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
14002 	  macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
14003 	}
14004       break;
14005 
14006     default:
14007       /* FIXME: Check if this is one of the itbl macros, since they
14008 	 are added dynamically.  */
14009       as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
14010       break;
14011     }
14012   if (!mips_opts.at && used_at)
14013     as_bad (_("macro used $at after \".set noat\""));
14014 }
14015 
14016 /* Implement macros in mips16 mode.  */
14017 
14018 static void
mips16_macro(struct mips_cl_insn * ip)14019 mips16_macro (struct mips_cl_insn *ip)
14020 {
14021   const struct mips_operand_array *operands;
14022   int mask;
14023   int tmp;
14024   expressionS expr1;
14025   int dbl;
14026   const char *s, *s2, *s3;
14027   unsigned int op[MAX_OPERANDS];
14028   unsigned int i;
14029 
14030   mask = ip->insn_mo->mask;
14031 
14032   operands = insn_operands (ip);
14033   for (i = 0; i < MAX_OPERANDS; i++)
14034     if (operands->operand[i])
14035       op[i] = insn_extract_operand (ip, operands->operand[i]);
14036     else
14037       op[i] = -1;
14038 
14039   expr1.X_op = O_constant;
14040   expr1.X_op_symbol = NULL;
14041   expr1.X_add_symbol = NULL;
14042   expr1.X_add_number = 1;
14043 
14044   dbl = 0;
14045 
14046   switch (mask)
14047     {
14048     default:
14049       abort ();
14050 
14051     case M_DDIV_3:
14052       dbl = 1;
14053       /* Fall through.  */
14054     case M_DIV_3:
14055       s = "mflo";
14056       goto do_div3;
14057     case M_DREM_3:
14058       dbl = 1;
14059       /* Fall through.  */
14060     case M_REM_3:
14061       s = "mfhi";
14062     do_div3:
14063       start_noreorder ();
14064       macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
14065       expr1.X_add_number = 2;
14066       macro_build (&expr1, "bnez", "x,p", op[2]);
14067       macro_build (NULL, "break", "6", 7);
14068 
14069       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
14070          since that causes an overflow.  We should do that as well,
14071          but I don't see how to do the comparisons without a temporary
14072          register.  */
14073       end_noreorder ();
14074       macro_build (NULL, s, "x", op[0]);
14075       break;
14076 
14077     case M_DIVU_3:
14078       s = "divu";
14079       s2 = "mflo";
14080       goto do_divu3;
14081     case M_REMU_3:
14082       s = "divu";
14083       s2 = "mfhi";
14084       goto do_divu3;
14085     case M_DDIVU_3:
14086       s = "ddivu";
14087       s2 = "mflo";
14088       goto do_divu3;
14089     case M_DREMU_3:
14090       s = "ddivu";
14091       s2 = "mfhi";
14092     do_divu3:
14093       start_noreorder ();
14094       macro_build (NULL, s, ".,x,y", op[1], op[2]);
14095       expr1.X_add_number = 2;
14096       macro_build (&expr1, "bnez", "x,p", op[2]);
14097       macro_build (NULL, "break", "6", 7);
14098       end_noreorder ();
14099       macro_build (NULL, s2, "x", op[0]);
14100       break;
14101 
14102     case M_DMUL:
14103       dbl = 1;
14104       /* Fall through.  */
14105     case M_MUL:
14106       macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
14107       macro_build (NULL, "mflo", "x", op[0]);
14108       break;
14109 
14110     case M_DSUBU_I:
14111       dbl = 1;
14112       goto do_subu;
14113     case M_SUBU_I:
14114     do_subu:
14115       imm_expr.X_add_number = -imm_expr.X_add_number;
14116       macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
14117       break;
14118 
14119     case M_SUBU_I_2:
14120       imm_expr.X_add_number = -imm_expr.X_add_number;
14121       macro_build (&imm_expr, "addiu", "x,k", op[0]);
14122       break;
14123 
14124     case M_DSUBU_I_2:
14125       imm_expr.X_add_number = -imm_expr.X_add_number;
14126       macro_build (&imm_expr, "daddiu", "y,j", op[0]);
14127       break;
14128 
14129     case M_BEQ:
14130       s = "cmp";
14131       s2 = "bteqz";
14132       goto do_branch;
14133     case M_BNE:
14134       s = "cmp";
14135       s2 = "btnez";
14136       goto do_branch;
14137     case M_BLT:
14138       s = "slt";
14139       s2 = "btnez";
14140       goto do_branch;
14141     case M_BLTU:
14142       s = "sltu";
14143       s2 = "btnez";
14144       goto do_branch;
14145     case M_BLE:
14146       s = "slt";
14147       s2 = "bteqz";
14148       goto do_reverse_branch;
14149     case M_BLEU:
14150       s = "sltu";
14151       s2 = "bteqz";
14152       goto do_reverse_branch;
14153     case M_BGE:
14154       s = "slt";
14155       s2 = "bteqz";
14156       goto do_branch;
14157     case M_BGEU:
14158       s = "sltu";
14159       s2 = "bteqz";
14160       goto do_branch;
14161     case M_BGT:
14162       s = "slt";
14163       s2 = "btnez";
14164       goto do_reverse_branch;
14165     case M_BGTU:
14166       s = "sltu";
14167       s2 = "btnez";
14168 
14169     do_reverse_branch:
14170       tmp = op[1];
14171       op[1] = op[0];
14172       op[0] = tmp;
14173 
14174     do_branch:
14175       macro_build (NULL, s, "x,y", op[0], op[1]);
14176       macro_build (&offset_expr, s2, "p");
14177       break;
14178 
14179     case M_BEQ_I:
14180       s = "cmpi";
14181       s2 = "bteqz";
14182       s3 = "x,U";
14183       goto do_branch_i;
14184     case M_BNE_I:
14185       s = "cmpi";
14186       s2 = "btnez";
14187       s3 = "x,U";
14188       goto do_branch_i;
14189     case M_BLT_I:
14190       s = "slti";
14191       s2 = "btnez";
14192       s3 = "x,8";
14193       goto do_branch_i;
14194     case M_BLTU_I:
14195       s = "sltiu";
14196       s2 = "btnez";
14197       s3 = "x,8";
14198       goto do_branch_i;
14199     case M_BLE_I:
14200       s = "slti";
14201       s2 = "btnez";
14202       s3 = "x,8";
14203       goto do_addone_branch_i;
14204     case M_BLEU_I:
14205       s = "sltiu";
14206       s2 = "btnez";
14207       s3 = "x,8";
14208       goto do_addone_branch_i;
14209     case M_BGE_I:
14210       s = "slti";
14211       s2 = "bteqz";
14212       s3 = "x,8";
14213       goto do_branch_i;
14214     case M_BGEU_I:
14215       s = "sltiu";
14216       s2 = "bteqz";
14217       s3 = "x,8";
14218       goto do_branch_i;
14219     case M_BGT_I:
14220       s = "slti";
14221       s2 = "bteqz";
14222       s3 = "x,8";
14223       goto do_addone_branch_i;
14224     case M_BGTU_I:
14225       s = "sltiu";
14226       s2 = "bteqz";
14227       s3 = "x,8";
14228 
14229     do_addone_branch_i:
14230       ++imm_expr.X_add_number;
14231 
14232     do_branch_i:
14233       macro_build (&imm_expr, s, s3, op[0]);
14234       macro_build (&offset_expr, s2, "p");
14235       break;
14236 
14237     case M_ABS:
14238       expr1.X_add_number = 0;
14239       macro_build (&expr1, "slti", "x,8", op[1]);
14240       if (op[0] != op[1])
14241 	macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
14242       expr1.X_add_number = 2;
14243       macro_build (&expr1, "bteqz", "p");
14244       macro_build (NULL, "neg", "x,w", op[0], op[0]);
14245       break;
14246     }
14247 }
14248 
14249 /* Look up instruction [START, START + LENGTH) in HASH.  Record any extra
14250    opcode bits in *OPCODE_EXTRA.  */
14251 
14252 static struct mips_opcode *
mips_lookup_insn(htab_t hash,const char * start,ssize_t length,unsigned int * opcode_extra)14253 mips_lookup_insn (htab_t hash, const char *start,
14254 		  ssize_t length, unsigned int *opcode_extra)
14255 {
14256   char *name, *dot, *p;
14257   unsigned int mask, suffix;
14258   ssize_t opend;
14259   struct mips_opcode *insn;
14260 
14261   /* Make a copy of the instruction so that we can fiddle with it.  */
14262   name = xstrndup (start, length);
14263 
14264   /* Look up the instruction as-is.  */
14265   insn = (struct mips_opcode *) str_hash_find (hash, name);
14266   if (insn)
14267     goto end;
14268 
14269   dot = strchr (name, '.');
14270   if (dot && dot[1])
14271     {
14272       /* Try to interpret the text after the dot as a VU0 channel suffix.  */
14273       p = mips_parse_vu0_channels (dot + 1, &mask);
14274       if (*p == 0 && mask != 0)
14275 	{
14276 	  *dot = 0;
14277 	  insn = (struct mips_opcode *) str_hash_find (hash, name);
14278 	  *dot = '.';
14279 	  if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14280 	    {
14281 	      *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
14282 	      goto end;
14283 	    }
14284 	}
14285     }
14286 
14287   if (mips_opts.micromips)
14288     {
14289       /* See if there's an instruction size override suffix,
14290 	 either `16' or `32', at the end of the mnemonic proper,
14291 	 that defines the operation, i.e. before the first `.'
14292 	 character if any.  Strip it and retry.  */
14293       opend = dot != NULL ? dot - name : length;
14294       if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14295 	suffix = 2;
14296       else if (opend >= 2 && name[opend - 2] == '3' && name[opend - 1] == '2')
14297 	suffix = 4;
14298       else
14299 	suffix = 0;
14300       if (suffix)
14301 	{
14302 	  memmove (name + opend - 2, name + opend, length - opend + 1);
14303 	  insn = (struct mips_opcode *) str_hash_find (hash, name);
14304 	  if (insn)
14305 	    {
14306 	      forced_insn_length = suffix;
14307 	      goto end;
14308 	    }
14309 	}
14310     }
14311 
14312   insn = NULL;
14313  end:
14314   free (name);
14315   return insn;
14316 }
14317 
14318 /* Assemble an instruction into its binary format.  If the instruction
14319    is a macro, set imm_expr and offset_expr to the values associated
14320    with "I" and "A" operands respectively.  Otherwise store the value
14321    of the relocatable field (if any) in offset_expr.  In both cases
14322    set offset_reloc to the relocation operators applied to offset_expr.  */
14323 
14324 static void
mips_ip(char * str,struct mips_cl_insn * insn)14325 mips_ip (char *str, struct mips_cl_insn *insn)
14326 {
14327   const struct mips_opcode *first, *past;
14328   htab_t hash;
14329   char format;
14330   size_t end;
14331   struct mips_operand_token *tokens;
14332   unsigned int opcode_extra;
14333 
14334   if (mips_opts.micromips)
14335     {
14336       hash = micromips_op_hash;
14337       past = &micromips_opcodes[bfd_micromips_num_opcodes];
14338     }
14339   else
14340     {
14341       hash = op_hash;
14342       past = &mips_opcodes[NUMOPCODES];
14343     }
14344   forced_insn_length = 0;
14345   opcode_extra = 0;
14346 
14347   /* We first try to match an instruction up to a space or to the end.  */
14348   for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14349     continue;
14350 
14351   first = mips_lookup_insn (hash, str, end, &opcode_extra);
14352   if (first == NULL)
14353     {
14354       set_insn_error (0, _("unrecognized opcode"));
14355       return;
14356     }
14357 
14358   if (strcmp (first->name, "li.s") == 0)
14359     format = 'f';
14360   else if (strcmp (first->name, "li.d") == 0)
14361     format = 'd';
14362   else
14363     format = 0;
14364   tokens = mips_parse_arguments (str + end, format);
14365   if (!tokens)
14366     return;
14367 
14368   if (!match_insns (insn, first, past, tokens, opcode_extra, false)
14369       && !match_insns (insn, first, past, tokens, opcode_extra, true))
14370     set_insn_error (0, _("invalid operands"));
14371 
14372   obstack_free (&mips_operand_tokens, tokens);
14373 }
14374 
14375 /* As for mips_ip, but used when assembling MIPS16 code.
14376    Also set forced_insn_length to the resulting instruction size in
14377    bytes if the user explicitly requested a small or extended instruction.  */
14378 
14379 static void
mips16_ip(char * str,struct mips_cl_insn * insn)14380 mips16_ip (char *str, struct mips_cl_insn *insn)
14381 {
14382   char *end, *s, c;
14383   struct mips_opcode *first;
14384   struct mips_operand_token *tokens;
14385   unsigned int l;
14386 
14387   for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
14388     ;
14389   end = s;
14390   c = *end;
14391 
14392   l = 0;
14393   switch (c)
14394     {
14395     case '\0':
14396       break;
14397 
14398     case ' ':
14399       s++;
14400       break;
14401 
14402     case '.':
14403       s++;
14404       if (*s == 't')
14405 	{
14406 	  l = 2;
14407 	  s++;
14408 	}
14409       else if (*s == 'e')
14410 	{
14411 	  l = 4;
14412 	  s++;
14413 	}
14414       if (*s == '\0')
14415 	break;
14416       else if (*s++ == ' ')
14417 	break;
14418       set_insn_error (0, _("unrecognized opcode"));
14419       return;
14420     }
14421   forced_insn_length = l;
14422 
14423   *end = 0;
14424   first = (struct mips_opcode *) str_hash_find (mips16_op_hash, str);
14425   *end = c;
14426 
14427   if (!first)
14428     {
14429       set_insn_error (0, _("unrecognized opcode"));
14430       return;
14431     }
14432 
14433   tokens = mips_parse_arguments (s, 0);
14434   if (!tokens)
14435     return;
14436 
14437   if (!match_mips16_insns (insn, first, tokens))
14438     set_insn_error (0, _("invalid operands"));
14439 
14440   obstack_free (&mips_operand_tokens, tokens);
14441 }
14442 
14443 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14444    NBITS is the number of significant bits in VAL.  */
14445 
14446 static unsigned long
mips16_immed_extend(offsetT val,unsigned int nbits)14447 mips16_immed_extend (offsetT val, unsigned int nbits)
14448 {
14449   int extval;
14450 
14451   extval = 0;
14452   val &= (1U << nbits) - 1;
14453   if (nbits == 16 || nbits == 9)
14454     {
14455       extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14456       val &= 0x1f;
14457     }
14458   else if (nbits == 15)
14459     {
14460       extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14461       val &= 0xf;
14462     }
14463   else if (nbits == 6)
14464     {
14465       extval = ((val & 0x1f) << 6) | (val & 0x20);
14466       val = 0;
14467     }
14468   return (extval << 16) | val;
14469 }
14470 
14471 /* Like decode_mips16_operand, but require the operand to be defined and
14472    require it to be an integer.  */
14473 
14474 static const struct mips_int_operand *
mips16_immed_operand(int type,bool extended_p)14475 mips16_immed_operand (int type, bool extended_p)
14476 {
14477   const struct mips_operand *operand;
14478 
14479   operand = decode_mips16_operand (type, extended_p);
14480   if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14481     abort ();
14482   return (const struct mips_int_operand *) operand;
14483 }
14484 
14485 /* Return true if SVAL fits OPERAND.  RELOC is as for mips16_immed.  */
14486 
14487 static bool
mips16_immed_in_range_p(const struct mips_int_operand * operand,bfd_reloc_code_real_type reloc,offsetT sval)14488 mips16_immed_in_range_p (const struct mips_int_operand *operand,
14489 			 bfd_reloc_code_real_type reloc, offsetT sval)
14490 {
14491   int min_val, max_val;
14492 
14493   min_val = mips_int_operand_min (operand);
14494   max_val = mips_int_operand_max (operand);
14495   if (reloc != BFD_RELOC_UNUSED)
14496     {
14497       if (min_val < 0)
14498 	sval = SEXT_16BIT (sval);
14499       else
14500 	sval &= 0xffff;
14501     }
14502 
14503   return (sval >= min_val
14504 	  && sval <= max_val
14505 	  && (sval & ((1 << operand->shift) - 1)) == 0);
14506 }
14507 
14508 /* Install immediate value VAL into MIPS16 instruction *INSN,
14509    extending it if necessary.  The instruction in *INSN may
14510    already be extended.
14511 
14512    RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14513    if none.  In the former case, VAL is a 16-bit number with no
14514    defined signedness.
14515 
14516    TYPE is the type of the immediate field.  USER_INSN_LENGTH
14517    is the length that the user requested, or 0 if none.  */
14518 
14519 static void
mips16_immed(const char * file,unsigned int line,int type,bfd_reloc_code_real_type reloc,offsetT val,unsigned int user_insn_length,unsigned long * insn)14520 mips16_immed (const char *file, unsigned int line, int type,
14521 	      bfd_reloc_code_real_type reloc, offsetT val,
14522 	      unsigned int user_insn_length, unsigned long *insn)
14523 {
14524   const struct mips_int_operand *operand;
14525   unsigned int uval, length;
14526 
14527   operand = mips16_immed_operand (type, false);
14528   if (!mips16_immed_in_range_p (operand, reloc, val))
14529     {
14530       /* We need an extended instruction.  */
14531       if (user_insn_length == 2)
14532 	as_bad_where (file, line, _("invalid unextended operand value"));
14533       else
14534 	*insn |= MIPS16_EXTEND;
14535     }
14536   else if (user_insn_length == 4)
14537     {
14538       /* The operand doesn't force an unextended instruction to be extended.
14539 	 Warn if the user wanted an extended instruction anyway.  */
14540       *insn |= MIPS16_EXTEND;
14541       as_warn_where (file, line,
14542 		     _("extended operand requested but not required"));
14543     }
14544 
14545   length = mips16_opcode_length (*insn);
14546   if (length == 4)
14547     {
14548       operand = mips16_immed_operand (type, true);
14549       if (!mips16_immed_in_range_p (operand, reloc, val))
14550 	as_bad_where (file, line,
14551 		      _("operand value out of range for instruction"));
14552     }
14553   uval = ((unsigned int) val >> operand->shift) - operand->bias;
14554   if (length == 2 || operand->root.lsb != 0)
14555     *insn = mips_insert_operand (&operand->root, *insn, uval);
14556   else
14557     *insn |= mips16_immed_extend (uval, operand->root.size);
14558 }
14559 
14560 struct percent_op_match
14561 {
14562   const char *str;
14563   bfd_reloc_code_real_type reloc;
14564 };
14565 
14566 static const struct percent_op_match mips_percent_op[] =
14567 {
14568   {"%lo", BFD_RELOC_LO16},
14569   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14570   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14571   {"%call16", BFD_RELOC_MIPS_CALL16},
14572   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14573   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14574   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14575   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14576   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14577   {"%got", BFD_RELOC_MIPS_GOT16},
14578   {"%gp_rel", BFD_RELOC_GPREL16},
14579   {"%gprel", BFD_RELOC_GPREL16},
14580   {"%half", BFD_RELOC_16},
14581   {"%highest", BFD_RELOC_MIPS_HIGHEST},
14582   {"%higher", BFD_RELOC_MIPS_HIGHER},
14583   {"%neg", BFD_RELOC_MIPS_SUB},
14584   {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14585   {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14586   {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14587   {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14588   {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14589   {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14590   {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14591   {"%hi", BFD_RELOC_HI16_S},
14592   {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14593   {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
14594 };
14595 
14596 static const struct percent_op_match mips16_percent_op[] =
14597 {
14598   {"%lo", BFD_RELOC_MIPS16_LO16},
14599   {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
14600   {"%gprel", BFD_RELOC_MIPS16_GPREL},
14601   {"%got", BFD_RELOC_MIPS16_GOT16},
14602   {"%call16", BFD_RELOC_MIPS16_CALL16},
14603   {"%hi", BFD_RELOC_MIPS16_HI16_S},
14604   {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14605   {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14606   {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14607   {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14608   {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14609   {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14610   {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14611 };
14612 
14613 
14614 /* Return true if *STR points to a relocation operator.  When returning true,
14615    move *STR over the operator and store its relocation code in *RELOC.
14616    Leave both *STR and *RELOC alone when returning false.  */
14617 
14618 static bool
parse_relocation(char ** str,bfd_reloc_code_real_type * reloc)14619 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14620 {
14621   const struct percent_op_match *percent_op;
14622   size_t limit, i;
14623 
14624   if (mips_opts.mips16)
14625     {
14626       percent_op = mips16_percent_op;
14627       limit = ARRAY_SIZE (mips16_percent_op);
14628     }
14629   else
14630     {
14631       percent_op = mips_percent_op;
14632       limit = ARRAY_SIZE (mips_percent_op);
14633     }
14634 
14635   for (i = 0; i < limit; i++)
14636     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14637       {
14638 	int len = strlen (percent_op[i].str);
14639 
14640 	if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14641 	  continue;
14642 
14643 	*str += strlen (percent_op[i].str);
14644 	*reloc = percent_op[i].reloc;
14645 
14646 	/* Check whether the output BFD supports this relocation.
14647 	   If not, issue an error and fall back on something safe.  */
14648 	if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14649 	  {
14650 	    as_bad (_("relocation %s isn't supported by the current ABI"),
14651 		    percent_op[i].str);
14652 	    *reloc = BFD_RELOC_UNUSED;
14653 	  }
14654 	return true;
14655       }
14656   return false;
14657 }
14658 
14659 
14660 /* Parse string STR as a 16-bit relocatable operand.  Store the
14661    expression in *EP and the relocations in the array starting
14662    at RELOC.  Return the number of relocation operators used.
14663 
14664    On exit, EXPR_END points to the first character after the expression.  */
14665 
14666 static size_t
my_getSmallExpression(expressionS * ep,bfd_reloc_code_real_type * reloc,char * str)14667 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14668 		       char *str)
14669 {
14670   bfd_reloc_code_real_type reversed_reloc[3];
14671   size_t reloc_index, i;
14672   int crux_depth, str_depth;
14673   char *crux;
14674 
14675   /* Search for the start of the main expression, recoding relocations
14676      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
14677      of the main expression and with CRUX_DEPTH containing the number
14678      of open brackets at that point.  */
14679   reloc_index = -1;
14680   str_depth = 0;
14681   do
14682     {
14683       reloc_index++;
14684       crux = str;
14685       crux_depth = str_depth;
14686 
14687       /* Skip over whitespace and brackets, keeping count of the number
14688 	 of brackets.  */
14689       while (*str == ' ' || *str == '\t' || *str == '(')
14690 	if (*str++ == '(')
14691 	  str_depth++;
14692     }
14693   while (*str == '%'
14694 	 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14695 	 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14696 
14697   my_getExpression (ep, crux);
14698   str = expr_end;
14699 
14700   /* Match every open bracket.  */
14701   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14702     if (*str++ == ')')
14703       crux_depth--;
14704 
14705   if (crux_depth > 0)
14706     as_bad (_("unclosed '('"));
14707 
14708   expr_end = str;
14709 
14710   for (i = 0; i < reloc_index; i++)
14711     reloc[i] = reversed_reloc[reloc_index - 1 - i];
14712 
14713   return reloc_index;
14714 }
14715 
14716 static void
my_getExpression(expressionS * ep,char * str)14717 my_getExpression (expressionS *ep, char *str)
14718 {
14719   char *save_in;
14720 
14721   save_in = input_line_pointer;
14722   input_line_pointer = str;
14723   expression (ep);
14724   expr_end = input_line_pointer;
14725   input_line_pointer = save_in;
14726 }
14727 
14728 const char *
md_atof(int type,char * litP,int * sizeP)14729 md_atof (int type, char *litP, int *sizeP)
14730 {
14731   return ieee_md_atof (type, litP, sizeP, target_big_endian);
14732 }
14733 
14734 void
md_number_to_chars(char * buf,valueT val,int n)14735 md_number_to_chars (char *buf, valueT val, int n)
14736 {
14737   if (target_big_endian)
14738     number_to_chars_bigendian (buf, val, n);
14739   else
14740     number_to_chars_littleendian (buf, val, n);
14741 }
14742 
support_64bit_objects(void)14743 static int support_64bit_objects(void)
14744 {
14745   const char **list, **l;
14746   int yes;
14747 
14748   list = bfd_target_list ();
14749   for (l = list; *l != NULL; l++)
14750     if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14751 	|| strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14752       break;
14753   yes = (*l != NULL);
14754   free (list);
14755   return yes;
14756 }
14757 
14758 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14759    NEW_VALUE.  Warn if another value was already specified.  Note:
14760    we have to defer parsing the -march and -mtune arguments in order
14761    to handle 'from-abi' correctly, since the ABI might be specified
14762    in a later argument.  */
14763 
14764 static void
mips_set_option_string(const char ** string_ptr,const char * new_value)14765 mips_set_option_string (const char **string_ptr, const char *new_value)
14766 {
14767   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14768     as_warn (_("a different %s was already specified, is now %s"),
14769 	     string_ptr == &mips_arch_string ? "-march" : "-mtune",
14770 	     new_value);
14771 
14772   *string_ptr = new_value;
14773 }
14774 
14775 int
md_parse_option(int c,const char * arg)14776 md_parse_option (int c, const char *arg)
14777 {
14778   unsigned int i;
14779 
14780   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14781     if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14782       {
14783 	file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
14784 					   c == mips_ases[i].option_on);
14785 	return 1;
14786       }
14787 
14788   switch (c)
14789     {
14790     case OPTION_CONSTRUCT_FLOATS:
14791       mips_disable_float_construction = 0;
14792       break;
14793 
14794     case OPTION_NO_CONSTRUCT_FLOATS:
14795       mips_disable_float_construction = 1;
14796       break;
14797 
14798     case OPTION_TRAP:
14799       mips_trap = 1;
14800       break;
14801 
14802     case OPTION_BREAK:
14803       mips_trap = 0;
14804       break;
14805 
14806     case OPTION_EB:
14807       target_big_endian = 1;
14808       break;
14809 
14810     case OPTION_EL:
14811       target_big_endian = 0;
14812       break;
14813 
14814     case 'O':
14815       if (arg == NULL)
14816 	mips_optimize = 1;
14817       else if (arg[0] == '0')
14818 	mips_optimize = 0;
14819       else if (arg[0] == '1')
14820 	mips_optimize = 1;
14821       else
14822 	mips_optimize = 2;
14823       break;
14824 
14825     case 'g':
14826       if (arg == NULL)
14827 	mips_debug = 2;
14828       else
14829 	mips_debug = atoi (arg);
14830       break;
14831 
14832     case OPTION_MIPS1:
14833       file_mips_opts.isa = ISA_MIPS1;
14834       break;
14835 
14836     case OPTION_MIPS2:
14837       file_mips_opts.isa = ISA_MIPS2;
14838       break;
14839 
14840     case OPTION_MIPS3:
14841       file_mips_opts.isa = ISA_MIPS3;
14842       break;
14843 
14844     case OPTION_MIPS4:
14845       file_mips_opts.isa = ISA_MIPS4;
14846       break;
14847 
14848     case OPTION_MIPS5:
14849       file_mips_opts.isa = ISA_MIPS5;
14850       break;
14851 
14852     case OPTION_MIPS32:
14853       file_mips_opts.isa = ISA_MIPS32;
14854       break;
14855 
14856     case OPTION_MIPS32R2:
14857       file_mips_opts.isa = ISA_MIPS32R2;
14858       break;
14859 
14860     case OPTION_MIPS32R3:
14861       file_mips_opts.isa = ISA_MIPS32R3;
14862       break;
14863 
14864     case OPTION_MIPS32R5:
14865       file_mips_opts.isa = ISA_MIPS32R5;
14866       break;
14867 
14868     case OPTION_MIPS32R6:
14869       file_mips_opts.isa = ISA_MIPS32R6;
14870       break;
14871 
14872     case OPTION_MIPS64R2:
14873       file_mips_opts.isa = ISA_MIPS64R2;
14874       break;
14875 
14876     case OPTION_MIPS64R3:
14877       file_mips_opts.isa = ISA_MIPS64R3;
14878       break;
14879 
14880     case OPTION_MIPS64R5:
14881       file_mips_opts.isa = ISA_MIPS64R5;
14882       break;
14883 
14884     case OPTION_MIPS64R6:
14885       file_mips_opts.isa = ISA_MIPS64R6;
14886       break;
14887 
14888     case OPTION_MIPS64:
14889       file_mips_opts.isa = ISA_MIPS64;
14890       break;
14891 
14892     case OPTION_MTUNE:
14893       mips_set_option_string (&mips_tune_string, arg);
14894       break;
14895 
14896     case OPTION_MARCH:
14897       mips_set_option_string (&mips_arch_string, arg);
14898       break;
14899 
14900     case OPTION_M4650:
14901       mips_set_option_string (&mips_arch_string, "4650");
14902       mips_set_option_string (&mips_tune_string, "4650");
14903       break;
14904 
14905     case OPTION_NO_M4650:
14906       break;
14907 
14908     case OPTION_M4010:
14909       mips_set_option_string (&mips_arch_string, "4010");
14910       mips_set_option_string (&mips_tune_string, "4010");
14911       break;
14912 
14913     case OPTION_NO_M4010:
14914       break;
14915 
14916     case OPTION_M4100:
14917       mips_set_option_string (&mips_arch_string, "4100");
14918       mips_set_option_string (&mips_tune_string, "4100");
14919       break;
14920 
14921     case OPTION_NO_M4100:
14922       break;
14923 
14924     case OPTION_M3900:
14925       mips_set_option_string (&mips_arch_string, "3900");
14926       mips_set_option_string (&mips_tune_string, "3900");
14927       break;
14928 
14929     case OPTION_NO_M3900:
14930       break;
14931 
14932     case OPTION_MICROMIPS:
14933       if (file_mips_opts.mips16 == 1)
14934 	{
14935 	  as_bad (_("-mmicromips cannot be used with -mips16"));
14936 	  return 0;
14937 	}
14938       file_mips_opts.micromips = 1;
14939       mips_no_prev_insn ();
14940       break;
14941 
14942     case OPTION_NO_MICROMIPS:
14943       file_mips_opts.micromips = 0;
14944       mips_no_prev_insn ();
14945       break;
14946 
14947     case OPTION_MIPS16:
14948       if (file_mips_opts.micromips == 1)
14949 	{
14950 	  as_bad (_("-mips16 cannot be used with -micromips"));
14951 	  return 0;
14952 	}
14953       file_mips_opts.mips16 = 1;
14954       mips_no_prev_insn ();
14955       break;
14956 
14957     case OPTION_NO_MIPS16:
14958       file_mips_opts.mips16 = 0;
14959       mips_no_prev_insn ();
14960       break;
14961 
14962     case OPTION_FIX_24K:
14963       mips_fix_24k = 1;
14964       break;
14965 
14966     case OPTION_NO_FIX_24K:
14967       mips_fix_24k = 0;
14968       break;
14969 
14970     case OPTION_FIX_RM7000:
14971       mips_fix_rm7000 = 1;
14972       break;
14973 
14974     case OPTION_NO_FIX_RM7000:
14975       mips_fix_rm7000 = 0;
14976       break;
14977 
14978     case OPTION_FIX_LOONGSON3_LLSC:
14979       mips_fix_loongson3_llsc = true;
14980       break;
14981 
14982     case OPTION_NO_FIX_LOONGSON3_LLSC:
14983       mips_fix_loongson3_llsc = false;
14984       break;
14985 
14986     case OPTION_FIX_LOONGSON2F_JUMP:
14987       mips_fix_loongson2f_jump = true;
14988       break;
14989 
14990     case OPTION_NO_FIX_LOONGSON2F_JUMP:
14991       mips_fix_loongson2f_jump = false;
14992       break;
14993 
14994     case OPTION_FIX_LOONGSON2F_NOP:
14995       mips_fix_loongson2f_nop = true;
14996       break;
14997 
14998     case OPTION_NO_FIX_LOONGSON2F_NOP:
14999       mips_fix_loongson2f_nop = false;
15000       break;
15001 
15002     case OPTION_FIX_VR4120:
15003       mips_fix_vr4120 = 1;
15004       break;
15005 
15006     case OPTION_NO_FIX_VR4120:
15007       mips_fix_vr4120 = 0;
15008       break;
15009 
15010     case OPTION_FIX_VR4130:
15011       mips_fix_vr4130 = 1;
15012       break;
15013 
15014     case OPTION_NO_FIX_VR4130:
15015       mips_fix_vr4130 = 0;
15016       break;
15017 
15018     case OPTION_FIX_CN63XXP1:
15019       mips_fix_cn63xxp1 = true;
15020       break;
15021 
15022     case OPTION_NO_FIX_CN63XXP1:
15023       mips_fix_cn63xxp1 = false;
15024       break;
15025 
15026     case OPTION_FIX_R5900:
15027       mips_fix_r5900 = true;
15028       mips_fix_r5900_explicit = true;
15029       break;
15030 
15031     case OPTION_NO_FIX_R5900:
15032       mips_fix_r5900 = false;
15033       mips_fix_r5900_explicit = true;
15034       break;
15035 
15036     case OPTION_RELAX_BRANCH:
15037       mips_relax_branch = 1;
15038       break;
15039 
15040     case OPTION_NO_RELAX_BRANCH:
15041       mips_relax_branch = 0;
15042       break;
15043 
15044     case OPTION_IGNORE_BRANCH_ISA:
15045       mips_ignore_branch_isa = true;
15046       break;
15047 
15048     case OPTION_NO_IGNORE_BRANCH_ISA:
15049       mips_ignore_branch_isa = false;
15050       break;
15051 
15052     case OPTION_INSN32:
15053       file_mips_opts.insn32 = true;
15054       break;
15055 
15056     case OPTION_NO_INSN32:
15057       file_mips_opts.insn32 = false;
15058       break;
15059 
15060     case OPTION_MSHARED:
15061       mips_in_shared = true;
15062       break;
15063 
15064     case OPTION_MNO_SHARED:
15065       mips_in_shared = false;
15066       break;
15067 
15068     case OPTION_MSYM32:
15069       file_mips_opts.sym32 = true;
15070       break;
15071 
15072     case OPTION_MNO_SYM32:
15073       file_mips_opts.sym32 = false;
15074       break;
15075 
15076       /* When generating ELF code, we permit -KPIC and -call_shared to
15077 	 select SVR4_PIC, and -non_shared to select no PIC.  This is
15078 	 intended to be compatible with Irix 5.  */
15079     case OPTION_CALL_SHARED:
15080       mips_pic = SVR4_PIC;
15081       mips_abicalls = true;
15082       break;
15083 
15084     case OPTION_CALL_NONPIC:
15085       mips_pic = NO_PIC;
15086       mips_abicalls = true;
15087       break;
15088 
15089     case OPTION_NON_SHARED:
15090       mips_pic = NO_PIC;
15091       mips_abicalls = false;
15092       break;
15093 
15094       /* The -xgot option tells the assembler to use 32 bit offsets
15095          when accessing the got in SVR4_PIC mode.  It is for Irix
15096          compatibility.  */
15097     case OPTION_XGOT:
15098       mips_big_got = 1;
15099       break;
15100 
15101     case 'G':
15102       g_switch_value = atoi (arg);
15103       g_switch_seen = 1;
15104       break;
15105 
15106       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15107 	 and -mabi=64.  */
15108     case OPTION_32:
15109       mips_abi = O32_ABI;
15110       break;
15111 
15112     case OPTION_N32:
15113       mips_abi = N32_ABI;
15114       break;
15115 
15116     case OPTION_64:
15117       mips_abi = N64_ABI;
15118       if (!support_64bit_objects())
15119 	as_fatal (_("no compiled in support for 64 bit object file format"));
15120       break;
15121 
15122     case OPTION_GP32:
15123       file_mips_opts.gp = 32;
15124       break;
15125 
15126     case OPTION_GP64:
15127       file_mips_opts.gp = 64;
15128       break;
15129 
15130     case OPTION_FP32:
15131       file_mips_opts.fp = 32;
15132       break;
15133 
15134     case OPTION_FPXX:
15135       file_mips_opts.fp = 0;
15136       break;
15137 
15138     case OPTION_FP64:
15139       file_mips_opts.fp = 64;
15140       break;
15141 
15142     case OPTION_ODD_SPREG:
15143       file_mips_opts.oddspreg = 1;
15144       break;
15145 
15146     case OPTION_NO_ODD_SPREG:
15147       file_mips_opts.oddspreg = 0;
15148       break;
15149 
15150     case OPTION_SINGLE_FLOAT:
15151       file_mips_opts.single_float = 1;
15152       break;
15153 
15154     case OPTION_DOUBLE_FLOAT:
15155       file_mips_opts.single_float = 0;
15156       break;
15157 
15158     case OPTION_SOFT_FLOAT:
15159       file_mips_opts.soft_float = 1;
15160       break;
15161 
15162     case OPTION_HARD_FLOAT:
15163       file_mips_opts.soft_float = 0;
15164       break;
15165 
15166     case OPTION_MABI:
15167       if (strcmp (arg, "32") == 0)
15168 	mips_abi = O32_ABI;
15169       else if (strcmp (arg, "o64") == 0)
15170 	mips_abi = O64_ABI;
15171       else if (strcmp (arg, "n32") == 0)
15172 	mips_abi = N32_ABI;
15173       else if (strcmp (arg, "64") == 0)
15174 	{
15175 	  mips_abi = N64_ABI;
15176 	  if (! support_64bit_objects())
15177 	    as_fatal (_("no compiled in support for 64 bit object file "
15178 			"format"));
15179 	}
15180       else if (strcmp (arg, "eabi") == 0)
15181 	mips_abi = EABI_ABI;
15182       else
15183 	{
15184 	  as_fatal (_("invalid abi -mabi=%s"), arg);
15185 	  return 0;
15186 	}
15187       break;
15188 
15189     case OPTION_M7000_HILO_FIX:
15190       mips_7000_hilo_fix = true;
15191       break;
15192 
15193     case OPTION_MNO_7000_HILO_FIX:
15194       mips_7000_hilo_fix = false;
15195       break;
15196 
15197     case OPTION_MDEBUG:
15198       mips_flag_mdebug = true;
15199       break;
15200 
15201     case OPTION_NO_MDEBUG:
15202       mips_flag_mdebug = false;
15203       break;
15204 
15205     case OPTION_PDR:
15206       mips_flag_pdr = true;
15207       break;
15208 
15209     case OPTION_NO_PDR:
15210       mips_flag_pdr = false;
15211       break;
15212 
15213     case OPTION_MVXWORKS_PIC:
15214       mips_pic = VXWORKS_PIC;
15215       break;
15216 
15217     case OPTION_NAN:
15218       if (strcmp (arg, "2008") == 0)
15219 	mips_nan2008 = 1;
15220       else if (strcmp (arg, "legacy") == 0)
15221 	mips_nan2008 = 0;
15222       else
15223 	{
15224 	  as_fatal (_("invalid NaN setting -mnan=%s"), arg);
15225 	  return 0;
15226 	}
15227       break;
15228 
15229     default:
15230       return 0;
15231     }
15232 
15233     mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15234 
15235   return 1;
15236 }
15237 
15238 /* Set up globals to tune for the ISA or processor described by INFO.  */
15239 
15240 static void
mips_set_tune(const struct mips_cpu_info * info)15241 mips_set_tune (const struct mips_cpu_info *info)
15242 {
15243   if (info != 0)
15244     mips_tune = info->cpu;
15245 }
15246 
15247 
15248 void
mips_after_parse_args(void)15249 mips_after_parse_args (void)
15250 {
15251   const struct mips_cpu_info *arch_info = 0;
15252   const struct mips_cpu_info *tune_info = 0;
15253 
15254   /* GP relative stuff not working for PE.  */
15255   if (startswith (TARGET_OS, "pe"))
15256     {
15257       if (g_switch_seen && g_switch_value != 0)
15258 	as_bad (_("-G not supported in this configuration"));
15259       g_switch_value = 0;
15260     }
15261 
15262   if (mips_abi == NO_ABI)
15263     mips_abi = MIPS_DEFAULT_ABI;
15264 
15265   /* The following code determines the architecture.
15266      Similar code was added to GCC 3.3 (see override_options() in
15267      config/mips/mips.c).  The GAS and GCC code should be kept in sync
15268      as much as possible.  */
15269 
15270   if (mips_arch_string != 0)
15271     arch_info = mips_parse_cpu ("-march", mips_arch_string);
15272 
15273   if (file_mips_opts.isa != ISA_UNKNOWN)
15274     {
15275       /* Handle -mipsN.  At this point, file_mips_opts.isa contains the
15276 	 ISA level specified by -mipsN, while arch_info->isa contains
15277 	 the -march selection (if any).  */
15278       if (arch_info != 0)
15279 	{
15280 	  /* -march takes precedence over -mipsN, since it is more descriptive.
15281 	     There's no harm in specifying both as long as the ISA levels
15282 	     are the same.  */
15283 	  if (file_mips_opts.isa != arch_info->isa)
15284 	    as_bad (_("-%s conflicts with the other architecture options,"
15285 		      " which imply -%s"),
15286 		    mips_cpu_info_from_isa (file_mips_opts.isa)->name,
15287 		    mips_cpu_info_from_isa (arch_info->isa)->name);
15288 	}
15289       else
15290 	arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
15291     }
15292 
15293   if (arch_info == 0)
15294     {
15295       arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15296       gas_assert (arch_info);
15297     }
15298 
15299   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15300     as_bad (_("-march=%s is not compatible with the selected ABI"),
15301 	    arch_info->name);
15302 
15303   file_mips_opts.arch = arch_info->cpu;
15304   file_mips_opts.isa = arch_info->isa;
15305   file_mips_opts.init_ase = arch_info->ase;
15306 
15307   /* The EVA Extension has instructions which are only valid when the R6 ISA
15308      is enabled.  This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
15309      present.  */
15310   if (((file_mips_opts.ase & ASE_EVA) != 0) && ISA_IS_R6 (file_mips_opts.isa))
15311     file_mips_opts.ase |= ASE_EVA_R6;
15312 
15313   /* Set up initial mips_opts state.  */
15314   mips_opts = file_mips_opts;
15315 
15316   /* For the R5900 default to `-mfix-r5900' unless the user told otherwise.  */
15317   if (!mips_fix_r5900_explicit)
15318     mips_fix_r5900 = file_mips_opts.arch == CPU_R5900;
15319 
15320   /* The register size inference code is now placed in
15321      file_mips_check_options.  */
15322 
15323   /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15324      processor.  */
15325   if (mips_tune_string != 0)
15326     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15327 
15328   if (tune_info == 0)
15329     mips_set_tune (arch_info);
15330   else
15331     mips_set_tune (tune_info);
15332 
15333   if (mips_flag_mdebug < 0)
15334     mips_flag_mdebug = 0;
15335 }
15336 
15337 void
mips_init_after_args(void)15338 mips_init_after_args (void)
15339 {
15340   /* Initialize opcodes.  */
15341   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15342   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15343 }
15344 
15345 long
md_pcrel_from(fixS * fixP)15346 md_pcrel_from (fixS *fixP)
15347 {
15348   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15349 
15350   switch (fixP->fx_r_type)
15351     {
15352     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15353     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15354       /* Return the address of the delay slot.  */
15355       return addr + 2;
15356 
15357     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15358     case BFD_RELOC_MICROMIPS_JMP:
15359     case BFD_RELOC_MIPS16_16_PCREL_S1:
15360     case BFD_RELOC_16_PCREL_S2:
15361     case BFD_RELOC_MIPS_21_PCREL_S2:
15362     case BFD_RELOC_MIPS_26_PCREL_S2:
15363     case BFD_RELOC_MIPS_JMP:
15364       /* Return the address of the delay slot.  */
15365       return addr + 4;
15366 
15367     case BFD_RELOC_MIPS_18_PCREL_S3:
15368       /* Return the aligned address of the doubleword containing
15369          the instruction.  */
15370       return addr & ~7;
15371 
15372     default:
15373       return addr;
15374     }
15375 }
15376 
15377 /* This is called before the symbol table is processed.  In order to
15378    work with gcc when using mips-tfile, we must keep all local labels.
15379    However, in other cases, we want to discard them.  If we were
15380    called with -g, but we didn't see any debugging information, it may
15381    mean that gcc is smuggling debugging information through to
15382    mips-tfile, in which case we must generate all local labels.  */
15383 
15384 void
mips_frob_file_before_adjust(void)15385 mips_frob_file_before_adjust (void)
15386 {
15387 #ifndef NO_ECOFF_DEBUGGING
15388   if (ECOFF_DEBUGGING
15389       && mips_debug != 0
15390       && ! ecoff_debugging_seen)
15391     flag_keep_locals = 1;
15392 #endif
15393 }
15394 
15395 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15396    the corresponding LO16 reloc.  This is called before md_apply_fix and
15397    tc_gen_reloc.  Unmatched relocs can only be generated by use of explicit
15398    relocation operators.
15399 
15400    For our purposes, a %lo() expression matches a %got() or %hi()
15401    expression if:
15402 
15403       (a) it refers to the same symbol; and
15404       (b) the offset applied in the %lo() expression is no lower than
15405 	  the offset applied in the %got() or %hi().
15406 
15407    (b) allows us to cope with code like:
15408 
15409 	lui	$4,%hi(foo)
15410 	lh	$4,%lo(foo+2)($4)
15411 
15412    ...which is legal on RELA targets, and has a well-defined behaviour
15413    if the user knows that adding 2 to "foo" will not induce a carry to
15414    the high 16 bits.
15415 
15416    When several %lo()s match a particular %got() or %hi(), we use the
15417    following rules to distinguish them:
15418 
15419      (1) %lo()s with smaller offsets are a better match than %lo()s with
15420          higher offsets.
15421 
15422      (2) %lo()s with no matching %got() or %hi() are better than those
15423          that already have a matching %got() or %hi().
15424 
15425      (3) later %lo()s are better than earlier %lo()s.
15426 
15427    These rules are applied in order.
15428 
15429    (1) means, among other things, that %lo()s with identical offsets are
15430    chosen if they exist.
15431 
15432    (2) means that we won't associate several high-part relocations with
15433    the same low-part relocation unless there's no alternative.  Having
15434    several high parts for the same low part is a GNU extension; this rule
15435    allows careful users to avoid it.
15436 
15437    (3) is purely cosmetic.  mips_hi_fixup_list is is in reverse order,
15438    with the last high-part relocation being at the front of the list.
15439    It therefore makes sense to choose the last matching low-part
15440    relocation, all other things being equal.  It's also easier
15441    to code that way.  */
15442 
15443 void
mips_frob_file(void)15444 mips_frob_file (void)
15445 {
15446   struct mips_hi_fixup *l;
15447   bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15448 
15449   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15450     {
15451       segment_info_type *seginfo;
15452       bool matched_lo_p;
15453       fixS **hi_pos, **lo_pos, **pos;
15454 
15455       gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15456 
15457       /* If a GOT16 relocation turns out to be against a global symbol,
15458 	 there isn't supposed to be a matching LO.  Ignore %gots against
15459 	 constants; we'll report an error for those later.  */
15460       if (got16_reloc_p (l->fixp->fx_r_type)
15461 	  && !(l->fixp->fx_addsy
15462 	       && pic_need_relax (l->fixp->fx_addsy)))
15463 	continue;
15464 
15465       /* Check quickly whether the next fixup happens to be a matching %lo.  */
15466       if (fixup_has_matching_lo_p (l->fixp))
15467 	continue;
15468 
15469       seginfo = seg_info (l->seg);
15470 
15471       /* Set HI_POS to the position of this relocation in the chain.
15472 	 Set LO_POS to the position of the chosen low-part relocation.
15473 	 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15474 	 relocation that matches an immediately-preceding high-part
15475 	 relocation.  */
15476       hi_pos = NULL;
15477       lo_pos = NULL;
15478       matched_lo_p = false;
15479       looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15480 
15481       for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15482 	{
15483 	  if (*pos == l->fixp)
15484 	    hi_pos = pos;
15485 
15486 	  if ((*pos)->fx_r_type == looking_for_rtype
15487 	      && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15488 	      && (*pos)->fx_offset >= l->fixp->fx_offset
15489 	      && (lo_pos == NULL
15490 		  || (*pos)->fx_offset < (*lo_pos)->fx_offset
15491 		  || (!matched_lo_p
15492 		      && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15493 	    lo_pos = pos;
15494 
15495 	  matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15496 			  && fixup_has_matching_lo_p (*pos));
15497 	}
15498 
15499       /* If we found a match, remove the high-part relocation from its
15500 	 current position and insert it before the low-part relocation.
15501 	 Make the offsets match so that fixup_has_matching_lo_p()
15502 	 will return true.
15503 
15504 	 We don't warn about unmatched high-part relocations since some
15505 	 versions of gcc have been known to emit dead "lui ...%hi(...)"
15506 	 instructions.  */
15507       if (lo_pos != NULL)
15508 	{
15509 	  l->fixp->fx_offset = (*lo_pos)->fx_offset;
15510 	  if (l->fixp->fx_next != *lo_pos)
15511 	    {
15512 	      *hi_pos = l->fixp->fx_next;
15513 	      l->fixp->fx_next = *lo_pos;
15514 	      *lo_pos = l->fixp;
15515 	    }
15516 	}
15517     }
15518 }
15519 
15520 int
mips_force_relocation(fixS * fixp)15521 mips_force_relocation (fixS *fixp)
15522 {
15523   if (generic_force_reloc (fixp))
15524     return 1;
15525 
15526   /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15527      so that the linker relaxation can update targets.  */
15528   if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15529       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15530       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15531     return 1;
15532 
15533   /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15534      and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15535      microMIPS symbols so that we can do cross-mode branch diagnostics
15536      and BAL to JALX conversion by the linker.  */
15537   if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15538        || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15539        || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15540       && fixp->fx_addsy
15541       && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15542     return 1;
15543 
15544   /* We want all PC-relative relocations to be kept for R6 relaxation.  */
15545   if (ISA_IS_R6 (file_mips_opts.isa)
15546       && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15547 	  || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15548 	  || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15549 	  || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15550 	  || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15551 	  || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15552 	  || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15553     return 1;
15554 
15555   return 0;
15556 }
15557 
15558 /* Implement TC_FORCE_RELOCATION_ABS.  */
15559 
15560 bool
mips_force_relocation_abs(fixS * fixp)15561 mips_force_relocation_abs (fixS *fixp)
15562 {
15563   if (generic_force_reloc (fixp))
15564     return true;
15565 
15566   /* These relocations do not have enough bits in the in-place addend
15567      to hold an arbitrary absolute section's offset.  */
15568   if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15569     return true;
15570 
15571   return false;
15572 }
15573 
15574 /* Read the instruction associated with RELOC from BUF.  */
15575 
15576 static unsigned int
read_reloc_insn(char * buf,bfd_reloc_code_real_type reloc)15577 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15578 {
15579   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15580     return read_compressed_insn (buf, 4);
15581   else
15582     return read_insn (buf);
15583 }
15584 
15585 /* Write instruction INSN to BUF, given that it has been relocated
15586    by RELOC.  */
15587 
15588 static void
write_reloc_insn(char * buf,bfd_reloc_code_real_type reloc,unsigned long insn)15589 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15590 		  unsigned long insn)
15591 {
15592   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15593     write_compressed_insn (buf, insn, 4);
15594   else
15595     write_insn (buf, insn);
15596 }
15597 
15598 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15599    to a symbol in another ISA mode, which cannot be converted to JALX.  */
15600 
15601 static bool
fix_bad_cross_mode_jump_p(fixS * fixP)15602 fix_bad_cross_mode_jump_p (fixS *fixP)
15603 {
15604   unsigned long opcode;
15605   int other;
15606   char *buf;
15607 
15608   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15609     return false;
15610 
15611   other = S_GET_OTHER (fixP->fx_addsy);
15612   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15613   opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15614   switch (fixP->fx_r_type)
15615     {
15616     case BFD_RELOC_MIPS_JMP:
15617       return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15618     case BFD_RELOC_MICROMIPS_JMP:
15619       return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15620     default:
15621       return false;
15622     }
15623 }
15624 
15625 /* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15626    jump to a symbol in the same ISA mode.  */
15627 
15628 static bool
fix_bad_same_mode_jalx_p(fixS * fixP)15629 fix_bad_same_mode_jalx_p (fixS *fixP)
15630 {
15631   unsigned long opcode;
15632   int other;
15633   char *buf;
15634 
15635   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15636     return false;
15637 
15638   other = S_GET_OTHER (fixP->fx_addsy);
15639   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15640   opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15641   switch (fixP->fx_r_type)
15642     {
15643     case BFD_RELOC_MIPS_JMP:
15644       return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15645     case BFD_RELOC_MIPS16_JMP:
15646       return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15647     case BFD_RELOC_MICROMIPS_JMP:
15648       return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15649     default:
15650       return false;
15651     }
15652 }
15653 
15654 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15655    to a symbol whose value plus addend is not aligned according to the
15656    ultimate (after linker relaxation) jump instruction's immediate field
15657    requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15658    regular MIPS code, to (1 << 2).  */
15659 
15660 static bool
fix_bad_misaligned_jump_p(fixS * fixP,int shift)15661 fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15662 {
15663   bool micro_to_mips_p;
15664   valueT val;
15665   int other;
15666 
15667   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15668     return false;
15669 
15670   other = S_GET_OTHER (fixP->fx_addsy);
15671   val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15672   val += fixP->fx_offset;
15673   micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15674 		     && !ELF_ST_IS_MICROMIPS (other));
15675   return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15676 	  != ELF_ST_IS_COMPRESSED (other));
15677 }
15678 
15679 /* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15680    to a symbol whose annotation indicates another ISA mode.  For absolute
15681    symbols check the ISA bit instead.
15682 
15683    We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15684    symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15685    MIPS symbols and associated with BAL instructions as these instructions
15686    may be converted to JALX by the linker.  */
15687 
15688 static bool
fix_bad_cross_mode_branch_p(fixS * fixP)15689 fix_bad_cross_mode_branch_p (fixS *fixP)
15690 {
15691   bool absolute_p;
15692   unsigned long opcode;
15693   asection *symsec;
15694   valueT val;
15695   int other;
15696   char *buf;
15697 
15698   if (mips_ignore_branch_isa)
15699     return false;
15700 
15701   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15702     return false;
15703 
15704   symsec = S_GET_SEGMENT (fixP->fx_addsy);
15705   absolute_p = bfd_is_abs_section (symsec);
15706 
15707   val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15708   other = S_GET_OTHER (fixP->fx_addsy);
15709 
15710   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15711   opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15712   switch (fixP->fx_r_type)
15713     {
15714     case BFD_RELOC_16_PCREL_S2:
15715       return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15716 	      && opcode != 0x0411);
15717     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15718       return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15719 	      && opcode != 0x4060);
15720     case BFD_RELOC_MIPS_21_PCREL_S2:
15721     case BFD_RELOC_MIPS_26_PCREL_S2:
15722       return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15723     case BFD_RELOC_MIPS16_16_PCREL_S1:
15724       return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15725     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15726     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15727       return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15728     default:
15729       abort ();
15730     }
15731 }
15732 
15733 /* Return TRUE if the symbol plus addend associated with a regular MIPS
15734    branch instruction pointed to by FIXP is not aligned according to the
15735    branch instruction's immediate field requirement.  We need the addend
15736    to preserve the ISA bit and also the sum must not have bit 2 set.  We
15737    must explicitly OR in the ISA bit from symbol annotation as the bit
15738    won't be set in the symbol's value then.  */
15739 
15740 static bool
fix_bad_misaligned_branch_p(fixS * fixP)15741 fix_bad_misaligned_branch_p (fixS *fixP)
15742 {
15743   bool absolute_p;
15744   asection *symsec;
15745   valueT isa_bit;
15746   valueT val;
15747   valueT off;
15748   int other;
15749 
15750   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15751     return false;
15752 
15753   symsec = S_GET_SEGMENT (fixP->fx_addsy);
15754   absolute_p = bfd_is_abs_section (symsec);
15755 
15756   val = S_GET_VALUE (fixP->fx_addsy);
15757   other = S_GET_OTHER (fixP->fx_addsy);
15758   off = fixP->fx_offset;
15759 
15760   isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15761   val |= ELF_ST_IS_COMPRESSED (other);
15762   val += off;
15763   return (val & 0x3) != isa_bit;
15764 }
15765 
15766 /* Calculate the relocation target by masking off ISA mode bit before
15767    combining symbol and addend.  */
15768 
15769 static valueT
fix_bad_misaligned_address(fixS * fixP)15770 fix_bad_misaligned_address (fixS *fixP)
15771 {
15772   valueT val;
15773   valueT off;
15774   unsigned isa_mode;
15775   gas_assert (fixP != NULL && fixP->fx_addsy != NULL);
15776   val = S_GET_VALUE (fixP->fx_addsy);
15777   off = fixP->fx_offset;
15778   isa_mode = (ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixP->fx_addsy))
15779 	      ? 1 : 0);
15780 
15781   return ((val & ~isa_mode) + off);
15782 }
15783 
15784 /* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15785    and its calculated value VAL.  */
15786 
15787 static void
fix_validate_branch(fixS * fixP,valueT val)15788 fix_validate_branch (fixS *fixP, valueT val)
15789 {
15790   if (fixP->fx_done && (val & 0x3) != 0)
15791     as_bad_where (fixP->fx_file, fixP->fx_line,
15792 		  _("branch to misaligned address (0x%lx)"),
15793 		  (long) (val + md_pcrel_from (fixP)));
15794   else if (fix_bad_cross_mode_branch_p (fixP))
15795     as_bad_where (fixP->fx_file, fixP->fx_line,
15796 		  _("branch to a symbol in another ISA mode"));
15797   else if (fix_bad_misaligned_branch_p (fixP))
15798     as_bad_where (fixP->fx_file, fixP->fx_line,
15799 		  _("branch to misaligned address (0x%lx)"),
15800 		  (long) fix_bad_misaligned_address (fixP));
15801   else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15802     as_bad_where (fixP->fx_file, fixP->fx_line,
15803 		  _("cannot encode misaligned addend "
15804 		    "in the relocatable field (0x%lx)"),
15805 		  (long) fixP->fx_offset);
15806 }
15807 
15808 /* Apply a fixup to the object file.  */
15809 
15810 void
md_apply_fix(fixS * fixP,valueT * valP,segT seg ATTRIBUTE_UNUSED)15811 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15812 {
15813   char *buf;
15814   unsigned long insn;
15815   reloc_howto_type *howto;
15816 
15817   if (fixP->fx_pcrel)
15818     switch (fixP->fx_r_type)
15819       {
15820       case BFD_RELOC_16_PCREL_S2:
15821       case BFD_RELOC_MIPS16_16_PCREL_S1:
15822       case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15823       case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15824       case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15825       case BFD_RELOC_32_PCREL:
15826       case BFD_RELOC_MIPS_21_PCREL_S2:
15827       case BFD_RELOC_MIPS_26_PCREL_S2:
15828       case BFD_RELOC_MIPS_18_PCREL_S3:
15829       case BFD_RELOC_MIPS_19_PCREL_S2:
15830       case BFD_RELOC_HI16_S_PCREL:
15831       case BFD_RELOC_LO16_PCREL:
15832 	break;
15833 
15834       case BFD_RELOC_32:
15835 	fixP->fx_r_type = BFD_RELOC_32_PCREL;
15836 	break;
15837 
15838       default:
15839 	as_bad_where (fixP->fx_file, fixP->fx_line,
15840 		      _("PC-relative reference to a different section"));
15841 	break;
15842       }
15843 
15844   /* Handle BFD_RELOC_8, since it's easy.  Punt on other bfd relocations
15845      that have no MIPS ELF equivalent.  */
15846   if (fixP->fx_r_type != BFD_RELOC_8)
15847     {
15848       howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15849       if (!howto)
15850 	return;
15851     }
15852 
15853   gas_assert (fixP->fx_size == 2
15854 	      || fixP->fx_size == 4
15855 	      || fixP->fx_r_type == BFD_RELOC_8
15856 	      || fixP->fx_r_type == BFD_RELOC_16
15857 	      || fixP->fx_r_type == BFD_RELOC_64
15858 	      || fixP->fx_r_type == BFD_RELOC_CTOR
15859 	      || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15860 	      || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15861 	      || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15862 	      || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15863 	      || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15864 	      || fixP->fx_r_type == BFD_RELOC_NONE);
15865 
15866   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15867 
15868   /* Don't treat parts of a composite relocation as done.  There are two
15869      reasons for this:
15870 
15871      (1) The second and third parts will be against 0 (RSS_UNDEF) but
15872 	 should nevertheless be emitted if the first part is.
15873 
15874      (2) In normal usage, composite relocations are never assembly-time
15875 	 constants.  The easiest way of dealing with the pathological
15876 	 exceptions is to generate a relocation against STN_UNDEF and
15877 	 leave everything up to the linker.  */
15878   if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15879     fixP->fx_done = 1;
15880 
15881   switch (fixP->fx_r_type)
15882     {
15883     case BFD_RELOC_MIPS_TLS_GD:
15884     case BFD_RELOC_MIPS_TLS_LDM:
15885     case BFD_RELOC_MIPS_TLS_DTPREL32:
15886     case BFD_RELOC_MIPS_TLS_DTPREL64:
15887     case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15888     case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15889     case BFD_RELOC_MIPS_TLS_GOTTPREL:
15890     case BFD_RELOC_MIPS_TLS_TPREL32:
15891     case BFD_RELOC_MIPS_TLS_TPREL64:
15892     case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15893     case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15894     case BFD_RELOC_MICROMIPS_TLS_GD:
15895     case BFD_RELOC_MICROMIPS_TLS_LDM:
15896     case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15897     case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15898     case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15899     case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15900     case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15901     case BFD_RELOC_MIPS16_TLS_GD:
15902     case BFD_RELOC_MIPS16_TLS_LDM:
15903     case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15904     case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15905     case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15906     case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15907     case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15908       if (fixP->fx_addsy)
15909 	S_SET_THREAD_LOCAL (fixP->fx_addsy);
15910       else
15911 	as_bad_where (fixP->fx_file, fixP->fx_line,
15912 		      _("TLS relocation against a constant"));
15913       break;
15914 
15915     case BFD_RELOC_MIPS_JMP:
15916     case BFD_RELOC_MIPS16_JMP:
15917     case BFD_RELOC_MICROMIPS_JMP:
15918       {
15919 	int shift;
15920 
15921 	gas_assert (!fixP->fx_done);
15922 
15923 	/* Shift is 2, unusually, for microMIPS JALX.  */
15924 	if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15925 	    && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15926 	  shift = 1;
15927 	else
15928 	  shift = 2;
15929 
15930 	if (fix_bad_cross_mode_jump_p (fixP))
15931 	  as_bad_where (fixP->fx_file, fixP->fx_line,
15932 			_("jump to a symbol in another ISA mode"));
15933 	else if (fix_bad_same_mode_jalx_p (fixP))
15934 	  as_bad_where (fixP->fx_file, fixP->fx_line,
15935 			_("JALX to a symbol in the same ISA mode"));
15936 	else if (fix_bad_misaligned_jump_p (fixP, shift))
15937 	  as_bad_where (fixP->fx_file, fixP->fx_line,
15938 			_("jump to misaligned address (0x%lx)"),
15939 			(long) fix_bad_misaligned_address (fixP));
15940 	else if (HAVE_IN_PLACE_ADDENDS
15941 		 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15942 	  as_bad_where (fixP->fx_file, fixP->fx_line,
15943 			_("cannot encode misaligned addend "
15944 			  "in the relocatable field (0x%lx)"),
15945 			(long) fixP->fx_offset);
15946       }
15947       /* Fall through.  */
15948 
15949     case BFD_RELOC_MIPS_SHIFT5:
15950     case BFD_RELOC_MIPS_SHIFT6:
15951     case BFD_RELOC_MIPS_GOT_DISP:
15952     case BFD_RELOC_MIPS_GOT_PAGE:
15953     case BFD_RELOC_MIPS_GOT_OFST:
15954     case BFD_RELOC_MIPS_SUB:
15955     case BFD_RELOC_MIPS_INSERT_A:
15956     case BFD_RELOC_MIPS_INSERT_B:
15957     case BFD_RELOC_MIPS_DELETE:
15958     case BFD_RELOC_MIPS_HIGHEST:
15959     case BFD_RELOC_MIPS_HIGHER:
15960     case BFD_RELOC_MIPS_SCN_DISP:
15961     case BFD_RELOC_MIPS_REL16:
15962     case BFD_RELOC_MIPS_RELGOT:
15963     case BFD_RELOC_MIPS_JALR:
15964     case BFD_RELOC_HI16:
15965     case BFD_RELOC_HI16_S:
15966     case BFD_RELOC_LO16:
15967     case BFD_RELOC_GPREL16:
15968     case BFD_RELOC_MIPS_LITERAL:
15969     case BFD_RELOC_MIPS_CALL16:
15970     case BFD_RELOC_MIPS_GOT16:
15971     case BFD_RELOC_GPREL32:
15972     case BFD_RELOC_MIPS_GOT_HI16:
15973     case BFD_RELOC_MIPS_GOT_LO16:
15974     case BFD_RELOC_MIPS_CALL_HI16:
15975     case BFD_RELOC_MIPS_CALL_LO16:
15976     case BFD_RELOC_HI16_S_PCREL:
15977     case BFD_RELOC_LO16_PCREL:
15978     case BFD_RELOC_MIPS16_GPREL:
15979     case BFD_RELOC_MIPS16_GOT16:
15980     case BFD_RELOC_MIPS16_CALL16:
15981     case BFD_RELOC_MIPS16_HI16:
15982     case BFD_RELOC_MIPS16_HI16_S:
15983     case BFD_RELOC_MIPS16_LO16:
15984     case BFD_RELOC_MICROMIPS_GOT_DISP:
15985     case BFD_RELOC_MICROMIPS_GOT_PAGE:
15986     case BFD_RELOC_MICROMIPS_GOT_OFST:
15987     case BFD_RELOC_MICROMIPS_SUB:
15988     case BFD_RELOC_MICROMIPS_HIGHEST:
15989     case BFD_RELOC_MICROMIPS_HIGHER:
15990     case BFD_RELOC_MICROMIPS_SCN_DISP:
15991     case BFD_RELOC_MICROMIPS_JALR:
15992     case BFD_RELOC_MICROMIPS_HI16:
15993     case BFD_RELOC_MICROMIPS_HI16_S:
15994     case BFD_RELOC_MICROMIPS_LO16:
15995     case BFD_RELOC_MICROMIPS_GPREL16:
15996     case BFD_RELOC_MICROMIPS_LITERAL:
15997     case BFD_RELOC_MICROMIPS_CALL16:
15998     case BFD_RELOC_MICROMIPS_GOT16:
15999     case BFD_RELOC_MICROMIPS_GOT_HI16:
16000     case BFD_RELOC_MICROMIPS_GOT_LO16:
16001     case BFD_RELOC_MICROMIPS_CALL_HI16:
16002     case BFD_RELOC_MICROMIPS_CALL_LO16:
16003     case BFD_RELOC_MIPS_EH:
16004       if (fixP->fx_done)
16005 	{
16006 	  offsetT value;
16007 
16008 	  if (calculate_reloc (fixP->fx_r_type, *valP, &value))
16009 	    {
16010 	      insn = read_reloc_insn (buf, fixP->fx_r_type);
16011 	      if (mips16_reloc_p (fixP->fx_r_type))
16012 		insn |= mips16_immed_extend (value, 16);
16013 	      else
16014 		insn |= (value & 0xffff);
16015 	      write_reloc_insn (buf, fixP->fx_r_type, insn);
16016 	    }
16017 	  else
16018 	    as_bad_where (fixP->fx_file, fixP->fx_line,
16019 			  _("unsupported constant in relocation"));
16020 	}
16021       break;
16022 
16023     case BFD_RELOC_64:
16024       /* This is handled like BFD_RELOC_32, but we output a sign
16025          extended value if we are only 32 bits.  */
16026       if (fixP->fx_done)
16027 	{
16028 	  if (8 <= sizeof (valueT))
16029 	    md_number_to_chars (buf, *valP, 8);
16030 	  else
16031 	    {
16032 	      valueT hiv;
16033 
16034 	      if ((*valP & 0x80000000) != 0)
16035 		hiv = 0xffffffff;
16036 	      else
16037 		hiv = 0;
16038 	      md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
16039 	      md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
16040 	    }
16041 	}
16042       break;
16043 
16044     case BFD_RELOC_RVA:
16045     case BFD_RELOC_32:
16046     case BFD_RELOC_32_PCREL:
16047     case BFD_RELOC_16:
16048     case BFD_RELOC_8:
16049       /* If we are deleting this reloc entry, we must fill in the
16050 	 value now.  This can happen if we have a .word which is not
16051 	 resolved when it appears but is later defined.  */
16052       if (fixP->fx_done)
16053 	md_number_to_chars (buf, *valP, fixP->fx_size);
16054       break;
16055 
16056     case BFD_RELOC_MIPS_21_PCREL_S2:
16057       fix_validate_branch (fixP, *valP);
16058       if (!fixP->fx_done)
16059 	break;
16060 
16061       if (*valP + 0x400000 <= 0x7fffff)
16062 	{
16063 	  insn = read_insn (buf);
16064 	  insn |= (*valP >> 2) & 0x1fffff;
16065 	  write_insn (buf, insn);
16066 	}
16067       else
16068 	as_bad_where (fixP->fx_file, fixP->fx_line,
16069 		      _("branch out of range"));
16070       break;
16071 
16072     case BFD_RELOC_MIPS_26_PCREL_S2:
16073       fix_validate_branch (fixP, *valP);
16074       if (!fixP->fx_done)
16075 	break;
16076 
16077       if (*valP + 0x8000000 <= 0xfffffff)
16078 	{
16079 	  insn = read_insn (buf);
16080 	  insn |= (*valP >> 2) & 0x3ffffff;
16081 	  write_insn (buf, insn);
16082 	}
16083       else
16084 	as_bad_where (fixP->fx_file, fixP->fx_line,
16085 		      _("branch out of range"));
16086       break;
16087 
16088     case BFD_RELOC_MIPS_18_PCREL_S3:
16089       if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
16090 	as_bad_where (fixP->fx_file, fixP->fx_line,
16091 		      _("PC-relative access using misaligned symbol (%lx)"),
16092 		      (long) S_GET_VALUE (fixP->fx_addsy));
16093       if ((fixP->fx_offset & 0x7) != 0)
16094 	as_bad_where (fixP->fx_file, fixP->fx_line,
16095 		      _("PC-relative access using misaligned offset (%lx)"),
16096 		      (long) fixP->fx_offset);
16097       if (!fixP->fx_done)
16098 	break;
16099 
16100       if (*valP + 0x100000 <= 0x1fffff)
16101 	{
16102 	  insn = read_insn (buf);
16103 	  insn |= (*valP >> 3) & 0x3ffff;
16104 	  write_insn (buf, insn);
16105 	}
16106       else
16107 	as_bad_where (fixP->fx_file, fixP->fx_line,
16108 		      _("PC-relative access out of range"));
16109       break;
16110 
16111     case BFD_RELOC_MIPS_19_PCREL_S2:
16112       if ((*valP & 0x3) != 0)
16113 	as_bad_where (fixP->fx_file, fixP->fx_line,
16114 		      _("PC-relative access to misaligned address (%lx)"),
16115 		      (long) *valP);
16116       if (!fixP->fx_done)
16117 	break;
16118 
16119       if (*valP + 0x100000 <= 0x1fffff)
16120 	{
16121 	  insn = read_insn (buf);
16122 	  insn |= (*valP >> 2) & 0x7ffff;
16123 	  write_insn (buf, insn);
16124 	}
16125       else
16126 	as_bad_where (fixP->fx_file, fixP->fx_line,
16127 		      _("PC-relative access out of range"));
16128       break;
16129 
16130     case BFD_RELOC_16_PCREL_S2:
16131       fix_validate_branch (fixP, *valP);
16132 
16133       /* We need to save the bits in the instruction since fixup_segment()
16134 	 might be deleting the relocation entry (i.e., a branch within
16135 	 the current segment).  */
16136       if (! fixP->fx_done)
16137 	break;
16138 
16139       /* Update old instruction data.  */
16140       insn = read_insn (buf);
16141 
16142       if (*valP + 0x20000 <= 0x3ffff)
16143 	{
16144 	  insn |= (*valP >> 2) & 0xffff;
16145 	  write_insn (buf, insn);
16146 	}
16147       else if (fixP->fx_tcbit2
16148 	       && fixP->fx_done
16149 	       && fixP->fx_frag->fr_address >= text_section->vma
16150 	       && (fixP->fx_frag->fr_address
16151 		   < text_section->vma + bfd_section_size (text_section))
16152 	       && ((insn & 0xffff0000) == 0x10000000	 /* beq $0,$0 */
16153 		   || (insn & 0xffff0000) == 0x04010000	 /* bgez $0 */
16154 		   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
16155 	{
16156 	  /* The branch offset is too large.  If this is an
16157              unconditional branch, and we are not generating PIC code,
16158              we can convert it to an absolute jump instruction.  */
16159 	  if ((insn & 0xffff0000) == 0x04110000)	 /* bgezal $0 */
16160 	    insn = 0x0c000000;	/* jal */
16161 	  else
16162 	    insn = 0x08000000;	/* j */
16163 	  fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
16164 	  fixP->fx_done = 0;
16165 	  fixP->fx_addsy = section_symbol (text_section);
16166 	  *valP += md_pcrel_from (fixP);
16167 	  write_insn (buf, insn);
16168 	}
16169       else
16170 	{
16171 	  /* If we got here, we have branch-relaxation disabled,
16172 	     and there's nothing we can do to fix this instruction
16173 	     without turning it into a longer sequence.  */
16174 	  as_bad_where (fixP->fx_file, fixP->fx_line,
16175 			_("branch out of range"));
16176 	}
16177       break;
16178 
16179     case BFD_RELOC_MIPS16_16_PCREL_S1:
16180     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16181     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16182     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
16183       gas_assert (!fixP->fx_done);
16184       if (fix_bad_cross_mode_branch_p (fixP))
16185 	as_bad_where (fixP->fx_file, fixP->fx_line,
16186 		      _("branch to a symbol in another ISA mode"));
16187       else if (fixP->fx_addsy
16188 	       && !S_FORCE_RELOC (fixP->fx_addsy, true)
16189 	       && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
16190 	       && (fixP->fx_offset & 0x1) != 0)
16191 	as_bad_where (fixP->fx_file, fixP->fx_line,
16192 		      _("branch to misaligned address (0x%lx)"),
16193 		      (long) fix_bad_misaligned_address (fixP));
16194       else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
16195 	as_bad_where (fixP->fx_file, fixP->fx_line,
16196 		      _("cannot encode misaligned addend "
16197 			"in the relocatable field (0x%lx)"),
16198 		      (long) fixP->fx_offset);
16199       break;
16200 
16201     case BFD_RELOC_VTABLE_INHERIT:
16202       fixP->fx_done = 0;
16203       if (fixP->fx_addsy
16204           && !S_IS_DEFINED (fixP->fx_addsy)
16205           && !S_IS_WEAK (fixP->fx_addsy))
16206         S_SET_WEAK (fixP->fx_addsy);
16207       break;
16208 
16209     case BFD_RELOC_NONE:
16210     case BFD_RELOC_VTABLE_ENTRY:
16211       fixP->fx_done = 0;
16212       break;
16213 
16214     default:
16215       abort ();
16216     }
16217 
16218   /* Remember value for tc_gen_reloc.  */
16219   fixP->fx_addnumber = *valP;
16220 }
16221 
16222 static symbolS *
get_symbol(void)16223 get_symbol (void)
16224 {
16225   int c;
16226   char *name;
16227   symbolS *p;
16228 
16229   c = get_symbol_name (&name);
16230   p = (symbolS *) symbol_find_or_make (name);
16231   (void) restore_line_pointer (c);
16232   return p;
16233 }
16234 
16235 /* Align the current frag to a given power of two.  If a particular
16236    fill byte should be used, FILL points to an integer that contains
16237    that byte, otherwise FILL is null.
16238 
16239    This function used to have the comment:
16240 
16241       The MIPS assembler also automatically adjusts any preceding label.
16242 
16243    The implementation therefore applied the adjustment to a maximum of
16244    one label.  However, other label adjustments are applied to batches
16245    of labels, and adjusting just one caused problems when new labels
16246    were added for the sake of debugging or unwind information.
16247    We therefore adjust all preceding labels (given as LABELS) instead.  */
16248 
16249 static void
mips_align(int to,int * fill,struct insn_label_list * labels)16250 mips_align (int to, int *fill, struct insn_label_list *labels)
16251 {
16252   mips_emit_delays ();
16253   mips_record_compressed_mode ();
16254   if (fill == NULL && subseg_text_p (now_seg))
16255     frag_align_code (to, 0);
16256   else
16257     frag_align (to, fill ? *fill : 0, 0);
16258   record_alignment (now_seg, to);
16259   mips_move_labels (labels, subseg_text_p (now_seg));
16260 }
16261 
16262 /* Align to a given power of two.  .align 0 turns off the automatic
16263    alignment used by the data creating pseudo-ops.  */
16264 
16265 static void
s_align(int x ATTRIBUTE_UNUSED)16266 s_align (int x ATTRIBUTE_UNUSED)
16267 {
16268   int temp, fill_value, *fill_ptr;
16269   long max_alignment = 28;
16270 
16271   /* o Note that the assembler pulls down any immediately preceding label
16272        to the aligned address.
16273      o It's not documented but auto alignment is reinstated by
16274        a .align pseudo instruction.
16275      o Note also that after auto alignment is turned off the mips assembler
16276        issues an error on attempt to assemble an improperly aligned data item.
16277        We don't.  */
16278 
16279   temp = get_absolute_expression ();
16280   if (temp > max_alignment)
16281     as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
16282   else if (temp < 0)
16283     {
16284       as_warn (_("alignment negative, 0 assumed"));
16285       temp = 0;
16286     }
16287   if (*input_line_pointer == ',')
16288     {
16289       ++input_line_pointer;
16290       fill_value = get_absolute_expression ();
16291       fill_ptr = &fill_value;
16292     }
16293   else
16294     fill_ptr = 0;
16295   if (temp)
16296     {
16297       segment_info_type *si = seg_info (now_seg);
16298       struct insn_label_list *l = si->label_list;
16299       /* Auto alignment should be switched on by next section change.  */
16300       auto_align = 1;
16301       mips_align (temp, fill_ptr, l);
16302     }
16303   else
16304     {
16305       auto_align = 0;
16306     }
16307 
16308   demand_empty_rest_of_line ();
16309 }
16310 
16311 static void
s_change_sec(int sec)16312 s_change_sec (int sec)
16313 {
16314   segT seg;
16315 
16316   /* The ELF backend needs to know that we are changing sections, so
16317      that .previous works correctly.  We could do something like check
16318      for an obj_section_change_hook macro, but that might be confusing
16319      as it would not be appropriate to use it in the section changing
16320      functions in read.c, since obj-elf.c intercepts those.  FIXME:
16321      This should be cleaner, somehow.  */
16322   obj_elf_section_change_hook ();
16323 
16324   mips_emit_delays ();
16325 
16326   switch (sec)
16327     {
16328     case 't':
16329       s_text (0);
16330       break;
16331     case 'd':
16332       s_data (0);
16333       break;
16334     case 'b':
16335       subseg_set (bss_section, (subsegT) get_absolute_expression ());
16336       demand_empty_rest_of_line ();
16337       break;
16338 
16339     case 'r':
16340       seg = subseg_new (RDATA_SECTION_NAME,
16341 			(subsegT) get_absolute_expression ());
16342       bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_READONLY
16343 				   | SEC_RELOC | SEC_DATA));
16344       if (!startswith (TARGET_OS, "elf"))
16345 	record_alignment (seg, 4);
16346       demand_empty_rest_of_line ();
16347       break;
16348 
16349     case 's':
16350       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
16351       bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_RELOC
16352 				   | SEC_DATA | SEC_SMALL_DATA));
16353       if (!startswith (TARGET_OS, "elf"))
16354 	record_alignment (seg, 4);
16355       demand_empty_rest_of_line ();
16356       break;
16357 
16358     case 'B':
16359       seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16360       bfd_set_section_flags (seg, SEC_ALLOC | SEC_SMALL_DATA);
16361       if (!startswith (TARGET_OS, "elf"))
16362 	record_alignment (seg, 4);
16363       demand_empty_rest_of_line ();
16364       break;
16365     }
16366 
16367   auto_align = 1;
16368 }
16369 
16370 void
s_change_section(int ignore ATTRIBUTE_UNUSED)16371 s_change_section (int ignore ATTRIBUTE_UNUSED)
16372 {
16373   char *saved_ilp;
16374   char *section_name;
16375   char c, endc;
16376   char next_c = 0;
16377   int section_type;
16378   int section_flag;
16379   int section_entry_size;
16380   int section_alignment;
16381 
16382   saved_ilp = input_line_pointer;
16383   endc = get_symbol_name (&section_name);
16384   c = (endc == '"' ? input_line_pointer[1] : endc);
16385   if (c)
16386     next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
16387 
16388   /* Do we have .section Name<,"flags">?  */
16389   if (c != ',' || (c == ',' && next_c == '"'))
16390     {
16391       /* Just after name is now '\0'.  */
16392       (void) restore_line_pointer (endc);
16393       input_line_pointer = saved_ilp;
16394       obj_elf_section (ignore);
16395       return;
16396     }
16397 
16398   section_name = xstrdup (section_name);
16399   c = restore_line_pointer (endc);
16400 
16401   input_line_pointer++;
16402 
16403   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
16404   if (c == ',')
16405     section_type = get_absolute_expression ();
16406   else
16407     section_type = 0;
16408 
16409   if (*input_line_pointer++ == ',')
16410     section_flag = get_absolute_expression ();
16411   else
16412     section_flag = 0;
16413 
16414   if (*input_line_pointer++ == ',')
16415     section_entry_size = get_absolute_expression ();
16416   else
16417     section_entry_size = 0;
16418 
16419   if (*input_line_pointer++ == ',')
16420     section_alignment = get_absolute_expression ();
16421   else
16422     section_alignment = 0;
16423 
16424   /* FIXME: really ignore?  */
16425   (void) section_alignment;
16426 
16427   /* When using the generic form of .section (as implemented by obj-elf.c),
16428      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
16429      traditionally had to fall back on the more common @progbits instead.
16430 
16431      There's nothing really harmful in this, since bfd will correct
16432      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
16433      means that, for backwards compatibility, the special_section entries
16434      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16435 
16436      Even so, we shouldn't force users of the MIPS .section syntax to
16437      incorrectly label the sections as SHT_PROGBITS.  The best compromise
16438      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16439      generic type-checking code.  */
16440   if (section_type == SHT_MIPS_DWARF)
16441     section_type = SHT_PROGBITS;
16442 
16443   obj_elf_change_section (section_name, section_type, section_flag,
16444 			  section_entry_size, 0, 0, 0);
16445 
16446   if (now_seg->name != section_name)
16447     free (section_name);
16448 }
16449 
16450 void
mips_enable_auto_align(void)16451 mips_enable_auto_align (void)
16452 {
16453   auto_align = 1;
16454 }
16455 
16456 static void
s_cons(int log_size)16457 s_cons (int log_size)
16458 {
16459   segment_info_type *si = seg_info (now_seg);
16460   struct insn_label_list *l = si->label_list;
16461 
16462   mips_emit_delays ();
16463   if (log_size > 0 && auto_align)
16464     mips_align (log_size, 0, l);
16465   cons (1 << log_size);
16466   mips_clear_insn_labels ();
16467 }
16468 
16469 static void
s_float_cons(int type)16470 s_float_cons (int type)
16471 {
16472   segment_info_type *si = seg_info (now_seg);
16473   struct insn_label_list *l = si->label_list;
16474 
16475   mips_emit_delays ();
16476 
16477   if (auto_align)
16478     {
16479       if (type == 'd')
16480 	mips_align (3, 0, l);
16481       else
16482 	mips_align (2, 0, l);
16483     }
16484 
16485   float_cons (type);
16486   mips_clear_insn_labels ();
16487 }
16488 
16489 /* Handle .globl.  We need to override it because on Irix 5 you are
16490    permitted to say
16491        .globl foo .text
16492    where foo is an undefined symbol, to mean that foo should be
16493    considered to be the address of a function.  */
16494 
16495 static void
s_mips_globl(int x ATTRIBUTE_UNUSED)16496 s_mips_globl (int x ATTRIBUTE_UNUSED)
16497 {
16498   char *name;
16499   int c;
16500   symbolS *symbolP;
16501 
16502   do
16503     {
16504       c = get_symbol_name (&name);
16505       symbolP = symbol_find_or_make (name);
16506       S_SET_EXTERNAL (symbolP);
16507 
16508       *input_line_pointer = c;
16509       SKIP_WHITESPACE_AFTER_NAME ();
16510 
16511       if (!is_end_of_line[(unsigned char) *input_line_pointer]
16512 	  && (*input_line_pointer != ','))
16513 	{
16514 	  char *secname;
16515 	  asection *sec;
16516 
16517 	  c = get_symbol_name (&secname);
16518 	  sec = bfd_get_section_by_name (stdoutput, secname);
16519 	  if (sec == NULL)
16520 	    as_bad (_("%s: no such section"), secname);
16521 	  (void) restore_line_pointer (c);
16522 
16523 	  if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16524 	    symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
16525 	}
16526 
16527       c = *input_line_pointer;
16528       if (c == ',')
16529 	{
16530 	  input_line_pointer++;
16531 	  SKIP_WHITESPACE ();
16532 	  if (is_end_of_line[(unsigned char) *input_line_pointer])
16533 	    c = '\n';
16534 	}
16535     }
16536   while (c == ',');
16537 
16538   demand_empty_rest_of_line ();
16539 }
16540 
16541 #ifdef TE_IRIX
16542 /* The Irix 5 and 6 assemblers set the type of any common symbol and
16543    any undefined non-function symbol to STT_OBJECT.  We try to be
16544    compatible, since newer Irix 5 and 6 linkers care.  */
16545 
16546 void
mips_frob_symbol(symbolS * symp ATTRIBUTE_UNUSED)16547 mips_frob_symbol (symbolS *symp ATTRIBUTE_UNUSED)
16548 {
16549   /* This late in assembly we can set BSF_OBJECT indiscriminately
16550      and let elf.c:swap_out_syms sort out the symbol type.  */
16551   flagword *flags = &symbol_get_bfdsym (symp)->flags;
16552   if ((*flags & (BSF_GLOBAL | BSF_WEAK)) != 0
16553       || !S_IS_DEFINED (symp))
16554     *flags |= BSF_OBJECT;
16555 }
16556 #endif
16557 
16558 static void
s_option(int x ATTRIBUTE_UNUSED)16559 s_option (int x ATTRIBUTE_UNUSED)
16560 {
16561   char *opt;
16562   char c;
16563 
16564   c = get_symbol_name (&opt);
16565 
16566   if (*opt == 'O')
16567     {
16568       /* FIXME: What does this mean?  */
16569     }
16570   else if (startswith (opt, "pic") && ISDIGIT (opt[3]) && opt[4] == '\0')
16571     {
16572       int i;
16573 
16574       i = atoi (opt + 3);
16575       if (i != 0 && i != 2)
16576 	as_bad (_(".option pic%d not supported"), i);
16577       else if (mips_pic == VXWORKS_PIC)
16578 	as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16579       else if (i == 0)
16580 	mips_pic = NO_PIC;
16581       else if (i == 2)
16582 	{
16583 	  mips_pic = SVR4_PIC;
16584 	  mips_abicalls = true;
16585 	}
16586 
16587       if (mips_pic == SVR4_PIC)
16588 	{
16589 	  if (g_switch_seen && g_switch_value != 0)
16590 	    as_warn (_("-G may not be used with SVR4 PIC code"));
16591 	  g_switch_value = 0;
16592 	  bfd_set_gp_size (stdoutput, 0);
16593 	}
16594     }
16595   else
16596     as_warn (_("unrecognized option \"%s\""), opt);
16597 
16598   (void) restore_line_pointer (c);
16599   demand_empty_rest_of_line ();
16600 }
16601 
16602 /* This structure is used to hold a stack of .set values.  */
16603 
16604 struct mips_option_stack
16605 {
16606   struct mips_option_stack *next;
16607   struct mips_set_options options;
16608 };
16609 
16610 static struct mips_option_stack *mips_opts_stack;
16611 
16612 /* Return status for .set/.module option handling.  */
16613 
16614 enum code_option_type
16615 {
16616   /* Unrecognized option.  */
16617   OPTION_TYPE_BAD = -1,
16618 
16619   /* Ordinary option.  */
16620   OPTION_TYPE_NORMAL,
16621 
16622   /* ISA changing option.  */
16623   OPTION_TYPE_ISA
16624 };
16625 
16626 /* Handle common .set/.module options.  Return status indicating option
16627    type.  */
16628 
16629 static enum code_option_type
parse_code_option(char * name)16630 parse_code_option (char * name)
16631 {
16632   bool isa_set = false;
16633   const struct mips_ase *ase;
16634 
16635   if (startswith (name, "at="))
16636     {
16637       char *s = name + 3;
16638 
16639       if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16640 	as_bad (_("unrecognized register name `%s'"), s);
16641     }
16642   else if (strcmp (name, "at") == 0)
16643     mips_opts.at = ATREG;
16644   else if (strcmp (name, "noat") == 0)
16645     mips_opts.at = ZERO;
16646   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16647     mips_opts.nomove = 0;
16648   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16649     mips_opts.nomove = 1;
16650   else if (strcmp (name, "bopt") == 0)
16651     mips_opts.nobopt = 0;
16652   else if (strcmp (name, "nobopt") == 0)
16653     mips_opts.nobopt = 1;
16654   else if (strcmp (name, "gp=32") == 0)
16655     mips_opts.gp = 32;
16656   else if (strcmp (name, "gp=64") == 0)
16657     mips_opts.gp = 64;
16658   else if (strcmp (name, "fp=32") == 0)
16659     mips_opts.fp = 32;
16660   else if (strcmp (name, "fp=xx") == 0)
16661     mips_opts.fp = 0;
16662   else if (strcmp (name, "fp=64") == 0)
16663     mips_opts.fp = 64;
16664   else if (strcmp (name, "softfloat") == 0)
16665     mips_opts.soft_float = 1;
16666   else if (strcmp (name, "hardfloat") == 0)
16667     mips_opts.soft_float = 0;
16668   else if (strcmp (name, "singlefloat") == 0)
16669     mips_opts.single_float = 1;
16670   else if (strcmp (name, "doublefloat") == 0)
16671     mips_opts.single_float = 0;
16672   else if (strcmp (name, "nooddspreg") == 0)
16673     mips_opts.oddspreg = 0;
16674   else if (strcmp (name, "oddspreg") == 0)
16675     mips_opts.oddspreg = 1;
16676   else if (strcmp (name, "mips16") == 0
16677 	   || strcmp (name, "MIPS-16") == 0)
16678     mips_opts.mips16 = 1;
16679   else if (strcmp (name, "nomips16") == 0
16680 	   || strcmp (name, "noMIPS-16") == 0)
16681     mips_opts.mips16 = 0;
16682   else if (strcmp (name, "micromips") == 0)
16683     mips_opts.micromips = 1;
16684   else if (strcmp (name, "nomicromips") == 0)
16685     mips_opts.micromips = 0;
16686   else if (name[0] == 'n'
16687 	   && name[1] == 'o'
16688 	   && (ase = mips_lookup_ase (name + 2)))
16689     mips_set_ase (ase, &mips_opts, false);
16690   else if ((ase = mips_lookup_ase (name)))
16691     mips_set_ase (ase, &mips_opts, true);
16692   else if (startswith (name, "mips") || startswith (name, "arch="))
16693     {
16694       /* Permit the user to change the ISA and architecture on the fly.
16695 	 Needless to say, misuse can cause serious problems.  */
16696       if (startswith (name, "arch="))
16697 	{
16698 	  const struct mips_cpu_info *p;
16699 
16700 	  p = mips_parse_cpu ("internal use", name + 5);
16701 	  if (!p)
16702 	    as_bad (_("unknown architecture %s"), name + 5);
16703 	  else
16704 	    {
16705 	      mips_opts.arch = p->cpu;
16706 	      mips_opts.isa = p->isa;
16707 	      isa_set = true;
16708 	      mips_opts.init_ase = p->ase;
16709 	    }
16710 	}
16711       else if (startswith (name, "mips"))
16712 	{
16713 	  const struct mips_cpu_info *p;
16714 
16715 	  p = mips_parse_cpu ("internal use", name);
16716 	  if (!p)
16717 	    as_bad (_("unknown ISA level %s"), name + 4);
16718 	  else
16719 	    {
16720 	      mips_opts.arch = p->cpu;
16721 	      mips_opts.isa = p->isa;
16722 	      isa_set = true;
16723 	      mips_opts.init_ase = p->ase;
16724 	    }
16725 	}
16726       else
16727 	as_bad (_("unknown ISA or architecture %s"), name);
16728     }
16729   else if (strcmp (name, "autoextend") == 0)
16730     mips_opts.noautoextend = 0;
16731   else if (strcmp (name, "noautoextend") == 0)
16732     mips_opts.noautoextend = 1;
16733   else if (strcmp (name, "insn32") == 0)
16734     mips_opts.insn32 = true;
16735   else if (strcmp (name, "noinsn32") == 0)
16736     mips_opts.insn32 = false;
16737   else if (strcmp (name, "sym32") == 0)
16738     mips_opts.sym32 = true;
16739   else if (strcmp (name, "nosym32") == 0)
16740     mips_opts.sym32 = false;
16741   else
16742     return OPTION_TYPE_BAD;
16743 
16744   return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
16745 }
16746 
16747 /* Handle the .set pseudo-op.  */
16748 
16749 static void
s_mipsset(int x ATTRIBUTE_UNUSED)16750 s_mipsset (int x ATTRIBUTE_UNUSED)
16751 {
16752   enum code_option_type type = OPTION_TYPE_NORMAL;
16753   char *name = input_line_pointer, ch;
16754 
16755   file_mips_check_options ();
16756 
16757   while (!is_end_of_line[(unsigned char) *input_line_pointer])
16758     ++input_line_pointer;
16759   ch = *input_line_pointer;
16760   *input_line_pointer = '\0';
16761 
16762   if (strchr (name, ','))
16763     {
16764       /* Generic ".set" directive; use the generic handler.  */
16765       *input_line_pointer = ch;
16766       input_line_pointer = name;
16767       s_set (0);
16768       return;
16769     }
16770 
16771   if (strcmp (name, "reorder") == 0)
16772     {
16773       if (mips_opts.noreorder)
16774 	end_noreorder ();
16775     }
16776   else if (strcmp (name, "noreorder") == 0)
16777     {
16778       if (!mips_opts.noreorder)
16779 	start_noreorder ();
16780     }
16781   else if (strcmp (name, "macro") == 0)
16782     mips_opts.warn_about_macros = 0;
16783   else if (strcmp (name, "nomacro") == 0)
16784     {
16785       if (mips_opts.noreorder == 0)
16786 	as_bad (_("`noreorder' must be set before `nomacro'"));
16787       mips_opts.warn_about_macros = 1;
16788     }
16789   else if (strcmp (name, "gp=default") == 0)
16790     mips_opts.gp = file_mips_opts.gp;
16791   else if (strcmp (name, "fp=default") == 0)
16792     mips_opts.fp = file_mips_opts.fp;
16793   else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16794     {
16795       mips_opts.isa = file_mips_opts.isa;
16796       mips_opts.arch = file_mips_opts.arch;
16797       mips_opts.init_ase = file_mips_opts.init_ase;
16798       mips_opts.gp = file_mips_opts.gp;
16799       mips_opts.fp = file_mips_opts.fp;
16800     }
16801   else if (strcmp (name, "push") == 0)
16802     {
16803       struct mips_option_stack *s;
16804 
16805       s = XNEW (struct mips_option_stack);
16806       s->next = mips_opts_stack;
16807       s->options = mips_opts;
16808       mips_opts_stack = s;
16809     }
16810   else if (strcmp (name, "pop") == 0)
16811     {
16812       struct mips_option_stack *s;
16813 
16814       s = mips_opts_stack;
16815       if (s == NULL)
16816 	as_bad (_(".set pop with no .set push"));
16817       else
16818 	{
16819 	  /* If we're changing the reorder mode we need to handle
16820              delay slots correctly.  */
16821 	  if (s->options.noreorder && ! mips_opts.noreorder)
16822 	    start_noreorder ();
16823 	  else if (! s->options.noreorder && mips_opts.noreorder)
16824 	    end_noreorder ();
16825 
16826 	  mips_opts = s->options;
16827 	  mips_opts_stack = s->next;
16828 	  free (s);
16829 	}
16830     }
16831   else
16832     {
16833       type = parse_code_option (name);
16834       if (type == OPTION_TYPE_BAD)
16835 	as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16836     }
16837 
16838   /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16839      registers based on what is supported by the arch/cpu.  */
16840   if (type == OPTION_TYPE_ISA)
16841     {
16842       switch (mips_opts.isa)
16843 	{
16844 	case 0:
16845 	  break;
16846 	case ISA_MIPS1:
16847 	  /* MIPS I cannot support FPXX.  */
16848 	  mips_opts.fp = 32;
16849 	  /* fall-through.  */
16850 	case ISA_MIPS2:
16851 	case ISA_MIPS32:
16852 	case ISA_MIPS32R2:
16853 	case ISA_MIPS32R3:
16854 	case ISA_MIPS32R5:
16855 	  mips_opts.gp = 32;
16856 	  if (mips_opts.fp != 0)
16857 	    mips_opts.fp = 32;
16858 	  break;
16859 	case ISA_MIPS32R6:
16860 	  mips_opts.gp = 32;
16861 	  mips_opts.fp = 64;
16862 	  break;
16863 	case ISA_MIPS3:
16864 	case ISA_MIPS4:
16865 	case ISA_MIPS5:
16866 	case ISA_MIPS64:
16867 	case ISA_MIPS64R2:
16868 	case ISA_MIPS64R3:
16869 	case ISA_MIPS64R5:
16870 	case ISA_MIPS64R6:
16871 	  mips_opts.gp = 64;
16872 	  if (mips_opts.fp != 0)
16873 	    {
16874 	      if (mips_opts.arch == CPU_R5900)
16875 		mips_opts.fp = 32;
16876 	      else
16877 		mips_opts.fp = 64;
16878 	    }
16879 	  break;
16880 	default:
16881 	  as_bad (_("unknown ISA level %s"), name + 4);
16882 	  break;
16883 	}
16884     }
16885 
16886   mips_check_options (&mips_opts, false);
16887 
16888   mips_check_isa_supports_ases ();
16889   *input_line_pointer = ch;
16890   demand_empty_rest_of_line ();
16891 }
16892 
16893 /* Handle the .module pseudo-op.  */
16894 
16895 static void
s_module(int ignore ATTRIBUTE_UNUSED)16896 s_module (int ignore ATTRIBUTE_UNUSED)
16897 {
16898   char *name = input_line_pointer, ch;
16899 
16900   while (!is_end_of_line[(unsigned char) *input_line_pointer])
16901     ++input_line_pointer;
16902   ch = *input_line_pointer;
16903   *input_line_pointer = '\0';
16904 
16905   if (!file_mips_opts_checked)
16906     {
16907       if (parse_code_option (name) == OPTION_TYPE_BAD)
16908 	as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16909 
16910       /* Update module level settings from mips_opts.  */
16911       file_mips_opts = mips_opts;
16912     }
16913   else
16914     as_bad (_(".module is not permitted after generating code"));
16915 
16916   *input_line_pointer = ch;
16917   demand_empty_rest_of_line ();
16918 }
16919 
16920 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
16921    .option pic2.  It means to generate SVR4 PIC calls.  */
16922 
16923 static void
s_abicalls(int ignore ATTRIBUTE_UNUSED)16924 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16925 {
16926   mips_pic = SVR4_PIC;
16927   mips_abicalls = true;
16928 
16929   if (g_switch_seen && g_switch_value != 0)
16930     as_warn (_("-G may not be used with SVR4 PIC code"));
16931   g_switch_value = 0;
16932 
16933   bfd_set_gp_size (stdoutput, 0);
16934   demand_empty_rest_of_line ();
16935 }
16936 
16937 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
16938    PIC code.  It sets the $gp register for the function based on the
16939    function address, which is in the register named in the argument.
16940    This uses a relocation against _gp_disp, which is handled specially
16941    by the linker.  The result is:
16942 	lui	$gp,%hi(_gp_disp)
16943 	addiu	$gp,$gp,%lo(_gp_disp)
16944 	addu	$gp,$gp,.cpload argument
16945    The .cpload argument is normally $25 == $t9.
16946 
16947    The -mno-shared option changes this to:
16948 	lui	$gp,%hi(__gnu_local_gp)
16949 	addiu	$gp,$gp,%lo(__gnu_local_gp)
16950    and the argument is ignored.  This saves an instruction, but the
16951    resulting code is not position independent; it uses an absolute
16952    address for __gnu_local_gp.  Thus code assembled with -mno-shared
16953    can go into an ordinary executable, but not into a shared library.  */
16954 
16955 static void
s_cpload(int ignore ATTRIBUTE_UNUSED)16956 s_cpload (int ignore ATTRIBUTE_UNUSED)
16957 {
16958   expressionS ex;
16959   int reg;
16960   int in_shared;
16961 
16962   file_mips_check_options ();
16963 
16964   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16965      .cpload is ignored.  */
16966   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16967     {
16968       s_ignore (0);
16969       return;
16970     }
16971 
16972   if (mips_opts.mips16)
16973     {
16974       as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16975       ignore_rest_of_line ();
16976       return;
16977     }
16978 
16979   /* .cpload should be in a .set noreorder section.  */
16980   if (mips_opts.noreorder == 0)
16981     as_warn (_(".cpload not in noreorder section"));
16982 
16983   reg = tc_get_register (0);
16984 
16985   /* If we need to produce a 64-bit address, we are better off using
16986      the default instruction sequence.  */
16987   in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16988 
16989   ex.X_op = O_symbol;
16990   ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16991                                          "__gnu_local_gp");
16992   ex.X_op_symbol = NULL;
16993   ex.X_add_number = 0;
16994 
16995   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16996   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16997 
16998   mips_mark_labels ();
16999   mips_assembling_insn = true;
17000 
17001   macro_start ();
17002   macro_build_lui (&ex, mips_gp_register);
17003   macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17004 	       mips_gp_register, BFD_RELOC_LO16);
17005   if (in_shared)
17006     macro_build (NULL, "addu", "d,v,t", mips_gp_register,
17007 		 mips_gp_register, reg);
17008   macro_end ();
17009 
17010   mips_assembling_insn = false;
17011   demand_empty_rest_of_line ();
17012 }
17013 
17014 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
17015      .cpsetup $reg1, offset|$reg2, label
17016 
17017    If offset is given, this results in:
17018      sd		$gp, offset($sp)
17019      lui	$gp, %hi(%neg(%gp_rel(label)))
17020      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
17021      daddu	$gp, $gp, $reg1
17022 
17023    If $reg2 is given, this results in:
17024      or		$reg2, $gp, $0
17025      lui	$gp, %hi(%neg(%gp_rel(label)))
17026      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
17027      daddu	$gp, $gp, $reg1
17028    $reg1 is normally $25 == $t9.
17029 
17030    The -mno-shared option replaces the last three instructions with
17031 	lui	$gp,%hi(_gp)
17032 	addiu	$gp,$gp,%lo(_gp)  */
17033 
17034 static void
s_cpsetup(int ignore ATTRIBUTE_UNUSED)17035 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
17036 {
17037   expressionS ex_off;
17038   expressionS ex_sym;
17039   int reg1;
17040 
17041   file_mips_check_options ();
17042 
17043   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
17044      We also need NewABI support.  */
17045   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17046     {
17047       s_ignore (0);
17048       return;
17049     }
17050 
17051   if (mips_opts.mips16)
17052     {
17053       as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
17054       ignore_rest_of_line ();
17055       return;
17056     }
17057 
17058   reg1 = tc_get_register (0);
17059   SKIP_WHITESPACE ();
17060   if (*input_line_pointer != ',')
17061     {
17062       as_bad (_("missing argument separator ',' for .cpsetup"));
17063       return;
17064     }
17065   else
17066     ++input_line_pointer;
17067   SKIP_WHITESPACE ();
17068   if (*input_line_pointer == '$')
17069     {
17070       mips_cpreturn_register = tc_get_register (0);
17071       mips_cpreturn_offset = -1;
17072     }
17073   else
17074     {
17075       mips_cpreturn_offset = get_absolute_expression ();
17076       mips_cpreturn_register = -1;
17077     }
17078   SKIP_WHITESPACE ();
17079   if (*input_line_pointer != ',')
17080     {
17081       as_bad (_("missing argument separator ',' for .cpsetup"));
17082       return;
17083     }
17084   else
17085     ++input_line_pointer;
17086   SKIP_WHITESPACE ();
17087   expression (&ex_sym);
17088 
17089   mips_mark_labels ();
17090   mips_assembling_insn = true;
17091 
17092   macro_start ();
17093   if (mips_cpreturn_register == -1)
17094     {
17095       ex_off.X_op = O_constant;
17096       ex_off.X_add_symbol = NULL;
17097       ex_off.X_op_symbol = NULL;
17098       ex_off.X_add_number = mips_cpreturn_offset;
17099 
17100       macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17101 		   BFD_RELOC_LO16, SP);
17102     }
17103   else
17104     move_register (mips_cpreturn_register, mips_gp_register);
17105 
17106   if (mips_in_shared || HAVE_64BIT_SYMBOLS)
17107     {
17108       macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
17109 		   -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
17110 		   BFD_RELOC_HI16_S);
17111 
17112       macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
17113 		   mips_gp_register, -1, BFD_RELOC_GPREL16,
17114 		   BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
17115 
17116       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
17117 		   mips_gp_register, reg1);
17118     }
17119   else
17120     {
17121       expressionS ex;
17122 
17123       ex.X_op = O_symbol;
17124       ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
17125       ex.X_op_symbol = NULL;
17126       ex.X_add_number = 0;
17127 
17128       /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
17129       symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
17130 
17131       macro_build_lui (&ex, mips_gp_register);
17132       macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17133 		   mips_gp_register, BFD_RELOC_LO16);
17134     }
17135 
17136   macro_end ();
17137 
17138   mips_assembling_insn = false;
17139   demand_empty_rest_of_line ();
17140 }
17141 
17142 static void
s_cplocal(int ignore ATTRIBUTE_UNUSED)17143 s_cplocal (int ignore ATTRIBUTE_UNUSED)
17144 {
17145   file_mips_check_options ();
17146 
17147   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
17148      .cplocal is ignored.  */
17149   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17150     {
17151       s_ignore (0);
17152       return;
17153     }
17154 
17155   if (mips_opts.mips16)
17156     {
17157       as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
17158       ignore_rest_of_line ();
17159       return;
17160     }
17161 
17162   mips_gp_register = tc_get_register (0);
17163   demand_empty_rest_of_line ();
17164 }
17165 
17166 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
17167    offset from $sp.  The offset is remembered, and after making a PIC
17168    call $gp is restored from that location.  */
17169 
17170 static void
s_cprestore(int ignore ATTRIBUTE_UNUSED)17171 s_cprestore (int ignore ATTRIBUTE_UNUSED)
17172 {
17173   expressionS ex;
17174 
17175   file_mips_check_options ();
17176 
17177   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
17178      .cprestore is ignored.  */
17179   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
17180     {
17181       s_ignore (0);
17182       return;
17183     }
17184 
17185   if (mips_opts.mips16)
17186     {
17187       as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
17188       ignore_rest_of_line ();
17189       return;
17190     }
17191 
17192   mips_cprestore_offset = get_absolute_expression ();
17193   mips_cprestore_valid = 1;
17194 
17195   ex.X_op = O_constant;
17196   ex.X_add_symbol = NULL;
17197   ex.X_op_symbol = NULL;
17198   ex.X_add_number = mips_cprestore_offset;
17199 
17200   mips_mark_labels ();
17201   mips_assembling_insn = true;
17202 
17203   macro_start ();
17204   macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
17205 				SP, HAVE_64BIT_ADDRESSES);
17206   macro_end ();
17207 
17208   mips_assembling_insn = false;
17209   demand_empty_rest_of_line ();
17210 }
17211 
17212 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
17213    was given in the preceding .cpsetup, it results in:
17214      ld		$gp, offset($sp)
17215 
17216    If a register $reg2 was given there, it results in:
17217      or		$gp, $reg2, $0  */
17218 
17219 static void
s_cpreturn(int ignore ATTRIBUTE_UNUSED)17220 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
17221 {
17222   expressionS ex;
17223 
17224   file_mips_check_options ();
17225 
17226   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
17227      We also need NewABI support.  */
17228   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17229     {
17230       s_ignore (0);
17231       return;
17232     }
17233 
17234   if (mips_opts.mips16)
17235     {
17236       as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
17237       ignore_rest_of_line ();
17238       return;
17239     }
17240 
17241   mips_mark_labels ();
17242   mips_assembling_insn = true;
17243 
17244   macro_start ();
17245   if (mips_cpreturn_register == -1)
17246     {
17247       ex.X_op = O_constant;
17248       ex.X_add_symbol = NULL;
17249       ex.X_op_symbol = NULL;
17250       ex.X_add_number = mips_cpreturn_offset;
17251 
17252       macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
17253     }
17254   else
17255     move_register (mips_gp_register, mips_cpreturn_register);
17256 
17257   macro_end ();
17258 
17259   mips_assembling_insn = false;
17260   demand_empty_rest_of_line ();
17261 }
17262 
17263 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17264    pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17265    DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17266    debug information or MIPS16 TLS.  */
17267 
17268 static void
s_tls_rel_directive(const size_t bytes,const char * dirstr,bfd_reloc_code_real_type rtype)17269 s_tls_rel_directive (const size_t bytes, const char *dirstr,
17270 		     bfd_reloc_code_real_type rtype)
17271 {
17272   expressionS ex;
17273   char *p;
17274 
17275   expression (&ex);
17276 
17277   if (ex.X_op != O_symbol)
17278     {
17279       as_bad (_("unsupported use of %s"), dirstr);
17280       ignore_rest_of_line ();
17281     }
17282 
17283   p = frag_more (bytes);
17284   md_number_to_chars (p, 0, bytes);
17285   fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, false, rtype);
17286   demand_empty_rest_of_line ();
17287   mips_clear_insn_labels ();
17288 }
17289 
17290 /* Handle .dtprelword.  */
17291 
17292 static void
s_dtprelword(int ignore ATTRIBUTE_UNUSED)17293 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17294 {
17295   s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
17296 }
17297 
17298 /* Handle .dtpreldword.  */
17299 
17300 static void
s_dtpreldword(int ignore ATTRIBUTE_UNUSED)17301 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17302 {
17303   s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17304 }
17305 
17306 /* Handle .tprelword.  */
17307 
17308 static void
s_tprelword(int ignore ATTRIBUTE_UNUSED)17309 s_tprelword (int ignore ATTRIBUTE_UNUSED)
17310 {
17311   s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17312 }
17313 
17314 /* Handle .tpreldword.  */
17315 
17316 static void
s_tpreldword(int ignore ATTRIBUTE_UNUSED)17317 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17318 {
17319   s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
17320 }
17321 
17322 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
17323    code.  It sets the offset to use in gp_rel relocations.  */
17324 
17325 static void
s_gpvalue(int ignore ATTRIBUTE_UNUSED)17326 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
17327 {
17328   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17329      We also need NewABI support.  */
17330   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17331     {
17332       s_ignore (0);
17333       return;
17334     }
17335 
17336   mips_gprel_offset = get_absolute_expression ();
17337 
17338   demand_empty_rest_of_line ();
17339 }
17340 
17341 /* Handle the .gpword pseudo-op.  This is used when generating PIC
17342    code.  It generates a 32 bit GP relative reloc.  */
17343 
17344 static void
s_gpword(int ignore ATTRIBUTE_UNUSED)17345 s_gpword (int ignore ATTRIBUTE_UNUSED)
17346 {
17347   segment_info_type *si;
17348   struct insn_label_list *l;
17349   expressionS ex;
17350   char *p;
17351 
17352   /* When not generating PIC code, this is treated as .word.  */
17353   if (mips_pic != SVR4_PIC)
17354     {
17355       s_cons (2);
17356       return;
17357     }
17358 
17359   si = seg_info (now_seg);
17360   l = si->label_list;
17361   mips_emit_delays ();
17362   if (auto_align)
17363     mips_align (2, 0, l);
17364 
17365   expression (&ex);
17366   mips_clear_insn_labels ();
17367 
17368   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17369     {
17370       as_bad (_("unsupported use of .gpword"));
17371       ignore_rest_of_line ();
17372     }
17373 
17374   p = frag_more (4);
17375   md_number_to_chars (p, 0, 4);
17376   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
17377 	       BFD_RELOC_GPREL32);
17378 
17379   demand_empty_rest_of_line ();
17380 }
17381 
17382 static void
s_gpdword(int ignore ATTRIBUTE_UNUSED)17383 s_gpdword (int ignore ATTRIBUTE_UNUSED)
17384 {
17385   segment_info_type *si;
17386   struct insn_label_list *l;
17387   expressionS ex;
17388   char *p;
17389 
17390   /* When not generating PIC code, this is treated as .dword.  */
17391   if (mips_pic != SVR4_PIC)
17392     {
17393       s_cons (3);
17394       return;
17395     }
17396 
17397   si = seg_info (now_seg);
17398   l = si->label_list;
17399   mips_emit_delays ();
17400   if (auto_align)
17401     mips_align (3, 0, l);
17402 
17403   expression (&ex);
17404   mips_clear_insn_labels ();
17405 
17406   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17407     {
17408       as_bad (_("unsupported use of .gpdword"));
17409       ignore_rest_of_line ();
17410     }
17411 
17412   p = frag_more (8);
17413   md_number_to_chars (p, 0, 8);
17414   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
17415 	       BFD_RELOC_GPREL32)->fx_tcbit = 1;
17416 
17417   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
17418   fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17419 	   false, BFD_RELOC_64)->fx_tcbit = 1;
17420 
17421   demand_empty_rest_of_line ();
17422 }
17423 
17424 /* Handle the .ehword pseudo-op.  This is used when generating unwinding
17425    tables.  It generates a R_MIPS_EH reloc.  */
17426 
17427 static void
s_ehword(int ignore ATTRIBUTE_UNUSED)17428 s_ehword (int ignore ATTRIBUTE_UNUSED)
17429 {
17430   expressionS ex;
17431   char *p;
17432 
17433   mips_emit_delays ();
17434 
17435   expression (&ex);
17436   mips_clear_insn_labels ();
17437 
17438   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17439     {
17440       as_bad (_("unsupported use of .ehword"));
17441       ignore_rest_of_line ();
17442     }
17443 
17444   p = frag_more (4);
17445   md_number_to_chars (p, 0, 4);
17446   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
17447 	       BFD_RELOC_32_PCREL);
17448 
17449   demand_empty_rest_of_line ();
17450 }
17451 
17452 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
17453    tables in SVR4 PIC code.  */
17454 
17455 static void
s_cpadd(int ignore ATTRIBUTE_UNUSED)17456 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17457 {
17458   int reg;
17459 
17460   file_mips_check_options ();
17461 
17462   /* This is ignored when not generating SVR4 PIC code.  */
17463   if (mips_pic != SVR4_PIC)
17464     {
17465       s_ignore (0);
17466       return;
17467     }
17468 
17469   mips_mark_labels ();
17470   mips_assembling_insn = true;
17471 
17472   /* Add $gp to the register named as an argument.  */
17473   macro_start ();
17474   reg = tc_get_register (0);
17475   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17476   macro_end ();
17477 
17478   mips_assembling_insn = false;
17479   demand_empty_rest_of_line ();
17480 }
17481 
17482 /* Handle the .insn pseudo-op.  This marks instruction labels in
17483    mips16/micromips mode.  This permits the linker to handle them specially,
17484    such as generating jalx instructions when needed.  We also make
17485    them odd for the duration of the assembly, in order to generate the
17486    right sort of code.  We will make them even in the adjust_symtab
17487    routine, while leaving them marked.  This is convenient for the
17488    debugger and the disassembler.  The linker knows to make them odd
17489    again.  */
17490 
17491 static void
s_insn(int ignore ATTRIBUTE_UNUSED)17492 s_insn (int ignore ATTRIBUTE_UNUSED)
17493 {
17494   file_mips_check_options ();
17495   file_ase_mips16 |= mips_opts.mips16;
17496   file_ase_micromips |= mips_opts.micromips;
17497 
17498   mips_mark_labels ();
17499 
17500   demand_empty_rest_of_line ();
17501 }
17502 
17503 /* Handle the .nan pseudo-op.  */
17504 
17505 static void
s_nan(int ignore ATTRIBUTE_UNUSED)17506 s_nan (int ignore ATTRIBUTE_UNUSED)
17507 {
17508   static const char str_legacy[] = "legacy";
17509   static const char str_2008[] = "2008";
17510   size_t i;
17511 
17512   for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17513 
17514   if (i == sizeof (str_2008) - 1
17515       && memcmp (input_line_pointer, str_2008, i) == 0)
17516     mips_nan2008 = 1;
17517   else if (i == sizeof (str_legacy) - 1
17518 	   && memcmp (input_line_pointer, str_legacy, i) == 0)
17519     {
17520       if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17521 	mips_nan2008 = 0;
17522       else
17523 	as_bad (_("`%s' does not support legacy NaN"),
17524 	          mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17525     }
17526   else
17527     as_bad (_("bad .nan directive"));
17528 
17529   input_line_pointer += i;
17530   demand_empty_rest_of_line ();
17531 }
17532 
17533 /* Handle a .stab[snd] directive.  Ideally these directives would be
17534    implemented in a transparent way, so that removing them would not
17535    have any effect on the generated instructions.  However, s_stab
17536    internally changes the section, so in practice we need to decide
17537    now whether the preceding label marks compressed code.  We do not
17538    support changing the compression mode of a label after a .stab*
17539    directive, such as in:
17540 
17541    foo:
17542 	.stabs ...
17543 	.set mips16
17544 
17545    so the current mode wins.  */
17546 
17547 static void
s_mips_stab(int type)17548 s_mips_stab (int type)
17549 {
17550   file_mips_check_options ();
17551   mips_mark_labels ();
17552   s_stab (type);
17553 }
17554 
17555 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.  */
17556 
17557 static void
s_mips_weakext(int ignore ATTRIBUTE_UNUSED)17558 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17559 {
17560   char *name;
17561   int c;
17562   symbolS *symbolP;
17563   expressionS exp;
17564 
17565   c = get_symbol_name (&name);
17566   symbolP = symbol_find_or_make (name);
17567   S_SET_WEAK (symbolP);
17568   *input_line_pointer = c;
17569 
17570   SKIP_WHITESPACE_AFTER_NAME ();
17571 
17572   if (! is_end_of_line[(unsigned char) *input_line_pointer])
17573     {
17574       if (S_IS_DEFINED (symbolP))
17575 	{
17576 	  as_bad (_("ignoring attempt to redefine symbol %s"),
17577 		  S_GET_NAME (symbolP));
17578 	  ignore_rest_of_line ();
17579 	  return;
17580 	}
17581 
17582       if (*input_line_pointer == ',')
17583 	{
17584 	  ++input_line_pointer;
17585 	  SKIP_WHITESPACE ();
17586 	}
17587 
17588       expression (&exp);
17589       if (exp.X_op != O_symbol)
17590 	{
17591 	  as_bad (_("bad .weakext directive"));
17592 	  ignore_rest_of_line ();
17593 	  return;
17594 	}
17595       symbol_set_value_expression (symbolP, &exp);
17596     }
17597 
17598   demand_empty_rest_of_line ();
17599 }
17600 
17601 /* Parse a register string into a number.  Called from the ECOFF code
17602    to parse .frame.  The argument is non-zero if this is the frame
17603    register, so that we can record it in mips_frame_reg.  */
17604 
17605 int
tc_get_register(int frame)17606 tc_get_register (int frame)
17607 {
17608   unsigned int reg;
17609 
17610   SKIP_WHITESPACE ();
17611   if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17612     reg = 0;
17613   if (frame)
17614     {
17615       mips_frame_reg = reg != 0 ? reg : SP;
17616       mips_frame_reg_valid = 1;
17617       mips_cprestore_valid = 0;
17618     }
17619   return reg;
17620 }
17621 
17622 valueT
md_section_align(asection * seg,valueT addr)17623 md_section_align (asection *seg, valueT addr)
17624 {
17625   int align = bfd_section_alignment (seg);
17626 
17627   /* We don't need to align ELF sections to the full alignment.
17628      However, Irix 5 may prefer that we align them at least to a 16
17629      byte boundary.  We don't bother to align the sections if we
17630      are targeted for an embedded system.  */
17631   if (startswith (TARGET_OS, "elf"))
17632     return addr;
17633   if (align > 4)
17634     align = 4;
17635 
17636   return ((addr + (1 << align) - 1) & -(1 << align));
17637 }
17638 
17639 /* Utility routine, called from above as well.  If called while the
17640    input file is still being read, it's only an approximation.  (For
17641    example, a symbol may later become defined which appeared to be
17642    undefined earlier.)  */
17643 
17644 static int
nopic_need_relax(symbolS * sym,int before_relaxing)17645 nopic_need_relax (symbolS *sym, int before_relaxing)
17646 {
17647   if (sym == 0)
17648     return 0;
17649 
17650   if (g_switch_value > 0)
17651     {
17652       const char *symname;
17653       int change;
17654 
17655       /* Find out whether this symbol can be referenced off the $gp
17656 	 register.  It can be if it is smaller than the -G size or if
17657 	 it is in the .sdata or .sbss section.  Certain symbols can
17658 	 not be referenced off the $gp, although it appears as though
17659 	 they can.  */
17660       symname = S_GET_NAME (sym);
17661       if (symname != (const char *) NULL
17662 	  && (strcmp (symname, "eprol") == 0
17663 	      || strcmp (symname, "etext") == 0
17664 	      || strcmp (symname, "_gp") == 0
17665 	      || strcmp (symname, "edata") == 0
17666 	      || strcmp (symname, "_fbss") == 0
17667 	      || strcmp (symname, "_fdata") == 0
17668 	      || strcmp (symname, "_ftext") == 0
17669 	      || strcmp (symname, "end") == 0
17670 	      || strcmp (symname, "_gp_disp") == 0))
17671 	change = 1;
17672       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17673 	       && (0
17674 #ifndef NO_ECOFF_DEBUGGING
17675 		   || (symbol_get_obj (sym)->ecoff_extern_size != 0
17676 		       && (symbol_get_obj (sym)->ecoff_extern_size
17677 			   <= g_switch_value))
17678 #endif
17679 		   /* We must defer this decision until after the whole
17680 		      file has been read, since there might be a .extern
17681 		      after the first use of this symbol.  */
17682 		   || (before_relaxing
17683 #ifndef NO_ECOFF_DEBUGGING
17684 		       && symbol_get_obj (sym)->ecoff_extern_size == 0
17685 #endif
17686 		       && S_GET_VALUE (sym) == 0)
17687 		   || (S_GET_VALUE (sym) != 0
17688 		       && S_GET_VALUE (sym) <= g_switch_value)))
17689 	change = 0;
17690       else
17691 	{
17692 	  const char *segname;
17693 
17694 	  segname = segment_name (S_GET_SEGMENT (sym));
17695 	  gas_assert (strcmp (segname, ".lit8") != 0
17696 		  && strcmp (segname, ".lit4") != 0);
17697 	  change = (strcmp (segname, ".sdata") != 0
17698 		    && strcmp (segname, ".sbss") != 0
17699 		    && !startswith (segname, ".sdata.")
17700 		    && !startswith (segname, ".sbss.")
17701 		    && !startswith (segname, ".gnu.linkonce.sb.")
17702 		    && !startswith (segname, ".gnu.linkonce.s."));
17703 	}
17704       return change;
17705     }
17706   else
17707     /* We are not optimizing for the $gp register.  */
17708     return 1;
17709 }
17710 
17711 
17712 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
17713 
17714 static bool
pic_need_relax(symbolS * sym)17715 pic_need_relax (symbolS *sym)
17716 {
17717   asection *symsec;
17718 
17719   /* Handle the case of a symbol equated to another symbol.  */
17720   while (symbol_equated_reloc_p (sym))
17721     {
17722       symbolS *n;
17723 
17724       /* It's possible to get a loop here in a badly written program.  */
17725       n = symbol_get_value_expression (sym)->X_add_symbol;
17726       if (n == sym)
17727 	break;
17728       sym = n;
17729     }
17730 
17731   if (symbol_section_p (sym))
17732     return true;
17733 
17734   symsec = S_GET_SEGMENT (sym);
17735 
17736   /* This must duplicate the test in adjust_reloc_syms.  */
17737   return (!bfd_is_und_section (symsec)
17738 	  && !bfd_is_abs_section (symsec)
17739 	  && !bfd_is_com_section (symsec)
17740 	  /* A global or weak symbol is treated as external.  */
17741 	  && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
17742 }
17743 
17744 /* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17745    convert a section-relative value VAL to the equivalent PC-relative
17746    value.  */
17747 
17748 static offsetT
mips16_pcrel_val(fragS * fragp,const struct mips_pcrel_operand * pcrel_op,offsetT val,long stretch)17749 mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17750 		  offsetT val, long stretch)
17751 {
17752   fragS *sym_frag;
17753   addressT addr;
17754 
17755   gas_assert (pcrel_op->root.root.type == OP_PCREL);
17756 
17757   sym_frag = symbol_get_frag (fragp->fr_symbol);
17758 
17759   /* If the relax_marker of the symbol fragment differs from the
17760      relax_marker of this fragment, we have not yet adjusted the
17761      symbol fragment fr_address.  We want to add in STRETCH in
17762      order to get a better estimate of the address.  This
17763      particularly matters because of the shift bits.  */
17764   if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17765     {
17766       fragS *f;
17767 
17768       /* Adjust stretch for any alignment frag.  Note that if have
17769 	 been expanding the earlier code, the symbol may be
17770 	 defined in what appears to be an earlier frag.  FIXME:
17771 	 This doesn't handle the fr_subtype field, which specifies
17772 	 a maximum number of bytes to skip when doing an
17773 	 alignment.  */
17774       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17775 	{
17776 	  if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17777 	    {
17778 	      if (stretch < 0)
17779 		stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17780 	      else
17781 		stretch &= ~((1 << (int) f->fr_offset) - 1);
17782 	      if (stretch == 0)
17783 		break;
17784 	    }
17785 	}
17786       if (f != NULL)
17787 	val += stretch;
17788     }
17789 
17790   addr = fragp->fr_address + fragp->fr_fix;
17791 
17792   /* The base address rules are complicated.  The base address of
17793      a branch is the following instruction.  The base address of a
17794      PC relative load or add is the instruction itself, but if it
17795      is in a delay slot (in which case it can not be extended) use
17796      the address of the instruction whose delay slot it is in.  */
17797   if (pcrel_op->include_isa_bit)
17798     {
17799       addr += 2;
17800 
17801       /* If we are currently assuming that this frag should be
17802 	 extended, then the current address is two bytes higher.  */
17803       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17804 	addr += 2;
17805 
17806       /* Ignore the low bit in the target, since it will be set
17807 	 for a text label.  */
17808       val &= -2;
17809     }
17810   else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17811     addr -= 4;
17812   else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17813     addr -= 2;
17814 
17815   val -= addr & -(1 << pcrel_op->align_log2);
17816 
17817   return val;
17818 }
17819 
17820 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17821    extended opcode.  SEC is the section the frag is in.  */
17822 
17823 static int
mips16_extended_frag(fragS * fragp,asection * sec,long stretch)17824 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17825 {
17826   const struct mips_int_operand *operand;
17827   offsetT val;
17828   segT symsec;
17829   int type;
17830 
17831   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17832     return 0;
17833   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17834     return 1;
17835 
17836   symsec = S_GET_SEGMENT (fragp->fr_symbol);
17837   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17838   operand = mips16_immed_operand (type, false);
17839   if (S_FORCE_RELOC (fragp->fr_symbol, true)
17840       || (operand->root.type == OP_PCREL
17841 	  ? sec != symsec
17842 	  : !bfd_is_abs_section (symsec)))
17843     return 1;
17844 
17845   val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17846 
17847   if (operand->root.type == OP_PCREL)
17848     {
17849       const struct mips_pcrel_operand *pcrel_op;
17850       offsetT maxtiny;
17851 
17852       if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
17853 	return 1;
17854 
17855       pcrel_op = (const struct mips_pcrel_operand *) operand;
17856       val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17857 
17858       /* If any of the shifted bits are set, we must use an extended
17859          opcode.  If the address depends on the size of this
17860          instruction, this can lead to a loop, so we arrange to always
17861          use an extended opcode.  */
17862       if ((val & ((1 << operand->shift) - 1)) != 0)
17863 	{
17864 	  fragp->fr_subtype =
17865 	    RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17866 	  return 1;
17867 	}
17868 
17869       /* If we are about to mark a frag as extended because the value
17870          is precisely the next value above maxtiny, then there is a
17871          chance of an infinite loop as in the following code:
17872 	     la	$4,foo
17873 	     .skip	1020
17874 	     .align	2
17875 	   foo:
17876 	 In this case when the la is extended, foo is 0x3fc bytes
17877 	 away, so the la can be shrunk, but then foo is 0x400 away, so
17878 	 the la must be extended.  To avoid this loop, we mark the
17879 	 frag as extended if it was small, and is about to become
17880 	 extended with the next value above maxtiny.  */
17881       maxtiny = mips_int_operand_max (operand);
17882       if (val == maxtiny + (1 << operand->shift)
17883 	  && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17884 	{
17885 	  fragp->fr_subtype =
17886 	    RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17887 	  return 1;
17888 	}
17889     }
17890 
17891   return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17892 }
17893 
17894 /* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17895    macro expansion.  SEC is the section the frag is in.  We only
17896    support PC-relative instructions (LA, DLA, LW, LD) here, in
17897    non-PIC code using 32-bit addressing.  */
17898 
17899 static int
mips16_macro_frag(fragS * fragp,asection * sec,long stretch)17900 mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17901 {
17902   const struct mips_pcrel_operand *pcrel_op;
17903   const struct mips_int_operand *operand;
17904   offsetT val;
17905   segT symsec;
17906   int type;
17907 
17908   gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17909 
17910   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17911     return 0;
17912   if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17913     return 0;
17914 
17915   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17916   switch (type)
17917     {
17918     case 'A':
17919     case 'B':
17920     case 'E':
17921       symsec = S_GET_SEGMENT (fragp->fr_symbol);
17922       if (bfd_is_abs_section (symsec))
17923 	return 1;
17924       if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17925 	return 0;
17926       if (S_FORCE_RELOC (fragp->fr_symbol, true) || sec != symsec)
17927 	return 1;
17928 
17929       operand = mips16_immed_operand (type, true);
17930       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17931       pcrel_op = (const struct mips_pcrel_operand *) operand;
17932       val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17933 
17934       return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17935 
17936     default:
17937       return 0;
17938     }
17939 }
17940 
17941 /* Compute the length of a branch sequence, and adjust the
17942    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
17943    worst-case length is computed, with UPDATE being used to indicate
17944    whether an unconditional (-1), branch-likely (+1) or regular (0)
17945    branch is to be computed.  */
17946 static int
relaxed_branch_length(fragS * fragp,asection * sec,int update)17947 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17948 {
17949   bool toofar;
17950   int length;
17951 
17952   if (fragp
17953       && S_IS_DEFINED (fragp->fr_symbol)
17954       && !S_IS_WEAK (fragp->fr_symbol)
17955       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17956     {
17957       addressT addr;
17958       offsetT val;
17959 
17960       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17961 
17962       addr = fragp->fr_address + fragp->fr_fix + 4;
17963 
17964       val -= addr;
17965 
17966       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17967     }
17968   else
17969     /* If the symbol is not defined or it's in a different segment,
17970        we emit the long sequence.  */
17971     toofar = true;
17972 
17973   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17974     fragp->fr_subtype
17975       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17976 			     RELAX_BRANCH_PIC (fragp->fr_subtype),
17977 			     RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17978 			     RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17979 			     RELAX_BRANCH_LINK (fragp->fr_subtype),
17980 			     toofar);
17981 
17982   length = 4;
17983   if (toofar)
17984     {
17985       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17986 	length += 8;
17987 
17988       if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
17989 	{
17990 	  /* Additional space for PIC loading of target address.  */
17991 	  length += 8;
17992 	  if (mips_opts.isa == ISA_MIPS1)
17993 	    /* Additional space for $at-stabilizing nop.  */
17994 	    length += 4;
17995 	}
17996 
17997       /* If branch is conditional.  */
17998       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17999 	length += 8;
18000     }
18001 
18002   return length;
18003 }
18004 
18005 /* Get a FRAG's branch instruction delay slot size, either from the
18006    short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
18007    or SHORT_INSN_SIZE otherwise.  */
18008 
18009 static int
frag_branch_delay_slot_size(fragS * fragp,bool al,int short_insn_size)18010 frag_branch_delay_slot_size (fragS *fragp, bool al, int short_insn_size)
18011 {
18012   char *buf = fragp->fr_literal + fragp->fr_fix;
18013 
18014   if (al)
18015     return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
18016   else
18017     return short_insn_size;
18018 }
18019 
18020 /* Compute the length of a branch sequence, and adjust the
18021    RELAX_MICROMIPS_TOOFAR32 bit accordingly.  If FRAGP is NULL, the
18022    worst-case length is computed, with UPDATE being used to indicate
18023    whether an unconditional (-1), or regular (0) branch is to be
18024    computed.  */
18025 
18026 static int
relaxed_micromips_32bit_branch_length(fragS * fragp,asection * sec,int update)18027 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
18028 {
18029   bool insn32 = true;
18030   bool nods = true;
18031   bool pic = true;
18032   bool al = true;
18033   int short_insn_size;
18034   bool toofar;
18035   int length;
18036 
18037   if (fragp)
18038     {
18039       insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18040       nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18041       pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18042       al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18043     }
18044   short_insn_size = insn32 ? 4 : 2;
18045 
18046   if (fragp
18047       && S_IS_DEFINED (fragp->fr_symbol)
18048       && !S_IS_WEAK (fragp->fr_symbol)
18049       && sec == S_GET_SEGMENT (fragp->fr_symbol))
18050     {
18051       addressT addr;
18052       offsetT val;
18053 
18054       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18055       /* Ignore the low bit in the target, since it will be set
18056 	 for a text label.  */
18057       if ((val & 1) != 0)
18058 	--val;
18059 
18060       addr = fragp->fr_address + fragp->fr_fix + 4;
18061 
18062       val -= addr;
18063 
18064       toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
18065     }
18066   else
18067     /* If the symbol is not defined or it's in a different segment,
18068        we emit the long sequence.  */
18069     toofar = true;
18070 
18071   if (fragp && update
18072       && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18073     fragp->fr_subtype = (toofar
18074 			 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
18075 			 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
18076 
18077   length = 4;
18078   if (toofar)
18079     {
18080       bool compact_known = fragp != NULL;
18081       bool compact = false;
18082       bool uncond;
18083 
18084       if (fragp)
18085 	{
18086 	  compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18087 	  uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
18088 	}
18089       else
18090 	uncond = update < 0;
18091 
18092       /* If label is out of range, we turn branch <br>:
18093 
18094 		<br>	label			# 4 bytes
18095 	    0:
18096 
18097          into:
18098 
18099 		j	label			# 4 bytes
18100 		nop				# 2/4 bytes if
18101 						#  compact && (!PIC || insn32)
18102 	    0:
18103        */
18104       if ((!pic || insn32) && (!compact_known || compact))
18105 	length += short_insn_size;
18106 
18107       /* If assembling PIC code, we further turn:
18108 
18109 			j	label			# 4 bytes
18110 
18111          into:
18112 
18113 			lw/ld	at, %got(label)(gp)	# 4 bytes
18114 			d/addiu	at, %lo(label)		# 4 bytes
18115 			jr/c	at			# 2/4 bytes
18116        */
18117       if (pic)
18118 	length += 4 + short_insn_size;
18119 
18120       /* Add an extra nop if the jump has no compact form and we need
18121          to fill the delay slot.  */
18122       if ((!pic || al) && nods)
18123 	length += (fragp
18124 		   ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
18125 		   : short_insn_size);
18126 
18127       /* If branch <br> is conditional, we prepend negated branch <brneg>:
18128 
18129 			<brneg>	0f			# 4 bytes
18130 			nop				# 2/4 bytes if !compact
18131        */
18132       if (!uncond)
18133 	length += (compact_known && compact) ? 4 : 4 + short_insn_size;
18134     }
18135   else if (nods)
18136     {
18137       /* Add an extra nop to fill the delay slot.  */
18138       gas_assert (fragp);
18139       length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
18140     }
18141 
18142   return length;
18143 }
18144 
18145 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
18146    bit accordingly.  */
18147 
18148 static int
relaxed_micromips_16bit_branch_length(fragS * fragp,asection * sec,int update)18149 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
18150 {
18151   bool toofar;
18152 
18153   if (fragp
18154       && S_IS_DEFINED (fragp->fr_symbol)
18155       && !S_IS_WEAK (fragp->fr_symbol)
18156       && sec == S_GET_SEGMENT (fragp->fr_symbol))
18157     {
18158       addressT addr;
18159       offsetT val;
18160       int type;
18161 
18162       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18163       /* Ignore the low bit in the target, since it will be set
18164 	 for a text label.  */
18165       if ((val & 1) != 0)
18166 	--val;
18167 
18168       /* Assume this is a 2-byte branch.  */
18169       addr = fragp->fr_address + fragp->fr_fix + 2;
18170 
18171       /* We try to avoid the infinite loop by not adding 2 more bytes for
18172 	 long branches.  */
18173 
18174       val -= addr;
18175 
18176       type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18177       if (type == 'D')
18178 	toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
18179       else if (type == 'E')
18180 	toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
18181       else
18182 	abort ();
18183     }
18184   else
18185     /* If the symbol is not defined or it's in a different segment,
18186        we emit a normal 32-bit branch.  */
18187     toofar = true;
18188 
18189   if (fragp && update
18190       && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18191     fragp->fr_subtype
18192       = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
18193 	       : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
18194 
18195   if (toofar)
18196     return 4;
18197 
18198   return 2;
18199 }
18200 
18201 /* Estimate the size of a frag before relaxing.  Unless this is the
18202    mips16, we are not really relaxing here, and the final size is
18203    encoded in the subtype information.  For the mips16, we have to
18204    decide whether we are using an extended opcode or not.  */
18205 
18206 int
md_estimate_size_before_relax(fragS * fragp,asection * segtype)18207 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
18208 {
18209   int change;
18210 
18211   if (RELAX_BRANCH_P (fragp->fr_subtype))
18212     {
18213 
18214       fragp->fr_var = relaxed_branch_length (fragp, segtype, false);
18215 
18216       return fragp->fr_var;
18217     }
18218 
18219   if (RELAX_MIPS16_P (fragp->fr_subtype))
18220     {
18221       /* We don't want to modify the EXTENDED bit here; it might get us
18222 	 into infinite loops.  We change it only in mips_relax_frag().  */
18223       if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18224 	return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
18225       else
18226 	return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
18227     }
18228 
18229   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18230     {
18231       int length = 4;
18232 
18233       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18234 	length = relaxed_micromips_16bit_branch_length (fragp, segtype, false);
18235       if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18236 	length = relaxed_micromips_32bit_branch_length (fragp, segtype, false);
18237       fragp->fr_var = length;
18238 
18239       return length;
18240     }
18241 
18242   if (mips_pic == VXWORKS_PIC)
18243     /* For vxworks, GOT16 relocations never have a corresponding LO16.  */
18244     change = 0;
18245   else if (RELAX_PIC (fragp->fr_subtype))
18246     change = pic_need_relax (fragp->fr_symbol);
18247   else
18248     change = nopic_need_relax (fragp->fr_symbol, 0);
18249 
18250   if (change)
18251     {
18252       fragp->fr_subtype |= RELAX_USE_SECOND;
18253       return -RELAX_FIRST (fragp->fr_subtype);
18254     }
18255   else
18256     return -RELAX_SECOND (fragp->fr_subtype);
18257 }
18258 
18259 /* This is called to see whether a reloc against a defined symbol
18260    should be converted into a reloc against a section.  */
18261 
18262 int
mips_fix_adjustable(fixS * fixp)18263 mips_fix_adjustable (fixS *fixp)
18264 {
18265   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18266       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18267     return 0;
18268 
18269   if (fixp->fx_addsy == NULL)
18270     return 1;
18271 
18272   /* Allow relocs used for EH tables.  */
18273   if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18274     return 1;
18275 
18276   /* If symbol SYM is in a mergeable section, relocations of the form
18277      SYM + 0 can usually be made section-relative.  The mergeable data
18278      is then identified by the section offset rather than by the symbol.
18279 
18280      However, if we're generating REL LO16 relocations, the offset is split
18281      between the LO16 and partnering high part relocation.  The linker will
18282      need to recalculate the complete offset in order to correctly identify
18283      the merge data.
18284 
18285      The linker has traditionally not looked for the partnering high part
18286      relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
18287      placed anywhere.  Rather than break backwards compatibility by changing
18288      this, it seems better not to force the issue, and instead keep the
18289      original symbol.  This will work with either linker behavior.  */
18290   if ((lo16_reloc_p (fixp->fx_r_type)
18291        || reloc_needs_lo_p (fixp->fx_r_type))
18292       && HAVE_IN_PLACE_ADDENDS
18293       && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
18294     return 0;
18295 
18296   /* There is no place to store an in-place offset for JALR relocations.  */
18297   if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
18298     return 0;
18299 
18300   /* Likewise an in-range offset of limited PC-relative relocations may
18301      overflow the in-place relocatable field if recalculated against the
18302      start address of the symbol's containing section.
18303 
18304      Also, PC relative relocations for MIPS R6 need to be symbol rather than
18305      section relative to allow linker relaxations to be performed later on.  */
18306   if (limited_pcrel_reloc_p (fixp->fx_r_type)
18307       && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
18308     return 0;
18309 
18310   /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18311      to a floating-point stub.  The same is true for non-R_MIPS16_26
18312      relocations against MIPS16 functions; in this case, the stub becomes
18313      the function's canonical address.
18314 
18315      Floating-point stubs are stored in unique .mips16.call.* or
18316      .mips16.fn.* sections.  If a stub T for function F is in section S,
18317      the first relocation in section S must be against F; this is how the
18318      linker determines the target function.  All relocations that might
18319      resolve to T must also be against F.  We therefore have the following
18320      restrictions, which are given in an intentionally-redundant way:
18321 
18322        1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18323 	  symbols.
18324 
18325        2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18326 	  if that stub might be used.
18327 
18328        3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18329 	  symbols.
18330 
18331        4. We cannot reduce a stub's relocations against MIPS16 symbols if
18332 	  that stub might be used.
18333 
18334      There is a further restriction:
18335 
18336        5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
18337 	  R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
18338 	  R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18339 	  R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18340 	  against MIPS16 or microMIPS symbols because we need to keep the
18341 	  MIPS16 or microMIPS symbol for the purpose of mode mismatch
18342 	  detection and JAL or BAL to JALX instruction conversion in the
18343 	  linker.
18344 
18345      For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
18346      against a MIPS16 symbol.  We deal with (5) by additionally leaving
18347      alone any jump and branch relocations against a microMIPS symbol.
18348 
18349      We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18350      relocation against some symbol R, no relocation against R may be
18351      reduced.  (Note that this deals with (2) as well as (1) because
18352      relocations against global symbols will never be reduced on ELF
18353      targets.)  This approach is a little simpler than trying to detect
18354      stub sections, and gives the "all or nothing" per-symbol consistency
18355      that we have for MIPS16 symbols.  */
18356   if (fixp->fx_subsy == NULL
18357       && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
18358 	  || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
18359 	      && (jmp_reloc_p (fixp->fx_r_type)
18360 		  || b_reloc_p (fixp->fx_r_type)))
18361 	  || *symbol_get_tc (fixp->fx_addsy)))
18362     return 0;
18363 
18364   return 1;
18365 }
18366 
18367 /* Translate internal representation of relocation info to BFD target
18368    format.  */
18369 
18370 arelent **
tc_gen_reloc(asection * section ATTRIBUTE_UNUSED,fixS * fixp)18371 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
18372 {
18373   static arelent *retval[4];
18374   arelent *reloc;
18375   bfd_reloc_code_real_type code;
18376 
18377   memset (retval, 0, sizeof(retval));
18378   reloc = retval[0] = XCNEW (arelent);
18379   reloc->sym_ptr_ptr = XNEW (asymbol *);
18380   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18381   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18382 
18383   if (fixp->fx_pcrel)
18384     {
18385       gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18386 		  || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
18387 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18388 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
18389 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18390 		  || fixp->fx_r_type == BFD_RELOC_32_PCREL
18391 		  || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18392 		  || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18393 		  || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18394 		  || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18395 		  || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18396 		  || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
18397 
18398       /* At this point, fx_addnumber is "symbol offset - pcrel address".
18399 	 Relocations want only the symbol offset.  */
18400       switch (fixp->fx_r_type)
18401 	{
18402 	case BFD_RELOC_MIPS_18_PCREL_S3:
18403 	  reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18404 	  break;
18405 	default:
18406 	  reloc->addend = fixp->fx_addnumber + reloc->address;
18407 	  break;
18408 	}
18409     }
18410   else if (HAVE_IN_PLACE_ADDENDS
18411 	   && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18412 	   && (read_compressed_insn (fixp->fx_frag->fr_literal
18413 				     + fixp->fx_where, 4) >> 26) == 0x3c)
18414     {
18415       /* Shift is 2, unusually, for microMIPS JALX.  Adjust the in-place
18416          addend accordingly.  */
18417       reloc->addend = fixp->fx_addnumber >> 1;
18418     }
18419   else
18420     reloc->addend = fixp->fx_addnumber;
18421 
18422   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18423      entry to be used in the relocation's section offset.  */
18424   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18425     {
18426       reloc->address = reloc->addend;
18427       reloc->addend = 0;
18428     }
18429 
18430   code = fixp->fx_r_type;
18431 
18432   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
18433   if (reloc->howto == NULL)
18434     {
18435       as_bad_where (fixp->fx_file, fixp->fx_line,
18436 		    _("cannot represent %s relocation in this object file"
18437 		      " format"),
18438 		    bfd_get_reloc_code_name (code));
18439       retval[0] = NULL;
18440     }
18441 
18442   return retval;
18443 }
18444 
18445 /* Relax a machine dependent frag.  This returns the amount by which
18446    the current size of the frag should change.  */
18447 
18448 int
mips_relax_frag(asection * sec,fragS * fragp,long stretch)18449 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
18450 {
18451   if (RELAX_BRANCH_P (fragp->fr_subtype))
18452     {
18453       offsetT old_var = fragp->fr_var;
18454 
18455       fragp->fr_var = relaxed_branch_length (fragp, sec, true);
18456 
18457       return fragp->fr_var - old_var;
18458     }
18459 
18460   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18461     {
18462       offsetT old_var = fragp->fr_var;
18463       offsetT new_var = 4;
18464 
18465       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18466 	new_var = relaxed_micromips_16bit_branch_length (fragp, sec, true);
18467       if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18468 	new_var = relaxed_micromips_32bit_branch_length (fragp, sec, true);
18469       fragp->fr_var = new_var;
18470 
18471       return new_var - old_var;
18472     }
18473 
18474   if (! RELAX_MIPS16_P (fragp->fr_subtype))
18475     return 0;
18476 
18477   if (!mips16_extended_frag (fragp, sec, stretch))
18478     {
18479       if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18480 	{
18481 	  fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18482 	  return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
18483 	}
18484       else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18485 	{
18486 	  fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18487 	  return -2;
18488 	}
18489       else
18490 	return 0;
18491     }
18492   else if (!mips16_macro_frag (fragp, sec, stretch))
18493     {
18494       if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18495 	{
18496 	  fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18497 	  fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18498 	  return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
18499 	}
18500       else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18501 	{
18502 	  fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18503 	  return 2;
18504 	}
18505       else
18506 	return 0;
18507     }
18508   else
18509     {
18510       if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18511 	return 0;
18512       else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18513 	{
18514 	  fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18515 	  fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18516 	  return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
18517 	}
18518       else
18519 	{
18520 	  fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18521 	  return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
18522 	}
18523     }
18524 
18525   return 0;
18526 }
18527 
18528 /* Convert a machine dependent frag.  */
18529 
18530 void
md_convert_frag(bfd * abfd ATTRIBUTE_UNUSED,segT asec,fragS * fragp)18531 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
18532 {
18533   if (RELAX_BRANCH_P (fragp->fr_subtype))
18534     {
18535       char *buf;
18536       unsigned long insn;
18537       fixS *fixp;
18538 
18539       buf = fragp->fr_literal + fragp->fr_fix;
18540       insn = read_insn (buf);
18541 
18542       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18543 	{
18544 	  /* We generate a fixup instead of applying it right now
18545 	     because, if there are linker relaxations, we're going to
18546 	     need the relocations.  */
18547 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18548 			  fragp->fr_symbol, fragp->fr_offset,
18549 			  true, BFD_RELOC_16_PCREL_S2);
18550 	  fixp->fx_file = fragp->fr_file;
18551 	  fixp->fx_line = fragp->fr_line;
18552 
18553 	  buf = write_insn (buf, insn);
18554 	}
18555       else
18556 	{
18557 	  int i;
18558 
18559 	  as_warn_where (fragp->fr_file, fragp->fr_line,
18560 			 _("relaxed out-of-range branch into a jump"));
18561 
18562 	  if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18563 	    goto uncond;
18564 
18565 	  if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18566 	    {
18567 	      /* Reverse the branch.  */
18568 	      switch ((insn >> 28) & 0xf)
18569 		{
18570 		case 4:
18571 		  if ((insn & 0xff000000) == 0x47000000
18572 		      || (insn & 0xff600000) == 0x45600000)
18573 		    {
18574 		      /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18575 			 reversed by tweaking bit 23.  */
18576 		      insn ^= 0x00800000;
18577 		    }
18578 		  else
18579 		    {
18580 		      /* bc[0-3][tf]l? instructions can have the condition
18581 			 reversed by tweaking a single TF bit, and their
18582 			 opcodes all have 0x4???????.  */
18583 		      gas_assert ((insn & 0xf3e00000) == 0x41000000);
18584 		      insn ^= 0x00010000;
18585 		    }
18586 		  break;
18587 
18588 		case 0:
18589 		  /* bltz	0x04000000	bgez	0x04010000
18590 		     bltzal	0x04100000	bgezal	0x04110000  */
18591 		  gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18592 		  insn ^= 0x00010000;
18593 		  break;
18594 
18595 		case 1:
18596 		  /* beq	0x10000000	bne	0x14000000
18597 		     blez	0x18000000	bgtz	0x1c000000  */
18598 		  insn ^= 0x04000000;
18599 		  break;
18600 
18601 		default:
18602 		  abort ();
18603 		}
18604 	    }
18605 
18606 	  if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18607 	    {
18608 	      /* Clear the and-link bit.  */
18609 	      gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18610 
18611 	      /* bltzal		0x04100000	bgezal	0x04110000
18612 		 bltzall	0x04120000	bgezall	0x04130000  */
18613 	      insn &= ~0x00100000;
18614 	    }
18615 
18616 	  /* Branch over the branch (if the branch was likely) or the
18617 	     full jump (not likely case).  Compute the offset from the
18618 	     current instruction to branch to.  */
18619 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18620 	    i = 16;
18621 	  else
18622 	    {
18623 	      /* How many bytes in instructions we've already emitted?  */
18624 	      i = buf - fragp->fr_literal - fragp->fr_fix;
18625 	      /* How many bytes in instructions from here to the end?  */
18626 	      i = fragp->fr_var - i;
18627 	    }
18628 	  /* Convert to instruction count.  */
18629 	  i >>= 2;
18630 	  /* Branch counts from the next instruction.  */
18631 	  i--;
18632 	  insn |= i;
18633 	  /* Branch over the jump.  */
18634 	  buf = write_insn (buf, insn);
18635 
18636 	  /* nop */
18637 	  buf = write_insn (buf, 0);
18638 
18639 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18640 	    {
18641 	      /* beql $0, $0, 2f */
18642 	      insn = 0x50000000;
18643 	      /* Compute the PC offset from the current instruction to
18644 		 the end of the variable frag.  */
18645 	      /* How many bytes in instructions we've already emitted?  */
18646 	      i = buf - fragp->fr_literal - fragp->fr_fix;
18647 	      /* How many bytes in instructions from here to the end?  */
18648 	      i = fragp->fr_var - i;
18649 	      /* Convert to instruction count.  */
18650 	      i >>= 2;
18651 	      /* Don't decrement i, because we want to branch over the
18652 		 delay slot.  */
18653 	      insn |= i;
18654 
18655 	      buf = write_insn (buf, insn);
18656 	      buf = write_insn (buf, 0);
18657 	    }
18658 
18659 	uncond:
18660 	  if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
18661 	    {
18662 	      /* j or jal.  */
18663 	      insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18664 		      ? 0x0c000000 : 0x08000000);
18665 
18666 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18667 			      fragp->fr_symbol, fragp->fr_offset,
18668 			      false, BFD_RELOC_MIPS_JMP);
18669 	      fixp->fx_file = fragp->fr_file;
18670 	      fixp->fx_line = fragp->fr_line;
18671 
18672 	      buf = write_insn (buf, insn);
18673 	    }
18674 	  else
18675 	    {
18676 	      unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18677 
18678 	      /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
18679 	      insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18680 	      insn |= at << OP_SH_RT;
18681 
18682 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18683 			      fragp->fr_symbol, fragp->fr_offset,
18684 			      false, BFD_RELOC_MIPS_GOT16);
18685 	      fixp->fx_file = fragp->fr_file;
18686 	      fixp->fx_line = fragp->fr_line;
18687 
18688 	      buf = write_insn (buf, insn);
18689 
18690 	      if (mips_opts.isa == ISA_MIPS1)
18691 		/* nop */
18692 		buf = write_insn (buf, 0);
18693 
18694 	      /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
18695 	      insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18696 	      insn |= at << OP_SH_RS | at << OP_SH_RT;
18697 
18698 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18699 			      fragp->fr_symbol, fragp->fr_offset,
18700 			      false, BFD_RELOC_LO16);
18701 	      fixp->fx_file = fragp->fr_file;
18702 	      fixp->fx_line = fragp->fr_line;
18703 
18704 	      buf = write_insn (buf, insn);
18705 
18706 	      /* j(al)r $at.  */
18707 	      if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18708 		insn = 0x0000f809;
18709 	      else
18710 		insn = 0x00000008;
18711 	      insn |= at << OP_SH_RS;
18712 
18713 	      buf = write_insn (buf, insn);
18714 	    }
18715 	}
18716 
18717       fragp->fr_fix += fragp->fr_var;
18718       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18719       return;
18720     }
18721 
18722   /* Relax microMIPS branches.  */
18723   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18724     {
18725       char *buf = fragp->fr_literal + fragp->fr_fix;
18726       bool compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18727       bool insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18728       bool nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18729       bool pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18730       bool al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18731       int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18732       bool short_ds;
18733       unsigned long insn;
18734       fixS *fixp;
18735 
18736       fragp->fr_fix += fragp->fr_var;
18737 
18738       /* Handle 16-bit branches that fit or are forced to fit.  */
18739       if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18740 	{
18741 	  /* We generate a fixup instead of applying it right now,
18742 	     because if there is linker relaxation, we're going to
18743 	     need the relocations.  */
18744 	  switch (type)
18745 	    {
18746 	    case 'D':
18747 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18748 			      fragp->fr_symbol, fragp->fr_offset,
18749 			      true, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18750 	      break;
18751 	    case 'E':
18752 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18753 			      fragp->fr_symbol, fragp->fr_offset,
18754 			      true, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18755 	      break;
18756 	    default:
18757 	      abort ();
18758 	    }
18759 
18760 	  fixp->fx_file = fragp->fr_file;
18761 	  fixp->fx_line = fragp->fr_line;
18762 
18763 	  /* These relocations can have an addend that won't fit in
18764 	     2 octets.  */
18765 	  fixp->fx_no_overflow = 1;
18766 
18767 	  return;
18768 	}
18769 
18770       /* Handle 32-bit branches that fit or are forced to fit.  */
18771       if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18772 	  || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18773 	{
18774 	  /* We generate a fixup instead of applying it right now,
18775 	     because if there is linker relaxation, we're going to
18776 	     need the relocations.  */
18777 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18778 			  fragp->fr_symbol, fragp->fr_offset,
18779 			  true, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18780 	  fixp->fx_file = fragp->fr_file;
18781 	  fixp->fx_line = fragp->fr_line;
18782 
18783 	  if (type == 0)
18784 	    {
18785 	      insn = read_compressed_insn (buf, 4);
18786 	      buf += 4;
18787 
18788 	      if (nods)
18789 		{
18790 		  /* Check the short-delay-slot bit.  */
18791 		  if (!al || (insn & 0x02000000) != 0)
18792 		    buf = write_compressed_insn (buf, 0x0c00, 2);
18793 		  else
18794 		    buf = write_compressed_insn (buf, 0x00000000, 4);
18795 		}
18796 
18797 	      gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18798 	      return;
18799 	    }
18800 	}
18801 
18802       /* Relax 16-bit branches to 32-bit branches.  */
18803       if (type != 0)
18804 	{
18805 	  insn = read_compressed_insn (buf, 2);
18806 
18807 	  if ((insn & 0xfc00) == 0xcc00)		/* b16  */
18808 	    insn = 0x94000000;				/* beq  */
18809 	  else if ((insn & 0xdc00) == 0x8c00)		/* beqz16/bnez16  */
18810 	    {
18811 	      unsigned long regno;
18812 
18813 	      regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18814 	      regno = micromips_to_32_reg_d_map [regno];
18815 	      insn = ((insn & 0x2000) << 16) | 0x94000000;	/* beq/bne  */
18816 	      insn |= regno << MICROMIPSOP_SH_RS;
18817 	    }
18818 	  else
18819 	    abort ();
18820 
18821 	  /* Nothing else to do, just write it out.  */
18822 	  if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18823 	      || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18824 	    {
18825 	      buf = write_compressed_insn (buf, insn, 4);
18826 	      if (nods)
18827 		buf = write_compressed_insn (buf, 0x0c00, 2);
18828 	      gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18829 	      return;
18830 	    }
18831 	}
18832       else
18833 	insn = read_compressed_insn (buf, 4);
18834 
18835       /* Relax 32-bit branches to a sequence of instructions.  */
18836       as_warn_where (fragp->fr_file, fragp->fr_line,
18837 		     _("relaxed out-of-range branch into a jump"));
18838 
18839       /* Set the short-delay-slot bit.  */
18840       short_ds = !al || (insn & 0x02000000) != 0;
18841 
18842       if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18843 	{
18844 	  symbolS *l;
18845 
18846 	  /* Reverse the branch.  */
18847 	  if ((insn & 0xfc000000) == 0x94000000			/* beq  */
18848 	      || (insn & 0xfc000000) == 0xb4000000)		/* bne  */
18849 	    insn ^= 0x20000000;
18850 	  else if ((insn & 0xffe00000) == 0x40000000		/* bltz  */
18851 		   || (insn & 0xffe00000) == 0x40400000		/* bgez  */
18852 		   || (insn & 0xffe00000) == 0x40800000		/* blez  */
18853 		   || (insn & 0xffe00000) == 0x40c00000		/* bgtz  */
18854 		   || (insn & 0xffe00000) == 0x40a00000		/* bnezc  */
18855 		   || (insn & 0xffe00000) == 0x40e00000		/* beqzc  */
18856 		   || (insn & 0xffe00000) == 0x40200000		/* bltzal  */
18857 		   || (insn & 0xffe00000) == 0x40600000		/* bgezal  */
18858 		   || (insn & 0xffe00000) == 0x42200000		/* bltzals  */
18859 		   || (insn & 0xffe00000) == 0x42600000)	/* bgezals  */
18860 	    insn ^= 0x00400000;
18861 	  else if ((insn & 0xffe30000) == 0x43800000		/* bc1f  */
18862 		   || (insn & 0xffe30000) == 0x43a00000		/* bc1t  */
18863 		   || (insn & 0xffe30000) == 0x42800000		/* bc2f  */
18864 		   || (insn & 0xffe30000) == 0x42a00000)	/* bc2t  */
18865 	    insn ^= 0x00200000;
18866 	  else if ((insn & 0xff000000) == 0x83000000		/* BZ.df
18867 								   BNZ.df  */
18868 		    || (insn & 0xff600000) == 0x81600000)	/* BZ.V
18869 								   BNZ.V */
18870 	    insn ^= 0x00800000;
18871 	  else
18872 	    abort ();
18873 
18874 	  if (al)
18875 	    {
18876 	      /* Clear the and-link and short-delay-slot bits.  */
18877 	      gas_assert ((insn & 0xfda00000) == 0x40200000);
18878 
18879 	      /* bltzal  0x40200000	bgezal  0x40600000  */
18880 	      /* bltzals 0x42200000	bgezals 0x42600000  */
18881 	      insn &= ~0x02200000;
18882 	    }
18883 
18884 	  /* Make a label at the end for use with the branch.  */
18885 	  l = symbol_new (micromips_label_name (), asec, fragp, fragp->fr_fix);
18886 	  micromips_label_inc ();
18887 	  S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18888 
18889 	  /* Refer to it.  */
18890 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, true,
18891 			  BFD_RELOC_MICROMIPS_16_PCREL_S1);
18892 	  fixp->fx_file = fragp->fr_file;
18893 	  fixp->fx_line = fragp->fr_line;
18894 
18895 	  /* Branch over the jump.  */
18896 	  buf = write_compressed_insn (buf, insn, 4);
18897 
18898 	  if (!compact)
18899 	    {
18900 	      /* nop  */
18901 	      if (insn32)
18902 		buf = write_compressed_insn (buf, 0x00000000, 4);
18903 	      else
18904 		buf = write_compressed_insn (buf, 0x0c00, 2);
18905 	    }
18906 	}
18907 
18908       if (!pic)
18909 	{
18910 	  unsigned long jal = (short_ds || nods
18911 			       ? 0x74000000 : 0xf4000000);	/* jal/s  */
18912 
18913 	  /* j/jal/jals <sym>  R_MICROMIPS_26_S1  */
18914 	  insn = al ? jal : 0xd4000000;
18915 
18916 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18917 			  fragp->fr_symbol, fragp->fr_offset,
18918 			  false, BFD_RELOC_MICROMIPS_JMP);
18919 	  fixp->fx_file = fragp->fr_file;
18920 	  fixp->fx_line = fragp->fr_line;
18921 
18922 	  buf = write_compressed_insn (buf, insn, 4);
18923 
18924 	  if (compact || nods)
18925 	    {
18926 	      /* nop  */
18927 	      if (insn32)
18928 		buf = write_compressed_insn (buf, 0x00000000, 4);
18929 	      else
18930 		buf = write_compressed_insn (buf, 0x0c00, 2);
18931 	    }
18932 	}
18933       else
18934 	{
18935 	  unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18936 
18937 	  /* lw/ld $at, <sym>($gp)  R_MICROMIPS_GOT16  */
18938 	  insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18939 	  insn |= at << MICROMIPSOP_SH_RT;
18940 
18941 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18942 			  fragp->fr_symbol, fragp->fr_offset,
18943 			  false, BFD_RELOC_MICROMIPS_GOT16);
18944 	  fixp->fx_file = fragp->fr_file;
18945 	  fixp->fx_line = fragp->fr_line;
18946 
18947 	  buf = write_compressed_insn (buf, insn, 4);
18948 
18949 	  /* d/addiu $at, $at, <sym>  R_MICROMIPS_LO16  */
18950 	  insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18951 	  insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18952 
18953 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18954 			  fragp->fr_symbol, fragp->fr_offset,
18955 			  false, BFD_RELOC_MICROMIPS_LO16);
18956 	  fixp->fx_file = fragp->fr_file;
18957 	  fixp->fx_line = fragp->fr_line;
18958 
18959 	  buf = write_compressed_insn (buf, insn, 4);
18960 
18961 	  if (insn32)
18962 	    {
18963 	      /* jr/jalr $at  */
18964 	      insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18965 	      insn |= at << MICROMIPSOP_SH_RS;
18966 
18967 	      buf = write_compressed_insn (buf, insn, 4);
18968 
18969 	      if (compact || nods)
18970 		/* nop  */
18971 		buf = write_compressed_insn (buf, 0x00000000, 4);
18972 	    }
18973 	  else
18974 	    {
18975 	      /* jr/jrc/jalr/jalrs $at  */
18976 	      unsigned long jalr = short_ds ? 0x45e0 : 0x45c0;	/* jalr/s  */
18977 	      unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c  */
18978 
18979 	      insn = al ? jalr : jr;
18980 	      insn |= at << MICROMIPSOP_SH_MJ;
18981 
18982 	      buf = write_compressed_insn (buf, insn, 2);
18983 	      if (al && nods)
18984 		{
18985 		  /* nop  */
18986 		  if (short_ds)
18987 		    buf = write_compressed_insn (buf, 0x0c00, 2);
18988 		  else
18989 		    buf = write_compressed_insn (buf, 0x00000000, 4);
18990 		}
18991 	    }
18992 	}
18993 
18994       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18995       return;
18996     }
18997 
18998   if (RELAX_MIPS16_P (fragp->fr_subtype))
18999     {
19000       int type;
19001       const struct mips_int_operand *operand;
19002       offsetT val;
19003       char *buf;
19004       unsigned int user_length;
19005       bool need_reloc;
19006       unsigned long insn;
19007       bool mac;
19008       bool ext;
19009       segT symsec;
19010 
19011       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
19012       operand = mips16_immed_operand (type, false);
19013 
19014       mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
19015       ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
19016       val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
19017 
19018       symsec = S_GET_SEGMENT (fragp->fr_symbol);
19019       need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, true)
19020 		    || (operand->root.type == OP_PCREL && !mac
19021 			? asec != symsec
19022 			: !bfd_is_abs_section (symsec)));
19023 
19024       if (operand->root.type == OP_PCREL && !mac)
19025 	{
19026 	  const struct mips_pcrel_operand *pcrel_op;
19027 
19028 	  pcrel_op = (const struct mips_pcrel_operand *) operand;
19029 
19030 	  if (pcrel_op->include_isa_bit && !need_reloc)
19031 	    {
19032 	      if (!mips_ignore_branch_isa
19033 		  && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
19034 		as_bad_where (fragp->fr_file, fragp->fr_line,
19035 			      _("branch to a symbol in another ISA mode"));
19036 	      else if ((fragp->fr_offset & 0x1) != 0)
19037 		as_bad_where (fragp->fr_file, fragp->fr_line,
19038 			      _("branch to misaligned address (0x%lx)"),
19039 			      (long) (resolve_symbol_value (fragp->fr_symbol)
19040 				      + (fragp->fr_offset & ~1)));
19041 	    }
19042 
19043 	  val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
19044 
19045 	  /* Make sure the section winds up with the alignment we have
19046              assumed.  */
19047 	  if (operand->shift > 0)
19048 	    record_alignment (asec, operand->shift);
19049 	}
19050 
19051       if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
19052 	  || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
19053 	{
19054 	  if (mac)
19055 	    as_warn_where (fragp->fr_file, fragp->fr_line,
19056 			   _("macro instruction expanded into multiple "
19057 			     "instructions in a branch delay slot"));
19058 	  else if (ext)
19059 	    as_warn_where (fragp->fr_file, fragp->fr_line,
19060 			   _("extended instruction in a branch delay slot"));
19061 	}
19062       else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
19063 	as_warn_where (fragp->fr_file, fragp->fr_line,
19064 		       _("macro instruction expanded into multiple "
19065 			 "instructions"));
19066 
19067       buf = fragp->fr_literal + fragp->fr_fix;
19068 
19069       insn = read_compressed_insn (buf, 2);
19070       if (ext)
19071 	insn |= MIPS16_EXTEND;
19072 
19073       if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
19074 	user_length = 4;
19075       else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
19076 	user_length = 2;
19077       else
19078 	user_length = 0;
19079 
19080       if (mac)
19081 	{
19082 	  unsigned long reg;
19083 	  unsigned long new;
19084 	  unsigned long op;
19085 	  bool e2;
19086 
19087 	  gas_assert (type == 'A' || type == 'B' || type == 'E');
19088 	  gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
19089 
19090 	  e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
19091 
19092 	  if (need_reloc)
19093 	    {
19094 	      fixS *fixp;
19095 
19096 	      gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
19097 
19098 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19099 			      fragp->fr_symbol, fragp->fr_offset,
19100 			      false, BFD_RELOC_MIPS16_HI16_S);
19101 	      fixp->fx_file = fragp->fr_file;
19102 	      fixp->fx_line = fragp->fr_line;
19103 
19104 	      fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
19105 			      fragp->fr_symbol, fragp->fr_offset,
19106 			      false, BFD_RELOC_MIPS16_LO16);
19107 	      fixp->fx_file = fragp->fr_file;
19108 	      fixp->fx_line = fragp->fr_line;
19109 
19110 	      val = 0;
19111 	    }
19112 
19113 	  switch (insn & 0xf800)
19114 	    {
19115 	    case 0x0800:					/* ADDIU */
19116 	      reg = (insn >> 8) & 0x7;
19117 	      op = 0xf0004800 | (reg << 8);
19118 	      break;
19119 	    case 0xb000:					/* LW */
19120 	      reg = (insn >> 8) & 0x7;
19121 	      op = 0xf0009800 | (reg << 8) | (reg << 5);
19122 	      break;
19123 	    case 0xf800:					/* I64 */
19124 	      reg = (insn >> 5) & 0x7;
19125 	      switch (insn & 0x0700)
19126 		{
19127 		case 0x0400:					/* LD */
19128 		  op = 0xf0003800 | (reg << 8) | (reg << 5);
19129 		  break;
19130 		case 0x0600:					/* DADDIU */
19131 		  op = 0xf000fd00 | (reg << 5);
19132 		  break;
19133 		default:
19134 		  abort ();
19135 		}
19136 	      break;
19137 	    default:
19138 	      abort ();
19139 	    }
19140 
19141 	  new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8);	/* LUI/LI */
19142 	  new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
19143 	  buf = write_compressed_insn (buf, new, 4);
19144 	  if (!e2)
19145 	    {
19146 	      new = 0xf4003000 | (reg << 8) | (reg << 5);	/* SLL */
19147 	      buf = write_compressed_insn (buf, new, 4);
19148 	    }
19149 	  op |= mips16_immed_extend (val, 16);
19150 	  buf = write_compressed_insn (buf, op, 4);
19151 
19152 	  fragp->fr_fix += e2 ? 8 : 12;
19153 	}
19154       else
19155 	{
19156 	  unsigned int length = ext ? 4 : 2;
19157 
19158 	  if (need_reloc)
19159 	    {
19160 	      bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
19161 	      fixS *fixp;
19162 
19163 	      switch (type)
19164 		{
19165 		case 'p':
19166 		case 'q':
19167 		  reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
19168 		  break;
19169 		default:
19170 		  break;
19171 		}
19172 	      if (mac || reloc == BFD_RELOC_NONE)
19173 		as_bad_where (fragp->fr_file, fragp->fr_line,
19174 			      _("unsupported relocation"));
19175 	      else if (ext)
19176 		{
19177 		  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19178 				  fragp->fr_symbol, fragp->fr_offset,
19179 				  true, reloc);
19180 		  fixp->fx_file = fragp->fr_file;
19181 		  fixp->fx_line = fragp->fr_line;
19182 		}
19183 	      else
19184 		as_bad_where (fragp->fr_file, fragp->fr_line,
19185 			      _("invalid unextended operand value"));
19186 	    }
19187 	  else
19188 	    mips16_immed (fragp->fr_file, fragp->fr_line, type,
19189 			  BFD_RELOC_UNUSED, val, user_length, &insn);
19190 
19191 	  gas_assert (mips16_opcode_length (insn) == length);
19192 	  write_compressed_insn (buf, insn, length);
19193 	  fragp->fr_fix += length;
19194 	}
19195     }
19196   else
19197     {
19198       relax_substateT subtype = fragp->fr_subtype;
19199       bool second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
19200       bool use_second = (subtype & RELAX_USE_SECOND) != 0;
19201       unsigned int first, second;
19202       fixS *fixp;
19203 
19204       first = RELAX_FIRST (subtype);
19205       second = RELAX_SECOND (subtype);
19206       fixp = (fixS *) fragp->fr_opcode;
19207 
19208       /* If the delay slot chosen does not match the size of the instruction,
19209          then emit a warning.  */
19210       if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
19211 	   || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
19212 	{
19213 	  relax_substateT s;
19214 	  const char *msg;
19215 
19216 	  s = subtype & (RELAX_DELAY_SLOT_16BIT
19217 			 | RELAX_DELAY_SLOT_SIZE_FIRST
19218 			 | RELAX_DELAY_SLOT_SIZE_SECOND);
19219 	  msg = macro_warning (s);
19220 	  if (msg != NULL)
19221 	    as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
19222 	  subtype &= ~s;
19223 	}
19224 
19225       /* Possibly emit a warning if we've chosen the longer option.  */
19226       if (use_second == second_longer)
19227 	{
19228 	  relax_substateT s;
19229 	  const char *msg;
19230 
19231 	  s = (subtype
19232 	       & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
19233 	  msg = macro_warning (s);
19234 	  if (msg != NULL)
19235 	    as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
19236 	  subtype &= ~s;
19237 	}
19238 
19239       /* Go through all the fixups for the first sequence.  Disable them
19240 	 (by marking them as done) if we're going to use the second
19241 	 sequence instead.  */
19242       while (fixp
19243 	     && fixp->fx_frag == fragp
19244 	     && fixp->fx_where + second < fragp->fr_fix)
19245 	{
19246 	  if (subtype & RELAX_USE_SECOND)
19247 	    fixp->fx_done = 1;
19248 	  fixp = fixp->fx_next;
19249 	}
19250 
19251       /* Go through the fixups for the second sequence.  Disable them if
19252 	 we're going to use the first sequence, otherwise adjust their
19253 	 addresses to account for the relaxation.  */
19254       while (fixp && fixp->fx_frag == fragp)
19255 	{
19256 	  if (subtype & RELAX_USE_SECOND)
19257 	    fixp->fx_where -= first;
19258 	  else
19259 	    fixp->fx_done = 1;
19260 	  fixp = fixp->fx_next;
19261 	}
19262 
19263       /* Now modify the frag contents.  */
19264       if (subtype & RELAX_USE_SECOND)
19265 	{
19266 	  char *start;
19267 
19268 	  start = fragp->fr_literal + fragp->fr_fix - first - second;
19269 	  memmove (start, start + first, second);
19270 	  fragp->fr_fix -= first;
19271 	}
19272       else
19273 	fragp->fr_fix -= second;
19274     }
19275 }
19276 
19277 /* This function is called after the relocs have been generated.
19278    We've been storing mips16 text labels as odd.  Here we convert them
19279    back to even for the convenience of the debugger.  */
19280 
19281 void
mips_frob_file_after_relocs(void)19282 mips_frob_file_after_relocs (void)
19283 {
19284   asymbol **syms;
19285   unsigned int count, i;
19286 
19287   syms = bfd_get_outsymbols (stdoutput);
19288   count = bfd_get_symcount (stdoutput);
19289   for (i = 0; i < count; i++, syms++)
19290     if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
19291 	&& ((*syms)->value & 1) != 0)
19292       {
19293 	(*syms)->value &= ~1;
19294 	/* If the symbol has an odd size, it was probably computed
19295 	   incorrectly, so adjust that as well.  */
19296 	if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
19297 	  ++elf_symbol (*syms)->internal_elf_sym.st_size;
19298       }
19299 }
19300 
19301 /* This function is called whenever a label is defined, including fake
19302    labels instantiated off the dot special symbol.  It is used when
19303    handling branch delays; if a branch has a label, we assume we cannot
19304    move it.  This also bumps the value of the symbol by 1 in compressed
19305    code.  */
19306 
19307 static void
mips_record_label(symbolS * sym)19308 mips_record_label (symbolS *sym)
19309 {
19310   segment_info_type *si = seg_info (now_seg);
19311   struct insn_label_list *l;
19312 
19313   if (free_insn_labels == NULL)
19314     l = XNEW (struct insn_label_list);
19315   else
19316     {
19317       l = free_insn_labels;
19318       free_insn_labels = l->next;
19319     }
19320 
19321   l->label = sym;
19322   l->next = si->label_list;
19323   si->label_list = l;
19324 }
19325 
19326 /* This function is called as tc_frob_label() whenever a label is defined
19327    and adds a DWARF-2 record we only want for true labels.  */
19328 
19329 void
mips_define_label(symbolS * sym)19330 mips_define_label (symbolS *sym)
19331 {
19332   mips_record_label (sym);
19333   dwarf2_emit_label (sym);
19334 }
19335 
19336 /* This function is called by tc_new_dot_label whenever a new dot symbol
19337    is defined.  */
19338 
19339 void
mips_add_dot_label(symbolS * sym)19340 mips_add_dot_label (symbolS *sym)
19341 {
19342   mips_record_label (sym);
19343   if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19344     mips_compressed_mark_label (sym);
19345 }
19346 
19347 /* Converting ASE flags from internal to .MIPS.abiflags values.  */
19348 static unsigned int
mips_convert_ase_flags(int ase)19349 mips_convert_ase_flags (int ase)
19350 {
19351   unsigned int ext_ases = 0;
19352 
19353   if (ase & ASE_DSP)
19354     ext_ases |= AFL_ASE_DSP;
19355   if (ase & ASE_DSPR2)
19356     ext_ases |= AFL_ASE_DSPR2;
19357   if (ase & ASE_DSPR3)
19358     ext_ases |= AFL_ASE_DSPR3;
19359   if (ase & ASE_EVA)
19360     ext_ases |= AFL_ASE_EVA;
19361   if (ase & ASE_MCU)
19362     ext_ases |= AFL_ASE_MCU;
19363   if (ase & ASE_MDMX)
19364     ext_ases |= AFL_ASE_MDMX;
19365   if (ase & ASE_MIPS3D)
19366     ext_ases |= AFL_ASE_MIPS3D;
19367   if (ase & ASE_MT)
19368     ext_ases |= AFL_ASE_MT;
19369   if (ase & ASE_SMARTMIPS)
19370     ext_ases |= AFL_ASE_SMARTMIPS;
19371   if (ase & ASE_VIRT)
19372     ext_ases |= AFL_ASE_VIRT;
19373   if (ase & ASE_MSA)
19374     ext_ases |= AFL_ASE_MSA;
19375   if (ase & ASE_XPA)
19376     ext_ases |= AFL_ASE_XPA;
19377   if (ase & ASE_MIPS16E2)
19378     ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
19379   if (ase & ASE_CRC)
19380     ext_ases |= AFL_ASE_CRC;
19381   if (ase & ASE_GINV)
19382     ext_ases |= AFL_ASE_GINV;
19383   if (ase & ASE_LOONGSON_MMI)
19384     ext_ases |= AFL_ASE_LOONGSON_MMI;
19385   if (ase & ASE_LOONGSON_CAM)
19386     ext_ases |= AFL_ASE_LOONGSON_CAM;
19387   if (ase & ASE_LOONGSON_EXT)
19388     ext_ases |= AFL_ASE_LOONGSON_EXT;
19389   if (ase & ASE_LOONGSON_EXT2)
19390     ext_ases |= AFL_ASE_LOONGSON_EXT2;
19391 
19392   return ext_ases;
19393 }
19394 /* Some special processing for a MIPS ELF file.  */
19395 
19396 void
mips_elf_final_processing(void)19397 mips_elf_final_processing (void)
19398 {
19399   int fpabi;
19400   Elf_Internal_ABIFlags_v0 flags;
19401 
19402   flags.version = 0;
19403   flags.isa_rev = 0;
19404   switch (file_mips_opts.isa)
19405     {
19406     case INSN_ISA1:
19407       flags.isa_level = 1;
19408       break;
19409     case INSN_ISA2:
19410       flags.isa_level = 2;
19411       break;
19412     case INSN_ISA3:
19413       flags.isa_level = 3;
19414       break;
19415     case INSN_ISA4:
19416       flags.isa_level = 4;
19417       break;
19418     case INSN_ISA5:
19419       flags.isa_level = 5;
19420       break;
19421     case INSN_ISA32:
19422       flags.isa_level = 32;
19423       flags.isa_rev = 1;
19424       break;
19425     case INSN_ISA32R2:
19426       flags.isa_level = 32;
19427       flags.isa_rev = 2;
19428       break;
19429     case INSN_ISA32R3:
19430       flags.isa_level = 32;
19431       flags.isa_rev = 3;
19432       break;
19433     case INSN_ISA32R5:
19434       flags.isa_level = 32;
19435       flags.isa_rev = 5;
19436       break;
19437     case INSN_ISA32R6:
19438       flags.isa_level = 32;
19439       flags.isa_rev = 6;
19440       break;
19441     case INSN_ISA64:
19442       flags.isa_level = 64;
19443       flags.isa_rev = 1;
19444       break;
19445     case INSN_ISA64R2:
19446       flags.isa_level = 64;
19447       flags.isa_rev = 2;
19448       break;
19449     case INSN_ISA64R3:
19450       flags.isa_level = 64;
19451       flags.isa_rev = 3;
19452       break;
19453     case INSN_ISA64R5:
19454       flags.isa_level = 64;
19455       flags.isa_rev = 5;
19456       break;
19457     case INSN_ISA64R6:
19458       flags.isa_level = 64;
19459       flags.isa_rev = 6;
19460       break;
19461     }
19462 
19463   flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19464   flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19465 		    : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19466 		    : (file_mips_opts.fp == 64) ? AFL_REG_64
19467 		    : AFL_REG_32;
19468   flags.cpr2_size = AFL_REG_NONE;
19469   flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19470                                            Tag_GNU_MIPS_ABI_FP);
19471   flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19472   flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19473   if (file_ase_mips16)
19474     flags.ases |= AFL_ASE_MIPS16;
19475   if (file_ase_micromips)
19476     flags.ases |= AFL_ASE_MICROMIPS;
19477   flags.flags1 = 0;
19478   if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19479        || file_mips_opts.fp == 64)
19480       && file_mips_opts.oddspreg)
19481     flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19482   flags.flags2 = 0;
19483 
19484   bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19485 				     ((Elf_External_ABIFlags_v0 *)
19486 				     mips_flags_frag));
19487 
19488   /* Write out the register information.  */
19489   if (mips_abi != N64_ABI)
19490     {
19491       Elf32_RegInfo s;
19492 
19493       s.ri_gprmask = mips_gprmask;
19494       s.ri_cprmask[0] = mips_cprmask[0];
19495       s.ri_cprmask[1] = mips_cprmask[1];
19496       s.ri_cprmask[2] = mips_cprmask[2];
19497       s.ri_cprmask[3] = mips_cprmask[3];
19498       /* The gp_value field is set by the MIPS ELF backend.  */
19499 
19500       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19501 				       ((Elf32_External_RegInfo *)
19502 					mips_regmask_frag));
19503     }
19504   else
19505     {
19506       Elf64_Internal_RegInfo s;
19507 
19508       s.ri_gprmask = mips_gprmask;
19509       s.ri_pad = 0;
19510       s.ri_cprmask[0] = mips_cprmask[0];
19511       s.ri_cprmask[1] = mips_cprmask[1];
19512       s.ri_cprmask[2] = mips_cprmask[2];
19513       s.ri_cprmask[3] = mips_cprmask[3];
19514       /* The gp_value field is set by the MIPS ELF backend.  */
19515 
19516       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19517 				       ((Elf64_External_RegInfo *)
19518 					mips_regmask_frag));
19519     }
19520 
19521   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
19522      sort of BFD interface for this.  */
19523   if (mips_any_noreorder)
19524     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19525   if (mips_pic != NO_PIC)
19526     {
19527       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
19528       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19529     }
19530   if (mips_abicalls)
19531     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19532 
19533   /* Set MIPS ELF flags for ASEs.  Note that not all ASEs have flags
19534      defined at present; this might need to change in future.  */
19535   if (file_ase_mips16)
19536     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
19537   if (file_ase_micromips)
19538     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
19539   if (file_mips_opts.ase & ASE_MDMX)
19540     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
19541 
19542   /* Set the MIPS ELF ABI flags.  */
19543   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
19544     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
19545   else if (mips_abi == O64_ABI)
19546     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
19547   else if (mips_abi == EABI_ABI)
19548     {
19549       if (file_mips_opts.gp == 64)
19550 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19551       else
19552 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19553     }
19554 
19555   /* Nothing to do for N32_ABI or N64_ABI.  */
19556 
19557   if (mips_32bitmode)
19558     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
19559 
19560   if (mips_nan2008 == 1)
19561     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19562 
19563   /* 32 bit code with 64 bit FP registers.  */
19564   fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19565 				    Tag_GNU_MIPS_ABI_FP);
19566   if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
19567     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
19568 }
19569 
19570 typedef struct proc {
19571   symbolS *func_sym;
19572   symbolS *func_end_sym;
19573   unsigned long reg_mask;
19574   unsigned long reg_offset;
19575   unsigned long fpreg_mask;
19576   unsigned long fpreg_offset;
19577   unsigned long frame_offset;
19578   unsigned long frame_reg;
19579   unsigned long pc_reg;
19580 } procS;
19581 
19582 static procS cur_proc;
19583 static procS *cur_proc_ptr;
19584 static int numprocs;
19585 
19586 /* Implement NOP_OPCODE.  We encode a MIPS16 nop as "1", a microMIPS nop
19587    as "2", and a normal nop as "0".  */
19588 
19589 #define NOP_OPCODE_MIPS		0
19590 #define NOP_OPCODE_MIPS16	1
19591 #define NOP_OPCODE_MICROMIPS	2
19592 
19593 char
mips_nop_opcode(void)19594 mips_nop_opcode (void)
19595 {
19596   if (seg_info (now_seg)->tc_segment_info_data.micromips)
19597     return NOP_OPCODE_MICROMIPS;
19598   else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19599     return NOP_OPCODE_MIPS16;
19600   else
19601     return NOP_OPCODE_MIPS;
19602 }
19603 
19604 /* Fill in an rs_align_code fragment.  Unlike elsewhere we want to use
19605    32-bit microMIPS NOPs here (if applicable).  */
19606 
19607 void
mips_handle_align(fragS * fragp)19608 mips_handle_align (fragS *fragp)
19609 {
19610   char nop_opcode;
19611   char *p;
19612   int bytes, size, excess;
19613   valueT opcode;
19614 
19615   if (fragp->fr_type != rs_align_code)
19616     return;
19617 
19618   p = fragp->fr_literal + fragp->fr_fix;
19619   nop_opcode = *p;
19620   switch (nop_opcode)
19621     {
19622     case NOP_OPCODE_MICROMIPS:
19623       opcode = micromips_nop32_insn.insn_opcode;
19624       size = 4;
19625       break;
19626     case NOP_OPCODE_MIPS16:
19627       opcode = mips16_nop_insn.insn_opcode;
19628       size = 2;
19629       break;
19630     case NOP_OPCODE_MIPS:
19631     default:
19632       opcode = nop_insn.insn_opcode;
19633       size = 4;
19634       break;
19635     }
19636 
19637   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19638   excess = bytes % size;
19639 
19640   /* Handle the leading part if we're not inserting a whole number of
19641      instructions, and make it the end of the fixed part of the frag.
19642      Try to fit in a short microMIPS NOP if applicable and possible,
19643      and use zeroes otherwise.  */
19644   gas_assert (excess < 4);
19645   fragp->fr_fix += excess;
19646   switch (excess)
19647     {
19648     case 3:
19649       *p++ = '\0';
19650       /* Fall through.  */
19651     case 2:
19652       if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
19653 	{
19654 	  p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
19655 	  break;
19656 	}
19657       *p++ = '\0';
19658       /* Fall through.  */
19659     case 1:
19660       *p++ = '\0';
19661       /* Fall through.  */
19662     case 0:
19663       break;
19664     }
19665 
19666   md_number_to_chars (p, opcode, size);
19667   fragp->fr_var = size;
19668 }
19669 
19670 static long
get_number(void)19671 get_number (void)
19672 {
19673   int negative = 0;
19674   long val = 0;
19675 
19676   if (*input_line_pointer == '-')
19677     {
19678       ++input_line_pointer;
19679       negative = 1;
19680     }
19681   if (!ISDIGIT (*input_line_pointer))
19682     as_bad (_("expected simple number"));
19683   if (input_line_pointer[0] == '0')
19684     {
19685       if (input_line_pointer[1] == 'x')
19686 	{
19687 	  input_line_pointer += 2;
19688 	  while (ISXDIGIT (*input_line_pointer))
19689 	    {
19690 	      val <<= 4;
19691 	      val |= hex_value (*input_line_pointer++);
19692 	    }
19693 	  return negative ? -val : val;
19694 	}
19695       else
19696 	{
19697 	  ++input_line_pointer;
19698 	  while (ISDIGIT (*input_line_pointer))
19699 	    {
19700 	      val <<= 3;
19701 	      val |= *input_line_pointer++ - '0';
19702 	    }
19703 	  return negative ? -val : val;
19704 	}
19705     }
19706   if (!ISDIGIT (*input_line_pointer))
19707     {
19708       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19709 	      *input_line_pointer, *input_line_pointer);
19710       as_warn (_("invalid number"));
19711       return -1;
19712     }
19713   while (ISDIGIT (*input_line_pointer))
19714     {
19715       val *= 10;
19716       val += *input_line_pointer++ - '0';
19717     }
19718   return negative ? -val : val;
19719 }
19720 
19721 /* The .file directive; just like the usual .file directive, but there
19722    is an initial number which is the ECOFF file index.  In the non-ECOFF
19723    case .file implies DWARF-2.  */
19724 
19725 static void
s_mips_file(int x ATTRIBUTE_UNUSED)19726 s_mips_file (int x ATTRIBUTE_UNUSED)
19727 {
19728   static int first_file_directive = 0;
19729 
19730   if (ECOFF_DEBUGGING)
19731     {
19732       get_number ();
19733       s_app_file (0);
19734     }
19735   else
19736     {
19737       char *filename;
19738 
19739       filename = dwarf2_directive_filename ();
19740 
19741       /* Versions of GCC up to 3.1 start files with a ".file"
19742 	 directive even for stabs output.  Make sure that this
19743 	 ".file" is handled.  Note that you need a version of GCC
19744          after 3.1 in order to support DWARF-2 on MIPS.  */
19745       if (filename != NULL && ! first_file_directive)
19746 	{
19747 	  (void) new_logical_line (filename, -1);
19748 	  s_app_file_string (filename, 0);
19749 	}
19750       first_file_directive = 1;
19751     }
19752 }
19753 
19754 /* The .loc directive, implying DWARF-2.  */
19755 
19756 static void
s_mips_loc(int x ATTRIBUTE_UNUSED)19757 s_mips_loc (int x ATTRIBUTE_UNUSED)
19758 {
19759   if (!ECOFF_DEBUGGING)
19760     dwarf2_directive_loc (0);
19761 }
19762 
19763 /* The .end directive.  */
19764 
19765 static void
s_mips_end(int x ATTRIBUTE_UNUSED)19766 s_mips_end (int x ATTRIBUTE_UNUSED)
19767 {
19768   symbolS *p;
19769 
19770   /* Following functions need their own .frame and .cprestore directives.  */
19771   mips_frame_reg_valid = 0;
19772   mips_cprestore_valid = 0;
19773 
19774   if (!is_end_of_line[(unsigned char) *input_line_pointer])
19775     {
19776       p = get_symbol ();
19777       demand_empty_rest_of_line ();
19778     }
19779   else
19780     p = NULL;
19781 
19782   if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
19783     as_warn (_(".end not in text section"));
19784 
19785   if (!cur_proc_ptr)
19786     {
19787       as_warn (_(".end directive without a preceding .ent directive"));
19788       demand_empty_rest_of_line ();
19789       return;
19790     }
19791 
19792   if (p != NULL)
19793     {
19794       gas_assert (S_GET_NAME (p));
19795       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
19796 	as_warn (_(".end symbol does not match .ent symbol"));
19797 
19798       if (debug_type == DEBUG_STABS)
19799 	stabs_generate_asm_endfunc (S_GET_NAME (p),
19800 				    S_GET_NAME (p));
19801     }
19802   else
19803     as_warn (_(".end directive missing or unknown symbol"));
19804 
19805   /* Create an expression to calculate the size of the function.  */
19806   if (p && cur_proc_ptr)
19807     {
19808       OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19809       expressionS *exp = XNEW (expressionS);
19810 
19811       obj->size = exp;
19812       exp->X_op = O_subtract;
19813       exp->X_add_symbol = symbol_temp_new_now ();
19814       exp->X_op_symbol = p;
19815       exp->X_add_number = 0;
19816 
19817       cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19818     }
19819 
19820 #ifdef md_flush_pending_output
19821   md_flush_pending_output ();
19822 #endif
19823 
19824   /* Generate a .pdr section.  */
19825   if (!ECOFF_DEBUGGING && mips_flag_pdr)
19826     {
19827       segT saved_seg = now_seg;
19828       subsegT saved_subseg = now_subseg;
19829       expressionS exp;
19830       char *fragp;
19831 
19832       gas_assert (pdr_seg);
19833       subseg_set (pdr_seg, 0);
19834 
19835       /* Write the symbol.  */
19836       exp.X_op = O_symbol;
19837       exp.X_add_symbol = p;
19838       exp.X_add_number = 0;
19839       emit_expr (&exp, 4);
19840 
19841       fragp = frag_more (7 * 4);
19842 
19843       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19844       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19845       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19846       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19847       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19848       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19849       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19850 
19851       subseg_set (saved_seg, saved_subseg);
19852     }
19853 
19854   cur_proc_ptr = NULL;
19855 }
19856 
19857 /* The .aent and .ent directives.  */
19858 
19859 static void
s_mips_ent(int aent)19860 s_mips_ent (int aent)
19861 {
19862   symbolS *symbolP;
19863 
19864   symbolP = get_symbol ();
19865   if (*input_line_pointer == ',')
19866     ++input_line_pointer;
19867   SKIP_WHITESPACE ();
19868   if (ISDIGIT (*input_line_pointer)
19869       || *input_line_pointer == '-')
19870     get_number ();
19871 
19872   if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
19873     as_warn (_(".ent or .aent not in text section"));
19874 
19875   if (!aent && cur_proc_ptr)
19876     as_warn (_("missing .end"));
19877 
19878   if (!aent)
19879     {
19880       /* This function needs its own .frame and .cprestore directives.  */
19881       mips_frame_reg_valid = 0;
19882       mips_cprestore_valid = 0;
19883 
19884       cur_proc_ptr = &cur_proc;
19885       memset (cur_proc_ptr, '\0', sizeof (procS));
19886 
19887       cur_proc_ptr->func_sym = symbolP;
19888 
19889       ++numprocs;
19890 
19891       if (debug_type == DEBUG_STABS)
19892         stabs_generate_asm_func (S_GET_NAME (symbolP),
19893 				 S_GET_NAME (symbolP));
19894     }
19895 
19896   symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19897 
19898   demand_empty_rest_of_line ();
19899 }
19900 
19901 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19902    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19903    s_mips_frame is used so that we can set the PDR information correctly.
19904    We can't use the ecoff routines because they make reference to the ecoff
19905    symbol table (in the mdebug section).  */
19906 
19907 static void
s_mips_frame(int ignore ATTRIBUTE_UNUSED)19908 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19909 {
19910   if (ECOFF_DEBUGGING)
19911     s_ignore (ignore);
19912   else
19913     {
19914       long val;
19915 
19916       if (cur_proc_ptr == (procS *) NULL)
19917 	{
19918 	  as_warn (_(".frame outside of .ent"));
19919 	  demand_empty_rest_of_line ();
19920 	  return;
19921 	}
19922 
19923       cur_proc_ptr->frame_reg = tc_get_register (1);
19924 
19925       SKIP_WHITESPACE ();
19926       if (*input_line_pointer++ != ','
19927 	  || get_absolute_expression_and_terminator (&val) != ',')
19928 	{
19929 	  as_warn (_("bad .frame directive"));
19930 	  --input_line_pointer;
19931 	  demand_empty_rest_of_line ();
19932 	  return;
19933 	}
19934 
19935       cur_proc_ptr->frame_offset = val;
19936       cur_proc_ptr->pc_reg = tc_get_register (0);
19937 
19938       demand_empty_rest_of_line ();
19939     }
19940 }
19941 
19942 /* The .fmask and .mask directives. If the mdebug section is present
19943    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19944    embedded targets, s_mips_mask is used so that we can set the PDR
19945    information correctly. We can't use the ecoff routines because they
19946    make reference to the ecoff symbol table (in the mdebug section).  */
19947 
19948 static void
s_mips_mask(int reg_type)19949 s_mips_mask (int reg_type)
19950 {
19951   if (ECOFF_DEBUGGING)
19952     s_ignore (reg_type);
19953   else
19954     {
19955       long mask, off;
19956 
19957       if (cur_proc_ptr == (procS *) NULL)
19958 	{
19959 	  as_warn (_(".mask/.fmask outside of .ent"));
19960 	  demand_empty_rest_of_line ();
19961 	  return;
19962 	}
19963 
19964       if (get_absolute_expression_and_terminator (&mask) != ',')
19965 	{
19966 	  as_warn (_("bad .mask/.fmask directive"));
19967 	  --input_line_pointer;
19968 	  demand_empty_rest_of_line ();
19969 	  return;
19970 	}
19971 
19972       off = get_absolute_expression ();
19973 
19974       if (reg_type == 'F')
19975 	{
19976 	  cur_proc_ptr->fpreg_mask = mask;
19977 	  cur_proc_ptr->fpreg_offset = off;
19978 	}
19979       else
19980 	{
19981 	  cur_proc_ptr->reg_mask = mask;
19982 	  cur_proc_ptr->reg_offset = off;
19983 	}
19984 
19985       demand_empty_rest_of_line ();
19986     }
19987 }
19988 
19989 /* A table describing all the processors gas knows about.  Names are
19990    matched in the order listed.
19991 
19992    To ease comparison, please keep this table in the same order as
19993    gcc's mips_cpu_info_table[].  */
19994 static const struct mips_cpu_info mips_cpu_info_table[] =
19995 {
19996   /* Entries for generic ISAs.  */
19997   { "mips1",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS1,    CPU_R3000 },
19998   { "mips2",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS2,    CPU_R6000 },
19999   { "mips3",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS3,    CPU_R4000 },
20000   { "mips4",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS4,    CPU_R8000 },
20001   { "mips5",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS5,    CPU_MIPS5 },
20002   { "mips32",         MIPS_CPU_IS_ISA, 0,	ISA_MIPS32,   CPU_MIPS32 },
20003   { "mips32r2",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R2, CPU_MIPS32R2 },
20004   { "mips32r3",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R3, CPU_MIPS32R3 },
20005   { "mips32r5",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R5, CPU_MIPS32R5 },
20006   { "mips32r6",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R6, CPU_MIPS32R6 },
20007   { "mips64",         MIPS_CPU_IS_ISA, 0,	ISA_MIPS64,   CPU_MIPS64 },
20008   { "mips64r2",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R2, CPU_MIPS64R2 },
20009   { "mips64r3",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R3, CPU_MIPS64R3 },
20010   { "mips64r5",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R5, CPU_MIPS64R5 },
20011   { "mips64r6",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R6, CPU_MIPS64R6 },
20012 
20013   /* MIPS I */
20014   { "r3000",          0, 0,			ISA_MIPS1,    CPU_R3000 },
20015   { "r2000",          0, 0,			ISA_MIPS1,    CPU_R3000 },
20016   { "r3900",          0, 0,			ISA_MIPS1,    CPU_R3900 },
20017 
20018   /* MIPS II */
20019   { "r6000",          0, 0,			ISA_MIPS2,    CPU_R6000 },
20020 
20021   /* MIPS III */
20022   { "r4000",          0, 0,			ISA_MIPS3,    CPU_R4000 },
20023   { "r4010",          0, 0,			ISA_MIPS2,    CPU_R4010 },
20024   { "vr4100",         0, 0,			ISA_MIPS3,    CPU_VR4100 },
20025   { "vr4111",         0, 0,			ISA_MIPS3,    CPU_R4111 },
20026   { "vr4120",         0, 0,			ISA_MIPS3,    CPU_VR4120 },
20027   { "vr4130",         0, 0,			ISA_MIPS3,    CPU_VR4120 },
20028   { "vr4181",         0, 0,			ISA_MIPS3,    CPU_R4111 },
20029   { "vr4300",         0, 0,			ISA_MIPS3,    CPU_R4300 },
20030   { "r4400",          0, 0,			ISA_MIPS3,    CPU_R4400 },
20031   { "r4600",          0, 0,			ISA_MIPS3,    CPU_R4600 },
20032   { "orion",          0, 0,			ISA_MIPS3,    CPU_R4600 },
20033   { "r4650",          0, 0,			ISA_MIPS3,    CPU_R4650 },
20034   { "r5900",          0, 0,			ISA_MIPS3,    CPU_R5900 },
20035   /* ST Microelectronics Loongson 2E and 2F cores.  */
20036   { "loongson2e",     0, 0,			ISA_MIPS3,    CPU_LOONGSON_2E },
20037   { "loongson2f",     0, ASE_LOONGSON_MMI,	ISA_MIPS3,    CPU_LOONGSON_2F },
20038 
20039   /* MIPS IV */
20040   { "r8000",          0, 0,			ISA_MIPS4,    CPU_R8000 },
20041   { "r10000",         0, 0,			ISA_MIPS4,    CPU_R10000 },
20042   { "r12000",         0, 0,			ISA_MIPS4,    CPU_R12000 },
20043   { "r14000",         0, 0,			ISA_MIPS4,    CPU_R14000 },
20044   { "r16000",         0, 0,			ISA_MIPS4,    CPU_R16000 },
20045   { "vr5000",         0, 0,			ISA_MIPS4,    CPU_R5000 },
20046   { "vr5400",         0, 0,			ISA_MIPS4,    CPU_VR5400 },
20047   { "vr5500",         0, 0,			ISA_MIPS4,    CPU_VR5500 },
20048   { "rm5200",         0, 0,			ISA_MIPS4,    CPU_R5000 },
20049   { "rm5230",         0, 0,			ISA_MIPS4,    CPU_R5000 },
20050   { "rm5231",         0, 0,			ISA_MIPS4,    CPU_R5000 },
20051   { "rm5261",         0, 0,			ISA_MIPS4,    CPU_R5000 },
20052   { "rm5721",         0, 0,			ISA_MIPS4,    CPU_R5000 },
20053   { "rm7000",         0, 0,			ISA_MIPS4,    CPU_RM7000 },
20054   { "rm9000",         0, 0,			ISA_MIPS4,    CPU_RM9000 },
20055 
20056   /* MIPS 32 */
20057   { "4kc",            0, 0,			ISA_MIPS32,   CPU_MIPS32 },
20058   { "4km",            0, 0,			ISA_MIPS32,   CPU_MIPS32 },
20059   { "4kp",            0, 0,			ISA_MIPS32,   CPU_MIPS32 },
20060   { "4ksc",           0, ASE_SMARTMIPS,		ISA_MIPS32,   CPU_MIPS32 },
20061 
20062   /* MIPS 32 Release 2 */
20063   { "4kec",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20064   { "4kem",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20065   { "4kep",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20066   { "4ksd",           0, ASE_SMARTMIPS,		ISA_MIPS32R2, CPU_MIPS32R2 },
20067   { "m4k",            0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20068   { "m4kp",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20069   { "m14k",           0, ASE_MCU,		ISA_MIPS32R2, CPU_MIPS32R2 },
20070   { "m14kc",          0, ASE_MCU,		ISA_MIPS32R2, CPU_MIPS32R2 },
20071   { "m14ke",          0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20072 						ISA_MIPS32R2, CPU_MIPS32R2 },
20073   { "m14kec",         0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20074 						ISA_MIPS32R2, CPU_MIPS32R2 },
20075   { "24kc",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20076   { "24kf2_1",        0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20077   { "24kf",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20078   { "24kf1_1",        0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20079   /* Deprecated forms of the above.  */
20080   { "24kfx",          0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20081   { "24kx",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20082   /* 24KE is a 24K with DSP ASE, other ASEs are optional.  */
20083   { "24kec",          0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
20084   { "24kef2_1",       0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
20085   { "24kef",          0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
20086   { "24kef1_1",       0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
20087   /* Deprecated forms of the above.  */
20088   { "24kefx",         0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
20089   { "24kex",          0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
20090   /* 34K is a 24K with DSP and MT ASE, other ASEs are optional.  */
20091   { "34kc",           0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20092   { "34kf2_1",        0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20093   { "34kf",           0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20094   { "34kf1_1",        0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20095   /* Deprecated forms of the above.  */
20096   { "34kfx",          0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20097   { "34kx",           0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20098   /* 34Kn is a 34kc without DSP.  */
20099   { "34kn",           0, ASE_MT,		ISA_MIPS32R2, CPU_MIPS32R2 },
20100   /* 74K with DSP and DSPR2 ASE, other ASEs are optional.  */
20101   { "74kc",           0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20102   { "74kf2_1",        0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20103   { "74kf",           0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20104   { "74kf1_1",        0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20105   { "74kf3_2",        0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20106   /* Deprecated forms of the above.  */
20107   { "74kfx",          0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20108   { "74kx",           0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20109   /* 1004K cores are multiprocessor versions of the 34K.  */
20110   { "1004kc",         0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20111   { "1004kf2_1",      0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20112   { "1004kf",         0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20113   { "1004kf1_1",      0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20114   /* interaptiv is the new name for 1004kf.  */
20115   { "interaptiv",     0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20116   { "interaptiv-mr2", 0,
20117     ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
20118     ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
20119   /* M5100 family.  */
20120   { "m5100",          0, ASE_MCU,		ISA_MIPS32R5, CPU_MIPS32R5 },
20121   { "m5101",          0, ASE_MCU,		ISA_MIPS32R5, CPU_MIPS32R5 },
20122   /* P5600 with EVA and Virtualization ASEs, other ASEs are optional.  */
20123   { "p5600",          0, ASE_VIRT | ASE_EVA | ASE_XPA,	ISA_MIPS32R5, CPU_MIPS32R5 },
20124 
20125   /* MIPS 64 */
20126   { "5kc",            0, 0,			ISA_MIPS64,   CPU_MIPS64 },
20127   { "5kf",            0, 0,			ISA_MIPS64,   CPU_MIPS64 },
20128   { "20kc",           0, ASE_MIPS3D,		ISA_MIPS64,   CPU_MIPS64 },
20129   { "25kf",           0, ASE_MIPS3D,		ISA_MIPS64,   CPU_MIPS64 },
20130 
20131   /* Broadcom SB-1 CPU core.  */
20132   { "sb1",            0, ASE_MIPS3D | ASE_MDMX,	ISA_MIPS64,   CPU_SB1 },
20133   /* Broadcom SB-1A CPU core.  */
20134   { "sb1a",           0, ASE_MIPS3D | ASE_MDMX,	ISA_MIPS64,   CPU_SB1 },
20135 
20136   /* MIPS 64 Release 2.  */
20137   /* Loongson CPU core.  */
20138   /* -march=loongson3a is an alias of -march=gs464 for compatibility.  */
20139   { "loongson3a",     0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
20140      ISA_MIPS64R2,	CPU_GS464 },
20141   { "gs464",          0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
20142      ISA_MIPS64R2,	CPU_GS464 },
20143   { "gs464e",         0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20144      | ASE_LOONGSON_EXT2,	ISA_MIPS64R2,	CPU_GS464E },
20145   { "gs264e",         0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20146      | ASE_LOONGSON_EXT2 | ASE_MSA | ASE_MSA64,	ISA_MIPS64R2,	CPU_GS264E },
20147 
20148   /* Cavium Networks Octeon CPU core.  */
20149   { "octeon",	      0, 0,			ISA_MIPS64R2, CPU_OCTEON },
20150   { "octeon+",	      0, 0,			ISA_MIPS64R2, CPU_OCTEONP },
20151   { "octeon2",	      0, 0,			ISA_MIPS64R2, CPU_OCTEON2 },
20152   { "octeon3",	      0, ASE_VIRT | ASE_VIRT64,	ISA_MIPS64R5, CPU_OCTEON3 },
20153 
20154   /* RMI Xlr */
20155   { "xlr",	      0, 0,			ISA_MIPS64,   CPU_XLR },
20156 
20157   /* Broadcom XLP.
20158      XLP is mostly like XLR, with the prominent exception that it is
20159      MIPS64R2 rather than MIPS64.  */
20160   { "xlp",	      0, 0,			ISA_MIPS64R2, CPU_XLR },
20161 
20162   /* MIPS 64 Release 6.  */
20163   { "i6400",	      0, ASE_VIRT | ASE_MSA,	ISA_MIPS64R6, CPU_MIPS64R6},
20164   { "i6500",	      0, ASE_VIRT | ASE_MSA | ASE_CRC | ASE_GINV,
20165 						ISA_MIPS64R6, CPU_MIPS64R6},
20166   { "p6600",	      0, ASE_VIRT | ASE_MSA,	ISA_MIPS64R6, CPU_MIPS64R6},
20167 
20168   /* End marker.  */
20169   { NULL, 0, 0, 0, 0 }
20170 };
20171 
20172 
20173 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
20174    with a final "000" replaced by "k".  Ignore case.
20175 
20176    Note: this function is shared between GCC and GAS.  */
20177 
20178 static bool
mips_strict_matching_cpu_name_p(const char * canonical,const char * given)20179 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
20180 {
20181   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
20182     given++, canonical++;
20183 
20184   return ((*given == 0 && *canonical == 0)
20185 	  || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
20186 }
20187 
20188 
20189 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
20190    CPU name.  We've traditionally allowed a lot of variation here.
20191 
20192    Note: this function is shared between GCC and GAS.  */
20193 
20194 static bool
mips_matching_cpu_name_p(const char * canonical,const char * given)20195 mips_matching_cpu_name_p (const char *canonical, const char *given)
20196 {
20197   /* First see if the name matches exactly, or with a final "000"
20198      turned into "k".  */
20199   if (mips_strict_matching_cpu_name_p (canonical, given))
20200     return true;
20201 
20202   /* If not, try comparing based on numerical designation alone.
20203      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
20204   if (TOLOWER (*given) == 'r')
20205     given++;
20206   if (!ISDIGIT (*given))
20207     return false;
20208 
20209   /* Skip over some well-known prefixes in the canonical name,
20210      hoping to find a number there too.  */
20211   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
20212     canonical += 2;
20213   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
20214     canonical += 2;
20215   else if (TOLOWER (canonical[0]) == 'r')
20216     canonical += 1;
20217 
20218   return mips_strict_matching_cpu_name_p (canonical, given);
20219 }
20220 
20221 
20222 /* Parse an option that takes the name of a processor as its argument.
20223    OPTION is the name of the option and CPU_STRING is the argument.
20224    Return the corresponding processor enumeration if the CPU_STRING is
20225    recognized, otherwise report an error and return null.
20226 
20227    A similar function exists in GCC.  */
20228 
20229 static const struct mips_cpu_info *
mips_parse_cpu(const char * option,const char * cpu_string)20230 mips_parse_cpu (const char *option, const char *cpu_string)
20231 {
20232   const struct mips_cpu_info *p;
20233 
20234   /* 'from-abi' selects the most compatible architecture for the given
20235      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
20236      EABIs, we have to decide whether we're using the 32-bit or 64-bit
20237      version.  Look first at the -mgp options, if given, otherwise base
20238      the choice on MIPS_DEFAULT_64BIT.
20239 
20240      Treat NO_ABI like the EABIs.  One reason to do this is that the
20241      plain 'mips' and 'mips64' configs have 'from-abi' as their default
20242      architecture.  This code picks MIPS I for 'mips' and MIPS III for
20243      'mips64', just as we did in the days before 'from-abi'.  */
20244   if (strcasecmp (cpu_string, "from-abi") == 0)
20245     {
20246       if (ABI_NEEDS_32BIT_REGS (mips_abi))
20247 	return mips_cpu_info_from_isa (ISA_MIPS1);
20248 
20249       if (ABI_NEEDS_64BIT_REGS (mips_abi))
20250 	return mips_cpu_info_from_isa (ISA_MIPS3);
20251 
20252       if (file_mips_opts.gp >= 0)
20253 	return mips_cpu_info_from_isa (file_mips_opts.gp == 32
20254 				       ? ISA_MIPS1 : ISA_MIPS3);
20255 
20256       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
20257 				     ? ISA_MIPS3
20258 				     : ISA_MIPS1);
20259     }
20260 
20261   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
20262   if (strcasecmp (cpu_string, "default") == 0)
20263     return 0;
20264 
20265   for (p = mips_cpu_info_table; p->name != 0; p++)
20266     if (mips_matching_cpu_name_p (p->name, cpu_string))
20267       return p;
20268 
20269   as_bad (_("bad value (%s) for %s"), cpu_string, option);
20270   return 0;
20271 }
20272 
20273 /* Return the canonical processor information for ISA (a member of the
20274    ISA_MIPS* enumeration).  */
20275 
20276 static const struct mips_cpu_info *
mips_cpu_info_from_isa(int isa)20277 mips_cpu_info_from_isa (int isa)
20278 {
20279   int i;
20280 
20281   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20282     if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
20283 	&& isa == mips_cpu_info_table[i].isa)
20284       return (&mips_cpu_info_table[i]);
20285 
20286   return NULL;
20287 }
20288 
20289 static const struct mips_cpu_info *
mips_cpu_info_from_arch(int arch)20290 mips_cpu_info_from_arch (int arch)
20291 {
20292   int i;
20293 
20294   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20295     if (arch == mips_cpu_info_table[i].cpu)
20296       return (&mips_cpu_info_table[i]);
20297 
20298   return NULL;
20299 }
20300 
20301 static void
show(FILE * stream,const char * string,int * col_p,int * first_p)20302 show (FILE *stream, const char *string, int *col_p, int *first_p)
20303 {
20304   if (*first_p)
20305     {
20306       fprintf (stream, "%24s", "");
20307       *col_p = 24;
20308     }
20309   else
20310     {
20311       fprintf (stream, ", ");
20312       *col_p += 2;
20313     }
20314 
20315   if (*col_p + strlen (string) > 72)
20316     {
20317       fprintf (stream, "\n%24s", "");
20318       *col_p = 24;
20319     }
20320 
20321   fprintf (stream, "%s", string);
20322   *col_p += strlen (string);
20323 
20324   *first_p = 0;
20325 }
20326 
20327 void
md_show_usage(FILE * stream)20328 md_show_usage (FILE *stream)
20329 {
20330   int column, first;
20331   size_t i;
20332 
20333   fprintf (stream, _("\
20334 MIPS options:\n\
20335 -EB			generate big endian output\n\
20336 -EL			generate little endian output\n\
20337 -g, -g2			do not remove unneeded NOPs or swap branches\n\
20338 -G NUM			allow referencing objects up to NUM bytes\n\
20339 			implicitly with the gp register [default 8]\n"));
20340   fprintf (stream, _("\
20341 -mips1			generate MIPS ISA I instructions\n\
20342 -mips2			generate MIPS ISA II instructions\n\
20343 -mips3			generate MIPS ISA III instructions\n\
20344 -mips4			generate MIPS ISA IV instructions\n\
20345 -mips5                  generate MIPS ISA V instructions\n\
20346 -mips32                 generate MIPS32 ISA instructions\n\
20347 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
20348 -mips32r3               generate MIPS32 release 3 ISA instructions\n\
20349 -mips32r5               generate MIPS32 release 5 ISA instructions\n\
20350 -mips32r6               generate MIPS32 release 6 ISA instructions\n\
20351 -mips64                 generate MIPS64 ISA instructions\n\
20352 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
20353 -mips64r3               generate MIPS64 release 3 ISA instructions\n\
20354 -mips64r5               generate MIPS64 release 5 ISA instructions\n\
20355 -mips64r6               generate MIPS64 release 6 ISA instructions\n\
20356 -march=CPU/-mtune=CPU	generate code/schedule for CPU, where CPU is one of:\n"));
20357 
20358   first = 1;
20359 
20360   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20361     show (stream, mips_cpu_info_table[i].name, &column, &first);
20362   show (stream, "from-abi", &column, &first);
20363   fputc ('\n', stream);
20364 
20365   fprintf (stream, _("\
20366 -mCPU			equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20367 -no-mCPU		don't generate code specific to CPU.\n\
20368 			For -mCPU and -no-mCPU, CPU must be one of:\n"));
20369 
20370   first = 1;
20371 
20372   show (stream, "3900", &column, &first);
20373   show (stream, "4010", &column, &first);
20374   show (stream, "4100", &column, &first);
20375   show (stream, "4650", &column, &first);
20376   fputc ('\n', stream);
20377 
20378   fprintf (stream, _("\
20379 -mips16			generate mips16 instructions\n\
20380 -no-mips16		do not generate mips16 instructions\n"));
20381   fprintf (stream, _("\
20382 -mmips16e2		generate MIPS16e2 instructions\n\
20383 -mno-mips16e2		do not generate MIPS16e2 instructions\n"));
20384   fprintf (stream, _("\
20385 -mmicromips		generate microMIPS instructions\n\
20386 -mno-micromips		do not generate microMIPS instructions\n"));
20387   fprintf (stream, _("\
20388 -msmartmips		generate smartmips instructions\n\
20389 -mno-smartmips		do not generate smartmips instructions\n"));
20390   fprintf (stream, _("\
20391 -mdsp			generate DSP instructions\n\
20392 -mno-dsp		do not generate DSP instructions\n"));
20393   fprintf (stream, _("\
20394 -mdspr2			generate DSP R2 instructions\n\
20395 -mno-dspr2		do not generate DSP R2 instructions\n"));
20396   fprintf (stream, _("\
20397 -mdspr3			generate DSP R3 instructions\n\
20398 -mno-dspr3		do not generate DSP R3 instructions\n"));
20399   fprintf (stream, _("\
20400 -mmt			generate MT instructions\n\
20401 -mno-mt			do not generate MT instructions\n"));
20402   fprintf (stream, _("\
20403 -mmcu			generate MCU instructions\n\
20404 -mno-mcu		do not generate MCU instructions\n"));
20405   fprintf (stream, _("\
20406 -mmsa			generate MSA instructions\n\
20407 -mno-msa		do not generate MSA instructions\n"));
20408   fprintf (stream, _("\
20409 -mxpa			generate eXtended Physical Address (XPA) instructions\n\
20410 -mno-xpa		do not generate eXtended Physical Address (XPA) instructions\n"));
20411   fprintf (stream, _("\
20412 -mvirt			generate Virtualization instructions\n\
20413 -mno-virt		do not generate Virtualization instructions\n"));
20414   fprintf (stream, _("\
20415 -mcrc			generate CRC instructions\n\
20416 -mno-crc		do not generate CRC instructions\n"));
20417   fprintf (stream, _("\
20418 -mginv			generate Global INValidate (GINV) instructions\n\
20419 -mno-ginv		do not generate Global INValidate instructions\n"));
20420   fprintf (stream, _("\
20421 -mloongson-mmi		generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20422 -mno-loongson-mmi	do not generate Loongson MultiMedia extensions Instructions\n"));
20423   fprintf (stream, _("\
20424 -mloongson-cam		generate Loongson Content Address Memory (CAM) instructions\n\
20425 -mno-loongson-cam	do not generate Loongson Content Address Memory Instructions\n"));
20426   fprintf (stream, _("\
20427 -mloongson-ext		generate Loongson EXTensions (EXT) instructions\n\
20428 -mno-loongson-ext	do not generate Loongson EXTensions Instructions\n"));
20429   fprintf (stream, _("\
20430 -mloongson-ext2		generate Loongson EXTensions R2 (EXT2) instructions\n\
20431 -mno-loongson-ext2	do not generate Loongson EXTensions R2 Instructions\n"));
20432   fprintf (stream, _("\
20433 -minsn32		only generate 32-bit microMIPS instructions\n\
20434 -mno-insn32		generate all microMIPS instructions\n"));
20435 #if DEFAULT_MIPS_FIX_LOONGSON3_LLSC
20436   fprintf (stream, _("\
20437 -mfix-loongson3-llsc	work around Loongson3 LL/SC errata, default\n\
20438 -mno-fix-loongson3-llsc	disable work around Loongson3 LL/SC errata\n"));
20439 #else
20440   fprintf (stream, _("\
20441 -mfix-loongson3-llsc	work around Loongson3 LL/SC errata\n\
20442 -mno-fix-loongson3-llsc	disable work around Loongson3 LL/SC errata, default\n"));
20443 #endif
20444   fprintf (stream, _("\
20445 -mfix-loongson2f-jump	work around Loongson2F JUMP instructions\n\
20446 -mfix-loongson2f-nop	work around Loongson2F NOP errata\n\
20447 -mfix-loongson3-llsc	work around Loongson3 LL/SC errata\n\
20448 -mno-fix-loongson3-llsc	disable work around Loongson3 LL/SC errata\n\
20449 -mfix-vr4120		work around certain VR4120 errata\n\
20450 -mfix-vr4130		work around VR4130 mflo/mfhi errata\n\
20451 -mfix-24k		insert a nop after ERET and DERET instructions\n\
20452 -mfix-cn63xxp1		work around CN63XXP1 PREF errata\n\
20453 -mfix-r5900		work around R5900 short loop errata\n\
20454 -mgp32			use 32-bit GPRs, regardless of the chosen ISA\n\
20455 -mfp32			use 32-bit FPRs, regardless of the chosen ISA\n\
20456 -msym32			assume all symbols have 32-bit values\n\
20457 -O0			do not remove unneeded NOPs, do not swap branches\n\
20458 -O, -O1			remove unneeded NOPs, do not swap branches\n\
20459 -O2			remove unneeded NOPs and swap branches\n\
20460 --trap, --no-break	trap exception on div by 0 and mult overflow\n\
20461 --break, --no-trap	break exception on div by 0 and mult overflow\n"));
20462   fprintf (stream, _("\
20463 -mhard-float		allow floating-point instructions\n\
20464 -msoft-float		do not allow floating-point instructions\n\
20465 -msingle-float		only allow 32-bit floating-point operations\n\
20466 -mdouble-float		allow 32-bit and 64-bit floating-point operations\n\
20467 --[no-]construct-floats	[dis]allow floating point values to be constructed\n\
20468 --[no-]relax-branch	[dis]allow out-of-range branches to be relaxed\n\
20469 -mignore-branch-isa	accept invalid branches requiring an ISA mode switch\n\
20470 -mno-ignore-branch-isa	reject invalid branches requiring an ISA mode switch\n\
20471 -mnan=ENCODING		select an IEEE 754 NaN encoding convention, either of:\n"));
20472 
20473   first = 1;
20474 
20475   show (stream, "legacy", &column, &first);
20476   show (stream, "2008", &column, &first);
20477 
20478   fputc ('\n', stream);
20479 
20480   fprintf (stream, _("\
20481 -KPIC, -call_shared	generate SVR4 position independent code\n\
20482 -call_nonpic		generate non-PIC code that can operate with DSOs\n\
20483 -mvxworks-pic		generate VxWorks position independent code\n\
20484 -non_shared		do not generate code that can operate with DSOs\n\
20485 -xgot			assume a 32 bit GOT\n\
20486 -mpdr, -mno-pdr		enable/disable creation of .pdr sections\n\
20487 -mshared, -mno-shared   disable/enable .cpload optimization for\n\
20488                         position dependent (non shared) code\n\
20489 -mabi=ABI		create ABI conformant object file for:\n"));
20490 
20491   first = 1;
20492 
20493   show (stream, "32", &column, &first);
20494   show (stream, "o64", &column, &first);
20495   show (stream, "n32", &column, &first);
20496   show (stream, "64", &column, &first);
20497   show (stream, "eabi", &column, &first);
20498 
20499   fputc ('\n', stream);
20500 
20501   fprintf (stream, _("\
20502 -32			create o32 ABI object file%s\n"),
20503 	   MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20504   fprintf (stream, _("\
20505 -n32			create n32 ABI object file%s\n"),
20506 	   MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20507   fprintf (stream, _("\
20508 -64			create 64 ABI object file%s\n"),
20509 	   MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
20510 }
20511 
20512 #ifdef TE_IRIX
20513 enum dwarf2_format
mips_dwarf2_format(asection * sec ATTRIBUTE_UNUSED)20514 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
20515 {
20516   if (HAVE_64BIT_SYMBOLS)
20517     return dwarf2_format_64bit_irix;
20518   else
20519     return dwarf2_format_32bit;
20520 }
20521 #endif
20522 
20523 int
mips_dwarf2_addr_size(void)20524 mips_dwarf2_addr_size (void)
20525 {
20526   if (HAVE_64BIT_OBJECTS)
20527     return 8;
20528   else
20529     return 4;
20530 }
20531 
20532 /* Standard calling conventions leave the CFA at SP on entry.  */
20533 void
mips_cfi_frame_initial_instructions(void)20534 mips_cfi_frame_initial_instructions (void)
20535 {
20536   cfi_add_CFA_def_cfa_register (SP);
20537 }
20538 
20539 int
tc_mips_regname_to_dw2regnum(char * regname)20540 tc_mips_regname_to_dw2regnum (char *regname)
20541 {
20542   unsigned int regnum = -1;
20543   unsigned int reg;
20544 
20545   if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20546     regnum = reg;
20547 
20548   return regnum;
20549 }
20550 
20551 /* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20552    Given a symbolic attribute NAME, return the proper integer value.
20553    Returns -1 if the attribute is not known.  */
20554 
20555 int
mips_convert_symbolic_attribute(const char * name)20556 mips_convert_symbolic_attribute (const char *name)
20557 {
20558   static const struct
20559   {
20560     const char * name;
20561     const int    tag;
20562   }
20563   attribute_table[] =
20564     {
20565 #define T(tag) {#tag, tag}
20566       T (Tag_GNU_MIPS_ABI_FP),
20567       T (Tag_GNU_MIPS_ABI_MSA),
20568 #undef T
20569     };
20570   unsigned int i;
20571 
20572   if (name == NULL)
20573     return -1;
20574 
20575   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20576     if (streq (name, attribute_table[i].name))
20577       return attribute_table[i].tag;
20578 
20579   return -1;
20580 }
20581 
20582 void
md_mips_end(void)20583 md_mips_end (void)
20584 {
20585   int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20586 
20587   mips_emit_delays ();
20588   if (cur_proc_ptr)
20589     as_warn (_("missing .end at end of assembly"));
20590 
20591   /* Just in case no code was emitted, do the consistency check.  */
20592   file_mips_check_options ();
20593 
20594   /* Set a floating-point ABI if the user did not.  */
20595   if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20596     {
20597       /* Perform consistency checks on the floating-point ABI.  */
20598       fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20599 					Tag_GNU_MIPS_ABI_FP);
20600       if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20601 	check_fpabi (fpabi);
20602     }
20603   else
20604     {
20605       /* Soft-float gets precedence over single-float, the two options should
20606          not be used together so this should not matter.  */
20607       if (file_mips_opts.soft_float == 1)
20608 	fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20609       /* Single-float gets precedence over all double_float cases.  */
20610       else if (file_mips_opts.single_float == 1)
20611 	fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20612       else
20613 	{
20614 	  switch (file_mips_opts.fp)
20615 	    {
20616 	    case 32:
20617 	      if (file_mips_opts.gp == 32)
20618 		fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20619 	      break;
20620 	    case 0:
20621 	      fpabi = Val_GNU_MIPS_ABI_FP_XX;
20622 	      break;
20623 	    case 64:
20624 	      if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20625 		fpabi = Val_GNU_MIPS_ABI_FP_64A;
20626 	      else if (file_mips_opts.gp == 32)
20627 		fpabi = Val_GNU_MIPS_ABI_FP_64;
20628 	      else
20629 		fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20630 	      break;
20631 	    }
20632 	}
20633 
20634       bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20635 				Tag_GNU_MIPS_ABI_FP, fpabi);
20636     }
20637 }
20638 
20639 /*  Returns the relocation type required for a particular CFI encoding.  */
20640 
20641 bfd_reloc_code_real_type
mips_cfi_reloc_for_encoding(int encoding)20642 mips_cfi_reloc_for_encoding (int encoding)
20643 {
20644   if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20645     return BFD_RELOC_32_PCREL;
20646   else return BFD_RELOC_NONE;
20647 }
20648