012935d354c5c89acba808f7509405db218a74d3
[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_mt;
220 int ase_mcu;
221 /* Whether we are assembling for the mips16 processor. 0 if we are
222 not, 1 if we are, and -1 if the value has not been initialized.
223 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
224 -nomips16 command line options, and the default CPU. */
225 int mips16;
226 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
227 1 if we are, and -1 if the value has not been initialized. Changed
228 by `.set micromips' and `.set nomicromips', and the -mmicromips
229 and -mno-micromips command line options, and the default CPU. */
230 int micromips;
231 /* Non-zero if we should not reorder instructions. Changed by `.set
232 reorder' and `.set noreorder'. */
233 int noreorder;
234 /* Non-zero if we should not permit the register designated "assembler
235 temporary" to be used in instructions. The value is the register
236 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
237 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
238 unsigned int at;
239 /* Non-zero if we should warn when a macro instruction expands into
240 more than one machine instruction. Changed by `.set nomacro' and
241 `.set macro'. */
242 int warn_about_macros;
243 /* Non-zero if we should not move instructions. Changed by `.set
244 move', `.set volatile', `.set nomove', and `.set novolatile'. */
245 int nomove;
246 /* Non-zero if we should not optimize branches by moving the target
247 of the branch into the delay slot. Actually, we don't perform
248 this optimization anyhow. Changed by `.set bopt' and `.set
249 nobopt'. */
250 int nobopt;
251 /* Non-zero if we should not autoextend mips16 instructions.
252 Changed by `.set autoextend' and `.set noautoextend'. */
253 int noautoextend;
254 /* Restrict general purpose registers and floating point registers
255 to 32 bit. This is initially determined when -mgp32 or -mfp32
256 is passed but can changed if the assembler code uses .set mipsN. */
257 int gp32;
258 int fp32;
259 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
260 command line option, and the default CPU. */
261 int arch;
262 /* True if ".set sym32" is in effect. */
263 bfd_boolean sym32;
264 /* True if floating-point operations are not allowed. Changed by .set
265 softfloat or .set hardfloat, by command line options -msoft-float or
266 -mhard-float. The default is false. */
267 bfd_boolean soft_float;
268
269 /* True if only single-precision floating-point operations are allowed.
270 Changed by .set singlefloat or .set doublefloat, command-line options
271 -msingle-float or -mdouble-float. The default is false. */
272 bfd_boolean single_float;
273 };
274
275 /* This is the struct we use to hold the current set of options. Note
276 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
277 -1 to indicate that they have not been initialized. */
278
279 /* True if -mgp32 was passed. */
280 static int file_mips_gp32 = -1;
281
282 /* True if -mfp32 was passed. */
283 static int file_mips_fp32 = -1;
284
285 /* 1 if -msoft-float, 0 if -mhard-float. The default is 0. */
286 static int file_mips_soft_float = 0;
287
288 /* 1 if -msingle-float, 0 if -mdouble-float. The default is 0. */
289 static int file_mips_single_float = 0;
290
291 static struct mips_set_options mips_opts =
292 {
293 /* isa */ ISA_UNKNOWN, /* ase_mips3d */ -1, /* ase_mdmx */ -1,
294 /* ase_smartmips */ 0, /* ase_dsp */ -1, /* ase_dspr2 */ -1, /* ase_mt */ -1,
295 /* ase_mcu */ -1, /* mips16 */ -1, /* micromips */ -1, /* noreorder */ 0,
296 /* at */ ATREG, /* warn_about_macros */ 0, /* nomove */ 0, /* nobopt */ 0,
297 /* noautoextend */ 0, /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN,
298 /* sym32 */ FALSE, /* soft_float */ FALSE, /* single_float */ FALSE
299 };
300
301 /* These variables are filled in with the masks of registers used.
302 The object format code reads them and puts them in the appropriate
303 place. */
304 unsigned long mips_gprmask;
305 unsigned long mips_cprmask[4];
306
307 /* MIPS ISA we are using for this output file. */
308 static int file_mips_isa = ISA_UNKNOWN;
309
310 /* True if any MIPS16 code was produced. */
311 static int file_ase_mips16;
312
313 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
314 || mips_opts.isa == ISA_MIPS32R2 \
315 || mips_opts.isa == ISA_MIPS64 \
316 || mips_opts.isa == ISA_MIPS64R2)
317
318 /* True if any microMIPS code was produced. */
319 static int file_ase_micromips;
320
321 /* True if we want to create R_MIPS_JALR for jalr $25. */
322 #ifdef TE_IRIX
323 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
324 #else
325 /* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
326 because there's no place for any addend, the only acceptable
327 expression is a bare symbol. */
328 #define MIPS_JALR_HINT_P(EXPR) \
329 (!HAVE_IN_PLACE_ADDENDS \
330 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
331 #endif
332
333 /* True if -mips3d was passed or implied by arguments passed on the
334 command line (e.g., by -march). */
335 static int file_ase_mips3d;
336
337 /* True if -mdmx was passed or implied by arguments passed on the
338 command line (e.g., by -march). */
339 static int file_ase_mdmx;
340
341 /* True if -msmartmips was passed or implied by arguments passed on the
342 command line (e.g., by -march). */
343 static int file_ase_smartmips;
344
345 #define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32 \
346 || mips_opts.isa == ISA_MIPS32R2)
347
348 /* True if -mdsp was passed or implied by arguments passed on the
349 command line (e.g., by -march). */
350 static int file_ase_dsp;
351
352 #define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2 \
353 || mips_opts.isa == ISA_MIPS64R2 \
354 || mips_opts.micromips)
355
356 #define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
357
358 /* True if -mdspr2 was passed or implied by arguments passed on the
359 command line (e.g., by -march). */
360 static int file_ase_dspr2;
361
362 #define ISA_SUPPORTS_DSPR2_ASE (mips_opts.isa == ISA_MIPS32R2 \
363 || mips_opts.isa == ISA_MIPS64R2 \
364 || mips_opts.micromips)
365
366 /* True if -mmt was passed or implied by arguments passed on the
367 command line (e.g., by -march). */
368 static int file_ase_mt;
369
370 #define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2 \
371 || mips_opts.isa == ISA_MIPS64R2)
372
373 #define ISA_SUPPORTS_MCU_ASE (mips_opts.isa == ISA_MIPS32R2 \
374 || mips_opts.isa == ISA_MIPS64R2 \
375 || mips_opts.micromips)
376
377 /* The argument of the -march= flag. The architecture we are assembling. */
378 static int file_mips_arch = CPU_UNKNOWN;
379 static const char *mips_arch_string;
380
381 /* The argument of the -mtune= flag. The architecture for which we
382 are optimizing. */
383 static int mips_tune = CPU_UNKNOWN;
384 static const char *mips_tune_string;
385
386 /* True when generating 32-bit code for a 64-bit processor. */
387 static int mips_32bitmode = 0;
388
389 /* True if the given ABI requires 32-bit registers. */
390 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
391
392 /* Likewise 64-bit registers. */
393 #define ABI_NEEDS_64BIT_REGS(ABI) \
394 ((ABI) == N32_ABI \
395 || (ABI) == N64_ABI \
396 || (ABI) == O64_ABI)
397
398 /* Return true if ISA supports 64 bit wide gp registers. */
399 #define ISA_HAS_64BIT_REGS(ISA) \
400 ((ISA) == ISA_MIPS3 \
401 || (ISA) == ISA_MIPS4 \
402 || (ISA) == ISA_MIPS5 \
403 || (ISA) == ISA_MIPS64 \
404 || (ISA) == ISA_MIPS64R2)
405
406 /* Return true if ISA supports 64 bit wide float registers. */
407 #define ISA_HAS_64BIT_FPRS(ISA) \
408 ((ISA) == ISA_MIPS3 \
409 || (ISA) == ISA_MIPS4 \
410 || (ISA) == ISA_MIPS5 \
411 || (ISA) == ISA_MIPS32R2 \
412 || (ISA) == ISA_MIPS64 \
413 || (ISA) == ISA_MIPS64R2)
414
415 /* Return true if ISA supports 64-bit right rotate (dror et al.)
416 instructions. */
417 #define ISA_HAS_DROR(ISA) \
418 ((ISA) == ISA_MIPS64R2 \
419 || (mips_opts.micromips \
420 && ISA_HAS_64BIT_REGS (ISA)) \
421 )
422
423 /* Return true if ISA supports 32-bit right rotate (ror et al.)
424 instructions. */
425 #define ISA_HAS_ROR(ISA) \
426 ((ISA) == ISA_MIPS32R2 \
427 || (ISA) == ISA_MIPS64R2 \
428 || mips_opts.ase_smartmips \
429 || mips_opts.micromips \
430 )
431
432 /* Return true if ISA supports single-precision floats in odd registers. */
433 #define ISA_HAS_ODD_SINGLE_FPR(ISA) \
434 ((ISA) == ISA_MIPS32 \
435 || (ISA) == ISA_MIPS32R2 \
436 || (ISA) == ISA_MIPS64 \
437 || (ISA) == ISA_MIPS64R2)
438
439 /* Return true if ISA supports move to/from high part of a 64-bit
440 floating-point register. */
441 #define ISA_HAS_MXHC1(ISA) \
442 ((ISA) == ISA_MIPS32R2 \
443 || (ISA) == ISA_MIPS64R2)
444
445 #define HAVE_32BIT_GPRS \
446 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
447
448 #define HAVE_32BIT_FPRS \
449 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
450
451 #define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
452 #define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
453
454 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
455
456 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
457
458 /* True if relocations are stored in-place. */
459 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
460
461 /* The ABI-derived address size. */
462 #define HAVE_64BIT_ADDRESSES \
463 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
464 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
465
466 /* The size of symbolic constants (i.e., expressions of the form
467 "SYMBOL" or "SYMBOL + OFFSET"). */
468 #define HAVE_32BIT_SYMBOLS \
469 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
470 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
471
472 /* Addresses are loaded in different ways, depending on the address size
473 in use. The n32 ABI Documentation also mandates the use of additions
474 with overflow checking, but existing implementations don't follow it. */
475 #define ADDRESS_ADD_INSN \
476 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
477
478 #define ADDRESS_ADDI_INSN \
479 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
480
481 #define ADDRESS_LOAD_INSN \
482 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
483
484 #define ADDRESS_STORE_INSN \
485 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
486
487 /* Return true if the given CPU supports the MIPS16 ASE. */
488 #define CPU_HAS_MIPS16(cpu) \
489 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
490 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
491
492 /* Return true if the given CPU supports the microMIPS ASE. */
493 #define CPU_HAS_MICROMIPS(cpu) 0
494
495 /* True if CPU has a dror instruction. */
496 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
497
498 /* True if CPU has a ror instruction. */
499 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
500
501 /* True if CPU is in the Octeon family */
502 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
503
504 /* True if CPU has seq/sne and seqi/snei instructions. */
505 #define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
506
507 /* True, if CPU has support for ldc1 and sdc1. */
508 #define CPU_HAS_LDC1_SDC1(CPU) \
509 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
510
511 /* True if mflo and mfhi can be immediately followed by instructions
512 which write to the HI and LO registers.
513
514 According to MIPS specifications, MIPS ISAs I, II, and III need
515 (at least) two instructions between the reads of HI/LO and
516 instructions which write them, and later ISAs do not. Contradicting
517 the MIPS specifications, some MIPS IV processor user manuals (e.g.
518 the UM for the NEC Vr5000) document needing the instructions between
519 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
520 MIPS64 and later ISAs to have the interlocks, plus any specific
521 earlier-ISA CPUs for which CPU documentation declares that the
522 instructions are really interlocked. */
523 #define hilo_interlocks \
524 (mips_opts.isa == ISA_MIPS32 \
525 || mips_opts.isa == ISA_MIPS32R2 \
526 || mips_opts.isa == ISA_MIPS64 \
527 || mips_opts.isa == ISA_MIPS64R2 \
528 || mips_opts.arch == CPU_R4010 \
529 || mips_opts.arch == CPU_R5900 \
530 || mips_opts.arch == CPU_R10000 \
531 || mips_opts.arch == CPU_R12000 \
532 || mips_opts.arch == CPU_R14000 \
533 || mips_opts.arch == CPU_R16000 \
534 || mips_opts.arch == CPU_RM7000 \
535 || mips_opts.arch == CPU_VR5500 \
536 || mips_opts.micromips \
537 )
538
539 /* Whether the processor uses hardware interlocks to protect reads
540 from the GPRs after they are loaded from memory, and thus does not
541 require nops to be inserted. This applies to instructions marked
542 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
543 level I and microMIPS mode instructions are always interlocked. */
544 #define gpr_interlocks \
545 (mips_opts.isa != ISA_MIPS1 \
546 || mips_opts.arch == CPU_R3900 \
547 || mips_opts.arch == CPU_R5900 \
548 || mips_opts.micromips \
549 )
550
551 /* Whether the processor uses hardware interlocks to avoid delays
552 required by coprocessor instructions, and thus does not require
553 nops to be inserted. This applies to instructions marked
554 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
555 between instructions marked INSN_WRITE_COND_CODE and ones marked
556 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
557 levels I, II, and III and microMIPS mode instructions are always
558 interlocked. */
559 /* Itbl support may require additional care here. */
560 #define cop_interlocks \
561 ((mips_opts.isa != ISA_MIPS1 \
562 && mips_opts.isa != ISA_MIPS2 \
563 && mips_opts.isa != ISA_MIPS3) \
564 || mips_opts.arch == CPU_R4300 \
565 || mips_opts.micromips \
566 )
567
568 /* Whether the processor uses hardware interlocks to protect reads
569 from coprocessor registers after they are loaded from memory, and
570 thus does not require nops to be inserted. This applies to
571 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
572 requires at MIPS ISA level I and microMIPS mode instructions are
573 always interlocked. */
574 #define cop_mem_interlocks \
575 (mips_opts.isa != ISA_MIPS1 \
576 || mips_opts.micromips \
577 )
578
579 /* Is this a mfhi or mflo instruction? */
580 #define MF_HILO_INSN(PINFO) \
581 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
582
583 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
584 has been selected. This implies, in particular, that addresses of text
585 labels have their LSB set. */
586 #define HAVE_CODE_COMPRESSION \
587 ((mips_opts.mips16 | mips_opts.micromips) != 0)
588
589 /* MIPS PIC level. */
590
591 enum mips_pic_level mips_pic;
592
593 /* 1 if we should generate 32 bit offsets from the $gp register in
594 SVR4_PIC mode. Currently has no meaning in other modes. */
595 static int mips_big_got = 0;
596
597 /* 1 if trap instructions should used for overflow rather than break
598 instructions. */
599 static int mips_trap = 0;
600
601 /* 1 if double width floating point constants should not be constructed
602 by assembling two single width halves into two single width floating
603 point registers which just happen to alias the double width destination
604 register. On some architectures this aliasing can be disabled by a bit
605 in the status register, and the setting of this bit cannot be determined
606 automatically at assemble time. */
607 static int mips_disable_float_construction;
608
609 /* Non-zero if any .set noreorder directives were used. */
610
611 static int mips_any_noreorder;
612
613 /* Non-zero if nops should be inserted when the register referenced in
614 an mfhi/mflo instruction is read in the next two instructions. */
615 static int mips_7000_hilo_fix;
616
617 /* The size of objects in the small data section. */
618 static unsigned int g_switch_value = 8;
619 /* Whether the -G option was used. */
620 static int g_switch_seen = 0;
621
622 #define N_RMASK 0xc4
623 #define N_VFP 0xd4
624
625 /* If we can determine in advance that GP optimization won't be
626 possible, we can skip the relaxation stuff that tries to produce
627 GP-relative references. This makes delay slot optimization work
628 better.
629
630 This function can only provide a guess, but it seems to work for
631 gcc output. It needs to guess right for gcc, otherwise gcc
632 will put what it thinks is a GP-relative instruction in a branch
633 delay slot.
634
635 I don't know if a fix is needed for the SVR4_PIC mode. I've only
636 fixed it for the non-PIC mode. KR 95/04/07 */
637 static int nopic_need_relax (symbolS *, int);
638
639 /* handle of the OPCODE hash table */
640 static struct hash_control *op_hash = NULL;
641
642 /* The opcode hash table we use for the mips16. */
643 static struct hash_control *mips16_op_hash = NULL;
644
645 /* The opcode hash table we use for the microMIPS ASE. */
646 static struct hash_control *micromips_op_hash = NULL;
647
648 /* This array holds the chars that always start a comment. If the
649 pre-processor is disabled, these aren't very useful */
650 const char comment_chars[] = "#";
651
652 /* This array holds the chars that only start a comment at the beginning of
653 a line. If the line seems to have the form '# 123 filename'
654 .line and .file directives will appear in the pre-processed output */
655 /* Note that input_file.c hand checks for '#' at the beginning of the
656 first line of the input file. This is because the compiler outputs
657 #NO_APP at the beginning of its output. */
658 /* Also note that C style comments are always supported. */
659 const char line_comment_chars[] = "#";
660
661 /* This array holds machine specific line separator characters. */
662 const char line_separator_chars[] = ";";
663
664 /* Chars that can be used to separate mant from exp in floating point nums */
665 const char EXP_CHARS[] = "eE";
666
667 /* Chars that mean this number is a floating point constant */
668 /* As in 0f12.456 */
669 /* or 0d1.2345e12 */
670 const char FLT_CHARS[] = "rRsSfFdDxXpP";
671
672 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
673 changed in read.c . Ideally it shouldn't have to know about it at all,
674 but nothing is ideal around here.
675 */
676
677 static char *insn_error;
678
679 static int auto_align = 1;
680
681 /* When outputting SVR4 PIC code, the assembler needs to know the
682 offset in the stack frame from which to restore the $gp register.
683 This is set by the .cprestore pseudo-op, and saved in this
684 variable. */
685 static offsetT mips_cprestore_offset = -1;
686
687 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
688 more optimizations, it can use a register value instead of a memory-saved
689 offset and even an other register than $gp as global pointer. */
690 static offsetT mips_cpreturn_offset = -1;
691 static int mips_cpreturn_register = -1;
692 static int mips_gp_register = GP;
693 static int mips_gprel_offset = 0;
694
695 /* Whether mips_cprestore_offset has been set in the current function
696 (or whether it has already been warned about, if not). */
697 static int mips_cprestore_valid = 0;
698
699 /* This is the register which holds the stack frame, as set by the
700 .frame pseudo-op. This is needed to implement .cprestore. */
701 static int mips_frame_reg = SP;
702
703 /* Whether mips_frame_reg has been set in the current function
704 (or whether it has already been warned about, if not). */
705 static int mips_frame_reg_valid = 0;
706
707 /* To output NOP instructions correctly, we need to keep information
708 about the previous two instructions. */
709
710 /* Whether we are optimizing. The default value of 2 means to remove
711 unneeded NOPs and swap branch instructions when possible. A value
712 of 1 means to not swap branches. A value of 0 means to always
713 insert NOPs. */
714 static int mips_optimize = 2;
715
716 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
717 equivalent to seeing no -g option at all. */
718 static int mips_debug = 0;
719
720 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
721 #define MAX_VR4130_NOPS 4
722
723 /* The maximum number of NOPs needed to fill delay slots. */
724 #define MAX_DELAY_NOPS 2
725
726 /* The maximum number of NOPs needed for any purpose. */
727 #define MAX_NOPS 4
728
729 /* A list of previous instructions, with index 0 being the most recent.
730 We need to look back MAX_NOPS instructions when filling delay slots
731 or working around processor errata. We need to look back one
732 instruction further if we're thinking about using history[0] to
733 fill a branch delay slot. */
734 static struct mips_cl_insn history[1 + MAX_NOPS];
735
736 /* Nop instructions used by emit_nop. */
737 static struct mips_cl_insn nop_insn;
738 static struct mips_cl_insn mips16_nop_insn;
739 static struct mips_cl_insn micromips_nop16_insn;
740 static struct mips_cl_insn micromips_nop32_insn;
741
742 /* The appropriate nop for the current mode. */
743 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn \
744 : (mips_opts.micromips ? &micromips_nop16_insn : &nop_insn))
745
746 /* The size of NOP_INSN in bytes. */
747 #define NOP_INSN_SIZE (HAVE_CODE_COMPRESSION ? 2 : 4)
748
749 /* If this is set, it points to a frag holding nop instructions which
750 were inserted before the start of a noreorder section. If those
751 nops turn out to be unnecessary, the size of the frag can be
752 decreased. */
753 static fragS *prev_nop_frag;
754
755 /* The number of nop instructions we created in prev_nop_frag. */
756 static int prev_nop_frag_holds;
757
758 /* The number of nop instructions that we know we need in
759 prev_nop_frag. */
760 static int prev_nop_frag_required;
761
762 /* The number of instructions we've seen since prev_nop_frag. */
763 static int prev_nop_frag_since;
764
765 /* For ECOFF and ELF, relocations against symbols are done in two
766 parts, with a HI relocation and a LO relocation. Each relocation
767 has only 16 bits of space to store an addend. This means that in
768 order for the linker to handle carries correctly, it must be able
769 to locate both the HI and the LO relocation. This means that the
770 relocations must appear in order in the relocation table.
771
772 In order to implement this, we keep track of each unmatched HI
773 relocation. We then sort them so that they immediately precede the
774 corresponding LO relocation. */
775
776 struct mips_hi_fixup
777 {
778 /* Next HI fixup. */
779 struct mips_hi_fixup *next;
780 /* This fixup. */
781 fixS *fixp;
782 /* The section this fixup is in. */
783 segT seg;
784 };
785
786 /* The list of unmatched HI relocs. */
787
788 static struct mips_hi_fixup *mips_hi_fixup_list;
789
790 /* The frag containing the last explicit relocation operator.
791 Null if explicit relocations have not been used. */
792
793 static fragS *prev_reloc_op_frag;
794
795 /* Map normal MIPS register numbers to mips16 register numbers. */
796
797 #define X ILLEGAL_REG
798 static const int mips32_to_16_reg_map[] =
799 {
800 X, X, 2, 3, 4, 5, 6, 7,
801 X, X, X, X, X, X, X, X,
802 0, 1, X, X, X, X, X, X,
803 X, X, X, X, X, X, X, X
804 };
805 #undef X
806
807 /* Map mips16 register numbers to normal MIPS register numbers. */
808
809 static const unsigned int mips16_to_32_reg_map[] =
810 {
811 16, 17, 2, 3, 4, 5, 6, 7
812 };
813
814 /* Map normal MIPS register numbers to microMIPS register numbers. */
815
816 #define mips32_to_micromips_reg_b_map mips32_to_16_reg_map
817 #define mips32_to_micromips_reg_c_map mips32_to_16_reg_map
818 #define mips32_to_micromips_reg_d_map mips32_to_16_reg_map
819 #define mips32_to_micromips_reg_e_map mips32_to_16_reg_map
820 #define mips32_to_micromips_reg_f_map mips32_to_16_reg_map
821 #define mips32_to_micromips_reg_g_map mips32_to_16_reg_map
822 #define mips32_to_micromips_reg_l_map mips32_to_16_reg_map
823
824 #define X ILLEGAL_REG
825 /* reg type h: 4, 5, 6. */
826 static const int mips32_to_micromips_reg_h_map[] =
827 {
828 X, X, X, X, 4, 5, 6, X,
829 X, X, X, X, X, X, X, X,
830 X, X, X, X, X, X, X, X,
831 X, X, X, X, X, X, X, X
832 };
833
834 /* reg type m: 0, 17, 2, 3, 16, 18, 19, 20. */
835 static const int mips32_to_micromips_reg_m_map[] =
836 {
837 0, X, 2, 3, X, X, X, X,
838 X, X, X, X, X, X, X, X,
839 4, 1, 5, 6, 7, X, X, X,
840 X, X, X, X, X, X, X, X
841 };
842
843 /* reg type q: 0, 2-7. 17. */
844 static const int mips32_to_micromips_reg_q_map[] =
845 {
846 0, X, 2, 3, 4, 5, 6, 7,
847 X, X, X, X, X, X, X, X,
848 X, 1, X, X, X, X, X, X,
849 X, X, X, X, X, X, X, X
850 };
851
852 #define mips32_to_micromips_reg_n_map mips32_to_micromips_reg_m_map
853 #undef X
854
855 /* Map microMIPS register numbers to normal MIPS register numbers. */
856
857 #define micromips_to_32_reg_b_map mips16_to_32_reg_map
858 #define micromips_to_32_reg_c_map mips16_to_32_reg_map
859 #define micromips_to_32_reg_d_map mips16_to_32_reg_map
860 #define micromips_to_32_reg_e_map mips16_to_32_reg_map
861 #define micromips_to_32_reg_f_map mips16_to_32_reg_map
862 #define micromips_to_32_reg_g_map mips16_to_32_reg_map
863
864 /* The microMIPS registers with type h. */
865 static const unsigned int micromips_to_32_reg_h_map[] =
866 {
867 5, 5, 6, 4, 4, 4, 4, 4
868 };
869
870 /* The microMIPS registers with type i. */
871 static const unsigned int micromips_to_32_reg_i_map[] =
872 {
873 6, 7, 7, 21, 22, 5, 6, 7
874 };
875
876 #define micromips_to_32_reg_l_map mips16_to_32_reg_map
877
878 /* The microMIPS registers with type m. */
879 static const unsigned int micromips_to_32_reg_m_map[] =
880 {
881 0, 17, 2, 3, 16, 18, 19, 20
882 };
883
884 #define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
885
886 /* The microMIPS registers with type q. */
887 static const unsigned int micromips_to_32_reg_q_map[] =
888 {
889 0, 17, 2, 3, 4, 5, 6, 7
890 };
891
892 /* microMIPS imm type B. */
893 static const int micromips_imm_b_map[] =
894 {
895 1, 4, 8, 12, 16, 20, 24, -1
896 };
897
898 /* microMIPS imm type C. */
899 static const int micromips_imm_c_map[] =
900 {
901 128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
902 };
903
904 /* Classifies the kind of instructions we're interested in when
905 implementing -mfix-vr4120. */
906 enum fix_vr4120_class
907 {
908 FIX_VR4120_MACC,
909 FIX_VR4120_DMACC,
910 FIX_VR4120_MULT,
911 FIX_VR4120_DMULT,
912 FIX_VR4120_DIV,
913 FIX_VR4120_MTHILO,
914 NUM_FIX_VR4120_CLASSES
915 };
916
917 /* ...likewise -mfix-loongson2f-jump. */
918 static bfd_boolean mips_fix_loongson2f_jump;
919
920 /* ...likewise -mfix-loongson2f-nop. */
921 static bfd_boolean mips_fix_loongson2f_nop;
922
923 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
924 static bfd_boolean mips_fix_loongson2f;
925
926 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
927 there must be at least one other instruction between an instruction
928 of type X and an instruction of type Y. */
929 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
930
931 /* True if -mfix-vr4120 is in force. */
932 static int mips_fix_vr4120;
933
934 /* ...likewise -mfix-vr4130. */
935 static int mips_fix_vr4130;
936
937 /* ...likewise -mfix-24k. */
938 static int mips_fix_24k;
939
940 /* ...likewise -mfix-cn63xxp1 */
941 static bfd_boolean mips_fix_cn63xxp1;
942
943 /* We don't relax branches by default, since this causes us to expand
944 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
945 fail to compute the offset before expanding the macro to the most
946 efficient expansion. */
947
948 static int mips_relax_branch;
949 \f
950 /* The expansion of many macros depends on the type of symbol that
951 they refer to. For example, when generating position-dependent code,
952 a macro that refers to a symbol may have two different expansions,
953 one which uses GP-relative addresses and one which uses absolute
954 addresses. When generating SVR4-style PIC, a macro may have
955 different expansions for local and global symbols.
956
957 We handle these situations by generating both sequences and putting
958 them in variant frags. In position-dependent code, the first sequence
959 will be the GP-relative one and the second sequence will be the
960 absolute one. In SVR4 PIC, the first sequence will be for global
961 symbols and the second will be for local symbols.
962
963 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
964 SECOND are the lengths of the two sequences in bytes. These fields
965 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
966 the subtype has the following flags:
967
968 RELAX_USE_SECOND
969 Set if it has been decided that we should use the second
970 sequence instead of the first.
971
972 RELAX_SECOND_LONGER
973 Set in the first variant frag if the macro's second implementation
974 is longer than its first. This refers to the macro as a whole,
975 not an individual relaxation.
976
977 RELAX_NOMACRO
978 Set in the first variant frag if the macro appeared in a .set nomacro
979 block and if one alternative requires a warning but the other does not.
980
981 RELAX_DELAY_SLOT
982 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
983 delay slot.
984
985 RELAX_DELAY_SLOT_16BIT
986 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
987 16-bit instruction.
988
989 RELAX_DELAY_SLOT_SIZE_FIRST
990 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
991 the macro is of the wrong size for the branch delay slot.
992
993 RELAX_DELAY_SLOT_SIZE_SECOND
994 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
995 the macro is of the wrong size for the branch delay slot.
996
997 The frag's "opcode" points to the first fixup for relaxable code.
998
999 Relaxable macros are generated using a sequence such as:
1000
1001 relax_start (SYMBOL);
1002 ... generate first expansion ...
1003 relax_switch ();
1004 ... generate second expansion ...
1005 relax_end ();
1006
1007 The code and fixups for the unwanted alternative are discarded
1008 by md_convert_frag. */
1009 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
1010
1011 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1012 #define RELAX_SECOND(X) ((X) & 0xff)
1013 #define RELAX_USE_SECOND 0x10000
1014 #define RELAX_SECOND_LONGER 0x20000
1015 #define RELAX_NOMACRO 0x40000
1016 #define RELAX_DELAY_SLOT 0x80000
1017 #define RELAX_DELAY_SLOT_16BIT 0x100000
1018 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
1019 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
1020
1021 /* Branch without likely bit. If label is out of range, we turn:
1022
1023 beq reg1, reg2, label
1024 delay slot
1025
1026 into
1027
1028 bne reg1, reg2, 0f
1029 nop
1030 j label
1031 0: delay slot
1032
1033 with the following opcode replacements:
1034
1035 beq <-> bne
1036 blez <-> bgtz
1037 bltz <-> bgez
1038 bc1f <-> bc1t
1039
1040 bltzal <-> bgezal (with jal label instead of j label)
1041
1042 Even though keeping the delay slot instruction in the delay slot of
1043 the branch would be more efficient, it would be very tricky to do
1044 correctly, because we'd have to introduce a variable frag *after*
1045 the delay slot instruction, and expand that instead. Let's do it
1046 the easy way for now, even if the branch-not-taken case now costs
1047 one additional instruction. Out-of-range branches are not supposed
1048 to be common, anyway.
1049
1050 Branch likely. If label is out of range, we turn:
1051
1052 beql reg1, reg2, label
1053 delay slot (annulled if branch not taken)
1054
1055 into
1056
1057 beql reg1, reg2, 1f
1058 nop
1059 beql $0, $0, 2f
1060 nop
1061 1: j[al] label
1062 delay slot (executed only if branch taken)
1063 2:
1064
1065 It would be possible to generate a shorter sequence by losing the
1066 likely bit, generating something like:
1067
1068 bne reg1, reg2, 0f
1069 nop
1070 j[al] label
1071 delay slot (executed only if branch taken)
1072 0:
1073
1074 beql -> bne
1075 bnel -> beq
1076 blezl -> bgtz
1077 bgtzl -> blez
1078 bltzl -> bgez
1079 bgezl -> bltz
1080 bc1fl -> bc1t
1081 bc1tl -> bc1f
1082
1083 bltzall -> bgezal (with jal label instead of j label)
1084 bgezall -> bltzal (ditto)
1085
1086
1087 but it's not clear that it would actually improve performance. */
1088 #define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1089 ((relax_substateT) \
1090 (0xc0000000 \
1091 | ((at) & 0x1f) \
1092 | ((toofar) ? 0x20 : 0) \
1093 | ((link) ? 0x40 : 0) \
1094 | ((likely) ? 0x80 : 0) \
1095 | ((uncond) ? 0x100 : 0)))
1096 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1097 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1098 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1099 #define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1100 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1101 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1102
1103 /* For mips16 code, we use an entirely different form of relaxation.
1104 mips16 supports two versions of most instructions which take
1105 immediate values: a small one which takes some small value, and a
1106 larger one which takes a 16 bit value. Since branches also follow
1107 this pattern, relaxing these values is required.
1108
1109 We can assemble both mips16 and normal MIPS code in a single
1110 object. Therefore, we need to support this type of relaxation at
1111 the same time that we support the relaxation described above. We
1112 use the high bit of the subtype field to distinguish these cases.
1113
1114 The information we store for this type of relaxation is the
1115 argument code found in the opcode file for this relocation, whether
1116 the user explicitly requested a small or extended form, and whether
1117 the relocation is in a jump or jal delay slot. That tells us the
1118 size of the value, and how it should be stored. We also store
1119 whether the fragment is considered to be extended or not. We also
1120 store whether this is known to be a branch to a different section,
1121 whether we have tried to relax this frag yet, and whether we have
1122 ever extended a PC relative fragment because of a shift count. */
1123 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1124 (0x80000000 \
1125 | ((type) & 0xff) \
1126 | ((small) ? 0x100 : 0) \
1127 | ((ext) ? 0x200 : 0) \
1128 | ((dslot) ? 0x400 : 0) \
1129 | ((jal_dslot) ? 0x800 : 0))
1130 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1131 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1132 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1133 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1134 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1135 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1136 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1137 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1138 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1139 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1140 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1141 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
1142
1143 /* For microMIPS code, we use relaxation similar to one we use for
1144 MIPS16 code. Some instructions that take immediate values support
1145 two encodings: a small one which takes some small value, and a
1146 larger one which takes a 16 bit value. As some branches also follow
1147 this pattern, relaxing these values is required.
1148
1149 We can assemble both microMIPS and normal MIPS code in a single
1150 object. Therefore, we need to support this type of relaxation at
1151 the same time that we support the relaxation described above. We
1152 use one of the high bits of the subtype field to distinguish these
1153 cases.
1154
1155 The information we store for this type of relaxation is the argument
1156 code found in the opcode file for this relocation, the register
1157 selected as the assembler temporary, whether the branch is
1158 unconditional, whether it is compact, whether it stores the link
1159 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1160 branches to a sequence of instructions is enabled, and whether the
1161 displacement of a branch is too large to fit as an immediate argument
1162 of a 16-bit and a 32-bit branch, respectively. */
1163 #define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1164 relax32, toofar16, toofar32) \
1165 (0x40000000 \
1166 | ((type) & 0xff) \
1167 | (((at) & 0x1f) << 8) \
1168 | ((uncond) ? 0x2000 : 0) \
1169 | ((compact) ? 0x4000 : 0) \
1170 | ((link) ? 0x8000 : 0) \
1171 | ((relax32) ? 0x10000 : 0) \
1172 | ((toofar16) ? 0x20000 : 0) \
1173 | ((toofar32) ? 0x40000 : 0))
1174 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1175 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1176 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1177 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1178 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1179 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1180 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1181
1182 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1183 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1184 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1185 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1186 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1187 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
1188
1189 /* Sign-extend 16-bit value X. */
1190 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1191
1192 /* Is the given value a sign-extended 32-bit value? */
1193 #define IS_SEXT_32BIT_NUM(x) \
1194 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1195 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1196
1197 /* Is the given value a sign-extended 16-bit value? */
1198 #define IS_SEXT_16BIT_NUM(x) \
1199 (((x) &~ (offsetT) 0x7fff) == 0 \
1200 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1201
1202 /* Is the given value a sign-extended 12-bit value? */
1203 #define IS_SEXT_12BIT_NUM(x) \
1204 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1205
1206 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
1207 #define IS_ZEXT_32BIT_NUM(x) \
1208 (((x) &~ (offsetT) 0xffffffff) == 0 \
1209 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1210
1211 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1212 VALUE << SHIFT. VALUE is evaluated exactly once. */
1213 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1214 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1215 | (((VALUE) & (MASK)) << (SHIFT)))
1216
1217 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1218 SHIFT places. */
1219 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1220 (((STRUCT) >> (SHIFT)) & (MASK))
1221
1222 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1223 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1224
1225 include/opcode/mips.h specifies operand fields using the macros
1226 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
1227 with "MIPS16OP" instead of "OP". */
1228 #define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1229 do \
1230 if (!(MICROMIPS)) \
1231 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1232 OP_MASK_##FIELD, OP_SH_##FIELD); \
1233 else \
1234 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1235 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1236 while (0)
1237 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1238 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1239 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1240
1241 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
1242 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1243 (!(MICROMIPS) \
1244 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1245 : EXTRACT_BITS ((INSN).insn_opcode, \
1246 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1247 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1248 EXTRACT_BITS ((INSN).insn_opcode, \
1249 MIPS16OP_MASK_##FIELD, \
1250 MIPS16OP_SH_##FIELD)
1251
1252 /* The MIPS16 EXTEND opcode, shifted left 16 places. */
1253 #define MIPS16_EXTEND (0xf000U << 16)
1254 \f
1255 /* Whether or not we are emitting a branch-likely macro. */
1256 static bfd_boolean emit_branch_likely_macro = FALSE;
1257
1258 /* Global variables used when generating relaxable macros. See the
1259 comment above RELAX_ENCODE for more details about how relaxation
1260 is used. */
1261 static struct {
1262 /* 0 if we're not emitting a relaxable macro.
1263 1 if we're emitting the first of the two relaxation alternatives.
1264 2 if we're emitting the second alternative. */
1265 int sequence;
1266
1267 /* The first relaxable fixup in the current frag. (In other words,
1268 the first fixup that refers to relaxable code.) */
1269 fixS *first_fixup;
1270
1271 /* sizes[0] says how many bytes of the first alternative are stored in
1272 the current frag. Likewise sizes[1] for the second alternative. */
1273 unsigned int sizes[2];
1274
1275 /* The symbol on which the choice of sequence depends. */
1276 symbolS *symbol;
1277 } mips_relax;
1278 \f
1279 /* Global variables used to decide whether a macro needs a warning. */
1280 static struct {
1281 /* True if the macro is in a branch delay slot. */
1282 bfd_boolean delay_slot_p;
1283
1284 /* Set to the length in bytes required if the macro is in a delay slot
1285 that requires a specific length of instruction, otherwise zero. */
1286 unsigned int delay_slot_length;
1287
1288 /* For relaxable macros, sizes[0] is the length of the first alternative
1289 in bytes and sizes[1] is the length of the second alternative.
1290 For non-relaxable macros, both elements give the length of the
1291 macro in bytes. */
1292 unsigned int sizes[2];
1293
1294 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1295 instruction of the first alternative in bytes and first_insn_sizes[1]
1296 is the length of the first instruction of the second alternative.
1297 For non-relaxable macros, both elements give the length of the first
1298 instruction in bytes.
1299
1300 Set to zero if we haven't yet seen the first instruction. */
1301 unsigned int first_insn_sizes[2];
1302
1303 /* For relaxable macros, insns[0] is the number of instructions for the
1304 first alternative and insns[1] is the number of instructions for the
1305 second alternative.
1306
1307 For non-relaxable macros, both elements give the number of
1308 instructions for the macro. */
1309 unsigned int insns[2];
1310
1311 /* The first variant frag for this macro. */
1312 fragS *first_frag;
1313 } mips_macro_warning;
1314 \f
1315 /* Prototypes for static functions. */
1316
1317 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1318
1319 static void append_insn
1320 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1321 bfd_boolean expansionp);
1322 static void mips_no_prev_insn (void);
1323 static void macro_build (expressionS *, const char *, const char *, ...);
1324 static void mips16_macro_build
1325 (expressionS *, const char *, const char *, va_list *);
1326 static void load_register (int, expressionS *, int);
1327 static void macro_start (void);
1328 static void macro_end (void);
1329 static void macro (struct mips_cl_insn * ip);
1330 static void mips16_macro (struct mips_cl_insn * ip);
1331 static void mips_ip (char *str, struct mips_cl_insn * ip);
1332 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1333 static void mips16_immed
1334 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1335 unsigned int, unsigned long *);
1336 static size_t my_getSmallExpression
1337 (expressionS *, bfd_reloc_code_real_type *, char *);
1338 static void my_getExpression (expressionS *, char *);
1339 static void s_align (int);
1340 static void s_change_sec (int);
1341 static void s_change_section (int);
1342 static void s_cons (int);
1343 static void s_float_cons (int);
1344 static void s_mips_globl (int);
1345 static void s_option (int);
1346 static void s_mipsset (int);
1347 static void s_abicalls (int);
1348 static void s_cpload (int);
1349 static void s_cpsetup (int);
1350 static void s_cplocal (int);
1351 static void s_cprestore (int);
1352 static void s_cpreturn (int);
1353 static void s_dtprelword (int);
1354 static void s_dtpreldword (int);
1355 static void s_tprelword (int);
1356 static void s_tpreldword (int);
1357 static void s_gpvalue (int);
1358 static void s_gpword (int);
1359 static void s_gpdword (int);
1360 static void s_cpadd (int);
1361 static void s_insn (int);
1362 static void md_obj_begin (void);
1363 static void md_obj_end (void);
1364 static void s_mips_ent (int);
1365 static void s_mips_end (int);
1366 static void s_mips_frame (int);
1367 static void s_mips_mask (int reg_type);
1368 static void s_mips_stab (int);
1369 static void s_mips_weakext (int);
1370 static void s_mips_file (int);
1371 static void s_mips_loc (int);
1372 static bfd_boolean pic_need_relax (symbolS *, asection *);
1373 static int relaxed_branch_length (fragS *, asection *, int);
1374 static int validate_mips_insn (const struct mips_opcode *);
1375 static int validate_micromips_insn (const struct mips_opcode *);
1376 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1377 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1378
1379 /* Table and functions used to map between CPU/ISA names, and
1380 ISA levels, and CPU numbers. */
1381
1382 struct mips_cpu_info
1383 {
1384 const char *name; /* CPU or ISA name. */
1385 int flags; /* ASEs available, or ISA flag. */
1386 int isa; /* ISA level. */
1387 int cpu; /* CPU number (default CPU if ISA). */
1388 };
1389
1390 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1391 #define MIPS_CPU_ASE_SMARTMIPS 0x0002 /* CPU implements SmartMIPS ASE */
1392 #define MIPS_CPU_ASE_DSP 0x0004 /* CPU implements DSP ASE */
1393 #define MIPS_CPU_ASE_MT 0x0008 /* CPU implements MT ASE */
1394 #define MIPS_CPU_ASE_MIPS3D 0x0010 /* CPU implements MIPS-3D ASE */
1395 #define MIPS_CPU_ASE_MDMX 0x0020 /* CPU implements MDMX ASE */
1396 #define MIPS_CPU_ASE_DSPR2 0x0040 /* CPU implements DSP R2 ASE */
1397 #define MIPS_CPU_ASE_MCU 0x0080 /* CPU implements MCU ASE */
1398
1399 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1400 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1401 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1402 \f
1403 /* Pseudo-op table.
1404
1405 The following pseudo-ops from the Kane and Heinrich MIPS book
1406 should be defined here, but are currently unsupported: .alias,
1407 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1408
1409 The following pseudo-ops from the Kane and Heinrich MIPS book are
1410 specific to the type of debugging information being generated, and
1411 should be defined by the object format: .aent, .begin, .bend,
1412 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1413 .vreg.
1414
1415 The following pseudo-ops from the Kane and Heinrich MIPS book are
1416 not MIPS CPU specific, but are also not specific to the object file
1417 format. This file is probably the best place to define them, but
1418 they are not currently supported: .asm0, .endr, .lab, .struct. */
1419
1420 static const pseudo_typeS mips_pseudo_table[] =
1421 {
1422 /* MIPS specific pseudo-ops. */
1423 {"option", s_option, 0},
1424 {"set", s_mipsset, 0},
1425 {"rdata", s_change_sec, 'r'},
1426 {"sdata", s_change_sec, 's'},
1427 {"livereg", s_ignore, 0},
1428 {"abicalls", s_abicalls, 0},
1429 {"cpload", s_cpload, 0},
1430 {"cpsetup", s_cpsetup, 0},
1431 {"cplocal", s_cplocal, 0},
1432 {"cprestore", s_cprestore, 0},
1433 {"cpreturn", s_cpreturn, 0},
1434 {"dtprelword", s_dtprelword, 0},
1435 {"dtpreldword", s_dtpreldword, 0},
1436 {"tprelword", s_tprelword, 0},
1437 {"tpreldword", s_tpreldword, 0},
1438 {"gpvalue", s_gpvalue, 0},
1439 {"gpword", s_gpword, 0},
1440 {"gpdword", s_gpdword, 0},
1441 {"cpadd", s_cpadd, 0},
1442 {"insn", s_insn, 0},
1443
1444 /* Relatively generic pseudo-ops that happen to be used on MIPS
1445 chips. */
1446 {"asciiz", stringer, 8 + 1},
1447 {"bss", s_change_sec, 'b'},
1448 {"err", s_err, 0},
1449 {"half", s_cons, 1},
1450 {"dword", s_cons, 3},
1451 {"weakext", s_mips_weakext, 0},
1452 {"origin", s_org, 0},
1453 {"repeat", s_rept, 0},
1454
1455 /* For MIPS this is non-standard, but we define it for consistency. */
1456 {"sbss", s_change_sec, 'B'},
1457
1458 /* These pseudo-ops are defined in read.c, but must be overridden
1459 here for one reason or another. */
1460 {"align", s_align, 0},
1461 {"byte", s_cons, 0},
1462 {"data", s_change_sec, 'd'},
1463 {"double", s_float_cons, 'd'},
1464 {"float", s_float_cons, 'f'},
1465 {"globl", s_mips_globl, 0},
1466 {"global", s_mips_globl, 0},
1467 {"hword", s_cons, 1},
1468 {"int", s_cons, 2},
1469 {"long", s_cons, 2},
1470 {"octa", s_cons, 4},
1471 {"quad", s_cons, 3},
1472 {"section", s_change_section, 0},
1473 {"short", s_cons, 1},
1474 {"single", s_float_cons, 'f'},
1475 {"stabd", s_mips_stab, 'd'},
1476 {"stabn", s_mips_stab, 'n'},
1477 {"stabs", s_mips_stab, 's'},
1478 {"text", s_change_sec, 't'},
1479 {"word", s_cons, 2},
1480
1481 { "extern", ecoff_directive_extern, 0},
1482
1483 { NULL, NULL, 0 },
1484 };
1485
1486 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1487 {
1488 /* These pseudo-ops should be defined by the object file format.
1489 However, a.out doesn't support them, so we have versions here. */
1490 {"aent", s_mips_ent, 1},
1491 {"bgnb", s_ignore, 0},
1492 {"end", s_mips_end, 0},
1493 {"endb", s_ignore, 0},
1494 {"ent", s_mips_ent, 0},
1495 {"file", s_mips_file, 0},
1496 {"fmask", s_mips_mask, 'F'},
1497 {"frame", s_mips_frame, 0},
1498 {"loc", s_mips_loc, 0},
1499 {"mask", s_mips_mask, 'R'},
1500 {"verstamp", s_ignore, 0},
1501 { NULL, NULL, 0 },
1502 };
1503
1504 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1505 purpose of the `.dc.a' internal pseudo-op. */
1506
1507 int
1508 mips_address_bytes (void)
1509 {
1510 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1511 }
1512
1513 extern void pop_insert (const pseudo_typeS *);
1514
1515 void
1516 mips_pop_insert (void)
1517 {
1518 pop_insert (mips_pseudo_table);
1519 if (! ECOFF_DEBUGGING)
1520 pop_insert (mips_nonecoff_pseudo_table);
1521 }
1522 \f
1523 /* Symbols labelling the current insn. */
1524
1525 struct insn_label_list
1526 {
1527 struct insn_label_list *next;
1528 symbolS *label;
1529 };
1530
1531 static struct insn_label_list *free_insn_labels;
1532 #define label_list tc_segment_info_data.labels
1533
1534 static void mips_clear_insn_labels (void);
1535 static void mips_mark_labels (void);
1536 static void mips_compressed_mark_labels (void);
1537
1538 static inline void
1539 mips_clear_insn_labels (void)
1540 {
1541 register struct insn_label_list **pl;
1542 segment_info_type *si;
1543
1544 if (now_seg)
1545 {
1546 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1547 ;
1548
1549 si = seg_info (now_seg);
1550 *pl = si->label_list;
1551 si->label_list = NULL;
1552 }
1553 }
1554
1555 /* Mark instruction labels in MIPS16/microMIPS mode. */
1556
1557 static inline void
1558 mips_mark_labels (void)
1559 {
1560 if (HAVE_CODE_COMPRESSION)
1561 mips_compressed_mark_labels ();
1562 }
1563 \f
1564 static char *expr_end;
1565
1566 /* Expressions which appear in instructions. These are set by
1567 mips_ip. */
1568
1569 static expressionS imm_expr;
1570 static expressionS imm2_expr;
1571 static expressionS offset_expr;
1572
1573 /* Relocs associated with imm_expr and offset_expr. */
1574
1575 static bfd_reloc_code_real_type imm_reloc[3]
1576 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1577 static bfd_reloc_code_real_type offset_reloc[3]
1578 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1579
1580 /* This is set to the resulting size of the instruction to be produced
1581 by mips16_ip if an explicit extension is used or by mips_ip if an
1582 explicit size is supplied. */
1583
1584 static unsigned int forced_insn_length;
1585
1586 /* True if we are assembling an instruction. All dot symbols defined during
1587 this time should be treated as code labels. */
1588
1589 static bfd_boolean mips_assembling_insn;
1590
1591 #ifdef OBJ_ELF
1592 /* The pdr segment for per procedure frame/regmask info. Not used for
1593 ECOFF debugging. */
1594
1595 static segT pdr_seg;
1596 #endif
1597
1598 /* The default target format to use. */
1599
1600 #if defined (TE_FreeBSD)
1601 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1602 #elif defined (TE_TMIPS)
1603 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1604 #else
1605 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1606 #endif
1607
1608 const char *
1609 mips_target_format (void)
1610 {
1611 switch (OUTPUT_FLAVOR)
1612 {
1613 case bfd_target_ecoff_flavour:
1614 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1615 case bfd_target_coff_flavour:
1616 return "pe-mips";
1617 case bfd_target_elf_flavour:
1618 #ifdef TE_VXWORKS
1619 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1620 return (target_big_endian
1621 ? "elf32-bigmips-vxworks"
1622 : "elf32-littlemips-vxworks");
1623 #endif
1624 return (target_big_endian
1625 ? (HAVE_64BIT_OBJECTS
1626 ? ELF_TARGET ("elf64-", "big")
1627 : (HAVE_NEWABI
1628 ? ELF_TARGET ("elf32-n", "big")
1629 : ELF_TARGET ("elf32-", "big")))
1630 : (HAVE_64BIT_OBJECTS
1631 ? ELF_TARGET ("elf64-", "little")
1632 : (HAVE_NEWABI
1633 ? ELF_TARGET ("elf32-n", "little")
1634 : ELF_TARGET ("elf32-", "little"))));
1635 default:
1636 abort ();
1637 return NULL;
1638 }
1639 }
1640
1641 /* Return the length of a microMIPS instruction in bytes. If bits of
1642 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1643 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1644 major opcode) will require further modifications to the opcode
1645 table. */
1646
1647 static inline unsigned int
1648 micromips_insn_length (const struct mips_opcode *mo)
1649 {
1650 return (mo->mask >> 16) == 0 ? 2 : 4;
1651 }
1652
1653 /* Return the length of MIPS16 instruction OPCODE. */
1654
1655 static inline unsigned int
1656 mips16_opcode_length (unsigned long opcode)
1657 {
1658 return (opcode >> 16) == 0 ? 2 : 4;
1659 }
1660
1661 /* Return the length of instruction INSN. */
1662
1663 static inline unsigned int
1664 insn_length (const struct mips_cl_insn *insn)
1665 {
1666 if (mips_opts.micromips)
1667 return micromips_insn_length (insn->insn_mo);
1668 else if (mips_opts.mips16)
1669 return mips16_opcode_length (insn->insn_opcode);
1670 else
1671 return 4;
1672 }
1673
1674 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1675
1676 static void
1677 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1678 {
1679 size_t i;
1680
1681 insn->insn_mo = mo;
1682 insn->insn_opcode = mo->match;
1683 insn->frag = NULL;
1684 insn->where = 0;
1685 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1686 insn->fixp[i] = NULL;
1687 insn->fixed_p = (mips_opts.noreorder > 0);
1688 insn->noreorder_p = (mips_opts.noreorder > 0);
1689 insn->mips16_absolute_jump_p = 0;
1690 insn->complete_p = 0;
1691 insn->cleared_p = 0;
1692 }
1693
1694 /* Record the current MIPS16/microMIPS mode in now_seg. */
1695
1696 static void
1697 mips_record_compressed_mode (void)
1698 {
1699 segment_info_type *si;
1700
1701 si = seg_info (now_seg);
1702 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1703 si->tc_segment_info_data.mips16 = mips_opts.mips16;
1704 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
1705 si->tc_segment_info_data.micromips = mips_opts.micromips;
1706 }
1707
1708 /* Read a standard MIPS instruction from BUF. */
1709
1710 static unsigned long
1711 read_insn (char *buf)
1712 {
1713 if (target_big_endian)
1714 return bfd_getb32 ((bfd_byte *) buf);
1715 else
1716 return bfd_getl32 ((bfd_byte *) buf);
1717 }
1718
1719 /* Write standard MIPS instruction INSN to BUF. Return a pointer to
1720 the next byte. */
1721
1722 static char *
1723 write_insn (char *buf, unsigned int insn)
1724 {
1725 md_number_to_chars (buf, insn, 4);
1726 return buf + 4;
1727 }
1728
1729 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
1730 has length LENGTH. */
1731
1732 static unsigned long
1733 read_compressed_insn (char *buf, unsigned int length)
1734 {
1735 unsigned long insn;
1736 unsigned int i;
1737
1738 insn = 0;
1739 for (i = 0; i < length; i += 2)
1740 {
1741 insn <<= 16;
1742 if (target_big_endian)
1743 insn |= bfd_getb16 ((char *) buf);
1744 else
1745 insn |= bfd_getl16 ((char *) buf);
1746 buf += 2;
1747 }
1748 return insn;
1749 }
1750
1751 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
1752 instruction is LENGTH bytes long. Return a pointer to the next byte. */
1753
1754 static char *
1755 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
1756 {
1757 unsigned int i;
1758
1759 for (i = 0; i < length; i += 2)
1760 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
1761 return buf + length;
1762 }
1763
1764 /* Install INSN at the location specified by its "frag" and "where" fields. */
1765
1766 static void
1767 install_insn (const struct mips_cl_insn *insn)
1768 {
1769 char *f = insn->frag->fr_literal + insn->where;
1770 if (HAVE_CODE_COMPRESSION)
1771 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1772 else
1773 write_insn (f, insn->insn_opcode);
1774 mips_record_compressed_mode ();
1775 }
1776
1777 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1778 and install the opcode in the new location. */
1779
1780 static void
1781 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1782 {
1783 size_t i;
1784
1785 insn->frag = frag;
1786 insn->where = where;
1787 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1788 if (insn->fixp[i] != NULL)
1789 {
1790 insn->fixp[i]->fx_frag = frag;
1791 insn->fixp[i]->fx_where = where;
1792 }
1793 install_insn (insn);
1794 }
1795
1796 /* Add INSN to the end of the output. */
1797
1798 static void
1799 add_fixed_insn (struct mips_cl_insn *insn)
1800 {
1801 char *f = frag_more (insn_length (insn));
1802 move_insn (insn, frag_now, f - frag_now->fr_literal);
1803 }
1804
1805 /* Start a variant frag and move INSN to the start of the variant part,
1806 marking it as fixed. The other arguments are as for frag_var. */
1807
1808 static void
1809 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1810 relax_substateT subtype, symbolS *symbol, offsetT offset)
1811 {
1812 frag_grow (max_chars);
1813 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1814 insn->fixed_p = 1;
1815 frag_var (rs_machine_dependent, max_chars, var,
1816 subtype, symbol, offset, NULL);
1817 }
1818
1819 /* Insert N copies of INSN into the history buffer, starting at
1820 position FIRST. Neither FIRST nor N need to be clipped. */
1821
1822 static void
1823 insert_into_history (unsigned int first, unsigned int n,
1824 const struct mips_cl_insn *insn)
1825 {
1826 if (mips_relax.sequence != 2)
1827 {
1828 unsigned int i;
1829
1830 for (i = ARRAY_SIZE (history); i-- > first;)
1831 if (i >= first + n)
1832 history[i] = history[i - n];
1833 else
1834 history[i] = *insn;
1835 }
1836 }
1837
1838 /* Initialize vr4120_conflicts. There is a bit of duplication here:
1839 the idea is to make it obvious at a glance that each errata is
1840 included. */
1841
1842 static void
1843 init_vr4120_conflicts (void)
1844 {
1845 #define CONFLICT(FIRST, SECOND) \
1846 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1847
1848 /* Errata 21 - [D]DIV[U] after [D]MACC */
1849 CONFLICT (MACC, DIV);
1850 CONFLICT (DMACC, DIV);
1851
1852 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1853 CONFLICT (DMULT, DMULT);
1854 CONFLICT (DMULT, DMACC);
1855 CONFLICT (DMACC, DMULT);
1856 CONFLICT (DMACC, DMACC);
1857
1858 /* Errata 24 - MT{LO,HI} after [D]MACC */
1859 CONFLICT (MACC, MTHILO);
1860 CONFLICT (DMACC, MTHILO);
1861
1862 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1863 instruction is executed immediately after a MACC or DMACC
1864 instruction, the result of [either instruction] is incorrect." */
1865 CONFLICT (MACC, MULT);
1866 CONFLICT (MACC, DMULT);
1867 CONFLICT (DMACC, MULT);
1868 CONFLICT (DMACC, DMULT);
1869
1870 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1871 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1872 DDIV or DDIVU instruction, the result of the MACC or
1873 DMACC instruction is incorrect.". */
1874 CONFLICT (DMULT, MACC);
1875 CONFLICT (DMULT, DMACC);
1876 CONFLICT (DIV, MACC);
1877 CONFLICT (DIV, DMACC);
1878
1879 #undef CONFLICT
1880 }
1881
1882 struct regname {
1883 const char *name;
1884 unsigned int num;
1885 };
1886
1887 #define RTYPE_MASK 0x1ff00
1888 #define RTYPE_NUM 0x00100
1889 #define RTYPE_FPU 0x00200
1890 #define RTYPE_FCC 0x00400
1891 #define RTYPE_VEC 0x00800
1892 #define RTYPE_GP 0x01000
1893 #define RTYPE_CP0 0x02000
1894 #define RTYPE_PC 0x04000
1895 #define RTYPE_ACC 0x08000
1896 #define RTYPE_CCC 0x10000
1897 #define RNUM_MASK 0x000ff
1898 #define RWARN 0x80000
1899
1900 #define GENERIC_REGISTER_NUMBERS \
1901 {"$0", RTYPE_NUM | 0}, \
1902 {"$1", RTYPE_NUM | 1}, \
1903 {"$2", RTYPE_NUM | 2}, \
1904 {"$3", RTYPE_NUM | 3}, \
1905 {"$4", RTYPE_NUM | 4}, \
1906 {"$5", RTYPE_NUM | 5}, \
1907 {"$6", RTYPE_NUM | 6}, \
1908 {"$7", RTYPE_NUM | 7}, \
1909 {"$8", RTYPE_NUM | 8}, \
1910 {"$9", RTYPE_NUM | 9}, \
1911 {"$10", RTYPE_NUM | 10}, \
1912 {"$11", RTYPE_NUM | 11}, \
1913 {"$12", RTYPE_NUM | 12}, \
1914 {"$13", RTYPE_NUM | 13}, \
1915 {"$14", RTYPE_NUM | 14}, \
1916 {"$15", RTYPE_NUM | 15}, \
1917 {"$16", RTYPE_NUM | 16}, \
1918 {"$17", RTYPE_NUM | 17}, \
1919 {"$18", RTYPE_NUM | 18}, \
1920 {"$19", RTYPE_NUM | 19}, \
1921 {"$20", RTYPE_NUM | 20}, \
1922 {"$21", RTYPE_NUM | 21}, \
1923 {"$22", RTYPE_NUM | 22}, \
1924 {"$23", RTYPE_NUM | 23}, \
1925 {"$24", RTYPE_NUM | 24}, \
1926 {"$25", RTYPE_NUM | 25}, \
1927 {"$26", RTYPE_NUM | 26}, \
1928 {"$27", RTYPE_NUM | 27}, \
1929 {"$28", RTYPE_NUM | 28}, \
1930 {"$29", RTYPE_NUM | 29}, \
1931 {"$30", RTYPE_NUM | 30}, \
1932 {"$31", RTYPE_NUM | 31}
1933
1934 #define FPU_REGISTER_NAMES \
1935 {"$f0", RTYPE_FPU | 0}, \
1936 {"$f1", RTYPE_FPU | 1}, \
1937 {"$f2", RTYPE_FPU | 2}, \
1938 {"$f3", RTYPE_FPU | 3}, \
1939 {"$f4", RTYPE_FPU | 4}, \
1940 {"$f5", RTYPE_FPU | 5}, \
1941 {"$f6", RTYPE_FPU | 6}, \
1942 {"$f7", RTYPE_FPU | 7}, \
1943 {"$f8", RTYPE_FPU | 8}, \
1944 {"$f9", RTYPE_FPU | 9}, \
1945 {"$f10", RTYPE_FPU | 10}, \
1946 {"$f11", RTYPE_FPU | 11}, \
1947 {"$f12", RTYPE_FPU | 12}, \
1948 {"$f13", RTYPE_FPU | 13}, \
1949 {"$f14", RTYPE_FPU | 14}, \
1950 {"$f15", RTYPE_FPU | 15}, \
1951 {"$f16", RTYPE_FPU | 16}, \
1952 {"$f17", RTYPE_FPU | 17}, \
1953 {"$f18", RTYPE_FPU | 18}, \
1954 {"$f19", RTYPE_FPU | 19}, \
1955 {"$f20", RTYPE_FPU | 20}, \
1956 {"$f21", RTYPE_FPU | 21}, \
1957 {"$f22", RTYPE_FPU | 22}, \
1958 {"$f23", RTYPE_FPU | 23}, \
1959 {"$f24", RTYPE_FPU | 24}, \
1960 {"$f25", RTYPE_FPU | 25}, \
1961 {"$f26", RTYPE_FPU | 26}, \
1962 {"$f27", RTYPE_FPU | 27}, \
1963 {"$f28", RTYPE_FPU | 28}, \
1964 {"$f29", RTYPE_FPU | 29}, \
1965 {"$f30", RTYPE_FPU | 30}, \
1966 {"$f31", RTYPE_FPU | 31}
1967
1968 #define FPU_CONDITION_CODE_NAMES \
1969 {"$fcc0", RTYPE_FCC | 0}, \
1970 {"$fcc1", RTYPE_FCC | 1}, \
1971 {"$fcc2", RTYPE_FCC | 2}, \
1972 {"$fcc3", RTYPE_FCC | 3}, \
1973 {"$fcc4", RTYPE_FCC | 4}, \
1974 {"$fcc5", RTYPE_FCC | 5}, \
1975 {"$fcc6", RTYPE_FCC | 6}, \
1976 {"$fcc7", RTYPE_FCC | 7}
1977
1978 #define COPROC_CONDITION_CODE_NAMES \
1979 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
1980 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
1981 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
1982 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
1983 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
1984 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
1985 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
1986 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
1987
1988 #define N32N64_SYMBOLIC_REGISTER_NAMES \
1989 {"$a4", RTYPE_GP | 8}, \
1990 {"$a5", RTYPE_GP | 9}, \
1991 {"$a6", RTYPE_GP | 10}, \
1992 {"$a7", RTYPE_GP | 11}, \
1993 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
1994 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
1995 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
1996 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
1997 {"$t0", RTYPE_GP | 12}, \
1998 {"$t1", RTYPE_GP | 13}, \
1999 {"$t2", RTYPE_GP | 14}, \
2000 {"$t3", RTYPE_GP | 15}
2001
2002 #define O32_SYMBOLIC_REGISTER_NAMES \
2003 {"$t0", RTYPE_GP | 8}, \
2004 {"$t1", RTYPE_GP | 9}, \
2005 {"$t2", RTYPE_GP | 10}, \
2006 {"$t3", RTYPE_GP | 11}, \
2007 {"$t4", RTYPE_GP | 12}, \
2008 {"$t5", RTYPE_GP | 13}, \
2009 {"$t6", RTYPE_GP | 14}, \
2010 {"$t7", RTYPE_GP | 15}, \
2011 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2012 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2013 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2014 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2015
2016 /* Remaining symbolic register names */
2017 #define SYMBOLIC_REGISTER_NAMES \
2018 {"$zero", RTYPE_GP | 0}, \
2019 {"$at", RTYPE_GP | 1}, \
2020 {"$AT", RTYPE_GP | 1}, \
2021 {"$v0", RTYPE_GP | 2}, \
2022 {"$v1", RTYPE_GP | 3}, \
2023 {"$a0", RTYPE_GP | 4}, \
2024 {"$a1", RTYPE_GP | 5}, \
2025 {"$a2", RTYPE_GP | 6}, \
2026 {"$a3", RTYPE_GP | 7}, \
2027 {"$s0", RTYPE_GP | 16}, \
2028 {"$s1", RTYPE_GP | 17}, \
2029 {"$s2", RTYPE_GP | 18}, \
2030 {"$s3", RTYPE_GP | 19}, \
2031 {"$s4", RTYPE_GP | 20}, \
2032 {"$s5", RTYPE_GP | 21}, \
2033 {"$s6", RTYPE_GP | 22}, \
2034 {"$s7", RTYPE_GP | 23}, \
2035 {"$t8", RTYPE_GP | 24}, \
2036 {"$t9", RTYPE_GP | 25}, \
2037 {"$k0", RTYPE_GP | 26}, \
2038 {"$kt0", RTYPE_GP | 26}, \
2039 {"$k1", RTYPE_GP | 27}, \
2040 {"$kt1", RTYPE_GP | 27}, \
2041 {"$gp", RTYPE_GP | 28}, \
2042 {"$sp", RTYPE_GP | 29}, \
2043 {"$s8", RTYPE_GP | 30}, \
2044 {"$fp", RTYPE_GP | 30}, \
2045 {"$ra", RTYPE_GP | 31}
2046
2047 #define MIPS16_SPECIAL_REGISTER_NAMES \
2048 {"$pc", RTYPE_PC | 0}
2049
2050 #define MDMX_VECTOR_REGISTER_NAMES \
2051 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2052 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2053 {"$v2", RTYPE_VEC | 2}, \
2054 {"$v3", RTYPE_VEC | 3}, \
2055 {"$v4", RTYPE_VEC | 4}, \
2056 {"$v5", RTYPE_VEC | 5}, \
2057 {"$v6", RTYPE_VEC | 6}, \
2058 {"$v7", RTYPE_VEC | 7}, \
2059 {"$v8", RTYPE_VEC | 8}, \
2060 {"$v9", RTYPE_VEC | 9}, \
2061 {"$v10", RTYPE_VEC | 10}, \
2062 {"$v11", RTYPE_VEC | 11}, \
2063 {"$v12", RTYPE_VEC | 12}, \
2064 {"$v13", RTYPE_VEC | 13}, \
2065 {"$v14", RTYPE_VEC | 14}, \
2066 {"$v15", RTYPE_VEC | 15}, \
2067 {"$v16", RTYPE_VEC | 16}, \
2068 {"$v17", RTYPE_VEC | 17}, \
2069 {"$v18", RTYPE_VEC | 18}, \
2070 {"$v19", RTYPE_VEC | 19}, \
2071 {"$v20", RTYPE_VEC | 20}, \
2072 {"$v21", RTYPE_VEC | 21}, \
2073 {"$v22", RTYPE_VEC | 22}, \
2074 {"$v23", RTYPE_VEC | 23}, \
2075 {"$v24", RTYPE_VEC | 24}, \
2076 {"$v25", RTYPE_VEC | 25}, \
2077 {"$v26", RTYPE_VEC | 26}, \
2078 {"$v27", RTYPE_VEC | 27}, \
2079 {"$v28", RTYPE_VEC | 28}, \
2080 {"$v29", RTYPE_VEC | 29}, \
2081 {"$v30", RTYPE_VEC | 30}, \
2082 {"$v31", RTYPE_VEC | 31}
2083
2084 #define MIPS_DSP_ACCUMULATOR_NAMES \
2085 {"$ac0", RTYPE_ACC | 0}, \
2086 {"$ac1", RTYPE_ACC | 1}, \
2087 {"$ac2", RTYPE_ACC | 2}, \
2088 {"$ac3", RTYPE_ACC | 3}
2089
2090 static const struct regname reg_names[] = {
2091 GENERIC_REGISTER_NUMBERS,
2092 FPU_REGISTER_NAMES,
2093 FPU_CONDITION_CODE_NAMES,
2094 COPROC_CONDITION_CODE_NAMES,
2095
2096 /* The $txx registers depends on the abi,
2097 these will be added later into the symbol table from
2098 one of the tables below once mips_abi is set after
2099 parsing of arguments from the command line. */
2100 SYMBOLIC_REGISTER_NAMES,
2101
2102 MIPS16_SPECIAL_REGISTER_NAMES,
2103 MDMX_VECTOR_REGISTER_NAMES,
2104 MIPS_DSP_ACCUMULATOR_NAMES,
2105 {0, 0}
2106 };
2107
2108 static const struct regname reg_names_o32[] = {
2109 O32_SYMBOLIC_REGISTER_NAMES,
2110 {0, 0}
2111 };
2112
2113 static const struct regname reg_names_n32n64[] = {
2114 N32N64_SYMBOLIC_REGISTER_NAMES,
2115 {0, 0}
2116 };
2117
2118 /* Check if S points at a valid register specifier according to TYPES.
2119 If so, then return 1, advance S to consume the specifier and store
2120 the register's number in REGNOP, otherwise return 0. */
2121
2122 static int
2123 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2124 {
2125 symbolS *symbolP;
2126 char *e;
2127 char save_c;
2128 int reg = -1;
2129
2130 /* Find end of name. */
2131 e = *s;
2132 if (is_name_beginner (*e))
2133 ++e;
2134 while (is_part_of_name (*e))
2135 ++e;
2136
2137 /* Terminate name. */
2138 save_c = *e;
2139 *e = '\0';
2140
2141 /* Look for a register symbol. */
2142 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2143 {
2144 int r = S_GET_VALUE (symbolP);
2145 if (r & types)
2146 reg = r & RNUM_MASK;
2147 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2148 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
2149 reg = (r & RNUM_MASK) - 2;
2150 }
2151 /* Else see if this is a register defined in an itbl entry. */
2152 else if ((types & RTYPE_GP) && itbl_have_entries)
2153 {
2154 char *n = *s;
2155 unsigned long r;
2156
2157 if (*n == '$')
2158 ++n;
2159 if (itbl_get_reg_val (n, &r))
2160 reg = r & RNUM_MASK;
2161 }
2162
2163 /* Advance to next token if a register was recognised. */
2164 if (reg >= 0)
2165 *s = e;
2166 else if (types & RWARN)
2167 as_warn (_("Unrecognized register name `%s'"), *s);
2168
2169 *e = save_c;
2170 if (regnop)
2171 *regnop = reg;
2172 return reg >= 0;
2173 }
2174
2175 /* Check if S points at a valid register list according to TYPES.
2176 If so, then return 1, advance S to consume the list and store
2177 the registers present on the list as a bitmask of ones in REGLISTP,
2178 otherwise return 0. A valid list comprises a comma-separated
2179 enumeration of valid single registers and/or dash-separated
2180 contiguous register ranges as determined by their numbers.
2181
2182 As a special exception if one of s0-s7 registers is specified as
2183 the range's lower delimiter and s8 (fp) is its upper one, then no
2184 registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2185 are selected; they have to be listed separately if needed. */
2186
2187 static int
2188 reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2189 {
2190 unsigned int reglist = 0;
2191 unsigned int lastregno;
2192 bfd_boolean ok = TRUE;
2193 unsigned int regmask;
2194 char *s_endlist = *s;
2195 char *s_reset = *s;
2196 unsigned int regno;
2197
2198 while (reg_lookup (s, types, &regno))
2199 {
2200 lastregno = regno;
2201 if (**s == '-')
2202 {
2203 (*s)++;
2204 ok = reg_lookup (s, types, &lastregno);
2205 if (ok && lastregno < regno)
2206 ok = FALSE;
2207 if (!ok)
2208 break;
2209 }
2210
2211 if (lastregno == FP && regno >= S0 && regno <= S7)
2212 {
2213 lastregno = S7;
2214 reglist |= 1 << FP;
2215 }
2216 regmask = 1 << lastregno;
2217 regmask = (regmask << 1) - 1;
2218 regmask ^= (1 << regno) - 1;
2219 reglist |= regmask;
2220
2221 s_endlist = *s;
2222 if (**s != ',')
2223 break;
2224 (*s)++;
2225 }
2226
2227 if (ok)
2228 *s = s_endlist;
2229 else
2230 *s = s_reset;
2231 if (reglistp)
2232 *reglistp = reglist;
2233 return ok && reglist != 0;
2234 }
2235
2236 /* Return TRUE if opcode MO is valid on the currently selected ISA and
2237 architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
2238
2239 static bfd_boolean
2240 is_opcode_valid (const struct mips_opcode *mo)
2241 {
2242 int isa = mips_opts.isa;
2243 int fp_s, fp_d;
2244
2245 if (mips_opts.ase_mdmx)
2246 isa |= INSN_MDMX;
2247 if (mips_opts.ase_dsp)
2248 isa |= INSN_DSP;
2249 if (mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
2250 isa |= INSN_DSP64;
2251 if (mips_opts.ase_dspr2)
2252 isa |= INSN_DSPR2;
2253 if (mips_opts.ase_mt)
2254 isa |= INSN_MT;
2255 if (mips_opts.ase_mips3d)
2256 isa |= INSN_MIPS3D;
2257 if (mips_opts.ase_smartmips)
2258 isa |= INSN_SMARTMIPS;
2259 if (mips_opts.ase_mcu)
2260 isa |= INSN_MCU;
2261
2262 if (!opcode_is_member (mo, isa, mips_opts.arch))
2263 return FALSE;
2264
2265 /* Check whether the instruction or macro requires single-precision or
2266 double-precision floating-point support. Note that this information is
2267 stored differently in the opcode table for insns and macros. */
2268 if (mo->pinfo == INSN_MACRO)
2269 {
2270 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2271 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2272 }
2273 else
2274 {
2275 fp_s = mo->pinfo & FP_S;
2276 fp_d = mo->pinfo & FP_D;
2277 }
2278
2279 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2280 return FALSE;
2281
2282 if (fp_s && mips_opts.soft_float)
2283 return FALSE;
2284
2285 return TRUE;
2286 }
2287
2288 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
2289 selected ISA and architecture. */
2290
2291 static bfd_boolean
2292 is_opcode_valid_16 (const struct mips_opcode *mo)
2293 {
2294 return opcode_is_member (mo, mips_opts.isa, mips_opts.arch);
2295 }
2296
2297 /* Return TRUE if the size of the microMIPS opcode MO matches one
2298 explicitly requested. Always TRUE in the standard MIPS mode. */
2299
2300 static bfd_boolean
2301 is_size_valid (const struct mips_opcode *mo)
2302 {
2303 if (!mips_opts.micromips)
2304 return TRUE;
2305
2306 if (!forced_insn_length)
2307 return TRUE;
2308 if (mo->pinfo == INSN_MACRO)
2309 return FALSE;
2310 return forced_insn_length == micromips_insn_length (mo);
2311 }
2312
2313 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
2314 of the preceding instruction. Always TRUE in the standard MIPS mode.
2315
2316 We don't accept macros in 16-bit delay slots to avoid a case where
2317 a macro expansion fails because it relies on a preceding 32-bit real
2318 instruction to have matched and does not handle the operands correctly.
2319 The only macros that may expand to 16-bit instructions are JAL that
2320 cannot be placed in a delay slot anyway, and corner cases of BALIGN
2321 and BGT (that likewise cannot be placed in a delay slot) that decay to
2322 a NOP. In all these cases the macros precede any corresponding real
2323 instruction definitions in the opcode table, so they will match in the
2324 second pass where the size of the delay slot is ignored and therefore
2325 produce correct code. */
2326
2327 static bfd_boolean
2328 is_delay_slot_valid (const struct mips_opcode *mo)
2329 {
2330 if (!mips_opts.micromips)
2331 return TRUE;
2332
2333 if (mo->pinfo == INSN_MACRO)
2334 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
2335 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2336 && micromips_insn_length (mo) != 4)
2337 return FALSE;
2338 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2339 && micromips_insn_length (mo) != 2)
2340 return FALSE;
2341
2342 return TRUE;
2343 }
2344
2345 /* This function is called once, at assembler startup time. It should set up
2346 all the tables, etc. that the MD part of the assembler will need. */
2347
2348 void
2349 md_begin (void)
2350 {
2351 const char *retval = NULL;
2352 int i = 0;
2353 int broken = 0;
2354
2355 if (mips_pic != NO_PIC)
2356 {
2357 if (g_switch_seen && g_switch_value != 0)
2358 as_bad (_("-G may not be used in position-independent code"));
2359 g_switch_value = 0;
2360 }
2361
2362 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
2363 as_warn (_("Could not set architecture and machine"));
2364
2365 op_hash = hash_new ();
2366
2367 for (i = 0; i < NUMOPCODES;)
2368 {
2369 const char *name = mips_opcodes[i].name;
2370
2371 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
2372 if (retval != NULL)
2373 {
2374 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2375 mips_opcodes[i].name, retval);
2376 /* Probably a memory allocation problem? Give up now. */
2377 as_fatal (_("Broken assembler. No assembly attempted."));
2378 }
2379 do
2380 {
2381 if (mips_opcodes[i].pinfo != INSN_MACRO)
2382 {
2383 if (!validate_mips_insn (&mips_opcodes[i]))
2384 broken = 1;
2385 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2386 {
2387 create_insn (&nop_insn, mips_opcodes + i);
2388 if (mips_fix_loongson2f_nop)
2389 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
2390 nop_insn.fixed_p = 1;
2391 }
2392 }
2393 ++i;
2394 }
2395 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2396 }
2397
2398 mips16_op_hash = hash_new ();
2399
2400 i = 0;
2401 while (i < bfd_mips16_num_opcodes)
2402 {
2403 const char *name = mips16_opcodes[i].name;
2404
2405 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
2406 if (retval != NULL)
2407 as_fatal (_("internal: can't hash `%s': %s"),
2408 mips16_opcodes[i].name, retval);
2409 do
2410 {
2411 if (mips16_opcodes[i].pinfo != INSN_MACRO
2412 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2413 != mips16_opcodes[i].match))
2414 {
2415 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2416 mips16_opcodes[i].name, mips16_opcodes[i].args);
2417 broken = 1;
2418 }
2419 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2420 {
2421 create_insn (&mips16_nop_insn, mips16_opcodes + i);
2422 mips16_nop_insn.fixed_p = 1;
2423 }
2424 ++i;
2425 }
2426 while (i < bfd_mips16_num_opcodes
2427 && strcmp (mips16_opcodes[i].name, name) == 0);
2428 }
2429
2430 micromips_op_hash = hash_new ();
2431
2432 i = 0;
2433 while (i < bfd_micromips_num_opcodes)
2434 {
2435 const char *name = micromips_opcodes[i].name;
2436
2437 retval = hash_insert (micromips_op_hash, name,
2438 (void *) &micromips_opcodes[i]);
2439 if (retval != NULL)
2440 as_fatal (_("internal: can't hash `%s': %s"),
2441 micromips_opcodes[i].name, retval);
2442 do
2443 if (micromips_opcodes[i].pinfo != INSN_MACRO)
2444 {
2445 struct mips_cl_insn *micromips_nop_insn;
2446
2447 if (!validate_micromips_insn (&micromips_opcodes[i]))
2448 broken = 1;
2449
2450 if (micromips_insn_length (micromips_opcodes + i) == 2)
2451 micromips_nop_insn = &micromips_nop16_insn;
2452 else if (micromips_insn_length (micromips_opcodes + i) == 4)
2453 micromips_nop_insn = &micromips_nop32_insn;
2454 else
2455 continue;
2456
2457 if (micromips_nop_insn->insn_mo == NULL
2458 && strcmp (name, "nop") == 0)
2459 {
2460 create_insn (micromips_nop_insn, micromips_opcodes + i);
2461 micromips_nop_insn->fixed_p = 1;
2462 }
2463 }
2464 while (++i < bfd_micromips_num_opcodes
2465 && strcmp (micromips_opcodes[i].name, name) == 0);
2466 }
2467
2468 if (broken)
2469 as_fatal (_("Broken assembler. No assembly attempted."));
2470
2471 /* We add all the general register names to the symbol table. This
2472 helps us detect invalid uses of them. */
2473 for (i = 0; reg_names[i].name; i++)
2474 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
2475 reg_names[i].num, /* & RNUM_MASK, */
2476 &zero_address_frag));
2477 if (HAVE_NEWABI)
2478 for (i = 0; reg_names_n32n64[i].name; i++)
2479 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
2480 reg_names_n32n64[i].num, /* & RNUM_MASK, */
2481 &zero_address_frag));
2482 else
2483 for (i = 0; reg_names_o32[i].name; i++)
2484 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
2485 reg_names_o32[i].num, /* & RNUM_MASK, */
2486 &zero_address_frag));
2487
2488 mips_no_prev_insn ();
2489
2490 mips_gprmask = 0;
2491 mips_cprmask[0] = 0;
2492 mips_cprmask[1] = 0;
2493 mips_cprmask[2] = 0;
2494 mips_cprmask[3] = 0;
2495
2496 /* set the default alignment for the text section (2**2) */
2497 record_alignment (text_section, 2);
2498
2499 bfd_set_gp_size (stdoutput, g_switch_value);
2500
2501 #ifdef OBJ_ELF
2502 if (IS_ELF)
2503 {
2504 /* On a native system other than VxWorks, sections must be aligned
2505 to 16 byte boundaries. When configured for an embedded ELF
2506 target, we don't bother. */
2507 if (strncmp (TARGET_OS, "elf", 3) != 0
2508 && strncmp (TARGET_OS, "vxworks", 7) != 0)
2509 {
2510 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2511 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2512 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2513 }
2514
2515 /* Create a .reginfo section for register masks and a .mdebug
2516 section for debugging information. */
2517 {
2518 segT seg;
2519 subsegT subseg;
2520 flagword flags;
2521 segT sec;
2522
2523 seg = now_seg;
2524 subseg = now_subseg;
2525
2526 /* The ABI says this section should be loaded so that the
2527 running program can access it. However, we don't load it
2528 if we are configured for an embedded target */
2529 flags = SEC_READONLY | SEC_DATA;
2530 if (strncmp (TARGET_OS, "elf", 3) != 0)
2531 flags |= SEC_ALLOC | SEC_LOAD;
2532
2533 if (mips_abi != N64_ABI)
2534 {
2535 sec = subseg_new (".reginfo", (subsegT) 0);
2536
2537 bfd_set_section_flags (stdoutput, sec, flags);
2538 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
2539
2540 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
2541 }
2542 else
2543 {
2544 /* The 64-bit ABI uses a .MIPS.options section rather than
2545 .reginfo section. */
2546 sec = subseg_new (".MIPS.options", (subsegT) 0);
2547 bfd_set_section_flags (stdoutput, sec, flags);
2548 bfd_set_section_alignment (stdoutput, sec, 3);
2549
2550 /* Set up the option header. */
2551 {
2552 Elf_Internal_Options opthdr;
2553 char *f;
2554
2555 opthdr.kind = ODK_REGINFO;
2556 opthdr.size = (sizeof (Elf_External_Options)
2557 + sizeof (Elf64_External_RegInfo));
2558 opthdr.section = 0;
2559 opthdr.info = 0;
2560 f = frag_more (sizeof (Elf_External_Options));
2561 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2562 (Elf_External_Options *) f);
2563
2564 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2565 }
2566 }
2567
2568 if (ECOFF_DEBUGGING)
2569 {
2570 sec = subseg_new (".mdebug", (subsegT) 0);
2571 (void) bfd_set_section_flags (stdoutput, sec,
2572 SEC_HAS_CONTENTS | SEC_READONLY);
2573 (void) bfd_set_section_alignment (stdoutput, sec, 2);
2574 }
2575 else if (mips_flag_pdr)
2576 {
2577 pdr_seg = subseg_new (".pdr", (subsegT) 0);
2578 (void) bfd_set_section_flags (stdoutput, pdr_seg,
2579 SEC_READONLY | SEC_RELOC
2580 | SEC_DEBUGGING);
2581 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2582 }
2583
2584 subseg_set (seg, subseg);
2585 }
2586 }
2587 #endif /* OBJ_ELF */
2588
2589 if (! ECOFF_DEBUGGING)
2590 md_obj_begin ();
2591
2592 if (mips_fix_vr4120)
2593 init_vr4120_conflicts ();
2594 }
2595
2596 void
2597 md_mips_end (void)
2598 {
2599 mips_emit_delays ();
2600 if (! ECOFF_DEBUGGING)
2601 md_obj_end ();
2602 }
2603
2604 void
2605 md_assemble (char *str)
2606 {
2607 struct mips_cl_insn insn;
2608 bfd_reloc_code_real_type unused_reloc[3]
2609 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2610
2611 imm_expr.X_op = O_absent;
2612 imm2_expr.X_op = O_absent;
2613 offset_expr.X_op = O_absent;
2614 imm_reloc[0] = BFD_RELOC_UNUSED;
2615 imm_reloc[1] = BFD_RELOC_UNUSED;
2616 imm_reloc[2] = BFD_RELOC_UNUSED;
2617 offset_reloc[0] = BFD_RELOC_UNUSED;
2618 offset_reloc[1] = BFD_RELOC_UNUSED;
2619 offset_reloc[2] = BFD_RELOC_UNUSED;
2620
2621 mips_mark_labels ();
2622 mips_assembling_insn = TRUE;
2623
2624 if (mips_opts.mips16)
2625 mips16_ip (str, &insn);
2626 else
2627 {
2628 mips_ip (str, &insn);
2629 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2630 str, insn.insn_opcode));
2631 }
2632
2633 if (insn_error)
2634 as_bad ("%s `%s'", insn_error, str);
2635 else if (insn.insn_mo->pinfo == INSN_MACRO)
2636 {
2637 macro_start ();
2638 if (mips_opts.mips16)
2639 mips16_macro (&insn);
2640 else
2641 macro (&insn);
2642 macro_end ();
2643 }
2644 else
2645 {
2646 if (imm_expr.X_op != O_absent)
2647 append_insn (&insn, &imm_expr, imm_reloc, FALSE);
2648 else if (offset_expr.X_op != O_absent)
2649 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
2650 else
2651 append_insn (&insn, NULL, unused_reloc, FALSE);
2652 }
2653
2654 mips_assembling_insn = FALSE;
2655 }
2656
2657 /* Convenience functions for abstracting away the differences between
2658 MIPS16 and non-MIPS16 relocations. */
2659
2660 static inline bfd_boolean
2661 mips16_reloc_p (bfd_reloc_code_real_type reloc)
2662 {
2663 switch (reloc)
2664 {
2665 case BFD_RELOC_MIPS16_JMP:
2666 case BFD_RELOC_MIPS16_GPREL:
2667 case BFD_RELOC_MIPS16_GOT16:
2668 case BFD_RELOC_MIPS16_CALL16:
2669 case BFD_RELOC_MIPS16_HI16_S:
2670 case BFD_RELOC_MIPS16_HI16:
2671 case BFD_RELOC_MIPS16_LO16:
2672 return TRUE;
2673
2674 default:
2675 return FALSE;
2676 }
2677 }
2678
2679 static inline bfd_boolean
2680 micromips_reloc_p (bfd_reloc_code_real_type reloc)
2681 {
2682 switch (reloc)
2683 {
2684 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2685 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2686 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2687 case BFD_RELOC_MICROMIPS_GPREL16:
2688 case BFD_RELOC_MICROMIPS_JMP:
2689 case BFD_RELOC_MICROMIPS_HI16:
2690 case BFD_RELOC_MICROMIPS_HI16_S:
2691 case BFD_RELOC_MICROMIPS_LO16:
2692 case BFD_RELOC_MICROMIPS_LITERAL:
2693 case BFD_RELOC_MICROMIPS_GOT16:
2694 case BFD_RELOC_MICROMIPS_CALL16:
2695 case BFD_RELOC_MICROMIPS_GOT_HI16:
2696 case BFD_RELOC_MICROMIPS_GOT_LO16:
2697 case BFD_RELOC_MICROMIPS_CALL_HI16:
2698 case BFD_RELOC_MICROMIPS_CALL_LO16:
2699 case BFD_RELOC_MICROMIPS_SUB:
2700 case BFD_RELOC_MICROMIPS_GOT_PAGE:
2701 case BFD_RELOC_MICROMIPS_GOT_OFST:
2702 case BFD_RELOC_MICROMIPS_GOT_DISP:
2703 case BFD_RELOC_MICROMIPS_HIGHEST:
2704 case BFD_RELOC_MICROMIPS_HIGHER:
2705 case BFD_RELOC_MICROMIPS_SCN_DISP:
2706 case BFD_RELOC_MICROMIPS_JALR:
2707 return TRUE;
2708
2709 default:
2710 return FALSE;
2711 }
2712 }
2713
2714 static inline bfd_boolean
2715 jmp_reloc_p (bfd_reloc_code_real_type reloc)
2716 {
2717 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
2718 }
2719
2720 static inline bfd_boolean
2721 got16_reloc_p (bfd_reloc_code_real_type reloc)
2722 {
2723 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
2724 || reloc == BFD_RELOC_MICROMIPS_GOT16);
2725 }
2726
2727 static inline bfd_boolean
2728 hi16_reloc_p (bfd_reloc_code_real_type reloc)
2729 {
2730 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
2731 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
2732 }
2733
2734 static inline bfd_boolean
2735 lo16_reloc_p (bfd_reloc_code_real_type reloc)
2736 {
2737 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
2738 || reloc == BFD_RELOC_MICROMIPS_LO16);
2739 }
2740
2741 static inline bfd_boolean
2742 jalr_reloc_p (bfd_reloc_code_real_type reloc)
2743 {
2744 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
2745 }
2746
2747 /* Return true if the given relocation might need a matching %lo().
2748 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2749 need a matching %lo() when applied to local symbols. */
2750
2751 static inline bfd_boolean
2752 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
2753 {
2754 return (HAVE_IN_PLACE_ADDENDS
2755 && (hi16_reloc_p (reloc)
2756 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2757 all GOT16 relocations evaluate to "G". */
2758 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
2759 }
2760
2761 /* Return the type of %lo() reloc needed by RELOC, given that
2762 reloc_needs_lo_p. */
2763
2764 static inline bfd_reloc_code_real_type
2765 matching_lo_reloc (bfd_reloc_code_real_type reloc)
2766 {
2767 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
2768 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
2769 : BFD_RELOC_LO16));
2770 }
2771
2772 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
2773 relocation. */
2774
2775 static inline bfd_boolean
2776 fixup_has_matching_lo_p (fixS *fixp)
2777 {
2778 return (fixp->fx_next != NULL
2779 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
2780 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2781 && fixp->fx_offset == fixp->fx_next->fx_offset);
2782 }
2783
2784 /* This function returns true if modifying a register requires a
2785 delay. */
2786
2787 static int
2788 reg_needs_delay (unsigned int reg)
2789 {
2790 unsigned long prev_pinfo;
2791
2792 prev_pinfo = history[0].insn_mo->pinfo;
2793 if (! mips_opts.noreorder
2794 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2795 && ! gpr_interlocks)
2796 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2797 && ! cop_interlocks)))
2798 {
2799 /* A load from a coprocessor or from memory. All load delays
2800 delay the use of general register rt for one instruction. */
2801 /* Itbl support may require additional care here. */
2802 know (prev_pinfo & INSN_WRITE_GPR_T);
2803 if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
2804 return 1;
2805 }
2806
2807 return 0;
2808 }
2809
2810 /* Move all labels in LABELS to the current insertion point. TEXT_P
2811 says whether the labels refer to text or data. */
2812
2813 static void
2814 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
2815 {
2816 struct insn_label_list *l;
2817 valueT val;
2818
2819 for (l = labels; l != NULL; l = l->next)
2820 {
2821 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
2822 symbol_set_frag (l->label, frag_now);
2823 val = (valueT) frag_now_fix ();
2824 /* MIPS16/microMIPS text labels are stored as odd. */
2825 if (text_p && HAVE_CODE_COMPRESSION)
2826 ++val;
2827 S_SET_VALUE (l->label, val);
2828 }
2829 }
2830
2831 /* Move all labels in insn_labels to the current insertion point
2832 and treat them as text labels. */
2833
2834 static void
2835 mips_move_text_labels (void)
2836 {
2837 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
2838 }
2839
2840 static bfd_boolean
2841 s_is_linkonce (symbolS *sym, segT from_seg)
2842 {
2843 bfd_boolean linkonce = FALSE;
2844 segT symseg = S_GET_SEGMENT (sym);
2845
2846 if (symseg != from_seg && !S_IS_LOCAL (sym))
2847 {
2848 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2849 linkonce = TRUE;
2850 #ifdef OBJ_ELF
2851 /* The GNU toolchain uses an extension for ELF: a section
2852 beginning with the magic string .gnu.linkonce is a
2853 linkonce section. */
2854 if (strncmp (segment_name (symseg), ".gnu.linkonce",
2855 sizeof ".gnu.linkonce" - 1) == 0)
2856 linkonce = TRUE;
2857 #endif
2858 }
2859 return linkonce;
2860 }
2861
2862 /* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
2863 linker to handle them specially, such as generating jalx instructions
2864 when needed. We also make them odd for the duration of the assembly,
2865 in order to generate the right sort of code. We will make them even
2866 in the adjust_symtab routine, while leaving them marked. This is
2867 convenient for the debugger and the disassembler. The linker knows
2868 to make them odd again. */
2869
2870 static void
2871 mips_compressed_mark_label (symbolS *label)
2872 {
2873 gas_assert (HAVE_CODE_COMPRESSION);
2874
2875 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
2876 if (IS_ELF)
2877 {
2878 if (mips_opts.mips16)
2879 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
2880 else
2881 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
2882 }
2883 #endif
2884 if ((S_GET_VALUE (label) & 1) == 0
2885 /* Don't adjust the address if the label is global or weak, or
2886 in a link-once section, since we'll be emitting symbol reloc
2887 references to it which will be patched up by the linker, and
2888 the final value of the symbol may or may not be MIPS16/microMIPS. */
2889 && !S_IS_WEAK (label)
2890 && !S_IS_EXTERNAL (label)
2891 && !s_is_linkonce (label, now_seg))
2892 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
2893 }
2894
2895 /* Mark preceding MIPS16 or microMIPS instruction labels. */
2896
2897 static void
2898 mips_compressed_mark_labels (void)
2899 {
2900 struct insn_label_list *l;
2901
2902 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
2903 mips_compressed_mark_label (l->label);
2904 }
2905
2906 /* End the current frag. Make it a variant frag and record the
2907 relaxation info. */
2908
2909 static void
2910 relax_close_frag (void)
2911 {
2912 mips_macro_warning.first_frag = frag_now;
2913 frag_var (rs_machine_dependent, 0, 0,
2914 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
2915 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2916
2917 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2918 mips_relax.first_fixup = 0;
2919 }
2920
2921 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
2922 See the comment above RELAX_ENCODE for more details. */
2923
2924 static void
2925 relax_start (symbolS *symbol)
2926 {
2927 gas_assert (mips_relax.sequence == 0);
2928 mips_relax.sequence = 1;
2929 mips_relax.symbol = symbol;
2930 }
2931
2932 /* Start generating the second version of a relaxable sequence.
2933 See the comment above RELAX_ENCODE for more details. */
2934
2935 static void
2936 relax_switch (void)
2937 {
2938 gas_assert (mips_relax.sequence == 1);
2939 mips_relax.sequence = 2;
2940 }
2941
2942 /* End the current relaxable sequence. */
2943
2944 static void
2945 relax_end (void)
2946 {
2947 gas_assert (mips_relax.sequence == 2);
2948 relax_close_frag ();
2949 mips_relax.sequence = 0;
2950 }
2951
2952 /* Return true if IP is a delayed branch or jump. */
2953
2954 static inline bfd_boolean
2955 delayed_branch_p (const struct mips_cl_insn *ip)
2956 {
2957 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2958 | INSN_COND_BRANCH_DELAY
2959 | INSN_COND_BRANCH_LIKELY)) != 0;
2960 }
2961
2962 /* Return true if IP is a compact branch or jump. */
2963
2964 static inline bfd_boolean
2965 compact_branch_p (const struct mips_cl_insn *ip)
2966 {
2967 if (mips_opts.mips16)
2968 return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
2969 | MIPS16_INSN_COND_BRANCH)) != 0;
2970 else
2971 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
2972 | INSN2_COND_BRANCH)) != 0;
2973 }
2974
2975 /* Return true if IP is an unconditional branch or jump. */
2976
2977 static inline bfd_boolean
2978 uncond_branch_p (const struct mips_cl_insn *ip)
2979 {
2980 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
2981 || (mips_opts.mips16
2982 ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
2983 : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
2984 }
2985
2986 /* Return true if IP is a branch-likely instruction. */
2987
2988 static inline bfd_boolean
2989 branch_likely_p (const struct mips_cl_insn *ip)
2990 {
2991 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
2992 }
2993
2994 /* Return the type of nop that should be used to fill the delay slot
2995 of delayed branch IP. */
2996
2997 static struct mips_cl_insn *
2998 get_delay_slot_nop (const struct mips_cl_insn *ip)
2999 {
3000 if (mips_opts.micromips
3001 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3002 return &micromips_nop32_insn;
3003 return NOP_INSN;
3004 }
3005
3006 /* Return the mask of core registers that IP reads or writes. */
3007
3008 static unsigned int
3009 gpr_mod_mask (const struct mips_cl_insn *ip)
3010 {
3011 unsigned long pinfo2;
3012 unsigned int mask;
3013
3014 mask = 0;
3015 pinfo2 = ip->insn_mo->pinfo2;
3016 if (mips_opts.micromips)
3017 {
3018 if (pinfo2 & INSN2_MOD_GPR_MD)
3019 mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
3020 if (pinfo2 & INSN2_MOD_GPR_MF)
3021 mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
3022 if (pinfo2 & INSN2_MOD_SP)
3023 mask |= 1 << SP;
3024 }
3025 return mask;
3026 }
3027
3028 /* Return the mask of core registers that IP reads. */
3029
3030 static unsigned int
3031 gpr_read_mask (const struct mips_cl_insn *ip)
3032 {
3033 unsigned long pinfo, pinfo2;
3034 unsigned int mask;
3035
3036 mask = gpr_mod_mask (ip);
3037 pinfo = ip->insn_mo->pinfo;
3038 pinfo2 = ip->insn_mo->pinfo2;
3039 if (mips_opts.mips16)
3040 {
3041 if (pinfo & MIPS16_INSN_READ_X)
3042 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3043 if (pinfo & MIPS16_INSN_READ_Y)
3044 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3045 if (pinfo & MIPS16_INSN_READ_T)
3046 mask |= 1 << TREG;
3047 if (pinfo & MIPS16_INSN_READ_SP)
3048 mask |= 1 << SP;
3049 if (pinfo & MIPS16_INSN_READ_31)
3050 mask |= 1 << RA;
3051 if (pinfo & MIPS16_INSN_READ_Z)
3052 mask |= 1 << (mips16_to_32_reg_map
3053 [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3054 if (pinfo & MIPS16_INSN_READ_GPR_X)
3055 mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3056 }
3057 else
3058 {
3059 if (pinfo2 & INSN2_READ_GPR_D)
3060 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3061 if (pinfo & INSN_READ_GPR_T)
3062 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3063 if (pinfo & INSN_READ_GPR_S)
3064 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3065 if (pinfo2 & INSN2_READ_GP)
3066 mask |= 1 << GP;
3067 if (pinfo2 & INSN2_READ_GPR_31)
3068 mask |= 1 << RA;
3069 if (pinfo2 & INSN2_READ_GPR_Z)
3070 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3071 }
3072 if (mips_opts.micromips)
3073 {
3074 if (pinfo2 & INSN2_READ_GPR_MC)
3075 mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3076 if (pinfo2 & INSN2_READ_GPR_ME)
3077 mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3078 if (pinfo2 & INSN2_READ_GPR_MG)
3079 mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3080 if (pinfo2 & INSN2_READ_GPR_MJ)
3081 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3082 if (pinfo2 & INSN2_READ_GPR_MMN)
3083 {
3084 mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3085 mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3086 }
3087 if (pinfo2 & INSN2_READ_GPR_MP)
3088 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3089 if (pinfo2 & INSN2_READ_GPR_MQ)
3090 mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3091 }
3092 /* Don't include register 0. */
3093 return mask & ~1;
3094 }
3095
3096 /* Return the mask of core registers that IP writes. */
3097
3098 static unsigned int
3099 gpr_write_mask (const struct mips_cl_insn *ip)
3100 {
3101 unsigned long pinfo, pinfo2;
3102 unsigned int mask;
3103
3104 mask = gpr_mod_mask (ip);
3105 pinfo = ip->insn_mo->pinfo;
3106 pinfo2 = ip->insn_mo->pinfo2;
3107 if (mips_opts.mips16)
3108 {
3109 if (pinfo & MIPS16_INSN_WRITE_X)
3110 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3111 if (pinfo & MIPS16_INSN_WRITE_Y)
3112 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3113 if (pinfo & MIPS16_INSN_WRITE_Z)
3114 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3115 if (pinfo & MIPS16_INSN_WRITE_T)
3116 mask |= 1 << TREG;
3117 if (pinfo & MIPS16_INSN_WRITE_SP)
3118 mask |= 1 << SP;
3119 if (pinfo & MIPS16_INSN_WRITE_31)
3120 mask |= 1 << RA;
3121 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3122 mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3123 }
3124 else
3125 {
3126 if (pinfo & INSN_WRITE_GPR_D)
3127 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3128 if (pinfo & INSN_WRITE_GPR_T)
3129 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3130 if (pinfo & INSN_WRITE_GPR_S)
3131 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3132 if (pinfo & INSN_WRITE_GPR_31)
3133 mask |= 1 << RA;
3134 if (pinfo2 & INSN2_WRITE_GPR_Z)
3135 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3136 }
3137 if (mips_opts.micromips)
3138 {
3139 if (pinfo2 & INSN2_WRITE_GPR_MB)
3140 mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3141 if (pinfo2 & INSN2_WRITE_GPR_MHI)
3142 {
3143 mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3144 mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3145 }
3146 if (pinfo2 & INSN2_WRITE_GPR_MJ)
3147 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3148 if (pinfo2 & INSN2_WRITE_GPR_MP)
3149 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3150 }
3151 /* Don't include register 0. */
3152 return mask & ~1;
3153 }
3154
3155 /* Return the mask of floating-point registers that IP reads. */
3156
3157 static unsigned int
3158 fpr_read_mask (const struct mips_cl_insn *ip)
3159 {
3160 unsigned long pinfo, pinfo2;
3161 unsigned int mask;
3162
3163 mask = 0;
3164 pinfo = ip->insn_mo->pinfo;
3165 pinfo2 = ip->insn_mo->pinfo2;
3166 if (!mips_opts.mips16)
3167 {
3168 if (pinfo2 & INSN2_READ_FPR_D)
3169 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3170 if (pinfo & INSN_READ_FPR_S)
3171 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3172 if (pinfo & INSN_READ_FPR_T)
3173 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3174 if (pinfo & INSN_READ_FPR_R)
3175 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
3176 if (pinfo2 & INSN2_READ_FPR_Z)
3177 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3178 }
3179 /* Conservatively treat all operands to an FP_D instruction are doubles.
3180 (This is overly pessimistic for things like cvt.d.s.) */
3181 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3182 mask |= mask << 1;
3183 return mask;
3184 }
3185
3186 /* Return the mask of floating-point registers that IP writes. */
3187
3188 static unsigned int
3189 fpr_write_mask (const struct mips_cl_insn *ip)
3190 {
3191 unsigned long pinfo, pinfo2;
3192 unsigned int mask;
3193
3194 mask = 0;
3195 pinfo = ip->insn_mo->pinfo;
3196 pinfo2 = ip->insn_mo->pinfo2;
3197 if (!mips_opts.mips16)
3198 {
3199 if (pinfo & INSN_WRITE_FPR_D)
3200 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3201 if (pinfo & INSN_WRITE_FPR_S)
3202 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3203 if (pinfo & INSN_WRITE_FPR_T)
3204 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3205 if (pinfo2 & INSN2_WRITE_FPR_Z)
3206 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3207 }
3208 /* Conservatively treat all operands to an FP_D instruction are doubles.
3209 (This is overly pessimistic for things like cvt.s.d.) */
3210 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3211 mask |= mask << 1;
3212 return mask;
3213 }
3214
3215 /* Classify an instruction according to the FIX_VR4120_* enumeration.
3216 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3217 by VR4120 errata. */
3218
3219 static unsigned int
3220 classify_vr4120_insn (const char *name)
3221 {
3222 if (strncmp (name, "macc", 4) == 0)
3223 return FIX_VR4120_MACC;
3224 if (strncmp (name, "dmacc", 5) == 0)
3225 return FIX_VR4120_DMACC;
3226 if (strncmp (name, "mult", 4) == 0)
3227 return FIX_VR4120_MULT;
3228 if (strncmp (name, "dmult", 5) == 0)
3229 return FIX_VR4120_DMULT;
3230 if (strstr (name, "div"))
3231 return FIX_VR4120_DIV;
3232 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3233 return FIX_VR4120_MTHILO;
3234 return NUM_FIX_VR4120_CLASSES;
3235 }
3236
3237 #define INSN_ERET 0x42000018
3238 #define INSN_DERET 0x4200001f
3239
3240 /* Return the number of instructions that must separate INSN1 and INSN2,
3241 where INSN1 is the earlier instruction. Return the worst-case value
3242 for any INSN2 if INSN2 is null. */
3243
3244 static unsigned int
3245 insns_between (const struct mips_cl_insn *insn1,
3246 const struct mips_cl_insn *insn2)
3247 {
3248 unsigned long pinfo1, pinfo2;
3249 unsigned int mask;
3250
3251 /* This function needs to know which pinfo flags are set for INSN2
3252 and which registers INSN2 uses. The former is stored in PINFO2 and
3253 the latter is tested via INSN2_USES_GPR. If INSN2 is null, PINFO2
3254 will have every flag set and INSN2_USES_GPR will always return true. */
3255 pinfo1 = insn1->insn_mo->pinfo;
3256 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
3257
3258 #define INSN2_USES_GPR(REG) \
3259 (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
3260
3261 /* For most targets, write-after-read dependencies on the HI and LO
3262 registers must be separated by at least two instructions. */
3263 if (!hilo_interlocks)
3264 {
3265 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3266 return 2;
3267 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3268 return 2;
3269 }
3270
3271 /* If we're working around r7000 errata, there must be two instructions
3272 between an mfhi or mflo and any instruction that uses the result. */
3273 if (mips_7000_hilo_fix
3274 && !mips_opts.micromips
3275 && MF_HILO_INSN (pinfo1)
3276 && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
3277 return 2;
3278
3279 /* If we're working around 24K errata, one instruction is required
3280 if an ERET or DERET is followed by a branch instruction. */
3281 if (mips_fix_24k && !mips_opts.micromips)
3282 {
3283 if (insn1->insn_opcode == INSN_ERET
3284 || insn1->insn_opcode == INSN_DERET)
3285 {
3286 if (insn2 == NULL
3287 || insn2->insn_opcode == INSN_ERET
3288 || insn2->insn_opcode == INSN_DERET
3289 || delayed_branch_p (insn2))
3290 return 1;
3291 }
3292 }
3293
3294 /* If working around VR4120 errata, check for combinations that need
3295 a single intervening instruction. */
3296 if (mips_fix_vr4120 && !mips_opts.micromips)
3297 {
3298 unsigned int class1, class2;
3299
3300 class1 = classify_vr4120_insn (insn1->insn_mo->name);
3301 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
3302 {
3303 if (insn2 == NULL)
3304 return 1;
3305 class2 = classify_vr4120_insn (insn2->insn_mo->name);
3306 if (vr4120_conflicts[class1] & (1 << class2))
3307 return 1;
3308 }
3309 }
3310
3311 if (!HAVE_CODE_COMPRESSION)
3312 {
3313 /* Check for GPR or coprocessor load delays. All such delays
3314 are on the RT register. */
3315 /* Itbl support may require additional care here. */
3316 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3317 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
3318 {
3319 know (pinfo1 & INSN_WRITE_GPR_T);
3320 if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
3321 return 1;
3322 }
3323
3324 /* Check for generic coprocessor hazards.
3325
3326 This case is not handled very well. There is no special
3327 knowledge of CP0 handling, and the coprocessors other than
3328 the floating point unit are not distinguished at all. */
3329 /* Itbl support may require additional care here. FIXME!
3330 Need to modify this to include knowledge about
3331 user specified delays! */
3332 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3333 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3334 {
3335 /* Handle cases where INSN1 writes to a known general coprocessor
3336 register. There must be a one instruction delay before INSN2
3337 if INSN2 reads that register, otherwise no delay is needed. */
3338 mask = fpr_write_mask (insn1);
3339 if (mask != 0)
3340 {
3341 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
3342 return 1;
3343 }
3344 else
3345 {
3346 /* Read-after-write dependencies on the control registers
3347 require a two-instruction gap. */
3348 if ((pinfo1 & INSN_WRITE_COND_CODE)
3349 && (pinfo2 & INSN_READ_COND_CODE))
3350 return 2;
3351
3352 /* We don't know exactly what INSN1 does. If INSN2 is
3353 also a coprocessor instruction, assume there must be
3354 a one instruction gap. */
3355 if (pinfo2 & INSN_COP)
3356 return 1;
3357 }
3358 }
3359
3360 /* Check for read-after-write dependencies on the coprocessor
3361 control registers in cases where INSN1 does not need a general
3362 coprocessor delay. This means that INSN1 is a floating point
3363 comparison instruction. */
3364 /* Itbl support may require additional care here. */
3365 else if (!cop_interlocks
3366 && (pinfo1 & INSN_WRITE_COND_CODE)
3367 && (pinfo2 & INSN_READ_COND_CODE))
3368 return 1;
3369 }
3370
3371 #undef INSN2_USES_GPR
3372
3373 return 0;
3374 }
3375
3376 /* Return the number of nops that would be needed to work around the
3377 VR4130 mflo/mfhi errata if instruction INSN immediately followed
3378 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
3379 that are contained within the first IGNORE instructions of HIST. */
3380
3381 static int
3382 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
3383 const struct mips_cl_insn *insn)
3384 {
3385 int i, j;
3386 unsigned int mask;
3387
3388 /* Check if the instruction writes to HI or LO. MTHI and MTLO
3389 are not affected by the errata. */
3390 if (insn != 0
3391 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3392 || strcmp (insn->insn_mo->name, "mtlo") == 0
3393 || strcmp (insn->insn_mo->name, "mthi") == 0))
3394 return 0;
3395
3396 /* Search for the first MFLO or MFHI. */
3397 for (i = 0; i < MAX_VR4130_NOPS; i++)
3398 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
3399 {
3400 /* Extract the destination register. */
3401 mask = gpr_write_mask (&hist[i]);
3402
3403 /* No nops are needed if INSN reads that register. */
3404 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
3405 return 0;
3406
3407 /* ...or if any of the intervening instructions do. */
3408 for (j = 0; j < i; j++)
3409 if (gpr_read_mask (&hist[j]) & mask)
3410 return 0;
3411
3412 if (i >= ignore)
3413 return MAX_VR4130_NOPS - i;
3414 }
3415 return 0;
3416 }
3417
3418 #define BASE_REG_EQ(INSN1, INSN2) \
3419 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3420 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3421
3422 /* Return the minimum alignment for this store instruction. */
3423
3424 static int
3425 fix_24k_align_to (const struct mips_opcode *mo)
3426 {
3427 if (strcmp (mo->name, "sh") == 0)
3428 return 2;
3429
3430 if (strcmp (mo->name, "swc1") == 0
3431 || strcmp (mo->name, "swc2") == 0
3432 || strcmp (mo->name, "sw") == 0
3433 || strcmp (mo->name, "sc") == 0
3434 || strcmp (mo->name, "s.s") == 0)
3435 return 4;
3436
3437 if (strcmp (mo->name, "sdc1") == 0
3438 || strcmp (mo->name, "sdc2") == 0
3439 || strcmp (mo->name, "s.d") == 0)
3440 return 8;
3441
3442 /* sb, swl, swr */
3443 return 1;
3444 }
3445
3446 struct fix_24k_store_info
3447 {
3448 /* Immediate offset, if any, for this store instruction. */
3449 short off;
3450 /* Alignment required by this store instruction. */
3451 int align_to;
3452 /* True for register offsets. */
3453 int register_offset;
3454 };
3455
3456 /* Comparison function used by qsort. */
3457
3458 static int
3459 fix_24k_sort (const void *a, const void *b)
3460 {
3461 const struct fix_24k_store_info *pos1 = a;
3462 const struct fix_24k_store_info *pos2 = b;
3463
3464 return (pos1->off - pos2->off);
3465 }
3466
3467 /* INSN is a store instruction. Try to record the store information
3468 in STINFO. Return false if the information isn't known. */
3469
3470 static bfd_boolean
3471 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
3472 const struct mips_cl_insn *insn)
3473 {
3474 /* The instruction must have a known offset. */
3475 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3476 return FALSE;
3477
3478 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3479 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3480 return TRUE;
3481 }
3482
3483 /* Return the number of nops that would be needed to work around the 24k
3484 "lost data on stores during refill" errata if instruction INSN
3485 immediately followed the 2 instructions described by HIST.
3486 Ignore hazards that are contained within the first IGNORE
3487 instructions of HIST.
3488
3489 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3490 for the data cache refills and store data. The following describes
3491 the scenario where the store data could be lost.
3492
3493 * A data cache miss, due to either a load or a store, causing fill
3494 data to be supplied by the memory subsystem
3495 * The first three doublewords of fill data are returned and written
3496 into the cache
3497 * A sequence of four stores occurs in consecutive cycles around the
3498 final doubleword of the fill:
3499 * Store A
3500 * Store B
3501 * Store C
3502 * Zero, One or more instructions
3503 * Store D
3504
3505 The four stores A-D must be to different doublewords of the line that
3506 is being filled. The fourth instruction in the sequence above permits
3507 the fill of the final doubleword to be transferred from the FSB into
3508 the cache. In the sequence above, the stores may be either integer
3509 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3510 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3511 different doublewords on the line. If the floating point unit is
3512 running in 1:2 mode, it is not possible to create the sequence above
3513 using only floating point store instructions.
3514
3515 In this case, the cache line being filled is incorrectly marked
3516 invalid, thereby losing the data from any store to the line that
3517 occurs between the original miss and the completion of the five
3518 cycle sequence shown above.
3519
3520 The workarounds are:
3521
3522 * Run the data cache in write-through mode.
3523 * Insert a non-store instruction between
3524 Store A and Store B or Store B and Store C. */
3525
3526 static int
3527 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
3528 const struct mips_cl_insn *insn)
3529 {
3530 struct fix_24k_store_info pos[3];
3531 int align, i, base_offset;
3532
3533 if (ignore >= 2)
3534 return 0;
3535
3536 /* If the previous instruction wasn't a store, there's nothing to
3537 worry about. */
3538 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3539 return 0;
3540
3541 /* If the instructions after the previous one are unknown, we have
3542 to assume the worst. */
3543 if (!insn)
3544 return 1;
3545
3546 /* Check whether we are dealing with three consecutive stores. */
3547 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3548 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3549 return 0;
3550
3551 /* If we don't know the relationship between the store addresses,
3552 assume the worst. */
3553 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
3554 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3555 return 1;
3556
3557 if (!fix_24k_record_store_info (&pos[0], insn)
3558 || !fix_24k_record_store_info (&pos[1], &hist[0])
3559 || !fix_24k_record_store_info (&pos[2], &hist[1]))
3560 return 1;
3561
3562 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3563
3564 /* Pick a value of ALIGN and X such that all offsets are adjusted by
3565 X bytes and such that the base register + X is known to be aligned
3566 to align bytes. */
3567
3568 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3569 align = 8;
3570 else
3571 {
3572 align = pos[0].align_to;
3573 base_offset = pos[0].off;
3574 for (i = 1; i < 3; i++)
3575 if (align < pos[i].align_to)
3576 {
3577 align = pos[i].align_to;
3578 base_offset = pos[i].off;
3579 }
3580 for (i = 0; i < 3; i++)
3581 pos[i].off -= base_offset;
3582 }
3583
3584 pos[0].off &= ~align + 1;
3585 pos[1].off &= ~align + 1;
3586 pos[2].off &= ~align + 1;
3587
3588 /* If any two stores write to the same chunk, they also write to the
3589 same doubleword. The offsets are still sorted at this point. */
3590 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3591 return 0;
3592
3593 /* A range of at least 9 bytes is needed for the stores to be in
3594 non-overlapping doublewords. */
3595 if (pos[2].off - pos[0].off <= 8)
3596 return 0;
3597
3598 if (pos[2].off - pos[1].off >= 24
3599 || pos[1].off - pos[0].off >= 24
3600 || pos[2].off - pos[0].off >= 32)
3601 return 0;
3602
3603 return 1;
3604 }
3605
3606 /* Return the number of nops that would be needed if instruction INSN
3607 immediately followed the MAX_NOPS instructions given by HIST,
3608 where HIST[0] is the most recent instruction. Ignore hazards
3609 between INSN and the first IGNORE instructions in HIST.
3610
3611 If INSN is null, return the worse-case number of nops for any
3612 instruction. */
3613
3614 static int
3615 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
3616 const struct mips_cl_insn *insn)
3617 {
3618 int i, nops, tmp_nops;
3619
3620 nops = 0;
3621 for (i = ignore; i < MAX_DELAY_NOPS; i++)
3622 {
3623 tmp_nops = insns_between (hist + i, insn) - i;
3624 if (tmp_nops > nops)
3625 nops = tmp_nops;
3626 }
3627
3628 if (mips_fix_vr4130 && !mips_opts.micromips)
3629 {
3630 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
3631 if (tmp_nops > nops)
3632 nops = tmp_nops;
3633 }
3634
3635 if (mips_fix_24k && !mips_opts.micromips)
3636 {
3637 tmp_nops = nops_for_24k (ignore, hist, insn);
3638 if (tmp_nops > nops)
3639 nops = tmp_nops;
3640 }
3641
3642 return nops;
3643 }
3644
3645 /* The variable arguments provide NUM_INSNS extra instructions that
3646 might be added to HIST. Return the largest number of nops that
3647 would be needed after the extended sequence, ignoring hazards
3648 in the first IGNORE instructions. */
3649
3650 static int
3651 nops_for_sequence (int num_insns, int ignore,
3652 const struct mips_cl_insn *hist, ...)
3653 {
3654 va_list args;
3655 struct mips_cl_insn buffer[MAX_NOPS];
3656 struct mips_cl_insn *cursor;
3657 int nops;
3658
3659 va_start (args, hist);
3660 cursor = buffer + num_insns;
3661 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
3662 while (cursor > buffer)
3663 *--cursor = *va_arg (args, const struct mips_cl_insn *);
3664
3665 nops = nops_for_insn (ignore, buffer, NULL);
3666 va_end (args);
3667 return nops;
3668 }
3669
3670 /* Like nops_for_insn, but if INSN is a branch, take into account the
3671 worst-case delay for the branch target. */
3672
3673 static int
3674 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
3675 const struct mips_cl_insn *insn)
3676 {
3677 int nops, tmp_nops;
3678
3679 nops = nops_for_insn (ignore, hist, insn);
3680 if (delayed_branch_p (insn))
3681 {
3682 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
3683 hist, insn, get_delay_slot_nop (insn));
3684 if (tmp_nops > nops)
3685 nops = tmp_nops;
3686 }
3687 else if (compact_branch_p (insn))
3688 {
3689 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
3690 if (tmp_nops > nops)
3691 nops = tmp_nops;
3692 }
3693 return nops;
3694 }
3695
3696 /* Fix NOP issue: Replace nops by "or at,at,zero". */
3697
3698 static void
3699 fix_loongson2f_nop (struct mips_cl_insn * ip)
3700 {
3701 gas_assert (!HAVE_CODE_COMPRESSION);
3702 if (strcmp (ip->insn_mo->name, "nop") == 0)
3703 ip->insn_opcode = LOONGSON2F_NOP_INSN;
3704 }
3705
3706 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
3707 jr target pc &= 'hffff_ffff_cfff_ffff. */
3708
3709 static void
3710 fix_loongson2f_jump (struct mips_cl_insn * ip)
3711 {
3712 gas_assert (!HAVE_CODE_COMPRESSION);
3713 if (strcmp (ip->insn_mo->name, "j") == 0
3714 || strcmp (ip->insn_mo->name, "jr") == 0
3715 || strcmp (ip->insn_mo->name, "jalr") == 0)
3716 {
3717 int sreg;
3718 expressionS ep;
3719
3720 if (! mips_opts.at)
3721 return;
3722
3723 sreg = EXTRACT_OPERAND (0, RS, *ip);
3724 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
3725 return;
3726
3727 ep.X_op = O_constant;
3728 ep.X_add_number = 0xcfff0000;
3729 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
3730 ep.X_add_number = 0xffff;
3731 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
3732 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
3733 }
3734 }
3735
3736 static void
3737 fix_loongson2f (struct mips_cl_insn * ip)
3738 {
3739 if (mips_fix_loongson2f_nop)
3740 fix_loongson2f_nop (ip);
3741
3742 if (mips_fix_loongson2f_jump)
3743 fix_loongson2f_jump (ip);
3744 }
3745
3746 /* IP is a branch that has a delay slot, and we need to fill it
3747 automatically. Return true if we can do that by swapping IP
3748 with the previous instruction.
3749 ADDRESS_EXPR is an operand of the instruction to be used with
3750 RELOC_TYPE. */
3751
3752 static bfd_boolean
3753 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
3754 bfd_reloc_code_real_type *reloc_type)
3755 {
3756 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
3757 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
3758
3759 /* -O2 and above is required for this optimization. */
3760 if (mips_optimize < 2)
3761 return FALSE;
3762
3763 /* If we have seen .set volatile or .set nomove, don't optimize. */
3764 if (mips_opts.nomove)
3765 return FALSE;
3766
3767 /* We can't swap if the previous instruction's position is fixed. */
3768 if (history[0].fixed_p)
3769 return FALSE;
3770
3771 /* If the previous previous insn was in a .set noreorder, we can't
3772 swap. Actually, the MIPS assembler will swap in this situation.
3773 However, gcc configured -with-gnu-as will generate code like
3774
3775 .set noreorder
3776 lw $4,XXX
3777 .set reorder
3778 INSN
3779 bne $4,$0,foo
3780
3781 in which we can not swap the bne and INSN. If gcc is not configured
3782 -with-gnu-as, it does not output the .set pseudo-ops. */
3783 if (history[1].noreorder_p)
3784 return FALSE;
3785
3786 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
3787 This means that the previous instruction was a 4-byte one anyhow. */
3788 if (mips_opts.mips16 && history[0].fixp[0])
3789 return FALSE;
3790
3791 /* If the branch is itself the target of a branch, we can not swap.
3792 We cheat on this; all we check for is whether there is a label on
3793 this instruction. If there are any branches to anything other than
3794 a label, users must use .set noreorder. */
3795 if (seg_info (now_seg)->label_list)
3796 return FALSE;
3797
3798 /* If the previous instruction is in a variant frag other than this
3799 branch's one, we cannot do the swap. This does not apply to
3800 MIPS16 code, which uses variant frags for different purposes. */
3801 if (!mips_opts.mips16
3802 && history[0].frag
3803 && history[0].frag->fr_type == rs_machine_dependent)
3804 return FALSE;
3805
3806 /* We do not swap with instructions that cannot architecturally
3807 be placed in a branch delay slot, such as SYNC or ERET. We
3808 also refrain from swapping with a trap instruction, since it
3809 complicates trap handlers to have the trap instruction be in
3810 a delay slot. */
3811 prev_pinfo = history[0].insn_mo->pinfo;
3812 if (prev_pinfo & INSN_NO_DELAY_SLOT)
3813 return FALSE;
3814
3815 /* Check for conflicts between the branch and the instructions
3816 before the candidate delay slot. */
3817 if (nops_for_insn (0, history + 1, ip) > 0)
3818 return FALSE;
3819
3820 /* Check for conflicts between the swapped sequence and the
3821 target of the branch. */
3822 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
3823 return FALSE;
3824
3825 /* If the branch reads a register that the previous
3826 instruction sets, we can not swap. */
3827 gpr_read = gpr_read_mask (ip);
3828 prev_gpr_write = gpr_write_mask (&history[0]);
3829 if (gpr_read & prev_gpr_write)
3830 return FALSE;
3831
3832 /* If the branch writes a register that the previous
3833 instruction sets, we can not swap. */
3834 gpr_write = gpr_write_mask (ip);
3835 if (gpr_write & prev_gpr_write)
3836 return FALSE;
3837
3838 /* If the branch writes a register that the previous
3839 instruction reads, we can not swap. */
3840 prev_gpr_read = gpr_read_mask (&history[0]);
3841 if (gpr_write & prev_gpr_read)
3842 return FALSE;
3843
3844 /* If one instruction sets a condition code and the
3845 other one uses a condition code, we can not swap. */
3846 pinfo = ip->insn_mo->pinfo;
3847 if ((pinfo & INSN_READ_COND_CODE)
3848 && (prev_pinfo & INSN_WRITE_COND_CODE))
3849 return FALSE;
3850 if ((pinfo & INSN_WRITE_COND_CODE)
3851 && (prev_pinfo & INSN_READ_COND_CODE))
3852 return FALSE;
3853
3854 /* If the previous instruction uses the PC, we can not swap. */
3855 prev_pinfo2 = history[0].insn_mo->pinfo2;
3856 if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
3857 return FALSE;
3858 if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
3859 return FALSE;
3860
3861 /* If the previous instruction has an incorrect size for a fixed
3862 branch delay slot in microMIPS mode, we cannot swap. */
3863 pinfo2 = ip->insn_mo->pinfo2;
3864 if (mips_opts.micromips
3865 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
3866 && insn_length (history) != 2)
3867 return FALSE;
3868 if (mips_opts.micromips
3869 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
3870 && insn_length (history) != 4)
3871 return FALSE;
3872
3873 /* On R5900 short loops need to be fixed by inserting a nop in
3874 the branch delay slots.
3875 A short loop can be terminated too early. */
3876 if (mips_opts.arch == CPU_R5900
3877 /* Check if instruction has a parameter, ignore "j $31". */
3878 && (address_expr != NULL)
3879 /* Parameter must be 16 bit. */
3880 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
3881 /* Branch to same segment. */
3882 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
3883 /* Branch to same code fragment. */
3884 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
3885 /* Can only calculate branch offset if value is known. */
3886 && symbol_constant_p(address_expr->X_add_symbol)
3887 /* Check if branch is really conditional. */
3888 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
3889 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
3890 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
3891 {
3892 int distance;
3893 /* Check if loop is shorter than 6 instructions including
3894 branch and delay slot. */
3895 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
3896 if (distance <= 20)
3897 {
3898 int i;
3899 int rv;
3900
3901 rv = FALSE;
3902 /* When the loop includes branches or jumps,
3903 it is not a short loop. */
3904 for (i = 0; i < (distance / 4); i++)
3905 {
3906 if ((history[i].cleared_p)
3907 || delayed_branch_p(&history[i]))
3908 {
3909 rv = TRUE;
3910 break;
3911 }
3912 }
3913 if (rv == FALSE)
3914 {
3915 /* Insert nop after branch to fix short loop. */
3916 return FALSE;
3917 }
3918 }
3919 }
3920
3921 return TRUE;
3922 }
3923
3924 /* Decide how we should add IP to the instruction stream.
3925 ADDRESS_EXPR is an operand of the instruction to be used with
3926 RELOC_TYPE. */
3927
3928 static enum append_method
3929 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
3930 bfd_reloc_code_real_type *reloc_type)
3931 {
3932 unsigned long pinfo;
3933
3934 /* The relaxed version of a macro sequence must be inherently
3935 hazard-free. */
3936 if (mips_relax.sequence == 2)
3937 return APPEND_ADD;
3938
3939 /* We must not dabble with instructions in a ".set norerorder" block. */
3940 if (mips_opts.noreorder)
3941 return APPEND_ADD;
3942
3943 /* Otherwise, it's our responsibility to fill branch delay slots. */
3944 if (delayed_branch_p (ip))
3945 {
3946 if (!branch_likely_p (ip)
3947 && can_swap_branch_p (ip, address_expr, reloc_type))
3948 return APPEND_SWAP;
3949
3950 pinfo = ip->insn_mo->pinfo;
3951 if (mips_opts.mips16
3952 && ISA_SUPPORTS_MIPS16E
3953 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
3954 return APPEND_ADD_COMPACT;
3955
3956 return APPEND_ADD_WITH_NOP;
3957 }
3958
3959 return APPEND_ADD;
3960 }
3961
3962 /* IP is a MIPS16 instruction whose opcode we have just changed.
3963 Point IP->insn_mo to the new opcode's definition. */
3964
3965 static void
3966 find_altered_mips16_opcode (struct mips_cl_insn *ip)
3967 {
3968 const struct mips_opcode *mo, *end;
3969
3970 end = &mips16_opcodes[bfd_mips16_num_opcodes];
3971 for (mo = ip->insn_mo; mo < end; mo++)
3972 if ((ip->insn_opcode & mo->mask) == mo->match)
3973 {
3974 ip->insn_mo = mo;
3975 return;
3976 }
3977 abort ();
3978 }
3979
3980 /* For microMIPS macros, we need to generate a local number label
3981 as the target of branches. */
3982 #define MICROMIPS_LABEL_CHAR '\037'
3983 static unsigned long micromips_target_label;
3984 static char micromips_target_name[32];
3985
3986 static char *
3987 micromips_label_name (void)
3988 {
3989 char *p = micromips_target_name;
3990 char symbol_name_temporary[24];
3991 unsigned long l;
3992 int i;
3993
3994 if (*p)
3995 return p;
3996
3997 i = 0;
3998 l = micromips_target_label;
3999 #ifdef LOCAL_LABEL_PREFIX
4000 *p++ = LOCAL_LABEL_PREFIX;
4001 #endif
4002 *p++ = 'L';
4003 *p++ = MICROMIPS_LABEL_CHAR;
4004 do
4005 {
4006 symbol_name_temporary[i++] = l % 10 + '0';
4007 l /= 10;
4008 }
4009 while (l != 0);
4010 while (i > 0)
4011 *p++ = symbol_name_temporary[--i];
4012 *p = '\0';
4013
4014 return micromips_target_name;
4015 }
4016
4017 static void
4018 micromips_label_expr (expressionS *label_expr)
4019 {
4020 label_expr->X_op = O_symbol;
4021 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
4022 label_expr->X_add_number = 0;
4023 }
4024
4025 static void
4026 micromips_label_inc (void)
4027 {
4028 micromips_target_label++;
4029 *micromips_target_name = '\0';
4030 }
4031
4032 static void
4033 micromips_add_label (void)
4034 {
4035 symbolS *s;
4036
4037 s = colon (micromips_label_name ());
4038 micromips_label_inc ();
4039 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
4040 if (IS_ELF)
4041 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
4042 #else
4043 (void) s;
4044 #endif
4045 }
4046
4047 /* If assembling microMIPS code, then return the microMIPS reloc
4048 corresponding to the requested one if any. Otherwise return
4049 the reloc unchanged. */
4050
4051 static bfd_reloc_code_real_type
4052 micromips_map_reloc (bfd_reloc_code_real_type reloc)
4053 {
4054 static const bfd_reloc_code_real_type relocs[][2] =
4055 {
4056 /* Keep sorted incrementally by the left-hand key. */
4057 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
4058 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
4059 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
4060 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
4061 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
4062 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
4063 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
4064 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
4065 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
4066 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
4067 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
4068 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
4069 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
4070 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
4071 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4072 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4073 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4074 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4075 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4076 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4077 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4078 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4079 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4080 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4081 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4082 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4083 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4084 };
4085 bfd_reloc_code_real_type r;
4086 size_t i;
4087
4088 if (!mips_opts.micromips)
4089 return reloc;
4090 for (i = 0; i < ARRAY_SIZE (relocs); i++)
4091 {
4092 r = relocs[i][0];
4093 if (r > reloc)
4094 return reloc;
4095 if (r == reloc)
4096 return relocs[i][1];
4097 }
4098 return reloc;
4099 }
4100
4101 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
4102 Return true on success, storing the resolved value in RESULT. */
4103
4104 static bfd_boolean
4105 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
4106 offsetT *result)
4107 {
4108 switch (reloc)
4109 {
4110 case BFD_RELOC_MIPS_HIGHEST:
4111 case BFD_RELOC_MICROMIPS_HIGHEST:
4112 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
4113 return TRUE;
4114
4115 case BFD_RELOC_MIPS_HIGHER:
4116 case BFD_RELOC_MICROMIPS_HIGHER:
4117 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
4118 return TRUE;
4119
4120 case BFD_RELOC_HI16_S:
4121 case BFD_RELOC_MICROMIPS_HI16_S:
4122 case BFD_RELOC_MIPS16_HI16_S:
4123 *result = ((operand + 0x8000) >> 16) & 0xffff;
4124 return TRUE;
4125
4126 case BFD_RELOC_HI16:
4127 case BFD_RELOC_MICROMIPS_HI16:
4128 case BFD_RELOC_MIPS16_HI16:
4129 *result = (operand >> 16) & 0xffff;
4130 return TRUE;
4131
4132 case BFD_RELOC_LO16:
4133 case BFD_RELOC_MICROMIPS_LO16:
4134 case BFD_RELOC_MIPS16_LO16:
4135 *result = operand & 0xffff;
4136 return TRUE;
4137
4138 case BFD_RELOC_UNUSED:
4139 *result = operand;
4140 return TRUE;
4141
4142 default:
4143 return FALSE;
4144 }
4145 }
4146
4147 /* Output an instruction. IP is the instruction information.
4148 ADDRESS_EXPR is an operand of the instruction to be used with
4149 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
4150 a macro expansion. */
4151
4152 static void
4153 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
4154 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
4155 {
4156 unsigned long prev_pinfo2, pinfo;
4157 bfd_boolean relaxed_branch = FALSE;
4158 enum append_method method;
4159 bfd_boolean relax32;
4160 int branch_disp;
4161
4162 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
4163 fix_loongson2f (ip);
4164
4165 file_ase_mips16 |= mips_opts.mips16;
4166 file_ase_micromips |= mips_opts.micromips;
4167
4168 prev_pinfo2 = history[0].insn_mo->pinfo2;
4169 pinfo = ip->insn_mo->pinfo;
4170
4171 if (mips_opts.micromips
4172 && !expansionp
4173 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4174 && micromips_insn_length (ip->insn_mo) != 2)
4175 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4176 && micromips_insn_length (ip->insn_mo) != 4)))
4177 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4178 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
4179
4180 if (address_expr == NULL)
4181 ip->complete_p = 1;
4182 else if (reloc_type[0] <= BFD_RELOC_UNUSED
4183 && reloc_type[1] == BFD_RELOC_UNUSED
4184 && reloc_type[2] == BFD_RELOC_UNUSED
4185 && address_expr->X_op == O_constant)
4186 {
4187 switch (*reloc_type)
4188 {
4189 case BFD_RELOC_MIPS_JMP:
4190 {
4191 int shift;
4192
4193 shift = mips_opts.micromips ? 1 : 2;
4194 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4195 as_bad (_("jump to misaligned address (0x%lx)"),
4196 (unsigned long) address_expr->X_add_number);
4197 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4198 & 0x3ffffff);
4199 ip->complete_p = 1;
4200 }
4201 break;
4202
4203 case BFD_RELOC_MIPS16_JMP:
4204 if ((address_expr->X_add_number & 3) != 0)
4205 as_bad (_("jump to misaligned address (0x%lx)"),
4206 (unsigned long) address_expr->X_add_number);
4207 ip->insn_opcode |=
4208 (((address_expr->X_add_number & 0x7c0000) << 3)
4209 | ((address_expr->X_add_number & 0xf800000) >> 7)
4210 | ((address_expr->X_add_number & 0x3fffc) >> 2));
4211 ip->complete_p = 1;
4212 break;
4213
4214 case BFD_RELOC_16_PCREL_S2:
4215 {
4216 int shift;
4217
4218 shift = mips_opts.micromips ? 1 : 2;
4219 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4220 as_bad (_("branch to misaligned address (0x%lx)"),
4221 (unsigned long) address_expr->X_add_number);
4222 if (!mips_relax_branch)
4223 {
4224 if ((address_expr->X_add_number + (1 << (shift + 15)))
4225 & ~((1 << (shift + 16)) - 1))
4226 as_bad (_("branch address range overflow (0x%lx)"),
4227 (unsigned long) address_expr->X_add_number);
4228 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4229 & 0xffff);
4230 }
4231 }
4232 break;
4233
4234 default:
4235 {
4236 offsetT value;
4237
4238 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
4239 &value))
4240 {
4241 ip->insn_opcode |= value & 0xffff;
4242 ip->complete_p = 1;
4243 }
4244 }
4245 break;
4246 }
4247 }
4248
4249 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4250 {
4251 /* There are a lot of optimizations we could do that we don't.
4252 In particular, we do not, in general, reorder instructions.
4253 If you use gcc with optimization, it will reorder
4254 instructions and generally do much more optimization then we
4255 do here; repeating all that work in the assembler would only
4256 benefit hand written assembly code, and does not seem worth
4257 it. */
4258 int nops = (mips_optimize == 0
4259 ? nops_for_insn (0, history, NULL)
4260 : nops_for_insn_or_target (0, history, ip));
4261 if (nops > 0)
4262 {
4263 fragS *old_frag;
4264 unsigned long old_frag_offset;
4265 int i;
4266
4267 old_frag = frag_now;
4268 old_frag_offset = frag_now_fix ();
4269
4270 for (i = 0; i < nops; i++)
4271 add_fixed_insn (NOP_INSN);
4272 insert_into_history (0, nops, NOP_INSN);
4273
4274 if (listing)
4275 {
4276 listing_prev_line ();
4277 /* We may be at the start of a variant frag. In case we
4278 are, make sure there is enough space for the frag
4279 after the frags created by listing_prev_line. The
4280 argument to frag_grow here must be at least as large
4281 as the argument to all other calls to frag_grow in
4282 this file. We don't have to worry about being in the
4283 middle of a variant frag, because the variants insert
4284 all needed nop instructions themselves. */
4285 frag_grow (40);
4286 }
4287
4288 mips_move_text_labels ();
4289
4290 #ifndef NO_ECOFF_DEBUGGING
4291 if (ECOFF_DEBUGGING)
4292 ecoff_fix_loc (old_frag, old_frag_offset);
4293 #endif
4294 }
4295 }
4296 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4297 {
4298 int nops;
4299
4300 /* Work out how many nops in prev_nop_frag are needed by IP,
4301 ignoring hazards generated by the first prev_nop_frag_since
4302 instructions. */
4303 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
4304 gas_assert (nops <= prev_nop_frag_holds);
4305
4306 /* Enforce NOPS as a minimum. */
4307 if (nops > prev_nop_frag_required)
4308 prev_nop_frag_required = nops;
4309
4310 if (prev_nop_frag_holds == prev_nop_frag_required)
4311 {
4312 /* Settle for the current number of nops. Update the history
4313 accordingly (for the benefit of any future .set reorder code). */
4314 prev_nop_frag = NULL;
4315 insert_into_history (prev_nop_frag_since,
4316 prev_nop_frag_holds, NOP_INSN);
4317 }
4318 else
4319 {
4320 /* Allow this instruction to replace one of the nops that was
4321 tentatively added to prev_nop_frag. */
4322 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
4323 prev_nop_frag_holds--;
4324 prev_nop_frag_since++;
4325 }
4326 }
4327
4328 method = get_append_method (ip, address_expr, reloc_type);
4329 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
4330
4331 #ifdef OBJ_ELF
4332 /* The value passed to dwarf2_emit_insn is the distance between
4333 the beginning of the current instruction and the address that
4334 should be recorded in the debug tables. This is normally the
4335 current address.
4336
4337 For MIPS16/microMIPS debug info we want to use ISA-encoded
4338 addresses, so we use -1 for an address higher by one than the
4339 current one.
4340
4341 If the instruction produced is a branch that we will swap with
4342 the preceding instruction, then we add the displacement by which
4343 the branch will be moved backwards. This is more appropriate
4344 and for MIPS16/microMIPS code also prevents a debugger from
4345 placing a breakpoint in the middle of the branch (and corrupting
4346 code if software breakpoints are used). */
4347 dwarf2_emit_insn ((HAVE_CODE_COMPRESSION ? -1 : 0) + branch_disp);
4348 #endif
4349
4350 relax32 = (mips_relax_branch
4351 /* Don't try branch relaxation within .set nomacro, or within
4352 .set noat if we use $at for PIC computations. If it turns
4353 out that the branch was out-of-range, we'll get an error. */
4354 && !mips_opts.warn_about_macros
4355 && (mips_opts.at || mips_pic == NO_PIC)
4356 /* Don't relax BPOSGE32/64 as they have no complementing
4357 branches. */
4358 && !(ip->insn_mo->membership & (INSN_DSP64 | INSN_DSP)));
4359
4360 if (!HAVE_CODE_COMPRESSION
4361 && address_expr
4362 && relax32
4363 && *reloc_type == BFD_RELOC_16_PCREL_S2
4364 && delayed_branch_p (ip))
4365 {
4366 relaxed_branch = TRUE;
4367 add_relaxed_insn (ip, (relaxed_branch_length
4368 (NULL, NULL,
4369 uncond_branch_p (ip) ? -1
4370 : branch_likely_p (ip) ? 1
4371 : 0)), 4,
4372 RELAX_BRANCH_ENCODE
4373 (AT,
4374 uncond_branch_p (ip),
4375 branch_likely_p (ip),
4376 pinfo & INSN_WRITE_GPR_31,
4377 0),
4378 address_expr->X_add_symbol,
4379 address_expr->X_add_number);
4380 *reloc_type = BFD_RELOC_UNUSED;
4381 }
4382 else if (mips_opts.micromips
4383 && address_expr
4384 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4385 || *reloc_type > BFD_RELOC_UNUSED)
4386 && (delayed_branch_p (ip) || compact_branch_p (ip))
4387 /* Don't try branch relaxation when users specify
4388 16-bit/32-bit instructions. */
4389 && !forced_insn_length)
4390 {
4391 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4392 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
4393 int uncond = uncond_branch_p (ip) ? -1 : 0;
4394 int compact = compact_branch_p (ip);
4395 int al = pinfo & INSN_WRITE_GPR_31;
4396 int length32;
4397
4398 gas_assert (address_expr != NULL);
4399 gas_assert (!mips_relax.sequence);
4400
4401 relaxed_branch = TRUE;
4402 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4403 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
4404 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4405 relax32, 0, 0),
4406 address_expr->X_add_symbol,
4407 address_expr->X_add_number);
4408 *reloc_type = BFD_RELOC_UNUSED;
4409 }
4410 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
4411 {
4412 /* We need to set up a variant frag. */
4413 gas_assert (address_expr != NULL);
4414 add_relaxed_insn (ip, 4, 0,
4415 RELAX_MIPS16_ENCODE
4416 (*reloc_type - BFD_RELOC_UNUSED,
4417 forced_insn_length == 2, forced_insn_length == 4,
4418 delayed_branch_p (&history[0]),
4419 history[0].mips16_absolute_jump_p),
4420 make_expr_symbol (address_expr), 0);
4421 }
4422 else if (mips_opts.mips16 && insn_length (ip) == 2)
4423 {
4424 if (!delayed_branch_p (ip))
4425 /* Make sure there is enough room to swap this instruction with
4426 a following jump instruction. */
4427 frag_grow (6);
4428 add_fixed_insn (ip);
4429 }
4430 else
4431 {
4432 if (mips_opts.mips16
4433 && mips_opts.noreorder
4434 && delayed_branch_p (&history[0]))
4435 as_warn (_("extended instruction in delay slot"));
4436
4437 if (mips_relax.sequence)
4438 {
4439 /* If we've reached the end of this frag, turn it into a variant
4440 frag and record the information for the instructions we've
4441 written so far. */
4442 if (frag_room () < 4)
4443 relax_close_frag ();
4444 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4445 }
4446
4447 if (mips_relax.sequence != 2)
4448 {
4449 if (mips_macro_warning.first_insn_sizes[0] == 0)
4450 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4451 mips_macro_warning.sizes[0] += insn_length (ip);
4452 mips_macro_warning.insns[0]++;
4453 }
4454 if (mips_relax.sequence != 1)
4455 {
4456 if (mips_macro_warning.first_insn_sizes[1] == 0)
4457 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4458 mips_macro_warning.sizes[1] += insn_length (ip);
4459 mips_macro_warning.insns[1]++;
4460 }
4461
4462 if (mips_opts.mips16)
4463 {
4464 ip->fixed_p = 1;
4465 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4466 }
4467 add_fixed_insn (ip);
4468 }
4469
4470 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
4471 {
4472 bfd_reloc_code_real_type final_type[3];
4473 reloc_howto_type *howto0;
4474 reloc_howto_type *howto;
4475 int i;
4476
4477 /* Perform any necessary conversion to microMIPS relocations
4478 and find out how many relocations there actually are. */
4479 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4480 final_type[i] = micromips_map_reloc (reloc_type[i]);
4481
4482 /* In a compound relocation, it is the final (outermost)
4483 operator that determines the relocated field. */
4484 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4485
4486 if (howto == NULL)
4487 {
4488 /* To reproduce this failure try assembling gas/testsuites/
4489 gas/mips/mips16-intermix.s with a mips-ecoff targeted
4490 assembler. */
4491 as_bad (_("Unsupported MIPS relocation number %d"),
4492 final_type[i - 1]);
4493 howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
4494 }
4495
4496 if (i > 1)
4497 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
4498 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4499 bfd_get_reloc_size (howto),
4500 address_expr,
4501 howto0 && howto0->pc_relative,
4502 final_type[0]);
4503
4504 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
4505 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
4506 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4507
4508 /* These relocations can have an addend that won't fit in
4509 4 octets for 64bit assembly. */
4510 if (HAVE_64BIT_GPRS
4511 && ! howto->partial_inplace
4512 && (reloc_type[0] == BFD_RELOC_16
4513 || reloc_type[0] == BFD_RELOC_32
4514 || reloc_type[0] == BFD_RELOC_MIPS_JMP
4515 || reloc_type[0] == BFD_RELOC_GPREL16
4516 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4517 || reloc_type[0] == BFD_RELOC_GPREL32
4518 || reloc_type[0] == BFD_RELOC_64
4519 || reloc_type[0] == BFD_RELOC_CTOR
4520 || reloc_type[0] == BFD_RELOC_MIPS_SUB
4521 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4522 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4523 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4524 || reloc_type[0] == BFD_RELOC_MIPS_REL16
4525 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4526 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4527 || hi16_reloc_p (reloc_type[0])
4528 || lo16_reloc_p (reloc_type[0])))
4529 ip->fixp[0]->fx_no_overflow = 1;
4530
4531 /* These relocations can have an addend that won't fit in 2 octets. */
4532 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
4533 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
4534 ip->fixp[0]->fx_no_overflow = 1;
4535
4536 if (mips_relax.sequence)
4537 {
4538 if (mips_relax.first_fixup == 0)
4539 mips_relax.first_fixup = ip->fixp[0];
4540 }
4541 else if (reloc_needs_lo_p (*reloc_type))
4542 {
4543 struct mips_hi_fixup *hi_fixup;
4544
4545 /* Reuse the last entry if it already has a matching %lo. */
4546 hi_fixup = mips_hi_fixup_list;
4547 if (hi_fixup == 0
4548 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4549 {
4550 hi_fixup = ((struct mips_hi_fixup *)
4551 xmalloc (sizeof (struct mips_hi_fixup)));
4552 hi_fixup->next = mips_hi_fixup_list;
4553 mips_hi_fixup_list = hi_fixup;
4554 }
4555 hi_fixup->fixp = ip->fixp[0];
4556 hi_fixup->seg = now_seg;
4557 }
4558
4559 /* Add fixups for the second and third relocations, if given.
4560 Note that the ABI allows the second relocation to be
4561 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
4562 moment we only use RSS_UNDEF, but we could add support
4563 for the others if it ever becomes necessary. */
4564 for (i = 1; i < 3; i++)
4565 if (reloc_type[i] != BFD_RELOC_UNUSED)
4566 {
4567 ip->fixp[i] = fix_new (ip->frag, ip->where,
4568 ip->fixp[0]->fx_size, NULL, 0,
4569 FALSE, final_type[i]);
4570
4571 /* Use fx_tcbit to mark compound relocs. */
4572 ip->fixp[0]->fx_tcbit = 1;
4573 ip->fixp[i]->fx_tcbit = 1;
4574 }
4575 }
4576 install_insn (ip);
4577
4578 /* Update the register mask information. */
4579 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4580 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
4581
4582 switch (method)
4583 {
4584 case APPEND_ADD:
4585 insert_into_history (0, 1, ip);
4586 break;
4587
4588 case APPEND_ADD_WITH_NOP:
4589 {
4590 struct mips_cl_insn *nop;
4591
4592 insert_into_history (0, 1, ip);
4593 nop = get_delay_slot_nop (ip);
4594 add_fixed_insn (nop);
4595 insert_into_history (0, 1, nop);
4596 if (mips_relax.sequence)
4597 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4598 }
4599 break;
4600
4601 case APPEND_ADD_COMPACT:
4602 /* Convert MIPS16 jr/jalr into a "compact" jump. */
4603 gas_assert (mips_opts.mips16);
4604 ip->insn_opcode |= 0x0080;
4605 find_altered_mips16_opcode (ip);
4606 install_insn (ip);
4607 insert_into_history (0, 1, ip);
4608 break;
4609
4610 case APPEND_SWAP:
4611 {
4612 struct mips_cl_insn delay = history[0];
4613 if (mips_opts.mips16)
4614 {
4615 know (delay.frag == ip->frag);
4616 move_insn (ip, delay.frag, delay.where);
4617 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4618 }
4619 else if (relaxed_branch || delay.frag != ip->frag)
4620 {
4621 /* Add the delay slot instruction to the end of the
4622 current frag and shrink the fixed part of the
4623 original frag. If the branch occupies the tail of
4624 the latter, move it backwards to cover the gap. */
4625 delay.frag->fr_fix -= branch_disp;
4626 if (delay.frag == ip->frag)
4627 move_insn (ip, ip->frag, ip->where - branch_disp);
4628 add_fixed_insn (&delay);
4629 }
4630 else
4631 {
4632 move_insn (&delay, ip->frag,
4633 ip->where - branch_disp + insn_length (ip));
4634 move_insn (ip, history[0].frag, history[0].where);
4635 }
4636 history[0] = *ip;
4637 delay.fixed_p = 1;
4638 insert_into_history (0, 1, &delay);
4639 }
4640 break;
4641 }
4642
4643 /* If we have just completed an unconditional branch, clear the history. */
4644 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4645 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
4646 {
4647 unsigned int i;
4648
4649 mips_no_prev_insn ();
4650
4651 for (i = 0; i < ARRAY_SIZE (history); i++)
4652 history[i].cleared_p = 1;
4653 }
4654
4655 /* We need to emit a label at the end of branch-likely macros. */
4656 if (emit_branch_likely_macro)
4657 {
4658 emit_branch_likely_macro = FALSE;
4659 micromips_add_label ();
4660 }
4661
4662 /* We just output an insn, so the next one doesn't have a label. */
4663 mips_clear_insn_labels ();
4664 }
4665
4666 /* Forget that there was any previous instruction or label.
4667 When BRANCH is true, the branch history is also flushed. */
4668
4669 static void
4670 mips_no_prev_insn (void)
4671 {
4672 prev_nop_frag = NULL;
4673 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
4674 mips_clear_insn_labels ();
4675 }
4676
4677 /* This function must be called before we emit something other than
4678 instructions. It is like mips_no_prev_insn except that it inserts
4679 any NOPS that might be needed by previous instructions. */
4680
4681 void
4682 mips_emit_delays (void)
4683 {
4684 if (! mips_opts.noreorder)
4685 {
4686 int nops = nops_for_insn (0, history, NULL);
4687 if (nops > 0)
4688 {
4689 while (nops-- > 0)
4690 add_fixed_insn (NOP_INSN);
4691 mips_move_text_labels ();
4692 }
4693 }
4694 mips_no_prev_insn ();
4695 }
4696
4697 /* Start a (possibly nested) noreorder block. */
4698
4699 static void
4700 start_noreorder (void)
4701 {
4702 if (mips_opts.noreorder == 0)
4703 {
4704 unsigned int i;
4705 int nops;
4706
4707 /* None of the instructions before the .set noreorder can be moved. */
4708 for (i = 0; i < ARRAY_SIZE (history); i++)
4709 history[i].fixed_p = 1;
4710
4711 /* Insert any nops that might be needed between the .set noreorder
4712 block and the previous instructions. We will later remove any
4713 nops that turn out not to be needed. */
4714 nops = nops_for_insn (0, history, NULL);
4715 if (nops > 0)
4716 {
4717 if (mips_optimize != 0)
4718 {
4719 /* Record the frag which holds the nop instructions, so
4720 that we can remove them if we don't need them. */
4721 frag_grow (nops * NOP_INSN_SIZE);
4722 prev_nop_frag = frag_now;
4723 prev_nop_frag_holds = nops;
4724 prev_nop_frag_required = 0;
4725 prev_nop_frag_since = 0;
4726 }
4727
4728 for (; nops > 0; --nops)
4729 add_fixed_insn (NOP_INSN);
4730
4731 /* Move on to a new frag, so that it is safe to simply
4732 decrease the size of prev_nop_frag. */
4733 frag_wane (frag_now);
4734 frag_new (0);
4735 mips_move_text_labels ();
4736 }
4737 mips_mark_labels ();
4738 mips_clear_insn_labels ();
4739 }
4740 mips_opts.noreorder++;
4741 mips_any_noreorder = 1;
4742 }
4743
4744 /* End a nested noreorder block. */
4745
4746 static void
4747 end_noreorder (void)
4748 {
4749 mips_opts.noreorder--;
4750 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
4751 {
4752 /* Commit to inserting prev_nop_frag_required nops and go back to
4753 handling nop insertion the .set reorder way. */
4754 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
4755 * NOP_INSN_SIZE);
4756 insert_into_history (prev_nop_frag_since,
4757 prev_nop_frag_required, NOP_INSN);
4758 prev_nop_frag = NULL;
4759 }
4760 }
4761
4762 /* Set up global variables for the start of a new macro. */
4763
4764 static void
4765 macro_start (void)
4766 {
4767 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
4768 memset (&mips_macro_warning.first_insn_sizes, 0,
4769 sizeof (mips_macro_warning.first_insn_sizes));
4770 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
4771 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
4772 && delayed_branch_p (&history[0]));
4773 switch (history[0].insn_mo->pinfo2
4774 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
4775 {
4776 case INSN2_BRANCH_DELAY_32BIT:
4777 mips_macro_warning.delay_slot_length = 4;
4778 break;
4779 case INSN2_BRANCH_DELAY_16BIT:
4780 mips_macro_warning.delay_slot_length = 2;
4781 break;
4782 default:
4783 mips_macro_warning.delay_slot_length = 0;
4784 break;
4785 }
4786 mips_macro_warning.first_frag = NULL;
4787 }
4788
4789 /* Given that a macro is longer than one instruction or of the wrong size,
4790 return the appropriate warning for it. Return null if no warning is
4791 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
4792 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
4793 and RELAX_NOMACRO. */
4794
4795 static const char *
4796 macro_warning (relax_substateT subtype)
4797 {
4798 if (subtype & RELAX_DELAY_SLOT)
4799 return _("Macro instruction expanded into multiple instructions"
4800 " in a branch delay slot");
4801 else if (subtype & RELAX_NOMACRO)
4802 return _("Macro instruction expanded into multiple instructions");
4803 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
4804 | RELAX_DELAY_SLOT_SIZE_SECOND))
4805 return ((subtype & RELAX_DELAY_SLOT_16BIT)
4806 ? _("Macro instruction expanded into a wrong size instruction"
4807 " in a 16-bit branch delay slot")
4808 : _("Macro instruction expanded into a wrong size instruction"
4809 " in a 32-bit branch delay slot"));
4810 else
4811 return 0;
4812 }
4813
4814 /* Finish up a macro. Emit warnings as appropriate. */
4815
4816 static void
4817 macro_end (void)
4818 {
4819 /* Relaxation warning flags. */
4820 relax_substateT subtype = 0;
4821
4822 /* Check delay slot size requirements. */
4823 if (mips_macro_warning.delay_slot_length == 2)
4824 subtype |= RELAX_DELAY_SLOT_16BIT;
4825 if (mips_macro_warning.delay_slot_length != 0)
4826 {
4827 if (mips_macro_warning.delay_slot_length
4828 != mips_macro_warning.first_insn_sizes[0])
4829 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
4830 if (mips_macro_warning.delay_slot_length
4831 != mips_macro_warning.first_insn_sizes[1])
4832 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
4833 }
4834
4835 /* Check instruction count requirements. */
4836 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
4837 {
4838 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
4839 subtype |= RELAX_SECOND_LONGER;
4840 if (mips_opts.warn_about_macros)
4841 subtype |= RELAX_NOMACRO;
4842 if (mips_macro_warning.delay_slot_p)
4843 subtype |= RELAX_DELAY_SLOT;
4844 }
4845
4846 /* If both alternatives fail to fill a delay slot correctly,
4847 emit the warning now. */
4848 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
4849 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
4850 {
4851 relax_substateT s;
4852 const char *msg;
4853
4854 s = subtype & (RELAX_DELAY_SLOT_16BIT
4855 | RELAX_DELAY_SLOT_SIZE_FIRST
4856 | RELAX_DELAY_SLOT_SIZE_SECOND);
4857 msg = macro_warning (s);
4858 if (msg != NULL)
4859 as_warn ("%s", msg);
4860 subtype &= ~s;
4861 }
4862
4863 /* If both implementations are longer than 1 instruction, then emit the
4864 warning now. */
4865 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
4866 {
4867 relax_substateT s;
4868 const char *msg;
4869
4870 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
4871 msg = macro_warning (s);
4872 if (msg != NULL)
4873 as_warn ("%s", msg);
4874 subtype &= ~s;
4875 }
4876
4877 /* If any flags still set, then one implementation might need a warning
4878 and the other either will need one of a different kind or none at all.
4879 Pass any remaining flags over to relaxation. */
4880 if (mips_macro_warning.first_frag != NULL)
4881 mips_macro_warning.first_frag->fr_subtype |= subtype;
4882 }
4883
4884 /* Instruction operand formats used in macros that vary between
4885 standard MIPS and microMIPS code. */
4886
4887 static const char * const brk_fmt[2] = { "c", "mF" };
4888 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
4889 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
4890 static const char * const lui_fmt[2] = { "t,u", "s,u" };
4891 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
4892 static const char * const mfhl_fmt[2] = { "d", "mj" };
4893 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
4894 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
4895
4896 #define BRK_FMT (brk_fmt[mips_opts.micromips])
4897 #define COP12_FMT (cop12_fmt[mips_opts.micromips])
4898 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
4899 #define LUI_FMT (lui_fmt[mips_opts.micromips])
4900 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
4901 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips])
4902 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
4903 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
4904
4905 /* Read a macro's relocation codes from *ARGS and store them in *R.
4906 The first argument in *ARGS will be either the code for a single
4907 relocation or -1 followed by the three codes that make up a
4908 composite relocation. */
4909
4910 static void
4911 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
4912 {
4913 int i, next;
4914
4915 next = va_arg (*args, int);
4916 if (next >= 0)
4917 r[0] = (bfd_reloc_code_real_type) next;
4918 else
4919 for (i = 0; i < 3; i++)
4920 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
4921 }
4922
4923 /* Build an instruction created by a macro expansion. This is passed
4924 a pointer to the count of instructions created so far, an
4925 expression, the name of the instruction to build, an operand format
4926 string, and corresponding arguments. */
4927
4928 static void
4929 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
4930 {
4931 const struct mips_opcode *mo = NULL;
4932 bfd_reloc_code_real_type r[3];
4933 const struct mips_opcode *amo;
4934 struct hash_control *hash;
4935 struct mips_cl_insn insn;
4936 va_list args;
4937
4938 va_start (args, fmt);
4939
4940 if (mips_opts.mips16)
4941 {
4942 mips16_macro_build (ep, name, fmt, &args);
4943 va_end (args);
4944 return;
4945 }
4946
4947 r[0] = BFD_RELOC_UNUSED;
4948 r[1] = BFD_RELOC_UNUSED;
4949 r[2] = BFD_RELOC_UNUSED;
4950 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
4951 amo = (struct mips_opcode *) hash_find (hash, name);
4952 gas_assert (amo);
4953 gas_assert (strcmp (name, amo->name) == 0);
4954
4955 do
4956 {
4957 /* Search until we get a match for NAME. It is assumed here that
4958 macros will never generate MDMX, MIPS-3D, or MT instructions.
4959 We try to match an instruction that fulfils the branch delay
4960 slot instruction length requirement (if any) of the previous
4961 instruction. While doing this we record the first instruction
4962 seen that matches all the other conditions and use it anyway
4963 if the requirement cannot be met; we will issue an appropriate
4964 warning later on. */
4965 if (strcmp (fmt, amo->args) == 0
4966 && amo->pinfo != INSN_MACRO
4967 && is_opcode_valid (amo)
4968 && is_size_valid (amo))
4969 {
4970 if (is_delay_slot_valid (amo))
4971 {
4972 mo = amo;
4973 break;
4974 }
4975 else if (!mo)
4976 mo = amo;
4977 }
4978
4979 ++amo;
4980 gas_assert (amo->name);
4981 }
4982 while (strcmp (name, amo->name) == 0);
4983
4984 gas_assert (mo);
4985 create_insn (&insn, mo);
4986 for (;;)
4987 {
4988 switch (*fmt++)
4989 {
4990 case '\0':
4991 break;
4992
4993 case ',':
4994 case '(':
4995 case ')':
4996 continue;
4997
4998 case '+':
4999 switch (*fmt++)
5000 {
5001 case 'A':
5002 case 'E':
5003 INSERT_OPERAND (mips_opts.micromips,
5004 EXTLSB, insn, va_arg (args, int));
5005 continue;
5006
5007 case 'B':
5008 case 'F':
5009 /* Note that in the macro case, these arguments are already
5010 in MSB form. (When handling the instruction in the
5011 non-macro case, these arguments are sizes from which
5012 MSB values must be calculated.) */
5013 INSERT_OPERAND (mips_opts.micromips,
5014 INSMSB, insn, va_arg (args, int));
5015 continue;
5016
5017 case 'C':
5018 case 'G':
5019 case 'H':
5020 /* Note that in the macro case, these arguments are already
5021 in MSBD form. (When handling the instruction in the
5022 non-macro case, these arguments are sizes from which
5023 MSBD values must be calculated.) */
5024 INSERT_OPERAND (mips_opts.micromips,
5025 EXTMSBD, insn, va_arg (args, int));
5026 continue;
5027
5028 case 'Q':
5029 gas_assert (!mips_opts.micromips);
5030 INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
5031 continue;
5032
5033 default:
5034 abort ();
5035 }
5036 continue;
5037
5038 case '2':
5039 INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
5040 continue;
5041
5042 case 'n':
5043 gas_assert (mips_opts.micromips);
5044 case 't':
5045 case 'w':
5046 case 'E':
5047 INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
5048 continue;
5049
5050 case 'c':
5051 gas_assert (!mips_opts.micromips);
5052 INSERT_OPERAND (0, CODE, insn, va_arg (args, int));
5053 continue;
5054
5055 case 'W':
5056 gas_assert (!mips_opts.micromips);
5057 case 'T':
5058 INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
5059 continue;
5060
5061 case 'G':
5062 if (mips_opts.micromips)
5063 INSERT_OPERAND (1, RS, insn, va_arg (args, int));
5064 else
5065 INSERT_OPERAND (0, RD, insn, va_arg (args, int));
5066 continue;
5067
5068 case 'K':
5069 gas_assert (!mips_opts.micromips);
5070 case 'd':
5071 INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
5072 continue;
5073
5074 case 'U':
5075 gas_assert (!mips_opts.micromips);
5076 {
5077 int tmp = va_arg (args, int);
5078
5079 INSERT_OPERAND (0, RT, insn, tmp);
5080 INSERT_OPERAND (0, RD, insn, tmp);
5081 }
5082 continue;
5083
5084 case 'V':
5085 case 'S':
5086 gas_assert (!mips_opts.micromips);
5087 INSERT_OPERAND (0, FS, insn, va_arg (args, int));
5088 continue;
5089
5090 case 'z':
5091 continue;
5092
5093 case '<':
5094 INSERT_OPERAND (mips_opts.micromips,
5095 SHAMT, insn, va_arg (args, int));
5096 continue;
5097
5098 case 'D':
5099 gas_assert (!mips_opts.micromips);
5100 INSERT_OPERAND (0, FD, insn, va_arg (args, int));
5101 continue;
5102
5103 case 'B':
5104 gas_assert (!mips_opts.micromips);
5105 INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
5106 continue;
5107
5108 case 'J':
5109 gas_assert (!mips_opts.micromips);
5110 INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
5111 continue;
5112
5113 case 'q':
5114 gas_assert (!mips_opts.micromips);
5115 INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
5116 continue;
5117
5118 case 'b':
5119 case 's':
5120 case 'r':
5121 case 'v':
5122 INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
5123 continue;
5124
5125 case 'i':
5126 case 'j':
5127 macro_read_relocs (&args, r);
5128 gas_assert (*r == BFD_RELOC_GPREL16
5129 || *r == BFD_RELOC_MIPS_HIGHER
5130 || *r == BFD_RELOC_HI16_S
5131 || *r == BFD_RELOC_LO16
5132 || *r == BFD_RELOC_MIPS_GOT_OFST);
5133 continue;
5134
5135 case 'o':
5136 macro_read_relocs (&args, r);
5137 continue;
5138
5139 case 'u':
5140 macro_read_relocs (&args, r);
5141 gas_assert (ep != NULL
5142 && (ep->X_op == O_constant
5143 || (ep->X_op == O_symbol
5144 && (*r == BFD_RELOC_MIPS_HIGHEST
5145 || *r == BFD_RELOC_HI16_S
5146 || *r == BFD_RELOC_HI16
5147 || *r == BFD_RELOC_GPREL16
5148 || *r == BFD_RELOC_MIPS_GOT_HI16
5149 || *r == BFD_RELOC_MIPS_CALL_HI16))));
5150 continue;
5151
5152 case 'p':
5153 gas_assert (ep != NULL);
5154
5155 /*
5156 * This allows macro() to pass an immediate expression for
5157 * creating short branches without creating a symbol.
5158 *
5159 * We don't allow branch relaxation for these branches, as
5160 * they should only appear in ".set nomacro" anyway.
5161 */
5162 if (ep->X_op == O_constant)
5163 {
5164 /* For microMIPS we always use relocations for branches.
5165 So we should not resolve immediate values. */
5166 gas_assert (!mips_opts.micromips);
5167
5168 if ((ep->X_add_number & 3) != 0)
5169 as_bad (_("branch to misaligned address (0x%lx)"),
5170 (unsigned long) ep->X_add_number);
5171 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5172 as_bad (_("branch address range overflow (0x%lx)"),
5173 (unsigned long) ep->X_add_number);
5174 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5175 ep = NULL;
5176 }
5177 else
5178 *r = BFD_RELOC_16_PCREL_S2;
5179 continue;
5180
5181 case 'a':
5182 gas_assert (ep != NULL);
5183 *r = BFD_RELOC_MIPS_JMP;
5184 continue;
5185
5186 case 'C':
5187 gas_assert (!mips_opts.micromips);
5188 INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
5189 continue;
5190
5191 case 'k':
5192 INSERT_OPERAND (mips_opts.micromips,
5193 CACHE, insn, va_arg (args, unsigned long));
5194 continue;
5195
5196 case '|':
5197 gas_assert (mips_opts.micromips);
5198 INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5199 continue;
5200
5201 case '.':
5202 gas_assert (mips_opts.micromips);
5203 INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5204 continue;
5205
5206 case '\\':
5207 INSERT_OPERAND (mips_opts.micromips,
5208 3BITPOS, insn, va_arg (args, unsigned int));
5209 continue;
5210
5211 case '~':
5212 INSERT_OPERAND (mips_opts.micromips,
5213 OFFSET12, insn, va_arg (args, unsigned long));
5214 continue;
5215
5216 case 'N':
5217 gas_assert (mips_opts.micromips);
5218 INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5219 continue;
5220
5221 case 'm': /* Opcode extension character. */
5222 gas_assert (mips_opts.micromips);
5223 switch (*fmt++)
5224 {
5225 case 'j':
5226 INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5227 break;
5228
5229 case 'p':
5230 INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5231 break;
5232
5233 case 'F':
5234 INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5235 break;
5236
5237 default:
5238 abort ();
5239 }
5240 continue;
5241
5242 default:
5243 abort ();
5244 }
5245 break;
5246 }
5247 va_end (args);
5248 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5249
5250 append_insn (&insn, ep, r, TRUE);
5251 }
5252
5253 static void
5254 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
5255 va_list *args)
5256 {
5257 struct mips_opcode *mo;
5258 struct mips_cl_insn insn;
5259 bfd_reloc_code_real_type r[3]
5260 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5261
5262 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
5263 gas_assert (mo);
5264 gas_assert (strcmp (name, mo->name) == 0);
5265
5266 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
5267 {
5268 ++mo;
5269 gas_assert (mo->name);
5270 gas_assert (strcmp (name, mo->name) == 0);
5271 }
5272
5273 create_insn (&insn, mo);
5274 for (;;)
5275 {
5276 int c;
5277
5278 c = *fmt++;
5279 switch (c)
5280 {
5281 case '\0':
5282 break;
5283
5284 case ',':
5285 case '(':
5286 case ')':
5287 continue;
5288
5289 case 'y':
5290 case 'w':
5291 MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
5292 continue;
5293
5294 case 'x':
5295 case 'v':
5296 MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
5297 continue;
5298
5299 case 'z':
5300 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
5301 continue;
5302
5303 case 'Z':
5304 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
5305 continue;
5306
5307 case '0':
5308 case 'S':
5309 case 'P':
5310 case 'R':
5311 continue;
5312
5313 case 'X':
5314 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
5315 continue;
5316
5317 case 'Y':
5318 {
5319 int regno;
5320
5321 regno = va_arg (*args, int);
5322 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
5323 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
5324 }
5325 continue;
5326
5327 case '<':
5328 case '>':
5329 case '4':
5330 case '5':
5331 case 'H':
5332 case 'W':
5333 case 'D':
5334 case 'j':
5335 case '8':
5336 case 'V':
5337 case 'C':
5338 case 'U':
5339 case 'k':
5340 case 'K':
5341 case 'p':
5342 case 'q':
5343 {
5344 offsetT value;
5345
5346 gas_assert (ep != NULL);
5347
5348 if (ep->X_op != O_constant)
5349 *r = (int) BFD_RELOC_UNUSED + c;
5350 else if (calculate_reloc (*r, ep->X_add_number, &value))
5351 {
5352 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
5353 ep = NULL;
5354 *r = BFD_RELOC_UNUSED;
5355 }
5356 }
5357 continue;
5358
5359 case '6':
5360 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
5361 continue;
5362 }
5363
5364 break;
5365 }
5366
5367 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5368
5369 append_insn (&insn, ep, r, TRUE);
5370 }
5371
5372 /*
5373 * Sign-extend 32-bit mode constants that have bit 31 set and all
5374 * higher bits unset.
5375 */
5376 static void
5377 normalize_constant_expr (expressionS *ex)
5378 {
5379 if (ex->X_op == O_constant
5380 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5381 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5382 - 0x80000000);
5383 }
5384
5385 /*
5386 * Sign-extend 32-bit mode address offsets that have bit 31 set and
5387 * all higher bits unset.
5388 */
5389 static void
5390 normalize_address_expr (expressionS *ex)
5391 {
5392 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5393 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5394 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5395 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5396 - 0x80000000);
5397 }
5398
5399 /*
5400 * Generate a "jalr" instruction with a relocation hint to the called
5401 * function. This occurs in NewABI PIC code.
5402 */
5403 static void
5404 macro_build_jalr (expressionS *ep, int cprestore)
5405 {
5406 static const bfd_reloc_code_real_type jalr_relocs[2]
5407 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5408 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5409 const char *jalr;
5410 char *f = NULL;
5411
5412 if (MIPS_JALR_HINT_P (ep))
5413 {
5414 frag_grow (8);
5415 f = frag_more (0);
5416 }
5417 if (mips_opts.micromips)
5418 {
5419 jalr = mips_opts.noreorder && !cprestore ? "jalr" : "jalrs";
5420 if (MIPS_JALR_HINT_P (ep)
5421 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
5422 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5423 else
5424 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5425 }
5426 else
5427 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
5428 if (MIPS_JALR_HINT_P (ep))
5429 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
5430 }
5431
5432 /*
5433 * Generate a "lui" instruction.
5434 */
5435 static void
5436 macro_build_lui (expressionS *ep, int regnum)
5437 {
5438 gas_assert (! mips_opts.mips16);
5439
5440 if (ep->X_op != O_constant)
5441 {
5442 gas_assert (ep->X_op == O_symbol);
5443 /* _gp_disp is a special case, used from s_cpload.
5444 __gnu_local_gp is used if mips_no_shared. */
5445 gas_assert (mips_pic == NO_PIC
5446 || (! HAVE_NEWABI
5447 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5448 || (! mips_in_shared
5449 && strcmp (S_GET_NAME (ep->X_add_symbol),
5450 "__gnu_local_gp") == 0));
5451 }
5452
5453 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
5454 }
5455
5456 /* Generate a sequence of instructions to do a load or store from a constant
5457 offset off of a base register (breg) into/from a target register (treg),
5458 using AT if necessary. */
5459 static void
5460 macro_build_ldst_constoffset (expressionS *ep, const char *op,
5461 int treg, int breg, int dbl)
5462 {
5463 gas_assert (ep->X_op == O_constant);
5464
5465 /* Sign-extending 32-bit constants makes their handling easier. */
5466 if (!dbl)
5467 normalize_constant_expr (ep);
5468
5469 /* Right now, this routine can only handle signed 32-bit constants. */
5470 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
5471 as_warn (_("operand overflow"));
5472
5473 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5474 {
5475 /* Signed 16-bit offset will fit in the op. Easy! */
5476 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
5477 }
5478 else
5479 {
5480 /* 32-bit offset, need multiple instructions and AT, like:
5481 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
5482 addu $tempreg,$tempreg,$breg
5483 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
5484 to handle the complete offset. */
5485 macro_build_lui (ep, AT);
5486 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5487 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
5488
5489 if (!mips_opts.at)
5490 as_bad (_("Macro used $at after \".set noat\""));
5491 }
5492 }
5493
5494 /* set_at()
5495 * Generates code to set the $at register to true (one)
5496 * if reg is less than the immediate expression.
5497 */
5498 static void
5499 set_at (int reg, int unsignedp)
5500 {
5501 if (imm_expr.X_op == O_constant
5502 && imm_expr.X_add_number >= -0x8000
5503 && imm_expr.X_add_number < 0x8000)
5504 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5505 AT, reg, BFD_RELOC_LO16);
5506 else
5507 {
5508 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5509 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
5510 }
5511 }
5512
5513 /* Warn if an expression is not a constant. */
5514
5515 static void
5516 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
5517 {
5518 if (ex->X_op == O_big)
5519 as_bad (_("unsupported large constant"));
5520 else if (ex->X_op != O_constant)
5521 as_bad (_("Instruction %s requires absolute expression"),
5522 ip->insn_mo->name);
5523
5524 if (HAVE_32BIT_GPRS)
5525 normalize_constant_expr (ex);
5526 }
5527
5528 /* Count the leading zeroes by performing a binary chop. This is a
5529 bulky bit of source, but performance is a LOT better for the
5530 majority of values than a simple loop to count the bits:
5531 for (lcnt = 0; (lcnt < 32); lcnt++)
5532 if ((v) & (1 << (31 - lcnt)))
5533 break;
5534 However it is not code size friendly, and the gain will drop a bit
5535 on certain cached systems.
5536 */
5537 #define COUNT_TOP_ZEROES(v) \
5538 (((v) & ~0xffff) == 0 \
5539 ? ((v) & ~0xff) == 0 \
5540 ? ((v) & ~0xf) == 0 \
5541 ? ((v) & ~0x3) == 0 \
5542 ? ((v) & ~0x1) == 0 \
5543 ? !(v) \
5544 ? 32 \
5545 : 31 \
5546 : 30 \
5547 : ((v) & ~0x7) == 0 \
5548 ? 29 \
5549 : 28 \
5550 : ((v) & ~0x3f) == 0 \
5551 ? ((v) & ~0x1f) == 0 \
5552 ? 27 \
5553 : 26 \
5554 : ((v) & ~0x7f) == 0 \
5555 ? 25 \
5556 : 24 \
5557 : ((v) & ~0xfff) == 0 \
5558 ? ((v) & ~0x3ff) == 0 \
5559 ? ((v) & ~0x1ff) == 0 \
5560 ? 23 \
5561 : 22 \
5562 : ((v) & ~0x7ff) == 0 \
5563 ? 21 \
5564 : 20 \
5565 : ((v) & ~0x3fff) == 0 \
5566 ? ((v) & ~0x1fff) == 0 \
5567 ? 19 \
5568 : 18 \
5569 : ((v) & ~0x7fff) == 0 \
5570 ? 17 \
5571 : 16 \
5572 : ((v) & ~0xffffff) == 0 \
5573 ? ((v) & ~0xfffff) == 0 \
5574 ? ((v) & ~0x3ffff) == 0 \
5575 ? ((v) & ~0x1ffff) == 0 \
5576 ? 15 \
5577 : 14 \
5578 : ((v) & ~0x7ffff) == 0 \
5579 ? 13 \
5580 : 12 \
5581 : ((v) & ~0x3fffff) == 0 \
5582 ? ((v) & ~0x1fffff) == 0 \
5583 ? 11 \
5584 : 10 \
5585 : ((v) & ~0x7fffff) == 0 \
5586 ? 9 \
5587 : 8 \
5588 : ((v) & ~0xfffffff) == 0 \
5589 ? ((v) & ~0x3ffffff) == 0 \
5590 ? ((v) & ~0x1ffffff) == 0 \
5591 ? 7 \
5592 : 6 \
5593 : ((v) & ~0x7ffffff) == 0 \
5594 ? 5 \
5595 : 4 \
5596 : ((v) & ~0x3fffffff) == 0 \
5597 ? ((v) & ~0x1fffffff) == 0 \
5598 ? 3 \
5599 : 2 \
5600 : ((v) & ~0x7fffffff) == 0 \
5601 ? 1 \
5602 : 0)
5603
5604 /* load_register()
5605 * This routine generates the least number of instructions necessary to load
5606 * an absolute expression value into a register.
5607 */
5608 static void
5609 load_register (int reg, expressionS *ep, int dbl)
5610 {
5611 int freg;
5612 expressionS hi32, lo32;
5613
5614 if (ep->X_op != O_big)
5615 {
5616 gas_assert (ep->X_op == O_constant);
5617
5618 /* Sign-extending 32-bit constants makes their handling easier. */
5619 if (!dbl)
5620 normalize_constant_expr (ep);
5621
5622 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
5623 {
5624 /* We can handle 16 bit signed values with an addiu to
5625 $zero. No need to ever use daddiu here, since $zero and
5626 the result are always correct in 32 bit mode. */
5627 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5628 return;
5629 }
5630 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5631 {
5632 /* We can handle 16 bit unsigned values with an ori to
5633 $zero. */
5634 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
5635 return;
5636 }
5637 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
5638 {
5639 /* 32 bit values require an lui. */
5640 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5641 if ((ep->X_add_number & 0xffff) != 0)
5642 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
5643 return;
5644 }
5645 }
5646
5647 /* The value is larger than 32 bits. */
5648
5649 if (!dbl || HAVE_32BIT_GPRS)
5650 {
5651 char value[32];
5652
5653 sprintf_vma (value, ep->X_add_number);
5654 as_bad (_("Number (0x%s) larger than 32 bits"), value);
5655 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5656 return;
5657 }
5658
5659 if (ep->X_op != O_big)
5660 {
5661 hi32 = *ep;
5662 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5663 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5664 hi32.X_add_number &= 0xffffffff;
5665 lo32 = *ep;
5666 lo32.X_add_number &= 0xffffffff;
5667 }
5668 else
5669 {
5670 gas_assert (ep->X_add_number > 2);
5671 if (ep->X_add_number == 3)
5672 generic_bignum[3] = 0;
5673 else if (ep->X_add_number > 4)
5674 as_bad (_("Number larger than 64 bits"));
5675 lo32.X_op = O_constant;
5676 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
5677 hi32.X_op = O_constant;
5678 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
5679 }
5680
5681 if (hi32.X_add_number == 0)
5682 freg = 0;
5683 else
5684 {
5685 int shift, bit;
5686 unsigned long hi, lo;
5687
5688 if (hi32.X_add_number == (offsetT) 0xffffffff)
5689 {
5690 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
5691 {
5692 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5693 return;
5694 }
5695 if (lo32.X_add_number & 0x80000000)
5696 {
5697 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5698 if (lo32.X_add_number & 0xffff)
5699 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
5700 return;
5701 }
5702 }
5703
5704 /* Check for 16bit shifted constant. We know that hi32 is
5705 non-zero, so start the mask on the first bit of the hi32
5706 value. */
5707 shift = 17;
5708 do
5709 {
5710 unsigned long himask, lomask;
5711
5712 if (shift < 32)
5713 {
5714 himask = 0xffff >> (32 - shift);
5715 lomask = (0xffff << shift) & 0xffffffff;
5716 }
5717 else
5718 {
5719 himask = 0xffff << (shift - 32);
5720 lomask = 0;
5721 }
5722 if ((hi32.X_add_number & ~(offsetT) himask) == 0
5723 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
5724 {
5725 expressionS tmp;
5726
5727 tmp.X_op = O_constant;
5728 if (shift < 32)
5729 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
5730 | (lo32.X_add_number >> shift));
5731 else
5732 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
5733 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
5734 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
5735 reg, reg, (shift >= 32) ? shift - 32 : shift);
5736 return;
5737 }
5738 ++shift;
5739 }
5740 while (shift <= (64 - 16));
5741
5742 /* Find the bit number of the lowest one bit, and store the
5743 shifted value in hi/lo. */
5744 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
5745 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
5746 if (lo != 0)
5747 {
5748 bit = 0;
5749 while ((lo & 1) == 0)
5750 {
5751 lo >>= 1;
5752 ++bit;
5753 }
5754 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
5755 hi >>= bit;
5756 }
5757 else
5758 {
5759 bit = 32;
5760 while ((hi & 1) == 0)
5761 {
5762 hi >>= 1;
5763 ++bit;
5764 }
5765 lo = hi;
5766 hi = 0;
5767 }
5768
5769 /* Optimize if the shifted value is a (power of 2) - 1. */
5770 if ((hi == 0 && ((lo + 1) & lo) == 0)
5771 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
5772 {
5773 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
5774 if (shift != 0)
5775 {
5776 expressionS tmp;
5777
5778 /* This instruction will set the register to be all
5779 ones. */
5780 tmp.X_op = O_constant;
5781 tmp.X_add_number = (offsetT) -1;
5782 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5783 if (bit != 0)
5784 {
5785 bit += shift;
5786 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
5787 reg, reg, (bit >= 32) ? bit - 32 : bit);
5788 }
5789 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
5790 reg, reg, (shift >= 32) ? shift - 32 : shift);
5791 return;
5792 }
5793 }
5794
5795 /* Sign extend hi32 before calling load_register, because we can
5796 generally get better code when we load a sign extended value. */
5797 if ((hi32.X_add_number & 0x80000000) != 0)
5798 hi32.X_add_number |= ~(offsetT) 0xffffffff;
5799 load_register (reg, &hi32, 0);
5800 freg = reg;
5801 }
5802 if ((lo32.X_add_number & 0xffff0000) == 0)
5803 {
5804 if (freg != 0)
5805 {
5806 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
5807 freg = reg;
5808 }
5809 }
5810 else
5811 {
5812 expressionS mid16;
5813
5814 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
5815 {
5816 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5817 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
5818 return;
5819 }
5820
5821 if (freg != 0)
5822 {
5823 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
5824 freg = reg;
5825 }
5826 mid16 = lo32;
5827 mid16.X_add_number >>= 16;
5828 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
5829 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5830 freg = reg;
5831 }
5832 if ((lo32.X_add_number & 0xffff) != 0)
5833 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
5834 }
5835
5836 static inline void
5837 load_delay_nop (void)
5838 {
5839 if (!gpr_interlocks)
5840 macro_build (NULL, "nop", "");
5841 }
5842
5843 /* Load an address into a register. */
5844
5845 static void
5846 load_address (int reg, expressionS *ep, int *used_at)
5847 {
5848 if (ep->X_op != O_constant
5849 && ep->X_op != O_symbol)
5850 {
5851 as_bad (_("expression too complex"));
5852 ep->X_op = O_constant;
5853 }
5854
5855 if (ep->X_op == O_constant)
5856 {
5857 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
5858 return;
5859 }
5860
5861 if (mips_pic == NO_PIC)
5862 {
5863 /* If this is a reference to a GP relative symbol, we want
5864 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
5865 Otherwise we want
5866 lui $reg,<sym> (BFD_RELOC_HI16_S)
5867 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
5868 If we have an addend, we always use the latter form.
5869
5870 With 64bit address space and a usable $at we want
5871 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5872 lui $at,<sym> (BFD_RELOC_HI16_S)
5873 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5874 daddiu $at,<sym> (BFD_RELOC_LO16)
5875 dsll32 $reg,0
5876 daddu $reg,$reg,$at
5877
5878 If $at is already in use, we use a path which is suboptimal
5879 on superscalar processors.
5880 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5881 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5882 dsll $reg,16
5883 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
5884 dsll $reg,16
5885 daddiu $reg,<sym> (BFD_RELOC_LO16)
5886
5887 For GP relative symbols in 64bit address space we can use
5888 the same sequence as in 32bit address space. */
5889 if (HAVE_64BIT_SYMBOLS)
5890 {
5891 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5892 && !nopic_need_relax (ep->X_add_symbol, 1))
5893 {
5894 relax_start (ep->X_add_symbol);
5895 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5896 mips_gp_register, BFD_RELOC_GPREL16);
5897 relax_switch ();
5898 }
5899
5900 if (*used_at == 0 && mips_opts.at)
5901 {
5902 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5903 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
5904 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5905 BFD_RELOC_MIPS_HIGHER);
5906 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
5907 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
5908 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
5909 *used_at = 1;
5910 }
5911 else
5912 {
5913 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5914 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5915 BFD_RELOC_MIPS_HIGHER);
5916 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5917 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
5918 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5919 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
5920 }
5921
5922 if (mips_relax.sequence)
5923 relax_end ();
5924 }
5925 else
5926 {
5927 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5928 && !nopic_need_relax (ep->X_add_symbol, 1))
5929 {
5930 relax_start (ep->X_add_symbol);
5931 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5932 mips_gp_register, BFD_RELOC_GPREL16);
5933 relax_switch ();
5934 }
5935 macro_build_lui (ep, reg);
5936 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
5937 reg, reg, BFD_RELOC_LO16);
5938 if (mips_relax.sequence)
5939 relax_end ();
5940 }
5941 }
5942 else if (!mips_big_got)
5943 {
5944 expressionS ex;
5945
5946 /* If this is a reference to an external symbol, we want
5947 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5948 Otherwise we want
5949 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5950 nop
5951 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
5952 If there is a constant, it must be added in after.
5953
5954 If we have NewABI, we want
5955 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5956 unless we're referencing a global symbol with a non-zero
5957 offset, in which case cst must be added separately. */
5958 if (HAVE_NEWABI)
5959 {
5960 if (ep->X_add_number)
5961 {
5962 ex.X_add_number = ep->X_add_number;
5963 ep->X_add_number = 0;
5964 relax_start (ep->X_add_symbol);
5965 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5966 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5967 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5968 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5969 ex.X_op = O_constant;
5970 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
5971 reg, reg, BFD_RELOC_LO16);
5972 ep->X_add_number = ex.X_add_number;
5973 relax_switch ();
5974 }
5975 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5976 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5977 if (mips_relax.sequence)
5978 relax_end ();
5979 }
5980 else
5981 {
5982 ex.X_add_number = ep->X_add_number;
5983 ep->X_add_number = 0;
5984 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5985 BFD_RELOC_MIPS_GOT16, mips_gp_register);
5986 load_delay_nop ();
5987 relax_start (ep->X_add_symbol);
5988 relax_switch ();
5989 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5990 BFD_RELOC_LO16);
5991 relax_end ();
5992
5993 if (ex.X_add_number != 0)
5994 {
5995 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5996 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5997 ex.X_op = O_constant;
5998 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
5999 reg, reg, BFD_RELOC_LO16);
6000 }
6001 }
6002 }
6003 else if (mips_big_got)
6004 {
6005 expressionS ex;
6006
6007 /* This is the large GOT case. If this is a reference to an
6008 external symbol, we want
6009 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6010 addu $reg,$reg,$gp
6011 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
6012
6013 Otherwise, for a reference to a local symbol in old ABI, we want
6014 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6015 nop
6016 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
6017 If there is a constant, it must be added in after.
6018
6019 In the NewABI, for local symbols, with or without offsets, we want:
6020 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6021 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
6022 */
6023 if (HAVE_NEWABI)
6024 {
6025 ex.X_add_number = ep->X_add_number;
6026 ep->X_add_number = 0;
6027 relax_start (ep->X_add_symbol);
6028 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
6029 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6030 reg, reg, mips_gp_register);
6031 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6032 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
6033 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6034 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6035 else if (ex.X_add_number)
6036 {
6037 ex.X_op = O_constant;
6038 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6039 BFD_RELOC_LO16);
6040 }
6041
6042 ep->X_add_number = ex.X_add_number;
6043 relax_switch ();
6044 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6045 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6046 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6047 BFD_RELOC_MIPS_GOT_OFST);
6048 relax_end ();
6049 }
6050 else
6051 {
6052 ex.X_add_number = ep->X_add_number;
6053 ep->X_add_number = 0;
6054 relax_start (ep->X_add_symbol);
6055 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
6056 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6057 reg, reg, mips_gp_register);
6058 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6059 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
6060 relax_switch ();
6061 if (reg_needs_delay (mips_gp_register))
6062 {
6063 /* We need a nop before loading from $gp. This special
6064 check is required because the lui which starts the main
6065 instruction stream does not refer to $gp, and so will not
6066 insert the nop which may be required. */
6067 macro_build (NULL, "nop", "");
6068 }
6069 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6070 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6071 load_delay_nop ();
6072 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6073 BFD_RELOC_LO16);
6074 relax_end ();
6075
6076 if (ex.X_add_number != 0)
6077 {
6078 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6079 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6080 ex.X_op = O_constant;
6081 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6082 BFD_RELOC_LO16);
6083 }
6084 }
6085 }
6086 else
6087 abort ();
6088
6089 if (!mips_opts.at && *used_at == 1)
6090 as_bad (_("Macro used $at after \".set noat\""));
6091 }
6092
6093 /* Move the contents of register SOURCE into register DEST. */
6094
6095 static void
6096 move_register (int dest, int source)
6097 {
6098 /* Prefer to use a 16-bit microMIPS instruction unless the previous
6099 instruction specifically requires a 32-bit one. */
6100 if (mips_opts.micromips
6101 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
6102 macro_build (NULL, "move", "mp,mj", dest, source);
6103 else
6104 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6105 dest, source, 0);
6106 }
6107
6108 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
6109 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6110 The two alternatives are:
6111
6112 Global symbol Local sybmol
6113 ------------- ------------
6114 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
6115 ... ...
6116 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6117
6118 load_got_offset emits the first instruction and add_got_offset
6119 emits the second for a 16-bit offset or add_got_offset_hilo emits
6120 a sequence to add a 32-bit offset using a scratch register. */
6121
6122 static void
6123 load_got_offset (int dest, expressionS *local)
6124 {
6125 expressionS global;
6126
6127 global = *local;
6128 global.X_add_number = 0;
6129
6130 relax_start (local->X_add_symbol);
6131 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6132 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6133 relax_switch ();
6134 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6135 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6136 relax_end ();
6137 }
6138
6139 static void
6140 add_got_offset (int dest, expressionS *local)
6141 {
6142 expressionS global;
6143
6144 global.X_op = O_constant;
6145 global.X_op_symbol = NULL;
6146 global.X_add_symbol = NULL;
6147 global.X_add_number = local->X_add_number;
6148
6149 relax_start (local->X_add_symbol);
6150 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
6151 dest, dest, BFD_RELOC_LO16);
6152 relax_switch ();
6153 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
6154 relax_end ();
6155 }
6156
6157 static void
6158 add_got_offset_hilo (int dest, expressionS *local, int tmp)
6159 {
6160 expressionS global;
6161 int hold_mips_optimize;
6162
6163 global.X_op = O_constant;
6164 global.X_op_symbol = NULL;
6165 global.X_add_symbol = NULL;
6166 global.X_add_number = local->X_add_number;
6167
6168 relax_start (local->X_add_symbol);
6169 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6170 relax_switch ();
6171 /* Set mips_optimize around the lui instruction to avoid
6172 inserting an unnecessary nop after the lw. */
6173 hold_mips_optimize = mips_optimize;
6174 mips_optimize = 2;
6175 macro_build_lui (&global, tmp);
6176 mips_optimize = hold_mips_optimize;
6177 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6178 relax_end ();
6179
6180 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6181 }
6182
6183 /* Emit a sequence of instructions to emulate a branch likely operation.
6184 BR is an ordinary branch corresponding to one to be emulated. BRNEG
6185 is its complementing branch with the original condition negated.
6186 CALL is set if the original branch specified the link operation.
6187 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6188
6189 Code like this is produced in the noreorder mode:
6190
6191 BRNEG <args>, 1f
6192 nop
6193 b <sym>
6194 delay slot (executed only if branch taken)
6195 1:
6196
6197 or, if CALL is set:
6198
6199 BRNEG <args>, 1f
6200 nop
6201 bal <sym>
6202 delay slot (executed only if branch taken)
6203 1:
6204
6205 In the reorder mode the delay slot would be filled with a nop anyway,
6206 so code produced is simply:
6207
6208 BR <args>, <sym>
6209 nop
6210
6211 This function is used when producing code for the microMIPS ASE that
6212 does not implement branch likely instructions in hardware. */
6213
6214 static void
6215 macro_build_branch_likely (const char *br, const char *brneg,
6216 int call, expressionS *ep, const char *fmt,
6217 unsigned int sreg, unsigned int treg)
6218 {
6219 int noreorder = mips_opts.noreorder;
6220 expressionS expr1;
6221
6222 gas_assert (mips_opts.micromips);
6223 start_noreorder ();
6224 if (noreorder)
6225 {
6226 micromips_label_expr (&expr1);
6227 macro_build (&expr1, brneg, fmt, sreg, treg);
6228 macro_build (NULL, "nop", "");
6229 macro_build (ep, call ? "bal" : "b", "p");
6230
6231 /* Set to true so that append_insn adds a label. */
6232 emit_branch_likely_macro = TRUE;
6233 }
6234 else
6235 {
6236 macro_build (ep, br, fmt, sreg, treg);
6237 macro_build (NULL, "nop", "");
6238 }
6239 end_noreorder ();
6240 }
6241
6242 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6243 the condition code tested. EP specifies the branch target. */
6244
6245 static void
6246 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6247 {
6248 const int call = 0;
6249 const char *brneg;
6250 const char *br;
6251
6252 switch (type)
6253 {
6254 case M_BC1FL:
6255 br = "bc1f";
6256 brneg = "bc1t";
6257 break;
6258 case M_BC1TL:
6259 br = "bc1t";
6260 brneg = "bc1f";
6261 break;
6262 case M_BC2FL:
6263 br = "bc2f";
6264 brneg = "bc2t";
6265 break;
6266 case M_BC2TL:
6267 br = "bc2t";
6268 brneg = "bc2f";
6269 break;
6270 default:
6271 abort ();
6272 }
6273 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6274 }
6275
6276 /* Emit a two-argument branch macro specified by TYPE, using SREG as
6277 the register tested. EP specifies the branch target. */
6278
6279 static void
6280 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6281 {
6282 const char *brneg = NULL;
6283 const char *br;
6284 int call = 0;
6285
6286 switch (type)
6287 {
6288 case M_BGEZ:
6289 br = "bgez";
6290 break;
6291 case M_BGEZL:
6292 br = mips_opts.micromips ? "bgez" : "bgezl";
6293 brneg = "bltz";
6294 break;
6295 case M_BGEZALL:
6296 gas_assert (mips_opts.micromips);
6297 br = "bgezals";
6298 brneg = "bltz";
6299 call = 1;
6300 break;
6301 case M_BGTZ:
6302 br = "bgtz";
6303 break;
6304 case M_BGTZL:
6305 br = mips_opts.micromips ? "bgtz" : "bgtzl";
6306 brneg = "blez";
6307 break;
6308 case M_BLEZ:
6309 br = "blez";
6310 break;
6311 case M_BLEZL:
6312 br = mips_opts.micromips ? "blez" : "blezl";
6313 brneg = "bgtz";
6314 break;
6315 case M_BLTZ:
6316 br = "bltz";
6317 break;
6318 case M_BLTZL:
6319 br = mips_opts.micromips ? "bltz" : "bltzl";
6320 brneg = "bgez";
6321 break;
6322 case M_BLTZALL:
6323 gas_assert (mips_opts.micromips);
6324 br = "bltzals";
6325 brneg = "bgez";
6326 call = 1;
6327 break;
6328 default:
6329 abort ();
6330 }
6331 if (mips_opts.micromips && brneg)
6332 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6333 else
6334 macro_build (ep, br, "s,p", sreg);
6335 }
6336
6337 /* Emit a three-argument branch macro specified by TYPE, using SREG and
6338 TREG as the registers tested. EP specifies the branch target. */
6339
6340 static void
6341 macro_build_branch_rsrt (int type, expressionS *ep,
6342 unsigned int sreg, unsigned int treg)
6343 {
6344 const char *brneg = NULL;
6345 const int call = 0;
6346 const char *br;
6347
6348 switch (type)
6349 {
6350 case M_BEQ:
6351 case M_BEQ_I:
6352 br = "beq";
6353 break;
6354 case M_BEQL:
6355 case M_BEQL_I:
6356 br = mips_opts.micromips ? "beq" : "beql";
6357 brneg = "bne";
6358 break;
6359 case M_BNE:
6360 case M_BNE_I:
6361 br = "bne";
6362 break;
6363 case M_BNEL:
6364 case M_BNEL_I:
6365 br = mips_opts.micromips ? "bne" : "bnel";
6366 brneg = "beq";
6367 break;
6368 default:
6369 abort ();
6370 }
6371 if (mips_opts.micromips && brneg)
6372 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6373 else
6374 macro_build (ep, br, "s,t,p", sreg, treg);
6375 }
6376
6377 /*
6378 * Build macros
6379 * This routine implements the seemingly endless macro or synthesized
6380 * instructions and addressing modes in the mips assembly language. Many
6381 * of these macros are simple and are similar to each other. These could
6382 * probably be handled by some kind of table or grammar approach instead of
6383 * this verbose method. Others are not simple macros but are more like
6384 * optimizing code generation.
6385 * One interesting optimization is when several store macros appear
6386 * consecutively that would load AT with the upper half of the same address.
6387 * The ensuing load upper instructions are ommited. This implies some kind
6388 * of global optimization. We currently only optimize within a single macro.
6389 * For many of the load and store macros if the address is specified as a
6390 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6391 * first load register 'at' with zero and use it as the base register. The
6392 * mips assembler simply uses register $zero. Just one tiny optimization
6393 * we're missing.
6394 */
6395 static void
6396 macro (struct mips_cl_insn *ip)
6397 {
6398 unsigned int treg, sreg, dreg, breg;
6399 unsigned int tempreg;
6400 int mask;
6401 int used_at = 0;
6402 expressionS label_expr;
6403 expressionS expr1;
6404 expressionS *ep;
6405 const char *s;
6406 const char *s2;
6407 const char *fmt;
6408 int likely = 0;
6409 int coproc = 0;
6410 int off12 = 0;
6411 int call = 0;
6412 int jals = 0;
6413 int dbl = 0;
6414 int imm = 0;
6415 int ust = 0;
6416 int lp = 0;
6417 int ab = 0;
6418 int off0 = 0;
6419 int off;
6420 offsetT maxnum;
6421 bfd_reloc_code_real_type r;
6422 int hold_mips_optimize;
6423
6424 gas_assert (! mips_opts.mips16);
6425
6426 treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6427 dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6428 sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
6429 mask = ip->insn_mo->mask;
6430
6431 label_expr.X_op = O_constant;
6432 label_expr.X_op_symbol = NULL;
6433 label_expr.X_add_symbol = NULL;
6434 label_expr.X_add_number = 0;
6435
6436 expr1.X_op = O_constant;
6437 expr1.X_op_symbol = NULL;
6438 expr1.X_add_symbol = NULL;
6439 expr1.X_add_number = 1;
6440
6441 switch (mask)
6442 {
6443 case M_DABS:
6444 dbl = 1;
6445 case M_ABS:
6446 /* bgez $a0,1f
6447 move v0,$a0
6448 sub v0,$zero,$a0
6449 1:
6450 */
6451
6452 start_noreorder ();
6453
6454 if (mips_opts.micromips)
6455 micromips_label_expr (&label_expr);
6456 else
6457 label_expr.X_add_number = 8;
6458 macro_build (&label_expr, "bgez", "s,p", sreg);
6459 if (dreg == sreg)
6460 macro_build (NULL, "nop", "");
6461 else
6462 move_register (dreg, sreg);
6463 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
6464 if (mips_opts.micromips)
6465 micromips_add_label ();
6466
6467 end_noreorder ();
6468 break;
6469
6470 case M_ADD_I:
6471 s = "addi";
6472 s2 = "add";
6473 goto do_addi;
6474 case M_ADDU_I:
6475 s = "addiu";
6476 s2 = "addu";
6477 goto do_addi;
6478 case M_DADD_I:
6479 dbl = 1;
6480 s = "daddi";
6481 s2 = "dadd";
6482 if (!mips_opts.micromips)
6483 goto do_addi;
6484 if (imm_expr.X_op == O_constant
6485 && imm_expr.X_add_number >= -0x200
6486 && imm_expr.X_add_number < 0x200)
6487 {
6488 macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6489 break;
6490 }
6491 goto do_addi_i;
6492 case M_DADDU_I:
6493 dbl = 1;
6494 s = "daddiu";
6495 s2 = "daddu";
6496 do_addi:
6497 if (imm_expr.X_op == O_constant
6498 && imm_expr.X_add_number >= -0x8000
6499 && imm_expr.X_add_number < 0x8000)
6500 {
6501 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
6502 break;
6503 }
6504 do_addi_i:
6505 used_at = 1;
6506 load_register (AT, &imm_expr, dbl);
6507 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6508 break;
6509
6510 case M_AND_I:
6511 s = "andi";
6512 s2 = "and";
6513 goto do_bit;
6514 case M_OR_I:
6515 s = "ori";
6516 s2 = "or";
6517 goto do_bit;
6518 case M_NOR_I:
6519 s = "";
6520 s2 = "nor";
6521 goto do_bit;
6522 case M_XOR_I:
6523 s = "xori";
6524 s2 = "xor";
6525 do_bit:
6526 if (imm_expr.X_op == O_constant
6527 && imm_expr.X_add_number >= 0
6528 && imm_expr.X_add_number < 0x10000)
6529 {
6530 if (mask != M_NOR_I)
6531 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
6532 else
6533 {
6534 macro_build (&imm_expr, "ori", "t,r,i",
6535 treg, sreg, BFD_RELOC_LO16);
6536 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
6537 }
6538 break;
6539 }
6540
6541 used_at = 1;
6542 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6543 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6544 break;
6545
6546 case M_BALIGN:
6547 switch (imm_expr.X_add_number)
6548 {
6549 case 0:
6550 macro_build (NULL, "nop", "");
6551 break;
6552 case 2:
6553 macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6554 break;
6555 case 1:
6556 case 3:
6557 macro_build (NULL, "balign", "t,s,2", treg, sreg,
6558 (int) imm_expr.X_add_number);
6559 break;
6560 default:
6561 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6562 (unsigned long) imm_expr.X_add_number);
6563 break;
6564 }
6565 break;
6566
6567 case M_BC1FL:
6568 case M_BC1TL:
6569 case M_BC2FL:
6570 case M_BC2TL:
6571 gas_assert (mips_opts.micromips);
6572 macro_build_branch_ccl (mask, &offset_expr,
6573 EXTRACT_OPERAND (1, BCC, *ip));
6574 break;
6575
6576 case M_BEQ_I:
6577 case M_BEQL_I:
6578 case M_BNE_I:
6579 case M_BNEL_I:
6580 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6581 treg = 0;
6582 else
6583 {
6584 treg = AT;
6585 used_at = 1;
6586 load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
6587 }
6588 /* Fall through. */
6589 case M_BEQL:
6590 case M_BNEL:
6591 macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
6592 break;
6593
6594 case M_BGEL:
6595 likely = 1;
6596 case M_BGE:
6597 if (treg == 0)
6598 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6599 else if (sreg == 0)
6600 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6601 else
6602 {
6603 used_at = 1;
6604 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6605 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6606 &offset_expr, AT, ZERO);
6607 }
6608 break;
6609
6610 case M_BGEZL:
6611 case M_BGEZALL:
6612 case M_BGTZL:
6613 case M_BLEZL:
6614 case M_BLTZL:
6615 case M_BLTZALL:
6616 macro_build_branch_rs (mask, &offset_expr, sreg);
6617 break;
6618
6619 case M_BGTL_I:
6620 likely = 1;
6621 case M_BGT_I:
6622 /* Check for > max integer. */
6623 maxnum = 0x7fffffff;
6624 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6625 {
6626 maxnum <<= 16;
6627 maxnum |= 0xffff;
6628 maxnum <<= 16;
6629 maxnum |= 0xffff;
6630 }
6631 if (imm_expr.X_op == O_constant
6632 && imm_expr.X_add_number >= maxnum
6633 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6634 {
6635 do_false:
6636 /* Result is always false. */
6637 if (! likely)
6638 macro_build (NULL, "nop", "");
6639 else
6640 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
6641 break;
6642 }
6643 if (imm_expr.X_op != O_constant)
6644 as_bad (_("Unsupported large constant"));
6645 ++imm_expr.X_add_number;
6646 /* FALLTHROUGH */
6647 case M_BGE_I:
6648 case M_BGEL_I:
6649 if (mask == M_BGEL_I)
6650 likely = 1;
6651 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6652 {
6653 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
6654 &offset_expr, sreg);
6655 break;
6656 }
6657 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6658 {
6659 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
6660 &offset_expr, sreg);
6661 break;
6662 }
6663 maxnum = 0x7fffffff;
6664 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6665 {
6666 maxnum <<= 16;
6667 maxnum |= 0xffff;
6668 maxnum <<= 16;
6669 maxnum |= 0xffff;
6670 }
6671 maxnum = - maxnum - 1;
6672 if (imm_expr.X_op == O_constant
6673 && imm_expr.X_add_number <= maxnum
6674 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6675 {
6676 do_true:
6677 /* result is always true */
6678 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
6679 macro_build (&offset_expr, "b", "p");
6680 break;
6681 }
6682 used_at = 1;
6683 set_at (sreg, 0);
6684 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6685 &offset_expr, AT, ZERO);
6686 break;
6687
6688 case M_BGEUL:
6689 likely = 1;
6690 case M_BGEU:
6691 if (treg == 0)
6692 goto do_true;
6693 else if (sreg == 0)
6694 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6695 &offset_expr, ZERO, treg);
6696 else
6697 {
6698 used_at = 1;
6699 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6700 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6701 &offset_expr, AT, ZERO);
6702 }
6703 break;
6704
6705 case M_BGTUL_I:
6706 likely = 1;
6707 case M_BGTU_I:
6708 if (sreg == 0
6709 || (HAVE_32BIT_GPRS
6710 && imm_expr.X_op == O_constant
6711 && imm_expr.X_add_number == -1))
6712 goto do_false;
6713 if (imm_expr.X_op != O_constant)
6714 as_bad (_("Unsupported large constant"));
6715 ++imm_expr.X_add_number;
6716 /* FALLTHROUGH */
6717 case M_BGEU_I:
6718 case M_BGEUL_I:
6719 if (mask == M_BGEUL_I)
6720 likely = 1;
6721 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6722 goto do_true;
6723 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6724 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6725 &offset_expr, sreg, ZERO);
6726 else
6727 {
6728 used_at = 1;
6729 set_at (sreg, 1);
6730 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6731 &offset_expr, AT, ZERO);
6732 }
6733 break;
6734
6735 case M_BGTL:
6736 likely = 1;
6737 case M_BGT:
6738 if (treg == 0)
6739 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
6740 else if (sreg == 0)
6741 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
6742 else
6743 {
6744 used_at = 1;
6745 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6746 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6747 &offset_expr, AT, ZERO);
6748 }
6749 break;
6750
6751 case M_BGTUL:
6752 likely = 1;
6753 case M_BGTU:
6754 if (treg == 0)
6755 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6756 &offset_expr, sreg, ZERO);
6757 else if (sreg == 0)
6758 goto do_false;
6759 else
6760 {
6761 used_at = 1;
6762 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6763 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6764 &offset_expr, AT, ZERO);
6765 }
6766 break;
6767
6768 case M_BLEL:
6769 likely = 1;
6770 case M_BLE:
6771 if (treg == 0)
6772 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6773 else if (sreg == 0)
6774 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
6775 else
6776 {
6777 used_at = 1;
6778 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6779 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6780 &offset_expr, AT, ZERO);
6781 }
6782 break;
6783
6784 case M_BLEL_I:
6785 likely = 1;
6786 case M_BLE_I:
6787 maxnum = 0x7fffffff;
6788 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6789 {
6790 maxnum <<= 16;
6791 maxnum |= 0xffff;
6792 maxnum <<= 16;
6793 maxnum |= 0xffff;
6794 }
6795 if (imm_expr.X_op == O_constant
6796 && imm_expr.X_add_number >= maxnum
6797 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6798 goto do_true;
6799 if (imm_expr.X_op != O_constant)
6800 as_bad (_("Unsupported large constant"));
6801 ++imm_expr.X_add_number;
6802 /* FALLTHROUGH */
6803 case M_BLT_I:
6804 case M_BLTL_I:
6805 if (mask == M_BLTL_I)
6806 likely = 1;
6807 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6808 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6809 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6810 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6811 else
6812 {
6813 used_at = 1;
6814 set_at (sreg, 0);
6815 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6816 &offset_expr, AT, ZERO);
6817 }
6818 break;
6819
6820 case M_BLEUL:
6821 likely = 1;
6822 case M_BLEU:
6823 if (treg == 0)
6824 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6825 &offset_expr, sreg, ZERO);
6826 else if (sreg == 0)
6827 goto do_true;
6828 else
6829 {
6830 used_at = 1;
6831 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6832 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6833 &offset_expr, AT, ZERO);
6834 }
6835 break;
6836
6837 case M_BLEUL_I:
6838 likely = 1;
6839 case M_BLEU_I:
6840 if (sreg == 0
6841 || (HAVE_32BIT_GPRS
6842 && imm_expr.X_op == O_constant
6843 && imm_expr.X_add_number == -1))
6844 goto do_true;
6845 if (imm_expr.X_op != O_constant)
6846 as_bad (_("Unsupported large constant"));
6847 ++imm_expr.X_add_number;
6848 /* FALLTHROUGH */
6849 case M_BLTU_I:
6850 case M_BLTUL_I:
6851 if (mask == M_BLTUL_I)
6852 likely = 1;
6853 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6854 goto do_false;
6855 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6856 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6857 &offset_expr, sreg, ZERO);
6858 else
6859 {
6860 used_at = 1;
6861 set_at (sreg, 1);
6862 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6863 &offset_expr, AT, ZERO);
6864 }
6865 break;
6866
6867 case M_BLTL:
6868 likely = 1;
6869 case M_BLT:
6870 if (treg == 0)
6871 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6872 else if (sreg == 0)
6873 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
6874 else
6875 {
6876 used_at = 1;
6877 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6878 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6879 &offset_expr, AT, ZERO);
6880 }
6881 break;
6882
6883 case M_BLTUL:
6884 likely = 1;
6885 case M_BLTU:
6886 if (treg == 0)
6887 goto do_false;
6888 else if (sreg == 0)
6889 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6890 &offset_expr, ZERO, treg);
6891 else
6892 {
6893 used_at = 1;
6894 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6895 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6896 &offset_expr, AT, ZERO);
6897 }
6898 break;
6899
6900 case M_DEXT:
6901 {
6902 /* Use unsigned arithmetic. */
6903 addressT pos;
6904 addressT size;
6905
6906 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
6907 {
6908 as_bad (_("Unsupported large constant"));
6909 pos = size = 1;
6910 }
6911 else
6912 {
6913 pos = imm_expr.X_add_number;
6914 size = imm2_expr.X_add_number;
6915 }
6916
6917 if (pos > 63)
6918 {
6919 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
6920 pos = 1;
6921 }
6922 if (size == 0 || size > 64 || (pos + size - 1) > 63)
6923 {
6924 as_bad (_("Improper extract size (%lu, position %lu)"),
6925 (unsigned long) size, (unsigned long) pos);
6926 size = 1;
6927 }
6928
6929 if (size <= 32 && pos < 32)
6930 {
6931 s = "dext";
6932 fmt = "t,r,+A,+C";
6933 }
6934 else if (size <= 32)
6935 {
6936 s = "dextu";
6937 fmt = "t,r,+E,+H";
6938 }
6939 else
6940 {
6941 s = "dextm";
6942 fmt = "t,r,+A,+G";
6943 }
6944 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6945 (int) (size - 1));
6946 }
6947 break;
6948
6949 case M_DINS:
6950 {
6951 /* Use unsigned arithmetic. */
6952 addressT pos;
6953 addressT size;
6954
6955 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
6956 {
6957 as_bad (_("Unsupported large constant"));
6958 pos = size = 1;
6959 }
6960 else
6961 {
6962 pos = imm_expr.X_add_number;
6963 size = imm2_expr.X_add_number;
6964 }
6965
6966 if (pos > 63)
6967 {
6968 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
6969 pos = 1;
6970 }
6971 if (size == 0 || size > 64 || (pos + size - 1) > 63)
6972 {
6973 as_bad (_("Improper insert size (%lu, position %lu)"),
6974 (unsigned long) size, (unsigned long) pos);
6975 size = 1;
6976 }
6977
6978 if (pos < 32 && (pos + size - 1) < 32)
6979 {
6980 s = "dins";
6981 fmt = "t,r,+A,+B";
6982 }
6983 else if (pos >= 32)
6984 {
6985 s = "dinsu";
6986 fmt = "t,r,+E,+F";
6987 }
6988 else
6989 {
6990 s = "dinsm";
6991 fmt = "t,r,+A,+F";
6992 }
6993 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6994 (int) (pos + size - 1));
6995 }
6996 break;
6997
6998 case M_DDIV_3:
6999 dbl = 1;
7000 case M_DIV_3:
7001 s = "mflo";
7002 goto do_div3;
7003 case M_DREM_3:
7004 dbl = 1;
7005 case M_REM_3:
7006 s = "mfhi";
7007 do_div3:
7008 if (treg == 0)
7009 {
7010 as_warn (_("Divide by zero."));
7011 if (mips_trap)
7012 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
7013 else
7014 macro_build (NULL, "break", BRK_FMT, 7);
7015 break;
7016 }
7017
7018 start_noreorder ();
7019 if (mips_trap)
7020 {
7021 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7022 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
7023 }
7024 else
7025 {
7026 if (mips_opts.micromips)
7027 micromips_label_expr (&label_expr);
7028 else
7029 label_expr.X_add_number = 8;
7030 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7031 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
7032 macro_build (NULL, "break", BRK_FMT, 7);
7033 if (mips_opts.micromips)
7034 micromips_add_label ();
7035 }
7036 expr1.X_add_number = -1;
7037 used_at = 1;
7038 load_register (AT, &expr1, dbl);
7039 if (mips_opts.micromips)
7040 micromips_label_expr (&label_expr);
7041 else
7042 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
7043 macro_build (&label_expr, "bne", "s,t,p", treg, AT);
7044 if (dbl)
7045 {
7046 expr1.X_add_number = 1;
7047 load_register (AT, &expr1, dbl);
7048 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
7049 }
7050 else
7051 {
7052 expr1.X_add_number = 0x80000000;
7053 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
7054 }
7055 if (mips_trap)
7056 {
7057 macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
7058 /* We want to close the noreorder block as soon as possible, so
7059 that later insns are available for delay slot filling. */
7060 end_noreorder ();
7061 }
7062 else
7063 {
7064 if (mips_opts.micromips)
7065 micromips_label_expr (&label_expr);
7066 else
7067 label_expr.X_add_number = 8;
7068 macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
7069 macro_build (NULL, "nop", "");
7070
7071 /* We want to close the noreorder block as soon as possible, so
7072 that later insns are available for delay slot filling. */
7073 end_noreorder ();
7074
7075 macro_build (NULL, "break", BRK_FMT, 6);
7076 }
7077 if (mips_opts.micromips)
7078 micromips_add_label ();
7079 macro_build (NULL, s, MFHL_FMT, dreg);
7080 break;
7081
7082 case M_DIV_3I:
7083 s = "div";
7084 s2 = "mflo";
7085 goto do_divi;
7086 case M_DIVU_3I:
7087 s = "divu";
7088 s2 = "mflo";
7089 goto do_divi;
7090 case M_REM_3I:
7091 s = "div";
7092 s2 = "mfhi";
7093 goto do_divi;
7094 case M_REMU_3I:
7095 s = "divu";
7096 s2 = "mfhi";
7097 goto do_divi;
7098 case M_DDIV_3I:
7099 dbl = 1;
7100 s = "ddiv";
7101 s2 = "mflo";
7102 goto do_divi;
7103 case M_DDIVU_3I:
7104 dbl = 1;
7105 s = "ddivu";
7106 s2 = "mflo";
7107 goto do_divi;
7108 case M_DREM_3I:
7109 dbl = 1;
7110 s = "ddiv";
7111 s2 = "mfhi";
7112 goto do_divi;
7113 case M_DREMU_3I:
7114 dbl = 1;
7115 s = "ddivu";
7116 s2 = "mfhi";
7117 do_divi:
7118 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7119 {
7120 as_warn (_("Divide by zero."));
7121 if (mips_trap)
7122 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
7123 else
7124 macro_build (NULL, "break", BRK_FMT, 7);
7125 break;
7126 }
7127 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7128 {
7129 if (strcmp (s2, "mflo") == 0)
7130 move_register (dreg, sreg);
7131 else
7132 move_register (dreg, ZERO);
7133 break;
7134 }
7135 if (imm_expr.X_op == O_constant
7136 && imm_expr.X_add_number == -1
7137 && s[strlen (s) - 1] != 'u')
7138 {
7139 if (strcmp (s2, "mflo") == 0)
7140 {
7141 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
7142 }
7143 else
7144 move_register (dreg, ZERO);
7145 break;
7146 }
7147
7148 used_at = 1;
7149 load_register (AT, &imm_expr, dbl);
7150 macro_build (NULL, s, "z,s,t", sreg, AT);
7151 macro_build (NULL, s2, MFHL_FMT, dreg);
7152 break;
7153
7154 case M_DIVU_3:
7155 s = "divu";
7156 s2 = "mflo";
7157 goto do_divu3;
7158 case M_REMU_3:
7159 s = "divu";
7160 s2 = "mfhi";
7161 goto do_divu3;
7162 case M_DDIVU_3:
7163 s = "ddivu";
7164 s2 = "mflo";
7165 goto do_divu3;
7166 case M_DREMU_3:
7167 s = "ddivu";
7168 s2 = "mfhi";
7169 do_divu3:
7170 start_noreorder ();
7171 if (mips_trap)
7172 {
7173 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7174 macro_build (NULL, s, "z,s,t", sreg, treg);
7175 /* We want to close the noreorder block as soon as possible, so
7176 that later insns are available for delay slot filling. */
7177 end_noreorder ();
7178 }
7179 else
7180 {
7181 if (mips_opts.micromips)
7182 micromips_label_expr (&label_expr);
7183 else
7184 label_expr.X_add_number = 8;
7185 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7186 macro_build (NULL, s, "z,s,t", sreg, treg);
7187
7188 /* We want to close the noreorder block as soon as possible, so
7189 that later insns are available for delay slot filling. */
7190 end_noreorder ();
7191 macro_build (NULL, "break", BRK_FMT, 7);
7192 if (mips_opts.micromips)
7193 micromips_add_label ();
7194 }
7195 macro_build (NULL, s2, MFHL_FMT, dreg);
7196 break;
7197
7198 case M_DLCA_AB:
7199 dbl = 1;
7200 case M_LCA_AB:
7201 call = 1;
7202 goto do_la;
7203 case M_DLA_AB:
7204 dbl = 1;
7205 case M_LA_AB:
7206 do_la:
7207 /* Load the address of a symbol into a register. If breg is not
7208 zero, we then add a base register to it. */
7209
7210 if (dbl && HAVE_32BIT_GPRS)
7211 as_warn (_("dla used to load 32-bit register"));
7212
7213 if (!dbl && HAVE_64BIT_OBJECTS)
7214 as_warn (_("la used to load 64-bit address"));
7215
7216 if (offset_expr.X_op == O_constant
7217 && offset_expr.X_add_number >= -0x8000
7218 && offset_expr.X_add_number < 0x8000)
7219 {
7220 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
7221 "t,r,j", treg, sreg, BFD_RELOC_LO16);
7222 break;
7223 }
7224
7225 if (mips_opts.at && (treg == breg))
7226 {
7227 tempreg = AT;
7228 used_at = 1;
7229 }
7230 else
7231 {
7232 tempreg = treg;
7233 }
7234
7235 if (offset_expr.X_op != O_symbol
7236 && offset_expr.X_op != O_constant)
7237 {
7238 as_bad (_("Expression too complex"));
7239 offset_expr.X_op = O_constant;
7240 }
7241
7242 if (offset_expr.X_op == O_constant)
7243 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
7244 else if (mips_pic == NO_PIC)
7245 {
7246 /* If this is a reference to a GP relative symbol, we want
7247 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
7248 Otherwise we want
7249 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
7250 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7251 If we have a constant, we need two instructions anyhow,
7252 so we may as well always use the latter form.
7253
7254 With 64bit address space and a usable $at we want
7255 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7256 lui $at,<sym> (BFD_RELOC_HI16_S)
7257 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7258 daddiu $at,<sym> (BFD_RELOC_LO16)
7259 dsll32 $tempreg,0
7260 daddu $tempreg,$tempreg,$at
7261
7262 If $at is already in use, we use a path which is suboptimal
7263 on superscalar processors.
7264 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7265 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7266 dsll $tempreg,16
7267 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
7268 dsll $tempreg,16
7269 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
7270
7271 For GP relative symbols in 64bit address space we can use
7272 the same sequence as in 32bit address space. */
7273 if (HAVE_64BIT_SYMBOLS)
7274 {
7275 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7276 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7277 {
7278 relax_start (offset_expr.X_add_symbol);
7279 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7280 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7281 relax_switch ();
7282 }
7283
7284 if (used_at == 0 && mips_opts.at)
7285 {
7286 macro_build (&offset_expr, "lui", LUI_FMT,
7287 tempreg, BFD_RELOC_MIPS_HIGHEST);
7288 macro_build (&offset_expr, "lui", LUI_FMT,
7289 AT, BFD_RELOC_HI16_S);
7290 macro_build (&offset_expr, "daddiu", "t,r,j",
7291 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7292 macro_build (&offset_expr, "daddiu", "t,r,j",
7293 AT, AT, BFD_RELOC_LO16);
7294 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
7295 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
7296 used_at = 1;
7297 }
7298 else
7299 {
7300 macro_build (&offset_expr, "lui", LUI_FMT,
7301 tempreg, BFD_RELOC_MIPS_HIGHEST);
7302 macro_build (&offset_expr, "daddiu", "t,r,j",
7303 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7304 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7305 macro_build (&offset_expr, "daddiu", "t,r,j",
7306 tempreg, tempreg, BFD_RELOC_HI16_S);
7307 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7308 macro_build (&offset_expr, "daddiu", "t,r,j",
7309 tempreg, tempreg, BFD_RELOC_LO16);
7310 }
7311
7312 if (mips_relax.sequence)
7313 relax_end ();
7314 }
7315 else
7316 {
7317 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7318 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7319 {
7320 relax_start (offset_expr.X_add_symbol);
7321 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7322 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7323 relax_switch ();
7324 }
7325 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
7326 as_bad (_("Offset too large"));
7327 macro_build_lui (&offset_expr, tempreg);
7328 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7329 tempreg, tempreg, BFD_RELOC_LO16);
7330 if (mips_relax.sequence)
7331 relax_end ();
7332 }
7333 }
7334 else if (!mips_big_got && !HAVE_NEWABI)
7335 {
7336 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7337
7338 /* If this is a reference to an external symbol, and there
7339 is no constant, we want
7340 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7341 or for lca or if tempreg is PIC_CALL_REG
7342 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7343 For a local symbol, we want
7344 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7345 nop
7346 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7347
7348 If we have a small constant, and this is a reference to
7349 an external symbol, we want
7350 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7351 nop
7352 addiu $tempreg,$tempreg,<constant>
7353 For a local symbol, we want the same instruction
7354 sequence, but we output a BFD_RELOC_LO16 reloc on the
7355 addiu instruction.
7356
7357 If we have a large constant, and this is a reference to
7358 an external symbol, we want
7359 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7360 lui $at,<hiconstant>
7361 addiu $at,$at,<loconstant>
7362 addu $tempreg,$tempreg,$at
7363 For a local symbol, we want the same instruction
7364 sequence, but we output a BFD_RELOC_LO16 reloc on the
7365 addiu instruction.
7366 */
7367
7368 if (offset_expr.X_add_number == 0)
7369 {
7370 if (mips_pic == SVR4_PIC
7371 && breg == 0
7372 && (call || tempreg == PIC_CALL_REG))
7373 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7374
7375 relax_start (offset_expr.X_add_symbol);
7376 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7377 lw_reloc_type, mips_gp_register);
7378 if (breg != 0)
7379 {
7380 /* We're going to put in an addu instruction using
7381 tempreg, so we may as well insert the nop right
7382 now. */
7383 load_delay_nop ();
7384 }
7385 relax_switch ();
7386 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7387 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
7388 load_delay_nop ();
7389 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7390 tempreg, tempreg, BFD_RELOC_LO16);
7391 relax_end ();
7392 /* FIXME: If breg == 0, and the next instruction uses
7393 $tempreg, then if this variant case is used an extra
7394 nop will be generated. */
7395 }
7396 else if (offset_expr.X_add_number >= -0x8000
7397 && offset_expr.X_add_number < 0x8000)
7398 {
7399 load_got_offset (tempreg, &offset_expr);
7400 load_delay_nop ();
7401 add_got_offset (tempreg, &offset_expr);
7402 }
7403 else
7404 {
7405 expr1.X_add_number = offset_expr.X_add_number;
7406 offset_expr.X_add_number =
7407 SEXT_16BIT (offset_expr.X_add_number);
7408 load_got_offset (tempreg, &offset_expr);
7409 offset_expr.X_add_number = expr1.X_add_number;
7410 /* If we are going to add in a base register, and the
7411 target register and the base register are the same,
7412 then we are using AT as a temporary register. Since
7413 we want to load the constant into AT, we add our
7414 current AT (from the global offset table) and the
7415 register into the register now, and pretend we were
7416 not using a base register. */
7417 if (breg == treg)
7418 {
7419 load_delay_nop ();
7420 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7421 treg, AT, breg);
7422 breg = 0;
7423 tempreg = treg;
7424 }
7425 add_got_offset_hilo (tempreg, &offset_expr, AT);
7426 used_at = 1;
7427 }
7428 }
7429 else if (!mips_big_got && HAVE_NEWABI)
7430 {
7431 int add_breg_early = 0;
7432
7433 /* If this is a reference to an external, and there is no
7434 constant, or local symbol (*), with or without a
7435 constant, we want
7436 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7437 or for lca or if tempreg is PIC_CALL_REG
7438 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7439
7440 If we have a small constant, and this is a reference to
7441 an external symbol, we want
7442 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7443 addiu $tempreg,$tempreg,<constant>
7444
7445 If we have a large constant, and this is a reference to
7446 an external symbol, we want
7447 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7448 lui $at,<hiconstant>
7449 addiu $at,$at,<loconstant>
7450 addu $tempreg,$tempreg,$at
7451
7452 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7453 local symbols, even though it introduces an additional
7454 instruction. */
7455
7456 if (offset_expr.X_add_number)
7457 {
7458 expr1.X_add_number = offset_expr.X_add_number;
7459 offset_expr.X_add_number = 0;
7460
7461 relax_start (offset_expr.X_add_symbol);
7462 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7463 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7464
7465 if (expr1.X_add_number >= -0x8000
7466 && expr1.X_add_number < 0x8000)
7467 {
7468 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7469 tempreg, tempreg, BFD_RELOC_LO16);
7470 }
7471 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7472 {
7473 /* If we are going to add in a base register, and the
7474 target register and the base register are the same,
7475 then we are using AT as a temporary register. Since
7476 we want to load the constant into AT, we add our
7477 current AT (from the global offset table) and the
7478 register into the register now, and pretend we were
7479 not using a base register. */
7480 if (breg != treg)
7481 dreg = tempreg;
7482 else
7483 {
7484 gas_assert (tempreg == AT);
7485 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7486 treg, AT, breg);
7487 dreg = treg;
7488 add_breg_early = 1;
7489 }
7490
7491 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7492 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7493 dreg, dreg, AT);
7494
7495 used_at = 1;
7496 }
7497 else
7498 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7499
7500 relax_switch ();
7501 offset_expr.X_add_number = expr1.X_add_number;
7502
7503 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7504 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7505 if (add_breg_early)
7506 {
7507 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7508 treg, tempreg, breg);
7509 breg = 0;
7510 tempreg = treg;
7511 }
7512 relax_end ();
7513 }
7514 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
7515 {
7516 relax_start (offset_expr.X_add_symbol);
7517 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7518 BFD_RELOC_MIPS_CALL16, mips_gp_register);
7519 relax_switch ();
7520 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7521 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7522 relax_end ();
7523 }
7524 else
7525 {
7526 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7527 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7528 }
7529 }
7530 else if (mips_big_got && !HAVE_NEWABI)
7531 {
7532 int gpdelay;
7533 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7534 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7535 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7536
7537 /* This is the large GOT case. If this is a reference to an
7538 external symbol, and there is no constant, we want
7539 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7540 addu $tempreg,$tempreg,$gp
7541 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7542 or for lca or if tempreg is PIC_CALL_REG
7543 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7544 addu $tempreg,$tempreg,$gp
7545 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7546 For a local symbol, we want
7547 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7548 nop
7549 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7550
7551 If we have a small constant, and this is a reference to
7552 an external symbol, we want
7553 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7554 addu $tempreg,$tempreg,$gp
7555 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7556 nop
7557 addiu $tempreg,$tempreg,<constant>
7558 For a local symbol, we want
7559 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7560 nop
7561 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7562
7563 If we have a large constant, and this is a reference to
7564 an external symbol, we want
7565 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7566 addu $tempreg,$tempreg,$gp
7567 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7568 lui $at,<hiconstant>
7569 addiu $at,$at,<loconstant>
7570 addu $tempreg,$tempreg,$at
7571 For a local symbol, we want
7572 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7573 lui $at,<hiconstant>
7574 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
7575 addu $tempreg,$tempreg,$at
7576 */
7577
7578 expr1.X_add_number = offset_expr.X_add_number;
7579 offset_expr.X_add_number = 0;
7580 relax_start (offset_expr.X_add_symbol);
7581 gpdelay = reg_needs_delay (mips_gp_register);
7582 if (expr1.X_add_number == 0 && breg == 0
7583 && (call || tempreg == PIC_CALL_REG))
7584 {
7585 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7586 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7587 }
7588 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7589 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7590 tempreg, tempreg, mips_gp_register);
7591 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7592 tempreg, lw_reloc_type, tempreg);
7593 if (expr1.X_add_number == 0)
7594 {
7595 if (breg != 0)
7596 {
7597 /* We're going to put in an addu instruction using
7598 tempreg, so we may as well insert the nop right
7599 now. */
7600 load_delay_nop ();
7601 }
7602 }
7603 else if (expr1.X_add_number >= -0x8000
7604 && expr1.X_add_number < 0x8000)
7605 {
7606 load_delay_nop ();
7607 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7608 tempreg, tempreg, BFD_RELOC_LO16);
7609 }
7610 else
7611 {
7612 /* If we are going to add in a base register, and the
7613 target register and the base register are the same,
7614 then we are using AT as a temporary register. Since
7615 we want to load the constant into AT, we add our
7616 current AT (from the global offset table) and the
7617 register into the register now, and pretend we were
7618 not using a base register. */
7619 if (breg != treg)
7620 dreg = tempreg;
7621 else
7622 {
7623 gas_assert (tempreg == AT);
7624 load_delay_nop ();
7625 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7626 treg, AT, breg);
7627 dreg = treg;
7628 }
7629
7630 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7631 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
7632
7633 used_at = 1;
7634 }
7635 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
7636 relax_switch ();
7637
7638 if (gpdelay)
7639 {
7640 /* This is needed because this instruction uses $gp, but
7641 the first instruction on the main stream does not. */
7642 macro_build (NULL, "nop", "");
7643 }
7644
7645 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7646 local_reloc_type, mips_gp_register);
7647 if (expr1.X_add_number >= -0x8000
7648 && expr1.X_add_number < 0x8000)
7649 {
7650 load_delay_nop ();
7651 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7652 tempreg, tempreg, BFD_RELOC_LO16);
7653 /* FIXME: If add_number is 0, and there was no base
7654 register, the external symbol case ended with a load,
7655 so if the symbol turns out to not be external, and
7656 the next instruction uses tempreg, an unnecessary nop
7657 will be inserted. */
7658 }
7659 else
7660 {
7661 if (breg == treg)
7662 {
7663 /* We must add in the base register now, as in the
7664 external symbol case. */
7665 gas_assert (tempreg == AT);
7666 load_delay_nop ();
7667 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7668 treg, AT, breg);
7669 tempreg = treg;
7670 /* We set breg to 0 because we have arranged to add
7671 it in in both cases. */
7672 breg = 0;
7673 }
7674
7675 macro_build_lui (&expr1, AT);
7676 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7677 AT, AT, BFD_RELOC_LO16);
7678 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7679 tempreg, tempreg, AT);
7680 used_at = 1;
7681 }
7682 relax_end ();
7683 }
7684 else if (mips_big_got && HAVE_NEWABI)
7685 {
7686 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7687 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7688 int add_breg_early = 0;
7689
7690 /* This is the large GOT case. If this is a reference to an
7691 external symbol, and there is no constant, we want
7692 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7693 add $tempreg,$tempreg,$gp
7694 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7695 or for lca or if tempreg is PIC_CALL_REG
7696 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7697 add $tempreg,$tempreg,$gp
7698 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7699
7700 If we have a small constant, and this is a reference to
7701 an external symbol, we want
7702 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7703 add $tempreg,$tempreg,$gp
7704 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7705 addi $tempreg,$tempreg,<constant>
7706
7707 If we have a large constant, and this is a reference to
7708 an external symbol, we want
7709 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7710 addu $tempreg,$tempreg,$gp
7711 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7712 lui $at,<hiconstant>
7713 addi $at,$at,<loconstant>
7714 add $tempreg,$tempreg,$at
7715
7716 If we have NewABI, and we know it's a local symbol, we want
7717 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
7718 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
7719 otherwise we have to resort to GOT_HI16/GOT_LO16. */
7720
7721 relax_start (offset_expr.X_add_symbol);
7722
7723 expr1.X_add_number = offset_expr.X_add_number;
7724 offset_expr.X_add_number = 0;
7725
7726 if (expr1.X_add_number == 0 && breg == 0
7727 && (call || tempreg == PIC_CALL_REG))
7728 {
7729 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7730 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7731 }
7732 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7733 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7734 tempreg, tempreg, mips_gp_register);
7735 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7736 tempreg, lw_reloc_type, tempreg);
7737
7738 if (expr1.X_add_number == 0)
7739 ;
7740 else if (expr1.X_add_number >= -0x8000
7741 && expr1.X_add_number < 0x8000)
7742 {
7743 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7744 tempreg, tempreg, BFD_RELOC_LO16);
7745 }
7746 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7747 {
7748 /* If we are going to add in a base register, and the
7749 target register and the base register are the same,
7750 then we are using AT as a temporary register. Since
7751 we want to load the constant into AT, we add our
7752 current AT (from the global offset table) and the
7753 register into the register now, and pretend we were
7754 not using a base register. */
7755 if (breg != treg)
7756 dreg = tempreg;
7757 else
7758 {
7759 gas_assert (tempreg == AT);
7760 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7761 treg, AT, breg);
7762 dreg = treg;
7763 add_breg_early = 1;
7764 }
7765
7766 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7767 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
7768
7769 used_at = 1;
7770 }
7771 else
7772 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7773
7774 relax_switch ();
7775 offset_expr.X_add_number = expr1.X_add_number;
7776 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7777 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
7778 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
7779 tempreg, BFD_RELOC_MIPS_GOT_OFST);
7780 if (add_breg_early)
7781 {
7782 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7783 treg, tempreg, breg);
7784 breg = 0;
7785 tempreg = treg;
7786 }
7787 relax_end ();
7788 }
7789 else
7790 abort ();
7791
7792 if (breg != 0)
7793 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
7794 break;
7795
7796 case M_MSGSND:
7797 gas_assert (!mips_opts.micromips);
7798 {
7799 unsigned long temp = (treg << 16) | (0x01);
7800 macro_build (NULL, "c2", "C", temp);
7801 }
7802 break;
7803
7804 case M_MSGLD:
7805 gas_assert (!mips_opts.micromips);
7806 {
7807 unsigned long temp = (0x02);
7808 macro_build (NULL, "c2", "C", temp);
7809 }
7810 break;
7811
7812 case M_MSGLD_T:
7813 gas_assert (!mips_opts.micromips);
7814 {
7815 unsigned long temp = (treg << 16) | (0x02);
7816 macro_build (NULL, "c2", "C", temp);
7817 }
7818 break;
7819
7820 case M_MSGWAIT:
7821 gas_assert (!mips_opts.micromips);
7822 macro_build (NULL, "c2", "C", 3);
7823 break;
7824
7825 case M_MSGWAIT_T:
7826 gas_assert (!mips_opts.micromips);
7827 {
7828 unsigned long temp = (treg << 16) | 0x03;
7829 macro_build (NULL, "c2", "C", temp);
7830 }
7831 break;
7832
7833 case M_J_A:
7834 /* The j instruction may not be used in PIC code, since it
7835 requires an absolute address. We convert it to a b
7836 instruction. */
7837 if (mips_pic == NO_PIC)
7838 macro_build (&offset_expr, "j", "a");
7839 else
7840 macro_build (&offset_expr, "b", "p");
7841 break;
7842
7843 /* The jal instructions must be handled as macros because when
7844 generating PIC code they expand to multi-instruction
7845 sequences. Normally they are simple instructions. */
7846 case M_JALS_1:
7847 dreg = RA;
7848 /* Fall through. */
7849 case M_JALS_2:
7850 gas_assert (mips_opts.micromips);
7851 jals = 1;
7852 goto jal;
7853 case M_JAL_1:
7854 dreg = RA;
7855 /* Fall through. */
7856 case M_JAL_2:
7857 jal:
7858 if (mips_pic == NO_PIC)
7859 {
7860 s = jals ? "jalrs" : "jalr";
7861 if (mips_opts.micromips
7862 && dreg == RA
7863 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7864 macro_build (NULL, s, "mj", sreg);
7865 else
7866 macro_build (NULL, s, JALR_FMT, dreg, sreg);
7867 }
7868 else
7869 {
7870 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
7871 && mips_cprestore_offset >= 0);
7872
7873 if (sreg != PIC_CALL_REG)
7874 as_warn (_("MIPS PIC call to register other than $25"));
7875
7876 s = (mips_opts.micromips && (!mips_opts.noreorder || cprestore)
7877 ? "jalrs" : "jalr");
7878 if (mips_opts.micromips
7879 && dreg == RA
7880 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7881 macro_build (NULL, s, "mj", sreg);
7882 else
7883 macro_build (NULL, s, JALR_FMT, dreg, sreg);
7884 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
7885 {
7886 if (mips_cprestore_offset < 0)
7887 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7888 else
7889 {
7890 if (!mips_frame_reg_valid)
7891 {
7892 as_warn (_("No .frame pseudo-op used in PIC code"));
7893 /* Quiet this warning. */
7894 mips_frame_reg_valid = 1;
7895 }
7896 if (!mips_cprestore_valid)
7897 {
7898 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7899 /* Quiet this warning. */
7900 mips_cprestore_valid = 1;
7901 }
7902 if (mips_opts.noreorder)
7903 macro_build (NULL, "nop", "");
7904 expr1.X_add_number = mips_cprestore_offset;
7905 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
7906 mips_gp_register,
7907 mips_frame_reg,
7908 HAVE_64BIT_ADDRESSES);
7909 }
7910 }
7911 }
7912
7913 break;
7914
7915 case M_JALS_A:
7916 gas_assert (mips_opts.micromips);
7917 jals = 1;
7918 /* Fall through. */
7919 case M_JAL_A:
7920 if (mips_pic == NO_PIC)
7921 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
7922 else if (mips_pic == SVR4_PIC)
7923 {
7924 /* If this is a reference to an external symbol, and we are
7925 using a small GOT, we want
7926 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7927 nop
7928 jalr $ra,$25
7929 nop
7930 lw $gp,cprestore($sp)
7931 The cprestore value is set using the .cprestore
7932 pseudo-op. If we are using a big GOT, we want
7933 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7934 addu $25,$25,$gp
7935 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
7936 nop
7937 jalr $ra,$25
7938 nop
7939 lw $gp,cprestore($sp)
7940 If the symbol is not external, we want
7941 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7942 nop
7943 addiu $25,$25,<sym> (BFD_RELOC_LO16)
7944 jalr $ra,$25
7945 nop
7946 lw $gp,cprestore($sp)
7947
7948 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
7949 sequences above, minus nops, unless the symbol is local,
7950 which enables us to use GOT_PAGE/GOT_OFST (big got) or
7951 GOT_DISP. */
7952 if (HAVE_NEWABI)
7953 {
7954 if (!mips_big_got)
7955 {
7956 relax_start (offset_expr.X_add_symbol);
7957 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7958 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
7959 mips_gp_register);
7960 relax_switch ();
7961 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7962 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
7963 mips_gp_register);
7964 relax_end ();
7965 }
7966 else
7967 {
7968 relax_start (offset_expr.X_add_symbol);
7969 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
7970 BFD_RELOC_MIPS_CALL_HI16);
7971 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
7972 PIC_CALL_REG, mips_gp_register);
7973 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7974 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
7975 PIC_CALL_REG);
7976 relax_switch ();
7977 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7978 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
7979 mips_gp_register);
7980 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7981 PIC_CALL_REG, PIC_CALL_REG,
7982 BFD_RELOC_MIPS_GOT_OFST);
7983 relax_end ();
7984 }
7985
7986 macro_build_jalr (&offset_expr, 0);
7987 }
7988 else
7989 {
7990 relax_start (offset_expr.X_add_symbol);
7991 if (!mips_big_got)
7992 {
7993 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7994 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
7995 mips_gp_register);
7996 load_delay_nop ();
7997 relax_switch ();
7998 }
7999 else
8000 {
8001 int gpdelay;
8002
8003 gpdelay = reg_needs_delay (mips_gp_register);
8004 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
8005 BFD_RELOC_MIPS_CALL_HI16);
8006 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8007 PIC_CALL_REG, mips_gp_register);
8008 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8009 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8010 PIC_CALL_REG);
8011 load_delay_nop ();
8012 relax_switch ();
8013 if (gpdelay)
8014 macro_build (NULL, "nop", "");
8015 }
8016 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8017 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
8018 mips_gp_register);
8019 load_delay_nop ();
8020 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8021 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
8022 relax_end ();
8023 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
8024
8025 if (mips_cprestore_offset < 0)
8026 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8027 else
8028 {
8029 if (!mips_frame_reg_valid)
8030 {
8031 as_warn (_("No .frame pseudo-op used in PIC code"));
8032 /* Quiet this warning. */
8033 mips_frame_reg_valid = 1;
8034 }
8035 if (!mips_cprestore_valid)
8036 {
8037 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8038 /* Quiet this warning. */
8039 mips_cprestore_valid = 1;
8040 }
8041 if (mips_opts.noreorder)
8042 macro_build (NULL, "nop", "");
8043 expr1.X_add_number = mips_cprestore_offset;
8044 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
8045 mips_gp_register,
8046 mips_frame_reg,
8047 HAVE_64BIT_ADDRESSES);
8048 }
8049 }
8050 }
8051 else if (mips_pic == VXWORKS_PIC)
8052 as_bad (_("Non-PIC jump used in PIC library"));
8053 else
8054 abort ();
8055
8056 break;
8057
8058 case M_ACLR_AB:
8059 ab = 1;
8060 case M_ACLR_OB:
8061 s = "aclr";
8062 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8063 fmt = "\\,~(b)";
8064 off12 = 1;
8065 goto ld_st;
8066 case M_ASET_AB:
8067 ab = 1;
8068 case M_ASET_OB:
8069 s = "aset";
8070 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8071 fmt = "\\,~(b)";
8072 off12 = 1;
8073 goto ld_st;
8074 case M_LB_AB:
8075 ab = 1;
8076 s = "lb";
8077 fmt = "t,o(b)";
8078 goto ld;
8079 case M_LBU_AB:
8080 ab = 1;
8081 s = "lbu";
8082 fmt = "t,o(b)";
8083 goto ld;
8084 case M_LH_AB:
8085 ab = 1;
8086 s = "lh";
8087 fmt = "t,o(b)";
8088 goto ld;
8089 case M_LHU_AB:
8090 ab = 1;
8091 s = "lhu";
8092 fmt = "t,o(b)";
8093 goto ld;
8094 case M_LW_AB:
8095 ab = 1;
8096 s = "lw";
8097 fmt = "t,o(b)";
8098 goto ld;
8099 case M_LWC0_AB:
8100 ab = 1;
8101 gas_assert (!mips_opts.micromips);
8102 s = "lwc0";
8103 fmt = "E,o(b)";
8104 /* Itbl support may require additional care here. */
8105 coproc = 1;
8106 goto ld_st;
8107 case M_LWC1_AB:
8108 ab = 1;
8109 s = "lwc1";
8110 fmt = "T,o(b)";
8111 /* Itbl support may require additional care here. */
8112 coproc = 1;
8113 goto ld_st;
8114 case M_LWC2_AB:
8115 ab = 1;
8116 case M_LWC2_OB:
8117 s = "lwc2";
8118 fmt = COP12_FMT;
8119 off12 = mips_opts.micromips;
8120 /* Itbl support may require additional care here. */
8121 coproc = 1;
8122 goto ld_st;
8123 case M_LWC3_AB:
8124 ab = 1;
8125 gas_assert (!mips_opts.micromips);
8126 s = "lwc3";
8127 fmt = "E,o(b)";
8128 /* Itbl support may require additional care here. */
8129 coproc = 1;
8130 goto ld_st;
8131 case M_LWL_AB:
8132 ab = 1;
8133 case M_LWL_OB:
8134 s = "lwl";
8135 fmt = MEM12_FMT;
8136 off12 = mips_opts.micromips;
8137 goto ld_st;
8138 case M_LWR_AB:
8139 ab = 1;
8140 case M_LWR_OB:
8141 s = "lwr";
8142 fmt = MEM12_FMT;
8143 off12 = mips_opts.micromips;
8144 goto ld_st;
8145 case M_LDC1_AB:
8146 ab = 1;
8147 s = "ldc1";
8148 fmt = "T,o(b)";
8149 /* Itbl support may require additional care here. */
8150 coproc = 1;
8151 goto ld_st;
8152 case M_LDC2_AB:
8153 ab = 1;
8154 case M_LDC2_OB:
8155 s = "ldc2";
8156 fmt = COP12_FMT;
8157 off12 = mips_opts.micromips;
8158 /* Itbl support may require additional care here. */
8159 coproc = 1;
8160 goto ld_st;
8161 case M_LDC3_AB:
8162 ab = 1;
8163 s = "ldc3";
8164 fmt = "E,o(b)";
8165 /* Itbl support may require additional care here. */
8166 coproc = 1;
8167 goto ld_st;
8168 case M_LDL_AB:
8169 ab = 1;
8170 case M_LDL_OB:
8171 s = "ldl";
8172 fmt = MEM12_FMT;
8173 off12 = mips_opts.micromips;
8174 goto ld_st;
8175 case M_LDR_AB:
8176 ab = 1;
8177 case M_LDR_OB:
8178 s = "ldr";
8179 fmt = MEM12_FMT;
8180 off12 = mips_opts.micromips;
8181 goto ld_st;
8182 case M_LL_AB:
8183 ab = 1;
8184 case M_LL_OB:
8185 s = "ll";
8186 fmt = MEM12_FMT;
8187 off12 = mips_opts.micromips;
8188 goto ld;
8189 case M_LLD_AB:
8190 ab = 1;
8191 case M_LLD_OB:
8192 s = "lld";
8193 fmt = MEM12_FMT;
8194 off12 = mips_opts.micromips;
8195 goto ld;
8196 case M_LWU_AB:
8197 ab = 1;
8198 case M_LWU_OB:
8199 s = "lwu";
8200 fmt = MEM12_FMT;
8201 off12 = mips_opts.micromips;
8202 goto ld;
8203 case M_LWP_AB:
8204 ab = 1;
8205 case M_LWP_OB:
8206 gas_assert (mips_opts.micromips);
8207 s = "lwp";
8208 fmt = "t,~(b)";
8209 off12 = 1;
8210 lp = 1;
8211 goto ld;
8212 case M_LDP_AB:
8213 ab = 1;
8214 case M_LDP_OB:
8215 gas_assert (mips_opts.micromips);
8216 s = "ldp";
8217 fmt = "t,~(b)";
8218 off12 = 1;
8219 lp = 1;
8220 goto ld;
8221 case M_LWM_AB:
8222 ab = 1;
8223 case M_LWM_OB:
8224 gas_assert (mips_opts.micromips);
8225 s = "lwm";
8226 fmt = "n,~(b)";
8227 off12 = 1;
8228 goto ld_st;
8229 case M_LDM_AB:
8230 ab = 1;
8231 case M_LDM_OB:
8232 gas_assert (mips_opts.micromips);
8233 s = "ldm";
8234 fmt = "n,~(b)";
8235 off12 = 1;
8236 goto ld_st;
8237
8238 ld:
8239 if (breg == treg + lp)
8240 goto ld_st;
8241 else
8242 tempreg = treg + lp;
8243 goto ld_noat;
8244
8245 case M_SB_AB:
8246 ab = 1;
8247 s = "sb";
8248 fmt = "t,o(b)";
8249 goto ld_st;
8250 case M_SH_AB:
8251 ab = 1;
8252 s = "sh";
8253 fmt = "t,o(b)";
8254 goto ld_st;
8255 case M_SW_AB:
8256 ab = 1;
8257 s = "sw";
8258 fmt = "t,o(b)";
8259 goto ld_st;
8260 case M_SWC0_AB:
8261 ab = 1;
8262 gas_assert (!mips_opts.micromips);
8263 s = "swc0";
8264 fmt = "E,o(b)";
8265 /* Itbl support may require additional care here. */
8266 coproc = 1;
8267 goto ld_st;
8268 case M_SWC1_AB:
8269 ab = 1;
8270 s = "swc1";
8271 fmt = "T,o(b)";
8272 /* Itbl support may require additional care here. */
8273 coproc = 1;
8274 goto ld_st;
8275 case M_SWC2_AB:
8276 ab = 1;
8277 case M_SWC2_OB:
8278 s = "swc2";
8279 fmt = COP12_FMT;
8280 off12 = mips_opts.micromips;
8281 /* Itbl support may require additional care here. */
8282 coproc = 1;
8283 goto ld_st;
8284 case M_SWC3_AB:
8285 ab = 1;
8286 gas_assert (!mips_opts.micromips);
8287 s = "swc3";
8288 fmt = "E,o(b)";
8289 /* Itbl support may require additional care here. */
8290 coproc = 1;
8291 goto ld_st;
8292 case M_SWL_AB:
8293 ab = 1;
8294 case M_SWL_OB:
8295 s = "swl";
8296 fmt = MEM12_FMT;
8297 off12 = mips_opts.micromips;
8298 goto ld_st;
8299 case M_SWR_AB:
8300 ab = 1;
8301 case M_SWR_OB:
8302 s = "swr";
8303 fmt = MEM12_FMT;
8304 off12 = mips_opts.micromips;
8305 goto ld_st;
8306 case M_SC_AB:
8307 ab = 1;
8308 case M_SC_OB:
8309 s = "sc";
8310 fmt = MEM12_FMT;
8311 off12 = mips_opts.micromips;
8312 goto ld_st;
8313 case M_SCD_AB:
8314 ab = 1;
8315 case M_SCD_OB:
8316 s = "scd";
8317 fmt = MEM12_FMT;
8318 off12 = mips_opts.micromips;
8319 goto ld_st;
8320 case M_CACHE_AB:
8321 ab = 1;
8322 case M_CACHE_OB:
8323 s = "cache";
8324 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
8325 off12 = mips_opts.micromips;
8326 goto ld_st;
8327 case M_PREF_AB:
8328 ab = 1;
8329 case M_PREF_OB:
8330 s = "pref";
8331 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
8332 off12 = mips_opts.micromips;
8333 goto ld_st;
8334 case M_SDC1_AB:
8335 ab = 1;
8336 s = "sdc1";
8337 fmt = "T,o(b)";
8338 coproc = 1;
8339 /* Itbl support may require additional care here. */
8340 goto ld_st;
8341 case M_SDC2_AB:
8342 ab = 1;
8343 case M_SDC2_OB:
8344 s = "sdc2";
8345 fmt = COP12_FMT;
8346 off12 = mips_opts.micromips;
8347 /* Itbl support may require additional care here. */
8348 coproc = 1;
8349 goto ld_st;
8350 case M_SDC3_AB:
8351 ab = 1;
8352 gas_assert (!mips_opts.micromips);
8353 s = "sdc3";
8354 fmt = "E,o(b)";
8355 /* Itbl support may require additional care here. */
8356 coproc = 1;
8357 goto ld_st;
8358 case M_SDL_AB:
8359 ab = 1;
8360 case M_SDL_OB:
8361 s = "sdl";
8362 fmt = MEM12_FMT;
8363 off12 = mips_opts.micromips;
8364 goto ld_st;
8365 case M_SDR_AB:
8366 ab = 1;
8367 case M_SDR_OB:
8368 s = "sdr";
8369 fmt = MEM12_FMT;
8370 off12 = mips_opts.micromips;
8371 goto ld_st;
8372 case M_SWP_AB:
8373 ab = 1;
8374 case M_SWP_OB:
8375 gas_assert (mips_opts.micromips);
8376 s = "swp";
8377 fmt = "t,~(b)";
8378 off12 = 1;
8379 goto ld_st;
8380 case M_SDP_AB:
8381 ab = 1;
8382 case M_SDP_OB:
8383 gas_assert (mips_opts.micromips);
8384 s = "sdp";
8385 fmt = "t,~(b)";
8386 off12 = 1;
8387 goto ld_st;
8388 case M_SWM_AB:
8389 ab = 1;
8390 case M_SWM_OB:
8391 gas_assert (mips_opts.micromips);
8392 s = "swm";
8393 fmt = "n,~(b)";
8394 off12 = 1;
8395 goto ld_st;
8396 case M_SDM_AB:
8397 ab = 1;
8398 case M_SDM_OB:
8399 gas_assert (mips_opts.micromips);
8400 s = "sdm";
8401 fmt = "n,~(b)";
8402 off12 = 1;
8403
8404 ld_st:
8405 tempreg = AT;
8406 used_at = 1;
8407 ld_noat:
8408 if (offset_expr.X_op != O_constant
8409 && offset_expr.X_op != O_symbol)
8410 {
8411 as_bad (_("Expression too complex"));
8412 offset_expr.X_op = O_constant;
8413 }
8414
8415 if (HAVE_32BIT_ADDRESSES
8416 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
8417 {
8418 char value [32];
8419
8420 sprintf_vma (value, offset_expr.X_add_number);
8421 as_bad (_("Number (0x%s) larger than 32 bits"), value);
8422 }
8423
8424 /* A constant expression in PIC code can be handled just as it
8425 is in non PIC code. */
8426 if (offset_expr.X_op == O_constant)
8427 {
8428 int hipart = 0;
8429
8430 expr1.X_add_number = offset_expr.X_add_number;
8431 normalize_address_expr (&expr1);
8432 if (!off12 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
8433 {
8434 expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8435 & ~(bfd_vma) 0xffff);
8436 hipart = 1;
8437 }
8438 else if (off12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
8439 {
8440 expr1.X_add_number = ((expr1.X_add_number + 0x800)
8441 & ~(bfd_vma) 0xfff);
8442 hipart = 1;
8443 }
8444 if (hipart)
8445 {
8446 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8447 if (breg != 0)
8448 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8449 tempreg, tempreg, breg);
8450 breg = tempreg;
8451 }
8452 if (off0)
8453 {
8454 if (offset_expr.X_add_number == 0)
8455 tempreg = breg;
8456 else
8457 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8458 "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8459 macro_build (NULL, s, fmt, treg, tempreg);
8460 }
8461 else if (!off12)
8462 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8463 else
8464 macro_build (NULL, s, fmt,
8465 treg, (unsigned long) offset_expr.X_add_number, breg);
8466 }
8467 else if (off12 || off0)
8468 {
8469 /* A 12-bit or 0-bit offset field is too narrow to be used
8470 for a low-part relocation, so load the whole address into
8471 the auxillary register. In the case of "A(b)" addresses,
8472 we first load absolute address "A" into the register and
8473 then add base register "b". In the case of "o(b)" addresses,
8474 we simply need to add 16-bit offset "o" to base register "b", and
8475 offset_reloc already contains the relocations associated
8476 with "o". */
8477 if (ab)
8478 {
8479 load_address (tempreg, &offset_expr, &used_at);
8480 if (breg != 0)
8481 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8482 tempreg, tempreg, breg);
8483 }
8484 else
8485 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8486 tempreg, breg, -1,
8487 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8488 expr1.X_add_number = 0;
8489 if (off0)
8490 macro_build (NULL, s, fmt, treg, tempreg);
8491 else
8492 macro_build (NULL, s, fmt,
8493 treg, (unsigned long) expr1.X_add_number, tempreg);
8494 }
8495 else if (mips_pic == NO_PIC)
8496 {
8497 /* If this is a reference to a GP relative symbol, and there
8498 is no base register, we want
8499 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
8500 Otherwise, if there is no base register, we want
8501 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8502 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8503 If we have a constant, we need two instructions anyhow,
8504 so we always use the latter form.
8505
8506 If we have a base register, and this is a reference to a
8507 GP relative symbol, we want
8508 addu $tempreg,$breg,$gp
8509 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
8510 Otherwise we want
8511 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8512 addu $tempreg,$tempreg,$breg
8513 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8514 With a constant we always use the latter case.
8515
8516 With 64bit address space and no base register and $at usable,
8517 we want
8518 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8519 lui $at,<sym> (BFD_RELOC_HI16_S)
8520 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8521 dsll32 $tempreg,0
8522 daddu $tempreg,$at
8523 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8524 If we have a base register, we want
8525 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8526 lui $at,<sym> (BFD_RELOC_HI16_S)
8527 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8528 daddu $at,$breg
8529 dsll32 $tempreg,0
8530 daddu $tempreg,$at
8531 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8532
8533 Without $at we can't generate the optimal path for superscalar
8534 processors here since this would require two temporary registers.
8535 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8536 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8537 dsll $tempreg,16
8538 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8539 dsll $tempreg,16
8540 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8541 If we have a base register, we want
8542 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8543 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8544 dsll $tempreg,16
8545 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8546 dsll $tempreg,16
8547 daddu $tempreg,$tempreg,$breg
8548 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8549
8550 For GP relative symbols in 64bit address space we can use
8551 the same sequence as in 32bit address space. */
8552 if (HAVE_64BIT_SYMBOLS)
8553 {
8554 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8555 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8556 {
8557 relax_start (offset_expr.X_add_symbol);
8558 if (breg == 0)
8559 {
8560 macro_build (&offset_expr, s, fmt, treg,
8561 BFD_RELOC_GPREL16, mips_gp_register);
8562 }
8563 else
8564 {
8565 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8566 tempreg, breg, mips_gp_register);
8567 macro_build (&offset_expr, s, fmt, treg,
8568 BFD_RELOC_GPREL16, tempreg);
8569 }
8570 relax_switch ();
8571 }
8572
8573 if (used_at == 0 && mips_opts.at)
8574 {
8575 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8576 BFD_RELOC_MIPS_HIGHEST);
8577 macro_build (&offset_expr, "lui", LUI_FMT, AT,
8578 BFD_RELOC_HI16_S);
8579 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8580 tempreg, BFD_RELOC_MIPS_HIGHER);
8581 if (breg != 0)
8582 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
8583 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
8584 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8585 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
8586 tempreg);
8587 used_at = 1;
8588 }
8589 else
8590 {
8591 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8592 BFD_RELOC_MIPS_HIGHEST);
8593 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8594 tempreg, BFD_RELOC_MIPS_HIGHER);
8595 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8596 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8597 tempreg, BFD_RELOC_HI16_S);
8598 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8599 if (breg != 0)
8600 macro_build (NULL, "daddu", "d,v,t",
8601 tempreg, tempreg, breg);
8602 macro_build (&offset_expr, s, fmt, treg,
8603 BFD_RELOC_LO16, tempreg);
8604 }
8605
8606 if (mips_relax.sequence)
8607 relax_end ();
8608 break;
8609 }
8610
8611 if (breg == 0)
8612 {
8613 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8614 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8615 {
8616 relax_start (offset_expr.X_add_symbol);
8617 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
8618 mips_gp_register);
8619 relax_switch ();
8620 }
8621 macro_build_lui (&offset_expr, tempreg);
8622 macro_build (&offset_expr, s, fmt, treg,
8623 BFD_RELOC_LO16, tempreg);
8624 if (mips_relax.sequence)
8625 relax_end ();
8626 }
8627 else
8628 {
8629 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8630 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8631 {
8632 relax_start (offset_expr.X_add_symbol);
8633 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8634 tempreg, breg, mips_gp_register);
8635 macro_build (&offset_expr, s, fmt, treg,
8636 BFD_RELOC_GPREL16, tempreg);
8637 relax_switch ();
8638 }
8639 macro_build_lui (&offset_expr, tempreg);
8640 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8641 tempreg, tempreg, breg);
8642 macro_build (&offset_expr, s, fmt, treg,
8643 BFD_RELOC_LO16, tempreg);
8644 if (mips_relax.sequence)
8645 relax_end ();
8646 }
8647 }
8648 else if (!mips_big_got)
8649 {
8650 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
8651
8652 /* If this is a reference to an external symbol, we want
8653 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8654 nop
8655 <op> $treg,0($tempreg)
8656 Otherwise we want
8657 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8658 nop
8659 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8660 <op> $treg,0($tempreg)
8661
8662 For NewABI, we want
8663 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8664 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
8665
8666 If there is a base register, we add it to $tempreg before
8667 the <op>. If there is a constant, we stick it in the
8668 <op> instruction. We don't handle constants larger than
8669 16 bits, because we have no way to load the upper 16 bits
8670 (actually, we could handle them for the subset of cases
8671 in which we are not using $at). */
8672 gas_assert (offset_expr.X_op == O_symbol);
8673 if (HAVE_NEWABI)
8674 {
8675 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8676 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8677 if (breg != 0)
8678 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8679 tempreg, tempreg, breg);
8680 macro_build (&offset_expr, s, fmt, treg,
8681 BFD_RELOC_MIPS_GOT_OFST, tempreg);
8682 break;
8683 }
8684 expr1.X_add_number = offset_expr.X_add_number;
8685 offset_expr.X_add_number = 0;
8686 if (expr1.X_add_number < -0x8000
8687 || expr1.X_add_number >= 0x8000)
8688 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8689 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8690 lw_reloc_type, mips_gp_register);
8691 load_delay_nop ();
8692 relax_start (offset_expr.X_add_symbol);
8693 relax_switch ();
8694 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8695 tempreg, BFD_RELOC_LO16);
8696 relax_end ();
8697 if (breg != 0)
8698 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8699 tempreg, tempreg, breg);
8700 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8701 }
8702 else if (mips_big_got && !HAVE_NEWABI)
8703 {
8704 int gpdelay;
8705
8706 /* If this is a reference to an external symbol, we want
8707 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8708 addu $tempreg,$tempreg,$gp
8709 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8710 <op> $treg,0($tempreg)
8711 Otherwise we want
8712 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8713 nop
8714 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8715 <op> $treg,0($tempreg)
8716 If there is a base register, we add it to $tempreg before
8717 the <op>. If there is a constant, we stick it in the
8718 <op> instruction. We don't handle constants larger than
8719 16 bits, because we have no way to load the upper 16 bits
8720 (actually, we could handle them for the subset of cases
8721 in which we are not using $at). */
8722 gas_assert (offset_expr.X_op == O_symbol);
8723 expr1.X_add_number = offset_expr.X_add_number;
8724 offset_expr.X_add_number = 0;
8725 if (expr1.X_add_number < -0x8000
8726 || expr1.X_add_number >= 0x8000)
8727 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8728 gpdelay = reg_needs_delay (mips_gp_register);
8729 relax_start (offset_expr.X_add_symbol);
8730 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8731 BFD_RELOC_MIPS_GOT_HI16);
8732 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8733 mips_gp_register);
8734 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8735 BFD_RELOC_MIPS_GOT_LO16, tempreg);
8736 relax_switch ();
8737 if (gpdelay)
8738 macro_build (NULL, "nop", "");
8739 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8740 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8741 load_delay_nop ();
8742 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8743 tempreg, BFD_RELOC_LO16);
8744 relax_end ();
8745
8746 if (breg != 0)
8747 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8748 tempreg, tempreg, breg);
8749 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8750 }
8751 else if (mips_big_got && HAVE_NEWABI)
8752 {
8753 /* If this is a reference to an external symbol, we want
8754 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8755 add $tempreg,$tempreg,$gp
8756 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8757 <op> $treg,<ofst>($tempreg)
8758 Otherwise, for local symbols, we want:
8759 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8760 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
8761 gas_assert (offset_expr.X_op == O_symbol);
8762 expr1.X_add_number = offset_expr.X_add_number;
8763 offset_expr.X_add_number = 0;
8764 if (expr1.X_add_number < -0x8000
8765 || expr1.X_add_number >= 0x8000)
8766 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8767 relax_start (offset_expr.X_add_symbol);
8768 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8769 BFD_RELOC_MIPS_GOT_HI16);
8770 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8771 mips_gp_register);
8772 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8773 BFD_RELOC_MIPS_GOT_LO16, tempreg);
8774 if (breg != 0)
8775 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8776 tempreg, tempreg, breg);
8777 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8778
8779 relax_switch ();
8780 offset_expr.X_add_number = expr1.X_add_number;
8781 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8782 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8783 if (breg != 0)
8784 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8785 tempreg, tempreg, breg);
8786 macro_build (&offset_expr, s, fmt, treg,
8787 BFD_RELOC_MIPS_GOT_OFST, tempreg);
8788 relax_end ();
8789 }
8790 else
8791 abort ();
8792
8793 break;
8794
8795 case M_LI:
8796 case M_LI_S:
8797 load_register (treg, &imm_expr, 0);
8798 break;
8799
8800 case M_DLI:
8801 load_register (treg, &imm_expr, 1);
8802 break;
8803
8804 case M_LI_SS:
8805 if (imm_expr.X_op == O_constant)
8806 {
8807 used_at = 1;
8808 load_register (AT, &imm_expr, 0);
8809 macro_build (NULL, "mtc1", "t,G", AT, treg);
8810 break;
8811 }
8812 else
8813 {
8814 gas_assert (offset_expr.X_op == O_symbol
8815 && strcmp (segment_name (S_GET_SEGMENT
8816 (offset_expr.X_add_symbol)),
8817 ".lit4") == 0
8818 && offset_expr.X_add_number == 0);
8819 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
8820 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8821 break;
8822 }
8823
8824 case M_LI_D:
8825 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
8826 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
8827 order 32 bits of the value and the low order 32 bits are either
8828 zero or in OFFSET_EXPR. */
8829 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8830 {
8831 if (HAVE_64BIT_GPRS)
8832 load_register (treg, &imm_expr, 1);
8833 else
8834 {
8835 int hreg, lreg;
8836
8837 if (target_big_endian)
8838 {
8839 hreg = treg;
8840 lreg = treg + 1;
8841 }
8842 else
8843 {
8844 hreg = treg + 1;
8845 lreg = treg;
8846 }
8847
8848 if (hreg <= 31)
8849 load_register (hreg, &imm_expr, 0);
8850 if (lreg <= 31)
8851 {
8852 if (offset_expr.X_op == O_absent)
8853 move_register (lreg, 0);
8854 else
8855 {
8856 gas_assert (offset_expr.X_op == O_constant);
8857 load_register (lreg, &offset_expr, 0);
8858 }
8859 }
8860 }
8861 break;
8862 }
8863
8864 /* We know that sym is in the .rdata section. First we get the
8865 upper 16 bits of the address. */
8866 if (mips_pic == NO_PIC)
8867 {
8868 macro_build_lui (&offset_expr, AT);
8869 used_at = 1;
8870 }
8871 else
8872 {
8873 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8874 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8875 used_at = 1;
8876 }
8877
8878 /* Now we load the register(s). */
8879 if (HAVE_64BIT_GPRS)
8880 {
8881 used_at = 1;
8882 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8883 }
8884 else
8885 {
8886 used_at = 1;
8887 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8888 if (treg != RA)
8889 {
8890 /* FIXME: How in the world do we deal with the possible
8891 overflow here? */
8892 offset_expr.X_add_number += 4;
8893 macro_build (&offset_expr, "lw", "t,o(b)",
8894 treg + 1, BFD_RELOC_LO16, AT);
8895 }
8896 }
8897 break;
8898
8899 case M_LI_DD:
8900 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
8901 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
8902 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
8903 the value and the low order 32 bits are either zero or in
8904 OFFSET_EXPR. */
8905 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8906 {
8907 used_at = 1;
8908 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
8909 if (HAVE_64BIT_FPRS)
8910 {
8911 gas_assert (HAVE_64BIT_GPRS);
8912 macro_build (NULL, "dmtc1", "t,S", AT, treg);
8913 }
8914 else
8915 {
8916 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
8917 if (offset_expr.X_op == O_absent)
8918 macro_build (NULL, "mtc1", "t,G", 0, treg);
8919 else
8920 {
8921 gas_assert (offset_expr.X_op == O_constant);
8922 load_register (AT, &offset_expr, 0);
8923 macro_build (NULL, "mtc1", "t,G", AT, treg);
8924 }
8925 }
8926 break;
8927 }
8928
8929 gas_assert (offset_expr.X_op == O_symbol
8930 && offset_expr.X_add_number == 0);
8931 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
8932 if (strcmp (s, ".lit8") == 0)
8933 {
8934 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
8935 {
8936 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
8937 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8938 break;
8939 }
8940 breg = mips_gp_register;
8941 r = BFD_RELOC_MIPS_LITERAL;
8942 goto dob;
8943 }
8944 else
8945 {
8946 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8947 used_at = 1;
8948 if (mips_pic != NO_PIC)
8949 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8950 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8951 else
8952 {
8953 /* FIXME: This won't work for a 64 bit address. */
8954 macro_build_lui (&offset_expr, AT);
8955 }
8956
8957 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
8958 {
8959 macro_build (&offset_expr, "ldc1", "T,o(b)",
8960 treg, BFD_RELOC_LO16, AT);
8961 break;
8962 }
8963 breg = AT;
8964 r = BFD_RELOC_LO16;
8965 goto dob;
8966 }
8967
8968 case M_L_DOB:
8969 /* Even on a big endian machine $fn comes before $fn+1. We have
8970 to adjust when loading from memory. */
8971 r = BFD_RELOC_LO16;
8972 dob:
8973 gas_assert (!mips_opts.micromips);
8974 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
8975 macro_build (&offset_expr, "lwc1", "T,o(b)",
8976 target_big_endian ? treg + 1 : treg, r, breg);
8977 /* FIXME: A possible overflow which I don't know how to deal
8978 with. */
8979 offset_expr.X_add_number += 4;
8980 macro_build (&offset_expr, "lwc1", "T,o(b)",
8981 target_big_endian ? treg : treg + 1, r, breg);
8982 break;
8983
8984 case M_S_DOB:
8985 gas_assert (!mips_opts.micromips);
8986 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
8987 /* Even on a big endian machine $fn comes before $fn+1. We have
8988 to adjust when storing to memory. */
8989 macro_build (&offset_expr, "swc1", "T,o(b)",
8990 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
8991 offset_expr.X_add_number += 4;
8992 macro_build (&offset_expr, "swc1", "T,o(b)",
8993 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8994 break;
8995
8996 case M_L_DAB:
8997 gas_assert (!mips_opts.micromips);
8998 /*
8999 * The MIPS assembler seems to check for X_add_number not
9000 * being double aligned and generating:
9001 * lui at,%hi(foo+1)
9002 * addu at,at,v1
9003 * addiu at,at,%lo(foo+1)
9004 * lwc1 f2,0(at)
9005 * lwc1 f3,4(at)
9006 * But, the resulting address is the same after relocation so why
9007 * generate the extra instruction?
9008 */
9009 /* Itbl support may require additional care here. */
9010 coproc = 1;
9011 fmt = "T,o(b)";
9012 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
9013 {
9014 s = "ldc1";
9015 goto ld_st;
9016 }
9017 s = "lwc1";
9018 goto ldd_std;
9019
9020 case M_S_DAB:
9021 gas_assert (!mips_opts.micromips);
9022 /* Itbl support may require additional care here. */
9023 coproc = 1;
9024 fmt = "T,o(b)";
9025 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
9026 {
9027 s = "sdc1";
9028 goto ld_st;
9029 }
9030 s = "swc1";
9031 goto ldd_std;
9032
9033 case M_LQ_AB:
9034 fmt = "t,o(b)";
9035 s = "lq";
9036 goto ld;
9037
9038 case M_SQ_AB:
9039 fmt = "t,o(b)";
9040 s = "sq";
9041 goto ld_st;
9042
9043 case M_LD_AB:
9044 fmt = "t,o(b)";
9045 if (HAVE_64BIT_GPRS)
9046 {
9047 s = "ld";
9048 goto ld;
9049 }
9050 s = "lw";
9051 goto ldd_std;
9052
9053 case M_SD_AB:
9054 fmt = "t,o(b)";
9055 if (HAVE_64BIT_GPRS)
9056 {
9057 s = "sd";
9058 goto ld_st;
9059 }
9060 s = "sw";
9061
9062 ldd_std:
9063 if (offset_expr.X_op != O_symbol
9064 && offset_expr.X_op != O_constant)
9065 {
9066 as_bad (_("Expression too complex"));
9067 offset_expr.X_op = O_constant;
9068 }
9069
9070 if (HAVE_32BIT_ADDRESSES
9071 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
9072 {
9073 char value [32];
9074
9075 sprintf_vma (value, offset_expr.X_add_number);
9076 as_bad (_("Number (0x%s) larger than 32 bits"), value);
9077 }
9078
9079 /* Even on a big endian machine $fn comes before $fn+1. We have
9080 to adjust when loading from memory. We set coproc if we must
9081 load $fn+1 first. */
9082 /* Itbl support may require additional care here. */
9083 if (!target_big_endian)
9084 coproc = 0;
9085
9086 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
9087 {
9088 /* If this is a reference to a GP relative symbol, we want
9089 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
9090 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
9091 If we have a base register, we use this
9092 addu $at,$breg,$gp
9093 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
9094 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
9095 If this is not a GP relative symbol, we want
9096 lui $at,<sym> (BFD_RELOC_HI16_S)
9097 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9098 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9099 If there is a base register, we add it to $at after the
9100 lui instruction. If there is a constant, we always use
9101 the last case. */
9102 if (offset_expr.X_op == O_symbol
9103 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9104 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9105 {
9106 relax_start (offset_expr.X_add_symbol);
9107 if (breg == 0)
9108 {
9109 tempreg = mips_gp_register;
9110 }
9111 else
9112 {
9113 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9114 AT, breg, mips_gp_register);
9115 tempreg = AT;
9116 used_at = 1;
9117 }
9118
9119 /* Itbl support may require additional care here. */
9120 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9121 BFD_RELOC_GPREL16, tempreg);
9122 offset_expr.X_add_number += 4;
9123
9124 /* Set mips_optimize to 2 to avoid inserting an
9125 undesired nop. */
9126 hold_mips_optimize = mips_optimize;
9127 mips_optimize = 2;
9128 /* Itbl support may require additional care here. */
9129 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9130 BFD_RELOC_GPREL16, tempreg);
9131 mips_optimize = hold_mips_optimize;
9132
9133 relax_switch ();
9134
9135 offset_expr.X_add_number -= 4;
9136 }
9137 used_at = 1;
9138 macro_build_lui (&offset_expr, AT);
9139 if (breg != 0)
9140 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9141 /* Itbl support may require additional care here. */
9142 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9143 BFD_RELOC_LO16, AT);
9144 /* FIXME: How do we handle overflow here? */
9145 offset_expr.X_add_number += 4;
9146 /* Itbl support may require additional care here. */
9147 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9148 BFD_RELOC_LO16, AT);
9149 if (mips_relax.sequence)
9150 relax_end ();
9151 }
9152 else if (!mips_big_got)
9153 {
9154 /* If this is a reference to an external symbol, we want
9155 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9156 nop
9157 <op> $treg,0($at)
9158 <op> $treg+1,4($at)
9159 Otherwise we want
9160 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9161 nop
9162 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9163 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9164 If there is a base register we add it to $at before the
9165 lwc1 instructions. If there is a constant we include it
9166 in the lwc1 instructions. */
9167 used_at = 1;
9168 expr1.X_add_number = offset_expr.X_add_number;
9169 if (expr1.X_add_number < -0x8000
9170 || expr1.X_add_number >= 0x8000 - 4)
9171 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9172 load_got_offset (AT, &offset_expr);
9173 load_delay_nop ();
9174 if (breg != 0)
9175 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9176
9177 /* Set mips_optimize to 2 to avoid inserting an undesired
9178 nop. */
9179 hold_mips_optimize = mips_optimize;
9180 mips_optimize = 2;
9181
9182 /* Itbl support may require additional care here. */
9183 relax_start (offset_expr.X_add_symbol);
9184 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9185 BFD_RELOC_LO16, AT);
9186 expr1.X_add_number += 4;
9187 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9188 BFD_RELOC_LO16, AT);
9189 relax_switch ();
9190 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9191 BFD_RELOC_LO16, AT);
9192 offset_expr.X_add_number += 4;
9193 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9194 BFD_RELOC_LO16, AT);
9195 relax_end ();
9196
9197 mips_optimize = hold_mips_optimize;
9198 }
9199 else if (mips_big_got)
9200 {
9201 int gpdelay;
9202
9203 /* If this is a reference to an external symbol, we want
9204 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9205 addu $at,$at,$gp
9206 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
9207 nop
9208 <op> $treg,0($at)
9209 <op> $treg+1,4($at)
9210 Otherwise we want
9211 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9212 nop
9213 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9214 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9215 If there is a base register we add it to $at before the
9216 lwc1 instructions. If there is a constant we include it
9217 in the lwc1 instructions. */
9218 used_at = 1;
9219 expr1.X_add_number = offset_expr.X_add_number;
9220 offset_expr.X_add_number = 0;
9221 if (expr1.X_add_number < -0x8000
9222 || expr1.X_add_number >= 0x8000 - 4)
9223 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9224 gpdelay = reg_needs_delay (mips_gp_register);
9225 relax_start (offset_expr.X_add_symbol);
9226 macro_build (&offset_expr, "lui", LUI_FMT,
9227 AT, BFD_RELOC_MIPS_GOT_HI16);
9228 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9229 AT, AT, mips_gp_register);
9230 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9231 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
9232 load_delay_nop ();
9233 if (breg != 0)
9234 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9235 /* Itbl support may require additional care here. */
9236 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9237 BFD_RELOC_LO16, AT);
9238 expr1.X_add_number += 4;
9239
9240 /* Set mips_optimize to 2 to avoid inserting an undesired
9241 nop. */
9242 hold_mips_optimize = mips_optimize;
9243 mips_optimize = 2;
9244 /* Itbl support may require additional care here. */
9245 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9246 BFD_RELOC_LO16, AT);
9247 mips_optimize = hold_mips_optimize;
9248 expr1.X_add_number -= 4;
9249
9250 relax_switch ();
9251 offset_expr.X_add_number = expr1.X_add_number;
9252 if (gpdelay)
9253 macro_build (NULL, "nop", "");
9254 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9255 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9256 load_delay_nop ();
9257 if (breg != 0)
9258 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9259 /* Itbl support may require additional care here. */
9260 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9261 BFD_RELOC_LO16, AT);
9262 offset_expr.X_add_number += 4;
9263
9264 /* Set mips_optimize to 2 to avoid inserting an undesired
9265 nop. */
9266 hold_mips_optimize = mips_optimize;
9267 mips_optimize = 2;
9268 /* Itbl support may require additional care here. */
9269 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9270 BFD_RELOC_LO16, AT);
9271 mips_optimize = hold_mips_optimize;
9272 relax_end ();
9273 }
9274 else
9275 abort ();
9276
9277 break;
9278
9279 case M_LD_OB:
9280 s = HAVE_64BIT_GPRS ? "ld" : "lw";
9281 goto sd_ob;
9282 case M_SD_OB:
9283 s = HAVE_64BIT_GPRS ? "sd" : "sw";
9284 sd_ob:
9285 macro_build (&offset_expr, s, "t,o(b)", treg,
9286 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9287 breg);
9288 if (!HAVE_64BIT_GPRS)
9289 {
9290 offset_expr.X_add_number += 4;
9291 macro_build (&offset_expr, s, "t,o(b)", treg + 1,
9292 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9293 breg);
9294 }
9295 break;
9296
9297
9298 case M_SAA_AB:
9299 ab = 1;
9300 case M_SAA_OB:
9301 s = "saa";
9302 off0 = 1;
9303 fmt = "t,(b)";
9304 goto ld_st;
9305 case M_SAAD_AB:
9306 ab = 1;
9307 case M_SAAD_OB:
9308 s = "saad";
9309 off0 = 1;
9310 fmt = "t,(b)";
9311 goto ld_st;
9312
9313 /* New code added to support COPZ instructions.
9314 This code builds table entries out of the macros in mip_opcodes.
9315 R4000 uses interlocks to handle coproc delays.
9316 Other chips (like the R3000) require nops to be inserted for delays.
9317
9318 FIXME: Currently, we require that the user handle delays.
9319 In order to fill delay slots for non-interlocked chips,
9320 we must have a way to specify delays based on the coprocessor.
9321 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9322 What are the side-effects of the cop instruction?
9323 What cache support might we have and what are its effects?
9324 Both coprocessor & memory require delays. how long???
9325 What registers are read/set/modified?
9326
9327 If an itbl is provided to interpret cop instructions,
9328 this knowledge can be encoded in the itbl spec. */
9329
9330 case M_COP0:
9331 s = "c0";
9332 goto copz;
9333 case M_COP1:
9334 s = "c1";
9335 goto copz;
9336 case M_COP2:
9337 s = "c2";
9338 goto copz;
9339 case M_COP3:
9340 s = "c3";
9341 copz:
9342 gas_assert (!mips_opts.micromips);
9343 /* For now we just do C (same as Cz). The parameter will be
9344 stored in insn_opcode by mips_ip. */
9345 macro_build (NULL, s, "C", ip->insn_opcode);
9346 break;
9347
9348 case M_MOVE:
9349 move_register (dreg, sreg);
9350 break;
9351
9352 case M_DMUL:
9353 dbl = 1;
9354 case M_MUL:
9355 if (mips_opts.arch == CPU_R5900)
9356 {
9357 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", dreg, sreg, treg);
9358 }
9359 else
9360 {
9361 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
9362 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9363 }
9364 break;
9365
9366 case M_DMUL_I:
9367 dbl = 1;
9368 case M_MUL_I:
9369 /* The MIPS assembler some times generates shifts and adds. I'm
9370 not trying to be that fancy. GCC should do this for us
9371 anyway. */
9372 used_at = 1;
9373 load_register (AT, &imm_expr, dbl);
9374 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
9375 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9376 break;
9377
9378 case M_DMULO_I:
9379 dbl = 1;
9380 case M_MULO_I:
9381 imm = 1;
9382 goto do_mulo;
9383
9384 case M_DMULO:
9385 dbl = 1;
9386 case M_MULO:
9387 do_mulo:
9388 start_noreorder ();
9389 used_at = 1;
9390 if (imm)
9391 load_register (AT, &imm_expr, dbl);
9392 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
9393 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9394 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9395 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9396 if (mips_trap)
9397 macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
9398 else
9399 {
9400 if (mips_opts.micromips)
9401 micromips_label_expr (&label_expr);
9402 else
9403 label_expr.X_add_number = 8;
9404 macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
9405 macro_build (NULL, "nop", "");
9406 macro_build (NULL, "break", BRK_FMT, 6);
9407 if (mips_opts.micromips)
9408 micromips_add_label ();
9409 }
9410 end_noreorder ();
9411 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9412 break;
9413
9414 case M_DMULOU_I:
9415 dbl = 1;
9416 case M_MULOU_I:
9417 imm = 1;
9418 goto do_mulou;
9419
9420 case M_DMULOU:
9421 dbl = 1;
9422 case M_MULOU:
9423 do_mulou:
9424 start_noreorder ();
9425 used_at = 1;
9426 if (imm)
9427 load_register (AT, &imm_expr, dbl);
9428 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
9429 sreg, imm ? AT : treg);
9430 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9431 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9432 if (mips_trap)
9433 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
9434 else
9435 {
9436 if (mips_opts.micromips)
9437 micromips_label_expr (&label_expr);
9438 else
9439 label_expr.X_add_number = 8;
9440 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
9441 macro_build (NULL, "nop", "");
9442 macro_build (NULL, "break", BRK_FMT, 6);
9443 if (mips_opts.micromips)
9444 micromips_add_label ();
9445 }
9446 end_noreorder ();
9447 break;
9448
9449 case M_DROL:
9450 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9451 {
9452 if (dreg == sreg)
9453 {
9454 tempreg = AT;
9455 used_at = 1;
9456 }
9457 else
9458 {
9459 tempreg = dreg;
9460 }
9461 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9462 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
9463 break;
9464 }
9465 used_at = 1;
9466 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9467 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9468 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9469 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9470 break;
9471
9472 case M_ROL:
9473 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9474 {
9475 if (dreg == sreg)
9476 {
9477 tempreg = AT;
9478 used_at = 1;
9479 }
9480 else
9481 {
9482 tempreg = dreg;
9483 }
9484 macro_build (NULL, "negu", "d,w", tempreg, treg);
9485 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
9486 break;
9487 }
9488 used_at = 1;
9489 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9490 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9491 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9492 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9493 break;
9494
9495 case M_DROL_I:
9496 {
9497 unsigned int rot;
9498 char *l;
9499 char *rr;
9500
9501 if (imm_expr.X_op != O_constant)
9502 as_bad (_("Improper rotate count"));
9503 rot = imm_expr.X_add_number & 0x3f;
9504 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9505 {
9506 rot = (64 - rot) & 0x3f;
9507 if (rot >= 32)
9508 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9509 else
9510 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9511 break;
9512 }
9513 if (rot == 0)
9514 {
9515 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9516 break;
9517 }
9518 l = (rot < 0x20) ? "dsll" : "dsll32";
9519 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
9520 rot &= 0x1f;
9521 used_at = 1;
9522 macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9523 macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9524 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9525 }
9526 break;
9527
9528 case M_ROL_I:
9529 {
9530 unsigned int rot;
9531
9532 if (imm_expr.X_op != O_constant)
9533 as_bad (_("Improper rotate count"));
9534 rot = imm_expr.X_add_number & 0x1f;
9535 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9536 {
9537 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
9538 break;
9539 }
9540 if (rot == 0)
9541 {
9542 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9543 break;
9544 }
9545 used_at = 1;
9546 macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9547 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9548 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9549 }
9550 break;
9551
9552 case M_DROR:
9553 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9554 {
9555 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
9556 break;
9557 }
9558 used_at = 1;
9559 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9560 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9561 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9562 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9563 break;
9564
9565 case M_ROR:
9566 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9567 {
9568 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
9569 break;
9570 }
9571 used_at = 1;
9572 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9573 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
9574 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
9575 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9576 break;
9577
9578 case M_DROR_I:
9579 {
9580 unsigned int rot;
9581 char *l;
9582 char *rr;
9583
9584 if (imm_expr.X_op != O_constant)
9585 as_bad (_("Improper rotate count"));
9586 rot = imm_expr.X_add_number & 0x3f;
9587 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9588 {
9589 if (rot >= 32)
9590 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9591 else
9592 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9593 break;
9594 }
9595 if (rot == 0)
9596 {
9597 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9598 break;
9599 }
9600 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
9601 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
9602 rot &= 0x1f;
9603 used_at = 1;
9604 macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
9605 macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9606 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9607 }
9608 break;
9609
9610 case M_ROR_I:
9611 {
9612 unsigned int rot;
9613
9614 if (imm_expr.X_op != O_constant)
9615 as_bad (_("Improper rotate count"));
9616 rot = imm_expr.X_add_number & 0x1f;
9617 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9618 {
9619 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
9620 break;
9621 }
9622 if (rot == 0)
9623 {
9624 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9625 break;
9626 }
9627 used_at = 1;
9628 macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
9629 macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9630 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9631 }
9632 break;
9633
9634 case M_SEQ:
9635 if (sreg == 0)
9636 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
9637 else if (treg == 0)
9638 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9639 else
9640 {
9641 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9642 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9643 }
9644 break;
9645
9646 case M_SEQ_I:
9647 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9648 {
9649 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9650 break;
9651 }
9652 if (sreg == 0)
9653 {
9654 as_warn (_("Instruction %s: result is always false"),
9655 ip->insn_mo->name);
9656 move_register (dreg, 0);
9657 break;
9658 }
9659 if (CPU_HAS_SEQ (mips_opts.arch)
9660 && -512 <= imm_expr.X_add_number
9661 && imm_expr.X_add_number < 512)
9662 {
9663 macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
9664 (int) imm_expr.X_add_number);
9665 break;
9666 }
9667 if (imm_expr.X_op == O_constant
9668 && imm_expr.X_add_number >= 0
9669 && imm_expr.X_add_number < 0x10000)
9670 {
9671 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
9672 }
9673 else if (imm_expr.X_op == O_constant
9674 && imm_expr.X_add_number > -0x8000
9675 && imm_expr.X_add_number < 0)
9676 {
9677 imm_expr.X_add_number = -imm_expr.X_add_number;
9678 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
9679 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9680 }
9681 else if (CPU_HAS_SEQ (mips_opts.arch))
9682 {
9683 used_at = 1;
9684 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9685 macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
9686 break;
9687 }
9688 else
9689 {
9690 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9691 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
9692 used_at = 1;
9693 }
9694 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9695 break;
9696
9697 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
9698 s = "slt";
9699 goto sge;
9700 case M_SGEU:
9701 s = "sltu";
9702 sge:
9703 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
9704 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9705 break;
9706
9707 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
9708 case M_SGEU_I:
9709 if (imm_expr.X_op == O_constant
9710 && imm_expr.X_add_number >= -0x8000
9711 && imm_expr.X_add_number < 0x8000)
9712 {
9713 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
9714 dreg, sreg, BFD_RELOC_LO16);
9715 }
9716 else
9717 {
9718 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9719 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
9720 dreg, sreg, AT);
9721 used_at = 1;
9722 }
9723 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9724 break;
9725
9726 case M_SGT: /* sreg > treg <==> treg < sreg */
9727 s = "slt";
9728 goto sgt;
9729 case M_SGTU:
9730 s = "sltu";
9731 sgt:
9732 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9733 break;
9734
9735 case M_SGT_I: /* sreg > I <==> I < sreg */
9736 s = "slt";
9737 goto sgti;
9738 case M_SGTU_I:
9739 s = "sltu";
9740 sgti:
9741 used_at = 1;
9742 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9743 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9744 break;
9745
9746 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
9747 s = "slt";
9748 goto sle;
9749 case M_SLEU:
9750 s = "sltu";
9751 sle:
9752 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9753 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9754 break;
9755
9756 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
9757 s = "slt";
9758 goto slei;
9759 case M_SLEU_I:
9760 s = "sltu";
9761 slei:
9762 used_at = 1;
9763 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9764 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9765 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9766 break;
9767
9768 case M_SLT_I:
9769 if (imm_expr.X_op == O_constant
9770 && imm_expr.X_add_number >= -0x8000
9771 && imm_expr.X_add_number < 0x8000)
9772 {
9773 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9774 break;
9775 }
9776 used_at = 1;
9777 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9778 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
9779 break;
9780
9781 case M_SLTU_I:
9782 if (imm_expr.X_op == O_constant
9783 && imm_expr.X_add_number >= -0x8000
9784 && imm_expr.X_add_number < 0x8000)
9785 {
9786 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
9787 BFD_RELOC_LO16);
9788 break;
9789 }
9790 used_at = 1;
9791 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9792 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
9793 break;
9794
9795 case M_SNE:
9796 if (sreg == 0)
9797 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
9798 else if (treg == 0)
9799 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
9800 else
9801 {
9802 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9803 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
9804 }
9805 break;
9806
9807 case M_SNE_I:
9808 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9809 {
9810 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
9811 break;
9812 }
9813 if (sreg == 0)
9814 {
9815 as_warn (_("Instruction %s: result is always true"),
9816 ip->insn_mo->name);
9817 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
9818 dreg, 0, BFD_RELOC_LO16);
9819 break;
9820 }
9821 if (CPU_HAS_SEQ (mips_opts.arch)
9822 && -512 <= imm_expr.X_add_number
9823 && imm_expr.X_add_number < 512)
9824 {
9825 macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
9826 (int) imm_expr.X_add_number);
9827 break;
9828 }
9829 if (imm_expr.X_op == O_constant
9830 && imm_expr.X_add_number >= 0
9831 && imm_expr.X_add_number < 0x10000)
9832 {
9833 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
9834 }
9835 else if (imm_expr.X_op == O_constant
9836 && imm_expr.X_add_number > -0x8000
9837 && imm_expr.X_add_number < 0)
9838 {
9839 imm_expr.X_add_number = -imm_expr.X_add_number;
9840 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
9841 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9842 }
9843 else if (CPU_HAS_SEQ (mips_opts.arch))
9844 {
9845 used_at = 1;
9846 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9847 macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
9848 break;
9849 }
9850 else
9851 {
9852 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9853 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
9854 used_at = 1;
9855 }
9856 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
9857 break;
9858
9859 case M_SUB_I:
9860 s = "addi";
9861 s2 = "sub";
9862 goto do_subi;
9863 case M_SUBU_I:
9864 s = "addiu";
9865 s2 = "subu";
9866 goto do_subi;
9867 case M_DSUB_I:
9868 dbl = 1;
9869 s = "daddi";
9870 s2 = "dsub";
9871 if (!mips_opts.micromips)
9872 goto do_subi;
9873 if (imm_expr.X_op == O_constant
9874 && imm_expr.X_add_number > -0x200
9875 && imm_expr.X_add_number <= 0x200)
9876 {
9877 macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
9878 break;
9879 }
9880 goto do_subi_i;
9881 case M_DSUBU_I:
9882 dbl = 1;
9883 s = "daddiu";
9884 s2 = "dsubu";
9885 do_subi:
9886 if (imm_expr.X_op == O_constant
9887 && imm_expr.X_add_number > -0x8000
9888 && imm_expr.X_add_number <= 0x8000)
9889 {
9890 imm_expr.X_add_number = -imm_expr.X_add_number;
9891 macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9892 break;
9893 }
9894 do_subi_i:
9895 used_at = 1;
9896 load_register (AT, &imm_expr, dbl);
9897 macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
9898 break;
9899
9900 case M_TEQ_I:
9901 s = "teq";
9902 goto trap;
9903 case M_TGE_I:
9904 s = "tge";
9905 goto trap;
9906 case M_TGEU_I:
9907 s = "tgeu";
9908 goto trap;
9909 case M_TLT_I:
9910 s = "tlt";
9911 goto trap;
9912 case M_TLTU_I:
9913 s = "tltu";
9914 goto trap;
9915 case M_TNE_I:
9916 s = "tne";
9917 trap:
9918 used_at = 1;
9919 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9920 macro_build (NULL, s, "s,t", sreg, AT);
9921 break;
9922
9923 case M_TRUNCWS:
9924 case M_TRUNCWD:
9925 gas_assert (!mips_opts.micromips);
9926 gas_assert (mips_opts.isa == ISA_MIPS1);
9927 used_at = 1;
9928 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
9929 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
9930
9931 /*
9932 * Is the double cfc1 instruction a bug in the mips assembler;
9933 * or is there a reason for it?
9934 */
9935 start_noreorder ();
9936 macro_build (NULL, "cfc1", "t,G", treg, RA);
9937 macro_build (NULL, "cfc1", "t,G", treg, RA);
9938 macro_build (NULL, "nop", "");
9939 expr1.X_add_number = 3;
9940 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
9941 expr1.X_add_number = 2;
9942 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
9943 macro_build (NULL, "ctc1", "t,G", AT, RA);
9944 macro_build (NULL, "nop", "");
9945 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
9946 dreg, sreg);
9947 macro_build (NULL, "ctc1", "t,G", treg, RA);
9948 macro_build (NULL, "nop", "");
9949 end_noreorder ();
9950 break;
9951
9952 case M_ULH_A:
9953 ab = 1;
9954 case M_ULH:
9955 s = "lb";
9956 s2 = "lbu";
9957 off = 1;
9958 goto uld_st;
9959 case M_ULHU_A:
9960 ab = 1;
9961 case M_ULHU:
9962 s = "lbu";
9963 s2 = "lbu";
9964 off = 1;
9965 goto uld_st;
9966 case M_ULW_A:
9967 ab = 1;
9968 case M_ULW:
9969 s = "lwl";
9970 s2 = "lwr";
9971 off12 = mips_opts.micromips;
9972 off = 3;
9973 goto uld_st;
9974 case M_ULD_A:
9975 ab = 1;
9976 case M_ULD:
9977 s = "ldl";
9978 s2 = "ldr";
9979 off12 = mips_opts.micromips;
9980 off = 7;
9981 goto uld_st;
9982 case M_USH_A:
9983 ab = 1;
9984 case M_USH:
9985 s = "sb";
9986 s2 = "sb";
9987 off = 1;
9988 ust = 1;
9989 goto uld_st;
9990 case M_USW_A:
9991 ab = 1;
9992 case M_USW:
9993 s = "swl";
9994 s2 = "swr";
9995 off12 = mips_opts.micromips;
9996 off = 3;
9997 ust = 1;
9998 goto uld_st;
9999 case M_USD_A:
10000 ab = 1;
10001 case M_USD:
10002 s = "sdl";
10003 s2 = "sdr";
10004 off12 = mips_opts.micromips;
10005 off = 7;
10006 ust = 1;
10007
10008 uld_st:
10009 if (!ab && offset_expr.X_add_number >= 0x8000 - off)
10010 as_bad (_("Operand overflow"));
10011
10012 ep = &offset_expr;
10013 expr1.X_add_number = 0;
10014 if (ab)
10015 {
10016 used_at = 1;
10017 tempreg = AT;
10018 load_address (tempreg, ep, &used_at);
10019 if (breg != 0)
10020 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10021 tempreg, tempreg, breg);
10022 breg = tempreg;
10023 tempreg = treg;
10024 ep = &expr1;
10025 }
10026 else if (off12
10027 && (offset_expr.X_op != O_constant
10028 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
10029 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
10030 {
10031 used_at = 1;
10032 tempreg = AT;
10033 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
10034 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10035 breg = tempreg;
10036 tempreg = treg;
10037 ep = &expr1;
10038 }
10039 else if (!ust && treg == breg)
10040 {
10041 used_at = 1;
10042 tempreg = AT;
10043 }
10044 else
10045 tempreg = treg;
10046
10047 if (off == 1)
10048 goto ulh_sh;
10049
10050 if (!target_big_endian)
10051 ep->X_add_number += off;
10052 if (!off12)
10053 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10054 else
10055 macro_build (NULL, s, "t,~(b)",
10056 tempreg, (unsigned long) ep->X_add_number, breg);
10057
10058 if (!target_big_endian)
10059 ep->X_add_number -= off;
10060 else
10061 ep->X_add_number += off;
10062 if (!off12)
10063 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10064 else
10065 macro_build (NULL, s2, "t,~(b)",
10066 tempreg, (unsigned long) ep->X_add_number, breg);
10067
10068 /* If necessary, move the result in tempreg to the final destination. */
10069 if (!ust && treg != tempreg)
10070 {
10071 /* Protect second load's delay slot. */
10072 load_delay_nop ();
10073 move_register (treg, tempreg);
10074 }
10075 break;
10076
10077 ulh_sh:
10078 used_at = 1;
10079 if (target_big_endian == ust)
10080 ep->X_add_number += off;
10081 tempreg = ust || ab ? treg : AT;
10082 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10083
10084 /* For halfword transfers we need a temporary register to shuffle
10085 bytes. Unfortunately for M_USH_A we have none available before
10086 the next store as AT holds the base address. We deal with this
10087 case by clobbering TREG and then restoring it as with ULH. */
10088 tempreg = ust == ab ? treg : AT;
10089 if (ust)
10090 macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10091
10092 if (target_big_endian == ust)
10093 ep->X_add_number -= off;
10094 else
10095 ep->X_add_number += off;
10096 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10097
10098 /* For M_USH_A re-retrieve the LSB. */
10099 if (ust && ab)
10100 {
10101 if (target_big_endian)
10102 ep->X_add_number += off;
10103 else
10104 ep->X_add_number -= off;
10105 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10106 }
10107 /* For ULH and M_USH_A OR the LSB in. */
10108 if (!ust || ab)
10109 {
10110 tempreg = !ab ? AT : treg;
10111 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10112 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10113 }
10114 break;
10115
10116 default:
10117 /* FIXME: Check if this is one of the itbl macros, since they
10118 are added dynamically. */
10119 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10120 break;
10121 }
10122 if (!mips_opts.at && used_at)
10123 as_bad (_("Macro used $at after \".set noat\""));
10124 }
10125
10126 /* Implement macros in mips16 mode. */
10127
10128 static void
10129 mips16_macro (struct mips_cl_insn *ip)
10130 {
10131 int mask;
10132 int xreg, yreg, zreg, tmp;
10133 expressionS expr1;
10134 int dbl;
10135 const char *s, *s2, *s3;
10136
10137 mask = ip->insn_mo->mask;
10138
10139 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10140 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10141 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
10142
10143 expr1.X_op = O_constant;
10144 expr1.X_op_symbol = NULL;
10145 expr1.X_add_symbol = NULL;
10146 expr1.X_add_number = 1;
10147
10148 dbl = 0;
10149
10150 switch (mask)
10151 {
10152 default:
10153 abort ();
10154
10155 case M_DDIV_3:
10156 dbl = 1;
10157 case M_DIV_3:
10158 s = "mflo";
10159 goto do_div3;
10160 case M_DREM_3:
10161 dbl = 1;
10162 case M_REM_3:
10163 s = "mfhi";
10164 do_div3:
10165 start_noreorder ();
10166 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
10167 expr1.X_add_number = 2;
10168 macro_build (&expr1, "bnez", "x,p", yreg);
10169 macro_build (NULL, "break", "6", 7);
10170
10171 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10172 since that causes an overflow. We should do that as well,
10173 but I don't see how to do the comparisons without a temporary
10174 register. */
10175 end_noreorder ();
10176 macro_build (NULL, s, "x", zreg);
10177 break;
10178
10179 case M_DIVU_3:
10180 s = "divu";
10181 s2 = "mflo";
10182 goto do_divu3;
10183 case M_REMU_3:
10184 s = "divu";
10185 s2 = "mfhi";
10186 goto do_divu3;
10187 case M_DDIVU_3:
10188 s = "ddivu";
10189 s2 = "mflo";
10190 goto do_divu3;
10191 case M_DREMU_3:
10192 s = "ddivu";
10193 s2 = "mfhi";
10194 do_divu3:
10195 start_noreorder ();
10196 macro_build (NULL, s, "0,x,y", xreg, yreg);
10197 expr1.X_add_number = 2;
10198 macro_build (&expr1, "bnez", "x,p", yreg);
10199 macro_build (NULL, "break", "6", 7);
10200 end_noreorder ();
10201 macro_build (NULL, s2, "x", zreg);
10202 break;
10203
10204 case M_DMUL:
10205 dbl = 1;
10206 case M_MUL:
10207 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10208 macro_build (NULL, "mflo", "x", zreg);
10209 break;
10210
10211 case M_DSUBU_I:
10212 dbl = 1;
10213 goto do_subu;
10214 case M_SUBU_I:
10215 do_subu:
10216 if (imm_expr.X_op != O_constant)
10217 as_bad (_("Unsupported large constant"));
10218 imm_expr.X_add_number = -imm_expr.X_add_number;
10219 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
10220 break;
10221
10222 case M_SUBU_I_2:
10223 if (imm_expr.X_op != O_constant)
10224 as_bad (_("Unsupported large constant"));
10225 imm_expr.X_add_number = -imm_expr.X_add_number;
10226 macro_build (&imm_expr, "addiu", "x,k", xreg);
10227 break;
10228
10229 case M_DSUBU_I_2:
10230 if (imm_expr.X_op != O_constant)
10231 as_bad (_("Unsupported large constant"));
10232 imm_expr.X_add_number = -imm_expr.X_add_number;
10233 macro_build (&imm_expr, "daddiu", "y,j", yreg);
10234 break;
10235
10236 case M_BEQ:
10237 s = "cmp";
10238 s2 = "bteqz";
10239 goto do_branch;
10240 case M_BNE:
10241 s = "cmp";
10242 s2 = "btnez";
10243 goto do_branch;
10244 case M_BLT:
10245 s = "slt";
10246 s2 = "btnez";
10247 goto do_branch;
10248 case M_BLTU:
10249 s = "sltu";
10250 s2 = "btnez";
10251 goto do_branch;
10252 case M_BLE:
10253 s = "slt";
10254 s2 = "bteqz";
10255 goto do_reverse_branch;
10256 case M_BLEU:
10257 s = "sltu";
10258 s2 = "bteqz";
10259 goto do_reverse_branch;
10260 case M_BGE:
10261 s = "slt";
10262 s2 = "bteqz";
10263 goto do_branch;
10264 case M_BGEU:
10265 s = "sltu";
10266 s2 = "bteqz";
10267 goto do_branch;
10268 case M_BGT:
10269 s = "slt";
10270 s2 = "btnez";
10271 goto do_reverse_branch;
10272 case M_BGTU:
10273 s = "sltu";
10274 s2 = "btnez";
10275
10276 do_reverse_branch:
10277 tmp = xreg;
10278 xreg = yreg;
10279 yreg = tmp;
10280
10281 do_branch:
10282 macro_build (NULL, s, "x,y", xreg, yreg);
10283 macro_build (&offset_expr, s2, "p");
10284 break;
10285
10286 case M_BEQ_I:
10287 s = "cmpi";
10288 s2 = "bteqz";
10289 s3 = "x,U";
10290 goto do_branch_i;
10291 case M_BNE_I:
10292 s = "cmpi";
10293 s2 = "btnez";
10294 s3 = "x,U";
10295 goto do_branch_i;
10296 case M_BLT_I:
10297 s = "slti";
10298 s2 = "btnez";
10299 s3 = "x,8";
10300 goto do_branch_i;
10301 case M_BLTU_I:
10302 s = "sltiu";
10303 s2 = "btnez";
10304 s3 = "x,8";
10305 goto do_branch_i;
10306 case M_BLE_I:
10307 s = "slti";
10308 s2 = "btnez";
10309 s3 = "x,8";
10310 goto do_addone_branch_i;
10311 case M_BLEU_I:
10312 s = "sltiu";
10313 s2 = "btnez";
10314 s3 = "x,8";
10315 goto do_addone_branch_i;
10316 case M_BGE_I:
10317 s = "slti";
10318 s2 = "bteqz";
10319 s3 = "x,8";
10320 goto do_branch_i;
10321 case M_BGEU_I:
10322 s = "sltiu";
10323 s2 = "bteqz";
10324 s3 = "x,8";
10325 goto do_branch_i;
10326 case M_BGT_I:
10327 s = "slti";
10328 s2 = "bteqz";
10329 s3 = "x,8";
10330 goto do_addone_branch_i;
10331 case M_BGTU_I:
10332 s = "sltiu";
10333 s2 = "bteqz";
10334 s3 = "x,8";
10335
10336 do_addone_branch_i:
10337 if (imm_expr.X_op != O_constant)
10338 as_bad (_("Unsupported large constant"));
10339 ++imm_expr.X_add_number;
10340
10341 do_branch_i:
10342 macro_build (&imm_expr, s, s3, xreg);
10343 macro_build (&offset_expr, s2, "p");
10344 break;
10345
10346 case M_ABS:
10347 expr1.X_add_number = 0;
10348 macro_build (&expr1, "slti", "x,8", yreg);
10349 if (xreg != yreg)
10350 move_register (xreg, yreg);
10351 expr1.X_add_number = 2;
10352 macro_build (&expr1, "bteqz", "p");
10353 macro_build (NULL, "neg", "x,w", xreg, xreg);
10354 }
10355 }
10356
10357 /* For consistency checking, verify that all bits are specified either
10358 by the match/mask part of the instruction definition, or by the
10359 operand list. */
10360 static int
10361 validate_mips_insn (const struct mips_opcode *opc)
10362 {
10363 const char *p = opc->args;
10364 char c;
10365 unsigned long used_bits = opc->mask;
10366
10367 if ((used_bits & opc->match) != opc->match)
10368 {
10369 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10370 opc->name, opc->args);
10371 return 0;
10372 }
10373 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
10374 while (*p)
10375 switch (c = *p++)
10376 {
10377 case ',': break;
10378 case '(': break;
10379 case ')': break;
10380 case '+':
10381 switch (c = *p++)
10382 {
10383 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
10384 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
10385 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
10386 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
10387 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10388 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10389 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10390 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
10391 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
10392 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10393 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10394 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10395 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10396 case 'I': break;
10397 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10398 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
10399 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
10400 case 'x': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10401 case 'X': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10402 case 'p': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10403 case 'P': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10404 case 'Q': USE_BITS (OP_MASK_SEQI, OP_SH_SEQI); break;
10405 case 's': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10406 case 'S': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10407 case 'z': USE_BITS (OP_MASK_RZ, OP_SH_RZ); break;
10408 case 'Z': USE_BITS (OP_MASK_FZ, OP_SH_FZ); break;
10409 case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10410 case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10411 case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
10412
10413 default:
10414 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10415 c, opc->name, opc->args);
10416 return 0;
10417 }
10418 break;
10419 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10420 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10421 case 'A': break;
10422 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
10423 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
10424 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10425 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10426 case 'F': break;
10427 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10428 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
10429 case 'I': break;
10430 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
10431 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10432 case 'L': break;
10433 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
10434 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
10435 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
10436 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
10437 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10438 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
10439 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10440 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10441 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10442 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10443 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10444 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10445 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10446 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
10447 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10448 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
10449 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10450 case 'f': break;
10451 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
10452 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10453 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10454 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
10455 case 'l': break;
10456 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10457 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10458 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
10459 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10460 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10461 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10462 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10463 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10464 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10465 case 'x': break;
10466 case 'z': break;
10467 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
10468 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
10469 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10470 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
10471 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
10472 case '[': break;
10473 case ']': break;
10474 case '1': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10475 case '2': USE_BITS (OP_MASK_BP, OP_SH_BP); break;
10476 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
10477 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
10478 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
10479 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10480 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
10481 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
10482 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
10483 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
10484 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
10485 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
10486 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
10487 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
10488 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
10489 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
10490 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
10491 case '\\': USE_BITS (OP_MASK_3BITPOS, OP_SH_3BITPOS); break;
10492 case '~': USE_BITS (OP_MASK_OFFSET12, OP_SH_OFFSET12); break;
10493 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10494 default:
10495 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10496 c, opc->name, opc->args);
10497 return 0;
10498 }
10499 #undef USE_BITS
10500 if (used_bits != 0xffffffff)
10501 {
10502 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10503 ~used_bits & 0xffffffff, opc->name, opc->args);
10504 return 0;
10505 }
10506 return 1;
10507 }
10508
10509 /* For consistency checking, verify that the length implied matches the
10510 major opcode and that all bits are specified either by the match/mask
10511 part of the instruction definition, or by the operand list. */
10512
10513 static int
10514 validate_micromips_insn (const struct mips_opcode *opc)
10515 {
10516 unsigned long match = opc->match;
10517 unsigned long mask = opc->mask;
10518 const char *p = opc->args;
10519 unsigned long insn_bits;
10520 unsigned long used_bits;
10521 unsigned long major;
10522 unsigned int length;
10523 char e;
10524 char c;
10525
10526 if ((mask & match) != match)
10527 {
10528 as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10529 opc->name, opc->args);
10530 return 0;
10531 }
10532 length = micromips_insn_length (opc);
10533 if (length != 2 && length != 4)
10534 {
10535 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10536 "%s %s"), length, opc->name, opc->args);
10537 return 0;
10538 }
10539 major = match >> (10 + 8 * (length - 2));
10540 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10541 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10542 {
10543 as_bad (_("Internal error: bad microMIPS opcode "
10544 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
10545 return 0;
10546 }
10547
10548 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
10549 insn_bits = 1 << 4 * length;
10550 insn_bits <<= 4 * length;
10551 insn_bits -= 1;
10552 used_bits = mask;
10553 #define USE_BITS(field) \
10554 (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10555 while (*p)
10556 switch (c = *p++)
10557 {
10558 case ',': break;
10559 case '(': break;
10560 case ')': break;
10561 case '+':
10562 e = c;
10563 switch (c = *p++)
10564 {
10565 case 'A': USE_BITS (EXTLSB); break;
10566 case 'B': USE_BITS (INSMSB); break;
10567 case 'C': USE_BITS (EXTMSBD); break;
10568 case 'D': USE_BITS (RS); USE_BITS (SEL); break;
10569 case 'E': USE_BITS (EXTLSB); break;
10570 case 'F': USE_BITS (INSMSB); break;
10571 case 'G': USE_BITS (EXTMSBD); break;
10572 case 'H': USE_BITS (EXTMSBD); break;
10573 default:
10574 as_bad (_("Internal error: bad mips opcode "
10575 "(unknown extension operand type `%c%c'): %s %s"),
10576 e, c, opc->name, opc->args);
10577 return 0;
10578 }
10579 break;
10580 case 'm':
10581 e = c;
10582 switch (c = *p++)
10583 {
10584 case 'A': USE_BITS (IMMA); break;
10585 case 'B': USE_BITS (IMMB); break;
10586 case 'C': USE_BITS (IMMC); break;
10587 case 'D': USE_BITS (IMMD); break;
10588 case 'E': USE_BITS (IMME); break;
10589 case 'F': USE_BITS (IMMF); break;
10590 case 'G': USE_BITS (IMMG); break;
10591 case 'H': USE_BITS (IMMH); break;
10592 case 'I': USE_BITS (IMMI); break;
10593 case 'J': USE_BITS (IMMJ); break;
10594 case 'L': USE_BITS (IMML); break;
10595 case 'M': USE_BITS (IMMM); break;
10596 case 'N': USE_BITS (IMMN); break;
10597 case 'O': USE_BITS (IMMO); break;
10598 case 'P': USE_BITS (IMMP); break;
10599 case 'Q': USE_BITS (IMMQ); break;
10600 case 'U': USE_BITS (IMMU); break;
10601 case 'W': USE_BITS (IMMW); break;
10602 case 'X': USE_BITS (IMMX); break;
10603 case 'Y': USE_BITS (IMMY); break;
10604 case 'Z': break;
10605 case 'a': break;
10606 case 'b': USE_BITS (MB); break;
10607 case 'c': USE_BITS (MC); break;
10608 case 'd': USE_BITS (MD); break;
10609 case 'e': USE_BITS (ME); break;
10610 case 'f': USE_BITS (MF); break;
10611 case 'g': USE_BITS (MG); break;
10612 case 'h': USE_BITS (MH); break;
10613 case 'i': USE_BITS (MI); break;
10614 case 'j': USE_BITS (MJ); break;
10615 case 'l': USE_BITS (ML); break;
10616 case 'm': USE_BITS (MM); break;
10617 case 'n': USE_BITS (MN); break;
10618 case 'p': USE_BITS (MP); break;
10619 case 'q': USE_BITS (MQ); break;
10620 case 'r': break;
10621 case 's': break;
10622 case 't': break;
10623 case 'x': break;
10624 case 'y': break;
10625 case 'z': break;
10626 default:
10627 as_bad (_("Internal error: bad mips opcode "
10628 "(unknown extension operand type `%c%c'): %s %s"),
10629 e, c, opc->name, opc->args);
10630 return 0;
10631 }
10632 break;
10633 case '.': USE_BITS (OFFSET10); break;
10634 case '1': USE_BITS (STYPE); break;
10635 case '2': USE_BITS (BP); break;
10636 case '3': USE_BITS (SA3); break;
10637 case '4': USE_BITS (SA4); break;
10638 case '5': USE_BITS (IMM8); break;
10639 case '6': USE_BITS (RS); break;
10640 case '7': USE_BITS (DSPACC); break;
10641 case '8': USE_BITS (WRDSP); break;
10642 case '0': USE_BITS (DSPSFT); break;
10643 case '<': USE_BITS (SHAMT); break;
10644 case '>': USE_BITS (SHAMT); break;
10645 case '@': USE_BITS (IMM10); break;
10646 case 'B': USE_BITS (CODE10); break;
10647 case 'C': USE_BITS (COPZ); break;
10648 case 'D': USE_BITS (FD); break;
10649 case 'E': USE_BITS (RT); break;
10650 case 'G': USE_BITS (RS); break;
10651 case 'H': USE_BITS (SEL); break;
10652 case 'K': USE_BITS (RS); break;
10653 case 'M': USE_BITS (CCC); break;
10654 case 'N': USE_BITS (BCC); break;
10655 case 'R': USE_BITS (FR); break;
10656 case 'S': USE_BITS (FS); break;
10657 case 'T': USE_BITS (FT); break;
10658 case 'V': USE_BITS (FS); break;
10659 case '\\': USE_BITS (3BITPOS); break;
10660 case '^': USE_BITS (RD); break;
10661 case 'a': USE_BITS (TARGET); break;
10662 case 'b': USE_BITS (RS); break;
10663 case 'c': USE_BITS (CODE); break;
10664 case 'd': USE_BITS (RD); break;
10665 case 'h': USE_BITS (PREFX); break;
10666 case 'i': USE_BITS (IMMEDIATE); break;
10667 case 'j': USE_BITS (DELTA); break;
10668 case 'k': USE_BITS (CACHE); break;
10669 case 'n': USE_BITS (RT); break;
10670 case 'o': USE_BITS (DELTA); break;
10671 case 'p': USE_BITS (DELTA); break;
10672 case 'q': USE_BITS (CODE2); break;
10673 case 'r': USE_BITS (RS); break;
10674 case 's': USE_BITS (RS); break;
10675 case 't': USE_BITS (RT); break;
10676 case 'u': USE_BITS (IMMEDIATE); break;
10677 case 'v': USE_BITS (RS); break;
10678 case 'w': USE_BITS (RT); break;
10679 case 'y': USE_BITS (RS3); break;
10680 case 'z': break;
10681 case '|': USE_BITS (TRAP); break;
10682 case '~': USE_BITS (OFFSET12); break;
10683 default:
10684 as_bad (_("Internal error: bad microMIPS opcode "
10685 "(unknown operand type `%c'): %s %s"),
10686 c, opc->name, opc->args);
10687 return 0;
10688 }
10689 #undef USE_BITS
10690 if (used_bits != insn_bits)
10691 {
10692 if (~used_bits & insn_bits)
10693 as_bad (_("Internal error: bad microMIPS opcode "
10694 "(bits 0x%lx undefined): %s %s"),
10695 ~used_bits & insn_bits, opc->name, opc->args);
10696 if (used_bits & ~insn_bits)
10697 as_bad (_("Internal error: bad microMIPS opcode "
10698 "(bits 0x%lx defined): %s %s"),
10699 used_bits & ~insn_bits, opc->name, opc->args);
10700 return 0;
10701 }
10702 return 1;
10703 }
10704
10705 /* UDI immediates. */
10706 struct mips_immed {
10707 char type;
10708 unsigned int shift;
10709 unsigned long mask;
10710 const char * desc;
10711 };
10712
10713 static const struct mips_immed mips_immed[] = {
10714 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
10715 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
10716 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
10717 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
10718 { 0,0,0,0 }
10719 };
10720
10721 /* Check whether an odd floating-point register is allowed. */
10722 static int
10723 mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
10724 {
10725 const char *s = insn->name;
10726
10727 if (insn->pinfo == INSN_MACRO)
10728 /* Let a macro pass, we'll catch it later when it is expanded. */
10729 return 1;
10730
10731 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || (mips_opts.arch == CPU_R5900))
10732 {
10733 /* Allow odd registers for single-precision ops. */
10734 switch (insn->pinfo & (FP_S | FP_D))
10735 {
10736 case FP_S:
10737 case 0:
10738 return 1; /* both single precision - ok */
10739 case FP_D:
10740 return 0; /* both double precision - fail */
10741 default:
10742 break;
10743 }
10744
10745 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
10746 s = strchr (insn->name, '.');
10747 if (argnum == 2)
10748 s = s != NULL ? strchr (s + 1, '.') : NULL;
10749 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
10750 }
10751
10752 /* Single-precision coprocessor loads and moves are OK too. */
10753 if ((insn->pinfo & FP_S)
10754 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
10755 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
10756 return 1;
10757
10758 return 0;
10759 }
10760
10761 /* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
10762 taking bits from BIT up. */
10763 static int
10764 expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
10765 {
10766 return (ep->X_op == O_constant
10767 && (ep->X_add_number & ((1 << bit) - 1)) == 0
10768 && ep->X_add_number >= min << bit
10769 && ep->X_add_number < max << bit);
10770 }
10771
10772 /* This routine assembles an instruction into its binary format. As a
10773 side effect, it sets one of the global variables imm_reloc or
10774 offset_reloc to the type of relocation to do if one of the operands
10775 is an address expression. */
10776
10777 static void
10778 mips_ip (char *str, struct mips_cl_insn *ip)
10779 {
10780 bfd_boolean wrong_delay_slot_insns = FALSE;
10781 bfd_boolean need_delay_slot_ok = TRUE;
10782 struct mips_opcode *firstinsn = NULL;
10783 const struct mips_opcode *past;
10784 struct hash_control *hash;
10785 char *s;
10786 const char *args;
10787 char c = 0;
10788 struct mips_opcode *insn;
10789 char *argsStart;
10790 unsigned int regno;
10791 unsigned int lastregno;
10792 unsigned int destregno = 0;
10793 unsigned int lastpos = 0;
10794 unsigned int limlo, limhi;
10795 int sizelo;
10796 char *s_reset;
10797 offsetT min_range, max_range;
10798 long opend;
10799 char *name;
10800 int argnum;
10801 unsigned int rtype;
10802 char *dot;
10803 long end;
10804
10805 insn_error = NULL;
10806
10807 if (mips_opts.micromips)
10808 {
10809 hash = micromips_op_hash;
10810 past = &micromips_opcodes[bfd_micromips_num_opcodes];
10811 }
10812 else
10813 {
10814 hash = op_hash;
10815 past = &mips_opcodes[NUMOPCODES];
10816 }
10817 forced_insn_length = 0;
10818 insn = NULL;
10819
10820 /* We first try to match an instruction up to a space or to the end. */
10821 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
10822 continue;
10823
10824 /* Make a copy of the instruction so that we can fiddle with it. */
10825 name = alloca (end + 1);
10826 memcpy (name, str, end);
10827 name[end] = '\0';
10828
10829 for (;;)
10830 {
10831 insn = (struct mips_opcode *) hash_find (hash, name);
10832
10833 if (insn != NULL || !mips_opts.micromips)
10834 break;
10835 if (forced_insn_length)
10836 break;
10837
10838 /* See if there's an instruction size override suffix,
10839 either `16' or `32', at the end of the mnemonic proper,
10840 that defines the operation, i.e. before the first `.'
10841 character if any. Strip it and retry. */
10842 dot = strchr (name, '.');
10843 opend = dot != NULL ? dot - name : end;
10844 if (opend < 3)
10845 break;
10846 if (name[opend - 2] == '1' && name[opend - 1] == '6')
10847 forced_insn_length = 2;
10848 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
10849 forced_insn_length = 4;
10850 else
10851 break;
10852 memcpy (name + opend - 2, name + opend, end - opend + 1);
10853 }
10854 if (insn == NULL)
10855 {
10856 insn_error = _("Unrecognized opcode");
10857 return;
10858 }
10859
10860 /* For microMIPS instructions placed in a fixed-length branch delay slot
10861 we make up to two passes over the relevant fragment of the opcode
10862 table. First we try instructions that meet the delay slot's length
10863 requirement. If none matched, then we retry with the remaining ones
10864 and if one matches, then we use it and then issue an appropriate
10865 warning later on. */
10866 argsStart = s = str + end;
10867 for (;;)
10868 {
10869 bfd_boolean delay_slot_ok;
10870 bfd_boolean size_ok;
10871 bfd_boolean ok;
10872
10873 gas_assert (strcmp (insn->name, name) == 0);
10874
10875 ok = is_opcode_valid (insn);
10876 size_ok = is_size_valid (insn);
10877 delay_slot_ok = is_delay_slot_valid (insn);
10878 if (!delay_slot_ok && !wrong_delay_slot_insns)
10879 {
10880 firstinsn = insn;
10881 wrong_delay_slot_insns = TRUE;
10882 }
10883 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
10884 {
10885 static char buf[256];
10886
10887 if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
10888 {
10889 ++insn;
10890 continue;
10891 }
10892 if (wrong_delay_slot_insns && need_delay_slot_ok)
10893 {
10894 gas_assert (firstinsn);
10895 need_delay_slot_ok = FALSE;
10896 past = insn + 1;
10897 insn = firstinsn;
10898 continue;
10899 }
10900
10901 if (insn_error)
10902 return;
10903
10904 if (!ok)
10905 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
10906 mips_cpu_info_from_arch (mips_opts.arch)->name,
10907 mips_cpu_info_from_isa (mips_opts.isa)->name);
10908 else
10909 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
10910 8 * forced_insn_length);
10911 insn_error = buf;
10912
10913 return;
10914 }
10915
10916 create_insn (ip, insn);
10917 insn_error = NULL;
10918 argnum = 1;
10919 lastregno = 0xffffffff;
10920 for (args = insn->args;; ++args)
10921 {
10922 int is_mdmx;
10923
10924 s += strspn (s, " \t");
10925 is_mdmx = 0;
10926 switch (*args)
10927 {
10928 case '\0': /* end of args */
10929 if (*s == '\0')
10930 return;
10931 break;
10932
10933 case '2':
10934 /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
10935 code) or 14 (for microMIPS code). */
10936 my_getExpression (&imm_expr, s);
10937 check_absolute_expr (ip, &imm_expr);
10938 if ((unsigned long) imm_expr.X_add_number != 1
10939 && (unsigned long) imm_expr.X_add_number != 3)
10940 {
10941 as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
10942 (unsigned long) imm_expr.X_add_number);
10943 }
10944 INSERT_OPERAND (mips_opts.micromips,
10945 BP, *ip, imm_expr.X_add_number);
10946 imm_expr.X_op = O_absent;
10947 s = expr_end;
10948 continue;
10949
10950 case '3':
10951 /* DSP 3-bit unsigned immediate in bit 13 (for standard MIPS
10952 code) or 21 (for microMIPS code). */
10953 {
10954 unsigned long mask = (mips_opts.micromips
10955 ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
10956
10957 my_getExpression (&imm_expr, s);
10958 check_absolute_expr (ip, &imm_expr);
10959 if ((unsigned long) imm_expr.X_add_number > mask)
10960 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10961 mask, (unsigned long) imm_expr.X_add_number);
10962 INSERT_OPERAND (mips_opts.micromips,
10963 SA3, *ip, imm_expr.X_add_number);
10964 imm_expr.X_op = O_absent;
10965 s = expr_end;
10966 }
10967 continue;
10968
10969 case '4':
10970 /* DSP 4-bit unsigned immediate in bit 12 (for standard MIPS
10971 code) or 21 (for microMIPS code). */
10972 {
10973 unsigned long mask = (mips_opts.micromips
10974 ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
10975
10976 my_getExpression (&imm_expr, s);
10977 check_absolute_expr (ip, &imm_expr);
10978 if ((unsigned long) imm_expr.X_add_number > mask)
10979 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10980 mask, (unsigned long) imm_expr.X_add_number);
10981 INSERT_OPERAND (mips_opts.micromips,
10982 SA4, *ip, imm_expr.X_add_number);
10983 imm_expr.X_op = O_absent;
10984 s = expr_end;
10985 }
10986 continue;
10987
10988 case '5':
10989 /* DSP 8-bit unsigned immediate in bit 13 (for standard MIPS
10990 code) or 16 (for microMIPS code). */
10991 {
10992 unsigned long mask = (mips_opts.micromips
10993 ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
10994
10995 my_getExpression (&imm_expr, s);
10996 check_absolute_expr (ip, &imm_expr);
10997 if ((unsigned long) imm_expr.X_add_number > mask)
10998 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10999 mask, (unsigned long) imm_expr.X_add_number);
11000 INSERT_OPERAND (mips_opts.micromips,
11001 IMM8, *ip, imm_expr.X_add_number);
11002 imm_expr.X_op = O_absent;
11003 s = expr_end;
11004 }
11005 continue;
11006
11007 case '6':
11008 /* DSP 5-bit unsigned immediate in bit 16 (for standard MIPS
11009 code) or 21 (for microMIPS code). */
11010 {
11011 unsigned long mask = (mips_opts.micromips
11012 ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
11013
11014 my_getExpression (&imm_expr, s);
11015 check_absolute_expr (ip, &imm_expr);
11016 if ((unsigned long) imm_expr.X_add_number > mask)
11017 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11018 mask, (unsigned long) imm_expr.X_add_number);
11019 INSERT_OPERAND (mips_opts.micromips,
11020 RS, *ip, imm_expr.X_add_number);
11021 imm_expr.X_op = O_absent;
11022 s = expr_end;
11023 }
11024 continue;
11025
11026 case '7': /* Four DSP accumulators in bits 11,12. */
11027 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11028 && s[3] >= '0' && s[3] <= '3')
11029 {
11030 regno = s[3] - '0';
11031 s += 4;
11032 INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
11033 continue;
11034 }
11035 else
11036 as_bad (_("Invalid dsp acc register"));
11037 break;
11038
11039 case '8':
11040 /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
11041 code) or 14 (for microMIPS code). */
11042 {
11043 unsigned long mask = (mips_opts.micromips
11044 ? MICROMIPSOP_MASK_WRDSP
11045 : OP_MASK_WRDSP);
11046
11047 my_getExpression (&imm_expr, s);
11048 check_absolute_expr (ip, &imm_expr);
11049 if ((unsigned long) imm_expr.X_add_number > mask)
11050 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11051 mask, (unsigned long) imm_expr.X_add_number);
11052 INSERT_OPERAND (mips_opts.micromips,
11053 WRDSP, *ip, imm_expr.X_add_number);
11054 imm_expr.X_op = O_absent;
11055 s = expr_end;
11056 }
11057 continue;
11058
11059 case '9': /* Four DSP accumulators in bits 21,22. */
11060 gas_assert (!mips_opts.micromips);
11061 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11062 && s[3] >= '0' && s[3] <= '3')
11063 {
11064 regno = s[3] - '0';
11065 s += 4;
11066 INSERT_OPERAND (0, DSPACC_S, *ip, regno);
11067 continue;
11068 }
11069 else
11070 as_bad (_("Invalid dsp acc register"));
11071 break;
11072
11073 case '0':
11074 /* DSP 6-bit signed immediate in bit 16 (for standard MIPS
11075 code) or 20 (for microMIPS code). */
11076 {
11077 long mask = (mips_opts.micromips
11078 ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
11079
11080 my_getExpression (&imm_expr, s);
11081 check_absolute_expr (ip, &imm_expr);
11082 min_range = -((mask + 1) >> 1);
11083 max_range = ((mask + 1) >> 1) - 1;
11084 if (imm_expr.X_add_number < min_range
11085 || imm_expr.X_add_number > max_range)
11086 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11087 (long) min_range, (long) max_range,
11088 (long) imm_expr.X_add_number);
11089 INSERT_OPERAND (mips_opts.micromips,
11090 DSPSFT, *ip, imm_expr.X_add_number);
11091 imm_expr.X_op = O_absent;
11092 s = expr_end;
11093 }
11094 continue;
11095
11096 case '\'': /* DSP 6-bit unsigned immediate in bit 16. */
11097 gas_assert (!mips_opts.micromips);
11098 my_getExpression (&imm_expr, s);
11099 check_absolute_expr (ip, &imm_expr);
11100 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11101 {
11102 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11103 OP_MASK_RDDSP,
11104 (unsigned long) imm_expr.X_add_number);
11105 }
11106 INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
11107 imm_expr.X_op = O_absent;
11108 s = expr_end;
11109 continue;
11110
11111 case ':': /* DSP 7-bit signed immediate in bit 19. */
11112 gas_assert (!mips_opts.micromips);
11113 my_getExpression (&imm_expr, s);
11114 check_absolute_expr (ip, &imm_expr);
11115 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11116 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11117 if (imm_expr.X_add_number < min_range ||
11118 imm_expr.X_add_number > max_range)
11119 {
11120 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11121 (long) min_range, (long) max_range,
11122 (long) imm_expr.X_add_number);
11123 }
11124 INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
11125 imm_expr.X_op = O_absent;
11126 s = expr_end;
11127 continue;
11128
11129 case '@': /* DSP 10-bit signed immediate in bit 16. */
11130 {
11131 long mask = (mips_opts.micromips
11132 ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11133
11134 my_getExpression (&imm_expr, s);
11135 check_absolute_expr (ip, &imm_expr);
11136 min_range = -((mask + 1) >> 1);
11137 max_range = ((mask + 1) >> 1) - 1;
11138 if (imm_expr.X_add_number < min_range
11139 || imm_expr.X_add_number > max_range)
11140 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11141 (long) min_range, (long) max_range,
11142 (long) imm_expr.X_add_number);
11143 INSERT_OPERAND (mips_opts.micromips,
11144 IMM10, *ip, imm_expr.X_add_number);
11145 imm_expr.X_op = O_absent;
11146 s = expr_end;
11147 }
11148 continue;
11149
11150 case '^': /* DSP 5-bit unsigned immediate in bit 11. */
11151 gas_assert (mips_opts.micromips);
11152 my_getExpression (&imm_expr, s);
11153 check_absolute_expr (ip, &imm_expr);
11154 if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11155 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11156 MICROMIPSOP_MASK_RD,
11157 (unsigned long) imm_expr.X_add_number);
11158 INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
11159 imm_expr.X_op = O_absent;
11160 s = expr_end;
11161 continue;
11162
11163 case '!': /* MT usermode flag bit. */
11164 gas_assert (!mips_opts.micromips);
11165 my_getExpression (&imm_expr, s);
11166 check_absolute_expr (ip, &imm_expr);
11167 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
11168 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11169 (unsigned long) imm_expr.X_add_number);
11170 INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
11171 imm_expr.X_op = O_absent;
11172 s = expr_end;
11173 continue;
11174
11175 case '$': /* MT load high flag bit. */
11176 gas_assert (!mips_opts.micromips);
11177 my_getExpression (&imm_expr, s);
11178 check_absolute_expr (ip, &imm_expr);
11179 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
11180 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11181 (unsigned long) imm_expr.X_add_number);
11182 INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
11183 imm_expr.X_op = O_absent;
11184 s = expr_end;
11185 continue;
11186
11187 case '*': /* Four DSP accumulators in bits 18,19. */
11188 gas_assert (!mips_opts.micromips);
11189 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11190 s[3] >= '0' && s[3] <= '3')
11191 {
11192 regno = s[3] - '0';
11193 s += 4;
11194 INSERT_OPERAND (0, MTACC_T, *ip, regno);
11195 continue;
11196 }
11197 else
11198 as_bad (_("Invalid dsp/smartmips acc register"));
11199 break;
11200
11201 case '&': /* Four DSP accumulators in bits 13,14. */
11202 gas_assert (!mips_opts.micromips);
11203 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11204 s[3] >= '0' && s[3] <= '3')
11205 {
11206 regno = s[3] - '0';
11207 s += 4;
11208 INSERT_OPERAND (0, MTACC_D, *ip, regno);
11209 continue;
11210 }
11211 else
11212 as_bad (_("Invalid dsp/smartmips acc register"));
11213 break;
11214
11215 case '\\': /* 3-bit bit position. */
11216 {
11217 unsigned long mask = (mips_opts.micromips
11218 ? MICROMIPSOP_MASK_3BITPOS
11219 : OP_MASK_3BITPOS);
11220
11221 my_getExpression (&imm_expr, s);
11222 check_absolute_expr (ip, &imm_expr);
11223 if ((unsigned long) imm_expr.X_add_number > mask)
11224 as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11225 ip->insn_mo->name,
11226 mask, (unsigned long) imm_expr.X_add_number);
11227 INSERT_OPERAND (mips_opts.micromips,
11228 3BITPOS, *ip, imm_expr.X_add_number);
11229 imm_expr.X_op = O_absent;
11230 s = expr_end;
11231 }
11232 continue;
11233
11234 case ',':
11235 ++argnum;
11236 if (*s++ == *args)
11237 continue;
11238 s--;
11239 switch (*++args)
11240 {
11241 case 'r':
11242 case 'v':
11243 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
11244 continue;
11245
11246 case 'w':
11247 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
11248 continue;
11249
11250 case 'W':
11251 gas_assert (!mips_opts.micromips);
11252 INSERT_OPERAND (0, FT, *ip, lastregno);
11253 continue;
11254
11255 case 'V':
11256 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
11257 continue;
11258 }
11259 break;
11260
11261 case '(':
11262 /* Handle optional base register.
11263 Either the base register is omitted or
11264 we must have a left paren. */
11265 /* This is dependent on the next operand specifier
11266 is a base register specification. */
11267 gas_assert (args[1] == 'b'
11268 || (mips_opts.micromips
11269 && args[1] == 'm'
11270 && (args[2] == 'l' || args[2] == 'n'
11271 || args[2] == 's' || args[2] == 'a')));
11272 if (*s == '\0' && args[1] == 'b')
11273 return;
11274 /* Fall through. */
11275
11276 case ')': /* These must match exactly. */
11277 if (*s++ == *args)
11278 continue;
11279 break;
11280
11281 case '[': /* These must match exactly. */
11282 case ']':
11283 gas_assert (!mips_opts.micromips);
11284 if (*s++ == *args)
11285 continue;
11286 break;
11287
11288 case '+': /* Opcode extension character. */
11289 switch (*++args)
11290 {
11291 case '1': /* UDI immediates. */
11292 case '2':
11293 case '3':
11294 case '4':
11295 gas_assert (!mips_opts.micromips);
11296 {
11297 const struct mips_immed *imm = mips_immed;
11298
11299 while (imm->type && imm->type != *args)
11300 ++imm;
11301 if (! imm->type)
11302 abort ();
11303 my_getExpression (&imm_expr, s);
11304 check_absolute_expr (ip, &imm_expr);
11305 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11306 {
11307 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11308 imm->desc ? imm->desc : ip->insn_mo->name,
11309 (unsigned long) imm_expr.X_add_number,
11310 (unsigned long) imm_expr.X_add_number);
11311 imm_expr.X_add_number &= imm->mask;
11312 }
11313 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11314 << imm->shift);
11315 imm_expr.X_op = O_absent;
11316 s = expr_end;
11317 }
11318 continue;
11319
11320 case 'A': /* ins/ext position, becomes LSB. */
11321 limlo = 0;
11322 limhi = 31;
11323 goto do_lsb;
11324 case 'E':
11325 limlo = 32;
11326 limhi = 63;
11327 goto do_lsb;
11328 do_lsb:
11329 my_getExpression (&imm_expr, s);
11330 check_absolute_expr (ip, &imm_expr);
11331 if ((unsigned long) imm_expr.X_add_number < limlo
11332 || (unsigned long) imm_expr.X_add_number > limhi)
11333 {
11334 as_bad (_("Improper position (%lu)"),
11335 (unsigned long) imm_expr.X_add_number);
11336 imm_expr.X_add_number = limlo;
11337 }
11338 lastpos = imm_expr.X_add_number;
11339 INSERT_OPERAND (mips_opts.micromips,
11340 EXTLSB, *ip, imm_expr.X_add_number);
11341 imm_expr.X_op = O_absent;
11342 s = expr_end;
11343 continue;
11344
11345 case 'B': /* ins size, becomes MSB. */
11346 limlo = 1;
11347 limhi = 32;
11348 goto do_msb;
11349 case 'F':
11350 limlo = 33;
11351 limhi = 64;
11352 goto do_msb;
11353 do_msb:
11354 my_getExpression (&imm_expr, s);
11355 check_absolute_expr (ip, &imm_expr);
11356 /* Check for negative input so that small negative numbers
11357 will not succeed incorrectly. The checks against
11358 (pos+size) transitively check "size" itself,
11359 assuming that "pos" is reasonable. */
11360 if ((long) imm_expr.X_add_number < 0
11361 || ((unsigned long) imm_expr.X_add_number
11362 + lastpos) < limlo
11363 || ((unsigned long) imm_expr.X_add_number
11364 + lastpos) > limhi)
11365 {
11366 as_bad (_("Improper insert size (%lu, position %lu)"),
11367 (unsigned long) imm_expr.X_add_number,
11368 (unsigned long) lastpos);
11369 imm_expr.X_add_number = limlo - lastpos;
11370 }
11371 INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11372 lastpos + imm_expr.X_add_number - 1);
11373 imm_expr.X_op = O_absent;
11374 s = expr_end;
11375 continue;
11376
11377 case 'C': /* ext size, becomes MSBD. */
11378 limlo = 1;
11379 limhi = 32;
11380 sizelo = 1;
11381 goto do_msbd;
11382 case 'G':
11383 limlo = 33;
11384 limhi = 64;
11385 sizelo = 33;
11386 goto do_msbd;
11387 case 'H':
11388 limlo = 33;
11389 limhi = 64;
11390 sizelo = 1;
11391 goto do_msbd;
11392 do_msbd:
11393 my_getExpression (&imm_expr, s);
11394 check_absolute_expr (ip, &imm_expr);
11395 /* The checks against (pos+size) don't transitively check
11396 "size" itself, assuming that "pos" is reasonable.
11397 We also need to check the lower bound of "size". */
11398 if ((long) imm_expr.X_add_number < sizelo
11399 || ((unsigned long) imm_expr.X_add_number
11400 + lastpos) < limlo
11401 || ((unsigned long) imm_expr.X_add_number
11402 + lastpos) > limhi)
11403 {
11404 as_bad (_("Improper extract size (%lu, position %lu)"),
11405 (unsigned long) imm_expr.X_add_number,
11406 (unsigned long) lastpos);
11407 imm_expr.X_add_number = limlo - lastpos;
11408 }
11409 INSERT_OPERAND (mips_opts.micromips,
11410 EXTMSBD, *ip, imm_expr.X_add_number - 1);
11411 imm_expr.X_op = O_absent;
11412 s = expr_end;
11413 continue;
11414
11415 case 'D':
11416 /* +D is for disassembly only; never match. */
11417 break;
11418
11419 case 'I':
11420 /* "+I" is like "I", except that imm2_expr is used. */
11421 my_getExpression (&imm2_expr, s);
11422 if (imm2_expr.X_op != O_big
11423 && imm2_expr.X_op != O_constant)
11424 insn_error = _("absolute expression required");
11425 if (HAVE_32BIT_GPRS)
11426 normalize_constant_expr (&imm2_expr);
11427 s = expr_end;
11428 continue;
11429
11430 case 'T': /* Coprocessor register. */
11431 gas_assert (!mips_opts.micromips);
11432 /* +T is for disassembly only; never match. */
11433 break;
11434
11435 case 't': /* Coprocessor register number. */
11436 gas_assert (!mips_opts.micromips);
11437 if (s[0] == '$' && ISDIGIT (s[1]))
11438 {
11439 ++s;
11440 regno = 0;
11441 do
11442 {
11443 regno *= 10;
11444 regno += *s - '0';
11445 ++s;
11446 }
11447 while (ISDIGIT (*s));
11448 if (regno > 31)
11449 as_bad (_("Invalid register number (%d)"), regno);
11450 else
11451 {
11452 INSERT_OPERAND (0, RT, *ip, regno);
11453 continue;
11454 }
11455 }
11456 else
11457 as_bad (_("Invalid coprocessor 0 register number"));
11458 break;
11459
11460 case 'x':
11461 /* bbit[01] and bbit[01]32 bit index. Give error if index
11462 is not in the valid range. */
11463 gas_assert (!mips_opts.micromips);
11464 my_getExpression (&imm_expr, s);
11465 check_absolute_expr (ip, &imm_expr);
11466 if ((unsigned) imm_expr.X_add_number > 31)
11467 {
11468 as_bad (_("Improper bit index (%lu)"),
11469 (unsigned long) imm_expr.X_add_number);
11470 imm_expr.X_add_number = 0;
11471 }
11472 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
11473 imm_expr.X_op = O_absent;
11474 s = expr_end;
11475 continue;
11476
11477 case 'X':
11478 /* bbit[01] bit index when bbit is used but we generate
11479 bbit[01]32 because the index is over 32. Move to the
11480 next candidate if index is not in the valid range. */
11481 gas_assert (!mips_opts.micromips);
11482 my_getExpression (&imm_expr, s);
11483 check_absolute_expr (ip, &imm_expr);
11484 if ((unsigned) imm_expr.X_add_number < 32
11485 || (unsigned) imm_expr.X_add_number > 63)
11486 break;
11487 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
11488 imm_expr.X_op = O_absent;
11489 s = expr_end;
11490 continue;
11491
11492 case 'p':
11493 /* cins, cins32, exts and exts32 position field. Give error
11494 if it's not in the valid range. */
11495 gas_assert (!mips_opts.micromips);
11496 my_getExpression (&imm_expr, s);
11497 check_absolute_expr (ip, &imm_expr);
11498 if ((unsigned) imm_expr.X_add_number > 31)
11499 {
11500 as_bad (_("Improper position (%lu)"),
11501 (unsigned long) imm_expr.X_add_number);
11502 imm_expr.X_add_number = 0;
11503 }
11504 /* Make the pos explicit to simplify +S. */
11505 lastpos = imm_expr.X_add_number + 32;
11506 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
11507 imm_expr.X_op = O_absent;
11508 s = expr_end;
11509 continue;
11510
11511 case 'P':
11512 /* cins, cins32, exts and exts32 position field. Move to
11513 the next candidate if it's not in the valid range. */
11514 gas_assert (!mips_opts.micromips);
11515 my_getExpression (&imm_expr, s);
11516 check_absolute_expr (ip, &imm_expr);
11517 if ((unsigned) imm_expr.X_add_number < 32
11518 || (unsigned) imm_expr.X_add_number > 63)
11519 break;
11520 lastpos = imm_expr.X_add_number;
11521 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
11522 imm_expr.X_op = O_absent;
11523 s = expr_end;
11524 continue;
11525
11526 case 's':
11527 /* cins and exts length-minus-one field. */
11528 gas_assert (!mips_opts.micromips);
11529 my_getExpression (&imm_expr, s);
11530 check_absolute_expr (ip, &imm_expr);
11531 if ((unsigned long) imm_expr.X_add_number > 31)
11532 {
11533 as_bad (_("Improper size (%lu)"),
11534 (unsigned long) imm_expr.X_add_number);
11535 imm_expr.X_add_number = 0;
11536 }
11537 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11538 imm_expr.X_op = O_absent;
11539 s = expr_end;
11540 continue;
11541
11542 case 'S':
11543 /* cins32/exts32 and cins/exts aliasing cint32/exts32
11544 length-minus-one field. */
11545 gas_assert (!mips_opts.micromips);
11546 my_getExpression (&imm_expr, s);
11547 check_absolute_expr (ip, &imm_expr);
11548 if ((long) imm_expr.X_add_number < 0
11549 || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11550 {
11551 as_bad (_("Improper size (%lu)"),
11552 (unsigned long) imm_expr.X_add_number);
11553 imm_expr.X_add_number = 0;
11554 }
11555 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11556 imm_expr.X_op = O_absent;
11557 s = expr_end;
11558 continue;
11559
11560 case 'Q':
11561 /* seqi/snei immediate field. */
11562 gas_assert (!mips_opts.micromips);
11563 my_getExpression (&imm_expr, s);
11564 check_absolute_expr (ip, &imm_expr);
11565 if ((long) imm_expr.X_add_number < -512
11566 || (long) imm_expr.X_add_number >= 512)
11567 {
11568 as_bad (_("Improper immediate (%ld)"),
11569 (long) imm_expr.X_add_number);
11570 imm_expr.X_add_number = 0;
11571 }
11572 INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
11573 imm_expr.X_op = O_absent;
11574 s = expr_end;
11575 continue;
11576
11577 case 'a': /* 8-bit signed offset in bit 6 */
11578 gas_assert (!mips_opts.micromips);
11579 my_getExpression (&imm_expr, s);
11580 check_absolute_expr (ip, &imm_expr);
11581 min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
11582 max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
11583 if (imm_expr.X_add_number < min_range
11584 || imm_expr.X_add_number > max_range)
11585 {
11586 as_bad (_("Offset not in range %ld..%ld (%ld)"),
11587 (long) min_range, (long) max_range,
11588 (long) imm_expr.X_add_number);
11589 }
11590 INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
11591 imm_expr.X_op = O_absent;
11592 s = expr_end;
11593 continue;
11594
11595 case 'b': /* 8-bit signed offset in bit 3 */
11596 gas_assert (!mips_opts.micromips);
11597 my_getExpression (&imm_expr, s);
11598 check_absolute_expr (ip, &imm_expr);
11599 min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
11600 max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
11601 if (imm_expr.X_add_number < min_range
11602 || imm_expr.X_add_number > max_range)
11603 {
11604 as_bad (_("Offset not in range %ld..%ld (%ld)"),
11605 (long) min_range, (long) max_range,
11606 (long) imm_expr.X_add_number);
11607 }
11608 INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
11609 imm_expr.X_op = O_absent;
11610 s = expr_end;
11611 continue;
11612
11613 case 'c': /* 9-bit signed offset in bit 6 */
11614 gas_assert (!mips_opts.micromips);
11615 my_getExpression (&imm_expr, s);
11616 check_absolute_expr (ip, &imm_expr);
11617 min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
11618 max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
11619 /* We check the offset range before adjusted. */
11620 min_range <<= 4;
11621 max_range <<= 4;
11622 if (imm_expr.X_add_number < min_range
11623 || imm_expr.X_add_number > max_range)
11624 {
11625 as_bad (_("Offset not in range %ld..%ld (%ld)"),
11626 (long) min_range, (long) max_range,
11627 (long) imm_expr.X_add_number);
11628 }
11629 if (imm_expr.X_add_number & 0xf)
11630 {
11631 as_bad (_("Offset not 16 bytes alignment (%ld)"),
11632 (long) imm_expr.X_add_number);
11633 }
11634 /* Right shift 4 bits to adjust the offset operand. */
11635 INSERT_OPERAND (0, OFFSET_C, *ip,
11636 imm_expr.X_add_number >> 4);
11637 imm_expr.X_op = O_absent;
11638 s = expr_end;
11639 continue;
11640
11641 case 'z':
11642 gas_assert (!mips_opts.micromips);
11643 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
11644 break;
11645 if (regno == AT && mips_opts.at)
11646 {
11647 if (mips_opts.at == ATREG)
11648 as_warn (_("used $at without \".set noat\""));
11649 else
11650 as_warn (_("used $%u with \".set at=$%u\""),
11651 regno, mips_opts.at);
11652 }
11653 INSERT_OPERAND (0, RZ, *ip, regno);
11654 continue;
11655
11656 case 'Z':
11657 gas_assert (!mips_opts.micromips);
11658 if (!reg_lookup (&s, RTYPE_FPU, &regno))
11659 break;
11660 INSERT_OPERAND (0, FZ, *ip, regno);
11661 continue;
11662
11663 default:
11664 as_bad (_("Internal error: bad %s opcode "
11665 "(unknown extension operand type `+%c'): %s %s"),
11666 mips_opts.micromips ? "microMIPS" : "MIPS",
11667 *args, insn->name, insn->args);
11668 /* Further processing is fruitless. */
11669 return;
11670 }
11671 break;
11672
11673 case '.': /* 10-bit offset. */
11674 gas_assert (mips_opts.micromips);
11675 case '~': /* 12-bit offset. */
11676 {
11677 int shift = *args == '.' ? 9 : 11;
11678 size_t i;
11679
11680 /* Check whether there is only a single bracketed expression
11681 left. If so, it must be the base register and the
11682 constant must be zero. */
11683 if (*s == '(' && strchr (s + 1, '(') == 0)
11684 continue;
11685
11686 /* If this value won't fit into the offset, then go find
11687 a macro that will generate a 16- or 32-bit offset code
11688 pattern. */
11689 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11690 if ((i == 0 && (imm_expr.X_op != O_constant
11691 || imm_expr.X_add_number >= 1 << shift
11692 || imm_expr.X_add_number < -1 << shift))
11693 || i > 0)
11694 {
11695 imm_expr.X_op = O_absent;
11696 break;
11697 }
11698 if (shift == 9)
11699 INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
11700 else
11701 INSERT_OPERAND (mips_opts.micromips,
11702 OFFSET12, *ip, imm_expr.X_add_number);
11703 imm_expr.X_op = O_absent;
11704 s = expr_end;
11705 }
11706 continue;
11707
11708 case '<': /* must be at least one digit */
11709 /*
11710 * According to the manual, if the shift amount is greater
11711 * than 31 or less than 0, then the shift amount should be
11712 * mod 32. In reality the mips assembler issues an error.
11713 * We issue a warning and mask out all but the low 5 bits.
11714 */
11715 my_getExpression (&imm_expr, s);
11716 check_absolute_expr (ip, &imm_expr);
11717 if ((unsigned long) imm_expr.X_add_number > 31)
11718 as_warn (_("Improper shift amount (%lu)"),
11719 (unsigned long) imm_expr.X_add_number);
11720 INSERT_OPERAND (mips_opts.micromips,
11721 SHAMT, *ip, imm_expr.X_add_number);
11722 imm_expr.X_op = O_absent;
11723 s = expr_end;
11724 continue;
11725
11726 case '>': /* shift amount minus 32 */
11727 my_getExpression (&imm_expr, s);
11728 check_absolute_expr (ip, &imm_expr);
11729 if ((unsigned long) imm_expr.X_add_number < 32
11730 || (unsigned long) imm_expr.X_add_number > 63)
11731 break;
11732 INSERT_OPERAND (mips_opts.micromips,
11733 SHAMT, *ip, imm_expr.X_add_number - 32);
11734 imm_expr.X_op = O_absent;
11735 s = expr_end;
11736 continue;
11737
11738 case 'k': /* CACHE code. */
11739 case 'h': /* PREFX code. */
11740 case '1': /* SYNC type. */
11741 my_getExpression (&imm_expr, s);
11742 check_absolute_expr (ip, &imm_expr);
11743 if ((unsigned long) imm_expr.X_add_number > 31)
11744 as_warn (_("Invalid value for `%s' (%lu)"),
11745 ip->insn_mo->name,
11746 (unsigned long) imm_expr.X_add_number);
11747 switch (*args)
11748 {
11749 case 'k':
11750 if (mips_fix_cn63xxp1
11751 && !mips_opts.micromips
11752 && strcmp ("pref", insn->name) == 0)
11753 switch (imm_expr.X_add_number)
11754 {
11755 case 5:
11756 case 25:
11757 case 26:
11758 case 27:
11759 case 28:
11760 case 29:
11761 case 30:
11762 case 31: /* These are ok. */
11763 break;
11764
11765 default: /* The rest must be changed to 28. */
11766 imm_expr.X_add_number = 28;
11767 break;
11768 }
11769 INSERT_OPERAND (mips_opts.micromips,
11770 CACHE, *ip, imm_expr.X_add_number);
11771 break;
11772 case 'h':
11773 INSERT_OPERAND (mips_opts.micromips,
11774 PREFX, *ip, imm_expr.X_add_number);
11775 break;
11776 case '1':
11777 INSERT_OPERAND (mips_opts.micromips,
11778 STYPE, *ip, imm_expr.X_add_number);
11779 break;
11780 }
11781 imm_expr.X_op = O_absent;
11782 s = expr_end;
11783 continue;
11784
11785 case 'c': /* BREAK code. */
11786 {
11787 unsigned long mask = (mips_opts.micromips
11788 ? MICROMIPSOP_MASK_CODE
11789 : OP_MASK_CODE);
11790
11791 my_getExpression (&imm_expr, s);
11792 check_absolute_expr (ip, &imm_expr);
11793 if ((unsigned long) imm_expr.X_add_number > mask)
11794 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11795 ip->insn_mo->name,
11796 mask, (unsigned long) imm_expr.X_add_number);
11797 INSERT_OPERAND (mips_opts.micromips,
11798 CODE, *ip, imm_expr.X_add_number);
11799 imm_expr.X_op = O_absent;
11800 s = expr_end;
11801 }
11802 continue;
11803
11804 case 'q': /* Lower BREAK code. */
11805 {
11806 unsigned long mask = (mips_opts.micromips
11807 ? MICROMIPSOP_MASK_CODE2
11808 : OP_MASK_CODE2);
11809
11810 my_getExpression (&imm_expr, s);
11811 check_absolute_expr (ip, &imm_expr);
11812 if ((unsigned long) imm_expr.X_add_number > mask)
11813 as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
11814 ip->insn_mo->name,
11815 mask, (unsigned long) imm_expr.X_add_number);
11816 INSERT_OPERAND (mips_opts.micromips,
11817 CODE2, *ip, imm_expr.X_add_number);
11818 imm_expr.X_op = O_absent;
11819 s = expr_end;
11820 }
11821 continue;
11822
11823 case 'B': /* 20- or 10-bit syscall/break/wait code. */
11824 {
11825 unsigned long mask = (mips_opts.micromips
11826 ? MICROMIPSOP_MASK_CODE10
11827 : OP_MASK_CODE20);
11828
11829 my_getExpression (&imm_expr, s);
11830 check_absolute_expr (ip, &imm_expr);
11831 if ((unsigned long) imm_expr.X_add_number > mask)
11832 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11833 ip->insn_mo->name,
11834 mask, (unsigned long) imm_expr.X_add_number);
11835 if (mips_opts.micromips)
11836 INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
11837 else
11838 INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
11839 imm_expr.X_op = O_absent;
11840 s = expr_end;
11841 }
11842 continue;
11843
11844 case 'C': /* 25- or 23-bit coprocessor code. */
11845 {
11846 unsigned long mask = (mips_opts.micromips
11847 ? MICROMIPSOP_MASK_COPZ
11848 : OP_MASK_COPZ);
11849
11850 my_getExpression (&imm_expr, s);
11851 check_absolute_expr (ip, &imm_expr);
11852 if ((unsigned long) imm_expr.X_add_number > mask)
11853 as_warn (_("Coproccesor code > %u bits (%lu)"),
11854 mips_opts.micromips ? 23U : 25U,
11855 (unsigned long) imm_expr.X_add_number);
11856 INSERT_OPERAND (mips_opts.micromips,
11857 COPZ, *ip, imm_expr.X_add_number);
11858 imm_expr.X_op = O_absent;
11859 s = expr_end;
11860 }
11861 continue;
11862
11863 case 'J': /* 19-bit WAIT code. */
11864 gas_assert (!mips_opts.micromips);
11865 my_getExpression (&imm_expr, s);
11866 check_absolute_expr (ip, &imm_expr);
11867 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
11868 {
11869 as_warn (_("Illegal 19-bit code (%lu)"),
11870 (unsigned long) imm_expr.X_add_number);
11871 imm_expr.X_add_number &= OP_MASK_CODE19;
11872 }
11873 INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
11874 imm_expr.X_op = O_absent;
11875 s = expr_end;
11876 continue;
11877
11878 case 'P': /* Performance register. */
11879 gas_assert (!mips_opts.micromips);
11880 my_getExpression (&imm_expr, s);
11881 check_absolute_expr (ip, &imm_expr);
11882 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
11883 as_warn (_("Invalid performance register (%lu)"),
11884 (unsigned long) imm_expr.X_add_number);
11885 if (imm_expr.X_add_number != 0 && mips_opts.arch == CPU_R5900
11886 && (!strcmp(insn->name,"mfps") || !strcmp(insn->name,"mtps")))
11887 as_warn (_("Invalid performance register (%lu)"),
11888 (unsigned long) imm_expr.X_add_number);
11889 INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
11890 imm_expr.X_op = O_absent;
11891 s = expr_end;
11892 continue;
11893
11894 case 'G': /* Coprocessor destination register. */
11895 {
11896 unsigned long opcode = ip->insn_opcode;
11897 unsigned long mask;
11898 unsigned int types;
11899 int cop0;
11900
11901 if (mips_opts.micromips)
11902 {
11903 mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
11904 | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
11905 | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
11906 opcode &= mask;
11907 switch (opcode)
11908 {
11909 case 0x000000fc: /* mfc0 */
11910 case 0x000002fc: /* mtc0 */
11911 case 0x580000fc: /* dmfc0 */
11912 case 0x580002fc: /* dmtc0 */
11913 cop0 = 1;
11914 break;
11915 default:
11916 cop0 = 0;
11917 break;
11918 }
11919 }
11920 else
11921 {
11922 opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
11923 cop0 = opcode == OP_OP_COP0;
11924 }
11925 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
11926 ok = reg_lookup (&s, types, &regno);
11927 if (mips_opts.micromips)
11928 INSERT_OPERAND (1, RS, *ip, regno);
11929 else
11930 INSERT_OPERAND (0, RD, *ip, regno);
11931 if (ok)
11932 {
11933 lastregno = regno;
11934 continue;
11935 }
11936 }
11937 break;
11938
11939 case 'y': /* ALNV.PS source register. */
11940 gas_assert (mips_opts.micromips);
11941 goto do_reg;
11942 case 'x': /* Ignore register name. */
11943 case 'U': /* Destination register (CLO/CLZ). */
11944 case 'g': /* Coprocessor destination register. */
11945 gas_assert (!mips_opts.micromips);
11946 case 'b': /* Base register. */
11947 case 'd': /* Destination register. */
11948 case 's': /* Source register. */
11949 case 't': /* Target register. */
11950 case 'r': /* Both target and source. */
11951 case 'v': /* Both dest and source. */
11952 case 'w': /* Both dest and target. */
11953 case 'E': /* Coprocessor target register. */
11954 case 'K': /* RDHWR destination register. */
11955 case 'z': /* Must be zero register. */
11956 do_reg:
11957 s_reset = s;
11958 if (*args == 'E' || *args == 'K')
11959 ok = reg_lookup (&s, RTYPE_NUM, &regno);
11960 else
11961 {
11962 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
11963 if (regno == AT && mips_opts.at)
11964 {
11965 if (mips_opts.at == ATREG)
11966 as_warn (_("Used $at without \".set noat\""));
11967 else
11968 as_warn (_("Used $%u with \".set at=$%u\""),
11969 regno, mips_opts.at);
11970 }
11971 }
11972 if (ok)
11973 {
11974 c = *args;
11975 if (*s == ' ')
11976 ++s;
11977 if (args[1] != *s)
11978 {
11979 if (c == 'r' || c == 'v' || c == 'w')
11980 {
11981 regno = lastregno;
11982 s = s_reset;
11983 ++args;
11984 }
11985 }
11986 /* 'z' only matches $0. */
11987 if (c == 'z' && regno != 0)
11988 break;
11989
11990 if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
11991 {
11992 if (regno == lastregno)
11993 {
11994 insn_error
11995 = _("Source and destination must be different");
11996 continue;
11997 }
11998 if (regno == 31 && lastregno == 0xffffffff)
11999 {
12000 insn_error
12001 = _("A destination register must be supplied");
12002 continue;
12003 }
12004 }
12005 /* Now that we have assembled one operand, we use the args
12006 string to figure out where it goes in the instruction. */
12007 switch (c)
12008 {
12009 case 'r':
12010 case 's':
12011 case 'v':
12012 case 'b':
12013 INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
12014 break;
12015
12016 case 'K':
12017 if (mips_opts.micromips)
12018 INSERT_OPERAND (1, RS, *ip, regno);
12019 else
12020 INSERT_OPERAND (0, RD, *ip, regno);
12021 break;
12022
12023 case 'd':
12024 case 'g':
12025 INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
12026 break;
12027
12028 case 'U':
12029 gas_assert (!mips_opts.micromips);
12030 INSERT_OPERAND (0, RD, *ip, regno);
12031 INSERT_OPERAND (0, RT, *ip, regno);
12032 break;
12033
12034 case 'w':
12035 case 't':
12036 case 'E':
12037 INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
12038 break;
12039
12040 case 'y':
12041 gas_assert (mips_opts.micromips);
12042 INSERT_OPERAND (1, RS3, *ip, regno);
12043 break;
12044
12045 case 'x':
12046 /* This case exists because on the r3000 trunc
12047 expands into a macro which requires a gp
12048 register. On the r6000 or r4000 it is
12049 assembled into a single instruction which
12050 ignores the register. Thus the insn version
12051 is MIPS_ISA2 and uses 'x', and the macro
12052 version is MIPS_ISA1 and uses 't'. */
12053 break;
12054
12055 case 'z':
12056 /* This case is for the div instruction, which
12057 acts differently if the destination argument
12058 is $0. This only matches $0, and is checked
12059 outside the switch. */
12060 break;
12061 }
12062 lastregno = regno;
12063 continue;
12064 }
12065 switch (*args++)
12066 {
12067 case 'r':
12068 case 'v':
12069 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
12070 continue;
12071
12072 case 'w':
12073 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
12074 continue;
12075 }
12076 break;
12077
12078 case 'O': /* MDMX alignment immediate constant. */
12079 gas_assert (!mips_opts.micromips);
12080 my_getExpression (&imm_expr, s);
12081 check_absolute_expr (ip, &imm_expr);
12082 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
12083 as_warn (_("Improper align amount (%ld), using low bits"),
12084 (long) imm_expr.X_add_number);
12085 INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
12086 imm_expr.X_op = O_absent;
12087 s = expr_end;
12088 continue;
12089
12090 case 'Q': /* MDMX vector, element sel, or const. */
12091 if (s[0] != '$')
12092 {
12093 /* MDMX Immediate. */
12094 gas_assert (!mips_opts.micromips);
12095 my_getExpression (&imm_expr, s);
12096 check_absolute_expr (ip, &imm_expr);
12097 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
12098 as_warn (_("Invalid MDMX Immediate (%ld)"),
12099 (long) imm_expr.X_add_number);
12100 INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
12101 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12102 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12103 else
12104 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
12105 imm_expr.X_op = O_absent;
12106 s = expr_end;
12107 continue;
12108 }
12109 /* Not MDMX Immediate. Fall through. */
12110 case 'X': /* MDMX destination register. */
12111 case 'Y': /* MDMX source register. */
12112 case 'Z': /* MDMX target register. */
12113 is_mdmx = 1;
12114 case 'W':
12115 gas_assert (!mips_opts.micromips);
12116 case 'D': /* Floating point destination register. */
12117 case 'S': /* Floating point source register. */
12118 case 'T': /* Floating point target register. */
12119 case 'R': /* Floating point source register. */
12120 case 'V':
12121 rtype = RTYPE_FPU;
12122 if (is_mdmx
12123 || (mips_opts.ase_mdmx
12124 && (ip->insn_mo->pinfo & FP_D)
12125 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12126 | INSN_COPROC_MEMORY_DELAY
12127 | INSN_LOAD_COPROC_DELAY
12128 | INSN_LOAD_MEMORY_DELAY
12129 | INSN_STORE_MEMORY))))
12130 rtype |= RTYPE_VEC;
12131 s_reset = s;
12132 if (reg_lookup (&s, rtype, &regno))
12133 {
12134 if ((regno & 1) != 0
12135 && HAVE_32BIT_FPRS
12136 && !mips_oddfpreg_ok (ip->insn_mo, argnum))
12137 as_warn (_("Float register should be even, was %d"),
12138 regno);
12139
12140 c = *args;
12141 if (*s == ' ')
12142 ++s;
12143 if (args[1] != *s)
12144 {
12145 if (c == 'V' || c == 'W')
12146 {
12147 regno = lastregno;
12148 s = s_reset;
12149 ++args;
12150 }
12151 }
12152 switch (c)
12153 {
12154 case 'D':
12155 case 'X':
12156 INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
12157 break;
12158
12159 case 'V':
12160 case 'S':
12161 case 'Y':
12162 INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
12163 break;
12164
12165 case 'Q':
12166 /* This is like 'Z', but also needs to fix the MDMX
12167 vector/scalar select bits. Note that the
12168 scalar immediate case is handled above. */
12169 if (*s == '[')
12170 {
12171 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12172 int max_el = (is_qh ? 3 : 7);
12173 s++;
12174 my_getExpression(&imm_expr, s);
12175 check_absolute_expr (ip, &imm_expr);
12176 s = expr_end;
12177 if (imm_expr.X_add_number > max_el)
12178 as_bad (_("Bad element selector %ld"),
12179 (long) imm_expr.X_add_number);
12180 imm_expr.X_add_number &= max_el;
12181 ip->insn_opcode |= (imm_expr.X_add_number
12182 << (OP_SH_VSEL +
12183 (is_qh ? 2 : 1)));
12184 imm_expr.X_op = O_absent;
12185 if (*s != ']')
12186 as_warn (_("Expecting ']' found '%s'"), s);
12187 else
12188 s++;
12189 }
12190 else
12191 {
12192 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12193 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12194 << OP_SH_VSEL);
12195 else
12196 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12197 OP_SH_VSEL);
12198 }
12199 /* Fall through. */
12200 case 'W':
12201 case 'T':
12202 case 'Z':
12203 INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
12204 break;
12205
12206 case 'R':
12207 INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
12208 break;
12209 }
12210 lastregno = regno;
12211 continue;
12212 }
12213
12214 switch (*args++)
12215 {
12216 case 'V':
12217 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
12218 continue;
12219
12220 case 'W':
12221 INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
12222 continue;
12223 }
12224 break;
12225
12226 case 'I':
12227 my_getExpression (&imm_expr, s);
12228 if (imm_expr.X_op != O_big
12229 && imm_expr.X_op != O_constant)
12230 insn_error = _("absolute expression required");
12231 if (HAVE_32BIT_GPRS)
12232 normalize_constant_expr (&imm_expr);
12233 s = expr_end;
12234 continue;
12235
12236 case 'A':
12237 my_getExpression (&offset_expr, s);
12238 normalize_address_expr (&offset_expr);
12239 *imm_reloc = BFD_RELOC_32;
12240 s = expr_end;
12241 continue;
12242
12243 case 'F':
12244 case 'L':
12245 case 'f':
12246 case 'l':
12247 {
12248 int f64;
12249 int using_gprs;
12250 char *save_in;
12251 char *err;
12252 unsigned char temp[8];
12253 int len;
12254 unsigned int length;
12255 segT seg;
12256 subsegT subseg;
12257 char *p;
12258
12259 /* These only appear as the last operand in an
12260 instruction, and every instruction that accepts
12261 them in any variant accepts them in all variants.
12262 This means we don't have to worry about backing out
12263 any changes if the instruction does not match.
12264
12265 The difference between them is the size of the
12266 floating point constant and where it goes. For 'F'
12267 and 'L' the constant is 64 bits; for 'f' and 'l' it
12268 is 32 bits. Where the constant is placed is based
12269 on how the MIPS assembler does things:
12270 F -- .rdata
12271 L -- .lit8
12272 f -- immediate value
12273 l -- .lit4
12274
12275 The .lit4 and .lit8 sections are only used if
12276 permitted by the -G argument.
12277
12278 The code below needs to know whether the target register
12279 is 32 or 64 bits wide. It relies on the fact 'f' and
12280 'F' are used with GPR-based instructions and 'l' and
12281 'L' are used with FPR-based instructions. */
12282
12283 f64 = *args == 'F' || *args == 'L';
12284 using_gprs = *args == 'F' || *args == 'f';
12285
12286 save_in = input_line_pointer;
12287 input_line_pointer = s;
12288 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12289 length = len;
12290 s = input_line_pointer;
12291 input_line_pointer = save_in;
12292 if (err != NULL && *err != '\0')
12293 {
12294 as_bad (_("Bad floating point constant: %s"), err);
12295 memset (temp, '\0', sizeof temp);
12296 length = f64 ? 8 : 4;
12297 }
12298
12299 gas_assert (length == (unsigned) (f64 ? 8 : 4));
12300
12301 if (*args == 'f'
12302 || (*args == 'l'
12303 && (g_switch_value < 4
12304 || (temp[0] == 0 && temp[1] == 0)
12305 || (temp[2] == 0 && temp[3] == 0))))
12306 {
12307 imm_expr.X_op = O_constant;
12308 if (!target_big_endian)
12309 imm_expr.X_add_number = bfd_getl32 (temp);
12310 else
12311 imm_expr.X_add_number = bfd_getb32 (temp);
12312 }
12313 else if (length > 4
12314 && !mips_disable_float_construction
12315 /* Constants can only be constructed in GPRs and
12316 copied to FPRs if the GPRs are at least as wide
12317 as the FPRs. Force the constant into memory if
12318 we are using 64-bit FPRs but the GPRs are only
12319 32 bits wide. */
12320 && (using_gprs
12321 || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
12322 && ((temp[0] == 0 && temp[1] == 0)
12323 || (temp[2] == 0 && temp[3] == 0))
12324 && ((temp[4] == 0 && temp[5] == 0)
12325 || (temp[6] == 0 && temp[7] == 0)))
12326 {
12327 /* The value is simple enough to load with a couple of
12328 instructions. If using 32-bit registers, set
12329 imm_expr to the high order 32 bits and offset_expr to
12330 the low order 32 bits. Otherwise, set imm_expr to
12331 the entire 64 bit constant. */
12332 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
12333 {
12334 imm_expr.X_op = O_constant;
12335 offset_expr.X_op = O_constant;
12336 if (!target_big_endian)
12337 {
12338 imm_expr.X_add_number = bfd_getl32 (temp + 4);
12339 offset_expr.X_add_number = bfd_getl32 (temp);
12340 }
12341 else
12342 {
12343 imm_expr.X_add_number = bfd_getb32 (temp);
12344 offset_expr.X_add_number = bfd_getb32 (temp + 4);
12345 }
12346 if (offset_expr.X_add_number == 0)
12347 offset_expr.X_op = O_absent;
12348 }
12349 else if (sizeof (imm_expr.X_add_number) > 4)
12350 {
12351 imm_expr.X_op = O_constant;
12352 if (!target_big_endian)
12353 imm_expr.X_add_number = bfd_getl64 (temp);
12354 else
12355 imm_expr.X_add_number = bfd_getb64 (temp);
12356 }
12357 else
12358 {
12359 imm_expr.X_op = O_big;
12360 imm_expr.X_add_number = 4;
12361 if (!target_big_endian)
12362 {
12363 generic_bignum[0] = bfd_getl16 (temp);
12364 generic_bignum[1] = bfd_getl16 (temp + 2);
12365 generic_bignum[2] = bfd_getl16 (temp + 4);
12366 generic_bignum[3] = bfd_getl16 (temp + 6);
12367 }
12368 else
12369 {
12370 generic_bignum[0] = bfd_getb16 (temp + 6);
12371 generic_bignum[1] = bfd_getb16 (temp + 4);
12372 generic_bignum[2] = bfd_getb16 (temp + 2);
12373 generic_bignum[3] = bfd_getb16 (temp);
12374 }
12375 }
12376 }
12377 else
12378 {
12379 const char *newname;
12380 segT new_seg;
12381
12382 /* Switch to the right section. */
12383 seg = now_seg;
12384 subseg = now_subseg;
12385 switch (*args)
12386 {
12387 default: /* unused default case avoids warnings. */
12388 case 'L':
12389 newname = RDATA_SECTION_NAME;
12390 if (g_switch_value >= 8)
12391 newname = ".lit8";
12392 break;
12393 case 'F':
12394 newname = RDATA_SECTION_NAME;
12395 break;
12396 case 'l':
12397 gas_assert (g_switch_value >= 4);
12398 newname = ".lit4";
12399 break;
12400 }
12401 new_seg = subseg_new (newname, (subsegT) 0);
12402 if (IS_ELF)
12403 bfd_set_section_flags (stdoutput, new_seg,
12404 (SEC_ALLOC
12405 | SEC_LOAD
12406 | SEC_READONLY
12407 | SEC_DATA));
12408 frag_align (*args == 'l' ? 2 : 3, 0, 0);
12409 if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
12410 record_alignment (new_seg, 4);
12411 else
12412 record_alignment (new_seg, *args == 'l' ? 2 : 3);
12413 if (seg == now_seg)
12414 as_bad (_("Can't use floating point insn in this section"));
12415
12416 /* Set the argument to the current address in the
12417 section. */
12418 offset_expr.X_op = O_symbol;
12419 offset_expr.X_add_symbol = symbol_temp_new_now ();
12420 offset_expr.X_add_number = 0;
12421
12422 /* Put the floating point number into the section. */
12423 p = frag_more ((int) length);
12424 memcpy (p, temp, length);
12425
12426 /* Switch back to the original section. */
12427 subseg_set (seg, subseg);
12428 }
12429 }
12430 continue;
12431
12432 case 'i': /* 16-bit unsigned immediate. */
12433 case 'j': /* 16-bit signed immediate. */
12434 *imm_reloc = BFD_RELOC_LO16;
12435 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12436 {
12437 int more;
12438 offsetT minval, maxval;
12439
12440 more = (insn + 1 < past
12441 && strcmp (insn->name, insn[1].name) == 0);
12442
12443 /* If the expression was written as an unsigned number,
12444 only treat it as signed if there are no more
12445 alternatives. */
12446 if (more
12447 && *args == 'j'
12448 && sizeof (imm_expr.X_add_number) <= 4
12449 && imm_expr.X_op == O_constant
12450 && imm_expr.X_add_number < 0
12451 && imm_expr.X_unsigned
12452 && HAVE_64BIT_GPRS)
12453 break;
12454
12455 /* For compatibility with older assemblers, we accept
12456 0x8000-0xffff as signed 16-bit numbers when only
12457 signed numbers are allowed. */
12458 if (*args == 'i')
12459 minval = 0, maxval = 0xffff;
12460 else if (more)
12461 minval = -0x8000, maxval = 0x7fff;
12462 else
12463 minval = -0x8000, maxval = 0xffff;
12464
12465 if (imm_expr.X_op != O_constant
12466 || imm_expr.X_add_number < minval
12467 || imm_expr.X_add_number > maxval)
12468 {
12469 if (more)
12470 break;
12471 if (imm_expr.X_op == O_constant
12472 || imm_expr.X_op == O_big)
12473 as_bad (_("Expression out of range"));
12474 }
12475 }
12476 s = expr_end;
12477 continue;
12478
12479 case 'o': /* 16-bit offset. */
12480 offset_reloc[0] = BFD_RELOC_LO16;
12481 offset_reloc[1] = BFD_RELOC_UNUSED;
12482 offset_reloc[2] = BFD_RELOC_UNUSED;
12483
12484 /* Check whether there is only a single bracketed expression
12485 left. If so, it must be the base register and the
12486 constant must be zero. */
12487 if (*s == '(' && strchr (s + 1, '(') == 0)
12488 {
12489 offset_expr.X_op = O_constant;
12490 offset_expr.X_add_number = 0;
12491 continue;
12492 }
12493
12494 /* If this value won't fit into a 16 bit offset, then go
12495 find a macro that will generate the 32 bit offset
12496 code pattern. */
12497 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12498 && (offset_expr.X_op != O_constant
12499 || offset_expr.X_add_number >= 0x8000
12500 || offset_expr.X_add_number < -0x8000))
12501 break;
12502
12503 s = expr_end;
12504 continue;
12505
12506 case 'p': /* PC-relative offset. */
12507 *offset_reloc = BFD_RELOC_16_PCREL_S2;
12508 my_getExpression (&offset_expr, s);
12509 s = expr_end;
12510 continue;
12511
12512 case 'u': /* Upper 16 bits. */
12513 *imm_reloc = BFD_RELOC_LO16;
12514 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12515 && imm_expr.X_op == O_constant
12516 && (imm_expr.X_add_number < 0
12517 || imm_expr.X_add_number >= 0x10000))
12518 as_bad (_("lui expression (%lu) not in range 0..65535"),
12519 (unsigned long) imm_expr.X_add_number);
12520 s = expr_end;
12521 continue;
12522
12523 case 'a': /* 26-bit address. */
12524 *offset_reloc = BFD_RELOC_MIPS_JMP;
12525 my_getExpression (&offset_expr, s);
12526 s = expr_end;
12527 continue;
12528
12529 case 'N': /* 3-bit branch condition code. */
12530 case 'M': /* 3-bit compare condition code. */
12531 rtype = RTYPE_CCC;
12532 if (ip->insn_mo->pinfo & (FP_D | FP_S))
12533 rtype |= RTYPE_FCC;
12534 if (!reg_lookup (&s, rtype, &regno))
12535 break;
12536 if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12537 || strcmp (str + strlen (str) - 5, "any2f") == 0
12538 || strcmp (str + strlen (str) - 5, "any2t") == 0)
12539 && (regno & 1) != 0)
12540 as_warn (_("Condition code register should be even for %s, "
12541 "was %d"),
12542 str, regno);
12543 if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12544 || strcmp (str + strlen (str) - 5, "any4t") == 0)
12545 && (regno & 3) != 0)
12546 as_warn (_("Condition code register should be 0 or 4 for %s, "
12547 "was %d"),
12548 str, regno);
12549 if (*args == 'N')
12550 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12551 else
12552 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
12553 continue;
12554
12555 case 'H':
12556 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
12557 s += 2;
12558 if (ISDIGIT (*s))
12559 {
12560 c = 0;
12561 do
12562 {
12563 c *= 10;
12564 c += *s - '0';
12565 ++s;
12566 }
12567 while (ISDIGIT (*s));
12568 }
12569 else
12570 c = 8; /* Invalid sel value. */
12571
12572 if (c > 7)
12573 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
12574 INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
12575 continue;
12576
12577 case 'e':
12578 gas_assert (!mips_opts.micromips);
12579 /* Must be at least one digit. */
12580 my_getExpression (&imm_expr, s);
12581 check_absolute_expr (ip, &imm_expr);
12582
12583 if ((unsigned long) imm_expr.X_add_number
12584 > (unsigned long) OP_MASK_VECBYTE)
12585 {
12586 as_bad (_("bad byte vector index (%ld)"),
12587 (long) imm_expr.X_add_number);
12588 imm_expr.X_add_number = 0;
12589 }
12590
12591 INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
12592 imm_expr.X_op = O_absent;
12593 s = expr_end;
12594 continue;
12595
12596 case '%':
12597 gas_assert (!mips_opts.micromips);
12598 my_getExpression (&imm_expr, s);
12599 check_absolute_expr (ip, &imm_expr);
12600
12601 if ((unsigned long) imm_expr.X_add_number
12602 > (unsigned long) OP_MASK_VECALIGN)
12603 {
12604 as_bad (_("bad byte vector index (%ld)"),
12605 (long) imm_expr.X_add_number);
12606 imm_expr.X_add_number = 0;
12607 }
12608
12609 INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
12610 imm_expr.X_op = O_absent;
12611 s = expr_end;
12612 continue;
12613
12614 case 'm': /* Opcode extension character. */
12615 gas_assert (mips_opts.micromips);
12616 c = *++args;
12617 switch (c)
12618 {
12619 case 'r':
12620 if (strncmp (s, "$pc", 3) == 0)
12621 {
12622 s += 3;
12623 continue;
12624 }
12625 break;
12626
12627 case 'a':
12628 case 'b':
12629 case 'c':
12630 case 'd':
12631 case 'e':
12632 case 'f':
12633 case 'g':
12634 case 'h':
12635 case 'i':
12636 case 'j':
12637 case 'l':
12638 case 'm':
12639 case 'n':
12640 case 'p':
12641 case 'q':
12642 case 's':
12643 case 't':
12644 case 'x':
12645 case 'y':
12646 case 'z':
12647 s_reset = s;
12648 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12649 if (regno == AT && mips_opts.at)
12650 {
12651 if (mips_opts.at == ATREG)
12652 as_warn (_("Used $at without \".set noat\""));
12653 else
12654 as_warn (_("Used $%u with \".set at=$%u\""),
12655 regno, mips_opts.at);
12656 }
12657 if (!ok)
12658 {
12659 if (c == 'c')
12660 {
12661 gas_assert (args[1] == ',');
12662 regno = lastregno;
12663 ++args;
12664 }
12665 else if (c == 't')
12666 {
12667 gas_assert (args[1] == ',');
12668 ++args;
12669 continue; /* Nothing to do. */
12670 }
12671 else
12672 break;
12673 }
12674
12675 if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
12676 {
12677 if (regno == lastregno)
12678 {
12679 insn_error
12680 = _("Source and destination must be different");
12681 continue;
12682 }
12683 if (regno == 31 && lastregno == 0xffffffff)
12684 {
12685 insn_error
12686 = _("A destination register must be supplied");
12687 continue;
12688 }
12689 }
12690
12691 if (*s == ' ')
12692 ++s;
12693 if (args[1] != *s)
12694 {
12695 if (c == 'e')
12696 {
12697 gas_assert (args[1] == ',');
12698 regno = lastregno;
12699 s = s_reset;
12700 ++args;
12701 }
12702 else if (c == 't')
12703 {
12704 gas_assert (args[1] == ',');
12705 s = s_reset;
12706 ++args;
12707 continue; /* Nothing to do. */
12708 }
12709 }
12710
12711 /* Make sure regno is the same as lastregno. */
12712 if (c == 't' && regno != lastregno)
12713 break;
12714
12715 /* Make sure regno is the same as destregno. */
12716 if (c == 'x' && regno != destregno)
12717 break;
12718
12719 /* We need to save regno, before regno maps to the
12720 microMIPS register encoding. */
12721 lastregno = regno;
12722
12723 if (c == 'f')
12724 destregno = regno;
12725
12726 switch (c)
12727 {
12728 case 'a':
12729 if (regno != GP)
12730 regno = ILLEGAL_REG;
12731 break;
12732
12733 case 'b':
12734 regno = mips32_to_micromips_reg_b_map[regno];
12735 break;
12736
12737 case 'c':
12738 regno = mips32_to_micromips_reg_c_map[regno];
12739 break;
12740
12741 case 'd':
12742 regno = mips32_to_micromips_reg_d_map[regno];
12743 break;
12744
12745 case 'e':
12746 regno = mips32_to_micromips_reg_e_map[regno];
12747 break;
12748
12749 case 'f':
12750 regno = mips32_to_micromips_reg_f_map[regno];
12751 break;
12752
12753 case 'g':
12754 regno = mips32_to_micromips_reg_g_map[regno];
12755 break;
12756
12757 case 'h':
12758 regno = mips32_to_micromips_reg_h_map[regno];
12759 break;
12760
12761 case 'i':
12762 switch (EXTRACT_OPERAND (1, MI, *ip))
12763 {
12764 case 4:
12765 if (regno == 21)
12766 regno = 3;
12767 else if (regno == 22)
12768 regno = 4;
12769 else if (regno == 5)
12770 regno = 5;
12771 else if (regno == 6)
12772 regno = 6;
12773 else if (regno == 7)
12774 regno = 7;
12775 else
12776 regno = ILLEGAL_REG;
12777 break;
12778
12779 case 5:
12780 if (regno == 6)
12781 regno = 0;
12782 else if (regno == 7)
12783 regno = 1;
12784 else
12785 regno = ILLEGAL_REG;
12786 break;
12787
12788 case 6:
12789 if (regno == 7)
12790 regno = 2;
12791 else
12792 regno = ILLEGAL_REG;
12793 break;
12794
12795 default:
12796 regno = ILLEGAL_REG;
12797 break;
12798 }
12799 break;
12800
12801 case 'l':
12802 regno = mips32_to_micromips_reg_l_map[regno];
12803 break;
12804
12805 case 'm':
12806 regno = mips32_to_micromips_reg_m_map[regno];
12807 break;
12808
12809 case 'n':
12810 regno = mips32_to_micromips_reg_n_map[regno];
12811 break;
12812
12813 case 'q':
12814 regno = mips32_to_micromips_reg_q_map[regno];
12815 break;
12816
12817 case 's':
12818 if (regno != SP)
12819 regno = ILLEGAL_REG;
12820 break;
12821
12822 case 'y':
12823 if (regno != 31)
12824 regno = ILLEGAL_REG;
12825 break;
12826
12827 case 'z':
12828 if (regno != ZERO)
12829 regno = ILLEGAL_REG;
12830 break;
12831
12832 case 'j': /* Do nothing. */
12833 case 'p':
12834 case 't':
12835 case 'x':
12836 break;
12837
12838 default:
12839 abort ();
12840 }
12841
12842 if (regno == ILLEGAL_REG)
12843 break;
12844
12845 switch (c)
12846 {
12847 case 'b':
12848 INSERT_OPERAND (1, MB, *ip, regno);
12849 break;
12850
12851 case 'c':
12852 INSERT_OPERAND (1, MC, *ip, regno);
12853 break;
12854
12855 case 'd':
12856 INSERT_OPERAND (1, MD, *ip, regno);
12857 break;
12858
12859 case 'e':
12860 INSERT_OPERAND (1, ME, *ip, regno);
12861 break;
12862
12863 case 'f':
12864 INSERT_OPERAND (1, MF, *ip, regno);
12865 break;
12866
12867 case 'g':
12868 INSERT_OPERAND (1, MG, *ip, regno);
12869 break;
12870
12871 case 'h':
12872 INSERT_OPERAND (1, MH, *ip, regno);
12873 break;
12874
12875 case 'i':
12876 INSERT_OPERAND (1, MI, *ip, regno);
12877 break;
12878
12879 case 'j':
12880 INSERT_OPERAND (1, MJ, *ip, regno);
12881 break;
12882
12883 case 'l':
12884 INSERT_OPERAND (1, ML, *ip, regno);
12885 break;
12886
12887 case 'm':
12888 INSERT_OPERAND (1, MM, *ip, regno);
12889 break;
12890
12891 case 'n':
12892 INSERT_OPERAND (1, MN, *ip, regno);
12893 break;
12894
12895 case 'p':
12896 INSERT_OPERAND (1, MP, *ip, regno);
12897 break;
12898
12899 case 'q':
12900 INSERT_OPERAND (1, MQ, *ip, regno);
12901 break;
12902
12903 case 'a': /* Do nothing. */
12904 case 's': /* Do nothing. */
12905 case 't': /* Do nothing. */
12906 case 'x': /* Do nothing. */
12907 case 'y': /* Do nothing. */
12908 case 'z': /* Do nothing. */
12909 break;
12910
12911 default:
12912 abort ();
12913 }
12914 continue;
12915
12916 case 'A':
12917 {
12918 bfd_reloc_code_real_type r[3];
12919 expressionS ep;
12920 int imm;
12921
12922 /* Check whether there is only a single bracketed
12923 expression left. If so, it must be the base register
12924 and the constant must be zero. */
12925 if (*s == '(' && strchr (s + 1, '(') == 0)
12926 {
12927 INSERT_OPERAND (1, IMMA, *ip, 0);
12928 continue;
12929 }
12930
12931 if (my_getSmallExpression (&ep, r, s) > 0
12932 || !expr_const_in_range (&ep, -64, 64, 2))
12933 break;
12934
12935 imm = ep.X_add_number >> 2;
12936 INSERT_OPERAND (1, IMMA, *ip, imm);
12937 }
12938 s = expr_end;
12939 continue;
12940
12941 case 'B':
12942 {
12943 bfd_reloc_code_real_type r[3];
12944 expressionS ep;
12945 int imm;
12946
12947 if (my_getSmallExpression (&ep, r, s) > 0
12948 || ep.X_op != O_constant)
12949 break;
12950
12951 for (imm = 0; imm < 8; imm++)
12952 if (micromips_imm_b_map[imm] == ep.X_add_number)
12953 break;
12954 if (imm >= 8)
12955 break;
12956
12957 INSERT_OPERAND (1, IMMB, *ip, imm);
12958 }
12959 s = expr_end;
12960 continue;
12961
12962 case 'C':
12963 {
12964 bfd_reloc_code_real_type r[3];
12965 expressionS ep;
12966 int imm;
12967
12968 if (my_getSmallExpression (&ep, r, s) > 0
12969 || ep.X_op != O_constant)
12970 break;
12971
12972 for (imm = 0; imm < 16; imm++)
12973 if (micromips_imm_c_map[imm] == ep.X_add_number)
12974 break;
12975 if (imm >= 16)
12976 break;
12977
12978 INSERT_OPERAND (1, IMMC, *ip, imm);
12979 }
12980 s = expr_end;
12981 continue;
12982
12983 case 'D': /* pc relative offset */
12984 case 'E': /* pc relative offset */
12985 my_getExpression (&offset_expr, s);
12986 if (offset_expr.X_op == O_register)
12987 break;
12988
12989 if (!forced_insn_length)
12990 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
12991 else if (c == 'D')
12992 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
12993 else
12994 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
12995 s = expr_end;
12996 continue;
12997
12998 case 'F':
12999 {
13000 bfd_reloc_code_real_type r[3];
13001 expressionS ep;
13002 int imm;
13003
13004 if (my_getSmallExpression (&ep, r, s) > 0
13005 || !expr_const_in_range (&ep, 0, 16, 0))
13006 break;
13007
13008 imm = ep.X_add_number;
13009 INSERT_OPERAND (1, IMMF, *ip, imm);
13010 }
13011 s = expr_end;
13012 continue;
13013
13014 case 'G':
13015 {
13016 bfd_reloc_code_real_type r[3];
13017 expressionS ep;
13018 int imm;
13019
13020 /* Check whether there is only a single bracketed
13021 expression left. If so, it must be the base register
13022 and the constant must be zero. */
13023 if (*s == '(' && strchr (s + 1, '(') == 0)
13024 {
13025 INSERT_OPERAND (1, IMMG, *ip, 0);
13026 continue;
13027 }
13028
13029 if (my_getSmallExpression (&ep, r, s) > 0
13030 || !expr_const_in_range (&ep, -1, 15, 0))
13031 break;
13032
13033 imm = ep.X_add_number & 15;
13034 INSERT_OPERAND (1, IMMG, *ip, imm);
13035 }
13036 s = expr_end;
13037 continue;
13038
13039 case 'H':
13040 {
13041 bfd_reloc_code_real_type r[3];
13042 expressionS ep;
13043 int imm;
13044
13045 /* Check whether there is only a single bracketed
13046 expression left. If so, it must be the base register
13047 and the constant must be zero. */
13048 if (*s == '(' && strchr (s + 1, '(') == 0)
13049 {
13050 INSERT_OPERAND (1, IMMH, *ip, 0);
13051 continue;
13052 }
13053
13054 if (my_getSmallExpression (&ep, r, s) > 0
13055 || !expr_const_in_range (&ep, 0, 16, 1))
13056 break;
13057
13058 imm = ep.X_add_number >> 1;
13059 INSERT_OPERAND (1, IMMH, *ip, imm);
13060 }
13061 s = expr_end;
13062 continue;
13063
13064 case 'I':
13065 {
13066 bfd_reloc_code_real_type r[3];
13067 expressionS ep;
13068 int imm;
13069
13070 if (my_getSmallExpression (&ep, r, s) > 0
13071 || !expr_const_in_range (&ep, -1, 127, 0))
13072 break;
13073
13074 imm = ep.X_add_number & 127;
13075 INSERT_OPERAND (1, IMMI, *ip, imm);
13076 }
13077 s = expr_end;
13078 continue;
13079
13080 case 'J':
13081 {
13082 bfd_reloc_code_real_type r[3];
13083 expressionS ep;
13084 int imm;
13085
13086 /* Check whether there is only a single bracketed
13087 expression left. If so, it must be the base register
13088 and the constant must be zero. */
13089 if (*s == '(' && strchr (s + 1, '(') == 0)
13090 {
13091 INSERT_OPERAND (1, IMMJ, *ip, 0);
13092 continue;
13093 }
13094
13095 if (my_getSmallExpression (&ep, r, s) > 0
13096 || !expr_const_in_range (&ep, 0, 16, 2))
13097 break;
13098
13099 imm = ep.X_add_number >> 2;
13100 INSERT_OPERAND (1, IMMJ, *ip, imm);
13101 }
13102 s = expr_end;
13103 continue;
13104
13105 case 'L':
13106 {
13107 bfd_reloc_code_real_type r[3];
13108 expressionS ep;
13109 int imm;
13110
13111 /* Check whether there is only a single bracketed
13112 expression left. If so, it must be the base register
13113 and the constant must be zero. */
13114 if (*s == '(' && strchr (s + 1, '(') == 0)
13115 {
13116 INSERT_OPERAND (1, IMML, *ip, 0);
13117 continue;
13118 }
13119
13120 if (my_getSmallExpression (&ep, r, s) > 0
13121 || !expr_const_in_range (&ep, 0, 16, 0))
13122 break;
13123
13124 imm = ep.X_add_number;
13125 INSERT_OPERAND (1, IMML, *ip, imm);
13126 }
13127 s = expr_end;
13128 continue;
13129
13130 case 'M':
13131 {
13132 bfd_reloc_code_real_type r[3];
13133 expressionS ep;
13134 int imm;
13135
13136 if (my_getSmallExpression (&ep, r, s) > 0
13137 || !expr_const_in_range (&ep, 1, 9, 0))
13138 break;
13139
13140 imm = ep.X_add_number & 7;
13141 INSERT_OPERAND (1, IMMM, *ip, imm);
13142 }
13143 s = expr_end;
13144 continue;
13145
13146 case 'N': /* Register list for lwm and swm. */
13147 {
13148 /* A comma-separated list of registers and/or
13149 dash-separated contiguous ranges including
13150 both ra and a set of one or more registers
13151 starting at s0 up to s3 which have to be
13152 consecutive, e.g.:
13153
13154 s0, ra
13155 s0, s1, ra, s2, s3
13156 s0-s2, ra
13157
13158 and any permutations of these. */
13159 unsigned int reglist;
13160 int imm;
13161
13162 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13163 break;
13164
13165 if ((reglist & 0xfff1ffff) != 0x80010000)
13166 break;
13167
13168 reglist = (reglist >> 17) & 7;
13169 reglist += 1;
13170 if ((reglist & -reglist) != reglist)
13171 break;
13172
13173 imm = ffs (reglist) - 1;
13174 INSERT_OPERAND (1, IMMN, *ip, imm);
13175 }
13176 continue;
13177
13178 case 'O': /* sdbbp 4-bit code. */
13179 {
13180 bfd_reloc_code_real_type r[3];
13181 expressionS ep;
13182 int imm;
13183
13184 if (my_getSmallExpression (&ep, r, s) > 0
13185 || !expr_const_in_range (&ep, 0, 16, 0))
13186 break;
13187
13188 imm = ep.X_add_number;
13189 INSERT_OPERAND (1, IMMO, *ip, imm);
13190 }
13191 s = expr_end;
13192 continue;
13193
13194 case 'P':
13195 {
13196 bfd_reloc_code_real_type r[3];
13197 expressionS ep;
13198 int imm;
13199
13200 if (my_getSmallExpression (&ep, r, s) > 0
13201 || !expr_const_in_range (&ep, 0, 32, 2))
13202 break;
13203
13204 imm = ep.X_add_number >> 2;
13205 INSERT_OPERAND (1, IMMP, *ip, imm);
13206 }
13207 s = expr_end;
13208 continue;
13209
13210 case 'Q':
13211 {
13212 bfd_reloc_code_real_type r[3];
13213 expressionS ep;
13214 int imm;
13215
13216 if (my_getSmallExpression (&ep, r, s) > 0
13217 || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13218 break;
13219
13220 imm = ep.X_add_number >> 2;
13221 INSERT_OPERAND (1, IMMQ, *ip, imm);
13222 }
13223 s = expr_end;
13224 continue;
13225
13226 case 'U':
13227 {
13228 bfd_reloc_code_real_type r[3];
13229 expressionS ep;
13230 int imm;
13231
13232 /* Check whether there is only a single bracketed
13233 expression left. If so, it must be the base register
13234 and the constant must be zero. */
13235 if (*s == '(' && strchr (s + 1, '(') == 0)
13236 {
13237 INSERT_OPERAND (1, IMMU, *ip, 0);
13238 continue;
13239 }
13240
13241 if (my_getSmallExpression (&ep, r, s) > 0
13242 || !expr_const_in_range (&ep, 0, 32, 2))
13243 break;
13244
13245 imm = ep.X_add_number >> 2;
13246 INSERT_OPERAND (1, IMMU, *ip, imm);
13247 }
13248 s = expr_end;
13249 continue;
13250
13251 case 'W':
13252 {
13253 bfd_reloc_code_real_type r[3];
13254 expressionS ep;
13255 int imm;
13256
13257 if (my_getSmallExpression (&ep, r, s) > 0
13258 || !expr_const_in_range (&ep, 0, 64, 2))
13259 break;
13260
13261 imm = ep.X_add_number >> 2;
13262 INSERT_OPERAND (1, IMMW, *ip, imm);
13263 }
13264 s = expr_end;
13265 continue;
13266
13267 case 'X':
13268 {
13269 bfd_reloc_code_real_type r[3];
13270 expressionS ep;
13271 int imm;
13272
13273 if (my_getSmallExpression (&ep, r, s) > 0
13274 || !expr_const_in_range (&ep, -8, 8, 0))
13275 break;
13276
13277 imm = ep.X_add_number;
13278 INSERT_OPERAND (1, IMMX, *ip, imm);
13279 }
13280 s = expr_end;
13281 continue;
13282
13283 case 'Y':
13284 {
13285 bfd_reloc_code_real_type r[3];
13286 expressionS ep;
13287 int imm;
13288
13289 if (my_getSmallExpression (&ep, r, s) > 0
13290 || expr_const_in_range (&ep, -2, 2, 2)
13291 || !expr_const_in_range (&ep, -258, 258, 2))
13292 break;
13293
13294 imm = ep.X_add_number >> 2;
13295 imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13296 INSERT_OPERAND (1, IMMY, *ip, imm);
13297 }
13298 s = expr_end;
13299 continue;
13300
13301 case 'Z':
13302 {
13303 bfd_reloc_code_real_type r[3];
13304 expressionS ep;
13305
13306 if (my_getSmallExpression (&ep, r, s) > 0
13307 || !expr_const_in_range (&ep, 0, 1, 0))
13308 break;
13309 }
13310 s = expr_end;
13311 continue;
13312
13313 default:
13314 as_bad (_("Internal error: bad microMIPS opcode "
13315 "(unknown extension operand type `m%c'): %s %s"),
13316 *args, insn->name, insn->args);
13317 /* Further processing is fruitless. */
13318 return;
13319 }
13320 break;
13321
13322 case 'n': /* Register list for 32-bit lwm and swm. */
13323 gas_assert (mips_opts.micromips);
13324 {
13325 /* A comma-separated list of registers and/or
13326 dash-separated contiguous ranges including
13327 at least one of ra and a set of one or more
13328 registers starting at s0 up to s7 and then
13329 s8 which have to be consecutive, e.g.:
13330
13331 ra
13332 s0
13333 ra, s0, s1, s2
13334 s0-s8
13335 s0-s5, ra
13336
13337 and any permutations of these. */
13338 unsigned int reglist;
13339 int imm;
13340 int ra;
13341
13342 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13343 break;
13344
13345 if ((reglist & 0x3f00ffff) != 0)
13346 break;
13347
13348 ra = (reglist >> 27) & 0x10;
13349 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13350 reglist += 1;
13351 if ((reglist & -reglist) != reglist)
13352 break;
13353
13354 imm = (ffs (reglist) - 1) | ra;
13355 INSERT_OPERAND (1, RT, *ip, imm);
13356 imm_expr.X_op = O_absent;
13357 }
13358 continue;
13359
13360 case '|': /* 4-bit trap code. */
13361 gas_assert (mips_opts.micromips);
13362 my_getExpression (&imm_expr, s);
13363 check_absolute_expr (ip, &imm_expr);
13364 if ((unsigned long) imm_expr.X_add_number
13365 > MICROMIPSOP_MASK_TRAP)
13366 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13367 (unsigned long) imm_expr.X_add_number,
13368 ip->insn_mo->name);
13369 INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
13370 imm_expr.X_op = O_absent;
13371 s = expr_end;
13372 continue;
13373
13374 default:
13375 as_bad (_("Bad char = '%c'\n"), *args);
13376 abort ();
13377 }
13378 break;
13379 }
13380 /* Args don't match. */
13381 s = argsStart;
13382 insn_error = _("Illegal operands");
13383 if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
13384 {
13385 ++insn;
13386 continue;
13387 }
13388 else if (wrong_delay_slot_insns && need_delay_slot_ok)
13389 {
13390 gas_assert (firstinsn);
13391 need_delay_slot_ok = FALSE;
13392 past = insn + 1;
13393 insn = firstinsn;
13394 continue;
13395 }
13396 return;
13397 }
13398 }
13399
13400 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13401
13402 /* This routine assembles an instruction into its binary format when
13403 assembling for the mips16. As a side effect, it sets one of the
13404 global variables imm_reloc or offset_reloc to the type of relocation
13405 to do if one of the operands is an address expression. It also sets
13406 forced_insn_length to the resulting instruction size in bytes if the
13407 user explicitly requested a small or extended instruction. */
13408
13409 static void
13410 mips16_ip (char *str, struct mips_cl_insn *ip)
13411 {
13412 char *s;
13413 const char *args;
13414 struct mips_opcode *insn;
13415 char *argsstart;
13416 unsigned int regno;
13417 unsigned int lastregno = 0;
13418 char *s_reset;
13419 size_t i;
13420
13421 insn_error = NULL;
13422
13423 forced_insn_length = 0;
13424
13425 for (s = str; ISLOWER (*s); ++s)
13426 ;
13427 switch (*s)
13428 {
13429 case '\0':
13430 break;
13431
13432 case ' ':
13433 *s++ = '\0';
13434 break;
13435
13436 case '.':
13437 if (s[1] == 't' && s[2] == ' ')
13438 {
13439 *s = '\0';
13440 forced_insn_length = 2;
13441 s += 3;
13442 break;
13443 }
13444 else if (s[1] == 'e' && s[2] == ' ')
13445 {
13446 *s = '\0';
13447 forced_insn_length = 4;
13448 s += 3;
13449 break;
13450 }
13451 /* Fall through. */
13452 default:
13453 insn_error = _("unknown opcode");
13454 return;
13455 }
13456
13457 if (mips_opts.noautoextend && !forced_insn_length)
13458 forced_insn_length = 2;
13459
13460 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13461 {
13462 insn_error = _("unrecognized opcode");
13463 return;
13464 }
13465
13466 argsstart = s;
13467 for (;;)
13468 {
13469 bfd_boolean ok;
13470
13471 gas_assert (strcmp (insn->name, str) == 0);
13472
13473 ok = is_opcode_valid_16 (insn);
13474 if (! ok)
13475 {
13476 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13477 && strcmp (insn->name, insn[1].name) == 0)
13478 {
13479 ++insn;
13480 continue;
13481 }
13482 else
13483 {
13484 if (!insn_error)
13485 {
13486 static char buf[100];
13487 sprintf (buf,
13488 _("Opcode not supported on this processor: %s (%s)"),
13489 mips_cpu_info_from_arch (mips_opts.arch)->name,
13490 mips_cpu_info_from_isa (mips_opts.isa)->name);
13491 insn_error = buf;
13492 }
13493 return;
13494 }
13495 }
13496
13497 create_insn (ip, insn);
13498 imm_expr.X_op = O_absent;
13499 imm_reloc[0] = BFD_RELOC_UNUSED;
13500 imm_reloc[1] = BFD_RELOC_UNUSED;
13501 imm_reloc[2] = BFD_RELOC_UNUSED;
13502 imm2_expr.X_op = O_absent;
13503 offset_expr.X_op = O_absent;
13504 offset_reloc[0] = BFD_RELOC_UNUSED;
13505 offset_reloc[1] = BFD_RELOC_UNUSED;
13506 offset_reloc[2] = BFD_RELOC_UNUSED;
13507 for (args = insn->args; 1; ++args)
13508 {
13509 int c;
13510
13511 if (*s == ' ')
13512 ++s;
13513
13514 /* In this switch statement we call break if we did not find
13515 a match, continue if we did find a match, or return if we
13516 are done. */
13517
13518 c = *args;
13519 switch (c)
13520 {
13521 case '\0':
13522 if (*s == '\0')
13523 {
13524 offsetT value;
13525
13526 /* Stuff the immediate value in now, if we can. */
13527 if (imm_expr.X_op == O_constant
13528 && *imm_reloc > BFD_RELOC_UNUSED
13529 && insn->pinfo != INSN_MACRO
13530 && calculate_reloc (*offset_reloc,
13531 imm_expr.X_add_number, &value))
13532 {
13533 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
13534 *offset_reloc, value, forced_insn_length,
13535 &ip->insn_opcode);
13536 imm_expr.X_op = O_absent;
13537 *imm_reloc = BFD_RELOC_UNUSED;
13538 *offset_reloc = BFD_RELOC_UNUSED;
13539 }
13540
13541 return;
13542 }
13543 break;
13544
13545 case ',':
13546 if (*s++ == c)
13547 continue;
13548 s--;
13549 switch (*++args)
13550 {
13551 case 'v':
13552 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13553 continue;
13554 case 'w':
13555 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13556 continue;
13557 }
13558 break;
13559
13560 case '(':
13561 case ')':
13562 if (*s++ == c)
13563 continue;
13564 break;
13565
13566 case 'v':
13567 case 'w':
13568 if (s[0] != '$')
13569 {
13570 if (c == 'v')
13571 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13572 else
13573 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13574 ++args;
13575 continue;
13576 }
13577 /* Fall through. */
13578 case 'x':
13579 case 'y':
13580 case 'z':
13581 case 'Z':
13582 case '0':
13583 case 'S':
13584 case 'R':
13585 case 'X':
13586 case 'Y':
13587 s_reset = s;
13588 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
13589 {
13590 if (c == 'v' || c == 'w')
13591 {
13592 if (c == 'v')
13593 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13594 else
13595 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13596 ++args;
13597 continue;
13598 }
13599 break;
13600 }
13601
13602 if (*s == ' ')
13603 ++s;
13604 if (args[1] != *s)
13605 {
13606 if (c == 'v' || c == 'w')
13607 {
13608 regno = mips16_to_32_reg_map[lastregno];
13609 s = s_reset;
13610 ++args;
13611 }
13612 }
13613
13614 switch (c)
13615 {
13616 case 'x':
13617 case 'y':
13618 case 'z':
13619 case 'v':
13620 case 'w':
13621 case 'Z':
13622 regno = mips32_to_16_reg_map[regno];
13623 break;
13624
13625 case '0':
13626 if (regno != 0)
13627 regno = ILLEGAL_REG;
13628 break;
13629
13630 case 'S':
13631 if (regno != SP)
13632 regno = ILLEGAL_REG;
13633 break;
13634
13635 case 'R':
13636 if (regno != RA)
13637 regno = ILLEGAL_REG;
13638 break;
13639
13640 case 'X':
13641 case 'Y':
13642 if (regno == AT && mips_opts.at)
13643 {
13644 if (mips_opts.at == ATREG)
13645 as_warn (_("used $at without \".set noat\""));
13646 else
13647 as_warn (_("used $%u with \".set at=$%u\""),
13648 regno, mips_opts.at);
13649 }
13650 break;
13651
13652 default:
13653 abort ();
13654 }
13655
13656 if (regno == ILLEGAL_REG)
13657 break;
13658
13659 switch (c)
13660 {
13661 case 'x':
13662 case 'v':
13663 MIPS16_INSERT_OPERAND (RX, *ip, regno);
13664 break;
13665 case 'y':
13666 case 'w':
13667 MIPS16_INSERT_OPERAND (RY, *ip, regno);
13668 break;
13669 case 'z':
13670 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
13671 break;
13672 case 'Z':
13673 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
13674 case '0':
13675 case 'S':
13676 case 'R':
13677 break;
13678 case 'X':
13679 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
13680 break;
13681 case 'Y':
13682 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
13683 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
13684 break;
13685 default:
13686 abort ();
13687 }
13688
13689 lastregno = regno;
13690 continue;
13691
13692 case 'P':
13693 if (strncmp (s, "$pc", 3) == 0)
13694 {
13695 s += 3;
13696 continue;
13697 }
13698 break;
13699
13700 case '5':
13701 case 'H':
13702 case 'W':
13703 case 'D':
13704 case 'j':
13705 case 'V':
13706 case 'C':
13707 case 'U':
13708 case 'k':
13709 case 'K':
13710 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
13711 if (i > 0)
13712 {
13713 if (imm_expr.X_op != O_constant)
13714 {
13715 forced_insn_length = 4;
13716 ip->insn_opcode |= MIPS16_EXTEND;
13717 }
13718 else
13719 {
13720 /* We need to relax this instruction. */
13721 *offset_reloc = *imm_reloc;
13722 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13723 }
13724 s = expr_end;
13725 continue;
13726 }
13727 *imm_reloc = BFD_RELOC_UNUSED;
13728 /* Fall through. */
13729 case '<':
13730 case '>':
13731 case '[':
13732 case ']':
13733 case '4':
13734 case '8':
13735 my_getExpression (&imm_expr, s);
13736 if (imm_expr.X_op == O_register)
13737 {
13738 /* What we thought was an expression turned out to
13739 be a register. */
13740
13741 if (s[0] == '(' && args[1] == '(')
13742 {
13743 /* It looks like the expression was omitted
13744 before a register indirection, which means
13745 that the expression is implicitly zero. We
13746 still set up imm_expr, so that we handle
13747 explicit extensions correctly. */
13748 imm_expr.X_op = O_constant;
13749 imm_expr.X_add_number = 0;
13750 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13751 continue;
13752 }
13753
13754 break;
13755 }
13756
13757 /* We need to relax this instruction. */
13758 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13759 s = expr_end;
13760 continue;
13761
13762 case 'p':
13763 case 'q':
13764 case 'A':
13765 case 'B':
13766 case 'E':
13767 /* We use offset_reloc rather than imm_reloc for the PC
13768 relative operands. This lets macros with both
13769 immediate and address operands work correctly. */
13770 my_getExpression (&offset_expr, s);
13771
13772 if (offset_expr.X_op == O_register)
13773 break;
13774
13775 /* We need to relax this instruction. */
13776 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13777 s = expr_end;
13778 continue;
13779
13780 case '6': /* break code */
13781 my_getExpression (&imm_expr, s);
13782 check_absolute_expr (ip, &imm_expr);
13783 if ((unsigned long) imm_expr.X_add_number > 63)
13784 as_warn (_("Invalid value for `%s' (%lu)"),
13785 ip->insn_mo->name,
13786 (unsigned long) imm_expr.X_add_number);
13787 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
13788 imm_expr.X_op = O_absent;
13789 s = expr_end;
13790 continue;
13791
13792 case 'a': /* 26 bit address */
13793 my_getExpression (&offset_expr, s);
13794 s = expr_end;
13795 *offset_reloc = BFD_RELOC_MIPS16_JMP;
13796 ip->insn_opcode <<= 16;
13797 continue;
13798
13799 case 'l': /* register list for entry macro */
13800 case 'L': /* register list for exit macro */
13801 {
13802 int mask;
13803
13804 if (c == 'l')
13805 mask = 0;
13806 else
13807 mask = 7 << 3;
13808 while (*s != '\0')
13809 {
13810 unsigned int freg, reg1, reg2;
13811
13812 while (*s == ' ' || *s == ',')
13813 ++s;
13814 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
13815 freg = 0;
13816 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
13817 freg = 1;
13818 else
13819 {
13820 as_bad (_("can't parse register list"));
13821 break;
13822 }
13823 if (*s == ' ')
13824 ++s;
13825 if (*s != '-')
13826 reg2 = reg1;
13827 else
13828 {
13829 ++s;
13830 if (!reg_lookup (&s, freg ? RTYPE_FPU
13831 : (RTYPE_GP | RTYPE_NUM), &reg2))
13832 {
13833 as_bad (_("invalid register list"));
13834 break;
13835 }
13836 }
13837 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
13838 {
13839 mask &= ~ (7 << 3);
13840 mask |= 5 << 3;
13841 }
13842 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
13843 {
13844 mask &= ~ (7 << 3);
13845 mask |= 6 << 3;
13846 }
13847 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
13848 mask |= (reg2 - 3) << 3;
13849 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
13850 mask |= (reg2 - 15) << 1;
13851 else if (reg1 == RA && reg2 == RA)
13852 mask |= 1;
13853 else
13854 {
13855 as_bad (_("invalid register list"));
13856 break;
13857 }
13858 }
13859 /* The mask is filled in in the opcode table for the
13860 benefit of the disassembler. We remove it before
13861 applying the actual mask. */
13862 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
13863 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
13864 }
13865 continue;
13866
13867 case 'm': /* Register list for save insn. */
13868 case 'M': /* Register list for restore insn. */
13869 {
13870 int opcode = ip->insn_opcode;
13871 int framesz = 0, seen_framesz = 0;
13872 int nargs = 0, statics = 0, sregs = 0;
13873
13874 while (*s != '\0')
13875 {
13876 unsigned int reg1, reg2;
13877
13878 SKIP_SPACE_TABS (s);
13879 while (*s == ',')
13880 ++s;
13881 SKIP_SPACE_TABS (s);
13882
13883 my_getExpression (&imm_expr, s);
13884 if (imm_expr.X_op == O_constant)
13885 {
13886 /* Handle the frame size. */
13887 if (seen_framesz)
13888 {
13889 as_bad (_("more than one frame size in list"));
13890 break;
13891 }
13892 seen_framesz = 1;
13893 framesz = imm_expr.X_add_number;
13894 imm_expr.X_op = O_absent;
13895 s = expr_end;
13896 continue;
13897 }
13898
13899 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
13900 {
13901 as_bad (_("can't parse register list"));
13902 break;
13903 }
13904
13905 while (*s == ' ')
13906 ++s;
13907
13908 if (*s != '-')
13909 reg2 = reg1;
13910 else
13911 {
13912 ++s;
13913 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
13914 || reg2 < reg1)
13915 {
13916 as_bad (_("can't parse register list"));
13917 break;
13918 }
13919 }
13920
13921 while (reg1 <= reg2)
13922 {
13923 if (reg1 >= 4 && reg1 <= 7)
13924 {
13925 if (!seen_framesz)
13926 /* args $a0-$a3 */
13927 nargs |= 1 << (reg1 - 4);
13928 else
13929 /* statics $a0-$a3 */
13930 statics |= 1 << (reg1 - 4);
13931 }
13932 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
13933 {
13934 /* $s0-$s8 */
13935 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
13936 }
13937 else if (reg1 == 31)
13938 {
13939 /* Add $ra to insn. */
13940 opcode |= 0x40;
13941 }
13942 else
13943 {
13944 as_bad (_("unexpected register in list"));
13945 break;
13946 }
13947 if (++reg1 == 24)
13948 reg1 = 30;
13949 }
13950 }
13951
13952 /* Encode args/statics combination. */
13953 if (nargs & statics)
13954 as_bad (_("arg/static registers overlap"));
13955 else if (nargs == 0xf)
13956 /* All $a0-$a3 are args. */
13957 opcode |= MIPS16_ALL_ARGS << 16;
13958 else if (statics == 0xf)
13959 /* All $a0-$a3 are statics. */
13960 opcode |= MIPS16_ALL_STATICS << 16;
13961 else
13962 {
13963 int narg = 0, nstat = 0;
13964
13965 /* Count arg registers. */
13966 while (nargs & 0x1)
13967 {
13968 nargs >>= 1;
13969 narg++;
13970 }
13971 if (nargs != 0)
13972 as_bad (_("invalid arg register list"));
13973
13974 /* Count static registers. */
13975 while (statics & 0x8)
13976 {
13977 statics = (statics << 1) & 0xf;
13978 nstat++;
13979 }
13980 if (statics != 0)
13981 as_bad (_("invalid static register list"));
13982
13983 /* Encode args/statics. */
13984 opcode |= ((narg << 2) | nstat) << 16;
13985 }
13986
13987 /* Encode $s0/$s1. */
13988 if (sregs & (1 << 0)) /* $s0 */
13989 opcode |= 0x20;
13990 if (sregs & (1 << 1)) /* $s1 */
13991 opcode |= 0x10;
13992 sregs >>= 2;
13993
13994 if (sregs != 0)
13995 {
13996 /* Count regs $s2-$s8. */
13997 int nsreg = 0;
13998 while (sregs & 1)
13999 {
14000 sregs >>= 1;
14001 nsreg++;
14002 }
14003 if (sregs != 0)
14004 as_bad (_("invalid static register list"));
14005 /* Encode $s2-$s8. */
14006 opcode |= nsreg << 24;
14007 }
14008
14009 /* Encode frame size. */
14010 if (!seen_framesz)
14011 as_bad (_("missing frame size"));
14012 else if ((framesz & 7) != 0 || framesz < 0
14013 || framesz > 0xff * 8)
14014 as_bad (_("invalid frame size"));
14015 else if (framesz != 128 || (opcode >> 16) != 0)
14016 {
14017 framesz /= 8;
14018 opcode |= (((framesz & 0xf0) << 16)
14019 | (framesz & 0x0f));
14020 }
14021
14022 /* Finally build the instruction. */
14023 if ((opcode >> 16) != 0 || framesz == 0)
14024 opcode |= MIPS16_EXTEND;
14025 ip->insn_opcode = opcode;
14026 }
14027 continue;
14028
14029 case 'e': /* extend code */
14030 my_getExpression (&imm_expr, s);
14031 check_absolute_expr (ip, &imm_expr);
14032 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
14033 {
14034 as_warn (_("Invalid value for `%s' (%lu)"),
14035 ip->insn_mo->name,
14036 (unsigned long) imm_expr.X_add_number);
14037 imm_expr.X_add_number &= 0x7ff;
14038 }
14039 ip->insn_opcode |= imm_expr.X_add_number;
14040 imm_expr.X_op = O_absent;
14041 s = expr_end;
14042 continue;
14043
14044 default:
14045 abort ();
14046 }
14047 break;
14048 }
14049
14050 /* Args don't match. */
14051 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14052 strcmp (insn->name, insn[1].name) == 0)
14053 {
14054 ++insn;
14055 s = argsstart;
14056 continue;
14057 }
14058
14059 insn_error = _("illegal operands");
14060
14061 return;
14062 }
14063 }
14064
14065 /* This structure holds information we know about a mips16 immediate
14066 argument type. */
14067
14068 struct mips16_immed_operand
14069 {
14070 /* The type code used in the argument string in the opcode table. */
14071 int type;
14072 /* The number of bits in the short form of the opcode. */
14073 int nbits;
14074 /* The number of bits in the extended form of the opcode. */
14075 int extbits;
14076 /* The amount by which the short form is shifted when it is used;
14077 for example, the sw instruction has a shift count of 2. */
14078 int shift;
14079 /* The amount by which the short form is shifted when it is stored
14080 into the instruction code. */
14081 int op_shift;
14082 /* Non-zero if the short form is unsigned. */
14083 int unsp;
14084 /* Non-zero if the extended form is unsigned. */
14085 int extu;
14086 /* Non-zero if the value is PC relative. */
14087 int pcrel;
14088 };
14089
14090 /* The mips16 immediate operand types. */
14091
14092 static const struct mips16_immed_operand mips16_immed_operands[] =
14093 {
14094 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14095 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14096 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14097 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14098 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14099 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14100 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14101 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14102 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14103 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14104 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14105 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14106 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14107 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14108 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14109 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14110 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14111 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14112 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14113 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14114 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14115 };
14116
14117 #define MIPS16_NUM_IMMED \
14118 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14119
14120 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14121 NBITS is the number of significant bits in VAL. */
14122
14123 static unsigned long
14124 mips16_immed_extend (offsetT val, unsigned int nbits)
14125 {
14126 int extval;
14127 if (nbits == 16)
14128 {
14129 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14130 val &= 0x1f;
14131 }
14132 else if (nbits == 15)
14133 {
14134 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14135 val &= 0xf;
14136 }
14137 else
14138 {
14139 extval = ((val & 0x1f) << 6) | (val & 0x20);
14140 val = 0;
14141 }
14142 return (extval << 16) | val;
14143 }
14144
14145 /* Install immediate value VAL into MIPS16 instruction *INSN,
14146 extending it if necessary. The instruction in *INSN may
14147 already be extended.
14148
14149 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14150 if none. In the former case, VAL is a 16-bit number with no
14151 defined signedness.
14152
14153 TYPE is the type of the immediate field. USER_INSN_LENGTH
14154 is the length that the user requested, or 0 if none. */
14155
14156 static void
14157 mips16_immed (char *file, unsigned int line, int type,
14158 bfd_reloc_code_real_type reloc, offsetT val,
14159 unsigned int user_insn_length, unsigned long *insn)
14160 {
14161 const struct mips16_immed_operand *op;
14162 int mintiny, maxtiny;
14163
14164 op = mips16_immed_operands;
14165 while (op->type != type)
14166 {
14167 ++op;
14168 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
14169 }
14170
14171 if (op->unsp)
14172 {
14173 if (type == '<' || type == '>' || type == '[' || type == ']')
14174 {
14175 mintiny = 1;
14176 maxtiny = 1 << op->nbits;
14177 }
14178 else
14179 {
14180 mintiny = 0;
14181 maxtiny = (1 << op->nbits) - 1;
14182 }
14183 if (reloc != BFD_RELOC_UNUSED)
14184 val &= 0xffff;
14185 }
14186 else
14187 {
14188 mintiny = - (1 << (op->nbits - 1));
14189 maxtiny = (1 << (op->nbits - 1)) - 1;
14190 if (reloc != BFD_RELOC_UNUSED)
14191 val = SEXT_16BIT (val);
14192 }
14193
14194 /* Branch offsets have an implicit 0 in the lowest bit. */
14195 if (type == 'p' || type == 'q')
14196 val /= 2;
14197
14198 if ((val & ((1 << op->shift) - 1)) != 0
14199 || val < (mintiny << op->shift)
14200 || val > (maxtiny << op->shift))
14201 {
14202 /* We need an extended instruction. */
14203 if (user_insn_length == 2)
14204 as_bad_where (file, line, _("invalid unextended operand value"));
14205 else
14206 *insn |= MIPS16_EXTEND;
14207 }
14208 else if (user_insn_length == 4)
14209 {
14210 /* The operand doesn't force an unextended instruction to be extended.
14211 Warn if the user wanted an extended instruction anyway. */
14212 *insn |= MIPS16_EXTEND;
14213 as_warn_where (file, line,
14214 _("extended operand requested but not required"));
14215 }
14216
14217 if (mips16_opcode_length (*insn) == 2)
14218 {
14219 int insnval;
14220
14221 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14222 insnval <<= op->op_shift;
14223 *insn |= insnval;
14224 }
14225 else
14226 {
14227 long minext, maxext;
14228
14229 if (reloc == BFD_RELOC_UNUSED)
14230 {
14231 if (op->extu)
14232 {
14233 minext = 0;
14234 maxext = (1 << op->extbits) - 1;
14235 }
14236 else
14237 {
14238 minext = - (1 << (op->extbits - 1));
14239 maxext = (1 << (op->extbits - 1)) - 1;
14240 }
14241 if (val < minext || val > maxext)
14242 as_bad_where (file, line,
14243 _("operand value out of range for instruction"));
14244 }
14245
14246 *insn |= mips16_immed_extend (val, op->extbits);
14247 }
14248 }
14249 \f
14250 struct percent_op_match
14251 {
14252 const char *str;
14253 bfd_reloc_code_real_type reloc;
14254 };
14255
14256 static const struct percent_op_match mips_percent_op[] =
14257 {
14258 {"%lo", BFD_RELOC_LO16},
14259 #ifdef OBJ_ELF
14260 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14261 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14262 {"%call16", BFD_RELOC_MIPS_CALL16},
14263 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14264 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14265 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14266 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14267 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14268 {"%got", BFD_RELOC_MIPS_GOT16},
14269 {"%gp_rel", BFD_RELOC_GPREL16},
14270 {"%half", BFD_RELOC_16},
14271 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14272 {"%higher", BFD_RELOC_MIPS_HIGHER},
14273 {"%neg", BFD_RELOC_MIPS_SUB},
14274 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14275 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14276 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14277 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14278 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14279 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14280 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14281 #endif
14282 {"%hi", BFD_RELOC_HI16_S}
14283 };
14284
14285 static const struct percent_op_match mips16_percent_op[] =
14286 {
14287 {"%lo", BFD_RELOC_MIPS16_LO16},
14288 {"%gprel", BFD_RELOC_MIPS16_GPREL},
14289 {"%got", BFD_RELOC_MIPS16_GOT16},
14290 {"%call16", BFD_RELOC_MIPS16_CALL16},
14291 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14292 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14293 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14294 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14295 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14296 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14297 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14298 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14299 };
14300
14301
14302 /* Return true if *STR points to a relocation operator. When returning true,
14303 move *STR over the operator and store its relocation code in *RELOC.
14304 Leave both *STR and *RELOC alone when returning false. */
14305
14306 static bfd_boolean
14307 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14308 {
14309 const struct percent_op_match *percent_op;
14310 size_t limit, i;
14311
14312 if (mips_opts.mips16)
14313 {
14314 percent_op = mips16_percent_op;
14315 limit = ARRAY_SIZE (mips16_percent_op);
14316 }
14317 else
14318 {
14319 percent_op = mips_percent_op;
14320 limit = ARRAY_SIZE (mips_percent_op);
14321 }
14322
14323 for (i = 0; i < limit; i++)
14324 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14325 {
14326 int len = strlen (percent_op[i].str);
14327
14328 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14329 continue;
14330
14331 *str += strlen (percent_op[i].str);
14332 *reloc = percent_op[i].reloc;
14333
14334 /* Check whether the output BFD supports this relocation.
14335 If not, issue an error and fall back on something safe. */
14336 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14337 {
14338 as_bad (_("relocation %s isn't supported by the current ABI"),
14339 percent_op[i].str);
14340 *reloc = BFD_RELOC_UNUSED;
14341 }
14342 return TRUE;
14343 }
14344 return FALSE;
14345 }
14346
14347
14348 /* Parse string STR as a 16-bit relocatable operand. Store the
14349 expression in *EP and the relocations in the array starting
14350 at RELOC. Return the number of relocation operators used.
14351
14352 On exit, EXPR_END points to the first character after the expression. */
14353
14354 static size_t
14355 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14356 char *str)
14357 {
14358 bfd_reloc_code_real_type reversed_reloc[3];
14359 size_t reloc_index, i;
14360 int crux_depth, str_depth;
14361 char *crux;
14362
14363 /* Search for the start of the main expression, recoding relocations
14364 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14365 of the main expression and with CRUX_DEPTH containing the number
14366 of open brackets at that point. */
14367 reloc_index = -1;
14368 str_depth = 0;
14369 do
14370 {
14371 reloc_index++;
14372 crux = str;
14373 crux_depth = str_depth;
14374
14375 /* Skip over whitespace and brackets, keeping count of the number
14376 of brackets. */
14377 while (*str == ' ' || *str == '\t' || *str == '(')
14378 if (*str++ == '(')
14379 str_depth++;
14380 }
14381 while (*str == '%'
14382 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14383 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14384
14385 my_getExpression (ep, crux);
14386 str = expr_end;
14387
14388 /* Match every open bracket. */
14389 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14390 if (*str++ == ')')
14391 crux_depth--;
14392
14393 if (crux_depth > 0)
14394 as_bad (_("unclosed '('"));
14395
14396 expr_end = str;
14397
14398 if (reloc_index != 0)
14399 {
14400 prev_reloc_op_frag = frag_now;
14401 for (i = 0; i < reloc_index; i++)
14402 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14403 }
14404
14405 return reloc_index;
14406 }
14407
14408 static void
14409 my_getExpression (expressionS *ep, char *str)
14410 {
14411 char *save_in;
14412
14413 save_in = input_line_pointer;
14414 input_line_pointer = str;
14415 expression (ep);
14416 expr_end = input_line_pointer;
14417 input_line_pointer = save_in;
14418 }
14419
14420 char *
14421 md_atof (int type, char *litP, int *sizeP)
14422 {
14423 return ieee_md_atof (type, litP, sizeP, target_big_endian);
14424 }
14425
14426 void
14427 md_number_to_chars (char *buf, valueT val, int n)
14428 {
14429 if (target_big_endian)
14430 number_to_chars_bigendian (buf, val, n);
14431 else
14432 number_to_chars_littleendian (buf, val, n);
14433 }
14434 \f
14435 #ifdef OBJ_ELF
14436 static int support_64bit_objects(void)
14437 {
14438 const char **list, **l;
14439 int yes;
14440
14441 list = bfd_target_list ();
14442 for (l = list; *l != NULL; l++)
14443 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14444 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14445 break;
14446 yes = (*l != NULL);
14447 free (list);
14448 return yes;
14449 }
14450 #endif /* OBJ_ELF */
14451
14452 const char *md_shortopts = "O::g::G:";
14453
14454 enum options
14455 {
14456 OPTION_MARCH = OPTION_MD_BASE,
14457 OPTION_MTUNE,
14458 OPTION_MIPS1,
14459 OPTION_MIPS2,
14460 OPTION_MIPS3,
14461 OPTION_MIPS4,
14462 OPTION_MIPS5,
14463 OPTION_MIPS32,
14464 OPTION_MIPS64,
14465 OPTION_MIPS32R2,
14466 OPTION_MIPS64R2,
14467 OPTION_MIPS16,
14468 OPTION_NO_MIPS16,
14469 OPTION_MIPS3D,
14470 OPTION_NO_MIPS3D,
14471 OPTION_MDMX,
14472 OPTION_NO_MDMX,
14473 OPTION_DSP,
14474 OPTION_NO_DSP,
14475 OPTION_MT,
14476 OPTION_NO_MT,
14477 OPTION_SMARTMIPS,
14478 OPTION_NO_SMARTMIPS,
14479 OPTION_DSPR2,
14480 OPTION_NO_DSPR2,
14481 OPTION_MICROMIPS,
14482 OPTION_NO_MICROMIPS,
14483 OPTION_MCU,
14484 OPTION_NO_MCU,
14485 OPTION_COMPAT_ARCH_BASE,
14486 OPTION_M4650,
14487 OPTION_NO_M4650,
14488 OPTION_M4010,
14489 OPTION_NO_M4010,
14490 OPTION_M4100,
14491 OPTION_NO_M4100,
14492 OPTION_M3900,
14493 OPTION_NO_M3900,
14494 OPTION_M7000_HILO_FIX,
14495 OPTION_MNO_7000_HILO_FIX,
14496 OPTION_FIX_24K,
14497 OPTION_NO_FIX_24K,
14498 OPTION_FIX_LOONGSON2F_JUMP,
14499 OPTION_NO_FIX_LOONGSON2F_JUMP,
14500 OPTION_FIX_LOONGSON2F_NOP,
14501 OPTION_NO_FIX_LOONGSON2F_NOP,
14502 OPTION_FIX_VR4120,
14503 OPTION_NO_FIX_VR4120,
14504 OPTION_FIX_VR4130,
14505 OPTION_NO_FIX_VR4130,
14506 OPTION_FIX_CN63XXP1,
14507 OPTION_NO_FIX_CN63XXP1,
14508 OPTION_TRAP,
14509 OPTION_BREAK,
14510 OPTION_EB,
14511 OPTION_EL,
14512 OPTION_FP32,
14513 OPTION_GP32,
14514 OPTION_CONSTRUCT_FLOATS,
14515 OPTION_NO_CONSTRUCT_FLOATS,
14516 OPTION_FP64,
14517 OPTION_GP64,
14518 OPTION_RELAX_BRANCH,
14519 OPTION_NO_RELAX_BRANCH,
14520 OPTION_MSHARED,
14521 OPTION_MNO_SHARED,
14522 OPTION_MSYM32,
14523 OPTION_MNO_SYM32,
14524 OPTION_SOFT_FLOAT,
14525 OPTION_HARD_FLOAT,
14526 OPTION_SINGLE_FLOAT,
14527 OPTION_DOUBLE_FLOAT,
14528 OPTION_32,
14529 #ifdef OBJ_ELF
14530 OPTION_CALL_SHARED,
14531 OPTION_CALL_NONPIC,
14532 OPTION_NON_SHARED,
14533 OPTION_XGOT,
14534 OPTION_MABI,
14535 OPTION_N32,
14536 OPTION_64,
14537 OPTION_MDEBUG,
14538 OPTION_NO_MDEBUG,
14539 OPTION_PDR,
14540 OPTION_NO_PDR,
14541 OPTION_MVXWORKS_PIC,
14542 #endif /* OBJ_ELF */
14543 OPTION_END_OF_ENUM
14544 };
14545
14546 struct option md_longopts[] =
14547 {
14548 /* Options which specify architecture. */
14549 {"march", required_argument, NULL, OPTION_MARCH},
14550 {"mtune", required_argument, NULL, OPTION_MTUNE},
14551 {"mips0", no_argument, NULL, OPTION_MIPS1},
14552 {"mips1", no_argument, NULL, OPTION_MIPS1},
14553 {"mips2", no_argument, NULL, OPTION_MIPS2},
14554 {"mips3", no_argument, NULL, OPTION_MIPS3},
14555 {"mips4", no_argument, NULL, OPTION_MIPS4},
14556 {"mips5", no_argument, NULL, OPTION_MIPS5},
14557 {"mips32", no_argument, NULL, OPTION_MIPS32},
14558 {"mips64", no_argument, NULL, OPTION_MIPS64},
14559 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
14560 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
14561
14562 /* Options which specify Application Specific Extensions (ASEs). */
14563 {"mips16", no_argument, NULL, OPTION_MIPS16},
14564 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
14565 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
14566 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
14567 {"mdmx", no_argument, NULL, OPTION_MDMX},
14568 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
14569 {"mdsp", no_argument, NULL, OPTION_DSP},
14570 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
14571 {"mmt", no_argument, NULL, OPTION_MT},
14572 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
14573 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
14574 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
14575 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
14576 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
14577 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
14578 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
14579 {"mmcu", no_argument, NULL, OPTION_MCU},
14580 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
14581
14582 /* Old-style architecture options. Don't add more of these. */
14583 {"m4650", no_argument, NULL, OPTION_M4650},
14584 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
14585 {"m4010", no_argument, NULL, OPTION_M4010},
14586 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
14587 {"m4100", no_argument, NULL, OPTION_M4100},
14588 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
14589 {"m3900", no_argument, NULL, OPTION_M3900},
14590 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
14591
14592 /* Options which enable bug fixes. */
14593 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
14594 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14595 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14596 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
14597 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
14598 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
14599 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
14600 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
14601 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
14602 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
14603 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
14604 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
14605 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
14606 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
14607 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
14608
14609 /* Miscellaneous options. */
14610 {"trap", no_argument, NULL, OPTION_TRAP},
14611 {"no-break", no_argument, NULL, OPTION_TRAP},
14612 {"break", no_argument, NULL, OPTION_BREAK},
14613 {"no-trap", no_argument, NULL, OPTION_BREAK},
14614 {"EB", no_argument, NULL, OPTION_EB},
14615 {"EL", no_argument, NULL, OPTION_EL},
14616 {"mfp32", no_argument, NULL, OPTION_FP32},
14617 {"mgp32", no_argument, NULL, OPTION_GP32},
14618 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
14619 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
14620 {"mfp64", no_argument, NULL, OPTION_FP64},
14621 {"mgp64", no_argument, NULL, OPTION_GP64},
14622 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
14623 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
14624 {"mshared", no_argument, NULL, OPTION_MSHARED},
14625 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
14626 {"msym32", no_argument, NULL, OPTION_MSYM32},
14627 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
14628 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
14629 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
14630 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
14631 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
14632
14633 /* Strictly speaking this next option is ELF specific,
14634 but we allow it for other ports as well in order to
14635 make testing easier. */
14636 {"32", no_argument, NULL, OPTION_32},
14637
14638 /* ELF-specific options. */
14639 #ifdef OBJ_ELF
14640 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
14641 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
14642 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
14643 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
14644 {"xgot", no_argument, NULL, OPTION_XGOT},
14645 {"mabi", required_argument, NULL, OPTION_MABI},
14646 {"n32", no_argument, NULL, OPTION_N32},
14647 {"64", no_argument, NULL, OPTION_64},
14648 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
14649 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
14650 {"mpdr", no_argument, NULL, OPTION_PDR},
14651 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
14652 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
14653 #endif /* OBJ_ELF */
14654
14655 {NULL, no_argument, NULL, 0}
14656 };
14657 size_t md_longopts_size = sizeof (md_longopts);
14658
14659 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14660 NEW_VALUE. Warn if another value was already specified. Note:
14661 we have to defer parsing the -march and -mtune arguments in order
14662 to handle 'from-abi' correctly, since the ABI might be specified
14663 in a later argument. */
14664
14665 static void
14666 mips_set_option_string (const char **string_ptr, const char *new_value)
14667 {
14668 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14669 as_warn (_("A different %s was already specified, is now %s"),
14670 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14671 new_value);
14672
14673 *string_ptr = new_value;
14674 }
14675
14676 int
14677 md_parse_option (int c, char *arg)
14678 {
14679 switch (c)
14680 {
14681 case OPTION_CONSTRUCT_FLOATS:
14682 mips_disable_float_construction = 0;
14683 break;
14684
14685 case OPTION_NO_CONSTRUCT_FLOATS:
14686 mips_disable_float_construction = 1;
14687 break;
14688
14689 case OPTION_TRAP:
14690 mips_trap = 1;
14691 break;
14692
14693 case OPTION_BREAK:
14694 mips_trap = 0;
14695 break;
14696
14697 case OPTION_EB:
14698 target_big_endian = 1;
14699 break;
14700
14701 case OPTION_EL:
14702 target_big_endian = 0;
14703 break;
14704
14705 case 'O':
14706 if (arg == NULL)
14707 mips_optimize = 1;
14708 else if (arg[0] == '0')
14709 mips_optimize = 0;
14710 else if (arg[0] == '1')
14711 mips_optimize = 1;
14712 else
14713 mips_optimize = 2;
14714 break;
14715
14716 case 'g':
14717 if (arg == NULL)
14718 mips_debug = 2;
14719 else
14720 mips_debug = atoi (arg);
14721 break;
14722
14723 case OPTION_MIPS1:
14724 file_mips_isa = ISA_MIPS1;
14725 break;
14726
14727 case OPTION_MIPS2:
14728 file_mips_isa = ISA_MIPS2;
14729 break;
14730
14731 case OPTION_MIPS3:
14732 file_mips_isa = ISA_MIPS3;
14733 break;
14734
14735 case OPTION_MIPS4:
14736 file_mips_isa = ISA_MIPS4;
14737 break;
14738
14739 case OPTION_MIPS5:
14740 file_mips_isa = ISA_MIPS5;
14741 break;
14742
14743 case OPTION_MIPS32:
14744 file_mips_isa = ISA_MIPS32;
14745 break;
14746
14747 case OPTION_MIPS32R2:
14748 file_mips_isa = ISA_MIPS32R2;
14749 break;
14750
14751 case OPTION_MIPS64R2:
14752 file_mips_isa = ISA_MIPS64R2;
14753 break;
14754
14755 case OPTION_MIPS64:
14756 file_mips_isa = ISA_MIPS64;
14757 break;
14758
14759 case OPTION_MTUNE:
14760 mips_set_option_string (&mips_tune_string, arg);
14761 break;
14762
14763 case OPTION_MARCH:
14764 mips_set_option_string (&mips_arch_string, arg);
14765 break;
14766
14767 case OPTION_M4650:
14768 mips_set_option_string (&mips_arch_string, "4650");
14769 mips_set_option_string (&mips_tune_string, "4650");
14770 break;
14771
14772 case OPTION_NO_M4650:
14773 break;
14774
14775 case OPTION_M4010:
14776 mips_set_option_string (&mips_arch_string, "4010");
14777 mips_set_option_string (&mips_tune_string, "4010");
14778 break;
14779
14780 case OPTION_NO_M4010:
14781 break;
14782
14783 case OPTION_M4100:
14784 mips_set_option_string (&mips_arch_string, "4100");
14785 mips_set_option_string (&mips_tune_string, "4100");
14786 break;
14787
14788 case OPTION_NO_M4100:
14789 break;
14790
14791 case OPTION_M3900:
14792 mips_set_option_string (&mips_arch_string, "3900");
14793 mips_set_option_string (&mips_tune_string, "3900");
14794 break;
14795
14796 case OPTION_NO_M3900:
14797 break;
14798
14799 case OPTION_MDMX:
14800 mips_opts.ase_mdmx = 1;
14801 break;
14802
14803 case OPTION_NO_MDMX:
14804 mips_opts.ase_mdmx = 0;
14805 break;
14806
14807 case OPTION_DSP:
14808 mips_opts.ase_dsp = 1;
14809 mips_opts.ase_dspr2 = 0;
14810 break;
14811
14812 case OPTION_NO_DSP:
14813 mips_opts.ase_dsp = 0;
14814 mips_opts.ase_dspr2 = 0;
14815 break;
14816
14817 case OPTION_DSPR2:
14818 mips_opts.ase_dspr2 = 1;
14819 mips_opts.ase_dsp = 1;
14820 break;
14821
14822 case OPTION_NO_DSPR2:
14823 mips_opts.ase_dspr2 = 0;
14824 mips_opts.ase_dsp = 0;
14825 break;
14826
14827 case OPTION_MT:
14828 mips_opts.ase_mt = 1;
14829 break;
14830
14831 case OPTION_NO_MT:
14832 mips_opts.ase_mt = 0;
14833 break;
14834
14835 case OPTION_MCU:
14836 mips_opts.ase_mcu = 1;
14837 break;
14838
14839 case OPTION_NO_MCU:
14840 mips_opts.ase_mcu = 0;
14841 break;
14842
14843 case OPTION_MICROMIPS:
14844 if (mips_opts.mips16 == 1)
14845 {
14846 as_bad (_("-mmicromips cannot be used with -mips16"));
14847 return 0;
14848 }
14849 mips_opts.micromips = 1;
14850 mips_no_prev_insn ();
14851 break;
14852
14853 case OPTION_NO_MICROMIPS:
14854 mips_opts.micromips = 0;
14855 mips_no_prev_insn ();
14856 break;
14857
14858 case OPTION_MIPS16:
14859 if (mips_opts.micromips == 1)
14860 {
14861 as_bad (_("-mips16 cannot be used with -micromips"));
14862 return 0;
14863 }
14864 mips_opts.mips16 = 1;
14865 mips_no_prev_insn ();
14866 break;
14867
14868 case OPTION_NO_MIPS16:
14869 mips_opts.mips16 = 0;
14870 mips_no_prev_insn ();
14871 break;
14872
14873 case OPTION_MIPS3D:
14874 mips_opts.ase_mips3d = 1;
14875 break;
14876
14877 case OPTION_NO_MIPS3D:
14878 mips_opts.ase_mips3d = 0;
14879 break;
14880
14881 case OPTION_SMARTMIPS:
14882 mips_opts.ase_smartmips = 1;
14883 break;
14884
14885 case OPTION_NO_SMARTMIPS:
14886 mips_opts.ase_smartmips = 0;
14887 break;
14888
14889 case OPTION_FIX_24K:
14890 mips_fix_24k = 1;
14891 break;
14892
14893 case OPTION_NO_FIX_24K:
14894 mips_fix_24k = 0;
14895 break;
14896
14897 case OPTION_FIX_LOONGSON2F_JUMP:
14898 mips_fix_loongson2f_jump = TRUE;
14899 break;
14900
14901 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14902 mips_fix_loongson2f_jump = FALSE;
14903 break;
14904
14905 case OPTION_FIX_LOONGSON2F_NOP:
14906 mips_fix_loongson2f_nop = TRUE;
14907 break;
14908
14909 case OPTION_NO_FIX_LOONGSON2F_NOP:
14910 mips_fix_loongson2f_nop = FALSE;
14911 break;
14912
14913 case OPTION_FIX_VR4120:
14914 mips_fix_vr4120 = 1;
14915 break;
14916
14917 case OPTION_NO_FIX_VR4120:
14918 mips_fix_vr4120 = 0;
14919 break;
14920
14921 case OPTION_FIX_VR4130:
14922 mips_fix_vr4130 = 1;
14923 break;
14924
14925 case OPTION_NO_FIX_VR4130:
14926 mips_fix_vr4130 = 0;
14927 break;
14928
14929 case OPTION_FIX_CN63XXP1:
14930 mips_fix_cn63xxp1 = TRUE;
14931 break;
14932
14933 case OPTION_NO_FIX_CN63XXP1:
14934 mips_fix_cn63xxp1 = FALSE;
14935 break;
14936
14937 case OPTION_RELAX_BRANCH:
14938 mips_relax_branch = 1;
14939 break;
14940
14941 case OPTION_NO_RELAX_BRANCH:
14942 mips_relax_branch = 0;
14943 break;
14944
14945 case OPTION_MSHARED:
14946 mips_in_shared = TRUE;
14947 break;
14948
14949 case OPTION_MNO_SHARED:
14950 mips_in_shared = FALSE;
14951 break;
14952
14953 case OPTION_MSYM32:
14954 mips_opts.sym32 = TRUE;
14955 break;
14956
14957 case OPTION_MNO_SYM32:
14958 mips_opts.sym32 = FALSE;
14959 break;
14960
14961 #ifdef OBJ_ELF
14962 /* When generating ELF code, we permit -KPIC and -call_shared to
14963 select SVR4_PIC, and -non_shared to select no PIC. This is
14964 intended to be compatible with Irix 5. */
14965 case OPTION_CALL_SHARED:
14966 if (!IS_ELF)
14967 {
14968 as_bad (_("-call_shared is supported only for ELF format"));
14969 return 0;
14970 }
14971 mips_pic = SVR4_PIC;
14972 mips_abicalls = TRUE;
14973 break;
14974
14975 case OPTION_CALL_NONPIC:
14976 if (!IS_ELF)
14977 {
14978 as_bad (_("-call_nonpic is supported only for ELF format"));
14979 return 0;
14980 }
14981 mips_pic = NO_PIC;
14982 mips_abicalls = TRUE;
14983 break;
14984
14985 case OPTION_NON_SHARED:
14986 if (!IS_ELF)
14987 {
14988 as_bad (_("-non_shared is supported only for ELF format"));
14989 return 0;
14990 }
14991 mips_pic = NO_PIC;
14992 mips_abicalls = FALSE;
14993 break;
14994
14995 /* The -xgot option tells the assembler to use 32 bit offsets
14996 when accessing the got in SVR4_PIC mode. It is for Irix
14997 compatibility. */
14998 case OPTION_XGOT:
14999 mips_big_got = 1;
15000 break;
15001 #endif /* OBJ_ELF */
15002
15003 case 'G':
15004 g_switch_value = atoi (arg);
15005 g_switch_seen = 1;
15006 break;
15007
15008 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15009 and -mabi=64. */
15010 case OPTION_32:
15011 if (IS_ELF)
15012 mips_abi = O32_ABI;
15013 /* We silently ignore -32 for non-ELF targets. This greatly
15014 simplifies the construction of the MIPS GAS test cases. */
15015 break;
15016
15017 #ifdef OBJ_ELF
15018 case OPTION_N32:
15019 if (!IS_ELF)
15020 {
15021 as_bad (_("-n32 is supported for ELF format only"));
15022 return 0;
15023 }
15024 mips_abi = N32_ABI;
15025 break;
15026
15027 case OPTION_64:
15028 if (!IS_ELF)
15029 {
15030 as_bad (_("-64 is supported for ELF format only"));
15031 return 0;
15032 }
15033 mips_abi = N64_ABI;
15034 if (!support_64bit_objects())
15035 as_fatal (_("No compiled in support for 64 bit object file format"));
15036 break;
15037 #endif /* OBJ_ELF */
15038
15039 case OPTION_GP32:
15040 file_mips_gp32 = 1;
15041 break;
15042
15043 case OPTION_GP64:
15044 file_mips_gp32 = 0;
15045 break;
15046
15047 case OPTION_FP32:
15048 file_mips_fp32 = 1;
15049 break;
15050
15051 case OPTION_FP64:
15052 file_mips_fp32 = 0;
15053 break;
15054
15055 case OPTION_SINGLE_FLOAT:
15056 file_mips_single_float = 1;
15057 break;
15058
15059 case OPTION_DOUBLE_FLOAT:
15060 file_mips_single_float = 0;
15061 break;
15062
15063 case OPTION_SOFT_FLOAT:
15064 file_mips_soft_float = 1;
15065 break;
15066
15067 case OPTION_HARD_FLOAT:
15068 file_mips_soft_float = 0;
15069 break;
15070
15071 #ifdef OBJ_ELF
15072 case OPTION_MABI:
15073 if (!IS_ELF)
15074 {
15075 as_bad (_("-mabi is supported for ELF format only"));
15076 return 0;
15077 }
15078 if (strcmp (arg, "32") == 0)
15079 mips_abi = O32_ABI;
15080 else if (strcmp (arg, "o64") == 0)
15081 mips_abi = O64_ABI;
15082 else if (strcmp (arg, "n32") == 0)
15083 mips_abi = N32_ABI;
15084 else if (strcmp (arg, "64") == 0)
15085 {
15086 mips_abi = N64_ABI;
15087 if (! support_64bit_objects())
15088 as_fatal (_("No compiled in support for 64 bit object file "
15089 "format"));
15090 }
15091 else if (strcmp (arg, "eabi") == 0)
15092 mips_abi = EABI_ABI;
15093 else
15094 {
15095 as_fatal (_("invalid abi -mabi=%s"), arg);
15096 return 0;
15097 }
15098 break;
15099 #endif /* OBJ_ELF */
15100
15101 case OPTION_M7000_HILO_FIX:
15102 mips_7000_hilo_fix = TRUE;
15103 break;
15104
15105 case OPTION_MNO_7000_HILO_FIX:
15106 mips_7000_hilo_fix = FALSE;
15107 break;
15108
15109 #ifdef OBJ_ELF
15110 case OPTION_MDEBUG:
15111 mips_flag_mdebug = TRUE;
15112 break;
15113
15114 case OPTION_NO_MDEBUG:
15115 mips_flag_mdebug = FALSE;
15116 break;
15117
15118 case OPTION_PDR:
15119 mips_flag_pdr = TRUE;
15120 break;
15121
15122 case OPTION_NO_PDR:
15123 mips_flag_pdr = FALSE;
15124 break;
15125
15126 case OPTION_MVXWORKS_PIC:
15127 mips_pic = VXWORKS_PIC;
15128 break;
15129 #endif /* OBJ_ELF */
15130
15131 default:
15132 return 0;
15133 }
15134
15135 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15136
15137 return 1;
15138 }
15139 \f
15140 /* Set up globals to generate code for the ISA or processor
15141 described by INFO. */
15142
15143 static void
15144 mips_set_architecture (const struct mips_cpu_info *info)
15145 {
15146 if (info != 0)
15147 {
15148 file_mips_arch = info->cpu;
15149 mips_opts.arch = info->cpu;
15150 mips_opts.isa = info->isa;
15151 }
15152 }
15153
15154
15155 /* Likewise for tuning. */
15156
15157 static void
15158 mips_set_tune (const struct mips_cpu_info *info)
15159 {
15160 if (info != 0)
15161 mips_tune = info->cpu;
15162 }
15163
15164
15165 void
15166 mips_after_parse_args (void)
15167 {
15168 const struct mips_cpu_info *arch_info = 0;
15169 const struct mips_cpu_info *tune_info = 0;
15170
15171 /* GP relative stuff not working for PE */
15172 if (strncmp (TARGET_OS, "pe", 2) == 0)
15173 {
15174 if (g_switch_seen && g_switch_value != 0)
15175 as_bad (_("-G not supported in this configuration."));
15176 g_switch_value = 0;
15177 }
15178
15179 if (mips_abi == NO_ABI)
15180 mips_abi = MIPS_DEFAULT_ABI;
15181
15182 /* The following code determines the architecture and register size.
15183 Similar code was added to GCC 3.3 (see override_options() in
15184 config/mips/mips.c). The GAS and GCC code should be kept in sync
15185 as much as possible. */
15186
15187 if (mips_arch_string != 0)
15188 arch_info = mips_parse_cpu ("-march", mips_arch_string);
15189
15190 if (file_mips_isa != ISA_UNKNOWN)
15191 {
15192 /* Handle -mipsN. At this point, file_mips_isa contains the
15193 ISA level specified by -mipsN, while arch_info->isa contains
15194 the -march selection (if any). */
15195 if (arch_info != 0)
15196 {
15197 /* -march takes precedence over -mipsN, since it is more descriptive.
15198 There's no harm in specifying both as long as the ISA levels
15199 are the same. */
15200 if (file_mips_isa != arch_info->isa)
15201 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15202 mips_cpu_info_from_isa (file_mips_isa)->name,
15203 mips_cpu_info_from_isa (arch_info->isa)->name);
15204 }
15205 else
15206 arch_info = mips_cpu_info_from_isa (file_mips_isa);
15207 }
15208
15209 if (arch_info == 0)
15210 {
15211 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15212 gas_assert (arch_info);
15213 }
15214
15215 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15216 as_bad (_("-march=%s is not compatible with the selected ABI"),
15217 arch_info->name);
15218
15219 mips_set_architecture (arch_info);
15220
15221 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
15222 if (mips_tune_string != 0)
15223 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15224
15225 if (tune_info == 0)
15226 mips_set_tune (arch_info);
15227 else
15228 mips_set_tune (tune_info);
15229
15230 if (file_mips_gp32 >= 0)
15231 {
15232 /* The user specified the size of the integer registers. Make sure
15233 it agrees with the ABI and ISA. */
15234 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15235 as_bad (_("-mgp64 used with a 32-bit processor"));
15236 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15237 as_bad (_("-mgp32 used with a 64-bit ABI"));
15238 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15239 as_bad (_("-mgp64 used with a 32-bit ABI"));
15240 }
15241 else
15242 {
15243 /* Infer the integer register size from the ABI and processor.
15244 Restrict ourselves to 32-bit registers if that's all the
15245 processor has, or if the ABI cannot handle 64-bit registers. */
15246 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15247 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
15248 }
15249
15250 switch (file_mips_fp32)
15251 {
15252 default:
15253 case -1:
15254 /* No user specified float register size.
15255 ??? GAS treats single-float processors as though they had 64-bit
15256 float registers (although it complains when double-precision
15257 instructions are used). As things stand, saying they have 32-bit
15258 registers would lead to spurious "register must be even" messages.
15259 So here we assume float registers are never smaller than the
15260 integer ones. */
15261 if (file_mips_gp32 == 0)
15262 /* 64-bit integer registers implies 64-bit float registers. */
15263 file_mips_fp32 = 0;
15264 else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
15265 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15266 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
15267 file_mips_fp32 = 0;
15268 else
15269 /* 32-bit float registers. */
15270 file_mips_fp32 = 1;
15271 break;
15272
15273 /* The user specified the size of the float registers. Check if it
15274 agrees with the ABI and ISA. */
15275 case 0:
15276 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15277 as_bad (_("-mfp64 used with a 32-bit fpu"));
15278 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15279 && !ISA_HAS_MXHC1 (mips_opts.isa))
15280 as_warn (_("-mfp64 used with a 32-bit ABI"));
15281 break;
15282 case 1:
15283 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15284 as_warn (_("-mfp32 used with a 64-bit ABI"));
15285 break;
15286 }
15287
15288 /* End of GCC-shared inference code. */
15289
15290 /* This flag is set when we have a 64-bit capable CPU but use only
15291 32-bit wide registers. Note that EABI does not use it. */
15292 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15293 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15294 || mips_abi == O32_ABI))
15295 mips_32bitmode = 1;
15296
15297 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15298 as_bad (_("trap exception not supported at ISA 1"));
15299
15300 /* If the selected architecture includes support for ASEs, enable
15301 generation of code for them. */
15302 if (mips_opts.mips16 == -1)
15303 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
15304 if (mips_opts.micromips == -1)
15305 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
15306 if (mips_opts.ase_mips3d == -1)
15307 mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
15308 && file_mips_fp32 == 0) ? 1 : 0;
15309 if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
15310 as_bad (_("-mfp32 used with -mips3d"));
15311
15312 if (mips_opts.ase_mdmx == -1)
15313 mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
15314 && file_mips_fp32 == 0) ? 1 : 0;
15315 if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
15316 as_bad (_("-mfp32 used with -mdmx"));
15317
15318 if (mips_opts.ase_smartmips == -1)
15319 mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
15320 if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
15321 as_warn (_("%s ISA does not support SmartMIPS"),
15322 mips_cpu_info_from_isa (mips_opts.isa)->name);
15323
15324 if (mips_opts.ase_dsp == -1)
15325 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15326 if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
15327 as_warn (_("%s ISA does not support DSP ASE"),
15328 mips_cpu_info_from_isa (mips_opts.isa)->name);
15329
15330 if (mips_opts.ase_dspr2 == -1)
15331 {
15332 mips_opts.ase_dspr2 = (arch_info->flags & MIPS_CPU_ASE_DSPR2) ? 1 : 0;
15333 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15334 }
15335 if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
15336 as_warn (_("%s ISA does not support DSP R2 ASE"),
15337 mips_cpu_info_from_isa (mips_opts.isa)->name);
15338
15339 if (mips_opts.ase_mt == -1)
15340 mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
15341 if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
15342 as_warn (_("%s ISA does not support MT ASE"),
15343 mips_cpu_info_from_isa (mips_opts.isa)->name);
15344
15345 if (mips_opts.ase_mcu == -1)
15346 mips_opts.ase_mcu = (arch_info->flags & MIPS_CPU_ASE_MCU) ? 1 : 0;
15347 if (mips_opts.ase_mcu && !ISA_SUPPORTS_MCU_ASE)
15348 as_warn (_("%s ISA does not support MCU ASE"),
15349 mips_cpu_info_from_isa (mips_opts.isa)->name);
15350
15351 file_mips_isa = mips_opts.isa;
15352 file_ase_mips3d = mips_opts.ase_mips3d;
15353 file_ase_mdmx = mips_opts.ase_mdmx;
15354 file_ase_smartmips = mips_opts.ase_smartmips;
15355 file_ase_dsp = mips_opts.ase_dsp;
15356 file_ase_dspr2 = mips_opts.ase_dspr2;
15357 file_ase_mt = mips_opts.ase_mt;
15358 mips_opts.gp32 = file_mips_gp32;
15359 mips_opts.fp32 = file_mips_fp32;
15360 mips_opts.soft_float = file_mips_soft_float;
15361 mips_opts.single_float = file_mips_single_float;
15362
15363 if (mips_flag_mdebug < 0)
15364 {
15365 #ifdef OBJ_MAYBE_ECOFF
15366 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
15367 mips_flag_mdebug = 1;
15368 else
15369 #endif /* OBJ_MAYBE_ECOFF */
15370 mips_flag_mdebug = 0;
15371 }
15372 }
15373 \f
15374 void
15375 mips_init_after_args (void)
15376 {
15377 /* initialize opcodes */
15378 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15379 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15380 }
15381
15382 long
15383 md_pcrel_from (fixS *fixP)
15384 {
15385 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15386 switch (fixP->fx_r_type)
15387 {
15388 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15389 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15390 /* Return the address of the delay slot. */
15391 return addr + 2;
15392
15393 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15394 case BFD_RELOC_MICROMIPS_JMP:
15395 case BFD_RELOC_16_PCREL_S2:
15396 case BFD_RELOC_MIPS_JMP:
15397 /* Return the address of the delay slot. */
15398 return addr + 4;
15399
15400 default:
15401 /* We have no relocation type for PC relative MIPS16 instructions. */
15402 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15403 as_bad_where (fixP->fx_file, fixP->fx_line,
15404 _("PC relative MIPS16 instruction references a different section"));
15405 return addr;
15406 }
15407 }
15408
15409 /* This is called before the symbol table is processed. In order to
15410 work with gcc when using mips-tfile, we must keep all local labels.
15411 However, in other cases, we want to discard them. If we were
15412 called with -g, but we didn't see any debugging information, it may
15413 mean that gcc is smuggling debugging information through to
15414 mips-tfile, in which case we must generate all local labels. */
15415
15416 void
15417 mips_frob_file_before_adjust (void)
15418 {
15419 #ifndef NO_ECOFF_DEBUGGING
15420 if (ECOFF_DEBUGGING
15421 && mips_debug != 0
15422 && ! ecoff_debugging_seen)
15423 flag_keep_locals = 1;
15424 #endif
15425 }
15426
15427 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15428 the corresponding LO16 reloc. This is called before md_apply_fix and
15429 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15430 relocation operators.
15431
15432 For our purposes, a %lo() expression matches a %got() or %hi()
15433 expression if:
15434
15435 (a) it refers to the same symbol; and
15436 (b) the offset applied in the %lo() expression is no lower than
15437 the offset applied in the %got() or %hi().
15438
15439 (b) allows us to cope with code like:
15440
15441 lui $4,%hi(foo)
15442 lh $4,%lo(foo+2)($4)
15443
15444 ...which is legal on RELA targets, and has a well-defined behaviour
15445 if the user knows that adding 2 to "foo" will not induce a carry to
15446 the high 16 bits.
15447
15448 When several %lo()s match a particular %got() or %hi(), we use the
15449 following rules to distinguish them:
15450
15451 (1) %lo()s with smaller offsets are a better match than %lo()s with
15452 higher offsets.
15453
15454 (2) %lo()s with no matching %got() or %hi() are better than those
15455 that already have a matching %got() or %hi().
15456
15457 (3) later %lo()s are better than earlier %lo()s.
15458
15459 These rules are applied in order.
15460
15461 (1) means, among other things, that %lo()s with identical offsets are
15462 chosen if they exist.
15463
15464 (2) means that we won't associate several high-part relocations with
15465 the same low-part relocation unless there's no alternative. Having
15466 several high parts for the same low part is a GNU extension; this rule
15467 allows careful users to avoid it.
15468
15469 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15470 with the last high-part relocation being at the front of the list.
15471 It therefore makes sense to choose the last matching low-part
15472 relocation, all other things being equal. It's also easier
15473 to code that way. */
15474
15475 void
15476 mips_frob_file (void)
15477 {
15478 struct mips_hi_fixup *l;
15479 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15480
15481 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15482 {
15483 segment_info_type *seginfo;
15484 bfd_boolean matched_lo_p;
15485 fixS **hi_pos, **lo_pos, **pos;
15486
15487 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15488
15489 /* If a GOT16 relocation turns out to be against a global symbol,
15490 there isn't supposed to be a matching LO. Ignore %gots against
15491 constants; we'll report an error for those later. */
15492 if (got16_reloc_p (l->fixp->fx_r_type)
15493 && !(l->fixp->fx_addsy
15494 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
15495 continue;
15496
15497 /* Check quickly whether the next fixup happens to be a matching %lo. */
15498 if (fixup_has_matching_lo_p (l->fixp))
15499 continue;
15500
15501 seginfo = seg_info (l->seg);
15502
15503 /* Set HI_POS to the position of this relocation in the chain.
15504 Set LO_POS to the position of the chosen low-part relocation.
15505 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15506 relocation that matches an immediately-preceding high-part
15507 relocation. */
15508 hi_pos = NULL;
15509 lo_pos = NULL;
15510 matched_lo_p = FALSE;
15511 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15512
15513 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15514 {
15515 if (*pos == l->fixp)
15516 hi_pos = pos;
15517
15518 if ((*pos)->fx_r_type == looking_for_rtype
15519 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15520 && (*pos)->fx_offset >= l->fixp->fx_offset
15521 && (lo_pos == NULL
15522 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15523 || (!matched_lo_p
15524 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15525 lo_pos = pos;
15526
15527 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15528 && fixup_has_matching_lo_p (*pos));
15529 }
15530
15531 /* If we found a match, remove the high-part relocation from its
15532 current position and insert it before the low-part relocation.
15533 Make the offsets match so that fixup_has_matching_lo_p()
15534 will return true.
15535
15536 We don't warn about unmatched high-part relocations since some
15537 versions of gcc have been known to emit dead "lui ...%hi(...)"
15538 instructions. */
15539 if (lo_pos != NULL)
15540 {
15541 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15542 if (l->fixp->fx_next != *lo_pos)
15543 {
15544 *hi_pos = l->fixp->fx_next;
15545 l->fixp->fx_next = *lo_pos;
15546 *lo_pos = l->fixp;
15547 }
15548 }
15549 }
15550 }
15551
15552 int
15553 mips_force_relocation (fixS *fixp)
15554 {
15555 if (generic_force_reloc (fixp))
15556 return 1;
15557
15558 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15559 so that the linker relaxation can update targets. */
15560 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15561 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15562 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15563 return 1;
15564
15565 return 0;
15566 }
15567
15568 /* Read the instruction associated with RELOC from BUF. */
15569
15570 static unsigned int
15571 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15572 {
15573 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15574 return read_compressed_insn (buf, 4);
15575 else
15576 return read_insn (buf);
15577 }
15578
15579 /* Write instruction INSN to BUF, given that it has been relocated
15580 by RELOC. */
15581
15582 static void
15583 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15584 unsigned long insn)
15585 {
15586 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15587 write_compressed_insn (buf, insn, 4);
15588 else
15589 write_insn (buf, insn);
15590 }
15591
15592 /* Apply a fixup to the object file. */
15593
15594 void
15595 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15596 {
15597 char *buf;
15598 unsigned long insn;
15599 reloc_howto_type *howto;
15600
15601 /* We ignore generic BFD relocations we don't know about. */
15602 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15603 if (! howto)
15604 return;
15605
15606 gas_assert (fixP->fx_size == 2
15607 || fixP->fx_size == 4
15608 || fixP->fx_r_type == BFD_RELOC_16
15609 || fixP->fx_r_type == BFD_RELOC_64
15610 || fixP->fx_r_type == BFD_RELOC_CTOR
15611 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15612 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15613 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15614 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15615 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
15616
15617 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15618
15619 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15620 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15621 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15622 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
15623
15624 /* Don't treat parts of a composite relocation as done. There are two
15625 reasons for this:
15626
15627 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15628 should nevertheless be emitted if the first part is.
15629
15630 (2) In normal usage, composite relocations are never assembly-time
15631 constants. The easiest way of dealing with the pathological
15632 exceptions is to generate a relocation against STN_UNDEF and
15633 leave everything up to the linker. */
15634 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15635 fixP->fx_done = 1;
15636
15637 switch (fixP->fx_r_type)
15638 {
15639 case BFD_RELOC_MIPS_TLS_GD:
15640 case BFD_RELOC_MIPS_TLS_LDM:
15641 case BFD_RELOC_MIPS_TLS_DTPREL32:
15642 case BFD_RELOC_MIPS_TLS_DTPREL64:
15643 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15644 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15645 case BFD_RELOC_MIPS_TLS_GOTTPREL:
15646 case BFD_RELOC_MIPS_TLS_TPREL32:
15647 case BFD_RELOC_MIPS_TLS_TPREL64:
15648 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15649 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15650 case BFD_RELOC_MICROMIPS_TLS_GD:
15651 case BFD_RELOC_MICROMIPS_TLS_LDM:
15652 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15653 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15654 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15655 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15656 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15657 case BFD_RELOC_MIPS16_TLS_GD:
15658 case BFD_RELOC_MIPS16_TLS_LDM:
15659 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15660 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15661 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15662 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15663 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15664 if (!fixP->fx_addsy)
15665 {
15666 as_bad_where (fixP->fx_file, fixP->fx_line,
15667 _("TLS relocation against a constant"));
15668 break;
15669 }
15670 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15671 /* fall through */
15672
15673 case BFD_RELOC_MIPS_JMP:
15674 case BFD_RELOC_MIPS_SHIFT5:
15675 case BFD_RELOC_MIPS_SHIFT6:
15676 case BFD_RELOC_MIPS_GOT_DISP:
15677 case BFD_RELOC_MIPS_GOT_PAGE:
15678 case BFD_RELOC_MIPS_GOT_OFST:
15679 case BFD_RELOC_MIPS_SUB:
15680 case BFD_RELOC_MIPS_INSERT_A:
15681 case BFD_RELOC_MIPS_INSERT_B:
15682 case BFD_RELOC_MIPS_DELETE:
15683 case BFD_RELOC_MIPS_HIGHEST:
15684 case BFD_RELOC_MIPS_HIGHER:
15685 case BFD_RELOC_MIPS_SCN_DISP:
15686 case BFD_RELOC_MIPS_REL16:
15687 case BFD_RELOC_MIPS_RELGOT:
15688 case BFD_RELOC_MIPS_JALR:
15689 case BFD_RELOC_HI16:
15690 case BFD_RELOC_HI16_S:
15691 case BFD_RELOC_LO16:
15692 case BFD_RELOC_GPREL16:
15693 case BFD_RELOC_MIPS_LITERAL:
15694 case BFD_RELOC_MIPS_CALL16:
15695 case BFD_RELOC_MIPS_GOT16:
15696 case BFD_RELOC_GPREL32:
15697 case BFD_RELOC_MIPS_GOT_HI16:
15698 case BFD_RELOC_MIPS_GOT_LO16:
15699 case BFD_RELOC_MIPS_CALL_HI16:
15700 case BFD_RELOC_MIPS_CALL_LO16:
15701 case BFD_RELOC_MIPS16_GPREL:
15702 case BFD_RELOC_MIPS16_GOT16:
15703 case BFD_RELOC_MIPS16_CALL16:
15704 case BFD_RELOC_MIPS16_HI16:
15705 case BFD_RELOC_MIPS16_HI16_S:
15706 case BFD_RELOC_MIPS16_LO16:
15707 case BFD_RELOC_MIPS16_JMP:
15708 case BFD_RELOC_MICROMIPS_JMP:
15709 case BFD_RELOC_MICROMIPS_GOT_DISP:
15710 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15711 case BFD_RELOC_MICROMIPS_GOT_OFST:
15712 case BFD_RELOC_MICROMIPS_SUB:
15713 case BFD_RELOC_MICROMIPS_HIGHEST:
15714 case BFD_RELOC_MICROMIPS_HIGHER:
15715 case BFD_RELOC_MICROMIPS_SCN_DISP:
15716 case BFD_RELOC_MICROMIPS_JALR:
15717 case BFD_RELOC_MICROMIPS_HI16:
15718 case BFD_RELOC_MICROMIPS_HI16_S:
15719 case BFD_RELOC_MICROMIPS_LO16:
15720 case BFD_RELOC_MICROMIPS_GPREL16:
15721 case BFD_RELOC_MICROMIPS_LITERAL:
15722 case BFD_RELOC_MICROMIPS_CALL16:
15723 case BFD_RELOC_MICROMIPS_GOT16:
15724 case BFD_RELOC_MICROMIPS_GOT_HI16:
15725 case BFD_RELOC_MICROMIPS_GOT_LO16:
15726 case BFD_RELOC_MICROMIPS_CALL_HI16:
15727 case BFD_RELOC_MICROMIPS_CALL_LO16:
15728 if (fixP->fx_done)
15729 {
15730 offsetT value;
15731
15732 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15733 {
15734 insn = read_reloc_insn (buf, fixP->fx_r_type);
15735 if (mips16_reloc_p (fixP->fx_r_type))
15736 insn |= mips16_immed_extend (value, 16);
15737 else
15738 insn |= (value & 0xffff);
15739 write_reloc_insn (buf, fixP->fx_r_type, insn);
15740 }
15741 else
15742 as_bad_where (fixP->fx_file, fixP->fx_line,
15743 _("Unsupported constant in relocation"));
15744 }
15745 break;
15746
15747 case BFD_RELOC_64:
15748 /* This is handled like BFD_RELOC_32, but we output a sign
15749 extended value if we are only 32 bits. */
15750 if (fixP->fx_done)
15751 {
15752 if (8 <= sizeof (valueT))
15753 md_number_to_chars (buf, *valP, 8);
15754 else
15755 {
15756 valueT hiv;
15757
15758 if ((*valP & 0x80000000) != 0)
15759 hiv = 0xffffffff;
15760 else
15761 hiv = 0;
15762 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15763 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
15764 }
15765 }
15766 break;
15767
15768 case BFD_RELOC_RVA:
15769 case BFD_RELOC_32:
15770 case BFD_RELOC_16:
15771 /* If we are deleting this reloc entry, we must fill in the
15772 value now. This can happen if we have a .word which is not
15773 resolved when it appears but is later defined. */
15774 if (fixP->fx_done)
15775 md_number_to_chars (buf, *valP, fixP->fx_size);
15776 break;
15777
15778 case BFD_RELOC_16_PCREL_S2:
15779 if ((*valP & 0x3) != 0)
15780 as_bad_where (fixP->fx_file, fixP->fx_line,
15781 _("Branch to misaligned address (%lx)"), (long) *valP);
15782
15783 /* We need to save the bits in the instruction since fixup_segment()
15784 might be deleting the relocation entry (i.e., a branch within
15785 the current segment). */
15786 if (! fixP->fx_done)
15787 break;
15788
15789 /* Update old instruction data. */
15790 insn = read_insn (buf);
15791
15792 if (*valP + 0x20000 <= 0x3ffff)
15793 {
15794 insn |= (*valP >> 2) & 0xffff;
15795 write_insn (buf, insn);
15796 }
15797 else if (mips_pic == NO_PIC
15798 && fixP->fx_done
15799 && fixP->fx_frag->fr_address >= text_section->vma
15800 && (fixP->fx_frag->fr_address
15801 < text_section->vma + bfd_get_section_size (text_section))
15802 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15803 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15804 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15805 {
15806 /* The branch offset is too large. If this is an
15807 unconditional branch, and we are not generating PIC code,
15808 we can convert it to an absolute jump instruction. */
15809 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15810 insn = 0x0c000000; /* jal */
15811 else
15812 insn = 0x08000000; /* j */
15813 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15814 fixP->fx_done = 0;
15815 fixP->fx_addsy = section_symbol (text_section);
15816 *valP += md_pcrel_from (fixP);
15817 write_insn (buf, insn);
15818 }
15819 else
15820 {
15821 /* If we got here, we have branch-relaxation disabled,
15822 and there's nothing we can do to fix this instruction
15823 without turning it into a longer sequence. */
15824 as_bad_where (fixP->fx_file, fixP->fx_line,
15825 _("Branch out of range"));
15826 }
15827 break;
15828
15829 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15830 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15831 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15832 /* We adjust the offset back to even. */
15833 if ((*valP & 0x1) != 0)
15834 --(*valP);
15835
15836 if (! fixP->fx_done)
15837 break;
15838
15839 /* Should never visit here, because we keep the relocation. */
15840 abort ();
15841 break;
15842
15843 case BFD_RELOC_VTABLE_INHERIT:
15844 fixP->fx_done = 0;
15845 if (fixP->fx_addsy
15846 && !S_IS_DEFINED (fixP->fx_addsy)
15847 && !S_IS_WEAK (fixP->fx_addsy))
15848 S_SET_WEAK (fixP->fx_addsy);
15849 break;
15850
15851 case BFD_RELOC_VTABLE_ENTRY:
15852 fixP->fx_done = 0;
15853 break;
15854
15855 default:
15856 abort ();
15857 }
15858
15859 /* Remember value for tc_gen_reloc. */
15860 fixP->fx_addnumber = *valP;
15861 }
15862
15863 static symbolS *
15864 get_symbol (void)
15865 {
15866 int c;
15867 char *name;
15868 symbolS *p;
15869
15870 name = input_line_pointer;
15871 c = get_symbol_end ();
15872 p = (symbolS *) symbol_find_or_make (name);
15873 *input_line_pointer = c;
15874 return p;
15875 }
15876
15877 /* Align the current frag to a given power of two. If a particular
15878 fill byte should be used, FILL points to an integer that contains
15879 that byte, otherwise FILL is null.
15880
15881 This function used to have the comment:
15882
15883 The MIPS assembler also automatically adjusts any preceding label.
15884
15885 The implementation therefore applied the adjustment to a maximum of
15886 one label. However, other label adjustments are applied to batches
15887 of labels, and adjusting just one caused problems when new labels
15888 were added for the sake of debugging or unwind information.
15889 We therefore adjust all preceding labels (given as LABELS) instead. */
15890
15891 static void
15892 mips_align (int to, int *fill, struct insn_label_list *labels)
15893 {
15894 mips_emit_delays ();
15895 mips_record_compressed_mode ();
15896 if (fill == NULL && subseg_text_p (now_seg))
15897 frag_align_code (to, 0);
15898 else
15899 frag_align (to, fill ? *fill : 0, 0);
15900 record_alignment (now_seg, to);
15901 mips_move_labels (labels, FALSE);
15902 }
15903
15904 /* Align to a given power of two. .align 0 turns off the automatic
15905 alignment used by the data creating pseudo-ops. */
15906
15907 static void
15908 s_align (int x ATTRIBUTE_UNUSED)
15909 {
15910 int temp, fill_value, *fill_ptr;
15911 long max_alignment = 28;
15912
15913 /* o Note that the assembler pulls down any immediately preceding label
15914 to the aligned address.
15915 o It's not documented but auto alignment is reinstated by
15916 a .align pseudo instruction.
15917 o Note also that after auto alignment is turned off the mips assembler
15918 issues an error on attempt to assemble an improperly aligned data item.
15919 We don't. */
15920
15921 temp = get_absolute_expression ();
15922 if (temp > max_alignment)
15923 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
15924 else if (temp < 0)
15925 {
15926 as_warn (_("Alignment negative: 0 assumed."));
15927 temp = 0;
15928 }
15929 if (*input_line_pointer == ',')
15930 {
15931 ++input_line_pointer;
15932 fill_value = get_absolute_expression ();
15933 fill_ptr = &fill_value;
15934 }
15935 else
15936 fill_ptr = 0;
15937 if (temp)
15938 {
15939 segment_info_type *si = seg_info (now_seg);
15940 struct insn_label_list *l = si->label_list;
15941 /* Auto alignment should be switched on by next section change. */
15942 auto_align = 1;
15943 mips_align (temp, fill_ptr, l);
15944 }
15945 else
15946 {
15947 auto_align = 0;
15948 }
15949
15950 demand_empty_rest_of_line ();
15951 }
15952
15953 static void
15954 s_change_sec (int sec)
15955 {
15956 segT seg;
15957
15958 #ifdef OBJ_ELF
15959 /* The ELF backend needs to know that we are changing sections, so
15960 that .previous works correctly. We could do something like check
15961 for an obj_section_change_hook macro, but that might be confusing
15962 as it would not be appropriate to use it in the section changing
15963 functions in read.c, since obj-elf.c intercepts those. FIXME:
15964 This should be cleaner, somehow. */
15965 if (IS_ELF)
15966 obj_elf_section_change_hook ();
15967 #endif
15968
15969 mips_emit_delays ();
15970
15971 switch (sec)
15972 {
15973 case 't':
15974 s_text (0);
15975 break;
15976 case 'd':
15977 s_data (0);
15978 break;
15979 case 'b':
15980 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15981 demand_empty_rest_of_line ();
15982 break;
15983
15984 case 'r':
15985 seg = subseg_new (RDATA_SECTION_NAME,
15986 (subsegT) get_absolute_expression ());
15987 if (IS_ELF)
15988 {
15989 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15990 | SEC_READONLY | SEC_RELOC
15991 | SEC_DATA));
15992 if (strncmp (TARGET_OS, "elf", 3) != 0)
15993 record_alignment (seg, 4);
15994 }
15995 demand_empty_rest_of_line ();
15996 break;
15997
15998 case 's':
15999 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
16000 if (IS_ELF)
16001 {
16002 bfd_set_section_flags (stdoutput, seg,
16003 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16004 if (strncmp (TARGET_OS, "elf", 3) != 0)
16005 record_alignment (seg, 4);
16006 }
16007 demand_empty_rest_of_line ();
16008 break;
16009
16010 case 'B':
16011 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16012 if (IS_ELF)
16013 {
16014 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16015 if (strncmp (TARGET_OS, "elf", 3) != 0)
16016 record_alignment (seg, 4);
16017 }
16018 demand_empty_rest_of_line ();
16019 break;
16020 }
16021
16022 auto_align = 1;
16023 }
16024
16025 void
16026 s_change_section (int ignore ATTRIBUTE_UNUSED)
16027 {
16028 #ifdef OBJ_ELF
16029 char *section_name;
16030 char c;
16031 char next_c = 0;
16032 int section_type;
16033 int section_flag;
16034 int section_entry_size;
16035 int section_alignment;
16036
16037 if (!IS_ELF)
16038 return;
16039
16040 section_name = input_line_pointer;
16041 c = get_symbol_end ();
16042 if (c)
16043 next_c = *(input_line_pointer + 1);
16044
16045 /* Do we have .section Name<,"flags">? */
16046 if (c != ',' || (c == ',' && next_c == '"'))
16047 {
16048 /* just after name is now '\0'. */
16049 *input_line_pointer = c;
16050 input_line_pointer = section_name;
16051 obj_elf_section (ignore);
16052 return;
16053 }
16054 input_line_pointer++;
16055
16056 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16057 if (c == ',')
16058 section_type = get_absolute_expression ();
16059 else
16060 section_type = 0;
16061 if (*input_line_pointer++ == ',')
16062 section_flag = get_absolute_expression ();
16063 else
16064 section_flag = 0;
16065 if (*input_line_pointer++ == ',')
16066 section_entry_size = get_absolute_expression ();
16067 else
16068 section_entry_size = 0;
16069 if (*input_line_pointer++ == ',')
16070 section_alignment = get_absolute_expression ();
16071 else
16072 section_alignment = 0;
16073 /* FIXME: really ignore? */
16074 (void) section_alignment;
16075
16076 section_name = xstrdup (section_name);
16077
16078 /* When using the generic form of .section (as implemented by obj-elf.c),
16079 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16080 traditionally had to fall back on the more common @progbits instead.
16081
16082 There's nothing really harmful in this, since bfd will correct
16083 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
16084 means that, for backwards compatibility, the special_section entries
16085 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16086
16087 Even so, we shouldn't force users of the MIPS .section syntax to
16088 incorrectly label the sections as SHT_PROGBITS. The best compromise
16089 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16090 generic type-checking code. */
16091 if (section_type == SHT_MIPS_DWARF)
16092 section_type = SHT_PROGBITS;
16093
16094 obj_elf_change_section (section_name, section_type, section_flag,
16095 section_entry_size, 0, 0, 0);
16096
16097 if (now_seg->name != section_name)
16098 free (section_name);
16099 #endif /* OBJ_ELF */
16100 }
16101
16102 void
16103 mips_enable_auto_align (void)
16104 {
16105 auto_align = 1;
16106 }
16107
16108 static void
16109 s_cons (int log_size)
16110 {
16111 segment_info_type *si = seg_info (now_seg);
16112 struct insn_label_list *l = si->label_list;
16113
16114 mips_emit_delays ();
16115 if (log_size > 0 && auto_align)
16116 mips_align (log_size, 0, l);
16117 cons (1 << log_size);
16118 mips_clear_insn_labels ();
16119 }
16120
16121 static void
16122 s_float_cons (int type)
16123 {
16124 segment_info_type *si = seg_info (now_seg);
16125 struct insn_label_list *l = si->label_list;
16126
16127 mips_emit_delays ();
16128
16129 if (auto_align)
16130 {
16131 if (type == 'd')
16132 mips_align (3, 0, l);
16133 else
16134 mips_align (2, 0, l);
16135 }
16136
16137 float_cons (type);
16138 mips_clear_insn_labels ();
16139 }
16140
16141 /* Handle .globl. We need to override it because on Irix 5 you are
16142 permitted to say
16143 .globl foo .text
16144 where foo is an undefined symbol, to mean that foo should be
16145 considered to be the address of a function. */
16146
16147 static void
16148 s_mips_globl (int x ATTRIBUTE_UNUSED)
16149 {
16150 char *name;
16151 int c;
16152 symbolS *symbolP;
16153 flagword flag;
16154
16155 do
16156 {
16157 name = input_line_pointer;
16158 c = get_symbol_end ();
16159 symbolP = symbol_find_or_make (name);
16160 S_SET_EXTERNAL (symbolP);
16161
16162 *input_line_pointer = c;
16163 SKIP_WHITESPACE ();
16164
16165 /* On Irix 5, every global symbol that is not explicitly labelled as
16166 being a function is apparently labelled as being an object. */
16167 flag = BSF_OBJECT;
16168
16169 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16170 && (*input_line_pointer != ','))
16171 {
16172 char *secname;
16173 asection *sec;
16174
16175 secname = input_line_pointer;
16176 c = get_symbol_end ();
16177 sec = bfd_get_section_by_name (stdoutput, secname);
16178 if (sec == NULL)
16179 as_bad (_("%s: no such section"), secname);
16180 *input_line_pointer = c;
16181
16182 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16183 flag = BSF_FUNCTION;
16184 }
16185
16186 symbol_get_bfdsym (symbolP)->flags |= flag;
16187
16188 c = *input_line_pointer;
16189 if (c == ',')
16190 {
16191 input_line_pointer++;
16192 SKIP_WHITESPACE ();
16193 if (is_end_of_line[(unsigned char) *input_line_pointer])
16194 c = '\n';
16195 }
16196 }
16197 while (c == ',');
16198
16199 demand_empty_rest_of_line ();
16200 }
16201
16202 static void
16203 s_option (int x ATTRIBUTE_UNUSED)
16204 {
16205 char *opt;
16206 char c;
16207
16208 opt = input_line_pointer;
16209 c = get_symbol_end ();
16210
16211 if (*opt == 'O')
16212 {
16213 /* FIXME: What does this mean? */
16214 }
16215 else if (strncmp (opt, "pic", 3) == 0)
16216 {
16217 int i;
16218
16219 i = atoi (opt + 3);
16220 if (i == 0)
16221 mips_pic = NO_PIC;
16222 else if (i == 2)
16223 {
16224 mips_pic = SVR4_PIC;
16225 mips_abicalls = TRUE;
16226 }
16227 else
16228 as_bad (_(".option pic%d not supported"), i);
16229
16230 if (mips_pic == SVR4_PIC)
16231 {
16232 if (g_switch_seen && g_switch_value != 0)
16233 as_warn (_("-G may not be used with SVR4 PIC code"));
16234 g_switch_value = 0;
16235 bfd_set_gp_size (stdoutput, 0);
16236 }
16237 }
16238 else
16239 as_warn (_("Unrecognized option \"%s\""), opt);
16240
16241 *input_line_pointer = c;
16242 demand_empty_rest_of_line ();
16243 }
16244
16245 /* This structure is used to hold a stack of .set values. */
16246
16247 struct mips_option_stack
16248 {
16249 struct mips_option_stack *next;
16250 struct mips_set_options options;
16251 };
16252
16253 static struct mips_option_stack *mips_opts_stack;
16254
16255 /* Handle the .set pseudo-op. */
16256
16257 static void
16258 s_mipsset (int x ATTRIBUTE_UNUSED)
16259 {
16260 char *name = input_line_pointer, ch;
16261
16262 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16263 ++input_line_pointer;
16264 ch = *input_line_pointer;
16265 *input_line_pointer = '\0';
16266
16267 if (strcmp (name, "reorder") == 0)
16268 {
16269 if (mips_opts.noreorder)
16270 end_noreorder ();
16271 }
16272 else if (strcmp (name, "noreorder") == 0)
16273 {
16274 if (!mips_opts.noreorder)
16275 start_noreorder ();
16276 }
16277 else if (strncmp (name, "at=", 3) == 0)
16278 {
16279 char *s = name + 3;
16280
16281 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16282 as_bad (_("Unrecognized register name `%s'"), s);
16283 }
16284 else if (strcmp (name, "at") == 0)
16285 {
16286 mips_opts.at = ATREG;
16287 }
16288 else if (strcmp (name, "noat") == 0)
16289 {
16290 mips_opts.at = ZERO;
16291 }
16292 else if (strcmp (name, "macro") == 0)
16293 {
16294 mips_opts.warn_about_macros = 0;
16295 }
16296 else if (strcmp (name, "nomacro") == 0)
16297 {
16298 if (mips_opts.noreorder == 0)
16299 as_bad (_("`noreorder' must be set before `nomacro'"));
16300 mips_opts.warn_about_macros = 1;
16301 }
16302 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16303 {
16304 mips_opts.nomove = 0;
16305 }
16306 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16307 {
16308 mips_opts.nomove = 1;
16309 }
16310 else if (strcmp (name, "bopt") == 0)
16311 {
16312 mips_opts.nobopt = 0;
16313 }
16314 else if (strcmp (name, "nobopt") == 0)
16315 {
16316 mips_opts.nobopt = 1;
16317 }
16318 else if (strcmp (name, "gp=default") == 0)
16319 mips_opts.gp32 = file_mips_gp32;
16320 else if (strcmp (name, "gp=32") == 0)
16321 mips_opts.gp32 = 1;
16322 else if (strcmp (name, "gp=64") == 0)
16323 {
16324 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
16325 as_warn (_("%s isa does not support 64-bit registers"),
16326 mips_cpu_info_from_isa (mips_opts.isa)->name);
16327 mips_opts.gp32 = 0;
16328 }
16329 else if (strcmp (name, "fp=default") == 0)
16330 mips_opts.fp32 = file_mips_fp32;
16331 else if (strcmp (name, "fp=32") == 0)
16332 mips_opts.fp32 = 1;
16333 else if (strcmp (name, "fp=64") == 0)
16334 {
16335 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
16336 as_warn (_("%s isa does not support 64-bit floating point registers"),
16337 mips_cpu_info_from_isa (mips_opts.isa)->name);
16338 mips_opts.fp32 = 0;
16339 }
16340 else if (strcmp (name, "softfloat") == 0)
16341 mips_opts.soft_float = 1;
16342 else if (strcmp (name, "hardfloat") == 0)
16343 mips_opts.soft_float = 0;
16344 else if (strcmp (name, "singlefloat") == 0)
16345 mips_opts.single_float = 1;
16346 else if (strcmp (name, "doublefloat") == 0)
16347 mips_opts.single_float = 0;
16348 else if (strcmp (name, "mips16") == 0
16349 || strcmp (name, "MIPS-16") == 0)
16350 {
16351 if (mips_opts.micromips == 1)
16352 as_fatal (_("`mips16' cannot be used with `micromips'"));
16353 mips_opts.mips16 = 1;
16354 }
16355 else if (strcmp (name, "nomips16") == 0
16356 || strcmp (name, "noMIPS-16") == 0)
16357 mips_opts.mips16 = 0;
16358 else if (strcmp (name, "micromips") == 0)
16359 {
16360 if (mips_opts.mips16 == 1)
16361 as_fatal (_("`micromips' cannot be used with `mips16'"));
16362 mips_opts.micromips = 1;
16363 }
16364 else if (strcmp (name, "nomicromips") == 0)
16365 mips_opts.micromips = 0;
16366 else if (strcmp (name, "smartmips") == 0)
16367 {
16368 if (!ISA_SUPPORTS_SMARTMIPS)
16369 as_warn (_("%s ISA does not support SmartMIPS ASE"),
16370 mips_cpu_info_from_isa (mips_opts.isa)->name);
16371 mips_opts.ase_smartmips = 1;
16372 }
16373 else if (strcmp (name, "nosmartmips") == 0)
16374 mips_opts.ase_smartmips = 0;
16375 else if (strcmp (name, "mips3d") == 0)
16376 mips_opts.ase_mips3d = 1;
16377 else if (strcmp (name, "nomips3d") == 0)
16378 mips_opts.ase_mips3d = 0;
16379 else if (strcmp (name, "mdmx") == 0)
16380 mips_opts.ase_mdmx = 1;
16381 else if (strcmp (name, "nomdmx") == 0)
16382 mips_opts.ase_mdmx = 0;
16383 else if (strcmp (name, "dsp") == 0)
16384 {
16385 if (!ISA_SUPPORTS_DSP_ASE)
16386 as_warn (_("%s ISA does not support DSP ASE"),
16387 mips_cpu_info_from_isa (mips_opts.isa)->name);
16388 mips_opts.ase_dsp = 1;
16389 mips_opts.ase_dspr2 = 0;
16390 }
16391 else if (strcmp (name, "nodsp") == 0)
16392 {
16393 mips_opts.ase_dsp = 0;
16394 mips_opts.ase_dspr2 = 0;
16395 }
16396 else if (strcmp (name, "dspr2") == 0)
16397 {
16398 if (!ISA_SUPPORTS_DSPR2_ASE)
16399 as_warn (_("%s ISA does not support DSP R2 ASE"),
16400 mips_cpu_info_from_isa (mips_opts.isa)->name);
16401 mips_opts.ase_dspr2 = 1;
16402 mips_opts.ase_dsp = 1;
16403 }
16404 else if (strcmp (name, "nodspr2") == 0)
16405 {
16406 mips_opts.ase_dspr2 = 0;
16407 mips_opts.ase_dsp = 0;
16408 }
16409 else if (strcmp (name, "mt") == 0)
16410 {
16411 if (!ISA_SUPPORTS_MT_ASE)
16412 as_warn (_("%s ISA does not support MT ASE"),
16413 mips_cpu_info_from_isa (mips_opts.isa)->name);
16414 mips_opts.ase_mt = 1;
16415 }
16416 else if (strcmp (name, "nomt") == 0)
16417 mips_opts.ase_mt = 0;
16418 else if (strcmp (name, "mcu") == 0)
16419 mips_opts.ase_mcu = 1;
16420 else if (strcmp (name, "nomcu") == 0)
16421 mips_opts.ase_mcu = 0;
16422 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16423 {
16424 int reset = 0;
16425
16426 /* Permit the user to change the ISA and architecture on the fly.
16427 Needless to say, misuse can cause serious problems. */
16428 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16429 {
16430 reset = 1;
16431 mips_opts.isa = file_mips_isa;
16432 mips_opts.arch = file_mips_arch;
16433 }
16434 else if (strncmp (name, "arch=", 5) == 0)
16435 {
16436 const struct mips_cpu_info *p;
16437
16438 p = mips_parse_cpu("internal use", name + 5);
16439 if (!p)
16440 as_bad (_("unknown architecture %s"), name + 5);
16441 else
16442 {
16443 mips_opts.arch = p->cpu;
16444 mips_opts.isa = p->isa;
16445 }
16446 }
16447 else if (strncmp (name, "mips", 4) == 0)
16448 {
16449 const struct mips_cpu_info *p;
16450
16451 p = mips_parse_cpu("internal use", name);
16452 if (!p)
16453 as_bad (_("unknown ISA level %s"), name + 4);
16454 else
16455 {
16456 mips_opts.arch = p->cpu;
16457 mips_opts.isa = p->isa;
16458 }
16459 }
16460 else
16461 as_bad (_("unknown ISA or architecture %s"), name);
16462
16463 switch (mips_opts.isa)
16464 {
16465 case 0:
16466 break;
16467 case ISA_MIPS1:
16468 case ISA_MIPS2:
16469 case ISA_MIPS32:
16470 case ISA_MIPS32R2:
16471 mips_opts.gp32 = 1;
16472 mips_opts.fp32 = 1;
16473 break;
16474 case ISA_MIPS3:
16475 case ISA_MIPS4:
16476 case ISA_MIPS5:
16477 case ISA_MIPS64:
16478 case ISA_MIPS64R2:
16479 mips_opts.gp32 = 0;
16480 if (mips_opts.arch == CPU_R5900)
16481 {
16482 mips_opts.fp32 = 1;
16483 }
16484 else
16485 {
16486 mips_opts.fp32 = 0;
16487 }
16488 break;
16489 default:
16490 as_bad (_("unknown ISA level %s"), name + 4);
16491 break;
16492 }
16493 if (reset)
16494 {
16495 mips_opts.gp32 = file_mips_gp32;
16496 mips_opts.fp32 = file_mips_fp32;
16497 }
16498 }
16499 else if (strcmp (name, "autoextend") == 0)
16500 mips_opts.noautoextend = 0;
16501 else if (strcmp (name, "noautoextend") == 0)
16502 mips_opts.noautoextend = 1;
16503 else if (strcmp (name, "push") == 0)
16504 {
16505 struct mips_option_stack *s;
16506
16507 s = (struct mips_option_stack *) xmalloc (sizeof *s);
16508 s->next = mips_opts_stack;
16509 s->options = mips_opts;
16510 mips_opts_stack = s;
16511 }
16512 else if (strcmp (name, "pop") == 0)
16513 {
16514 struct mips_option_stack *s;
16515
16516 s = mips_opts_stack;
16517 if (s == NULL)
16518 as_bad (_(".set pop with no .set push"));
16519 else
16520 {
16521 /* If we're changing the reorder mode we need to handle
16522 delay slots correctly. */
16523 if (s->options.noreorder && ! mips_opts.noreorder)
16524 start_noreorder ();
16525 else if (! s->options.noreorder && mips_opts.noreorder)
16526 end_noreorder ();
16527
16528 mips_opts = s->options;
16529 mips_opts_stack = s->next;
16530 free (s);
16531 }
16532 }
16533 else if (strcmp (name, "sym32") == 0)
16534 mips_opts.sym32 = TRUE;
16535 else if (strcmp (name, "nosym32") == 0)
16536 mips_opts.sym32 = FALSE;
16537 else if (strchr (name, ','))
16538 {
16539 /* Generic ".set" directive; use the generic handler. */
16540 *input_line_pointer = ch;
16541 input_line_pointer = name;
16542 s_set (0);
16543 return;
16544 }
16545 else
16546 {
16547 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16548 }
16549 *input_line_pointer = ch;
16550 demand_empty_rest_of_line ();
16551 }
16552
16553 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
16554 .option pic2. It means to generate SVR4 PIC calls. */
16555
16556 static void
16557 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16558 {
16559 mips_pic = SVR4_PIC;
16560 mips_abicalls = TRUE;
16561
16562 if (g_switch_seen && g_switch_value != 0)
16563 as_warn (_("-G may not be used with SVR4 PIC code"));
16564 g_switch_value = 0;
16565
16566 bfd_set_gp_size (stdoutput, 0);
16567 demand_empty_rest_of_line ();
16568 }
16569
16570 /* Handle the .cpload pseudo-op. This is used when generating SVR4
16571 PIC code. It sets the $gp register for the function based on the
16572 function address, which is in the register named in the argument.
16573 This uses a relocation against _gp_disp, which is handled specially
16574 by the linker. The result is:
16575 lui $gp,%hi(_gp_disp)
16576 addiu $gp,$gp,%lo(_gp_disp)
16577 addu $gp,$gp,.cpload argument
16578 The .cpload argument is normally $25 == $t9.
16579
16580 The -mno-shared option changes this to:
16581 lui $gp,%hi(__gnu_local_gp)
16582 addiu $gp,$gp,%lo(__gnu_local_gp)
16583 and the argument is ignored. This saves an instruction, but the
16584 resulting code is not position independent; it uses an absolute
16585 address for __gnu_local_gp. Thus code assembled with -mno-shared
16586 can go into an ordinary executable, but not into a shared library. */
16587
16588 static void
16589 s_cpload (int ignore ATTRIBUTE_UNUSED)
16590 {
16591 expressionS ex;
16592 int reg;
16593 int in_shared;
16594
16595 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16596 .cpload is ignored. */
16597 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16598 {
16599 s_ignore (0);
16600 return;
16601 }
16602
16603 if (mips_opts.mips16)
16604 {
16605 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16606 ignore_rest_of_line ();
16607 return;
16608 }
16609
16610 /* .cpload should be in a .set noreorder section. */
16611 if (mips_opts.noreorder == 0)
16612 as_warn (_(".cpload not in noreorder section"));
16613
16614 reg = tc_get_register (0);
16615
16616 /* If we need to produce a 64-bit address, we are better off using
16617 the default instruction sequence. */
16618 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16619
16620 ex.X_op = O_symbol;
16621 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16622 "__gnu_local_gp");
16623 ex.X_op_symbol = NULL;
16624 ex.X_add_number = 0;
16625
16626 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16627 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16628
16629 mips_mark_labels ();
16630 mips_assembling_insn = TRUE;
16631
16632 macro_start ();
16633 macro_build_lui (&ex, mips_gp_register);
16634 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16635 mips_gp_register, BFD_RELOC_LO16);
16636 if (in_shared)
16637 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16638 mips_gp_register, reg);
16639 macro_end ();
16640
16641 mips_assembling_insn = FALSE;
16642 demand_empty_rest_of_line ();
16643 }
16644
16645 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16646 .cpsetup $reg1, offset|$reg2, label
16647
16648 If offset is given, this results in:
16649 sd $gp, offset($sp)
16650 lui $gp, %hi(%neg(%gp_rel(label)))
16651 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16652 daddu $gp, $gp, $reg1
16653
16654 If $reg2 is given, this results in:
16655 daddu $reg2, $gp, $0
16656 lui $gp, %hi(%neg(%gp_rel(label)))
16657 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16658 daddu $gp, $gp, $reg1
16659 $reg1 is normally $25 == $t9.
16660
16661 The -mno-shared option replaces the last three instructions with
16662 lui $gp,%hi(_gp)
16663 addiu $gp,$gp,%lo(_gp) */
16664
16665 static void
16666 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16667 {
16668 expressionS ex_off;
16669 expressionS ex_sym;
16670 int reg1;
16671
16672 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16673 We also need NewABI support. */
16674 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16675 {
16676 s_ignore (0);
16677 return;
16678 }
16679
16680 if (mips_opts.mips16)
16681 {
16682 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16683 ignore_rest_of_line ();
16684 return;
16685 }
16686
16687 reg1 = tc_get_register (0);
16688 SKIP_WHITESPACE ();
16689 if (*input_line_pointer != ',')
16690 {
16691 as_bad (_("missing argument separator ',' for .cpsetup"));
16692 return;
16693 }
16694 else
16695 ++input_line_pointer;
16696 SKIP_WHITESPACE ();
16697 if (*input_line_pointer == '$')
16698 {
16699 mips_cpreturn_register = tc_get_register (0);
16700 mips_cpreturn_offset = -1;
16701 }
16702 else
16703 {
16704 mips_cpreturn_offset = get_absolute_expression ();
16705 mips_cpreturn_register = -1;
16706 }
16707 SKIP_WHITESPACE ();
16708 if (*input_line_pointer != ',')
16709 {
16710 as_bad (_("missing argument separator ',' for .cpsetup"));
16711 return;
16712 }
16713 else
16714 ++input_line_pointer;
16715 SKIP_WHITESPACE ();
16716 expression (&ex_sym);
16717
16718 mips_mark_labels ();
16719 mips_assembling_insn = TRUE;
16720
16721 macro_start ();
16722 if (mips_cpreturn_register == -1)
16723 {
16724 ex_off.X_op = O_constant;
16725 ex_off.X_add_symbol = NULL;
16726 ex_off.X_op_symbol = NULL;
16727 ex_off.X_add_number = mips_cpreturn_offset;
16728
16729 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16730 BFD_RELOC_LO16, SP);
16731 }
16732 else
16733 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
16734 mips_gp_register, 0);
16735
16736 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16737 {
16738 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16739 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16740 BFD_RELOC_HI16_S);
16741
16742 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16743 mips_gp_register, -1, BFD_RELOC_GPREL16,
16744 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16745
16746 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16747 mips_gp_register, reg1);
16748 }
16749 else
16750 {
16751 expressionS ex;
16752
16753 ex.X_op = O_symbol;
16754 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16755 ex.X_op_symbol = NULL;
16756 ex.X_add_number = 0;
16757
16758 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16759 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16760
16761 macro_build_lui (&ex, mips_gp_register);
16762 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16763 mips_gp_register, BFD_RELOC_LO16);
16764 }
16765
16766 macro_end ();
16767
16768 mips_assembling_insn = FALSE;
16769 demand_empty_rest_of_line ();
16770 }
16771
16772 static void
16773 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16774 {
16775 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16776 .cplocal is ignored. */
16777 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16778 {
16779 s_ignore (0);
16780 return;
16781 }
16782
16783 if (mips_opts.mips16)
16784 {
16785 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16786 ignore_rest_of_line ();
16787 return;
16788 }
16789
16790 mips_gp_register = tc_get_register (0);
16791 demand_empty_rest_of_line ();
16792 }
16793
16794 /* Handle the .cprestore pseudo-op. This stores $gp into a given
16795 offset from $sp. The offset is remembered, and after making a PIC
16796 call $gp is restored from that location. */
16797
16798 static void
16799 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16800 {
16801 expressionS ex;
16802
16803 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16804 .cprestore is ignored. */
16805 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16806 {
16807 s_ignore (0);
16808 return;
16809 }
16810
16811 if (mips_opts.mips16)
16812 {
16813 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16814 ignore_rest_of_line ();
16815 return;
16816 }
16817
16818 mips_cprestore_offset = get_absolute_expression ();
16819 mips_cprestore_valid = 1;
16820
16821 ex.X_op = O_constant;
16822 ex.X_add_symbol = NULL;
16823 ex.X_op_symbol = NULL;
16824 ex.X_add_number = mips_cprestore_offset;
16825
16826 mips_mark_labels ();
16827 mips_assembling_insn = TRUE;
16828
16829 macro_start ();
16830 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16831 SP, HAVE_64BIT_ADDRESSES);
16832 macro_end ();
16833
16834 mips_assembling_insn = FALSE;
16835 demand_empty_rest_of_line ();
16836 }
16837
16838 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16839 was given in the preceding .cpsetup, it results in:
16840 ld $gp, offset($sp)
16841
16842 If a register $reg2 was given there, it results in:
16843 daddu $gp, $reg2, $0 */
16844
16845 static void
16846 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16847 {
16848 expressionS ex;
16849
16850 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16851 We also need NewABI support. */
16852 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16853 {
16854 s_ignore (0);
16855 return;
16856 }
16857
16858 if (mips_opts.mips16)
16859 {
16860 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16861 ignore_rest_of_line ();
16862 return;
16863 }
16864
16865 mips_mark_labels ();
16866 mips_assembling_insn = TRUE;
16867
16868 macro_start ();
16869 if (mips_cpreturn_register == -1)
16870 {
16871 ex.X_op = O_constant;
16872 ex.X_add_symbol = NULL;
16873 ex.X_op_symbol = NULL;
16874 ex.X_add_number = mips_cpreturn_offset;
16875
16876 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16877 }
16878 else
16879 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
16880 mips_cpreturn_register, 0);
16881 macro_end ();
16882
16883 mips_assembling_insn = FALSE;
16884 demand_empty_rest_of_line ();
16885 }
16886
16887 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16888 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16889 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16890 debug information or MIPS16 TLS. */
16891
16892 static void
16893 s_tls_rel_directive (const size_t bytes, const char *dirstr,
16894 bfd_reloc_code_real_type rtype)
16895 {
16896 expressionS ex;
16897 char *p;
16898
16899 expression (&ex);
16900
16901 if (ex.X_op != O_symbol)
16902 {
16903 as_bad (_("Unsupported use of %s"), dirstr);
16904 ignore_rest_of_line ();
16905 }
16906
16907 p = frag_more (bytes);
16908 md_number_to_chars (p, 0, bytes);
16909 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
16910 demand_empty_rest_of_line ();
16911 mips_clear_insn_labels ();
16912 }
16913
16914 /* Handle .dtprelword. */
16915
16916 static void
16917 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16918 {
16919 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
16920 }
16921
16922 /* Handle .dtpreldword. */
16923
16924 static void
16925 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16926 {
16927 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16928 }
16929
16930 /* Handle .tprelword. */
16931
16932 static void
16933 s_tprelword (int ignore ATTRIBUTE_UNUSED)
16934 {
16935 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16936 }
16937
16938 /* Handle .tpreldword. */
16939
16940 static void
16941 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16942 {
16943 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
16944 }
16945
16946 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16947 code. It sets the offset to use in gp_rel relocations. */
16948
16949 static void
16950 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
16951 {
16952 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16953 We also need NewABI support. */
16954 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16955 {
16956 s_ignore (0);
16957 return;
16958 }
16959
16960 mips_gprel_offset = get_absolute_expression ();
16961
16962 demand_empty_rest_of_line ();
16963 }
16964
16965 /* Handle the .gpword pseudo-op. This is used when generating PIC
16966 code. It generates a 32 bit GP relative reloc. */
16967
16968 static void
16969 s_gpword (int ignore ATTRIBUTE_UNUSED)
16970 {
16971 segment_info_type *si;
16972 struct insn_label_list *l;
16973 expressionS ex;
16974 char *p;
16975
16976 /* When not generating PIC code, this is treated as .word. */
16977 if (mips_pic != SVR4_PIC)
16978 {
16979 s_cons (2);
16980 return;
16981 }
16982
16983 si = seg_info (now_seg);
16984 l = si->label_list;
16985 mips_emit_delays ();
16986 if (auto_align)
16987 mips_align (2, 0, l);
16988
16989 expression (&ex);
16990 mips_clear_insn_labels ();
16991
16992 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16993 {
16994 as_bad (_("Unsupported use of .gpword"));
16995 ignore_rest_of_line ();
16996 }
16997
16998 p = frag_more (4);
16999 md_number_to_chars (p, 0, 4);
17000 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17001 BFD_RELOC_GPREL32);
17002
17003 demand_empty_rest_of_line ();
17004 }
17005
17006 static void
17007 s_gpdword (int ignore ATTRIBUTE_UNUSED)
17008 {
17009 segment_info_type *si;
17010 struct insn_label_list *l;
17011 expressionS ex;
17012 char *p;
17013
17014 /* When not generating PIC code, this is treated as .dword. */
17015 if (mips_pic != SVR4_PIC)
17016 {
17017 s_cons (3);
17018 return;
17019 }
17020
17021 si = seg_info (now_seg);
17022 l = si->label_list;
17023 mips_emit_delays ();
17024 if (auto_align)
17025 mips_align (3, 0, l);
17026
17027 expression (&ex);
17028 mips_clear_insn_labels ();
17029
17030 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17031 {
17032 as_bad (_("Unsupported use of .gpdword"));
17033 ignore_rest_of_line ();
17034 }
17035
17036 p = frag_more (8);
17037 md_number_to_chars (p, 0, 8);
17038 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17039 BFD_RELOC_GPREL32)->fx_tcbit = 1;
17040
17041 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
17042 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17043 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
17044
17045 demand_empty_rest_of_line ();
17046 }
17047
17048 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
17049 tables in SVR4 PIC code. */
17050
17051 static void
17052 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17053 {
17054 int reg;
17055
17056 /* This is ignored when not generating SVR4 PIC code. */
17057 if (mips_pic != SVR4_PIC)
17058 {
17059 s_ignore (0);
17060 return;
17061 }
17062
17063 mips_mark_labels ();
17064 mips_assembling_insn = TRUE;
17065
17066 /* Add $gp to the register named as an argument. */
17067 macro_start ();
17068 reg = tc_get_register (0);
17069 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17070 macro_end ();
17071
17072 mips_assembling_insn = FALSE;
17073 demand_empty_rest_of_line ();
17074 }
17075
17076 /* Handle the .insn pseudo-op. This marks instruction labels in
17077 mips16/micromips mode. This permits the linker to handle them specially,
17078 such as generating jalx instructions when needed. We also make
17079 them odd for the duration of the assembly, in order to generate the
17080 right sort of code. We will make them even in the adjust_symtab
17081 routine, while leaving them marked. This is convenient for the
17082 debugger and the disassembler. The linker knows to make them odd
17083 again. */
17084
17085 static void
17086 s_insn (int ignore ATTRIBUTE_UNUSED)
17087 {
17088 mips_mark_labels ();
17089
17090 demand_empty_rest_of_line ();
17091 }
17092
17093 /* Handle a .stab[snd] directive. Ideally these directives would be
17094 implemented in a transparent way, so that removing them would not
17095 have any effect on the generated instructions. However, s_stab
17096 internally changes the section, so in practice we need to decide
17097 now whether the preceding label marks compressed code. We do not
17098 support changing the compression mode of a label after a .stab*
17099 directive, such as in:
17100
17101 foo:
17102 .stabs ...
17103 .set mips16
17104
17105 so the current mode wins. */
17106
17107 static void
17108 s_mips_stab (int type)
17109 {
17110 mips_mark_labels ();
17111 s_stab (type);
17112 }
17113
17114 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
17115
17116 static void
17117 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17118 {
17119 char *name;
17120 int c;
17121 symbolS *symbolP;
17122 expressionS exp;
17123
17124 name = input_line_pointer;
17125 c = get_symbol_end ();
17126 symbolP = symbol_find_or_make (name);
17127 S_SET_WEAK (symbolP);
17128 *input_line_pointer = c;
17129
17130 SKIP_WHITESPACE ();
17131
17132 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17133 {
17134 if (S_IS_DEFINED (symbolP))
17135 {
17136 as_bad (_("ignoring attempt to redefine symbol %s"),
17137 S_GET_NAME (symbolP));
17138 ignore_rest_of_line ();
17139 return;
17140 }
17141
17142 if (*input_line_pointer == ',')
17143 {
17144 ++input_line_pointer;
17145 SKIP_WHITESPACE ();
17146 }
17147
17148 expression (&exp);
17149 if (exp.X_op != O_symbol)
17150 {
17151 as_bad (_("bad .weakext directive"));
17152 ignore_rest_of_line ();
17153 return;
17154 }
17155 symbol_set_value_expression (symbolP, &exp);
17156 }
17157
17158 demand_empty_rest_of_line ();
17159 }
17160
17161 /* Parse a register string into a number. Called from the ECOFF code
17162 to parse .frame. The argument is non-zero if this is the frame
17163 register, so that we can record it in mips_frame_reg. */
17164
17165 int
17166 tc_get_register (int frame)
17167 {
17168 unsigned int reg;
17169
17170 SKIP_WHITESPACE ();
17171 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17172 reg = 0;
17173 if (frame)
17174 {
17175 mips_frame_reg = reg != 0 ? reg : SP;
17176 mips_frame_reg_valid = 1;
17177 mips_cprestore_valid = 0;
17178 }
17179 return reg;
17180 }
17181
17182 valueT
17183 md_section_align (asection *seg, valueT addr)
17184 {
17185 int align = bfd_get_section_alignment (stdoutput, seg);
17186
17187 if (IS_ELF)
17188 {
17189 /* We don't need to align ELF sections to the full alignment.
17190 However, Irix 5 may prefer that we align them at least to a 16
17191 byte boundary. We don't bother to align the sections if we
17192 are targeted for an embedded system. */
17193 if (strncmp (TARGET_OS, "elf", 3) == 0)
17194 return addr;
17195 if (align > 4)
17196 align = 4;
17197 }
17198
17199 return ((addr + (1 << align) - 1) & (-1 << align));
17200 }
17201
17202 /* Utility routine, called from above as well. If called while the
17203 input file is still being read, it's only an approximation. (For
17204 example, a symbol may later become defined which appeared to be
17205 undefined earlier.) */
17206
17207 static int
17208 nopic_need_relax (symbolS *sym, int before_relaxing)
17209 {
17210 if (sym == 0)
17211 return 0;
17212
17213 if (g_switch_value > 0)
17214 {
17215 const char *symname;
17216 int change;
17217
17218 /* Find out whether this symbol can be referenced off the $gp
17219 register. It can be if it is smaller than the -G size or if
17220 it is in the .sdata or .sbss section. Certain symbols can
17221 not be referenced off the $gp, although it appears as though
17222 they can. */
17223 symname = S_GET_NAME (sym);
17224 if (symname != (const char *) NULL
17225 && (strcmp (symname, "eprol") == 0
17226 || strcmp (symname, "etext") == 0
17227 || strcmp (symname, "_gp") == 0
17228 || strcmp (symname, "edata") == 0
17229 || strcmp (symname, "_fbss") == 0
17230 || strcmp (symname, "_fdata") == 0
17231 || strcmp (symname, "_ftext") == 0
17232 || strcmp (symname, "end") == 0
17233 || strcmp (symname, "_gp_disp") == 0))
17234 change = 1;
17235 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17236 && (0
17237 #ifndef NO_ECOFF_DEBUGGING
17238 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17239 && (symbol_get_obj (sym)->ecoff_extern_size
17240 <= g_switch_value))
17241 #endif
17242 /* We must defer this decision until after the whole
17243 file has been read, since there might be a .extern
17244 after the first use of this symbol. */
17245 || (before_relaxing
17246 #ifndef NO_ECOFF_DEBUGGING
17247 && symbol_get_obj (sym)->ecoff_extern_size == 0
17248 #endif
17249 && S_GET_VALUE (sym) == 0)
17250 || (S_GET_VALUE (sym) != 0
17251 && S_GET_VALUE (sym) <= g_switch_value)))
17252 change = 0;
17253 else
17254 {
17255 const char *segname;
17256
17257 segname = segment_name (S_GET_SEGMENT (sym));
17258 gas_assert (strcmp (segname, ".lit8") != 0
17259 && strcmp (segname, ".lit4") != 0);
17260 change = (strcmp (segname, ".sdata") != 0
17261 && strcmp (segname, ".sbss") != 0
17262 && strncmp (segname, ".sdata.", 7) != 0
17263 && strncmp (segname, ".sbss.", 6) != 0
17264 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17265 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17266 }
17267 return change;
17268 }
17269 else
17270 /* We are not optimizing for the $gp register. */
17271 return 1;
17272 }
17273
17274
17275 /* Return true if the given symbol should be considered local for SVR4 PIC. */
17276
17277 static bfd_boolean
17278 pic_need_relax (symbolS *sym, asection *segtype)
17279 {
17280 asection *symsec;
17281
17282 /* Handle the case of a symbol equated to another symbol. */
17283 while (symbol_equated_reloc_p (sym))
17284 {
17285 symbolS *n;
17286
17287 /* It's possible to get a loop here in a badly written program. */
17288 n = symbol_get_value_expression (sym)->X_add_symbol;
17289 if (n == sym)
17290 break;
17291 sym = n;
17292 }
17293
17294 if (symbol_section_p (sym))
17295 return TRUE;
17296
17297 symsec = S_GET_SEGMENT (sym);
17298
17299 /* This must duplicate the test in adjust_reloc_syms. */
17300 return (!bfd_is_und_section (symsec)
17301 && !bfd_is_abs_section (symsec)
17302 && !bfd_is_com_section (symsec)
17303 && !s_is_linkonce (sym, segtype)
17304 #ifdef OBJ_ELF
17305 /* A global or weak symbol is treated as external. */
17306 && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
17307 #endif
17308 );
17309 }
17310
17311
17312 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17313 extended opcode. SEC is the section the frag is in. */
17314
17315 static int
17316 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17317 {
17318 int type;
17319 const struct mips16_immed_operand *op;
17320 offsetT val;
17321 int mintiny, maxtiny;
17322 segT symsec;
17323 fragS *sym_frag;
17324
17325 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17326 return 0;
17327 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17328 return 1;
17329
17330 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17331 op = mips16_immed_operands;
17332 while (op->type != type)
17333 {
17334 ++op;
17335 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
17336 }
17337
17338 if (op->unsp)
17339 {
17340 if (type == '<' || type == '>' || type == '[' || type == ']')
17341 {
17342 mintiny = 1;
17343 maxtiny = 1 << op->nbits;
17344 }
17345 else
17346 {
17347 mintiny = 0;
17348 maxtiny = (1 << op->nbits) - 1;
17349 }
17350 }
17351 else
17352 {
17353 mintiny = - (1 << (op->nbits - 1));
17354 maxtiny = (1 << (op->nbits - 1)) - 1;
17355 }
17356
17357 sym_frag = symbol_get_frag (fragp->fr_symbol);
17358 val = S_GET_VALUE (fragp->fr_symbol);
17359 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17360
17361 if (op->pcrel)
17362 {
17363 addressT addr;
17364
17365 /* We won't have the section when we are called from
17366 mips_relax_frag. However, we will always have been called
17367 from md_estimate_size_before_relax first. If this is a
17368 branch to a different section, we mark it as such. If SEC is
17369 NULL, and the frag is not marked, then it must be a branch to
17370 the same section. */
17371 if (sec == NULL)
17372 {
17373 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17374 return 1;
17375 }
17376 else
17377 {
17378 /* Must have been called from md_estimate_size_before_relax. */
17379 if (symsec != sec)
17380 {
17381 fragp->fr_subtype =
17382 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17383
17384 /* FIXME: We should support this, and let the linker
17385 catch branches and loads that are out of range. */
17386 as_bad_where (fragp->fr_file, fragp->fr_line,
17387 _("unsupported PC relative reference to different section"));
17388
17389 return 1;
17390 }
17391 if (fragp != sym_frag && sym_frag->fr_address == 0)
17392 /* Assume non-extended on the first relaxation pass.
17393 The address we have calculated will be bogus if this is
17394 a forward branch to another frag, as the forward frag
17395 will have fr_address == 0. */
17396 return 0;
17397 }
17398
17399 /* In this case, we know for sure that the symbol fragment is in
17400 the same section. If the relax_marker of the symbol fragment
17401 differs from the relax_marker of this fragment, we have not
17402 yet adjusted the symbol fragment fr_address. We want to add
17403 in STRETCH in order to get a better estimate of the address.
17404 This particularly matters because of the shift bits. */
17405 if (stretch != 0
17406 && sym_frag->relax_marker != fragp->relax_marker)
17407 {
17408 fragS *f;
17409
17410 /* Adjust stretch for any alignment frag. Note that if have
17411 been expanding the earlier code, the symbol may be
17412 defined in what appears to be an earlier frag. FIXME:
17413 This doesn't handle the fr_subtype field, which specifies
17414 a maximum number of bytes to skip when doing an
17415 alignment. */
17416 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17417 {
17418 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17419 {
17420 if (stretch < 0)
17421 stretch = - ((- stretch)
17422 & ~ ((1 << (int) f->fr_offset) - 1));
17423 else
17424 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17425 if (stretch == 0)
17426 break;
17427 }
17428 }
17429 if (f != NULL)
17430 val += stretch;
17431 }
17432
17433 addr = fragp->fr_address + fragp->fr_fix;
17434
17435 /* The base address rules are complicated. The base address of
17436 a branch is the following instruction. The base address of a
17437 PC relative load or add is the instruction itself, but if it
17438 is in a delay slot (in which case it can not be extended) use
17439 the address of the instruction whose delay slot it is in. */
17440 if (type == 'p' || type == 'q')
17441 {
17442 addr += 2;
17443
17444 /* If we are currently assuming that this frag should be
17445 extended, then, the current address is two bytes
17446 higher. */
17447 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17448 addr += 2;
17449
17450 /* Ignore the low bit in the target, since it will be set
17451 for a text label. */
17452 if ((val & 1) != 0)
17453 --val;
17454 }
17455 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17456 addr -= 4;
17457 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17458 addr -= 2;
17459
17460 val -= addr & ~ ((1 << op->shift) - 1);
17461
17462 /* Branch offsets have an implicit 0 in the lowest bit. */
17463 if (type == 'p' || type == 'q')
17464 val /= 2;
17465
17466 /* If any of the shifted bits are set, we must use an extended
17467 opcode. If the address depends on the size of this
17468 instruction, this can lead to a loop, so we arrange to always
17469 use an extended opcode. We only check this when we are in
17470 the main relaxation loop, when SEC is NULL. */
17471 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17472 {
17473 fragp->fr_subtype =
17474 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17475 return 1;
17476 }
17477
17478 /* If we are about to mark a frag as extended because the value
17479 is precisely maxtiny + 1, then there is a chance of an
17480 infinite loop as in the following code:
17481 la $4,foo
17482 .skip 1020
17483 .align 2
17484 foo:
17485 In this case when the la is extended, foo is 0x3fc bytes
17486 away, so the la can be shrunk, but then foo is 0x400 away, so
17487 the la must be extended. To avoid this loop, we mark the
17488 frag as extended if it was small, and is about to become
17489 extended with a value of maxtiny + 1. */
17490 if (val == ((maxtiny + 1) << op->shift)
17491 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17492 && sec == NULL)
17493 {
17494 fragp->fr_subtype =
17495 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17496 return 1;
17497 }
17498 }
17499 else if (symsec != absolute_section && sec != NULL)
17500 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17501
17502 if ((val & ((1 << op->shift) - 1)) != 0
17503 || val < (mintiny << op->shift)
17504 || val > (maxtiny << op->shift))
17505 return 1;
17506 else
17507 return 0;
17508 }
17509
17510 /* Compute the length of a branch sequence, and adjust the
17511 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17512 worst-case length is computed, with UPDATE being used to indicate
17513 whether an unconditional (-1), branch-likely (+1) or regular (0)
17514 branch is to be computed. */
17515 static int
17516 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17517 {
17518 bfd_boolean toofar;
17519 int length;
17520
17521 if (fragp
17522 && S_IS_DEFINED (fragp->fr_symbol)
17523 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17524 {
17525 addressT addr;
17526 offsetT val;
17527
17528 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17529
17530 addr = fragp->fr_address + fragp->fr_fix + 4;
17531
17532 val -= addr;
17533
17534 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17535 }
17536 else if (fragp)
17537 /* If the symbol is not defined or it's in a different segment,
17538 assume the user knows what's going on and emit a short
17539 branch. */
17540 toofar = FALSE;
17541 else
17542 toofar = TRUE;
17543
17544 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17545 fragp->fr_subtype
17546 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17547 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17548 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17549 RELAX_BRANCH_LINK (fragp->fr_subtype),
17550 toofar);
17551
17552 length = 4;
17553 if (toofar)
17554 {
17555 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17556 length += 8;
17557
17558 if (mips_pic != NO_PIC)
17559 {
17560 /* Additional space for PIC loading of target address. */
17561 length += 8;
17562 if (mips_opts.isa == ISA_MIPS1)
17563 /* Additional space for $at-stabilizing nop. */
17564 length += 4;
17565 }
17566
17567 /* If branch is conditional. */
17568 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17569 length += 8;
17570 }
17571
17572 return length;
17573 }
17574
17575 /* Compute the length of a branch sequence, and adjust the
17576 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17577 worst-case length is computed, with UPDATE being used to indicate
17578 whether an unconditional (-1), or regular (0) branch is to be
17579 computed. */
17580
17581 static int
17582 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17583 {
17584 bfd_boolean toofar;
17585 int length;
17586
17587 if (fragp
17588 && S_IS_DEFINED (fragp->fr_symbol)
17589 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17590 {
17591 addressT addr;
17592 offsetT val;
17593
17594 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17595 /* Ignore the low bit in the target, since it will be set
17596 for a text label. */
17597 if ((val & 1) != 0)
17598 --val;
17599
17600 addr = fragp->fr_address + fragp->fr_fix + 4;
17601
17602 val -= addr;
17603
17604 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17605 }
17606 else if (fragp)
17607 /* If the symbol is not defined or it's in a different segment,
17608 assume the user knows what's going on and emit a short
17609 branch. */
17610 toofar = FALSE;
17611 else
17612 toofar = TRUE;
17613
17614 if (fragp && update
17615 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17616 fragp->fr_subtype = (toofar
17617 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17618 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17619
17620 length = 4;
17621 if (toofar)
17622 {
17623 bfd_boolean compact_known = fragp != NULL;
17624 bfd_boolean compact = FALSE;
17625 bfd_boolean uncond;
17626
17627 if (compact_known)
17628 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17629 if (fragp)
17630 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17631 else
17632 uncond = update < 0;
17633
17634 /* If label is out of range, we turn branch <br>:
17635
17636 <br> label # 4 bytes
17637 0:
17638
17639 into:
17640
17641 j label # 4 bytes
17642 nop # 2 bytes if compact && !PIC
17643 0:
17644 */
17645 if (mips_pic == NO_PIC && (!compact_known || compact))
17646 length += 2;
17647
17648 /* If assembling PIC code, we further turn:
17649
17650 j label # 4 bytes
17651
17652 into:
17653
17654 lw/ld at, %got(label)(gp) # 4 bytes
17655 d/addiu at, %lo(label) # 4 bytes
17656 jr/c at # 2 bytes
17657 */
17658 if (mips_pic != NO_PIC)
17659 length += 6;
17660
17661 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17662
17663 <brneg> 0f # 4 bytes
17664 nop # 2 bytes if !compact
17665 */
17666 if (!uncond)
17667 length += (compact_known && compact) ? 4 : 6;
17668 }
17669
17670 return length;
17671 }
17672
17673 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17674 bit accordingly. */
17675
17676 static int
17677 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17678 {
17679 bfd_boolean toofar;
17680
17681 if (fragp
17682 && S_IS_DEFINED (fragp->fr_symbol)
17683 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17684 {
17685 addressT addr;
17686 offsetT val;
17687 int type;
17688
17689 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17690 /* Ignore the low bit in the target, since it will be set
17691 for a text label. */
17692 if ((val & 1) != 0)
17693 --val;
17694
17695 /* Assume this is a 2-byte branch. */
17696 addr = fragp->fr_address + fragp->fr_fix + 2;
17697
17698 /* We try to avoid the infinite loop by not adding 2 more bytes for
17699 long branches. */
17700
17701 val -= addr;
17702
17703 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17704 if (type == 'D')
17705 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17706 else if (type == 'E')
17707 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17708 else
17709 abort ();
17710 }
17711 else
17712 /* If the symbol is not defined or it's in a different segment,
17713 we emit a normal 32-bit branch. */
17714 toofar = TRUE;
17715
17716 if (fragp && update
17717 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17718 fragp->fr_subtype
17719 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17720 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17721
17722 if (toofar)
17723 return 4;
17724
17725 return 2;
17726 }
17727
17728 /* Estimate the size of a frag before relaxing. Unless this is the
17729 mips16, we are not really relaxing here, and the final size is
17730 encoded in the subtype information. For the mips16, we have to
17731 decide whether we are using an extended opcode or not. */
17732
17733 int
17734 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17735 {
17736 int change;
17737
17738 if (RELAX_BRANCH_P (fragp->fr_subtype))
17739 {
17740
17741 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17742
17743 return fragp->fr_var;
17744 }
17745
17746 if (RELAX_MIPS16_P (fragp->fr_subtype))
17747 /* We don't want to modify the EXTENDED bit here; it might get us
17748 into infinite loops. We change it only in mips_relax_frag(). */
17749 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
17750
17751 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17752 {
17753 int length = 4;
17754
17755 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17756 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17757 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17758 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17759 fragp->fr_var = length;
17760
17761 return length;
17762 }
17763
17764 if (mips_pic == NO_PIC)
17765 change = nopic_need_relax (fragp->fr_symbol, 0);
17766 else if (mips_pic == SVR4_PIC)
17767 change = pic_need_relax (fragp->fr_symbol, segtype);
17768 else if (mips_pic == VXWORKS_PIC)
17769 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17770 change = 0;
17771 else
17772 abort ();
17773
17774 if (change)
17775 {
17776 fragp->fr_subtype |= RELAX_USE_SECOND;
17777 return -RELAX_FIRST (fragp->fr_subtype);
17778 }
17779 else
17780 return -RELAX_SECOND (fragp->fr_subtype);
17781 }
17782
17783 /* This is called to see whether a reloc against a defined symbol
17784 should be converted into a reloc against a section. */
17785
17786 int
17787 mips_fix_adjustable (fixS *fixp)
17788 {
17789 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17790 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17791 return 0;
17792
17793 if (fixp->fx_addsy == NULL)
17794 return 1;
17795
17796 /* If symbol SYM is in a mergeable section, relocations of the form
17797 SYM + 0 can usually be made section-relative. The mergeable data
17798 is then identified by the section offset rather than by the symbol.
17799
17800 However, if we're generating REL LO16 relocations, the offset is split
17801 between the LO16 and parterning high part relocation. The linker will
17802 need to recalculate the complete offset in order to correctly identify
17803 the merge data.
17804
17805 The linker has traditionally not looked for the parterning high part
17806 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17807 placed anywhere. Rather than break backwards compatibility by changing
17808 this, it seems better not to force the issue, and instead keep the
17809 original symbol. This will work with either linker behavior. */
17810 if ((lo16_reloc_p (fixp->fx_r_type)
17811 || reloc_needs_lo_p (fixp->fx_r_type))
17812 && HAVE_IN_PLACE_ADDENDS
17813 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17814 return 0;
17815
17816 /* There is no place to store an in-place offset for JALR relocations.
17817 Likewise an in-range offset of PC-relative relocations may overflow
17818 the in-place relocatable field if recalculated against the start
17819 address of the symbol's containing section. */
17820 if (HAVE_IN_PLACE_ADDENDS
17821 && (fixp->fx_pcrel || jalr_reloc_p (fixp->fx_r_type)))
17822 return 0;
17823
17824 #ifdef OBJ_ELF
17825 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17826 to a floating-point stub. The same is true for non-R_MIPS16_26
17827 relocations against MIPS16 functions; in this case, the stub becomes
17828 the function's canonical address.
17829
17830 Floating-point stubs are stored in unique .mips16.call.* or
17831 .mips16.fn.* sections. If a stub T for function F is in section S,
17832 the first relocation in section S must be against F; this is how the
17833 linker determines the target function. All relocations that might
17834 resolve to T must also be against F. We therefore have the following
17835 restrictions, which are given in an intentionally-redundant way:
17836
17837 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17838 symbols.
17839
17840 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17841 if that stub might be used.
17842
17843 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17844 symbols.
17845
17846 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17847 that stub might be used.
17848
17849 There is a further restriction:
17850
17851 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17852 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17853 targets with in-place addends; the relocation field cannot
17854 encode the low bit.
17855
17856 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17857 against a MIPS16 symbol. We deal with (5) by by not reducing any
17858 such relocations on REL targets.
17859
17860 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17861 relocation against some symbol R, no relocation against R may be
17862 reduced. (Note that this deals with (2) as well as (1) because
17863 relocations against global symbols will never be reduced on ELF
17864 targets.) This approach is a little simpler than trying to detect
17865 stub sections, and gives the "all or nothing" per-symbol consistency
17866 that we have for MIPS16 symbols. */
17867 if (IS_ELF
17868 && fixp->fx_subsy == NULL
17869 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
17870 || *symbol_get_tc (fixp->fx_addsy)
17871 || (HAVE_IN_PLACE_ADDENDS
17872 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17873 && jmp_reloc_p (fixp->fx_r_type))))
17874 return 0;
17875 #endif
17876
17877 return 1;
17878 }
17879
17880 /* Translate internal representation of relocation info to BFD target
17881 format. */
17882
17883 arelent **
17884 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
17885 {
17886 static arelent *retval[4];
17887 arelent *reloc;
17888 bfd_reloc_code_real_type code;
17889
17890 memset (retval, 0, sizeof(retval));
17891 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
17892 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
17893 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
17894 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17895
17896 if (fixp->fx_pcrel)
17897 {
17898 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17899 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17900 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17901 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
17902
17903 /* At this point, fx_addnumber is "symbol offset - pcrel address".
17904 Relocations want only the symbol offset. */
17905 reloc->addend = fixp->fx_addnumber + reloc->address;
17906 if (!IS_ELF)
17907 {
17908 /* A gruesome hack which is a result of the gruesome gas
17909 reloc handling. What's worse, for COFF (as opposed to
17910 ECOFF), we might need yet another copy of reloc->address.
17911 See bfd_install_relocation. */
17912 reloc->addend += reloc->address;
17913 }
17914 }
17915 else
17916 reloc->addend = fixp->fx_addnumber;
17917
17918 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17919 entry to be used in the relocation's section offset. */
17920 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17921 {
17922 reloc->address = reloc->addend;
17923 reloc->addend = 0;
17924 }
17925
17926 code = fixp->fx_r_type;
17927
17928 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
17929 if (reloc->howto == NULL)
17930 {
17931 as_bad_where (fixp->fx_file, fixp->fx_line,
17932 _("Can not represent %s relocation in this object file format"),
17933 bfd_get_reloc_code_name (code));
17934 retval[0] = NULL;
17935 }
17936
17937 return retval;
17938 }
17939
17940 /* Relax a machine dependent frag. This returns the amount by which
17941 the current size of the frag should change. */
17942
17943 int
17944 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
17945 {
17946 if (RELAX_BRANCH_P (fragp->fr_subtype))
17947 {
17948 offsetT old_var = fragp->fr_var;
17949
17950 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
17951
17952 return fragp->fr_var - old_var;
17953 }
17954
17955 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17956 {
17957 offsetT old_var = fragp->fr_var;
17958 offsetT new_var = 4;
17959
17960 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17961 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17962 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17963 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17964 fragp->fr_var = new_var;
17965
17966 return new_var - old_var;
17967 }
17968
17969 if (! RELAX_MIPS16_P (fragp->fr_subtype))
17970 return 0;
17971
17972 if (mips16_extended_frag (fragp, NULL, stretch))
17973 {
17974 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17975 return 0;
17976 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17977 return 2;
17978 }
17979 else
17980 {
17981 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17982 return 0;
17983 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17984 return -2;
17985 }
17986
17987 return 0;
17988 }
17989
17990 /* Convert a machine dependent frag. */
17991
17992 void
17993 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
17994 {
17995 if (RELAX_BRANCH_P (fragp->fr_subtype))
17996 {
17997 char *buf;
17998 unsigned long insn;
17999 expressionS exp;
18000 fixS *fixp;
18001
18002 buf = fragp->fr_literal + fragp->fr_fix;
18003 insn = read_insn (buf);
18004
18005 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18006 {
18007 /* We generate a fixup instead of applying it right now
18008 because, if there are linker relaxations, we're going to
18009 need the relocations. */
18010 exp.X_op = O_symbol;
18011 exp.X_add_symbol = fragp->fr_symbol;
18012 exp.X_add_number = fragp->fr_offset;
18013
18014 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18015 BFD_RELOC_16_PCREL_S2);
18016 fixp->fx_file = fragp->fr_file;
18017 fixp->fx_line = fragp->fr_line;
18018
18019 buf = write_insn (buf, insn);
18020 }
18021 else
18022 {
18023 int i;
18024
18025 as_warn_where (fragp->fr_file, fragp->fr_line,
18026 _("Relaxed out-of-range branch into a jump"));
18027
18028 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18029 goto uncond;
18030
18031 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18032 {
18033 /* Reverse the branch. */
18034 switch ((insn >> 28) & 0xf)
18035 {
18036 case 4:
18037 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
18038 have the condition reversed by tweaking a single
18039 bit, and their opcodes all have 0x4???????. */
18040 gas_assert ((insn & 0xf1000000) == 0x41000000);
18041 insn ^= 0x00010000;
18042 break;
18043
18044 case 0:
18045 /* bltz 0x04000000 bgez 0x04010000
18046 bltzal 0x04100000 bgezal 0x04110000 */
18047 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18048 insn ^= 0x00010000;
18049 break;
18050
18051 case 1:
18052 /* beq 0x10000000 bne 0x14000000
18053 blez 0x18000000 bgtz 0x1c000000 */
18054 insn ^= 0x04000000;
18055 break;
18056
18057 default:
18058 abort ();
18059 }
18060 }
18061
18062 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18063 {
18064 /* Clear the and-link bit. */
18065 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18066
18067 /* bltzal 0x04100000 bgezal 0x04110000
18068 bltzall 0x04120000 bgezall 0x04130000 */
18069 insn &= ~0x00100000;
18070 }
18071
18072 /* Branch over the branch (if the branch was likely) or the
18073 full jump (not likely case). Compute the offset from the
18074 current instruction to branch to. */
18075 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18076 i = 16;
18077 else
18078 {
18079 /* How many bytes in instructions we've already emitted? */
18080 i = buf - fragp->fr_literal - fragp->fr_fix;
18081 /* How many bytes in instructions from here to the end? */
18082 i = fragp->fr_var - i;
18083 }
18084 /* Convert to instruction count. */
18085 i >>= 2;
18086 /* Branch counts from the next instruction. */
18087 i--;
18088 insn |= i;
18089 /* Branch over the jump. */
18090 buf = write_insn (buf, insn);
18091
18092 /* nop */
18093 buf = write_insn (buf, 0);
18094
18095 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18096 {
18097 /* beql $0, $0, 2f */
18098 insn = 0x50000000;
18099 /* Compute the PC offset from the current instruction to
18100 the end of the variable frag. */
18101 /* How many bytes in instructions we've already emitted? */
18102 i = buf - fragp->fr_literal - fragp->fr_fix;
18103 /* How many bytes in instructions from here to the end? */
18104 i = fragp->fr_var - i;
18105 /* Convert to instruction count. */
18106 i >>= 2;
18107 /* Don't decrement i, because we want to branch over the
18108 delay slot. */
18109 insn |= i;
18110
18111 buf = write_insn (buf, insn);
18112 buf = write_insn (buf, 0);
18113 }
18114
18115 uncond:
18116 if (mips_pic == NO_PIC)
18117 {
18118 /* j or jal. */
18119 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18120 ? 0x0c000000 : 0x08000000);
18121 exp.X_op = O_symbol;
18122 exp.X_add_symbol = fragp->fr_symbol;
18123 exp.X_add_number = fragp->fr_offset;
18124
18125 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18126 FALSE, BFD_RELOC_MIPS_JMP);
18127 fixp->fx_file = fragp->fr_file;
18128 fixp->fx_line = fragp->fr_line;
18129
18130 buf = write_insn (buf, insn);
18131 }
18132 else
18133 {
18134 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18135
18136 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
18137 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18138 insn |= at << OP_SH_RT;
18139 exp.X_op = O_symbol;
18140 exp.X_add_symbol = fragp->fr_symbol;
18141 exp.X_add_number = fragp->fr_offset;
18142
18143 if (fragp->fr_offset)
18144 {
18145 exp.X_add_symbol = make_expr_symbol (&exp);
18146 exp.X_add_number = 0;
18147 }
18148
18149 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18150 FALSE, BFD_RELOC_MIPS_GOT16);
18151 fixp->fx_file = fragp->fr_file;
18152 fixp->fx_line = fragp->fr_line;
18153
18154 buf = write_insn (buf, insn);
18155
18156 if (mips_opts.isa == ISA_MIPS1)
18157 /* nop */
18158 buf = write_insn (buf, 0);
18159
18160 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
18161 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18162 insn |= at << OP_SH_RS | at << OP_SH_RT;
18163
18164 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18165 FALSE, BFD_RELOC_LO16);
18166 fixp->fx_file = fragp->fr_file;
18167 fixp->fx_line = fragp->fr_line;
18168
18169 buf = write_insn (buf, insn);
18170
18171 /* j(al)r $at. */
18172 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18173 insn = 0x0000f809;
18174 else
18175 insn = 0x00000008;
18176 insn |= at << OP_SH_RS;
18177
18178 buf = write_insn (buf, insn);
18179 }
18180 }
18181
18182 fragp->fr_fix += fragp->fr_var;
18183 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18184 return;
18185 }
18186
18187 /* Relax microMIPS branches. */
18188 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18189 {
18190 char *buf = fragp->fr_literal + fragp->fr_fix;
18191 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18192 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18193 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18194 bfd_boolean short_ds;
18195 unsigned long insn;
18196 expressionS exp;
18197 fixS *fixp;
18198
18199 exp.X_op = O_symbol;
18200 exp.X_add_symbol = fragp->fr_symbol;
18201 exp.X_add_number = fragp->fr_offset;
18202
18203 fragp->fr_fix += fragp->fr_var;
18204
18205 /* Handle 16-bit branches that fit or are forced to fit. */
18206 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18207 {
18208 /* We generate a fixup instead of applying it right now,
18209 because if there is linker relaxation, we're going to
18210 need the relocations. */
18211 if (type == 'D')
18212 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18213 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18214 else if (type == 'E')
18215 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18216 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18217 else
18218 abort ();
18219
18220 fixp->fx_file = fragp->fr_file;
18221 fixp->fx_line = fragp->fr_line;
18222
18223 /* These relocations can have an addend that won't fit in
18224 2 octets. */
18225 fixp->fx_no_overflow = 1;
18226
18227 return;
18228 }
18229
18230 /* Handle 32-bit branches that fit or are forced to fit. */
18231 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18232 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18233 {
18234 /* We generate a fixup instead of applying it right now,
18235 because if there is linker relaxation, we're going to
18236 need the relocations. */
18237 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18238 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18239 fixp->fx_file = fragp->fr_file;
18240 fixp->fx_line = fragp->fr_line;
18241
18242 if (type == 0)
18243 return;
18244 }
18245
18246 /* Relax 16-bit branches to 32-bit branches. */
18247 if (type != 0)
18248 {
18249 insn = read_compressed_insn (buf, 2);
18250
18251 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18252 insn = 0x94000000; /* beq */
18253 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18254 {
18255 unsigned long regno;
18256
18257 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18258 regno = micromips_to_32_reg_d_map [regno];
18259 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18260 insn |= regno << MICROMIPSOP_SH_RS;
18261 }
18262 else
18263 abort ();
18264
18265 /* Nothing else to do, just write it out. */
18266 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18267 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18268 {
18269 buf = write_compressed_insn (buf, insn, 4);
18270 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18271 return;
18272 }
18273 }
18274 else
18275 insn = read_compressed_insn (buf, 4);
18276
18277 /* Relax 32-bit branches to a sequence of instructions. */
18278 as_warn_where (fragp->fr_file, fragp->fr_line,
18279 _("Relaxed out-of-range branch into a jump"));
18280
18281 /* Set the short-delay-slot bit. */
18282 short_ds = al && (insn & 0x02000000) != 0;
18283
18284 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18285 {
18286 symbolS *l;
18287
18288 /* Reverse the branch. */
18289 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18290 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18291 insn ^= 0x20000000;
18292 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18293 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18294 || (insn & 0xffe00000) == 0x40800000 /* blez */
18295 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18296 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18297 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18298 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18299 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18300 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18301 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18302 insn ^= 0x00400000;
18303 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18304 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18305 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18306 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18307 insn ^= 0x00200000;
18308 else
18309 abort ();
18310
18311 if (al)
18312 {
18313 /* Clear the and-link and short-delay-slot bits. */
18314 gas_assert ((insn & 0xfda00000) == 0x40200000);
18315
18316 /* bltzal 0x40200000 bgezal 0x40600000 */
18317 /* bltzals 0x42200000 bgezals 0x42600000 */
18318 insn &= ~0x02200000;
18319 }
18320
18321 /* Make a label at the end for use with the branch. */
18322 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18323 micromips_label_inc ();
18324 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
18325 if (IS_ELF)
18326 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18327 #endif
18328
18329 /* Refer to it. */
18330 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18331 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18332 fixp->fx_file = fragp->fr_file;
18333 fixp->fx_line = fragp->fr_line;
18334
18335 /* Branch over the jump. */
18336 buf = write_compressed_insn (buf, insn, 4);
18337 if (!compact)
18338 /* nop */
18339 buf = write_compressed_insn (buf, 0x0c00, 2);
18340 }
18341
18342 if (mips_pic == NO_PIC)
18343 {
18344 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
18345
18346 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18347 insn = al ? jal : 0xd4000000;
18348
18349 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18350 BFD_RELOC_MICROMIPS_JMP);
18351 fixp->fx_file = fragp->fr_file;
18352 fixp->fx_line = fragp->fr_line;
18353
18354 buf = write_compressed_insn (buf, insn, 4);
18355 if (compact)
18356 /* nop */
18357 buf = write_compressed_insn (buf, 0x0c00, 2);
18358 }
18359 else
18360 {
18361 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18362 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18363 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
18364
18365 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18366 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18367 insn |= at << MICROMIPSOP_SH_RT;
18368
18369 if (exp.X_add_number)
18370 {
18371 exp.X_add_symbol = make_expr_symbol (&exp);
18372 exp.X_add_number = 0;
18373 }
18374
18375 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18376 BFD_RELOC_MICROMIPS_GOT16);
18377 fixp->fx_file = fragp->fr_file;
18378 fixp->fx_line = fragp->fr_line;
18379
18380 buf = write_compressed_insn (buf, insn, 4);
18381
18382 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18383 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18384 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18385
18386 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18387 BFD_RELOC_MICROMIPS_LO16);
18388 fixp->fx_file = fragp->fr_file;
18389 fixp->fx_line = fragp->fr_line;
18390
18391 buf = write_compressed_insn (buf, insn, 4);
18392
18393 /* jr/jrc/jalr/jalrs $at */
18394 insn = al ? jalr : jr;
18395 insn |= at << MICROMIPSOP_SH_MJ;
18396
18397 buf = write_compressed_insn (buf, insn, 2);
18398 }
18399
18400 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18401 return;
18402 }
18403
18404 if (RELAX_MIPS16_P (fragp->fr_subtype))
18405 {
18406 int type;
18407 const struct mips16_immed_operand *op;
18408 offsetT val;
18409 char *buf;
18410 unsigned int user_length, length;
18411 unsigned long insn;
18412 bfd_boolean ext;
18413
18414 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18415 op = mips16_immed_operands;
18416 while (op->type != type)
18417 ++op;
18418
18419 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18420 val = resolve_symbol_value (fragp->fr_symbol);
18421 if (op->pcrel)
18422 {
18423 addressT addr;
18424
18425 addr = fragp->fr_address + fragp->fr_fix;
18426
18427 /* The rules for the base address of a PC relative reloc are
18428 complicated; see mips16_extended_frag. */
18429 if (type == 'p' || type == 'q')
18430 {
18431 addr += 2;
18432 if (ext)
18433 addr += 2;
18434 /* Ignore the low bit in the target, since it will be
18435 set for a text label. */
18436 if ((val & 1) != 0)
18437 --val;
18438 }
18439 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18440 addr -= 4;
18441 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18442 addr -= 2;
18443
18444 addr &= ~ (addressT) ((1 << op->shift) - 1);
18445 val -= addr;
18446
18447 /* Make sure the section winds up with the alignment we have
18448 assumed. */
18449 if (op->shift > 0)
18450 record_alignment (asec, op->shift);
18451 }
18452
18453 if (ext
18454 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18455 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18456 as_warn_where (fragp->fr_file, fragp->fr_line,
18457 _("extended instruction in delay slot"));
18458
18459 buf = fragp->fr_literal + fragp->fr_fix;
18460
18461 insn = read_compressed_insn (buf, 2);
18462 if (ext)
18463 insn |= MIPS16_EXTEND;
18464
18465 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18466 user_length = 4;
18467 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18468 user_length = 2;
18469 else
18470 user_length = 0;
18471
18472 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18473 BFD_RELOC_UNUSED, val, user_length, &insn);
18474
18475 length = (ext ? 4 : 2);
18476 gas_assert (mips16_opcode_length (insn) == length);
18477 write_compressed_insn (buf, insn, length);
18478 fragp->fr_fix += length;
18479 }
18480 else
18481 {
18482 relax_substateT subtype = fragp->fr_subtype;
18483 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18484 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18485 int first, second;
18486 fixS *fixp;
18487
18488 first = RELAX_FIRST (subtype);
18489 second = RELAX_SECOND (subtype);
18490 fixp = (fixS *) fragp->fr_opcode;
18491
18492 /* If the delay slot chosen does not match the size of the instruction,
18493 then emit a warning. */
18494 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18495 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18496 {
18497 relax_substateT s;
18498 const char *msg;
18499
18500 s = subtype & (RELAX_DELAY_SLOT_16BIT
18501 | RELAX_DELAY_SLOT_SIZE_FIRST
18502 | RELAX_DELAY_SLOT_SIZE_SECOND);
18503 msg = macro_warning (s);
18504 if (msg != NULL)
18505 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18506 subtype &= ~s;
18507 }
18508
18509 /* Possibly emit a warning if we've chosen the longer option. */
18510 if (use_second == second_longer)
18511 {
18512 relax_substateT s;
18513 const char *msg;
18514
18515 s = (subtype
18516 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18517 msg = macro_warning (s);
18518 if (msg != NULL)
18519 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18520 subtype &= ~s;
18521 }
18522
18523 /* Go through all the fixups for the first sequence. Disable them
18524 (by marking them as done) if we're going to use the second
18525 sequence instead. */
18526 while (fixp
18527 && fixp->fx_frag == fragp
18528 && fixp->fx_where < fragp->fr_fix - second)
18529 {
18530 if (subtype & RELAX_USE_SECOND)
18531 fixp->fx_done = 1;
18532 fixp = fixp->fx_next;
18533 }
18534
18535 /* Go through the fixups for the second sequence. Disable them if
18536 we're going to use the first sequence, otherwise adjust their
18537 addresses to account for the relaxation. */
18538 while (fixp && fixp->fx_frag == fragp)
18539 {
18540 if (subtype & RELAX_USE_SECOND)
18541 fixp->fx_where -= first;
18542 else
18543 fixp->fx_done = 1;
18544 fixp = fixp->fx_next;
18545 }
18546
18547 /* Now modify the frag contents. */
18548 if (subtype & RELAX_USE_SECOND)
18549 {
18550 char *start;
18551
18552 start = fragp->fr_literal + fragp->fr_fix - first - second;
18553 memmove (start, start + first, second);
18554 fragp->fr_fix -= first;
18555 }
18556 else
18557 fragp->fr_fix -= second;
18558 }
18559 }
18560
18561 #ifdef OBJ_ELF
18562
18563 /* This function is called after the relocs have been generated.
18564 We've been storing mips16 text labels as odd. Here we convert them
18565 back to even for the convenience of the debugger. */
18566
18567 void
18568 mips_frob_file_after_relocs (void)
18569 {
18570 asymbol **syms;
18571 unsigned int count, i;
18572
18573 if (!IS_ELF)
18574 return;
18575
18576 syms = bfd_get_outsymbols (stdoutput);
18577 count = bfd_get_symcount (stdoutput);
18578 for (i = 0; i < count; i++, syms++)
18579 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18580 && ((*syms)->value & 1) != 0)
18581 {
18582 (*syms)->value &= ~1;
18583 /* If the symbol has an odd size, it was probably computed
18584 incorrectly, so adjust that as well. */
18585 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18586 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18587 }
18588 }
18589
18590 #endif
18591
18592 /* This function is called whenever a label is defined, including fake
18593 labels instantiated off the dot special symbol. It is used when
18594 handling branch delays; if a branch has a label, we assume we cannot
18595 move it. This also bumps the value of the symbol by 1 in compressed
18596 code. */
18597
18598 static void
18599 mips_record_label (symbolS *sym)
18600 {
18601 segment_info_type *si = seg_info (now_seg);
18602 struct insn_label_list *l;
18603
18604 if (free_insn_labels == NULL)
18605 l = (struct insn_label_list *) xmalloc (sizeof *l);
18606 else
18607 {
18608 l = free_insn_labels;
18609 free_insn_labels = l->next;
18610 }
18611
18612 l->label = sym;
18613 l->next = si->label_list;
18614 si->label_list = l;
18615 }
18616
18617 /* This function is called as tc_frob_label() whenever a label is defined
18618 and adds a DWARF-2 record we only want for true labels. */
18619
18620 void
18621 mips_define_label (symbolS *sym)
18622 {
18623 mips_record_label (sym);
18624 #ifdef OBJ_ELF
18625 dwarf2_emit_label (sym);
18626 #endif
18627 }
18628
18629 /* This function is called by tc_new_dot_label whenever a new dot symbol
18630 is defined. */
18631
18632 void
18633 mips_add_dot_label (symbolS *sym)
18634 {
18635 mips_record_label (sym);
18636 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18637 mips_compressed_mark_label (sym);
18638 }
18639 \f
18640 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
18641
18642 /* Some special processing for a MIPS ELF file. */
18643
18644 void
18645 mips_elf_final_processing (void)
18646 {
18647 /* Write out the register information. */
18648 if (mips_abi != N64_ABI)
18649 {
18650 Elf32_RegInfo s;
18651
18652 s.ri_gprmask = mips_gprmask;
18653 s.ri_cprmask[0] = mips_cprmask[0];
18654 s.ri_cprmask[1] = mips_cprmask[1];
18655 s.ri_cprmask[2] = mips_cprmask[2];
18656 s.ri_cprmask[3] = mips_cprmask[3];
18657 /* The gp_value field is set by the MIPS ELF backend. */
18658
18659 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18660 ((Elf32_External_RegInfo *)
18661 mips_regmask_frag));
18662 }
18663 else
18664 {
18665 Elf64_Internal_RegInfo s;
18666
18667 s.ri_gprmask = mips_gprmask;
18668 s.ri_pad = 0;
18669 s.ri_cprmask[0] = mips_cprmask[0];
18670 s.ri_cprmask[1] = mips_cprmask[1];
18671 s.ri_cprmask[2] = mips_cprmask[2];
18672 s.ri_cprmask[3] = mips_cprmask[3];
18673 /* The gp_value field is set by the MIPS ELF backend. */
18674
18675 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18676 ((Elf64_External_RegInfo *)
18677 mips_regmask_frag));
18678 }
18679
18680 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18681 sort of BFD interface for this. */
18682 if (mips_any_noreorder)
18683 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18684 if (mips_pic != NO_PIC)
18685 {
18686 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
18687 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18688 }
18689 if (mips_abicalls)
18690 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18691
18692 /* Set MIPS ELF flags for ASEs. */
18693 /* We may need to define a new flag for DSP ASE, and set this flag when
18694 file_ase_dsp is true. */
18695 /* Same for DSP R2. */
18696 /* We may need to define a new flag for MT ASE, and set this flag when
18697 file_ase_mt is true. */
18698 if (file_ase_mips16)
18699 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
18700 if (file_ase_micromips)
18701 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
18702 #if 0 /* XXX FIXME */
18703 if (file_ase_mips3d)
18704 elf_elfheader (stdoutput)->e_flags |= ???;
18705 #endif
18706 if (file_ase_mdmx)
18707 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
18708
18709 /* Set the MIPS ELF ABI flags. */
18710 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
18711 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
18712 else if (mips_abi == O64_ABI)
18713 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
18714 else if (mips_abi == EABI_ABI)
18715 {
18716 if (!file_mips_gp32)
18717 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18718 else
18719 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18720 }
18721 else if (mips_abi == N32_ABI)
18722 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18723
18724 /* Nothing to do for N64_ABI. */
18725
18726 if (mips_32bitmode)
18727 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
18728
18729 #if 0 /* XXX FIXME */
18730 /* 32 bit code with 64 bit FP registers. */
18731 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18732 elf_elfheader (stdoutput)->e_flags |= ???;
18733 #endif
18734 }
18735
18736 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
18737 \f
18738 typedef struct proc {
18739 symbolS *func_sym;
18740 symbolS *func_end_sym;
18741 unsigned long reg_mask;
18742 unsigned long reg_offset;
18743 unsigned long fpreg_mask;
18744 unsigned long fpreg_offset;
18745 unsigned long frame_offset;
18746 unsigned long frame_reg;
18747 unsigned long pc_reg;
18748 } procS;
18749
18750 static procS cur_proc;
18751 static procS *cur_proc_ptr;
18752 static int numprocs;
18753
18754 /* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
18755 as "2", and a normal nop as "0". */
18756
18757 #define NOP_OPCODE_MIPS 0
18758 #define NOP_OPCODE_MIPS16 1
18759 #define NOP_OPCODE_MICROMIPS 2
18760
18761 char
18762 mips_nop_opcode (void)
18763 {
18764 if (seg_info (now_seg)->tc_segment_info_data.micromips)
18765 return NOP_OPCODE_MICROMIPS;
18766 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18767 return NOP_OPCODE_MIPS16;
18768 else
18769 return NOP_OPCODE_MIPS;
18770 }
18771
18772 /* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
18773 32-bit microMIPS NOPs here (if applicable). */
18774
18775 void
18776 mips_handle_align (fragS *fragp)
18777 {
18778 char nop_opcode;
18779 char *p;
18780 int bytes, size, excess;
18781 valueT opcode;
18782
18783 if (fragp->fr_type != rs_align_code)
18784 return;
18785
18786 p = fragp->fr_literal + fragp->fr_fix;
18787 nop_opcode = *p;
18788 switch (nop_opcode)
18789 {
18790 case NOP_OPCODE_MICROMIPS:
18791 opcode = micromips_nop32_insn.insn_opcode;
18792 size = 4;
18793 break;
18794 case NOP_OPCODE_MIPS16:
18795 opcode = mips16_nop_insn.insn_opcode;
18796 size = 2;
18797 break;
18798 case NOP_OPCODE_MIPS:
18799 default:
18800 opcode = nop_insn.insn_opcode;
18801 size = 4;
18802 break;
18803 }
18804
18805 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18806 excess = bytes % size;
18807
18808 /* Handle the leading part if we're not inserting a whole number of
18809 instructions, and make it the end of the fixed part of the frag.
18810 Try to fit in a short microMIPS NOP if applicable and possible,
18811 and use zeroes otherwise. */
18812 gas_assert (excess < 4);
18813 fragp->fr_fix += excess;
18814 switch (excess)
18815 {
18816 case 3:
18817 *p++ = '\0';
18818 /* Fall through. */
18819 case 2:
18820 if (nop_opcode == NOP_OPCODE_MICROMIPS)
18821 {
18822 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
18823 break;
18824 }
18825 *p++ = '\0';
18826 /* Fall through. */
18827 case 1:
18828 *p++ = '\0';
18829 /* Fall through. */
18830 case 0:
18831 break;
18832 }
18833
18834 md_number_to_chars (p, opcode, size);
18835 fragp->fr_var = size;
18836 }
18837
18838 static void
18839 md_obj_begin (void)
18840 {
18841 }
18842
18843 static void
18844 md_obj_end (void)
18845 {
18846 /* Check for premature end, nesting errors, etc. */
18847 if (cur_proc_ptr)
18848 as_warn (_("missing .end at end of assembly"));
18849 }
18850
18851 static long
18852 get_number (void)
18853 {
18854 int negative = 0;
18855 long val = 0;
18856
18857 if (*input_line_pointer == '-')
18858 {
18859 ++input_line_pointer;
18860 negative = 1;
18861 }
18862 if (!ISDIGIT (*input_line_pointer))
18863 as_bad (_("expected simple number"));
18864 if (input_line_pointer[0] == '0')
18865 {
18866 if (input_line_pointer[1] == 'x')
18867 {
18868 input_line_pointer += 2;
18869 while (ISXDIGIT (*input_line_pointer))
18870 {
18871 val <<= 4;
18872 val |= hex_value (*input_line_pointer++);
18873 }
18874 return negative ? -val : val;
18875 }
18876 else
18877 {
18878 ++input_line_pointer;
18879 while (ISDIGIT (*input_line_pointer))
18880 {
18881 val <<= 3;
18882 val |= *input_line_pointer++ - '0';
18883 }
18884 return negative ? -val : val;
18885 }
18886 }
18887 if (!ISDIGIT (*input_line_pointer))
18888 {
18889 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18890 *input_line_pointer, *input_line_pointer);
18891 as_warn (_("invalid number"));
18892 return -1;
18893 }
18894 while (ISDIGIT (*input_line_pointer))
18895 {
18896 val *= 10;
18897 val += *input_line_pointer++ - '0';
18898 }
18899 return negative ? -val : val;
18900 }
18901
18902 /* The .file directive; just like the usual .file directive, but there
18903 is an initial number which is the ECOFF file index. In the non-ECOFF
18904 case .file implies DWARF-2. */
18905
18906 static void
18907 s_mips_file (int x ATTRIBUTE_UNUSED)
18908 {
18909 static int first_file_directive = 0;
18910
18911 if (ECOFF_DEBUGGING)
18912 {
18913 get_number ();
18914 s_app_file (0);
18915 }
18916 else
18917 {
18918 char *filename;
18919
18920 filename = dwarf2_directive_file (0);
18921
18922 /* Versions of GCC up to 3.1 start files with a ".file"
18923 directive even for stabs output. Make sure that this
18924 ".file" is handled. Note that you need a version of GCC
18925 after 3.1 in order to support DWARF-2 on MIPS. */
18926 if (filename != NULL && ! first_file_directive)
18927 {
18928 (void) new_logical_line (filename, -1);
18929 s_app_file_string (filename, 0);
18930 }
18931 first_file_directive = 1;
18932 }
18933 }
18934
18935 /* The .loc directive, implying DWARF-2. */
18936
18937 static void
18938 s_mips_loc (int x ATTRIBUTE_UNUSED)
18939 {
18940 if (!ECOFF_DEBUGGING)
18941 dwarf2_directive_loc (0);
18942 }
18943
18944 /* The .end directive. */
18945
18946 static void
18947 s_mips_end (int x ATTRIBUTE_UNUSED)
18948 {
18949 symbolS *p;
18950
18951 /* Following functions need their own .frame and .cprestore directives. */
18952 mips_frame_reg_valid = 0;
18953 mips_cprestore_valid = 0;
18954
18955 if (!is_end_of_line[(unsigned char) *input_line_pointer])
18956 {
18957 p = get_symbol ();
18958 demand_empty_rest_of_line ();
18959 }
18960 else
18961 p = NULL;
18962
18963 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
18964 as_warn (_(".end not in text section"));
18965
18966 if (!cur_proc_ptr)
18967 {
18968 as_warn (_(".end directive without a preceding .ent directive."));
18969 demand_empty_rest_of_line ();
18970 return;
18971 }
18972
18973 if (p != NULL)
18974 {
18975 gas_assert (S_GET_NAME (p));
18976 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
18977 as_warn (_(".end symbol does not match .ent symbol."));
18978
18979 if (debug_type == DEBUG_STABS)
18980 stabs_generate_asm_endfunc (S_GET_NAME (p),
18981 S_GET_NAME (p));
18982 }
18983 else
18984 as_warn (_(".end directive missing or unknown symbol"));
18985
18986 #ifdef OBJ_ELF
18987 /* Create an expression to calculate the size of the function. */
18988 if (p && cur_proc_ptr)
18989 {
18990 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
18991 expressionS *exp = xmalloc (sizeof (expressionS));
18992
18993 obj->size = exp;
18994 exp->X_op = O_subtract;
18995 exp->X_add_symbol = symbol_temp_new_now ();
18996 exp->X_op_symbol = p;
18997 exp->X_add_number = 0;
18998
18999 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19000 }
19001
19002 /* Generate a .pdr section. */
19003 if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
19004 {
19005 segT saved_seg = now_seg;
19006 subsegT saved_subseg = now_subseg;
19007 expressionS exp;
19008 char *fragp;
19009
19010 #ifdef md_flush_pending_output
19011 md_flush_pending_output ();
19012 #endif
19013
19014 gas_assert (pdr_seg);
19015 subseg_set (pdr_seg, 0);
19016
19017 /* Write the symbol. */
19018 exp.X_op = O_symbol;
19019 exp.X_add_symbol = p;
19020 exp.X_add_number = 0;
19021 emit_expr (&exp, 4);
19022
19023 fragp = frag_more (7 * 4);
19024
19025 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19026 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19027 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19028 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19029 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19030 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19031 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19032
19033 subseg_set (saved_seg, saved_subseg);
19034 }
19035 #endif /* OBJ_ELF */
19036
19037 cur_proc_ptr = NULL;
19038 }
19039
19040 /* The .aent and .ent directives. */
19041
19042 static void
19043 s_mips_ent (int aent)
19044 {
19045 symbolS *symbolP;
19046
19047 symbolP = get_symbol ();
19048 if (*input_line_pointer == ',')
19049 ++input_line_pointer;
19050 SKIP_WHITESPACE ();
19051 if (ISDIGIT (*input_line_pointer)
19052 || *input_line_pointer == '-')
19053 get_number ();
19054
19055 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19056 as_warn (_(".ent or .aent not in text section."));
19057
19058 if (!aent && cur_proc_ptr)
19059 as_warn (_("missing .end"));
19060
19061 if (!aent)
19062 {
19063 /* This function needs its own .frame and .cprestore directives. */
19064 mips_frame_reg_valid = 0;
19065 mips_cprestore_valid = 0;
19066
19067 cur_proc_ptr = &cur_proc;
19068 memset (cur_proc_ptr, '\0', sizeof (procS));
19069
19070 cur_proc_ptr->func_sym = symbolP;
19071
19072 ++numprocs;
19073
19074 if (debug_type == DEBUG_STABS)
19075 stabs_generate_asm_func (S_GET_NAME (symbolP),
19076 S_GET_NAME (symbolP));
19077 }
19078
19079 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19080
19081 demand_empty_rest_of_line ();
19082 }
19083
19084 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19085 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19086 s_mips_frame is used so that we can set the PDR information correctly.
19087 We can't use the ecoff routines because they make reference to the ecoff
19088 symbol table (in the mdebug section). */
19089
19090 static void
19091 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19092 {
19093 #ifdef OBJ_ELF
19094 if (IS_ELF && !ECOFF_DEBUGGING)
19095 {
19096 long val;
19097
19098 if (cur_proc_ptr == (procS *) NULL)
19099 {
19100 as_warn (_(".frame outside of .ent"));
19101 demand_empty_rest_of_line ();
19102 return;
19103 }
19104
19105 cur_proc_ptr->frame_reg = tc_get_register (1);
19106
19107 SKIP_WHITESPACE ();
19108 if (*input_line_pointer++ != ','
19109 || get_absolute_expression_and_terminator (&val) != ',')
19110 {
19111 as_warn (_("Bad .frame directive"));
19112 --input_line_pointer;
19113 demand_empty_rest_of_line ();
19114 return;
19115 }
19116
19117 cur_proc_ptr->frame_offset = val;
19118 cur_proc_ptr->pc_reg = tc_get_register (0);
19119
19120 demand_empty_rest_of_line ();
19121 }
19122 else
19123 #endif /* OBJ_ELF */
19124 s_ignore (ignore);
19125 }
19126
19127 /* The .fmask and .mask directives. If the mdebug section is present
19128 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19129 embedded targets, s_mips_mask is used so that we can set the PDR
19130 information correctly. We can't use the ecoff routines because they
19131 make reference to the ecoff symbol table (in the mdebug section). */
19132
19133 static void
19134 s_mips_mask (int reg_type)
19135 {
19136 #ifdef OBJ_ELF
19137 if (IS_ELF && !ECOFF_DEBUGGING)
19138 {
19139 long mask, off;
19140
19141 if (cur_proc_ptr == (procS *) NULL)
19142 {
19143 as_warn (_(".mask/.fmask outside of .ent"));
19144 demand_empty_rest_of_line ();
19145 return;
19146 }
19147
19148 if (get_absolute_expression_and_terminator (&mask) != ',')
19149 {
19150 as_warn (_("Bad .mask/.fmask directive"));
19151 --input_line_pointer;
19152 demand_empty_rest_of_line ();
19153 return;
19154 }
19155
19156 off = get_absolute_expression ();
19157
19158 if (reg_type == 'F')
19159 {
19160 cur_proc_ptr->fpreg_mask = mask;
19161 cur_proc_ptr->fpreg_offset = off;
19162 }
19163 else
19164 {
19165 cur_proc_ptr->reg_mask = mask;
19166 cur_proc_ptr->reg_offset = off;
19167 }
19168
19169 demand_empty_rest_of_line ();
19170 }
19171 else
19172 #endif /* OBJ_ELF */
19173 s_ignore (reg_type);
19174 }
19175
19176 /* A table describing all the processors gas knows about. Names are
19177 matched in the order listed.
19178
19179 To ease comparison, please keep this table in the same order as
19180 gcc's mips_cpu_info_table[]. */
19181 static const struct mips_cpu_info mips_cpu_info_table[] =
19182 {
19183 /* Entries for generic ISAs */
19184 { "mips1", MIPS_CPU_IS_ISA, ISA_MIPS1, CPU_R3000 },
19185 { "mips2", MIPS_CPU_IS_ISA, ISA_MIPS2, CPU_R6000 },
19186 { "mips3", MIPS_CPU_IS_ISA, ISA_MIPS3, CPU_R4000 },
19187 { "mips4", MIPS_CPU_IS_ISA, ISA_MIPS4, CPU_R8000 },
19188 { "mips5", MIPS_CPU_IS_ISA, ISA_MIPS5, CPU_MIPS5 },
19189 { "mips32", MIPS_CPU_IS_ISA, ISA_MIPS32, CPU_MIPS32 },
19190 { "mips32r2", MIPS_CPU_IS_ISA, ISA_MIPS32R2, CPU_MIPS32R2 },
19191 { "mips64", MIPS_CPU_IS_ISA, ISA_MIPS64, CPU_MIPS64 },
19192 { "mips64r2", MIPS_CPU_IS_ISA, ISA_MIPS64R2, CPU_MIPS64R2 },
19193
19194 /* MIPS I */
19195 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
19196 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
19197 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
19198
19199 /* MIPS II */
19200 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
19201
19202 /* MIPS III */
19203 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
19204 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
19205 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
19206 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
19207 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
19208 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
19209 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
19210 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
19211 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
19212 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
19213 { "orion", 0, ISA_MIPS3, CPU_R4600 },
19214 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
19215 { "r5900", 0, ISA_MIPS3, CPU_R5900 },
19216 /* ST Microelectronics Loongson 2E and 2F cores */
19217 { "loongson2e", 0, ISA_MIPS3, CPU_LOONGSON_2E },
19218 { "loongson2f", 0, ISA_MIPS3, CPU_LOONGSON_2F },
19219
19220 /* MIPS IV */
19221 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
19222 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
19223 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
19224 { "r14000", 0, ISA_MIPS4, CPU_R14000 },
19225 { "r16000", 0, ISA_MIPS4, CPU_R16000 },
19226 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
19227 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
19228 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
19229 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
19230 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
19231 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
19232 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
19233 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
19234 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
19235 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
19236
19237 /* MIPS 32 */
19238 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
19239 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
19240 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
19241 { "4ksc", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19242
19243 /* MIPS 32 Release 2 */
19244 { "4kec", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19245 { "4kem", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19246 { "4kep", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19247 { "4ksd", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19248 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19249 { "m4kp", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19250 { "m14k", MIPS_CPU_ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19251 { "m14kc", MIPS_CPU_ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19252 { "m14ke", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19253 ISA_MIPS32R2, CPU_MIPS32R2 },
19254 { "m14kec", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19255 ISA_MIPS32R2, CPU_MIPS32R2 },
19256 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19257 { "24kf2_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19258 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19259 { "24kf1_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19260 /* Deprecated forms of the above. */
19261 { "24kfx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19262 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19263 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
19264 { "24kec", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19265 { "24kef2_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19266 { "24kef", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19267 { "24kef1_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19268 /* Deprecated forms of the above. */
19269 { "24kefx", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19270 { "24kex", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19271 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
19272 { "34kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19273 ISA_MIPS32R2, CPU_MIPS32R2 },
19274 { "34kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19275 ISA_MIPS32R2, CPU_MIPS32R2 },
19276 { "34kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19277 ISA_MIPS32R2, CPU_MIPS32R2 },
19278 { "34kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19279 ISA_MIPS32R2, CPU_MIPS32R2 },
19280 /* Deprecated forms of the above. */
19281 { "34kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19282 ISA_MIPS32R2, CPU_MIPS32R2 },
19283 { "34kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19284 ISA_MIPS32R2, CPU_MIPS32R2 },
19285 /* 34Kn is a 34kc without DSP. */
19286 { "34kn", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19287 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19288 { "74kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19289 ISA_MIPS32R2, CPU_MIPS32R2 },
19290 { "74kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19291 ISA_MIPS32R2, CPU_MIPS32R2 },
19292 { "74kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19293 ISA_MIPS32R2, CPU_MIPS32R2 },
19294 { "74kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19295 ISA_MIPS32R2, CPU_MIPS32R2 },
19296 { "74kf3_2", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19297 ISA_MIPS32R2, CPU_MIPS32R2 },
19298 /* Deprecated forms of the above. */
19299 { "74kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19300 ISA_MIPS32R2, CPU_MIPS32R2 },
19301 { "74kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19302 ISA_MIPS32R2, CPU_MIPS32R2 },
19303 /* 1004K cores are multiprocessor versions of the 34K. */
19304 { "1004kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19305 ISA_MIPS32R2, CPU_MIPS32R2 },
19306 { "1004kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19307 ISA_MIPS32R2, CPU_MIPS32R2 },
19308 { "1004kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19309 ISA_MIPS32R2, CPU_MIPS32R2 },
19310 { "1004kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19311 ISA_MIPS32R2, CPU_MIPS32R2 },
19312
19313 /* MIPS 64 */
19314 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
19315 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
19316 { "20kc", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19317 { "25kf", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19318
19319 /* Broadcom SB-1 CPU core */
19320 { "sb1", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19321 ISA_MIPS64, CPU_SB1 },
19322 /* Broadcom SB-1A CPU core */
19323 { "sb1a", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19324 ISA_MIPS64, CPU_SB1 },
19325
19326 { "loongson3a", 0, ISA_MIPS64, CPU_LOONGSON_3A },
19327
19328 /* MIPS 64 Release 2 */
19329
19330 /* Cavium Networks Octeon CPU core */
19331 { "octeon", 0, ISA_MIPS64R2, CPU_OCTEON },
19332 { "octeon+", 0, ISA_MIPS64R2, CPU_OCTEONP },
19333 { "octeon2", 0, ISA_MIPS64R2, CPU_OCTEON2 },
19334
19335 /* RMI Xlr */
19336 { "xlr", 0, ISA_MIPS64, CPU_XLR },
19337
19338 /* Broadcom XLP.
19339 XLP is mostly like XLR, with the prominent exception that it is
19340 MIPS64R2 rather than MIPS64. */
19341 { "xlp", 0, ISA_MIPS64R2, CPU_XLR },
19342
19343 /* End marker */
19344 { NULL, 0, 0, 0 }
19345 };
19346
19347
19348 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19349 with a final "000" replaced by "k". Ignore case.
19350
19351 Note: this function is shared between GCC and GAS. */
19352
19353 static bfd_boolean
19354 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19355 {
19356 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19357 given++, canonical++;
19358
19359 return ((*given == 0 && *canonical == 0)
19360 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19361 }
19362
19363
19364 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19365 CPU name. We've traditionally allowed a lot of variation here.
19366
19367 Note: this function is shared between GCC and GAS. */
19368
19369 static bfd_boolean
19370 mips_matching_cpu_name_p (const char *canonical, const char *given)
19371 {
19372 /* First see if the name matches exactly, or with a final "000"
19373 turned into "k". */
19374 if (mips_strict_matching_cpu_name_p (canonical, given))
19375 return TRUE;
19376
19377 /* If not, try comparing based on numerical designation alone.
19378 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19379 if (TOLOWER (*given) == 'r')
19380 given++;
19381 if (!ISDIGIT (*given))
19382 return FALSE;
19383
19384 /* Skip over some well-known prefixes in the canonical name,
19385 hoping to find a number there too. */
19386 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19387 canonical += 2;
19388 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19389 canonical += 2;
19390 else if (TOLOWER (canonical[0]) == 'r')
19391 canonical += 1;
19392
19393 return mips_strict_matching_cpu_name_p (canonical, given);
19394 }
19395
19396
19397 /* Parse an option that takes the name of a processor as its argument.
19398 OPTION is the name of the option and CPU_STRING is the argument.
19399 Return the corresponding processor enumeration if the CPU_STRING is
19400 recognized, otherwise report an error and return null.
19401
19402 A similar function exists in GCC. */
19403
19404 static const struct mips_cpu_info *
19405 mips_parse_cpu (const char *option, const char *cpu_string)
19406 {
19407 const struct mips_cpu_info *p;
19408
19409 /* 'from-abi' selects the most compatible architecture for the given
19410 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19411 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19412 version. Look first at the -mgp options, if given, otherwise base
19413 the choice on MIPS_DEFAULT_64BIT.
19414
19415 Treat NO_ABI like the EABIs. One reason to do this is that the
19416 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19417 architecture. This code picks MIPS I for 'mips' and MIPS III for
19418 'mips64', just as we did in the days before 'from-abi'. */
19419 if (strcasecmp (cpu_string, "from-abi") == 0)
19420 {
19421 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19422 return mips_cpu_info_from_isa (ISA_MIPS1);
19423
19424 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19425 return mips_cpu_info_from_isa (ISA_MIPS3);
19426
19427 if (file_mips_gp32 >= 0)
19428 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19429
19430 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19431 ? ISA_MIPS3
19432 : ISA_MIPS1);
19433 }
19434
19435 /* 'default' has traditionally been a no-op. Probably not very useful. */
19436 if (strcasecmp (cpu_string, "default") == 0)
19437 return 0;
19438
19439 for (p = mips_cpu_info_table; p->name != 0; p++)
19440 if (mips_matching_cpu_name_p (p->name, cpu_string))
19441 return p;
19442
19443 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
19444 return 0;
19445 }
19446
19447 /* Return the canonical processor information for ISA (a member of the
19448 ISA_MIPS* enumeration). */
19449
19450 static const struct mips_cpu_info *
19451 mips_cpu_info_from_isa (int isa)
19452 {
19453 int i;
19454
19455 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19456 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19457 && isa == mips_cpu_info_table[i].isa)
19458 return (&mips_cpu_info_table[i]);
19459
19460 return NULL;
19461 }
19462
19463 static const struct mips_cpu_info *
19464 mips_cpu_info_from_arch (int arch)
19465 {
19466 int i;
19467
19468 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19469 if (arch == mips_cpu_info_table[i].cpu)
19470 return (&mips_cpu_info_table[i]);
19471
19472 return NULL;
19473 }
19474 \f
19475 static void
19476 show (FILE *stream, const char *string, int *col_p, int *first_p)
19477 {
19478 if (*first_p)
19479 {
19480 fprintf (stream, "%24s", "");
19481 *col_p = 24;
19482 }
19483 else
19484 {
19485 fprintf (stream, ", ");
19486 *col_p += 2;
19487 }
19488
19489 if (*col_p + strlen (string) > 72)
19490 {
19491 fprintf (stream, "\n%24s", "");
19492 *col_p = 24;
19493 }
19494
19495 fprintf (stream, "%s", string);
19496 *col_p += strlen (string);
19497
19498 *first_p = 0;
19499 }
19500
19501 void
19502 md_show_usage (FILE *stream)
19503 {
19504 int column, first;
19505 size_t i;
19506
19507 fprintf (stream, _("\
19508 MIPS options:\n\
19509 -EB generate big endian output\n\
19510 -EL generate little endian output\n\
19511 -g, -g2 do not remove unneeded NOPs or swap branches\n\
19512 -G NUM allow referencing objects up to NUM bytes\n\
19513 implicitly with the gp register [default 8]\n"));
19514 fprintf (stream, _("\
19515 -mips1 generate MIPS ISA I instructions\n\
19516 -mips2 generate MIPS ISA II instructions\n\
19517 -mips3 generate MIPS ISA III instructions\n\
19518 -mips4 generate MIPS ISA IV instructions\n\
19519 -mips5 generate MIPS ISA V instructions\n\
19520 -mips32 generate MIPS32 ISA instructions\n\
19521 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
19522 -mips64 generate MIPS64 ISA instructions\n\
19523 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
19524 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19525
19526 first = 1;
19527
19528 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19529 show (stream, mips_cpu_info_table[i].name, &column, &first);
19530 show (stream, "from-abi", &column, &first);
19531 fputc ('\n', stream);
19532
19533 fprintf (stream, _("\
19534 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19535 -no-mCPU don't generate code specific to CPU.\n\
19536 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19537
19538 first = 1;
19539
19540 show (stream, "3900", &column, &first);
19541 show (stream, "4010", &column, &first);
19542 show (stream, "4100", &column, &first);
19543 show (stream, "4650", &column, &first);
19544 fputc ('\n', stream);
19545
19546 fprintf (stream, _("\
19547 -mips16 generate mips16 instructions\n\
19548 -no-mips16 do not generate mips16 instructions\n"));
19549 fprintf (stream, _("\
19550 -mmicromips generate microMIPS instructions\n\
19551 -mno-micromips do not generate microMIPS instructions\n"));
19552 fprintf (stream, _("\
19553 -msmartmips generate smartmips instructions\n\
19554 -mno-smartmips do not generate smartmips instructions\n"));
19555 fprintf (stream, _("\
19556 -mdsp generate DSP instructions\n\
19557 -mno-dsp do not generate DSP instructions\n"));
19558 fprintf (stream, _("\
19559 -mdspr2 generate DSP R2 instructions\n\
19560 -mno-dspr2 do not generate DSP R2 instructions\n"));
19561 fprintf (stream, _("\
19562 -mmt generate MT instructions\n\
19563 -mno-mt do not generate MT instructions\n"));
19564 fprintf (stream, _("\
19565 -mmcu generate MCU instructions\n\
19566 -mno-mcu do not generate MCU instructions\n"));
19567 fprintf (stream, _("\
19568 -mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19569 -mfix-loongson2f-nop work around Loongson2F NOP errata\n\
19570 -mfix-vr4120 work around certain VR4120 errata\n\
19571 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
19572 -mfix-24k insert a nop after ERET and DERET instructions\n\
19573 -mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
19574 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19575 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
19576 -msym32 assume all symbols have 32-bit values\n\
19577 -O0 remove unneeded NOPs, do not swap branches\n\
19578 -O remove unneeded NOPs and swap branches\n\
19579 --trap, --no-break trap exception on div by 0 and mult overflow\n\
19580 --break, --no-trap break exception on div by 0 and mult overflow\n"));
19581 fprintf (stream, _("\
19582 -mhard-float allow floating-point instructions\n\
19583 -msoft-float do not allow floating-point instructions\n\
19584 -msingle-float only allow 32-bit floating-point operations\n\
19585 -mdouble-float allow 32-bit and 64-bit floating-point operations\n\
19586 --[no-]construct-floats [dis]allow floating point values to be constructed\n"
19587 ));
19588 #ifdef OBJ_ELF
19589 fprintf (stream, _("\
19590 -KPIC, -call_shared generate SVR4 position independent code\n\
19591 -call_nonpic generate non-PIC code that can operate with DSOs\n\
19592 -mvxworks-pic generate VxWorks position independent code\n\
19593 -non_shared do not generate code that can operate with DSOs\n\
19594 -xgot assume a 32 bit GOT\n\
19595 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
19596 -mshared, -mno-shared disable/enable .cpload optimization for\n\
19597 position dependent (non shared) code\n\
19598 -mabi=ABI create ABI conformant object file for:\n"));
19599
19600 first = 1;
19601
19602 show (stream, "32", &column, &first);
19603 show (stream, "o64", &column, &first);
19604 show (stream, "n32", &column, &first);
19605 show (stream, "64", &column, &first);
19606 show (stream, "eabi", &column, &first);
19607
19608 fputc ('\n', stream);
19609
19610 fprintf (stream, _("\
19611 -32 create o32 ABI object file (default)\n\
19612 -n32 create n32 ABI object file\n\
19613 -64 create 64 ABI object file\n"));
19614 #endif
19615 }
19616
19617 #ifdef TE_IRIX
19618 enum dwarf2_format
19619 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
19620 {
19621 if (HAVE_64BIT_SYMBOLS)
19622 return dwarf2_format_64bit_irix;
19623 else
19624 return dwarf2_format_32bit;
19625 }
19626 #endif
19627
19628 int
19629 mips_dwarf2_addr_size (void)
19630 {
19631 if (HAVE_64BIT_OBJECTS)
19632 return 8;
19633 else
19634 return 4;
19635 }
19636
19637 /* Standard calling conventions leave the CFA at SP on entry. */
19638 void
19639 mips_cfi_frame_initial_instructions (void)
19640 {
19641 cfi_add_CFA_def_cfa_register (SP);
19642 }
19643
19644 int
19645 tc_mips_regname_to_dw2regnum (char *regname)
19646 {
19647 unsigned int regnum = -1;
19648 unsigned int reg;
19649
19650 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19651 regnum = reg;
19652
19653 return regnum;
19654 }
This page took 0.446205 seconds and 4 git commands to generate.