gas/
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
4 Free Software Foundation, Inc.
5 Contributed by the OSF and Ralph Campbell.
6 Written by Keith Knowles and Ralph Campbell, working independently.
7 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
8 Support.
9
10 This file is part of GAS.
11
12 GAS is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
16
17 GAS is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GAS; see the file COPYING. If not, write to the Free
24 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
25 02110-1301, USA. */
26
27 #include "as.h"
28 #include "config.h"
29 #include "subsegs.h"
30 #include "safe-ctype.h"
31
32 #include "opcode/mips.h"
33 #include "itbl-ops.h"
34 #include "dwarf2dbg.h"
35 #include "dw2gencfi.h"
36
37 #ifdef DEBUG
38 #define DBG(x) printf x
39 #else
40 #define DBG(x)
41 #endif
42
43 #ifdef OBJ_MAYBE_ELF
44 /* Clean up namespace so we can include obj-elf.h too. */
45 static int mips_output_flavor (void);
46 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
47 #undef OBJ_PROCESS_STAB
48 #undef OUTPUT_FLAVOR
49 #undef S_GET_ALIGN
50 #undef S_GET_SIZE
51 #undef S_SET_ALIGN
52 #undef S_SET_SIZE
53 #undef obj_frob_file
54 #undef obj_frob_file_after_relocs
55 #undef obj_frob_symbol
56 #undef obj_pop_insert
57 #undef obj_sec_sym_ok_for_reloc
58 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60 #include "obj-elf.h"
61 /* Fix any of them that we actually care about. */
62 #undef OUTPUT_FLAVOR
63 #define OUTPUT_FLAVOR mips_output_flavor()
64 #endif
65
66 #if defined (OBJ_ELF)
67 #include "elf/mips.h"
68 #endif
69
70 #ifndef ECOFF_DEBUGGING
71 #define NO_ECOFF_DEBUGGING
72 #define ECOFF_DEBUGGING 0
73 #endif
74
75 int mips_flag_mdebug = -1;
76
77 /* Control generation of .pdr sections. Off by default on IRIX: the native
78 linker doesn't know about and discards them, but relocations against them
79 remain, leading to rld crashes. */
80 #ifdef TE_IRIX
81 int mips_flag_pdr = FALSE;
82 #else
83 int mips_flag_pdr = TRUE;
84 #endif
85
86 #include "ecoff.h"
87
88 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89 static char *mips_regmask_frag;
90 #endif
91
92 #define ZERO 0
93 #define ATREG 1
94 #define S0 16
95 #define S7 23
96 #define TREG 24
97 #define PIC_CALL_REG 25
98 #define KT0 26
99 #define KT1 27
100 #define GP 28
101 #define SP 29
102 #define FP 30
103 #define RA 31
104
105 #define ILLEGAL_REG (32)
106
107 #define AT mips_opts.at
108
109 /* Allow override of standard little-endian ECOFF format. */
110
111 #ifndef ECOFF_LITTLE_FORMAT
112 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
113 #endif
114
115 extern int target_big_endian;
116
117 /* The name of the readonly data section. */
118 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
119 ? ".rdata" \
120 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
121 ? ".rdata" \
122 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
123 ? ".rodata" \
124 : (abort (), ""))
125
126 /* Ways in which an instruction can be "appended" to the output. */
127 enum append_method {
128 /* Just add it normally. */
129 APPEND_ADD,
130
131 /* Add it normally and then add a nop. */
132 APPEND_ADD_WITH_NOP,
133
134 /* Turn an instruction with a delay slot into a "compact" version. */
135 APPEND_ADD_COMPACT,
136
137 /* Insert the instruction before the last one. */
138 APPEND_SWAP
139 };
140
141 /* Information about an instruction, including its format, operands
142 and fixups. */
143 struct mips_cl_insn
144 {
145 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
146 const struct mips_opcode *insn_mo;
147
148 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
149 a copy of INSN_MO->match with the operands filled in. If we have
150 decided to use an extended MIPS16 instruction, this includes the
151 extension. */
152 unsigned long insn_opcode;
153
154 /* The frag that contains the instruction. */
155 struct frag *frag;
156
157 /* The offset into FRAG of the first instruction byte. */
158 long where;
159
160 /* The relocs associated with the instruction, if any. */
161 fixS *fixp[3];
162
163 /* True if this entry cannot be moved from its current position. */
164 unsigned int fixed_p : 1;
165
166 /* True if this instruction occurred in a .set noreorder block. */
167 unsigned int noreorder_p : 1;
168
169 /* True for mips16 instructions that jump to an absolute address. */
170 unsigned int mips16_absolute_jump_p : 1;
171
172 /* True if this instruction is complete. */
173 unsigned int complete_p : 1;
174
175 /* True if this instruction is cleared from history by unconditional
176 branch. */
177 unsigned int cleared_p : 1;
178 };
179
180 /* The ABI to use. */
181 enum mips_abi_level
182 {
183 NO_ABI = 0,
184 O32_ABI,
185 O64_ABI,
186 N32_ABI,
187 N64_ABI,
188 EABI_ABI
189 };
190
191 /* MIPS ABI we are using for this output file. */
192 static enum mips_abi_level mips_abi = NO_ABI;
193
194 /* Whether or not we have code that can call pic code. */
195 int mips_abicalls = FALSE;
196
197 /* Whether or not we have code which can be put into a shared
198 library. */
199 static bfd_boolean mips_in_shared = TRUE;
200
201 /* This is the set of options which may be modified by the .set
202 pseudo-op. We use a struct so that .set push and .set pop are more
203 reliable. */
204
205 struct mips_set_options
206 {
207 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
208 if it has not been initialized. Changed by `.set mipsN', and the
209 -mipsN command line option, and the default CPU. */
210 int isa;
211 /* Enabled Application Specific Extensions (ASEs). These are set to -1
212 if they have not been initialized. Changed by `.set <asename>', by
213 command line options, and based on the default architecture. */
214 int ase_mips3d;
215 int ase_mdmx;
216 int ase_smartmips;
217 int ase_dsp;
218 int ase_dspr2;
219 int ase_eva;
220 int ase_mt;
221 int ase_mcu;
222 int ase_virt;
223 /* Whether we are assembling for the mips16 processor. 0 if we are
224 not, 1 if we are, and -1 if the value has not been initialized.
225 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
226 -nomips16 command line options, and the default CPU. */
227 int mips16;
228 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
229 1 if we are, and -1 if the value has not been initialized. Changed
230 by `.set micromips' and `.set nomicromips', and the -mmicromips
231 and -mno-micromips command line options, and the default CPU. */
232 int micromips;
233 /* Non-zero if we should not reorder instructions. Changed by `.set
234 reorder' and `.set noreorder'. */
235 int noreorder;
236 /* Non-zero if we should not permit the register designated "assembler
237 temporary" to be used in instructions. The value is the register
238 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
239 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
240 unsigned int at;
241 /* Non-zero if we should warn when a macro instruction expands into
242 more than one machine instruction. Changed by `.set nomacro' and
243 `.set macro'. */
244 int warn_about_macros;
245 /* Non-zero if we should not move instructions. Changed by `.set
246 move', `.set volatile', `.set nomove', and `.set novolatile'. */
247 int nomove;
248 /* Non-zero if we should not optimize branches by moving the target
249 of the branch into the delay slot. Actually, we don't perform
250 this optimization anyhow. Changed by `.set bopt' and `.set
251 nobopt'. */
252 int nobopt;
253 /* Non-zero if we should not autoextend mips16 instructions.
254 Changed by `.set autoextend' and `.set noautoextend'. */
255 int noautoextend;
256 /* Restrict general purpose registers and floating point registers
257 to 32 bit. This is initially determined when -mgp32 or -mfp32
258 is passed but can changed if the assembler code uses .set mipsN. */
259 int gp32;
260 int fp32;
261 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
262 command line option, and the default CPU. */
263 int arch;
264 /* True if ".set sym32" is in effect. */
265 bfd_boolean sym32;
266 /* True if floating-point operations are not allowed. Changed by .set
267 softfloat or .set hardfloat, by command line options -msoft-float or
268 -mhard-float. The default is false. */
269 bfd_boolean soft_float;
270
271 /* True if only single-precision floating-point operations are allowed.
272 Changed by .set singlefloat or .set doublefloat, command-line options
273 -msingle-float or -mdouble-float. The default is false. */
274 bfd_boolean single_float;
275 };
276
277 /* This is the struct we use to hold the current set of options. Note
278 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
279 -1 to indicate that they have not been initialized. */
280
281 /* True if -mgp32 was passed. */
282 static int file_mips_gp32 = -1;
283
284 /* True if -mfp32 was passed. */
285 static int file_mips_fp32 = -1;
286
287 /* 1 if -msoft-float, 0 if -mhard-float. The default is 0. */
288 static int file_mips_soft_float = 0;
289
290 /* 1 if -msingle-float, 0 if -mdouble-float. The default is 0. */
291 static int file_mips_single_float = 0;
292
293 static struct mips_set_options mips_opts =
294 {
295 /* isa */ ISA_UNKNOWN, /* ase_mips3d */ -1, /* ase_mdmx */ -1,
296 /* ase_smartmips */ 0, /* ase_dsp */ -1, /* ase_dspr2 */ -1,
297 /* ase_eva */ -1, /* ase_mt */ -1, /* ase_mcu */ -1,
298 /* ase_virt */ -1, /* mips16 */ -1, /* micromips */ -1,
299 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
300 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* gp32 */ 0,
301 /* fp32 */ 0, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
302 /* soft_float */ FALSE, /* single_float */ FALSE
303 };
304
305 /* These variables are filled in with the masks of registers used.
306 The object format code reads them and puts them in the appropriate
307 place. */
308 unsigned long mips_gprmask;
309 unsigned long mips_cprmask[4];
310
311 /* MIPS ISA we are using for this output file. */
312 static int file_mips_isa = ISA_UNKNOWN;
313
314 /* True if any MIPS16 code was produced. */
315 static int file_ase_mips16;
316
317 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
318 || mips_opts.isa == ISA_MIPS32R2 \
319 || mips_opts.isa == ISA_MIPS64 \
320 || mips_opts.isa == ISA_MIPS64R2)
321
322 /* True if any microMIPS code was produced. */
323 static int file_ase_micromips;
324
325 /* True if we want to create R_MIPS_JALR for jalr $25. */
326 #ifdef TE_IRIX
327 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
328 #else
329 /* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
330 because there's no place for any addend, the only acceptable
331 expression is a bare symbol. */
332 #define MIPS_JALR_HINT_P(EXPR) \
333 (!HAVE_IN_PLACE_ADDENDS \
334 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
335 #endif
336
337 /* True if -mips3d was passed or implied by arguments passed on the
338 command line (e.g., by -march). */
339 static int file_ase_mips3d;
340
341 /* True if -mdmx was passed or implied by arguments passed on the
342 command line (e.g., by -march). */
343 static int file_ase_mdmx;
344
345 /* True if -msmartmips was passed or implied by arguments passed on the
346 command line (e.g., by -march). */
347 static int file_ase_smartmips;
348
349 #define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32 \
350 || mips_opts.isa == ISA_MIPS32R2)
351
352 /* True if -mdsp was passed or implied by arguments passed on the
353 command line (e.g., by -march). */
354 static int file_ase_dsp;
355
356 #define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2 \
357 || mips_opts.isa == ISA_MIPS64R2 \
358 || mips_opts.micromips)
359
360 #define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
361
362 /* True if -mdspr2 was passed or implied by arguments passed on the
363 command line (e.g., by -march). */
364 static int file_ase_dspr2;
365
366 #define ISA_SUPPORTS_DSPR2_ASE (mips_opts.isa == ISA_MIPS32R2 \
367 || mips_opts.isa == ISA_MIPS64R2 \
368 || mips_opts.micromips)
369
370 /* True if -meva was passed or implied by arguments passed on the
371 command line (e.g., by -march). */
372 static int file_ase_eva;
373
374 #define ISA_SUPPORTS_EVA_ASE (mips_opts.isa == ISA_MIPS32R2 \
375 || mips_opts.isa == ISA_MIPS64R2 \
376 || mips_opts.micromips)
377
378 /* True if -mmt was passed or implied by arguments passed on the
379 command line (e.g., by -march). */
380 static int file_ase_mt;
381
382 #define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2 \
383 || mips_opts.isa == ISA_MIPS64R2)
384
385 #define ISA_SUPPORTS_MCU_ASE (mips_opts.isa == ISA_MIPS32R2 \
386 || mips_opts.isa == ISA_MIPS64R2 \
387 || mips_opts.micromips)
388
389 /* True if -mvirt was passed or implied by arguments passed on the
390 command line (e.g., by -march). */
391 static int file_ase_virt;
392
393 #define ISA_SUPPORTS_VIRT_ASE (mips_opts.isa == ISA_MIPS32R2 \
394 || mips_opts.isa == ISA_MIPS64R2 \
395 || mips_opts.micromips)
396
397 #define ISA_SUPPORTS_VIRT64_ASE (mips_opts.isa == ISA_MIPS64R2 \
398 || (mips_opts.micromips \
399 && ISA_HAS_64BIT_REGS (mips_opts.isa)))
400
401 /* The argument of the -march= flag. The architecture we are assembling. */
402 static int file_mips_arch = CPU_UNKNOWN;
403 static const char *mips_arch_string;
404
405 /* The argument of the -mtune= flag. The architecture for which we
406 are optimizing. */
407 static int mips_tune = CPU_UNKNOWN;
408 static const char *mips_tune_string;
409
410 /* True when generating 32-bit code for a 64-bit processor. */
411 static int mips_32bitmode = 0;
412
413 /* True if the given ABI requires 32-bit registers. */
414 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
415
416 /* Likewise 64-bit registers. */
417 #define ABI_NEEDS_64BIT_REGS(ABI) \
418 ((ABI) == N32_ABI \
419 || (ABI) == N64_ABI \
420 || (ABI) == O64_ABI)
421
422 /* Return true if ISA supports 64 bit wide gp registers. */
423 #define ISA_HAS_64BIT_REGS(ISA) \
424 ((ISA) == ISA_MIPS3 \
425 || (ISA) == ISA_MIPS4 \
426 || (ISA) == ISA_MIPS5 \
427 || (ISA) == ISA_MIPS64 \
428 || (ISA) == ISA_MIPS64R2)
429
430 /* Return true if ISA supports 64 bit wide float registers. */
431 #define ISA_HAS_64BIT_FPRS(ISA) \
432 ((ISA) == ISA_MIPS3 \
433 || (ISA) == ISA_MIPS4 \
434 || (ISA) == ISA_MIPS5 \
435 || (ISA) == ISA_MIPS32R2 \
436 || (ISA) == ISA_MIPS64 \
437 || (ISA) == ISA_MIPS64R2)
438
439 /* Return true if ISA supports 64-bit right rotate (dror et al.)
440 instructions. */
441 #define ISA_HAS_DROR(ISA) \
442 ((ISA) == ISA_MIPS64R2 \
443 || (mips_opts.micromips \
444 && ISA_HAS_64BIT_REGS (ISA)) \
445 )
446
447 /* Return true if ISA supports 32-bit right rotate (ror et al.)
448 instructions. */
449 #define ISA_HAS_ROR(ISA) \
450 ((ISA) == ISA_MIPS32R2 \
451 || (ISA) == ISA_MIPS64R2 \
452 || mips_opts.ase_smartmips \
453 || mips_opts.micromips \
454 )
455
456 /* Return true if ISA supports single-precision floats in odd registers. */
457 #define ISA_HAS_ODD_SINGLE_FPR(ISA) \
458 ((ISA) == ISA_MIPS32 \
459 || (ISA) == ISA_MIPS32R2 \
460 || (ISA) == ISA_MIPS64 \
461 || (ISA) == ISA_MIPS64R2)
462
463 /* Return true if ISA supports move to/from high part of a 64-bit
464 floating-point register. */
465 #define ISA_HAS_MXHC1(ISA) \
466 ((ISA) == ISA_MIPS32R2 \
467 || (ISA) == ISA_MIPS64R2)
468
469 #define HAVE_32BIT_GPRS \
470 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
471
472 #define HAVE_32BIT_FPRS \
473 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
474
475 #define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
476 #define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
477
478 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
479
480 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
481
482 /* True if relocations are stored in-place. */
483 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
484
485 /* The ABI-derived address size. */
486 #define HAVE_64BIT_ADDRESSES \
487 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
488 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
489
490 /* The size of symbolic constants (i.e., expressions of the form
491 "SYMBOL" or "SYMBOL + OFFSET"). */
492 #define HAVE_32BIT_SYMBOLS \
493 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
494 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
495
496 /* Addresses are loaded in different ways, depending on the address size
497 in use. The n32 ABI Documentation also mandates the use of additions
498 with overflow checking, but existing implementations don't follow it. */
499 #define ADDRESS_ADD_INSN \
500 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
501
502 #define ADDRESS_ADDI_INSN \
503 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
504
505 #define ADDRESS_LOAD_INSN \
506 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
507
508 #define ADDRESS_STORE_INSN \
509 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
510
511 /* Return true if the given CPU supports the MIPS16 ASE. */
512 #define CPU_HAS_MIPS16(cpu) \
513 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
514 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
515
516 /* Return true if the given CPU supports the microMIPS ASE. */
517 #define CPU_HAS_MICROMIPS(cpu) 0
518
519 /* True if CPU has a dror instruction. */
520 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
521
522 /* True if CPU has a ror instruction. */
523 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
524
525 /* True if CPU is in the Octeon family */
526 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
527
528 /* True if CPU has seq/sne and seqi/snei instructions. */
529 #define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
530
531 /* True, if CPU has support for ldc1 and sdc1. */
532 #define CPU_HAS_LDC1_SDC1(CPU) \
533 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
534
535 /* True if mflo and mfhi can be immediately followed by instructions
536 which write to the HI and LO registers.
537
538 According to MIPS specifications, MIPS ISAs I, II, and III need
539 (at least) two instructions between the reads of HI/LO and
540 instructions which write them, and later ISAs do not. Contradicting
541 the MIPS specifications, some MIPS IV processor user manuals (e.g.
542 the UM for the NEC Vr5000) document needing the instructions between
543 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
544 MIPS64 and later ISAs to have the interlocks, plus any specific
545 earlier-ISA CPUs for which CPU documentation declares that the
546 instructions are really interlocked. */
547 #define hilo_interlocks \
548 (mips_opts.isa == ISA_MIPS32 \
549 || mips_opts.isa == ISA_MIPS32R2 \
550 || mips_opts.isa == ISA_MIPS64 \
551 || mips_opts.isa == ISA_MIPS64R2 \
552 || mips_opts.arch == CPU_R4010 \
553 || mips_opts.arch == CPU_R5900 \
554 || mips_opts.arch == CPU_R10000 \
555 || mips_opts.arch == CPU_R12000 \
556 || mips_opts.arch == CPU_R14000 \
557 || mips_opts.arch == CPU_R16000 \
558 || mips_opts.arch == CPU_RM7000 \
559 || mips_opts.arch == CPU_VR5500 \
560 || mips_opts.micromips \
561 )
562
563 /* Whether the processor uses hardware interlocks to protect reads
564 from the GPRs after they are loaded from memory, and thus does not
565 require nops to be inserted. This applies to instructions marked
566 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
567 level I and microMIPS mode instructions are always interlocked. */
568 #define gpr_interlocks \
569 (mips_opts.isa != ISA_MIPS1 \
570 || mips_opts.arch == CPU_R3900 \
571 || mips_opts.arch == CPU_R5900 \
572 || mips_opts.micromips \
573 )
574
575 /* Whether the processor uses hardware interlocks to avoid delays
576 required by coprocessor instructions, and thus does not require
577 nops to be inserted. This applies to instructions marked
578 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
579 between instructions marked INSN_WRITE_COND_CODE and ones marked
580 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
581 levels I, II, and III and microMIPS mode instructions are always
582 interlocked. */
583 /* Itbl support may require additional care here. */
584 #define cop_interlocks \
585 ((mips_opts.isa != ISA_MIPS1 \
586 && mips_opts.isa != ISA_MIPS2 \
587 && mips_opts.isa != ISA_MIPS3) \
588 || mips_opts.arch == CPU_R4300 \
589 || mips_opts.micromips \
590 )
591
592 /* Whether the processor uses hardware interlocks to protect reads
593 from coprocessor registers after they are loaded from memory, and
594 thus does not require nops to be inserted. This applies to
595 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
596 requires at MIPS ISA level I and microMIPS mode instructions are
597 always interlocked. */
598 #define cop_mem_interlocks \
599 (mips_opts.isa != ISA_MIPS1 \
600 || mips_opts.micromips \
601 )
602
603 /* Is this a mfhi or mflo instruction? */
604 #define MF_HILO_INSN(PINFO) \
605 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
606
607 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
608 has been selected. This implies, in particular, that addresses of text
609 labels have their LSB set. */
610 #define HAVE_CODE_COMPRESSION \
611 ((mips_opts.mips16 | mips_opts.micromips) != 0)
612
613 /* MIPS PIC level. */
614
615 enum mips_pic_level mips_pic;
616
617 /* 1 if we should generate 32 bit offsets from the $gp register in
618 SVR4_PIC mode. Currently has no meaning in other modes. */
619 static int mips_big_got = 0;
620
621 /* 1 if trap instructions should used for overflow rather than break
622 instructions. */
623 static int mips_trap = 0;
624
625 /* 1 if double width floating point constants should not be constructed
626 by assembling two single width halves into two single width floating
627 point registers which just happen to alias the double width destination
628 register. On some architectures this aliasing can be disabled by a bit
629 in the status register, and the setting of this bit cannot be determined
630 automatically at assemble time. */
631 static int mips_disable_float_construction;
632
633 /* Non-zero if any .set noreorder directives were used. */
634
635 static int mips_any_noreorder;
636
637 /* Non-zero if nops should be inserted when the register referenced in
638 an mfhi/mflo instruction is read in the next two instructions. */
639 static int mips_7000_hilo_fix;
640
641 /* The size of objects in the small data section. */
642 static unsigned int g_switch_value = 8;
643 /* Whether the -G option was used. */
644 static int g_switch_seen = 0;
645
646 #define N_RMASK 0xc4
647 #define N_VFP 0xd4
648
649 /* If we can determine in advance that GP optimization won't be
650 possible, we can skip the relaxation stuff that tries to produce
651 GP-relative references. This makes delay slot optimization work
652 better.
653
654 This function can only provide a guess, but it seems to work for
655 gcc output. It needs to guess right for gcc, otherwise gcc
656 will put what it thinks is a GP-relative instruction in a branch
657 delay slot.
658
659 I don't know if a fix is needed for the SVR4_PIC mode. I've only
660 fixed it for the non-PIC mode. KR 95/04/07 */
661 static int nopic_need_relax (symbolS *, int);
662
663 /* handle of the OPCODE hash table */
664 static struct hash_control *op_hash = NULL;
665
666 /* The opcode hash table we use for the mips16. */
667 static struct hash_control *mips16_op_hash = NULL;
668
669 /* The opcode hash table we use for the microMIPS ASE. */
670 static struct hash_control *micromips_op_hash = NULL;
671
672 /* This array holds the chars that always start a comment. If the
673 pre-processor is disabled, these aren't very useful */
674 const char comment_chars[] = "#";
675
676 /* This array holds the chars that only start a comment at the beginning of
677 a line. If the line seems to have the form '# 123 filename'
678 .line and .file directives will appear in the pre-processed output */
679 /* Note that input_file.c hand checks for '#' at the beginning of the
680 first line of the input file. This is because the compiler outputs
681 #NO_APP at the beginning of its output. */
682 /* Also note that C style comments are always supported. */
683 const char line_comment_chars[] = "#";
684
685 /* This array holds machine specific line separator characters. */
686 const char line_separator_chars[] = ";";
687
688 /* Chars that can be used to separate mant from exp in floating point nums */
689 const char EXP_CHARS[] = "eE";
690
691 /* Chars that mean this number is a floating point constant */
692 /* As in 0f12.456 */
693 /* or 0d1.2345e12 */
694 const char FLT_CHARS[] = "rRsSfFdDxXpP";
695
696 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
697 changed in read.c . Ideally it shouldn't have to know about it at all,
698 but nothing is ideal around here.
699 */
700
701 static char *insn_error;
702
703 static int auto_align = 1;
704
705 /* When outputting SVR4 PIC code, the assembler needs to know the
706 offset in the stack frame from which to restore the $gp register.
707 This is set by the .cprestore pseudo-op, and saved in this
708 variable. */
709 static offsetT mips_cprestore_offset = -1;
710
711 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
712 more optimizations, it can use a register value instead of a memory-saved
713 offset and even an other register than $gp as global pointer. */
714 static offsetT mips_cpreturn_offset = -1;
715 static int mips_cpreturn_register = -1;
716 static int mips_gp_register = GP;
717 static int mips_gprel_offset = 0;
718
719 /* Whether mips_cprestore_offset has been set in the current function
720 (or whether it has already been warned about, if not). */
721 static int mips_cprestore_valid = 0;
722
723 /* This is the register which holds the stack frame, as set by the
724 .frame pseudo-op. This is needed to implement .cprestore. */
725 static int mips_frame_reg = SP;
726
727 /* Whether mips_frame_reg has been set in the current function
728 (or whether it has already been warned about, if not). */
729 static int mips_frame_reg_valid = 0;
730
731 /* To output NOP instructions correctly, we need to keep information
732 about the previous two instructions. */
733
734 /* Whether we are optimizing. The default value of 2 means to remove
735 unneeded NOPs and swap branch instructions when possible. A value
736 of 1 means to not swap branches. A value of 0 means to always
737 insert NOPs. */
738 static int mips_optimize = 2;
739
740 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
741 equivalent to seeing no -g option at all. */
742 static int mips_debug = 0;
743
744 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
745 #define MAX_VR4130_NOPS 4
746
747 /* The maximum number of NOPs needed to fill delay slots. */
748 #define MAX_DELAY_NOPS 2
749
750 /* The maximum number of NOPs needed for any purpose. */
751 #define MAX_NOPS 4
752
753 /* A list of previous instructions, with index 0 being the most recent.
754 We need to look back MAX_NOPS instructions when filling delay slots
755 or working around processor errata. We need to look back one
756 instruction further if we're thinking about using history[0] to
757 fill a branch delay slot. */
758 static struct mips_cl_insn history[1 + MAX_NOPS];
759
760 /* Nop instructions used by emit_nop. */
761 static struct mips_cl_insn nop_insn;
762 static struct mips_cl_insn mips16_nop_insn;
763 static struct mips_cl_insn micromips_nop16_insn;
764 static struct mips_cl_insn micromips_nop32_insn;
765
766 /* The appropriate nop for the current mode. */
767 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn \
768 : (mips_opts.micromips ? &micromips_nop16_insn : &nop_insn))
769
770 /* The size of NOP_INSN in bytes. */
771 #define NOP_INSN_SIZE (HAVE_CODE_COMPRESSION ? 2 : 4)
772
773 /* If this is set, it points to a frag holding nop instructions which
774 were inserted before the start of a noreorder section. If those
775 nops turn out to be unnecessary, the size of the frag can be
776 decreased. */
777 static fragS *prev_nop_frag;
778
779 /* The number of nop instructions we created in prev_nop_frag. */
780 static int prev_nop_frag_holds;
781
782 /* The number of nop instructions that we know we need in
783 prev_nop_frag. */
784 static int prev_nop_frag_required;
785
786 /* The number of instructions we've seen since prev_nop_frag. */
787 static int prev_nop_frag_since;
788
789 /* For ECOFF and ELF, relocations against symbols are done in two
790 parts, with a HI relocation and a LO relocation. Each relocation
791 has only 16 bits of space to store an addend. This means that in
792 order for the linker to handle carries correctly, it must be able
793 to locate both the HI and the LO relocation. This means that the
794 relocations must appear in order in the relocation table.
795
796 In order to implement this, we keep track of each unmatched HI
797 relocation. We then sort them so that they immediately precede the
798 corresponding LO relocation. */
799
800 struct mips_hi_fixup
801 {
802 /* Next HI fixup. */
803 struct mips_hi_fixup *next;
804 /* This fixup. */
805 fixS *fixp;
806 /* The section this fixup is in. */
807 segT seg;
808 };
809
810 /* The list of unmatched HI relocs. */
811
812 static struct mips_hi_fixup *mips_hi_fixup_list;
813
814 /* The frag containing the last explicit relocation operator.
815 Null if explicit relocations have not been used. */
816
817 static fragS *prev_reloc_op_frag;
818
819 /* Map normal MIPS register numbers to mips16 register numbers. */
820
821 #define X ILLEGAL_REG
822 static const int mips32_to_16_reg_map[] =
823 {
824 X, X, 2, 3, 4, 5, 6, 7,
825 X, X, X, X, X, X, X, X,
826 0, 1, X, X, X, X, X, X,
827 X, X, X, X, X, X, X, X
828 };
829 #undef X
830
831 /* Map mips16 register numbers to normal MIPS register numbers. */
832
833 static const unsigned int mips16_to_32_reg_map[] =
834 {
835 16, 17, 2, 3, 4, 5, 6, 7
836 };
837
838 /* Map normal MIPS register numbers to microMIPS register numbers. */
839
840 #define mips32_to_micromips_reg_b_map mips32_to_16_reg_map
841 #define mips32_to_micromips_reg_c_map mips32_to_16_reg_map
842 #define mips32_to_micromips_reg_d_map mips32_to_16_reg_map
843 #define mips32_to_micromips_reg_e_map mips32_to_16_reg_map
844 #define mips32_to_micromips_reg_f_map mips32_to_16_reg_map
845 #define mips32_to_micromips_reg_g_map mips32_to_16_reg_map
846 #define mips32_to_micromips_reg_l_map mips32_to_16_reg_map
847
848 #define X ILLEGAL_REG
849 /* reg type h: 4, 5, 6. */
850 static const int mips32_to_micromips_reg_h_map[] =
851 {
852 X, X, X, X, 4, 5, 6, X,
853 X, X, X, X, X, X, X, X,
854 X, X, X, X, X, X, X, X,
855 X, X, X, X, X, X, X, X
856 };
857
858 /* reg type m: 0, 17, 2, 3, 16, 18, 19, 20. */
859 static const int mips32_to_micromips_reg_m_map[] =
860 {
861 0, X, 2, 3, X, X, X, X,
862 X, X, X, X, X, X, X, X,
863 4, 1, 5, 6, 7, X, X, X,
864 X, X, X, X, X, X, X, X
865 };
866
867 /* reg type q: 0, 2-7. 17. */
868 static const int mips32_to_micromips_reg_q_map[] =
869 {
870 0, X, 2, 3, 4, 5, 6, 7,
871 X, X, X, X, X, X, X, X,
872 X, 1, X, X, X, X, X, X,
873 X, X, X, X, X, X, X, X
874 };
875
876 #define mips32_to_micromips_reg_n_map mips32_to_micromips_reg_m_map
877 #undef X
878
879 /* Map microMIPS register numbers to normal MIPS register numbers. */
880
881 #define micromips_to_32_reg_b_map mips16_to_32_reg_map
882 #define micromips_to_32_reg_c_map mips16_to_32_reg_map
883 #define micromips_to_32_reg_d_map mips16_to_32_reg_map
884 #define micromips_to_32_reg_e_map mips16_to_32_reg_map
885 #define micromips_to_32_reg_f_map mips16_to_32_reg_map
886 #define micromips_to_32_reg_g_map mips16_to_32_reg_map
887
888 /* The microMIPS registers with type h. */
889 static const unsigned int micromips_to_32_reg_h_map[] =
890 {
891 5, 5, 6, 4, 4, 4, 4, 4
892 };
893
894 /* The microMIPS registers with type i. */
895 static const unsigned int micromips_to_32_reg_i_map[] =
896 {
897 6, 7, 7, 21, 22, 5, 6, 7
898 };
899
900 #define micromips_to_32_reg_l_map mips16_to_32_reg_map
901
902 /* The microMIPS registers with type m. */
903 static const unsigned int micromips_to_32_reg_m_map[] =
904 {
905 0, 17, 2, 3, 16, 18, 19, 20
906 };
907
908 #define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
909
910 /* The microMIPS registers with type q. */
911 static const unsigned int micromips_to_32_reg_q_map[] =
912 {
913 0, 17, 2, 3, 4, 5, 6, 7
914 };
915
916 /* microMIPS imm type B. */
917 static const int micromips_imm_b_map[] =
918 {
919 1, 4, 8, 12, 16, 20, 24, -1
920 };
921
922 /* microMIPS imm type C. */
923 static const int micromips_imm_c_map[] =
924 {
925 128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
926 };
927
928 /* Classifies the kind of instructions we're interested in when
929 implementing -mfix-vr4120. */
930 enum fix_vr4120_class
931 {
932 FIX_VR4120_MACC,
933 FIX_VR4120_DMACC,
934 FIX_VR4120_MULT,
935 FIX_VR4120_DMULT,
936 FIX_VR4120_DIV,
937 FIX_VR4120_MTHILO,
938 NUM_FIX_VR4120_CLASSES
939 };
940
941 /* ...likewise -mfix-loongson2f-jump. */
942 static bfd_boolean mips_fix_loongson2f_jump;
943
944 /* ...likewise -mfix-loongson2f-nop. */
945 static bfd_boolean mips_fix_loongson2f_nop;
946
947 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
948 static bfd_boolean mips_fix_loongson2f;
949
950 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
951 there must be at least one other instruction between an instruction
952 of type X and an instruction of type Y. */
953 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
954
955 /* True if -mfix-vr4120 is in force. */
956 static int mips_fix_vr4120;
957
958 /* ...likewise -mfix-vr4130. */
959 static int mips_fix_vr4130;
960
961 /* ...likewise -mfix-24k. */
962 static int mips_fix_24k;
963
964 /* ...likewise -mfix-cn63xxp1 */
965 static bfd_boolean mips_fix_cn63xxp1;
966
967 /* We don't relax branches by default, since this causes us to expand
968 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
969 fail to compute the offset before expanding the macro to the most
970 efficient expansion. */
971
972 static int mips_relax_branch;
973 \f
974 /* The expansion of many macros depends on the type of symbol that
975 they refer to. For example, when generating position-dependent code,
976 a macro that refers to a symbol may have two different expansions,
977 one which uses GP-relative addresses and one which uses absolute
978 addresses. When generating SVR4-style PIC, a macro may have
979 different expansions for local and global symbols.
980
981 We handle these situations by generating both sequences and putting
982 them in variant frags. In position-dependent code, the first sequence
983 will be the GP-relative one and the second sequence will be the
984 absolute one. In SVR4 PIC, the first sequence will be for global
985 symbols and the second will be for local symbols.
986
987 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
988 SECOND are the lengths of the two sequences in bytes. These fields
989 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
990 the subtype has the following flags:
991
992 RELAX_USE_SECOND
993 Set if it has been decided that we should use the second
994 sequence instead of the first.
995
996 RELAX_SECOND_LONGER
997 Set in the first variant frag if the macro's second implementation
998 is longer than its first. This refers to the macro as a whole,
999 not an individual relaxation.
1000
1001 RELAX_NOMACRO
1002 Set in the first variant frag if the macro appeared in a .set nomacro
1003 block and if one alternative requires a warning but the other does not.
1004
1005 RELAX_DELAY_SLOT
1006 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
1007 delay slot.
1008
1009 RELAX_DELAY_SLOT_16BIT
1010 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
1011 16-bit instruction.
1012
1013 RELAX_DELAY_SLOT_SIZE_FIRST
1014 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1015 the macro is of the wrong size for the branch delay slot.
1016
1017 RELAX_DELAY_SLOT_SIZE_SECOND
1018 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1019 the macro is of the wrong size for the branch delay slot.
1020
1021 The frag's "opcode" points to the first fixup for relaxable code.
1022
1023 Relaxable macros are generated using a sequence such as:
1024
1025 relax_start (SYMBOL);
1026 ... generate first expansion ...
1027 relax_switch ();
1028 ... generate second expansion ...
1029 relax_end ();
1030
1031 The code and fixups for the unwanted alternative are discarded
1032 by md_convert_frag. */
1033 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
1034
1035 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1036 #define RELAX_SECOND(X) ((X) & 0xff)
1037 #define RELAX_USE_SECOND 0x10000
1038 #define RELAX_SECOND_LONGER 0x20000
1039 #define RELAX_NOMACRO 0x40000
1040 #define RELAX_DELAY_SLOT 0x80000
1041 #define RELAX_DELAY_SLOT_16BIT 0x100000
1042 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
1043 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
1044
1045 /* Branch without likely bit. If label is out of range, we turn:
1046
1047 beq reg1, reg2, label
1048 delay slot
1049
1050 into
1051
1052 bne reg1, reg2, 0f
1053 nop
1054 j label
1055 0: delay slot
1056
1057 with the following opcode replacements:
1058
1059 beq <-> bne
1060 blez <-> bgtz
1061 bltz <-> bgez
1062 bc1f <-> bc1t
1063
1064 bltzal <-> bgezal (with jal label instead of j label)
1065
1066 Even though keeping the delay slot instruction in the delay slot of
1067 the branch would be more efficient, it would be very tricky to do
1068 correctly, because we'd have to introduce a variable frag *after*
1069 the delay slot instruction, and expand that instead. Let's do it
1070 the easy way for now, even if the branch-not-taken case now costs
1071 one additional instruction. Out-of-range branches are not supposed
1072 to be common, anyway.
1073
1074 Branch likely. If label is out of range, we turn:
1075
1076 beql reg1, reg2, label
1077 delay slot (annulled if branch not taken)
1078
1079 into
1080
1081 beql reg1, reg2, 1f
1082 nop
1083 beql $0, $0, 2f
1084 nop
1085 1: j[al] label
1086 delay slot (executed only if branch taken)
1087 2:
1088
1089 It would be possible to generate a shorter sequence by losing the
1090 likely bit, generating something like:
1091
1092 bne reg1, reg2, 0f
1093 nop
1094 j[al] label
1095 delay slot (executed only if branch taken)
1096 0:
1097
1098 beql -> bne
1099 bnel -> beq
1100 blezl -> bgtz
1101 bgtzl -> blez
1102 bltzl -> bgez
1103 bgezl -> bltz
1104 bc1fl -> bc1t
1105 bc1tl -> bc1f
1106
1107 bltzall -> bgezal (with jal label instead of j label)
1108 bgezall -> bltzal (ditto)
1109
1110
1111 but it's not clear that it would actually improve performance. */
1112 #define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1113 ((relax_substateT) \
1114 (0xc0000000 \
1115 | ((at) & 0x1f) \
1116 | ((toofar) ? 0x20 : 0) \
1117 | ((link) ? 0x40 : 0) \
1118 | ((likely) ? 0x80 : 0) \
1119 | ((uncond) ? 0x100 : 0)))
1120 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1121 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1122 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1123 #define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1124 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1125 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1126
1127 /* For mips16 code, we use an entirely different form of relaxation.
1128 mips16 supports two versions of most instructions which take
1129 immediate values: a small one which takes some small value, and a
1130 larger one which takes a 16 bit value. Since branches also follow
1131 this pattern, relaxing these values is required.
1132
1133 We can assemble both mips16 and normal MIPS code in a single
1134 object. Therefore, we need to support this type of relaxation at
1135 the same time that we support the relaxation described above. We
1136 use the high bit of the subtype field to distinguish these cases.
1137
1138 The information we store for this type of relaxation is the
1139 argument code found in the opcode file for this relocation, whether
1140 the user explicitly requested a small or extended form, and whether
1141 the relocation is in a jump or jal delay slot. That tells us the
1142 size of the value, and how it should be stored. We also store
1143 whether the fragment is considered to be extended or not. We also
1144 store whether this is known to be a branch to a different section,
1145 whether we have tried to relax this frag yet, and whether we have
1146 ever extended a PC relative fragment because of a shift count. */
1147 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1148 (0x80000000 \
1149 | ((type) & 0xff) \
1150 | ((small) ? 0x100 : 0) \
1151 | ((ext) ? 0x200 : 0) \
1152 | ((dslot) ? 0x400 : 0) \
1153 | ((jal_dslot) ? 0x800 : 0))
1154 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1155 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1156 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1157 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1158 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1159 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1160 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1161 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1162 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1163 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1164 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1165 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
1166
1167 /* For microMIPS code, we use relaxation similar to one we use for
1168 MIPS16 code. Some instructions that take immediate values support
1169 two encodings: a small one which takes some small value, and a
1170 larger one which takes a 16 bit value. As some branches also follow
1171 this pattern, relaxing these values is required.
1172
1173 We can assemble both microMIPS and normal MIPS code in a single
1174 object. Therefore, we need to support this type of relaxation at
1175 the same time that we support the relaxation described above. We
1176 use one of the high bits of the subtype field to distinguish these
1177 cases.
1178
1179 The information we store for this type of relaxation is the argument
1180 code found in the opcode file for this relocation, the register
1181 selected as the assembler temporary, whether the branch is
1182 unconditional, whether it is compact, whether it stores the link
1183 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1184 branches to a sequence of instructions is enabled, and whether the
1185 displacement of a branch is too large to fit as an immediate argument
1186 of a 16-bit and a 32-bit branch, respectively. */
1187 #define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1188 relax32, toofar16, toofar32) \
1189 (0x40000000 \
1190 | ((type) & 0xff) \
1191 | (((at) & 0x1f) << 8) \
1192 | ((uncond) ? 0x2000 : 0) \
1193 | ((compact) ? 0x4000 : 0) \
1194 | ((link) ? 0x8000 : 0) \
1195 | ((relax32) ? 0x10000 : 0) \
1196 | ((toofar16) ? 0x20000 : 0) \
1197 | ((toofar32) ? 0x40000 : 0))
1198 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1199 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1200 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1201 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1202 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1203 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1204 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1205
1206 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1207 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1208 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1209 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1210 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1211 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
1212
1213 /* Sign-extend 16-bit value X. */
1214 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1215
1216 /* Is the given value a sign-extended 32-bit value? */
1217 #define IS_SEXT_32BIT_NUM(x) \
1218 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1219 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1220
1221 /* Is the given value a sign-extended 16-bit value? */
1222 #define IS_SEXT_16BIT_NUM(x) \
1223 (((x) &~ (offsetT) 0x7fff) == 0 \
1224 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1225
1226 /* Is the given value a sign-extended 12-bit value? */
1227 #define IS_SEXT_12BIT_NUM(x) \
1228 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1229
1230 /* Is the given value a sign-extended 9-bit value? */
1231 #define IS_SEXT_9BIT_NUM(x) \
1232 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1233
1234 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
1235 #define IS_ZEXT_32BIT_NUM(x) \
1236 (((x) &~ (offsetT) 0xffffffff) == 0 \
1237 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1238
1239 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1240 VALUE << SHIFT. VALUE is evaluated exactly once. */
1241 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1242 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1243 | (((VALUE) & (MASK)) << (SHIFT)))
1244
1245 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1246 SHIFT places. */
1247 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1248 (((STRUCT) >> (SHIFT)) & (MASK))
1249
1250 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1251 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1252
1253 include/opcode/mips.h specifies operand fields using the macros
1254 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
1255 with "MIPS16OP" instead of "OP". */
1256 #define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1257 do \
1258 if (!(MICROMIPS)) \
1259 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1260 OP_MASK_##FIELD, OP_SH_##FIELD); \
1261 else \
1262 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1263 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1264 while (0)
1265 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1266 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1267 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1268
1269 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
1270 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1271 (!(MICROMIPS) \
1272 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1273 : EXTRACT_BITS ((INSN).insn_opcode, \
1274 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1275 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1276 EXTRACT_BITS ((INSN).insn_opcode, \
1277 MIPS16OP_MASK_##FIELD, \
1278 MIPS16OP_SH_##FIELD)
1279
1280 /* The MIPS16 EXTEND opcode, shifted left 16 places. */
1281 #define MIPS16_EXTEND (0xf000U << 16)
1282 \f
1283 /* Whether or not we are emitting a branch-likely macro. */
1284 static bfd_boolean emit_branch_likely_macro = FALSE;
1285
1286 /* Global variables used when generating relaxable macros. See the
1287 comment above RELAX_ENCODE for more details about how relaxation
1288 is used. */
1289 static struct {
1290 /* 0 if we're not emitting a relaxable macro.
1291 1 if we're emitting the first of the two relaxation alternatives.
1292 2 if we're emitting the second alternative. */
1293 int sequence;
1294
1295 /* The first relaxable fixup in the current frag. (In other words,
1296 the first fixup that refers to relaxable code.) */
1297 fixS *first_fixup;
1298
1299 /* sizes[0] says how many bytes of the first alternative are stored in
1300 the current frag. Likewise sizes[1] for the second alternative. */
1301 unsigned int sizes[2];
1302
1303 /* The symbol on which the choice of sequence depends. */
1304 symbolS *symbol;
1305 } mips_relax;
1306 \f
1307 /* Global variables used to decide whether a macro needs a warning. */
1308 static struct {
1309 /* True if the macro is in a branch delay slot. */
1310 bfd_boolean delay_slot_p;
1311
1312 /* Set to the length in bytes required if the macro is in a delay slot
1313 that requires a specific length of instruction, otherwise zero. */
1314 unsigned int delay_slot_length;
1315
1316 /* For relaxable macros, sizes[0] is the length of the first alternative
1317 in bytes and sizes[1] is the length of the second alternative.
1318 For non-relaxable macros, both elements give the length of the
1319 macro in bytes. */
1320 unsigned int sizes[2];
1321
1322 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1323 instruction of the first alternative in bytes and first_insn_sizes[1]
1324 is the length of the first instruction of the second alternative.
1325 For non-relaxable macros, both elements give the length of the first
1326 instruction in bytes.
1327
1328 Set to zero if we haven't yet seen the first instruction. */
1329 unsigned int first_insn_sizes[2];
1330
1331 /* For relaxable macros, insns[0] is the number of instructions for the
1332 first alternative and insns[1] is the number of instructions for the
1333 second alternative.
1334
1335 For non-relaxable macros, both elements give the number of
1336 instructions for the macro. */
1337 unsigned int insns[2];
1338
1339 /* The first variant frag for this macro. */
1340 fragS *first_frag;
1341 } mips_macro_warning;
1342 \f
1343 /* Prototypes for static functions. */
1344
1345 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1346
1347 static void append_insn
1348 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1349 bfd_boolean expansionp);
1350 static void mips_no_prev_insn (void);
1351 static void macro_build (expressionS *, const char *, const char *, ...);
1352 static void mips16_macro_build
1353 (expressionS *, const char *, const char *, va_list *);
1354 static void load_register (int, expressionS *, int);
1355 static void macro_start (void);
1356 static void macro_end (void);
1357 static void macro (struct mips_cl_insn * ip);
1358 static void mips16_macro (struct mips_cl_insn * ip);
1359 static void mips_ip (char *str, struct mips_cl_insn * ip);
1360 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1361 static void mips16_immed
1362 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1363 unsigned int, unsigned long *);
1364 static size_t my_getSmallExpression
1365 (expressionS *, bfd_reloc_code_real_type *, char *);
1366 static void my_getExpression (expressionS *, char *);
1367 static void s_align (int);
1368 static void s_change_sec (int);
1369 static void s_change_section (int);
1370 static void s_cons (int);
1371 static void s_float_cons (int);
1372 static void s_mips_globl (int);
1373 static void s_option (int);
1374 static void s_mipsset (int);
1375 static void s_abicalls (int);
1376 static void s_cpload (int);
1377 static void s_cpsetup (int);
1378 static void s_cplocal (int);
1379 static void s_cprestore (int);
1380 static void s_cpreturn (int);
1381 static void s_dtprelword (int);
1382 static void s_dtpreldword (int);
1383 static void s_tprelword (int);
1384 static void s_tpreldword (int);
1385 static void s_gpvalue (int);
1386 static void s_gpword (int);
1387 static void s_gpdword (int);
1388 static void s_ehword (int);
1389 static void s_cpadd (int);
1390 static void s_insn (int);
1391 static void md_obj_begin (void);
1392 static void md_obj_end (void);
1393 static void s_mips_ent (int);
1394 static void s_mips_end (int);
1395 static void s_mips_frame (int);
1396 static void s_mips_mask (int reg_type);
1397 static void s_mips_stab (int);
1398 static void s_mips_weakext (int);
1399 static void s_mips_file (int);
1400 static void s_mips_loc (int);
1401 static bfd_boolean pic_need_relax (symbolS *, asection *);
1402 static int relaxed_branch_length (fragS *, asection *, int);
1403 static int validate_mips_insn (const struct mips_opcode *);
1404 static int validate_micromips_insn (const struct mips_opcode *);
1405 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1406 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1407
1408 /* Table and functions used to map between CPU/ISA names, and
1409 ISA levels, and CPU numbers. */
1410
1411 struct mips_cpu_info
1412 {
1413 const char *name; /* CPU or ISA name. */
1414 int flags; /* MIPS_CPU_* flags. */
1415 int ase; /* Set of ASEs implemented by the CPU. */
1416 int isa; /* ISA level. */
1417 int cpu; /* CPU number (default CPU if ISA). */
1418 };
1419
1420 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1421
1422 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1423 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1424 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1425 \f
1426 /* Pseudo-op table.
1427
1428 The following pseudo-ops from the Kane and Heinrich MIPS book
1429 should be defined here, but are currently unsupported: .alias,
1430 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1431
1432 The following pseudo-ops from the Kane and Heinrich MIPS book are
1433 specific to the type of debugging information being generated, and
1434 should be defined by the object format: .aent, .begin, .bend,
1435 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1436 .vreg.
1437
1438 The following pseudo-ops from the Kane and Heinrich MIPS book are
1439 not MIPS CPU specific, but are also not specific to the object file
1440 format. This file is probably the best place to define them, but
1441 they are not currently supported: .asm0, .endr, .lab, .struct. */
1442
1443 static const pseudo_typeS mips_pseudo_table[] =
1444 {
1445 /* MIPS specific pseudo-ops. */
1446 {"option", s_option, 0},
1447 {"set", s_mipsset, 0},
1448 {"rdata", s_change_sec, 'r'},
1449 {"sdata", s_change_sec, 's'},
1450 {"livereg", s_ignore, 0},
1451 {"abicalls", s_abicalls, 0},
1452 {"cpload", s_cpload, 0},
1453 {"cpsetup", s_cpsetup, 0},
1454 {"cplocal", s_cplocal, 0},
1455 {"cprestore", s_cprestore, 0},
1456 {"cpreturn", s_cpreturn, 0},
1457 {"dtprelword", s_dtprelword, 0},
1458 {"dtpreldword", s_dtpreldword, 0},
1459 {"tprelword", s_tprelword, 0},
1460 {"tpreldword", s_tpreldword, 0},
1461 {"gpvalue", s_gpvalue, 0},
1462 {"gpword", s_gpword, 0},
1463 {"gpdword", s_gpdword, 0},
1464 {"ehword", s_ehword, 0},
1465 {"cpadd", s_cpadd, 0},
1466 {"insn", s_insn, 0},
1467
1468 /* Relatively generic pseudo-ops that happen to be used on MIPS
1469 chips. */
1470 {"asciiz", stringer, 8 + 1},
1471 {"bss", s_change_sec, 'b'},
1472 {"err", s_err, 0},
1473 {"half", s_cons, 1},
1474 {"dword", s_cons, 3},
1475 {"weakext", s_mips_weakext, 0},
1476 {"origin", s_org, 0},
1477 {"repeat", s_rept, 0},
1478
1479 /* For MIPS this is non-standard, but we define it for consistency. */
1480 {"sbss", s_change_sec, 'B'},
1481
1482 /* These pseudo-ops are defined in read.c, but must be overridden
1483 here for one reason or another. */
1484 {"align", s_align, 0},
1485 {"byte", s_cons, 0},
1486 {"data", s_change_sec, 'd'},
1487 {"double", s_float_cons, 'd'},
1488 {"float", s_float_cons, 'f'},
1489 {"globl", s_mips_globl, 0},
1490 {"global", s_mips_globl, 0},
1491 {"hword", s_cons, 1},
1492 {"int", s_cons, 2},
1493 {"long", s_cons, 2},
1494 {"octa", s_cons, 4},
1495 {"quad", s_cons, 3},
1496 {"section", s_change_section, 0},
1497 {"short", s_cons, 1},
1498 {"single", s_float_cons, 'f'},
1499 {"stabd", s_mips_stab, 'd'},
1500 {"stabn", s_mips_stab, 'n'},
1501 {"stabs", s_mips_stab, 's'},
1502 {"text", s_change_sec, 't'},
1503 {"word", s_cons, 2},
1504
1505 { "extern", ecoff_directive_extern, 0},
1506
1507 { NULL, NULL, 0 },
1508 };
1509
1510 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1511 {
1512 /* These pseudo-ops should be defined by the object file format.
1513 However, a.out doesn't support them, so we have versions here. */
1514 {"aent", s_mips_ent, 1},
1515 {"bgnb", s_ignore, 0},
1516 {"end", s_mips_end, 0},
1517 {"endb", s_ignore, 0},
1518 {"ent", s_mips_ent, 0},
1519 {"file", s_mips_file, 0},
1520 {"fmask", s_mips_mask, 'F'},
1521 {"frame", s_mips_frame, 0},
1522 {"loc", s_mips_loc, 0},
1523 {"mask", s_mips_mask, 'R'},
1524 {"verstamp", s_ignore, 0},
1525 { NULL, NULL, 0 },
1526 };
1527
1528 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1529 purpose of the `.dc.a' internal pseudo-op. */
1530
1531 int
1532 mips_address_bytes (void)
1533 {
1534 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1535 }
1536
1537 extern void pop_insert (const pseudo_typeS *);
1538
1539 void
1540 mips_pop_insert (void)
1541 {
1542 pop_insert (mips_pseudo_table);
1543 if (! ECOFF_DEBUGGING)
1544 pop_insert (mips_nonecoff_pseudo_table);
1545 }
1546 \f
1547 /* Symbols labelling the current insn. */
1548
1549 struct insn_label_list
1550 {
1551 struct insn_label_list *next;
1552 symbolS *label;
1553 };
1554
1555 static struct insn_label_list *free_insn_labels;
1556 #define label_list tc_segment_info_data.labels
1557
1558 static void mips_clear_insn_labels (void);
1559 static void mips_mark_labels (void);
1560 static void mips_compressed_mark_labels (void);
1561
1562 static inline void
1563 mips_clear_insn_labels (void)
1564 {
1565 register struct insn_label_list **pl;
1566 segment_info_type *si;
1567
1568 if (now_seg)
1569 {
1570 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1571 ;
1572
1573 si = seg_info (now_seg);
1574 *pl = si->label_list;
1575 si->label_list = NULL;
1576 }
1577 }
1578
1579 /* Mark instruction labels in MIPS16/microMIPS mode. */
1580
1581 static inline void
1582 mips_mark_labels (void)
1583 {
1584 if (HAVE_CODE_COMPRESSION)
1585 mips_compressed_mark_labels ();
1586 }
1587 \f
1588 static char *expr_end;
1589
1590 /* Expressions which appear in instructions. These are set by
1591 mips_ip. */
1592
1593 static expressionS imm_expr;
1594 static expressionS imm2_expr;
1595 static expressionS offset_expr;
1596
1597 /* Relocs associated with imm_expr and offset_expr. */
1598
1599 static bfd_reloc_code_real_type imm_reloc[3]
1600 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1601 static bfd_reloc_code_real_type offset_reloc[3]
1602 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1603
1604 /* This is set to the resulting size of the instruction to be produced
1605 by mips16_ip if an explicit extension is used or by mips_ip if an
1606 explicit size is supplied. */
1607
1608 static unsigned int forced_insn_length;
1609
1610 /* True if we are assembling an instruction. All dot symbols defined during
1611 this time should be treated as code labels. */
1612
1613 static bfd_boolean mips_assembling_insn;
1614
1615 #ifdef OBJ_ELF
1616 /* The pdr segment for per procedure frame/regmask info. Not used for
1617 ECOFF debugging. */
1618
1619 static segT pdr_seg;
1620 #endif
1621
1622 /* The default target format to use. */
1623
1624 #if defined (TE_FreeBSD)
1625 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1626 #elif defined (TE_TMIPS)
1627 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1628 #else
1629 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1630 #endif
1631
1632 const char *
1633 mips_target_format (void)
1634 {
1635 switch (OUTPUT_FLAVOR)
1636 {
1637 case bfd_target_ecoff_flavour:
1638 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1639 case bfd_target_coff_flavour:
1640 return "pe-mips";
1641 case bfd_target_elf_flavour:
1642 #ifdef TE_VXWORKS
1643 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1644 return (target_big_endian
1645 ? "elf32-bigmips-vxworks"
1646 : "elf32-littlemips-vxworks");
1647 #endif
1648 return (target_big_endian
1649 ? (HAVE_64BIT_OBJECTS
1650 ? ELF_TARGET ("elf64-", "big")
1651 : (HAVE_NEWABI
1652 ? ELF_TARGET ("elf32-n", "big")
1653 : ELF_TARGET ("elf32-", "big")))
1654 : (HAVE_64BIT_OBJECTS
1655 ? ELF_TARGET ("elf64-", "little")
1656 : (HAVE_NEWABI
1657 ? ELF_TARGET ("elf32-n", "little")
1658 : ELF_TARGET ("elf32-", "little"))));
1659 default:
1660 abort ();
1661 return NULL;
1662 }
1663 }
1664
1665 /* Return the length of a microMIPS instruction in bytes. If bits of
1666 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1667 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1668 major opcode) will require further modifications to the opcode
1669 table. */
1670
1671 static inline unsigned int
1672 micromips_insn_length (const struct mips_opcode *mo)
1673 {
1674 return (mo->mask >> 16) == 0 ? 2 : 4;
1675 }
1676
1677 /* Return the length of MIPS16 instruction OPCODE. */
1678
1679 static inline unsigned int
1680 mips16_opcode_length (unsigned long opcode)
1681 {
1682 return (opcode >> 16) == 0 ? 2 : 4;
1683 }
1684
1685 /* Return the length of instruction INSN. */
1686
1687 static inline unsigned int
1688 insn_length (const struct mips_cl_insn *insn)
1689 {
1690 if (mips_opts.micromips)
1691 return micromips_insn_length (insn->insn_mo);
1692 else if (mips_opts.mips16)
1693 return mips16_opcode_length (insn->insn_opcode);
1694 else
1695 return 4;
1696 }
1697
1698 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1699
1700 static void
1701 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1702 {
1703 size_t i;
1704
1705 insn->insn_mo = mo;
1706 insn->insn_opcode = mo->match;
1707 insn->frag = NULL;
1708 insn->where = 0;
1709 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1710 insn->fixp[i] = NULL;
1711 insn->fixed_p = (mips_opts.noreorder > 0);
1712 insn->noreorder_p = (mips_opts.noreorder > 0);
1713 insn->mips16_absolute_jump_p = 0;
1714 insn->complete_p = 0;
1715 insn->cleared_p = 0;
1716 }
1717
1718 /* Record the current MIPS16/microMIPS mode in now_seg. */
1719
1720 static void
1721 mips_record_compressed_mode (void)
1722 {
1723 segment_info_type *si;
1724
1725 si = seg_info (now_seg);
1726 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1727 si->tc_segment_info_data.mips16 = mips_opts.mips16;
1728 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
1729 si->tc_segment_info_data.micromips = mips_opts.micromips;
1730 }
1731
1732 /* Read a standard MIPS instruction from BUF. */
1733
1734 static unsigned long
1735 read_insn (char *buf)
1736 {
1737 if (target_big_endian)
1738 return bfd_getb32 ((bfd_byte *) buf);
1739 else
1740 return bfd_getl32 ((bfd_byte *) buf);
1741 }
1742
1743 /* Write standard MIPS instruction INSN to BUF. Return a pointer to
1744 the next byte. */
1745
1746 static char *
1747 write_insn (char *buf, unsigned int insn)
1748 {
1749 md_number_to_chars (buf, insn, 4);
1750 return buf + 4;
1751 }
1752
1753 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
1754 has length LENGTH. */
1755
1756 static unsigned long
1757 read_compressed_insn (char *buf, unsigned int length)
1758 {
1759 unsigned long insn;
1760 unsigned int i;
1761
1762 insn = 0;
1763 for (i = 0; i < length; i += 2)
1764 {
1765 insn <<= 16;
1766 if (target_big_endian)
1767 insn |= bfd_getb16 ((char *) buf);
1768 else
1769 insn |= bfd_getl16 ((char *) buf);
1770 buf += 2;
1771 }
1772 return insn;
1773 }
1774
1775 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
1776 instruction is LENGTH bytes long. Return a pointer to the next byte. */
1777
1778 static char *
1779 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
1780 {
1781 unsigned int i;
1782
1783 for (i = 0; i < length; i += 2)
1784 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
1785 return buf + length;
1786 }
1787
1788 /* Install INSN at the location specified by its "frag" and "where" fields. */
1789
1790 static void
1791 install_insn (const struct mips_cl_insn *insn)
1792 {
1793 char *f = insn->frag->fr_literal + insn->where;
1794 if (HAVE_CODE_COMPRESSION)
1795 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1796 else
1797 write_insn (f, insn->insn_opcode);
1798 mips_record_compressed_mode ();
1799 }
1800
1801 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1802 and install the opcode in the new location. */
1803
1804 static void
1805 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1806 {
1807 size_t i;
1808
1809 insn->frag = frag;
1810 insn->where = where;
1811 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1812 if (insn->fixp[i] != NULL)
1813 {
1814 insn->fixp[i]->fx_frag = frag;
1815 insn->fixp[i]->fx_where = where;
1816 }
1817 install_insn (insn);
1818 }
1819
1820 /* Add INSN to the end of the output. */
1821
1822 static void
1823 add_fixed_insn (struct mips_cl_insn *insn)
1824 {
1825 char *f = frag_more (insn_length (insn));
1826 move_insn (insn, frag_now, f - frag_now->fr_literal);
1827 }
1828
1829 /* Start a variant frag and move INSN to the start of the variant part,
1830 marking it as fixed. The other arguments are as for frag_var. */
1831
1832 static void
1833 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1834 relax_substateT subtype, symbolS *symbol, offsetT offset)
1835 {
1836 frag_grow (max_chars);
1837 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1838 insn->fixed_p = 1;
1839 frag_var (rs_machine_dependent, max_chars, var,
1840 subtype, symbol, offset, NULL);
1841 }
1842
1843 /* Insert N copies of INSN into the history buffer, starting at
1844 position FIRST. Neither FIRST nor N need to be clipped. */
1845
1846 static void
1847 insert_into_history (unsigned int first, unsigned int n,
1848 const struct mips_cl_insn *insn)
1849 {
1850 if (mips_relax.sequence != 2)
1851 {
1852 unsigned int i;
1853
1854 for (i = ARRAY_SIZE (history); i-- > first;)
1855 if (i >= first + n)
1856 history[i] = history[i - n];
1857 else
1858 history[i] = *insn;
1859 }
1860 }
1861
1862 /* Initialize vr4120_conflicts. There is a bit of duplication here:
1863 the idea is to make it obvious at a glance that each errata is
1864 included. */
1865
1866 static void
1867 init_vr4120_conflicts (void)
1868 {
1869 #define CONFLICT(FIRST, SECOND) \
1870 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1871
1872 /* Errata 21 - [D]DIV[U] after [D]MACC */
1873 CONFLICT (MACC, DIV);
1874 CONFLICT (DMACC, DIV);
1875
1876 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1877 CONFLICT (DMULT, DMULT);
1878 CONFLICT (DMULT, DMACC);
1879 CONFLICT (DMACC, DMULT);
1880 CONFLICT (DMACC, DMACC);
1881
1882 /* Errata 24 - MT{LO,HI} after [D]MACC */
1883 CONFLICT (MACC, MTHILO);
1884 CONFLICT (DMACC, MTHILO);
1885
1886 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1887 instruction is executed immediately after a MACC or DMACC
1888 instruction, the result of [either instruction] is incorrect." */
1889 CONFLICT (MACC, MULT);
1890 CONFLICT (MACC, DMULT);
1891 CONFLICT (DMACC, MULT);
1892 CONFLICT (DMACC, DMULT);
1893
1894 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1895 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1896 DDIV or DDIVU instruction, the result of the MACC or
1897 DMACC instruction is incorrect.". */
1898 CONFLICT (DMULT, MACC);
1899 CONFLICT (DMULT, DMACC);
1900 CONFLICT (DIV, MACC);
1901 CONFLICT (DIV, DMACC);
1902
1903 #undef CONFLICT
1904 }
1905
1906 struct regname {
1907 const char *name;
1908 unsigned int num;
1909 };
1910
1911 #define RTYPE_MASK 0x1ff00
1912 #define RTYPE_NUM 0x00100
1913 #define RTYPE_FPU 0x00200
1914 #define RTYPE_FCC 0x00400
1915 #define RTYPE_VEC 0x00800
1916 #define RTYPE_GP 0x01000
1917 #define RTYPE_CP0 0x02000
1918 #define RTYPE_PC 0x04000
1919 #define RTYPE_ACC 0x08000
1920 #define RTYPE_CCC 0x10000
1921 #define RNUM_MASK 0x000ff
1922 #define RWARN 0x80000
1923
1924 #define GENERIC_REGISTER_NUMBERS \
1925 {"$0", RTYPE_NUM | 0}, \
1926 {"$1", RTYPE_NUM | 1}, \
1927 {"$2", RTYPE_NUM | 2}, \
1928 {"$3", RTYPE_NUM | 3}, \
1929 {"$4", RTYPE_NUM | 4}, \
1930 {"$5", RTYPE_NUM | 5}, \
1931 {"$6", RTYPE_NUM | 6}, \
1932 {"$7", RTYPE_NUM | 7}, \
1933 {"$8", RTYPE_NUM | 8}, \
1934 {"$9", RTYPE_NUM | 9}, \
1935 {"$10", RTYPE_NUM | 10}, \
1936 {"$11", RTYPE_NUM | 11}, \
1937 {"$12", RTYPE_NUM | 12}, \
1938 {"$13", RTYPE_NUM | 13}, \
1939 {"$14", RTYPE_NUM | 14}, \
1940 {"$15", RTYPE_NUM | 15}, \
1941 {"$16", RTYPE_NUM | 16}, \
1942 {"$17", RTYPE_NUM | 17}, \
1943 {"$18", RTYPE_NUM | 18}, \
1944 {"$19", RTYPE_NUM | 19}, \
1945 {"$20", RTYPE_NUM | 20}, \
1946 {"$21", RTYPE_NUM | 21}, \
1947 {"$22", RTYPE_NUM | 22}, \
1948 {"$23", RTYPE_NUM | 23}, \
1949 {"$24", RTYPE_NUM | 24}, \
1950 {"$25", RTYPE_NUM | 25}, \
1951 {"$26", RTYPE_NUM | 26}, \
1952 {"$27", RTYPE_NUM | 27}, \
1953 {"$28", RTYPE_NUM | 28}, \
1954 {"$29", RTYPE_NUM | 29}, \
1955 {"$30", RTYPE_NUM | 30}, \
1956 {"$31", RTYPE_NUM | 31}
1957
1958 #define FPU_REGISTER_NAMES \
1959 {"$f0", RTYPE_FPU | 0}, \
1960 {"$f1", RTYPE_FPU | 1}, \
1961 {"$f2", RTYPE_FPU | 2}, \
1962 {"$f3", RTYPE_FPU | 3}, \
1963 {"$f4", RTYPE_FPU | 4}, \
1964 {"$f5", RTYPE_FPU | 5}, \
1965 {"$f6", RTYPE_FPU | 6}, \
1966 {"$f7", RTYPE_FPU | 7}, \
1967 {"$f8", RTYPE_FPU | 8}, \
1968 {"$f9", RTYPE_FPU | 9}, \
1969 {"$f10", RTYPE_FPU | 10}, \
1970 {"$f11", RTYPE_FPU | 11}, \
1971 {"$f12", RTYPE_FPU | 12}, \
1972 {"$f13", RTYPE_FPU | 13}, \
1973 {"$f14", RTYPE_FPU | 14}, \
1974 {"$f15", RTYPE_FPU | 15}, \
1975 {"$f16", RTYPE_FPU | 16}, \
1976 {"$f17", RTYPE_FPU | 17}, \
1977 {"$f18", RTYPE_FPU | 18}, \
1978 {"$f19", RTYPE_FPU | 19}, \
1979 {"$f20", RTYPE_FPU | 20}, \
1980 {"$f21", RTYPE_FPU | 21}, \
1981 {"$f22", RTYPE_FPU | 22}, \
1982 {"$f23", RTYPE_FPU | 23}, \
1983 {"$f24", RTYPE_FPU | 24}, \
1984 {"$f25", RTYPE_FPU | 25}, \
1985 {"$f26", RTYPE_FPU | 26}, \
1986 {"$f27", RTYPE_FPU | 27}, \
1987 {"$f28", RTYPE_FPU | 28}, \
1988 {"$f29", RTYPE_FPU | 29}, \
1989 {"$f30", RTYPE_FPU | 30}, \
1990 {"$f31", RTYPE_FPU | 31}
1991
1992 #define FPU_CONDITION_CODE_NAMES \
1993 {"$fcc0", RTYPE_FCC | 0}, \
1994 {"$fcc1", RTYPE_FCC | 1}, \
1995 {"$fcc2", RTYPE_FCC | 2}, \
1996 {"$fcc3", RTYPE_FCC | 3}, \
1997 {"$fcc4", RTYPE_FCC | 4}, \
1998 {"$fcc5", RTYPE_FCC | 5}, \
1999 {"$fcc6", RTYPE_FCC | 6}, \
2000 {"$fcc7", RTYPE_FCC | 7}
2001
2002 #define COPROC_CONDITION_CODE_NAMES \
2003 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2004 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2005 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2006 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2007 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2008 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2009 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2010 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2011
2012 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2013 {"$a4", RTYPE_GP | 8}, \
2014 {"$a5", RTYPE_GP | 9}, \
2015 {"$a6", RTYPE_GP | 10}, \
2016 {"$a7", RTYPE_GP | 11}, \
2017 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2018 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2019 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2020 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2021 {"$t0", RTYPE_GP | 12}, \
2022 {"$t1", RTYPE_GP | 13}, \
2023 {"$t2", RTYPE_GP | 14}, \
2024 {"$t3", RTYPE_GP | 15}
2025
2026 #define O32_SYMBOLIC_REGISTER_NAMES \
2027 {"$t0", RTYPE_GP | 8}, \
2028 {"$t1", RTYPE_GP | 9}, \
2029 {"$t2", RTYPE_GP | 10}, \
2030 {"$t3", RTYPE_GP | 11}, \
2031 {"$t4", RTYPE_GP | 12}, \
2032 {"$t5", RTYPE_GP | 13}, \
2033 {"$t6", RTYPE_GP | 14}, \
2034 {"$t7", RTYPE_GP | 15}, \
2035 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2036 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2037 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2038 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2039
2040 /* Remaining symbolic register names */
2041 #define SYMBOLIC_REGISTER_NAMES \
2042 {"$zero", RTYPE_GP | 0}, \
2043 {"$at", RTYPE_GP | 1}, \
2044 {"$AT", RTYPE_GP | 1}, \
2045 {"$v0", RTYPE_GP | 2}, \
2046 {"$v1", RTYPE_GP | 3}, \
2047 {"$a0", RTYPE_GP | 4}, \
2048 {"$a1", RTYPE_GP | 5}, \
2049 {"$a2", RTYPE_GP | 6}, \
2050 {"$a3", RTYPE_GP | 7}, \
2051 {"$s0", RTYPE_GP | 16}, \
2052 {"$s1", RTYPE_GP | 17}, \
2053 {"$s2", RTYPE_GP | 18}, \
2054 {"$s3", RTYPE_GP | 19}, \
2055 {"$s4", RTYPE_GP | 20}, \
2056 {"$s5", RTYPE_GP | 21}, \
2057 {"$s6", RTYPE_GP | 22}, \
2058 {"$s7", RTYPE_GP | 23}, \
2059 {"$t8", RTYPE_GP | 24}, \
2060 {"$t9", RTYPE_GP | 25}, \
2061 {"$k0", RTYPE_GP | 26}, \
2062 {"$kt0", RTYPE_GP | 26}, \
2063 {"$k1", RTYPE_GP | 27}, \
2064 {"$kt1", RTYPE_GP | 27}, \
2065 {"$gp", RTYPE_GP | 28}, \
2066 {"$sp", RTYPE_GP | 29}, \
2067 {"$s8", RTYPE_GP | 30}, \
2068 {"$fp", RTYPE_GP | 30}, \
2069 {"$ra", RTYPE_GP | 31}
2070
2071 #define MIPS16_SPECIAL_REGISTER_NAMES \
2072 {"$pc", RTYPE_PC | 0}
2073
2074 #define MDMX_VECTOR_REGISTER_NAMES \
2075 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2076 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2077 {"$v2", RTYPE_VEC | 2}, \
2078 {"$v3", RTYPE_VEC | 3}, \
2079 {"$v4", RTYPE_VEC | 4}, \
2080 {"$v5", RTYPE_VEC | 5}, \
2081 {"$v6", RTYPE_VEC | 6}, \
2082 {"$v7", RTYPE_VEC | 7}, \
2083 {"$v8", RTYPE_VEC | 8}, \
2084 {"$v9", RTYPE_VEC | 9}, \
2085 {"$v10", RTYPE_VEC | 10}, \
2086 {"$v11", RTYPE_VEC | 11}, \
2087 {"$v12", RTYPE_VEC | 12}, \
2088 {"$v13", RTYPE_VEC | 13}, \
2089 {"$v14", RTYPE_VEC | 14}, \
2090 {"$v15", RTYPE_VEC | 15}, \
2091 {"$v16", RTYPE_VEC | 16}, \
2092 {"$v17", RTYPE_VEC | 17}, \
2093 {"$v18", RTYPE_VEC | 18}, \
2094 {"$v19", RTYPE_VEC | 19}, \
2095 {"$v20", RTYPE_VEC | 20}, \
2096 {"$v21", RTYPE_VEC | 21}, \
2097 {"$v22", RTYPE_VEC | 22}, \
2098 {"$v23", RTYPE_VEC | 23}, \
2099 {"$v24", RTYPE_VEC | 24}, \
2100 {"$v25", RTYPE_VEC | 25}, \
2101 {"$v26", RTYPE_VEC | 26}, \
2102 {"$v27", RTYPE_VEC | 27}, \
2103 {"$v28", RTYPE_VEC | 28}, \
2104 {"$v29", RTYPE_VEC | 29}, \
2105 {"$v30", RTYPE_VEC | 30}, \
2106 {"$v31", RTYPE_VEC | 31}
2107
2108 #define MIPS_DSP_ACCUMULATOR_NAMES \
2109 {"$ac0", RTYPE_ACC | 0}, \
2110 {"$ac1", RTYPE_ACC | 1}, \
2111 {"$ac2", RTYPE_ACC | 2}, \
2112 {"$ac3", RTYPE_ACC | 3}
2113
2114 static const struct regname reg_names[] = {
2115 GENERIC_REGISTER_NUMBERS,
2116 FPU_REGISTER_NAMES,
2117 FPU_CONDITION_CODE_NAMES,
2118 COPROC_CONDITION_CODE_NAMES,
2119
2120 /* The $txx registers depends on the abi,
2121 these will be added later into the symbol table from
2122 one of the tables below once mips_abi is set after
2123 parsing of arguments from the command line. */
2124 SYMBOLIC_REGISTER_NAMES,
2125
2126 MIPS16_SPECIAL_REGISTER_NAMES,
2127 MDMX_VECTOR_REGISTER_NAMES,
2128 MIPS_DSP_ACCUMULATOR_NAMES,
2129 {0, 0}
2130 };
2131
2132 static const struct regname reg_names_o32[] = {
2133 O32_SYMBOLIC_REGISTER_NAMES,
2134 {0, 0}
2135 };
2136
2137 static const struct regname reg_names_n32n64[] = {
2138 N32N64_SYMBOLIC_REGISTER_NAMES,
2139 {0, 0}
2140 };
2141
2142 /* Check if S points at a valid register specifier according to TYPES.
2143 If so, then return 1, advance S to consume the specifier and store
2144 the register's number in REGNOP, otherwise return 0. */
2145
2146 static int
2147 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2148 {
2149 symbolS *symbolP;
2150 char *e;
2151 char save_c;
2152 int reg = -1;
2153
2154 /* Find end of name. */
2155 e = *s;
2156 if (is_name_beginner (*e))
2157 ++e;
2158 while (is_part_of_name (*e))
2159 ++e;
2160
2161 /* Terminate name. */
2162 save_c = *e;
2163 *e = '\0';
2164
2165 /* Look for a register symbol. */
2166 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2167 {
2168 int r = S_GET_VALUE (symbolP);
2169 if (r & types)
2170 reg = r & RNUM_MASK;
2171 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2172 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
2173 reg = (r & RNUM_MASK) - 2;
2174 }
2175 /* Else see if this is a register defined in an itbl entry. */
2176 else if ((types & RTYPE_GP) && itbl_have_entries)
2177 {
2178 char *n = *s;
2179 unsigned long r;
2180
2181 if (*n == '$')
2182 ++n;
2183 if (itbl_get_reg_val (n, &r))
2184 reg = r & RNUM_MASK;
2185 }
2186
2187 /* Advance to next token if a register was recognised. */
2188 if (reg >= 0)
2189 *s = e;
2190 else if (types & RWARN)
2191 as_warn (_("Unrecognized register name `%s'"), *s);
2192
2193 *e = save_c;
2194 if (regnop)
2195 *regnop = reg;
2196 return reg >= 0;
2197 }
2198
2199 /* Check if S points at a valid register list according to TYPES.
2200 If so, then return 1, advance S to consume the list and store
2201 the registers present on the list as a bitmask of ones in REGLISTP,
2202 otherwise return 0. A valid list comprises a comma-separated
2203 enumeration of valid single registers and/or dash-separated
2204 contiguous register ranges as determined by their numbers.
2205
2206 As a special exception if one of s0-s7 registers is specified as
2207 the range's lower delimiter and s8 (fp) is its upper one, then no
2208 registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2209 are selected; they have to be listed separately if needed. */
2210
2211 static int
2212 reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2213 {
2214 unsigned int reglist = 0;
2215 unsigned int lastregno;
2216 bfd_boolean ok = TRUE;
2217 unsigned int regmask;
2218 char *s_endlist = *s;
2219 char *s_reset = *s;
2220 unsigned int regno;
2221
2222 while (reg_lookup (s, types, &regno))
2223 {
2224 lastregno = regno;
2225 if (**s == '-')
2226 {
2227 (*s)++;
2228 ok = reg_lookup (s, types, &lastregno);
2229 if (ok && lastregno < regno)
2230 ok = FALSE;
2231 if (!ok)
2232 break;
2233 }
2234
2235 if (lastregno == FP && regno >= S0 && regno <= S7)
2236 {
2237 lastregno = S7;
2238 reglist |= 1 << FP;
2239 }
2240 regmask = 1 << lastregno;
2241 regmask = (regmask << 1) - 1;
2242 regmask ^= (1 << regno) - 1;
2243 reglist |= regmask;
2244
2245 s_endlist = *s;
2246 if (**s != ',')
2247 break;
2248 (*s)++;
2249 }
2250
2251 if (ok)
2252 *s = s_endlist;
2253 else
2254 *s = s_reset;
2255 if (reglistp)
2256 *reglistp = reglist;
2257 return ok && reglist != 0;
2258 }
2259
2260 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
2261 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
2262
2263 static bfd_boolean
2264 is_opcode_valid (const struct mips_opcode *mo)
2265 {
2266 int isa = mips_opts.isa;
2267 int ase = 0;
2268 int fp_s, fp_d;
2269
2270 if (mips_opts.ase_mdmx)
2271 ase |= ASE_MDMX;
2272 if (mips_opts.ase_dsp)
2273 ase |= ASE_DSP;
2274 if (mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
2275 ase |= ASE_DSP64;
2276 if (mips_opts.ase_dspr2)
2277 ase |= ASE_DSPR2;
2278 if (mips_opts.ase_eva)
2279 ase |= ASE_EVA;
2280 if (mips_opts.ase_mt)
2281 ase |= ASE_MT;
2282 if (mips_opts.ase_mips3d)
2283 ase |= ASE_MIPS3D;
2284 if (mips_opts.ase_smartmips)
2285 ase |= ASE_SMARTMIPS;
2286 if (mips_opts.ase_mcu)
2287 ase |= ASE_MCU;
2288 if (mips_opts.ase_virt)
2289 ase |= ASE_VIRT;
2290 if (mips_opts.ase_virt && ISA_SUPPORTS_VIRT64_ASE)
2291 ase |= ASE_VIRT64;
2292
2293 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
2294 return FALSE;
2295
2296 /* Check whether the instruction or macro requires single-precision or
2297 double-precision floating-point support. Note that this information is
2298 stored differently in the opcode table for insns and macros. */
2299 if (mo->pinfo == INSN_MACRO)
2300 {
2301 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2302 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2303 }
2304 else
2305 {
2306 fp_s = mo->pinfo & FP_S;
2307 fp_d = mo->pinfo & FP_D;
2308 }
2309
2310 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2311 return FALSE;
2312
2313 if (fp_s && mips_opts.soft_float)
2314 return FALSE;
2315
2316 return TRUE;
2317 }
2318
2319 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
2320 selected ISA and architecture. */
2321
2322 static bfd_boolean
2323 is_opcode_valid_16 (const struct mips_opcode *mo)
2324 {
2325 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
2326 }
2327
2328 /* Return TRUE if the size of the microMIPS opcode MO matches one
2329 explicitly requested. Always TRUE in the standard MIPS mode. */
2330
2331 static bfd_boolean
2332 is_size_valid (const struct mips_opcode *mo)
2333 {
2334 if (!mips_opts.micromips)
2335 return TRUE;
2336
2337 if (!forced_insn_length)
2338 return TRUE;
2339 if (mo->pinfo == INSN_MACRO)
2340 return FALSE;
2341 return forced_insn_length == micromips_insn_length (mo);
2342 }
2343
2344 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
2345 of the preceding instruction. Always TRUE in the standard MIPS mode.
2346
2347 We don't accept macros in 16-bit delay slots to avoid a case where
2348 a macro expansion fails because it relies on a preceding 32-bit real
2349 instruction to have matched and does not handle the operands correctly.
2350 The only macros that may expand to 16-bit instructions are JAL that
2351 cannot be placed in a delay slot anyway, and corner cases of BALIGN
2352 and BGT (that likewise cannot be placed in a delay slot) that decay to
2353 a NOP. In all these cases the macros precede any corresponding real
2354 instruction definitions in the opcode table, so they will match in the
2355 second pass where the size of the delay slot is ignored and therefore
2356 produce correct code. */
2357
2358 static bfd_boolean
2359 is_delay_slot_valid (const struct mips_opcode *mo)
2360 {
2361 if (!mips_opts.micromips)
2362 return TRUE;
2363
2364 if (mo->pinfo == INSN_MACRO)
2365 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
2366 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2367 && micromips_insn_length (mo) != 4)
2368 return FALSE;
2369 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2370 && micromips_insn_length (mo) != 2)
2371 return FALSE;
2372
2373 return TRUE;
2374 }
2375
2376 /* This function is called once, at assembler startup time. It should set up
2377 all the tables, etc. that the MD part of the assembler will need. */
2378
2379 void
2380 md_begin (void)
2381 {
2382 const char *retval = NULL;
2383 int i = 0;
2384 int broken = 0;
2385
2386 if (mips_pic != NO_PIC)
2387 {
2388 if (g_switch_seen && g_switch_value != 0)
2389 as_bad (_("-G may not be used in position-independent code"));
2390 g_switch_value = 0;
2391 }
2392
2393 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
2394 as_warn (_("Could not set architecture and machine"));
2395
2396 op_hash = hash_new ();
2397
2398 for (i = 0; i < NUMOPCODES;)
2399 {
2400 const char *name = mips_opcodes[i].name;
2401
2402 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
2403 if (retval != NULL)
2404 {
2405 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2406 mips_opcodes[i].name, retval);
2407 /* Probably a memory allocation problem? Give up now. */
2408 as_fatal (_("Broken assembler. No assembly attempted."));
2409 }
2410 do
2411 {
2412 if (mips_opcodes[i].pinfo != INSN_MACRO)
2413 {
2414 if (!validate_mips_insn (&mips_opcodes[i]))
2415 broken = 1;
2416 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2417 {
2418 create_insn (&nop_insn, mips_opcodes + i);
2419 if (mips_fix_loongson2f_nop)
2420 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
2421 nop_insn.fixed_p = 1;
2422 }
2423 }
2424 ++i;
2425 }
2426 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2427 }
2428
2429 mips16_op_hash = hash_new ();
2430
2431 i = 0;
2432 while (i < bfd_mips16_num_opcodes)
2433 {
2434 const char *name = mips16_opcodes[i].name;
2435
2436 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
2437 if (retval != NULL)
2438 as_fatal (_("internal: can't hash `%s': %s"),
2439 mips16_opcodes[i].name, retval);
2440 do
2441 {
2442 if (mips16_opcodes[i].pinfo != INSN_MACRO
2443 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2444 != mips16_opcodes[i].match))
2445 {
2446 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2447 mips16_opcodes[i].name, mips16_opcodes[i].args);
2448 broken = 1;
2449 }
2450 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2451 {
2452 create_insn (&mips16_nop_insn, mips16_opcodes + i);
2453 mips16_nop_insn.fixed_p = 1;
2454 }
2455 ++i;
2456 }
2457 while (i < bfd_mips16_num_opcodes
2458 && strcmp (mips16_opcodes[i].name, name) == 0);
2459 }
2460
2461 micromips_op_hash = hash_new ();
2462
2463 i = 0;
2464 while (i < bfd_micromips_num_opcodes)
2465 {
2466 const char *name = micromips_opcodes[i].name;
2467
2468 retval = hash_insert (micromips_op_hash, name,
2469 (void *) &micromips_opcodes[i]);
2470 if (retval != NULL)
2471 as_fatal (_("internal: can't hash `%s': %s"),
2472 micromips_opcodes[i].name, retval);
2473 do
2474 if (micromips_opcodes[i].pinfo != INSN_MACRO)
2475 {
2476 struct mips_cl_insn *micromips_nop_insn;
2477
2478 if (!validate_micromips_insn (&micromips_opcodes[i]))
2479 broken = 1;
2480
2481 if (micromips_insn_length (micromips_opcodes + i) == 2)
2482 micromips_nop_insn = &micromips_nop16_insn;
2483 else if (micromips_insn_length (micromips_opcodes + i) == 4)
2484 micromips_nop_insn = &micromips_nop32_insn;
2485 else
2486 continue;
2487
2488 if (micromips_nop_insn->insn_mo == NULL
2489 && strcmp (name, "nop") == 0)
2490 {
2491 create_insn (micromips_nop_insn, micromips_opcodes + i);
2492 micromips_nop_insn->fixed_p = 1;
2493 }
2494 }
2495 while (++i < bfd_micromips_num_opcodes
2496 && strcmp (micromips_opcodes[i].name, name) == 0);
2497 }
2498
2499 if (broken)
2500 as_fatal (_("Broken assembler. No assembly attempted."));
2501
2502 /* We add all the general register names to the symbol table. This
2503 helps us detect invalid uses of them. */
2504 for (i = 0; reg_names[i].name; i++)
2505 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
2506 reg_names[i].num, /* & RNUM_MASK, */
2507 &zero_address_frag));
2508 if (HAVE_NEWABI)
2509 for (i = 0; reg_names_n32n64[i].name; i++)
2510 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
2511 reg_names_n32n64[i].num, /* & RNUM_MASK, */
2512 &zero_address_frag));
2513 else
2514 for (i = 0; reg_names_o32[i].name; i++)
2515 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
2516 reg_names_o32[i].num, /* & RNUM_MASK, */
2517 &zero_address_frag));
2518
2519 mips_no_prev_insn ();
2520
2521 mips_gprmask = 0;
2522 mips_cprmask[0] = 0;
2523 mips_cprmask[1] = 0;
2524 mips_cprmask[2] = 0;
2525 mips_cprmask[3] = 0;
2526
2527 /* set the default alignment for the text section (2**2) */
2528 record_alignment (text_section, 2);
2529
2530 bfd_set_gp_size (stdoutput, g_switch_value);
2531
2532 #ifdef OBJ_ELF
2533 if (IS_ELF)
2534 {
2535 /* On a native system other than VxWorks, sections must be aligned
2536 to 16 byte boundaries. When configured for an embedded ELF
2537 target, we don't bother. */
2538 if (strncmp (TARGET_OS, "elf", 3) != 0
2539 && strncmp (TARGET_OS, "vxworks", 7) != 0)
2540 {
2541 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2542 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2543 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2544 }
2545
2546 /* Create a .reginfo section for register masks and a .mdebug
2547 section for debugging information. */
2548 {
2549 segT seg;
2550 subsegT subseg;
2551 flagword flags;
2552 segT sec;
2553
2554 seg = now_seg;
2555 subseg = now_subseg;
2556
2557 /* The ABI says this section should be loaded so that the
2558 running program can access it. However, we don't load it
2559 if we are configured for an embedded target */
2560 flags = SEC_READONLY | SEC_DATA;
2561 if (strncmp (TARGET_OS, "elf", 3) != 0)
2562 flags |= SEC_ALLOC | SEC_LOAD;
2563
2564 if (mips_abi != N64_ABI)
2565 {
2566 sec = subseg_new (".reginfo", (subsegT) 0);
2567
2568 bfd_set_section_flags (stdoutput, sec, flags);
2569 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
2570
2571 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
2572 }
2573 else
2574 {
2575 /* The 64-bit ABI uses a .MIPS.options section rather than
2576 .reginfo section. */
2577 sec = subseg_new (".MIPS.options", (subsegT) 0);
2578 bfd_set_section_flags (stdoutput, sec, flags);
2579 bfd_set_section_alignment (stdoutput, sec, 3);
2580
2581 /* Set up the option header. */
2582 {
2583 Elf_Internal_Options opthdr;
2584 char *f;
2585
2586 opthdr.kind = ODK_REGINFO;
2587 opthdr.size = (sizeof (Elf_External_Options)
2588 + sizeof (Elf64_External_RegInfo));
2589 opthdr.section = 0;
2590 opthdr.info = 0;
2591 f = frag_more (sizeof (Elf_External_Options));
2592 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2593 (Elf_External_Options *) f);
2594
2595 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2596 }
2597 }
2598
2599 if (ECOFF_DEBUGGING)
2600 {
2601 sec = subseg_new (".mdebug", (subsegT) 0);
2602 (void) bfd_set_section_flags (stdoutput, sec,
2603 SEC_HAS_CONTENTS | SEC_READONLY);
2604 (void) bfd_set_section_alignment (stdoutput, sec, 2);
2605 }
2606 else if (mips_flag_pdr)
2607 {
2608 pdr_seg = subseg_new (".pdr", (subsegT) 0);
2609 (void) bfd_set_section_flags (stdoutput, pdr_seg,
2610 SEC_READONLY | SEC_RELOC
2611 | SEC_DEBUGGING);
2612 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2613 }
2614
2615 subseg_set (seg, subseg);
2616 }
2617 }
2618 #endif /* OBJ_ELF */
2619
2620 if (! ECOFF_DEBUGGING)
2621 md_obj_begin ();
2622
2623 if (mips_fix_vr4120)
2624 init_vr4120_conflicts ();
2625 }
2626
2627 void
2628 md_mips_end (void)
2629 {
2630 mips_emit_delays ();
2631 if (! ECOFF_DEBUGGING)
2632 md_obj_end ();
2633 }
2634
2635 void
2636 md_assemble (char *str)
2637 {
2638 struct mips_cl_insn insn;
2639 bfd_reloc_code_real_type unused_reloc[3]
2640 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2641
2642 imm_expr.X_op = O_absent;
2643 imm2_expr.X_op = O_absent;
2644 offset_expr.X_op = O_absent;
2645 imm_reloc[0] = BFD_RELOC_UNUSED;
2646 imm_reloc[1] = BFD_RELOC_UNUSED;
2647 imm_reloc[2] = BFD_RELOC_UNUSED;
2648 offset_reloc[0] = BFD_RELOC_UNUSED;
2649 offset_reloc[1] = BFD_RELOC_UNUSED;
2650 offset_reloc[2] = BFD_RELOC_UNUSED;
2651
2652 mips_mark_labels ();
2653 mips_assembling_insn = TRUE;
2654
2655 if (mips_opts.mips16)
2656 mips16_ip (str, &insn);
2657 else
2658 {
2659 mips_ip (str, &insn);
2660 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2661 str, insn.insn_opcode));
2662 }
2663
2664 if (insn_error)
2665 as_bad ("%s `%s'", insn_error, str);
2666 else if (insn.insn_mo->pinfo == INSN_MACRO)
2667 {
2668 macro_start ();
2669 if (mips_opts.mips16)
2670 mips16_macro (&insn);
2671 else
2672 macro (&insn);
2673 macro_end ();
2674 }
2675 else
2676 {
2677 if (imm_expr.X_op != O_absent)
2678 append_insn (&insn, &imm_expr, imm_reloc, FALSE);
2679 else if (offset_expr.X_op != O_absent)
2680 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
2681 else
2682 append_insn (&insn, NULL, unused_reloc, FALSE);
2683 }
2684
2685 mips_assembling_insn = FALSE;
2686 }
2687
2688 /* Convenience functions for abstracting away the differences between
2689 MIPS16 and non-MIPS16 relocations. */
2690
2691 static inline bfd_boolean
2692 mips16_reloc_p (bfd_reloc_code_real_type reloc)
2693 {
2694 switch (reloc)
2695 {
2696 case BFD_RELOC_MIPS16_JMP:
2697 case BFD_RELOC_MIPS16_GPREL:
2698 case BFD_RELOC_MIPS16_GOT16:
2699 case BFD_RELOC_MIPS16_CALL16:
2700 case BFD_RELOC_MIPS16_HI16_S:
2701 case BFD_RELOC_MIPS16_HI16:
2702 case BFD_RELOC_MIPS16_LO16:
2703 return TRUE;
2704
2705 default:
2706 return FALSE;
2707 }
2708 }
2709
2710 static inline bfd_boolean
2711 micromips_reloc_p (bfd_reloc_code_real_type reloc)
2712 {
2713 switch (reloc)
2714 {
2715 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2716 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2717 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2718 case BFD_RELOC_MICROMIPS_GPREL16:
2719 case BFD_RELOC_MICROMIPS_JMP:
2720 case BFD_RELOC_MICROMIPS_HI16:
2721 case BFD_RELOC_MICROMIPS_HI16_S:
2722 case BFD_RELOC_MICROMIPS_LO16:
2723 case BFD_RELOC_MICROMIPS_LITERAL:
2724 case BFD_RELOC_MICROMIPS_GOT16:
2725 case BFD_RELOC_MICROMIPS_CALL16:
2726 case BFD_RELOC_MICROMIPS_GOT_HI16:
2727 case BFD_RELOC_MICROMIPS_GOT_LO16:
2728 case BFD_RELOC_MICROMIPS_CALL_HI16:
2729 case BFD_RELOC_MICROMIPS_CALL_LO16:
2730 case BFD_RELOC_MICROMIPS_SUB:
2731 case BFD_RELOC_MICROMIPS_GOT_PAGE:
2732 case BFD_RELOC_MICROMIPS_GOT_OFST:
2733 case BFD_RELOC_MICROMIPS_GOT_DISP:
2734 case BFD_RELOC_MICROMIPS_HIGHEST:
2735 case BFD_RELOC_MICROMIPS_HIGHER:
2736 case BFD_RELOC_MICROMIPS_SCN_DISP:
2737 case BFD_RELOC_MICROMIPS_JALR:
2738 return TRUE;
2739
2740 default:
2741 return FALSE;
2742 }
2743 }
2744
2745 static inline bfd_boolean
2746 jmp_reloc_p (bfd_reloc_code_real_type reloc)
2747 {
2748 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
2749 }
2750
2751 static inline bfd_boolean
2752 got16_reloc_p (bfd_reloc_code_real_type reloc)
2753 {
2754 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
2755 || reloc == BFD_RELOC_MICROMIPS_GOT16);
2756 }
2757
2758 static inline bfd_boolean
2759 hi16_reloc_p (bfd_reloc_code_real_type reloc)
2760 {
2761 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
2762 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
2763 }
2764
2765 static inline bfd_boolean
2766 lo16_reloc_p (bfd_reloc_code_real_type reloc)
2767 {
2768 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
2769 || reloc == BFD_RELOC_MICROMIPS_LO16);
2770 }
2771
2772 static inline bfd_boolean
2773 jalr_reloc_p (bfd_reloc_code_real_type reloc)
2774 {
2775 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
2776 }
2777
2778 /* Return true if RELOC is a PC-relative relocation that does not have
2779 full address range. */
2780
2781 static inline bfd_boolean
2782 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
2783 {
2784 switch (reloc)
2785 {
2786 case BFD_RELOC_16_PCREL_S2:
2787 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2788 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2789 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2790 return TRUE;
2791
2792 case BFD_RELOC_32_PCREL:
2793 return HAVE_64BIT_ADDRESSES;
2794
2795 default:
2796 return FALSE;
2797 }
2798 }
2799
2800 /* Return true if the given relocation might need a matching %lo().
2801 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2802 need a matching %lo() when applied to local symbols. */
2803
2804 static inline bfd_boolean
2805 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
2806 {
2807 return (HAVE_IN_PLACE_ADDENDS
2808 && (hi16_reloc_p (reloc)
2809 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2810 all GOT16 relocations evaluate to "G". */
2811 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
2812 }
2813
2814 /* Return the type of %lo() reloc needed by RELOC, given that
2815 reloc_needs_lo_p. */
2816
2817 static inline bfd_reloc_code_real_type
2818 matching_lo_reloc (bfd_reloc_code_real_type reloc)
2819 {
2820 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
2821 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
2822 : BFD_RELOC_LO16));
2823 }
2824
2825 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
2826 relocation. */
2827
2828 static inline bfd_boolean
2829 fixup_has_matching_lo_p (fixS *fixp)
2830 {
2831 return (fixp->fx_next != NULL
2832 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
2833 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2834 && fixp->fx_offset == fixp->fx_next->fx_offset);
2835 }
2836
2837 /* This function returns true if modifying a register requires a
2838 delay. */
2839
2840 static int
2841 reg_needs_delay (unsigned int reg)
2842 {
2843 unsigned long prev_pinfo;
2844
2845 prev_pinfo = history[0].insn_mo->pinfo;
2846 if (! mips_opts.noreorder
2847 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2848 && ! gpr_interlocks)
2849 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2850 && ! cop_interlocks)))
2851 {
2852 /* A load from a coprocessor or from memory. All load delays
2853 delay the use of general register rt for one instruction. */
2854 /* Itbl support may require additional care here. */
2855 know (prev_pinfo & INSN_WRITE_GPR_T);
2856 if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
2857 return 1;
2858 }
2859
2860 return 0;
2861 }
2862
2863 /* Move all labels in LABELS to the current insertion point. TEXT_P
2864 says whether the labels refer to text or data. */
2865
2866 static void
2867 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
2868 {
2869 struct insn_label_list *l;
2870 valueT val;
2871
2872 for (l = labels; l != NULL; l = l->next)
2873 {
2874 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
2875 symbol_set_frag (l->label, frag_now);
2876 val = (valueT) frag_now_fix ();
2877 /* MIPS16/microMIPS text labels are stored as odd. */
2878 if (text_p && HAVE_CODE_COMPRESSION)
2879 ++val;
2880 S_SET_VALUE (l->label, val);
2881 }
2882 }
2883
2884 /* Move all labels in insn_labels to the current insertion point
2885 and treat them as text labels. */
2886
2887 static void
2888 mips_move_text_labels (void)
2889 {
2890 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
2891 }
2892
2893 static bfd_boolean
2894 s_is_linkonce (symbolS *sym, segT from_seg)
2895 {
2896 bfd_boolean linkonce = FALSE;
2897 segT symseg = S_GET_SEGMENT (sym);
2898
2899 if (symseg != from_seg && !S_IS_LOCAL (sym))
2900 {
2901 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2902 linkonce = TRUE;
2903 #ifdef OBJ_ELF
2904 /* The GNU toolchain uses an extension for ELF: a section
2905 beginning with the magic string .gnu.linkonce is a
2906 linkonce section. */
2907 if (strncmp (segment_name (symseg), ".gnu.linkonce",
2908 sizeof ".gnu.linkonce" - 1) == 0)
2909 linkonce = TRUE;
2910 #endif
2911 }
2912 return linkonce;
2913 }
2914
2915 /* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
2916 linker to handle them specially, such as generating jalx instructions
2917 when needed. We also make them odd for the duration of the assembly,
2918 in order to generate the right sort of code. We will make them even
2919 in the adjust_symtab routine, while leaving them marked. This is
2920 convenient for the debugger and the disassembler. The linker knows
2921 to make them odd again. */
2922
2923 static void
2924 mips_compressed_mark_label (symbolS *label)
2925 {
2926 gas_assert (HAVE_CODE_COMPRESSION);
2927
2928 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
2929 if (IS_ELF)
2930 {
2931 if (mips_opts.mips16)
2932 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
2933 else
2934 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
2935 }
2936 #endif
2937 if ((S_GET_VALUE (label) & 1) == 0
2938 /* Don't adjust the address if the label is global or weak, or
2939 in a link-once section, since we'll be emitting symbol reloc
2940 references to it which will be patched up by the linker, and
2941 the final value of the symbol may or may not be MIPS16/microMIPS. */
2942 && !S_IS_WEAK (label)
2943 && !S_IS_EXTERNAL (label)
2944 && !s_is_linkonce (label, now_seg))
2945 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
2946 }
2947
2948 /* Mark preceding MIPS16 or microMIPS instruction labels. */
2949
2950 static void
2951 mips_compressed_mark_labels (void)
2952 {
2953 struct insn_label_list *l;
2954
2955 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
2956 mips_compressed_mark_label (l->label);
2957 }
2958
2959 /* End the current frag. Make it a variant frag and record the
2960 relaxation info. */
2961
2962 static void
2963 relax_close_frag (void)
2964 {
2965 mips_macro_warning.first_frag = frag_now;
2966 frag_var (rs_machine_dependent, 0, 0,
2967 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
2968 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2969
2970 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2971 mips_relax.first_fixup = 0;
2972 }
2973
2974 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
2975 See the comment above RELAX_ENCODE for more details. */
2976
2977 static void
2978 relax_start (symbolS *symbol)
2979 {
2980 gas_assert (mips_relax.sequence == 0);
2981 mips_relax.sequence = 1;
2982 mips_relax.symbol = symbol;
2983 }
2984
2985 /* Start generating the second version of a relaxable sequence.
2986 See the comment above RELAX_ENCODE for more details. */
2987
2988 static void
2989 relax_switch (void)
2990 {
2991 gas_assert (mips_relax.sequence == 1);
2992 mips_relax.sequence = 2;
2993 }
2994
2995 /* End the current relaxable sequence. */
2996
2997 static void
2998 relax_end (void)
2999 {
3000 gas_assert (mips_relax.sequence == 2);
3001 relax_close_frag ();
3002 mips_relax.sequence = 0;
3003 }
3004
3005 /* Return true if IP is a delayed branch or jump. */
3006
3007 static inline bfd_boolean
3008 delayed_branch_p (const struct mips_cl_insn *ip)
3009 {
3010 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
3011 | INSN_COND_BRANCH_DELAY
3012 | INSN_COND_BRANCH_LIKELY)) != 0;
3013 }
3014
3015 /* Return true if IP is a compact branch or jump. */
3016
3017 static inline bfd_boolean
3018 compact_branch_p (const struct mips_cl_insn *ip)
3019 {
3020 if (mips_opts.mips16)
3021 return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
3022 | MIPS16_INSN_COND_BRANCH)) != 0;
3023 else
3024 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
3025 | INSN2_COND_BRANCH)) != 0;
3026 }
3027
3028 /* Return true if IP is an unconditional branch or jump. */
3029
3030 static inline bfd_boolean
3031 uncond_branch_p (const struct mips_cl_insn *ip)
3032 {
3033 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
3034 || (mips_opts.mips16
3035 ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
3036 : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
3037 }
3038
3039 /* Return true if IP is a branch-likely instruction. */
3040
3041 static inline bfd_boolean
3042 branch_likely_p (const struct mips_cl_insn *ip)
3043 {
3044 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
3045 }
3046
3047 /* Return the type of nop that should be used to fill the delay slot
3048 of delayed branch IP. */
3049
3050 static struct mips_cl_insn *
3051 get_delay_slot_nop (const struct mips_cl_insn *ip)
3052 {
3053 if (mips_opts.micromips
3054 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3055 return &micromips_nop32_insn;
3056 return NOP_INSN;
3057 }
3058
3059 /* Return the mask of core registers that IP reads or writes. */
3060
3061 static unsigned int
3062 gpr_mod_mask (const struct mips_cl_insn *ip)
3063 {
3064 unsigned long pinfo2;
3065 unsigned int mask;
3066
3067 mask = 0;
3068 pinfo2 = ip->insn_mo->pinfo2;
3069 if (mips_opts.micromips)
3070 {
3071 if (pinfo2 & INSN2_MOD_GPR_MD)
3072 mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
3073 if (pinfo2 & INSN2_MOD_GPR_MF)
3074 mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
3075 if (pinfo2 & INSN2_MOD_SP)
3076 mask |= 1 << SP;
3077 }
3078 return mask;
3079 }
3080
3081 /* Return the mask of core registers that IP reads. */
3082
3083 static unsigned int
3084 gpr_read_mask (const struct mips_cl_insn *ip)
3085 {
3086 unsigned long pinfo, pinfo2;
3087 unsigned int mask;
3088
3089 mask = gpr_mod_mask (ip);
3090 pinfo = ip->insn_mo->pinfo;
3091 pinfo2 = ip->insn_mo->pinfo2;
3092 if (mips_opts.mips16)
3093 {
3094 if (pinfo & MIPS16_INSN_READ_X)
3095 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3096 if (pinfo & MIPS16_INSN_READ_Y)
3097 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3098 if (pinfo & MIPS16_INSN_READ_T)
3099 mask |= 1 << TREG;
3100 if (pinfo & MIPS16_INSN_READ_SP)
3101 mask |= 1 << SP;
3102 if (pinfo & MIPS16_INSN_READ_31)
3103 mask |= 1 << RA;
3104 if (pinfo & MIPS16_INSN_READ_Z)
3105 mask |= 1 << (mips16_to_32_reg_map
3106 [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3107 if (pinfo & MIPS16_INSN_READ_GPR_X)
3108 mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3109 }
3110 else
3111 {
3112 if (pinfo2 & INSN2_READ_GPR_D)
3113 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3114 if (pinfo & INSN_READ_GPR_T)
3115 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3116 if (pinfo & INSN_READ_GPR_S)
3117 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3118 if (pinfo2 & INSN2_READ_GP)
3119 mask |= 1 << GP;
3120 if (pinfo2 & INSN2_READ_GPR_31)
3121 mask |= 1 << RA;
3122 if (pinfo2 & INSN2_READ_GPR_Z)
3123 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3124 }
3125 if (mips_opts.micromips)
3126 {
3127 if (pinfo2 & INSN2_READ_GPR_MC)
3128 mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3129 if (pinfo2 & INSN2_READ_GPR_ME)
3130 mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3131 if (pinfo2 & INSN2_READ_GPR_MG)
3132 mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3133 if (pinfo2 & INSN2_READ_GPR_MJ)
3134 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3135 if (pinfo2 & INSN2_READ_GPR_MMN)
3136 {
3137 mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3138 mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3139 }
3140 if (pinfo2 & INSN2_READ_GPR_MP)
3141 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3142 if (pinfo2 & INSN2_READ_GPR_MQ)
3143 mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3144 }
3145 /* Don't include register 0. */
3146 return mask & ~1;
3147 }
3148
3149 /* Return the mask of core registers that IP writes. */
3150
3151 static unsigned int
3152 gpr_write_mask (const struct mips_cl_insn *ip)
3153 {
3154 unsigned long pinfo, pinfo2;
3155 unsigned int mask;
3156
3157 mask = gpr_mod_mask (ip);
3158 pinfo = ip->insn_mo->pinfo;
3159 pinfo2 = ip->insn_mo->pinfo2;
3160 if (mips_opts.mips16)
3161 {
3162 if (pinfo & MIPS16_INSN_WRITE_X)
3163 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3164 if (pinfo & MIPS16_INSN_WRITE_Y)
3165 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3166 if (pinfo & MIPS16_INSN_WRITE_Z)
3167 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3168 if (pinfo & MIPS16_INSN_WRITE_T)
3169 mask |= 1 << TREG;
3170 if (pinfo & MIPS16_INSN_WRITE_SP)
3171 mask |= 1 << SP;
3172 if (pinfo & MIPS16_INSN_WRITE_31)
3173 mask |= 1 << RA;
3174 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3175 mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3176 }
3177 else
3178 {
3179 if (pinfo & INSN_WRITE_GPR_D)
3180 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3181 if (pinfo & INSN_WRITE_GPR_T)
3182 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3183 if (pinfo & INSN_WRITE_GPR_S)
3184 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3185 if (pinfo & INSN_WRITE_GPR_31)
3186 mask |= 1 << RA;
3187 if (pinfo2 & INSN2_WRITE_GPR_Z)
3188 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3189 }
3190 if (mips_opts.micromips)
3191 {
3192 if (pinfo2 & INSN2_WRITE_GPR_MB)
3193 mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3194 if (pinfo2 & INSN2_WRITE_GPR_MHI)
3195 {
3196 mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3197 mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3198 }
3199 if (pinfo2 & INSN2_WRITE_GPR_MJ)
3200 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3201 if (pinfo2 & INSN2_WRITE_GPR_MP)
3202 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3203 }
3204 /* Don't include register 0. */
3205 return mask & ~1;
3206 }
3207
3208 /* Return the mask of floating-point registers that IP reads. */
3209
3210 static unsigned int
3211 fpr_read_mask (const struct mips_cl_insn *ip)
3212 {
3213 unsigned long pinfo, pinfo2;
3214 unsigned int mask;
3215
3216 mask = 0;
3217 pinfo = ip->insn_mo->pinfo;
3218 pinfo2 = ip->insn_mo->pinfo2;
3219 if (!mips_opts.mips16)
3220 {
3221 if (pinfo2 & INSN2_READ_FPR_D)
3222 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3223 if (pinfo & INSN_READ_FPR_S)
3224 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3225 if (pinfo & INSN_READ_FPR_T)
3226 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3227 if (pinfo & INSN_READ_FPR_R)
3228 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
3229 if (pinfo2 & INSN2_READ_FPR_Z)
3230 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3231 }
3232 /* Conservatively treat all operands to an FP_D instruction are doubles.
3233 (This is overly pessimistic for things like cvt.d.s.) */
3234 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3235 mask |= mask << 1;
3236 return mask;
3237 }
3238
3239 /* Return the mask of floating-point registers that IP writes. */
3240
3241 static unsigned int
3242 fpr_write_mask (const struct mips_cl_insn *ip)
3243 {
3244 unsigned long pinfo, pinfo2;
3245 unsigned int mask;
3246
3247 mask = 0;
3248 pinfo = ip->insn_mo->pinfo;
3249 pinfo2 = ip->insn_mo->pinfo2;
3250 if (!mips_opts.mips16)
3251 {
3252 if (pinfo & INSN_WRITE_FPR_D)
3253 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3254 if (pinfo & INSN_WRITE_FPR_S)
3255 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3256 if (pinfo & INSN_WRITE_FPR_T)
3257 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3258 if (pinfo2 & INSN2_WRITE_FPR_Z)
3259 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3260 }
3261 /* Conservatively treat all operands to an FP_D instruction are doubles.
3262 (This is overly pessimistic for things like cvt.s.d.) */
3263 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3264 mask |= mask << 1;
3265 return mask;
3266 }
3267
3268 /* Classify an instruction according to the FIX_VR4120_* enumeration.
3269 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3270 by VR4120 errata. */
3271
3272 static unsigned int
3273 classify_vr4120_insn (const char *name)
3274 {
3275 if (strncmp (name, "macc", 4) == 0)
3276 return FIX_VR4120_MACC;
3277 if (strncmp (name, "dmacc", 5) == 0)
3278 return FIX_VR4120_DMACC;
3279 if (strncmp (name, "mult", 4) == 0)
3280 return FIX_VR4120_MULT;
3281 if (strncmp (name, "dmult", 5) == 0)
3282 return FIX_VR4120_DMULT;
3283 if (strstr (name, "div"))
3284 return FIX_VR4120_DIV;
3285 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3286 return FIX_VR4120_MTHILO;
3287 return NUM_FIX_VR4120_CLASSES;
3288 }
3289
3290 #define INSN_ERET 0x42000018
3291 #define INSN_DERET 0x4200001f
3292
3293 /* Return the number of instructions that must separate INSN1 and INSN2,
3294 where INSN1 is the earlier instruction. Return the worst-case value
3295 for any INSN2 if INSN2 is null. */
3296
3297 static unsigned int
3298 insns_between (const struct mips_cl_insn *insn1,
3299 const struct mips_cl_insn *insn2)
3300 {
3301 unsigned long pinfo1, pinfo2;
3302 unsigned int mask;
3303
3304 /* This function needs to know which pinfo flags are set for INSN2
3305 and which registers INSN2 uses. The former is stored in PINFO2 and
3306 the latter is tested via INSN2_USES_GPR. If INSN2 is null, PINFO2
3307 will have every flag set and INSN2_USES_GPR will always return true. */
3308 pinfo1 = insn1->insn_mo->pinfo;
3309 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
3310
3311 #define INSN2_USES_GPR(REG) \
3312 (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
3313
3314 /* For most targets, write-after-read dependencies on the HI and LO
3315 registers must be separated by at least two instructions. */
3316 if (!hilo_interlocks)
3317 {
3318 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3319 return 2;
3320 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3321 return 2;
3322 }
3323
3324 /* If we're working around r7000 errata, there must be two instructions
3325 between an mfhi or mflo and any instruction that uses the result. */
3326 if (mips_7000_hilo_fix
3327 && !mips_opts.micromips
3328 && MF_HILO_INSN (pinfo1)
3329 && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
3330 return 2;
3331
3332 /* If we're working around 24K errata, one instruction is required
3333 if an ERET or DERET is followed by a branch instruction. */
3334 if (mips_fix_24k && !mips_opts.micromips)
3335 {
3336 if (insn1->insn_opcode == INSN_ERET
3337 || insn1->insn_opcode == INSN_DERET)
3338 {
3339 if (insn2 == NULL
3340 || insn2->insn_opcode == INSN_ERET
3341 || insn2->insn_opcode == INSN_DERET
3342 || delayed_branch_p (insn2))
3343 return 1;
3344 }
3345 }
3346
3347 /* If working around VR4120 errata, check for combinations that need
3348 a single intervening instruction. */
3349 if (mips_fix_vr4120 && !mips_opts.micromips)
3350 {
3351 unsigned int class1, class2;
3352
3353 class1 = classify_vr4120_insn (insn1->insn_mo->name);
3354 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
3355 {
3356 if (insn2 == NULL)
3357 return 1;
3358 class2 = classify_vr4120_insn (insn2->insn_mo->name);
3359 if (vr4120_conflicts[class1] & (1 << class2))
3360 return 1;
3361 }
3362 }
3363
3364 if (!HAVE_CODE_COMPRESSION)
3365 {
3366 /* Check for GPR or coprocessor load delays. All such delays
3367 are on the RT register. */
3368 /* Itbl support may require additional care here. */
3369 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3370 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
3371 {
3372 know (pinfo1 & INSN_WRITE_GPR_T);
3373 if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
3374 return 1;
3375 }
3376
3377 /* Check for generic coprocessor hazards.
3378
3379 This case is not handled very well. There is no special
3380 knowledge of CP0 handling, and the coprocessors other than
3381 the floating point unit are not distinguished at all. */
3382 /* Itbl support may require additional care here. FIXME!
3383 Need to modify this to include knowledge about
3384 user specified delays! */
3385 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3386 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3387 {
3388 /* Handle cases where INSN1 writes to a known general coprocessor
3389 register. There must be a one instruction delay before INSN2
3390 if INSN2 reads that register, otherwise no delay is needed. */
3391 mask = fpr_write_mask (insn1);
3392 if (mask != 0)
3393 {
3394 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
3395 return 1;
3396 }
3397 else
3398 {
3399 /* Read-after-write dependencies on the control registers
3400 require a two-instruction gap. */
3401 if ((pinfo1 & INSN_WRITE_COND_CODE)
3402 && (pinfo2 & INSN_READ_COND_CODE))
3403 return 2;
3404
3405 /* We don't know exactly what INSN1 does. If INSN2 is
3406 also a coprocessor instruction, assume there must be
3407 a one instruction gap. */
3408 if (pinfo2 & INSN_COP)
3409 return 1;
3410 }
3411 }
3412
3413 /* Check for read-after-write dependencies on the coprocessor
3414 control registers in cases where INSN1 does not need a general
3415 coprocessor delay. This means that INSN1 is a floating point
3416 comparison instruction. */
3417 /* Itbl support may require additional care here. */
3418 else if (!cop_interlocks
3419 && (pinfo1 & INSN_WRITE_COND_CODE)
3420 && (pinfo2 & INSN_READ_COND_CODE))
3421 return 1;
3422 }
3423
3424 #undef INSN2_USES_GPR
3425
3426 return 0;
3427 }
3428
3429 /* Return the number of nops that would be needed to work around the
3430 VR4130 mflo/mfhi errata if instruction INSN immediately followed
3431 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
3432 that are contained within the first IGNORE instructions of HIST. */
3433
3434 static int
3435 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
3436 const struct mips_cl_insn *insn)
3437 {
3438 int i, j;
3439 unsigned int mask;
3440
3441 /* Check if the instruction writes to HI or LO. MTHI and MTLO
3442 are not affected by the errata. */
3443 if (insn != 0
3444 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3445 || strcmp (insn->insn_mo->name, "mtlo") == 0
3446 || strcmp (insn->insn_mo->name, "mthi") == 0))
3447 return 0;
3448
3449 /* Search for the first MFLO or MFHI. */
3450 for (i = 0; i < MAX_VR4130_NOPS; i++)
3451 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
3452 {
3453 /* Extract the destination register. */
3454 mask = gpr_write_mask (&hist[i]);
3455
3456 /* No nops are needed if INSN reads that register. */
3457 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
3458 return 0;
3459
3460 /* ...or if any of the intervening instructions do. */
3461 for (j = 0; j < i; j++)
3462 if (gpr_read_mask (&hist[j]) & mask)
3463 return 0;
3464
3465 if (i >= ignore)
3466 return MAX_VR4130_NOPS - i;
3467 }
3468 return 0;
3469 }
3470
3471 #define BASE_REG_EQ(INSN1, INSN2) \
3472 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3473 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3474
3475 /* Return the minimum alignment for this store instruction. */
3476
3477 static int
3478 fix_24k_align_to (const struct mips_opcode *mo)
3479 {
3480 if (strcmp (mo->name, "sh") == 0)
3481 return 2;
3482
3483 if (strcmp (mo->name, "swc1") == 0
3484 || strcmp (mo->name, "swc2") == 0
3485 || strcmp (mo->name, "sw") == 0
3486 || strcmp (mo->name, "sc") == 0
3487 || strcmp (mo->name, "s.s") == 0)
3488 return 4;
3489
3490 if (strcmp (mo->name, "sdc1") == 0
3491 || strcmp (mo->name, "sdc2") == 0
3492 || strcmp (mo->name, "s.d") == 0)
3493 return 8;
3494
3495 /* sb, swl, swr */
3496 return 1;
3497 }
3498
3499 struct fix_24k_store_info
3500 {
3501 /* Immediate offset, if any, for this store instruction. */
3502 short off;
3503 /* Alignment required by this store instruction. */
3504 int align_to;
3505 /* True for register offsets. */
3506 int register_offset;
3507 };
3508
3509 /* Comparison function used by qsort. */
3510
3511 static int
3512 fix_24k_sort (const void *a, const void *b)
3513 {
3514 const struct fix_24k_store_info *pos1 = a;
3515 const struct fix_24k_store_info *pos2 = b;
3516
3517 return (pos1->off - pos2->off);
3518 }
3519
3520 /* INSN is a store instruction. Try to record the store information
3521 in STINFO. Return false if the information isn't known. */
3522
3523 static bfd_boolean
3524 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
3525 const struct mips_cl_insn *insn)
3526 {
3527 /* The instruction must have a known offset. */
3528 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3529 return FALSE;
3530
3531 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3532 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3533 return TRUE;
3534 }
3535
3536 /* Return the number of nops that would be needed to work around the 24k
3537 "lost data on stores during refill" errata if instruction INSN
3538 immediately followed the 2 instructions described by HIST.
3539 Ignore hazards that are contained within the first IGNORE
3540 instructions of HIST.
3541
3542 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3543 for the data cache refills and store data. The following describes
3544 the scenario where the store data could be lost.
3545
3546 * A data cache miss, due to either a load or a store, causing fill
3547 data to be supplied by the memory subsystem
3548 * The first three doublewords of fill data are returned and written
3549 into the cache
3550 * A sequence of four stores occurs in consecutive cycles around the
3551 final doubleword of the fill:
3552 * Store A
3553 * Store B
3554 * Store C
3555 * Zero, One or more instructions
3556 * Store D
3557
3558 The four stores A-D must be to different doublewords of the line that
3559 is being filled. The fourth instruction in the sequence above permits
3560 the fill of the final doubleword to be transferred from the FSB into
3561 the cache. In the sequence above, the stores may be either integer
3562 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3563 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3564 different doublewords on the line. If the floating point unit is
3565 running in 1:2 mode, it is not possible to create the sequence above
3566 using only floating point store instructions.
3567
3568 In this case, the cache line being filled is incorrectly marked
3569 invalid, thereby losing the data from any store to the line that
3570 occurs between the original miss and the completion of the five
3571 cycle sequence shown above.
3572
3573 The workarounds are:
3574
3575 * Run the data cache in write-through mode.
3576 * Insert a non-store instruction between
3577 Store A and Store B or Store B and Store C. */
3578
3579 static int
3580 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
3581 const struct mips_cl_insn *insn)
3582 {
3583 struct fix_24k_store_info pos[3];
3584 int align, i, base_offset;
3585
3586 if (ignore >= 2)
3587 return 0;
3588
3589 /* If the previous instruction wasn't a store, there's nothing to
3590 worry about. */
3591 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3592 return 0;
3593
3594 /* If the instructions after the previous one are unknown, we have
3595 to assume the worst. */
3596 if (!insn)
3597 return 1;
3598
3599 /* Check whether we are dealing with three consecutive stores. */
3600 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3601 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3602 return 0;
3603
3604 /* If we don't know the relationship between the store addresses,
3605 assume the worst. */
3606 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
3607 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3608 return 1;
3609
3610 if (!fix_24k_record_store_info (&pos[0], insn)
3611 || !fix_24k_record_store_info (&pos[1], &hist[0])
3612 || !fix_24k_record_store_info (&pos[2], &hist[1]))
3613 return 1;
3614
3615 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3616
3617 /* Pick a value of ALIGN and X such that all offsets are adjusted by
3618 X bytes and such that the base register + X is known to be aligned
3619 to align bytes. */
3620
3621 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3622 align = 8;
3623 else
3624 {
3625 align = pos[0].align_to;
3626 base_offset = pos[0].off;
3627 for (i = 1; i < 3; i++)
3628 if (align < pos[i].align_to)
3629 {
3630 align = pos[i].align_to;
3631 base_offset = pos[i].off;
3632 }
3633 for (i = 0; i < 3; i++)
3634 pos[i].off -= base_offset;
3635 }
3636
3637 pos[0].off &= ~align + 1;
3638 pos[1].off &= ~align + 1;
3639 pos[2].off &= ~align + 1;
3640
3641 /* If any two stores write to the same chunk, they also write to the
3642 same doubleword. The offsets are still sorted at this point. */
3643 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3644 return 0;
3645
3646 /* A range of at least 9 bytes is needed for the stores to be in
3647 non-overlapping doublewords. */
3648 if (pos[2].off - pos[0].off <= 8)
3649 return 0;
3650
3651 if (pos[2].off - pos[1].off >= 24
3652 || pos[1].off - pos[0].off >= 24
3653 || pos[2].off - pos[0].off >= 32)
3654 return 0;
3655
3656 return 1;
3657 }
3658
3659 /* Return the number of nops that would be needed if instruction INSN
3660 immediately followed the MAX_NOPS instructions given by HIST,
3661 where HIST[0] is the most recent instruction. Ignore hazards
3662 between INSN and the first IGNORE instructions in HIST.
3663
3664 If INSN is null, return the worse-case number of nops for any
3665 instruction. */
3666
3667 static int
3668 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
3669 const struct mips_cl_insn *insn)
3670 {
3671 int i, nops, tmp_nops;
3672
3673 nops = 0;
3674 for (i = ignore; i < MAX_DELAY_NOPS; i++)
3675 {
3676 tmp_nops = insns_between (hist + i, insn) - i;
3677 if (tmp_nops > nops)
3678 nops = tmp_nops;
3679 }
3680
3681 if (mips_fix_vr4130 && !mips_opts.micromips)
3682 {
3683 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
3684 if (tmp_nops > nops)
3685 nops = tmp_nops;
3686 }
3687
3688 if (mips_fix_24k && !mips_opts.micromips)
3689 {
3690 tmp_nops = nops_for_24k (ignore, hist, insn);
3691 if (tmp_nops > nops)
3692 nops = tmp_nops;
3693 }
3694
3695 return nops;
3696 }
3697
3698 /* The variable arguments provide NUM_INSNS extra instructions that
3699 might be added to HIST. Return the largest number of nops that
3700 would be needed after the extended sequence, ignoring hazards
3701 in the first IGNORE instructions. */
3702
3703 static int
3704 nops_for_sequence (int num_insns, int ignore,
3705 const struct mips_cl_insn *hist, ...)
3706 {
3707 va_list args;
3708 struct mips_cl_insn buffer[MAX_NOPS];
3709 struct mips_cl_insn *cursor;
3710 int nops;
3711
3712 va_start (args, hist);
3713 cursor = buffer + num_insns;
3714 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
3715 while (cursor > buffer)
3716 *--cursor = *va_arg (args, const struct mips_cl_insn *);
3717
3718 nops = nops_for_insn (ignore, buffer, NULL);
3719 va_end (args);
3720 return nops;
3721 }
3722
3723 /* Like nops_for_insn, but if INSN is a branch, take into account the
3724 worst-case delay for the branch target. */
3725
3726 static int
3727 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
3728 const struct mips_cl_insn *insn)
3729 {
3730 int nops, tmp_nops;
3731
3732 nops = nops_for_insn (ignore, hist, insn);
3733 if (delayed_branch_p (insn))
3734 {
3735 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
3736 hist, insn, get_delay_slot_nop (insn));
3737 if (tmp_nops > nops)
3738 nops = tmp_nops;
3739 }
3740 else if (compact_branch_p (insn))
3741 {
3742 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
3743 if (tmp_nops > nops)
3744 nops = tmp_nops;
3745 }
3746 return nops;
3747 }
3748
3749 /* Fix NOP issue: Replace nops by "or at,at,zero". */
3750
3751 static void
3752 fix_loongson2f_nop (struct mips_cl_insn * ip)
3753 {
3754 gas_assert (!HAVE_CODE_COMPRESSION);
3755 if (strcmp (ip->insn_mo->name, "nop") == 0)
3756 ip->insn_opcode = LOONGSON2F_NOP_INSN;
3757 }
3758
3759 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
3760 jr target pc &= 'hffff_ffff_cfff_ffff. */
3761
3762 static void
3763 fix_loongson2f_jump (struct mips_cl_insn * ip)
3764 {
3765 gas_assert (!HAVE_CODE_COMPRESSION);
3766 if (strcmp (ip->insn_mo->name, "j") == 0
3767 || strcmp (ip->insn_mo->name, "jr") == 0
3768 || strcmp (ip->insn_mo->name, "jalr") == 0)
3769 {
3770 int sreg;
3771 expressionS ep;
3772
3773 if (! mips_opts.at)
3774 return;
3775
3776 sreg = EXTRACT_OPERAND (0, RS, *ip);
3777 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
3778 return;
3779
3780 ep.X_op = O_constant;
3781 ep.X_add_number = 0xcfff0000;
3782 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
3783 ep.X_add_number = 0xffff;
3784 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
3785 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
3786 }
3787 }
3788
3789 static void
3790 fix_loongson2f (struct mips_cl_insn * ip)
3791 {
3792 if (mips_fix_loongson2f_nop)
3793 fix_loongson2f_nop (ip);
3794
3795 if (mips_fix_loongson2f_jump)
3796 fix_loongson2f_jump (ip);
3797 }
3798
3799 /* IP is a branch that has a delay slot, and we need to fill it
3800 automatically. Return true if we can do that by swapping IP
3801 with the previous instruction.
3802 ADDRESS_EXPR is an operand of the instruction to be used with
3803 RELOC_TYPE. */
3804
3805 static bfd_boolean
3806 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
3807 bfd_reloc_code_real_type *reloc_type)
3808 {
3809 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
3810 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
3811
3812 /* -O2 and above is required for this optimization. */
3813 if (mips_optimize < 2)
3814 return FALSE;
3815
3816 /* If we have seen .set volatile or .set nomove, don't optimize. */
3817 if (mips_opts.nomove)
3818 return FALSE;
3819
3820 /* We can't swap if the previous instruction's position is fixed. */
3821 if (history[0].fixed_p)
3822 return FALSE;
3823
3824 /* If the previous previous insn was in a .set noreorder, we can't
3825 swap. Actually, the MIPS assembler will swap in this situation.
3826 However, gcc configured -with-gnu-as will generate code like
3827
3828 .set noreorder
3829 lw $4,XXX
3830 .set reorder
3831 INSN
3832 bne $4,$0,foo
3833
3834 in which we can not swap the bne and INSN. If gcc is not configured
3835 -with-gnu-as, it does not output the .set pseudo-ops. */
3836 if (history[1].noreorder_p)
3837 return FALSE;
3838
3839 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
3840 This means that the previous instruction was a 4-byte one anyhow. */
3841 if (mips_opts.mips16 && history[0].fixp[0])
3842 return FALSE;
3843
3844 /* If the branch is itself the target of a branch, we can not swap.
3845 We cheat on this; all we check for is whether there is a label on
3846 this instruction. If there are any branches to anything other than
3847 a label, users must use .set noreorder. */
3848 if (seg_info (now_seg)->label_list)
3849 return FALSE;
3850
3851 /* If the previous instruction is in a variant frag other than this
3852 branch's one, we cannot do the swap. This does not apply to
3853 MIPS16 code, which uses variant frags for different purposes. */
3854 if (!mips_opts.mips16
3855 && history[0].frag
3856 && history[0].frag->fr_type == rs_machine_dependent)
3857 return FALSE;
3858
3859 /* We do not swap with instructions that cannot architecturally
3860 be placed in a branch delay slot, such as SYNC or ERET. We
3861 also refrain from swapping with a trap instruction, since it
3862 complicates trap handlers to have the trap instruction be in
3863 a delay slot. */
3864 prev_pinfo = history[0].insn_mo->pinfo;
3865 if (prev_pinfo & INSN_NO_DELAY_SLOT)
3866 return FALSE;
3867
3868 /* Check for conflicts between the branch and the instructions
3869 before the candidate delay slot. */
3870 if (nops_for_insn (0, history + 1, ip) > 0)
3871 return FALSE;
3872
3873 /* Check for conflicts between the swapped sequence and the
3874 target of the branch. */
3875 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
3876 return FALSE;
3877
3878 /* If the branch reads a register that the previous
3879 instruction sets, we can not swap. */
3880 gpr_read = gpr_read_mask (ip);
3881 prev_gpr_write = gpr_write_mask (&history[0]);
3882 if (gpr_read & prev_gpr_write)
3883 return FALSE;
3884
3885 /* If the branch writes a register that the previous
3886 instruction sets, we can not swap. */
3887 gpr_write = gpr_write_mask (ip);
3888 if (gpr_write & prev_gpr_write)
3889 return FALSE;
3890
3891 /* If the branch writes a register that the previous
3892 instruction reads, we can not swap. */
3893 prev_gpr_read = gpr_read_mask (&history[0]);
3894 if (gpr_write & prev_gpr_read)
3895 return FALSE;
3896
3897 /* If one instruction sets a condition code and the
3898 other one uses a condition code, we can not swap. */
3899 pinfo = ip->insn_mo->pinfo;
3900 if ((pinfo & INSN_READ_COND_CODE)
3901 && (prev_pinfo & INSN_WRITE_COND_CODE))
3902 return FALSE;
3903 if ((pinfo & INSN_WRITE_COND_CODE)
3904 && (prev_pinfo & INSN_READ_COND_CODE))
3905 return FALSE;
3906
3907 /* If the previous instruction uses the PC, we can not swap. */
3908 prev_pinfo2 = history[0].insn_mo->pinfo2;
3909 if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
3910 return FALSE;
3911 if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
3912 return FALSE;
3913
3914 /* If the previous instruction has an incorrect size for a fixed
3915 branch delay slot in microMIPS mode, we cannot swap. */
3916 pinfo2 = ip->insn_mo->pinfo2;
3917 if (mips_opts.micromips
3918 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
3919 && insn_length (history) != 2)
3920 return FALSE;
3921 if (mips_opts.micromips
3922 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
3923 && insn_length (history) != 4)
3924 return FALSE;
3925
3926 /* On R5900 short loops need to be fixed by inserting a nop in
3927 the branch delay slots.
3928 A short loop can be terminated too early. */
3929 if (mips_opts.arch == CPU_R5900
3930 /* Check if instruction has a parameter, ignore "j $31". */
3931 && (address_expr != NULL)
3932 /* Parameter must be 16 bit. */
3933 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
3934 /* Branch to same segment. */
3935 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
3936 /* Branch to same code fragment. */
3937 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
3938 /* Can only calculate branch offset if value is known. */
3939 && symbol_constant_p(address_expr->X_add_symbol)
3940 /* Check if branch is really conditional. */
3941 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
3942 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
3943 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
3944 {
3945 int distance;
3946 /* Check if loop is shorter than 6 instructions including
3947 branch and delay slot. */
3948 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
3949 if (distance <= 20)
3950 {
3951 int i;
3952 int rv;
3953
3954 rv = FALSE;
3955 /* When the loop includes branches or jumps,
3956 it is not a short loop. */
3957 for (i = 0; i < (distance / 4); i++)
3958 {
3959 if ((history[i].cleared_p)
3960 || delayed_branch_p(&history[i]))
3961 {
3962 rv = TRUE;
3963 break;
3964 }
3965 }
3966 if (rv == FALSE)
3967 {
3968 /* Insert nop after branch to fix short loop. */
3969 return FALSE;
3970 }
3971 }
3972 }
3973
3974 return TRUE;
3975 }
3976
3977 /* Decide how we should add IP to the instruction stream.
3978 ADDRESS_EXPR is an operand of the instruction to be used with
3979 RELOC_TYPE. */
3980
3981 static enum append_method
3982 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
3983 bfd_reloc_code_real_type *reloc_type)
3984 {
3985 unsigned long pinfo;
3986
3987 /* The relaxed version of a macro sequence must be inherently
3988 hazard-free. */
3989 if (mips_relax.sequence == 2)
3990 return APPEND_ADD;
3991
3992 /* We must not dabble with instructions in a ".set norerorder" block. */
3993 if (mips_opts.noreorder)
3994 return APPEND_ADD;
3995
3996 /* Otherwise, it's our responsibility to fill branch delay slots. */
3997 if (delayed_branch_p (ip))
3998 {
3999 if (!branch_likely_p (ip)
4000 && can_swap_branch_p (ip, address_expr, reloc_type))
4001 return APPEND_SWAP;
4002
4003 pinfo = ip->insn_mo->pinfo;
4004 if (mips_opts.mips16
4005 && ISA_SUPPORTS_MIPS16E
4006 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
4007 return APPEND_ADD_COMPACT;
4008
4009 return APPEND_ADD_WITH_NOP;
4010 }
4011
4012 return APPEND_ADD;
4013 }
4014
4015 /* IP is a MIPS16 instruction whose opcode we have just changed.
4016 Point IP->insn_mo to the new opcode's definition. */
4017
4018 static void
4019 find_altered_mips16_opcode (struct mips_cl_insn *ip)
4020 {
4021 const struct mips_opcode *mo, *end;
4022
4023 end = &mips16_opcodes[bfd_mips16_num_opcodes];
4024 for (mo = ip->insn_mo; mo < end; mo++)
4025 if ((ip->insn_opcode & mo->mask) == mo->match)
4026 {
4027 ip->insn_mo = mo;
4028 return;
4029 }
4030 abort ();
4031 }
4032
4033 /* For microMIPS macros, we need to generate a local number label
4034 as the target of branches. */
4035 #define MICROMIPS_LABEL_CHAR '\037'
4036 static unsigned long micromips_target_label;
4037 static char micromips_target_name[32];
4038
4039 static char *
4040 micromips_label_name (void)
4041 {
4042 char *p = micromips_target_name;
4043 char symbol_name_temporary[24];
4044 unsigned long l;
4045 int i;
4046
4047 if (*p)
4048 return p;
4049
4050 i = 0;
4051 l = micromips_target_label;
4052 #ifdef LOCAL_LABEL_PREFIX
4053 *p++ = LOCAL_LABEL_PREFIX;
4054 #endif
4055 *p++ = 'L';
4056 *p++ = MICROMIPS_LABEL_CHAR;
4057 do
4058 {
4059 symbol_name_temporary[i++] = l % 10 + '0';
4060 l /= 10;
4061 }
4062 while (l != 0);
4063 while (i > 0)
4064 *p++ = symbol_name_temporary[--i];
4065 *p = '\0';
4066
4067 return micromips_target_name;
4068 }
4069
4070 static void
4071 micromips_label_expr (expressionS *label_expr)
4072 {
4073 label_expr->X_op = O_symbol;
4074 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
4075 label_expr->X_add_number = 0;
4076 }
4077
4078 static void
4079 micromips_label_inc (void)
4080 {
4081 micromips_target_label++;
4082 *micromips_target_name = '\0';
4083 }
4084
4085 static void
4086 micromips_add_label (void)
4087 {
4088 symbolS *s;
4089
4090 s = colon (micromips_label_name ());
4091 micromips_label_inc ();
4092 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
4093 if (IS_ELF)
4094 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
4095 #else
4096 (void) s;
4097 #endif
4098 }
4099
4100 /* If assembling microMIPS code, then return the microMIPS reloc
4101 corresponding to the requested one if any. Otherwise return
4102 the reloc unchanged. */
4103
4104 static bfd_reloc_code_real_type
4105 micromips_map_reloc (bfd_reloc_code_real_type reloc)
4106 {
4107 static const bfd_reloc_code_real_type relocs[][2] =
4108 {
4109 /* Keep sorted incrementally by the left-hand key. */
4110 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
4111 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
4112 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
4113 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
4114 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
4115 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
4116 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
4117 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
4118 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
4119 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
4120 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
4121 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
4122 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
4123 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
4124 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4125 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4126 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4127 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4128 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4129 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4130 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4131 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4132 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4133 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4134 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4135 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4136 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4137 };
4138 bfd_reloc_code_real_type r;
4139 size_t i;
4140
4141 if (!mips_opts.micromips)
4142 return reloc;
4143 for (i = 0; i < ARRAY_SIZE (relocs); i++)
4144 {
4145 r = relocs[i][0];
4146 if (r > reloc)
4147 return reloc;
4148 if (r == reloc)
4149 return relocs[i][1];
4150 }
4151 return reloc;
4152 }
4153
4154 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
4155 Return true on success, storing the resolved value in RESULT. */
4156
4157 static bfd_boolean
4158 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
4159 offsetT *result)
4160 {
4161 switch (reloc)
4162 {
4163 case BFD_RELOC_MIPS_HIGHEST:
4164 case BFD_RELOC_MICROMIPS_HIGHEST:
4165 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
4166 return TRUE;
4167
4168 case BFD_RELOC_MIPS_HIGHER:
4169 case BFD_RELOC_MICROMIPS_HIGHER:
4170 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
4171 return TRUE;
4172
4173 case BFD_RELOC_HI16_S:
4174 case BFD_RELOC_MICROMIPS_HI16_S:
4175 case BFD_RELOC_MIPS16_HI16_S:
4176 *result = ((operand + 0x8000) >> 16) & 0xffff;
4177 return TRUE;
4178
4179 case BFD_RELOC_HI16:
4180 case BFD_RELOC_MICROMIPS_HI16:
4181 case BFD_RELOC_MIPS16_HI16:
4182 *result = (operand >> 16) & 0xffff;
4183 return TRUE;
4184
4185 case BFD_RELOC_LO16:
4186 case BFD_RELOC_MICROMIPS_LO16:
4187 case BFD_RELOC_MIPS16_LO16:
4188 *result = operand & 0xffff;
4189 return TRUE;
4190
4191 case BFD_RELOC_UNUSED:
4192 *result = operand;
4193 return TRUE;
4194
4195 default:
4196 return FALSE;
4197 }
4198 }
4199
4200 /* Output an instruction. IP is the instruction information.
4201 ADDRESS_EXPR is an operand of the instruction to be used with
4202 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
4203 a macro expansion. */
4204
4205 static void
4206 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
4207 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
4208 {
4209 unsigned long prev_pinfo2, pinfo;
4210 bfd_boolean relaxed_branch = FALSE;
4211 enum append_method method;
4212 bfd_boolean relax32;
4213 int branch_disp;
4214
4215 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
4216 fix_loongson2f (ip);
4217
4218 file_ase_mips16 |= mips_opts.mips16;
4219 file_ase_micromips |= mips_opts.micromips;
4220
4221 prev_pinfo2 = history[0].insn_mo->pinfo2;
4222 pinfo = ip->insn_mo->pinfo;
4223
4224 if (mips_opts.micromips
4225 && !expansionp
4226 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4227 && micromips_insn_length (ip->insn_mo) != 2)
4228 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4229 && micromips_insn_length (ip->insn_mo) != 4)))
4230 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4231 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
4232
4233 if (address_expr == NULL)
4234 ip->complete_p = 1;
4235 else if (reloc_type[0] <= BFD_RELOC_UNUSED
4236 && reloc_type[1] == BFD_RELOC_UNUSED
4237 && reloc_type[2] == BFD_RELOC_UNUSED
4238 && address_expr->X_op == O_constant)
4239 {
4240 switch (*reloc_type)
4241 {
4242 case BFD_RELOC_MIPS_JMP:
4243 {
4244 int shift;
4245
4246 shift = mips_opts.micromips ? 1 : 2;
4247 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4248 as_bad (_("jump to misaligned address (0x%lx)"),
4249 (unsigned long) address_expr->X_add_number);
4250 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4251 & 0x3ffffff);
4252 ip->complete_p = 1;
4253 }
4254 break;
4255
4256 case BFD_RELOC_MIPS16_JMP:
4257 if ((address_expr->X_add_number & 3) != 0)
4258 as_bad (_("jump to misaligned address (0x%lx)"),
4259 (unsigned long) address_expr->X_add_number);
4260 ip->insn_opcode |=
4261 (((address_expr->X_add_number & 0x7c0000) << 3)
4262 | ((address_expr->X_add_number & 0xf800000) >> 7)
4263 | ((address_expr->X_add_number & 0x3fffc) >> 2));
4264 ip->complete_p = 1;
4265 break;
4266
4267 case BFD_RELOC_16_PCREL_S2:
4268 {
4269 int shift;
4270
4271 shift = mips_opts.micromips ? 1 : 2;
4272 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4273 as_bad (_("branch to misaligned address (0x%lx)"),
4274 (unsigned long) address_expr->X_add_number);
4275 if (!mips_relax_branch)
4276 {
4277 if ((address_expr->X_add_number + (1 << (shift + 15)))
4278 & ~((1 << (shift + 16)) - 1))
4279 as_bad (_("branch address range overflow (0x%lx)"),
4280 (unsigned long) address_expr->X_add_number);
4281 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4282 & 0xffff);
4283 }
4284 }
4285 break;
4286
4287 default:
4288 {
4289 offsetT value;
4290
4291 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
4292 &value))
4293 {
4294 ip->insn_opcode |= value & 0xffff;
4295 ip->complete_p = 1;
4296 }
4297 }
4298 break;
4299 }
4300 }
4301
4302 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4303 {
4304 /* There are a lot of optimizations we could do that we don't.
4305 In particular, we do not, in general, reorder instructions.
4306 If you use gcc with optimization, it will reorder
4307 instructions and generally do much more optimization then we
4308 do here; repeating all that work in the assembler would only
4309 benefit hand written assembly code, and does not seem worth
4310 it. */
4311 int nops = (mips_optimize == 0
4312 ? nops_for_insn (0, history, NULL)
4313 : nops_for_insn_or_target (0, history, ip));
4314 if (nops > 0)
4315 {
4316 fragS *old_frag;
4317 unsigned long old_frag_offset;
4318 int i;
4319
4320 old_frag = frag_now;
4321 old_frag_offset = frag_now_fix ();
4322
4323 for (i = 0; i < nops; i++)
4324 add_fixed_insn (NOP_INSN);
4325 insert_into_history (0, nops, NOP_INSN);
4326
4327 if (listing)
4328 {
4329 listing_prev_line ();
4330 /* We may be at the start of a variant frag. In case we
4331 are, make sure there is enough space for the frag
4332 after the frags created by listing_prev_line. The
4333 argument to frag_grow here must be at least as large
4334 as the argument to all other calls to frag_grow in
4335 this file. We don't have to worry about being in the
4336 middle of a variant frag, because the variants insert
4337 all needed nop instructions themselves. */
4338 frag_grow (40);
4339 }
4340
4341 mips_move_text_labels ();
4342
4343 #ifndef NO_ECOFF_DEBUGGING
4344 if (ECOFF_DEBUGGING)
4345 ecoff_fix_loc (old_frag, old_frag_offset);
4346 #endif
4347 }
4348 }
4349 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4350 {
4351 int nops;
4352
4353 /* Work out how many nops in prev_nop_frag are needed by IP,
4354 ignoring hazards generated by the first prev_nop_frag_since
4355 instructions. */
4356 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
4357 gas_assert (nops <= prev_nop_frag_holds);
4358
4359 /* Enforce NOPS as a minimum. */
4360 if (nops > prev_nop_frag_required)
4361 prev_nop_frag_required = nops;
4362
4363 if (prev_nop_frag_holds == prev_nop_frag_required)
4364 {
4365 /* Settle for the current number of nops. Update the history
4366 accordingly (for the benefit of any future .set reorder code). */
4367 prev_nop_frag = NULL;
4368 insert_into_history (prev_nop_frag_since,
4369 prev_nop_frag_holds, NOP_INSN);
4370 }
4371 else
4372 {
4373 /* Allow this instruction to replace one of the nops that was
4374 tentatively added to prev_nop_frag. */
4375 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
4376 prev_nop_frag_holds--;
4377 prev_nop_frag_since++;
4378 }
4379 }
4380
4381 method = get_append_method (ip, address_expr, reloc_type);
4382 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
4383
4384 #ifdef OBJ_ELF
4385 dwarf2_emit_insn (0);
4386 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
4387 so "move" the instruction address accordingly.
4388
4389 Also, it doesn't seem appropriate for the assembler to reorder .loc
4390 entries. If this instruction is a branch that we are going to swap
4391 with the previous instruction, the two instructions should be
4392 treated as a unit, and the debug information for both instructions
4393 should refer to the start of the branch sequence. Using the
4394 current position is certainly wrong when swapping a 32-bit branch
4395 and a 16-bit delay slot, since the current position would then be
4396 in the middle of a branch. */
4397 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
4398 #endif
4399
4400 relax32 = (mips_relax_branch
4401 /* Don't try branch relaxation within .set nomacro, or within
4402 .set noat if we use $at for PIC computations. If it turns
4403 out that the branch was out-of-range, we'll get an error. */
4404 && !mips_opts.warn_about_macros
4405 && (mips_opts.at || mips_pic == NO_PIC)
4406 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
4407 as they have no complementing branches. */
4408 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
4409
4410 if (!HAVE_CODE_COMPRESSION
4411 && address_expr
4412 && relax32
4413 && *reloc_type == BFD_RELOC_16_PCREL_S2
4414 && delayed_branch_p (ip))
4415 {
4416 relaxed_branch = TRUE;
4417 add_relaxed_insn (ip, (relaxed_branch_length
4418 (NULL, NULL,
4419 uncond_branch_p (ip) ? -1
4420 : branch_likely_p (ip) ? 1
4421 : 0)), 4,
4422 RELAX_BRANCH_ENCODE
4423 (AT,
4424 uncond_branch_p (ip),
4425 branch_likely_p (ip),
4426 pinfo & INSN_WRITE_GPR_31,
4427 0),
4428 address_expr->X_add_symbol,
4429 address_expr->X_add_number);
4430 *reloc_type = BFD_RELOC_UNUSED;
4431 }
4432 else if (mips_opts.micromips
4433 && address_expr
4434 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4435 || *reloc_type > BFD_RELOC_UNUSED)
4436 && (delayed_branch_p (ip) || compact_branch_p (ip))
4437 /* Don't try branch relaxation when users specify
4438 16-bit/32-bit instructions. */
4439 && !forced_insn_length)
4440 {
4441 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4442 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
4443 int uncond = uncond_branch_p (ip) ? -1 : 0;
4444 int compact = compact_branch_p (ip);
4445 int al = pinfo & INSN_WRITE_GPR_31;
4446 int length32;
4447
4448 gas_assert (address_expr != NULL);
4449 gas_assert (!mips_relax.sequence);
4450
4451 relaxed_branch = TRUE;
4452 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4453 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
4454 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4455 relax32, 0, 0),
4456 address_expr->X_add_symbol,
4457 address_expr->X_add_number);
4458 *reloc_type = BFD_RELOC_UNUSED;
4459 }
4460 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
4461 {
4462 /* We need to set up a variant frag. */
4463 gas_assert (address_expr != NULL);
4464 add_relaxed_insn (ip, 4, 0,
4465 RELAX_MIPS16_ENCODE
4466 (*reloc_type - BFD_RELOC_UNUSED,
4467 forced_insn_length == 2, forced_insn_length == 4,
4468 delayed_branch_p (&history[0]),
4469 history[0].mips16_absolute_jump_p),
4470 make_expr_symbol (address_expr), 0);
4471 }
4472 else if (mips_opts.mips16 && insn_length (ip) == 2)
4473 {
4474 if (!delayed_branch_p (ip))
4475 /* Make sure there is enough room to swap this instruction with
4476 a following jump instruction. */
4477 frag_grow (6);
4478 add_fixed_insn (ip);
4479 }
4480 else
4481 {
4482 if (mips_opts.mips16
4483 && mips_opts.noreorder
4484 && delayed_branch_p (&history[0]))
4485 as_warn (_("extended instruction in delay slot"));
4486
4487 if (mips_relax.sequence)
4488 {
4489 /* If we've reached the end of this frag, turn it into a variant
4490 frag and record the information for the instructions we've
4491 written so far. */
4492 if (frag_room () < 4)
4493 relax_close_frag ();
4494 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4495 }
4496
4497 if (mips_relax.sequence != 2)
4498 {
4499 if (mips_macro_warning.first_insn_sizes[0] == 0)
4500 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4501 mips_macro_warning.sizes[0] += insn_length (ip);
4502 mips_macro_warning.insns[0]++;
4503 }
4504 if (mips_relax.sequence != 1)
4505 {
4506 if (mips_macro_warning.first_insn_sizes[1] == 0)
4507 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4508 mips_macro_warning.sizes[1] += insn_length (ip);
4509 mips_macro_warning.insns[1]++;
4510 }
4511
4512 if (mips_opts.mips16)
4513 {
4514 ip->fixed_p = 1;
4515 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4516 }
4517 add_fixed_insn (ip);
4518 }
4519
4520 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
4521 {
4522 bfd_reloc_code_real_type final_type[3];
4523 reloc_howto_type *howto0;
4524 reloc_howto_type *howto;
4525 int i;
4526
4527 /* Perform any necessary conversion to microMIPS relocations
4528 and find out how many relocations there actually are. */
4529 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4530 final_type[i] = micromips_map_reloc (reloc_type[i]);
4531
4532 /* In a compound relocation, it is the final (outermost)
4533 operator that determines the relocated field. */
4534 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4535
4536 if (howto == NULL)
4537 {
4538 /* To reproduce this failure try assembling gas/testsuites/
4539 gas/mips/mips16-intermix.s with a mips-ecoff targeted
4540 assembler. */
4541 as_bad (_("Unsupported MIPS relocation number %d"),
4542 final_type[i - 1]);
4543 howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
4544 }
4545
4546 if (i > 1)
4547 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
4548 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4549 bfd_get_reloc_size (howto),
4550 address_expr,
4551 howto0 && howto0->pc_relative,
4552 final_type[0]);
4553
4554 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
4555 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
4556 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4557
4558 /* These relocations can have an addend that won't fit in
4559 4 octets for 64bit assembly. */
4560 if (HAVE_64BIT_GPRS
4561 && ! howto->partial_inplace
4562 && (reloc_type[0] == BFD_RELOC_16
4563 || reloc_type[0] == BFD_RELOC_32
4564 || reloc_type[0] == BFD_RELOC_MIPS_JMP
4565 || reloc_type[0] == BFD_RELOC_GPREL16
4566 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4567 || reloc_type[0] == BFD_RELOC_GPREL32
4568 || reloc_type[0] == BFD_RELOC_64
4569 || reloc_type[0] == BFD_RELOC_CTOR
4570 || reloc_type[0] == BFD_RELOC_MIPS_SUB
4571 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4572 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4573 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4574 || reloc_type[0] == BFD_RELOC_MIPS_REL16
4575 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4576 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4577 || hi16_reloc_p (reloc_type[0])
4578 || lo16_reloc_p (reloc_type[0])))
4579 ip->fixp[0]->fx_no_overflow = 1;
4580
4581 /* These relocations can have an addend that won't fit in 2 octets. */
4582 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
4583 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
4584 ip->fixp[0]->fx_no_overflow = 1;
4585
4586 if (mips_relax.sequence)
4587 {
4588 if (mips_relax.first_fixup == 0)
4589 mips_relax.first_fixup = ip->fixp[0];
4590 }
4591 else if (reloc_needs_lo_p (*reloc_type))
4592 {
4593 struct mips_hi_fixup *hi_fixup;
4594
4595 /* Reuse the last entry if it already has a matching %lo. */
4596 hi_fixup = mips_hi_fixup_list;
4597 if (hi_fixup == 0
4598 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4599 {
4600 hi_fixup = ((struct mips_hi_fixup *)
4601 xmalloc (sizeof (struct mips_hi_fixup)));
4602 hi_fixup->next = mips_hi_fixup_list;
4603 mips_hi_fixup_list = hi_fixup;
4604 }
4605 hi_fixup->fixp = ip->fixp[0];
4606 hi_fixup->seg = now_seg;
4607 }
4608
4609 /* Add fixups for the second and third relocations, if given.
4610 Note that the ABI allows the second relocation to be
4611 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
4612 moment we only use RSS_UNDEF, but we could add support
4613 for the others if it ever becomes necessary. */
4614 for (i = 1; i < 3; i++)
4615 if (reloc_type[i] != BFD_RELOC_UNUSED)
4616 {
4617 ip->fixp[i] = fix_new (ip->frag, ip->where,
4618 ip->fixp[0]->fx_size, NULL, 0,
4619 FALSE, final_type[i]);
4620
4621 /* Use fx_tcbit to mark compound relocs. */
4622 ip->fixp[0]->fx_tcbit = 1;
4623 ip->fixp[i]->fx_tcbit = 1;
4624 }
4625 }
4626 install_insn (ip);
4627
4628 /* Update the register mask information. */
4629 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4630 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
4631
4632 switch (method)
4633 {
4634 case APPEND_ADD:
4635 insert_into_history (0, 1, ip);
4636 break;
4637
4638 case APPEND_ADD_WITH_NOP:
4639 {
4640 struct mips_cl_insn *nop;
4641
4642 insert_into_history (0, 1, ip);
4643 nop = get_delay_slot_nop (ip);
4644 add_fixed_insn (nop);
4645 insert_into_history (0, 1, nop);
4646 if (mips_relax.sequence)
4647 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4648 }
4649 break;
4650
4651 case APPEND_ADD_COMPACT:
4652 /* Convert MIPS16 jr/jalr into a "compact" jump. */
4653 gas_assert (mips_opts.mips16);
4654 ip->insn_opcode |= 0x0080;
4655 find_altered_mips16_opcode (ip);
4656 install_insn (ip);
4657 insert_into_history (0, 1, ip);
4658 break;
4659
4660 case APPEND_SWAP:
4661 {
4662 struct mips_cl_insn delay = history[0];
4663 if (mips_opts.mips16)
4664 {
4665 know (delay.frag == ip->frag);
4666 move_insn (ip, delay.frag, delay.where);
4667 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4668 }
4669 else if (relaxed_branch || delay.frag != ip->frag)
4670 {
4671 /* Add the delay slot instruction to the end of the
4672 current frag and shrink the fixed part of the
4673 original frag. If the branch occupies the tail of
4674 the latter, move it backwards to cover the gap. */
4675 delay.frag->fr_fix -= branch_disp;
4676 if (delay.frag == ip->frag)
4677 move_insn (ip, ip->frag, ip->where - branch_disp);
4678 add_fixed_insn (&delay);
4679 }
4680 else
4681 {
4682 move_insn (&delay, ip->frag,
4683 ip->where - branch_disp + insn_length (ip));
4684 move_insn (ip, history[0].frag, history[0].where);
4685 }
4686 history[0] = *ip;
4687 delay.fixed_p = 1;
4688 insert_into_history (0, 1, &delay);
4689 }
4690 break;
4691 }
4692
4693 /* If we have just completed an unconditional branch, clear the history. */
4694 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4695 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
4696 {
4697 unsigned int i;
4698
4699 mips_no_prev_insn ();
4700
4701 for (i = 0; i < ARRAY_SIZE (history); i++)
4702 history[i].cleared_p = 1;
4703 }
4704
4705 /* We need to emit a label at the end of branch-likely macros. */
4706 if (emit_branch_likely_macro)
4707 {
4708 emit_branch_likely_macro = FALSE;
4709 micromips_add_label ();
4710 }
4711
4712 /* We just output an insn, so the next one doesn't have a label. */
4713 mips_clear_insn_labels ();
4714 }
4715
4716 /* Forget that there was any previous instruction or label.
4717 When BRANCH is true, the branch history is also flushed. */
4718
4719 static void
4720 mips_no_prev_insn (void)
4721 {
4722 prev_nop_frag = NULL;
4723 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
4724 mips_clear_insn_labels ();
4725 }
4726
4727 /* This function must be called before we emit something other than
4728 instructions. It is like mips_no_prev_insn except that it inserts
4729 any NOPS that might be needed by previous instructions. */
4730
4731 void
4732 mips_emit_delays (void)
4733 {
4734 if (! mips_opts.noreorder)
4735 {
4736 int nops = nops_for_insn (0, history, NULL);
4737 if (nops > 0)
4738 {
4739 while (nops-- > 0)
4740 add_fixed_insn (NOP_INSN);
4741 mips_move_text_labels ();
4742 }
4743 }
4744 mips_no_prev_insn ();
4745 }
4746
4747 /* Start a (possibly nested) noreorder block. */
4748
4749 static void
4750 start_noreorder (void)
4751 {
4752 if (mips_opts.noreorder == 0)
4753 {
4754 unsigned int i;
4755 int nops;
4756
4757 /* None of the instructions before the .set noreorder can be moved. */
4758 for (i = 0; i < ARRAY_SIZE (history); i++)
4759 history[i].fixed_p = 1;
4760
4761 /* Insert any nops that might be needed between the .set noreorder
4762 block and the previous instructions. We will later remove any
4763 nops that turn out not to be needed. */
4764 nops = nops_for_insn (0, history, NULL);
4765 if (nops > 0)
4766 {
4767 if (mips_optimize != 0)
4768 {
4769 /* Record the frag which holds the nop instructions, so
4770 that we can remove them if we don't need them. */
4771 frag_grow (nops * NOP_INSN_SIZE);
4772 prev_nop_frag = frag_now;
4773 prev_nop_frag_holds = nops;
4774 prev_nop_frag_required = 0;
4775 prev_nop_frag_since = 0;
4776 }
4777
4778 for (; nops > 0; --nops)
4779 add_fixed_insn (NOP_INSN);
4780
4781 /* Move on to a new frag, so that it is safe to simply
4782 decrease the size of prev_nop_frag. */
4783 frag_wane (frag_now);
4784 frag_new (0);
4785 mips_move_text_labels ();
4786 }
4787 mips_mark_labels ();
4788 mips_clear_insn_labels ();
4789 }
4790 mips_opts.noreorder++;
4791 mips_any_noreorder = 1;
4792 }
4793
4794 /* End a nested noreorder block. */
4795
4796 static void
4797 end_noreorder (void)
4798 {
4799 mips_opts.noreorder--;
4800 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
4801 {
4802 /* Commit to inserting prev_nop_frag_required nops and go back to
4803 handling nop insertion the .set reorder way. */
4804 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
4805 * NOP_INSN_SIZE);
4806 insert_into_history (prev_nop_frag_since,
4807 prev_nop_frag_required, NOP_INSN);
4808 prev_nop_frag = NULL;
4809 }
4810 }
4811
4812 /* Set up global variables for the start of a new macro. */
4813
4814 static void
4815 macro_start (void)
4816 {
4817 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
4818 memset (&mips_macro_warning.first_insn_sizes, 0,
4819 sizeof (mips_macro_warning.first_insn_sizes));
4820 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
4821 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
4822 && delayed_branch_p (&history[0]));
4823 switch (history[0].insn_mo->pinfo2
4824 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
4825 {
4826 case INSN2_BRANCH_DELAY_32BIT:
4827 mips_macro_warning.delay_slot_length = 4;
4828 break;
4829 case INSN2_BRANCH_DELAY_16BIT:
4830 mips_macro_warning.delay_slot_length = 2;
4831 break;
4832 default:
4833 mips_macro_warning.delay_slot_length = 0;
4834 break;
4835 }
4836 mips_macro_warning.first_frag = NULL;
4837 }
4838
4839 /* Given that a macro is longer than one instruction or of the wrong size,
4840 return the appropriate warning for it. Return null if no warning is
4841 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
4842 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
4843 and RELAX_NOMACRO. */
4844
4845 static const char *
4846 macro_warning (relax_substateT subtype)
4847 {
4848 if (subtype & RELAX_DELAY_SLOT)
4849 return _("Macro instruction expanded into multiple instructions"
4850 " in a branch delay slot");
4851 else if (subtype & RELAX_NOMACRO)
4852 return _("Macro instruction expanded into multiple instructions");
4853 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
4854 | RELAX_DELAY_SLOT_SIZE_SECOND))
4855 return ((subtype & RELAX_DELAY_SLOT_16BIT)
4856 ? _("Macro instruction expanded into a wrong size instruction"
4857 " in a 16-bit branch delay slot")
4858 : _("Macro instruction expanded into a wrong size instruction"
4859 " in a 32-bit branch delay slot"));
4860 else
4861 return 0;
4862 }
4863
4864 /* Finish up a macro. Emit warnings as appropriate. */
4865
4866 static void
4867 macro_end (void)
4868 {
4869 /* Relaxation warning flags. */
4870 relax_substateT subtype = 0;
4871
4872 /* Check delay slot size requirements. */
4873 if (mips_macro_warning.delay_slot_length == 2)
4874 subtype |= RELAX_DELAY_SLOT_16BIT;
4875 if (mips_macro_warning.delay_slot_length != 0)
4876 {
4877 if (mips_macro_warning.delay_slot_length
4878 != mips_macro_warning.first_insn_sizes[0])
4879 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
4880 if (mips_macro_warning.delay_slot_length
4881 != mips_macro_warning.first_insn_sizes[1])
4882 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
4883 }
4884
4885 /* Check instruction count requirements. */
4886 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
4887 {
4888 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
4889 subtype |= RELAX_SECOND_LONGER;
4890 if (mips_opts.warn_about_macros)
4891 subtype |= RELAX_NOMACRO;
4892 if (mips_macro_warning.delay_slot_p)
4893 subtype |= RELAX_DELAY_SLOT;
4894 }
4895
4896 /* If both alternatives fail to fill a delay slot correctly,
4897 emit the warning now. */
4898 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
4899 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
4900 {
4901 relax_substateT s;
4902 const char *msg;
4903
4904 s = subtype & (RELAX_DELAY_SLOT_16BIT
4905 | RELAX_DELAY_SLOT_SIZE_FIRST
4906 | RELAX_DELAY_SLOT_SIZE_SECOND);
4907 msg = macro_warning (s);
4908 if (msg != NULL)
4909 as_warn ("%s", msg);
4910 subtype &= ~s;
4911 }
4912
4913 /* If both implementations are longer than 1 instruction, then emit the
4914 warning now. */
4915 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
4916 {
4917 relax_substateT s;
4918 const char *msg;
4919
4920 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
4921 msg = macro_warning (s);
4922 if (msg != NULL)
4923 as_warn ("%s", msg);
4924 subtype &= ~s;
4925 }
4926
4927 /* If any flags still set, then one implementation might need a warning
4928 and the other either will need one of a different kind or none at all.
4929 Pass any remaining flags over to relaxation. */
4930 if (mips_macro_warning.first_frag != NULL)
4931 mips_macro_warning.first_frag->fr_subtype |= subtype;
4932 }
4933
4934 /* Instruction operand formats used in macros that vary between
4935 standard MIPS and microMIPS code. */
4936
4937 static const char * const brk_fmt[2] = { "c", "mF" };
4938 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
4939 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
4940 static const char * const lui_fmt[2] = { "t,u", "s,u" };
4941 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
4942 static const char * const mfhl_fmt[2] = { "d", "mj" };
4943 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
4944 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
4945
4946 #define BRK_FMT (brk_fmt[mips_opts.micromips])
4947 #define COP12_FMT (cop12_fmt[mips_opts.micromips])
4948 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
4949 #define LUI_FMT (lui_fmt[mips_opts.micromips])
4950 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
4951 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips])
4952 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
4953 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
4954
4955 /* Read a macro's relocation codes from *ARGS and store them in *R.
4956 The first argument in *ARGS will be either the code for a single
4957 relocation or -1 followed by the three codes that make up a
4958 composite relocation. */
4959
4960 static void
4961 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
4962 {
4963 int i, next;
4964
4965 next = va_arg (*args, int);
4966 if (next >= 0)
4967 r[0] = (bfd_reloc_code_real_type) next;
4968 else
4969 for (i = 0; i < 3; i++)
4970 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
4971 }
4972
4973 /* Build an instruction created by a macro expansion. This is passed
4974 a pointer to the count of instructions created so far, an
4975 expression, the name of the instruction to build, an operand format
4976 string, and corresponding arguments. */
4977
4978 static void
4979 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
4980 {
4981 const struct mips_opcode *mo = NULL;
4982 bfd_reloc_code_real_type r[3];
4983 const struct mips_opcode *amo;
4984 struct hash_control *hash;
4985 struct mips_cl_insn insn;
4986 va_list args;
4987
4988 va_start (args, fmt);
4989
4990 if (mips_opts.mips16)
4991 {
4992 mips16_macro_build (ep, name, fmt, &args);
4993 va_end (args);
4994 return;
4995 }
4996
4997 r[0] = BFD_RELOC_UNUSED;
4998 r[1] = BFD_RELOC_UNUSED;
4999 r[2] = BFD_RELOC_UNUSED;
5000 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
5001 amo = (struct mips_opcode *) hash_find (hash, name);
5002 gas_assert (amo);
5003 gas_assert (strcmp (name, amo->name) == 0);
5004
5005 do
5006 {
5007 /* Search until we get a match for NAME. It is assumed here that
5008 macros will never generate MDMX, MIPS-3D, or MT instructions.
5009 We try to match an instruction that fulfils the branch delay
5010 slot instruction length requirement (if any) of the previous
5011 instruction. While doing this we record the first instruction
5012 seen that matches all the other conditions and use it anyway
5013 if the requirement cannot be met; we will issue an appropriate
5014 warning later on. */
5015 if (strcmp (fmt, amo->args) == 0
5016 && amo->pinfo != INSN_MACRO
5017 && is_opcode_valid (amo)
5018 && is_size_valid (amo))
5019 {
5020 if (is_delay_slot_valid (amo))
5021 {
5022 mo = amo;
5023 break;
5024 }
5025 else if (!mo)
5026 mo = amo;
5027 }
5028
5029 ++amo;
5030 gas_assert (amo->name);
5031 }
5032 while (strcmp (name, amo->name) == 0);
5033
5034 gas_assert (mo);
5035 create_insn (&insn, mo);
5036 for (;;)
5037 {
5038 switch (*fmt++)
5039 {
5040 case '\0':
5041 break;
5042
5043 case ',':
5044 case '(':
5045 case ')':
5046 continue;
5047
5048 case '+':
5049 switch (*fmt++)
5050 {
5051 case 'A':
5052 case 'E':
5053 INSERT_OPERAND (mips_opts.micromips,
5054 EXTLSB, insn, va_arg (args, int));
5055 continue;
5056
5057 case 'B':
5058 case 'F':
5059 /* Note that in the macro case, these arguments are already
5060 in MSB form. (When handling the instruction in the
5061 non-macro case, these arguments are sizes from which
5062 MSB values must be calculated.) */
5063 INSERT_OPERAND (mips_opts.micromips,
5064 INSMSB, insn, va_arg (args, int));
5065 continue;
5066
5067 case 'J':
5068 gas_assert (!mips_opts.micromips);
5069 INSERT_OPERAND (0, CODE10, insn, va_arg (args, int));
5070 continue;
5071
5072 case 'C':
5073 case 'G':
5074 case 'H':
5075 /* Note that in the macro case, these arguments are already
5076 in MSBD form. (When handling the instruction in the
5077 non-macro case, these arguments are sizes from which
5078 MSBD values must be calculated.) */
5079 INSERT_OPERAND (mips_opts.micromips,
5080 EXTMSBD, insn, va_arg (args, int));
5081 continue;
5082
5083 case 'Q':
5084 gas_assert (!mips_opts.micromips);
5085 INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
5086 continue;
5087
5088 case 'j':
5089 INSERT_OPERAND (mips_opts.micromips, EVAOFFSET, insn, va_arg (args, int));
5090 continue;
5091
5092 default:
5093 abort ();
5094 }
5095 continue;
5096
5097 case '2':
5098 INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
5099 continue;
5100
5101 case 'n':
5102 gas_assert (mips_opts.micromips);
5103 case 't':
5104 case 'w':
5105 case 'E':
5106 INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
5107 continue;
5108
5109 case 'c':
5110 gas_assert (!mips_opts.micromips);
5111 INSERT_OPERAND (0, CODE, insn, va_arg (args, int));
5112 continue;
5113
5114 case 'W':
5115 gas_assert (!mips_opts.micromips);
5116 case 'T':
5117 INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
5118 continue;
5119
5120 case 'G':
5121 if (mips_opts.micromips)
5122 INSERT_OPERAND (1, RS, insn, va_arg (args, int));
5123 else
5124 INSERT_OPERAND (0, RD, insn, va_arg (args, int));
5125 continue;
5126
5127 case 'K':
5128 gas_assert (!mips_opts.micromips);
5129 case 'd':
5130 INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
5131 continue;
5132
5133 case 'U':
5134 gas_assert (!mips_opts.micromips);
5135 {
5136 int tmp = va_arg (args, int);
5137
5138 INSERT_OPERAND (0, RT, insn, tmp);
5139 INSERT_OPERAND (0, RD, insn, tmp);
5140 }
5141 continue;
5142
5143 case 'V':
5144 case 'S':
5145 gas_assert (!mips_opts.micromips);
5146 INSERT_OPERAND (0, FS, insn, va_arg (args, int));
5147 continue;
5148
5149 case 'z':
5150 continue;
5151
5152 case '<':
5153 INSERT_OPERAND (mips_opts.micromips,
5154 SHAMT, insn, va_arg (args, int));
5155 continue;
5156
5157 case 'D':
5158 gas_assert (!mips_opts.micromips);
5159 INSERT_OPERAND (0, FD, insn, va_arg (args, int));
5160 continue;
5161
5162 case 'B':
5163 gas_assert (!mips_opts.micromips);
5164 INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
5165 continue;
5166
5167 case 'J':
5168 gas_assert (!mips_opts.micromips);
5169 INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
5170 continue;
5171
5172 case 'q':
5173 gas_assert (!mips_opts.micromips);
5174 INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
5175 continue;
5176
5177 case 'b':
5178 case 's':
5179 case 'r':
5180 case 'v':
5181 INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
5182 continue;
5183
5184 case 'i':
5185 case 'j':
5186 macro_read_relocs (&args, r);
5187 gas_assert (*r == BFD_RELOC_GPREL16
5188 || *r == BFD_RELOC_MIPS_HIGHER
5189 || *r == BFD_RELOC_HI16_S
5190 || *r == BFD_RELOC_LO16
5191 || *r == BFD_RELOC_MIPS_GOT_OFST);
5192 continue;
5193
5194 case 'o':
5195 macro_read_relocs (&args, r);
5196 continue;
5197
5198 case 'u':
5199 macro_read_relocs (&args, r);
5200 gas_assert (ep != NULL
5201 && (ep->X_op == O_constant
5202 || (ep->X_op == O_symbol
5203 && (*r == BFD_RELOC_MIPS_HIGHEST
5204 || *r == BFD_RELOC_HI16_S
5205 || *r == BFD_RELOC_HI16
5206 || *r == BFD_RELOC_GPREL16
5207 || *r == BFD_RELOC_MIPS_GOT_HI16
5208 || *r == BFD_RELOC_MIPS_CALL_HI16))));
5209 continue;
5210
5211 case 'p':
5212 gas_assert (ep != NULL);
5213
5214 /*
5215 * This allows macro() to pass an immediate expression for
5216 * creating short branches without creating a symbol.
5217 *
5218 * We don't allow branch relaxation for these branches, as
5219 * they should only appear in ".set nomacro" anyway.
5220 */
5221 if (ep->X_op == O_constant)
5222 {
5223 /* For microMIPS we always use relocations for branches.
5224 So we should not resolve immediate values. */
5225 gas_assert (!mips_opts.micromips);
5226
5227 if ((ep->X_add_number & 3) != 0)
5228 as_bad (_("branch to misaligned address (0x%lx)"),
5229 (unsigned long) ep->X_add_number);
5230 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5231 as_bad (_("branch address range overflow (0x%lx)"),
5232 (unsigned long) ep->X_add_number);
5233 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5234 ep = NULL;
5235 }
5236 else
5237 *r = BFD_RELOC_16_PCREL_S2;
5238 continue;
5239
5240 case 'a':
5241 gas_assert (ep != NULL);
5242 *r = BFD_RELOC_MIPS_JMP;
5243 continue;
5244
5245 case 'C':
5246 gas_assert (!mips_opts.micromips);
5247 INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
5248 continue;
5249
5250 case 'k':
5251 INSERT_OPERAND (mips_opts.micromips,
5252 CACHE, insn, va_arg (args, unsigned long));
5253 continue;
5254
5255 case '|':
5256 gas_assert (mips_opts.micromips);
5257 INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5258 continue;
5259
5260 case '.':
5261 gas_assert (mips_opts.micromips);
5262 INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5263 continue;
5264
5265 case '\\':
5266 INSERT_OPERAND (mips_opts.micromips,
5267 3BITPOS, insn, va_arg (args, unsigned int));
5268 continue;
5269
5270 case '~':
5271 INSERT_OPERAND (mips_opts.micromips,
5272 OFFSET12, insn, va_arg (args, unsigned long));
5273 continue;
5274
5275 case 'N':
5276 gas_assert (mips_opts.micromips);
5277 INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5278 continue;
5279
5280 case 'm': /* Opcode extension character. */
5281 gas_assert (mips_opts.micromips);
5282 switch (*fmt++)
5283 {
5284 case 'j':
5285 INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5286 break;
5287
5288 case 'p':
5289 INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5290 break;
5291
5292 case 'F':
5293 INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5294 break;
5295
5296 default:
5297 abort ();
5298 }
5299 continue;
5300
5301 default:
5302 abort ();
5303 }
5304 break;
5305 }
5306 va_end (args);
5307 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5308
5309 append_insn (&insn, ep, r, TRUE);
5310 }
5311
5312 static void
5313 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
5314 va_list *args)
5315 {
5316 struct mips_opcode *mo;
5317 struct mips_cl_insn insn;
5318 bfd_reloc_code_real_type r[3]
5319 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5320
5321 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
5322 gas_assert (mo);
5323 gas_assert (strcmp (name, mo->name) == 0);
5324
5325 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
5326 {
5327 ++mo;
5328 gas_assert (mo->name);
5329 gas_assert (strcmp (name, mo->name) == 0);
5330 }
5331
5332 create_insn (&insn, mo);
5333 for (;;)
5334 {
5335 int c;
5336
5337 c = *fmt++;
5338 switch (c)
5339 {
5340 case '\0':
5341 break;
5342
5343 case ',':
5344 case '(':
5345 case ')':
5346 continue;
5347
5348 case 'y':
5349 case 'w':
5350 MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
5351 continue;
5352
5353 case 'x':
5354 case 'v':
5355 MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
5356 continue;
5357
5358 case 'z':
5359 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
5360 continue;
5361
5362 case 'Z':
5363 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
5364 continue;
5365
5366 case '0':
5367 case 'S':
5368 case 'P':
5369 case 'R':
5370 continue;
5371
5372 case 'X':
5373 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
5374 continue;
5375
5376 case 'Y':
5377 {
5378 int regno;
5379
5380 regno = va_arg (*args, int);
5381 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
5382 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
5383 }
5384 continue;
5385
5386 case '<':
5387 case '>':
5388 case '4':
5389 case '5':
5390 case 'H':
5391 case 'W':
5392 case 'D':
5393 case 'j':
5394 case '8':
5395 case 'V':
5396 case 'C':
5397 case 'U':
5398 case 'k':
5399 case 'K':
5400 case 'p':
5401 case 'q':
5402 {
5403 offsetT value;
5404
5405 gas_assert (ep != NULL);
5406
5407 if (ep->X_op != O_constant)
5408 *r = (int) BFD_RELOC_UNUSED + c;
5409 else if (calculate_reloc (*r, ep->X_add_number, &value))
5410 {
5411 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
5412 ep = NULL;
5413 *r = BFD_RELOC_UNUSED;
5414 }
5415 }
5416 continue;
5417
5418 case '6':
5419 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
5420 continue;
5421 }
5422
5423 break;
5424 }
5425
5426 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5427
5428 append_insn (&insn, ep, r, TRUE);
5429 }
5430
5431 /*
5432 * Sign-extend 32-bit mode constants that have bit 31 set and all
5433 * higher bits unset.
5434 */
5435 static void
5436 normalize_constant_expr (expressionS *ex)
5437 {
5438 if (ex->X_op == O_constant
5439 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5440 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5441 - 0x80000000);
5442 }
5443
5444 /*
5445 * Sign-extend 32-bit mode address offsets that have bit 31 set and
5446 * all higher bits unset.
5447 */
5448 static void
5449 normalize_address_expr (expressionS *ex)
5450 {
5451 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5452 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5453 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5454 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5455 - 0x80000000);
5456 }
5457
5458 /*
5459 * Generate a "jalr" instruction with a relocation hint to the called
5460 * function. This occurs in NewABI PIC code.
5461 */
5462 static void
5463 macro_build_jalr (expressionS *ep, int cprestore)
5464 {
5465 static const bfd_reloc_code_real_type jalr_relocs[2]
5466 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5467 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5468 const char *jalr;
5469 char *f = NULL;
5470
5471 if (MIPS_JALR_HINT_P (ep))
5472 {
5473 frag_grow (8);
5474 f = frag_more (0);
5475 }
5476 if (mips_opts.micromips)
5477 {
5478 jalr = mips_opts.noreorder && !cprestore ? "jalr" : "jalrs";
5479 if (MIPS_JALR_HINT_P (ep)
5480 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
5481 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5482 else
5483 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5484 }
5485 else
5486 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
5487 if (MIPS_JALR_HINT_P (ep))
5488 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
5489 }
5490
5491 /*
5492 * Generate a "lui" instruction.
5493 */
5494 static void
5495 macro_build_lui (expressionS *ep, int regnum)
5496 {
5497 gas_assert (! mips_opts.mips16);
5498
5499 if (ep->X_op != O_constant)
5500 {
5501 gas_assert (ep->X_op == O_symbol);
5502 /* _gp_disp is a special case, used from s_cpload.
5503 __gnu_local_gp is used if mips_no_shared. */
5504 gas_assert (mips_pic == NO_PIC
5505 || (! HAVE_NEWABI
5506 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5507 || (! mips_in_shared
5508 && strcmp (S_GET_NAME (ep->X_add_symbol),
5509 "__gnu_local_gp") == 0));
5510 }
5511
5512 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
5513 }
5514
5515 /* Generate a sequence of instructions to do a load or store from a constant
5516 offset off of a base register (breg) into/from a target register (treg),
5517 using AT if necessary. */
5518 static void
5519 macro_build_ldst_constoffset (expressionS *ep, const char *op,
5520 int treg, int breg, int dbl)
5521 {
5522 gas_assert (ep->X_op == O_constant);
5523
5524 /* Sign-extending 32-bit constants makes their handling easier. */
5525 if (!dbl)
5526 normalize_constant_expr (ep);
5527
5528 /* Right now, this routine can only handle signed 32-bit constants. */
5529 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
5530 as_warn (_("operand overflow"));
5531
5532 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5533 {
5534 /* Signed 16-bit offset will fit in the op. Easy! */
5535 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
5536 }
5537 else
5538 {
5539 /* 32-bit offset, need multiple instructions and AT, like:
5540 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
5541 addu $tempreg,$tempreg,$breg
5542 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
5543 to handle the complete offset. */
5544 macro_build_lui (ep, AT);
5545 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5546 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
5547
5548 if (!mips_opts.at)
5549 as_bad (_("Macro used $at after \".set noat\""));
5550 }
5551 }
5552
5553 /* set_at()
5554 * Generates code to set the $at register to true (one)
5555 * if reg is less than the immediate expression.
5556 */
5557 static void
5558 set_at (int reg, int unsignedp)
5559 {
5560 if (imm_expr.X_op == O_constant
5561 && imm_expr.X_add_number >= -0x8000
5562 && imm_expr.X_add_number < 0x8000)
5563 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5564 AT, reg, BFD_RELOC_LO16);
5565 else
5566 {
5567 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5568 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
5569 }
5570 }
5571
5572 /* Warn if an expression is not a constant. */
5573
5574 static void
5575 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
5576 {
5577 if (ex->X_op == O_big)
5578 as_bad (_("unsupported large constant"));
5579 else if (ex->X_op != O_constant)
5580 as_bad (_("Instruction %s requires absolute expression"),
5581 ip->insn_mo->name);
5582
5583 if (HAVE_32BIT_GPRS)
5584 normalize_constant_expr (ex);
5585 }
5586
5587 /* Count the leading zeroes by performing a binary chop. This is a
5588 bulky bit of source, but performance is a LOT better for the
5589 majority of values than a simple loop to count the bits:
5590 for (lcnt = 0; (lcnt < 32); lcnt++)
5591 if ((v) & (1 << (31 - lcnt)))
5592 break;
5593 However it is not code size friendly, and the gain will drop a bit
5594 on certain cached systems.
5595 */
5596 #define COUNT_TOP_ZEROES(v) \
5597 (((v) & ~0xffff) == 0 \
5598 ? ((v) & ~0xff) == 0 \
5599 ? ((v) & ~0xf) == 0 \
5600 ? ((v) & ~0x3) == 0 \
5601 ? ((v) & ~0x1) == 0 \
5602 ? !(v) \
5603 ? 32 \
5604 : 31 \
5605 : 30 \
5606 : ((v) & ~0x7) == 0 \
5607 ? 29 \
5608 : 28 \
5609 : ((v) & ~0x3f) == 0 \
5610 ? ((v) & ~0x1f) == 0 \
5611 ? 27 \
5612 : 26 \
5613 : ((v) & ~0x7f) == 0 \
5614 ? 25 \
5615 : 24 \
5616 : ((v) & ~0xfff) == 0 \
5617 ? ((v) & ~0x3ff) == 0 \
5618 ? ((v) & ~0x1ff) == 0 \
5619 ? 23 \
5620 : 22 \
5621 : ((v) & ~0x7ff) == 0 \
5622 ? 21 \
5623 : 20 \
5624 : ((v) & ~0x3fff) == 0 \
5625 ? ((v) & ~0x1fff) == 0 \
5626 ? 19 \
5627 : 18 \
5628 : ((v) & ~0x7fff) == 0 \
5629 ? 17 \
5630 : 16 \
5631 : ((v) & ~0xffffff) == 0 \
5632 ? ((v) & ~0xfffff) == 0 \
5633 ? ((v) & ~0x3ffff) == 0 \
5634 ? ((v) & ~0x1ffff) == 0 \
5635 ? 15 \
5636 : 14 \
5637 : ((v) & ~0x7ffff) == 0 \
5638 ? 13 \
5639 : 12 \
5640 : ((v) & ~0x3fffff) == 0 \
5641 ? ((v) & ~0x1fffff) == 0 \
5642 ? 11 \
5643 : 10 \
5644 : ((v) & ~0x7fffff) == 0 \
5645 ? 9 \
5646 : 8 \
5647 : ((v) & ~0xfffffff) == 0 \
5648 ? ((v) & ~0x3ffffff) == 0 \
5649 ? ((v) & ~0x1ffffff) == 0 \
5650 ? 7 \
5651 : 6 \
5652 : ((v) & ~0x7ffffff) == 0 \
5653 ? 5 \
5654 : 4 \
5655 : ((v) & ~0x3fffffff) == 0 \
5656 ? ((v) & ~0x1fffffff) == 0 \
5657 ? 3 \
5658 : 2 \
5659 : ((v) & ~0x7fffffff) == 0 \
5660 ? 1 \
5661 : 0)
5662
5663 /* load_register()
5664 * This routine generates the least number of instructions necessary to load
5665 * an absolute expression value into a register.
5666 */
5667 static void
5668 load_register (int reg, expressionS *ep, int dbl)
5669 {
5670 int freg;
5671 expressionS hi32, lo32;
5672
5673 if (ep->X_op != O_big)
5674 {
5675 gas_assert (ep->X_op == O_constant);
5676
5677 /* Sign-extending 32-bit constants makes their handling easier. */
5678 if (!dbl)
5679 normalize_constant_expr (ep);
5680
5681 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
5682 {
5683 /* We can handle 16 bit signed values with an addiu to
5684 $zero. No need to ever use daddiu here, since $zero and
5685 the result are always correct in 32 bit mode. */
5686 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5687 return;
5688 }
5689 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5690 {
5691 /* We can handle 16 bit unsigned values with an ori to
5692 $zero. */
5693 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
5694 return;
5695 }
5696 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
5697 {
5698 /* 32 bit values require an lui. */
5699 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5700 if ((ep->X_add_number & 0xffff) != 0)
5701 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
5702 return;
5703 }
5704 }
5705
5706 /* The value is larger than 32 bits. */
5707
5708 if (!dbl || HAVE_32BIT_GPRS)
5709 {
5710 char value[32];
5711
5712 sprintf_vma (value, ep->X_add_number);
5713 as_bad (_("Number (0x%s) larger than 32 bits"), value);
5714 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5715 return;
5716 }
5717
5718 if (ep->X_op != O_big)
5719 {
5720 hi32 = *ep;
5721 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5722 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5723 hi32.X_add_number &= 0xffffffff;
5724 lo32 = *ep;
5725 lo32.X_add_number &= 0xffffffff;
5726 }
5727 else
5728 {
5729 gas_assert (ep->X_add_number > 2);
5730 if (ep->X_add_number == 3)
5731 generic_bignum[3] = 0;
5732 else if (ep->X_add_number > 4)
5733 as_bad (_("Number larger than 64 bits"));
5734 lo32.X_op = O_constant;
5735 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
5736 hi32.X_op = O_constant;
5737 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
5738 }
5739
5740 if (hi32.X_add_number == 0)
5741 freg = 0;
5742 else
5743 {
5744 int shift, bit;
5745 unsigned long hi, lo;
5746
5747 if (hi32.X_add_number == (offsetT) 0xffffffff)
5748 {
5749 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
5750 {
5751 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5752 return;
5753 }
5754 if (lo32.X_add_number & 0x80000000)
5755 {
5756 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5757 if (lo32.X_add_number & 0xffff)
5758 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
5759 return;
5760 }
5761 }
5762
5763 /* Check for 16bit shifted constant. We know that hi32 is
5764 non-zero, so start the mask on the first bit of the hi32
5765 value. */
5766 shift = 17;
5767 do
5768 {
5769 unsigned long himask, lomask;
5770
5771 if (shift < 32)
5772 {
5773 himask = 0xffff >> (32 - shift);
5774 lomask = (0xffff << shift) & 0xffffffff;
5775 }
5776 else
5777 {
5778 himask = 0xffff << (shift - 32);
5779 lomask = 0;
5780 }
5781 if ((hi32.X_add_number & ~(offsetT) himask) == 0
5782 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
5783 {
5784 expressionS tmp;
5785
5786 tmp.X_op = O_constant;
5787 if (shift < 32)
5788 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
5789 | (lo32.X_add_number >> shift));
5790 else
5791 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
5792 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
5793 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
5794 reg, reg, (shift >= 32) ? shift - 32 : shift);
5795 return;
5796 }
5797 ++shift;
5798 }
5799 while (shift <= (64 - 16));
5800
5801 /* Find the bit number of the lowest one bit, and store the
5802 shifted value in hi/lo. */
5803 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
5804 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
5805 if (lo != 0)
5806 {
5807 bit = 0;
5808 while ((lo & 1) == 0)
5809 {
5810 lo >>= 1;
5811 ++bit;
5812 }
5813 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
5814 hi >>= bit;
5815 }
5816 else
5817 {
5818 bit = 32;
5819 while ((hi & 1) == 0)
5820 {
5821 hi >>= 1;
5822 ++bit;
5823 }
5824 lo = hi;
5825 hi = 0;
5826 }
5827
5828 /* Optimize if the shifted value is a (power of 2) - 1. */
5829 if ((hi == 0 && ((lo + 1) & lo) == 0)
5830 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
5831 {
5832 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
5833 if (shift != 0)
5834 {
5835 expressionS tmp;
5836
5837 /* This instruction will set the register to be all
5838 ones. */
5839 tmp.X_op = O_constant;
5840 tmp.X_add_number = (offsetT) -1;
5841 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5842 if (bit != 0)
5843 {
5844 bit += shift;
5845 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
5846 reg, reg, (bit >= 32) ? bit - 32 : bit);
5847 }
5848 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
5849 reg, reg, (shift >= 32) ? shift - 32 : shift);
5850 return;
5851 }
5852 }
5853
5854 /* Sign extend hi32 before calling load_register, because we can
5855 generally get better code when we load a sign extended value. */
5856 if ((hi32.X_add_number & 0x80000000) != 0)
5857 hi32.X_add_number |= ~(offsetT) 0xffffffff;
5858 load_register (reg, &hi32, 0);
5859 freg = reg;
5860 }
5861 if ((lo32.X_add_number & 0xffff0000) == 0)
5862 {
5863 if (freg != 0)
5864 {
5865 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
5866 freg = reg;
5867 }
5868 }
5869 else
5870 {
5871 expressionS mid16;
5872
5873 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
5874 {
5875 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5876 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
5877 return;
5878 }
5879
5880 if (freg != 0)
5881 {
5882 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
5883 freg = reg;
5884 }
5885 mid16 = lo32;
5886 mid16.X_add_number >>= 16;
5887 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
5888 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5889 freg = reg;
5890 }
5891 if ((lo32.X_add_number & 0xffff) != 0)
5892 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
5893 }
5894
5895 static inline void
5896 load_delay_nop (void)
5897 {
5898 if (!gpr_interlocks)
5899 macro_build (NULL, "nop", "");
5900 }
5901
5902 /* Load an address into a register. */
5903
5904 static void
5905 load_address (int reg, expressionS *ep, int *used_at)
5906 {
5907 if (ep->X_op != O_constant
5908 && ep->X_op != O_symbol)
5909 {
5910 as_bad (_("expression too complex"));
5911 ep->X_op = O_constant;
5912 }
5913
5914 if (ep->X_op == O_constant)
5915 {
5916 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
5917 return;
5918 }
5919
5920 if (mips_pic == NO_PIC)
5921 {
5922 /* If this is a reference to a GP relative symbol, we want
5923 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
5924 Otherwise we want
5925 lui $reg,<sym> (BFD_RELOC_HI16_S)
5926 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
5927 If we have an addend, we always use the latter form.
5928
5929 With 64bit address space and a usable $at we want
5930 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5931 lui $at,<sym> (BFD_RELOC_HI16_S)
5932 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5933 daddiu $at,<sym> (BFD_RELOC_LO16)
5934 dsll32 $reg,0
5935 daddu $reg,$reg,$at
5936
5937 If $at is already in use, we use a path which is suboptimal
5938 on superscalar processors.
5939 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5940 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5941 dsll $reg,16
5942 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
5943 dsll $reg,16
5944 daddiu $reg,<sym> (BFD_RELOC_LO16)
5945
5946 For GP relative symbols in 64bit address space we can use
5947 the same sequence as in 32bit address space. */
5948 if (HAVE_64BIT_SYMBOLS)
5949 {
5950 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5951 && !nopic_need_relax (ep->X_add_symbol, 1))
5952 {
5953 relax_start (ep->X_add_symbol);
5954 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5955 mips_gp_register, BFD_RELOC_GPREL16);
5956 relax_switch ();
5957 }
5958
5959 if (*used_at == 0 && mips_opts.at)
5960 {
5961 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5962 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
5963 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5964 BFD_RELOC_MIPS_HIGHER);
5965 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
5966 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
5967 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
5968 *used_at = 1;
5969 }
5970 else
5971 {
5972 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5973 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5974 BFD_RELOC_MIPS_HIGHER);
5975 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5976 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
5977 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5978 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
5979 }
5980
5981 if (mips_relax.sequence)
5982 relax_end ();
5983 }
5984 else
5985 {
5986 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5987 && !nopic_need_relax (ep->X_add_symbol, 1))
5988 {
5989 relax_start (ep->X_add_symbol);
5990 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5991 mips_gp_register, BFD_RELOC_GPREL16);
5992 relax_switch ();
5993 }
5994 macro_build_lui (ep, reg);
5995 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
5996 reg, reg, BFD_RELOC_LO16);
5997 if (mips_relax.sequence)
5998 relax_end ();
5999 }
6000 }
6001 else if (!mips_big_got)
6002 {
6003 expressionS ex;
6004
6005 /* If this is a reference to an external symbol, we want
6006 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6007 Otherwise we want
6008 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6009 nop
6010 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
6011 If there is a constant, it must be added in after.
6012
6013 If we have NewABI, we want
6014 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
6015 unless we're referencing a global symbol with a non-zero
6016 offset, in which case cst must be added separately. */
6017 if (HAVE_NEWABI)
6018 {
6019 if (ep->X_add_number)
6020 {
6021 ex.X_add_number = ep->X_add_number;
6022 ep->X_add_number = 0;
6023 relax_start (ep->X_add_symbol);
6024 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6025 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
6026 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6027 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6028 ex.X_op = O_constant;
6029 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
6030 reg, reg, BFD_RELOC_LO16);
6031 ep->X_add_number = ex.X_add_number;
6032 relax_switch ();
6033 }
6034 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6035 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
6036 if (mips_relax.sequence)
6037 relax_end ();
6038 }
6039 else
6040 {
6041 ex.X_add_number = ep->X_add_number;
6042 ep->X_add_number = 0;
6043 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6044 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6045 load_delay_nop ();
6046 relax_start (ep->X_add_symbol);
6047 relax_switch ();
6048 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6049 BFD_RELOC_LO16);
6050 relax_end ();
6051
6052 if (ex.X_add_number != 0)
6053 {
6054 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6055 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6056 ex.X_op = O_constant;
6057 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
6058 reg, reg, BFD_RELOC_LO16);
6059 }
6060 }
6061 }
6062 else if (mips_big_got)
6063 {
6064 expressionS ex;
6065
6066 /* This is the large GOT case. If this is a reference to an
6067 external symbol, we want
6068 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6069 addu $reg,$reg,$gp
6070 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
6071
6072 Otherwise, for a reference to a local symbol in old ABI, we want
6073 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6074 nop
6075 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
6076 If there is a constant, it must be added in after.
6077
6078 In the NewABI, for local symbols, with or without offsets, we want:
6079 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6080 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
6081 */
6082 if (HAVE_NEWABI)
6083 {
6084 ex.X_add_number = ep->X_add_number;
6085 ep->X_add_number = 0;
6086 relax_start (ep->X_add_symbol);
6087 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
6088 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6089 reg, reg, mips_gp_register);
6090 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6091 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
6092 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6093 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6094 else if (ex.X_add_number)
6095 {
6096 ex.X_op = O_constant;
6097 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6098 BFD_RELOC_LO16);
6099 }
6100
6101 ep->X_add_number = ex.X_add_number;
6102 relax_switch ();
6103 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6104 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6105 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6106 BFD_RELOC_MIPS_GOT_OFST);
6107 relax_end ();
6108 }
6109 else
6110 {
6111 ex.X_add_number = ep->X_add_number;
6112 ep->X_add_number = 0;
6113 relax_start (ep->X_add_symbol);
6114 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
6115 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6116 reg, reg, mips_gp_register);
6117 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6118 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
6119 relax_switch ();
6120 if (reg_needs_delay (mips_gp_register))
6121 {
6122 /* We need a nop before loading from $gp. This special
6123 check is required because the lui which starts the main
6124 instruction stream does not refer to $gp, and so will not
6125 insert the nop which may be required. */
6126 macro_build (NULL, "nop", "");
6127 }
6128 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6129 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6130 load_delay_nop ();
6131 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6132 BFD_RELOC_LO16);
6133 relax_end ();
6134
6135 if (ex.X_add_number != 0)
6136 {
6137 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6138 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6139 ex.X_op = O_constant;
6140 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6141 BFD_RELOC_LO16);
6142 }
6143 }
6144 }
6145 else
6146 abort ();
6147
6148 if (!mips_opts.at && *used_at == 1)
6149 as_bad (_("Macro used $at after \".set noat\""));
6150 }
6151
6152 /* Move the contents of register SOURCE into register DEST. */
6153
6154 static void
6155 move_register (int dest, int source)
6156 {
6157 /* Prefer to use a 16-bit microMIPS instruction unless the previous
6158 instruction specifically requires a 32-bit one. */
6159 if (mips_opts.micromips
6160 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
6161 macro_build (NULL, "move", "mp,mj", dest, source);
6162 else
6163 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6164 dest, source, 0);
6165 }
6166
6167 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
6168 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6169 The two alternatives are:
6170
6171 Global symbol Local sybmol
6172 ------------- ------------
6173 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
6174 ... ...
6175 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6176
6177 load_got_offset emits the first instruction and add_got_offset
6178 emits the second for a 16-bit offset or add_got_offset_hilo emits
6179 a sequence to add a 32-bit offset using a scratch register. */
6180
6181 static void
6182 load_got_offset (int dest, expressionS *local)
6183 {
6184 expressionS global;
6185
6186 global = *local;
6187 global.X_add_number = 0;
6188
6189 relax_start (local->X_add_symbol);
6190 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6191 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6192 relax_switch ();
6193 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6194 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6195 relax_end ();
6196 }
6197
6198 static void
6199 add_got_offset (int dest, expressionS *local)
6200 {
6201 expressionS global;
6202
6203 global.X_op = O_constant;
6204 global.X_op_symbol = NULL;
6205 global.X_add_symbol = NULL;
6206 global.X_add_number = local->X_add_number;
6207
6208 relax_start (local->X_add_symbol);
6209 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
6210 dest, dest, BFD_RELOC_LO16);
6211 relax_switch ();
6212 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
6213 relax_end ();
6214 }
6215
6216 static void
6217 add_got_offset_hilo (int dest, expressionS *local, int tmp)
6218 {
6219 expressionS global;
6220 int hold_mips_optimize;
6221
6222 global.X_op = O_constant;
6223 global.X_op_symbol = NULL;
6224 global.X_add_symbol = NULL;
6225 global.X_add_number = local->X_add_number;
6226
6227 relax_start (local->X_add_symbol);
6228 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6229 relax_switch ();
6230 /* Set mips_optimize around the lui instruction to avoid
6231 inserting an unnecessary nop after the lw. */
6232 hold_mips_optimize = mips_optimize;
6233 mips_optimize = 2;
6234 macro_build_lui (&global, tmp);
6235 mips_optimize = hold_mips_optimize;
6236 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6237 relax_end ();
6238
6239 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6240 }
6241
6242 /* Emit a sequence of instructions to emulate a branch likely operation.
6243 BR is an ordinary branch corresponding to one to be emulated. BRNEG
6244 is its complementing branch with the original condition negated.
6245 CALL is set if the original branch specified the link operation.
6246 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6247
6248 Code like this is produced in the noreorder mode:
6249
6250 BRNEG <args>, 1f
6251 nop
6252 b <sym>
6253 delay slot (executed only if branch taken)
6254 1:
6255
6256 or, if CALL is set:
6257
6258 BRNEG <args>, 1f
6259 nop
6260 bal <sym>
6261 delay slot (executed only if branch taken)
6262 1:
6263
6264 In the reorder mode the delay slot would be filled with a nop anyway,
6265 so code produced is simply:
6266
6267 BR <args>, <sym>
6268 nop
6269
6270 This function is used when producing code for the microMIPS ASE that
6271 does not implement branch likely instructions in hardware. */
6272
6273 static void
6274 macro_build_branch_likely (const char *br, const char *brneg,
6275 int call, expressionS *ep, const char *fmt,
6276 unsigned int sreg, unsigned int treg)
6277 {
6278 int noreorder = mips_opts.noreorder;
6279 expressionS expr1;
6280
6281 gas_assert (mips_opts.micromips);
6282 start_noreorder ();
6283 if (noreorder)
6284 {
6285 micromips_label_expr (&expr1);
6286 macro_build (&expr1, brneg, fmt, sreg, treg);
6287 macro_build (NULL, "nop", "");
6288 macro_build (ep, call ? "bal" : "b", "p");
6289
6290 /* Set to true so that append_insn adds a label. */
6291 emit_branch_likely_macro = TRUE;
6292 }
6293 else
6294 {
6295 macro_build (ep, br, fmt, sreg, treg);
6296 macro_build (NULL, "nop", "");
6297 }
6298 end_noreorder ();
6299 }
6300
6301 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6302 the condition code tested. EP specifies the branch target. */
6303
6304 static void
6305 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6306 {
6307 const int call = 0;
6308 const char *brneg;
6309 const char *br;
6310
6311 switch (type)
6312 {
6313 case M_BC1FL:
6314 br = "bc1f";
6315 brneg = "bc1t";
6316 break;
6317 case M_BC1TL:
6318 br = "bc1t";
6319 brneg = "bc1f";
6320 break;
6321 case M_BC2FL:
6322 br = "bc2f";
6323 brneg = "bc2t";
6324 break;
6325 case M_BC2TL:
6326 br = "bc2t";
6327 brneg = "bc2f";
6328 break;
6329 default:
6330 abort ();
6331 }
6332 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6333 }
6334
6335 /* Emit a two-argument branch macro specified by TYPE, using SREG as
6336 the register tested. EP specifies the branch target. */
6337
6338 static void
6339 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6340 {
6341 const char *brneg = NULL;
6342 const char *br;
6343 int call = 0;
6344
6345 switch (type)
6346 {
6347 case M_BGEZ:
6348 br = "bgez";
6349 break;
6350 case M_BGEZL:
6351 br = mips_opts.micromips ? "bgez" : "bgezl";
6352 brneg = "bltz";
6353 break;
6354 case M_BGEZALL:
6355 gas_assert (mips_opts.micromips);
6356 br = "bgezals";
6357 brneg = "bltz";
6358 call = 1;
6359 break;
6360 case M_BGTZ:
6361 br = "bgtz";
6362 break;
6363 case M_BGTZL:
6364 br = mips_opts.micromips ? "bgtz" : "bgtzl";
6365 brneg = "blez";
6366 break;
6367 case M_BLEZ:
6368 br = "blez";
6369 break;
6370 case M_BLEZL:
6371 br = mips_opts.micromips ? "blez" : "blezl";
6372 brneg = "bgtz";
6373 break;
6374 case M_BLTZ:
6375 br = "bltz";
6376 break;
6377 case M_BLTZL:
6378 br = mips_opts.micromips ? "bltz" : "bltzl";
6379 brneg = "bgez";
6380 break;
6381 case M_BLTZALL:
6382 gas_assert (mips_opts.micromips);
6383 br = "bltzals";
6384 brneg = "bgez";
6385 call = 1;
6386 break;
6387 default:
6388 abort ();
6389 }
6390 if (mips_opts.micromips && brneg)
6391 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6392 else
6393 macro_build (ep, br, "s,p", sreg);
6394 }
6395
6396 /* Emit a three-argument branch macro specified by TYPE, using SREG and
6397 TREG as the registers tested. EP specifies the branch target. */
6398
6399 static void
6400 macro_build_branch_rsrt (int type, expressionS *ep,
6401 unsigned int sreg, unsigned int treg)
6402 {
6403 const char *brneg = NULL;
6404 const int call = 0;
6405 const char *br;
6406
6407 switch (type)
6408 {
6409 case M_BEQ:
6410 case M_BEQ_I:
6411 br = "beq";
6412 break;
6413 case M_BEQL:
6414 case M_BEQL_I:
6415 br = mips_opts.micromips ? "beq" : "beql";
6416 brneg = "bne";
6417 break;
6418 case M_BNE:
6419 case M_BNE_I:
6420 br = "bne";
6421 break;
6422 case M_BNEL:
6423 case M_BNEL_I:
6424 br = mips_opts.micromips ? "bne" : "bnel";
6425 brneg = "beq";
6426 break;
6427 default:
6428 abort ();
6429 }
6430 if (mips_opts.micromips && brneg)
6431 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6432 else
6433 macro_build (ep, br, "s,t,p", sreg, treg);
6434 }
6435
6436 /*
6437 * Build macros
6438 * This routine implements the seemingly endless macro or synthesized
6439 * instructions and addressing modes in the mips assembly language. Many
6440 * of these macros are simple and are similar to each other. These could
6441 * probably be handled by some kind of table or grammar approach instead of
6442 * this verbose method. Others are not simple macros but are more like
6443 * optimizing code generation.
6444 * One interesting optimization is when several store macros appear
6445 * consecutively that would load AT with the upper half of the same address.
6446 * The ensuing load upper instructions are ommited. This implies some kind
6447 * of global optimization. We currently only optimize within a single macro.
6448 * For many of the load and store macros if the address is specified as a
6449 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6450 * first load register 'at' with zero and use it as the base register. The
6451 * mips assembler simply uses register $zero. Just one tiny optimization
6452 * we're missing.
6453 */
6454 static void
6455 macro (struct mips_cl_insn *ip)
6456 {
6457 unsigned int treg, sreg, dreg, breg;
6458 unsigned int tempreg;
6459 int mask;
6460 int used_at = 0;
6461 expressionS label_expr;
6462 expressionS expr1;
6463 expressionS *ep;
6464 const char *s;
6465 const char *s2;
6466 const char *fmt;
6467 int likely = 0;
6468 int coproc = 0;
6469 int offbits = 16;
6470 int call = 0;
6471 int jals = 0;
6472 int dbl = 0;
6473 int imm = 0;
6474 int ust = 0;
6475 int lp = 0;
6476 int ab = 0;
6477 int off;
6478 offsetT maxnum;
6479 bfd_reloc_code_real_type r;
6480 int hold_mips_optimize;
6481
6482 gas_assert (! mips_opts.mips16);
6483
6484 treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6485 dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6486 sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
6487 mask = ip->insn_mo->mask;
6488
6489 label_expr.X_op = O_constant;
6490 label_expr.X_op_symbol = NULL;
6491 label_expr.X_add_symbol = NULL;
6492 label_expr.X_add_number = 0;
6493
6494 expr1.X_op = O_constant;
6495 expr1.X_op_symbol = NULL;
6496 expr1.X_add_symbol = NULL;
6497 expr1.X_add_number = 1;
6498
6499 switch (mask)
6500 {
6501 case M_DABS:
6502 dbl = 1;
6503 case M_ABS:
6504 /* bgez $a0,1f
6505 move v0,$a0
6506 sub v0,$zero,$a0
6507 1:
6508 */
6509
6510 start_noreorder ();
6511
6512 if (mips_opts.micromips)
6513 micromips_label_expr (&label_expr);
6514 else
6515 label_expr.X_add_number = 8;
6516 macro_build (&label_expr, "bgez", "s,p", sreg);
6517 if (dreg == sreg)
6518 macro_build (NULL, "nop", "");
6519 else
6520 move_register (dreg, sreg);
6521 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
6522 if (mips_opts.micromips)
6523 micromips_add_label ();
6524
6525 end_noreorder ();
6526 break;
6527
6528 case M_ADD_I:
6529 s = "addi";
6530 s2 = "add";
6531 goto do_addi;
6532 case M_ADDU_I:
6533 s = "addiu";
6534 s2 = "addu";
6535 goto do_addi;
6536 case M_DADD_I:
6537 dbl = 1;
6538 s = "daddi";
6539 s2 = "dadd";
6540 if (!mips_opts.micromips)
6541 goto do_addi;
6542 if (imm_expr.X_op == O_constant
6543 && imm_expr.X_add_number >= -0x200
6544 && imm_expr.X_add_number < 0x200)
6545 {
6546 macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6547 break;
6548 }
6549 goto do_addi_i;
6550 case M_DADDU_I:
6551 dbl = 1;
6552 s = "daddiu";
6553 s2 = "daddu";
6554 do_addi:
6555 if (imm_expr.X_op == O_constant
6556 && imm_expr.X_add_number >= -0x8000
6557 && imm_expr.X_add_number < 0x8000)
6558 {
6559 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
6560 break;
6561 }
6562 do_addi_i:
6563 used_at = 1;
6564 load_register (AT, &imm_expr, dbl);
6565 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6566 break;
6567
6568 case M_AND_I:
6569 s = "andi";
6570 s2 = "and";
6571 goto do_bit;
6572 case M_OR_I:
6573 s = "ori";
6574 s2 = "or";
6575 goto do_bit;
6576 case M_NOR_I:
6577 s = "";
6578 s2 = "nor";
6579 goto do_bit;
6580 case M_XOR_I:
6581 s = "xori";
6582 s2 = "xor";
6583 do_bit:
6584 if (imm_expr.X_op == O_constant
6585 && imm_expr.X_add_number >= 0
6586 && imm_expr.X_add_number < 0x10000)
6587 {
6588 if (mask != M_NOR_I)
6589 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
6590 else
6591 {
6592 macro_build (&imm_expr, "ori", "t,r,i",
6593 treg, sreg, BFD_RELOC_LO16);
6594 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
6595 }
6596 break;
6597 }
6598
6599 used_at = 1;
6600 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6601 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6602 break;
6603
6604 case M_BALIGN:
6605 switch (imm_expr.X_add_number)
6606 {
6607 case 0:
6608 macro_build (NULL, "nop", "");
6609 break;
6610 case 2:
6611 macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6612 break;
6613 case 1:
6614 case 3:
6615 macro_build (NULL, "balign", "t,s,2", treg, sreg,
6616 (int) imm_expr.X_add_number);
6617 break;
6618 default:
6619 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6620 (unsigned long) imm_expr.X_add_number);
6621 break;
6622 }
6623 break;
6624
6625 case M_BC1FL:
6626 case M_BC1TL:
6627 case M_BC2FL:
6628 case M_BC2TL:
6629 gas_assert (mips_opts.micromips);
6630 macro_build_branch_ccl (mask, &offset_expr,
6631 EXTRACT_OPERAND (1, BCC, *ip));
6632 break;
6633
6634 case M_BEQ_I:
6635 case M_BEQL_I:
6636 case M_BNE_I:
6637 case M_BNEL_I:
6638 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6639 treg = 0;
6640 else
6641 {
6642 treg = AT;
6643 used_at = 1;
6644 load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
6645 }
6646 /* Fall through. */
6647 case M_BEQL:
6648 case M_BNEL:
6649 macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
6650 break;
6651
6652 case M_BGEL:
6653 likely = 1;
6654 case M_BGE:
6655 if (treg == 0)
6656 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6657 else if (sreg == 0)
6658 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6659 else
6660 {
6661 used_at = 1;
6662 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6663 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6664 &offset_expr, AT, ZERO);
6665 }
6666 break;
6667
6668 case M_BGEZL:
6669 case M_BGEZALL:
6670 case M_BGTZL:
6671 case M_BLEZL:
6672 case M_BLTZL:
6673 case M_BLTZALL:
6674 macro_build_branch_rs (mask, &offset_expr, sreg);
6675 break;
6676
6677 case M_BGTL_I:
6678 likely = 1;
6679 case M_BGT_I:
6680 /* Check for > max integer. */
6681 maxnum = 0x7fffffff;
6682 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6683 {
6684 maxnum <<= 16;
6685 maxnum |= 0xffff;
6686 maxnum <<= 16;
6687 maxnum |= 0xffff;
6688 }
6689 if (imm_expr.X_op == O_constant
6690 && imm_expr.X_add_number >= maxnum
6691 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6692 {
6693 do_false:
6694 /* Result is always false. */
6695 if (! likely)
6696 macro_build (NULL, "nop", "");
6697 else
6698 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
6699 break;
6700 }
6701 if (imm_expr.X_op != O_constant)
6702 as_bad (_("Unsupported large constant"));
6703 ++imm_expr.X_add_number;
6704 /* FALLTHROUGH */
6705 case M_BGE_I:
6706 case M_BGEL_I:
6707 if (mask == M_BGEL_I)
6708 likely = 1;
6709 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6710 {
6711 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
6712 &offset_expr, sreg);
6713 break;
6714 }
6715 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6716 {
6717 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
6718 &offset_expr, sreg);
6719 break;
6720 }
6721 maxnum = 0x7fffffff;
6722 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6723 {
6724 maxnum <<= 16;
6725 maxnum |= 0xffff;
6726 maxnum <<= 16;
6727 maxnum |= 0xffff;
6728 }
6729 maxnum = - maxnum - 1;
6730 if (imm_expr.X_op == O_constant
6731 && imm_expr.X_add_number <= maxnum
6732 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6733 {
6734 do_true:
6735 /* result is always true */
6736 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
6737 macro_build (&offset_expr, "b", "p");
6738 break;
6739 }
6740 used_at = 1;
6741 set_at (sreg, 0);
6742 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6743 &offset_expr, AT, ZERO);
6744 break;
6745
6746 case M_BGEUL:
6747 likely = 1;
6748 case M_BGEU:
6749 if (treg == 0)
6750 goto do_true;
6751 else if (sreg == 0)
6752 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6753 &offset_expr, ZERO, treg);
6754 else
6755 {
6756 used_at = 1;
6757 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6758 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6759 &offset_expr, AT, ZERO);
6760 }
6761 break;
6762
6763 case M_BGTUL_I:
6764 likely = 1;
6765 case M_BGTU_I:
6766 if (sreg == 0
6767 || (HAVE_32BIT_GPRS
6768 && imm_expr.X_op == O_constant
6769 && imm_expr.X_add_number == -1))
6770 goto do_false;
6771 if (imm_expr.X_op != O_constant)
6772 as_bad (_("Unsupported large constant"));
6773 ++imm_expr.X_add_number;
6774 /* FALLTHROUGH */
6775 case M_BGEU_I:
6776 case M_BGEUL_I:
6777 if (mask == M_BGEUL_I)
6778 likely = 1;
6779 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6780 goto do_true;
6781 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6782 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6783 &offset_expr, sreg, ZERO);
6784 else
6785 {
6786 used_at = 1;
6787 set_at (sreg, 1);
6788 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6789 &offset_expr, AT, ZERO);
6790 }
6791 break;
6792
6793 case M_BGTL:
6794 likely = 1;
6795 case M_BGT:
6796 if (treg == 0)
6797 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
6798 else if (sreg == 0)
6799 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
6800 else
6801 {
6802 used_at = 1;
6803 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6804 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6805 &offset_expr, AT, ZERO);
6806 }
6807 break;
6808
6809 case M_BGTUL:
6810 likely = 1;
6811 case M_BGTU:
6812 if (treg == 0)
6813 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6814 &offset_expr, sreg, ZERO);
6815 else if (sreg == 0)
6816 goto do_false;
6817 else
6818 {
6819 used_at = 1;
6820 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6821 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6822 &offset_expr, AT, ZERO);
6823 }
6824 break;
6825
6826 case M_BLEL:
6827 likely = 1;
6828 case M_BLE:
6829 if (treg == 0)
6830 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6831 else if (sreg == 0)
6832 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
6833 else
6834 {
6835 used_at = 1;
6836 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6837 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6838 &offset_expr, AT, ZERO);
6839 }
6840 break;
6841
6842 case M_BLEL_I:
6843 likely = 1;
6844 case M_BLE_I:
6845 maxnum = 0x7fffffff;
6846 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6847 {
6848 maxnum <<= 16;
6849 maxnum |= 0xffff;
6850 maxnum <<= 16;
6851 maxnum |= 0xffff;
6852 }
6853 if (imm_expr.X_op == O_constant
6854 && imm_expr.X_add_number >= maxnum
6855 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6856 goto do_true;
6857 if (imm_expr.X_op != O_constant)
6858 as_bad (_("Unsupported large constant"));
6859 ++imm_expr.X_add_number;
6860 /* FALLTHROUGH */
6861 case M_BLT_I:
6862 case M_BLTL_I:
6863 if (mask == M_BLTL_I)
6864 likely = 1;
6865 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6866 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6867 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6868 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6869 else
6870 {
6871 used_at = 1;
6872 set_at (sreg, 0);
6873 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6874 &offset_expr, AT, ZERO);
6875 }
6876 break;
6877
6878 case M_BLEUL:
6879 likely = 1;
6880 case M_BLEU:
6881 if (treg == 0)
6882 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6883 &offset_expr, sreg, ZERO);
6884 else if (sreg == 0)
6885 goto do_true;
6886 else
6887 {
6888 used_at = 1;
6889 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6890 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6891 &offset_expr, AT, ZERO);
6892 }
6893 break;
6894
6895 case M_BLEUL_I:
6896 likely = 1;
6897 case M_BLEU_I:
6898 if (sreg == 0
6899 || (HAVE_32BIT_GPRS
6900 && imm_expr.X_op == O_constant
6901 && imm_expr.X_add_number == -1))
6902 goto do_true;
6903 if (imm_expr.X_op != O_constant)
6904 as_bad (_("Unsupported large constant"));
6905 ++imm_expr.X_add_number;
6906 /* FALLTHROUGH */
6907 case M_BLTU_I:
6908 case M_BLTUL_I:
6909 if (mask == M_BLTUL_I)
6910 likely = 1;
6911 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6912 goto do_false;
6913 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6914 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6915 &offset_expr, sreg, ZERO);
6916 else
6917 {
6918 used_at = 1;
6919 set_at (sreg, 1);
6920 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6921 &offset_expr, AT, ZERO);
6922 }
6923 break;
6924
6925 case M_BLTL:
6926 likely = 1;
6927 case M_BLT:
6928 if (treg == 0)
6929 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6930 else if (sreg == 0)
6931 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
6932 else
6933 {
6934 used_at = 1;
6935 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6936 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6937 &offset_expr, AT, ZERO);
6938 }
6939 break;
6940
6941 case M_BLTUL:
6942 likely = 1;
6943 case M_BLTU:
6944 if (treg == 0)
6945 goto do_false;
6946 else if (sreg == 0)
6947 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6948 &offset_expr, ZERO, treg);
6949 else
6950 {
6951 used_at = 1;
6952 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6953 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6954 &offset_expr, AT, ZERO);
6955 }
6956 break;
6957
6958 case M_DEXT:
6959 {
6960 /* Use unsigned arithmetic. */
6961 addressT pos;
6962 addressT size;
6963
6964 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
6965 {
6966 as_bad (_("Unsupported large constant"));
6967 pos = size = 1;
6968 }
6969 else
6970 {
6971 pos = imm_expr.X_add_number;
6972 size = imm2_expr.X_add_number;
6973 }
6974
6975 if (pos > 63)
6976 {
6977 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
6978 pos = 1;
6979 }
6980 if (size == 0 || size > 64 || (pos + size - 1) > 63)
6981 {
6982 as_bad (_("Improper extract size (%lu, position %lu)"),
6983 (unsigned long) size, (unsigned long) pos);
6984 size = 1;
6985 }
6986
6987 if (size <= 32 && pos < 32)
6988 {
6989 s = "dext";
6990 fmt = "t,r,+A,+C";
6991 }
6992 else if (size <= 32)
6993 {
6994 s = "dextu";
6995 fmt = "t,r,+E,+H";
6996 }
6997 else
6998 {
6999 s = "dextm";
7000 fmt = "t,r,+A,+G";
7001 }
7002 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
7003 (int) (size - 1));
7004 }
7005 break;
7006
7007 case M_DINS:
7008 {
7009 /* Use unsigned arithmetic. */
7010 addressT pos;
7011 addressT size;
7012
7013 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
7014 {
7015 as_bad (_("Unsupported large constant"));
7016 pos = size = 1;
7017 }
7018 else
7019 {
7020 pos = imm_expr.X_add_number;
7021 size = imm2_expr.X_add_number;
7022 }
7023
7024 if (pos > 63)
7025 {
7026 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
7027 pos = 1;
7028 }
7029 if (size == 0 || size > 64 || (pos + size - 1) > 63)
7030 {
7031 as_bad (_("Improper insert size (%lu, position %lu)"),
7032 (unsigned long) size, (unsigned long) pos);
7033 size = 1;
7034 }
7035
7036 if (pos < 32 && (pos + size - 1) < 32)
7037 {
7038 s = "dins";
7039 fmt = "t,r,+A,+B";
7040 }
7041 else if (pos >= 32)
7042 {
7043 s = "dinsu";
7044 fmt = "t,r,+E,+F";
7045 }
7046 else
7047 {
7048 s = "dinsm";
7049 fmt = "t,r,+A,+F";
7050 }
7051 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
7052 (int) (pos + size - 1));
7053 }
7054 break;
7055
7056 case M_DDIV_3:
7057 dbl = 1;
7058 case M_DIV_3:
7059 s = "mflo";
7060 goto do_div3;
7061 case M_DREM_3:
7062 dbl = 1;
7063 case M_REM_3:
7064 s = "mfhi";
7065 do_div3:
7066 if (treg == 0)
7067 {
7068 as_warn (_("Divide by zero."));
7069 if (mips_trap)
7070 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
7071 else
7072 macro_build (NULL, "break", BRK_FMT, 7);
7073 break;
7074 }
7075
7076 start_noreorder ();
7077 if (mips_trap)
7078 {
7079 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7080 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
7081 }
7082 else
7083 {
7084 if (mips_opts.micromips)
7085 micromips_label_expr (&label_expr);
7086 else
7087 label_expr.X_add_number = 8;
7088 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7089 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
7090 macro_build (NULL, "break", BRK_FMT, 7);
7091 if (mips_opts.micromips)
7092 micromips_add_label ();
7093 }
7094 expr1.X_add_number = -1;
7095 used_at = 1;
7096 load_register (AT, &expr1, dbl);
7097 if (mips_opts.micromips)
7098 micromips_label_expr (&label_expr);
7099 else
7100 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
7101 macro_build (&label_expr, "bne", "s,t,p", treg, AT);
7102 if (dbl)
7103 {
7104 expr1.X_add_number = 1;
7105 load_register (AT, &expr1, dbl);
7106 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
7107 }
7108 else
7109 {
7110 expr1.X_add_number = 0x80000000;
7111 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
7112 }
7113 if (mips_trap)
7114 {
7115 macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
7116 /* We want to close the noreorder block as soon as possible, so
7117 that later insns are available for delay slot filling. */
7118 end_noreorder ();
7119 }
7120 else
7121 {
7122 if (mips_opts.micromips)
7123 micromips_label_expr (&label_expr);
7124 else
7125 label_expr.X_add_number = 8;
7126 macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
7127 macro_build (NULL, "nop", "");
7128
7129 /* We want to close the noreorder block as soon as possible, so
7130 that later insns are available for delay slot filling. */
7131 end_noreorder ();
7132
7133 macro_build (NULL, "break", BRK_FMT, 6);
7134 }
7135 if (mips_opts.micromips)
7136 micromips_add_label ();
7137 macro_build (NULL, s, MFHL_FMT, dreg);
7138 break;
7139
7140 case M_DIV_3I:
7141 s = "div";
7142 s2 = "mflo";
7143 goto do_divi;
7144 case M_DIVU_3I:
7145 s = "divu";
7146 s2 = "mflo";
7147 goto do_divi;
7148 case M_REM_3I:
7149 s = "div";
7150 s2 = "mfhi";
7151 goto do_divi;
7152 case M_REMU_3I:
7153 s = "divu";
7154 s2 = "mfhi";
7155 goto do_divi;
7156 case M_DDIV_3I:
7157 dbl = 1;
7158 s = "ddiv";
7159 s2 = "mflo";
7160 goto do_divi;
7161 case M_DDIVU_3I:
7162 dbl = 1;
7163 s = "ddivu";
7164 s2 = "mflo";
7165 goto do_divi;
7166 case M_DREM_3I:
7167 dbl = 1;
7168 s = "ddiv";
7169 s2 = "mfhi";
7170 goto do_divi;
7171 case M_DREMU_3I:
7172 dbl = 1;
7173 s = "ddivu";
7174 s2 = "mfhi";
7175 do_divi:
7176 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7177 {
7178 as_warn (_("Divide by zero."));
7179 if (mips_trap)
7180 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
7181 else
7182 macro_build (NULL, "break", BRK_FMT, 7);
7183 break;
7184 }
7185 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7186 {
7187 if (strcmp (s2, "mflo") == 0)
7188 move_register (dreg, sreg);
7189 else
7190 move_register (dreg, ZERO);
7191 break;
7192 }
7193 if (imm_expr.X_op == O_constant
7194 && imm_expr.X_add_number == -1
7195 && s[strlen (s) - 1] != 'u')
7196 {
7197 if (strcmp (s2, "mflo") == 0)
7198 {
7199 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
7200 }
7201 else
7202 move_register (dreg, ZERO);
7203 break;
7204 }
7205
7206 used_at = 1;
7207 load_register (AT, &imm_expr, dbl);
7208 macro_build (NULL, s, "z,s,t", sreg, AT);
7209 macro_build (NULL, s2, MFHL_FMT, dreg);
7210 break;
7211
7212 case M_DIVU_3:
7213 s = "divu";
7214 s2 = "mflo";
7215 goto do_divu3;
7216 case M_REMU_3:
7217 s = "divu";
7218 s2 = "mfhi";
7219 goto do_divu3;
7220 case M_DDIVU_3:
7221 s = "ddivu";
7222 s2 = "mflo";
7223 goto do_divu3;
7224 case M_DREMU_3:
7225 s = "ddivu";
7226 s2 = "mfhi";
7227 do_divu3:
7228 start_noreorder ();
7229 if (mips_trap)
7230 {
7231 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7232 macro_build (NULL, s, "z,s,t", sreg, treg);
7233 /* We want to close the noreorder block as soon as possible, so
7234 that later insns are available for delay slot filling. */
7235 end_noreorder ();
7236 }
7237 else
7238 {
7239 if (mips_opts.micromips)
7240 micromips_label_expr (&label_expr);
7241 else
7242 label_expr.X_add_number = 8;
7243 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7244 macro_build (NULL, s, "z,s,t", sreg, treg);
7245
7246 /* We want to close the noreorder block as soon as possible, so
7247 that later insns are available for delay slot filling. */
7248 end_noreorder ();
7249 macro_build (NULL, "break", BRK_FMT, 7);
7250 if (mips_opts.micromips)
7251 micromips_add_label ();
7252 }
7253 macro_build (NULL, s2, MFHL_FMT, dreg);
7254 break;
7255
7256 case M_DLCA_AB:
7257 dbl = 1;
7258 case M_LCA_AB:
7259 call = 1;
7260 goto do_la;
7261 case M_DLA_AB:
7262 dbl = 1;
7263 case M_LA_AB:
7264 do_la:
7265 /* Load the address of a symbol into a register. If breg is not
7266 zero, we then add a base register to it. */
7267
7268 if (dbl && HAVE_32BIT_GPRS)
7269 as_warn (_("dla used to load 32-bit register"));
7270
7271 if (!dbl && HAVE_64BIT_OBJECTS)
7272 as_warn (_("la used to load 64-bit address"));
7273
7274 if (offset_expr.X_op == O_constant
7275 && offset_expr.X_add_number >= -0x8000
7276 && offset_expr.X_add_number < 0x8000)
7277 {
7278 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
7279 "t,r,j", treg, sreg, BFD_RELOC_LO16);
7280 break;
7281 }
7282
7283 if (mips_opts.at && (treg == breg))
7284 {
7285 tempreg = AT;
7286 used_at = 1;
7287 }
7288 else
7289 {
7290 tempreg = treg;
7291 }
7292
7293 if (offset_expr.X_op != O_symbol
7294 && offset_expr.X_op != O_constant)
7295 {
7296 as_bad (_("Expression too complex"));
7297 offset_expr.X_op = O_constant;
7298 }
7299
7300 if (offset_expr.X_op == O_constant)
7301 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
7302 else if (mips_pic == NO_PIC)
7303 {
7304 /* If this is a reference to a GP relative symbol, we want
7305 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
7306 Otherwise we want
7307 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
7308 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7309 If we have a constant, we need two instructions anyhow,
7310 so we may as well always use the latter form.
7311
7312 With 64bit address space and a usable $at we want
7313 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7314 lui $at,<sym> (BFD_RELOC_HI16_S)
7315 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7316 daddiu $at,<sym> (BFD_RELOC_LO16)
7317 dsll32 $tempreg,0
7318 daddu $tempreg,$tempreg,$at
7319
7320 If $at is already in use, we use a path which is suboptimal
7321 on superscalar processors.
7322 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7323 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7324 dsll $tempreg,16
7325 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
7326 dsll $tempreg,16
7327 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
7328
7329 For GP relative symbols in 64bit address space we can use
7330 the same sequence as in 32bit address space. */
7331 if (HAVE_64BIT_SYMBOLS)
7332 {
7333 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7334 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7335 {
7336 relax_start (offset_expr.X_add_symbol);
7337 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7338 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7339 relax_switch ();
7340 }
7341
7342 if (used_at == 0 && mips_opts.at)
7343 {
7344 macro_build (&offset_expr, "lui", LUI_FMT,
7345 tempreg, BFD_RELOC_MIPS_HIGHEST);
7346 macro_build (&offset_expr, "lui", LUI_FMT,
7347 AT, BFD_RELOC_HI16_S);
7348 macro_build (&offset_expr, "daddiu", "t,r,j",
7349 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7350 macro_build (&offset_expr, "daddiu", "t,r,j",
7351 AT, AT, BFD_RELOC_LO16);
7352 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
7353 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
7354 used_at = 1;
7355 }
7356 else
7357 {
7358 macro_build (&offset_expr, "lui", LUI_FMT,
7359 tempreg, BFD_RELOC_MIPS_HIGHEST);
7360 macro_build (&offset_expr, "daddiu", "t,r,j",
7361 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7362 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7363 macro_build (&offset_expr, "daddiu", "t,r,j",
7364 tempreg, tempreg, BFD_RELOC_HI16_S);
7365 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7366 macro_build (&offset_expr, "daddiu", "t,r,j",
7367 tempreg, tempreg, BFD_RELOC_LO16);
7368 }
7369
7370 if (mips_relax.sequence)
7371 relax_end ();
7372 }
7373 else
7374 {
7375 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7376 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7377 {
7378 relax_start (offset_expr.X_add_symbol);
7379 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7380 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7381 relax_switch ();
7382 }
7383 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
7384 as_bad (_("Offset too large"));
7385 macro_build_lui (&offset_expr, tempreg);
7386 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7387 tempreg, tempreg, BFD_RELOC_LO16);
7388 if (mips_relax.sequence)
7389 relax_end ();
7390 }
7391 }
7392 else if (!mips_big_got && !HAVE_NEWABI)
7393 {
7394 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7395
7396 /* If this is a reference to an external symbol, and there
7397 is no constant, we want
7398 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7399 or for lca or if tempreg is PIC_CALL_REG
7400 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7401 For a local symbol, we want
7402 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7403 nop
7404 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7405
7406 If we have a small constant, and this is a reference to
7407 an external symbol, we want
7408 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7409 nop
7410 addiu $tempreg,$tempreg,<constant>
7411 For a local symbol, we want the same instruction
7412 sequence, but we output a BFD_RELOC_LO16 reloc on the
7413 addiu instruction.
7414
7415 If we have a large constant, and this is a reference to
7416 an external symbol, we want
7417 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7418 lui $at,<hiconstant>
7419 addiu $at,$at,<loconstant>
7420 addu $tempreg,$tempreg,$at
7421 For a local symbol, we want the same instruction
7422 sequence, but we output a BFD_RELOC_LO16 reloc on the
7423 addiu instruction.
7424 */
7425
7426 if (offset_expr.X_add_number == 0)
7427 {
7428 if (mips_pic == SVR4_PIC
7429 && breg == 0
7430 && (call || tempreg == PIC_CALL_REG))
7431 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7432
7433 relax_start (offset_expr.X_add_symbol);
7434 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7435 lw_reloc_type, mips_gp_register);
7436 if (breg != 0)
7437 {
7438 /* We're going to put in an addu instruction using
7439 tempreg, so we may as well insert the nop right
7440 now. */
7441 load_delay_nop ();
7442 }
7443 relax_switch ();
7444 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7445 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
7446 load_delay_nop ();
7447 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7448 tempreg, tempreg, BFD_RELOC_LO16);
7449 relax_end ();
7450 /* FIXME: If breg == 0, and the next instruction uses
7451 $tempreg, then if this variant case is used an extra
7452 nop will be generated. */
7453 }
7454 else if (offset_expr.X_add_number >= -0x8000
7455 && offset_expr.X_add_number < 0x8000)
7456 {
7457 load_got_offset (tempreg, &offset_expr);
7458 load_delay_nop ();
7459 add_got_offset (tempreg, &offset_expr);
7460 }
7461 else
7462 {
7463 expr1.X_add_number = offset_expr.X_add_number;
7464 offset_expr.X_add_number =
7465 SEXT_16BIT (offset_expr.X_add_number);
7466 load_got_offset (tempreg, &offset_expr);
7467 offset_expr.X_add_number = expr1.X_add_number;
7468 /* If we are going to add in a base register, and the
7469 target register and the base register are the same,
7470 then we are using AT as a temporary register. Since
7471 we want to load the constant into AT, we add our
7472 current AT (from the global offset table) and the
7473 register into the register now, and pretend we were
7474 not using a base register. */
7475 if (breg == treg)
7476 {
7477 load_delay_nop ();
7478 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7479 treg, AT, breg);
7480 breg = 0;
7481 tempreg = treg;
7482 }
7483 add_got_offset_hilo (tempreg, &offset_expr, AT);
7484 used_at = 1;
7485 }
7486 }
7487 else if (!mips_big_got && HAVE_NEWABI)
7488 {
7489 int add_breg_early = 0;
7490
7491 /* If this is a reference to an external, and there is no
7492 constant, or local symbol (*), with or without a
7493 constant, we want
7494 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7495 or for lca or if tempreg is PIC_CALL_REG
7496 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7497
7498 If we have a small constant, and this is a reference to
7499 an external symbol, we want
7500 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7501 addiu $tempreg,$tempreg,<constant>
7502
7503 If we have a large constant, and this is a reference to
7504 an external symbol, we want
7505 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7506 lui $at,<hiconstant>
7507 addiu $at,$at,<loconstant>
7508 addu $tempreg,$tempreg,$at
7509
7510 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7511 local symbols, even though it introduces an additional
7512 instruction. */
7513
7514 if (offset_expr.X_add_number)
7515 {
7516 expr1.X_add_number = offset_expr.X_add_number;
7517 offset_expr.X_add_number = 0;
7518
7519 relax_start (offset_expr.X_add_symbol);
7520 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7521 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7522
7523 if (expr1.X_add_number >= -0x8000
7524 && expr1.X_add_number < 0x8000)
7525 {
7526 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7527 tempreg, tempreg, BFD_RELOC_LO16);
7528 }
7529 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7530 {
7531 /* If we are going to add in a base register, and the
7532 target register and the base register are the same,
7533 then we are using AT as a temporary register. Since
7534 we want to load the constant into AT, we add our
7535 current AT (from the global offset table) and the
7536 register into the register now, and pretend we were
7537 not using a base register. */
7538 if (breg != treg)
7539 dreg = tempreg;
7540 else
7541 {
7542 gas_assert (tempreg == AT);
7543 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7544 treg, AT, breg);
7545 dreg = treg;
7546 add_breg_early = 1;
7547 }
7548
7549 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7550 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7551 dreg, dreg, AT);
7552
7553 used_at = 1;
7554 }
7555 else
7556 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7557
7558 relax_switch ();
7559 offset_expr.X_add_number = expr1.X_add_number;
7560
7561 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7562 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7563 if (add_breg_early)
7564 {
7565 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7566 treg, tempreg, breg);
7567 breg = 0;
7568 tempreg = treg;
7569 }
7570 relax_end ();
7571 }
7572 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
7573 {
7574 relax_start (offset_expr.X_add_symbol);
7575 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7576 BFD_RELOC_MIPS_CALL16, mips_gp_register);
7577 relax_switch ();
7578 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7579 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7580 relax_end ();
7581 }
7582 else
7583 {
7584 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7585 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7586 }
7587 }
7588 else if (mips_big_got && !HAVE_NEWABI)
7589 {
7590 int gpdelay;
7591 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7592 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7593 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7594
7595 /* This is the large GOT case. If this is a reference to an
7596 external symbol, and there is no constant, we want
7597 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7598 addu $tempreg,$tempreg,$gp
7599 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7600 or for lca or if tempreg is PIC_CALL_REG
7601 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7602 addu $tempreg,$tempreg,$gp
7603 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7604 For a local symbol, we want
7605 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7606 nop
7607 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7608
7609 If we have a small constant, and this is a reference to
7610 an external symbol, we want
7611 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7612 addu $tempreg,$tempreg,$gp
7613 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7614 nop
7615 addiu $tempreg,$tempreg,<constant>
7616 For a local symbol, we want
7617 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7618 nop
7619 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7620
7621 If we have a large constant, and this is a reference to
7622 an external symbol, we want
7623 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7624 addu $tempreg,$tempreg,$gp
7625 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7626 lui $at,<hiconstant>
7627 addiu $at,$at,<loconstant>
7628 addu $tempreg,$tempreg,$at
7629 For a local symbol, we want
7630 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7631 lui $at,<hiconstant>
7632 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
7633 addu $tempreg,$tempreg,$at
7634 */
7635
7636 expr1.X_add_number = offset_expr.X_add_number;
7637 offset_expr.X_add_number = 0;
7638 relax_start (offset_expr.X_add_symbol);
7639 gpdelay = reg_needs_delay (mips_gp_register);
7640 if (expr1.X_add_number == 0 && breg == 0
7641 && (call || tempreg == PIC_CALL_REG))
7642 {
7643 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7644 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7645 }
7646 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7647 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7648 tempreg, tempreg, mips_gp_register);
7649 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7650 tempreg, lw_reloc_type, tempreg);
7651 if (expr1.X_add_number == 0)
7652 {
7653 if (breg != 0)
7654 {
7655 /* We're going to put in an addu instruction using
7656 tempreg, so we may as well insert the nop right
7657 now. */
7658 load_delay_nop ();
7659 }
7660 }
7661 else if (expr1.X_add_number >= -0x8000
7662 && expr1.X_add_number < 0x8000)
7663 {
7664 load_delay_nop ();
7665 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7666 tempreg, tempreg, BFD_RELOC_LO16);
7667 }
7668 else
7669 {
7670 /* If we are going to add in a base register, and the
7671 target register and the base register are the same,
7672 then we are using AT as a temporary register. Since
7673 we want to load the constant into AT, we add our
7674 current AT (from the global offset table) and the
7675 register into the register now, and pretend we were
7676 not using a base register. */
7677 if (breg != treg)
7678 dreg = tempreg;
7679 else
7680 {
7681 gas_assert (tempreg == AT);
7682 load_delay_nop ();
7683 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7684 treg, AT, breg);
7685 dreg = treg;
7686 }
7687
7688 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7689 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
7690
7691 used_at = 1;
7692 }
7693 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
7694 relax_switch ();
7695
7696 if (gpdelay)
7697 {
7698 /* This is needed because this instruction uses $gp, but
7699 the first instruction on the main stream does not. */
7700 macro_build (NULL, "nop", "");
7701 }
7702
7703 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7704 local_reloc_type, mips_gp_register);
7705 if (expr1.X_add_number >= -0x8000
7706 && expr1.X_add_number < 0x8000)
7707 {
7708 load_delay_nop ();
7709 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7710 tempreg, tempreg, BFD_RELOC_LO16);
7711 /* FIXME: If add_number is 0, and there was no base
7712 register, the external symbol case ended with a load,
7713 so if the symbol turns out to not be external, and
7714 the next instruction uses tempreg, an unnecessary nop
7715 will be inserted. */
7716 }
7717 else
7718 {
7719 if (breg == treg)
7720 {
7721 /* We must add in the base register now, as in the
7722 external symbol case. */
7723 gas_assert (tempreg == AT);
7724 load_delay_nop ();
7725 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7726 treg, AT, breg);
7727 tempreg = treg;
7728 /* We set breg to 0 because we have arranged to add
7729 it in in both cases. */
7730 breg = 0;
7731 }
7732
7733 macro_build_lui (&expr1, AT);
7734 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7735 AT, AT, BFD_RELOC_LO16);
7736 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7737 tempreg, tempreg, AT);
7738 used_at = 1;
7739 }
7740 relax_end ();
7741 }
7742 else if (mips_big_got && HAVE_NEWABI)
7743 {
7744 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7745 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7746 int add_breg_early = 0;
7747
7748 /* This is the large GOT case. If this is a reference to an
7749 external symbol, and there is no constant, we want
7750 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7751 add $tempreg,$tempreg,$gp
7752 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7753 or for lca or if tempreg is PIC_CALL_REG
7754 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7755 add $tempreg,$tempreg,$gp
7756 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7757
7758 If we have a small constant, and this is a reference to
7759 an external symbol, we want
7760 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7761 add $tempreg,$tempreg,$gp
7762 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7763 addi $tempreg,$tempreg,<constant>
7764
7765 If we have a large constant, and this is a reference to
7766 an external symbol, we want
7767 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7768 addu $tempreg,$tempreg,$gp
7769 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7770 lui $at,<hiconstant>
7771 addi $at,$at,<loconstant>
7772 add $tempreg,$tempreg,$at
7773
7774 If we have NewABI, and we know it's a local symbol, we want
7775 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
7776 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
7777 otherwise we have to resort to GOT_HI16/GOT_LO16. */
7778
7779 relax_start (offset_expr.X_add_symbol);
7780
7781 expr1.X_add_number = offset_expr.X_add_number;
7782 offset_expr.X_add_number = 0;
7783
7784 if (expr1.X_add_number == 0 && breg == 0
7785 && (call || tempreg == PIC_CALL_REG))
7786 {
7787 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7788 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7789 }
7790 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7791 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7792 tempreg, tempreg, mips_gp_register);
7793 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7794 tempreg, lw_reloc_type, tempreg);
7795
7796 if (expr1.X_add_number == 0)
7797 ;
7798 else if (expr1.X_add_number >= -0x8000
7799 && expr1.X_add_number < 0x8000)
7800 {
7801 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7802 tempreg, tempreg, BFD_RELOC_LO16);
7803 }
7804 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7805 {
7806 /* If we are going to add in a base register, and the
7807 target register and the base register are the same,
7808 then we are using AT as a temporary register. Since
7809 we want to load the constant into AT, we add our
7810 current AT (from the global offset table) and the
7811 register into the register now, and pretend we were
7812 not using a base register. */
7813 if (breg != treg)
7814 dreg = tempreg;
7815 else
7816 {
7817 gas_assert (tempreg == AT);
7818 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7819 treg, AT, breg);
7820 dreg = treg;
7821 add_breg_early = 1;
7822 }
7823
7824 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7825 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
7826
7827 used_at = 1;
7828 }
7829 else
7830 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7831
7832 relax_switch ();
7833 offset_expr.X_add_number = expr1.X_add_number;
7834 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7835 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
7836 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
7837 tempreg, BFD_RELOC_MIPS_GOT_OFST);
7838 if (add_breg_early)
7839 {
7840 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7841 treg, tempreg, breg);
7842 breg = 0;
7843 tempreg = treg;
7844 }
7845 relax_end ();
7846 }
7847 else
7848 abort ();
7849
7850 if (breg != 0)
7851 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
7852 break;
7853
7854 case M_MSGSND:
7855 gas_assert (!mips_opts.micromips);
7856 {
7857 unsigned long temp = (treg << 16) | (0x01);
7858 macro_build (NULL, "c2", "C", temp);
7859 }
7860 break;
7861
7862 case M_MSGLD:
7863 gas_assert (!mips_opts.micromips);
7864 {
7865 unsigned long temp = (0x02);
7866 macro_build (NULL, "c2", "C", temp);
7867 }
7868 break;
7869
7870 case M_MSGLD_T:
7871 gas_assert (!mips_opts.micromips);
7872 {
7873 unsigned long temp = (treg << 16) | (0x02);
7874 macro_build (NULL, "c2", "C", temp);
7875 }
7876 break;
7877
7878 case M_MSGWAIT:
7879 gas_assert (!mips_opts.micromips);
7880 macro_build (NULL, "c2", "C", 3);
7881 break;
7882
7883 case M_MSGWAIT_T:
7884 gas_assert (!mips_opts.micromips);
7885 {
7886 unsigned long temp = (treg << 16) | 0x03;
7887 macro_build (NULL, "c2", "C", temp);
7888 }
7889 break;
7890
7891 case M_J_A:
7892 /* The j instruction may not be used in PIC code, since it
7893 requires an absolute address. We convert it to a b
7894 instruction. */
7895 if (mips_pic == NO_PIC)
7896 macro_build (&offset_expr, "j", "a");
7897 else
7898 macro_build (&offset_expr, "b", "p");
7899 break;
7900
7901 /* The jal instructions must be handled as macros because when
7902 generating PIC code they expand to multi-instruction
7903 sequences. Normally they are simple instructions. */
7904 case M_JALS_1:
7905 dreg = RA;
7906 /* Fall through. */
7907 case M_JALS_2:
7908 gas_assert (mips_opts.micromips);
7909 jals = 1;
7910 goto jal;
7911 case M_JAL_1:
7912 dreg = RA;
7913 /* Fall through. */
7914 case M_JAL_2:
7915 jal:
7916 if (mips_pic == NO_PIC)
7917 {
7918 s = jals ? "jalrs" : "jalr";
7919 if (mips_opts.micromips
7920 && dreg == RA
7921 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7922 macro_build (NULL, s, "mj", sreg);
7923 else
7924 macro_build (NULL, s, JALR_FMT, dreg, sreg);
7925 }
7926 else
7927 {
7928 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
7929 && mips_cprestore_offset >= 0);
7930
7931 if (sreg != PIC_CALL_REG)
7932 as_warn (_("MIPS PIC call to register other than $25"));
7933
7934 s = (mips_opts.micromips && (!mips_opts.noreorder || cprestore)
7935 ? "jalrs" : "jalr");
7936 if (mips_opts.micromips
7937 && dreg == RA
7938 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7939 macro_build (NULL, s, "mj", sreg);
7940 else
7941 macro_build (NULL, s, JALR_FMT, dreg, sreg);
7942 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
7943 {
7944 if (mips_cprestore_offset < 0)
7945 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7946 else
7947 {
7948 if (!mips_frame_reg_valid)
7949 {
7950 as_warn (_("No .frame pseudo-op used in PIC code"));
7951 /* Quiet this warning. */
7952 mips_frame_reg_valid = 1;
7953 }
7954 if (!mips_cprestore_valid)
7955 {
7956 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7957 /* Quiet this warning. */
7958 mips_cprestore_valid = 1;
7959 }
7960 if (mips_opts.noreorder)
7961 macro_build (NULL, "nop", "");
7962 expr1.X_add_number = mips_cprestore_offset;
7963 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
7964 mips_gp_register,
7965 mips_frame_reg,
7966 HAVE_64BIT_ADDRESSES);
7967 }
7968 }
7969 }
7970
7971 break;
7972
7973 case M_JALS_A:
7974 gas_assert (mips_opts.micromips);
7975 jals = 1;
7976 /* Fall through. */
7977 case M_JAL_A:
7978 if (mips_pic == NO_PIC)
7979 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
7980 else if (mips_pic == SVR4_PIC)
7981 {
7982 /* If this is a reference to an external symbol, and we are
7983 using a small GOT, we want
7984 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7985 nop
7986 jalr $ra,$25
7987 nop
7988 lw $gp,cprestore($sp)
7989 The cprestore value is set using the .cprestore
7990 pseudo-op. If we are using a big GOT, we want
7991 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7992 addu $25,$25,$gp
7993 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
7994 nop
7995 jalr $ra,$25
7996 nop
7997 lw $gp,cprestore($sp)
7998 If the symbol is not external, we want
7999 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8000 nop
8001 addiu $25,$25,<sym> (BFD_RELOC_LO16)
8002 jalr $ra,$25
8003 nop
8004 lw $gp,cprestore($sp)
8005
8006 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
8007 sequences above, minus nops, unless the symbol is local,
8008 which enables us to use GOT_PAGE/GOT_OFST (big got) or
8009 GOT_DISP. */
8010 if (HAVE_NEWABI)
8011 {
8012 if (!mips_big_got)
8013 {
8014 relax_start (offset_expr.X_add_symbol);
8015 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8016 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
8017 mips_gp_register);
8018 relax_switch ();
8019 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8020 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
8021 mips_gp_register);
8022 relax_end ();
8023 }
8024 else
8025 {
8026 relax_start (offset_expr.X_add_symbol);
8027 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
8028 BFD_RELOC_MIPS_CALL_HI16);
8029 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8030 PIC_CALL_REG, mips_gp_register);
8031 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8032 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8033 PIC_CALL_REG);
8034 relax_switch ();
8035 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8036 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
8037 mips_gp_register);
8038 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8039 PIC_CALL_REG, PIC_CALL_REG,
8040 BFD_RELOC_MIPS_GOT_OFST);
8041 relax_end ();
8042 }
8043
8044 macro_build_jalr (&offset_expr, 0);
8045 }
8046 else
8047 {
8048 relax_start (offset_expr.X_add_symbol);
8049 if (!mips_big_got)
8050 {
8051 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8052 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
8053 mips_gp_register);
8054 load_delay_nop ();
8055 relax_switch ();
8056 }
8057 else
8058 {
8059 int gpdelay;
8060
8061 gpdelay = reg_needs_delay (mips_gp_register);
8062 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
8063 BFD_RELOC_MIPS_CALL_HI16);
8064 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8065 PIC_CALL_REG, mips_gp_register);
8066 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8067 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8068 PIC_CALL_REG);
8069 load_delay_nop ();
8070 relax_switch ();
8071 if (gpdelay)
8072 macro_build (NULL, "nop", "");
8073 }
8074 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8075 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
8076 mips_gp_register);
8077 load_delay_nop ();
8078 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8079 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
8080 relax_end ();
8081 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
8082
8083 if (mips_cprestore_offset < 0)
8084 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8085 else
8086 {
8087 if (!mips_frame_reg_valid)
8088 {
8089 as_warn (_("No .frame pseudo-op used in PIC code"));
8090 /* Quiet this warning. */
8091 mips_frame_reg_valid = 1;
8092 }
8093 if (!mips_cprestore_valid)
8094 {
8095 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8096 /* Quiet this warning. */
8097 mips_cprestore_valid = 1;
8098 }
8099 if (mips_opts.noreorder)
8100 macro_build (NULL, "nop", "");
8101 expr1.X_add_number = mips_cprestore_offset;
8102 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
8103 mips_gp_register,
8104 mips_frame_reg,
8105 HAVE_64BIT_ADDRESSES);
8106 }
8107 }
8108 }
8109 else if (mips_pic == VXWORKS_PIC)
8110 as_bad (_("Non-PIC jump used in PIC library"));
8111 else
8112 abort ();
8113
8114 break;
8115
8116 case M_LBUE_AB:
8117 ab = 1;
8118 case M_LBUE_OB:
8119 s = "lbue";
8120 fmt = "t,+j(b)";
8121 offbits = 9;
8122 goto ld_st;
8123 case M_LHUE_AB:
8124 ab = 1;
8125 case M_LHUE_OB:
8126 s = "lhue";
8127 fmt = "t,+j(b)";
8128 offbits = 9;
8129 goto ld_st;
8130 case M_LBE_AB:
8131 ab = 1;
8132 case M_LBE_OB:
8133 s = "lbe";
8134 fmt = "t,+j(b)";
8135 offbits = 9;
8136 goto ld_st;
8137 case M_LHE_AB:
8138 ab = 1;
8139 case M_LHE_OB:
8140 s = "lhe";
8141 fmt = "t,+j(b)";
8142 offbits = 9;
8143 goto ld_st;
8144 case M_LLE_AB:
8145 ab = 1;
8146 case M_LLE_OB:
8147 s = "lle";
8148 fmt = "t,+j(b)";
8149 offbits = 9;
8150 goto ld_st;
8151 case M_LWE_AB:
8152 ab = 1;
8153 case M_LWE_OB:
8154 s = "lwe";
8155 fmt = "t,+j(b)";
8156 offbits = 9;
8157 goto ld_st;
8158 case M_LWLE_AB:
8159 ab = 1;
8160 case M_LWLE_OB:
8161 s = "lwle";
8162 fmt = "t,+j(b)";
8163 offbits = 9;
8164 goto ld_st;
8165 case M_LWRE_AB:
8166 ab = 1;
8167 case M_LWRE_OB:
8168 s = "lwre";
8169 fmt = "t,+j(b)";
8170 offbits = 9;
8171 goto ld_st;
8172 case M_SBE_AB:
8173 ab = 1;
8174 case M_SBE_OB:
8175 s = "sbe";
8176 fmt = "t,+j(b)";
8177 offbits = 9;
8178 goto ld_st;
8179 case M_SCE_AB:
8180 ab = 1;
8181 case M_SCE_OB:
8182 s = "sce";
8183 fmt = "t,+j(b)";
8184 offbits = 9;
8185 goto ld_st;
8186 case M_SHE_AB:
8187 ab = 1;
8188 case M_SHE_OB:
8189 s = "she";
8190 fmt = "t,+j(b)";
8191 offbits = 9;
8192 goto ld_st;
8193 case M_SWE_AB:
8194 ab = 1;
8195 case M_SWE_OB:
8196 s = "swe";
8197 fmt = "t,+j(b)";
8198 offbits = 9;
8199 goto ld_st;
8200 case M_SWLE_AB:
8201 ab = 1;
8202 case M_SWLE_OB:
8203 s = "swle";
8204 fmt = "t,+j(b)";
8205 offbits = 9;
8206 goto ld_st;
8207 case M_SWRE_AB:
8208 ab = 1;
8209 case M_SWRE_OB:
8210 s = "swre";
8211 fmt = "t,+j(b)";
8212 offbits = 9;
8213 goto ld_st;
8214 case M_ACLR_AB:
8215 ab = 1;
8216 case M_ACLR_OB:
8217 s = "aclr";
8218 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8219 fmt = "\\,~(b)";
8220 offbits = 12;
8221 goto ld_st;
8222 case M_ASET_AB:
8223 ab = 1;
8224 case M_ASET_OB:
8225 s = "aset";
8226 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8227 fmt = "\\,~(b)";
8228 offbits = 12;
8229 goto ld_st;
8230 case M_LB_AB:
8231 ab = 1;
8232 s = "lb";
8233 fmt = "t,o(b)";
8234 goto ld;
8235 case M_LBU_AB:
8236 ab = 1;
8237 s = "lbu";
8238 fmt = "t,o(b)";
8239 goto ld;
8240 case M_LH_AB:
8241 ab = 1;
8242 s = "lh";
8243 fmt = "t,o(b)";
8244 goto ld;
8245 case M_LHU_AB:
8246 ab = 1;
8247 s = "lhu";
8248 fmt = "t,o(b)";
8249 goto ld;
8250 case M_LW_AB:
8251 ab = 1;
8252 s = "lw";
8253 fmt = "t,o(b)";
8254 goto ld;
8255 case M_LWC0_AB:
8256 ab = 1;
8257 gas_assert (!mips_opts.micromips);
8258 s = "lwc0";
8259 fmt = "E,o(b)";
8260 /* Itbl support may require additional care here. */
8261 coproc = 1;
8262 goto ld_st;
8263 case M_LWC1_AB:
8264 ab = 1;
8265 s = "lwc1";
8266 fmt = "T,o(b)";
8267 /* Itbl support may require additional care here. */
8268 coproc = 1;
8269 goto ld_st;
8270 case M_LWC2_AB:
8271 ab = 1;
8272 case M_LWC2_OB:
8273 s = "lwc2";
8274 fmt = COP12_FMT;
8275 offbits = (mips_opts.micromips ? 12 : 16);
8276 /* Itbl support may require additional care here. */
8277 coproc = 1;
8278 goto ld_st;
8279 case M_LWC3_AB:
8280 ab = 1;
8281 gas_assert (!mips_opts.micromips);
8282 s = "lwc3";
8283 fmt = "E,o(b)";
8284 /* Itbl support may require additional care here. */
8285 coproc = 1;
8286 goto ld_st;
8287 case M_LWL_AB:
8288 ab = 1;
8289 case M_LWL_OB:
8290 s = "lwl";
8291 fmt = MEM12_FMT;
8292 offbits = (mips_opts.micromips ? 12 : 16);
8293 goto ld_st;
8294 case M_LWR_AB:
8295 ab = 1;
8296 case M_LWR_OB:
8297 s = "lwr";
8298 fmt = MEM12_FMT;
8299 offbits = (mips_opts.micromips ? 12 : 16);
8300 goto ld_st;
8301 case M_LDC1_AB:
8302 ab = 1;
8303 s = "ldc1";
8304 fmt = "T,o(b)";
8305 /* Itbl support may require additional care here. */
8306 coproc = 1;
8307 goto ld_st;
8308 case M_LDC2_AB:
8309 ab = 1;
8310 case M_LDC2_OB:
8311 s = "ldc2";
8312 fmt = COP12_FMT;
8313 offbits = (mips_opts.micromips ? 12 : 16);
8314 /* Itbl support may require additional care here. */
8315 coproc = 1;
8316 goto ld_st;
8317 case M_LQC2_AB:
8318 ab = 1;
8319 s = "lqc2";
8320 fmt = "E,o(b)";
8321 /* Itbl support may require additional care here. */
8322 coproc = 1;
8323 goto ld_st;
8324 case M_LDC3_AB:
8325 ab = 1;
8326 s = "ldc3";
8327 fmt = "E,o(b)";
8328 /* Itbl support may require additional care here. */
8329 coproc = 1;
8330 goto ld_st;
8331 case M_LDL_AB:
8332 ab = 1;
8333 case M_LDL_OB:
8334 s = "ldl";
8335 fmt = MEM12_FMT;
8336 offbits = (mips_opts.micromips ? 12 : 16);
8337 goto ld_st;
8338 case M_LDR_AB:
8339 ab = 1;
8340 case M_LDR_OB:
8341 s = "ldr";
8342 fmt = MEM12_FMT;
8343 offbits = (mips_opts.micromips ? 12 : 16);
8344 goto ld_st;
8345 case M_LL_AB:
8346 ab = 1;
8347 case M_LL_OB:
8348 s = "ll";
8349 fmt = MEM12_FMT;
8350 offbits = (mips_opts.micromips ? 12 : 16);
8351 goto ld;
8352 case M_LLD_AB:
8353 ab = 1;
8354 case M_LLD_OB:
8355 s = "lld";
8356 fmt = MEM12_FMT;
8357 offbits = (mips_opts.micromips ? 12 : 16);
8358 goto ld;
8359 case M_LWU_AB:
8360 ab = 1;
8361 case M_LWU_OB:
8362 s = "lwu";
8363 fmt = MEM12_FMT;
8364 offbits = (mips_opts.micromips ? 12 : 16);
8365 goto ld;
8366 case M_LWP_AB:
8367 ab = 1;
8368 case M_LWP_OB:
8369 gas_assert (mips_opts.micromips);
8370 s = "lwp";
8371 fmt = "t,~(b)";
8372 offbits = 12;
8373 lp = 1;
8374 goto ld;
8375 case M_LDP_AB:
8376 ab = 1;
8377 case M_LDP_OB:
8378 gas_assert (mips_opts.micromips);
8379 s = "ldp";
8380 fmt = "t,~(b)";
8381 offbits = 12;
8382 lp = 1;
8383 goto ld;
8384 case M_LWM_AB:
8385 ab = 1;
8386 case M_LWM_OB:
8387 gas_assert (mips_opts.micromips);
8388 s = "lwm";
8389 fmt = "n,~(b)";
8390 offbits = 12;
8391 goto ld_st;
8392 case M_LDM_AB:
8393 ab = 1;
8394 case M_LDM_OB:
8395 gas_assert (mips_opts.micromips);
8396 s = "ldm";
8397 fmt = "n,~(b)";
8398 offbits = 12;
8399 goto ld_st;
8400
8401 ld:
8402 /* We don't want to use $0 as tempreg. */
8403 if (breg == treg + lp || treg + lp == ZERO)
8404 goto ld_st;
8405 else
8406 tempreg = treg + lp;
8407 goto ld_noat;
8408
8409 case M_SB_AB:
8410 ab = 1;
8411 s = "sb";
8412 fmt = "t,o(b)";
8413 goto ld_st;
8414 case M_SH_AB:
8415 ab = 1;
8416 s = "sh";
8417 fmt = "t,o(b)";
8418 goto ld_st;
8419 case M_SW_AB:
8420 ab = 1;
8421 s = "sw";
8422 fmt = "t,o(b)";
8423 goto ld_st;
8424 case M_SWC0_AB:
8425 ab = 1;
8426 gas_assert (!mips_opts.micromips);
8427 s = "swc0";
8428 fmt = "E,o(b)";
8429 /* Itbl support may require additional care here. */
8430 coproc = 1;
8431 goto ld_st;
8432 case M_SWC1_AB:
8433 ab = 1;
8434 s = "swc1";
8435 fmt = "T,o(b)";
8436 /* Itbl support may require additional care here. */
8437 coproc = 1;
8438 goto ld_st;
8439 case M_SWC2_AB:
8440 ab = 1;
8441 case M_SWC2_OB:
8442 s = "swc2";
8443 fmt = COP12_FMT;
8444 offbits = (mips_opts.micromips ? 12 : 16);
8445 /* Itbl support may require additional care here. */
8446 coproc = 1;
8447 goto ld_st;
8448 case M_SWC3_AB:
8449 ab = 1;
8450 gas_assert (!mips_opts.micromips);
8451 s = "swc3";
8452 fmt = "E,o(b)";
8453 /* Itbl support may require additional care here. */
8454 coproc = 1;
8455 goto ld_st;
8456 case M_SWL_AB:
8457 ab = 1;
8458 case M_SWL_OB:
8459 s = "swl";
8460 fmt = MEM12_FMT;
8461 offbits = (mips_opts.micromips ? 12 : 16);
8462 goto ld_st;
8463 case M_SWR_AB:
8464 ab = 1;
8465 case M_SWR_OB:
8466 s = "swr";
8467 fmt = MEM12_FMT;
8468 offbits = (mips_opts.micromips ? 12 : 16);
8469 goto ld_st;
8470 case M_SC_AB:
8471 ab = 1;
8472 case M_SC_OB:
8473 s = "sc";
8474 fmt = MEM12_FMT;
8475 offbits = (mips_opts.micromips ? 12 : 16);
8476 goto ld_st;
8477 case M_SCD_AB:
8478 ab = 1;
8479 case M_SCD_OB:
8480 s = "scd";
8481 fmt = MEM12_FMT;
8482 offbits = (mips_opts.micromips ? 12 : 16);
8483 goto ld_st;
8484 case M_CACHE_AB:
8485 ab = 1;
8486 case M_CACHE_OB:
8487 s = "cache";
8488 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
8489 offbits = (mips_opts.micromips ? 12 : 16);
8490 goto ld_st;
8491 case M_CACHEE_AB:
8492 ab = 1;
8493 case M_CACHEE_OB:
8494 s = "cachee";
8495 fmt = "k,+j(b)";
8496 offbits = 9;
8497 goto ld_st;
8498 case M_PREF_AB:
8499 ab = 1;
8500 case M_PREF_OB:
8501 s = "pref";
8502 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
8503 offbits = (mips_opts.micromips ? 12 : 16);
8504 goto ld_st;
8505 case M_PREFE_AB:
8506 ab = 1;
8507 case M_PREFE_OB:
8508 s = "prefe";
8509 fmt = "k,+j(b)";
8510 offbits = 9;
8511 goto ld_st;
8512 case M_SDC1_AB:
8513 ab = 1;
8514 s = "sdc1";
8515 fmt = "T,o(b)";
8516 coproc = 1;
8517 /* Itbl support may require additional care here. */
8518 goto ld_st;
8519 case M_SDC2_AB:
8520 ab = 1;
8521 case M_SDC2_OB:
8522 s = "sdc2";
8523 fmt = COP12_FMT;
8524 offbits = (mips_opts.micromips ? 12 : 16);
8525 /* Itbl support may require additional care here. */
8526 coproc = 1;
8527 goto ld_st;
8528 case M_SQC2_AB:
8529 ab = 1;
8530 s = "sqc2";
8531 fmt = "E,o(b)";
8532 /* Itbl support may require additional care here. */
8533 coproc = 1;
8534 goto ld_st;
8535 case M_SDC3_AB:
8536 ab = 1;
8537 gas_assert (!mips_opts.micromips);
8538 s = "sdc3";
8539 fmt = "E,o(b)";
8540 /* Itbl support may require additional care here. */
8541 coproc = 1;
8542 goto ld_st;
8543 case M_SDL_AB:
8544 ab = 1;
8545 case M_SDL_OB:
8546 s = "sdl";
8547 fmt = MEM12_FMT;
8548 offbits = (mips_opts.micromips ? 12 : 16);
8549 goto ld_st;
8550 case M_SDR_AB:
8551 ab = 1;
8552 case M_SDR_OB:
8553 s = "sdr";
8554 fmt = MEM12_FMT;
8555 offbits = (mips_opts.micromips ? 12 : 16);
8556 goto ld_st;
8557 case M_SWP_AB:
8558 ab = 1;
8559 case M_SWP_OB:
8560 gas_assert (mips_opts.micromips);
8561 s = "swp";
8562 fmt = "t,~(b)";
8563 offbits = 12;
8564 goto ld_st;
8565 case M_SDP_AB:
8566 ab = 1;
8567 case M_SDP_OB:
8568 gas_assert (mips_opts.micromips);
8569 s = "sdp";
8570 fmt = "t,~(b)";
8571 offbits = 12;
8572 goto ld_st;
8573 case M_SWM_AB:
8574 ab = 1;
8575 case M_SWM_OB:
8576 gas_assert (mips_opts.micromips);
8577 s = "swm";
8578 fmt = "n,~(b)";
8579 offbits = 12;
8580 goto ld_st;
8581 case M_SDM_AB:
8582 ab = 1;
8583 case M_SDM_OB:
8584 gas_assert (mips_opts.micromips);
8585 s = "sdm";
8586 fmt = "n,~(b)";
8587 offbits = 12;
8588
8589 ld_st:
8590 tempreg = AT;
8591 used_at = 1;
8592 ld_noat:
8593 if (offset_expr.X_op != O_constant
8594 && offset_expr.X_op != O_symbol)
8595 {
8596 as_bad (_("Expression too complex"));
8597 offset_expr.X_op = O_constant;
8598 }
8599
8600 if (HAVE_32BIT_ADDRESSES
8601 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
8602 {
8603 char value [32];
8604
8605 sprintf_vma (value, offset_expr.X_add_number);
8606 as_bad (_("Number (0x%s) larger than 32 bits"), value);
8607 }
8608
8609 /* A constant expression in PIC code can be handled just as it
8610 is in non PIC code. */
8611 if (offset_expr.X_op == O_constant)
8612 {
8613 int hipart = 0;
8614
8615 expr1.X_add_number = offset_expr.X_add_number;
8616 normalize_address_expr (&expr1);
8617 if ((offbits == 0 || offbits == 16)
8618 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
8619 {
8620 expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8621 & ~(bfd_vma) 0xffff);
8622 hipart = 1;
8623 }
8624 else if (offbits == 12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
8625 {
8626 expr1.X_add_number = ((expr1.X_add_number + 0x800)
8627 & ~(bfd_vma) 0xfff);
8628 hipart = 1;
8629 }
8630 else if (offbits == 9 && !IS_SEXT_9BIT_NUM (expr1.X_add_number))
8631 {
8632 expr1.X_add_number = ((expr1.X_add_number + 0x100)
8633 & ~(bfd_vma) 0x1ff);
8634 hipart = 1;
8635 }
8636 if (hipart)
8637 {
8638 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8639 if (breg != 0)
8640 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8641 tempreg, tempreg, breg);
8642 breg = tempreg;
8643 }
8644 if (offbits == 0)
8645 {
8646 if (offset_expr.X_add_number == 0)
8647 tempreg = breg;
8648 else
8649 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8650 "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8651 macro_build (NULL, s, fmt, treg, tempreg);
8652 }
8653 else if (offbits == 16)
8654 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8655 else
8656 macro_build (NULL, s, fmt,
8657 treg, (unsigned long) offset_expr.X_add_number, breg);
8658 }
8659 else if (offbits != 16)
8660 {
8661 /* The offset field is too narrow to be used for a low-part
8662 relocation, so load the whole address into the auxillary
8663 register. In the case of "A(b)" addresses, we first load
8664 absolute address "A" into the register and then add base
8665 register "b". In the case of "o(b)" addresses, we simply
8666 need to add 16-bit offset "o" to base register "b", and
8667 offset_reloc already contains the relocations associated
8668 with "o". */
8669 if (ab)
8670 {
8671 load_address (tempreg, &offset_expr, &used_at);
8672 if (breg != 0)
8673 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8674 tempreg, tempreg, breg);
8675 }
8676 else
8677 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8678 tempreg, breg, -1,
8679 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8680 expr1.X_add_number = 0;
8681 if (offbits == 0)
8682 macro_build (NULL, s, fmt, treg, tempreg);
8683 else
8684 macro_build (NULL, s, fmt,
8685 treg, (unsigned long) expr1.X_add_number, tempreg);
8686 }
8687 else if (mips_pic == NO_PIC)
8688 {
8689 /* If this is a reference to a GP relative symbol, and there
8690 is no base register, we want
8691 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
8692 Otherwise, if there is no base register, we want
8693 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8694 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8695 If we have a constant, we need two instructions anyhow,
8696 so we always use the latter form.
8697
8698 If we have a base register, and this is a reference to a
8699 GP relative symbol, we want
8700 addu $tempreg,$breg,$gp
8701 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
8702 Otherwise we want
8703 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8704 addu $tempreg,$tempreg,$breg
8705 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8706 With a constant we always use the latter case.
8707
8708 With 64bit address space and no base register and $at usable,
8709 we want
8710 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8711 lui $at,<sym> (BFD_RELOC_HI16_S)
8712 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8713 dsll32 $tempreg,0
8714 daddu $tempreg,$at
8715 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8716 If we have a base register, we want
8717 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8718 lui $at,<sym> (BFD_RELOC_HI16_S)
8719 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8720 daddu $at,$breg
8721 dsll32 $tempreg,0
8722 daddu $tempreg,$at
8723 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8724
8725 Without $at we can't generate the optimal path for superscalar
8726 processors here since this would require two temporary registers.
8727 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8728 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8729 dsll $tempreg,16
8730 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8731 dsll $tempreg,16
8732 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8733 If we have a base register, we want
8734 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8735 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8736 dsll $tempreg,16
8737 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8738 dsll $tempreg,16
8739 daddu $tempreg,$tempreg,$breg
8740 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8741
8742 For GP relative symbols in 64bit address space we can use
8743 the same sequence as in 32bit address space. */
8744 if (HAVE_64BIT_SYMBOLS)
8745 {
8746 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8747 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8748 {
8749 relax_start (offset_expr.X_add_symbol);
8750 if (breg == 0)
8751 {
8752 macro_build (&offset_expr, s, fmt, treg,
8753 BFD_RELOC_GPREL16, mips_gp_register);
8754 }
8755 else
8756 {
8757 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8758 tempreg, breg, mips_gp_register);
8759 macro_build (&offset_expr, s, fmt, treg,
8760 BFD_RELOC_GPREL16, tempreg);
8761 }
8762 relax_switch ();
8763 }
8764
8765 if (used_at == 0 && mips_opts.at)
8766 {
8767 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8768 BFD_RELOC_MIPS_HIGHEST);
8769 macro_build (&offset_expr, "lui", LUI_FMT, AT,
8770 BFD_RELOC_HI16_S);
8771 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8772 tempreg, BFD_RELOC_MIPS_HIGHER);
8773 if (breg != 0)
8774 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
8775 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
8776 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8777 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
8778 tempreg);
8779 used_at = 1;
8780 }
8781 else
8782 {
8783 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8784 BFD_RELOC_MIPS_HIGHEST);
8785 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8786 tempreg, BFD_RELOC_MIPS_HIGHER);
8787 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8788 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8789 tempreg, BFD_RELOC_HI16_S);
8790 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8791 if (breg != 0)
8792 macro_build (NULL, "daddu", "d,v,t",
8793 tempreg, tempreg, breg);
8794 macro_build (&offset_expr, s, fmt, treg,
8795 BFD_RELOC_LO16, tempreg);
8796 }
8797
8798 if (mips_relax.sequence)
8799 relax_end ();
8800 break;
8801 }
8802
8803 if (breg == 0)
8804 {
8805 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8806 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8807 {
8808 relax_start (offset_expr.X_add_symbol);
8809 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
8810 mips_gp_register);
8811 relax_switch ();
8812 }
8813 macro_build_lui (&offset_expr, tempreg);
8814 macro_build (&offset_expr, s, fmt, treg,
8815 BFD_RELOC_LO16, tempreg);
8816 if (mips_relax.sequence)
8817 relax_end ();
8818 }
8819 else
8820 {
8821 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8822 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8823 {
8824 relax_start (offset_expr.X_add_symbol);
8825 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8826 tempreg, breg, mips_gp_register);
8827 macro_build (&offset_expr, s, fmt, treg,
8828 BFD_RELOC_GPREL16, tempreg);
8829 relax_switch ();
8830 }
8831 macro_build_lui (&offset_expr, tempreg);
8832 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8833 tempreg, tempreg, breg);
8834 macro_build (&offset_expr, s, fmt, treg,
8835 BFD_RELOC_LO16, tempreg);
8836 if (mips_relax.sequence)
8837 relax_end ();
8838 }
8839 }
8840 else if (!mips_big_got)
8841 {
8842 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
8843
8844 /* If this is a reference to an external symbol, we want
8845 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8846 nop
8847 <op> $treg,0($tempreg)
8848 Otherwise we want
8849 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8850 nop
8851 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8852 <op> $treg,0($tempreg)
8853
8854 For NewABI, we want
8855 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8856 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
8857
8858 If there is a base register, we add it to $tempreg before
8859 the <op>. If there is a constant, we stick it in the
8860 <op> instruction. We don't handle constants larger than
8861 16 bits, because we have no way to load the upper 16 bits
8862 (actually, we could handle them for the subset of cases
8863 in which we are not using $at). */
8864 gas_assert (offset_expr.X_op == O_symbol);
8865 if (HAVE_NEWABI)
8866 {
8867 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8868 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8869 if (breg != 0)
8870 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8871 tempreg, tempreg, breg);
8872 macro_build (&offset_expr, s, fmt, treg,
8873 BFD_RELOC_MIPS_GOT_OFST, tempreg);
8874 break;
8875 }
8876 expr1.X_add_number = offset_expr.X_add_number;
8877 offset_expr.X_add_number = 0;
8878 if (expr1.X_add_number < -0x8000
8879 || expr1.X_add_number >= 0x8000)
8880 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8881 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8882 lw_reloc_type, mips_gp_register);
8883 load_delay_nop ();
8884 relax_start (offset_expr.X_add_symbol);
8885 relax_switch ();
8886 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8887 tempreg, BFD_RELOC_LO16);
8888 relax_end ();
8889 if (breg != 0)
8890 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8891 tempreg, tempreg, breg);
8892 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8893 }
8894 else if (mips_big_got && !HAVE_NEWABI)
8895 {
8896 int gpdelay;
8897
8898 /* If this is a reference to an external symbol, we want
8899 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8900 addu $tempreg,$tempreg,$gp
8901 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8902 <op> $treg,0($tempreg)
8903 Otherwise we want
8904 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8905 nop
8906 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8907 <op> $treg,0($tempreg)
8908 If there is a base register, we add it to $tempreg before
8909 the <op>. If there is a constant, we stick it in the
8910 <op> instruction. We don't handle constants larger than
8911 16 bits, because we have no way to load the upper 16 bits
8912 (actually, we could handle them for the subset of cases
8913 in which we are not using $at). */
8914 gas_assert (offset_expr.X_op == O_symbol);
8915 expr1.X_add_number = offset_expr.X_add_number;
8916 offset_expr.X_add_number = 0;
8917 if (expr1.X_add_number < -0x8000
8918 || expr1.X_add_number >= 0x8000)
8919 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8920 gpdelay = reg_needs_delay (mips_gp_register);
8921 relax_start (offset_expr.X_add_symbol);
8922 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8923 BFD_RELOC_MIPS_GOT_HI16);
8924 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8925 mips_gp_register);
8926 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8927 BFD_RELOC_MIPS_GOT_LO16, tempreg);
8928 relax_switch ();
8929 if (gpdelay)
8930 macro_build (NULL, "nop", "");
8931 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8932 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8933 load_delay_nop ();
8934 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8935 tempreg, BFD_RELOC_LO16);
8936 relax_end ();
8937
8938 if (breg != 0)
8939 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8940 tempreg, tempreg, breg);
8941 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8942 }
8943 else if (mips_big_got && HAVE_NEWABI)
8944 {
8945 /* If this is a reference to an external symbol, we want
8946 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8947 add $tempreg,$tempreg,$gp
8948 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8949 <op> $treg,<ofst>($tempreg)
8950 Otherwise, for local symbols, we want:
8951 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8952 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
8953 gas_assert (offset_expr.X_op == O_symbol);
8954 expr1.X_add_number = offset_expr.X_add_number;
8955 offset_expr.X_add_number = 0;
8956 if (expr1.X_add_number < -0x8000
8957 || expr1.X_add_number >= 0x8000)
8958 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8959 relax_start (offset_expr.X_add_symbol);
8960 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8961 BFD_RELOC_MIPS_GOT_HI16);
8962 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8963 mips_gp_register);
8964 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8965 BFD_RELOC_MIPS_GOT_LO16, tempreg);
8966 if (breg != 0)
8967 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8968 tempreg, tempreg, breg);
8969 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8970
8971 relax_switch ();
8972 offset_expr.X_add_number = expr1.X_add_number;
8973 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8974 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8975 if (breg != 0)
8976 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8977 tempreg, tempreg, breg);
8978 macro_build (&offset_expr, s, fmt, treg,
8979 BFD_RELOC_MIPS_GOT_OFST, tempreg);
8980 relax_end ();
8981 }
8982 else
8983 abort ();
8984
8985 break;
8986
8987 case M_LI:
8988 case M_LI_S:
8989 load_register (treg, &imm_expr, 0);
8990 break;
8991
8992 case M_DLI:
8993 load_register (treg, &imm_expr, 1);
8994 break;
8995
8996 case M_LI_SS:
8997 if (imm_expr.X_op == O_constant)
8998 {
8999 used_at = 1;
9000 load_register (AT, &imm_expr, 0);
9001 macro_build (NULL, "mtc1", "t,G", AT, treg);
9002 break;
9003 }
9004 else
9005 {
9006 gas_assert (offset_expr.X_op == O_symbol
9007 && strcmp (segment_name (S_GET_SEGMENT
9008 (offset_expr.X_add_symbol)),
9009 ".lit4") == 0
9010 && offset_expr.X_add_number == 0);
9011 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
9012 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
9013 break;
9014 }
9015
9016 case M_LI_D:
9017 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
9018 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
9019 order 32 bits of the value and the low order 32 bits are either
9020 zero or in OFFSET_EXPR. */
9021 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
9022 {
9023 if (HAVE_64BIT_GPRS)
9024 load_register (treg, &imm_expr, 1);
9025 else
9026 {
9027 int hreg, lreg;
9028
9029 if (target_big_endian)
9030 {
9031 hreg = treg;
9032 lreg = treg + 1;
9033 }
9034 else
9035 {
9036 hreg = treg + 1;
9037 lreg = treg;
9038 }
9039
9040 if (hreg <= 31)
9041 load_register (hreg, &imm_expr, 0);
9042 if (lreg <= 31)
9043 {
9044 if (offset_expr.X_op == O_absent)
9045 move_register (lreg, 0);
9046 else
9047 {
9048 gas_assert (offset_expr.X_op == O_constant);
9049 load_register (lreg, &offset_expr, 0);
9050 }
9051 }
9052 }
9053 break;
9054 }
9055
9056 /* We know that sym is in the .rdata section. First we get the
9057 upper 16 bits of the address. */
9058 if (mips_pic == NO_PIC)
9059 {
9060 macro_build_lui (&offset_expr, AT);
9061 used_at = 1;
9062 }
9063 else
9064 {
9065 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9066 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9067 used_at = 1;
9068 }
9069
9070 /* Now we load the register(s). */
9071 if (HAVE_64BIT_GPRS)
9072 {
9073 used_at = 1;
9074 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
9075 }
9076 else
9077 {
9078 used_at = 1;
9079 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
9080 if (treg != RA)
9081 {
9082 /* FIXME: How in the world do we deal with the possible
9083 overflow here? */
9084 offset_expr.X_add_number += 4;
9085 macro_build (&offset_expr, "lw", "t,o(b)",
9086 treg + 1, BFD_RELOC_LO16, AT);
9087 }
9088 }
9089 break;
9090
9091 case M_LI_DD:
9092 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
9093 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
9094 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
9095 the value and the low order 32 bits are either zero or in
9096 OFFSET_EXPR. */
9097 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
9098 {
9099 used_at = 1;
9100 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
9101 if (HAVE_64BIT_FPRS)
9102 {
9103 gas_assert (HAVE_64BIT_GPRS);
9104 macro_build (NULL, "dmtc1", "t,S", AT, treg);
9105 }
9106 else
9107 {
9108 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
9109 if (offset_expr.X_op == O_absent)
9110 macro_build (NULL, "mtc1", "t,G", 0, treg);
9111 else
9112 {
9113 gas_assert (offset_expr.X_op == O_constant);
9114 load_register (AT, &offset_expr, 0);
9115 macro_build (NULL, "mtc1", "t,G", AT, treg);
9116 }
9117 }
9118 break;
9119 }
9120
9121 gas_assert (offset_expr.X_op == O_symbol
9122 && offset_expr.X_add_number == 0);
9123 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
9124 if (strcmp (s, ".lit8") == 0)
9125 {
9126 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
9127 {
9128 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
9129 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
9130 break;
9131 }
9132 breg = mips_gp_register;
9133 r = BFD_RELOC_MIPS_LITERAL;
9134 goto dob;
9135 }
9136 else
9137 {
9138 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
9139 used_at = 1;
9140 if (mips_pic != NO_PIC)
9141 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9142 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9143 else
9144 {
9145 /* FIXME: This won't work for a 64 bit address. */
9146 macro_build_lui (&offset_expr, AT);
9147 }
9148
9149 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
9150 {
9151 macro_build (&offset_expr, "ldc1", "T,o(b)",
9152 treg, BFD_RELOC_LO16, AT);
9153 break;
9154 }
9155 breg = AT;
9156 r = BFD_RELOC_LO16;
9157 goto dob;
9158 }
9159
9160 case M_L_DOB:
9161 /* Even on a big endian machine $fn comes before $fn+1. We have
9162 to adjust when loading from memory. */
9163 r = BFD_RELOC_LO16;
9164 dob:
9165 gas_assert (!mips_opts.micromips);
9166 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
9167 macro_build (&offset_expr, "lwc1", "T,o(b)",
9168 target_big_endian ? treg + 1 : treg, r, breg);
9169 /* FIXME: A possible overflow which I don't know how to deal
9170 with. */
9171 offset_expr.X_add_number += 4;
9172 macro_build (&offset_expr, "lwc1", "T,o(b)",
9173 target_big_endian ? treg : treg + 1, r, breg);
9174 break;
9175
9176 case M_S_DOB:
9177 gas_assert (!mips_opts.micromips);
9178 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
9179 /* Even on a big endian machine $fn comes before $fn+1. We have
9180 to adjust when storing to memory. */
9181 macro_build (&offset_expr, "swc1", "T,o(b)",
9182 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
9183 offset_expr.X_add_number += 4;
9184 macro_build (&offset_expr, "swc1", "T,o(b)",
9185 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
9186 break;
9187
9188 case M_L_DAB:
9189 gas_assert (!mips_opts.micromips);
9190 /*
9191 * The MIPS assembler seems to check for X_add_number not
9192 * being double aligned and generating:
9193 * lui at,%hi(foo+1)
9194 * addu at,at,v1
9195 * addiu at,at,%lo(foo+1)
9196 * lwc1 f2,0(at)
9197 * lwc1 f3,4(at)
9198 * But, the resulting address is the same after relocation so why
9199 * generate the extra instruction?
9200 */
9201 /* Itbl support may require additional care here. */
9202 coproc = 1;
9203 fmt = "T,o(b)";
9204 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
9205 {
9206 s = "ldc1";
9207 goto ld_st;
9208 }
9209 s = "lwc1";
9210 goto ldd_std;
9211
9212 case M_S_DAB:
9213 gas_assert (!mips_opts.micromips);
9214 /* Itbl support may require additional care here. */
9215 coproc = 1;
9216 fmt = "T,o(b)";
9217 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
9218 {
9219 s = "sdc1";
9220 goto ld_st;
9221 }
9222 s = "swc1";
9223 goto ldd_std;
9224
9225 case M_LQ_AB:
9226 fmt = "t,o(b)";
9227 s = "lq";
9228 goto ld;
9229
9230 case M_SQ_AB:
9231 fmt = "t,o(b)";
9232 s = "sq";
9233 goto ld_st;
9234
9235 case M_LD_AB:
9236 fmt = "t,o(b)";
9237 if (HAVE_64BIT_GPRS)
9238 {
9239 s = "ld";
9240 goto ld;
9241 }
9242 s = "lw";
9243 goto ldd_std;
9244
9245 case M_SD_AB:
9246 fmt = "t,o(b)";
9247 if (HAVE_64BIT_GPRS)
9248 {
9249 s = "sd";
9250 goto ld_st;
9251 }
9252 s = "sw";
9253
9254 ldd_std:
9255 if (offset_expr.X_op != O_symbol
9256 && offset_expr.X_op != O_constant)
9257 {
9258 as_bad (_("Expression too complex"));
9259 offset_expr.X_op = O_constant;
9260 }
9261
9262 if (HAVE_32BIT_ADDRESSES
9263 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
9264 {
9265 char value [32];
9266
9267 sprintf_vma (value, offset_expr.X_add_number);
9268 as_bad (_("Number (0x%s) larger than 32 bits"), value);
9269 }
9270
9271 /* Even on a big endian machine $fn comes before $fn+1. We have
9272 to adjust when loading from memory. We set coproc if we must
9273 load $fn+1 first. */
9274 /* Itbl support may require additional care here. */
9275 if (!target_big_endian)
9276 coproc = 0;
9277
9278 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
9279 {
9280 /* If this is a reference to a GP relative symbol, we want
9281 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
9282 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
9283 If we have a base register, we use this
9284 addu $at,$breg,$gp
9285 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
9286 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
9287 If this is not a GP relative symbol, we want
9288 lui $at,<sym> (BFD_RELOC_HI16_S)
9289 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9290 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9291 If there is a base register, we add it to $at after the
9292 lui instruction. If there is a constant, we always use
9293 the last case. */
9294 if (offset_expr.X_op == O_symbol
9295 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9296 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9297 {
9298 relax_start (offset_expr.X_add_symbol);
9299 if (breg == 0)
9300 {
9301 tempreg = mips_gp_register;
9302 }
9303 else
9304 {
9305 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9306 AT, breg, mips_gp_register);
9307 tempreg = AT;
9308 used_at = 1;
9309 }
9310
9311 /* Itbl support may require additional care here. */
9312 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9313 BFD_RELOC_GPREL16, tempreg);
9314 offset_expr.X_add_number += 4;
9315
9316 /* Set mips_optimize to 2 to avoid inserting an
9317 undesired nop. */
9318 hold_mips_optimize = mips_optimize;
9319 mips_optimize = 2;
9320 /* Itbl support may require additional care here. */
9321 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9322 BFD_RELOC_GPREL16, tempreg);
9323 mips_optimize = hold_mips_optimize;
9324
9325 relax_switch ();
9326
9327 offset_expr.X_add_number -= 4;
9328 }
9329 used_at = 1;
9330 macro_build_lui (&offset_expr, AT);
9331 if (breg != 0)
9332 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9333 /* Itbl support may require additional care here. */
9334 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9335 BFD_RELOC_LO16, AT);
9336 /* FIXME: How do we handle overflow here? */
9337 offset_expr.X_add_number += 4;
9338 /* Itbl support may require additional care here. */
9339 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9340 BFD_RELOC_LO16, AT);
9341 if (mips_relax.sequence)
9342 relax_end ();
9343 }
9344 else if (!mips_big_got)
9345 {
9346 /* If this is a reference to an external symbol, we want
9347 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9348 nop
9349 <op> $treg,0($at)
9350 <op> $treg+1,4($at)
9351 Otherwise we want
9352 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9353 nop
9354 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9355 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9356 If there is a base register we add it to $at before the
9357 lwc1 instructions. If there is a constant we include it
9358 in the lwc1 instructions. */
9359 used_at = 1;
9360 expr1.X_add_number = offset_expr.X_add_number;
9361 if (expr1.X_add_number < -0x8000
9362 || expr1.X_add_number >= 0x8000 - 4)
9363 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9364 load_got_offset (AT, &offset_expr);
9365 load_delay_nop ();
9366 if (breg != 0)
9367 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9368
9369 /* Set mips_optimize to 2 to avoid inserting an undesired
9370 nop. */
9371 hold_mips_optimize = mips_optimize;
9372 mips_optimize = 2;
9373
9374 /* Itbl support may require additional care here. */
9375 relax_start (offset_expr.X_add_symbol);
9376 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9377 BFD_RELOC_LO16, AT);
9378 expr1.X_add_number += 4;
9379 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9380 BFD_RELOC_LO16, AT);
9381 relax_switch ();
9382 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9383 BFD_RELOC_LO16, AT);
9384 offset_expr.X_add_number += 4;
9385 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9386 BFD_RELOC_LO16, AT);
9387 relax_end ();
9388
9389 mips_optimize = hold_mips_optimize;
9390 }
9391 else if (mips_big_got)
9392 {
9393 int gpdelay;
9394
9395 /* If this is a reference to an external symbol, we want
9396 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9397 addu $at,$at,$gp
9398 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
9399 nop
9400 <op> $treg,0($at)
9401 <op> $treg+1,4($at)
9402 Otherwise we want
9403 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9404 nop
9405 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9406 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9407 If there is a base register we add it to $at before the
9408 lwc1 instructions. If there is a constant we include it
9409 in the lwc1 instructions. */
9410 used_at = 1;
9411 expr1.X_add_number = offset_expr.X_add_number;
9412 offset_expr.X_add_number = 0;
9413 if (expr1.X_add_number < -0x8000
9414 || expr1.X_add_number >= 0x8000 - 4)
9415 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9416 gpdelay = reg_needs_delay (mips_gp_register);
9417 relax_start (offset_expr.X_add_symbol);
9418 macro_build (&offset_expr, "lui", LUI_FMT,
9419 AT, BFD_RELOC_MIPS_GOT_HI16);
9420 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9421 AT, AT, mips_gp_register);
9422 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9423 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
9424 load_delay_nop ();
9425 if (breg != 0)
9426 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9427 /* Itbl support may require additional care here. */
9428 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9429 BFD_RELOC_LO16, AT);
9430 expr1.X_add_number += 4;
9431
9432 /* Set mips_optimize to 2 to avoid inserting an undesired
9433 nop. */
9434 hold_mips_optimize = mips_optimize;
9435 mips_optimize = 2;
9436 /* Itbl support may require additional care here. */
9437 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9438 BFD_RELOC_LO16, AT);
9439 mips_optimize = hold_mips_optimize;
9440 expr1.X_add_number -= 4;
9441
9442 relax_switch ();
9443 offset_expr.X_add_number = expr1.X_add_number;
9444 if (gpdelay)
9445 macro_build (NULL, "nop", "");
9446 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9447 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9448 load_delay_nop ();
9449 if (breg != 0)
9450 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9451 /* Itbl support may require additional care here. */
9452 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9453 BFD_RELOC_LO16, AT);
9454 offset_expr.X_add_number += 4;
9455
9456 /* Set mips_optimize to 2 to avoid inserting an undesired
9457 nop. */
9458 hold_mips_optimize = mips_optimize;
9459 mips_optimize = 2;
9460 /* Itbl support may require additional care here. */
9461 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9462 BFD_RELOC_LO16, AT);
9463 mips_optimize = hold_mips_optimize;
9464 relax_end ();
9465 }
9466 else
9467 abort ();
9468
9469 break;
9470
9471 case M_LD_OB:
9472 s = HAVE_64BIT_GPRS ? "ld" : "lw";
9473 goto sd_ob;
9474 case M_SD_OB:
9475 s = HAVE_64BIT_GPRS ? "sd" : "sw";
9476 sd_ob:
9477 macro_build (&offset_expr, s, "t,o(b)", treg,
9478 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9479 breg);
9480 if (!HAVE_64BIT_GPRS)
9481 {
9482 offset_expr.X_add_number += 4;
9483 macro_build (&offset_expr, s, "t,o(b)", treg + 1,
9484 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9485 breg);
9486 }
9487 break;
9488
9489
9490 case M_SAA_AB:
9491 ab = 1;
9492 case M_SAA_OB:
9493 s = "saa";
9494 offbits = 0;
9495 fmt = "t,(b)";
9496 goto ld_st;
9497 case M_SAAD_AB:
9498 ab = 1;
9499 case M_SAAD_OB:
9500 s = "saad";
9501 offbits = 0;
9502 fmt = "t,(b)";
9503 goto ld_st;
9504
9505 /* New code added to support COPZ instructions.
9506 This code builds table entries out of the macros in mip_opcodes.
9507 R4000 uses interlocks to handle coproc delays.
9508 Other chips (like the R3000) require nops to be inserted for delays.
9509
9510 FIXME: Currently, we require that the user handle delays.
9511 In order to fill delay slots for non-interlocked chips,
9512 we must have a way to specify delays based on the coprocessor.
9513 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9514 What are the side-effects of the cop instruction?
9515 What cache support might we have and what are its effects?
9516 Both coprocessor & memory require delays. how long???
9517 What registers are read/set/modified?
9518
9519 If an itbl is provided to interpret cop instructions,
9520 this knowledge can be encoded in the itbl spec. */
9521
9522 case M_COP0:
9523 s = "c0";
9524 goto copz;
9525 case M_COP1:
9526 s = "c1";
9527 goto copz;
9528 case M_COP2:
9529 s = "c2";
9530 goto copz;
9531 case M_COP3:
9532 s = "c3";
9533 copz:
9534 gas_assert (!mips_opts.micromips);
9535 /* For now we just do C (same as Cz). The parameter will be
9536 stored in insn_opcode by mips_ip. */
9537 macro_build (NULL, s, "C", ip->insn_opcode);
9538 break;
9539
9540 case M_MOVE:
9541 move_register (dreg, sreg);
9542 break;
9543
9544 case M_DMUL:
9545 dbl = 1;
9546 case M_MUL:
9547 if (mips_opts.arch == CPU_R5900)
9548 {
9549 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", dreg, sreg, treg);
9550 }
9551 else
9552 {
9553 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
9554 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9555 }
9556 break;
9557
9558 case M_DMUL_I:
9559 dbl = 1;
9560 case M_MUL_I:
9561 /* The MIPS assembler some times generates shifts and adds. I'm
9562 not trying to be that fancy. GCC should do this for us
9563 anyway. */
9564 used_at = 1;
9565 load_register (AT, &imm_expr, dbl);
9566 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
9567 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9568 break;
9569
9570 case M_DMULO_I:
9571 dbl = 1;
9572 case M_MULO_I:
9573 imm = 1;
9574 goto do_mulo;
9575
9576 case M_DMULO:
9577 dbl = 1;
9578 case M_MULO:
9579 do_mulo:
9580 start_noreorder ();
9581 used_at = 1;
9582 if (imm)
9583 load_register (AT, &imm_expr, dbl);
9584 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
9585 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9586 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9587 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9588 if (mips_trap)
9589 macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
9590 else
9591 {
9592 if (mips_opts.micromips)
9593 micromips_label_expr (&label_expr);
9594 else
9595 label_expr.X_add_number = 8;
9596 macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
9597 macro_build (NULL, "nop", "");
9598 macro_build (NULL, "break", BRK_FMT, 6);
9599 if (mips_opts.micromips)
9600 micromips_add_label ();
9601 }
9602 end_noreorder ();
9603 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9604 break;
9605
9606 case M_DMULOU_I:
9607 dbl = 1;
9608 case M_MULOU_I:
9609 imm = 1;
9610 goto do_mulou;
9611
9612 case M_DMULOU:
9613 dbl = 1;
9614 case M_MULOU:
9615 do_mulou:
9616 start_noreorder ();
9617 used_at = 1;
9618 if (imm)
9619 load_register (AT, &imm_expr, dbl);
9620 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
9621 sreg, imm ? AT : treg);
9622 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9623 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9624 if (mips_trap)
9625 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
9626 else
9627 {
9628 if (mips_opts.micromips)
9629 micromips_label_expr (&label_expr);
9630 else
9631 label_expr.X_add_number = 8;
9632 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
9633 macro_build (NULL, "nop", "");
9634 macro_build (NULL, "break", BRK_FMT, 6);
9635 if (mips_opts.micromips)
9636 micromips_add_label ();
9637 }
9638 end_noreorder ();
9639 break;
9640
9641 case M_DROL:
9642 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9643 {
9644 if (dreg == sreg)
9645 {
9646 tempreg = AT;
9647 used_at = 1;
9648 }
9649 else
9650 {
9651 tempreg = dreg;
9652 }
9653 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9654 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
9655 break;
9656 }
9657 used_at = 1;
9658 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9659 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9660 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9661 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9662 break;
9663
9664 case M_ROL:
9665 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9666 {
9667 if (dreg == sreg)
9668 {
9669 tempreg = AT;
9670 used_at = 1;
9671 }
9672 else
9673 {
9674 tempreg = dreg;
9675 }
9676 macro_build (NULL, "negu", "d,w", tempreg, treg);
9677 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
9678 break;
9679 }
9680 used_at = 1;
9681 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9682 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9683 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9684 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9685 break;
9686
9687 case M_DROL_I:
9688 {
9689 unsigned int rot;
9690 char *l;
9691 char *rr;
9692
9693 if (imm_expr.X_op != O_constant)
9694 as_bad (_("Improper rotate count"));
9695 rot = imm_expr.X_add_number & 0x3f;
9696 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9697 {
9698 rot = (64 - rot) & 0x3f;
9699 if (rot >= 32)
9700 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9701 else
9702 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9703 break;
9704 }
9705 if (rot == 0)
9706 {
9707 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9708 break;
9709 }
9710 l = (rot < 0x20) ? "dsll" : "dsll32";
9711 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
9712 rot &= 0x1f;
9713 used_at = 1;
9714 macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9715 macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9716 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9717 }
9718 break;
9719
9720 case M_ROL_I:
9721 {
9722 unsigned int rot;
9723
9724 if (imm_expr.X_op != O_constant)
9725 as_bad (_("Improper rotate count"));
9726 rot = imm_expr.X_add_number & 0x1f;
9727 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9728 {
9729 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
9730 break;
9731 }
9732 if (rot == 0)
9733 {
9734 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9735 break;
9736 }
9737 used_at = 1;
9738 macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9739 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9740 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9741 }
9742 break;
9743
9744 case M_DROR:
9745 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9746 {
9747 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
9748 break;
9749 }
9750 used_at = 1;
9751 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9752 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9753 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9754 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9755 break;
9756
9757 case M_ROR:
9758 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9759 {
9760 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
9761 break;
9762 }
9763 used_at = 1;
9764 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9765 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
9766 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
9767 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9768 break;
9769
9770 case M_DROR_I:
9771 {
9772 unsigned int rot;
9773 char *l;
9774 char *rr;
9775
9776 if (imm_expr.X_op != O_constant)
9777 as_bad (_("Improper rotate count"));
9778 rot = imm_expr.X_add_number & 0x3f;
9779 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9780 {
9781 if (rot >= 32)
9782 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9783 else
9784 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9785 break;
9786 }
9787 if (rot == 0)
9788 {
9789 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9790 break;
9791 }
9792 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
9793 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
9794 rot &= 0x1f;
9795 used_at = 1;
9796 macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
9797 macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9798 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9799 }
9800 break;
9801
9802 case M_ROR_I:
9803 {
9804 unsigned int rot;
9805
9806 if (imm_expr.X_op != O_constant)
9807 as_bad (_("Improper rotate count"));
9808 rot = imm_expr.X_add_number & 0x1f;
9809 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9810 {
9811 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
9812 break;
9813 }
9814 if (rot == 0)
9815 {
9816 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9817 break;
9818 }
9819 used_at = 1;
9820 macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
9821 macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9822 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9823 }
9824 break;
9825
9826 case M_SEQ:
9827 if (sreg == 0)
9828 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
9829 else if (treg == 0)
9830 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9831 else
9832 {
9833 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9834 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9835 }
9836 break;
9837
9838 case M_SEQ_I:
9839 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9840 {
9841 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9842 break;
9843 }
9844 if (sreg == 0)
9845 {
9846 as_warn (_("Instruction %s: result is always false"),
9847 ip->insn_mo->name);
9848 move_register (dreg, 0);
9849 break;
9850 }
9851 if (CPU_HAS_SEQ (mips_opts.arch)
9852 && -512 <= imm_expr.X_add_number
9853 && imm_expr.X_add_number < 512)
9854 {
9855 macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
9856 (int) imm_expr.X_add_number);
9857 break;
9858 }
9859 if (imm_expr.X_op == O_constant
9860 && imm_expr.X_add_number >= 0
9861 && imm_expr.X_add_number < 0x10000)
9862 {
9863 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
9864 }
9865 else if (imm_expr.X_op == O_constant
9866 && imm_expr.X_add_number > -0x8000
9867 && imm_expr.X_add_number < 0)
9868 {
9869 imm_expr.X_add_number = -imm_expr.X_add_number;
9870 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
9871 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9872 }
9873 else if (CPU_HAS_SEQ (mips_opts.arch))
9874 {
9875 used_at = 1;
9876 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9877 macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
9878 break;
9879 }
9880 else
9881 {
9882 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9883 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
9884 used_at = 1;
9885 }
9886 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9887 break;
9888
9889 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
9890 s = "slt";
9891 goto sge;
9892 case M_SGEU:
9893 s = "sltu";
9894 sge:
9895 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
9896 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9897 break;
9898
9899 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
9900 case M_SGEU_I:
9901 if (imm_expr.X_op == O_constant
9902 && imm_expr.X_add_number >= -0x8000
9903 && imm_expr.X_add_number < 0x8000)
9904 {
9905 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
9906 dreg, sreg, BFD_RELOC_LO16);
9907 }
9908 else
9909 {
9910 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9911 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
9912 dreg, sreg, AT);
9913 used_at = 1;
9914 }
9915 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9916 break;
9917
9918 case M_SGT: /* sreg > treg <==> treg < sreg */
9919 s = "slt";
9920 goto sgt;
9921 case M_SGTU:
9922 s = "sltu";
9923 sgt:
9924 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9925 break;
9926
9927 case M_SGT_I: /* sreg > I <==> I < sreg */
9928 s = "slt";
9929 goto sgti;
9930 case M_SGTU_I:
9931 s = "sltu";
9932 sgti:
9933 used_at = 1;
9934 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9935 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9936 break;
9937
9938 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
9939 s = "slt";
9940 goto sle;
9941 case M_SLEU:
9942 s = "sltu";
9943 sle:
9944 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9945 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9946 break;
9947
9948 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
9949 s = "slt";
9950 goto slei;
9951 case M_SLEU_I:
9952 s = "sltu";
9953 slei:
9954 used_at = 1;
9955 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9956 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9957 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9958 break;
9959
9960 case M_SLT_I:
9961 if (imm_expr.X_op == O_constant
9962 && imm_expr.X_add_number >= -0x8000
9963 && imm_expr.X_add_number < 0x8000)
9964 {
9965 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9966 break;
9967 }
9968 used_at = 1;
9969 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9970 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
9971 break;
9972
9973 case M_SLTU_I:
9974 if (imm_expr.X_op == O_constant
9975 && imm_expr.X_add_number >= -0x8000
9976 && imm_expr.X_add_number < 0x8000)
9977 {
9978 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
9979 BFD_RELOC_LO16);
9980 break;
9981 }
9982 used_at = 1;
9983 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9984 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
9985 break;
9986
9987 case M_SNE:
9988 if (sreg == 0)
9989 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
9990 else if (treg == 0)
9991 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
9992 else
9993 {
9994 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9995 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
9996 }
9997 break;
9998
9999 case M_SNE_I:
10000 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
10001 {
10002 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
10003 break;
10004 }
10005 if (sreg == 0)
10006 {
10007 as_warn (_("Instruction %s: result is always true"),
10008 ip->insn_mo->name);
10009 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
10010 dreg, 0, BFD_RELOC_LO16);
10011 break;
10012 }
10013 if (CPU_HAS_SEQ (mips_opts.arch)
10014 && -512 <= imm_expr.X_add_number
10015 && imm_expr.X_add_number < 512)
10016 {
10017 macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
10018 (int) imm_expr.X_add_number);
10019 break;
10020 }
10021 if (imm_expr.X_op == O_constant
10022 && imm_expr.X_add_number >= 0
10023 && imm_expr.X_add_number < 0x10000)
10024 {
10025 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
10026 }
10027 else if (imm_expr.X_op == O_constant
10028 && imm_expr.X_add_number > -0x8000
10029 && imm_expr.X_add_number < 0)
10030 {
10031 imm_expr.X_add_number = -imm_expr.X_add_number;
10032 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
10033 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10034 }
10035 else if (CPU_HAS_SEQ (mips_opts.arch))
10036 {
10037 used_at = 1;
10038 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10039 macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
10040 break;
10041 }
10042 else
10043 {
10044 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10045 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
10046 used_at = 1;
10047 }
10048 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
10049 break;
10050
10051 case M_SUB_I:
10052 s = "addi";
10053 s2 = "sub";
10054 goto do_subi;
10055 case M_SUBU_I:
10056 s = "addiu";
10057 s2 = "subu";
10058 goto do_subi;
10059 case M_DSUB_I:
10060 dbl = 1;
10061 s = "daddi";
10062 s2 = "dsub";
10063 if (!mips_opts.micromips)
10064 goto do_subi;
10065 if (imm_expr.X_op == O_constant
10066 && imm_expr.X_add_number > -0x200
10067 && imm_expr.X_add_number <= 0x200)
10068 {
10069 macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
10070 break;
10071 }
10072 goto do_subi_i;
10073 case M_DSUBU_I:
10074 dbl = 1;
10075 s = "daddiu";
10076 s2 = "dsubu";
10077 do_subi:
10078 if (imm_expr.X_op == O_constant
10079 && imm_expr.X_add_number > -0x8000
10080 && imm_expr.X_add_number <= 0x8000)
10081 {
10082 imm_expr.X_add_number = -imm_expr.X_add_number;
10083 macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10084 break;
10085 }
10086 do_subi_i:
10087 used_at = 1;
10088 load_register (AT, &imm_expr, dbl);
10089 macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
10090 break;
10091
10092 case M_TEQ_I:
10093 s = "teq";
10094 goto trap;
10095 case M_TGE_I:
10096 s = "tge";
10097 goto trap;
10098 case M_TGEU_I:
10099 s = "tgeu";
10100 goto trap;
10101 case M_TLT_I:
10102 s = "tlt";
10103 goto trap;
10104 case M_TLTU_I:
10105 s = "tltu";
10106 goto trap;
10107 case M_TNE_I:
10108 s = "tne";
10109 trap:
10110 used_at = 1;
10111 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10112 macro_build (NULL, s, "s,t", sreg, AT);
10113 break;
10114
10115 case M_TRUNCWS:
10116 case M_TRUNCWD:
10117 gas_assert (!mips_opts.micromips);
10118 gas_assert (mips_opts.isa == ISA_MIPS1);
10119 used_at = 1;
10120 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
10121 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
10122
10123 /*
10124 * Is the double cfc1 instruction a bug in the mips assembler;
10125 * or is there a reason for it?
10126 */
10127 start_noreorder ();
10128 macro_build (NULL, "cfc1", "t,G", treg, RA);
10129 macro_build (NULL, "cfc1", "t,G", treg, RA);
10130 macro_build (NULL, "nop", "");
10131 expr1.X_add_number = 3;
10132 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
10133 expr1.X_add_number = 2;
10134 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
10135 macro_build (NULL, "ctc1", "t,G", AT, RA);
10136 macro_build (NULL, "nop", "");
10137 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
10138 dreg, sreg);
10139 macro_build (NULL, "ctc1", "t,G", treg, RA);
10140 macro_build (NULL, "nop", "");
10141 end_noreorder ();
10142 break;
10143
10144 case M_ULH_A:
10145 ab = 1;
10146 case M_ULH:
10147 s = "lb";
10148 s2 = "lbu";
10149 off = 1;
10150 goto uld_st;
10151 case M_ULHU_A:
10152 ab = 1;
10153 case M_ULHU:
10154 s = "lbu";
10155 s2 = "lbu";
10156 off = 1;
10157 goto uld_st;
10158 case M_ULW_A:
10159 ab = 1;
10160 case M_ULW:
10161 s = "lwl";
10162 s2 = "lwr";
10163 offbits = (mips_opts.micromips ? 12 : 16);
10164 off = 3;
10165 goto uld_st;
10166 case M_ULD_A:
10167 ab = 1;
10168 case M_ULD:
10169 s = "ldl";
10170 s2 = "ldr";
10171 offbits = (mips_opts.micromips ? 12 : 16);
10172 off = 7;
10173 goto uld_st;
10174 case M_USH_A:
10175 ab = 1;
10176 case M_USH:
10177 s = "sb";
10178 s2 = "sb";
10179 off = 1;
10180 ust = 1;
10181 goto uld_st;
10182 case M_USW_A:
10183 ab = 1;
10184 case M_USW:
10185 s = "swl";
10186 s2 = "swr";
10187 offbits = (mips_opts.micromips ? 12 : 16);
10188 off = 3;
10189 ust = 1;
10190 goto uld_st;
10191 case M_USD_A:
10192 ab = 1;
10193 case M_USD:
10194 s = "sdl";
10195 s2 = "sdr";
10196 offbits = (mips_opts.micromips ? 12 : 16);
10197 off = 7;
10198 ust = 1;
10199
10200 uld_st:
10201 if (!ab && offset_expr.X_add_number >= 0x8000 - off)
10202 as_bad (_("Operand overflow"));
10203
10204 ep = &offset_expr;
10205 expr1.X_add_number = 0;
10206 if (ab)
10207 {
10208 used_at = 1;
10209 tempreg = AT;
10210 load_address (tempreg, ep, &used_at);
10211 if (breg != 0)
10212 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10213 tempreg, tempreg, breg);
10214 breg = tempreg;
10215 tempreg = treg;
10216 ep = &expr1;
10217 }
10218 else if (offbits == 12
10219 && (offset_expr.X_op != O_constant
10220 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
10221 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
10222 {
10223 used_at = 1;
10224 tempreg = AT;
10225 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
10226 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10227 breg = tempreg;
10228 tempreg = treg;
10229 ep = &expr1;
10230 }
10231 else if (!ust && treg == breg)
10232 {
10233 used_at = 1;
10234 tempreg = AT;
10235 }
10236 else
10237 tempreg = treg;
10238
10239 if (off == 1)
10240 goto ulh_sh;
10241
10242 if (!target_big_endian)
10243 ep->X_add_number += off;
10244 if (offbits != 12)
10245 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10246 else
10247 macro_build (NULL, s, "t,~(b)",
10248 tempreg, (unsigned long) ep->X_add_number, breg);
10249
10250 if (!target_big_endian)
10251 ep->X_add_number -= off;
10252 else
10253 ep->X_add_number += off;
10254 if (offbits != 12)
10255 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10256 else
10257 macro_build (NULL, s2, "t,~(b)",
10258 tempreg, (unsigned long) ep->X_add_number, breg);
10259
10260 /* If necessary, move the result in tempreg to the final destination. */
10261 if (!ust && treg != tempreg)
10262 {
10263 /* Protect second load's delay slot. */
10264 load_delay_nop ();
10265 move_register (treg, tempreg);
10266 }
10267 break;
10268
10269 ulh_sh:
10270 used_at = 1;
10271 if (target_big_endian == ust)
10272 ep->X_add_number += off;
10273 tempreg = ust || ab ? treg : AT;
10274 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10275
10276 /* For halfword transfers we need a temporary register to shuffle
10277 bytes. Unfortunately for M_USH_A we have none available before
10278 the next store as AT holds the base address. We deal with this
10279 case by clobbering TREG and then restoring it as with ULH. */
10280 tempreg = ust == ab ? treg : AT;
10281 if (ust)
10282 macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10283
10284 if (target_big_endian == ust)
10285 ep->X_add_number -= off;
10286 else
10287 ep->X_add_number += off;
10288 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10289
10290 /* For M_USH_A re-retrieve the LSB. */
10291 if (ust && ab)
10292 {
10293 if (target_big_endian)
10294 ep->X_add_number += off;
10295 else
10296 ep->X_add_number -= off;
10297 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10298 }
10299 /* For ULH and M_USH_A OR the LSB in. */
10300 if (!ust || ab)
10301 {
10302 tempreg = !ab ? AT : treg;
10303 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10304 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10305 }
10306 break;
10307
10308 default:
10309 /* FIXME: Check if this is one of the itbl macros, since they
10310 are added dynamically. */
10311 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10312 break;
10313 }
10314 if (!mips_opts.at && used_at)
10315 as_bad (_("Macro used $at after \".set noat\""));
10316 }
10317
10318 /* Implement macros in mips16 mode. */
10319
10320 static void
10321 mips16_macro (struct mips_cl_insn *ip)
10322 {
10323 int mask;
10324 int xreg, yreg, zreg, tmp;
10325 expressionS expr1;
10326 int dbl;
10327 const char *s, *s2, *s3;
10328
10329 mask = ip->insn_mo->mask;
10330
10331 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10332 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10333 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
10334
10335 expr1.X_op = O_constant;
10336 expr1.X_op_symbol = NULL;
10337 expr1.X_add_symbol = NULL;
10338 expr1.X_add_number = 1;
10339
10340 dbl = 0;
10341
10342 switch (mask)
10343 {
10344 default:
10345 abort ();
10346
10347 case M_DDIV_3:
10348 dbl = 1;
10349 case M_DIV_3:
10350 s = "mflo";
10351 goto do_div3;
10352 case M_DREM_3:
10353 dbl = 1;
10354 case M_REM_3:
10355 s = "mfhi";
10356 do_div3:
10357 start_noreorder ();
10358 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
10359 expr1.X_add_number = 2;
10360 macro_build (&expr1, "bnez", "x,p", yreg);
10361 macro_build (NULL, "break", "6", 7);
10362
10363 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10364 since that causes an overflow. We should do that as well,
10365 but I don't see how to do the comparisons without a temporary
10366 register. */
10367 end_noreorder ();
10368 macro_build (NULL, s, "x", zreg);
10369 break;
10370
10371 case M_DIVU_3:
10372 s = "divu";
10373 s2 = "mflo";
10374 goto do_divu3;
10375 case M_REMU_3:
10376 s = "divu";
10377 s2 = "mfhi";
10378 goto do_divu3;
10379 case M_DDIVU_3:
10380 s = "ddivu";
10381 s2 = "mflo";
10382 goto do_divu3;
10383 case M_DREMU_3:
10384 s = "ddivu";
10385 s2 = "mfhi";
10386 do_divu3:
10387 start_noreorder ();
10388 macro_build (NULL, s, "0,x,y", xreg, yreg);
10389 expr1.X_add_number = 2;
10390 macro_build (&expr1, "bnez", "x,p", yreg);
10391 macro_build (NULL, "break", "6", 7);
10392 end_noreorder ();
10393 macro_build (NULL, s2, "x", zreg);
10394 break;
10395
10396 case M_DMUL:
10397 dbl = 1;
10398 case M_MUL:
10399 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10400 macro_build (NULL, "mflo", "x", zreg);
10401 break;
10402
10403 case M_DSUBU_I:
10404 dbl = 1;
10405 goto do_subu;
10406 case M_SUBU_I:
10407 do_subu:
10408 if (imm_expr.X_op != O_constant)
10409 as_bad (_("Unsupported large constant"));
10410 imm_expr.X_add_number = -imm_expr.X_add_number;
10411 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
10412 break;
10413
10414 case M_SUBU_I_2:
10415 if (imm_expr.X_op != O_constant)
10416 as_bad (_("Unsupported large constant"));
10417 imm_expr.X_add_number = -imm_expr.X_add_number;
10418 macro_build (&imm_expr, "addiu", "x,k", xreg);
10419 break;
10420
10421 case M_DSUBU_I_2:
10422 if (imm_expr.X_op != O_constant)
10423 as_bad (_("Unsupported large constant"));
10424 imm_expr.X_add_number = -imm_expr.X_add_number;
10425 macro_build (&imm_expr, "daddiu", "y,j", yreg);
10426 break;
10427
10428 case M_BEQ:
10429 s = "cmp";
10430 s2 = "bteqz";
10431 goto do_branch;
10432 case M_BNE:
10433 s = "cmp";
10434 s2 = "btnez";
10435 goto do_branch;
10436 case M_BLT:
10437 s = "slt";
10438 s2 = "btnez";
10439 goto do_branch;
10440 case M_BLTU:
10441 s = "sltu";
10442 s2 = "btnez";
10443 goto do_branch;
10444 case M_BLE:
10445 s = "slt";
10446 s2 = "bteqz";
10447 goto do_reverse_branch;
10448 case M_BLEU:
10449 s = "sltu";
10450 s2 = "bteqz";
10451 goto do_reverse_branch;
10452 case M_BGE:
10453 s = "slt";
10454 s2 = "bteqz";
10455 goto do_branch;
10456 case M_BGEU:
10457 s = "sltu";
10458 s2 = "bteqz";
10459 goto do_branch;
10460 case M_BGT:
10461 s = "slt";
10462 s2 = "btnez";
10463 goto do_reverse_branch;
10464 case M_BGTU:
10465 s = "sltu";
10466 s2 = "btnez";
10467
10468 do_reverse_branch:
10469 tmp = xreg;
10470 xreg = yreg;
10471 yreg = tmp;
10472
10473 do_branch:
10474 macro_build (NULL, s, "x,y", xreg, yreg);
10475 macro_build (&offset_expr, s2, "p");
10476 break;
10477
10478 case M_BEQ_I:
10479 s = "cmpi";
10480 s2 = "bteqz";
10481 s3 = "x,U";
10482 goto do_branch_i;
10483 case M_BNE_I:
10484 s = "cmpi";
10485 s2 = "btnez";
10486 s3 = "x,U";
10487 goto do_branch_i;
10488 case M_BLT_I:
10489 s = "slti";
10490 s2 = "btnez";
10491 s3 = "x,8";
10492 goto do_branch_i;
10493 case M_BLTU_I:
10494 s = "sltiu";
10495 s2 = "btnez";
10496 s3 = "x,8";
10497 goto do_branch_i;
10498 case M_BLE_I:
10499 s = "slti";
10500 s2 = "btnez";
10501 s3 = "x,8";
10502 goto do_addone_branch_i;
10503 case M_BLEU_I:
10504 s = "sltiu";
10505 s2 = "btnez";
10506 s3 = "x,8";
10507 goto do_addone_branch_i;
10508 case M_BGE_I:
10509 s = "slti";
10510 s2 = "bteqz";
10511 s3 = "x,8";
10512 goto do_branch_i;
10513 case M_BGEU_I:
10514 s = "sltiu";
10515 s2 = "bteqz";
10516 s3 = "x,8";
10517 goto do_branch_i;
10518 case M_BGT_I:
10519 s = "slti";
10520 s2 = "bteqz";
10521 s3 = "x,8";
10522 goto do_addone_branch_i;
10523 case M_BGTU_I:
10524 s = "sltiu";
10525 s2 = "bteqz";
10526 s3 = "x,8";
10527
10528 do_addone_branch_i:
10529 if (imm_expr.X_op != O_constant)
10530 as_bad (_("Unsupported large constant"));
10531 ++imm_expr.X_add_number;
10532
10533 do_branch_i:
10534 macro_build (&imm_expr, s, s3, xreg);
10535 macro_build (&offset_expr, s2, "p");
10536 break;
10537
10538 case M_ABS:
10539 expr1.X_add_number = 0;
10540 macro_build (&expr1, "slti", "x,8", yreg);
10541 if (xreg != yreg)
10542 move_register (xreg, yreg);
10543 expr1.X_add_number = 2;
10544 macro_build (&expr1, "bteqz", "p");
10545 macro_build (NULL, "neg", "x,w", xreg, xreg);
10546 }
10547 }
10548
10549 /* For consistency checking, verify that all bits are specified either
10550 by the match/mask part of the instruction definition, or by the
10551 operand list. */
10552 static int
10553 validate_mips_insn (const struct mips_opcode *opc)
10554 {
10555 const char *p = opc->args;
10556 char c;
10557 unsigned long used_bits = opc->mask;
10558
10559 if ((used_bits & opc->match) != opc->match)
10560 {
10561 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10562 opc->name, opc->args);
10563 return 0;
10564 }
10565 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
10566 while (*p)
10567 switch (c = *p++)
10568 {
10569 case ',': break;
10570 case '(': break;
10571 case ')': break;
10572 case '+':
10573 switch (c = *p++)
10574 {
10575 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
10576 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
10577 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
10578 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
10579 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10580 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10581 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10582 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
10583 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
10584 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10585 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10586 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10587 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10588 case 'I': break;
10589 case 'J': USE_BITS (OP_MASK_CODE10, OP_SH_CODE10); break;
10590 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10591 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
10592 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
10593 case 'x': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10594 case 'X': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10595 case 'p': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10596 case 'P': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10597 case 'Q': USE_BITS (OP_MASK_SEQI, OP_SH_SEQI); break;
10598 case 's': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10599 case 'S': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10600 case 'z': USE_BITS (OP_MASK_RZ, OP_SH_RZ); break;
10601 case 'Z': USE_BITS (OP_MASK_FZ, OP_SH_FZ); break;
10602 case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10603 case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10604 case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
10605 case 'j': USE_BITS (OP_MASK_EVAOFFSET, OP_SH_EVAOFFSET); break;
10606
10607 default:
10608 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10609 c, opc->name, opc->args);
10610 return 0;
10611 }
10612 break;
10613 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10614 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10615 case 'A': break;
10616 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
10617 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
10618 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10619 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10620 case 'F': break;
10621 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10622 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
10623 case 'I': break;
10624 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
10625 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10626 case 'L': break;
10627 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
10628 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
10629 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
10630 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
10631 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10632 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
10633 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10634 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10635 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10636 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10637 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10638 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10639 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10640 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
10641 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10642 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
10643 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10644 case 'f': break;
10645 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
10646 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10647 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10648 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
10649 case 'l': break;
10650 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10651 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10652 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
10653 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10654 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10655 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10656 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10657 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10658 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10659 case 'x': break;
10660 case 'z': break;
10661 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
10662 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
10663 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10664 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
10665 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
10666 case '[': break;
10667 case ']': break;
10668 case '1': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10669 case '2': USE_BITS (OP_MASK_BP, OP_SH_BP); break;
10670 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
10671 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
10672 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
10673 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10674 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
10675 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
10676 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
10677 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
10678 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
10679 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
10680 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
10681 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
10682 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
10683 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
10684 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
10685 case '\\': USE_BITS (OP_MASK_3BITPOS, OP_SH_3BITPOS); break;
10686 case '~': USE_BITS (OP_MASK_OFFSET12, OP_SH_OFFSET12); break;
10687 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10688 default:
10689 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10690 c, opc->name, opc->args);
10691 return 0;
10692 }
10693 #undef USE_BITS
10694 if (used_bits != 0xffffffff)
10695 {
10696 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10697 ~used_bits & 0xffffffff, opc->name, opc->args);
10698 return 0;
10699 }
10700 return 1;
10701 }
10702
10703 /* For consistency checking, verify that the length implied matches the
10704 major opcode and that all bits are specified either by the match/mask
10705 part of the instruction definition, or by the operand list. */
10706
10707 static int
10708 validate_micromips_insn (const struct mips_opcode *opc)
10709 {
10710 unsigned long match = opc->match;
10711 unsigned long mask = opc->mask;
10712 const char *p = opc->args;
10713 unsigned long insn_bits;
10714 unsigned long used_bits;
10715 unsigned long major;
10716 unsigned int length;
10717 char e;
10718 char c;
10719
10720 if ((mask & match) != match)
10721 {
10722 as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10723 opc->name, opc->args);
10724 return 0;
10725 }
10726 length = micromips_insn_length (opc);
10727 if (length != 2 && length != 4)
10728 {
10729 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10730 "%s %s"), length, opc->name, opc->args);
10731 return 0;
10732 }
10733 major = match >> (10 + 8 * (length - 2));
10734 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10735 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10736 {
10737 as_bad (_("Internal error: bad microMIPS opcode "
10738 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
10739 return 0;
10740 }
10741
10742 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
10743 insn_bits = 1 << 4 * length;
10744 insn_bits <<= 4 * length;
10745 insn_bits -= 1;
10746 used_bits = mask;
10747 #define USE_BITS(field) \
10748 (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10749 while (*p)
10750 switch (c = *p++)
10751 {
10752 case ',': break;
10753 case '(': break;
10754 case ')': break;
10755 case '+':
10756 e = c;
10757 switch (c = *p++)
10758 {
10759 case 'A': USE_BITS (EXTLSB); break;
10760 case 'B': USE_BITS (INSMSB); break;
10761 case 'C': USE_BITS (EXTMSBD); break;
10762 case 'D': USE_BITS (RS); USE_BITS (SEL); break;
10763 case 'E': USE_BITS (EXTLSB); break;
10764 case 'F': USE_BITS (INSMSB); break;
10765 case 'G': USE_BITS (EXTMSBD); break;
10766 case 'H': USE_BITS (EXTMSBD); break;
10767 case 'j': USE_BITS (EVAOFFSET); break;
10768 default:
10769 as_bad (_("Internal error: bad mips opcode "
10770 "(unknown extension operand type `%c%c'): %s %s"),
10771 e, c, opc->name, opc->args);
10772 return 0;
10773 }
10774 break;
10775 case 'm':
10776 e = c;
10777 switch (c = *p++)
10778 {
10779 case 'A': USE_BITS (IMMA); break;
10780 case 'B': USE_BITS (IMMB); break;
10781 case 'C': USE_BITS (IMMC); break;
10782 case 'D': USE_BITS (IMMD); break;
10783 case 'E': USE_BITS (IMME); break;
10784 case 'F': USE_BITS (IMMF); break;
10785 case 'G': USE_BITS (IMMG); break;
10786 case 'H': USE_BITS (IMMH); break;
10787 case 'I': USE_BITS (IMMI); break;
10788 case 'J': USE_BITS (IMMJ); break;
10789 case 'L': USE_BITS (IMML); break;
10790 case 'M': USE_BITS (IMMM); break;
10791 case 'N': USE_BITS (IMMN); break;
10792 case 'O': USE_BITS (IMMO); break;
10793 case 'P': USE_BITS (IMMP); break;
10794 case 'Q': USE_BITS (IMMQ); break;
10795 case 'U': USE_BITS (IMMU); break;
10796 case 'W': USE_BITS (IMMW); break;
10797 case 'X': USE_BITS (IMMX); break;
10798 case 'Y': USE_BITS (IMMY); break;
10799 case 'Z': break;
10800 case 'a': break;
10801 case 'b': USE_BITS (MB); break;
10802 case 'c': USE_BITS (MC); break;
10803 case 'd': USE_BITS (MD); break;
10804 case 'e': USE_BITS (ME); break;
10805 case 'f': USE_BITS (MF); break;
10806 case 'g': USE_BITS (MG); break;
10807 case 'h': USE_BITS (MH); break;
10808 case 'i': USE_BITS (MI); break;
10809 case 'j': USE_BITS (MJ); break;
10810 case 'l': USE_BITS (ML); break;
10811 case 'm': USE_BITS (MM); break;
10812 case 'n': USE_BITS (MN); break;
10813 case 'p': USE_BITS (MP); break;
10814 case 'q': USE_BITS (MQ); break;
10815 case 'r': break;
10816 case 's': break;
10817 case 't': break;
10818 case 'x': break;
10819 case 'y': break;
10820 case 'z': break;
10821 default:
10822 as_bad (_("Internal error: bad mips opcode "
10823 "(unknown extension operand type `%c%c'): %s %s"),
10824 e, c, opc->name, opc->args);
10825 return 0;
10826 }
10827 break;
10828 case '.': USE_BITS (OFFSET10); break;
10829 case '1': USE_BITS (STYPE); break;
10830 case '2': USE_BITS (BP); break;
10831 case '3': USE_BITS (SA3); break;
10832 case '4': USE_BITS (SA4); break;
10833 case '5': USE_BITS (IMM8); break;
10834 case '6': USE_BITS (RS); break;
10835 case '7': USE_BITS (DSPACC); break;
10836 case '8': USE_BITS (WRDSP); break;
10837 case '0': USE_BITS (DSPSFT); break;
10838 case '<': USE_BITS (SHAMT); break;
10839 case '>': USE_BITS (SHAMT); break;
10840 case '@': USE_BITS (IMM10); break;
10841 case 'B': USE_BITS (CODE10); break;
10842 case 'C': USE_BITS (COPZ); break;
10843 case 'D': USE_BITS (FD); break;
10844 case 'E': USE_BITS (RT); break;
10845 case 'G': USE_BITS (RS); break;
10846 case 'H': USE_BITS (SEL); break;
10847 case 'K': USE_BITS (RS); break;
10848 case 'M': USE_BITS (CCC); break;
10849 case 'N': USE_BITS (BCC); break;
10850 case 'R': USE_BITS (FR); break;
10851 case 'S': USE_BITS (FS); break;
10852 case 'T': USE_BITS (FT); break;
10853 case 'V': USE_BITS (FS); break;
10854 case '\\': USE_BITS (3BITPOS); break;
10855 case '^': USE_BITS (RD); break;
10856 case 'a': USE_BITS (TARGET); break;
10857 case 'b': USE_BITS (RS); break;
10858 case 'c': USE_BITS (CODE); break;
10859 case 'd': USE_BITS (RD); break;
10860 case 'h': USE_BITS (PREFX); break;
10861 case 'i': USE_BITS (IMMEDIATE); break;
10862 case 'j': USE_BITS (DELTA); break;
10863 case 'k': USE_BITS (CACHE); break;
10864 case 'n': USE_BITS (RT); break;
10865 case 'o': USE_BITS (DELTA); break;
10866 case 'p': USE_BITS (DELTA); break;
10867 case 'q': USE_BITS (CODE2); break;
10868 case 'r': USE_BITS (RS); break;
10869 case 's': USE_BITS (RS); break;
10870 case 't': USE_BITS (RT); break;
10871 case 'u': USE_BITS (IMMEDIATE); break;
10872 case 'v': USE_BITS (RS); break;
10873 case 'w': USE_BITS (RT); break;
10874 case 'y': USE_BITS (RS3); break;
10875 case 'z': break;
10876 case '|': USE_BITS (TRAP); break;
10877 case '~': USE_BITS (OFFSET12); break;
10878 default:
10879 as_bad (_("Internal error: bad microMIPS opcode "
10880 "(unknown operand type `%c'): %s %s"),
10881 c, opc->name, opc->args);
10882 return 0;
10883 }
10884 #undef USE_BITS
10885 if (used_bits != insn_bits)
10886 {
10887 if (~used_bits & insn_bits)
10888 as_bad (_("Internal error: bad microMIPS opcode "
10889 "(bits 0x%lx undefined): %s %s"),
10890 ~used_bits & insn_bits, opc->name, opc->args);
10891 if (used_bits & ~insn_bits)
10892 as_bad (_("Internal error: bad microMIPS opcode "
10893 "(bits 0x%lx defined): %s %s"),
10894 used_bits & ~insn_bits, opc->name, opc->args);
10895 return 0;
10896 }
10897 return 1;
10898 }
10899
10900 /* UDI immediates. */
10901 struct mips_immed {
10902 char type;
10903 unsigned int shift;
10904 unsigned long mask;
10905 const char * desc;
10906 };
10907
10908 static const struct mips_immed mips_immed[] = {
10909 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
10910 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
10911 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
10912 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
10913 { 0,0,0,0 }
10914 };
10915
10916 /* Check whether an odd floating-point register is allowed. */
10917 static int
10918 mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
10919 {
10920 const char *s = insn->name;
10921
10922 if (insn->pinfo == INSN_MACRO)
10923 /* Let a macro pass, we'll catch it later when it is expanded. */
10924 return 1;
10925
10926 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || (mips_opts.arch == CPU_R5900))
10927 {
10928 /* Allow odd registers for single-precision ops. */
10929 switch (insn->pinfo & (FP_S | FP_D))
10930 {
10931 case FP_S:
10932 case 0:
10933 return 1; /* both single precision - ok */
10934 case FP_D:
10935 return 0; /* both double precision - fail */
10936 default:
10937 break;
10938 }
10939
10940 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
10941 s = strchr (insn->name, '.');
10942 if (argnum == 2)
10943 s = s != NULL ? strchr (s + 1, '.') : NULL;
10944 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
10945 }
10946
10947 /* Single-precision coprocessor loads and moves are OK too. */
10948 if ((insn->pinfo & FP_S)
10949 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
10950 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
10951 return 1;
10952
10953 return 0;
10954 }
10955
10956 /* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
10957 taking bits from BIT up. */
10958 static int
10959 expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
10960 {
10961 return (ep->X_op == O_constant
10962 && (ep->X_add_number & ((1 << bit) - 1)) == 0
10963 && ep->X_add_number >= min << bit
10964 && ep->X_add_number < max << bit);
10965 }
10966
10967 /* This routine assembles an instruction into its binary format. As a
10968 side effect, it sets one of the global variables imm_reloc or
10969 offset_reloc to the type of relocation to do if one of the operands
10970 is an address expression. */
10971
10972 static void
10973 mips_ip (char *str, struct mips_cl_insn *ip)
10974 {
10975 bfd_boolean wrong_delay_slot_insns = FALSE;
10976 bfd_boolean need_delay_slot_ok = TRUE;
10977 struct mips_opcode *firstinsn = NULL;
10978 const struct mips_opcode *past;
10979 struct hash_control *hash;
10980 char *s;
10981 const char *args;
10982 char c = 0;
10983 struct mips_opcode *insn;
10984 char *argsStart;
10985 unsigned int regno;
10986 unsigned int lastregno;
10987 unsigned int destregno = 0;
10988 unsigned int lastpos = 0;
10989 unsigned int limlo, limhi;
10990 int sizelo;
10991 char *s_reset;
10992 offsetT min_range, max_range;
10993 long opend;
10994 char *name;
10995 int argnum;
10996 unsigned int rtype;
10997 char *dot;
10998 long end;
10999
11000 insn_error = NULL;
11001
11002 if (mips_opts.micromips)
11003 {
11004 hash = micromips_op_hash;
11005 past = &micromips_opcodes[bfd_micromips_num_opcodes];
11006 }
11007 else
11008 {
11009 hash = op_hash;
11010 past = &mips_opcodes[NUMOPCODES];
11011 }
11012 forced_insn_length = 0;
11013 insn = NULL;
11014
11015 /* We first try to match an instruction up to a space or to the end. */
11016 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
11017 continue;
11018
11019 /* Make a copy of the instruction so that we can fiddle with it. */
11020 name = alloca (end + 1);
11021 memcpy (name, str, end);
11022 name[end] = '\0';
11023
11024 for (;;)
11025 {
11026 insn = (struct mips_opcode *) hash_find (hash, name);
11027
11028 if (insn != NULL || !mips_opts.micromips)
11029 break;
11030 if (forced_insn_length)
11031 break;
11032
11033 /* See if there's an instruction size override suffix,
11034 either `16' or `32', at the end of the mnemonic proper,
11035 that defines the operation, i.e. before the first `.'
11036 character if any. Strip it and retry. */
11037 dot = strchr (name, '.');
11038 opend = dot != NULL ? dot - name : end;
11039 if (opend < 3)
11040 break;
11041 if (name[opend - 2] == '1' && name[opend - 1] == '6')
11042 forced_insn_length = 2;
11043 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
11044 forced_insn_length = 4;
11045 else
11046 break;
11047 memcpy (name + opend - 2, name + opend, end - opend + 1);
11048 }
11049 if (insn == NULL)
11050 {
11051 insn_error = _("Unrecognized opcode");
11052 return;
11053 }
11054
11055 /* For microMIPS instructions placed in a fixed-length branch delay slot
11056 we make up to two passes over the relevant fragment of the opcode
11057 table. First we try instructions that meet the delay slot's length
11058 requirement. If none matched, then we retry with the remaining ones
11059 and if one matches, then we use it and then issue an appropriate
11060 warning later on. */
11061 argsStart = s = str + end;
11062 for (;;)
11063 {
11064 bfd_boolean delay_slot_ok;
11065 bfd_boolean size_ok;
11066 bfd_boolean ok;
11067
11068 gas_assert (strcmp (insn->name, name) == 0);
11069
11070 ok = is_opcode_valid (insn);
11071 size_ok = is_size_valid (insn);
11072 delay_slot_ok = is_delay_slot_valid (insn);
11073 if (!delay_slot_ok && !wrong_delay_slot_insns)
11074 {
11075 firstinsn = insn;
11076 wrong_delay_slot_insns = TRUE;
11077 }
11078 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
11079 {
11080 static char buf[256];
11081
11082 if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
11083 {
11084 ++insn;
11085 continue;
11086 }
11087 if (wrong_delay_slot_insns && need_delay_slot_ok)
11088 {
11089 gas_assert (firstinsn);
11090 need_delay_slot_ok = FALSE;
11091 past = insn + 1;
11092 insn = firstinsn;
11093 continue;
11094 }
11095
11096 if (insn_error)
11097 return;
11098
11099 if (!ok)
11100 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
11101 mips_cpu_info_from_arch (mips_opts.arch)->name,
11102 mips_cpu_info_from_isa (mips_opts.isa)->name);
11103 else
11104 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
11105 8 * forced_insn_length);
11106 insn_error = buf;
11107
11108 return;
11109 }
11110
11111 create_insn (ip, insn);
11112 insn_error = NULL;
11113 argnum = 1;
11114 lastregno = 0xffffffff;
11115 for (args = insn->args;; ++args)
11116 {
11117 int is_mdmx;
11118
11119 s += strspn (s, " \t");
11120 is_mdmx = 0;
11121 switch (*args)
11122 {
11123 case '\0': /* end of args */
11124 if (*s == '\0')
11125 return;
11126 break;
11127
11128 case '2':
11129 /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
11130 code) or 14 (for microMIPS code). */
11131 my_getExpression (&imm_expr, s);
11132 check_absolute_expr (ip, &imm_expr);
11133 if ((unsigned long) imm_expr.X_add_number != 1
11134 && (unsigned long) imm_expr.X_add_number != 3)
11135 {
11136 as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
11137 (unsigned long) imm_expr.X_add_number);
11138 }
11139 INSERT_OPERAND (mips_opts.micromips,
11140 BP, *ip, imm_expr.X_add_number);
11141 imm_expr.X_op = O_absent;
11142 s = expr_end;
11143 continue;
11144
11145 case '3':
11146 /* DSP 3-bit unsigned immediate in bit 13 (for standard MIPS
11147 code) or 21 (for microMIPS code). */
11148 {
11149 unsigned long mask = (mips_opts.micromips
11150 ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
11151
11152 my_getExpression (&imm_expr, s);
11153 check_absolute_expr (ip, &imm_expr);
11154 if ((unsigned long) imm_expr.X_add_number > mask)
11155 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11156 mask, (unsigned long) imm_expr.X_add_number);
11157 INSERT_OPERAND (mips_opts.micromips,
11158 SA3, *ip, imm_expr.X_add_number);
11159 imm_expr.X_op = O_absent;
11160 s = expr_end;
11161 }
11162 continue;
11163
11164 case '4':
11165 /* DSP 4-bit unsigned immediate in bit 12 (for standard MIPS
11166 code) or 21 (for microMIPS code). */
11167 {
11168 unsigned long mask = (mips_opts.micromips
11169 ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
11170
11171 my_getExpression (&imm_expr, s);
11172 check_absolute_expr (ip, &imm_expr);
11173 if ((unsigned long) imm_expr.X_add_number > mask)
11174 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11175 mask, (unsigned long) imm_expr.X_add_number);
11176 INSERT_OPERAND (mips_opts.micromips,
11177 SA4, *ip, imm_expr.X_add_number);
11178 imm_expr.X_op = O_absent;
11179 s = expr_end;
11180 }
11181 continue;
11182
11183 case '5':
11184 /* DSP 8-bit unsigned immediate in bit 13 (for standard MIPS
11185 code) or 16 (for microMIPS code). */
11186 {
11187 unsigned long mask = (mips_opts.micromips
11188 ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
11189
11190 my_getExpression (&imm_expr, s);
11191 check_absolute_expr (ip, &imm_expr);
11192 if ((unsigned long) imm_expr.X_add_number > mask)
11193 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11194 mask, (unsigned long) imm_expr.X_add_number);
11195 INSERT_OPERAND (mips_opts.micromips,
11196 IMM8, *ip, imm_expr.X_add_number);
11197 imm_expr.X_op = O_absent;
11198 s = expr_end;
11199 }
11200 continue;
11201
11202 case '6':
11203 /* DSP 5-bit unsigned immediate in bit 16 (for standard MIPS
11204 code) or 21 (for microMIPS code). */
11205 {
11206 unsigned long mask = (mips_opts.micromips
11207 ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
11208
11209 my_getExpression (&imm_expr, s);
11210 check_absolute_expr (ip, &imm_expr);
11211 if ((unsigned long) imm_expr.X_add_number > mask)
11212 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11213 mask, (unsigned long) imm_expr.X_add_number);
11214 INSERT_OPERAND (mips_opts.micromips,
11215 RS, *ip, imm_expr.X_add_number);
11216 imm_expr.X_op = O_absent;
11217 s = expr_end;
11218 }
11219 continue;
11220
11221 case '7': /* Four DSP accumulators in bits 11,12. */
11222 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11223 && s[3] >= '0' && s[3] <= '3')
11224 {
11225 regno = s[3] - '0';
11226 s += 4;
11227 INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
11228 continue;
11229 }
11230 else
11231 as_bad (_("Invalid dsp acc register"));
11232 break;
11233
11234 case '8':
11235 /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
11236 code) or 14 (for microMIPS code). */
11237 {
11238 unsigned long mask = (mips_opts.micromips
11239 ? MICROMIPSOP_MASK_WRDSP
11240 : OP_MASK_WRDSP);
11241
11242 my_getExpression (&imm_expr, s);
11243 check_absolute_expr (ip, &imm_expr);
11244 if ((unsigned long) imm_expr.X_add_number > mask)
11245 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11246 mask, (unsigned long) imm_expr.X_add_number);
11247 INSERT_OPERAND (mips_opts.micromips,
11248 WRDSP, *ip, imm_expr.X_add_number);
11249 imm_expr.X_op = O_absent;
11250 s = expr_end;
11251 }
11252 continue;
11253
11254 case '9': /* Four DSP accumulators in bits 21,22. */
11255 gas_assert (!mips_opts.micromips);
11256 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11257 && s[3] >= '0' && s[3] <= '3')
11258 {
11259 regno = s[3] - '0';
11260 s += 4;
11261 INSERT_OPERAND (0, DSPACC_S, *ip, regno);
11262 continue;
11263 }
11264 else
11265 as_bad (_("Invalid dsp acc register"));
11266 break;
11267
11268 case '0':
11269 /* DSP 6-bit signed immediate in bit 16 (for standard MIPS
11270 code) or 20 (for microMIPS code). */
11271 {
11272 long mask = (mips_opts.micromips
11273 ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
11274
11275 my_getExpression (&imm_expr, s);
11276 check_absolute_expr (ip, &imm_expr);
11277 min_range = -((mask + 1) >> 1);
11278 max_range = ((mask + 1) >> 1) - 1;
11279 if (imm_expr.X_add_number < min_range
11280 || imm_expr.X_add_number > max_range)
11281 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11282 (long) min_range, (long) max_range,
11283 (long) imm_expr.X_add_number);
11284 INSERT_OPERAND (mips_opts.micromips,
11285 DSPSFT, *ip, imm_expr.X_add_number);
11286 imm_expr.X_op = O_absent;
11287 s = expr_end;
11288 }
11289 continue;
11290
11291 case '\'': /* DSP 6-bit unsigned immediate in bit 16. */
11292 gas_assert (!mips_opts.micromips);
11293 my_getExpression (&imm_expr, s);
11294 check_absolute_expr (ip, &imm_expr);
11295 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11296 {
11297 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11298 OP_MASK_RDDSP,
11299 (unsigned long) imm_expr.X_add_number);
11300 }
11301 INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
11302 imm_expr.X_op = O_absent;
11303 s = expr_end;
11304 continue;
11305
11306 case ':': /* DSP 7-bit signed immediate in bit 19. */
11307 gas_assert (!mips_opts.micromips);
11308 my_getExpression (&imm_expr, s);
11309 check_absolute_expr (ip, &imm_expr);
11310 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11311 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11312 if (imm_expr.X_add_number < min_range ||
11313 imm_expr.X_add_number > max_range)
11314 {
11315 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11316 (long) min_range, (long) max_range,
11317 (long) imm_expr.X_add_number);
11318 }
11319 INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
11320 imm_expr.X_op = O_absent;
11321 s = expr_end;
11322 continue;
11323
11324 case '@': /* DSP 10-bit signed immediate in bit 16. */
11325 {
11326 long mask = (mips_opts.micromips
11327 ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11328
11329 my_getExpression (&imm_expr, s);
11330 check_absolute_expr (ip, &imm_expr);
11331 min_range = -((mask + 1) >> 1);
11332 max_range = ((mask + 1) >> 1) - 1;
11333 if (imm_expr.X_add_number < min_range
11334 || imm_expr.X_add_number > max_range)
11335 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11336 (long) min_range, (long) max_range,
11337 (long) imm_expr.X_add_number);
11338 INSERT_OPERAND (mips_opts.micromips,
11339 IMM10, *ip, imm_expr.X_add_number);
11340 imm_expr.X_op = O_absent;
11341 s = expr_end;
11342 }
11343 continue;
11344
11345 case '^': /* DSP 5-bit unsigned immediate in bit 11. */
11346 gas_assert (mips_opts.micromips);
11347 my_getExpression (&imm_expr, s);
11348 check_absolute_expr (ip, &imm_expr);
11349 if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11350 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11351 MICROMIPSOP_MASK_RD,
11352 (unsigned long) imm_expr.X_add_number);
11353 INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
11354 imm_expr.X_op = O_absent;
11355 s = expr_end;
11356 continue;
11357
11358 case '!': /* MT usermode flag bit. */
11359 gas_assert (!mips_opts.micromips);
11360 my_getExpression (&imm_expr, s);
11361 check_absolute_expr (ip, &imm_expr);
11362 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
11363 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11364 (unsigned long) imm_expr.X_add_number);
11365 INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
11366 imm_expr.X_op = O_absent;
11367 s = expr_end;
11368 continue;
11369
11370 case '$': /* MT load high flag bit. */
11371 gas_assert (!mips_opts.micromips);
11372 my_getExpression (&imm_expr, s);
11373 check_absolute_expr (ip, &imm_expr);
11374 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
11375 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11376 (unsigned long) imm_expr.X_add_number);
11377 INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
11378 imm_expr.X_op = O_absent;
11379 s = expr_end;
11380 continue;
11381
11382 case '*': /* Four DSP accumulators in bits 18,19. */
11383 gas_assert (!mips_opts.micromips);
11384 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11385 s[3] >= '0' && s[3] <= '3')
11386 {
11387 regno = s[3] - '0';
11388 s += 4;
11389 INSERT_OPERAND (0, MTACC_T, *ip, regno);
11390 continue;
11391 }
11392 else
11393 as_bad (_("Invalid dsp/smartmips acc register"));
11394 break;
11395
11396 case '&': /* Four DSP accumulators in bits 13,14. */
11397 gas_assert (!mips_opts.micromips);
11398 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11399 s[3] >= '0' && s[3] <= '3')
11400 {
11401 regno = s[3] - '0';
11402 s += 4;
11403 INSERT_OPERAND (0, MTACC_D, *ip, regno);
11404 continue;
11405 }
11406 else
11407 as_bad (_("Invalid dsp/smartmips acc register"));
11408 break;
11409
11410 case '\\': /* 3-bit bit position. */
11411 {
11412 unsigned long mask = (mips_opts.micromips
11413 ? MICROMIPSOP_MASK_3BITPOS
11414 : OP_MASK_3BITPOS);
11415
11416 my_getExpression (&imm_expr, s);
11417 check_absolute_expr (ip, &imm_expr);
11418 if ((unsigned long) imm_expr.X_add_number > mask)
11419 as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11420 ip->insn_mo->name,
11421 mask, (unsigned long) imm_expr.X_add_number);
11422 INSERT_OPERAND (mips_opts.micromips,
11423 3BITPOS, *ip, imm_expr.X_add_number);
11424 imm_expr.X_op = O_absent;
11425 s = expr_end;
11426 }
11427 continue;
11428
11429 case ',':
11430 ++argnum;
11431 if (*s++ == *args)
11432 continue;
11433 s--;
11434 switch (*++args)
11435 {
11436 case 'r':
11437 case 'v':
11438 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
11439 continue;
11440
11441 case 'w':
11442 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
11443 continue;
11444
11445 case 'W':
11446 gas_assert (!mips_opts.micromips);
11447 INSERT_OPERAND (0, FT, *ip, lastregno);
11448 continue;
11449
11450 case 'V':
11451 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
11452 continue;
11453 }
11454 break;
11455
11456 case '(':
11457 /* Handle optional base register.
11458 Either the base register is omitted or
11459 we must have a left paren. */
11460 /* This is dependent on the next operand specifier
11461 is a base register specification. */
11462 gas_assert (args[1] == 'b'
11463 || (mips_opts.micromips
11464 && args[1] == 'm'
11465 && (args[2] == 'l' || args[2] == 'n'
11466 || args[2] == 's' || args[2] == 'a')));
11467 if (*s == '\0' && args[1] == 'b')
11468 return;
11469 /* Fall through. */
11470
11471 case ')': /* These must match exactly. */
11472 if (*s++ == *args)
11473 continue;
11474 break;
11475
11476 case '[': /* These must match exactly. */
11477 case ']':
11478 gas_assert (!mips_opts.micromips);
11479 if (*s++ == *args)
11480 continue;
11481 break;
11482
11483 case '+': /* Opcode extension character. */
11484 switch (*++args)
11485 {
11486 case '1': /* UDI immediates. */
11487 case '2':
11488 case '3':
11489 case '4':
11490 gas_assert (!mips_opts.micromips);
11491 {
11492 const struct mips_immed *imm = mips_immed;
11493
11494 while (imm->type && imm->type != *args)
11495 ++imm;
11496 if (! imm->type)
11497 abort ();
11498 my_getExpression (&imm_expr, s);
11499 check_absolute_expr (ip, &imm_expr);
11500 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11501 {
11502 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11503 imm->desc ? imm->desc : ip->insn_mo->name,
11504 (unsigned long) imm_expr.X_add_number,
11505 (unsigned long) imm_expr.X_add_number);
11506 imm_expr.X_add_number &= imm->mask;
11507 }
11508 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11509 << imm->shift);
11510 imm_expr.X_op = O_absent;
11511 s = expr_end;
11512 }
11513 continue;
11514
11515 case 'J': /* 10-bit hypcall code. */
11516 gas_assert (!mips_opts.micromips);
11517 {
11518 unsigned long mask = OP_MASK_CODE10;
11519
11520 my_getExpression (&imm_expr, s);
11521 check_absolute_expr (ip, &imm_expr);
11522 if ((unsigned long) imm_expr.X_add_number > mask)
11523 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11524 ip->insn_mo->name,
11525 mask, (unsigned long) imm_expr.X_add_number);
11526 INSERT_OPERAND (0, CODE10, *ip, imm_expr.X_add_number);
11527 imm_expr.X_op = O_absent;
11528 s = expr_end;
11529 }
11530 continue;
11531
11532 case 'A': /* ins/ext position, becomes LSB. */
11533 limlo = 0;
11534 limhi = 31;
11535 goto do_lsb;
11536 case 'E':
11537 limlo = 32;
11538 limhi = 63;
11539 goto do_lsb;
11540 do_lsb:
11541 my_getExpression (&imm_expr, s);
11542 check_absolute_expr (ip, &imm_expr);
11543 if ((unsigned long) imm_expr.X_add_number < limlo
11544 || (unsigned long) imm_expr.X_add_number > limhi)
11545 {
11546 as_bad (_("Improper position (%lu)"),
11547 (unsigned long) imm_expr.X_add_number);
11548 imm_expr.X_add_number = limlo;
11549 }
11550 lastpos = imm_expr.X_add_number;
11551 INSERT_OPERAND (mips_opts.micromips,
11552 EXTLSB, *ip, imm_expr.X_add_number);
11553 imm_expr.X_op = O_absent;
11554 s = expr_end;
11555 continue;
11556
11557 case 'B': /* ins size, becomes MSB. */
11558 limlo = 1;
11559 limhi = 32;
11560 goto do_msb;
11561 case 'F':
11562 limlo = 33;
11563 limhi = 64;
11564 goto do_msb;
11565 do_msb:
11566 my_getExpression (&imm_expr, s);
11567 check_absolute_expr (ip, &imm_expr);
11568 /* Check for negative input so that small negative numbers
11569 will not succeed incorrectly. The checks against
11570 (pos+size) transitively check "size" itself,
11571 assuming that "pos" is reasonable. */
11572 if ((long) imm_expr.X_add_number < 0
11573 || ((unsigned long) imm_expr.X_add_number
11574 + lastpos) < limlo
11575 || ((unsigned long) imm_expr.X_add_number
11576 + lastpos) > limhi)
11577 {
11578 as_bad (_("Improper insert size (%lu, position %lu)"),
11579 (unsigned long) imm_expr.X_add_number,
11580 (unsigned long) lastpos);
11581 imm_expr.X_add_number = limlo - lastpos;
11582 }
11583 INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11584 lastpos + imm_expr.X_add_number - 1);
11585 imm_expr.X_op = O_absent;
11586 s = expr_end;
11587 continue;
11588
11589 case 'C': /* ext size, becomes MSBD. */
11590 limlo = 1;
11591 limhi = 32;
11592 sizelo = 1;
11593 goto do_msbd;
11594 case 'G':
11595 limlo = 33;
11596 limhi = 64;
11597 sizelo = 33;
11598 goto do_msbd;
11599 case 'H':
11600 limlo = 33;
11601 limhi = 64;
11602 sizelo = 1;
11603 goto do_msbd;
11604 do_msbd:
11605 my_getExpression (&imm_expr, s);
11606 check_absolute_expr (ip, &imm_expr);
11607 /* The checks against (pos+size) don't transitively check
11608 "size" itself, assuming that "pos" is reasonable.
11609 We also need to check the lower bound of "size". */
11610 if ((long) imm_expr.X_add_number < sizelo
11611 || ((unsigned long) imm_expr.X_add_number
11612 + lastpos) < limlo
11613 || ((unsigned long) imm_expr.X_add_number
11614 + lastpos) > limhi)
11615 {
11616 as_bad (_("Improper extract size (%lu, position %lu)"),
11617 (unsigned long) imm_expr.X_add_number,
11618 (unsigned long) lastpos);
11619 imm_expr.X_add_number = limlo - lastpos;
11620 }
11621 INSERT_OPERAND (mips_opts.micromips,
11622 EXTMSBD, *ip, imm_expr.X_add_number - 1);
11623 imm_expr.X_op = O_absent;
11624 s = expr_end;
11625 continue;
11626
11627 case 'D':
11628 /* +D is for disassembly only; never match. */
11629 break;
11630
11631 case 'I':
11632 /* "+I" is like "I", except that imm2_expr is used. */
11633 my_getExpression (&imm2_expr, s);
11634 if (imm2_expr.X_op != O_big
11635 && imm2_expr.X_op != O_constant)
11636 insn_error = _("absolute expression required");
11637 if (HAVE_32BIT_GPRS)
11638 normalize_constant_expr (&imm2_expr);
11639 s = expr_end;
11640 continue;
11641
11642 case 'T': /* Coprocessor register. */
11643 gas_assert (!mips_opts.micromips);
11644 /* +T is for disassembly only; never match. */
11645 break;
11646
11647 case 't': /* Coprocessor register number. */
11648 gas_assert (!mips_opts.micromips);
11649 if (s[0] == '$' && ISDIGIT (s[1]))
11650 {
11651 ++s;
11652 regno = 0;
11653 do
11654 {
11655 regno *= 10;
11656 regno += *s - '0';
11657 ++s;
11658 }
11659 while (ISDIGIT (*s));
11660 if (regno > 31)
11661 as_bad (_("Invalid register number (%d)"), regno);
11662 else
11663 {
11664 INSERT_OPERAND (0, RT, *ip, regno);
11665 continue;
11666 }
11667 }
11668 else
11669 as_bad (_("Invalid coprocessor 0 register number"));
11670 break;
11671
11672 case 'x':
11673 /* bbit[01] and bbit[01]32 bit index. Give error if index
11674 is not in the valid range. */
11675 gas_assert (!mips_opts.micromips);
11676 my_getExpression (&imm_expr, s);
11677 check_absolute_expr (ip, &imm_expr);
11678 if ((unsigned) imm_expr.X_add_number > 31)
11679 {
11680 as_bad (_("Improper bit index (%lu)"),
11681 (unsigned long) imm_expr.X_add_number);
11682 imm_expr.X_add_number = 0;
11683 }
11684 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
11685 imm_expr.X_op = O_absent;
11686 s = expr_end;
11687 continue;
11688
11689 case 'X':
11690 /* bbit[01] bit index when bbit is used but we generate
11691 bbit[01]32 because the index is over 32. Move to the
11692 next candidate if index is not in the valid range. */
11693 gas_assert (!mips_opts.micromips);
11694 my_getExpression (&imm_expr, s);
11695 check_absolute_expr (ip, &imm_expr);
11696 if ((unsigned) imm_expr.X_add_number < 32
11697 || (unsigned) imm_expr.X_add_number > 63)
11698 break;
11699 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
11700 imm_expr.X_op = O_absent;
11701 s = expr_end;
11702 continue;
11703
11704 case 'p':
11705 /* cins, cins32, exts and exts32 position field. Give error
11706 if it's not in the valid range. */
11707 gas_assert (!mips_opts.micromips);
11708 my_getExpression (&imm_expr, s);
11709 check_absolute_expr (ip, &imm_expr);
11710 if ((unsigned) imm_expr.X_add_number > 31)
11711 {
11712 as_bad (_("Improper position (%lu)"),
11713 (unsigned long) imm_expr.X_add_number);
11714 imm_expr.X_add_number = 0;
11715 }
11716 /* Make the pos explicit to simplify +S. */
11717 lastpos = imm_expr.X_add_number + 32;
11718 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
11719 imm_expr.X_op = O_absent;
11720 s = expr_end;
11721 continue;
11722
11723 case 'P':
11724 /* cins, cins32, exts and exts32 position field. Move to
11725 the next candidate if it's not in the valid range. */
11726 gas_assert (!mips_opts.micromips);
11727 my_getExpression (&imm_expr, s);
11728 check_absolute_expr (ip, &imm_expr);
11729 if ((unsigned) imm_expr.X_add_number < 32
11730 || (unsigned) imm_expr.X_add_number > 63)
11731 break;
11732 lastpos = imm_expr.X_add_number;
11733 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
11734 imm_expr.X_op = O_absent;
11735 s = expr_end;
11736 continue;
11737
11738 case 's':
11739 /* cins and exts length-minus-one field. */
11740 gas_assert (!mips_opts.micromips);
11741 my_getExpression (&imm_expr, s);
11742 check_absolute_expr (ip, &imm_expr);
11743 if ((unsigned long) imm_expr.X_add_number > 31)
11744 {
11745 as_bad (_("Improper size (%lu)"),
11746 (unsigned long) imm_expr.X_add_number);
11747 imm_expr.X_add_number = 0;
11748 }
11749 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11750 imm_expr.X_op = O_absent;
11751 s = expr_end;
11752 continue;
11753
11754 case 'S':
11755 /* cins32/exts32 and cins/exts aliasing cint32/exts32
11756 length-minus-one field. */
11757 gas_assert (!mips_opts.micromips);
11758 my_getExpression (&imm_expr, s);
11759 check_absolute_expr (ip, &imm_expr);
11760 if ((long) imm_expr.X_add_number < 0
11761 || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11762 {
11763 as_bad (_("Improper size (%lu)"),
11764 (unsigned long) imm_expr.X_add_number);
11765 imm_expr.X_add_number = 0;
11766 }
11767 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11768 imm_expr.X_op = O_absent;
11769 s = expr_end;
11770 continue;
11771
11772 case 'Q':
11773 /* seqi/snei immediate field. */
11774 gas_assert (!mips_opts.micromips);
11775 my_getExpression (&imm_expr, s);
11776 check_absolute_expr (ip, &imm_expr);
11777 if ((long) imm_expr.X_add_number < -512
11778 || (long) imm_expr.X_add_number >= 512)
11779 {
11780 as_bad (_("Improper immediate (%ld)"),
11781 (long) imm_expr.X_add_number);
11782 imm_expr.X_add_number = 0;
11783 }
11784 INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
11785 imm_expr.X_op = O_absent;
11786 s = expr_end;
11787 continue;
11788
11789 case 'a': /* 8-bit signed offset in bit 6 */
11790 gas_assert (!mips_opts.micromips);
11791 my_getExpression (&imm_expr, s);
11792 check_absolute_expr (ip, &imm_expr);
11793 min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
11794 max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
11795 if (imm_expr.X_add_number < min_range
11796 || imm_expr.X_add_number > max_range)
11797 {
11798 as_bad (_("Offset not in range %ld..%ld (%ld)"),
11799 (long) min_range, (long) max_range,
11800 (long) imm_expr.X_add_number);
11801 }
11802 INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
11803 imm_expr.X_op = O_absent;
11804 s = expr_end;
11805 continue;
11806
11807 case 'b': /* 8-bit signed offset in bit 3 */
11808 gas_assert (!mips_opts.micromips);
11809 my_getExpression (&imm_expr, s);
11810 check_absolute_expr (ip, &imm_expr);
11811 min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
11812 max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
11813 if (imm_expr.X_add_number < min_range
11814 || imm_expr.X_add_number > max_range)
11815 {
11816 as_bad (_("Offset not in range %ld..%ld (%ld)"),
11817 (long) min_range, (long) max_range,
11818 (long) imm_expr.X_add_number);
11819 }
11820 INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
11821 imm_expr.X_op = O_absent;
11822 s = expr_end;
11823 continue;
11824
11825 case 'c': /* 9-bit signed offset in bit 6 */
11826 gas_assert (!mips_opts.micromips);
11827 my_getExpression (&imm_expr, s);
11828 check_absolute_expr (ip, &imm_expr);
11829 min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
11830 max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
11831 /* We check the offset range before adjusted. */
11832 min_range <<= 4;
11833 max_range <<= 4;
11834 if (imm_expr.X_add_number < min_range
11835 || imm_expr.X_add_number > max_range)
11836 {
11837 as_bad (_("Offset not in range %ld..%ld (%ld)"),
11838 (long) min_range, (long) max_range,
11839 (long) imm_expr.X_add_number);
11840 }
11841 if (imm_expr.X_add_number & 0xf)
11842 {
11843 as_bad (_("Offset not 16 bytes alignment (%ld)"),
11844 (long) imm_expr.X_add_number);
11845 }
11846 /* Right shift 4 bits to adjust the offset operand. */
11847 INSERT_OPERAND (0, OFFSET_C, *ip,
11848 imm_expr.X_add_number >> 4);
11849 imm_expr.X_op = O_absent;
11850 s = expr_end;
11851 continue;
11852
11853 case 'z':
11854 gas_assert (!mips_opts.micromips);
11855 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
11856 break;
11857 if (regno == AT && mips_opts.at)
11858 {
11859 if (mips_opts.at == ATREG)
11860 as_warn (_("used $at without \".set noat\""));
11861 else
11862 as_warn (_("used $%u with \".set at=$%u\""),
11863 regno, mips_opts.at);
11864 }
11865 INSERT_OPERAND (0, RZ, *ip, regno);
11866 continue;
11867
11868 case 'Z':
11869 gas_assert (!mips_opts.micromips);
11870 if (!reg_lookup (&s, RTYPE_FPU, &regno))
11871 break;
11872 INSERT_OPERAND (0, FZ, *ip, regno);
11873 continue;
11874
11875 case 'j':
11876 {
11877 int shift = 8;
11878 size_t i;
11879 /* Check whether there is only a single bracketed expression
11880 left. If so, it must be the base register and the
11881 constant must be zero. */
11882 if (*s == '(' && strchr (s + 1, '(') == 0)
11883 continue;
11884
11885 /* If this value won't fit into the offset, then go find
11886 a macro that will generate a 16- or 32-bit offset code
11887 pattern. */
11888 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11889 if ((i == 0 && (imm_expr.X_op != O_constant
11890 || imm_expr.X_add_number >= 1 << shift
11891 || imm_expr.X_add_number < -1 << shift))
11892 || i > 0)
11893 {
11894 imm_expr.X_op = O_absent;
11895 break;
11896 }
11897 INSERT_OPERAND (mips_opts.micromips, EVAOFFSET, *ip,
11898 imm_expr.X_add_number);
11899 imm_expr.X_op = O_absent;
11900 s = expr_end;
11901 }
11902 continue;
11903
11904 default:
11905 as_bad (_("Internal error: bad %s opcode "
11906 "(unknown extension operand type `+%c'): %s %s"),
11907 mips_opts.micromips ? "microMIPS" : "MIPS",
11908 *args, insn->name, insn->args);
11909 /* Further processing is fruitless. */
11910 return;
11911 }
11912 break;
11913
11914 case '.': /* 10-bit offset. */
11915 gas_assert (mips_opts.micromips);
11916 case '~': /* 12-bit offset. */
11917 {
11918 int shift = *args == '.' ? 9 : 11;
11919 size_t i;
11920
11921 /* Check whether there is only a single bracketed expression
11922 left. If so, it must be the base register and the
11923 constant must be zero. */
11924 if (*s == '(' && strchr (s + 1, '(') == 0)
11925 continue;
11926
11927 /* If this value won't fit into the offset, then go find
11928 a macro that will generate a 16- or 32-bit offset code
11929 pattern. */
11930 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11931 if ((i == 0 && (imm_expr.X_op != O_constant
11932 || imm_expr.X_add_number >= 1 << shift
11933 || imm_expr.X_add_number < -1 << shift))
11934 || i > 0)
11935 {
11936 imm_expr.X_op = O_absent;
11937 break;
11938 }
11939 if (shift == 9)
11940 INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
11941 else
11942 INSERT_OPERAND (mips_opts.micromips,
11943 OFFSET12, *ip, imm_expr.X_add_number);
11944 imm_expr.X_op = O_absent;
11945 s = expr_end;
11946 }
11947 continue;
11948
11949 case '<': /* must be at least one digit */
11950 /*
11951 * According to the manual, if the shift amount is greater
11952 * than 31 or less than 0, then the shift amount should be
11953 * mod 32. In reality the mips assembler issues an error.
11954 * We issue a warning and mask out all but the low 5 bits.
11955 */
11956 my_getExpression (&imm_expr, s);
11957 check_absolute_expr (ip, &imm_expr);
11958 if ((unsigned long) imm_expr.X_add_number > 31)
11959 as_warn (_("Improper shift amount (%lu)"),
11960 (unsigned long) imm_expr.X_add_number);
11961 INSERT_OPERAND (mips_opts.micromips,
11962 SHAMT, *ip, imm_expr.X_add_number);
11963 imm_expr.X_op = O_absent;
11964 s = expr_end;
11965 continue;
11966
11967 case '>': /* shift amount minus 32 */
11968 my_getExpression (&imm_expr, s);
11969 check_absolute_expr (ip, &imm_expr);
11970 if ((unsigned long) imm_expr.X_add_number < 32
11971 || (unsigned long) imm_expr.X_add_number > 63)
11972 break;
11973 INSERT_OPERAND (mips_opts.micromips,
11974 SHAMT, *ip, imm_expr.X_add_number - 32);
11975 imm_expr.X_op = O_absent;
11976 s = expr_end;
11977 continue;
11978
11979 case 'k': /* CACHE code. */
11980 case 'h': /* PREFX code. */
11981 case '1': /* SYNC type. */
11982 my_getExpression (&imm_expr, s);
11983 check_absolute_expr (ip, &imm_expr);
11984 if ((unsigned long) imm_expr.X_add_number > 31)
11985 as_warn (_("Invalid value for `%s' (%lu)"),
11986 ip->insn_mo->name,
11987 (unsigned long) imm_expr.X_add_number);
11988 switch (*args)
11989 {
11990 case 'k':
11991 if (mips_fix_cn63xxp1
11992 && !mips_opts.micromips
11993 && strcmp ("pref", insn->name) == 0)
11994 switch (imm_expr.X_add_number)
11995 {
11996 case 5:
11997 case 25:
11998 case 26:
11999 case 27:
12000 case 28:
12001 case 29:
12002 case 30:
12003 case 31: /* These are ok. */
12004 break;
12005
12006 default: /* The rest must be changed to 28. */
12007 imm_expr.X_add_number = 28;
12008 break;
12009 }
12010 INSERT_OPERAND (mips_opts.micromips,
12011 CACHE, *ip, imm_expr.X_add_number);
12012 break;
12013 case 'h':
12014 INSERT_OPERAND (mips_opts.micromips,
12015 PREFX, *ip, imm_expr.X_add_number);
12016 break;
12017 case '1':
12018 INSERT_OPERAND (mips_opts.micromips,
12019 STYPE, *ip, imm_expr.X_add_number);
12020 break;
12021 }
12022 imm_expr.X_op = O_absent;
12023 s = expr_end;
12024 continue;
12025
12026 case 'c': /* BREAK code. */
12027 {
12028 unsigned long mask = (mips_opts.micromips
12029 ? MICROMIPSOP_MASK_CODE
12030 : OP_MASK_CODE);
12031
12032 my_getExpression (&imm_expr, s);
12033 check_absolute_expr (ip, &imm_expr);
12034 if ((unsigned long) imm_expr.X_add_number > mask)
12035 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
12036 ip->insn_mo->name,
12037 mask, (unsigned long) imm_expr.X_add_number);
12038 INSERT_OPERAND (mips_opts.micromips,
12039 CODE, *ip, imm_expr.X_add_number);
12040 imm_expr.X_op = O_absent;
12041 s = expr_end;
12042 }
12043 continue;
12044
12045 case 'q': /* Lower BREAK code. */
12046 {
12047 unsigned long mask = (mips_opts.micromips
12048 ? MICROMIPSOP_MASK_CODE2
12049 : OP_MASK_CODE2);
12050
12051 my_getExpression (&imm_expr, s);
12052 check_absolute_expr (ip, &imm_expr);
12053 if ((unsigned long) imm_expr.X_add_number > mask)
12054 as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
12055 ip->insn_mo->name,
12056 mask, (unsigned long) imm_expr.X_add_number);
12057 INSERT_OPERAND (mips_opts.micromips,
12058 CODE2, *ip, imm_expr.X_add_number);
12059 imm_expr.X_op = O_absent;
12060 s = expr_end;
12061 }
12062 continue;
12063
12064 case 'B': /* 20- or 10-bit syscall/break/wait code. */
12065 {
12066 unsigned long mask = (mips_opts.micromips
12067 ? MICROMIPSOP_MASK_CODE10
12068 : OP_MASK_CODE20);
12069
12070 my_getExpression (&imm_expr, s);
12071 check_absolute_expr (ip, &imm_expr);
12072 if ((unsigned long) imm_expr.X_add_number > mask)
12073 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
12074 ip->insn_mo->name,
12075 mask, (unsigned long) imm_expr.X_add_number);
12076 if (mips_opts.micromips)
12077 INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
12078 else
12079 INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
12080 imm_expr.X_op = O_absent;
12081 s = expr_end;
12082 }
12083 continue;
12084
12085 case 'C': /* 25- or 23-bit coprocessor code. */
12086 {
12087 unsigned long mask = (mips_opts.micromips
12088 ? MICROMIPSOP_MASK_COPZ
12089 : OP_MASK_COPZ);
12090
12091 my_getExpression (&imm_expr, s);
12092 check_absolute_expr (ip, &imm_expr);
12093 if ((unsigned long) imm_expr.X_add_number > mask)
12094 as_warn (_("Coproccesor code > %u bits (%lu)"),
12095 mips_opts.micromips ? 23U : 25U,
12096 (unsigned long) imm_expr.X_add_number);
12097 INSERT_OPERAND (mips_opts.micromips,
12098 COPZ, *ip, imm_expr.X_add_number);
12099 imm_expr.X_op = O_absent;
12100 s = expr_end;
12101 }
12102 continue;
12103
12104 case 'J': /* 19-bit WAIT code. */
12105 gas_assert (!mips_opts.micromips);
12106 my_getExpression (&imm_expr, s);
12107 check_absolute_expr (ip, &imm_expr);
12108 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
12109 {
12110 as_warn (_("Illegal 19-bit code (%lu)"),
12111 (unsigned long) imm_expr.X_add_number);
12112 imm_expr.X_add_number &= OP_MASK_CODE19;
12113 }
12114 INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
12115 imm_expr.X_op = O_absent;
12116 s = expr_end;
12117 continue;
12118
12119 case 'P': /* Performance register. */
12120 gas_assert (!mips_opts.micromips);
12121 my_getExpression (&imm_expr, s);
12122 check_absolute_expr (ip, &imm_expr);
12123 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
12124 as_warn (_("Invalid performance register (%lu)"),
12125 (unsigned long) imm_expr.X_add_number);
12126 if (imm_expr.X_add_number != 0 && mips_opts.arch == CPU_R5900
12127 && (!strcmp(insn->name,"mfps") || !strcmp(insn->name,"mtps")))
12128 as_warn (_("Invalid performance register (%lu)"),
12129 (unsigned long) imm_expr.X_add_number);
12130 INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
12131 imm_expr.X_op = O_absent;
12132 s = expr_end;
12133 continue;
12134
12135 case 'G': /* Coprocessor destination register. */
12136 {
12137 unsigned long opcode = ip->insn_opcode;
12138 unsigned long mask;
12139 unsigned int types;
12140 int cop0;
12141
12142 if (mips_opts.micromips)
12143 {
12144 mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
12145 | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
12146 | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
12147 opcode &= mask;
12148 switch (opcode)
12149 {
12150 case 0x000000fc: /* mfc0 */
12151 case 0x000002fc: /* mtc0 */
12152 case 0x580000fc: /* dmfc0 */
12153 case 0x580002fc: /* dmtc0 */
12154 cop0 = 1;
12155 break;
12156 default:
12157 cop0 = 0;
12158 break;
12159 }
12160 }
12161 else
12162 {
12163 opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
12164 cop0 = opcode == OP_OP_COP0;
12165 }
12166 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
12167 ok = reg_lookup (&s, types, &regno);
12168 if (mips_opts.micromips)
12169 INSERT_OPERAND (1, RS, *ip, regno);
12170 else
12171 INSERT_OPERAND (0, RD, *ip, regno);
12172 if (ok)
12173 {
12174 lastregno = regno;
12175 continue;
12176 }
12177 }
12178 break;
12179
12180 case 'y': /* ALNV.PS source register. */
12181 gas_assert (mips_opts.micromips);
12182 goto do_reg;
12183 case 'x': /* Ignore register name. */
12184 case 'U': /* Destination register (CLO/CLZ). */
12185 case 'g': /* Coprocessor destination register. */
12186 gas_assert (!mips_opts.micromips);
12187 case 'b': /* Base register. */
12188 case 'd': /* Destination register. */
12189 case 's': /* Source register. */
12190 case 't': /* Target register. */
12191 case 'r': /* Both target and source. */
12192 case 'v': /* Both dest and source. */
12193 case 'w': /* Both dest and target. */
12194 case 'E': /* Coprocessor target register. */
12195 case 'K': /* RDHWR destination register. */
12196 case 'z': /* Must be zero register. */
12197 do_reg:
12198 s_reset = s;
12199 if (*args == 'E' || *args == 'K')
12200 ok = reg_lookup (&s, RTYPE_NUM, &regno);
12201 else
12202 {
12203 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12204 if (regno == AT && mips_opts.at)
12205 {
12206 if (mips_opts.at == ATREG)
12207 as_warn (_("Used $at without \".set noat\""));
12208 else
12209 as_warn (_("Used $%u with \".set at=$%u\""),
12210 regno, mips_opts.at);
12211 }
12212 }
12213 if (ok)
12214 {
12215 c = *args;
12216 if (*s == ' ')
12217 ++s;
12218 if (args[1] != *s)
12219 {
12220 if (c == 'r' || c == 'v' || c == 'w')
12221 {
12222 regno = lastregno;
12223 s = s_reset;
12224 ++args;
12225 }
12226 }
12227 /* 'z' only matches $0. */
12228 if (c == 'z' && regno != 0)
12229 break;
12230
12231 if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
12232 {
12233 if (regno == lastregno)
12234 {
12235 insn_error
12236 = _("Source and destination must be different");
12237 continue;
12238 }
12239 if (regno == 31 && lastregno == 0xffffffff)
12240 {
12241 insn_error
12242 = _("A destination register must be supplied");
12243 continue;
12244 }
12245 }
12246 /* Now that we have assembled one operand, we use the args
12247 string to figure out where it goes in the instruction. */
12248 switch (c)
12249 {
12250 case 'r':
12251 case 's':
12252 case 'v':
12253 case 'b':
12254 INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
12255 break;
12256
12257 case 'K':
12258 if (mips_opts.micromips)
12259 INSERT_OPERAND (1, RS, *ip, regno);
12260 else
12261 INSERT_OPERAND (0, RD, *ip, regno);
12262 break;
12263
12264 case 'd':
12265 case 'g':
12266 INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
12267 break;
12268
12269 case 'U':
12270 gas_assert (!mips_opts.micromips);
12271 INSERT_OPERAND (0, RD, *ip, regno);
12272 INSERT_OPERAND (0, RT, *ip, regno);
12273 break;
12274
12275 case 'w':
12276 case 't':
12277 case 'E':
12278 INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
12279 break;
12280
12281 case 'y':
12282 gas_assert (mips_opts.micromips);
12283 INSERT_OPERAND (1, RS3, *ip, regno);
12284 break;
12285
12286 case 'x':
12287 /* This case exists because on the r3000 trunc
12288 expands into a macro which requires a gp
12289 register. On the r6000 or r4000 it is
12290 assembled into a single instruction which
12291 ignores the register. Thus the insn version
12292 is MIPS_ISA2 and uses 'x', and the macro
12293 version is MIPS_ISA1 and uses 't'. */
12294 break;
12295
12296 case 'z':
12297 /* This case is for the div instruction, which
12298 acts differently if the destination argument
12299 is $0. This only matches $0, and is checked
12300 outside the switch. */
12301 break;
12302 }
12303 lastregno = regno;
12304 continue;
12305 }
12306 switch (*args++)
12307 {
12308 case 'r':
12309 case 'v':
12310 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
12311 continue;
12312
12313 case 'w':
12314 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
12315 continue;
12316 }
12317 break;
12318
12319 case 'O': /* MDMX alignment immediate constant. */
12320 gas_assert (!mips_opts.micromips);
12321 my_getExpression (&imm_expr, s);
12322 check_absolute_expr (ip, &imm_expr);
12323 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
12324 as_warn (_("Improper align amount (%ld), using low bits"),
12325 (long) imm_expr.X_add_number);
12326 INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
12327 imm_expr.X_op = O_absent;
12328 s = expr_end;
12329 continue;
12330
12331 case 'Q': /* MDMX vector, element sel, or const. */
12332 if (s[0] != '$')
12333 {
12334 /* MDMX Immediate. */
12335 gas_assert (!mips_opts.micromips);
12336 my_getExpression (&imm_expr, s);
12337 check_absolute_expr (ip, &imm_expr);
12338 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
12339 as_warn (_("Invalid MDMX Immediate (%ld)"),
12340 (long) imm_expr.X_add_number);
12341 INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
12342 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12343 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12344 else
12345 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
12346 imm_expr.X_op = O_absent;
12347 s = expr_end;
12348 continue;
12349 }
12350 /* Not MDMX Immediate. Fall through. */
12351 case 'X': /* MDMX destination register. */
12352 case 'Y': /* MDMX source register. */
12353 case 'Z': /* MDMX target register. */
12354 is_mdmx = 1;
12355 case 'W':
12356 gas_assert (!mips_opts.micromips);
12357 case 'D': /* Floating point destination register. */
12358 case 'S': /* Floating point source register. */
12359 case 'T': /* Floating point target register. */
12360 case 'R': /* Floating point source register. */
12361 case 'V':
12362 rtype = RTYPE_FPU;
12363 if (is_mdmx
12364 || (mips_opts.ase_mdmx
12365 && (ip->insn_mo->pinfo & FP_D)
12366 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12367 | INSN_COPROC_MEMORY_DELAY
12368 | INSN_LOAD_COPROC_DELAY
12369 | INSN_LOAD_MEMORY_DELAY
12370 | INSN_STORE_MEMORY))))
12371 rtype |= RTYPE_VEC;
12372 s_reset = s;
12373 if (reg_lookup (&s, rtype, &regno))
12374 {
12375 if ((regno & 1) != 0
12376 && HAVE_32BIT_FPRS
12377 && !mips_oddfpreg_ok (ip->insn_mo, argnum))
12378 as_warn (_("Float register should be even, was %d"),
12379 regno);
12380
12381 c = *args;
12382 if (*s == ' ')
12383 ++s;
12384 if (args[1] != *s)
12385 {
12386 if (c == 'V' || c == 'W')
12387 {
12388 regno = lastregno;
12389 s = s_reset;
12390 ++args;
12391 }
12392 }
12393 switch (c)
12394 {
12395 case 'D':
12396 case 'X':
12397 INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
12398 break;
12399
12400 case 'V':
12401 case 'S':
12402 case 'Y':
12403 INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
12404 break;
12405
12406 case 'Q':
12407 /* This is like 'Z', but also needs to fix the MDMX
12408 vector/scalar select bits. Note that the
12409 scalar immediate case is handled above. */
12410 if (*s == '[')
12411 {
12412 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12413 int max_el = (is_qh ? 3 : 7);
12414 s++;
12415 my_getExpression(&imm_expr, s);
12416 check_absolute_expr (ip, &imm_expr);
12417 s = expr_end;
12418 if (imm_expr.X_add_number > max_el)
12419 as_bad (_("Bad element selector %ld"),
12420 (long) imm_expr.X_add_number);
12421 imm_expr.X_add_number &= max_el;
12422 ip->insn_opcode |= (imm_expr.X_add_number
12423 << (OP_SH_VSEL +
12424 (is_qh ? 2 : 1)));
12425 imm_expr.X_op = O_absent;
12426 if (*s != ']')
12427 as_warn (_("Expecting ']' found '%s'"), s);
12428 else
12429 s++;
12430 }
12431 else
12432 {
12433 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12434 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12435 << OP_SH_VSEL);
12436 else
12437 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12438 OP_SH_VSEL);
12439 }
12440 /* Fall through. */
12441 case 'W':
12442 case 'T':
12443 case 'Z':
12444 INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
12445 break;
12446
12447 case 'R':
12448 INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
12449 break;
12450 }
12451 lastregno = regno;
12452 continue;
12453 }
12454
12455 switch (*args++)
12456 {
12457 case 'V':
12458 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
12459 continue;
12460
12461 case 'W':
12462 INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
12463 continue;
12464 }
12465 break;
12466
12467 case 'I':
12468 my_getExpression (&imm_expr, s);
12469 if (imm_expr.X_op != O_big
12470 && imm_expr.X_op != O_constant)
12471 insn_error = _("absolute expression required");
12472 if (HAVE_32BIT_GPRS)
12473 normalize_constant_expr (&imm_expr);
12474 s = expr_end;
12475 continue;
12476
12477 case 'A':
12478 my_getExpression (&offset_expr, s);
12479 normalize_address_expr (&offset_expr);
12480 *imm_reloc = BFD_RELOC_32;
12481 s = expr_end;
12482 continue;
12483
12484 case 'F':
12485 case 'L':
12486 case 'f':
12487 case 'l':
12488 {
12489 int f64;
12490 int using_gprs;
12491 char *save_in;
12492 char *err;
12493 unsigned char temp[8];
12494 int len;
12495 unsigned int length;
12496 segT seg;
12497 subsegT subseg;
12498 char *p;
12499
12500 /* These only appear as the last operand in an
12501 instruction, and every instruction that accepts
12502 them in any variant accepts them in all variants.
12503 This means we don't have to worry about backing out
12504 any changes if the instruction does not match.
12505
12506 The difference between them is the size of the
12507 floating point constant and where it goes. For 'F'
12508 and 'L' the constant is 64 bits; for 'f' and 'l' it
12509 is 32 bits. Where the constant is placed is based
12510 on how the MIPS assembler does things:
12511 F -- .rdata
12512 L -- .lit8
12513 f -- immediate value
12514 l -- .lit4
12515
12516 The .lit4 and .lit8 sections are only used if
12517 permitted by the -G argument.
12518
12519 The code below needs to know whether the target register
12520 is 32 or 64 bits wide. It relies on the fact 'f' and
12521 'F' are used with GPR-based instructions and 'l' and
12522 'L' are used with FPR-based instructions. */
12523
12524 f64 = *args == 'F' || *args == 'L';
12525 using_gprs = *args == 'F' || *args == 'f';
12526
12527 save_in = input_line_pointer;
12528 input_line_pointer = s;
12529 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12530 length = len;
12531 s = input_line_pointer;
12532 input_line_pointer = save_in;
12533 if (err != NULL && *err != '\0')
12534 {
12535 as_bad (_("Bad floating point constant: %s"), err);
12536 memset (temp, '\0', sizeof temp);
12537 length = f64 ? 8 : 4;
12538 }
12539
12540 gas_assert (length == (unsigned) (f64 ? 8 : 4));
12541
12542 if (*args == 'f'
12543 || (*args == 'l'
12544 && (g_switch_value < 4
12545 || (temp[0] == 0 && temp[1] == 0)
12546 || (temp[2] == 0 && temp[3] == 0))))
12547 {
12548 imm_expr.X_op = O_constant;
12549 if (!target_big_endian)
12550 imm_expr.X_add_number = bfd_getl32 (temp);
12551 else
12552 imm_expr.X_add_number = bfd_getb32 (temp);
12553 }
12554 else if (length > 4
12555 && !mips_disable_float_construction
12556 /* Constants can only be constructed in GPRs and
12557 copied to FPRs if the GPRs are at least as wide
12558 as the FPRs. Force the constant into memory if
12559 we are using 64-bit FPRs but the GPRs are only
12560 32 bits wide. */
12561 && (using_gprs
12562 || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
12563 && ((temp[0] == 0 && temp[1] == 0)
12564 || (temp[2] == 0 && temp[3] == 0))
12565 && ((temp[4] == 0 && temp[5] == 0)
12566 || (temp[6] == 0 && temp[7] == 0)))
12567 {
12568 /* The value is simple enough to load with a couple of
12569 instructions. If using 32-bit registers, set
12570 imm_expr to the high order 32 bits and offset_expr to
12571 the low order 32 bits. Otherwise, set imm_expr to
12572 the entire 64 bit constant. */
12573 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
12574 {
12575 imm_expr.X_op = O_constant;
12576 offset_expr.X_op = O_constant;
12577 if (!target_big_endian)
12578 {
12579 imm_expr.X_add_number = bfd_getl32 (temp + 4);
12580 offset_expr.X_add_number = bfd_getl32 (temp);
12581 }
12582 else
12583 {
12584 imm_expr.X_add_number = bfd_getb32 (temp);
12585 offset_expr.X_add_number = bfd_getb32 (temp + 4);
12586 }
12587 if (offset_expr.X_add_number == 0)
12588 offset_expr.X_op = O_absent;
12589 }
12590 else if (sizeof (imm_expr.X_add_number) > 4)
12591 {
12592 imm_expr.X_op = O_constant;
12593 if (!target_big_endian)
12594 imm_expr.X_add_number = bfd_getl64 (temp);
12595 else
12596 imm_expr.X_add_number = bfd_getb64 (temp);
12597 }
12598 else
12599 {
12600 imm_expr.X_op = O_big;
12601 imm_expr.X_add_number = 4;
12602 if (!target_big_endian)
12603 {
12604 generic_bignum[0] = bfd_getl16 (temp);
12605 generic_bignum[1] = bfd_getl16 (temp + 2);
12606 generic_bignum[2] = bfd_getl16 (temp + 4);
12607 generic_bignum[3] = bfd_getl16 (temp + 6);
12608 }
12609 else
12610 {
12611 generic_bignum[0] = bfd_getb16 (temp + 6);
12612 generic_bignum[1] = bfd_getb16 (temp + 4);
12613 generic_bignum[2] = bfd_getb16 (temp + 2);
12614 generic_bignum[3] = bfd_getb16 (temp);
12615 }
12616 }
12617 }
12618 else
12619 {
12620 const char *newname;
12621 segT new_seg;
12622
12623 /* Switch to the right section. */
12624 seg = now_seg;
12625 subseg = now_subseg;
12626 switch (*args)
12627 {
12628 default: /* unused default case avoids warnings. */
12629 case 'L':
12630 newname = RDATA_SECTION_NAME;
12631 if (g_switch_value >= 8)
12632 newname = ".lit8";
12633 break;
12634 case 'F':
12635 newname = RDATA_SECTION_NAME;
12636 break;
12637 case 'l':
12638 gas_assert (g_switch_value >= 4);
12639 newname = ".lit4";
12640 break;
12641 }
12642 new_seg = subseg_new (newname, (subsegT) 0);
12643 if (IS_ELF)
12644 bfd_set_section_flags (stdoutput, new_seg,
12645 (SEC_ALLOC
12646 | SEC_LOAD
12647 | SEC_READONLY
12648 | SEC_DATA));
12649 frag_align (*args == 'l' ? 2 : 3, 0, 0);
12650 if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
12651 record_alignment (new_seg, 4);
12652 else
12653 record_alignment (new_seg, *args == 'l' ? 2 : 3);
12654 if (seg == now_seg)
12655 as_bad (_("Can't use floating point insn in this section"));
12656
12657 /* Set the argument to the current address in the
12658 section. */
12659 offset_expr.X_op = O_symbol;
12660 offset_expr.X_add_symbol = symbol_temp_new_now ();
12661 offset_expr.X_add_number = 0;
12662
12663 /* Put the floating point number into the section. */
12664 p = frag_more ((int) length);
12665 memcpy (p, temp, length);
12666
12667 /* Switch back to the original section. */
12668 subseg_set (seg, subseg);
12669 }
12670 }
12671 continue;
12672
12673 case 'i': /* 16-bit unsigned immediate. */
12674 case 'j': /* 16-bit signed immediate. */
12675 *imm_reloc = BFD_RELOC_LO16;
12676 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12677 {
12678 int more;
12679 offsetT minval, maxval;
12680
12681 more = (insn + 1 < past
12682 && strcmp (insn->name, insn[1].name) == 0);
12683
12684 /* If the expression was written as an unsigned number,
12685 only treat it as signed if there are no more
12686 alternatives. */
12687 if (more
12688 && *args == 'j'
12689 && sizeof (imm_expr.X_add_number) <= 4
12690 && imm_expr.X_op == O_constant
12691 && imm_expr.X_add_number < 0
12692 && imm_expr.X_unsigned
12693 && HAVE_64BIT_GPRS)
12694 break;
12695
12696 /* For compatibility with older assemblers, we accept
12697 0x8000-0xffff as signed 16-bit numbers when only
12698 signed numbers are allowed. */
12699 if (*args == 'i')
12700 minval = 0, maxval = 0xffff;
12701 else if (more)
12702 minval = -0x8000, maxval = 0x7fff;
12703 else
12704 minval = -0x8000, maxval = 0xffff;
12705
12706 if (imm_expr.X_op != O_constant
12707 || imm_expr.X_add_number < minval
12708 || imm_expr.X_add_number > maxval)
12709 {
12710 if (more)
12711 break;
12712 if (imm_expr.X_op == O_constant
12713 || imm_expr.X_op == O_big)
12714 as_bad (_("Expression out of range"));
12715 }
12716 }
12717 s = expr_end;
12718 continue;
12719
12720 case 'o': /* 16-bit offset. */
12721 offset_reloc[0] = BFD_RELOC_LO16;
12722 offset_reloc[1] = BFD_RELOC_UNUSED;
12723 offset_reloc[2] = BFD_RELOC_UNUSED;
12724
12725 /* Check whether there is only a single bracketed expression
12726 left. If so, it must be the base register and the
12727 constant must be zero. */
12728 if (*s == '(' && strchr (s + 1, '(') == 0)
12729 {
12730 offset_expr.X_op = O_constant;
12731 offset_expr.X_add_number = 0;
12732 continue;
12733 }
12734
12735 /* If this value won't fit into a 16 bit offset, then go
12736 find a macro that will generate the 32 bit offset
12737 code pattern. */
12738 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12739 && (offset_expr.X_op != O_constant
12740 || offset_expr.X_add_number >= 0x8000
12741 || offset_expr.X_add_number < -0x8000))
12742 break;
12743
12744 s = expr_end;
12745 continue;
12746
12747 case 'p': /* PC-relative offset. */
12748 *offset_reloc = BFD_RELOC_16_PCREL_S2;
12749 my_getExpression (&offset_expr, s);
12750 s = expr_end;
12751 continue;
12752
12753 case 'u': /* Upper 16 bits. */
12754 *imm_reloc = BFD_RELOC_LO16;
12755 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12756 && imm_expr.X_op == O_constant
12757 && (imm_expr.X_add_number < 0
12758 || imm_expr.X_add_number >= 0x10000))
12759 as_bad (_("lui expression (%lu) not in range 0..65535"),
12760 (unsigned long) imm_expr.X_add_number);
12761 s = expr_end;
12762 continue;
12763
12764 case 'a': /* 26-bit address. */
12765 *offset_reloc = BFD_RELOC_MIPS_JMP;
12766 my_getExpression (&offset_expr, s);
12767 s = expr_end;
12768 continue;
12769
12770 case 'N': /* 3-bit branch condition code. */
12771 case 'M': /* 3-bit compare condition code. */
12772 rtype = RTYPE_CCC;
12773 if (ip->insn_mo->pinfo & (FP_D | FP_S))
12774 rtype |= RTYPE_FCC;
12775 if (!reg_lookup (&s, rtype, &regno))
12776 break;
12777 if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12778 || strcmp (str + strlen (str) - 5, "any2f") == 0
12779 || strcmp (str + strlen (str) - 5, "any2t") == 0)
12780 && (regno & 1) != 0)
12781 as_warn (_("Condition code register should be even for %s, "
12782 "was %d"),
12783 str, regno);
12784 if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12785 || strcmp (str + strlen (str) - 5, "any4t") == 0)
12786 && (regno & 3) != 0)
12787 as_warn (_("Condition code register should be 0 or 4 for %s, "
12788 "was %d"),
12789 str, regno);
12790 if (*args == 'N')
12791 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12792 else
12793 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
12794 continue;
12795
12796 case 'H':
12797 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
12798 s += 2;
12799 if (ISDIGIT (*s))
12800 {
12801 c = 0;
12802 do
12803 {
12804 c *= 10;
12805 c += *s - '0';
12806 ++s;
12807 }
12808 while (ISDIGIT (*s));
12809 }
12810 else
12811 c = 8; /* Invalid sel value. */
12812
12813 if (c > 7)
12814 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
12815 INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
12816 continue;
12817
12818 case 'e':
12819 gas_assert (!mips_opts.micromips);
12820 /* Must be at least one digit. */
12821 my_getExpression (&imm_expr, s);
12822 check_absolute_expr (ip, &imm_expr);
12823
12824 if ((unsigned long) imm_expr.X_add_number
12825 > (unsigned long) OP_MASK_VECBYTE)
12826 {
12827 as_bad (_("bad byte vector index (%ld)"),
12828 (long) imm_expr.X_add_number);
12829 imm_expr.X_add_number = 0;
12830 }
12831
12832 INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
12833 imm_expr.X_op = O_absent;
12834 s = expr_end;
12835 continue;
12836
12837 case '%':
12838 gas_assert (!mips_opts.micromips);
12839 my_getExpression (&imm_expr, s);
12840 check_absolute_expr (ip, &imm_expr);
12841
12842 if ((unsigned long) imm_expr.X_add_number
12843 > (unsigned long) OP_MASK_VECALIGN)
12844 {
12845 as_bad (_("bad byte vector index (%ld)"),
12846 (long) imm_expr.X_add_number);
12847 imm_expr.X_add_number = 0;
12848 }
12849
12850 INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
12851 imm_expr.X_op = O_absent;
12852 s = expr_end;
12853 continue;
12854
12855 case 'm': /* Opcode extension character. */
12856 gas_assert (mips_opts.micromips);
12857 c = *++args;
12858 switch (c)
12859 {
12860 case 'r':
12861 if (strncmp (s, "$pc", 3) == 0)
12862 {
12863 s += 3;
12864 continue;
12865 }
12866 break;
12867
12868 case 'a':
12869 case 'b':
12870 case 'c':
12871 case 'd':
12872 case 'e':
12873 case 'f':
12874 case 'g':
12875 case 'h':
12876 case 'i':
12877 case 'j':
12878 case 'l':
12879 case 'm':
12880 case 'n':
12881 case 'p':
12882 case 'q':
12883 case 's':
12884 case 't':
12885 case 'x':
12886 case 'y':
12887 case 'z':
12888 s_reset = s;
12889 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12890 if (regno == AT && mips_opts.at)
12891 {
12892 if (mips_opts.at == ATREG)
12893 as_warn (_("Used $at without \".set noat\""));
12894 else
12895 as_warn (_("Used $%u with \".set at=$%u\""),
12896 regno, mips_opts.at);
12897 }
12898 if (!ok)
12899 {
12900 if (c == 'c')
12901 {
12902 gas_assert (args[1] == ',');
12903 regno = lastregno;
12904 ++args;
12905 }
12906 else if (c == 't')
12907 {
12908 gas_assert (args[1] == ',');
12909 ++args;
12910 continue; /* Nothing to do. */
12911 }
12912 else
12913 break;
12914 }
12915
12916 if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
12917 {
12918 if (regno == lastregno)
12919 {
12920 insn_error
12921 = _("Source and destination must be different");
12922 continue;
12923 }
12924 if (regno == 31 && lastregno == 0xffffffff)
12925 {
12926 insn_error
12927 = _("A destination register must be supplied");
12928 continue;
12929 }
12930 }
12931
12932 if (*s == ' ')
12933 ++s;
12934 if (args[1] != *s)
12935 {
12936 if (c == 'e')
12937 {
12938 gas_assert (args[1] == ',');
12939 regno = lastregno;
12940 s = s_reset;
12941 ++args;
12942 }
12943 else if (c == 't')
12944 {
12945 gas_assert (args[1] == ',');
12946 s = s_reset;
12947 ++args;
12948 continue; /* Nothing to do. */
12949 }
12950 }
12951
12952 /* Make sure regno is the same as lastregno. */
12953 if (c == 't' && regno != lastregno)
12954 break;
12955
12956 /* Make sure regno is the same as destregno. */
12957 if (c == 'x' && regno != destregno)
12958 break;
12959
12960 /* We need to save regno, before regno maps to the
12961 microMIPS register encoding. */
12962 lastregno = regno;
12963
12964 if (c == 'f')
12965 destregno = regno;
12966
12967 switch (c)
12968 {
12969 case 'a':
12970 if (regno != GP)
12971 regno = ILLEGAL_REG;
12972 break;
12973
12974 case 'b':
12975 regno = mips32_to_micromips_reg_b_map[regno];
12976 break;
12977
12978 case 'c':
12979 regno = mips32_to_micromips_reg_c_map[regno];
12980 break;
12981
12982 case 'd':
12983 regno = mips32_to_micromips_reg_d_map[regno];
12984 break;
12985
12986 case 'e':
12987 regno = mips32_to_micromips_reg_e_map[regno];
12988 break;
12989
12990 case 'f':
12991 regno = mips32_to_micromips_reg_f_map[regno];
12992 break;
12993
12994 case 'g':
12995 regno = mips32_to_micromips_reg_g_map[regno];
12996 break;
12997
12998 case 'h':
12999 regno = mips32_to_micromips_reg_h_map[regno];
13000 break;
13001
13002 case 'i':
13003 switch (EXTRACT_OPERAND (1, MI, *ip))
13004 {
13005 case 4:
13006 if (regno == 21)
13007 regno = 3;
13008 else if (regno == 22)
13009 regno = 4;
13010 else if (regno == 5)
13011 regno = 5;
13012 else if (regno == 6)
13013 regno = 6;
13014 else if (regno == 7)
13015 regno = 7;
13016 else
13017 regno = ILLEGAL_REG;
13018 break;
13019
13020 case 5:
13021 if (regno == 6)
13022 regno = 0;
13023 else if (regno == 7)
13024 regno = 1;
13025 else
13026 regno = ILLEGAL_REG;
13027 break;
13028
13029 case 6:
13030 if (regno == 7)
13031 regno = 2;
13032 else
13033 regno = ILLEGAL_REG;
13034 break;
13035
13036 default:
13037 regno = ILLEGAL_REG;
13038 break;
13039 }
13040 break;
13041
13042 case 'l':
13043 regno = mips32_to_micromips_reg_l_map[regno];
13044 break;
13045
13046 case 'm':
13047 regno = mips32_to_micromips_reg_m_map[regno];
13048 break;
13049
13050 case 'n':
13051 regno = mips32_to_micromips_reg_n_map[regno];
13052 break;
13053
13054 case 'q':
13055 regno = mips32_to_micromips_reg_q_map[regno];
13056 break;
13057
13058 case 's':
13059 if (regno != SP)
13060 regno = ILLEGAL_REG;
13061 break;
13062
13063 case 'y':
13064 if (regno != 31)
13065 regno = ILLEGAL_REG;
13066 break;
13067
13068 case 'z':
13069 if (regno != ZERO)
13070 regno = ILLEGAL_REG;
13071 break;
13072
13073 case 'j': /* Do nothing. */
13074 case 'p':
13075 case 't':
13076 case 'x':
13077 break;
13078
13079 default:
13080 abort ();
13081 }
13082
13083 if (regno == ILLEGAL_REG)
13084 break;
13085
13086 switch (c)
13087 {
13088 case 'b':
13089 INSERT_OPERAND (1, MB, *ip, regno);
13090 break;
13091
13092 case 'c':
13093 INSERT_OPERAND (1, MC, *ip, regno);
13094 break;
13095
13096 case 'd':
13097 INSERT_OPERAND (1, MD, *ip, regno);
13098 break;
13099
13100 case 'e':
13101 INSERT_OPERAND (1, ME, *ip, regno);
13102 break;
13103
13104 case 'f':
13105 INSERT_OPERAND (1, MF, *ip, regno);
13106 break;
13107
13108 case 'g':
13109 INSERT_OPERAND (1, MG, *ip, regno);
13110 break;
13111
13112 case 'h':
13113 INSERT_OPERAND (1, MH, *ip, regno);
13114 break;
13115
13116 case 'i':
13117 INSERT_OPERAND (1, MI, *ip, regno);
13118 break;
13119
13120 case 'j':
13121 INSERT_OPERAND (1, MJ, *ip, regno);
13122 break;
13123
13124 case 'l':
13125 INSERT_OPERAND (1, ML, *ip, regno);
13126 break;
13127
13128 case 'm':
13129 INSERT_OPERAND (1, MM, *ip, regno);
13130 break;
13131
13132 case 'n':
13133 INSERT_OPERAND (1, MN, *ip, regno);
13134 break;
13135
13136 case 'p':
13137 INSERT_OPERAND (1, MP, *ip, regno);
13138 break;
13139
13140 case 'q':
13141 INSERT_OPERAND (1, MQ, *ip, regno);
13142 break;
13143
13144 case 'a': /* Do nothing. */
13145 case 's': /* Do nothing. */
13146 case 't': /* Do nothing. */
13147 case 'x': /* Do nothing. */
13148 case 'y': /* Do nothing. */
13149 case 'z': /* Do nothing. */
13150 break;
13151
13152 default:
13153 abort ();
13154 }
13155 continue;
13156
13157 case 'A':
13158 {
13159 bfd_reloc_code_real_type r[3];
13160 expressionS ep;
13161 int imm;
13162
13163 /* Check whether there is only a single bracketed
13164 expression left. If so, it must be the base register
13165 and the constant must be zero. */
13166 if (*s == '(' && strchr (s + 1, '(') == 0)
13167 {
13168 INSERT_OPERAND (1, IMMA, *ip, 0);
13169 continue;
13170 }
13171
13172 if (my_getSmallExpression (&ep, r, s) > 0
13173 || !expr_const_in_range (&ep, -64, 64, 2))
13174 break;
13175
13176 imm = ep.X_add_number >> 2;
13177 INSERT_OPERAND (1, IMMA, *ip, imm);
13178 }
13179 s = expr_end;
13180 continue;
13181
13182 case 'B':
13183 {
13184 bfd_reloc_code_real_type r[3];
13185 expressionS ep;
13186 int imm;
13187
13188 if (my_getSmallExpression (&ep, r, s) > 0
13189 || ep.X_op != O_constant)
13190 break;
13191
13192 for (imm = 0; imm < 8; imm++)
13193 if (micromips_imm_b_map[imm] == ep.X_add_number)
13194 break;
13195 if (imm >= 8)
13196 break;
13197
13198 INSERT_OPERAND (1, IMMB, *ip, imm);
13199 }
13200 s = expr_end;
13201 continue;
13202
13203 case 'C':
13204 {
13205 bfd_reloc_code_real_type r[3];
13206 expressionS ep;
13207 int imm;
13208
13209 if (my_getSmallExpression (&ep, r, s) > 0
13210 || ep.X_op != O_constant)
13211 break;
13212
13213 for (imm = 0; imm < 16; imm++)
13214 if (micromips_imm_c_map[imm] == ep.X_add_number)
13215 break;
13216 if (imm >= 16)
13217 break;
13218
13219 INSERT_OPERAND (1, IMMC, *ip, imm);
13220 }
13221 s = expr_end;
13222 continue;
13223
13224 case 'D': /* pc relative offset */
13225 case 'E': /* pc relative offset */
13226 my_getExpression (&offset_expr, s);
13227 if (offset_expr.X_op == O_register)
13228 break;
13229
13230 if (!forced_insn_length)
13231 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13232 else if (c == 'D')
13233 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
13234 else
13235 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
13236 s = expr_end;
13237 continue;
13238
13239 case 'F':
13240 {
13241 bfd_reloc_code_real_type r[3];
13242 expressionS ep;
13243 int imm;
13244
13245 if (my_getSmallExpression (&ep, r, s) > 0
13246 || !expr_const_in_range (&ep, 0, 16, 0))
13247 break;
13248
13249 imm = ep.X_add_number;
13250 INSERT_OPERAND (1, IMMF, *ip, imm);
13251 }
13252 s = expr_end;
13253 continue;
13254
13255 case 'G':
13256 {
13257 bfd_reloc_code_real_type r[3];
13258 expressionS ep;
13259 int imm;
13260
13261 /* Check whether there is only a single bracketed
13262 expression left. If so, it must be the base register
13263 and the constant must be zero. */
13264 if (*s == '(' && strchr (s + 1, '(') == 0)
13265 {
13266 INSERT_OPERAND (1, IMMG, *ip, 0);
13267 continue;
13268 }
13269
13270 if (my_getSmallExpression (&ep, r, s) > 0
13271 || !expr_const_in_range (&ep, -1, 15, 0))
13272 break;
13273
13274 imm = ep.X_add_number & 15;
13275 INSERT_OPERAND (1, IMMG, *ip, imm);
13276 }
13277 s = expr_end;
13278 continue;
13279
13280 case 'H':
13281 {
13282 bfd_reloc_code_real_type r[3];
13283 expressionS ep;
13284 int imm;
13285
13286 /* Check whether there is only a single bracketed
13287 expression left. If so, it must be the base register
13288 and the constant must be zero. */
13289 if (*s == '(' && strchr (s + 1, '(') == 0)
13290 {
13291 INSERT_OPERAND (1, IMMH, *ip, 0);
13292 continue;
13293 }
13294
13295 if (my_getSmallExpression (&ep, r, s) > 0
13296 || !expr_const_in_range (&ep, 0, 16, 1))
13297 break;
13298
13299 imm = ep.X_add_number >> 1;
13300 INSERT_OPERAND (1, IMMH, *ip, imm);
13301 }
13302 s = expr_end;
13303 continue;
13304
13305 case 'I':
13306 {
13307 bfd_reloc_code_real_type r[3];
13308 expressionS ep;
13309 int imm;
13310
13311 if (my_getSmallExpression (&ep, r, s) > 0
13312 || !expr_const_in_range (&ep, -1, 127, 0))
13313 break;
13314
13315 imm = ep.X_add_number & 127;
13316 INSERT_OPERAND (1, IMMI, *ip, imm);
13317 }
13318 s = expr_end;
13319 continue;
13320
13321 case 'J':
13322 {
13323 bfd_reloc_code_real_type r[3];
13324 expressionS ep;
13325 int imm;
13326
13327 /* Check whether there is only a single bracketed
13328 expression left. If so, it must be the base register
13329 and the constant must be zero. */
13330 if (*s == '(' && strchr (s + 1, '(') == 0)
13331 {
13332 INSERT_OPERAND (1, IMMJ, *ip, 0);
13333 continue;
13334 }
13335
13336 if (my_getSmallExpression (&ep, r, s) > 0
13337 || !expr_const_in_range (&ep, 0, 16, 2))
13338 break;
13339
13340 imm = ep.X_add_number >> 2;
13341 INSERT_OPERAND (1, IMMJ, *ip, imm);
13342 }
13343 s = expr_end;
13344 continue;
13345
13346 case 'L':
13347 {
13348 bfd_reloc_code_real_type r[3];
13349 expressionS ep;
13350 int imm;
13351
13352 /* Check whether there is only a single bracketed
13353 expression left. If so, it must be the base register
13354 and the constant must be zero. */
13355 if (*s == '(' && strchr (s + 1, '(') == 0)
13356 {
13357 INSERT_OPERAND (1, IMML, *ip, 0);
13358 continue;
13359 }
13360
13361 if (my_getSmallExpression (&ep, r, s) > 0
13362 || !expr_const_in_range (&ep, 0, 16, 0))
13363 break;
13364
13365 imm = ep.X_add_number;
13366 INSERT_OPERAND (1, IMML, *ip, imm);
13367 }
13368 s = expr_end;
13369 continue;
13370
13371 case 'M':
13372 {
13373 bfd_reloc_code_real_type r[3];
13374 expressionS ep;
13375 int imm;
13376
13377 if (my_getSmallExpression (&ep, r, s) > 0
13378 || !expr_const_in_range (&ep, 1, 9, 0))
13379 break;
13380
13381 imm = ep.X_add_number & 7;
13382 INSERT_OPERAND (1, IMMM, *ip, imm);
13383 }
13384 s = expr_end;
13385 continue;
13386
13387 case 'N': /* Register list for lwm and swm. */
13388 {
13389 /* A comma-separated list of registers and/or
13390 dash-separated contiguous ranges including
13391 both ra and a set of one or more registers
13392 starting at s0 up to s3 which have to be
13393 consecutive, e.g.:
13394
13395 s0, ra
13396 s0, s1, ra, s2, s3
13397 s0-s2, ra
13398
13399 and any permutations of these. */
13400 unsigned int reglist;
13401 int imm;
13402
13403 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13404 break;
13405
13406 if ((reglist & 0xfff1ffff) != 0x80010000)
13407 break;
13408
13409 reglist = (reglist >> 17) & 7;
13410 reglist += 1;
13411 if ((reglist & -reglist) != reglist)
13412 break;
13413
13414 imm = ffs (reglist) - 1;
13415 INSERT_OPERAND (1, IMMN, *ip, imm);
13416 }
13417 continue;
13418
13419 case 'O': /* sdbbp 4-bit code. */
13420 {
13421 bfd_reloc_code_real_type r[3];
13422 expressionS ep;
13423 int imm;
13424
13425 if (my_getSmallExpression (&ep, r, s) > 0
13426 || !expr_const_in_range (&ep, 0, 16, 0))
13427 break;
13428
13429 imm = ep.X_add_number;
13430 INSERT_OPERAND (1, IMMO, *ip, imm);
13431 }
13432 s = expr_end;
13433 continue;
13434
13435 case 'P':
13436 {
13437 bfd_reloc_code_real_type r[3];
13438 expressionS ep;
13439 int imm;
13440
13441 if (my_getSmallExpression (&ep, r, s) > 0
13442 || !expr_const_in_range (&ep, 0, 32, 2))
13443 break;
13444
13445 imm = ep.X_add_number >> 2;
13446 INSERT_OPERAND (1, IMMP, *ip, imm);
13447 }
13448 s = expr_end;
13449 continue;
13450
13451 case 'Q':
13452 {
13453 bfd_reloc_code_real_type r[3];
13454 expressionS ep;
13455 int imm;
13456
13457 if (my_getSmallExpression (&ep, r, s) > 0
13458 || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13459 break;
13460
13461 imm = ep.X_add_number >> 2;
13462 INSERT_OPERAND (1, IMMQ, *ip, imm);
13463 }
13464 s = expr_end;
13465 continue;
13466
13467 case 'U':
13468 {
13469 bfd_reloc_code_real_type r[3];
13470 expressionS ep;
13471 int imm;
13472
13473 /* Check whether there is only a single bracketed
13474 expression left. If so, it must be the base register
13475 and the constant must be zero. */
13476 if (*s == '(' && strchr (s + 1, '(') == 0)
13477 {
13478 INSERT_OPERAND (1, IMMU, *ip, 0);
13479 continue;
13480 }
13481
13482 if (my_getSmallExpression (&ep, r, s) > 0
13483 || !expr_const_in_range (&ep, 0, 32, 2))
13484 break;
13485
13486 imm = ep.X_add_number >> 2;
13487 INSERT_OPERAND (1, IMMU, *ip, imm);
13488 }
13489 s = expr_end;
13490 continue;
13491
13492 case 'W':
13493 {
13494 bfd_reloc_code_real_type r[3];
13495 expressionS ep;
13496 int imm;
13497
13498 if (my_getSmallExpression (&ep, r, s) > 0
13499 || !expr_const_in_range (&ep, 0, 64, 2))
13500 break;
13501
13502 imm = ep.X_add_number >> 2;
13503 INSERT_OPERAND (1, IMMW, *ip, imm);
13504 }
13505 s = expr_end;
13506 continue;
13507
13508 case 'X':
13509 {
13510 bfd_reloc_code_real_type r[3];
13511 expressionS ep;
13512 int imm;
13513
13514 if (my_getSmallExpression (&ep, r, s) > 0
13515 || !expr_const_in_range (&ep, -8, 8, 0))
13516 break;
13517
13518 imm = ep.X_add_number;
13519 INSERT_OPERAND (1, IMMX, *ip, imm);
13520 }
13521 s = expr_end;
13522 continue;
13523
13524 case 'Y':
13525 {
13526 bfd_reloc_code_real_type r[3];
13527 expressionS ep;
13528 int imm;
13529
13530 if (my_getSmallExpression (&ep, r, s) > 0
13531 || expr_const_in_range (&ep, -2, 2, 2)
13532 || !expr_const_in_range (&ep, -258, 258, 2))
13533 break;
13534
13535 imm = ep.X_add_number >> 2;
13536 imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13537 INSERT_OPERAND (1, IMMY, *ip, imm);
13538 }
13539 s = expr_end;
13540 continue;
13541
13542 case 'Z':
13543 {
13544 bfd_reloc_code_real_type r[3];
13545 expressionS ep;
13546
13547 if (my_getSmallExpression (&ep, r, s) > 0
13548 || !expr_const_in_range (&ep, 0, 1, 0))
13549 break;
13550 }
13551 s = expr_end;
13552 continue;
13553
13554 default:
13555 as_bad (_("Internal error: bad microMIPS opcode "
13556 "(unknown extension operand type `m%c'): %s %s"),
13557 *args, insn->name, insn->args);
13558 /* Further processing is fruitless. */
13559 return;
13560 }
13561 break;
13562
13563 case 'n': /* Register list for 32-bit lwm and swm. */
13564 gas_assert (mips_opts.micromips);
13565 {
13566 /* A comma-separated list of registers and/or
13567 dash-separated contiguous ranges including
13568 at least one of ra and a set of one or more
13569 registers starting at s0 up to s7 and then
13570 s8 which have to be consecutive, e.g.:
13571
13572 ra
13573 s0
13574 ra, s0, s1, s2
13575 s0-s8
13576 s0-s5, ra
13577
13578 and any permutations of these. */
13579 unsigned int reglist;
13580 int imm;
13581 int ra;
13582
13583 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13584 break;
13585
13586 if ((reglist & 0x3f00ffff) != 0)
13587 break;
13588
13589 ra = (reglist >> 27) & 0x10;
13590 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13591 reglist += 1;
13592 if ((reglist & -reglist) != reglist)
13593 break;
13594
13595 imm = (ffs (reglist) - 1) | ra;
13596 INSERT_OPERAND (1, RT, *ip, imm);
13597 imm_expr.X_op = O_absent;
13598 }
13599 continue;
13600
13601 case '|': /* 4-bit trap code. */
13602 gas_assert (mips_opts.micromips);
13603 my_getExpression (&imm_expr, s);
13604 check_absolute_expr (ip, &imm_expr);
13605 if ((unsigned long) imm_expr.X_add_number
13606 > MICROMIPSOP_MASK_TRAP)
13607 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13608 (unsigned long) imm_expr.X_add_number,
13609 ip->insn_mo->name);
13610 INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
13611 imm_expr.X_op = O_absent;
13612 s = expr_end;
13613 continue;
13614
13615 default:
13616 as_bad (_("Bad char = '%c'\n"), *args);
13617 abort ();
13618 }
13619 break;
13620 }
13621 /* Args don't match. */
13622 s = argsStart;
13623 insn_error = _("Illegal operands");
13624 if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
13625 {
13626 ++insn;
13627 continue;
13628 }
13629 else if (wrong_delay_slot_insns && need_delay_slot_ok)
13630 {
13631 gas_assert (firstinsn);
13632 need_delay_slot_ok = FALSE;
13633 past = insn + 1;
13634 insn = firstinsn;
13635 continue;
13636 }
13637 return;
13638 }
13639 }
13640
13641 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13642
13643 /* This routine assembles an instruction into its binary format when
13644 assembling for the mips16. As a side effect, it sets one of the
13645 global variables imm_reloc or offset_reloc to the type of relocation
13646 to do if one of the operands is an address expression. It also sets
13647 forced_insn_length to the resulting instruction size in bytes if the
13648 user explicitly requested a small or extended instruction. */
13649
13650 static void
13651 mips16_ip (char *str, struct mips_cl_insn *ip)
13652 {
13653 char *s;
13654 const char *args;
13655 struct mips_opcode *insn;
13656 char *argsstart;
13657 unsigned int regno;
13658 unsigned int lastregno = 0;
13659 char *s_reset;
13660 size_t i;
13661
13662 insn_error = NULL;
13663
13664 forced_insn_length = 0;
13665
13666 for (s = str; ISLOWER (*s); ++s)
13667 ;
13668 switch (*s)
13669 {
13670 case '\0':
13671 break;
13672
13673 case ' ':
13674 *s++ = '\0';
13675 break;
13676
13677 case '.':
13678 if (s[1] == 't' && s[2] == ' ')
13679 {
13680 *s = '\0';
13681 forced_insn_length = 2;
13682 s += 3;
13683 break;
13684 }
13685 else if (s[1] == 'e' && s[2] == ' ')
13686 {
13687 *s = '\0';
13688 forced_insn_length = 4;
13689 s += 3;
13690 break;
13691 }
13692 /* Fall through. */
13693 default:
13694 insn_error = _("unknown opcode");
13695 return;
13696 }
13697
13698 if (mips_opts.noautoextend && !forced_insn_length)
13699 forced_insn_length = 2;
13700
13701 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13702 {
13703 insn_error = _("unrecognized opcode");
13704 return;
13705 }
13706
13707 argsstart = s;
13708 for (;;)
13709 {
13710 bfd_boolean ok;
13711
13712 gas_assert (strcmp (insn->name, str) == 0);
13713
13714 ok = is_opcode_valid_16 (insn);
13715 if (! ok)
13716 {
13717 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13718 && strcmp (insn->name, insn[1].name) == 0)
13719 {
13720 ++insn;
13721 continue;
13722 }
13723 else
13724 {
13725 if (!insn_error)
13726 {
13727 static char buf[100];
13728 sprintf (buf,
13729 _("Opcode not supported on this processor: %s (%s)"),
13730 mips_cpu_info_from_arch (mips_opts.arch)->name,
13731 mips_cpu_info_from_isa (mips_opts.isa)->name);
13732 insn_error = buf;
13733 }
13734 return;
13735 }
13736 }
13737
13738 create_insn (ip, insn);
13739 imm_expr.X_op = O_absent;
13740 imm_reloc[0] = BFD_RELOC_UNUSED;
13741 imm_reloc[1] = BFD_RELOC_UNUSED;
13742 imm_reloc[2] = BFD_RELOC_UNUSED;
13743 imm2_expr.X_op = O_absent;
13744 offset_expr.X_op = O_absent;
13745 offset_reloc[0] = BFD_RELOC_UNUSED;
13746 offset_reloc[1] = BFD_RELOC_UNUSED;
13747 offset_reloc[2] = BFD_RELOC_UNUSED;
13748 for (args = insn->args; 1; ++args)
13749 {
13750 int c;
13751
13752 if (*s == ' ')
13753 ++s;
13754
13755 /* In this switch statement we call break if we did not find
13756 a match, continue if we did find a match, or return if we
13757 are done. */
13758
13759 c = *args;
13760 switch (c)
13761 {
13762 case '\0':
13763 if (*s == '\0')
13764 {
13765 offsetT value;
13766
13767 /* Stuff the immediate value in now, if we can. */
13768 if (imm_expr.X_op == O_constant
13769 && *imm_reloc > BFD_RELOC_UNUSED
13770 && insn->pinfo != INSN_MACRO
13771 && calculate_reloc (*offset_reloc,
13772 imm_expr.X_add_number, &value))
13773 {
13774 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
13775 *offset_reloc, value, forced_insn_length,
13776 &ip->insn_opcode);
13777 imm_expr.X_op = O_absent;
13778 *imm_reloc = BFD_RELOC_UNUSED;
13779 *offset_reloc = BFD_RELOC_UNUSED;
13780 }
13781
13782 return;
13783 }
13784 break;
13785
13786 case ',':
13787 if (*s++ == c)
13788 continue;
13789 s--;
13790 switch (*++args)
13791 {
13792 case 'v':
13793 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13794 continue;
13795 case 'w':
13796 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13797 continue;
13798 }
13799 break;
13800
13801 case '(':
13802 case ')':
13803 if (*s++ == c)
13804 continue;
13805 break;
13806
13807 case 'v':
13808 case 'w':
13809 if (s[0] != '$')
13810 {
13811 if (c == 'v')
13812 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13813 else
13814 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13815 ++args;
13816 continue;
13817 }
13818 /* Fall through. */
13819 case 'x':
13820 case 'y':
13821 case 'z':
13822 case 'Z':
13823 case '0':
13824 case 'S':
13825 case 'R':
13826 case 'X':
13827 case 'Y':
13828 s_reset = s;
13829 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
13830 {
13831 if (c == 'v' || c == 'w')
13832 {
13833 if (c == 'v')
13834 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13835 else
13836 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13837 ++args;
13838 continue;
13839 }
13840 break;
13841 }
13842
13843 if (*s == ' ')
13844 ++s;
13845 if (args[1] != *s)
13846 {
13847 if (c == 'v' || c == 'w')
13848 {
13849 regno = mips16_to_32_reg_map[lastregno];
13850 s = s_reset;
13851 ++args;
13852 }
13853 }
13854
13855 switch (c)
13856 {
13857 case 'x':
13858 case 'y':
13859 case 'z':
13860 case 'v':
13861 case 'w':
13862 case 'Z':
13863 regno = mips32_to_16_reg_map[regno];
13864 break;
13865
13866 case '0':
13867 if (regno != 0)
13868 regno = ILLEGAL_REG;
13869 break;
13870
13871 case 'S':
13872 if (regno != SP)
13873 regno = ILLEGAL_REG;
13874 break;
13875
13876 case 'R':
13877 if (regno != RA)
13878 regno = ILLEGAL_REG;
13879 break;
13880
13881 case 'X':
13882 case 'Y':
13883 if (regno == AT && mips_opts.at)
13884 {
13885 if (mips_opts.at == ATREG)
13886 as_warn (_("used $at without \".set noat\""));
13887 else
13888 as_warn (_("used $%u with \".set at=$%u\""),
13889 regno, mips_opts.at);
13890 }
13891 break;
13892
13893 default:
13894 abort ();
13895 }
13896
13897 if (regno == ILLEGAL_REG)
13898 break;
13899
13900 switch (c)
13901 {
13902 case 'x':
13903 case 'v':
13904 MIPS16_INSERT_OPERAND (RX, *ip, regno);
13905 break;
13906 case 'y':
13907 case 'w':
13908 MIPS16_INSERT_OPERAND (RY, *ip, regno);
13909 break;
13910 case 'z':
13911 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
13912 break;
13913 case 'Z':
13914 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
13915 case '0':
13916 case 'S':
13917 case 'R':
13918 break;
13919 case 'X':
13920 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
13921 break;
13922 case 'Y':
13923 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
13924 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
13925 break;
13926 default:
13927 abort ();
13928 }
13929
13930 lastregno = regno;
13931 continue;
13932
13933 case 'P':
13934 if (strncmp (s, "$pc", 3) == 0)
13935 {
13936 s += 3;
13937 continue;
13938 }
13939 break;
13940
13941 case '5':
13942 case 'H':
13943 case 'W':
13944 case 'D':
13945 case 'j':
13946 case 'V':
13947 case 'C':
13948 case 'U':
13949 case 'k':
13950 case 'K':
13951 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
13952 if (i > 0)
13953 {
13954 if (imm_expr.X_op != O_constant)
13955 {
13956 forced_insn_length = 4;
13957 ip->insn_opcode |= MIPS16_EXTEND;
13958 }
13959 else
13960 {
13961 /* We need to relax this instruction. */
13962 *offset_reloc = *imm_reloc;
13963 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13964 }
13965 s = expr_end;
13966 continue;
13967 }
13968 *imm_reloc = BFD_RELOC_UNUSED;
13969 /* Fall through. */
13970 case '<':
13971 case '>':
13972 case '[':
13973 case ']':
13974 case '4':
13975 case '8':
13976 my_getExpression (&imm_expr, s);
13977 if (imm_expr.X_op == O_register)
13978 {
13979 /* What we thought was an expression turned out to
13980 be a register. */
13981
13982 if (s[0] == '(' && args[1] == '(')
13983 {
13984 /* It looks like the expression was omitted
13985 before a register indirection, which means
13986 that the expression is implicitly zero. We
13987 still set up imm_expr, so that we handle
13988 explicit extensions correctly. */
13989 imm_expr.X_op = O_constant;
13990 imm_expr.X_add_number = 0;
13991 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13992 continue;
13993 }
13994
13995 break;
13996 }
13997
13998 /* We need to relax this instruction. */
13999 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
14000 s = expr_end;
14001 continue;
14002
14003 case 'p':
14004 case 'q':
14005 case 'A':
14006 case 'B':
14007 case 'E':
14008 /* We use offset_reloc rather than imm_reloc for the PC
14009 relative operands. This lets macros with both
14010 immediate and address operands work correctly. */
14011 my_getExpression (&offset_expr, s);
14012
14013 if (offset_expr.X_op == O_register)
14014 break;
14015
14016 /* We need to relax this instruction. */
14017 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
14018 s = expr_end;
14019 continue;
14020
14021 case '6': /* break code */
14022 my_getExpression (&imm_expr, s);
14023 check_absolute_expr (ip, &imm_expr);
14024 if ((unsigned long) imm_expr.X_add_number > 63)
14025 as_warn (_("Invalid value for `%s' (%lu)"),
14026 ip->insn_mo->name,
14027 (unsigned long) imm_expr.X_add_number);
14028 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
14029 imm_expr.X_op = O_absent;
14030 s = expr_end;
14031 continue;
14032
14033 case 'a': /* 26 bit address */
14034 my_getExpression (&offset_expr, s);
14035 s = expr_end;
14036 *offset_reloc = BFD_RELOC_MIPS16_JMP;
14037 ip->insn_opcode <<= 16;
14038 continue;
14039
14040 case 'l': /* register list for entry macro */
14041 case 'L': /* register list for exit macro */
14042 {
14043 int mask;
14044
14045 if (c == 'l')
14046 mask = 0;
14047 else
14048 mask = 7 << 3;
14049 while (*s != '\0')
14050 {
14051 unsigned int freg, reg1, reg2;
14052
14053 while (*s == ' ' || *s == ',')
14054 ++s;
14055 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
14056 freg = 0;
14057 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
14058 freg = 1;
14059 else
14060 {
14061 as_bad (_("can't parse register list"));
14062 break;
14063 }
14064 if (*s == ' ')
14065 ++s;
14066 if (*s != '-')
14067 reg2 = reg1;
14068 else
14069 {
14070 ++s;
14071 if (!reg_lookup (&s, freg ? RTYPE_FPU
14072 : (RTYPE_GP | RTYPE_NUM), &reg2))
14073 {
14074 as_bad (_("invalid register list"));
14075 break;
14076 }
14077 }
14078 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
14079 {
14080 mask &= ~ (7 << 3);
14081 mask |= 5 << 3;
14082 }
14083 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
14084 {
14085 mask &= ~ (7 << 3);
14086 mask |= 6 << 3;
14087 }
14088 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
14089 mask |= (reg2 - 3) << 3;
14090 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
14091 mask |= (reg2 - 15) << 1;
14092 else if (reg1 == RA && reg2 == RA)
14093 mask |= 1;
14094 else
14095 {
14096 as_bad (_("invalid register list"));
14097 break;
14098 }
14099 }
14100 /* The mask is filled in in the opcode table for the
14101 benefit of the disassembler. We remove it before
14102 applying the actual mask. */
14103 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
14104 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
14105 }
14106 continue;
14107
14108 case 'm': /* Register list for save insn. */
14109 case 'M': /* Register list for restore insn. */
14110 {
14111 int opcode = ip->insn_opcode;
14112 int framesz = 0, seen_framesz = 0;
14113 int nargs = 0, statics = 0, sregs = 0;
14114
14115 while (*s != '\0')
14116 {
14117 unsigned int reg1, reg2;
14118
14119 SKIP_SPACE_TABS (s);
14120 while (*s == ',')
14121 ++s;
14122 SKIP_SPACE_TABS (s);
14123
14124 my_getExpression (&imm_expr, s);
14125 if (imm_expr.X_op == O_constant)
14126 {
14127 /* Handle the frame size. */
14128 if (seen_framesz)
14129 {
14130 as_bad (_("more than one frame size in list"));
14131 break;
14132 }
14133 seen_framesz = 1;
14134 framesz = imm_expr.X_add_number;
14135 imm_expr.X_op = O_absent;
14136 s = expr_end;
14137 continue;
14138 }
14139
14140 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
14141 {
14142 as_bad (_("can't parse register list"));
14143 break;
14144 }
14145
14146 while (*s == ' ')
14147 ++s;
14148
14149 if (*s != '-')
14150 reg2 = reg1;
14151 else
14152 {
14153 ++s;
14154 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
14155 || reg2 < reg1)
14156 {
14157 as_bad (_("can't parse register list"));
14158 break;
14159 }
14160 }
14161
14162 while (reg1 <= reg2)
14163 {
14164 if (reg1 >= 4 && reg1 <= 7)
14165 {
14166 if (!seen_framesz)
14167 /* args $a0-$a3 */
14168 nargs |= 1 << (reg1 - 4);
14169 else
14170 /* statics $a0-$a3 */
14171 statics |= 1 << (reg1 - 4);
14172 }
14173 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
14174 {
14175 /* $s0-$s8 */
14176 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
14177 }
14178 else if (reg1 == 31)
14179 {
14180 /* Add $ra to insn. */
14181 opcode |= 0x40;
14182 }
14183 else
14184 {
14185 as_bad (_("unexpected register in list"));
14186 break;
14187 }
14188 if (++reg1 == 24)
14189 reg1 = 30;
14190 }
14191 }
14192
14193 /* Encode args/statics combination. */
14194 if (nargs & statics)
14195 as_bad (_("arg/static registers overlap"));
14196 else if (nargs == 0xf)
14197 /* All $a0-$a3 are args. */
14198 opcode |= MIPS16_ALL_ARGS << 16;
14199 else if (statics == 0xf)
14200 /* All $a0-$a3 are statics. */
14201 opcode |= MIPS16_ALL_STATICS << 16;
14202 else
14203 {
14204 int narg = 0, nstat = 0;
14205
14206 /* Count arg registers. */
14207 while (nargs & 0x1)
14208 {
14209 nargs >>= 1;
14210 narg++;
14211 }
14212 if (nargs != 0)
14213 as_bad (_("invalid arg register list"));
14214
14215 /* Count static registers. */
14216 while (statics & 0x8)
14217 {
14218 statics = (statics << 1) & 0xf;
14219 nstat++;
14220 }
14221 if (statics != 0)
14222 as_bad (_("invalid static register list"));
14223
14224 /* Encode args/statics. */
14225 opcode |= ((narg << 2) | nstat) << 16;
14226 }
14227
14228 /* Encode $s0/$s1. */
14229 if (sregs & (1 << 0)) /* $s0 */
14230 opcode |= 0x20;
14231 if (sregs & (1 << 1)) /* $s1 */
14232 opcode |= 0x10;
14233 sregs >>= 2;
14234
14235 if (sregs != 0)
14236 {
14237 /* Count regs $s2-$s8. */
14238 int nsreg = 0;
14239 while (sregs & 1)
14240 {
14241 sregs >>= 1;
14242 nsreg++;
14243 }
14244 if (sregs != 0)
14245 as_bad (_("invalid static register list"));
14246 /* Encode $s2-$s8. */
14247 opcode |= nsreg << 24;
14248 }
14249
14250 /* Encode frame size. */
14251 if (!seen_framesz)
14252 as_bad (_("missing frame size"));
14253 else if ((framesz & 7) != 0 || framesz < 0
14254 || framesz > 0xff * 8)
14255 as_bad (_("invalid frame size"));
14256 else if (framesz != 128 || (opcode >> 16) != 0)
14257 {
14258 framesz /= 8;
14259 opcode |= (((framesz & 0xf0) << 16)
14260 | (framesz & 0x0f));
14261 }
14262
14263 /* Finally build the instruction. */
14264 if ((opcode >> 16) != 0 || framesz == 0)
14265 opcode |= MIPS16_EXTEND;
14266 ip->insn_opcode = opcode;
14267 }
14268 continue;
14269
14270 case 'e': /* extend code */
14271 my_getExpression (&imm_expr, s);
14272 check_absolute_expr (ip, &imm_expr);
14273 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
14274 {
14275 as_warn (_("Invalid value for `%s' (%lu)"),
14276 ip->insn_mo->name,
14277 (unsigned long) imm_expr.X_add_number);
14278 imm_expr.X_add_number &= 0x7ff;
14279 }
14280 ip->insn_opcode |= imm_expr.X_add_number;
14281 imm_expr.X_op = O_absent;
14282 s = expr_end;
14283 continue;
14284
14285 default:
14286 abort ();
14287 }
14288 break;
14289 }
14290
14291 /* Args don't match. */
14292 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14293 strcmp (insn->name, insn[1].name) == 0)
14294 {
14295 ++insn;
14296 s = argsstart;
14297 continue;
14298 }
14299
14300 insn_error = _("illegal operands");
14301
14302 return;
14303 }
14304 }
14305
14306 /* This structure holds information we know about a mips16 immediate
14307 argument type. */
14308
14309 struct mips16_immed_operand
14310 {
14311 /* The type code used in the argument string in the opcode table. */
14312 int type;
14313 /* The number of bits in the short form of the opcode. */
14314 int nbits;
14315 /* The number of bits in the extended form of the opcode. */
14316 int extbits;
14317 /* The amount by which the short form is shifted when it is used;
14318 for example, the sw instruction has a shift count of 2. */
14319 int shift;
14320 /* The amount by which the short form is shifted when it is stored
14321 into the instruction code. */
14322 int op_shift;
14323 /* Non-zero if the short form is unsigned. */
14324 int unsp;
14325 /* Non-zero if the extended form is unsigned. */
14326 int extu;
14327 /* Non-zero if the value is PC relative. */
14328 int pcrel;
14329 };
14330
14331 /* The mips16 immediate operand types. */
14332
14333 static const struct mips16_immed_operand mips16_immed_operands[] =
14334 {
14335 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14336 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14337 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14338 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14339 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14340 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14341 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14342 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14343 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14344 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14345 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14346 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14347 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14348 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14349 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14350 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14351 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14352 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14353 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14354 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14355 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14356 };
14357
14358 #define MIPS16_NUM_IMMED \
14359 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14360
14361 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14362 NBITS is the number of significant bits in VAL. */
14363
14364 static unsigned long
14365 mips16_immed_extend (offsetT val, unsigned int nbits)
14366 {
14367 int extval;
14368 if (nbits == 16)
14369 {
14370 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14371 val &= 0x1f;
14372 }
14373 else if (nbits == 15)
14374 {
14375 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14376 val &= 0xf;
14377 }
14378 else
14379 {
14380 extval = ((val & 0x1f) << 6) | (val & 0x20);
14381 val = 0;
14382 }
14383 return (extval << 16) | val;
14384 }
14385
14386 /* Install immediate value VAL into MIPS16 instruction *INSN,
14387 extending it if necessary. The instruction in *INSN may
14388 already be extended.
14389
14390 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14391 if none. In the former case, VAL is a 16-bit number with no
14392 defined signedness.
14393
14394 TYPE is the type of the immediate field. USER_INSN_LENGTH
14395 is the length that the user requested, or 0 if none. */
14396
14397 static void
14398 mips16_immed (char *file, unsigned int line, int type,
14399 bfd_reloc_code_real_type reloc, offsetT val,
14400 unsigned int user_insn_length, unsigned long *insn)
14401 {
14402 const struct mips16_immed_operand *op;
14403 int mintiny, maxtiny;
14404
14405 op = mips16_immed_operands;
14406 while (op->type != type)
14407 {
14408 ++op;
14409 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
14410 }
14411
14412 if (op->unsp)
14413 {
14414 if (type == '<' || type == '>' || type == '[' || type == ']')
14415 {
14416 mintiny = 1;
14417 maxtiny = 1 << op->nbits;
14418 }
14419 else
14420 {
14421 mintiny = 0;
14422 maxtiny = (1 << op->nbits) - 1;
14423 }
14424 if (reloc != BFD_RELOC_UNUSED)
14425 val &= 0xffff;
14426 }
14427 else
14428 {
14429 mintiny = - (1 << (op->nbits - 1));
14430 maxtiny = (1 << (op->nbits - 1)) - 1;
14431 if (reloc != BFD_RELOC_UNUSED)
14432 val = SEXT_16BIT (val);
14433 }
14434
14435 /* Branch offsets have an implicit 0 in the lowest bit. */
14436 if (type == 'p' || type == 'q')
14437 val /= 2;
14438
14439 if ((val & ((1 << op->shift) - 1)) != 0
14440 || val < (mintiny << op->shift)
14441 || val > (maxtiny << op->shift))
14442 {
14443 /* We need an extended instruction. */
14444 if (user_insn_length == 2)
14445 as_bad_where (file, line, _("invalid unextended operand value"));
14446 else
14447 *insn |= MIPS16_EXTEND;
14448 }
14449 else if (user_insn_length == 4)
14450 {
14451 /* The operand doesn't force an unextended instruction to be extended.
14452 Warn if the user wanted an extended instruction anyway. */
14453 *insn |= MIPS16_EXTEND;
14454 as_warn_where (file, line,
14455 _("extended operand requested but not required"));
14456 }
14457
14458 if (mips16_opcode_length (*insn) == 2)
14459 {
14460 int insnval;
14461
14462 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14463 insnval <<= op->op_shift;
14464 *insn |= insnval;
14465 }
14466 else
14467 {
14468 long minext, maxext;
14469
14470 if (reloc == BFD_RELOC_UNUSED)
14471 {
14472 if (op->extu)
14473 {
14474 minext = 0;
14475 maxext = (1 << op->extbits) - 1;
14476 }
14477 else
14478 {
14479 minext = - (1 << (op->extbits - 1));
14480 maxext = (1 << (op->extbits - 1)) - 1;
14481 }
14482 if (val < minext || val > maxext)
14483 as_bad_where (file, line,
14484 _("operand value out of range for instruction"));
14485 }
14486
14487 *insn |= mips16_immed_extend (val, op->extbits);
14488 }
14489 }
14490 \f
14491 struct percent_op_match
14492 {
14493 const char *str;
14494 bfd_reloc_code_real_type reloc;
14495 };
14496
14497 static const struct percent_op_match mips_percent_op[] =
14498 {
14499 {"%lo", BFD_RELOC_LO16},
14500 #ifdef OBJ_ELF
14501 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14502 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14503 {"%call16", BFD_RELOC_MIPS_CALL16},
14504 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14505 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14506 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14507 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14508 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14509 {"%got", BFD_RELOC_MIPS_GOT16},
14510 {"%gp_rel", BFD_RELOC_GPREL16},
14511 {"%half", BFD_RELOC_16},
14512 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14513 {"%higher", BFD_RELOC_MIPS_HIGHER},
14514 {"%neg", BFD_RELOC_MIPS_SUB},
14515 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14516 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14517 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14518 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14519 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14520 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14521 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14522 #endif
14523 {"%hi", BFD_RELOC_HI16_S}
14524 };
14525
14526 static const struct percent_op_match mips16_percent_op[] =
14527 {
14528 {"%lo", BFD_RELOC_MIPS16_LO16},
14529 {"%gprel", BFD_RELOC_MIPS16_GPREL},
14530 {"%got", BFD_RELOC_MIPS16_GOT16},
14531 {"%call16", BFD_RELOC_MIPS16_CALL16},
14532 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14533 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14534 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14535 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14536 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14537 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14538 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14539 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14540 };
14541
14542
14543 /* Return true if *STR points to a relocation operator. When returning true,
14544 move *STR over the operator and store its relocation code in *RELOC.
14545 Leave both *STR and *RELOC alone when returning false. */
14546
14547 static bfd_boolean
14548 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14549 {
14550 const struct percent_op_match *percent_op;
14551 size_t limit, i;
14552
14553 if (mips_opts.mips16)
14554 {
14555 percent_op = mips16_percent_op;
14556 limit = ARRAY_SIZE (mips16_percent_op);
14557 }
14558 else
14559 {
14560 percent_op = mips_percent_op;
14561 limit = ARRAY_SIZE (mips_percent_op);
14562 }
14563
14564 for (i = 0; i < limit; i++)
14565 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14566 {
14567 int len = strlen (percent_op[i].str);
14568
14569 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14570 continue;
14571
14572 *str += strlen (percent_op[i].str);
14573 *reloc = percent_op[i].reloc;
14574
14575 /* Check whether the output BFD supports this relocation.
14576 If not, issue an error and fall back on something safe. */
14577 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14578 {
14579 as_bad (_("relocation %s isn't supported by the current ABI"),
14580 percent_op[i].str);
14581 *reloc = BFD_RELOC_UNUSED;
14582 }
14583 return TRUE;
14584 }
14585 return FALSE;
14586 }
14587
14588
14589 /* Parse string STR as a 16-bit relocatable operand. Store the
14590 expression in *EP and the relocations in the array starting
14591 at RELOC. Return the number of relocation operators used.
14592
14593 On exit, EXPR_END points to the first character after the expression. */
14594
14595 static size_t
14596 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14597 char *str)
14598 {
14599 bfd_reloc_code_real_type reversed_reloc[3];
14600 size_t reloc_index, i;
14601 int crux_depth, str_depth;
14602 char *crux;
14603
14604 /* Search for the start of the main expression, recoding relocations
14605 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14606 of the main expression and with CRUX_DEPTH containing the number
14607 of open brackets at that point. */
14608 reloc_index = -1;
14609 str_depth = 0;
14610 do
14611 {
14612 reloc_index++;
14613 crux = str;
14614 crux_depth = str_depth;
14615
14616 /* Skip over whitespace and brackets, keeping count of the number
14617 of brackets. */
14618 while (*str == ' ' || *str == '\t' || *str == '(')
14619 if (*str++ == '(')
14620 str_depth++;
14621 }
14622 while (*str == '%'
14623 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14624 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14625
14626 my_getExpression (ep, crux);
14627 str = expr_end;
14628
14629 /* Match every open bracket. */
14630 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14631 if (*str++ == ')')
14632 crux_depth--;
14633
14634 if (crux_depth > 0)
14635 as_bad (_("unclosed '('"));
14636
14637 expr_end = str;
14638
14639 if (reloc_index != 0)
14640 {
14641 prev_reloc_op_frag = frag_now;
14642 for (i = 0; i < reloc_index; i++)
14643 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14644 }
14645
14646 return reloc_index;
14647 }
14648
14649 static void
14650 my_getExpression (expressionS *ep, char *str)
14651 {
14652 char *save_in;
14653
14654 save_in = input_line_pointer;
14655 input_line_pointer = str;
14656 expression (ep);
14657 expr_end = input_line_pointer;
14658 input_line_pointer = save_in;
14659 }
14660
14661 char *
14662 md_atof (int type, char *litP, int *sizeP)
14663 {
14664 return ieee_md_atof (type, litP, sizeP, target_big_endian);
14665 }
14666
14667 void
14668 md_number_to_chars (char *buf, valueT val, int n)
14669 {
14670 if (target_big_endian)
14671 number_to_chars_bigendian (buf, val, n);
14672 else
14673 number_to_chars_littleendian (buf, val, n);
14674 }
14675 \f
14676 #ifdef OBJ_ELF
14677 static int support_64bit_objects(void)
14678 {
14679 const char **list, **l;
14680 int yes;
14681
14682 list = bfd_target_list ();
14683 for (l = list; *l != NULL; l++)
14684 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14685 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14686 break;
14687 yes = (*l != NULL);
14688 free (list);
14689 return yes;
14690 }
14691 #endif /* OBJ_ELF */
14692
14693 const char *md_shortopts = "O::g::G:";
14694
14695 enum options
14696 {
14697 OPTION_MARCH = OPTION_MD_BASE,
14698 OPTION_MTUNE,
14699 OPTION_MIPS1,
14700 OPTION_MIPS2,
14701 OPTION_MIPS3,
14702 OPTION_MIPS4,
14703 OPTION_MIPS5,
14704 OPTION_MIPS32,
14705 OPTION_MIPS64,
14706 OPTION_MIPS32R2,
14707 OPTION_MIPS64R2,
14708 OPTION_MIPS16,
14709 OPTION_NO_MIPS16,
14710 OPTION_MIPS3D,
14711 OPTION_NO_MIPS3D,
14712 OPTION_MDMX,
14713 OPTION_NO_MDMX,
14714 OPTION_DSP,
14715 OPTION_NO_DSP,
14716 OPTION_MT,
14717 OPTION_NO_MT,
14718 OPTION_VIRT,
14719 OPTION_NO_VIRT,
14720 OPTION_SMARTMIPS,
14721 OPTION_NO_SMARTMIPS,
14722 OPTION_DSPR2,
14723 OPTION_NO_DSPR2,
14724 OPTION_EVA,
14725 OPTION_NO_EVA,
14726 OPTION_MICROMIPS,
14727 OPTION_NO_MICROMIPS,
14728 OPTION_MCU,
14729 OPTION_NO_MCU,
14730 OPTION_COMPAT_ARCH_BASE,
14731 OPTION_M4650,
14732 OPTION_NO_M4650,
14733 OPTION_M4010,
14734 OPTION_NO_M4010,
14735 OPTION_M4100,
14736 OPTION_NO_M4100,
14737 OPTION_M3900,
14738 OPTION_NO_M3900,
14739 OPTION_M7000_HILO_FIX,
14740 OPTION_MNO_7000_HILO_FIX,
14741 OPTION_FIX_24K,
14742 OPTION_NO_FIX_24K,
14743 OPTION_FIX_LOONGSON2F_JUMP,
14744 OPTION_NO_FIX_LOONGSON2F_JUMP,
14745 OPTION_FIX_LOONGSON2F_NOP,
14746 OPTION_NO_FIX_LOONGSON2F_NOP,
14747 OPTION_FIX_VR4120,
14748 OPTION_NO_FIX_VR4120,
14749 OPTION_FIX_VR4130,
14750 OPTION_NO_FIX_VR4130,
14751 OPTION_FIX_CN63XXP1,
14752 OPTION_NO_FIX_CN63XXP1,
14753 OPTION_TRAP,
14754 OPTION_BREAK,
14755 OPTION_EB,
14756 OPTION_EL,
14757 OPTION_FP32,
14758 OPTION_GP32,
14759 OPTION_CONSTRUCT_FLOATS,
14760 OPTION_NO_CONSTRUCT_FLOATS,
14761 OPTION_FP64,
14762 OPTION_GP64,
14763 OPTION_RELAX_BRANCH,
14764 OPTION_NO_RELAX_BRANCH,
14765 OPTION_MSHARED,
14766 OPTION_MNO_SHARED,
14767 OPTION_MSYM32,
14768 OPTION_MNO_SYM32,
14769 OPTION_SOFT_FLOAT,
14770 OPTION_HARD_FLOAT,
14771 OPTION_SINGLE_FLOAT,
14772 OPTION_DOUBLE_FLOAT,
14773 OPTION_32,
14774 #ifdef OBJ_ELF
14775 OPTION_CALL_SHARED,
14776 OPTION_CALL_NONPIC,
14777 OPTION_NON_SHARED,
14778 OPTION_XGOT,
14779 OPTION_MABI,
14780 OPTION_N32,
14781 OPTION_64,
14782 OPTION_MDEBUG,
14783 OPTION_NO_MDEBUG,
14784 OPTION_PDR,
14785 OPTION_NO_PDR,
14786 OPTION_MVXWORKS_PIC,
14787 #endif /* OBJ_ELF */
14788 OPTION_END_OF_ENUM
14789 };
14790
14791 struct option md_longopts[] =
14792 {
14793 /* Options which specify architecture. */
14794 {"march", required_argument, NULL, OPTION_MARCH},
14795 {"mtune", required_argument, NULL, OPTION_MTUNE},
14796 {"mips0", no_argument, NULL, OPTION_MIPS1},
14797 {"mips1", no_argument, NULL, OPTION_MIPS1},
14798 {"mips2", no_argument, NULL, OPTION_MIPS2},
14799 {"mips3", no_argument, NULL, OPTION_MIPS3},
14800 {"mips4", no_argument, NULL, OPTION_MIPS4},
14801 {"mips5", no_argument, NULL, OPTION_MIPS5},
14802 {"mips32", no_argument, NULL, OPTION_MIPS32},
14803 {"mips64", no_argument, NULL, OPTION_MIPS64},
14804 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
14805 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
14806
14807 /* Options which specify Application Specific Extensions (ASEs). */
14808 {"mips16", no_argument, NULL, OPTION_MIPS16},
14809 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
14810 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
14811 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
14812 {"mdmx", no_argument, NULL, OPTION_MDMX},
14813 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
14814 {"mdsp", no_argument, NULL, OPTION_DSP},
14815 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
14816 {"mmt", no_argument, NULL, OPTION_MT},
14817 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
14818 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
14819 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
14820 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
14821 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
14822 {"meva", no_argument, NULL, OPTION_EVA},
14823 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
14824 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
14825 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
14826 {"mmcu", no_argument, NULL, OPTION_MCU},
14827 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
14828 {"mvirt", no_argument, NULL, OPTION_VIRT},
14829 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
14830
14831 /* Old-style architecture options. Don't add more of these. */
14832 {"m4650", no_argument, NULL, OPTION_M4650},
14833 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
14834 {"m4010", no_argument, NULL, OPTION_M4010},
14835 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
14836 {"m4100", no_argument, NULL, OPTION_M4100},
14837 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
14838 {"m3900", no_argument, NULL, OPTION_M3900},
14839 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
14840
14841 /* Options which enable bug fixes. */
14842 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
14843 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14844 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14845 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
14846 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
14847 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
14848 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
14849 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
14850 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
14851 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
14852 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
14853 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
14854 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
14855 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
14856 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
14857
14858 /* Miscellaneous options. */
14859 {"trap", no_argument, NULL, OPTION_TRAP},
14860 {"no-break", no_argument, NULL, OPTION_TRAP},
14861 {"break", no_argument, NULL, OPTION_BREAK},
14862 {"no-trap", no_argument, NULL, OPTION_BREAK},
14863 {"EB", no_argument, NULL, OPTION_EB},
14864 {"EL", no_argument, NULL, OPTION_EL},
14865 {"mfp32", no_argument, NULL, OPTION_FP32},
14866 {"mgp32", no_argument, NULL, OPTION_GP32},
14867 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
14868 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
14869 {"mfp64", no_argument, NULL, OPTION_FP64},
14870 {"mgp64", no_argument, NULL, OPTION_GP64},
14871 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
14872 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
14873 {"mshared", no_argument, NULL, OPTION_MSHARED},
14874 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
14875 {"msym32", no_argument, NULL, OPTION_MSYM32},
14876 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
14877 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
14878 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
14879 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
14880 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
14881
14882 /* Strictly speaking this next option is ELF specific,
14883 but we allow it for other ports as well in order to
14884 make testing easier. */
14885 {"32", no_argument, NULL, OPTION_32},
14886
14887 /* ELF-specific options. */
14888 #ifdef OBJ_ELF
14889 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
14890 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
14891 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
14892 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
14893 {"xgot", no_argument, NULL, OPTION_XGOT},
14894 {"mabi", required_argument, NULL, OPTION_MABI},
14895 {"n32", no_argument, NULL, OPTION_N32},
14896 {"64", no_argument, NULL, OPTION_64},
14897 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
14898 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
14899 {"mpdr", no_argument, NULL, OPTION_PDR},
14900 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
14901 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
14902 #endif /* OBJ_ELF */
14903
14904 {NULL, no_argument, NULL, 0}
14905 };
14906 size_t md_longopts_size = sizeof (md_longopts);
14907
14908 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14909 NEW_VALUE. Warn if another value was already specified. Note:
14910 we have to defer parsing the -march and -mtune arguments in order
14911 to handle 'from-abi' correctly, since the ABI might be specified
14912 in a later argument. */
14913
14914 static void
14915 mips_set_option_string (const char **string_ptr, const char *new_value)
14916 {
14917 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14918 as_warn (_("A different %s was already specified, is now %s"),
14919 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14920 new_value);
14921
14922 *string_ptr = new_value;
14923 }
14924
14925 int
14926 md_parse_option (int c, char *arg)
14927 {
14928 switch (c)
14929 {
14930 case OPTION_CONSTRUCT_FLOATS:
14931 mips_disable_float_construction = 0;
14932 break;
14933
14934 case OPTION_NO_CONSTRUCT_FLOATS:
14935 mips_disable_float_construction = 1;
14936 break;
14937
14938 case OPTION_TRAP:
14939 mips_trap = 1;
14940 break;
14941
14942 case OPTION_BREAK:
14943 mips_trap = 0;
14944 break;
14945
14946 case OPTION_EB:
14947 target_big_endian = 1;
14948 break;
14949
14950 case OPTION_EL:
14951 target_big_endian = 0;
14952 break;
14953
14954 case 'O':
14955 if (arg == NULL)
14956 mips_optimize = 1;
14957 else if (arg[0] == '0')
14958 mips_optimize = 0;
14959 else if (arg[0] == '1')
14960 mips_optimize = 1;
14961 else
14962 mips_optimize = 2;
14963 break;
14964
14965 case 'g':
14966 if (arg == NULL)
14967 mips_debug = 2;
14968 else
14969 mips_debug = atoi (arg);
14970 break;
14971
14972 case OPTION_MIPS1:
14973 file_mips_isa = ISA_MIPS1;
14974 break;
14975
14976 case OPTION_MIPS2:
14977 file_mips_isa = ISA_MIPS2;
14978 break;
14979
14980 case OPTION_MIPS3:
14981 file_mips_isa = ISA_MIPS3;
14982 break;
14983
14984 case OPTION_MIPS4:
14985 file_mips_isa = ISA_MIPS4;
14986 break;
14987
14988 case OPTION_MIPS5:
14989 file_mips_isa = ISA_MIPS5;
14990 break;
14991
14992 case OPTION_MIPS32:
14993 file_mips_isa = ISA_MIPS32;
14994 break;
14995
14996 case OPTION_MIPS32R2:
14997 file_mips_isa = ISA_MIPS32R2;
14998 break;
14999
15000 case OPTION_MIPS64R2:
15001 file_mips_isa = ISA_MIPS64R2;
15002 break;
15003
15004 case OPTION_MIPS64:
15005 file_mips_isa = ISA_MIPS64;
15006 break;
15007
15008 case OPTION_MTUNE:
15009 mips_set_option_string (&mips_tune_string, arg);
15010 break;
15011
15012 case OPTION_MARCH:
15013 mips_set_option_string (&mips_arch_string, arg);
15014 break;
15015
15016 case OPTION_M4650:
15017 mips_set_option_string (&mips_arch_string, "4650");
15018 mips_set_option_string (&mips_tune_string, "4650");
15019 break;
15020
15021 case OPTION_NO_M4650:
15022 break;
15023
15024 case OPTION_M4010:
15025 mips_set_option_string (&mips_arch_string, "4010");
15026 mips_set_option_string (&mips_tune_string, "4010");
15027 break;
15028
15029 case OPTION_NO_M4010:
15030 break;
15031
15032 case OPTION_M4100:
15033 mips_set_option_string (&mips_arch_string, "4100");
15034 mips_set_option_string (&mips_tune_string, "4100");
15035 break;
15036
15037 case OPTION_NO_M4100:
15038 break;
15039
15040 case OPTION_M3900:
15041 mips_set_option_string (&mips_arch_string, "3900");
15042 mips_set_option_string (&mips_tune_string, "3900");
15043 break;
15044
15045 case OPTION_NO_M3900:
15046 break;
15047
15048 case OPTION_MDMX:
15049 mips_opts.ase_mdmx = 1;
15050 break;
15051
15052 case OPTION_NO_MDMX:
15053 mips_opts.ase_mdmx = 0;
15054 break;
15055
15056 case OPTION_DSP:
15057 mips_opts.ase_dsp = 1;
15058 mips_opts.ase_dspr2 = 0;
15059 break;
15060
15061 case OPTION_NO_DSP:
15062 mips_opts.ase_dsp = 0;
15063 mips_opts.ase_dspr2 = 0;
15064 break;
15065
15066 case OPTION_DSPR2:
15067 mips_opts.ase_dspr2 = 1;
15068 mips_opts.ase_dsp = 1;
15069 break;
15070
15071 case OPTION_NO_DSPR2:
15072 mips_opts.ase_dspr2 = 0;
15073 mips_opts.ase_dsp = 0;
15074 break;
15075
15076 case OPTION_EVA:
15077 mips_opts.ase_eva = 1;
15078 break;
15079
15080 case OPTION_NO_EVA:
15081 mips_opts.ase_eva = 0;
15082 break;
15083
15084 case OPTION_MT:
15085 mips_opts.ase_mt = 1;
15086 break;
15087
15088 case OPTION_NO_MT:
15089 mips_opts.ase_mt = 0;
15090 break;
15091
15092 case OPTION_MCU:
15093 mips_opts.ase_mcu = 1;
15094 break;
15095
15096 case OPTION_NO_MCU:
15097 mips_opts.ase_mcu = 0;
15098 break;
15099
15100 case OPTION_MICROMIPS:
15101 if (mips_opts.mips16 == 1)
15102 {
15103 as_bad (_("-mmicromips cannot be used with -mips16"));
15104 return 0;
15105 }
15106 mips_opts.micromips = 1;
15107 mips_no_prev_insn ();
15108 break;
15109
15110 case OPTION_NO_MICROMIPS:
15111 mips_opts.micromips = 0;
15112 mips_no_prev_insn ();
15113 break;
15114
15115 case OPTION_VIRT:
15116 mips_opts.ase_virt = 1;
15117 break;
15118
15119 case OPTION_NO_VIRT:
15120 mips_opts.ase_virt = 0;
15121 break;
15122
15123 case OPTION_MIPS16:
15124 if (mips_opts.micromips == 1)
15125 {
15126 as_bad (_("-mips16 cannot be used with -micromips"));
15127 return 0;
15128 }
15129 mips_opts.mips16 = 1;
15130 mips_no_prev_insn ();
15131 break;
15132
15133 case OPTION_NO_MIPS16:
15134 mips_opts.mips16 = 0;
15135 mips_no_prev_insn ();
15136 break;
15137
15138 case OPTION_MIPS3D:
15139 mips_opts.ase_mips3d = 1;
15140 break;
15141
15142 case OPTION_NO_MIPS3D:
15143 mips_opts.ase_mips3d = 0;
15144 break;
15145
15146 case OPTION_SMARTMIPS:
15147 mips_opts.ase_smartmips = 1;
15148 break;
15149
15150 case OPTION_NO_SMARTMIPS:
15151 mips_opts.ase_smartmips = 0;
15152 break;
15153
15154 case OPTION_FIX_24K:
15155 mips_fix_24k = 1;
15156 break;
15157
15158 case OPTION_NO_FIX_24K:
15159 mips_fix_24k = 0;
15160 break;
15161
15162 case OPTION_FIX_LOONGSON2F_JUMP:
15163 mips_fix_loongson2f_jump = TRUE;
15164 break;
15165
15166 case OPTION_NO_FIX_LOONGSON2F_JUMP:
15167 mips_fix_loongson2f_jump = FALSE;
15168 break;
15169
15170 case OPTION_FIX_LOONGSON2F_NOP:
15171 mips_fix_loongson2f_nop = TRUE;
15172 break;
15173
15174 case OPTION_NO_FIX_LOONGSON2F_NOP:
15175 mips_fix_loongson2f_nop = FALSE;
15176 break;
15177
15178 case OPTION_FIX_VR4120:
15179 mips_fix_vr4120 = 1;
15180 break;
15181
15182 case OPTION_NO_FIX_VR4120:
15183 mips_fix_vr4120 = 0;
15184 break;
15185
15186 case OPTION_FIX_VR4130:
15187 mips_fix_vr4130 = 1;
15188 break;
15189
15190 case OPTION_NO_FIX_VR4130:
15191 mips_fix_vr4130 = 0;
15192 break;
15193
15194 case OPTION_FIX_CN63XXP1:
15195 mips_fix_cn63xxp1 = TRUE;
15196 break;
15197
15198 case OPTION_NO_FIX_CN63XXP1:
15199 mips_fix_cn63xxp1 = FALSE;
15200 break;
15201
15202 case OPTION_RELAX_BRANCH:
15203 mips_relax_branch = 1;
15204 break;
15205
15206 case OPTION_NO_RELAX_BRANCH:
15207 mips_relax_branch = 0;
15208 break;
15209
15210 case OPTION_MSHARED:
15211 mips_in_shared = TRUE;
15212 break;
15213
15214 case OPTION_MNO_SHARED:
15215 mips_in_shared = FALSE;
15216 break;
15217
15218 case OPTION_MSYM32:
15219 mips_opts.sym32 = TRUE;
15220 break;
15221
15222 case OPTION_MNO_SYM32:
15223 mips_opts.sym32 = FALSE;
15224 break;
15225
15226 #ifdef OBJ_ELF
15227 /* When generating ELF code, we permit -KPIC and -call_shared to
15228 select SVR4_PIC, and -non_shared to select no PIC. This is
15229 intended to be compatible with Irix 5. */
15230 case OPTION_CALL_SHARED:
15231 if (!IS_ELF)
15232 {
15233 as_bad (_("-call_shared is supported only for ELF format"));
15234 return 0;
15235 }
15236 mips_pic = SVR4_PIC;
15237 mips_abicalls = TRUE;
15238 break;
15239
15240 case OPTION_CALL_NONPIC:
15241 if (!IS_ELF)
15242 {
15243 as_bad (_("-call_nonpic is supported only for ELF format"));
15244 return 0;
15245 }
15246 mips_pic = NO_PIC;
15247 mips_abicalls = TRUE;
15248 break;
15249
15250 case OPTION_NON_SHARED:
15251 if (!IS_ELF)
15252 {
15253 as_bad (_("-non_shared is supported only for ELF format"));
15254 return 0;
15255 }
15256 mips_pic = NO_PIC;
15257 mips_abicalls = FALSE;
15258 break;
15259
15260 /* The -xgot option tells the assembler to use 32 bit offsets
15261 when accessing the got in SVR4_PIC mode. It is for Irix
15262 compatibility. */
15263 case OPTION_XGOT:
15264 mips_big_got = 1;
15265 break;
15266 #endif /* OBJ_ELF */
15267
15268 case 'G':
15269 g_switch_value = atoi (arg);
15270 g_switch_seen = 1;
15271 break;
15272
15273 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15274 and -mabi=64. */
15275 case OPTION_32:
15276 if (IS_ELF)
15277 mips_abi = O32_ABI;
15278 /* We silently ignore -32 for non-ELF targets. This greatly
15279 simplifies the construction of the MIPS GAS test cases. */
15280 break;
15281
15282 #ifdef OBJ_ELF
15283 case OPTION_N32:
15284 if (!IS_ELF)
15285 {
15286 as_bad (_("-n32 is supported for ELF format only"));
15287 return 0;
15288 }
15289 mips_abi = N32_ABI;
15290 break;
15291
15292 case OPTION_64:
15293 if (!IS_ELF)
15294 {
15295 as_bad (_("-64 is supported for ELF format only"));
15296 return 0;
15297 }
15298 mips_abi = N64_ABI;
15299 if (!support_64bit_objects())
15300 as_fatal (_("No compiled in support for 64 bit object file format"));
15301 break;
15302 #endif /* OBJ_ELF */
15303
15304 case OPTION_GP32:
15305 file_mips_gp32 = 1;
15306 break;
15307
15308 case OPTION_GP64:
15309 file_mips_gp32 = 0;
15310 break;
15311
15312 case OPTION_FP32:
15313 file_mips_fp32 = 1;
15314 break;
15315
15316 case OPTION_FP64:
15317 file_mips_fp32 = 0;
15318 break;
15319
15320 case OPTION_SINGLE_FLOAT:
15321 file_mips_single_float = 1;
15322 break;
15323
15324 case OPTION_DOUBLE_FLOAT:
15325 file_mips_single_float = 0;
15326 break;
15327
15328 case OPTION_SOFT_FLOAT:
15329 file_mips_soft_float = 1;
15330 break;
15331
15332 case OPTION_HARD_FLOAT:
15333 file_mips_soft_float = 0;
15334 break;
15335
15336 #ifdef OBJ_ELF
15337 case OPTION_MABI:
15338 if (!IS_ELF)
15339 {
15340 as_bad (_("-mabi is supported for ELF format only"));
15341 return 0;
15342 }
15343 if (strcmp (arg, "32") == 0)
15344 mips_abi = O32_ABI;
15345 else if (strcmp (arg, "o64") == 0)
15346 mips_abi = O64_ABI;
15347 else if (strcmp (arg, "n32") == 0)
15348 mips_abi = N32_ABI;
15349 else if (strcmp (arg, "64") == 0)
15350 {
15351 mips_abi = N64_ABI;
15352 if (! support_64bit_objects())
15353 as_fatal (_("No compiled in support for 64 bit object file "
15354 "format"));
15355 }
15356 else if (strcmp (arg, "eabi") == 0)
15357 mips_abi = EABI_ABI;
15358 else
15359 {
15360 as_fatal (_("invalid abi -mabi=%s"), arg);
15361 return 0;
15362 }
15363 break;
15364 #endif /* OBJ_ELF */
15365
15366 case OPTION_M7000_HILO_FIX:
15367 mips_7000_hilo_fix = TRUE;
15368 break;
15369
15370 case OPTION_MNO_7000_HILO_FIX:
15371 mips_7000_hilo_fix = FALSE;
15372 break;
15373
15374 #ifdef OBJ_ELF
15375 case OPTION_MDEBUG:
15376 mips_flag_mdebug = TRUE;
15377 break;
15378
15379 case OPTION_NO_MDEBUG:
15380 mips_flag_mdebug = FALSE;
15381 break;
15382
15383 case OPTION_PDR:
15384 mips_flag_pdr = TRUE;
15385 break;
15386
15387 case OPTION_NO_PDR:
15388 mips_flag_pdr = FALSE;
15389 break;
15390
15391 case OPTION_MVXWORKS_PIC:
15392 mips_pic = VXWORKS_PIC;
15393 break;
15394 #endif /* OBJ_ELF */
15395
15396 default:
15397 return 0;
15398 }
15399
15400 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15401
15402 return 1;
15403 }
15404 \f
15405 /* Set up globals to generate code for the ISA or processor
15406 described by INFO. */
15407
15408 static void
15409 mips_set_architecture (const struct mips_cpu_info *info)
15410 {
15411 if (info != 0)
15412 {
15413 file_mips_arch = info->cpu;
15414 mips_opts.arch = info->cpu;
15415 mips_opts.isa = info->isa;
15416 }
15417 }
15418
15419
15420 /* Likewise for tuning. */
15421
15422 static void
15423 mips_set_tune (const struct mips_cpu_info *info)
15424 {
15425 if (info != 0)
15426 mips_tune = info->cpu;
15427 }
15428
15429
15430 void
15431 mips_after_parse_args (void)
15432 {
15433 const struct mips_cpu_info *arch_info = 0;
15434 const struct mips_cpu_info *tune_info = 0;
15435
15436 /* GP relative stuff not working for PE */
15437 if (strncmp (TARGET_OS, "pe", 2) == 0)
15438 {
15439 if (g_switch_seen && g_switch_value != 0)
15440 as_bad (_("-G not supported in this configuration."));
15441 g_switch_value = 0;
15442 }
15443
15444 if (mips_abi == NO_ABI)
15445 mips_abi = MIPS_DEFAULT_ABI;
15446
15447 /* The following code determines the architecture and register size.
15448 Similar code was added to GCC 3.3 (see override_options() in
15449 config/mips/mips.c). The GAS and GCC code should be kept in sync
15450 as much as possible. */
15451
15452 if (mips_arch_string != 0)
15453 arch_info = mips_parse_cpu ("-march", mips_arch_string);
15454
15455 if (file_mips_isa != ISA_UNKNOWN)
15456 {
15457 /* Handle -mipsN. At this point, file_mips_isa contains the
15458 ISA level specified by -mipsN, while arch_info->isa contains
15459 the -march selection (if any). */
15460 if (arch_info != 0)
15461 {
15462 /* -march takes precedence over -mipsN, since it is more descriptive.
15463 There's no harm in specifying both as long as the ISA levels
15464 are the same. */
15465 if (file_mips_isa != arch_info->isa)
15466 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15467 mips_cpu_info_from_isa (file_mips_isa)->name,
15468 mips_cpu_info_from_isa (arch_info->isa)->name);
15469 }
15470 else
15471 arch_info = mips_cpu_info_from_isa (file_mips_isa);
15472 }
15473
15474 if (arch_info == 0)
15475 {
15476 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15477 gas_assert (arch_info);
15478 }
15479
15480 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15481 as_bad (_("-march=%s is not compatible with the selected ABI"),
15482 arch_info->name);
15483
15484 mips_set_architecture (arch_info);
15485
15486 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
15487 if (mips_tune_string != 0)
15488 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15489
15490 if (tune_info == 0)
15491 mips_set_tune (arch_info);
15492 else
15493 mips_set_tune (tune_info);
15494
15495 if (file_mips_gp32 >= 0)
15496 {
15497 /* The user specified the size of the integer registers. Make sure
15498 it agrees with the ABI and ISA. */
15499 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15500 as_bad (_("-mgp64 used with a 32-bit processor"));
15501 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15502 as_bad (_("-mgp32 used with a 64-bit ABI"));
15503 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15504 as_bad (_("-mgp64 used with a 32-bit ABI"));
15505 }
15506 else
15507 {
15508 /* Infer the integer register size from the ABI and processor.
15509 Restrict ourselves to 32-bit registers if that's all the
15510 processor has, or if the ABI cannot handle 64-bit registers. */
15511 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15512 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
15513 }
15514
15515 switch (file_mips_fp32)
15516 {
15517 default:
15518 case -1:
15519 /* No user specified float register size.
15520 ??? GAS treats single-float processors as though they had 64-bit
15521 float registers (although it complains when double-precision
15522 instructions are used). As things stand, saying they have 32-bit
15523 registers would lead to spurious "register must be even" messages.
15524 So here we assume float registers are never smaller than the
15525 integer ones. */
15526 if (file_mips_gp32 == 0)
15527 /* 64-bit integer registers implies 64-bit float registers. */
15528 file_mips_fp32 = 0;
15529 else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
15530 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15531 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
15532 file_mips_fp32 = 0;
15533 else
15534 /* 32-bit float registers. */
15535 file_mips_fp32 = 1;
15536 break;
15537
15538 /* The user specified the size of the float registers. Check if it
15539 agrees with the ABI and ISA. */
15540 case 0:
15541 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15542 as_bad (_("-mfp64 used with a 32-bit fpu"));
15543 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15544 && !ISA_HAS_MXHC1 (mips_opts.isa))
15545 as_warn (_("-mfp64 used with a 32-bit ABI"));
15546 break;
15547 case 1:
15548 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15549 as_warn (_("-mfp32 used with a 64-bit ABI"));
15550 break;
15551 }
15552
15553 /* End of GCC-shared inference code. */
15554
15555 /* This flag is set when we have a 64-bit capable CPU but use only
15556 32-bit wide registers. Note that EABI does not use it. */
15557 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15558 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15559 || mips_abi == O32_ABI))
15560 mips_32bitmode = 1;
15561
15562 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15563 as_bad (_("trap exception not supported at ISA 1"));
15564
15565 /* If the selected architecture includes support for ASEs, enable
15566 generation of code for them. */
15567 if (mips_opts.mips16 == -1)
15568 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
15569 if (mips_opts.micromips == -1)
15570 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
15571 if (mips_opts.ase_mips3d == -1)
15572 mips_opts.ase_mips3d = ((arch_info->ase & ASE_MIPS3D)
15573 && file_mips_fp32 == 0) ? 1 : 0;
15574 if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
15575 as_bad (_("-mfp32 used with -mips3d"));
15576
15577 if (mips_opts.ase_mdmx == -1)
15578 mips_opts.ase_mdmx = ((arch_info->ase & ASE_MDMX)
15579 && file_mips_fp32 == 0) ? 1 : 0;
15580 if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
15581 as_bad (_("-mfp32 used with -mdmx"));
15582
15583 if (mips_opts.ase_smartmips == -1)
15584 mips_opts.ase_smartmips = (arch_info->ase & ASE_SMARTMIPS) ? 1 : 0;
15585 if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
15586 as_warn (_("%s ISA does not support SmartMIPS"),
15587 mips_cpu_info_from_isa (mips_opts.isa)->name);
15588
15589 if (mips_opts.ase_dsp == -1)
15590 mips_opts.ase_dsp = (arch_info->ase & ASE_DSP) ? 1 : 0;
15591 if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
15592 as_warn (_("%s ISA does not support DSP ASE"),
15593 mips_cpu_info_from_isa (mips_opts.isa)->name);
15594
15595 if (mips_opts.ase_dspr2 == -1)
15596 {
15597 mips_opts.ase_dspr2 = (arch_info->ase & ASE_DSPR2) ? 1 : 0;
15598 mips_opts.ase_dsp = (arch_info->ase & ASE_DSP) ? 1 : 0;
15599 }
15600 if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
15601 as_warn (_("%s ISA does not support DSP R2 ASE"),
15602 mips_cpu_info_from_isa (mips_opts.isa)->name);
15603
15604 if (mips_opts.ase_eva == -1)
15605 mips_opts.ase_eva = (arch_info->ase & ASE_EVA) ? 1 : 0;
15606 if (mips_opts.ase_eva && !ISA_SUPPORTS_EVA_ASE)
15607 as_warn (_("%s ISA does not support EVA ASE"),
15608 mips_cpu_info_from_isa (mips_opts.isa)->name);
15609
15610 if (mips_opts.ase_mt == -1)
15611 mips_opts.ase_mt = (arch_info->ase & ASE_MT) ? 1 : 0;
15612 if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
15613 as_warn (_("%s ISA does not support MT ASE"),
15614 mips_cpu_info_from_isa (mips_opts.isa)->name);
15615
15616 if (mips_opts.ase_mcu == -1)
15617 mips_opts.ase_mcu = (arch_info->ase & ASE_MCU) ? 1 : 0;
15618 if (mips_opts.ase_mcu && !ISA_SUPPORTS_MCU_ASE)
15619 as_warn (_("%s ISA does not support MCU ASE"),
15620 mips_cpu_info_from_isa (mips_opts.isa)->name);
15621
15622 if (mips_opts.ase_virt == -1)
15623 mips_opts.ase_virt = (arch_info->ase & ASE_VIRT) ? 1 : 0;
15624 if (mips_opts.ase_virt && !ISA_SUPPORTS_VIRT_ASE)
15625 as_warn (_("%s ISA does not support Virtualization ASE"),
15626 mips_cpu_info_from_isa (mips_opts.isa)->name);
15627
15628 file_mips_isa = mips_opts.isa;
15629 file_ase_mips3d = mips_opts.ase_mips3d;
15630 file_ase_mdmx = mips_opts.ase_mdmx;
15631 file_ase_smartmips = mips_opts.ase_smartmips;
15632 file_ase_dsp = mips_opts.ase_dsp;
15633 file_ase_dspr2 = mips_opts.ase_dspr2;
15634 file_ase_eva = mips_opts.ase_eva;
15635 file_ase_mt = mips_opts.ase_mt;
15636 file_ase_virt = mips_opts.ase_virt;
15637 mips_opts.gp32 = file_mips_gp32;
15638 mips_opts.fp32 = file_mips_fp32;
15639 mips_opts.soft_float = file_mips_soft_float;
15640 mips_opts.single_float = file_mips_single_float;
15641
15642 if (mips_flag_mdebug < 0)
15643 {
15644 #ifdef OBJ_MAYBE_ECOFF
15645 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
15646 mips_flag_mdebug = 1;
15647 else
15648 #endif /* OBJ_MAYBE_ECOFF */
15649 mips_flag_mdebug = 0;
15650 }
15651 }
15652 \f
15653 void
15654 mips_init_after_args (void)
15655 {
15656 /* initialize opcodes */
15657 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15658 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15659 }
15660
15661 long
15662 md_pcrel_from (fixS *fixP)
15663 {
15664 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15665 switch (fixP->fx_r_type)
15666 {
15667 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15668 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15669 /* Return the address of the delay slot. */
15670 return addr + 2;
15671
15672 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15673 case BFD_RELOC_MICROMIPS_JMP:
15674 case BFD_RELOC_16_PCREL_S2:
15675 case BFD_RELOC_MIPS_JMP:
15676 /* Return the address of the delay slot. */
15677 return addr + 4;
15678
15679 case BFD_RELOC_32_PCREL:
15680 return addr;
15681
15682 default:
15683 /* We have no relocation type for PC relative MIPS16 instructions. */
15684 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15685 as_bad_where (fixP->fx_file, fixP->fx_line,
15686 _("PC relative MIPS16 instruction references a different section"));
15687 return addr;
15688 }
15689 }
15690
15691 /* This is called before the symbol table is processed. In order to
15692 work with gcc when using mips-tfile, we must keep all local labels.
15693 However, in other cases, we want to discard them. If we were
15694 called with -g, but we didn't see any debugging information, it may
15695 mean that gcc is smuggling debugging information through to
15696 mips-tfile, in which case we must generate all local labels. */
15697
15698 void
15699 mips_frob_file_before_adjust (void)
15700 {
15701 #ifndef NO_ECOFF_DEBUGGING
15702 if (ECOFF_DEBUGGING
15703 && mips_debug != 0
15704 && ! ecoff_debugging_seen)
15705 flag_keep_locals = 1;
15706 #endif
15707 }
15708
15709 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15710 the corresponding LO16 reloc. This is called before md_apply_fix and
15711 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15712 relocation operators.
15713
15714 For our purposes, a %lo() expression matches a %got() or %hi()
15715 expression if:
15716
15717 (a) it refers to the same symbol; and
15718 (b) the offset applied in the %lo() expression is no lower than
15719 the offset applied in the %got() or %hi().
15720
15721 (b) allows us to cope with code like:
15722
15723 lui $4,%hi(foo)
15724 lh $4,%lo(foo+2)($4)
15725
15726 ...which is legal on RELA targets, and has a well-defined behaviour
15727 if the user knows that adding 2 to "foo" will not induce a carry to
15728 the high 16 bits.
15729
15730 When several %lo()s match a particular %got() or %hi(), we use the
15731 following rules to distinguish them:
15732
15733 (1) %lo()s with smaller offsets are a better match than %lo()s with
15734 higher offsets.
15735
15736 (2) %lo()s with no matching %got() or %hi() are better than those
15737 that already have a matching %got() or %hi().
15738
15739 (3) later %lo()s are better than earlier %lo()s.
15740
15741 These rules are applied in order.
15742
15743 (1) means, among other things, that %lo()s with identical offsets are
15744 chosen if they exist.
15745
15746 (2) means that we won't associate several high-part relocations with
15747 the same low-part relocation unless there's no alternative. Having
15748 several high parts for the same low part is a GNU extension; this rule
15749 allows careful users to avoid it.
15750
15751 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15752 with the last high-part relocation being at the front of the list.
15753 It therefore makes sense to choose the last matching low-part
15754 relocation, all other things being equal. It's also easier
15755 to code that way. */
15756
15757 void
15758 mips_frob_file (void)
15759 {
15760 struct mips_hi_fixup *l;
15761 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15762
15763 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15764 {
15765 segment_info_type *seginfo;
15766 bfd_boolean matched_lo_p;
15767 fixS **hi_pos, **lo_pos, **pos;
15768
15769 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15770
15771 /* If a GOT16 relocation turns out to be against a global symbol,
15772 there isn't supposed to be a matching LO. Ignore %gots against
15773 constants; we'll report an error for those later. */
15774 if (got16_reloc_p (l->fixp->fx_r_type)
15775 && !(l->fixp->fx_addsy
15776 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
15777 continue;
15778
15779 /* Check quickly whether the next fixup happens to be a matching %lo. */
15780 if (fixup_has_matching_lo_p (l->fixp))
15781 continue;
15782
15783 seginfo = seg_info (l->seg);
15784
15785 /* Set HI_POS to the position of this relocation in the chain.
15786 Set LO_POS to the position of the chosen low-part relocation.
15787 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15788 relocation that matches an immediately-preceding high-part
15789 relocation. */
15790 hi_pos = NULL;
15791 lo_pos = NULL;
15792 matched_lo_p = FALSE;
15793 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15794
15795 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15796 {
15797 if (*pos == l->fixp)
15798 hi_pos = pos;
15799
15800 if ((*pos)->fx_r_type == looking_for_rtype
15801 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15802 && (*pos)->fx_offset >= l->fixp->fx_offset
15803 && (lo_pos == NULL
15804 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15805 || (!matched_lo_p
15806 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15807 lo_pos = pos;
15808
15809 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15810 && fixup_has_matching_lo_p (*pos));
15811 }
15812
15813 /* If we found a match, remove the high-part relocation from its
15814 current position and insert it before the low-part relocation.
15815 Make the offsets match so that fixup_has_matching_lo_p()
15816 will return true.
15817
15818 We don't warn about unmatched high-part relocations since some
15819 versions of gcc have been known to emit dead "lui ...%hi(...)"
15820 instructions. */
15821 if (lo_pos != NULL)
15822 {
15823 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15824 if (l->fixp->fx_next != *lo_pos)
15825 {
15826 *hi_pos = l->fixp->fx_next;
15827 l->fixp->fx_next = *lo_pos;
15828 *lo_pos = l->fixp;
15829 }
15830 }
15831 }
15832 }
15833
15834 int
15835 mips_force_relocation (fixS *fixp)
15836 {
15837 if (generic_force_reloc (fixp))
15838 return 1;
15839
15840 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15841 so that the linker relaxation can update targets. */
15842 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15843 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15844 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15845 return 1;
15846
15847 return 0;
15848 }
15849
15850 /* Read the instruction associated with RELOC from BUF. */
15851
15852 static unsigned int
15853 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15854 {
15855 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15856 return read_compressed_insn (buf, 4);
15857 else
15858 return read_insn (buf);
15859 }
15860
15861 /* Write instruction INSN to BUF, given that it has been relocated
15862 by RELOC. */
15863
15864 static void
15865 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15866 unsigned long insn)
15867 {
15868 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15869 write_compressed_insn (buf, insn, 4);
15870 else
15871 write_insn (buf, insn);
15872 }
15873
15874 /* Apply a fixup to the object file. */
15875
15876 void
15877 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15878 {
15879 char *buf;
15880 unsigned long insn;
15881 reloc_howto_type *howto;
15882
15883 /* We ignore generic BFD relocations we don't know about. */
15884 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15885 if (! howto)
15886 return;
15887
15888 gas_assert (fixP->fx_size == 2
15889 || fixP->fx_size == 4
15890 || fixP->fx_r_type == BFD_RELOC_16
15891 || fixP->fx_r_type == BFD_RELOC_64
15892 || fixP->fx_r_type == BFD_RELOC_CTOR
15893 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15894 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15895 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15896 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15897 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
15898
15899 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15900
15901 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15902 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15903 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15904 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
15905 || fixP->fx_r_type == BFD_RELOC_32_PCREL);
15906
15907 /* Don't treat parts of a composite relocation as done. There are two
15908 reasons for this:
15909
15910 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15911 should nevertheless be emitted if the first part is.
15912
15913 (2) In normal usage, composite relocations are never assembly-time
15914 constants. The easiest way of dealing with the pathological
15915 exceptions is to generate a relocation against STN_UNDEF and
15916 leave everything up to the linker. */
15917 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15918 fixP->fx_done = 1;
15919
15920 switch (fixP->fx_r_type)
15921 {
15922 case BFD_RELOC_MIPS_TLS_GD:
15923 case BFD_RELOC_MIPS_TLS_LDM:
15924 case BFD_RELOC_MIPS_TLS_DTPREL32:
15925 case BFD_RELOC_MIPS_TLS_DTPREL64:
15926 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15927 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15928 case BFD_RELOC_MIPS_TLS_GOTTPREL:
15929 case BFD_RELOC_MIPS_TLS_TPREL32:
15930 case BFD_RELOC_MIPS_TLS_TPREL64:
15931 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15932 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15933 case BFD_RELOC_MICROMIPS_TLS_GD:
15934 case BFD_RELOC_MICROMIPS_TLS_LDM:
15935 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15936 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15937 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15938 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15939 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15940 case BFD_RELOC_MIPS16_TLS_GD:
15941 case BFD_RELOC_MIPS16_TLS_LDM:
15942 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15943 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15944 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15945 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15946 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15947 if (!fixP->fx_addsy)
15948 {
15949 as_bad_where (fixP->fx_file, fixP->fx_line,
15950 _("TLS relocation against a constant"));
15951 break;
15952 }
15953 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15954 /* fall through */
15955
15956 case BFD_RELOC_MIPS_JMP:
15957 case BFD_RELOC_MIPS_SHIFT5:
15958 case BFD_RELOC_MIPS_SHIFT6:
15959 case BFD_RELOC_MIPS_GOT_DISP:
15960 case BFD_RELOC_MIPS_GOT_PAGE:
15961 case BFD_RELOC_MIPS_GOT_OFST:
15962 case BFD_RELOC_MIPS_SUB:
15963 case BFD_RELOC_MIPS_INSERT_A:
15964 case BFD_RELOC_MIPS_INSERT_B:
15965 case BFD_RELOC_MIPS_DELETE:
15966 case BFD_RELOC_MIPS_HIGHEST:
15967 case BFD_RELOC_MIPS_HIGHER:
15968 case BFD_RELOC_MIPS_SCN_DISP:
15969 case BFD_RELOC_MIPS_REL16:
15970 case BFD_RELOC_MIPS_RELGOT:
15971 case BFD_RELOC_MIPS_JALR:
15972 case BFD_RELOC_HI16:
15973 case BFD_RELOC_HI16_S:
15974 case BFD_RELOC_LO16:
15975 case BFD_RELOC_GPREL16:
15976 case BFD_RELOC_MIPS_LITERAL:
15977 case BFD_RELOC_MIPS_CALL16:
15978 case BFD_RELOC_MIPS_GOT16:
15979 case BFD_RELOC_GPREL32:
15980 case BFD_RELOC_MIPS_GOT_HI16:
15981 case BFD_RELOC_MIPS_GOT_LO16:
15982 case BFD_RELOC_MIPS_CALL_HI16:
15983 case BFD_RELOC_MIPS_CALL_LO16:
15984 case BFD_RELOC_MIPS16_GPREL:
15985 case BFD_RELOC_MIPS16_GOT16:
15986 case BFD_RELOC_MIPS16_CALL16:
15987 case BFD_RELOC_MIPS16_HI16:
15988 case BFD_RELOC_MIPS16_HI16_S:
15989 case BFD_RELOC_MIPS16_LO16:
15990 case BFD_RELOC_MIPS16_JMP:
15991 case BFD_RELOC_MICROMIPS_JMP:
15992 case BFD_RELOC_MICROMIPS_GOT_DISP:
15993 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15994 case BFD_RELOC_MICROMIPS_GOT_OFST:
15995 case BFD_RELOC_MICROMIPS_SUB:
15996 case BFD_RELOC_MICROMIPS_HIGHEST:
15997 case BFD_RELOC_MICROMIPS_HIGHER:
15998 case BFD_RELOC_MICROMIPS_SCN_DISP:
15999 case BFD_RELOC_MICROMIPS_JALR:
16000 case BFD_RELOC_MICROMIPS_HI16:
16001 case BFD_RELOC_MICROMIPS_HI16_S:
16002 case BFD_RELOC_MICROMIPS_LO16:
16003 case BFD_RELOC_MICROMIPS_GPREL16:
16004 case BFD_RELOC_MICROMIPS_LITERAL:
16005 case BFD_RELOC_MICROMIPS_CALL16:
16006 case BFD_RELOC_MICROMIPS_GOT16:
16007 case BFD_RELOC_MICROMIPS_GOT_HI16:
16008 case BFD_RELOC_MICROMIPS_GOT_LO16:
16009 case BFD_RELOC_MICROMIPS_CALL_HI16:
16010 case BFD_RELOC_MICROMIPS_CALL_LO16:
16011 case BFD_RELOC_MIPS_EH:
16012 if (fixP->fx_done)
16013 {
16014 offsetT value;
16015
16016 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
16017 {
16018 insn = read_reloc_insn (buf, fixP->fx_r_type);
16019 if (mips16_reloc_p (fixP->fx_r_type))
16020 insn |= mips16_immed_extend (value, 16);
16021 else
16022 insn |= (value & 0xffff);
16023 write_reloc_insn (buf, fixP->fx_r_type, insn);
16024 }
16025 else
16026 as_bad_where (fixP->fx_file, fixP->fx_line,
16027 _("Unsupported constant in relocation"));
16028 }
16029 break;
16030
16031 case BFD_RELOC_64:
16032 /* This is handled like BFD_RELOC_32, but we output a sign
16033 extended value if we are only 32 bits. */
16034 if (fixP->fx_done)
16035 {
16036 if (8 <= sizeof (valueT))
16037 md_number_to_chars (buf, *valP, 8);
16038 else
16039 {
16040 valueT hiv;
16041
16042 if ((*valP & 0x80000000) != 0)
16043 hiv = 0xffffffff;
16044 else
16045 hiv = 0;
16046 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
16047 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
16048 }
16049 }
16050 break;
16051
16052 case BFD_RELOC_RVA:
16053 case BFD_RELOC_32:
16054 case BFD_RELOC_32_PCREL:
16055 case BFD_RELOC_16:
16056 /* If we are deleting this reloc entry, we must fill in the
16057 value now. This can happen if we have a .word which is not
16058 resolved when it appears but is later defined. */
16059 if (fixP->fx_done)
16060 md_number_to_chars (buf, *valP, fixP->fx_size);
16061 break;
16062
16063 case BFD_RELOC_16_PCREL_S2:
16064 if ((*valP & 0x3) != 0)
16065 as_bad_where (fixP->fx_file, fixP->fx_line,
16066 _("Branch to misaligned address (%lx)"), (long) *valP);
16067
16068 /* We need to save the bits in the instruction since fixup_segment()
16069 might be deleting the relocation entry (i.e., a branch within
16070 the current segment). */
16071 if (! fixP->fx_done)
16072 break;
16073
16074 /* Update old instruction data. */
16075 insn = read_insn (buf);
16076
16077 if (*valP + 0x20000 <= 0x3ffff)
16078 {
16079 insn |= (*valP >> 2) & 0xffff;
16080 write_insn (buf, insn);
16081 }
16082 else if (mips_pic == NO_PIC
16083 && fixP->fx_done
16084 && fixP->fx_frag->fr_address >= text_section->vma
16085 && (fixP->fx_frag->fr_address
16086 < text_section->vma + bfd_get_section_size (text_section))
16087 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
16088 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
16089 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
16090 {
16091 /* The branch offset is too large. If this is an
16092 unconditional branch, and we are not generating PIC code,
16093 we can convert it to an absolute jump instruction. */
16094 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
16095 insn = 0x0c000000; /* jal */
16096 else
16097 insn = 0x08000000; /* j */
16098 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
16099 fixP->fx_done = 0;
16100 fixP->fx_addsy = section_symbol (text_section);
16101 *valP += md_pcrel_from (fixP);
16102 write_insn (buf, insn);
16103 }
16104 else
16105 {
16106 /* If we got here, we have branch-relaxation disabled,
16107 and there's nothing we can do to fix this instruction
16108 without turning it into a longer sequence. */
16109 as_bad_where (fixP->fx_file, fixP->fx_line,
16110 _("Branch out of range"));
16111 }
16112 break;
16113
16114 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16115 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16116 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
16117 /* We adjust the offset back to even. */
16118 if ((*valP & 0x1) != 0)
16119 --(*valP);
16120
16121 if (! fixP->fx_done)
16122 break;
16123
16124 /* Should never visit here, because we keep the relocation. */
16125 abort ();
16126 break;
16127
16128 case BFD_RELOC_VTABLE_INHERIT:
16129 fixP->fx_done = 0;
16130 if (fixP->fx_addsy
16131 && !S_IS_DEFINED (fixP->fx_addsy)
16132 && !S_IS_WEAK (fixP->fx_addsy))
16133 S_SET_WEAK (fixP->fx_addsy);
16134 break;
16135
16136 case BFD_RELOC_VTABLE_ENTRY:
16137 fixP->fx_done = 0;
16138 break;
16139
16140 default:
16141 abort ();
16142 }
16143
16144 /* Remember value for tc_gen_reloc. */
16145 fixP->fx_addnumber = *valP;
16146 }
16147
16148 static symbolS *
16149 get_symbol (void)
16150 {
16151 int c;
16152 char *name;
16153 symbolS *p;
16154
16155 name = input_line_pointer;
16156 c = get_symbol_end ();
16157 p = (symbolS *) symbol_find_or_make (name);
16158 *input_line_pointer = c;
16159 return p;
16160 }
16161
16162 /* Align the current frag to a given power of two. If a particular
16163 fill byte should be used, FILL points to an integer that contains
16164 that byte, otherwise FILL is null.
16165
16166 This function used to have the comment:
16167
16168 The MIPS assembler also automatically adjusts any preceding label.
16169
16170 The implementation therefore applied the adjustment to a maximum of
16171 one label. However, other label adjustments are applied to batches
16172 of labels, and adjusting just one caused problems when new labels
16173 were added for the sake of debugging or unwind information.
16174 We therefore adjust all preceding labels (given as LABELS) instead. */
16175
16176 static void
16177 mips_align (int to, int *fill, struct insn_label_list *labels)
16178 {
16179 mips_emit_delays ();
16180 mips_record_compressed_mode ();
16181 if (fill == NULL && subseg_text_p (now_seg))
16182 frag_align_code (to, 0);
16183 else
16184 frag_align (to, fill ? *fill : 0, 0);
16185 record_alignment (now_seg, to);
16186 mips_move_labels (labels, FALSE);
16187 }
16188
16189 /* Align to a given power of two. .align 0 turns off the automatic
16190 alignment used by the data creating pseudo-ops. */
16191
16192 static void
16193 s_align (int x ATTRIBUTE_UNUSED)
16194 {
16195 int temp, fill_value, *fill_ptr;
16196 long max_alignment = 28;
16197
16198 /* o Note that the assembler pulls down any immediately preceding label
16199 to the aligned address.
16200 o It's not documented but auto alignment is reinstated by
16201 a .align pseudo instruction.
16202 o Note also that after auto alignment is turned off the mips assembler
16203 issues an error on attempt to assemble an improperly aligned data item.
16204 We don't. */
16205
16206 temp = get_absolute_expression ();
16207 if (temp > max_alignment)
16208 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
16209 else if (temp < 0)
16210 {
16211 as_warn (_("Alignment negative: 0 assumed."));
16212 temp = 0;
16213 }
16214 if (*input_line_pointer == ',')
16215 {
16216 ++input_line_pointer;
16217 fill_value = get_absolute_expression ();
16218 fill_ptr = &fill_value;
16219 }
16220 else
16221 fill_ptr = 0;
16222 if (temp)
16223 {
16224 segment_info_type *si = seg_info (now_seg);
16225 struct insn_label_list *l = si->label_list;
16226 /* Auto alignment should be switched on by next section change. */
16227 auto_align = 1;
16228 mips_align (temp, fill_ptr, l);
16229 }
16230 else
16231 {
16232 auto_align = 0;
16233 }
16234
16235 demand_empty_rest_of_line ();
16236 }
16237
16238 static void
16239 s_change_sec (int sec)
16240 {
16241 segT seg;
16242
16243 #ifdef OBJ_ELF
16244 /* The ELF backend needs to know that we are changing sections, so
16245 that .previous works correctly. We could do something like check
16246 for an obj_section_change_hook macro, but that might be confusing
16247 as it would not be appropriate to use it in the section changing
16248 functions in read.c, since obj-elf.c intercepts those. FIXME:
16249 This should be cleaner, somehow. */
16250 if (IS_ELF)
16251 obj_elf_section_change_hook ();
16252 #endif
16253
16254 mips_emit_delays ();
16255
16256 switch (sec)
16257 {
16258 case 't':
16259 s_text (0);
16260 break;
16261 case 'd':
16262 s_data (0);
16263 break;
16264 case 'b':
16265 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16266 demand_empty_rest_of_line ();
16267 break;
16268
16269 case 'r':
16270 seg = subseg_new (RDATA_SECTION_NAME,
16271 (subsegT) get_absolute_expression ());
16272 if (IS_ELF)
16273 {
16274 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16275 | SEC_READONLY | SEC_RELOC
16276 | SEC_DATA));
16277 if (strncmp (TARGET_OS, "elf", 3) != 0)
16278 record_alignment (seg, 4);
16279 }
16280 demand_empty_rest_of_line ();
16281 break;
16282
16283 case 's':
16284 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
16285 if (IS_ELF)
16286 {
16287 bfd_set_section_flags (stdoutput, seg,
16288 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16289 if (strncmp (TARGET_OS, "elf", 3) != 0)
16290 record_alignment (seg, 4);
16291 }
16292 demand_empty_rest_of_line ();
16293 break;
16294
16295 case 'B':
16296 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16297 if (IS_ELF)
16298 {
16299 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16300 if (strncmp (TARGET_OS, "elf", 3) != 0)
16301 record_alignment (seg, 4);
16302 }
16303 demand_empty_rest_of_line ();
16304 break;
16305 }
16306
16307 auto_align = 1;
16308 }
16309
16310 void
16311 s_change_section (int ignore ATTRIBUTE_UNUSED)
16312 {
16313 #ifdef OBJ_ELF
16314 char *section_name;
16315 char c;
16316 char next_c = 0;
16317 int section_type;
16318 int section_flag;
16319 int section_entry_size;
16320 int section_alignment;
16321
16322 if (!IS_ELF)
16323 return;
16324
16325 section_name = input_line_pointer;
16326 c = get_symbol_end ();
16327 if (c)
16328 next_c = *(input_line_pointer + 1);
16329
16330 /* Do we have .section Name<,"flags">? */
16331 if (c != ',' || (c == ',' && next_c == '"'))
16332 {
16333 /* just after name is now '\0'. */
16334 *input_line_pointer = c;
16335 input_line_pointer = section_name;
16336 obj_elf_section (ignore);
16337 return;
16338 }
16339 input_line_pointer++;
16340
16341 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16342 if (c == ',')
16343 section_type = get_absolute_expression ();
16344 else
16345 section_type = 0;
16346 if (*input_line_pointer++ == ',')
16347 section_flag = get_absolute_expression ();
16348 else
16349 section_flag = 0;
16350 if (*input_line_pointer++ == ',')
16351 section_entry_size = get_absolute_expression ();
16352 else
16353 section_entry_size = 0;
16354 if (*input_line_pointer++ == ',')
16355 section_alignment = get_absolute_expression ();
16356 else
16357 section_alignment = 0;
16358 /* FIXME: really ignore? */
16359 (void) section_alignment;
16360
16361 section_name = xstrdup (section_name);
16362
16363 /* When using the generic form of .section (as implemented by obj-elf.c),
16364 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16365 traditionally had to fall back on the more common @progbits instead.
16366
16367 There's nothing really harmful in this, since bfd will correct
16368 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
16369 means that, for backwards compatibility, the special_section entries
16370 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16371
16372 Even so, we shouldn't force users of the MIPS .section syntax to
16373 incorrectly label the sections as SHT_PROGBITS. The best compromise
16374 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16375 generic type-checking code. */
16376 if (section_type == SHT_MIPS_DWARF)
16377 section_type = SHT_PROGBITS;
16378
16379 obj_elf_change_section (section_name, section_type, section_flag,
16380 section_entry_size, 0, 0, 0);
16381
16382 if (now_seg->name != section_name)
16383 free (section_name);
16384 #endif /* OBJ_ELF */
16385 }
16386
16387 void
16388 mips_enable_auto_align (void)
16389 {
16390 auto_align = 1;
16391 }
16392
16393 static void
16394 s_cons (int log_size)
16395 {
16396 segment_info_type *si = seg_info (now_seg);
16397 struct insn_label_list *l = si->label_list;
16398
16399 mips_emit_delays ();
16400 if (log_size > 0 && auto_align)
16401 mips_align (log_size, 0, l);
16402 cons (1 << log_size);
16403 mips_clear_insn_labels ();
16404 }
16405
16406 static void
16407 s_float_cons (int type)
16408 {
16409 segment_info_type *si = seg_info (now_seg);
16410 struct insn_label_list *l = si->label_list;
16411
16412 mips_emit_delays ();
16413
16414 if (auto_align)
16415 {
16416 if (type == 'd')
16417 mips_align (3, 0, l);
16418 else
16419 mips_align (2, 0, l);
16420 }
16421
16422 float_cons (type);
16423 mips_clear_insn_labels ();
16424 }
16425
16426 /* Handle .globl. We need to override it because on Irix 5 you are
16427 permitted to say
16428 .globl foo .text
16429 where foo is an undefined symbol, to mean that foo should be
16430 considered to be the address of a function. */
16431
16432 static void
16433 s_mips_globl (int x ATTRIBUTE_UNUSED)
16434 {
16435 char *name;
16436 int c;
16437 symbolS *symbolP;
16438 flagword flag;
16439
16440 do
16441 {
16442 name = input_line_pointer;
16443 c = get_symbol_end ();
16444 symbolP = symbol_find_or_make (name);
16445 S_SET_EXTERNAL (symbolP);
16446
16447 *input_line_pointer = c;
16448 SKIP_WHITESPACE ();
16449
16450 /* On Irix 5, every global symbol that is not explicitly labelled as
16451 being a function is apparently labelled as being an object. */
16452 flag = BSF_OBJECT;
16453
16454 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16455 && (*input_line_pointer != ','))
16456 {
16457 char *secname;
16458 asection *sec;
16459
16460 secname = input_line_pointer;
16461 c = get_symbol_end ();
16462 sec = bfd_get_section_by_name (stdoutput, secname);
16463 if (sec == NULL)
16464 as_bad (_("%s: no such section"), secname);
16465 *input_line_pointer = c;
16466
16467 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16468 flag = BSF_FUNCTION;
16469 }
16470
16471 symbol_get_bfdsym (symbolP)->flags |= flag;
16472
16473 c = *input_line_pointer;
16474 if (c == ',')
16475 {
16476 input_line_pointer++;
16477 SKIP_WHITESPACE ();
16478 if (is_end_of_line[(unsigned char) *input_line_pointer])
16479 c = '\n';
16480 }
16481 }
16482 while (c == ',');
16483
16484 demand_empty_rest_of_line ();
16485 }
16486
16487 static void
16488 s_option (int x ATTRIBUTE_UNUSED)
16489 {
16490 char *opt;
16491 char c;
16492
16493 opt = input_line_pointer;
16494 c = get_symbol_end ();
16495
16496 if (*opt == 'O')
16497 {
16498 /* FIXME: What does this mean? */
16499 }
16500 else if (strncmp (opt, "pic", 3) == 0)
16501 {
16502 int i;
16503
16504 i = atoi (opt + 3);
16505 if (i == 0)
16506 mips_pic = NO_PIC;
16507 else if (i == 2)
16508 {
16509 mips_pic = SVR4_PIC;
16510 mips_abicalls = TRUE;
16511 }
16512 else
16513 as_bad (_(".option pic%d not supported"), i);
16514
16515 if (mips_pic == SVR4_PIC)
16516 {
16517 if (g_switch_seen && g_switch_value != 0)
16518 as_warn (_("-G may not be used with SVR4 PIC code"));
16519 g_switch_value = 0;
16520 bfd_set_gp_size (stdoutput, 0);
16521 }
16522 }
16523 else
16524 as_warn (_("Unrecognized option \"%s\""), opt);
16525
16526 *input_line_pointer = c;
16527 demand_empty_rest_of_line ();
16528 }
16529
16530 /* This structure is used to hold a stack of .set values. */
16531
16532 struct mips_option_stack
16533 {
16534 struct mips_option_stack *next;
16535 struct mips_set_options options;
16536 };
16537
16538 static struct mips_option_stack *mips_opts_stack;
16539
16540 /* Handle the .set pseudo-op. */
16541
16542 static void
16543 s_mipsset (int x ATTRIBUTE_UNUSED)
16544 {
16545 char *name = input_line_pointer, ch;
16546
16547 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16548 ++input_line_pointer;
16549 ch = *input_line_pointer;
16550 *input_line_pointer = '\0';
16551
16552 if (strcmp (name, "reorder") == 0)
16553 {
16554 if (mips_opts.noreorder)
16555 end_noreorder ();
16556 }
16557 else if (strcmp (name, "noreorder") == 0)
16558 {
16559 if (!mips_opts.noreorder)
16560 start_noreorder ();
16561 }
16562 else if (strncmp (name, "at=", 3) == 0)
16563 {
16564 char *s = name + 3;
16565
16566 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16567 as_bad (_("Unrecognized register name `%s'"), s);
16568 }
16569 else if (strcmp (name, "at") == 0)
16570 {
16571 mips_opts.at = ATREG;
16572 }
16573 else if (strcmp (name, "noat") == 0)
16574 {
16575 mips_opts.at = ZERO;
16576 }
16577 else if (strcmp (name, "macro") == 0)
16578 {
16579 mips_opts.warn_about_macros = 0;
16580 }
16581 else if (strcmp (name, "nomacro") == 0)
16582 {
16583 if (mips_opts.noreorder == 0)
16584 as_bad (_("`noreorder' must be set before `nomacro'"));
16585 mips_opts.warn_about_macros = 1;
16586 }
16587 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16588 {
16589 mips_opts.nomove = 0;
16590 }
16591 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16592 {
16593 mips_opts.nomove = 1;
16594 }
16595 else if (strcmp (name, "bopt") == 0)
16596 {
16597 mips_opts.nobopt = 0;
16598 }
16599 else if (strcmp (name, "nobopt") == 0)
16600 {
16601 mips_opts.nobopt = 1;
16602 }
16603 else if (strcmp (name, "gp=default") == 0)
16604 mips_opts.gp32 = file_mips_gp32;
16605 else if (strcmp (name, "gp=32") == 0)
16606 mips_opts.gp32 = 1;
16607 else if (strcmp (name, "gp=64") == 0)
16608 {
16609 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
16610 as_warn (_("%s isa does not support 64-bit registers"),
16611 mips_cpu_info_from_isa (mips_opts.isa)->name);
16612 mips_opts.gp32 = 0;
16613 }
16614 else if (strcmp (name, "fp=default") == 0)
16615 mips_opts.fp32 = file_mips_fp32;
16616 else if (strcmp (name, "fp=32") == 0)
16617 mips_opts.fp32 = 1;
16618 else if (strcmp (name, "fp=64") == 0)
16619 {
16620 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
16621 as_warn (_("%s isa does not support 64-bit floating point registers"),
16622 mips_cpu_info_from_isa (mips_opts.isa)->name);
16623 mips_opts.fp32 = 0;
16624 }
16625 else if (strcmp (name, "softfloat") == 0)
16626 mips_opts.soft_float = 1;
16627 else if (strcmp (name, "hardfloat") == 0)
16628 mips_opts.soft_float = 0;
16629 else if (strcmp (name, "singlefloat") == 0)
16630 mips_opts.single_float = 1;
16631 else if (strcmp (name, "doublefloat") == 0)
16632 mips_opts.single_float = 0;
16633 else if (strcmp (name, "mips16") == 0
16634 || strcmp (name, "MIPS-16") == 0)
16635 {
16636 if (mips_opts.micromips == 1)
16637 as_fatal (_("`mips16' cannot be used with `micromips'"));
16638 mips_opts.mips16 = 1;
16639 }
16640 else if (strcmp (name, "nomips16") == 0
16641 || strcmp (name, "noMIPS-16") == 0)
16642 mips_opts.mips16 = 0;
16643 else if (strcmp (name, "micromips") == 0)
16644 {
16645 if (mips_opts.mips16 == 1)
16646 as_fatal (_("`micromips' cannot be used with `mips16'"));
16647 mips_opts.micromips = 1;
16648 }
16649 else if (strcmp (name, "nomicromips") == 0)
16650 mips_opts.micromips = 0;
16651 else if (strcmp (name, "smartmips") == 0)
16652 {
16653 if (!ISA_SUPPORTS_SMARTMIPS)
16654 as_warn (_("%s ISA does not support SmartMIPS ASE"),
16655 mips_cpu_info_from_isa (mips_opts.isa)->name);
16656 mips_opts.ase_smartmips = 1;
16657 }
16658 else if (strcmp (name, "nosmartmips") == 0)
16659 mips_opts.ase_smartmips = 0;
16660 else if (strcmp (name, "mips3d") == 0)
16661 mips_opts.ase_mips3d = 1;
16662 else if (strcmp (name, "nomips3d") == 0)
16663 mips_opts.ase_mips3d = 0;
16664 else if (strcmp (name, "mdmx") == 0)
16665 mips_opts.ase_mdmx = 1;
16666 else if (strcmp (name, "nomdmx") == 0)
16667 mips_opts.ase_mdmx = 0;
16668 else if (strcmp (name, "dsp") == 0)
16669 {
16670 if (!ISA_SUPPORTS_DSP_ASE)
16671 as_warn (_("%s ISA does not support DSP ASE"),
16672 mips_cpu_info_from_isa (mips_opts.isa)->name);
16673 mips_opts.ase_dsp = 1;
16674 mips_opts.ase_dspr2 = 0;
16675 }
16676 else if (strcmp (name, "nodsp") == 0)
16677 {
16678 mips_opts.ase_dsp = 0;
16679 mips_opts.ase_dspr2 = 0;
16680 }
16681 else if (strcmp (name, "dspr2") == 0)
16682 {
16683 if (!ISA_SUPPORTS_DSPR2_ASE)
16684 as_warn (_("%s ISA does not support DSP R2 ASE"),
16685 mips_cpu_info_from_isa (mips_opts.isa)->name);
16686 mips_opts.ase_dspr2 = 1;
16687 mips_opts.ase_dsp = 1;
16688 }
16689 else if (strcmp (name, "nodspr2") == 0)
16690 {
16691 mips_opts.ase_dspr2 = 0;
16692 mips_opts.ase_dsp = 0;
16693 }
16694 else if (strcmp (name, "eva") == 0)
16695 {
16696 if (!ISA_SUPPORTS_EVA_ASE)
16697 as_warn (_("%s ISA does not support EVA ASE"),
16698 mips_cpu_info_from_isa (mips_opts.isa)->name);
16699 mips_opts.ase_eva = 1;
16700 }
16701 else if (strcmp (name, "noeva") == 0)
16702 mips_opts.ase_eva = 0;
16703 else if (strcmp (name, "mt") == 0)
16704 {
16705 if (!ISA_SUPPORTS_MT_ASE)
16706 as_warn (_("%s ISA does not support MT ASE"),
16707 mips_cpu_info_from_isa (mips_opts.isa)->name);
16708 mips_opts.ase_mt = 1;
16709 }
16710 else if (strcmp (name, "nomt") == 0)
16711 mips_opts.ase_mt = 0;
16712 else if (strcmp (name, "mcu") == 0)
16713 mips_opts.ase_mcu = 1;
16714 else if (strcmp (name, "nomcu") == 0)
16715 mips_opts.ase_mcu = 0;
16716 else if (strcmp (name, "virt") == 0)
16717 {
16718 if (!ISA_SUPPORTS_VIRT_ASE)
16719 as_warn (_("%s ISA does not support Virtualization ASE"),
16720 mips_cpu_info_from_isa (mips_opts.isa)->name);
16721 mips_opts.ase_virt = 1;
16722 }
16723 else if (strcmp (name, "novirt") == 0)
16724 mips_opts.ase_virt = 0;
16725 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16726 {
16727 int reset = 0;
16728
16729 /* Permit the user to change the ISA and architecture on the fly.
16730 Needless to say, misuse can cause serious problems. */
16731 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16732 {
16733 reset = 1;
16734 mips_opts.isa = file_mips_isa;
16735 mips_opts.arch = file_mips_arch;
16736 }
16737 else if (strncmp (name, "arch=", 5) == 0)
16738 {
16739 const struct mips_cpu_info *p;
16740
16741 p = mips_parse_cpu("internal use", name + 5);
16742 if (!p)
16743 as_bad (_("unknown architecture %s"), name + 5);
16744 else
16745 {
16746 mips_opts.arch = p->cpu;
16747 mips_opts.isa = p->isa;
16748 }
16749 }
16750 else if (strncmp (name, "mips", 4) == 0)
16751 {
16752 const struct mips_cpu_info *p;
16753
16754 p = mips_parse_cpu("internal use", name);
16755 if (!p)
16756 as_bad (_("unknown ISA level %s"), name + 4);
16757 else
16758 {
16759 mips_opts.arch = p->cpu;
16760 mips_opts.isa = p->isa;
16761 }
16762 }
16763 else
16764 as_bad (_("unknown ISA or architecture %s"), name);
16765
16766 switch (mips_opts.isa)
16767 {
16768 case 0:
16769 break;
16770 case ISA_MIPS1:
16771 case ISA_MIPS2:
16772 case ISA_MIPS32:
16773 case ISA_MIPS32R2:
16774 mips_opts.gp32 = 1;
16775 mips_opts.fp32 = 1;
16776 break;
16777 case ISA_MIPS3:
16778 case ISA_MIPS4:
16779 case ISA_MIPS5:
16780 case ISA_MIPS64:
16781 case ISA_MIPS64R2:
16782 mips_opts.gp32 = 0;
16783 if (mips_opts.arch == CPU_R5900)
16784 {
16785 mips_opts.fp32 = 1;
16786 }
16787 else
16788 {
16789 mips_opts.fp32 = 0;
16790 }
16791 break;
16792 default:
16793 as_bad (_("unknown ISA level %s"), name + 4);
16794 break;
16795 }
16796 if (reset)
16797 {
16798 mips_opts.gp32 = file_mips_gp32;
16799 mips_opts.fp32 = file_mips_fp32;
16800 }
16801 }
16802 else if (strcmp (name, "autoextend") == 0)
16803 mips_opts.noautoextend = 0;
16804 else if (strcmp (name, "noautoextend") == 0)
16805 mips_opts.noautoextend = 1;
16806 else if (strcmp (name, "push") == 0)
16807 {
16808 struct mips_option_stack *s;
16809
16810 s = (struct mips_option_stack *) xmalloc (sizeof *s);
16811 s->next = mips_opts_stack;
16812 s->options = mips_opts;
16813 mips_opts_stack = s;
16814 }
16815 else if (strcmp (name, "pop") == 0)
16816 {
16817 struct mips_option_stack *s;
16818
16819 s = mips_opts_stack;
16820 if (s == NULL)
16821 as_bad (_(".set pop with no .set push"));
16822 else
16823 {
16824 /* If we're changing the reorder mode we need to handle
16825 delay slots correctly. */
16826 if (s->options.noreorder && ! mips_opts.noreorder)
16827 start_noreorder ();
16828 else if (! s->options.noreorder && mips_opts.noreorder)
16829 end_noreorder ();
16830
16831 mips_opts = s->options;
16832 mips_opts_stack = s->next;
16833 free (s);
16834 }
16835 }
16836 else if (strcmp (name, "sym32") == 0)
16837 mips_opts.sym32 = TRUE;
16838 else if (strcmp (name, "nosym32") == 0)
16839 mips_opts.sym32 = FALSE;
16840 else if (strchr (name, ','))
16841 {
16842 /* Generic ".set" directive; use the generic handler. */
16843 *input_line_pointer = ch;
16844 input_line_pointer = name;
16845 s_set (0);
16846 return;
16847 }
16848 else
16849 {
16850 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16851 }
16852 *input_line_pointer = ch;
16853 demand_empty_rest_of_line ();
16854 }
16855
16856 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
16857 .option pic2. It means to generate SVR4 PIC calls. */
16858
16859 static void
16860 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16861 {
16862 mips_pic = SVR4_PIC;
16863 mips_abicalls = TRUE;
16864
16865 if (g_switch_seen && g_switch_value != 0)
16866 as_warn (_("-G may not be used with SVR4 PIC code"));
16867 g_switch_value = 0;
16868
16869 bfd_set_gp_size (stdoutput, 0);
16870 demand_empty_rest_of_line ();
16871 }
16872
16873 /* Handle the .cpload pseudo-op. This is used when generating SVR4
16874 PIC code. It sets the $gp register for the function based on the
16875 function address, which is in the register named in the argument.
16876 This uses a relocation against _gp_disp, which is handled specially
16877 by the linker. The result is:
16878 lui $gp,%hi(_gp_disp)
16879 addiu $gp,$gp,%lo(_gp_disp)
16880 addu $gp,$gp,.cpload argument
16881 The .cpload argument is normally $25 == $t9.
16882
16883 The -mno-shared option changes this to:
16884 lui $gp,%hi(__gnu_local_gp)
16885 addiu $gp,$gp,%lo(__gnu_local_gp)
16886 and the argument is ignored. This saves an instruction, but the
16887 resulting code is not position independent; it uses an absolute
16888 address for __gnu_local_gp. Thus code assembled with -mno-shared
16889 can go into an ordinary executable, but not into a shared library. */
16890
16891 static void
16892 s_cpload (int ignore ATTRIBUTE_UNUSED)
16893 {
16894 expressionS ex;
16895 int reg;
16896 int in_shared;
16897
16898 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16899 .cpload is ignored. */
16900 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16901 {
16902 s_ignore (0);
16903 return;
16904 }
16905
16906 if (mips_opts.mips16)
16907 {
16908 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16909 ignore_rest_of_line ();
16910 return;
16911 }
16912
16913 /* .cpload should be in a .set noreorder section. */
16914 if (mips_opts.noreorder == 0)
16915 as_warn (_(".cpload not in noreorder section"));
16916
16917 reg = tc_get_register (0);
16918
16919 /* If we need to produce a 64-bit address, we are better off using
16920 the default instruction sequence. */
16921 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16922
16923 ex.X_op = O_symbol;
16924 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16925 "__gnu_local_gp");
16926 ex.X_op_symbol = NULL;
16927 ex.X_add_number = 0;
16928
16929 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16930 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16931
16932 mips_mark_labels ();
16933 mips_assembling_insn = TRUE;
16934
16935 macro_start ();
16936 macro_build_lui (&ex, mips_gp_register);
16937 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16938 mips_gp_register, BFD_RELOC_LO16);
16939 if (in_shared)
16940 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16941 mips_gp_register, reg);
16942 macro_end ();
16943
16944 mips_assembling_insn = FALSE;
16945 demand_empty_rest_of_line ();
16946 }
16947
16948 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16949 .cpsetup $reg1, offset|$reg2, label
16950
16951 If offset is given, this results in:
16952 sd $gp, offset($sp)
16953 lui $gp, %hi(%neg(%gp_rel(label)))
16954 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16955 daddu $gp, $gp, $reg1
16956
16957 If $reg2 is given, this results in:
16958 daddu $reg2, $gp, $0
16959 lui $gp, %hi(%neg(%gp_rel(label)))
16960 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16961 daddu $gp, $gp, $reg1
16962 $reg1 is normally $25 == $t9.
16963
16964 The -mno-shared option replaces the last three instructions with
16965 lui $gp,%hi(_gp)
16966 addiu $gp,$gp,%lo(_gp) */
16967
16968 static void
16969 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16970 {
16971 expressionS ex_off;
16972 expressionS ex_sym;
16973 int reg1;
16974
16975 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16976 We also need NewABI support. */
16977 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16978 {
16979 s_ignore (0);
16980 return;
16981 }
16982
16983 if (mips_opts.mips16)
16984 {
16985 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16986 ignore_rest_of_line ();
16987 return;
16988 }
16989
16990 reg1 = tc_get_register (0);
16991 SKIP_WHITESPACE ();
16992 if (*input_line_pointer != ',')
16993 {
16994 as_bad (_("missing argument separator ',' for .cpsetup"));
16995 return;
16996 }
16997 else
16998 ++input_line_pointer;
16999 SKIP_WHITESPACE ();
17000 if (*input_line_pointer == '$')
17001 {
17002 mips_cpreturn_register = tc_get_register (0);
17003 mips_cpreturn_offset = -1;
17004 }
17005 else
17006 {
17007 mips_cpreturn_offset = get_absolute_expression ();
17008 mips_cpreturn_register = -1;
17009 }
17010 SKIP_WHITESPACE ();
17011 if (*input_line_pointer != ',')
17012 {
17013 as_bad (_("missing argument separator ',' for .cpsetup"));
17014 return;
17015 }
17016 else
17017 ++input_line_pointer;
17018 SKIP_WHITESPACE ();
17019 expression (&ex_sym);
17020
17021 mips_mark_labels ();
17022 mips_assembling_insn = TRUE;
17023
17024 macro_start ();
17025 if (mips_cpreturn_register == -1)
17026 {
17027 ex_off.X_op = O_constant;
17028 ex_off.X_add_symbol = NULL;
17029 ex_off.X_op_symbol = NULL;
17030 ex_off.X_add_number = mips_cpreturn_offset;
17031
17032 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17033 BFD_RELOC_LO16, SP);
17034 }
17035 else
17036 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17037 mips_gp_register, 0);
17038
17039 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
17040 {
17041 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
17042 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
17043 BFD_RELOC_HI16_S);
17044
17045 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
17046 mips_gp_register, -1, BFD_RELOC_GPREL16,
17047 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
17048
17049 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
17050 mips_gp_register, reg1);
17051 }
17052 else
17053 {
17054 expressionS ex;
17055
17056 ex.X_op = O_symbol;
17057 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
17058 ex.X_op_symbol = NULL;
17059 ex.X_add_number = 0;
17060
17061 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
17062 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
17063
17064 macro_build_lui (&ex, mips_gp_register);
17065 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17066 mips_gp_register, BFD_RELOC_LO16);
17067 }
17068
17069 macro_end ();
17070
17071 mips_assembling_insn = FALSE;
17072 demand_empty_rest_of_line ();
17073 }
17074
17075 static void
17076 s_cplocal (int ignore ATTRIBUTE_UNUSED)
17077 {
17078 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
17079 .cplocal is ignored. */
17080 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17081 {
17082 s_ignore (0);
17083 return;
17084 }
17085
17086 if (mips_opts.mips16)
17087 {
17088 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
17089 ignore_rest_of_line ();
17090 return;
17091 }
17092
17093 mips_gp_register = tc_get_register (0);
17094 demand_empty_rest_of_line ();
17095 }
17096
17097 /* Handle the .cprestore pseudo-op. This stores $gp into a given
17098 offset from $sp. The offset is remembered, and after making a PIC
17099 call $gp is restored from that location. */
17100
17101 static void
17102 s_cprestore (int ignore ATTRIBUTE_UNUSED)
17103 {
17104 expressionS ex;
17105
17106 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
17107 .cprestore is ignored. */
17108 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
17109 {
17110 s_ignore (0);
17111 return;
17112 }
17113
17114 if (mips_opts.mips16)
17115 {
17116 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
17117 ignore_rest_of_line ();
17118 return;
17119 }
17120
17121 mips_cprestore_offset = get_absolute_expression ();
17122 mips_cprestore_valid = 1;
17123
17124 ex.X_op = O_constant;
17125 ex.X_add_symbol = NULL;
17126 ex.X_op_symbol = NULL;
17127 ex.X_add_number = mips_cprestore_offset;
17128
17129 mips_mark_labels ();
17130 mips_assembling_insn = TRUE;
17131
17132 macro_start ();
17133 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
17134 SP, HAVE_64BIT_ADDRESSES);
17135 macro_end ();
17136
17137 mips_assembling_insn = FALSE;
17138 demand_empty_rest_of_line ();
17139 }
17140
17141 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
17142 was given in the preceding .cpsetup, it results in:
17143 ld $gp, offset($sp)
17144
17145 If a register $reg2 was given there, it results in:
17146 daddu $gp, $reg2, $0 */
17147
17148 static void
17149 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
17150 {
17151 expressionS ex;
17152
17153 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
17154 We also need NewABI support. */
17155 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17156 {
17157 s_ignore (0);
17158 return;
17159 }
17160
17161 if (mips_opts.mips16)
17162 {
17163 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
17164 ignore_rest_of_line ();
17165 return;
17166 }
17167
17168 mips_mark_labels ();
17169 mips_assembling_insn = TRUE;
17170
17171 macro_start ();
17172 if (mips_cpreturn_register == -1)
17173 {
17174 ex.X_op = O_constant;
17175 ex.X_add_symbol = NULL;
17176 ex.X_op_symbol = NULL;
17177 ex.X_add_number = mips_cpreturn_offset;
17178
17179 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
17180 }
17181 else
17182 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17183 mips_cpreturn_register, 0);
17184 macro_end ();
17185
17186 mips_assembling_insn = FALSE;
17187 demand_empty_rest_of_line ();
17188 }
17189
17190 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17191 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17192 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17193 debug information or MIPS16 TLS. */
17194
17195 static void
17196 s_tls_rel_directive (const size_t bytes, const char *dirstr,
17197 bfd_reloc_code_real_type rtype)
17198 {
17199 expressionS ex;
17200 char *p;
17201
17202 expression (&ex);
17203
17204 if (ex.X_op != O_symbol)
17205 {
17206 as_bad (_("Unsupported use of %s"), dirstr);
17207 ignore_rest_of_line ();
17208 }
17209
17210 p = frag_more (bytes);
17211 md_number_to_chars (p, 0, bytes);
17212 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
17213 demand_empty_rest_of_line ();
17214 mips_clear_insn_labels ();
17215 }
17216
17217 /* Handle .dtprelword. */
17218
17219 static void
17220 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17221 {
17222 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
17223 }
17224
17225 /* Handle .dtpreldword. */
17226
17227 static void
17228 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17229 {
17230 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17231 }
17232
17233 /* Handle .tprelword. */
17234
17235 static void
17236 s_tprelword (int ignore ATTRIBUTE_UNUSED)
17237 {
17238 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17239 }
17240
17241 /* Handle .tpreldword. */
17242
17243 static void
17244 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17245 {
17246 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
17247 }
17248
17249 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17250 code. It sets the offset to use in gp_rel relocations. */
17251
17252 static void
17253 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
17254 {
17255 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17256 We also need NewABI support. */
17257 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17258 {
17259 s_ignore (0);
17260 return;
17261 }
17262
17263 mips_gprel_offset = get_absolute_expression ();
17264
17265 demand_empty_rest_of_line ();
17266 }
17267
17268 /* Handle the .gpword pseudo-op. This is used when generating PIC
17269 code. It generates a 32 bit GP relative reloc. */
17270
17271 static void
17272 s_gpword (int ignore ATTRIBUTE_UNUSED)
17273 {
17274 segment_info_type *si;
17275 struct insn_label_list *l;
17276 expressionS ex;
17277 char *p;
17278
17279 /* When not generating PIC code, this is treated as .word. */
17280 if (mips_pic != SVR4_PIC)
17281 {
17282 s_cons (2);
17283 return;
17284 }
17285
17286 si = seg_info (now_seg);
17287 l = si->label_list;
17288 mips_emit_delays ();
17289 if (auto_align)
17290 mips_align (2, 0, l);
17291
17292 expression (&ex);
17293 mips_clear_insn_labels ();
17294
17295 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17296 {
17297 as_bad (_("Unsupported use of .gpword"));
17298 ignore_rest_of_line ();
17299 }
17300
17301 p = frag_more (4);
17302 md_number_to_chars (p, 0, 4);
17303 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17304 BFD_RELOC_GPREL32);
17305
17306 demand_empty_rest_of_line ();
17307 }
17308
17309 static void
17310 s_gpdword (int ignore ATTRIBUTE_UNUSED)
17311 {
17312 segment_info_type *si;
17313 struct insn_label_list *l;
17314 expressionS ex;
17315 char *p;
17316
17317 /* When not generating PIC code, this is treated as .dword. */
17318 if (mips_pic != SVR4_PIC)
17319 {
17320 s_cons (3);
17321 return;
17322 }
17323
17324 si = seg_info (now_seg);
17325 l = si->label_list;
17326 mips_emit_delays ();
17327 if (auto_align)
17328 mips_align (3, 0, l);
17329
17330 expression (&ex);
17331 mips_clear_insn_labels ();
17332
17333 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17334 {
17335 as_bad (_("Unsupported use of .gpdword"));
17336 ignore_rest_of_line ();
17337 }
17338
17339 p = frag_more (8);
17340 md_number_to_chars (p, 0, 8);
17341 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17342 BFD_RELOC_GPREL32)->fx_tcbit = 1;
17343
17344 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
17345 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17346 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
17347
17348 demand_empty_rest_of_line ();
17349 }
17350
17351 /* Handle the .ehword pseudo-op. This is used when generating unwinding
17352 tables. It generates a R_MIPS_EH reloc. */
17353
17354 static void
17355 s_ehword (int ignore ATTRIBUTE_UNUSED)
17356 {
17357 expressionS ex;
17358 char *p;
17359
17360 mips_emit_delays ();
17361
17362 expression (&ex);
17363 mips_clear_insn_labels ();
17364
17365 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17366 {
17367 as_bad (_("Unsupported use of .ehword"));
17368 ignore_rest_of_line ();
17369 }
17370
17371 p = frag_more (4);
17372 md_number_to_chars (p, 0, 4);
17373 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17374 BFD_RELOC_MIPS_EH);
17375
17376 demand_empty_rest_of_line ();
17377 }
17378
17379 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
17380 tables in SVR4 PIC code. */
17381
17382 static void
17383 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17384 {
17385 int reg;
17386
17387 /* This is ignored when not generating SVR4 PIC code. */
17388 if (mips_pic != SVR4_PIC)
17389 {
17390 s_ignore (0);
17391 return;
17392 }
17393
17394 mips_mark_labels ();
17395 mips_assembling_insn = TRUE;
17396
17397 /* Add $gp to the register named as an argument. */
17398 macro_start ();
17399 reg = tc_get_register (0);
17400 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17401 macro_end ();
17402
17403 mips_assembling_insn = FALSE;
17404 demand_empty_rest_of_line ();
17405 }
17406
17407 /* Handle the .insn pseudo-op. This marks instruction labels in
17408 mips16/micromips mode. This permits the linker to handle them specially,
17409 such as generating jalx instructions when needed. We also make
17410 them odd for the duration of the assembly, in order to generate the
17411 right sort of code. We will make them even in the adjust_symtab
17412 routine, while leaving them marked. This is convenient for the
17413 debugger and the disassembler. The linker knows to make them odd
17414 again. */
17415
17416 static void
17417 s_insn (int ignore ATTRIBUTE_UNUSED)
17418 {
17419 mips_mark_labels ();
17420
17421 demand_empty_rest_of_line ();
17422 }
17423
17424 /* Handle a .stab[snd] directive. Ideally these directives would be
17425 implemented in a transparent way, so that removing them would not
17426 have any effect on the generated instructions. However, s_stab
17427 internally changes the section, so in practice we need to decide
17428 now whether the preceding label marks compressed code. We do not
17429 support changing the compression mode of a label after a .stab*
17430 directive, such as in:
17431
17432 foo:
17433 .stabs ...
17434 .set mips16
17435
17436 so the current mode wins. */
17437
17438 static void
17439 s_mips_stab (int type)
17440 {
17441 mips_mark_labels ();
17442 s_stab (type);
17443 }
17444
17445 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
17446
17447 static void
17448 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17449 {
17450 char *name;
17451 int c;
17452 symbolS *symbolP;
17453 expressionS exp;
17454
17455 name = input_line_pointer;
17456 c = get_symbol_end ();
17457 symbolP = symbol_find_or_make (name);
17458 S_SET_WEAK (symbolP);
17459 *input_line_pointer = c;
17460
17461 SKIP_WHITESPACE ();
17462
17463 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17464 {
17465 if (S_IS_DEFINED (symbolP))
17466 {
17467 as_bad (_("ignoring attempt to redefine symbol %s"),
17468 S_GET_NAME (symbolP));
17469 ignore_rest_of_line ();
17470 return;
17471 }
17472
17473 if (*input_line_pointer == ',')
17474 {
17475 ++input_line_pointer;
17476 SKIP_WHITESPACE ();
17477 }
17478
17479 expression (&exp);
17480 if (exp.X_op != O_symbol)
17481 {
17482 as_bad (_("bad .weakext directive"));
17483 ignore_rest_of_line ();
17484 return;
17485 }
17486 symbol_set_value_expression (symbolP, &exp);
17487 }
17488
17489 demand_empty_rest_of_line ();
17490 }
17491
17492 /* Parse a register string into a number. Called from the ECOFF code
17493 to parse .frame. The argument is non-zero if this is the frame
17494 register, so that we can record it in mips_frame_reg. */
17495
17496 int
17497 tc_get_register (int frame)
17498 {
17499 unsigned int reg;
17500
17501 SKIP_WHITESPACE ();
17502 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17503 reg = 0;
17504 if (frame)
17505 {
17506 mips_frame_reg = reg != 0 ? reg : SP;
17507 mips_frame_reg_valid = 1;
17508 mips_cprestore_valid = 0;
17509 }
17510 return reg;
17511 }
17512
17513 valueT
17514 md_section_align (asection *seg, valueT addr)
17515 {
17516 int align = bfd_get_section_alignment (stdoutput, seg);
17517
17518 if (IS_ELF)
17519 {
17520 /* We don't need to align ELF sections to the full alignment.
17521 However, Irix 5 may prefer that we align them at least to a 16
17522 byte boundary. We don't bother to align the sections if we
17523 are targeted for an embedded system. */
17524 if (strncmp (TARGET_OS, "elf", 3) == 0)
17525 return addr;
17526 if (align > 4)
17527 align = 4;
17528 }
17529
17530 return ((addr + (1 << align) - 1) & (-1 << align));
17531 }
17532
17533 /* Utility routine, called from above as well. If called while the
17534 input file is still being read, it's only an approximation. (For
17535 example, a symbol may later become defined which appeared to be
17536 undefined earlier.) */
17537
17538 static int
17539 nopic_need_relax (symbolS *sym, int before_relaxing)
17540 {
17541 if (sym == 0)
17542 return 0;
17543
17544 if (g_switch_value > 0)
17545 {
17546 const char *symname;
17547 int change;
17548
17549 /* Find out whether this symbol can be referenced off the $gp
17550 register. It can be if it is smaller than the -G size or if
17551 it is in the .sdata or .sbss section. Certain symbols can
17552 not be referenced off the $gp, although it appears as though
17553 they can. */
17554 symname = S_GET_NAME (sym);
17555 if (symname != (const char *) NULL
17556 && (strcmp (symname, "eprol") == 0
17557 || strcmp (symname, "etext") == 0
17558 || strcmp (symname, "_gp") == 0
17559 || strcmp (symname, "edata") == 0
17560 || strcmp (symname, "_fbss") == 0
17561 || strcmp (symname, "_fdata") == 0
17562 || strcmp (symname, "_ftext") == 0
17563 || strcmp (symname, "end") == 0
17564 || strcmp (symname, "_gp_disp") == 0))
17565 change = 1;
17566 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17567 && (0
17568 #ifndef NO_ECOFF_DEBUGGING
17569 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17570 && (symbol_get_obj (sym)->ecoff_extern_size
17571 <= g_switch_value))
17572 #endif
17573 /* We must defer this decision until after the whole
17574 file has been read, since there might be a .extern
17575 after the first use of this symbol. */
17576 || (before_relaxing
17577 #ifndef NO_ECOFF_DEBUGGING
17578 && symbol_get_obj (sym)->ecoff_extern_size == 0
17579 #endif
17580 && S_GET_VALUE (sym) == 0)
17581 || (S_GET_VALUE (sym) != 0
17582 && S_GET_VALUE (sym) <= g_switch_value)))
17583 change = 0;
17584 else
17585 {
17586 const char *segname;
17587
17588 segname = segment_name (S_GET_SEGMENT (sym));
17589 gas_assert (strcmp (segname, ".lit8") != 0
17590 && strcmp (segname, ".lit4") != 0);
17591 change = (strcmp (segname, ".sdata") != 0
17592 && strcmp (segname, ".sbss") != 0
17593 && strncmp (segname, ".sdata.", 7) != 0
17594 && strncmp (segname, ".sbss.", 6) != 0
17595 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17596 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17597 }
17598 return change;
17599 }
17600 else
17601 /* We are not optimizing for the $gp register. */
17602 return 1;
17603 }
17604
17605
17606 /* Return true if the given symbol should be considered local for SVR4 PIC. */
17607
17608 static bfd_boolean
17609 pic_need_relax (symbolS *sym, asection *segtype)
17610 {
17611 asection *symsec;
17612
17613 /* Handle the case of a symbol equated to another symbol. */
17614 while (symbol_equated_reloc_p (sym))
17615 {
17616 symbolS *n;
17617
17618 /* It's possible to get a loop here in a badly written program. */
17619 n = symbol_get_value_expression (sym)->X_add_symbol;
17620 if (n == sym)
17621 break;
17622 sym = n;
17623 }
17624
17625 if (symbol_section_p (sym))
17626 return TRUE;
17627
17628 symsec = S_GET_SEGMENT (sym);
17629
17630 /* This must duplicate the test in adjust_reloc_syms. */
17631 return (!bfd_is_und_section (symsec)
17632 && !bfd_is_abs_section (symsec)
17633 && !bfd_is_com_section (symsec)
17634 && !s_is_linkonce (sym, segtype)
17635 #ifdef OBJ_ELF
17636 /* A global or weak symbol is treated as external. */
17637 && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
17638 #endif
17639 );
17640 }
17641
17642
17643 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17644 extended opcode. SEC is the section the frag is in. */
17645
17646 static int
17647 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17648 {
17649 int type;
17650 const struct mips16_immed_operand *op;
17651 offsetT val;
17652 int mintiny, maxtiny;
17653 segT symsec;
17654 fragS *sym_frag;
17655
17656 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17657 return 0;
17658 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17659 return 1;
17660
17661 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17662 op = mips16_immed_operands;
17663 while (op->type != type)
17664 {
17665 ++op;
17666 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
17667 }
17668
17669 if (op->unsp)
17670 {
17671 if (type == '<' || type == '>' || type == '[' || type == ']')
17672 {
17673 mintiny = 1;
17674 maxtiny = 1 << op->nbits;
17675 }
17676 else
17677 {
17678 mintiny = 0;
17679 maxtiny = (1 << op->nbits) - 1;
17680 }
17681 }
17682 else
17683 {
17684 mintiny = - (1 << (op->nbits - 1));
17685 maxtiny = (1 << (op->nbits - 1)) - 1;
17686 }
17687
17688 sym_frag = symbol_get_frag (fragp->fr_symbol);
17689 val = S_GET_VALUE (fragp->fr_symbol);
17690 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17691
17692 if (op->pcrel)
17693 {
17694 addressT addr;
17695
17696 /* We won't have the section when we are called from
17697 mips_relax_frag. However, we will always have been called
17698 from md_estimate_size_before_relax first. If this is a
17699 branch to a different section, we mark it as such. If SEC is
17700 NULL, and the frag is not marked, then it must be a branch to
17701 the same section. */
17702 if (sec == NULL)
17703 {
17704 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17705 return 1;
17706 }
17707 else
17708 {
17709 /* Must have been called from md_estimate_size_before_relax. */
17710 if (symsec != sec)
17711 {
17712 fragp->fr_subtype =
17713 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17714
17715 /* FIXME: We should support this, and let the linker
17716 catch branches and loads that are out of range. */
17717 as_bad_where (fragp->fr_file, fragp->fr_line,
17718 _("unsupported PC relative reference to different section"));
17719
17720 return 1;
17721 }
17722 if (fragp != sym_frag && sym_frag->fr_address == 0)
17723 /* Assume non-extended on the first relaxation pass.
17724 The address we have calculated will be bogus if this is
17725 a forward branch to another frag, as the forward frag
17726 will have fr_address == 0. */
17727 return 0;
17728 }
17729
17730 /* In this case, we know for sure that the symbol fragment is in
17731 the same section. If the relax_marker of the symbol fragment
17732 differs from the relax_marker of this fragment, we have not
17733 yet adjusted the symbol fragment fr_address. We want to add
17734 in STRETCH in order to get a better estimate of the address.
17735 This particularly matters because of the shift bits. */
17736 if (stretch != 0
17737 && sym_frag->relax_marker != fragp->relax_marker)
17738 {
17739 fragS *f;
17740
17741 /* Adjust stretch for any alignment frag. Note that if have
17742 been expanding the earlier code, the symbol may be
17743 defined in what appears to be an earlier frag. FIXME:
17744 This doesn't handle the fr_subtype field, which specifies
17745 a maximum number of bytes to skip when doing an
17746 alignment. */
17747 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17748 {
17749 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17750 {
17751 if (stretch < 0)
17752 stretch = - ((- stretch)
17753 & ~ ((1 << (int) f->fr_offset) - 1));
17754 else
17755 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17756 if (stretch == 0)
17757 break;
17758 }
17759 }
17760 if (f != NULL)
17761 val += stretch;
17762 }
17763
17764 addr = fragp->fr_address + fragp->fr_fix;
17765
17766 /* The base address rules are complicated. The base address of
17767 a branch is the following instruction. The base address of a
17768 PC relative load or add is the instruction itself, but if it
17769 is in a delay slot (in which case it can not be extended) use
17770 the address of the instruction whose delay slot it is in. */
17771 if (type == 'p' || type == 'q')
17772 {
17773 addr += 2;
17774
17775 /* If we are currently assuming that this frag should be
17776 extended, then, the current address is two bytes
17777 higher. */
17778 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17779 addr += 2;
17780
17781 /* Ignore the low bit in the target, since it will be set
17782 for a text label. */
17783 if ((val & 1) != 0)
17784 --val;
17785 }
17786 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17787 addr -= 4;
17788 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17789 addr -= 2;
17790
17791 val -= addr & ~ ((1 << op->shift) - 1);
17792
17793 /* Branch offsets have an implicit 0 in the lowest bit. */
17794 if (type == 'p' || type == 'q')
17795 val /= 2;
17796
17797 /* If any of the shifted bits are set, we must use an extended
17798 opcode. If the address depends on the size of this
17799 instruction, this can lead to a loop, so we arrange to always
17800 use an extended opcode. We only check this when we are in
17801 the main relaxation loop, when SEC is NULL. */
17802 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17803 {
17804 fragp->fr_subtype =
17805 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17806 return 1;
17807 }
17808
17809 /* If we are about to mark a frag as extended because the value
17810 is precisely maxtiny + 1, then there is a chance of an
17811 infinite loop as in the following code:
17812 la $4,foo
17813 .skip 1020
17814 .align 2
17815 foo:
17816 In this case when the la is extended, foo is 0x3fc bytes
17817 away, so the la can be shrunk, but then foo is 0x400 away, so
17818 the la must be extended. To avoid this loop, we mark the
17819 frag as extended if it was small, and is about to become
17820 extended with a value of maxtiny + 1. */
17821 if (val == ((maxtiny + 1) << op->shift)
17822 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17823 && sec == NULL)
17824 {
17825 fragp->fr_subtype =
17826 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17827 return 1;
17828 }
17829 }
17830 else if (symsec != absolute_section && sec != NULL)
17831 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17832
17833 if ((val & ((1 << op->shift) - 1)) != 0
17834 || val < (mintiny << op->shift)
17835 || val > (maxtiny << op->shift))
17836 return 1;
17837 else
17838 return 0;
17839 }
17840
17841 /* Compute the length of a branch sequence, and adjust the
17842 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17843 worst-case length is computed, with UPDATE being used to indicate
17844 whether an unconditional (-1), branch-likely (+1) or regular (0)
17845 branch is to be computed. */
17846 static int
17847 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17848 {
17849 bfd_boolean toofar;
17850 int length;
17851
17852 if (fragp
17853 && S_IS_DEFINED (fragp->fr_symbol)
17854 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17855 {
17856 addressT addr;
17857 offsetT val;
17858
17859 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17860
17861 addr = fragp->fr_address + fragp->fr_fix + 4;
17862
17863 val -= addr;
17864
17865 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17866 }
17867 else if (fragp)
17868 /* If the symbol is not defined or it's in a different segment,
17869 assume the user knows what's going on and emit a short
17870 branch. */
17871 toofar = FALSE;
17872 else
17873 toofar = TRUE;
17874
17875 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17876 fragp->fr_subtype
17877 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17878 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17879 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17880 RELAX_BRANCH_LINK (fragp->fr_subtype),
17881 toofar);
17882
17883 length = 4;
17884 if (toofar)
17885 {
17886 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17887 length += 8;
17888
17889 if (mips_pic != NO_PIC)
17890 {
17891 /* Additional space for PIC loading of target address. */
17892 length += 8;
17893 if (mips_opts.isa == ISA_MIPS1)
17894 /* Additional space for $at-stabilizing nop. */
17895 length += 4;
17896 }
17897
17898 /* If branch is conditional. */
17899 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17900 length += 8;
17901 }
17902
17903 return length;
17904 }
17905
17906 /* Compute the length of a branch sequence, and adjust the
17907 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17908 worst-case length is computed, with UPDATE being used to indicate
17909 whether an unconditional (-1), or regular (0) branch is to be
17910 computed. */
17911
17912 static int
17913 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17914 {
17915 bfd_boolean toofar;
17916 int length;
17917
17918 if (fragp
17919 && S_IS_DEFINED (fragp->fr_symbol)
17920 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17921 {
17922 addressT addr;
17923 offsetT val;
17924
17925 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17926 /* Ignore the low bit in the target, since it will be set
17927 for a text label. */
17928 if ((val & 1) != 0)
17929 --val;
17930
17931 addr = fragp->fr_address + fragp->fr_fix + 4;
17932
17933 val -= addr;
17934
17935 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17936 }
17937 else if (fragp)
17938 /* If the symbol is not defined or it's in a different segment,
17939 assume the user knows what's going on and emit a short
17940 branch. */
17941 toofar = FALSE;
17942 else
17943 toofar = TRUE;
17944
17945 if (fragp && update
17946 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17947 fragp->fr_subtype = (toofar
17948 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17949 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17950
17951 length = 4;
17952 if (toofar)
17953 {
17954 bfd_boolean compact_known = fragp != NULL;
17955 bfd_boolean compact = FALSE;
17956 bfd_boolean uncond;
17957
17958 if (compact_known)
17959 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17960 if (fragp)
17961 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17962 else
17963 uncond = update < 0;
17964
17965 /* If label is out of range, we turn branch <br>:
17966
17967 <br> label # 4 bytes
17968 0:
17969
17970 into:
17971
17972 j label # 4 bytes
17973 nop # 2 bytes if compact && !PIC
17974 0:
17975 */
17976 if (mips_pic == NO_PIC && (!compact_known || compact))
17977 length += 2;
17978
17979 /* If assembling PIC code, we further turn:
17980
17981 j label # 4 bytes
17982
17983 into:
17984
17985 lw/ld at, %got(label)(gp) # 4 bytes
17986 d/addiu at, %lo(label) # 4 bytes
17987 jr/c at # 2 bytes
17988 */
17989 if (mips_pic != NO_PIC)
17990 length += 6;
17991
17992 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17993
17994 <brneg> 0f # 4 bytes
17995 nop # 2 bytes if !compact
17996 */
17997 if (!uncond)
17998 length += (compact_known && compact) ? 4 : 6;
17999 }
18000
18001 return length;
18002 }
18003
18004 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
18005 bit accordingly. */
18006
18007 static int
18008 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
18009 {
18010 bfd_boolean toofar;
18011
18012 if (fragp
18013 && S_IS_DEFINED (fragp->fr_symbol)
18014 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18015 {
18016 addressT addr;
18017 offsetT val;
18018 int type;
18019
18020 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18021 /* Ignore the low bit in the target, since it will be set
18022 for a text label. */
18023 if ((val & 1) != 0)
18024 --val;
18025
18026 /* Assume this is a 2-byte branch. */
18027 addr = fragp->fr_address + fragp->fr_fix + 2;
18028
18029 /* We try to avoid the infinite loop by not adding 2 more bytes for
18030 long branches. */
18031
18032 val -= addr;
18033
18034 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18035 if (type == 'D')
18036 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
18037 else if (type == 'E')
18038 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
18039 else
18040 abort ();
18041 }
18042 else
18043 /* If the symbol is not defined or it's in a different segment,
18044 we emit a normal 32-bit branch. */
18045 toofar = TRUE;
18046
18047 if (fragp && update
18048 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18049 fragp->fr_subtype
18050 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
18051 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
18052
18053 if (toofar)
18054 return 4;
18055
18056 return 2;
18057 }
18058
18059 /* Estimate the size of a frag before relaxing. Unless this is the
18060 mips16, we are not really relaxing here, and the final size is
18061 encoded in the subtype information. For the mips16, we have to
18062 decide whether we are using an extended opcode or not. */
18063
18064 int
18065 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
18066 {
18067 int change;
18068
18069 if (RELAX_BRANCH_P (fragp->fr_subtype))
18070 {
18071
18072 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
18073
18074 return fragp->fr_var;
18075 }
18076
18077 if (RELAX_MIPS16_P (fragp->fr_subtype))
18078 /* We don't want to modify the EXTENDED bit here; it might get us
18079 into infinite loops. We change it only in mips_relax_frag(). */
18080 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
18081
18082 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18083 {
18084 int length = 4;
18085
18086 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18087 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
18088 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18089 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
18090 fragp->fr_var = length;
18091
18092 return length;
18093 }
18094
18095 if (mips_pic == NO_PIC)
18096 change = nopic_need_relax (fragp->fr_symbol, 0);
18097 else if (mips_pic == SVR4_PIC)
18098 change = pic_need_relax (fragp->fr_symbol, segtype);
18099 else if (mips_pic == VXWORKS_PIC)
18100 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
18101 change = 0;
18102 else
18103 abort ();
18104
18105 if (change)
18106 {
18107 fragp->fr_subtype |= RELAX_USE_SECOND;
18108 return -RELAX_FIRST (fragp->fr_subtype);
18109 }
18110 else
18111 return -RELAX_SECOND (fragp->fr_subtype);
18112 }
18113
18114 /* This is called to see whether a reloc against a defined symbol
18115 should be converted into a reloc against a section. */
18116
18117 int
18118 mips_fix_adjustable (fixS *fixp)
18119 {
18120 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18121 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18122 return 0;
18123
18124 if (fixp->fx_addsy == NULL)
18125 return 1;
18126
18127 /* If symbol SYM is in a mergeable section, relocations of the form
18128 SYM + 0 can usually be made section-relative. The mergeable data
18129 is then identified by the section offset rather than by the symbol.
18130
18131 However, if we're generating REL LO16 relocations, the offset is split
18132 between the LO16 and parterning high part relocation. The linker will
18133 need to recalculate the complete offset in order to correctly identify
18134 the merge data.
18135
18136 The linker has traditionally not looked for the parterning high part
18137 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
18138 placed anywhere. Rather than break backwards compatibility by changing
18139 this, it seems better not to force the issue, and instead keep the
18140 original symbol. This will work with either linker behavior. */
18141 if ((lo16_reloc_p (fixp->fx_r_type)
18142 || reloc_needs_lo_p (fixp->fx_r_type))
18143 && HAVE_IN_PLACE_ADDENDS
18144 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
18145 return 0;
18146
18147 /* There is no place to store an in-place offset for JALR relocations.
18148 Likewise an in-range offset of limited PC-relative relocations may
18149 overflow the in-place relocatable field if recalculated against the
18150 start address of the symbol's containing section. */
18151 if (HAVE_IN_PLACE_ADDENDS
18152 && (limited_pcrel_reloc_p (fixp->fx_r_type)
18153 || jalr_reloc_p (fixp->fx_r_type)))
18154 return 0;
18155
18156 #ifdef OBJ_ELF
18157 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18158 to a floating-point stub. The same is true for non-R_MIPS16_26
18159 relocations against MIPS16 functions; in this case, the stub becomes
18160 the function's canonical address.
18161
18162 Floating-point stubs are stored in unique .mips16.call.* or
18163 .mips16.fn.* sections. If a stub T for function F is in section S,
18164 the first relocation in section S must be against F; this is how the
18165 linker determines the target function. All relocations that might
18166 resolve to T must also be against F. We therefore have the following
18167 restrictions, which are given in an intentionally-redundant way:
18168
18169 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18170 symbols.
18171
18172 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18173 if that stub might be used.
18174
18175 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18176 symbols.
18177
18178 4. We cannot reduce a stub's relocations against MIPS16 symbols if
18179 that stub might be used.
18180
18181 There is a further restriction:
18182
18183 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
18184 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
18185 targets with in-place addends; the relocation field cannot
18186 encode the low bit.
18187
18188 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
18189 against a MIPS16 symbol. We deal with (5) by by not reducing any
18190 such relocations on REL targets.
18191
18192 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18193 relocation against some symbol R, no relocation against R may be
18194 reduced. (Note that this deals with (2) as well as (1) because
18195 relocations against global symbols will never be reduced on ELF
18196 targets.) This approach is a little simpler than trying to detect
18197 stub sections, and gives the "all or nothing" per-symbol consistency
18198 that we have for MIPS16 symbols. */
18199 if (IS_ELF
18200 && fixp->fx_subsy == NULL
18201 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
18202 || *symbol_get_tc (fixp->fx_addsy)
18203 || (HAVE_IN_PLACE_ADDENDS
18204 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
18205 && jmp_reloc_p (fixp->fx_r_type))))
18206 return 0;
18207 #endif
18208
18209 return 1;
18210 }
18211
18212 /* Translate internal representation of relocation info to BFD target
18213 format. */
18214
18215 arelent **
18216 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
18217 {
18218 static arelent *retval[4];
18219 arelent *reloc;
18220 bfd_reloc_code_real_type code;
18221
18222 memset (retval, 0, sizeof(retval));
18223 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
18224 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
18225 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18226 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18227
18228 if (fixp->fx_pcrel)
18229 {
18230 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18231 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18232 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
18233 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18234 || fixp->fx_r_type == BFD_RELOC_32_PCREL);
18235
18236 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18237 Relocations want only the symbol offset. */
18238 reloc->addend = fixp->fx_addnumber + reloc->address;
18239 if (!IS_ELF)
18240 {
18241 /* A gruesome hack which is a result of the gruesome gas
18242 reloc handling. What's worse, for COFF (as opposed to
18243 ECOFF), we might need yet another copy of reloc->address.
18244 See bfd_install_relocation. */
18245 reloc->addend += reloc->address;
18246 }
18247 }
18248 else
18249 reloc->addend = fixp->fx_addnumber;
18250
18251 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18252 entry to be used in the relocation's section offset. */
18253 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18254 {
18255 reloc->address = reloc->addend;
18256 reloc->addend = 0;
18257 }
18258
18259 code = fixp->fx_r_type;
18260
18261 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
18262 if (reloc->howto == NULL)
18263 {
18264 as_bad_where (fixp->fx_file, fixp->fx_line,
18265 _("Can not represent %s relocation in this object file format"),
18266 bfd_get_reloc_code_name (code));
18267 retval[0] = NULL;
18268 }
18269
18270 return retval;
18271 }
18272
18273 /* Relax a machine dependent frag. This returns the amount by which
18274 the current size of the frag should change. */
18275
18276 int
18277 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
18278 {
18279 if (RELAX_BRANCH_P (fragp->fr_subtype))
18280 {
18281 offsetT old_var = fragp->fr_var;
18282
18283 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
18284
18285 return fragp->fr_var - old_var;
18286 }
18287
18288 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18289 {
18290 offsetT old_var = fragp->fr_var;
18291 offsetT new_var = 4;
18292
18293 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18294 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18295 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18296 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18297 fragp->fr_var = new_var;
18298
18299 return new_var - old_var;
18300 }
18301
18302 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18303 return 0;
18304
18305 if (mips16_extended_frag (fragp, NULL, stretch))
18306 {
18307 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18308 return 0;
18309 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18310 return 2;
18311 }
18312 else
18313 {
18314 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18315 return 0;
18316 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18317 return -2;
18318 }
18319
18320 return 0;
18321 }
18322
18323 /* Convert a machine dependent frag. */
18324
18325 void
18326 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
18327 {
18328 if (RELAX_BRANCH_P (fragp->fr_subtype))
18329 {
18330 char *buf;
18331 unsigned long insn;
18332 expressionS exp;
18333 fixS *fixp;
18334
18335 buf = fragp->fr_literal + fragp->fr_fix;
18336 insn = read_insn (buf);
18337
18338 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18339 {
18340 /* We generate a fixup instead of applying it right now
18341 because, if there are linker relaxations, we're going to
18342 need the relocations. */
18343 exp.X_op = O_symbol;
18344 exp.X_add_symbol = fragp->fr_symbol;
18345 exp.X_add_number = fragp->fr_offset;
18346
18347 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18348 BFD_RELOC_16_PCREL_S2);
18349 fixp->fx_file = fragp->fr_file;
18350 fixp->fx_line = fragp->fr_line;
18351
18352 buf = write_insn (buf, insn);
18353 }
18354 else
18355 {
18356 int i;
18357
18358 as_warn_where (fragp->fr_file, fragp->fr_line,
18359 _("Relaxed out-of-range branch into a jump"));
18360
18361 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18362 goto uncond;
18363
18364 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18365 {
18366 /* Reverse the branch. */
18367 switch ((insn >> 28) & 0xf)
18368 {
18369 case 4:
18370 /* bc[0-3][tf]l? instructions can have the condition
18371 reversed by tweaking a single TF bit, and their
18372 opcodes all have 0x4???????. */
18373 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18374 insn ^= 0x00010000;
18375 break;
18376
18377 case 0:
18378 /* bltz 0x04000000 bgez 0x04010000
18379 bltzal 0x04100000 bgezal 0x04110000 */
18380 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18381 insn ^= 0x00010000;
18382 break;
18383
18384 case 1:
18385 /* beq 0x10000000 bne 0x14000000
18386 blez 0x18000000 bgtz 0x1c000000 */
18387 insn ^= 0x04000000;
18388 break;
18389
18390 default:
18391 abort ();
18392 }
18393 }
18394
18395 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18396 {
18397 /* Clear the and-link bit. */
18398 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18399
18400 /* bltzal 0x04100000 bgezal 0x04110000
18401 bltzall 0x04120000 bgezall 0x04130000 */
18402 insn &= ~0x00100000;
18403 }
18404
18405 /* Branch over the branch (if the branch was likely) or the
18406 full jump (not likely case). Compute the offset from the
18407 current instruction to branch to. */
18408 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18409 i = 16;
18410 else
18411 {
18412 /* How many bytes in instructions we've already emitted? */
18413 i = buf - fragp->fr_literal - fragp->fr_fix;
18414 /* How many bytes in instructions from here to the end? */
18415 i = fragp->fr_var - i;
18416 }
18417 /* Convert to instruction count. */
18418 i >>= 2;
18419 /* Branch counts from the next instruction. */
18420 i--;
18421 insn |= i;
18422 /* Branch over the jump. */
18423 buf = write_insn (buf, insn);
18424
18425 /* nop */
18426 buf = write_insn (buf, 0);
18427
18428 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18429 {
18430 /* beql $0, $0, 2f */
18431 insn = 0x50000000;
18432 /* Compute the PC offset from the current instruction to
18433 the end of the variable frag. */
18434 /* How many bytes in instructions we've already emitted? */
18435 i = buf - fragp->fr_literal - fragp->fr_fix;
18436 /* How many bytes in instructions from here to the end? */
18437 i = fragp->fr_var - i;
18438 /* Convert to instruction count. */
18439 i >>= 2;
18440 /* Don't decrement i, because we want to branch over the
18441 delay slot. */
18442 insn |= i;
18443
18444 buf = write_insn (buf, insn);
18445 buf = write_insn (buf, 0);
18446 }
18447
18448 uncond:
18449 if (mips_pic == NO_PIC)
18450 {
18451 /* j or jal. */
18452 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18453 ? 0x0c000000 : 0x08000000);
18454 exp.X_op = O_symbol;
18455 exp.X_add_symbol = fragp->fr_symbol;
18456 exp.X_add_number = fragp->fr_offset;
18457
18458 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18459 FALSE, BFD_RELOC_MIPS_JMP);
18460 fixp->fx_file = fragp->fr_file;
18461 fixp->fx_line = fragp->fr_line;
18462
18463 buf = write_insn (buf, insn);
18464 }
18465 else
18466 {
18467 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18468
18469 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
18470 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18471 insn |= at << OP_SH_RT;
18472 exp.X_op = O_symbol;
18473 exp.X_add_symbol = fragp->fr_symbol;
18474 exp.X_add_number = fragp->fr_offset;
18475
18476 if (fragp->fr_offset)
18477 {
18478 exp.X_add_symbol = make_expr_symbol (&exp);
18479 exp.X_add_number = 0;
18480 }
18481
18482 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18483 FALSE, BFD_RELOC_MIPS_GOT16);
18484 fixp->fx_file = fragp->fr_file;
18485 fixp->fx_line = fragp->fr_line;
18486
18487 buf = write_insn (buf, insn);
18488
18489 if (mips_opts.isa == ISA_MIPS1)
18490 /* nop */
18491 buf = write_insn (buf, 0);
18492
18493 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
18494 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18495 insn |= at << OP_SH_RS | at << OP_SH_RT;
18496
18497 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18498 FALSE, BFD_RELOC_LO16);
18499 fixp->fx_file = fragp->fr_file;
18500 fixp->fx_line = fragp->fr_line;
18501
18502 buf = write_insn (buf, insn);
18503
18504 /* j(al)r $at. */
18505 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18506 insn = 0x0000f809;
18507 else
18508 insn = 0x00000008;
18509 insn |= at << OP_SH_RS;
18510
18511 buf = write_insn (buf, insn);
18512 }
18513 }
18514
18515 fragp->fr_fix += fragp->fr_var;
18516 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18517 return;
18518 }
18519
18520 /* Relax microMIPS branches. */
18521 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18522 {
18523 char *buf = fragp->fr_literal + fragp->fr_fix;
18524 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18525 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18526 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18527 bfd_boolean short_ds;
18528 unsigned long insn;
18529 expressionS exp;
18530 fixS *fixp;
18531
18532 exp.X_op = O_symbol;
18533 exp.X_add_symbol = fragp->fr_symbol;
18534 exp.X_add_number = fragp->fr_offset;
18535
18536 fragp->fr_fix += fragp->fr_var;
18537
18538 /* Handle 16-bit branches that fit or are forced to fit. */
18539 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18540 {
18541 /* We generate a fixup instead of applying it right now,
18542 because if there is linker relaxation, we're going to
18543 need the relocations. */
18544 if (type == 'D')
18545 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18546 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18547 else if (type == 'E')
18548 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18549 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18550 else
18551 abort ();
18552
18553 fixp->fx_file = fragp->fr_file;
18554 fixp->fx_line = fragp->fr_line;
18555
18556 /* These relocations can have an addend that won't fit in
18557 2 octets. */
18558 fixp->fx_no_overflow = 1;
18559
18560 return;
18561 }
18562
18563 /* Handle 32-bit branches that fit or are forced to fit. */
18564 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18565 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18566 {
18567 /* We generate a fixup instead of applying it right now,
18568 because if there is linker relaxation, we're going to
18569 need the relocations. */
18570 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18571 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18572 fixp->fx_file = fragp->fr_file;
18573 fixp->fx_line = fragp->fr_line;
18574
18575 if (type == 0)
18576 return;
18577 }
18578
18579 /* Relax 16-bit branches to 32-bit branches. */
18580 if (type != 0)
18581 {
18582 insn = read_compressed_insn (buf, 2);
18583
18584 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18585 insn = 0x94000000; /* beq */
18586 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18587 {
18588 unsigned long regno;
18589
18590 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18591 regno = micromips_to_32_reg_d_map [regno];
18592 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18593 insn |= regno << MICROMIPSOP_SH_RS;
18594 }
18595 else
18596 abort ();
18597
18598 /* Nothing else to do, just write it out. */
18599 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18600 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18601 {
18602 buf = write_compressed_insn (buf, insn, 4);
18603 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18604 return;
18605 }
18606 }
18607 else
18608 insn = read_compressed_insn (buf, 4);
18609
18610 /* Relax 32-bit branches to a sequence of instructions. */
18611 as_warn_where (fragp->fr_file, fragp->fr_line,
18612 _("Relaxed out-of-range branch into a jump"));
18613
18614 /* Set the short-delay-slot bit. */
18615 short_ds = al && (insn & 0x02000000) != 0;
18616
18617 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18618 {
18619 symbolS *l;
18620
18621 /* Reverse the branch. */
18622 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18623 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18624 insn ^= 0x20000000;
18625 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18626 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18627 || (insn & 0xffe00000) == 0x40800000 /* blez */
18628 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18629 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18630 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18631 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18632 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18633 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18634 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18635 insn ^= 0x00400000;
18636 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18637 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18638 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18639 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18640 insn ^= 0x00200000;
18641 else
18642 abort ();
18643
18644 if (al)
18645 {
18646 /* Clear the and-link and short-delay-slot bits. */
18647 gas_assert ((insn & 0xfda00000) == 0x40200000);
18648
18649 /* bltzal 0x40200000 bgezal 0x40600000 */
18650 /* bltzals 0x42200000 bgezals 0x42600000 */
18651 insn &= ~0x02200000;
18652 }
18653
18654 /* Make a label at the end for use with the branch. */
18655 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18656 micromips_label_inc ();
18657 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
18658 if (IS_ELF)
18659 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18660 #endif
18661
18662 /* Refer to it. */
18663 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18664 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18665 fixp->fx_file = fragp->fr_file;
18666 fixp->fx_line = fragp->fr_line;
18667
18668 /* Branch over the jump. */
18669 buf = write_compressed_insn (buf, insn, 4);
18670 if (!compact)
18671 /* nop */
18672 buf = write_compressed_insn (buf, 0x0c00, 2);
18673 }
18674
18675 if (mips_pic == NO_PIC)
18676 {
18677 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
18678
18679 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18680 insn = al ? jal : 0xd4000000;
18681
18682 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18683 BFD_RELOC_MICROMIPS_JMP);
18684 fixp->fx_file = fragp->fr_file;
18685 fixp->fx_line = fragp->fr_line;
18686
18687 buf = write_compressed_insn (buf, insn, 4);
18688 if (compact)
18689 /* nop */
18690 buf = write_compressed_insn (buf, 0x0c00, 2);
18691 }
18692 else
18693 {
18694 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18695 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18696 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
18697
18698 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18699 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18700 insn |= at << MICROMIPSOP_SH_RT;
18701
18702 if (exp.X_add_number)
18703 {
18704 exp.X_add_symbol = make_expr_symbol (&exp);
18705 exp.X_add_number = 0;
18706 }
18707
18708 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18709 BFD_RELOC_MICROMIPS_GOT16);
18710 fixp->fx_file = fragp->fr_file;
18711 fixp->fx_line = fragp->fr_line;
18712
18713 buf = write_compressed_insn (buf, insn, 4);
18714
18715 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18716 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18717 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18718
18719 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18720 BFD_RELOC_MICROMIPS_LO16);
18721 fixp->fx_file = fragp->fr_file;
18722 fixp->fx_line = fragp->fr_line;
18723
18724 buf = write_compressed_insn (buf, insn, 4);
18725
18726 /* jr/jrc/jalr/jalrs $at */
18727 insn = al ? jalr : jr;
18728 insn |= at << MICROMIPSOP_SH_MJ;
18729
18730 buf = write_compressed_insn (buf, insn, 2);
18731 }
18732
18733 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18734 return;
18735 }
18736
18737 if (RELAX_MIPS16_P (fragp->fr_subtype))
18738 {
18739 int type;
18740 const struct mips16_immed_operand *op;
18741 offsetT val;
18742 char *buf;
18743 unsigned int user_length, length;
18744 unsigned long insn;
18745 bfd_boolean ext;
18746
18747 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18748 op = mips16_immed_operands;
18749 while (op->type != type)
18750 ++op;
18751
18752 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18753 val = resolve_symbol_value (fragp->fr_symbol);
18754 if (op->pcrel)
18755 {
18756 addressT addr;
18757
18758 addr = fragp->fr_address + fragp->fr_fix;
18759
18760 /* The rules for the base address of a PC relative reloc are
18761 complicated; see mips16_extended_frag. */
18762 if (type == 'p' || type == 'q')
18763 {
18764 addr += 2;
18765 if (ext)
18766 addr += 2;
18767 /* Ignore the low bit in the target, since it will be
18768 set for a text label. */
18769 if ((val & 1) != 0)
18770 --val;
18771 }
18772 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18773 addr -= 4;
18774 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18775 addr -= 2;
18776
18777 addr &= ~ (addressT) ((1 << op->shift) - 1);
18778 val -= addr;
18779
18780 /* Make sure the section winds up with the alignment we have
18781 assumed. */
18782 if (op->shift > 0)
18783 record_alignment (asec, op->shift);
18784 }
18785
18786 if (ext
18787 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18788 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18789 as_warn_where (fragp->fr_file, fragp->fr_line,
18790 _("extended instruction in delay slot"));
18791
18792 buf = fragp->fr_literal + fragp->fr_fix;
18793
18794 insn = read_compressed_insn (buf, 2);
18795 if (ext)
18796 insn |= MIPS16_EXTEND;
18797
18798 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18799 user_length = 4;
18800 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18801 user_length = 2;
18802 else
18803 user_length = 0;
18804
18805 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18806 BFD_RELOC_UNUSED, val, user_length, &insn);
18807
18808 length = (ext ? 4 : 2);
18809 gas_assert (mips16_opcode_length (insn) == length);
18810 write_compressed_insn (buf, insn, length);
18811 fragp->fr_fix += length;
18812 }
18813 else
18814 {
18815 relax_substateT subtype = fragp->fr_subtype;
18816 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18817 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18818 int first, second;
18819 fixS *fixp;
18820
18821 first = RELAX_FIRST (subtype);
18822 second = RELAX_SECOND (subtype);
18823 fixp = (fixS *) fragp->fr_opcode;
18824
18825 /* If the delay slot chosen does not match the size of the instruction,
18826 then emit a warning. */
18827 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18828 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18829 {
18830 relax_substateT s;
18831 const char *msg;
18832
18833 s = subtype & (RELAX_DELAY_SLOT_16BIT
18834 | RELAX_DELAY_SLOT_SIZE_FIRST
18835 | RELAX_DELAY_SLOT_SIZE_SECOND);
18836 msg = macro_warning (s);
18837 if (msg != NULL)
18838 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18839 subtype &= ~s;
18840 }
18841
18842 /* Possibly emit a warning if we've chosen the longer option. */
18843 if (use_second == second_longer)
18844 {
18845 relax_substateT s;
18846 const char *msg;
18847
18848 s = (subtype
18849 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18850 msg = macro_warning (s);
18851 if (msg != NULL)
18852 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18853 subtype &= ~s;
18854 }
18855
18856 /* Go through all the fixups for the first sequence. Disable them
18857 (by marking them as done) if we're going to use the second
18858 sequence instead. */
18859 while (fixp
18860 && fixp->fx_frag == fragp
18861 && fixp->fx_where < fragp->fr_fix - second)
18862 {
18863 if (subtype & RELAX_USE_SECOND)
18864 fixp->fx_done = 1;
18865 fixp = fixp->fx_next;
18866 }
18867
18868 /* Go through the fixups for the second sequence. Disable them if
18869 we're going to use the first sequence, otherwise adjust their
18870 addresses to account for the relaxation. */
18871 while (fixp && fixp->fx_frag == fragp)
18872 {
18873 if (subtype & RELAX_USE_SECOND)
18874 fixp->fx_where -= first;
18875 else
18876 fixp->fx_done = 1;
18877 fixp = fixp->fx_next;
18878 }
18879
18880 /* Now modify the frag contents. */
18881 if (subtype & RELAX_USE_SECOND)
18882 {
18883 char *start;
18884
18885 start = fragp->fr_literal + fragp->fr_fix - first - second;
18886 memmove (start, start + first, second);
18887 fragp->fr_fix -= first;
18888 }
18889 else
18890 fragp->fr_fix -= second;
18891 }
18892 }
18893
18894 #ifdef OBJ_ELF
18895
18896 /* This function is called after the relocs have been generated.
18897 We've been storing mips16 text labels as odd. Here we convert them
18898 back to even for the convenience of the debugger. */
18899
18900 void
18901 mips_frob_file_after_relocs (void)
18902 {
18903 asymbol **syms;
18904 unsigned int count, i;
18905
18906 if (!IS_ELF)
18907 return;
18908
18909 syms = bfd_get_outsymbols (stdoutput);
18910 count = bfd_get_symcount (stdoutput);
18911 for (i = 0; i < count; i++, syms++)
18912 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18913 && ((*syms)->value & 1) != 0)
18914 {
18915 (*syms)->value &= ~1;
18916 /* If the symbol has an odd size, it was probably computed
18917 incorrectly, so adjust that as well. */
18918 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18919 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18920 }
18921 }
18922
18923 #endif
18924
18925 /* This function is called whenever a label is defined, including fake
18926 labels instantiated off the dot special symbol. It is used when
18927 handling branch delays; if a branch has a label, we assume we cannot
18928 move it. This also bumps the value of the symbol by 1 in compressed
18929 code. */
18930
18931 static void
18932 mips_record_label (symbolS *sym)
18933 {
18934 segment_info_type *si = seg_info (now_seg);
18935 struct insn_label_list *l;
18936
18937 if (free_insn_labels == NULL)
18938 l = (struct insn_label_list *) xmalloc (sizeof *l);
18939 else
18940 {
18941 l = free_insn_labels;
18942 free_insn_labels = l->next;
18943 }
18944
18945 l->label = sym;
18946 l->next = si->label_list;
18947 si->label_list = l;
18948 }
18949
18950 /* This function is called as tc_frob_label() whenever a label is defined
18951 and adds a DWARF-2 record we only want for true labels. */
18952
18953 void
18954 mips_define_label (symbolS *sym)
18955 {
18956 mips_record_label (sym);
18957 #ifdef OBJ_ELF
18958 dwarf2_emit_label (sym);
18959 #endif
18960 }
18961
18962 /* This function is called by tc_new_dot_label whenever a new dot symbol
18963 is defined. */
18964
18965 void
18966 mips_add_dot_label (symbolS *sym)
18967 {
18968 mips_record_label (sym);
18969 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18970 mips_compressed_mark_label (sym);
18971 }
18972 \f
18973 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
18974
18975 /* Some special processing for a MIPS ELF file. */
18976
18977 void
18978 mips_elf_final_processing (void)
18979 {
18980 /* Write out the register information. */
18981 if (mips_abi != N64_ABI)
18982 {
18983 Elf32_RegInfo s;
18984
18985 s.ri_gprmask = mips_gprmask;
18986 s.ri_cprmask[0] = mips_cprmask[0];
18987 s.ri_cprmask[1] = mips_cprmask[1];
18988 s.ri_cprmask[2] = mips_cprmask[2];
18989 s.ri_cprmask[3] = mips_cprmask[3];
18990 /* The gp_value field is set by the MIPS ELF backend. */
18991
18992 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18993 ((Elf32_External_RegInfo *)
18994 mips_regmask_frag));
18995 }
18996 else
18997 {
18998 Elf64_Internal_RegInfo s;
18999
19000 s.ri_gprmask = mips_gprmask;
19001 s.ri_pad = 0;
19002 s.ri_cprmask[0] = mips_cprmask[0];
19003 s.ri_cprmask[1] = mips_cprmask[1];
19004 s.ri_cprmask[2] = mips_cprmask[2];
19005 s.ri_cprmask[3] = mips_cprmask[3];
19006 /* The gp_value field is set by the MIPS ELF backend. */
19007
19008 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19009 ((Elf64_External_RegInfo *)
19010 mips_regmask_frag));
19011 }
19012
19013 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19014 sort of BFD interface for this. */
19015 if (mips_any_noreorder)
19016 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19017 if (mips_pic != NO_PIC)
19018 {
19019 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
19020 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19021 }
19022 if (mips_abicalls)
19023 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19024
19025 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19026 defined at present; this might need to change in future. */
19027 if (file_ase_mips16)
19028 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
19029 if (file_ase_micromips)
19030 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
19031 #if 0 /* XXX FIXME */
19032 if (file_ase_mips3d)
19033 elf_elfheader (stdoutput)->e_flags |= ???;
19034 #endif
19035 if (file_ase_mdmx)
19036 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
19037
19038 /* Set the MIPS ELF ABI flags. */
19039 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
19040 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
19041 else if (mips_abi == O64_ABI)
19042 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
19043 else if (mips_abi == EABI_ABI)
19044 {
19045 if (!file_mips_gp32)
19046 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19047 else
19048 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19049 }
19050 else if (mips_abi == N32_ABI)
19051 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
19052
19053 /* Nothing to do for N64_ABI. */
19054
19055 if (mips_32bitmode)
19056 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
19057
19058 #if 0 /* XXX FIXME */
19059 /* 32 bit code with 64 bit FP registers. */
19060 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
19061 elf_elfheader (stdoutput)->e_flags |= ???;
19062 #endif
19063 }
19064
19065 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
19066 \f
19067 typedef struct proc {
19068 symbolS *func_sym;
19069 symbolS *func_end_sym;
19070 unsigned long reg_mask;
19071 unsigned long reg_offset;
19072 unsigned long fpreg_mask;
19073 unsigned long fpreg_offset;
19074 unsigned long frame_offset;
19075 unsigned long frame_reg;
19076 unsigned long pc_reg;
19077 } procS;
19078
19079 static procS cur_proc;
19080 static procS *cur_proc_ptr;
19081 static int numprocs;
19082
19083 /* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19084 as "2", and a normal nop as "0". */
19085
19086 #define NOP_OPCODE_MIPS 0
19087 #define NOP_OPCODE_MIPS16 1
19088 #define NOP_OPCODE_MICROMIPS 2
19089
19090 char
19091 mips_nop_opcode (void)
19092 {
19093 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19094 return NOP_OPCODE_MICROMIPS;
19095 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19096 return NOP_OPCODE_MIPS16;
19097 else
19098 return NOP_OPCODE_MIPS;
19099 }
19100
19101 /* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19102 32-bit microMIPS NOPs here (if applicable). */
19103
19104 void
19105 mips_handle_align (fragS *fragp)
19106 {
19107 char nop_opcode;
19108 char *p;
19109 int bytes, size, excess;
19110 valueT opcode;
19111
19112 if (fragp->fr_type != rs_align_code)
19113 return;
19114
19115 p = fragp->fr_literal + fragp->fr_fix;
19116 nop_opcode = *p;
19117 switch (nop_opcode)
19118 {
19119 case NOP_OPCODE_MICROMIPS:
19120 opcode = micromips_nop32_insn.insn_opcode;
19121 size = 4;
19122 break;
19123 case NOP_OPCODE_MIPS16:
19124 opcode = mips16_nop_insn.insn_opcode;
19125 size = 2;
19126 break;
19127 case NOP_OPCODE_MIPS:
19128 default:
19129 opcode = nop_insn.insn_opcode;
19130 size = 4;
19131 break;
19132 }
19133
19134 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19135 excess = bytes % size;
19136
19137 /* Handle the leading part if we're not inserting a whole number of
19138 instructions, and make it the end of the fixed part of the frag.
19139 Try to fit in a short microMIPS NOP if applicable and possible,
19140 and use zeroes otherwise. */
19141 gas_assert (excess < 4);
19142 fragp->fr_fix += excess;
19143 switch (excess)
19144 {
19145 case 3:
19146 *p++ = '\0';
19147 /* Fall through. */
19148 case 2:
19149 if (nop_opcode == NOP_OPCODE_MICROMIPS)
19150 {
19151 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
19152 break;
19153 }
19154 *p++ = '\0';
19155 /* Fall through. */
19156 case 1:
19157 *p++ = '\0';
19158 /* Fall through. */
19159 case 0:
19160 break;
19161 }
19162
19163 md_number_to_chars (p, opcode, size);
19164 fragp->fr_var = size;
19165 }
19166
19167 static void
19168 md_obj_begin (void)
19169 {
19170 }
19171
19172 static void
19173 md_obj_end (void)
19174 {
19175 /* Check for premature end, nesting errors, etc. */
19176 if (cur_proc_ptr)
19177 as_warn (_("missing .end at end of assembly"));
19178 }
19179
19180 static long
19181 get_number (void)
19182 {
19183 int negative = 0;
19184 long val = 0;
19185
19186 if (*input_line_pointer == '-')
19187 {
19188 ++input_line_pointer;
19189 negative = 1;
19190 }
19191 if (!ISDIGIT (*input_line_pointer))
19192 as_bad (_("expected simple number"));
19193 if (input_line_pointer[0] == '0')
19194 {
19195 if (input_line_pointer[1] == 'x')
19196 {
19197 input_line_pointer += 2;
19198 while (ISXDIGIT (*input_line_pointer))
19199 {
19200 val <<= 4;
19201 val |= hex_value (*input_line_pointer++);
19202 }
19203 return negative ? -val : val;
19204 }
19205 else
19206 {
19207 ++input_line_pointer;
19208 while (ISDIGIT (*input_line_pointer))
19209 {
19210 val <<= 3;
19211 val |= *input_line_pointer++ - '0';
19212 }
19213 return negative ? -val : val;
19214 }
19215 }
19216 if (!ISDIGIT (*input_line_pointer))
19217 {
19218 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19219 *input_line_pointer, *input_line_pointer);
19220 as_warn (_("invalid number"));
19221 return -1;
19222 }
19223 while (ISDIGIT (*input_line_pointer))
19224 {
19225 val *= 10;
19226 val += *input_line_pointer++ - '0';
19227 }
19228 return negative ? -val : val;
19229 }
19230
19231 /* The .file directive; just like the usual .file directive, but there
19232 is an initial number which is the ECOFF file index. In the non-ECOFF
19233 case .file implies DWARF-2. */
19234
19235 static void
19236 s_mips_file (int x ATTRIBUTE_UNUSED)
19237 {
19238 static int first_file_directive = 0;
19239
19240 if (ECOFF_DEBUGGING)
19241 {
19242 get_number ();
19243 s_app_file (0);
19244 }
19245 else
19246 {
19247 char *filename;
19248
19249 filename = dwarf2_directive_file (0);
19250
19251 /* Versions of GCC up to 3.1 start files with a ".file"
19252 directive even for stabs output. Make sure that this
19253 ".file" is handled. Note that you need a version of GCC
19254 after 3.1 in order to support DWARF-2 on MIPS. */
19255 if (filename != NULL && ! first_file_directive)
19256 {
19257 (void) new_logical_line (filename, -1);
19258 s_app_file_string (filename, 0);
19259 }
19260 first_file_directive = 1;
19261 }
19262 }
19263
19264 /* The .loc directive, implying DWARF-2. */
19265
19266 static void
19267 s_mips_loc (int x ATTRIBUTE_UNUSED)
19268 {
19269 if (!ECOFF_DEBUGGING)
19270 dwarf2_directive_loc (0);
19271 }
19272
19273 /* The .end directive. */
19274
19275 static void
19276 s_mips_end (int x ATTRIBUTE_UNUSED)
19277 {
19278 symbolS *p;
19279
19280 /* Following functions need their own .frame and .cprestore directives. */
19281 mips_frame_reg_valid = 0;
19282 mips_cprestore_valid = 0;
19283
19284 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19285 {
19286 p = get_symbol ();
19287 demand_empty_rest_of_line ();
19288 }
19289 else
19290 p = NULL;
19291
19292 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19293 as_warn (_(".end not in text section"));
19294
19295 if (!cur_proc_ptr)
19296 {
19297 as_warn (_(".end directive without a preceding .ent directive."));
19298 demand_empty_rest_of_line ();
19299 return;
19300 }
19301
19302 if (p != NULL)
19303 {
19304 gas_assert (S_GET_NAME (p));
19305 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
19306 as_warn (_(".end symbol does not match .ent symbol."));
19307
19308 if (debug_type == DEBUG_STABS)
19309 stabs_generate_asm_endfunc (S_GET_NAME (p),
19310 S_GET_NAME (p));
19311 }
19312 else
19313 as_warn (_(".end directive missing or unknown symbol"));
19314
19315 #ifdef OBJ_ELF
19316 /* Create an expression to calculate the size of the function. */
19317 if (p && cur_proc_ptr)
19318 {
19319 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19320 expressionS *exp = xmalloc (sizeof (expressionS));
19321
19322 obj->size = exp;
19323 exp->X_op = O_subtract;
19324 exp->X_add_symbol = symbol_temp_new_now ();
19325 exp->X_op_symbol = p;
19326 exp->X_add_number = 0;
19327
19328 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19329 }
19330
19331 /* Generate a .pdr section. */
19332 if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
19333 {
19334 segT saved_seg = now_seg;
19335 subsegT saved_subseg = now_subseg;
19336 expressionS exp;
19337 char *fragp;
19338
19339 #ifdef md_flush_pending_output
19340 md_flush_pending_output ();
19341 #endif
19342
19343 gas_assert (pdr_seg);
19344 subseg_set (pdr_seg, 0);
19345
19346 /* Write the symbol. */
19347 exp.X_op = O_symbol;
19348 exp.X_add_symbol = p;
19349 exp.X_add_number = 0;
19350 emit_expr (&exp, 4);
19351
19352 fragp = frag_more (7 * 4);
19353
19354 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19355 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19356 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19357 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19358 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19359 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19360 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19361
19362 subseg_set (saved_seg, saved_subseg);
19363 }
19364 #endif /* OBJ_ELF */
19365
19366 cur_proc_ptr = NULL;
19367 }
19368
19369 /* The .aent and .ent directives. */
19370
19371 static void
19372 s_mips_ent (int aent)
19373 {
19374 symbolS *symbolP;
19375
19376 symbolP = get_symbol ();
19377 if (*input_line_pointer == ',')
19378 ++input_line_pointer;
19379 SKIP_WHITESPACE ();
19380 if (ISDIGIT (*input_line_pointer)
19381 || *input_line_pointer == '-')
19382 get_number ();
19383
19384 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19385 as_warn (_(".ent or .aent not in text section."));
19386
19387 if (!aent && cur_proc_ptr)
19388 as_warn (_("missing .end"));
19389
19390 if (!aent)
19391 {
19392 /* This function needs its own .frame and .cprestore directives. */
19393 mips_frame_reg_valid = 0;
19394 mips_cprestore_valid = 0;
19395
19396 cur_proc_ptr = &cur_proc;
19397 memset (cur_proc_ptr, '\0', sizeof (procS));
19398
19399 cur_proc_ptr->func_sym = symbolP;
19400
19401 ++numprocs;
19402
19403 if (debug_type == DEBUG_STABS)
19404 stabs_generate_asm_func (S_GET_NAME (symbolP),
19405 S_GET_NAME (symbolP));
19406 }
19407
19408 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19409
19410 demand_empty_rest_of_line ();
19411 }
19412
19413 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19414 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19415 s_mips_frame is used so that we can set the PDR information correctly.
19416 We can't use the ecoff routines because they make reference to the ecoff
19417 symbol table (in the mdebug section). */
19418
19419 static void
19420 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19421 {
19422 #ifdef OBJ_ELF
19423 if (IS_ELF && !ECOFF_DEBUGGING)
19424 {
19425 long val;
19426
19427 if (cur_proc_ptr == (procS *) NULL)
19428 {
19429 as_warn (_(".frame outside of .ent"));
19430 demand_empty_rest_of_line ();
19431 return;
19432 }
19433
19434 cur_proc_ptr->frame_reg = tc_get_register (1);
19435
19436 SKIP_WHITESPACE ();
19437 if (*input_line_pointer++ != ','
19438 || get_absolute_expression_and_terminator (&val) != ',')
19439 {
19440 as_warn (_("Bad .frame directive"));
19441 --input_line_pointer;
19442 demand_empty_rest_of_line ();
19443 return;
19444 }
19445
19446 cur_proc_ptr->frame_offset = val;
19447 cur_proc_ptr->pc_reg = tc_get_register (0);
19448
19449 demand_empty_rest_of_line ();
19450 }
19451 else
19452 #endif /* OBJ_ELF */
19453 s_ignore (ignore);
19454 }
19455
19456 /* The .fmask and .mask directives. If the mdebug section is present
19457 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19458 embedded targets, s_mips_mask is used so that we can set the PDR
19459 information correctly. We can't use the ecoff routines because they
19460 make reference to the ecoff symbol table (in the mdebug section). */
19461
19462 static void
19463 s_mips_mask (int reg_type)
19464 {
19465 #ifdef OBJ_ELF
19466 if (IS_ELF && !ECOFF_DEBUGGING)
19467 {
19468 long mask, off;
19469
19470 if (cur_proc_ptr == (procS *) NULL)
19471 {
19472 as_warn (_(".mask/.fmask outside of .ent"));
19473 demand_empty_rest_of_line ();
19474 return;
19475 }
19476
19477 if (get_absolute_expression_and_terminator (&mask) != ',')
19478 {
19479 as_warn (_("Bad .mask/.fmask directive"));
19480 --input_line_pointer;
19481 demand_empty_rest_of_line ();
19482 return;
19483 }
19484
19485 off = get_absolute_expression ();
19486
19487 if (reg_type == 'F')
19488 {
19489 cur_proc_ptr->fpreg_mask = mask;
19490 cur_proc_ptr->fpreg_offset = off;
19491 }
19492 else
19493 {
19494 cur_proc_ptr->reg_mask = mask;
19495 cur_proc_ptr->reg_offset = off;
19496 }
19497
19498 demand_empty_rest_of_line ();
19499 }
19500 else
19501 #endif /* OBJ_ELF */
19502 s_ignore (reg_type);
19503 }
19504
19505 /* A table describing all the processors gas knows about. Names are
19506 matched in the order listed.
19507
19508 To ease comparison, please keep this table in the same order as
19509 gcc's mips_cpu_info_table[]. */
19510 static const struct mips_cpu_info mips_cpu_info_table[] =
19511 {
19512 /* Entries for generic ISAs */
19513 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19514 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19515 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19516 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19517 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19518 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19519 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19520 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19521 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
19522
19523 /* MIPS I */
19524 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19525 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19526 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
19527
19528 /* MIPS II */
19529 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
19530
19531 /* MIPS III */
19532 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19533 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19534 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19535 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19536 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19537 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19538 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19539 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19540 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19541 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19542 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19543 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19544 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
19545 /* ST Microelectronics Loongson 2E and 2F cores */
19546 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19547 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
19548
19549 /* MIPS IV */
19550 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19551 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19552 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19553 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19554 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19555 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19556 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19557 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19558 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19559 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19560 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19561 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19562 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19563 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19564 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
19565
19566 /* MIPS 32 */
19567 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19568 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19569 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19570 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19571
19572 /* MIPS 32 Release 2 */
19573 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19574 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19575 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19576 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19577 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19578 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19579 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19580 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19581 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19582 ISA_MIPS32R2, CPU_MIPS32R2 },
19583 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19584 ISA_MIPS32R2, CPU_MIPS32R2 },
19585 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19586 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19587 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19588 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19589 /* Deprecated forms of the above. */
19590 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19591 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19592 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
19593 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19594 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19595 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19596 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19597 /* Deprecated forms of the above. */
19598 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19599 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19600 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
19601 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19602 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19603 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19604 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19605 /* Deprecated forms of the above. */
19606 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19607 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19608 /* 34Kn is a 34kc without DSP. */
19609 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19610 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19611 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19612 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19613 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19614 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19615 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19616 /* Deprecated forms of the above. */
19617 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19618 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19619 /* 1004K cores are multiprocessor versions of the 34K. */
19620 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19621 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19622 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19623 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19624
19625 /* MIPS 64 */
19626 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19627 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19628 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19629 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19630
19631 /* Broadcom SB-1 CPU core */
19632 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19633 /* Broadcom SB-1A CPU core */
19634 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19635
19636 { "loongson3a", 0, 0, ISA_MIPS64, CPU_LOONGSON_3A },
19637
19638 /* MIPS 64 Release 2 */
19639
19640 /* Cavium Networks Octeon CPU core */
19641 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19642 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19643 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
19644
19645 /* RMI Xlr */
19646 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
19647
19648 /* Broadcom XLP.
19649 XLP is mostly like XLR, with the prominent exception that it is
19650 MIPS64R2 rather than MIPS64. */
19651 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
19652
19653 /* End marker */
19654 { NULL, 0, 0, 0, 0 }
19655 };
19656
19657
19658 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19659 with a final "000" replaced by "k". Ignore case.
19660
19661 Note: this function is shared between GCC and GAS. */
19662
19663 static bfd_boolean
19664 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19665 {
19666 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19667 given++, canonical++;
19668
19669 return ((*given == 0 && *canonical == 0)
19670 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19671 }
19672
19673
19674 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19675 CPU name. We've traditionally allowed a lot of variation here.
19676
19677 Note: this function is shared between GCC and GAS. */
19678
19679 static bfd_boolean
19680 mips_matching_cpu_name_p (const char *canonical, const char *given)
19681 {
19682 /* First see if the name matches exactly, or with a final "000"
19683 turned into "k". */
19684 if (mips_strict_matching_cpu_name_p (canonical, given))
19685 return TRUE;
19686
19687 /* If not, try comparing based on numerical designation alone.
19688 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19689 if (TOLOWER (*given) == 'r')
19690 given++;
19691 if (!ISDIGIT (*given))
19692 return FALSE;
19693
19694 /* Skip over some well-known prefixes in the canonical name,
19695 hoping to find a number there too. */
19696 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19697 canonical += 2;
19698 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19699 canonical += 2;
19700 else if (TOLOWER (canonical[0]) == 'r')
19701 canonical += 1;
19702
19703 return mips_strict_matching_cpu_name_p (canonical, given);
19704 }
19705
19706
19707 /* Parse an option that takes the name of a processor as its argument.
19708 OPTION is the name of the option and CPU_STRING is the argument.
19709 Return the corresponding processor enumeration if the CPU_STRING is
19710 recognized, otherwise report an error and return null.
19711
19712 A similar function exists in GCC. */
19713
19714 static const struct mips_cpu_info *
19715 mips_parse_cpu (const char *option, const char *cpu_string)
19716 {
19717 const struct mips_cpu_info *p;
19718
19719 /* 'from-abi' selects the most compatible architecture for the given
19720 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19721 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19722 version. Look first at the -mgp options, if given, otherwise base
19723 the choice on MIPS_DEFAULT_64BIT.
19724
19725 Treat NO_ABI like the EABIs. One reason to do this is that the
19726 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19727 architecture. This code picks MIPS I for 'mips' and MIPS III for
19728 'mips64', just as we did in the days before 'from-abi'. */
19729 if (strcasecmp (cpu_string, "from-abi") == 0)
19730 {
19731 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19732 return mips_cpu_info_from_isa (ISA_MIPS1);
19733
19734 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19735 return mips_cpu_info_from_isa (ISA_MIPS3);
19736
19737 if (file_mips_gp32 >= 0)
19738 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19739
19740 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19741 ? ISA_MIPS3
19742 : ISA_MIPS1);
19743 }
19744
19745 /* 'default' has traditionally been a no-op. Probably not very useful. */
19746 if (strcasecmp (cpu_string, "default") == 0)
19747 return 0;
19748
19749 for (p = mips_cpu_info_table; p->name != 0; p++)
19750 if (mips_matching_cpu_name_p (p->name, cpu_string))
19751 return p;
19752
19753 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
19754 return 0;
19755 }
19756
19757 /* Return the canonical processor information for ISA (a member of the
19758 ISA_MIPS* enumeration). */
19759
19760 static const struct mips_cpu_info *
19761 mips_cpu_info_from_isa (int isa)
19762 {
19763 int i;
19764
19765 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19766 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19767 && isa == mips_cpu_info_table[i].isa)
19768 return (&mips_cpu_info_table[i]);
19769
19770 return NULL;
19771 }
19772
19773 static const struct mips_cpu_info *
19774 mips_cpu_info_from_arch (int arch)
19775 {
19776 int i;
19777
19778 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19779 if (arch == mips_cpu_info_table[i].cpu)
19780 return (&mips_cpu_info_table[i]);
19781
19782 return NULL;
19783 }
19784 \f
19785 static void
19786 show (FILE *stream, const char *string, int *col_p, int *first_p)
19787 {
19788 if (*first_p)
19789 {
19790 fprintf (stream, "%24s", "");
19791 *col_p = 24;
19792 }
19793 else
19794 {
19795 fprintf (stream, ", ");
19796 *col_p += 2;
19797 }
19798
19799 if (*col_p + strlen (string) > 72)
19800 {
19801 fprintf (stream, "\n%24s", "");
19802 *col_p = 24;
19803 }
19804
19805 fprintf (stream, "%s", string);
19806 *col_p += strlen (string);
19807
19808 *first_p = 0;
19809 }
19810
19811 void
19812 md_show_usage (FILE *stream)
19813 {
19814 int column, first;
19815 size_t i;
19816
19817 fprintf (stream, _("\
19818 MIPS options:\n\
19819 -EB generate big endian output\n\
19820 -EL generate little endian output\n\
19821 -g, -g2 do not remove unneeded NOPs or swap branches\n\
19822 -G NUM allow referencing objects up to NUM bytes\n\
19823 implicitly with the gp register [default 8]\n"));
19824 fprintf (stream, _("\
19825 -mips1 generate MIPS ISA I instructions\n\
19826 -mips2 generate MIPS ISA II instructions\n\
19827 -mips3 generate MIPS ISA III instructions\n\
19828 -mips4 generate MIPS ISA IV instructions\n\
19829 -mips5 generate MIPS ISA V instructions\n\
19830 -mips32 generate MIPS32 ISA instructions\n\
19831 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
19832 -mips64 generate MIPS64 ISA instructions\n\
19833 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
19834 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19835
19836 first = 1;
19837
19838 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19839 show (stream, mips_cpu_info_table[i].name, &column, &first);
19840 show (stream, "from-abi", &column, &first);
19841 fputc ('\n', stream);
19842
19843 fprintf (stream, _("\
19844 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19845 -no-mCPU don't generate code specific to CPU.\n\
19846 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19847
19848 first = 1;
19849
19850 show (stream, "3900", &column, &first);
19851 show (stream, "4010", &column, &first);
19852 show (stream, "4100", &column, &first);
19853 show (stream, "4650", &column, &first);
19854 fputc ('\n', stream);
19855
19856 fprintf (stream, _("\
19857 -mips16 generate mips16 instructions\n\
19858 -no-mips16 do not generate mips16 instructions\n"));
19859 fprintf (stream, _("\
19860 -mmicromips generate microMIPS instructions\n\
19861 -mno-micromips do not generate microMIPS instructions\n"));
19862 fprintf (stream, _("\
19863 -msmartmips generate smartmips instructions\n\
19864 -mno-smartmips do not generate smartmips instructions\n"));
19865 fprintf (stream, _("\
19866 -mdsp generate DSP instructions\n\
19867 -mno-dsp do not generate DSP instructions\n"));
19868 fprintf (stream, _("\
19869 -mdspr2 generate DSP R2 instructions\n\
19870 -mno-dspr2 do not generate DSP R2 instructions\n"));
19871 fprintf (stream, _("\
19872 -mmt generate MT instructions\n\
19873 -mno-mt do not generate MT instructions\n"));
19874 fprintf (stream, _("\
19875 -mmcu generate MCU instructions\n\
19876 -mno-mcu do not generate MCU instructions\n"));
19877 fprintf (stream, _("\
19878 -mvirt generate Virtualization instructions\n\
19879 -mno-virt do not generate Virtualization instructions\n"));
19880 fprintf (stream, _("\
19881 -mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19882 -mfix-loongson2f-nop work around Loongson2F NOP errata\n\
19883 -mfix-vr4120 work around certain VR4120 errata\n\
19884 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
19885 -mfix-24k insert a nop after ERET and DERET instructions\n\
19886 -mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
19887 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19888 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
19889 -msym32 assume all symbols have 32-bit values\n\
19890 -O0 remove unneeded NOPs, do not swap branches\n\
19891 -O remove unneeded NOPs and swap branches\n\
19892 --trap, --no-break trap exception on div by 0 and mult overflow\n\
19893 --break, --no-trap break exception on div by 0 and mult overflow\n"));
19894 fprintf (stream, _("\
19895 -mhard-float allow floating-point instructions\n\
19896 -msoft-float do not allow floating-point instructions\n\
19897 -msingle-float only allow 32-bit floating-point operations\n\
19898 -mdouble-float allow 32-bit and 64-bit floating-point operations\n\
19899 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
19900 --[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n"
19901 ));
19902 #ifdef OBJ_ELF
19903 fprintf (stream, _("\
19904 -KPIC, -call_shared generate SVR4 position independent code\n\
19905 -call_nonpic generate non-PIC code that can operate with DSOs\n\
19906 -mvxworks-pic generate VxWorks position independent code\n\
19907 -non_shared do not generate code that can operate with DSOs\n\
19908 -xgot assume a 32 bit GOT\n\
19909 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
19910 -mshared, -mno-shared disable/enable .cpload optimization for\n\
19911 position dependent (non shared) code\n\
19912 -mabi=ABI create ABI conformant object file for:\n"));
19913
19914 first = 1;
19915
19916 show (stream, "32", &column, &first);
19917 show (stream, "o64", &column, &first);
19918 show (stream, "n32", &column, &first);
19919 show (stream, "64", &column, &first);
19920 show (stream, "eabi", &column, &first);
19921
19922 fputc ('\n', stream);
19923
19924 fprintf (stream, _("\
19925 -32 create o32 ABI object file (default)\n\
19926 -n32 create n32 ABI object file\n\
19927 -64 create 64 ABI object file\n"));
19928 #endif
19929 }
19930
19931 #ifdef TE_IRIX
19932 enum dwarf2_format
19933 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
19934 {
19935 if (HAVE_64BIT_SYMBOLS)
19936 return dwarf2_format_64bit_irix;
19937 else
19938 return dwarf2_format_32bit;
19939 }
19940 #endif
19941
19942 int
19943 mips_dwarf2_addr_size (void)
19944 {
19945 if (HAVE_64BIT_OBJECTS)
19946 return 8;
19947 else
19948 return 4;
19949 }
19950
19951 /* Standard calling conventions leave the CFA at SP on entry. */
19952 void
19953 mips_cfi_frame_initial_instructions (void)
19954 {
19955 cfi_add_CFA_def_cfa_register (SP);
19956 }
19957
19958 int
19959 tc_mips_regname_to_dw2regnum (char *regname)
19960 {
19961 unsigned int regnum = -1;
19962 unsigned int reg;
19963
19964 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19965 regnum = reg;
19966
19967 return regnum;
19968 }
This page took 0.447334 seconds and 4 git commands to generate.