]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/tc-mips.c
MIPS: Add Global INValidate ASE support
[thirdparty/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright (C) 1993-2018 Free Software Foundation, Inc.
3 Contributed by the OSF and Ralph Campbell.
4 Written by Keith Knowles and Ralph Campbell, working independently.
5 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6 Support.
7
8 This file is part of GAS.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
24
25 #include "as.h"
26 #include "config.h"
27 #include "subsegs.h"
28 #include "safe-ctype.h"
29
30 #include "opcode/mips.h"
31 #include "itbl-ops.h"
32 #include "dwarf2dbg.h"
33 #include "dw2gencfi.h"
34
35 /* Check assumptions made in this file. */
36 typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
37 typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
38
39 #ifdef DEBUG
40 #define DBG(x) printf x
41 #else
42 #define DBG(x)
43 #endif
44
45 #define streq(a, b) (strcmp (a, b) == 0)
46
47 #define SKIP_SPACE_TABS(S) \
48 do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49
50 /* Clean up namespace so we can include obj-elf.h too. */
51 static int mips_output_flavor (void);
52 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
53 #undef OBJ_PROCESS_STAB
54 #undef OUTPUT_FLAVOR
55 #undef S_GET_ALIGN
56 #undef S_GET_SIZE
57 #undef S_SET_ALIGN
58 #undef S_SET_SIZE
59 #undef obj_frob_file
60 #undef obj_frob_file_after_relocs
61 #undef obj_frob_symbol
62 #undef obj_pop_insert
63 #undef obj_sec_sym_ok_for_reloc
64 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
65
66 #include "obj-elf.h"
67 /* Fix any of them that we actually care about. */
68 #undef OUTPUT_FLAVOR
69 #define OUTPUT_FLAVOR mips_output_flavor()
70
71 #include "elf/mips.h"
72
73 #ifndef ECOFF_DEBUGGING
74 #define NO_ECOFF_DEBUGGING
75 #define ECOFF_DEBUGGING 0
76 #endif
77
78 int mips_flag_mdebug = -1;
79
80 /* Control generation of .pdr sections. Off by default on IRIX: the native
81 linker doesn't know about and discards them, but relocations against them
82 remain, leading to rld crashes. */
83 #ifdef TE_IRIX
84 int mips_flag_pdr = FALSE;
85 #else
86 int mips_flag_pdr = TRUE;
87 #endif
88
89 #include "ecoff.h"
90
91 static char *mips_regmask_frag;
92 static char *mips_flags_frag;
93
94 #define ZERO 0
95 #define ATREG 1
96 #define S0 16
97 #define S7 23
98 #define TREG 24
99 #define PIC_CALL_REG 25
100 #define KT0 26
101 #define KT1 27
102 #define GP 28
103 #define SP 29
104 #define FP 30
105 #define RA 31
106
107 #define ILLEGAL_REG (32)
108
109 #define AT mips_opts.at
110
111 extern int target_big_endian;
112
113 /* The name of the readonly data section. */
114 #define RDATA_SECTION_NAME ".rodata"
115
116 /* Ways in which an instruction can be "appended" to the output. */
117 enum append_method {
118 /* Just add it normally. */
119 APPEND_ADD,
120
121 /* Add it normally and then add a nop. */
122 APPEND_ADD_WITH_NOP,
123
124 /* Turn an instruction with a delay slot into a "compact" version. */
125 APPEND_ADD_COMPACT,
126
127 /* Insert the instruction before the last one. */
128 APPEND_SWAP
129 };
130
131 /* Information about an instruction, including its format, operands
132 and fixups. */
133 struct mips_cl_insn
134 {
135 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
136 const struct mips_opcode *insn_mo;
137
138 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
139 a copy of INSN_MO->match with the operands filled in. If we have
140 decided to use an extended MIPS16 instruction, this includes the
141 extension. */
142 unsigned long insn_opcode;
143
144 /* The frag that contains the instruction. */
145 struct frag *frag;
146
147 /* The offset into FRAG of the first instruction byte. */
148 long where;
149
150 /* The relocs associated with the instruction, if any. */
151 fixS *fixp[3];
152
153 /* True if this entry cannot be moved from its current position. */
154 unsigned int fixed_p : 1;
155
156 /* True if this instruction occurred in a .set noreorder block. */
157 unsigned int noreorder_p : 1;
158
159 /* True for mips16 instructions that jump to an absolute address. */
160 unsigned int mips16_absolute_jump_p : 1;
161
162 /* True if this instruction is complete. */
163 unsigned int complete_p : 1;
164
165 /* True if this instruction is cleared from history by unconditional
166 branch. */
167 unsigned int cleared_p : 1;
168 };
169
170 /* The ABI to use. */
171 enum mips_abi_level
172 {
173 NO_ABI = 0,
174 O32_ABI,
175 O64_ABI,
176 N32_ABI,
177 N64_ABI,
178 EABI_ABI
179 };
180
181 /* MIPS ABI we are using for this output file. */
182 static enum mips_abi_level mips_abi = NO_ABI;
183
184 /* Whether or not we have code that can call pic code. */
185 int mips_abicalls = FALSE;
186
187 /* Whether or not we have code which can be put into a shared
188 library. */
189 static bfd_boolean mips_in_shared = TRUE;
190
191 /* This is the set of options which may be modified by the .set
192 pseudo-op. We use a struct so that .set push and .set pop are more
193 reliable. */
194
195 struct mips_set_options
196 {
197 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
198 if it has not been initialized. Changed by `.set mipsN', and the
199 -mipsN command line option, and the default CPU. */
200 int isa;
201 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
202 <asename>', by command line options, and based on the default
203 architecture. */
204 int ase;
205 /* Whether we are assembling for the mips16 processor. 0 if we are
206 not, 1 if we are, and -1 if the value has not been initialized.
207 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
208 -nomips16 command line options, and the default CPU. */
209 int mips16;
210 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
211 1 if we are, and -1 if the value has not been initialized. Changed
212 by `.set micromips' and `.set nomicromips', and the -mmicromips
213 and -mno-micromips command line options, and the default CPU. */
214 int micromips;
215 /* Non-zero if we should not reorder instructions. Changed by `.set
216 reorder' and `.set noreorder'. */
217 int noreorder;
218 /* Non-zero if we should not permit the register designated "assembler
219 temporary" to be used in instructions. The value is the register
220 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
221 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
222 unsigned int at;
223 /* Non-zero if we should warn when a macro instruction expands into
224 more than one machine instruction. Changed by `.set nomacro' and
225 `.set macro'. */
226 int warn_about_macros;
227 /* Non-zero if we should not move instructions. Changed by `.set
228 move', `.set volatile', `.set nomove', and `.set novolatile'. */
229 int nomove;
230 /* Non-zero if we should not optimize branches by moving the target
231 of the branch into the delay slot. Actually, we don't perform
232 this optimization anyhow. Changed by `.set bopt' and `.set
233 nobopt'. */
234 int nobopt;
235 /* Non-zero if we should not autoextend mips16 instructions.
236 Changed by `.set autoextend' and `.set noautoextend'. */
237 int noautoextend;
238 /* True if we should only emit 32-bit microMIPS instructions.
239 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
240 and -mno-insn32 command line options. */
241 bfd_boolean insn32;
242 /* Restrict general purpose registers and floating point registers
243 to 32 bit. This is initially determined when -mgp32 or -mfp32
244 is passed but can changed if the assembler code uses .set mipsN. */
245 int gp;
246 int fp;
247 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
248 command line option, and the default CPU. */
249 int arch;
250 /* True if ".set sym32" is in effect. */
251 bfd_boolean sym32;
252 /* True if floating-point operations are not allowed. Changed by .set
253 softfloat or .set hardfloat, by command line options -msoft-float or
254 -mhard-float. The default is false. */
255 bfd_boolean soft_float;
256
257 /* True if only single-precision floating-point operations are allowed.
258 Changed by .set singlefloat or .set doublefloat, command-line options
259 -msingle-float or -mdouble-float. The default is false. */
260 bfd_boolean single_float;
261
262 /* 1 if single-precision operations on odd-numbered registers are
263 allowed. */
264 int oddspreg;
265 };
266
267 /* Specifies whether module level options have been checked yet. */
268 static bfd_boolean file_mips_opts_checked = FALSE;
269
270 /* Do we support nan2008? 0 if we don't, 1 if we do, and -1 if the
271 value has not been initialized. Changed by `.nan legacy' and
272 `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
273 options, and the default CPU. */
274 static int mips_nan2008 = -1;
275
276 /* This is the struct we use to hold the module level set of options.
277 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
278 fp fields to -1 to indicate that they have not been initialized. */
279
280 static struct mips_set_options file_mips_opts =
281 {
282 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
283 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
284 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
285 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
286 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
287 };
288
289 /* This is similar to file_mips_opts, but for the current set of options. */
290
291 static struct mips_set_options mips_opts =
292 {
293 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
294 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
295 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
296 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
297 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
298 };
299
300 /* Which bits of file_ase were explicitly set or cleared by ASE options. */
301 static unsigned int file_ase_explicit;
302
303 /* These variables are filled in with the masks of registers used.
304 The object format code reads them and puts them in the appropriate
305 place. */
306 unsigned long mips_gprmask;
307 unsigned long mips_cprmask[4];
308
309 /* True if any MIPS16 code was produced. */
310 static int file_ase_mips16;
311
312 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
313 || mips_opts.isa == ISA_MIPS32R2 \
314 || mips_opts.isa == ISA_MIPS32R3 \
315 || mips_opts.isa == ISA_MIPS32R5 \
316 || mips_opts.isa == ISA_MIPS64 \
317 || mips_opts.isa == ISA_MIPS64R2 \
318 || mips_opts.isa == ISA_MIPS64R3 \
319 || mips_opts.isa == ISA_MIPS64R5)
320
321 /* True if any microMIPS code was produced. */
322 static int file_ase_micromips;
323
324 /* True if we want to create R_MIPS_JALR for jalr $25. */
325 #ifdef TE_IRIX
326 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
327 #else
328 /* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
329 because there's no place for any addend, the only acceptable
330 expression is a bare symbol. */
331 #define MIPS_JALR_HINT_P(EXPR) \
332 (!HAVE_IN_PLACE_ADDENDS \
333 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
334 #endif
335
336 /* The argument of the -march= flag. The architecture we are assembling. */
337 static const char *mips_arch_string;
338
339 /* The argument of the -mtune= flag. The architecture for which we
340 are optimizing. */
341 static int mips_tune = CPU_UNKNOWN;
342 static const char *mips_tune_string;
343
344 /* True when generating 32-bit code for a 64-bit processor. */
345 static int mips_32bitmode = 0;
346
347 /* True if the given ABI requires 32-bit registers. */
348 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
349
350 /* Likewise 64-bit registers. */
351 #define ABI_NEEDS_64BIT_REGS(ABI) \
352 ((ABI) == N32_ABI \
353 || (ABI) == N64_ABI \
354 || (ABI) == O64_ABI)
355
356 #define ISA_IS_R6(ISA) \
357 ((ISA) == ISA_MIPS32R6 \
358 || (ISA) == ISA_MIPS64R6)
359
360 /* Return true if ISA supports 64 bit wide gp registers. */
361 #define ISA_HAS_64BIT_REGS(ISA) \
362 ((ISA) == ISA_MIPS3 \
363 || (ISA) == ISA_MIPS4 \
364 || (ISA) == ISA_MIPS5 \
365 || (ISA) == ISA_MIPS64 \
366 || (ISA) == ISA_MIPS64R2 \
367 || (ISA) == ISA_MIPS64R3 \
368 || (ISA) == ISA_MIPS64R5 \
369 || (ISA) == ISA_MIPS64R6)
370
371 /* Return true if ISA supports 64 bit wide float registers. */
372 #define ISA_HAS_64BIT_FPRS(ISA) \
373 ((ISA) == ISA_MIPS3 \
374 || (ISA) == ISA_MIPS4 \
375 || (ISA) == ISA_MIPS5 \
376 || (ISA) == ISA_MIPS32R2 \
377 || (ISA) == ISA_MIPS32R3 \
378 || (ISA) == ISA_MIPS32R5 \
379 || (ISA) == ISA_MIPS32R6 \
380 || (ISA) == ISA_MIPS64 \
381 || (ISA) == ISA_MIPS64R2 \
382 || (ISA) == ISA_MIPS64R3 \
383 || (ISA) == ISA_MIPS64R5 \
384 || (ISA) == ISA_MIPS64R6)
385
386 /* Return true if ISA supports 64-bit right rotate (dror et al.)
387 instructions. */
388 #define ISA_HAS_DROR(ISA) \
389 ((ISA) == ISA_MIPS64R2 \
390 || (ISA) == ISA_MIPS64R3 \
391 || (ISA) == ISA_MIPS64R5 \
392 || (ISA) == ISA_MIPS64R6 \
393 || (mips_opts.micromips \
394 && ISA_HAS_64BIT_REGS (ISA)) \
395 )
396
397 /* Return true if ISA supports 32-bit right rotate (ror et al.)
398 instructions. */
399 #define ISA_HAS_ROR(ISA) \
400 ((ISA) == ISA_MIPS32R2 \
401 || (ISA) == ISA_MIPS32R3 \
402 || (ISA) == ISA_MIPS32R5 \
403 || (ISA) == ISA_MIPS32R6 \
404 || (ISA) == ISA_MIPS64R2 \
405 || (ISA) == ISA_MIPS64R3 \
406 || (ISA) == ISA_MIPS64R5 \
407 || (ISA) == ISA_MIPS64R6 \
408 || (mips_opts.ase & ASE_SMARTMIPS) \
409 || mips_opts.micromips \
410 )
411
412 /* Return true if ISA supports single-precision floats in odd registers. */
413 #define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
414 (((ISA) == ISA_MIPS32 \
415 || (ISA) == ISA_MIPS32R2 \
416 || (ISA) == ISA_MIPS32R3 \
417 || (ISA) == ISA_MIPS32R5 \
418 || (ISA) == ISA_MIPS32R6 \
419 || (ISA) == ISA_MIPS64 \
420 || (ISA) == ISA_MIPS64R2 \
421 || (ISA) == ISA_MIPS64R3 \
422 || (ISA) == ISA_MIPS64R5 \
423 || (ISA) == ISA_MIPS64R6 \
424 || (CPU) == CPU_R5900) \
425 && (CPU) != CPU_LOONGSON_3A)
426
427 /* Return true if ISA supports move to/from high part of a 64-bit
428 floating-point register. */
429 #define ISA_HAS_MXHC1(ISA) \
430 ((ISA) == ISA_MIPS32R2 \
431 || (ISA) == ISA_MIPS32R3 \
432 || (ISA) == ISA_MIPS32R5 \
433 || (ISA) == ISA_MIPS32R6 \
434 || (ISA) == ISA_MIPS64R2 \
435 || (ISA) == ISA_MIPS64R3 \
436 || (ISA) == ISA_MIPS64R5 \
437 || (ISA) == ISA_MIPS64R6)
438
439 /* Return true if ISA supports legacy NAN. */
440 #define ISA_HAS_LEGACY_NAN(ISA) \
441 ((ISA) == ISA_MIPS1 \
442 || (ISA) == ISA_MIPS2 \
443 || (ISA) == ISA_MIPS3 \
444 || (ISA) == ISA_MIPS4 \
445 || (ISA) == ISA_MIPS5 \
446 || (ISA) == ISA_MIPS32 \
447 || (ISA) == ISA_MIPS32R2 \
448 || (ISA) == ISA_MIPS32R3 \
449 || (ISA) == ISA_MIPS32R5 \
450 || (ISA) == ISA_MIPS64 \
451 || (ISA) == ISA_MIPS64R2 \
452 || (ISA) == ISA_MIPS64R3 \
453 || (ISA) == ISA_MIPS64R5)
454
455 #define GPR_SIZE \
456 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
457 ? 32 \
458 : mips_opts.gp)
459
460 #define FPR_SIZE \
461 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
462 ? 32 \
463 : mips_opts.fp)
464
465 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
466
467 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
468
469 /* True if relocations are stored in-place. */
470 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
471
472 /* The ABI-derived address size. */
473 #define HAVE_64BIT_ADDRESSES \
474 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
475 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
476
477 /* The size of symbolic constants (i.e., expressions of the form
478 "SYMBOL" or "SYMBOL + OFFSET"). */
479 #define HAVE_32BIT_SYMBOLS \
480 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
481 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
482
483 /* Addresses are loaded in different ways, depending on the address size
484 in use. The n32 ABI Documentation also mandates the use of additions
485 with overflow checking, but existing implementations don't follow it. */
486 #define ADDRESS_ADD_INSN \
487 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
488
489 #define ADDRESS_ADDI_INSN \
490 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
491
492 #define ADDRESS_LOAD_INSN \
493 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
494
495 #define ADDRESS_STORE_INSN \
496 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
497
498 /* Return true if the given CPU supports the MIPS16 ASE. */
499 #define CPU_HAS_MIPS16(cpu) \
500 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
501 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
502
503 /* Return true if the given CPU supports the microMIPS ASE. */
504 #define CPU_HAS_MICROMIPS(cpu) 0
505
506 /* True if CPU has a dror instruction. */
507 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
508
509 /* True if CPU has a ror instruction. */
510 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
511
512 /* True if CPU is in the Octeon family */
513 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
514 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
515
516 /* True if CPU has seq/sne and seqi/snei instructions. */
517 #define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
518
519 /* True, if CPU has support for ldc1 and sdc1. */
520 #define CPU_HAS_LDC1_SDC1(CPU) \
521 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
522
523 /* True if mflo and mfhi can be immediately followed by instructions
524 which write to the HI and LO registers.
525
526 According to MIPS specifications, MIPS ISAs I, II, and III need
527 (at least) two instructions between the reads of HI/LO and
528 instructions which write them, and later ISAs do not. Contradicting
529 the MIPS specifications, some MIPS IV processor user manuals (e.g.
530 the UM for the NEC Vr5000) document needing the instructions between
531 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
532 MIPS64 and later ISAs to have the interlocks, plus any specific
533 earlier-ISA CPUs for which CPU documentation declares that the
534 instructions are really interlocked. */
535 #define hilo_interlocks \
536 (mips_opts.isa == ISA_MIPS32 \
537 || mips_opts.isa == ISA_MIPS32R2 \
538 || mips_opts.isa == ISA_MIPS32R3 \
539 || mips_opts.isa == ISA_MIPS32R5 \
540 || mips_opts.isa == ISA_MIPS32R6 \
541 || mips_opts.isa == ISA_MIPS64 \
542 || mips_opts.isa == ISA_MIPS64R2 \
543 || mips_opts.isa == ISA_MIPS64R3 \
544 || mips_opts.isa == ISA_MIPS64R5 \
545 || mips_opts.isa == ISA_MIPS64R6 \
546 || mips_opts.arch == CPU_R4010 \
547 || mips_opts.arch == CPU_R5900 \
548 || mips_opts.arch == CPU_R10000 \
549 || mips_opts.arch == CPU_R12000 \
550 || mips_opts.arch == CPU_R14000 \
551 || mips_opts.arch == CPU_R16000 \
552 || mips_opts.arch == CPU_RM7000 \
553 || mips_opts.arch == CPU_VR5500 \
554 || mips_opts.micromips \
555 )
556
557 /* Whether the processor uses hardware interlocks to protect reads
558 from the GPRs after they are loaded from memory, and thus does not
559 require nops to be inserted. This applies to instructions marked
560 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
561 level I and microMIPS mode instructions are always interlocked. */
562 #define gpr_interlocks \
563 (mips_opts.isa != ISA_MIPS1 \
564 || mips_opts.arch == CPU_R3900 \
565 || mips_opts.arch == CPU_R5900 \
566 || mips_opts.micromips \
567 )
568
569 /* Whether the processor uses hardware interlocks to avoid delays
570 required by coprocessor instructions, and thus does not require
571 nops to be inserted. This applies to instructions marked
572 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
573 instructions marked INSN_WRITE_COND_CODE and ones marked
574 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
575 levels I, II, and III and microMIPS mode instructions are always
576 interlocked. */
577 /* Itbl support may require additional care here. */
578 #define cop_interlocks \
579 ((mips_opts.isa != ISA_MIPS1 \
580 && mips_opts.isa != ISA_MIPS2 \
581 && mips_opts.isa != ISA_MIPS3) \
582 || mips_opts.arch == CPU_R4300 \
583 || mips_opts.micromips \
584 )
585
586 /* Whether the processor uses hardware interlocks to protect reads
587 from coprocessor registers after they are loaded from memory, and
588 thus does not require nops to be inserted. This applies to
589 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
590 requires at MIPS ISA level I and microMIPS mode instructions are
591 always interlocked. */
592 #define cop_mem_interlocks \
593 (mips_opts.isa != ISA_MIPS1 \
594 || mips_opts.micromips \
595 )
596
597 /* Is this a mfhi or mflo instruction? */
598 #define MF_HILO_INSN(PINFO) \
599 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
600
601 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
602 has been selected. This implies, in particular, that addresses of text
603 labels have their LSB set. */
604 #define HAVE_CODE_COMPRESSION \
605 ((mips_opts.mips16 | mips_opts.micromips) != 0)
606
607 /* The minimum and maximum signed values that can be stored in a GPR. */
608 #define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
609 #define GPR_SMIN (-GPR_SMAX - 1)
610
611 /* MIPS PIC level. */
612
613 enum mips_pic_level mips_pic;
614
615 /* 1 if we should generate 32 bit offsets from the $gp register in
616 SVR4_PIC mode. Currently has no meaning in other modes. */
617 static int mips_big_got = 0;
618
619 /* 1 if trap instructions should used for overflow rather than break
620 instructions. */
621 static int mips_trap = 0;
622
623 /* 1 if double width floating point constants should not be constructed
624 by assembling two single width halves into two single width floating
625 point registers which just happen to alias the double width destination
626 register. On some architectures this aliasing can be disabled by a bit
627 in the status register, and the setting of this bit cannot be determined
628 automatically at assemble time. */
629 static int mips_disable_float_construction;
630
631 /* Non-zero if any .set noreorder directives were used. */
632
633 static int mips_any_noreorder;
634
635 /* Non-zero if nops should be inserted when the register referenced in
636 an mfhi/mflo instruction is read in the next two instructions. */
637 static int mips_7000_hilo_fix;
638
639 /* The size of objects in the small data section. */
640 static unsigned int g_switch_value = 8;
641 /* Whether the -G option was used. */
642 static int g_switch_seen = 0;
643
644 #define N_RMASK 0xc4
645 #define N_VFP 0xd4
646
647 /* If we can determine in advance that GP optimization won't be
648 possible, we can skip the relaxation stuff that tries to produce
649 GP-relative references. This makes delay slot optimization work
650 better.
651
652 This function can only provide a guess, but it seems to work for
653 gcc output. It needs to guess right for gcc, otherwise gcc
654 will put what it thinks is a GP-relative instruction in a branch
655 delay slot.
656
657 I don't know if a fix is needed for the SVR4_PIC mode. I've only
658 fixed it for the non-PIC mode. KR 95/04/07 */
659 static int nopic_need_relax (symbolS *, int);
660
661 /* handle of the OPCODE hash table */
662 static struct hash_control *op_hash = NULL;
663
664 /* The opcode hash table we use for the mips16. */
665 static struct hash_control *mips16_op_hash = NULL;
666
667 /* The opcode hash table we use for the microMIPS ASE. */
668 static struct hash_control *micromips_op_hash = NULL;
669
670 /* This array holds the chars that always start a comment. If the
671 pre-processor is disabled, these aren't very useful */
672 const char comment_chars[] = "#";
673
674 /* This array holds the chars that only start a comment at the beginning of
675 a line. If the line seems to have the form '# 123 filename'
676 .line and .file directives will appear in the pre-processed output */
677 /* Note that input_file.c hand checks for '#' at the beginning of the
678 first line of the input file. This is because the compiler outputs
679 #NO_APP at the beginning of its output. */
680 /* Also note that C style comments are always supported. */
681 const char line_comment_chars[] = "#";
682
683 /* This array holds machine specific line separator characters. */
684 const char line_separator_chars[] = ";";
685
686 /* Chars that can be used to separate mant from exp in floating point nums */
687 const char EXP_CHARS[] = "eE";
688
689 /* Chars that mean this number is a floating point constant */
690 /* As in 0f12.456 */
691 /* or 0d1.2345e12 */
692 const char FLT_CHARS[] = "rRsSfFdDxXpP";
693
694 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
695 changed in read.c . Ideally it shouldn't have to know about it at all,
696 but nothing is ideal around here.
697 */
698
699 /* Types of printf format used for instruction-related error messages.
700 "I" means int ("%d") and "S" means string ("%s"). */
701 enum mips_insn_error_format {
702 ERR_FMT_PLAIN,
703 ERR_FMT_I,
704 ERR_FMT_SS,
705 };
706
707 /* Information about an error that was found while assembling the current
708 instruction. */
709 struct mips_insn_error {
710 /* We sometimes need to match an instruction against more than one
711 opcode table entry. Errors found during this matching are reported
712 against a particular syntactic argument rather than against the
713 instruction as a whole. We grade these messages so that errors
714 against argument N have a greater priority than an error against
715 any argument < N, since the former implies that arguments up to N
716 were acceptable and that the opcode entry was therefore a closer match.
717 If several matches report an error against the same argument,
718 we only use that error if it is the same in all cases.
719
720 min_argnum is the minimum argument number for which an error message
721 should be accepted. It is 0 if MSG is against the instruction as
722 a whole. */
723 int min_argnum;
724
725 /* The printf()-style message, including its format and arguments. */
726 enum mips_insn_error_format format;
727 const char *msg;
728 union {
729 int i;
730 const char *ss[2];
731 } u;
732 };
733
734 /* The error that should be reported for the current instruction. */
735 static struct mips_insn_error insn_error;
736
737 static int auto_align = 1;
738
739 /* When outputting SVR4 PIC code, the assembler needs to know the
740 offset in the stack frame from which to restore the $gp register.
741 This is set by the .cprestore pseudo-op, and saved in this
742 variable. */
743 static offsetT mips_cprestore_offset = -1;
744
745 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
746 more optimizations, it can use a register value instead of a memory-saved
747 offset and even an other register than $gp as global pointer. */
748 static offsetT mips_cpreturn_offset = -1;
749 static int mips_cpreturn_register = -1;
750 static int mips_gp_register = GP;
751 static int mips_gprel_offset = 0;
752
753 /* Whether mips_cprestore_offset has been set in the current function
754 (or whether it has already been warned about, if not). */
755 static int mips_cprestore_valid = 0;
756
757 /* This is the register which holds the stack frame, as set by the
758 .frame pseudo-op. This is needed to implement .cprestore. */
759 static int mips_frame_reg = SP;
760
761 /* Whether mips_frame_reg has been set in the current function
762 (or whether it has already been warned about, if not). */
763 static int mips_frame_reg_valid = 0;
764
765 /* To output NOP instructions correctly, we need to keep information
766 about the previous two instructions. */
767
768 /* Whether we are optimizing. The default value of 2 means to remove
769 unneeded NOPs and swap branch instructions when possible. A value
770 of 1 means to not swap branches. A value of 0 means to always
771 insert NOPs. */
772 static int mips_optimize = 2;
773
774 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
775 equivalent to seeing no -g option at all. */
776 static int mips_debug = 0;
777
778 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
779 #define MAX_VR4130_NOPS 4
780
781 /* The maximum number of NOPs needed to fill delay slots. */
782 #define MAX_DELAY_NOPS 2
783
784 /* The maximum number of NOPs needed for any purpose. */
785 #define MAX_NOPS 4
786
787 /* A list of previous instructions, with index 0 being the most recent.
788 We need to look back MAX_NOPS instructions when filling delay slots
789 or working around processor errata. We need to look back one
790 instruction further if we're thinking about using history[0] to
791 fill a branch delay slot. */
792 static struct mips_cl_insn history[1 + MAX_NOPS];
793
794 /* Arrays of operands for each instruction. */
795 #define MAX_OPERANDS 6
796 struct mips_operand_array {
797 const struct mips_operand *operand[MAX_OPERANDS];
798 };
799 static struct mips_operand_array *mips_operands;
800 static struct mips_operand_array *mips16_operands;
801 static struct mips_operand_array *micromips_operands;
802
803 /* Nop instructions used by emit_nop. */
804 static struct mips_cl_insn nop_insn;
805 static struct mips_cl_insn mips16_nop_insn;
806 static struct mips_cl_insn micromips_nop16_insn;
807 static struct mips_cl_insn micromips_nop32_insn;
808
809 /* The appropriate nop for the current mode. */
810 #define NOP_INSN (mips_opts.mips16 \
811 ? &mips16_nop_insn \
812 : (mips_opts.micromips \
813 ? (mips_opts.insn32 \
814 ? &micromips_nop32_insn \
815 : &micromips_nop16_insn) \
816 : &nop_insn))
817
818 /* The size of NOP_INSN in bytes. */
819 #define NOP_INSN_SIZE ((mips_opts.mips16 \
820 || (mips_opts.micromips && !mips_opts.insn32)) \
821 ? 2 : 4)
822
823 /* If this is set, it points to a frag holding nop instructions which
824 were inserted before the start of a noreorder section. If those
825 nops turn out to be unnecessary, the size of the frag can be
826 decreased. */
827 static fragS *prev_nop_frag;
828
829 /* The number of nop instructions we created in prev_nop_frag. */
830 static int prev_nop_frag_holds;
831
832 /* The number of nop instructions that we know we need in
833 prev_nop_frag. */
834 static int prev_nop_frag_required;
835
836 /* The number of instructions we've seen since prev_nop_frag. */
837 static int prev_nop_frag_since;
838
839 /* Relocations against symbols are sometimes done in two parts, with a HI
840 relocation and a LO relocation. Each relocation has only 16 bits of
841 space to store an addend. This means that in order for the linker to
842 handle carries correctly, it must be able to locate both the HI and
843 the LO relocation. This means that the relocations must appear in
844 order in the relocation table.
845
846 In order to implement this, we keep track of each unmatched HI
847 relocation. We then sort them so that they immediately precede the
848 corresponding LO relocation. */
849
850 struct mips_hi_fixup
851 {
852 /* Next HI fixup. */
853 struct mips_hi_fixup *next;
854 /* This fixup. */
855 fixS *fixp;
856 /* The section this fixup is in. */
857 segT seg;
858 };
859
860 /* The list of unmatched HI relocs. */
861
862 static struct mips_hi_fixup *mips_hi_fixup_list;
863
864 /* The frag containing the last explicit relocation operator.
865 Null if explicit relocations have not been used. */
866
867 static fragS *prev_reloc_op_frag;
868
869 /* Map mips16 register numbers to normal MIPS register numbers. */
870
871 static const unsigned int mips16_to_32_reg_map[] =
872 {
873 16, 17, 2, 3, 4, 5, 6, 7
874 };
875
876 /* Map microMIPS register numbers to normal MIPS register numbers. */
877
878 #define micromips_to_32_reg_d_map mips16_to_32_reg_map
879
880 /* The microMIPS registers with type h. */
881 static const unsigned int micromips_to_32_reg_h_map1[] =
882 {
883 5, 5, 6, 4, 4, 4, 4, 4
884 };
885 static const unsigned int micromips_to_32_reg_h_map2[] =
886 {
887 6, 7, 7, 21, 22, 5, 6, 7
888 };
889
890 /* The microMIPS registers with type m. */
891 static const unsigned int micromips_to_32_reg_m_map[] =
892 {
893 0, 17, 2, 3, 16, 18, 19, 20
894 };
895
896 #define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
897
898 /* Classifies the kind of instructions we're interested in when
899 implementing -mfix-vr4120. */
900 enum fix_vr4120_class
901 {
902 FIX_VR4120_MACC,
903 FIX_VR4120_DMACC,
904 FIX_VR4120_MULT,
905 FIX_VR4120_DMULT,
906 FIX_VR4120_DIV,
907 FIX_VR4120_MTHILO,
908 NUM_FIX_VR4120_CLASSES
909 };
910
911 /* ...likewise -mfix-loongson2f-jump. */
912 static bfd_boolean mips_fix_loongson2f_jump;
913
914 /* ...likewise -mfix-loongson2f-nop. */
915 static bfd_boolean mips_fix_loongson2f_nop;
916
917 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
918 static bfd_boolean mips_fix_loongson2f;
919
920 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
921 there must be at least one other instruction between an instruction
922 of type X and an instruction of type Y. */
923 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
924
925 /* True if -mfix-vr4120 is in force. */
926 static int mips_fix_vr4120;
927
928 /* ...likewise -mfix-vr4130. */
929 static int mips_fix_vr4130;
930
931 /* ...likewise -mfix-24k. */
932 static int mips_fix_24k;
933
934 /* ...likewise -mfix-rm7000 */
935 static int mips_fix_rm7000;
936
937 /* ...likewise -mfix-cn63xxp1 */
938 static bfd_boolean mips_fix_cn63xxp1;
939
940 /* We don't relax branches by default, since this causes us to expand
941 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
942 fail to compute the offset before expanding the macro to the most
943 efficient expansion. */
944
945 static int mips_relax_branch;
946
947 /* TRUE if checks are suppressed for invalid branches between ISA modes.
948 Needed for broken assembly produced by some GCC versions and some
949 sloppy code out there, where branches to data labels are present. */
950 static bfd_boolean mips_ignore_branch_isa;
951 \f
952 /* The expansion of many macros depends on the type of symbol that
953 they refer to. For example, when generating position-dependent code,
954 a macro that refers to a symbol may have two different expansions,
955 one which uses GP-relative addresses and one which uses absolute
956 addresses. When generating SVR4-style PIC, a macro may have
957 different expansions for local and global symbols.
958
959 We handle these situations by generating both sequences and putting
960 them in variant frags. In position-dependent code, the first sequence
961 will be the GP-relative one and the second sequence will be the
962 absolute one. In SVR4 PIC, the first sequence will be for global
963 symbols and the second will be for local symbols.
964
965 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
966 SECOND are the lengths of the two sequences in bytes. These fields
967 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
968 the subtype has the following flags:
969
970 RELAX_PIC
971 Set if generating PIC code.
972
973 RELAX_USE_SECOND
974 Set if it has been decided that we should use the second
975 sequence instead of the first.
976
977 RELAX_SECOND_LONGER
978 Set in the first variant frag if the macro's second implementation
979 is longer than its first. This refers to the macro as a whole,
980 not an individual relaxation.
981
982 RELAX_NOMACRO
983 Set in the first variant frag if the macro appeared in a .set nomacro
984 block and if one alternative requires a warning but the other does not.
985
986 RELAX_DELAY_SLOT
987 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
988 delay slot.
989
990 RELAX_DELAY_SLOT_16BIT
991 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
992 16-bit instruction.
993
994 RELAX_DELAY_SLOT_SIZE_FIRST
995 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
996 the macro is of the wrong size for the branch delay slot.
997
998 RELAX_DELAY_SLOT_SIZE_SECOND
999 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1000 the macro is of the wrong size for the branch delay slot.
1001
1002 The frag's "opcode" points to the first fixup for relaxable code.
1003
1004 Relaxable macros are generated using a sequence such as:
1005
1006 relax_start (SYMBOL);
1007 ... generate first expansion ...
1008 relax_switch ();
1009 ... generate second expansion ...
1010 relax_end ();
1011
1012 The code and fixups for the unwanted alternative are discarded
1013 by md_convert_frag. */
1014 #define RELAX_ENCODE(FIRST, SECOND, PIC) \
1015 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
1016
1017 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1018 #define RELAX_SECOND(X) ((X) & 0xff)
1019 #define RELAX_PIC(X) (((X) & 0x10000) != 0)
1020 #define RELAX_USE_SECOND 0x20000
1021 #define RELAX_SECOND_LONGER 0x40000
1022 #define RELAX_NOMACRO 0x80000
1023 #define RELAX_DELAY_SLOT 0x100000
1024 #define RELAX_DELAY_SLOT_16BIT 0x200000
1025 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1026 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
1027
1028 /* Branch without likely bit. If label is out of range, we turn:
1029
1030 beq reg1, reg2, label
1031 delay slot
1032
1033 into
1034
1035 bne reg1, reg2, 0f
1036 nop
1037 j label
1038 0: delay slot
1039
1040 with the following opcode replacements:
1041
1042 beq <-> bne
1043 blez <-> bgtz
1044 bltz <-> bgez
1045 bc1f <-> bc1t
1046
1047 bltzal <-> bgezal (with jal label instead of j label)
1048
1049 Even though keeping the delay slot instruction in the delay slot of
1050 the branch would be more efficient, it would be very tricky to do
1051 correctly, because we'd have to introduce a variable frag *after*
1052 the delay slot instruction, and expand that instead. Let's do it
1053 the easy way for now, even if the branch-not-taken case now costs
1054 one additional instruction. Out-of-range branches are not supposed
1055 to be common, anyway.
1056
1057 Branch likely. If label is out of range, we turn:
1058
1059 beql reg1, reg2, label
1060 delay slot (annulled if branch not taken)
1061
1062 into
1063
1064 beql reg1, reg2, 1f
1065 nop
1066 beql $0, $0, 2f
1067 nop
1068 1: j[al] label
1069 delay slot (executed only if branch taken)
1070 2:
1071
1072 It would be possible to generate a shorter sequence by losing the
1073 likely bit, generating something like:
1074
1075 bne reg1, reg2, 0f
1076 nop
1077 j[al] label
1078 delay slot (executed only if branch taken)
1079 0:
1080
1081 beql -> bne
1082 bnel -> beq
1083 blezl -> bgtz
1084 bgtzl -> blez
1085 bltzl -> bgez
1086 bgezl -> bltz
1087 bc1fl -> bc1t
1088 bc1tl -> bc1f
1089
1090 bltzall -> bgezal (with jal label instead of j label)
1091 bgezall -> bltzal (ditto)
1092
1093
1094 but it's not clear that it would actually improve performance. */
1095 #define RELAX_BRANCH_ENCODE(at, pic, \
1096 uncond, likely, link, toofar) \
1097 ((relax_substateT) \
1098 (0xc0000000 \
1099 | ((at) & 0x1f) \
1100 | ((pic) ? 0x20 : 0) \
1101 | ((toofar) ? 0x40 : 0) \
1102 | ((link) ? 0x80 : 0) \
1103 | ((likely) ? 0x100 : 0) \
1104 | ((uncond) ? 0x200 : 0)))
1105 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1106 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1107 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1108 #define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1109 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1110 #define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
1111 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1112
1113 /* For mips16 code, we use an entirely different form of relaxation.
1114 mips16 supports two versions of most instructions which take
1115 immediate values: a small one which takes some small value, and a
1116 larger one which takes a 16 bit value. Since branches also follow
1117 this pattern, relaxing these values is required.
1118
1119 We can assemble both mips16 and normal MIPS code in a single
1120 object. Therefore, we need to support this type of relaxation at
1121 the same time that we support the relaxation described above. We
1122 use the high bit of the subtype field to distinguish these cases.
1123
1124 The information we store for this type of relaxation is the
1125 argument code found in the opcode file for this relocation, whether
1126 the user explicitly requested a small or extended form, and whether
1127 the relocation is in a jump or jal delay slot. That tells us the
1128 size of the value, and how it should be stored. We also store
1129 whether the fragment is considered to be extended or not. We also
1130 store whether this is known to be a branch to a different section,
1131 whether we have tried to relax this frag yet, and whether we have
1132 ever extended a PC relative fragment because of a shift count. */
1133 #define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
1134 small, ext, \
1135 dslot, jal_dslot) \
1136 (0x80000000 \
1137 | ((type) & 0xff) \
1138 | ((e2) ? 0x100 : 0) \
1139 | ((pic) ? 0x200 : 0) \
1140 | ((sym32) ? 0x400 : 0) \
1141 | ((nomacro) ? 0x800 : 0) \
1142 | ((small) ? 0x1000 : 0) \
1143 | ((ext) ? 0x2000 : 0) \
1144 | ((dslot) ? 0x4000 : 0) \
1145 | ((jal_dslot) ? 0x8000 : 0))
1146
1147 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1148 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1149 #define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1150 #define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1151 #define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1152 #define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1153 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1154 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1155 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1156 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1157
1158 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1159 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1160 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1161 #define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1162 #define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1163 #define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1164 #define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1165 #define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1166 #define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
1167
1168 /* For microMIPS code, we use relaxation similar to one we use for
1169 MIPS16 code. Some instructions that take immediate values support
1170 two encodings: a small one which takes some small value, and a
1171 larger one which takes a 16 bit value. As some branches also follow
1172 this pattern, relaxing these values is required.
1173
1174 We can assemble both microMIPS and normal MIPS code in a single
1175 object. Therefore, we need to support this type of relaxation at
1176 the same time that we support the relaxation described above. We
1177 use one of the high bits of the subtype field to distinguish these
1178 cases.
1179
1180 The information we store for this type of relaxation is the argument
1181 code found in the opcode file for this relocation, the register
1182 selected as the assembler temporary, whether in the 32-bit
1183 instruction mode, whether the branch is unconditional, whether it is
1184 compact, whether there is no delay-slot instruction available to fill
1185 in, whether it stores the link address implicitly in $ra, whether
1186 relaxation of out-of-range 32-bit branches to a sequence of
1187 instructions is enabled, and whether the displacement of a branch is
1188 too large to fit as an immediate argument of a 16-bit and a 32-bit
1189 branch, respectively. */
1190 #define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
1191 uncond, compact, link, nods, \
1192 relax32, toofar16, toofar32) \
1193 (0x40000000 \
1194 | ((type) & 0xff) \
1195 | (((at) & 0x1f) << 8) \
1196 | ((insn32) ? 0x2000 : 0) \
1197 | ((pic) ? 0x4000 : 0) \
1198 | ((uncond) ? 0x8000 : 0) \
1199 | ((compact) ? 0x10000 : 0) \
1200 | ((link) ? 0x20000 : 0) \
1201 | ((nods) ? 0x40000 : 0) \
1202 | ((relax32) ? 0x80000 : 0) \
1203 | ((toofar16) ? 0x100000 : 0) \
1204 | ((toofar32) ? 0x200000 : 0))
1205 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1206 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1207 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1208 #define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
1209 #define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1210 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1211 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1212 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1213 #define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1214 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1215
1216 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1217 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1218 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1219 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1220 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1221 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
1222
1223 /* Sign-extend 16-bit value X. */
1224 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1225
1226 /* Is the given value a sign-extended 32-bit value? */
1227 #define IS_SEXT_32BIT_NUM(x) \
1228 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1229 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1230
1231 /* Is the given value a sign-extended 16-bit value? */
1232 #define IS_SEXT_16BIT_NUM(x) \
1233 (((x) &~ (offsetT) 0x7fff) == 0 \
1234 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1235
1236 /* Is the given value a sign-extended 12-bit value? */
1237 #define IS_SEXT_12BIT_NUM(x) \
1238 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1239
1240 /* Is the given value a sign-extended 9-bit value? */
1241 #define IS_SEXT_9BIT_NUM(x) \
1242 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1243
1244 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
1245 #define IS_ZEXT_32BIT_NUM(x) \
1246 (((x) &~ (offsetT) 0xffffffff) == 0 \
1247 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1248
1249 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1250 SHIFT places. */
1251 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1252 (((STRUCT) >> (SHIFT)) & (MASK))
1253
1254 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
1255 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1256 (!(MICROMIPS) \
1257 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1258 : EXTRACT_BITS ((INSN).insn_opcode, \
1259 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1260 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1261 EXTRACT_BITS ((INSN).insn_opcode, \
1262 MIPS16OP_MASK_##FIELD, \
1263 MIPS16OP_SH_##FIELD)
1264
1265 /* The MIPS16 EXTEND opcode, shifted left 16 places. */
1266 #define MIPS16_EXTEND (0xf000U << 16)
1267 \f
1268 /* Whether or not we are emitting a branch-likely macro. */
1269 static bfd_boolean emit_branch_likely_macro = FALSE;
1270
1271 /* Global variables used when generating relaxable macros. See the
1272 comment above RELAX_ENCODE for more details about how relaxation
1273 is used. */
1274 static struct {
1275 /* 0 if we're not emitting a relaxable macro.
1276 1 if we're emitting the first of the two relaxation alternatives.
1277 2 if we're emitting the second alternative. */
1278 int sequence;
1279
1280 /* The first relaxable fixup in the current frag. (In other words,
1281 the first fixup that refers to relaxable code.) */
1282 fixS *first_fixup;
1283
1284 /* sizes[0] says how many bytes of the first alternative are stored in
1285 the current frag. Likewise sizes[1] for the second alternative. */
1286 unsigned int sizes[2];
1287
1288 /* The symbol on which the choice of sequence depends. */
1289 symbolS *symbol;
1290 } mips_relax;
1291 \f
1292 /* Global variables used to decide whether a macro needs a warning. */
1293 static struct {
1294 /* True if the macro is in a branch delay slot. */
1295 bfd_boolean delay_slot_p;
1296
1297 /* Set to the length in bytes required if the macro is in a delay slot
1298 that requires a specific length of instruction, otherwise zero. */
1299 unsigned int delay_slot_length;
1300
1301 /* For relaxable macros, sizes[0] is the length of the first alternative
1302 in bytes and sizes[1] is the length of the second alternative.
1303 For non-relaxable macros, both elements give the length of the
1304 macro in bytes. */
1305 unsigned int sizes[2];
1306
1307 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1308 instruction of the first alternative in bytes and first_insn_sizes[1]
1309 is the length of the first instruction of the second alternative.
1310 For non-relaxable macros, both elements give the length of the first
1311 instruction in bytes.
1312
1313 Set to zero if we haven't yet seen the first instruction. */
1314 unsigned int first_insn_sizes[2];
1315
1316 /* For relaxable macros, insns[0] is the number of instructions for the
1317 first alternative and insns[1] is the number of instructions for the
1318 second alternative.
1319
1320 For non-relaxable macros, both elements give the number of
1321 instructions for the macro. */
1322 unsigned int insns[2];
1323
1324 /* The first variant frag for this macro. */
1325 fragS *first_frag;
1326 } mips_macro_warning;
1327 \f
1328 /* Prototypes for static functions. */
1329
1330 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1331
1332 static void append_insn
1333 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1334 bfd_boolean expansionp);
1335 static void mips_no_prev_insn (void);
1336 static void macro_build (expressionS *, const char *, const char *, ...);
1337 static void mips16_macro_build
1338 (expressionS *, const char *, const char *, va_list *);
1339 static void load_register (int, expressionS *, int);
1340 static void macro_start (void);
1341 static void macro_end (void);
1342 static void macro (struct mips_cl_insn *ip, char *str);
1343 static void mips16_macro (struct mips_cl_insn * ip);
1344 static void mips_ip (char *str, struct mips_cl_insn * ip);
1345 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1346 static unsigned long mips16_immed_extend (offsetT, unsigned int);
1347 static void mips16_immed
1348 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1349 unsigned int, unsigned long *);
1350 static size_t my_getSmallExpression
1351 (expressionS *, bfd_reloc_code_real_type *, char *);
1352 static void my_getExpression (expressionS *, char *);
1353 static void s_align (int);
1354 static void s_change_sec (int);
1355 static void s_change_section (int);
1356 static void s_cons (int);
1357 static void s_float_cons (int);
1358 static void s_mips_globl (int);
1359 static void s_option (int);
1360 static void s_mipsset (int);
1361 static void s_abicalls (int);
1362 static void s_cpload (int);
1363 static void s_cpsetup (int);
1364 static void s_cplocal (int);
1365 static void s_cprestore (int);
1366 static void s_cpreturn (int);
1367 static void s_dtprelword (int);
1368 static void s_dtpreldword (int);
1369 static void s_tprelword (int);
1370 static void s_tpreldword (int);
1371 static void s_gpvalue (int);
1372 static void s_gpword (int);
1373 static void s_gpdword (int);
1374 static void s_ehword (int);
1375 static void s_cpadd (int);
1376 static void s_insn (int);
1377 static void s_nan (int);
1378 static void s_module (int);
1379 static void s_mips_ent (int);
1380 static void s_mips_end (int);
1381 static void s_mips_frame (int);
1382 static void s_mips_mask (int reg_type);
1383 static void s_mips_stab (int);
1384 static void s_mips_weakext (int);
1385 static void s_mips_file (int);
1386 static void s_mips_loc (int);
1387 static bfd_boolean pic_need_relax (symbolS *);
1388 static int relaxed_branch_length (fragS *, asection *, int);
1389 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1390 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1391 static void file_mips_check_options (void);
1392
1393 /* Table and functions used to map between CPU/ISA names, and
1394 ISA levels, and CPU numbers. */
1395
1396 struct mips_cpu_info
1397 {
1398 const char *name; /* CPU or ISA name. */
1399 int flags; /* MIPS_CPU_* flags. */
1400 int ase; /* Set of ASEs implemented by the CPU. */
1401 int isa; /* ISA level. */
1402 int cpu; /* CPU number (default CPU if ISA). */
1403 };
1404
1405 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1406
1407 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1408 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1409 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1410 \f
1411 /* Command-line options. */
1412 const char *md_shortopts = "O::g::G:";
1413
1414 enum options
1415 {
1416 OPTION_MARCH = OPTION_MD_BASE,
1417 OPTION_MTUNE,
1418 OPTION_MIPS1,
1419 OPTION_MIPS2,
1420 OPTION_MIPS3,
1421 OPTION_MIPS4,
1422 OPTION_MIPS5,
1423 OPTION_MIPS32,
1424 OPTION_MIPS64,
1425 OPTION_MIPS32R2,
1426 OPTION_MIPS32R3,
1427 OPTION_MIPS32R5,
1428 OPTION_MIPS32R6,
1429 OPTION_MIPS64R2,
1430 OPTION_MIPS64R3,
1431 OPTION_MIPS64R5,
1432 OPTION_MIPS64R6,
1433 OPTION_MIPS16,
1434 OPTION_NO_MIPS16,
1435 OPTION_MIPS3D,
1436 OPTION_NO_MIPS3D,
1437 OPTION_MDMX,
1438 OPTION_NO_MDMX,
1439 OPTION_DSP,
1440 OPTION_NO_DSP,
1441 OPTION_MT,
1442 OPTION_NO_MT,
1443 OPTION_VIRT,
1444 OPTION_NO_VIRT,
1445 OPTION_MSA,
1446 OPTION_NO_MSA,
1447 OPTION_SMARTMIPS,
1448 OPTION_NO_SMARTMIPS,
1449 OPTION_DSPR2,
1450 OPTION_NO_DSPR2,
1451 OPTION_DSPR3,
1452 OPTION_NO_DSPR3,
1453 OPTION_EVA,
1454 OPTION_NO_EVA,
1455 OPTION_XPA,
1456 OPTION_NO_XPA,
1457 OPTION_MICROMIPS,
1458 OPTION_NO_MICROMIPS,
1459 OPTION_MCU,
1460 OPTION_NO_MCU,
1461 OPTION_MIPS16E2,
1462 OPTION_NO_MIPS16E2,
1463 OPTION_CRC,
1464 OPTION_NO_CRC,
1465 OPTION_M4650,
1466 OPTION_NO_M4650,
1467 OPTION_M4010,
1468 OPTION_NO_M4010,
1469 OPTION_M4100,
1470 OPTION_NO_M4100,
1471 OPTION_M3900,
1472 OPTION_NO_M3900,
1473 OPTION_M7000_HILO_FIX,
1474 OPTION_MNO_7000_HILO_FIX,
1475 OPTION_FIX_24K,
1476 OPTION_NO_FIX_24K,
1477 OPTION_FIX_RM7000,
1478 OPTION_NO_FIX_RM7000,
1479 OPTION_FIX_LOONGSON2F_JUMP,
1480 OPTION_NO_FIX_LOONGSON2F_JUMP,
1481 OPTION_FIX_LOONGSON2F_NOP,
1482 OPTION_NO_FIX_LOONGSON2F_NOP,
1483 OPTION_FIX_VR4120,
1484 OPTION_NO_FIX_VR4120,
1485 OPTION_FIX_VR4130,
1486 OPTION_NO_FIX_VR4130,
1487 OPTION_FIX_CN63XXP1,
1488 OPTION_NO_FIX_CN63XXP1,
1489 OPTION_TRAP,
1490 OPTION_BREAK,
1491 OPTION_EB,
1492 OPTION_EL,
1493 OPTION_FP32,
1494 OPTION_GP32,
1495 OPTION_CONSTRUCT_FLOATS,
1496 OPTION_NO_CONSTRUCT_FLOATS,
1497 OPTION_FP64,
1498 OPTION_FPXX,
1499 OPTION_GP64,
1500 OPTION_RELAX_BRANCH,
1501 OPTION_NO_RELAX_BRANCH,
1502 OPTION_IGNORE_BRANCH_ISA,
1503 OPTION_NO_IGNORE_BRANCH_ISA,
1504 OPTION_INSN32,
1505 OPTION_NO_INSN32,
1506 OPTION_MSHARED,
1507 OPTION_MNO_SHARED,
1508 OPTION_MSYM32,
1509 OPTION_MNO_SYM32,
1510 OPTION_SOFT_FLOAT,
1511 OPTION_HARD_FLOAT,
1512 OPTION_SINGLE_FLOAT,
1513 OPTION_DOUBLE_FLOAT,
1514 OPTION_32,
1515 OPTION_CALL_SHARED,
1516 OPTION_CALL_NONPIC,
1517 OPTION_NON_SHARED,
1518 OPTION_XGOT,
1519 OPTION_MABI,
1520 OPTION_N32,
1521 OPTION_64,
1522 OPTION_MDEBUG,
1523 OPTION_NO_MDEBUG,
1524 OPTION_PDR,
1525 OPTION_NO_PDR,
1526 OPTION_MVXWORKS_PIC,
1527 OPTION_NAN,
1528 OPTION_ODD_SPREG,
1529 OPTION_NO_ODD_SPREG,
1530 OPTION_GINV,
1531 OPTION_NO_GINV,
1532 OPTION_END_OF_ENUM
1533 };
1534
1535 struct option md_longopts[] =
1536 {
1537 /* Options which specify architecture. */
1538 {"march", required_argument, NULL, OPTION_MARCH},
1539 {"mtune", required_argument, NULL, OPTION_MTUNE},
1540 {"mips0", no_argument, NULL, OPTION_MIPS1},
1541 {"mips1", no_argument, NULL, OPTION_MIPS1},
1542 {"mips2", no_argument, NULL, OPTION_MIPS2},
1543 {"mips3", no_argument, NULL, OPTION_MIPS3},
1544 {"mips4", no_argument, NULL, OPTION_MIPS4},
1545 {"mips5", no_argument, NULL, OPTION_MIPS5},
1546 {"mips32", no_argument, NULL, OPTION_MIPS32},
1547 {"mips64", no_argument, NULL, OPTION_MIPS64},
1548 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1549 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1550 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
1551 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
1552 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1553 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1554 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
1555 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
1556
1557 /* Options which specify Application Specific Extensions (ASEs). */
1558 {"mips16", no_argument, NULL, OPTION_MIPS16},
1559 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1560 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1561 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1562 {"mdmx", no_argument, NULL, OPTION_MDMX},
1563 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1564 {"mdsp", no_argument, NULL, OPTION_DSP},
1565 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1566 {"mmt", no_argument, NULL, OPTION_MT},
1567 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1568 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1569 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1570 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1571 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1572 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1573 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
1574 {"meva", no_argument, NULL, OPTION_EVA},
1575 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1576 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1577 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1578 {"mmcu", no_argument, NULL, OPTION_MCU},
1579 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1580 {"mvirt", no_argument, NULL, OPTION_VIRT},
1581 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1582 {"mmsa", no_argument, NULL, OPTION_MSA},
1583 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
1584 {"mxpa", no_argument, NULL, OPTION_XPA},
1585 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
1586 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1587 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
1588 {"mcrc", no_argument, NULL, OPTION_CRC},
1589 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
1590 {"mginv", no_argument, NULL, OPTION_GINV},
1591 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
1592
1593 /* Old-style architecture options. Don't add more of these. */
1594 {"m4650", no_argument, NULL, OPTION_M4650},
1595 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1596 {"m4010", no_argument, NULL, OPTION_M4010},
1597 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1598 {"m4100", no_argument, NULL, OPTION_M4100},
1599 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1600 {"m3900", no_argument, NULL, OPTION_M3900},
1601 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1602
1603 /* Options which enable bug fixes. */
1604 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1605 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1606 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1607 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1608 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1609 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1610 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1611 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1612 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1613 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1614 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1615 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1616 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1617 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1618 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
1619 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1620 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1621
1622 /* Miscellaneous options. */
1623 {"trap", no_argument, NULL, OPTION_TRAP},
1624 {"no-break", no_argument, NULL, OPTION_TRAP},
1625 {"break", no_argument, NULL, OPTION_BREAK},
1626 {"no-trap", no_argument, NULL, OPTION_BREAK},
1627 {"EB", no_argument, NULL, OPTION_EB},
1628 {"EL", no_argument, NULL, OPTION_EL},
1629 {"mfp32", no_argument, NULL, OPTION_FP32},
1630 {"mgp32", no_argument, NULL, OPTION_GP32},
1631 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1632 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1633 {"mfp64", no_argument, NULL, OPTION_FP64},
1634 {"mfpxx", no_argument, NULL, OPTION_FPXX},
1635 {"mgp64", no_argument, NULL, OPTION_GP64},
1636 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1637 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1638 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1639 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
1640 {"minsn32", no_argument, NULL, OPTION_INSN32},
1641 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1642 {"mshared", no_argument, NULL, OPTION_MSHARED},
1643 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1644 {"msym32", no_argument, NULL, OPTION_MSYM32},
1645 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1646 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1647 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1648 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1649 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1650 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1651 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
1652
1653 /* Strictly speaking this next option is ELF specific,
1654 but we allow it for other ports as well in order to
1655 make testing easier. */
1656 {"32", no_argument, NULL, OPTION_32},
1657
1658 /* ELF-specific options. */
1659 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1660 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1661 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1662 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1663 {"xgot", no_argument, NULL, OPTION_XGOT},
1664 {"mabi", required_argument, NULL, OPTION_MABI},
1665 {"n32", no_argument, NULL, OPTION_N32},
1666 {"64", no_argument, NULL, OPTION_64},
1667 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1668 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1669 {"mpdr", no_argument, NULL, OPTION_PDR},
1670 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1671 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1672 {"mnan", required_argument, NULL, OPTION_NAN},
1673
1674 {NULL, no_argument, NULL, 0}
1675 };
1676 size_t md_longopts_size = sizeof (md_longopts);
1677 \f
1678 /* Information about either an Application Specific Extension or an
1679 optional architecture feature that, for simplicity, we treat in the
1680 same way as an ASE. */
1681 struct mips_ase
1682 {
1683 /* The name of the ASE, used in both the command-line and .set options. */
1684 const char *name;
1685
1686 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1687 and 64-bit architectures, the flags here refer to the subset that
1688 is available on both. */
1689 unsigned int flags;
1690
1691 /* The ASE_* flag used for instructions that are available on 64-bit
1692 architectures but that are not included in FLAGS. */
1693 unsigned int flags64;
1694
1695 /* The command-line options that turn the ASE on and off. */
1696 int option_on;
1697 int option_off;
1698
1699 /* The minimum required architecture revisions for MIPS32, MIPS64,
1700 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1701 int mips32_rev;
1702 int mips64_rev;
1703 int micromips32_rev;
1704 int micromips64_rev;
1705
1706 /* The architecture where the ASE was removed or -1 if the extension has not
1707 been removed. */
1708 int rem_rev;
1709 };
1710
1711 /* A table of all supported ASEs. */
1712 static const struct mips_ase mips_ases[] = {
1713 { "dsp", ASE_DSP, ASE_DSP64,
1714 OPTION_DSP, OPTION_NO_DSP,
1715 2, 2, 2, 2,
1716 -1 },
1717
1718 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1719 OPTION_DSPR2, OPTION_NO_DSPR2,
1720 2, 2, 2, 2,
1721 -1 },
1722
1723 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1724 OPTION_DSPR3, OPTION_NO_DSPR3,
1725 6, 6, -1, -1,
1726 -1 },
1727
1728 { "eva", ASE_EVA, 0,
1729 OPTION_EVA, OPTION_NO_EVA,
1730 2, 2, 2, 2,
1731 -1 },
1732
1733 { "mcu", ASE_MCU, 0,
1734 OPTION_MCU, OPTION_NO_MCU,
1735 2, 2, 2, 2,
1736 -1 },
1737
1738 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1739 { "mdmx", ASE_MDMX, 0,
1740 OPTION_MDMX, OPTION_NO_MDMX,
1741 -1, 1, -1, -1,
1742 6 },
1743
1744 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1745 { "mips3d", ASE_MIPS3D, 0,
1746 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1747 2, 1, -1, -1,
1748 6 },
1749
1750 { "mt", ASE_MT, 0,
1751 OPTION_MT, OPTION_NO_MT,
1752 2, 2, -1, -1,
1753 -1 },
1754
1755 { "smartmips", ASE_SMARTMIPS, 0,
1756 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1757 1, -1, -1, -1,
1758 6 },
1759
1760 { "virt", ASE_VIRT, ASE_VIRT64,
1761 OPTION_VIRT, OPTION_NO_VIRT,
1762 2, 2, 2, 2,
1763 -1 },
1764
1765 { "msa", ASE_MSA, ASE_MSA64,
1766 OPTION_MSA, OPTION_NO_MSA,
1767 2, 2, 2, 2,
1768 -1 },
1769
1770 { "xpa", ASE_XPA, 0,
1771 OPTION_XPA, OPTION_NO_XPA,
1772 2, 2, 2, 2,
1773 -1 },
1774
1775 { "mips16e2", ASE_MIPS16E2, 0,
1776 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1777 2, 2, -1, -1,
1778 6 },
1779
1780 { "crc", ASE_CRC, ASE_CRC64,
1781 OPTION_CRC, OPTION_NO_CRC,
1782 6, 6, -1, -1,
1783 -1 },
1784
1785 { "ginv", ASE_GINV, 0,
1786 OPTION_GINV, OPTION_NO_GINV,
1787 6, 6, 6, 6,
1788 -1 },
1789 };
1790
1791 /* The set of ASEs that require -mfp64. */
1792 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
1793
1794 /* Groups of ASE_* flags that represent different revisions of an ASE. */
1795 static const unsigned int mips_ase_groups[] = {
1796 ASE_DSP | ASE_DSPR2 | ASE_DSPR3
1797 };
1798 \f
1799 /* Pseudo-op table.
1800
1801 The following pseudo-ops from the Kane and Heinrich MIPS book
1802 should be defined here, but are currently unsupported: .alias,
1803 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1804
1805 The following pseudo-ops from the Kane and Heinrich MIPS book are
1806 specific to the type of debugging information being generated, and
1807 should be defined by the object format: .aent, .begin, .bend,
1808 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1809 .vreg.
1810
1811 The following pseudo-ops from the Kane and Heinrich MIPS book are
1812 not MIPS CPU specific, but are also not specific to the object file
1813 format. This file is probably the best place to define them, but
1814 they are not currently supported: .asm0, .endr, .lab, .struct. */
1815
1816 static const pseudo_typeS mips_pseudo_table[] =
1817 {
1818 /* MIPS specific pseudo-ops. */
1819 {"option", s_option, 0},
1820 {"set", s_mipsset, 0},
1821 {"rdata", s_change_sec, 'r'},
1822 {"sdata", s_change_sec, 's'},
1823 {"livereg", s_ignore, 0},
1824 {"abicalls", s_abicalls, 0},
1825 {"cpload", s_cpload, 0},
1826 {"cpsetup", s_cpsetup, 0},
1827 {"cplocal", s_cplocal, 0},
1828 {"cprestore", s_cprestore, 0},
1829 {"cpreturn", s_cpreturn, 0},
1830 {"dtprelword", s_dtprelword, 0},
1831 {"dtpreldword", s_dtpreldword, 0},
1832 {"tprelword", s_tprelword, 0},
1833 {"tpreldword", s_tpreldword, 0},
1834 {"gpvalue", s_gpvalue, 0},
1835 {"gpword", s_gpword, 0},
1836 {"gpdword", s_gpdword, 0},
1837 {"ehword", s_ehword, 0},
1838 {"cpadd", s_cpadd, 0},
1839 {"insn", s_insn, 0},
1840 {"nan", s_nan, 0},
1841 {"module", s_module, 0},
1842
1843 /* Relatively generic pseudo-ops that happen to be used on MIPS
1844 chips. */
1845 {"asciiz", stringer, 8 + 1},
1846 {"bss", s_change_sec, 'b'},
1847 {"err", s_err, 0},
1848 {"half", s_cons, 1},
1849 {"dword", s_cons, 3},
1850 {"weakext", s_mips_weakext, 0},
1851 {"origin", s_org, 0},
1852 {"repeat", s_rept, 0},
1853
1854 /* For MIPS this is non-standard, but we define it for consistency. */
1855 {"sbss", s_change_sec, 'B'},
1856
1857 /* These pseudo-ops are defined in read.c, but must be overridden
1858 here for one reason or another. */
1859 {"align", s_align, 0},
1860 {"byte", s_cons, 0},
1861 {"data", s_change_sec, 'd'},
1862 {"double", s_float_cons, 'd'},
1863 {"float", s_float_cons, 'f'},
1864 {"globl", s_mips_globl, 0},
1865 {"global", s_mips_globl, 0},
1866 {"hword", s_cons, 1},
1867 {"int", s_cons, 2},
1868 {"long", s_cons, 2},
1869 {"octa", s_cons, 4},
1870 {"quad", s_cons, 3},
1871 {"section", s_change_section, 0},
1872 {"short", s_cons, 1},
1873 {"single", s_float_cons, 'f'},
1874 {"stabd", s_mips_stab, 'd'},
1875 {"stabn", s_mips_stab, 'n'},
1876 {"stabs", s_mips_stab, 's'},
1877 {"text", s_change_sec, 't'},
1878 {"word", s_cons, 2},
1879
1880 { "extern", ecoff_directive_extern, 0},
1881
1882 { NULL, NULL, 0 },
1883 };
1884
1885 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1886 {
1887 /* These pseudo-ops should be defined by the object file format.
1888 However, a.out doesn't support them, so we have versions here. */
1889 {"aent", s_mips_ent, 1},
1890 {"bgnb", s_ignore, 0},
1891 {"end", s_mips_end, 0},
1892 {"endb", s_ignore, 0},
1893 {"ent", s_mips_ent, 0},
1894 {"file", s_mips_file, 0},
1895 {"fmask", s_mips_mask, 'F'},
1896 {"frame", s_mips_frame, 0},
1897 {"loc", s_mips_loc, 0},
1898 {"mask", s_mips_mask, 'R'},
1899 {"verstamp", s_ignore, 0},
1900 { NULL, NULL, 0 },
1901 };
1902
1903 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1904 purpose of the `.dc.a' internal pseudo-op. */
1905
1906 int
1907 mips_address_bytes (void)
1908 {
1909 file_mips_check_options ();
1910 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1911 }
1912
1913 extern void pop_insert (const pseudo_typeS *);
1914
1915 void
1916 mips_pop_insert (void)
1917 {
1918 pop_insert (mips_pseudo_table);
1919 if (! ECOFF_DEBUGGING)
1920 pop_insert (mips_nonecoff_pseudo_table);
1921 }
1922 \f
1923 /* Symbols labelling the current insn. */
1924
1925 struct insn_label_list
1926 {
1927 struct insn_label_list *next;
1928 symbolS *label;
1929 };
1930
1931 static struct insn_label_list *free_insn_labels;
1932 #define label_list tc_segment_info_data.labels
1933
1934 static void mips_clear_insn_labels (void);
1935 static void mips_mark_labels (void);
1936 static void mips_compressed_mark_labels (void);
1937
1938 static inline void
1939 mips_clear_insn_labels (void)
1940 {
1941 struct insn_label_list **pl;
1942 segment_info_type *si;
1943
1944 if (now_seg)
1945 {
1946 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1947 ;
1948
1949 si = seg_info (now_seg);
1950 *pl = si->label_list;
1951 si->label_list = NULL;
1952 }
1953 }
1954
1955 /* Mark instruction labels in MIPS16/microMIPS mode. */
1956
1957 static inline void
1958 mips_mark_labels (void)
1959 {
1960 if (HAVE_CODE_COMPRESSION)
1961 mips_compressed_mark_labels ();
1962 }
1963 \f
1964 static char *expr_end;
1965
1966 /* An expression in a macro instruction. This is set by mips_ip and
1967 mips16_ip and when populated is always an O_constant. */
1968
1969 static expressionS imm_expr;
1970
1971 /* The relocatable field in an instruction and the relocs associated
1972 with it. These variables are used for instructions like LUI and
1973 JAL as well as true offsets. They are also used for address
1974 operands in macros. */
1975
1976 static expressionS offset_expr;
1977 static bfd_reloc_code_real_type offset_reloc[3]
1978 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1979
1980 /* This is set to the resulting size of the instruction to be produced
1981 by mips16_ip if an explicit extension is used or by mips_ip if an
1982 explicit size is supplied. */
1983
1984 static unsigned int forced_insn_length;
1985
1986 /* True if we are assembling an instruction. All dot symbols defined during
1987 this time should be treated as code labels. */
1988
1989 static bfd_boolean mips_assembling_insn;
1990
1991 /* The pdr segment for per procedure frame/regmask info. Not used for
1992 ECOFF debugging. */
1993
1994 static segT pdr_seg;
1995
1996 /* The default target format to use. */
1997
1998 #if defined (TE_FreeBSD)
1999 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2000 #elif defined (TE_TMIPS)
2001 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2002 #else
2003 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2004 #endif
2005
2006 const char *
2007 mips_target_format (void)
2008 {
2009 switch (OUTPUT_FLAVOR)
2010 {
2011 case bfd_target_elf_flavour:
2012 #ifdef TE_VXWORKS
2013 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2014 return (target_big_endian
2015 ? "elf32-bigmips-vxworks"
2016 : "elf32-littlemips-vxworks");
2017 #endif
2018 return (target_big_endian
2019 ? (HAVE_64BIT_OBJECTS
2020 ? ELF_TARGET ("elf64-", "big")
2021 : (HAVE_NEWABI
2022 ? ELF_TARGET ("elf32-n", "big")
2023 : ELF_TARGET ("elf32-", "big")))
2024 : (HAVE_64BIT_OBJECTS
2025 ? ELF_TARGET ("elf64-", "little")
2026 : (HAVE_NEWABI
2027 ? ELF_TARGET ("elf32-n", "little")
2028 : ELF_TARGET ("elf32-", "little"))));
2029 default:
2030 abort ();
2031 return NULL;
2032 }
2033 }
2034
2035 /* Return the ISA revision that is currently in use, or 0 if we are
2036 generating code for MIPS V or below. */
2037
2038 static int
2039 mips_isa_rev (void)
2040 {
2041 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2042 return 2;
2043
2044 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2045 return 3;
2046
2047 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2048 return 5;
2049
2050 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2051 return 6;
2052
2053 /* microMIPS implies revision 2 or above. */
2054 if (mips_opts.micromips)
2055 return 2;
2056
2057 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2058 return 1;
2059
2060 return 0;
2061 }
2062
2063 /* Return the mask of all ASEs that are revisions of those in FLAGS. */
2064
2065 static unsigned int
2066 mips_ase_mask (unsigned int flags)
2067 {
2068 unsigned int i;
2069
2070 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2071 if (flags & mips_ase_groups[i])
2072 flags |= mips_ase_groups[i];
2073 return flags;
2074 }
2075
2076 /* Check whether the current ISA supports ASE. Issue a warning if
2077 appropriate. */
2078
2079 static void
2080 mips_check_isa_supports_ase (const struct mips_ase *ase)
2081 {
2082 const char *base;
2083 int min_rev, size;
2084 static unsigned int warned_isa;
2085 static unsigned int warned_fp32;
2086
2087 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2088 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2089 else
2090 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2091 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2092 && (warned_isa & ase->flags) != ase->flags)
2093 {
2094 warned_isa |= ase->flags;
2095 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2096 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2097 if (min_rev < 0)
2098 as_warn (_("the %d-bit %s architecture does not support the"
2099 " `%s' extension"), size, base, ase->name);
2100 else
2101 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
2102 ase->name, base, size, min_rev);
2103 }
2104 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2105 && (warned_isa & ase->flags) != ase->flags)
2106 {
2107 warned_isa |= ase->flags;
2108 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2109 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2110 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2111 ase->name, base, size, ase->rem_rev);
2112 }
2113
2114 if ((ase->flags & FP64_ASES)
2115 && mips_opts.fp != 64
2116 && (warned_fp32 & ase->flags) != ase->flags)
2117 {
2118 warned_fp32 |= ase->flags;
2119 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
2120 }
2121 }
2122
2123 /* Check all enabled ASEs to see whether they are supported by the
2124 chosen architecture. */
2125
2126 static void
2127 mips_check_isa_supports_ases (void)
2128 {
2129 unsigned int i, mask;
2130
2131 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2132 {
2133 mask = mips_ase_mask (mips_ases[i].flags);
2134 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2135 mips_check_isa_supports_ase (&mips_ases[i]);
2136 }
2137 }
2138
2139 /* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2140 that were affected. */
2141
2142 static unsigned int
2143 mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2144 bfd_boolean enabled_p)
2145 {
2146 unsigned int mask;
2147
2148 mask = mips_ase_mask (ase->flags);
2149 opts->ase &= ~mask;
2150
2151 /* Clear combination ASE flags, which need to be recalculated based on
2152 updated regular ASE settings. */
2153 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT);
2154
2155 if (enabled_p)
2156 opts->ase |= ase->flags;
2157
2158 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2159 instructions which are only valid when both ASEs are enabled.
2160 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2161 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2162 {
2163 opts->ase |= ASE_XPA_VIRT;
2164 mask |= ASE_XPA_VIRT;
2165 }
2166 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2167 {
2168 opts->ase |= ASE_MIPS16E2_MT;
2169 mask |= ASE_MIPS16E2_MT;
2170 }
2171
2172 return mask;
2173 }
2174
2175 /* Return the ASE called NAME, or null if none. */
2176
2177 static const struct mips_ase *
2178 mips_lookup_ase (const char *name)
2179 {
2180 unsigned int i;
2181
2182 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2183 if (strcmp (name, mips_ases[i].name) == 0)
2184 return &mips_ases[i];
2185 return NULL;
2186 }
2187
2188 /* Return the length of a microMIPS instruction in bytes. If bits of
2189 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2190 otherwise it is a 32-bit instruction. */
2191
2192 static inline unsigned int
2193 micromips_insn_length (const struct mips_opcode *mo)
2194 {
2195 return mips_opcode_32bit_p (mo) ? 4 : 2;
2196 }
2197
2198 /* Return the length of MIPS16 instruction OPCODE. */
2199
2200 static inline unsigned int
2201 mips16_opcode_length (unsigned long opcode)
2202 {
2203 return (opcode >> 16) == 0 ? 2 : 4;
2204 }
2205
2206 /* Return the length of instruction INSN. */
2207
2208 static inline unsigned int
2209 insn_length (const struct mips_cl_insn *insn)
2210 {
2211 if (mips_opts.micromips)
2212 return micromips_insn_length (insn->insn_mo);
2213 else if (mips_opts.mips16)
2214 return mips16_opcode_length (insn->insn_opcode);
2215 else
2216 return 4;
2217 }
2218
2219 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2220
2221 static void
2222 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2223 {
2224 size_t i;
2225
2226 insn->insn_mo = mo;
2227 insn->insn_opcode = mo->match;
2228 insn->frag = NULL;
2229 insn->where = 0;
2230 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2231 insn->fixp[i] = NULL;
2232 insn->fixed_p = (mips_opts.noreorder > 0);
2233 insn->noreorder_p = (mips_opts.noreorder > 0);
2234 insn->mips16_absolute_jump_p = 0;
2235 insn->complete_p = 0;
2236 insn->cleared_p = 0;
2237 }
2238
2239 /* Get a list of all the operands in INSN. */
2240
2241 static const struct mips_operand_array *
2242 insn_operands (const struct mips_cl_insn *insn)
2243 {
2244 if (insn->insn_mo >= &mips_opcodes[0]
2245 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2246 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2247
2248 if (insn->insn_mo >= &mips16_opcodes[0]
2249 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2250 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2251
2252 if (insn->insn_mo >= &micromips_opcodes[0]
2253 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2254 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2255
2256 abort ();
2257 }
2258
2259 /* Get a description of operand OPNO of INSN. */
2260
2261 static const struct mips_operand *
2262 insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2263 {
2264 const struct mips_operand_array *operands;
2265
2266 operands = insn_operands (insn);
2267 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2268 abort ();
2269 return operands->operand[opno];
2270 }
2271
2272 /* Install UVAL as the value of OPERAND in INSN. */
2273
2274 static inline void
2275 insn_insert_operand (struct mips_cl_insn *insn,
2276 const struct mips_operand *operand, unsigned int uval)
2277 {
2278 if (mips_opts.mips16
2279 && operand->type == OP_INT && operand->lsb == 0
2280 && mips_opcode_32bit_p (insn->insn_mo))
2281 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2282 else
2283 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2284 }
2285
2286 /* Extract the value of OPERAND from INSN. */
2287
2288 static inline unsigned
2289 insn_extract_operand (const struct mips_cl_insn *insn,
2290 const struct mips_operand *operand)
2291 {
2292 return mips_extract_operand (operand, insn->insn_opcode);
2293 }
2294
2295 /* Record the current MIPS16/microMIPS mode in now_seg. */
2296
2297 static void
2298 mips_record_compressed_mode (void)
2299 {
2300 segment_info_type *si;
2301
2302 si = seg_info (now_seg);
2303 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2304 si->tc_segment_info_data.mips16 = mips_opts.mips16;
2305 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2306 si->tc_segment_info_data.micromips = mips_opts.micromips;
2307 }
2308
2309 /* Read a standard MIPS instruction from BUF. */
2310
2311 static unsigned long
2312 read_insn (char *buf)
2313 {
2314 if (target_big_endian)
2315 return bfd_getb32 ((bfd_byte *) buf);
2316 else
2317 return bfd_getl32 ((bfd_byte *) buf);
2318 }
2319
2320 /* Write standard MIPS instruction INSN to BUF. Return a pointer to
2321 the next byte. */
2322
2323 static char *
2324 write_insn (char *buf, unsigned int insn)
2325 {
2326 md_number_to_chars (buf, insn, 4);
2327 return buf + 4;
2328 }
2329
2330 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2331 has length LENGTH. */
2332
2333 static unsigned long
2334 read_compressed_insn (char *buf, unsigned int length)
2335 {
2336 unsigned long insn;
2337 unsigned int i;
2338
2339 insn = 0;
2340 for (i = 0; i < length; i += 2)
2341 {
2342 insn <<= 16;
2343 if (target_big_endian)
2344 insn |= bfd_getb16 ((char *) buf);
2345 else
2346 insn |= bfd_getl16 ((char *) buf);
2347 buf += 2;
2348 }
2349 return insn;
2350 }
2351
2352 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2353 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2354
2355 static char *
2356 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2357 {
2358 unsigned int i;
2359
2360 for (i = 0; i < length; i += 2)
2361 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2362 return buf + length;
2363 }
2364
2365 /* Install INSN at the location specified by its "frag" and "where" fields. */
2366
2367 static void
2368 install_insn (const struct mips_cl_insn *insn)
2369 {
2370 char *f = insn->frag->fr_literal + insn->where;
2371 if (HAVE_CODE_COMPRESSION)
2372 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2373 else
2374 write_insn (f, insn->insn_opcode);
2375 mips_record_compressed_mode ();
2376 }
2377
2378 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2379 and install the opcode in the new location. */
2380
2381 static void
2382 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2383 {
2384 size_t i;
2385
2386 insn->frag = frag;
2387 insn->where = where;
2388 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2389 if (insn->fixp[i] != NULL)
2390 {
2391 insn->fixp[i]->fx_frag = frag;
2392 insn->fixp[i]->fx_where = where;
2393 }
2394 install_insn (insn);
2395 }
2396
2397 /* Add INSN to the end of the output. */
2398
2399 static void
2400 add_fixed_insn (struct mips_cl_insn *insn)
2401 {
2402 char *f = frag_more (insn_length (insn));
2403 move_insn (insn, frag_now, f - frag_now->fr_literal);
2404 }
2405
2406 /* Start a variant frag and move INSN to the start of the variant part,
2407 marking it as fixed. The other arguments are as for frag_var. */
2408
2409 static void
2410 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2411 relax_substateT subtype, symbolS *symbol, offsetT offset)
2412 {
2413 frag_grow (max_chars);
2414 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2415 insn->fixed_p = 1;
2416 frag_var (rs_machine_dependent, max_chars, var,
2417 subtype, symbol, offset, NULL);
2418 }
2419
2420 /* Insert N copies of INSN into the history buffer, starting at
2421 position FIRST. Neither FIRST nor N need to be clipped. */
2422
2423 static void
2424 insert_into_history (unsigned int first, unsigned int n,
2425 const struct mips_cl_insn *insn)
2426 {
2427 if (mips_relax.sequence != 2)
2428 {
2429 unsigned int i;
2430
2431 for (i = ARRAY_SIZE (history); i-- > first;)
2432 if (i >= first + n)
2433 history[i] = history[i - n];
2434 else
2435 history[i] = *insn;
2436 }
2437 }
2438
2439 /* Clear the error in insn_error. */
2440
2441 static void
2442 clear_insn_error (void)
2443 {
2444 memset (&insn_error, 0, sizeof (insn_error));
2445 }
2446
2447 /* Possibly record error message MSG for the current instruction.
2448 If the error is about a particular argument, ARGNUM is the 1-based
2449 number of that argument, otherwise it is 0. FORMAT is the format
2450 of MSG. Return true if MSG was used, false if the current message
2451 was kept. */
2452
2453 static bfd_boolean
2454 set_insn_error_format (int argnum, enum mips_insn_error_format format,
2455 const char *msg)
2456 {
2457 if (argnum == 0)
2458 {
2459 /* Give priority to errors against specific arguments, and to
2460 the first whole-instruction message. */
2461 if (insn_error.msg)
2462 return FALSE;
2463 }
2464 else
2465 {
2466 /* Keep insn_error if it is against a later argument. */
2467 if (argnum < insn_error.min_argnum)
2468 return FALSE;
2469
2470 /* If both errors are against the same argument but are different,
2471 give up on reporting a specific error for this argument.
2472 See the comment about mips_insn_error for details. */
2473 if (argnum == insn_error.min_argnum
2474 && insn_error.msg
2475 && strcmp (insn_error.msg, msg) != 0)
2476 {
2477 insn_error.msg = 0;
2478 insn_error.min_argnum += 1;
2479 return FALSE;
2480 }
2481 }
2482 insn_error.min_argnum = argnum;
2483 insn_error.format = format;
2484 insn_error.msg = msg;
2485 return TRUE;
2486 }
2487
2488 /* Record an instruction error with no % format fields. ARGNUM and MSG are
2489 as for set_insn_error_format. */
2490
2491 static void
2492 set_insn_error (int argnum, const char *msg)
2493 {
2494 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2495 }
2496
2497 /* Record an instruction error with one %d field I. ARGNUM and MSG are
2498 as for set_insn_error_format. */
2499
2500 static void
2501 set_insn_error_i (int argnum, const char *msg, int i)
2502 {
2503 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2504 insn_error.u.i = i;
2505 }
2506
2507 /* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2508 are as for set_insn_error_format. */
2509
2510 static void
2511 set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2512 {
2513 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2514 {
2515 insn_error.u.ss[0] = s1;
2516 insn_error.u.ss[1] = s2;
2517 }
2518 }
2519
2520 /* Report the error in insn_error, which is against assembly code STR. */
2521
2522 static void
2523 report_insn_error (const char *str)
2524 {
2525 const char *msg = concat (insn_error.msg, " `%s'", NULL);
2526
2527 switch (insn_error.format)
2528 {
2529 case ERR_FMT_PLAIN:
2530 as_bad (msg, str);
2531 break;
2532
2533 case ERR_FMT_I:
2534 as_bad (msg, insn_error.u.i, str);
2535 break;
2536
2537 case ERR_FMT_SS:
2538 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2539 break;
2540 }
2541
2542 free ((char *) msg);
2543 }
2544
2545 /* Initialize vr4120_conflicts. There is a bit of duplication here:
2546 the idea is to make it obvious at a glance that each errata is
2547 included. */
2548
2549 static void
2550 init_vr4120_conflicts (void)
2551 {
2552 #define CONFLICT(FIRST, SECOND) \
2553 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2554
2555 /* Errata 21 - [D]DIV[U] after [D]MACC */
2556 CONFLICT (MACC, DIV);
2557 CONFLICT (DMACC, DIV);
2558
2559 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2560 CONFLICT (DMULT, DMULT);
2561 CONFLICT (DMULT, DMACC);
2562 CONFLICT (DMACC, DMULT);
2563 CONFLICT (DMACC, DMACC);
2564
2565 /* Errata 24 - MT{LO,HI} after [D]MACC */
2566 CONFLICT (MACC, MTHILO);
2567 CONFLICT (DMACC, MTHILO);
2568
2569 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2570 instruction is executed immediately after a MACC or DMACC
2571 instruction, the result of [either instruction] is incorrect." */
2572 CONFLICT (MACC, MULT);
2573 CONFLICT (MACC, DMULT);
2574 CONFLICT (DMACC, MULT);
2575 CONFLICT (DMACC, DMULT);
2576
2577 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2578 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2579 DDIV or DDIVU instruction, the result of the MACC or
2580 DMACC instruction is incorrect.". */
2581 CONFLICT (DMULT, MACC);
2582 CONFLICT (DMULT, DMACC);
2583 CONFLICT (DIV, MACC);
2584 CONFLICT (DIV, DMACC);
2585
2586 #undef CONFLICT
2587 }
2588
2589 struct regname {
2590 const char *name;
2591 unsigned int num;
2592 };
2593
2594 #define RNUM_MASK 0x00000ff
2595 #define RTYPE_MASK 0x0ffff00
2596 #define RTYPE_NUM 0x0000100
2597 #define RTYPE_FPU 0x0000200
2598 #define RTYPE_FCC 0x0000400
2599 #define RTYPE_VEC 0x0000800
2600 #define RTYPE_GP 0x0001000
2601 #define RTYPE_CP0 0x0002000
2602 #define RTYPE_PC 0x0004000
2603 #define RTYPE_ACC 0x0008000
2604 #define RTYPE_CCC 0x0010000
2605 #define RTYPE_VI 0x0020000
2606 #define RTYPE_VF 0x0040000
2607 #define RTYPE_R5900_I 0x0080000
2608 #define RTYPE_R5900_Q 0x0100000
2609 #define RTYPE_R5900_R 0x0200000
2610 #define RTYPE_R5900_ACC 0x0400000
2611 #define RTYPE_MSA 0x0800000
2612 #define RWARN 0x8000000
2613
2614 #define GENERIC_REGISTER_NUMBERS \
2615 {"$0", RTYPE_NUM | 0}, \
2616 {"$1", RTYPE_NUM | 1}, \
2617 {"$2", RTYPE_NUM | 2}, \
2618 {"$3", RTYPE_NUM | 3}, \
2619 {"$4", RTYPE_NUM | 4}, \
2620 {"$5", RTYPE_NUM | 5}, \
2621 {"$6", RTYPE_NUM | 6}, \
2622 {"$7", RTYPE_NUM | 7}, \
2623 {"$8", RTYPE_NUM | 8}, \
2624 {"$9", RTYPE_NUM | 9}, \
2625 {"$10", RTYPE_NUM | 10}, \
2626 {"$11", RTYPE_NUM | 11}, \
2627 {"$12", RTYPE_NUM | 12}, \
2628 {"$13", RTYPE_NUM | 13}, \
2629 {"$14", RTYPE_NUM | 14}, \
2630 {"$15", RTYPE_NUM | 15}, \
2631 {"$16", RTYPE_NUM | 16}, \
2632 {"$17", RTYPE_NUM | 17}, \
2633 {"$18", RTYPE_NUM | 18}, \
2634 {"$19", RTYPE_NUM | 19}, \
2635 {"$20", RTYPE_NUM | 20}, \
2636 {"$21", RTYPE_NUM | 21}, \
2637 {"$22", RTYPE_NUM | 22}, \
2638 {"$23", RTYPE_NUM | 23}, \
2639 {"$24", RTYPE_NUM | 24}, \
2640 {"$25", RTYPE_NUM | 25}, \
2641 {"$26", RTYPE_NUM | 26}, \
2642 {"$27", RTYPE_NUM | 27}, \
2643 {"$28", RTYPE_NUM | 28}, \
2644 {"$29", RTYPE_NUM | 29}, \
2645 {"$30", RTYPE_NUM | 30}, \
2646 {"$31", RTYPE_NUM | 31}
2647
2648 #define FPU_REGISTER_NAMES \
2649 {"$f0", RTYPE_FPU | 0}, \
2650 {"$f1", RTYPE_FPU | 1}, \
2651 {"$f2", RTYPE_FPU | 2}, \
2652 {"$f3", RTYPE_FPU | 3}, \
2653 {"$f4", RTYPE_FPU | 4}, \
2654 {"$f5", RTYPE_FPU | 5}, \
2655 {"$f6", RTYPE_FPU | 6}, \
2656 {"$f7", RTYPE_FPU | 7}, \
2657 {"$f8", RTYPE_FPU | 8}, \
2658 {"$f9", RTYPE_FPU | 9}, \
2659 {"$f10", RTYPE_FPU | 10}, \
2660 {"$f11", RTYPE_FPU | 11}, \
2661 {"$f12", RTYPE_FPU | 12}, \
2662 {"$f13", RTYPE_FPU | 13}, \
2663 {"$f14", RTYPE_FPU | 14}, \
2664 {"$f15", RTYPE_FPU | 15}, \
2665 {"$f16", RTYPE_FPU | 16}, \
2666 {"$f17", RTYPE_FPU | 17}, \
2667 {"$f18", RTYPE_FPU | 18}, \
2668 {"$f19", RTYPE_FPU | 19}, \
2669 {"$f20", RTYPE_FPU | 20}, \
2670 {"$f21", RTYPE_FPU | 21}, \
2671 {"$f22", RTYPE_FPU | 22}, \
2672 {"$f23", RTYPE_FPU | 23}, \
2673 {"$f24", RTYPE_FPU | 24}, \
2674 {"$f25", RTYPE_FPU | 25}, \
2675 {"$f26", RTYPE_FPU | 26}, \
2676 {"$f27", RTYPE_FPU | 27}, \
2677 {"$f28", RTYPE_FPU | 28}, \
2678 {"$f29", RTYPE_FPU | 29}, \
2679 {"$f30", RTYPE_FPU | 30}, \
2680 {"$f31", RTYPE_FPU | 31}
2681
2682 #define FPU_CONDITION_CODE_NAMES \
2683 {"$fcc0", RTYPE_FCC | 0}, \
2684 {"$fcc1", RTYPE_FCC | 1}, \
2685 {"$fcc2", RTYPE_FCC | 2}, \
2686 {"$fcc3", RTYPE_FCC | 3}, \
2687 {"$fcc4", RTYPE_FCC | 4}, \
2688 {"$fcc5", RTYPE_FCC | 5}, \
2689 {"$fcc6", RTYPE_FCC | 6}, \
2690 {"$fcc7", RTYPE_FCC | 7}
2691
2692 #define COPROC_CONDITION_CODE_NAMES \
2693 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2694 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2695 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2696 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2697 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2698 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2699 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2700 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2701
2702 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2703 {"$a4", RTYPE_GP | 8}, \
2704 {"$a5", RTYPE_GP | 9}, \
2705 {"$a6", RTYPE_GP | 10}, \
2706 {"$a7", RTYPE_GP | 11}, \
2707 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2708 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2709 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2710 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2711 {"$t0", RTYPE_GP | 12}, \
2712 {"$t1", RTYPE_GP | 13}, \
2713 {"$t2", RTYPE_GP | 14}, \
2714 {"$t3", RTYPE_GP | 15}
2715
2716 #define O32_SYMBOLIC_REGISTER_NAMES \
2717 {"$t0", RTYPE_GP | 8}, \
2718 {"$t1", RTYPE_GP | 9}, \
2719 {"$t2", RTYPE_GP | 10}, \
2720 {"$t3", RTYPE_GP | 11}, \
2721 {"$t4", RTYPE_GP | 12}, \
2722 {"$t5", RTYPE_GP | 13}, \
2723 {"$t6", RTYPE_GP | 14}, \
2724 {"$t7", RTYPE_GP | 15}, \
2725 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2726 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2727 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2728 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2729
2730 /* Remaining symbolic register names */
2731 #define SYMBOLIC_REGISTER_NAMES \
2732 {"$zero", RTYPE_GP | 0}, \
2733 {"$at", RTYPE_GP | 1}, \
2734 {"$AT", RTYPE_GP | 1}, \
2735 {"$v0", RTYPE_GP | 2}, \
2736 {"$v1", RTYPE_GP | 3}, \
2737 {"$a0", RTYPE_GP | 4}, \
2738 {"$a1", RTYPE_GP | 5}, \
2739 {"$a2", RTYPE_GP | 6}, \
2740 {"$a3", RTYPE_GP | 7}, \
2741 {"$s0", RTYPE_GP | 16}, \
2742 {"$s1", RTYPE_GP | 17}, \
2743 {"$s2", RTYPE_GP | 18}, \
2744 {"$s3", RTYPE_GP | 19}, \
2745 {"$s4", RTYPE_GP | 20}, \
2746 {"$s5", RTYPE_GP | 21}, \
2747 {"$s6", RTYPE_GP | 22}, \
2748 {"$s7", RTYPE_GP | 23}, \
2749 {"$t8", RTYPE_GP | 24}, \
2750 {"$t9", RTYPE_GP | 25}, \
2751 {"$k0", RTYPE_GP | 26}, \
2752 {"$kt0", RTYPE_GP | 26}, \
2753 {"$k1", RTYPE_GP | 27}, \
2754 {"$kt1", RTYPE_GP | 27}, \
2755 {"$gp", RTYPE_GP | 28}, \
2756 {"$sp", RTYPE_GP | 29}, \
2757 {"$s8", RTYPE_GP | 30}, \
2758 {"$fp", RTYPE_GP | 30}, \
2759 {"$ra", RTYPE_GP | 31}
2760
2761 #define MIPS16_SPECIAL_REGISTER_NAMES \
2762 {"$pc", RTYPE_PC | 0}
2763
2764 #define MDMX_VECTOR_REGISTER_NAMES \
2765 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2766 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2767 {"$v2", RTYPE_VEC | 2}, \
2768 {"$v3", RTYPE_VEC | 3}, \
2769 {"$v4", RTYPE_VEC | 4}, \
2770 {"$v5", RTYPE_VEC | 5}, \
2771 {"$v6", RTYPE_VEC | 6}, \
2772 {"$v7", RTYPE_VEC | 7}, \
2773 {"$v8", RTYPE_VEC | 8}, \
2774 {"$v9", RTYPE_VEC | 9}, \
2775 {"$v10", RTYPE_VEC | 10}, \
2776 {"$v11", RTYPE_VEC | 11}, \
2777 {"$v12", RTYPE_VEC | 12}, \
2778 {"$v13", RTYPE_VEC | 13}, \
2779 {"$v14", RTYPE_VEC | 14}, \
2780 {"$v15", RTYPE_VEC | 15}, \
2781 {"$v16", RTYPE_VEC | 16}, \
2782 {"$v17", RTYPE_VEC | 17}, \
2783 {"$v18", RTYPE_VEC | 18}, \
2784 {"$v19", RTYPE_VEC | 19}, \
2785 {"$v20", RTYPE_VEC | 20}, \
2786 {"$v21", RTYPE_VEC | 21}, \
2787 {"$v22", RTYPE_VEC | 22}, \
2788 {"$v23", RTYPE_VEC | 23}, \
2789 {"$v24", RTYPE_VEC | 24}, \
2790 {"$v25", RTYPE_VEC | 25}, \
2791 {"$v26", RTYPE_VEC | 26}, \
2792 {"$v27", RTYPE_VEC | 27}, \
2793 {"$v28", RTYPE_VEC | 28}, \
2794 {"$v29", RTYPE_VEC | 29}, \
2795 {"$v30", RTYPE_VEC | 30}, \
2796 {"$v31", RTYPE_VEC | 31}
2797
2798 #define R5900_I_NAMES \
2799 {"$I", RTYPE_R5900_I | 0}
2800
2801 #define R5900_Q_NAMES \
2802 {"$Q", RTYPE_R5900_Q | 0}
2803
2804 #define R5900_R_NAMES \
2805 {"$R", RTYPE_R5900_R | 0}
2806
2807 #define R5900_ACC_NAMES \
2808 {"$ACC", RTYPE_R5900_ACC | 0 }
2809
2810 #define MIPS_DSP_ACCUMULATOR_NAMES \
2811 {"$ac0", RTYPE_ACC | 0}, \
2812 {"$ac1", RTYPE_ACC | 1}, \
2813 {"$ac2", RTYPE_ACC | 2}, \
2814 {"$ac3", RTYPE_ACC | 3}
2815
2816 static const struct regname reg_names[] = {
2817 GENERIC_REGISTER_NUMBERS,
2818 FPU_REGISTER_NAMES,
2819 FPU_CONDITION_CODE_NAMES,
2820 COPROC_CONDITION_CODE_NAMES,
2821
2822 /* The $txx registers depends on the abi,
2823 these will be added later into the symbol table from
2824 one of the tables below once mips_abi is set after
2825 parsing of arguments from the command line. */
2826 SYMBOLIC_REGISTER_NAMES,
2827
2828 MIPS16_SPECIAL_REGISTER_NAMES,
2829 MDMX_VECTOR_REGISTER_NAMES,
2830 R5900_I_NAMES,
2831 R5900_Q_NAMES,
2832 R5900_R_NAMES,
2833 R5900_ACC_NAMES,
2834 MIPS_DSP_ACCUMULATOR_NAMES,
2835 {0, 0}
2836 };
2837
2838 static const struct regname reg_names_o32[] = {
2839 O32_SYMBOLIC_REGISTER_NAMES,
2840 {0, 0}
2841 };
2842
2843 static const struct regname reg_names_n32n64[] = {
2844 N32N64_SYMBOLIC_REGISTER_NAMES,
2845 {0, 0}
2846 };
2847
2848 /* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2849 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2850 of these register symbols, return the associated vector register,
2851 otherwise return SYMVAL itself. */
2852
2853 static unsigned int
2854 mips_prefer_vec_regno (unsigned int symval)
2855 {
2856 if ((symval & -2) == (RTYPE_GP | 2))
2857 return RTYPE_VEC | (symval & 1);
2858 return symval;
2859 }
2860
2861 /* Return true if string [S, E) is a valid register name, storing its
2862 symbol value in *SYMVAL_PTR if so. */
2863
2864 static bfd_boolean
2865 mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
2866 {
2867 char save_c;
2868 symbolS *symbol;
2869
2870 /* Terminate name. */
2871 save_c = *e;
2872 *e = '\0';
2873
2874 /* Look up the name. */
2875 symbol = symbol_find (s);
2876 *e = save_c;
2877
2878 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2879 return FALSE;
2880
2881 *symval_ptr = S_GET_VALUE (symbol);
2882 return TRUE;
2883 }
2884
2885 /* Return true if the string at *SPTR is a valid register name. Allow it
2886 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2887 is nonnull.
2888
2889 When returning true, move *SPTR past the register, store the
2890 register's symbol value in *SYMVAL_PTR and the channel mask in
2891 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2892 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2893 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2894
2895 static bfd_boolean
2896 mips_parse_register (char **sptr, unsigned int *symval_ptr,
2897 unsigned int *channels_ptr)
2898 {
2899 char *s, *e, *m;
2900 const char *q;
2901 unsigned int channels, symval, bit;
2902
2903 /* Find end of name. */
2904 s = e = *sptr;
2905 if (is_name_beginner (*e))
2906 ++e;
2907 while (is_part_of_name (*e))
2908 ++e;
2909
2910 channels = 0;
2911 if (!mips_parse_register_1 (s, e, &symval))
2912 {
2913 if (!channels_ptr)
2914 return FALSE;
2915
2916 /* Eat characters from the end of the string that are valid
2917 channel suffixes. The preceding register must be $ACC or
2918 end with a digit, so there is no ambiguity. */
2919 bit = 1;
2920 m = e;
2921 for (q = "wzyx"; *q; q++, bit <<= 1)
2922 if (m > s && m[-1] == *q)
2923 {
2924 --m;
2925 channels |= bit;
2926 }
2927
2928 if (channels == 0
2929 || !mips_parse_register_1 (s, m, &symval)
2930 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2931 return FALSE;
2932 }
2933
2934 *sptr = e;
2935 *symval_ptr = symval;
2936 if (channels_ptr)
2937 *channels_ptr = channels;
2938 return TRUE;
2939 }
2940
2941 /* Check if SPTR points at a valid register specifier according to TYPES.
2942 If so, then return 1, advance S to consume the specifier and store
2943 the register's number in REGNOP, otherwise return 0. */
2944
2945 static int
2946 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2947 {
2948 unsigned int regno;
2949
2950 if (mips_parse_register (s, &regno, NULL))
2951 {
2952 if (types & RTYPE_VEC)
2953 regno = mips_prefer_vec_regno (regno);
2954 if (regno & types)
2955 regno &= RNUM_MASK;
2956 else
2957 regno = ~0;
2958 }
2959 else
2960 {
2961 if (types & RWARN)
2962 as_warn (_("unrecognized register name `%s'"), *s);
2963 regno = ~0;
2964 }
2965 if (regnop)
2966 *regnop = regno;
2967 return regno <= RNUM_MASK;
2968 }
2969
2970 /* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2971 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2972
2973 static char *
2974 mips_parse_vu0_channels (char *s, unsigned int *channels)
2975 {
2976 unsigned int i;
2977
2978 *channels = 0;
2979 for (i = 0; i < 4; i++)
2980 if (*s == "xyzw"[i])
2981 {
2982 *channels |= 1 << (3 - i);
2983 ++s;
2984 }
2985 return s;
2986 }
2987
2988 /* Token types for parsed operand lists. */
2989 enum mips_operand_token_type {
2990 /* A plain register, e.g. $f2. */
2991 OT_REG,
2992
2993 /* A 4-bit XYZW channel mask. */
2994 OT_CHANNELS,
2995
2996 /* A constant vector index, e.g. [1]. */
2997 OT_INTEGER_INDEX,
2998
2999 /* A register vector index, e.g. [$2]. */
3000 OT_REG_INDEX,
3001
3002 /* A continuous range of registers, e.g. $s0-$s4. */
3003 OT_REG_RANGE,
3004
3005 /* A (possibly relocated) expression. */
3006 OT_INTEGER,
3007
3008 /* A floating-point value. */
3009 OT_FLOAT,
3010
3011 /* A single character. This can be '(', ')' or ',', but '(' only appears
3012 before OT_REGs. */
3013 OT_CHAR,
3014
3015 /* A doubled character, either "--" or "++". */
3016 OT_DOUBLE_CHAR,
3017
3018 /* The end of the operand list. */
3019 OT_END
3020 };
3021
3022 /* A parsed operand token. */
3023 struct mips_operand_token
3024 {
3025 /* The type of token. */
3026 enum mips_operand_token_type type;
3027 union
3028 {
3029 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
3030 unsigned int regno;
3031
3032 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3033 unsigned int channels;
3034
3035 /* The integer value of an OT_INTEGER_INDEX. */
3036 addressT index;
3037
3038 /* The two register symbol values involved in an OT_REG_RANGE. */
3039 struct {
3040 unsigned int regno1;
3041 unsigned int regno2;
3042 } reg_range;
3043
3044 /* The value of an OT_INTEGER. The value is represented as an
3045 expression and the relocation operators that were applied to
3046 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3047 relocation operators were used. */
3048 struct {
3049 expressionS value;
3050 bfd_reloc_code_real_type relocs[3];
3051 } integer;
3052
3053 /* The binary data for an OT_FLOAT constant, and the number of bytes
3054 in the constant. */
3055 struct {
3056 unsigned char data[8];
3057 int length;
3058 } flt;
3059
3060 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
3061 char ch;
3062 } u;
3063 };
3064
3065 /* An obstack used to construct lists of mips_operand_tokens. */
3066 static struct obstack mips_operand_tokens;
3067
3068 /* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3069
3070 static void
3071 mips_add_token (struct mips_operand_token *token,
3072 enum mips_operand_token_type type)
3073 {
3074 token->type = type;
3075 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3076 }
3077
3078 /* Check whether S is '(' followed by a register name. Add OT_CHAR
3079 and OT_REG tokens for them if so, and return a pointer to the first
3080 unconsumed character. Return null otherwise. */
3081
3082 static char *
3083 mips_parse_base_start (char *s)
3084 {
3085 struct mips_operand_token token;
3086 unsigned int regno, channels;
3087 bfd_boolean decrement_p;
3088
3089 if (*s != '(')
3090 return 0;
3091
3092 ++s;
3093 SKIP_SPACE_TABS (s);
3094
3095 /* Only match "--" as part of a base expression. In other contexts "--X"
3096 is a double negative. */
3097 decrement_p = (s[0] == '-' && s[1] == '-');
3098 if (decrement_p)
3099 {
3100 s += 2;
3101 SKIP_SPACE_TABS (s);
3102 }
3103
3104 /* Allow a channel specifier because that leads to better error messages
3105 than treating something like "$vf0x++" as an expression. */
3106 if (!mips_parse_register (&s, &regno, &channels))
3107 return 0;
3108
3109 token.u.ch = '(';
3110 mips_add_token (&token, OT_CHAR);
3111
3112 if (decrement_p)
3113 {
3114 token.u.ch = '-';
3115 mips_add_token (&token, OT_DOUBLE_CHAR);
3116 }
3117
3118 token.u.regno = regno;
3119 mips_add_token (&token, OT_REG);
3120
3121 if (channels)
3122 {
3123 token.u.channels = channels;
3124 mips_add_token (&token, OT_CHANNELS);
3125 }
3126
3127 /* For consistency, only match "++" as part of base expressions too. */
3128 SKIP_SPACE_TABS (s);
3129 if (s[0] == '+' && s[1] == '+')
3130 {
3131 s += 2;
3132 token.u.ch = '+';
3133 mips_add_token (&token, OT_DOUBLE_CHAR);
3134 }
3135
3136 return s;
3137 }
3138
3139 /* Parse one or more tokens from S. Return a pointer to the first
3140 unconsumed character on success. Return null if an error was found
3141 and store the error text in insn_error. FLOAT_FORMAT is as for
3142 mips_parse_arguments. */
3143
3144 static char *
3145 mips_parse_argument_token (char *s, char float_format)
3146 {
3147 char *end, *save_in;
3148 const char *err;
3149 unsigned int regno1, regno2, channels;
3150 struct mips_operand_token token;
3151
3152 /* First look for "($reg", since we want to treat that as an
3153 OT_CHAR and OT_REG rather than an expression. */
3154 end = mips_parse_base_start (s);
3155 if (end)
3156 return end;
3157
3158 /* Handle other characters that end up as OT_CHARs. */
3159 if (*s == ')' || *s == ',')
3160 {
3161 token.u.ch = *s;
3162 mips_add_token (&token, OT_CHAR);
3163 ++s;
3164 return s;
3165 }
3166
3167 /* Handle tokens that start with a register. */
3168 if (mips_parse_register (&s, &regno1, &channels))
3169 {
3170 if (channels)
3171 {
3172 /* A register and a VU0 channel suffix. */
3173 token.u.regno = regno1;
3174 mips_add_token (&token, OT_REG);
3175
3176 token.u.channels = channels;
3177 mips_add_token (&token, OT_CHANNELS);
3178 return s;
3179 }
3180
3181 SKIP_SPACE_TABS (s);
3182 if (*s == '-')
3183 {
3184 /* A register range. */
3185 ++s;
3186 SKIP_SPACE_TABS (s);
3187 if (!mips_parse_register (&s, &regno2, NULL))
3188 {
3189 set_insn_error (0, _("invalid register range"));
3190 return 0;
3191 }
3192
3193 token.u.reg_range.regno1 = regno1;
3194 token.u.reg_range.regno2 = regno2;
3195 mips_add_token (&token, OT_REG_RANGE);
3196 return s;
3197 }
3198
3199 /* Add the register itself. */
3200 token.u.regno = regno1;
3201 mips_add_token (&token, OT_REG);
3202
3203 /* Check for a vector index. */
3204 if (*s == '[')
3205 {
3206 ++s;
3207 SKIP_SPACE_TABS (s);
3208 if (mips_parse_register (&s, &token.u.regno, NULL))
3209 mips_add_token (&token, OT_REG_INDEX);
3210 else
3211 {
3212 expressionS element;
3213
3214 my_getExpression (&element, s);
3215 if (element.X_op != O_constant)
3216 {
3217 set_insn_error (0, _("vector element must be constant"));
3218 return 0;
3219 }
3220 s = expr_end;
3221 token.u.index = element.X_add_number;
3222 mips_add_token (&token, OT_INTEGER_INDEX);
3223 }
3224 SKIP_SPACE_TABS (s);
3225 if (*s != ']')
3226 {
3227 set_insn_error (0, _("missing `]'"));
3228 return 0;
3229 }
3230 ++s;
3231 }
3232 return s;
3233 }
3234
3235 if (float_format)
3236 {
3237 /* First try to treat expressions as floats. */
3238 save_in = input_line_pointer;
3239 input_line_pointer = s;
3240 err = md_atof (float_format, (char *) token.u.flt.data,
3241 &token.u.flt.length);
3242 end = input_line_pointer;
3243 input_line_pointer = save_in;
3244 if (err && *err)
3245 {
3246 set_insn_error (0, err);
3247 return 0;
3248 }
3249 if (s != end)
3250 {
3251 mips_add_token (&token, OT_FLOAT);
3252 return end;
3253 }
3254 }
3255
3256 /* Treat everything else as an integer expression. */
3257 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3258 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3259 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3260 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3261 s = expr_end;
3262 mips_add_token (&token, OT_INTEGER);
3263 return s;
3264 }
3265
3266 /* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3267 if expressions should be treated as 32-bit floating-point constants,
3268 'd' if they should be treated as 64-bit floating-point constants,
3269 or 0 if they should be treated as integer expressions (the usual case).
3270
3271 Return a list of tokens on success, otherwise return 0. The caller
3272 must obstack_free the list after use. */
3273
3274 static struct mips_operand_token *
3275 mips_parse_arguments (char *s, char float_format)
3276 {
3277 struct mips_operand_token token;
3278
3279 SKIP_SPACE_TABS (s);
3280 while (*s)
3281 {
3282 s = mips_parse_argument_token (s, float_format);
3283 if (!s)
3284 {
3285 obstack_free (&mips_operand_tokens,
3286 obstack_finish (&mips_operand_tokens));
3287 return 0;
3288 }
3289 SKIP_SPACE_TABS (s);
3290 }
3291 mips_add_token (&token, OT_END);
3292 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
3293 }
3294
3295 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3296 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
3297
3298 static bfd_boolean
3299 is_opcode_valid (const struct mips_opcode *mo)
3300 {
3301 int isa = mips_opts.isa;
3302 int ase = mips_opts.ase;
3303 int fp_s, fp_d;
3304 unsigned int i;
3305
3306 if (ISA_HAS_64BIT_REGS (isa))
3307 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3308 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3309 ase |= mips_ases[i].flags64;
3310
3311 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
3312 return FALSE;
3313
3314 /* Check whether the instruction or macro requires single-precision or
3315 double-precision floating-point support. Note that this information is
3316 stored differently in the opcode table for insns and macros. */
3317 if (mo->pinfo == INSN_MACRO)
3318 {
3319 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3320 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3321 }
3322 else
3323 {
3324 fp_s = mo->pinfo & FP_S;
3325 fp_d = mo->pinfo & FP_D;
3326 }
3327
3328 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3329 return FALSE;
3330
3331 if (fp_s && mips_opts.soft_float)
3332 return FALSE;
3333
3334 return TRUE;
3335 }
3336
3337 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
3338 selected ISA and architecture. */
3339
3340 static bfd_boolean
3341 is_opcode_valid_16 (const struct mips_opcode *mo)
3342 {
3343 int isa = mips_opts.isa;
3344 int ase = mips_opts.ase;
3345 unsigned int i;
3346
3347 if (ISA_HAS_64BIT_REGS (isa))
3348 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3349 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3350 ase |= mips_ases[i].flags64;
3351
3352 return opcode_is_member (mo, isa, ase, mips_opts.arch);
3353 }
3354
3355 /* Return TRUE if the size of the microMIPS opcode MO matches one
3356 explicitly requested. Always TRUE in the standard MIPS mode.
3357 Use is_size_valid_16 for MIPS16 opcodes. */
3358
3359 static bfd_boolean
3360 is_size_valid (const struct mips_opcode *mo)
3361 {
3362 if (!mips_opts.micromips)
3363 return TRUE;
3364
3365 if (mips_opts.insn32)
3366 {
3367 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3368 return FALSE;
3369 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3370 return FALSE;
3371 }
3372 if (!forced_insn_length)
3373 return TRUE;
3374 if (mo->pinfo == INSN_MACRO)
3375 return FALSE;
3376 return forced_insn_length == micromips_insn_length (mo);
3377 }
3378
3379 /* Return TRUE if the size of the MIPS16 opcode MO matches one
3380 explicitly requested. */
3381
3382 static bfd_boolean
3383 is_size_valid_16 (const struct mips_opcode *mo)
3384 {
3385 if (!forced_insn_length)
3386 return TRUE;
3387 if (mo->pinfo == INSN_MACRO)
3388 return FALSE;
3389 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3390 return FALSE;
3391 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3392 return FALSE;
3393 return TRUE;
3394 }
3395
3396 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
3397 of the preceding instruction. Always TRUE in the standard MIPS mode.
3398
3399 We don't accept macros in 16-bit delay slots to avoid a case where
3400 a macro expansion fails because it relies on a preceding 32-bit real
3401 instruction to have matched and does not handle the operands correctly.
3402 The only macros that may expand to 16-bit instructions are JAL that
3403 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3404 and BGT (that likewise cannot be placed in a delay slot) that decay to
3405 a NOP. In all these cases the macros precede any corresponding real
3406 instruction definitions in the opcode table, so they will match in the
3407 second pass where the size of the delay slot is ignored and therefore
3408 produce correct code. */
3409
3410 static bfd_boolean
3411 is_delay_slot_valid (const struct mips_opcode *mo)
3412 {
3413 if (!mips_opts.micromips)
3414 return TRUE;
3415
3416 if (mo->pinfo == INSN_MACRO)
3417 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
3418 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3419 && micromips_insn_length (mo) != 4)
3420 return FALSE;
3421 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3422 && micromips_insn_length (mo) != 2)
3423 return FALSE;
3424
3425 return TRUE;
3426 }
3427
3428 /* For consistency checking, verify that all bits of OPCODE are specified
3429 either by the match/mask part of the instruction definition, or by the
3430 operand list. Also build up a list of operands in OPERANDS.
3431
3432 INSN_BITS says which bits of the instruction are significant.
3433 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3434 provides the mips_operand description of each operand. DECODE_OPERAND
3435 is null for MIPS16 instructions. */
3436
3437 static int
3438 validate_mips_insn (const struct mips_opcode *opcode,
3439 unsigned long insn_bits,
3440 const struct mips_operand *(*decode_operand) (const char *),
3441 struct mips_operand_array *operands)
3442 {
3443 const char *s;
3444 unsigned long used_bits, doubled, undefined, opno, mask;
3445 const struct mips_operand *operand;
3446
3447 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3448 if ((mask & opcode->match) != opcode->match)
3449 {
3450 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3451 opcode->name, opcode->args);
3452 return 0;
3453 }
3454 used_bits = 0;
3455 opno = 0;
3456 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3457 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
3458 for (s = opcode->args; *s; ++s)
3459 switch (*s)
3460 {
3461 case ',':
3462 case '(':
3463 case ')':
3464 break;
3465
3466 case '#':
3467 s++;
3468 break;
3469
3470 default:
3471 if (!decode_operand)
3472 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
3473 else
3474 operand = decode_operand (s);
3475 if (!operand && opcode->pinfo != INSN_MACRO)
3476 {
3477 as_bad (_("internal: unknown operand type: %s %s"),
3478 opcode->name, opcode->args);
3479 return 0;
3480 }
3481 gas_assert (opno < MAX_OPERANDS);
3482 operands->operand[opno] = operand;
3483 if (!decode_operand && operand
3484 && operand->type == OP_INT && operand->lsb == 0
3485 && mips_opcode_32bit_p (opcode))
3486 used_bits |= mips16_immed_extend (-1, operand->size);
3487 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
3488 {
3489 used_bits = mips_insert_operand (operand, used_bits, -1);
3490 if (operand->type == OP_MDMX_IMM_REG)
3491 /* Bit 5 is the format selector (OB vs QH). The opcode table
3492 has separate entries for each format. */
3493 used_bits &= ~(1 << (operand->lsb + 5));
3494 if (operand->type == OP_ENTRY_EXIT_LIST)
3495 used_bits &= ~(mask & 0x700);
3496 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3497 operand field that cannot be fully described with LSB/SIZE. */
3498 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3499 used_bits &= ~0x6000;
3500 }
3501 /* Skip prefix characters. */
3502 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
3503 ++s;
3504 opno += 1;
3505 break;
3506 }
3507 doubled = used_bits & mask & insn_bits;
3508 if (doubled)
3509 {
3510 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3511 " %s %s"), doubled, opcode->name, opcode->args);
3512 return 0;
3513 }
3514 used_bits |= mask;
3515 undefined = ~used_bits & insn_bits;
3516 if (opcode->pinfo != INSN_MACRO && undefined)
3517 {
3518 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3519 undefined, opcode->name, opcode->args);
3520 return 0;
3521 }
3522 used_bits &= ~insn_bits;
3523 if (used_bits)
3524 {
3525 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3526 used_bits, opcode->name, opcode->args);
3527 return 0;
3528 }
3529 return 1;
3530 }
3531
3532 /* The MIPS16 version of validate_mips_insn. */
3533
3534 static int
3535 validate_mips16_insn (const struct mips_opcode *opcode,
3536 struct mips_operand_array *operands)
3537 {
3538 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
3539
3540 return validate_mips_insn (opcode, insn_bits, 0, operands);
3541 }
3542
3543 /* The microMIPS version of validate_mips_insn. */
3544
3545 static int
3546 validate_micromips_insn (const struct mips_opcode *opc,
3547 struct mips_operand_array *operands)
3548 {
3549 unsigned long insn_bits;
3550 unsigned long major;
3551 unsigned int length;
3552
3553 if (opc->pinfo == INSN_MACRO)
3554 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3555 operands);
3556
3557 length = micromips_insn_length (opc);
3558 if (length != 2 && length != 4)
3559 {
3560 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
3561 "%s %s"), length, opc->name, opc->args);
3562 return 0;
3563 }
3564 major = opc->match >> (10 + 8 * (length - 2));
3565 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3566 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3567 {
3568 as_bad (_("internal error: bad microMIPS opcode "
3569 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3570 return 0;
3571 }
3572
3573 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3574 insn_bits = 1 << 4 * length;
3575 insn_bits <<= 4 * length;
3576 insn_bits -= 1;
3577 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3578 operands);
3579 }
3580
3581 /* This function is called once, at assembler startup time. It should set up
3582 all the tables, etc. that the MD part of the assembler will need. */
3583
3584 void
3585 md_begin (void)
3586 {
3587 const char *retval = NULL;
3588 int i = 0;
3589 int broken = 0;
3590
3591 if (mips_pic != NO_PIC)
3592 {
3593 if (g_switch_seen && g_switch_value != 0)
3594 as_bad (_("-G may not be used in position-independent code"));
3595 g_switch_value = 0;
3596 }
3597 else if (mips_abicalls)
3598 {
3599 if (g_switch_seen && g_switch_value != 0)
3600 as_bad (_("-G may not be used with abicalls"));
3601 g_switch_value = 0;
3602 }
3603
3604 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3605 as_warn (_("could not set architecture and machine"));
3606
3607 op_hash = hash_new ();
3608
3609 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
3610 for (i = 0; i < NUMOPCODES;)
3611 {
3612 const char *name = mips_opcodes[i].name;
3613
3614 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
3615 if (retval != NULL)
3616 {
3617 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3618 mips_opcodes[i].name, retval);
3619 /* Probably a memory allocation problem? Give up now. */
3620 as_fatal (_("broken assembler, no assembly attempted"));
3621 }
3622 do
3623 {
3624 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3625 decode_mips_operand, &mips_operands[i]))
3626 broken = 1;
3627 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3628 {
3629 create_insn (&nop_insn, mips_opcodes + i);
3630 if (mips_fix_loongson2f_nop)
3631 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3632 nop_insn.fixed_p = 1;
3633 }
3634 ++i;
3635 }
3636 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3637 }
3638
3639 mips16_op_hash = hash_new ();
3640 mips16_operands = XCNEWVEC (struct mips_operand_array,
3641 bfd_mips16_num_opcodes);
3642
3643 i = 0;
3644 while (i < bfd_mips16_num_opcodes)
3645 {
3646 const char *name = mips16_opcodes[i].name;
3647
3648 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
3649 if (retval != NULL)
3650 as_fatal (_("internal: can't hash `%s': %s"),
3651 mips16_opcodes[i].name, retval);
3652 do
3653 {
3654 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3655 broken = 1;
3656 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3657 {
3658 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3659 mips16_nop_insn.fixed_p = 1;
3660 }
3661 ++i;
3662 }
3663 while (i < bfd_mips16_num_opcodes
3664 && strcmp (mips16_opcodes[i].name, name) == 0);
3665 }
3666
3667 micromips_op_hash = hash_new ();
3668 micromips_operands = XCNEWVEC (struct mips_operand_array,
3669 bfd_micromips_num_opcodes);
3670
3671 i = 0;
3672 while (i < bfd_micromips_num_opcodes)
3673 {
3674 const char *name = micromips_opcodes[i].name;
3675
3676 retval = hash_insert (micromips_op_hash, name,
3677 (void *) &micromips_opcodes[i]);
3678 if (retval != NULL)
3679 as_fatal (_("internal: can't hash `%s': %s"),
3680 micromips_opcodes[i].name, retval);
3681 do
3682 {
3683 struct mips_cl_insn *micromips_nop_insn;
3684
3685 if (!validate_micromips_insn (&micromips_opcodes[i],
3686 &micromips_operands[i]))
3687 broken = 1;
3688
3689 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3690 {
3691 if (micromips_insn_length (micromips_opcodes + i) == 2)
3692 micromips_nop_insn = &micromips_nop16_insn;
3693 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3694 micromips_nop_insn = &micromips_nop32_insn;
3695 else
3696 continue;
3697
3698 if (micromips_nop_insn->insn_mo == NULL
3699 && strcmp (name, "nop") == 0)
3700 {
3701 create_insn (micromips_nop_insn, micromips_opcodes + i);
3702 micromips_nop_insn->fixed_p = 1;
3703 }
3704 }
3705 }
3706 while (++i < bfd_micromips_num_opcodes
3707 && strcmp (micromips_opcodes[i].name, name) == 0);
3708 }
3709
3710 if (broken)
3711 as_fatal (_("broken assembler, no assembly attempted"));
3712
3713 /* We add all the general register names to the symbol table. This
3714 helps us detect invalid uses of them. */
3715 for (i = 0; reg_names[i].name; i++)
3716 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
3717 reg_names[i].num, /* & RNUM_MASK, */
3718 &zero_address_frag));
3719 if (HAVE_NEWABI)
3720 for (i = 0; reg_names_n32n64[i].name; i++)
3721 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
3722 reg_names_n32n64[i].num, /* & RNUM_MASK, */
3723 &zero_address_frag));
3724 else
3725 for (i = 0; reg_names_o32[i].name; i++)
3726 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
3727 reg_names_o32[i].num, /* & RNUM_MASK, */
3728 &zero_address_frag));
3729
3730 for (i = 0; i < 32; i++)
3731 {
3732 char regname[6];
3733
3734 /* R5900 VU0 floating-point register. */
3735 sprintf (regname, "$vf%d", i);
3736 symbol_table_insert (symbol_new (regname, reg_section,
3737 RTYPE_VF | i, &zero_address_frag));
3738
3739 /* R5900 VU0 integer register. */
3740 sprintf (regname, "$vi%d", i);
3741 symbol_table_insert (symbol_new (regname, reg_section,
3742 RTYPE_VI | i, &zero_address_frag));
3743
3744 /* MSA register. */
3745 sprintf (regname, "$w%d", i);
3746 symbol_table_insert (symbol_new (regname, reg_section,
3747 RTYPE_MSA | i, &zero_address_frag));
3748 }
3749
3750 obstack_init (&mips_operand_tokens);
3751
3752 mips_no_prev_insn ();
3753
3754 mips_gprmask = 0;
3755 mips_cprmask[0] = 0;
3756 mips_cprmask[1] = 0;
3757 mips_cprmask[2] = 0;
3758 mips_cprmask[3] = 0;
3759
3760 /* set the default alignment for the text section (2**2) */
3761 record_alignment (text_section, 2);
3762
3763 bfd_set_gp_size (stdoutput, g_switch_value);
3764
3765 /* On a native system other than VxWorks, sections must be aligned
3766 to 16 byte boundaries. When configured for an embedded ELF
3767 target, we don't bother. */
3768 if (strncmp (TARGET_OS, "elf", 3) != 0
3769 && strncmp (TARGET_OS, "vxworks", 7) != 0)
3770 {
3771 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3772 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3773 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3774 }
3775
3776 /* Create a .reginfo section for register masks and a .mdebug
3777 section for debugging information. */
3778 {
3779 segT seg;
3780 subsegT subseg;
3781 flagword flags;
3782 segT sec;
3783
3784 seg = now_seg;
3785 subseg = now_subseg;
3786
3787 /* The ABI says this section should be loaded so that the
3788 running program can access it. However, we don't load it
3789 if we are configured for an embedded target */
3790 flags = SEC_READONLY | SEC_DATA;
3791 if (strncmp (TARGET_OS, "elf", 3) != 0)
3792 flags |= SEC_ALLOC | SEC_LOAD;
3793
3794 if (mips_abi != N64_ABI)
3795 {
3796 sec = subseg_new (".reginfo", (subsegT) 0);
3797
3798 bfd_set_section_flags (stdoutput, sec, flags);
3799 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
3800
3801 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3802 }
3803 else
3804 {
3805 /* The 64-bit ABI uses a .MIPS.options section rather than
3806 .reginfo section. */
3807 sec = subseg_new (".MIPS.options", (subsegT) 0);
3808 bfd_set_section_flags (stdoutput, sec, flags);
3809 bfd_set_section_alignment (stdoutput, sec, 3);
3810
3811 /* Set up the option header. */
3812 {
3813 Elf_Internal_Options opthdr;
3814 char *f;
3815
3816 opthdr.kind = ODK_REGINFO;
3817 opthdr.size = (sizeof (Elf_External_Options)
3818 + sizeof (Elf64_External_RegInfo));
3819 opthdr.section = 0;
3820 opthdr.info = 0;
3821 f = frag_more (sizeof (Elf_External_Options));
3822 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3823 (Elf_External_Options *) f);
3824
3825 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3826 }
3827 }
3828
3829 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3830 bfd_set_section_flags (stdoutput, sec,
3831 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3832 bfd_set_section_alignment (stdoutput, sec, 3);
3833 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3834
3835 if (ECOFF_DEBUGGING)
3836 {
3837 sec = subseg_new (".mdebug", (subsegT) 0);
3838 (void) bfd_set_section_flags (stdoutput, sec,
3839 SEC_HAS_CONTENTS | SEC_READONLY);
3840 (void) bfd_set_section_alignment (stdoutput, sec, 2);
3841 }
3842 else if (mips_flag_pdr)
3843 {
3844 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3845 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3846 SEC_READONLY | SEC_RELOC
3847 | SEC_DEBUGGING);
3848 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3849 }
3850
3851 subseg_set (seg, subseg);
3852 }
3853
3854 if (mips_fix_vr4120)
3855 init_vr4120_conflicts ();
3856 }
3857
3858 static inline void
3859 fpabi_incompatible_with (int fpabi, const char *what)
3860 {
3861 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3862 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3863 }
3864
3865 static inline void
3866 fpabi_requires (int fpabi, const char *what)
3867 {
3868 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3869 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3870 }
3871
3872 /* Check -mabi and register sizes against the specified FP ABI. */
3873 static void
3874 check_fpabi (int fpabi)
3875 {
3876 switch (fpabi)
3877 {
3878 case Val_GNU_MIPS_ABI_FP_DOUBLE:
3879 if (file_mips_opts.soft_float)
3880 fpabi_incompatible_with (fpabi, "softfloat");
3881 else if (file_mips_opts.single_float)
3882 fpabi_incompatible_with (fpabi, "singlefloat");
3883 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3884 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3885 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3886 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
3887 break;
3888
3889 case Val_GNU_MIPS_ABI_FP_XX:
3890 if (mips_abi != O32_ABI)
3891 fpabi_requires (fpabi, "-mabi=32");
3892 else if (file_mips_opts.soft_float)
3893 fpabi_incompatible_with (fpabi, "softfloat");
3894 else if (file_mips_opts.single_float)
3895 fpabi_incompatible_with (fpabi, "singlefloat");
3896 else if (file_mips_opts.fp != 0)
3897 fpabi_requires (fpabi, "fp=xx");
3898 break;
3899
3900 case Val_GNU_MIPS_ABI_FP_64A:
3901 case Val_GNU_MIPS_ABI_FP_64:
3902 if (mips_abi != O32_ABI)
3903 fpabi_requires (fpabi, "-mabi=32");
3904 else if (file_mips_opts.soft_float)
3905 fpabi_incompatible_with (fpabi, "softfloat");
3906 else if (file_mips_opts.single_float)
3907 fpabi_incompatible_with (fpabi, "singlefloat");
3908 else if (file_mips_opts.fp != 64)
3909 fpabi_requires (fpabi, "fp=64");
3910 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3911 fpabi_incompatible_with (fpabi, "nooddspreg");
3912 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3913 fpabi_requires (fpabi, "nooddspreg");
3914 break;
3915
3916 case Val_GNU_MIPS_ABI_FP_SINGLE:
3917 if (file_mips_opts.soft_float)
3918 fpabi_incompatible_with (fpabi, "softfloat");
3919 else if (!file_mips_opts.single_float)
3920 fpabi_requires (fpabi, "singlefloat");
3921 break;
3922
3923 case Val_GNU_MIPS_ABI_FP_SOFT:
3924 if (!file_mips_opts.soft_float)
3925 fpabi_requires (fpabi, "softfloat");
3926 break;
3927
3928 case Val_GNU_MIPS_ABI_FP_OLD_64:
3929 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3930 Tag_GNU_MIPS_ABI_FP, fpabi);
3931 break;
3932
3933 case Val_GNU_MIPS_ABI_FP_NAN2008:
3934 /* Silently ignore compatibility value. */
3935 break;
3936
3937 default:
3938 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3939 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3940 break;
3941 }
3942 }
3943
3944 /* Perform consistency checks on the current options. */
3945
3946 static void
3947 mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3948 {
3949 /* Check the size of integer registers agrees with the ABI and ISA. */
3950 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3951 as_bad (_("`gp=64' used with a 32-bit processor"));
3952 else if (abi_checks
3953 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3954 as_bad (_("`gp=32' used with a 64-bit ABI"));
3955 else if (abi_checks
3956 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3957 as_bad (_("`gp=64' used with a 32-bit ABI"));
3958
3959 /* Check the size of the float registers agrees with the ABI and ISA. */
3960 switch (opts->fp)
3961 {
3962 case 0:
3963 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
3964 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
3965 else if (opts->single_float == 1)
3966 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
3967 break;
3968 case 64:
3969 if (!ISA_HAS_64BIT_FPRS (opts->isa))
3970 as_bad (_("`fp=64' used with a 32-bit fpu"));
3971 else if (abi_checks
3972 && ABI_NEEDS_32BIT_REGS (mips_abi)
3973 && !ISA_HAS_MXHC1 (opts->isa))
3974 as_warn (_("`fp=64' used with a 32-bit ABI"));
3975 break;
3976 case 32:
3977 if (abi_checks
3978 && ABI_NEEDS_64BIT_REGS (mips_abi))
3979 as_warn (_("`fp=32' used with a 64-bit ABI"));
3980 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
3981 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
3982 break;
3983 default:
3984 as_bad (_("Unknown size of floating point registers"));
3985 break;
3986 }
3987
3988 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
3989 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
3990
3991 if (opts->micromips == 1 && opts->mips16 == 1)
3992 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
3993 else if (ISA_IS_R6 (opts->isa)
3994 && (opts->micromips == 1
3995 || opts->mips16 == 1))
3996 as_fatal (_("`%s' cannot be used with `%s'"),
3997 opts->micromips ? "micromips" : "mips16",
3998 mips_cpu_info_from_isa (opts->isa)->name);
3999
4000 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4001 as_fatal (_("branch relaxation is not supported in `%s'"),
4002 mips_cpu_info_from_isa (opts->isa)->name);
4003 }
4004
4005 /* Perform consistency checks on the module level options exactly once.
4006 This is a deferred check that happens:
4007 at the first .set directive
4008 or, at the first pseudo op that generates code (inc .dc.a)
4009 or, at the first instruction
4010 or, at the end. */
4011
4012 static void
4013 file_mips_check_options (void)
4014 {
4015 const struct mips_cpu_info *arch_info = 0;
4016
4017 if (file_mips_opts_checked)
4018 return;
4019
4020 /* The following code determines the register size.
4021 Similar code was added to GCC 3.3 (see override_options() in
4022 config/mips/mips.c). The GAS and GCC code should be kept in sync
4023 as much as possible. */
4024
4025 if (file_mips_opts.gp < 0)
4026 {
4027 /* Infer the integer register size from the ABI and processor.
4028 Restrict ourselves to 32-bit registers if that's all the
4029 processor has, or if the ABI cannot handle 64-bit registers. */
4030 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4031 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4032 ? 32 : 64;
4033 }
4034
4035 if (file_mips_opts.fp < 0)
4036 {
4037 /* No user specified float register size.
4038 ??? GAS treats single-float processors as though they had 64-bit
4039 float registers (although it complains when double-precision
4040 instructions are used). As things stand, saying they have 32-bit
4041 registers would lead to spurious "register must be even" messages.
4042 So here we assume float registers are never smaller than the
4043 integer ones. */
4044 if (file_mips_opts.gp == 64)
4045 /* 64-bit integer registers implies 64-bit float registers. */
4046 file_mips_opts.fp = 64;
4047 else if ((file_mips_opts.ase & FP64_ASES)
4048 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4049 /* Handle ASEs that require 64-bit float registers, if possible. */
4050 file_mips_opts.fp = 64;
4051 else if (ISA_IS_R6 (mips_opts.isa))
4052 /* R6 implies 64-bit float registers. */
4053 file_mips_opts.fp = 64;
4054 else
4055 /* 32-bit float registers. */
4056 file_mips_opts.fp = 32;
4057 }
4058
4059 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
4060
4061 /* Disable operations on odd-numbered floating-point registers by default
4062 when using the FPXX ABI. */
4063 if (file_mips_opts.oddspreg < 0)
4064 {
4065 if (file_mips_opts.fp == 0)
4066 file_mips_opts.oddspreg = 0;
4067 else
4068 file_mips_opts.oddspreg = 1;
4069 }
4070
4071 /* End of GCC-shared inference code. */
4072
4073 /* This flag is set when we have a 64-bit capable CPU but use only
4074 32-bit wide registers. Note that EABI does not use it. */
4075 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4076 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4077 || mips_abi == O32_ABI))
4078 mips_32bitmode = 1;
4079
4080 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4081 as_bad (_("trap exception not supported at ISA 1"));
4082
4083 /* If the selected architecture includes support for ASEs, enable
4084 generation of code for them. */
4085 if (file_mips_opts.mips16 == -1)
4086 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4087 if (file_mips_opts.micromips == -1)
4088 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4089 ? 1 : 0;
4090
4091 if (mips_nan2008 == -1)
4092 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4093 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4094 as_fatal (_("`%s' does not support legacy NaN"),
4095 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4096
4097 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4098 being selected implicitly. */
4099 if (file_mips_opts.fp != 64)
4100 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4101
4102 /* If the user didn't explicitly select or deselect a particular ASE,
4103 use the default setting for the CPU. */
4104 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4105
4106 /* Set up the current options. These may change throughout assembly. */
4107 mips_opts = file_mips_opts;
4108
4109 mips_check_isa_supports_ases ();
4110 mips_check_options (&file_mips_opts, TRUE);
4111 file_mips_opts_checked = TRUE;
4112
4113 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4114 as_warn (_("could not set architecture and machine"));
4115 }
4116
4117 void
4118 md_assemble (char *str)
4119 {
4120 struct mips_cl_insn insn;
4121 bfd_reloc_code_real_type unused_reloc[3]
4122 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
4123
4124 file_mips_check_options ();
4125
4126 imm_expr.X_op = O_absent;
4127 offset_expr.X_op = O_absent;
4128 offset_reloc[0] = BFD_RELOC_UNUSED;
4129 offset_reloc[1] = BFD_RELOC_UNUSED;
4130 offset_reloc[2] = BFD_RELOC_UNUSED;
4131
4132 mips_mark_labels ();
4133 mips_assembling_insn = TRUE;
4134 clear_insn_error ();
4135
4136 if (mips_opts.mips16)
4137 mips16_ip (str, &insn);
4138 else
4139 {
4140 mips_ip (str, &insn);
4141 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4142 str, insn.insn_opcode));
4143 }
4144
4145 if (insn_error.msg)
4146 report_insn_error (str);
4147 else if (insn.insn_mo->pinfo == INSN_MACRO)
4148 {
4149 macro_start ();
4150 if (mips_opts.mips16)
4151 mips16_macro (&insn);
4152 else
4153 macro (&insn, str);
4154 macro_end ();
4155 }
4156 else
4157 {
4158 if (offset_expr.X_op != O_absent)
4159 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
4160 else
4161 append_insn (&insn, NULL, unused_reloc, FALSE);
4162 }
4163
4164 mips_assembling_insn = FALSE;
4165 }
4166
4167 /* Convenience functions for abstracting away the differences between
4168 MIPS16 and non-MIPS16 relocations. */
4169
4170 static inline bfd_boolean
4171 mips16_reloc_p (bfd_reloc_code_real_type reloc)
4172 {
4173 switch (reloc)
4174 {
4175 case BFD_RELOC_MIPS16_JMP:
4176 case BFD_RELOC_MIPS16_GPREL:
4177 case BFD_RELOC_MIPS16_GOT16:
4178 case BFD_RELOC_MIPS16_CALL16:
4179 case BFD_RELOC_MIPS16_HI16_S:
4180 case BFD_RELOC_MIPS16_HI16:
4181 case BFD_RELOC_MIPS16_LO16:
4182 case BFD_RELOC_MIPS16_16_PCREL_S1:
4183 return TRUE;
4184
4185 default:
4186 return FALSE;
4187 }
4188 }
4189
4190 static inline bfd_boolean
4191 micromips_reloc_p (bfd_reloc_code_real_type reloc)
4192 {
4193 switch (reloc)
4194 {
4195 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4196 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4197 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4198 case BFD_RELOC_MICROMIPS_GPREL16:
4199 case BFD_RELOC_MICROMIPS_JMP:
4200 case BFD_RELOC_MICROMIPS_HI16:
4201 case BFD_RELOC_MICROMIPS_HI16_S:
4202 case BFD_RELOC_MICROMIPS_LO16:
4203 case BFD_RELOC_MICROMIPS_LITERAL:
4204 case BFD_RELOC_MICROMIPS_GOT16:
4205 case BFD_RELOC_MICROMIPS_CALL16:
4206 case BFD_RELOC_MICROMIPS_GOT_HI16:
4207 case BFD_RELOC_MICROMIPS_GOT_LO16:
4208 case BFD_RELOC_MICROMIPS_CALL_HI16:
4209 case BFD_RELOC_MICROMIPS_CALL_LO16:
4210 case BFD_RELOC_MICROMIPS_SUB:
4211 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4212 case BFD_RELOC_MICROMIPS_GOT_OFST:
4213 case BFD_RELOC_MICROMIPS_GOT_DISP:
4214 case BFD_RELOC_MICROMIPS_HIGHEST:
4215 case BFD_RELOC_MICROMIPS_HIGHER:
4216 case BFD_RELOC_MICROMIPS_SCN_DISP:
4217 case BFD_RELOC_MICROMIPS_JALR:
4218 return TRUE;
4219
4220 default:
4221 return FALSE;
4222 }
4223 }
4224
4225 static inline bfd_boolean
4226 jmp_reloc_p (bfd_reloc_code_real_type reloc)
4227 {
4228 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4229 }
4230
4231 static inline bfd_boolean
4232 b_reloc_p (bfd_reloc_code_real_type reloc)
4233 {
4234 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4235 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4236 || reloc == BFD_RELOC_16_PCREL_S2
4237 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
4238 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4239 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4240 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4241 }
4242
4243 static inline bfd_boolean
4244 got16_reloc_p (bfd_reloc_code_real_type reloc)
4245 {
4246 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
4247 || reloc == BFD_RELOC_MICROMIPS_GOT16);
4248 }
4249
4250 static inline bfd_boolean
4251 hi16_reloc_p (bfd_reloc_code_real_type reloc)
4252 {
4253 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
4254 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
4255 }
4256
4257 static inline bfd_boolean
4258 lo16_reloc_p (bfd_reloc_code_real_type reloc)
4259 {
4260 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
4261 || reloc == BFD_RELOC_MICROMIPS_LO16);
4262 }
4263
4264 static inline bfd_boolean
4265 jalr_reloc_p (bfd_reloc_code_real_type reloc)
4266 {
4267 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
4268 }
4269
4270 static inline bfd_boolean
4271 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4272 {
4273 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4274 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4275 }
4276
4277 /* Return true if RELOC is a PC-relative relocation that does not have
4278 full address range. */
4279
4280 static inline bfd_boolean
4281 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4282 {
4283 switch (reloc)
4284 {
4285 case BFD_RELOC_16_PCREL_S2:
4286 case BFD_RELOC_MIPS16_16_PCREL_S1:
4287 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4288 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4289 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4290 case BFD_RELOC_MIPS_21_PCREL_S2:
4291 case BFD_RELOC_MIPS_26_PCREL_S2:
4292 case BFD_RELOC_MIPS_18_PCREL_S3:
4293 case BFD_RELOC_MIPS_19_PCREL_S2:
4294 return TRUE;
4295
4296 case BFD_RELOC_32_PCREL:
4297 case BFD_RELOC_HI16_S_PCREL:
4298 case BFD_RELOC_LO16_PCREL:
4299 return HAVE_64BIT_ADDRESSES;
4300
4301 default:
4302 return FALSE;
4303 }
4304 }
4305
4306 /* Return true if the given relocation might need a matching %lo().
4307 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4308 need a matching %lo() when applied to local symbols. */
4309
4310 static inline bfd_boolean
4311 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
4312 {
4313 return (HAVE_IN_PLACE_ADDENDS
4314 && (hi16_reloc_p (reloc)
4315 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4316 all GOT16 relocations evaluate to "G". */
4317 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4318 }
4319
4320 /* Return the type of %lo() reloc needed by RELOC, given that
4321 reloc_needs_lo_p. */
4322
4323 static inline bfd_reloc_code_real_type
4324 matching_lo_reloc (bfd_reloc_code_real_type reloc)
4325 {
4326 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4327 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4328 : BFD_RELOC_LO16));
4329 }
4330
4331 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
4332 relocation. */
4333
4334 static inline bfd_boolean
4335 fixup_has_matching_lo_p (fixS *fixp)
4336 {
4337 return (fixp->fx_next != NULL
4338 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
4339 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4340 && fixp->fx_offset == fixp->fx_next->fx_offset);
4341 }
4342
4343 /* Move all labels in LABELS to the current insertion point. TEXT_P
4344 says whether the labels refer to text or data. */
4345
4346 static void
4347 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
4348 {
4349 struct insn_label_list *l;
4350 valueT val;
4351
4352 for (l = labels; l != NULL; l = l->next)
4353 {
4354 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
4355 symbol_set_frag (l->label, frag_now);
4356 val = (valueT) frag_now_fix ();
4357 /* MIPS16/microMIPS text labels are stored as odd. */
4358 if (text_p && HAVE_CODE_COMPRESSION)
4359 ++val;
4360 S_SET_VALUE (l->label, val);
4361 }
4362 }
4363
4364 /* Move all labels in insn_labels to the current insertion point
4365 and treat them as text labels. */
4366
4367 static void
4368 mips_move_text_labels (void)
4369 {
4370 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4371 }
4372
4373 /* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4374
4375 static bfd_boolean
4376 s_is_linkonce (symbolS *sym, segT from_seg)
4377 {
4378 bfd_boolean linkonce = FALSE;
4379 segT symseg = S_GET_SEGMENT (sym);
4380
4381 if (symseg != from_seg && !S_IS_LOCAL (sym))
4382 {
4383 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4384 linkonce = TRUE;
4385 /* The GNU toolchain uses an extension for ELF: a section
4386 beginning with the magic string .gnu.linkonce is a
4387 linkonce section. */
4388 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4389 sizeof ".gnu.linkonce" - 1) == 0)
4390 linkonce = TRUE;
4391 }
4392 return linkonce;
4393 }
4394
4395 /* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
4396 linker to handle them specially, such as generating jalx instructions
4397 when needed. We also make them odd for the duration of the assembly,
4398 in order to generate the right sort of code. We will make them even
4399 in the adjust_symtab routine, while leaving them marked. This is
4400 convenient for the debugger and the disassembler. The linker knows
4401 to make them odd again. */
4402
4403 static void
4404 mips_compressed_mark_label (symbolS *label)
4405 {
4406 gas_assert (HAVE_CODE_COMPRESSION);
4407
4408 if (mips_opts.mips16)
4409 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4410 else
4411 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
4412 if ((S_GET_VALUE (label) & 1) == 0
4413 /* Don't adjust the address if the label is global or weak, or
4414 in a link-once section, since we'll be emitting symbol reloc
4415 references to it which will be patched up by the linker, and
4416 the final value of the symbol may or may not be MIPS16/microMIPS. */
4417 && !S_IS_WEAK (label)
4418 && !S_IS_EXTERNAL (label)
4419 && !s_is_linkonce (label, now_seg))
4420 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4421 }
4422
4423 /* Mark preceding MIPS16 or microMIPS instruction labels. */
4424
4425 static void
4426 mips_compressed_mark_labels (void)
4427 {
4428 struct insn_label_list *l;
4429
4430 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4431 mips_compressed_mark_label (l->label);
4432 }
4433
4434 /* End the current frag. Make it a variant frag and record the
4435 relaxation info. */
4436
4437 static void
4438 relax_close_frag (void)
4439 {
4440 mips_macro_warning.first_frag = frag_now;
4441 frag_var (rs_machine_dependent, 0, 0,
4442 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4443 mips_pic != NO_PIC),
4444 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4445
4446 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4447 mips_relax.first_fixup = 0;
4448 }
4449
4450 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
4451 See the comment above RELAX_ENCODE for more details. */
4452
4453 static void
4454 relax_start (symbolS *symbol)
4455 {
4456 gas_assert (mips_relax.sequence == 0);
4457 mips_relax.sequence = 1;
4458 mips_relax.symbol = symbol;
4459 }
4460
4461 /* Start generating the second version of a relaxable sequence.
4462 See the comment above RELAX_ENCODE for more details. */
4463
4464 static void
4465 relax_switch (void)
4466 {
4467 gas_assert (mips_relax.sequence == 1);
4468 mips_relax.sequence = 2;
4469 }
4470
4471 /* End the current relaxable sequence. */
4472
4473 static void
4474 relax_end (void)
4475 {
4476 gas_assert (mips_relax.sequence == 2);
4477 relax_close_frag ();
4478 mips_relax.sequence = 0;
4479 }
4480
4481 /* Return true if IP is a delayed branch or jump. */
4482
4483 static inline bfd_boolean
4484 delayed_branch_p (const struct mips_cl_insn *ip)
4485 {
4486 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4487 | INSN_COND_BRANCH_DELAY
4488 | INSN_COND_BRANCH_LIKELY)) != 0;
4489 }
4490
4491 /* Return true if IP is a compact branch or jump. */
4492
4493 static inline bfd_boolean
4494 compact_branch_p (const struct mips_cl_insn *ip)
4495 {
4496 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4497 | INSN2_COND_BRANCH)) != 0;
4498 }
4499
4500 /* Return true if IP is an unconditional branch or jump. */
4501
4502 static inline bfd_boolean
4503 uncond_branch_p (const struct mips_cl_insn *ip)
4504 {
4505 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
4506 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
4507 }
4508
4509 /* Return true if IP is a branch-likely instruction. */
4510
4511 static inline bfd_boolean
4512 branch_likely_p (const struct mips_cl_insn *ip)
4513 {
4514 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4515 }
4516
4517 /* Return the type of nop that should be used to fill the delay slot
4518 of delayed branch IP. */
4519
4520 static struct mips_cl_insn *
4521 get_delay_slot_nop (const struct mips_cl_insn *ip)
4522 {
4523 if (mips_opts.micromips
4524 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4525 return &micromips_nop32_insn;
4526 return NOP_INSN;
4527 }
4528
4529 /* Return a mask that has bit N set if OPCODE reads the register(s)
4530 in operand N. */
4531
4532 static unsigned int
4533 insn_read_mask (const struct mips_opcode *opcode)
4534 {
4535 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4536 }
4537
4538 /* Return a mask that has bit N set if OPCODE writes to the register(s)
4539 in operand N. */
4540
4541 static unsigned int
4542 insn_write_mask (const struct mips_opcode *opcode)
4543 {
4544 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4545 }
4546
4547 /* Return a mask of the registers specified by operand OPERAND of INSN.
4548 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4549 is set. */
4550
4551 static unsigned int
4552 operand_reg_mask (const struct mips_cl_insn *insn,
4553 const struct mips_operand *operand,
4554 unsigned int type_mask)
4555 {
4556 unsigned int uval, vsel;
4557
4558 switch (operand->type)
4559 {
4560 case OP_INT:
4561 case OP_MAPPED_INT:
4562 case OP_MSB:
4563 case OP_PCREL:
4564 case OP_PERF_REG:
4565 case OP_ADDIUSP_INT:
4566 case OP_ENTRY_EXIT_LIST:
4567 case OP_REPEAT_DEST_REG:
4568 case OP_REPEAT_PREV_REG:
4569 case OP_PC:
4570 case OP_VU0_SUFFIX:
4571 case OP_VU0_MATCH_SUFFIX:
4572 case OP_IMM_INDEX:
4573 abort ();
4574
4575 case OP_REG28:
4576 return 1 << 28;
4577
4578 case OP_REG:
4579 case OP_OPTIONAL_REG:
4580 {
4581 const struct mips_reg_operand *reg_op;
4582
4583 reg_op = (const struct mips_reg_operand *) operand;
4584 if (!(type_mask & (1 << reg_op->reg_type)))
4585 return 0;
4586 uval = insn_extract_operand (insn, operand);
4587 return 1 << mips_decode_reg_operand (reg_op, uval);
4588 }
4589
4590 case OP_REG_PAIR:
4591 {
4592 const struct mips_reg_pair_operand *pair_op;
4593
4594 pair_op = (const struct mips_reg_pair_operand *) operand;
4595 if (!(type_mask & (1 << pair_op->reg_type)))
4596 return 0;
4597 uval = insn_extract_operand (insn, operand);
4598 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4599 }
4600
4601 case OP_CLO_CLZ_DEST:
4602 if (!(type_mask & (1 << OP_REG_GP)))
4603 return 0;
4604 uval = insn_extract_operand (insn, operand);
4605 return (1 << (uval & 31)) | (1 << (uval >> 5));
4606
4607 case OP_SAME_RS_RT:
4608 if (!(type_mask & (1 << OP_REG_GP)))
4609 return 0;
4610 uval = insn_extract_operand (insn, operand);
4611 gas_assert ((uval & 31) == (uval >> 5));
4612 return 1 << (uval & 31);
4613
4614 case OP_CHECK_PREV:
4615 case OP_NON_ZERO_REG:
4616 if (!(type_mask & (1 << OP_REG_GP)))
4617 return 0;
4618 uval = insn_extract_operand (insn, operand);
4619 return 1 << (uval & 31);
4620
4621 case OP_LWM_SWM_LIST:
4622 abort ();
4623
4624 case OP_SAVE_RESTORE_LIST:
4625 abort ();
4626
4627 case OP_MDMX_IMM_REG:
4628 if (!(type_mask & (1 << OP_REG_VEC)))
4629 return 0;
4630 uval = insn_extract_operand (insn, operand);
4631 vsel = uval >> 5;
4632 if ((vsel & 0x18) == 0x18)
4633 return 0;
4634 return 1 << (uval & 31);
4635
4636 case OP_REG_INDEX:
4637 if (!(type_mask & (1 << OP_REG_GP)))
4638 return 0;
4639 return 1 << insn_extract_operand (insn, operand);
4640 }
4641 abort ();
4642 }
4643
4644 /* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4645 where bit N of OPNO_MASK is set if operand N should be included.
4646 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4647 is set. */
4648
4649 static unsigned int
4650 insn_reg_mask (const struct mips_cl_insn *insn,
4651 unsigned int type_mask, unsigned int opno_mask)
4652 {
4653 unsigned int opno, reg_mask;
4654
4655 opno = 0;
4656 reg_mask = 0;
4657 while (opno_mask != 0)
4658 {
4659 if (opno_mask & 1)
4660 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4661 opno_mask >>= 1;
4662 opno += 1;
4663 }
4664 return reg_mask;
4665 }
4666
4667 /* Return the mask of core registers that IP reads. */
4668
4669 static unsigned int
4670 gpr_read_mask (const struct mips_cl_insn *ip)
4671 {
4672 unsigned long pinfo, pinfo2;
4673 unsigned int mask;
4674
4675 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4676 pinfo = ip->insn_mo->pinfo;
4677 pinfo2 = ip->insn_mo->pinfo2;
4678 if (pinfo & INSN_UDI)
4679 {
4680 /* UDI instructions have traditionally been assumed to read RS
4681 and RT. */
4682 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4683 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4684 }
4685 if (pinfo & INSN_READ_GPR_24)
4686 mask |= 1 << 24;
4687 if (pinfo2 & INSN2_READ_GPR_16)
4688 mask |= 1 << 16;
4689 if (pinfo2 & INSN2_READ_SP)
4690 mask |= 1 << SP;
4691 if (pinfo2 & INSN2_READ_GPR_31)
4692 mask |= 1 << 31;
4693 /* Don't include register 0. */
4694 return mask & ~1;
4695 }
4696
4697 /* Return the mask of core registers that IP writes. */
4698
4699 static unsigned int
4700 gpr_write_mask (const struct mips_cl_insn *ip)
4701 {
4702 unsigned long pinfo, pinfo2;
4703 unsigned int mask;
4704
4705 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4706 pinfo = ip->insn_mo->pinfo;
4707 pinfo2 = ip->insn_mo->pinfo2;
4708 if (pinfo & INSN_WRITE_GPR_24)
4709 mask |= 1 << 24;
4710 if (pinfo & INSN_WRITE_GPR_31)
4711 mask |= 1 << 31;
4712 if (pinfo & INSN_UDI)
4713 /* UDI instructions have traditionally been assumed to write to RD. */
4714 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4715 if (pinfo2 & INSN2_WRITE_SP)
4716 mask |= 1 << SP;
4717 /* Don't include register 0. */
4718 return mask & ~1;
4719 }
4720
4721 /* Return the mask of floating-point registers that IP reads. */
4722
4723 static unsigned int
4724 fpr_read_mask (const struct mips_cl_insn *ip)
4725 {
4726 unsigned long pinfo;
4727 unsigned int mask;
4728
4729 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4730 | (1 << OP_REG_MSA)),
4731 insn_read_mask (ip->insn_mo));
4732 pinfo = ip->insn_mo->pinfo;
4733 /* Conservatively treat all operands to an FP_D instruction are doubles.
4734 (This is overly pessimistic for things like cvt.d.s.) */
4735 if (FPR_SIZE != 64 && (pinfo & FP_D))
4736 mask |= mask << 1;
4737 return mask;
4738 }
4739
4740 /* Return the mask of floating-point registers that IP writes. */
4741
4742 static unsigned int
4743 fpr_write_mask (const struct mips_cl_insn *ip)
4744 {
4745 unsigned long pinfo;
4746 unsigned int mask;
4747
4748 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4749 | (1 << OP_REG_MSA)),
4750 insn_write_mask (ip->insn_mo));
4751 pinfo = ip->insn_mo->pinfo;
4752 /* Conservatively treat all operands to an FP_D instruction are doubles.
4753 (This is overly pessimistic for things like cvt.s.d.) */
4754 if (FPR_SIZE != 64 && (pinfo & FP_D))
4755 mask |= mask << 1;
4756 return mask;
4757 }
4758
4759 /* Operand OPNUM of INSN is an odd-numbered floating-point register.
4760 Check whether that is allowed. */
4761
4762 static bfd_boolean
4763 mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4764 {
4765 const char *s = insn->name;
4766 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4767 || FPR_SIZE == 64)
4768 && mips_opts.oddspreg;
4769
4770 if (insn->pinfo == INSN_MACRO)
4771 /* Let a macro pass, we'll catch it later when it is expanded. */
4772 return TRUE;
4773
4774 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4775 otherwise it depends on oddspreg. */
4776 if ((insn->pinfo & FP_S)
4777 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
4778 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
4779 return FPR_SIZE == 32 || oddspreg;
4780
4781 /* Allow odd registers for single-precision ops and double-precision if the
4782 floating-point registers are 64-bit wide. */
4783 switch (insn->pinfo & (FP_S | FP_D))
4784 {
4785 case FP_S:
4786 case 0:
4787 return oddspreg;
4788 case FP_D:
4789 return FPR_SIZE == 64;
4790 default:
4791 break;
4792 }
4793
4794 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4795 s = strchr (insn->name, '.');
4796 if (s != NULL && opnum == 2)
4797 s = strchr (s + 1, '.');
4798 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4799 return oddspreg;
4800
4801 return FPR_SIZE == 64;
4802 }
4803
4804 /* Information about an instruction argument that we're trying to match. */
4805 struct mips_arg_info
4806 {
4807 /* The instruction so far. */
4808 struct mips_cl_insn *insn;
4809
4810 /* The first unconsumed operand token. */
4811 struct mips_operand_token *token;
4812
4813 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4814 int opnum;
4815
4816 /* The 1-based argument number, for error reporting. This does not
4817 count elided optional registers, etc.. */
4818 int argnum;
4819
4820 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4821 unsigned int last_regno;
4822
4823 /* If the first operand was an OP_REG, this is the register that it
4824 specified, otherwise it is ILLEGAL_REG. */
4825 unsigned int dest_regno;
4826
4827 /* The value of the last OP_INT operand. Only used for OP_MSB,
4828 where it gives the lsb position. */
4829 unsigned int last_op_int;
4830
4831 /* If true, match routines should assume that no later instruction
4832 alternative matches and should therefore be as accommodating as
4833 possible. Match routines should not report errors if something
4834 is only invalid for !LAX_MATCH. */
4835 bfd_boolean lax_match;
4836
4837 /* True if a reference to the current AT register was seen. */
4838 bfd_boolean seen_at;
4839 };
4840
4841 /* Record that the argument is out of range. */
4842
4843 static void
4844 match_out_of_range (struct mips_arg_info *arg)
4845 {
4846 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4847 }
4848
4849 /* Record that the argument isn't constant but needs to be. */
4850
4851 static void
4852 match_not_constant (struct mips_arg_info *arg)
4853 {
4854 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4855 arg->argnum);
4856 }
4857
4858 /* Try to match an OT_CHAR token for character CH. Consume the token
4859 and return true on success, otherwise return false. */
4860
4861 static bfd_boolean
4862 match_char (struct mips_arg_info *arg, char ch)
4863 {
4864 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4865 {
4866 ++arg->token;
4867 if (ch == ',')
4868 arg->argnum += 1;
4869 return TRUE;
4870 }
4871 return FALSE;
4872 }
4873
4874 /* Try to get an expression from the next tokens in ARG. Consume the
4875 tokens and return true on success, storing the expression value in
4876 VALUE and relocation types in R. */
4877
4878 static bfd_boolean
4879 match_expression (struct mips_arg_info *arg, expressionS *value,
4880 bfd_reloc_code_real_type *r)
4881 {
4882 /* If the next token is a '(' that was parsed as being part of a base
4883 expression, assume we have an elided offset. The later match will fail
4884 if this turns out to be wrong. */
4885 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4886 {
4887 value->X_op = O_constant;
4888 value->X_add_number = 0;
4889 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4890 return TRUE;
4891 }
4892
4893 /* Reject register-based expressions such as "0+$2" and "(($2))".
4894 For plain registers the default error seems more appropriate. */
4895 if (arg->token->type == OT_INTEGER
4896 && arg->token->u.integer.value.X_op == O_register)
4897 {
4898 set_insn_error (arg->argnum, _("register value used as expression"));
4899 return FALSE;
4900 }
4901
4902 if (arg->token->type == OT_INTEGER)
4903 {
4904 *value = arg->token->u.integer.value;
4905 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4906 ++arg->token;
4907 return TRUE;
4908 }
4909
4910 set_insn_error_i
4911 (arg->argnum, _("operand %d must be an immediate expression"),
4912 arg->argnum);
4913 return FALSE;
4914 }
4915
4916 /* Try to get a constant expression from the next tokens in ARG. Consume
4917 the tokens and return true on success, storing the constant value
4918 in *VALUE. */
4919
4920 static bfd_boolean
4921 match_const_int (struct mips_arg_info *arg, offsetT *value)
4922 {
4923 expressionS ex;
4924 bfd_reloc_code_real_type r[3];
4925
4926 if (!match_expression (arg, &ex, r))
4927 return FALSE;
4928
4929 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
4930 *value = ex.X_add_number;
4931 else
4932 {
4933 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
4934 match_out_of_range (arg);
4935 else
4936 match_not_constant (arg);
4937 return FALSE;
4938 }
4939 return TRUE;
4940 }
4941
4942 /* Return the RTYPE_* flags for a register operand of type TYPE that
4943 appears in instruction OPCODE. */
4944
4945 static unsigned int
4946 convert_reg_type (const struct mips_opcode *opcode,
4947 enum mips_reg_operand_type type)
4948 {
4949 switch (type)
4950 {
4951 case OP_REG_GP:
4952 return RTYPE_NUM | RTYPE_GP;
4953
4954 case OP_REG_FP:
4955 /* Allow vector register names for MDMX if the instruction is a 64-bit
4956 FPR load, store or move (including moves to and from GPRs). */
4957 if ((mips_opts.ase & ASE_MDMX)
4958 && (opcode->pinfo & FP_D)
4959 && (opcode->pinfo & (INSN_COPROC_MOVE
4960 | INSN_COPROC_MEMORY_DELAY
4961 | INSN_LOAD_COPROC
4962 | INSN_LOAD_MEMORY
4963 | INSN_STORE_MEMORY)))
4964 return RTYPE_FPU | RTYPE_VEC;
4965 return RTYPE_FPU;
4966
4967 case OP_REG_CCC:
4968 if (opcode->pinfo & (FP_D | FP_S))
4969 return RTYPE_CCC | RTYPE_FCC;
4970 return RTYPE_CCC;
4971
4972 case OP_REG_VEC:
4973 if (opcode->membership & INSN_5400)
4974 return RTYPE_FPU;
4975 return RTYPE_FPU | RTYPE_VEC;
4976
4977 case OP_REG_ACC:
4978 return RTYPE_ACC;
4979
4980 case OP_REG_COPRO:
4981 if (opcode->name[strlen (opcode->name) - 1] == '0')
4982 return RTYPE_NUM | RTYPE_CP0;
4983 return RTYPE_NUM;
4984
4985 case OP_REG_HW:
4986 return RTYPE_NUM;
4987
4988 case OP_REG_VI:
4989 return RTYPE_NUM | RTYPE_VI;
4990
4991 case OP_REG_VF:
4992 return RTYPE_NUM | RTYPE_VF;
4993
4994 case OP_REG_R5900_I:
4995 return RTYPE_R5900_I;
4996
4997 case OP_REG_R5900_Q:
4998 return RTYPE_R5900_Q;
4999
5000 case OP_REG_R5900_R:
5001 return RTYPE_R5900_R;
5002
5003 case OP_REG_R5900_ACC:
5004 return RTYPE_R5900_ACC;
5005
5006 case OP_REG_MSA:
5007 return RTYPE_MSA;
5008
5009 case OP_REG_MSA_CTRL:
5010 return RTYPE_NUM;
5011 }
5012 abort ();
5013 }
5014
5015 /* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5016
5017 static void
5018 check_regno (struct mips_arg_info *arg,
5019 enum mips_reg_operand_type type, unsigned int regno)
5020 {
5021 if (AT && type == OP_REG_GP && regno == AT)
5022 arg->seen_at = TRUE;
5023
5024 if (type == OP_REG_FP
5025 && (regno & 1) != 0
5026 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
5027 {
5028 /* This was a warning prior to introducing O32 FPXX and FP64 support
5029 so maintain a warning for FP32 but raise an error for the new
5030 cases. */
5031 if (FPR_SIZE == 32)
5032 as_warn (_("float register should be even, was %d"), regno);
5033 else
5034 as_bad (_("float register should be even, was %d"), regno);
5035 }
5036
5037 if (type == OP_REG_CCC)
5038 {
5039 const char *name;
5040 size_t length;
5041
5042 name = arg->insn->insn_mo->name;
5043 length = strlen (name);
5044 if ((regno & 1) != 0
5045 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5046 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
5047 as_warn (_("condition code register should be even for %s, was %d"),
5048 name, regno);
5049
5050 if ((regno & 3) != 0
5051 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
5052 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
5053 name, regno);
5054 }
5055 }
5056
5057 /* ARG is a register with symbol value SYMVAL. Try to interpret it as
5058 a register of type TYPE. Return true on success, storing the register
5059 number in *REGNO and warning about any dubious uses. */
5060
5061 static bfd_boolean
5062 match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5063 unsigned int symval, unsigned int *regno)
5064 {
5065 if (type == OP_REG_VEC)
5066 symval = mips_prefer_vec_regno (symval);
5067 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5068 return FALSE;
5069
5070 *regno = symval & RNUM_MASK;
5071 check_regno (arg, type, *regno);
5072 return TRUE;
5073 }
5074
5075 /* Try to interpret the next token in ARG as a register of type TYPE.
5076 Consume the token and return true on success, storing the register
5077 number in *REGNO. Return false on failure. */
5078
5079 static bfd_boolean
5080 match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5081 unsigned int *regno)
5082 {
5083 if (arg->token->type == OT_REG
5084 && match_regno (arg, type, arg->token->u.regno, regno))
5085 {
5086 ++arg->token;
5087 return TRUE;
5088 }
5089 return FALSE;
5090 }
5091
5092 /* Try to interpret the next token in ARG as a range of registers of type TYPE.
5093 Consume the token and return true on success, storing the register numbers
5094 in *REGNO1 and *REGNO2. Return false on failure. */
5095
5096 static bfd_boolean
5097 match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5098 unsigned int *regno1, unsigned int *regno2)
5099 {
5100 if (match_reg (arg, type, regno1))
5101 {
5102 *regno2 = *regno1;
5103 return TRUE;
5104 }
5105 if (arg->token->type == OT_REG_RANGE
5106 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5107 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5108 && *regno1 <= *regno2)
5109 {
5110 ++arg->token;
5111 return TRUE;
5112 }
5113 return FALSE;
5114 }
5115
5116 /* OP_INT matcher. */
5117
5118 static bfd_boolean
5119 match_int_operand (struct mips_arg_info *arg,
5120 const struct mips_operand *operand_base)
5121 {
5122 const struct mips_int_operand *operand;
5123 unsigned int uval;
5124 int min_val, max_val, factor;
5125 offsetT sval;
5126
5127 operand = (const struct mips_int_operand *) operand_base;
5128 factor = 1 << operand->shift;
5129 min_val = mips_int_operand_min (operand);
5130 max_val = mips_int_operand_max (operand);
5131
5132 if (operand_base->lsb == 0
5133 && operand_base->size == 16
5134 && operand->shift == 0
5135 && operand->bias == 0
5136 && (operand->max_val == 32767 || operand->max_val == 65535))
5137 {
5138 /* The operand can be relocated. */
5139 if (!match_expression (arg, &offset_expr, offset_reloc))
5140 return FALSE;
5141
5142 if (offset_expr.X_op == O_big)
5143 {
5144 match_out_of_range (arg);
5145 return FALSE;
5146 }
5147
5148 if (offset_reloc[0] != BFD_RELOC_UNUSED)
5149 /* Relocation operators were used. Accept the argument and
5150 leave the relocation value in offset_expr and offset_relocs
5151 for the caller to process. */
5152 return TRUE;
5153
5154 if (offset_expr.X_op != O_constant)
5155 {
5156 /* Accept non-constant operands if no later alternative matches,
5157 leaving it for the caller to process. */
5158 if (!arg->lax_match)
5159 {
5160 match_not_constant (arg);
5161 return FALSE;
5162 }
5163 offset_reloc[0] = BFD_RELOC_LO16;
5164 return TRUE;
5165 }
5166
5167 /* Clear the global state; we're going to install the operand
5168 ourselves. */
5169 sval = offset_expr.X_add_number;
5170 offset_expr.X_op = O_absent;
5171
5172 /* For compatibility with older assemblers, we accept
5173 0x8000-0xffff as signed 16-bit numbers when only
5174 signed numbers are allowed. */
5175 if (sval > max_val)
5176 {
5177 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5178 if (!arg->lax_match && sval <= max_val)
5179 {
5180 match_out_of_range (arg);
5181 return FALSE;
5182 }
5183 }
5184 }
5185 else
5186 {
5187 if (!match_const_int (arg, &sval))
5188 return FALSE;
5189 }
5190
5191 arg->last_op_int = sval;
5192
5193 if (sval < min_val || sval > max_val || sval % factor)
5194 {
5195 match_out_of_range (arg);
5196 return FALSE;
5197 }
5198
5199 uval = (unsigned int) sval >> operand->shift;
5200 uval -= operand->bias;
5201
5202 /* Handle -mfix-cn63xxp1. */
5203 if (arg->opnum == 1
5204 && mips_fix_cn63xxp1
5205 && !mips_opts.micromips
5206 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5207 switch (uval)
5208 {
5209 case 5:
5210 case 25:
5211 case 26:
5212 case 27:
5213 case 28:
5214 case 29:
5215 case 30:
5216 case 31:
5217 /* These are ok. */
5218 break;
5219
5220 default:
5221 /* The rest must be changed to 28. */
5222 uval = 28;
5223 break;
5224 }
5225
5226 insn_insert_operand (arg->insn, operand_base, uval);
5227 return TRUE;
5228 }
5229
5230 /* OP_MAPPED_INT matcher. */
5231
5232 static bfd_boolean
5233 match_mapped_int_operand (struct mips_arg_info *arg,
5234 const struct mips_operand *operand_base)
5235 {
5236 const struct mips_mapped_int_operand *operand;
5237 unsigned int uval, num_vals;
5238 offsetT sval;
5239
5240 operand = (const struct mips_mapped_int_operand *) operand_base;
5241 if (!match_const_int (arg, &sval))
5242 return FALSE;
5243
5244 num_vals = 1 << operand_base->size;
5245 for (uval = 0; uval < num_vals; uval++)
5246 if (operand->int_map[uval] == sval)
5247 break;
5248 if (uval == num_vals)
5249 {
5250 match_out_of_range (arg);
5251 return FALSE;
5252 }
5253
5254 insn_insert_operand (arg->insn, operand_base, uval);
5255 return TRUE;
5256 }
5257
5258 /* OP_MSB matcher. */
5259
5260 static bfd_boolean
5261 match_msb_operand (struct mips_arg_info *arg,
5262 const struct mips_operand *operand_base)
5263 {
5264 const struct mips_msb_operand *operand;
5265 int min_val, max_val, max_high;
5266 offsetT size, sval, high;
5267
5268 operand = (const struct mips_msb_operand *) operand_base;
5269 min_val = operand->bias;
5270 max_val = min_val + (1 << operand_base->size) - 1;
5271 max_high = operand->opsize;
5272
5273 if (!match_const_int (arg, &size))
5274 return FALSE;
5275
5276 high = size + arg->last_op_int;
5277 sval = operand->add_lsb ? high : size;
5278
5279 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5280 {
5281 match_out_of_range (arg);
5282 return FALSE;
5283 }
5284 insn_insert_operand (arg->insn, operand_base, sval - min_val);
5285 return TRUE;
5286 }
5287
5288 /* OP_REG matcher. */
5289
5290 static bfd_boolean
5291 match_reg_operand (struct mips_arg_info *arg,
5292 const struct mips_operand *operand_base)
5293 {
5294 const struct mips_reg_operand *operand;
5295 unsigned int regno, uval, num_vals;
5296
5297 operand = (const struct mips_reg_operand *) operand_base;
5298 if (!match_reg (arg, operand->reg_type, &regno))
5299 return FALSE;
5300
5301 if (operand->reg_map)
5302 {
5303 num_vals = 1 << operand->root.size;
5304 for (uval = 0; uval < num_vals; uval++)
5305 if (operand->reg_map[uval] == regno)
5306 break;
5307 if (num_vals == uval)
5308 return FALSE;
5309 }
5310 else
5311 uval = regno;
5312
5313 arg->last_regno = regno;
5314 if (arg->opnum == 1)
5315 arg->dest_regno = regno;
5316 insn_insert_operand (arg->insn, operand_base, uval);
5317 return TRUE;
5318 }
5319
5320 /* OP_REG_PAIR matcher. */
5321
5322 static bfd_boolean
5323 match_reg_pair_operand (struct mips_arg_info *arg,
5324 const struct mips_operand *operand_base)
5325 {
5326 const struct mips_reg_pair_operand *operand;
5327 unsigned int regno1, regno2, uval, num_vals;
5328
5329 operand = (const struct mips_reg_pair_operand *) operand_base;
5330 if (!match_reg (arg, operand->reg_type, &regno1)
5331 || !match_char (arg, ',')
5332 || !match_reg (arg, operand->reg_type, &regno2))
5333 return FALSE;
5334
5335 num_vals = 1 << operand_base->size;
5336 for (uval = 0; uval < num_vals; uval++)
5337 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5338 break;
5339 if (uval == num_vals)
5340 return FALSE;
5341
5342 insn_insert_operand (arg->insn, operand_base, uval);
5343 return TRUE;
5344 }
5345
5346 /* OP_PCREL matcher. The caller chooses the relocation type. */
5347
5348 static bfd_boolean
5349 match_pcrel_operand (struct mips_arg_info *arg)
5350 {
5351 bfd_reloc_code_real_type r[3];
5352
5353 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
5354 }
5355
5356 /* OP_PERF_REG matcher. */
5357
5358 static bfd_boolean
5359 match_perf_reg_operand (struct mips_arg_info *arg,
5360 const struct mips_operand *operand)
5361 {
5362 offsetT sval;
5363
5364 if (!match_const_int (arg, &sval))
5365 return FALSE;
5366
5367 if (sval != 0
5368 && (sval != 1
5369 || (mips_opts.arch == CPU_R5900
5370 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5371 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5372 {
5373 set_insn_error (arg->argnum, _("invalid performance register"));
5374 return FALSE;
5375 }
5376
5377 insn_insert_operand (arg->insn, operand, sval);
5378 return TRUE;
5379 }
5380
5381 /* OP_ADDIUSP matcher. */
5382
5383 static bfd_boolean
5384 match_addiusp_operand (struct mips_arg_info *arg,
5385 const struct mips_operand *operand)
5386 {
5387 offsetT sval;
5388 unsigned int uval;
5389
5390 if (!match_const_int (arg, &sval))
5391 return FALSE;
5392
5393 if (sval % 4)
5394 {
5395 match_out_of_range (arg);
5396 return FALSE;
5397 }
5398
5399 sval /= 4;
5400 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
5401 {
5402 match_out_of_range (arg);
5403 return FALSE;
5404 }
5405
5406 uval = (unsigned int) sval;
5407 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5408 insn_insert_operand (arg->insn, operand, uval);
5409 return TRUE;
5410 }
5411
5412 /* OP_CLO_CLZ_DEST matcher. */
5413
5414 static bfd_boolean
5415 match_clo_clz_dest_operand (struct mips_arg_info *arg,
5416 const struct mips_operand *operand)
5417 {
5418 unsigned int regno;
5419
5420 if (!match_reg (arg, OP_REG_GP, &regno))
5421 return FALSE;
5422
5423 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5424 return TRUE;
5425 }
5426
5427 /* OP_CHECK_PREV matcher. */
5428
5429 static bfd_boolean
5430 match_check_prev_operand (struct mips_arg_info *arg,
5431 const struct mips_operand *operand_base)
5432 {
5433 const struct mips_check_prev_operand *operand;
5434 unsigned int regno;
5435
5436 operand = (const struct mips_check_prev_operand *) operand_base;
5437
5438 if (!match_reg (arg, OP_REG_GP, &regno))
5439 return FALSE;
5440
5441 if (!operand->zero_ok && regno == 0)
5442 return FALSE;
5443
5444 if ((operand->less_than_ok && regno < arg->last_regno)
5445 || (operand->greater_than_ok && regno > arg->last_regno)
5446 || (operand->equal_ok && regno == arg->last_regno))
5447 {
5448 arg->last_regno = regno;
5449 insn_insert_operand (arg->insn, operand_base, regno);
5450 return TRUE;
5451 }
5452
5453 return FALSE;
5454 }
5455
5456 /* OP_SAME_RS_RT matcher. */
5457
5458 static bfd_boolean
5459 match_same_rs_rt_operand (struct mips_arg_info *arg,
5460 const struct mips_operand *operand)
5461 {
5462 unsigned int regno;
5463
5464 if (!match_reg (arg, OP_REG_GP, &regno))
5465 return FALSE;
5466
5467 if (regno == 0)
5468 {
5469 set_insn_error (arg->argnum, _("the source register must not be $0"));
5470 return FALSE;
5471 }
5472
5473 arg->last_regno = regno;
5474
5475 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5476 return TRUE;
5477 }
5478
5479 /* OP_LWM_SWM_LIST matcher. */
5480
5481 static bfd_boolean
5482 match_lwm_swm_list_operand (struct mips_arg_info *arg,
5483 const struct mips_operand *operand)
5484 {
5485 unsigned int reglist, sregs, ra, regno1, regno2;
5486 struct mips_arg_info reset;
5487
5488 reglist = 0;
5489 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5490 return FALSE;
5491 do
5492 {
5493 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5494 {
5495 reglist |= 1 << FP;
5496 regno2 = S7;
5497 }
5498 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5499 reset = *arg;
5500 }
5501 while (match_char (arg, ',')
5502 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5503 *arg = reset;
5504
5505 if (operand->size == 2)
5506 {
5507 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5508
5509 s0, ra
5510 s0, s1, ra, s2, s3
5511 s0-s2, ra
5512
5513 and any permutations of these. */
5514 if ((reglist & 0xfff1ffff) != 0x80010000)
5515 return FALSE;
5516
5517 sregs = (reglist >> 17) & 7;
5518 ra = 0;
5519 }
5520 else
5521 {
5522 /* The list must include at least one of ra and s0-sN,
5523 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5524 which are $23 and $30 respectively.) E.g.:
5525
5526 ra
5527 s0
5528 ra, s0, s1, s2
5529 s0-s8
5530 s0-s5, ra
5531
5532 and any permutations of these. */
5533 if ((reglist & 0x3f00ffff) != 0)
5534 return FALSE;
5535
5536 ra = (reglist >> 27) & 0x10;
5537 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5538 }
5539 sregs += 1;
5540 if ((sregs & -sregs) != sregs)
5541 return FALSE;
5542
5543 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5544 return TRUE;
5545 }
5546
5547 /* OP_ENTRY_EXIT_LIST matcher. */
5548
5549 static unsigned int
5550 match_entry_exit_operand (struct mips_arg_info *arg,
5551 const struct mips_operand *operand)
5552 {
5553 unsigned int mask;
5554 bfd_boolean is_exit;
5555
5556 /* The format is the same for both ENTRY and EXIT, but the constraints
5557 are different. */
5558 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5559 mask = (is_exit ? 7 << 3 : 0);
5560 do
5561 {
5562 unsigned int regno1, regno2;
5563 bfd_boolean is_freg;
5564
5565 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5566 is_freg = FALSE;
5567 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5568 is_freg = TRUE;
5569 else
5570 return FALSE;
5571
5572 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5573 {
5574 mask &= ~(7 << 3);
5575 mask |= (5 + regno2) << 3;
5576 }
5577 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5578 mask |= (regno2 - 3) << 3;
5579 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5580 mask |= (regno2 - 15) << 1;
5581 else if (regno1 == RA && regno2 == RA)
5582 mask |= 1;
5583 else
5584 return FALSE;
5585 }
5586 while (match_char (arg, ','));
5587
5588 insn_insert_operand (arg->insn, operand, mask);
5589 return TRUE;
5590 }
5591
5592 /* Encode regular MIPS SAVE/RESTORE instruction operands according to
5593 the argument register mask AMASK, the number of static registers
5594 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5595 respectively, and the frame size FRAME_SIZE. */
5596
5597 static unsigned int
5598 mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5599 unsigned int ra, unsigned int s0, unsigned int s1,
5600 unsigned int frame_size)
5601 {
5602 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5603 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5604 }
5605
5606 /* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5607 argument register mask AMASK, the number of static registers saved
5608 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5609 respectively, and the frame size FRAME_SIZE. */
5610
5611 static unsigned int
5612 mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5613 unsigned int ra, unsigned int s0, unsigned int s1,
5614 unsigned int frame_size)
5615 {
5616 unsigned int args;
5617
5618 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5619 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5620 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5621 | ((frame_size & 0xf0) << 16));
5622 return args;
5623 }
5624
5625 /* OP_SAVE_RESTORE_LIST matcher. */
5626
5627 static bfd_boolean
5628 match_save_restore_list_operand (struct mips_arg_info *arg)
5629 {
5630 unsigned int opcode, args, statics, sregs;
5631 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
5632 unsigned int arg_mask, ra, s0, s1;
5633 offsetT frame_size;
5634
5635 opcode = arg->insn->insn_opcode;
5636 frame_size = 0;
5637 num_frame_sizes = 0;
5638 args = 0;
5639 statics = 0;
5640 sregs = 0;
5641 ra = 0;
5642 s0 = 0;
5643 s1 = 0;
5644 do
5645 {
5646 unsigned int regno1, regno2;
5647
5648 if (arg->token->type == OT_INTEGER)
5649 {
5650 /* Handle the frame size. */
5651 if (!match_const_int (arg, &frame_size))
5652 return FALSE;
5653 num_frame_sizes += 1;
5654 }
5655 else
5656 {
5657 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5658 return FALSE;
5659
5660 while (regno1 <= regno2)
5661 {
5662 if (regno1 >= 4 && regno1 <= 7)
5663 {
5664 if (num_frame_sizes == 0)
5665 /* args $a0-$a3 */
5666 args |= 1 << (regno1 - 4);
5667 else
5668 /* statics $a0-$a3 */
5669 statics |= 1 << (regno1 - 4);
5670 }
5671 else if (regno1 >= 16 && regno1 <= 23)
5672 /* $s0-$s7 */
5673 sregs |= 1 << (regno1 - 16);
5674 else if (regno1 == 30)
5675 /* $s8 */
5676 sregs |= 1 << 8;
5677 else if (regno1 == 31)
5678 /* Add $ra to insn. */
5679 ra = 1;
5680 else
5681 return FALSE;
5682 regno1 += 1;
5683 if (regno1 == 24)
5684 regno1 = 30;
5685 }
5686 }
5687 }
5688 while (match_char (arg, ','));
5689
5690 /* Encode args/statics combination. */
5691 if (args & statics)
5692 return FALSE;
5693 else if (args == 0xf)
5694 /* All $a0-$a3 are args. */
5695 arg_mask = MIPS_SVRS_ALL_ARGS;
5696 else if (statics == 0xf)
5697 /* All $a0-$a3 are statics. */
5698 arg_mask = MIPS_SVRS_ALL_STATICS;
5699 else
5700 {
5701 /* Count arg registers. */
5702 num_args = 0;
5703 while (args & 0x1)
5704 {
5705 args >>= 1;
5706 num_args += 1;
5707 }
5708 if (args != 0)
5709 return FALSE;
5710
5711 /* Count static registers. */
5712 num_statics = 0;
5713 while (statics & 0x8)
5714 {
5715 statics = (statics << 1) & 0xf;
5716 num_statics += 1;
5717 }
5718 if (statics != 0)
5719 return FALSE;
5720
5721 /* Encode args/statics. */
5722 arg_mask = (num_args << 2) | num_statics;
5723 }
5724
5725 /* Encode $s0/$s1. */
5726 if (sregs & (1 << 0)) /* $s0 */
5727 s0 = 1;
5728 if (sregs & (1 << 1)) /* $s1 */
5729 s1 = 1;
5730 sregs >>= 2;
5731
5732 /* Encode $s2-$s8. */
5733 num_sregs = 0;
5734 while (sregs & 1)
5735 {
5736 sregs >>= 1;
5737 num_sregs += 1;
5738 }
5739 if (sregs != 0)
5740 return FALSE;
5741
5742 /* Encode frame size. */
5743 if (num_frame_sizes == 0)
5744 {
5745 set_insn_error (arg->argnum, _("missing frame size"));
5746 return FALSE;
5747 }
5748 if (num_frame_sizes > 1)
5749 {
5750 set_insn_error (arg->argnum, _("frame size specified twice"));
5751 return FALSE;
5752 }
5753 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5754 {
5755 set_insn_error (arg->argnum, _("invalid frame size"));
5756 return FALSE;
5757 }
5758 frame_size /= 8;
5759
5760 /* Finally build the instruction. */
5761 if (mips_opts.mips16)
5762 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5763 frame_size);
5764 else if (!mips_opts.micromips)
5765 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5766 frame_size);
5767 else
5768 abort ();
5769
5770 arg->insn->insn_opcode = opcode;
5771 return TRUE;
5772 }
5773
5774 /* OP_MDMX_IMM_REG matcher. */
5775
5776 static bfd_boolean
5777 match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
5778 const struct mips_operand *operand)
5779 {
5780 unsigned int regno, uval;
5781 bfd_boolean is_qh;
5782 const struct mips_opcode *opcode;
5783
5784 /* The mips_opcode records whether this is an octobyte or quadhalf
5785 instruction. Start out with that bit in place. */
5786 opcode = arg->insn->insn_mo;
5787 uval = mips_extract_operand (operand, opcode->match);
5788 is_qh = (uval != 0);
5789
5790 if (arg->token->type == OT_REG)
5791 {
5792 if ((opcode->membership & INSN_5400)
5793 && strcmp (opcode->name, "rzu.ob") == 0)
5794 {
5795 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5796 arg->argnum);
5797 return FALSE;
5798 }
5799
5800 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5801 return FALSE;
5802 ++arg->token;
5803
5804 /* Check whether this is a vector register or a broadcast of
5805 a single element. */
5806 if (arg->token->type == OT_INTEGER_INDEX)
5807 {
5808 if (arg->token->u.index > (is_qh ? 3 : 7))
5809 {
5810 set_insn_error (arg->argnum, _("invalid element selector"));
5811 return FALSE;
5812 }
5813 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5814 ++arg->token;
5815 }
5816 else
5817 {
5818 /* A full vector. */
5819 if ((opcode->membership & INSN_5400)
5820 && (strcmp (opcode->name, "sll.ob") == 0
5821 || strcmp (opcode->name, "srl.ob") == 0))
5822 {
5823 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5824 arg->argnum);
5825 return FALSE;
5826 }
5827
5828 if (is_qh)
5829 uval |= MDMX_FMTSEL_VEC_QH << 5;
5830 else
5831 uval |= MDMX_FMTSEL_VEC_OB << 5;
5832 }
5833 uval |= regno;
5834 }
5835 else
5836 {
5837 offsetT sval;
5838
5839 if (!match_const_int (arg, &sval))
5840 return FALSE;
5841 if (sval < 0 || sval > 31)
5842 {
5843 match_out_of_range (arg);
5844 return FALSE;
5845 }
5846 uval |= (sval & 31);
5847 if (is_qh)
5848 uval |= MDMX_FMTSEL_IMM_QH << 5;
5849 else
5850 uval |= MDMX_FMTSEL_IMM_OB << 5;
5851 }
5852 insn_insert_operand (arg->insn, operand, uval);
5853 return TRUE;
5854 }
5855
5856 /* OP_IMM_INDEX matcher. */
5857
5858 static bfd_boolean
5859 match_imm_index_operand (struct mips_arg_info *arg,
5860 const struct mips_operand *operand)
5861 {
5862 unsigned int max_val;
5863
5864 if (arg->token->type != OT_INTEGER_INDEX)
5865 return FALSE;
5866
5867 max_val = (1 << operand->size) - 1;
5868 if (arg->token->u.index > max_val)
5869 {
5870 match_out_of_range (arg);
5871 return FALSE;
5872 }
5873 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5874 ++arg->token;
5875 return TRUE;
5876 }
5877
5878 /* OP_REG_INDEX matcher. */
5879
5880 static bfd_boolean
5881 match_reg_index_operand (struct mips_arg_info *arg,
5882 const struct mips_operand *operand)
5883 {
5884 unsigned int regno;
5885
5886 if (arg->token->type != OT_REG_INDEX)
5887 return FALSE;
5888
5889 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5890 return FALSE;
5891
5892 insn_insert_operand (arg->insn, operand, regno);
5893 ++arg->token;
5894 return TRUE;
5895 }
5896
5897 /* OP_PC matcher. */
5898
5899 static bfd_boolean
5900 match_pc_operand (struct mips_arg_info *arg)
5901 {
5902 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5903 {
5904 ++arg->token;
5905 return TRUE;
5906 }
5907 return FALSE;
5908 }
5909
5910 /* OP_REG28 matcher. */
5911
5912 static bfd_boolean
5913 match_reg28_operand (struct mips_arg_info *arg)
5914 {
5915 unsigned int regno;
5916
5917 if (arg->token->type == OT_REG
5918 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5919 && regno == GP)
5920 {
5921 ++arg->token;
5922 return TRUE;
5923 }
5924 return FALSE;
5925 }
5926
5927 /* OP_NON_ZERO_REG matcher. */
5928
5929 static bfd_boolean
5930 match_non_zero_reg_operand (struct mips_arg_info *arg,
5931 const struct mips_operand *operand)
5932 {
5933 unsigned int regno;
5934
5935 if (!match_reg (arg, OP_REG_GP, &regno))
5936 return FALSE;
5937
5938 if (regno == 0)
5939 return FALSE;
5940
5941 arg->last_regno = regno;
5942 insn_insert_operand (arg->insn, operand, regno);
5943 return TRUE;
5944 }
5945
5946 /* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5947 register that we need to match. */
5948
5949 static bfd_boolean
5950 match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
5951 {
5952 unsigned int regno;
5953
5954 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
5955 }
5956
5957 /* Try to match a floating-point constant from ARG for LI.S or LI.D.
5958 LENGTH is the length of the value in bytes (4 for float, 8 for double)
5959 and USING_GPRS says whether the destination is a GPR rather than an FPR.
5960
5961 Return the constant in IMM and OFFSET as follows:
5962
5963 - If the constant should be loaded via memory, set IMM to O_absent and
5964 OFFSET to the memory address.
5965
5966 - Otherwise, if the constant should be loaded into two 32-bit registers,
5967 set IMM to the O_constant to load into the high register and OFFSET
5968 to the corresponding value for the low register.
5969
5970 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5971
5972 These constants only appear as the last operand in an instruction,
5973 and every instruction that accepts them in any variant accepts them
5974 in all variants. This means we don't have to worry about backing out
5975 any changes if the instruction does not match. We just match
5976 unconditionally and report an error if the constant is invalid. */
5977
5978 static bfd_boolean
5979 match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5980 expressionS *offset, int length, bfd_boolean using_gprs)
5981 {
5982 char *p;
5983 segT seg, new_seg;
5984 subsegT subseg;
5985 const char *newname;
5986 unsigned char *data;
5987
5988 /* Where the constant is placed is based on how the MIPS assembler
5989 does things:
5990
5991 length == 4 && using_gprs -- immediate value only
5992 length == 8 && using_gprs -- .rdata or immediate value
5993 length == 4 && !using_gprs -- .lit4 or immediate value
5994 length == 8 && !using_gprs -- .lit8 or immediate value
5995
5996 The .lit4 and .lit8 sections are only used if permitted by the
5997 -G argument. */
5998 if (arg->token->type != OT_FLOAT)
5999 {
6000 set_insn_error (arg->argnum, _("floating-point expression required"));
6001 return FALSE;
6002 }
6003
6004 gas_assert (arg->token->u.flt.length == length);
6005 data = arg->token->u.flt.data;
6006 ++arg->token;
6007
6008 /* Handle 32-bit constants for which an immediate value is best. */
6009 if (length == 4
6010 && (using_gprs
6011 || g_switch_value < 4
6012 || (data[0] == 0 && data[1] == 0)
6013 || (data[2] == 0 && data[3] == 0)))
6014 {
6015 imm->X_op = O_constant;
6016 if (!target_big_endian)
6017 imm->X_add_number = bfd_getl32 (data);
6018 else
6019 imm->X_add_number = bfd_getb32 (data);
6020 offset->X_op = O_absent;
6021 return TRUE;
6022 }
6023
6024 /* Handle 64-bit constants for which an immediate value is best. */
6025 if (length == 8
6026 && !mips_disable_float_construction
6027 /* Constants can only be constructed in GPRs and copied to FPRs if the
6028 GPRs are at least as wide as the FPRs or MTHC1 is available.
6029 Unlike most tests for 32-bit floating-point registers this check
6030 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6031 permit 64-bit moves without MXHC1.
6032 Force the constant into memory otherwise. */
6033 && (using_gprs
6034 || GPR_SIZE == 64
6035 || ISA_HAS_MXHC1 (mips_opts.isa)
6036 || FPR_SIZE == 32)
6037 && ((data[0] == 0 && data[1] == 0)
6038 || (data[2] == 0 && data[3] == 0))
6039 && ((data[4] == 0 && data[5] == 0)
6040 || (data[6] == 0 && data[7] == 0)))
6041 {
6042 /* The value is simple enough to load with a couple of instructions.
6043 If using 32-bit registers, set IMM to the high order 32 bits and
6044 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6045 64 bit constant. */
6046 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
6047 {
6048 imm->X_op = O_constant;
6049 offset->X_op = O_constant;
6050 if (!target_big_endian)
6051 {
6052 imm->X_add_number = bfd_getl32 (data + 4);
6053 offset->X_add_number = bfd_getl32 (data);
6054 }
6055 else
6056 {
6057 imm->X_add_number = bfd_getb32 (data);
6058 offset->X_add_number = bfd_getb32 (data + 4);
6059 }
6060 if (offset->X_add_number == 0)
6061 offset->X_op = O_absent;
6062 }
6063 else
6064 {
6065 imm->X_op = O_constant;
6066 if (!target_big_endian)
6067 imm->X_add_number = bfd_getl64 (data);
6068 else
6069 imm->X_add_number = bfd_getb64 (data);
6070 offset->X_op = O_absent;
6071 }
6072 return TRUE;
6073 }
6074
6075 /* Switch to the right section. */
6076 seg = now_seg;
6077 subseg = now_subseg;
6078 if (length == 4)
6079 {
6080 gas_assert (!using_gprs && g_switch_value >= 4);
6081 newname = ".lit4";
6082 }
6083 else
6084 {
6085 if (using_gprs || g_switch_value < 8)
6086 newname = RDATA_SECTION_NAME;
6087 else
6088 newname = ".lit8";
6089 }
6090
6091 new_seg = subseg_new (newname, (subsegT) 0);
6092 bfd_set_section_flags (stdoutput, new_seg,
6093 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6094 frag_align (length == 4 ? 2 : 3, 0, 0);
6095 if (strncmp (TARGET_OS, "elf", 3) != 0)
6096 record_alignment (new_seg, 4);
6097 else
6098 record_alignment (new_seg, length == 4 ? 2 : 3);
6099 if (seg == now_seg)
6100 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
6101
6102 /* Set the argument to the current address in the section. */
6103 imm->X_op = O_absent;
6104 offset->X_op = O_symbol;
6105 offset->X_add_symbol = symbol_temp_new_now ();
6106 offset->X_add_number = 0;
6107
6108 /* Put the floating point number into the section. */
6109 p = frag_more (length);
6110 memcpy (p, data, length);
6111
6112 /* Switch back to the original section. */
6113 subseg_set (seg, subseg);
6114 return TRUE;
6115 }
6116
6117 /* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6118 them. */
6119
6120 static bfd_boolean
6121 match_vu0_suffix_operand (struct mips_arg_info *arg,
6122 const struct mips_operand *operand,
6123 bfd_boolean match_p)
6124 {
6125 unsigned int uval;
6126
6127 /* The operand can be an XYZW mask or a single 2-bit channel index
6128 (with X being 0). */
6129 gas_assert (operand->size == 2 || operand->size == 4);
6130
6131 /* The suffix can be omitted when it is already part of the opcode. */
6132 if (arg->token->type != OT_CHANNELS)
6133 return match_p;
6134
6135 uval = arg->token->u.channels;
6136 if (operand->size == 2)
6137 {
6138 /* Check that a single bit is set and convert it into a 2-bit index. */
6139 if ((uval & -uval) != uval)
6140 return FALSE;
6141 uval = 4 - ffs (uval);
6142 }
6143
6144 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6145 return FALSE;
6146
6147 ++arg->token;
6148 if (!match_p)
6149 insn_insert_operand (arg->insn, operand, uval);
6150 return TRUE;
6151 }
6152
6153 /* Try to match a token from ARG against OPERAND. Consume the token
6154 and return true on success, otherwise return false. */
6155
6156 static bfd_boolean
6157 match_operand (struct mips_arg_info *arg,
6158 const struct mips_operand *operand)
6159 {
6160 switch (operand->type)
6161 {
6162 case OP_INT:
6163 return match_int_operand (arg, operand);
6164
6165 case OP_MAPPED_INT:
6166 return match_mapped_int_operand (arg, operand);
6167
6168 case OP_MSB:
6169 return match_msb_operand (arg, operand);
6170
6171 case OP_REG:
6172 case OP_OPTIONAL_REG:
6173 return match_reg_operand (arg, operand);
6174
6175 case OP_REG_PAIR:
6176 return match_reg_pair_operand (arg, operand);
6177
6178 case OP_PCREL:
6179 return match_pcrel_operand (arg);
6180
6181 case OP_PERF_REG:
6182 return match_perf_reg_operand (arg, operand);
6183
6184 case OP_ADDIUSP_INT:
6185 return match_addiusp_operand (arg, operand);
6186
6187 case OP_CLO_CLZ_DEST:
6188 return match_clo_clz_dest_operand (arg, operand);
6189
6190 case OP_LWM_SWM_LIST:
6191 return match_lwm_swm_list_operand (arg, operand);
6192
6193 case OP_ENTRY_EXIT_LIST:
6194 return match_entry_exit_operand (arg, operand);
6195
6196 case OP_SAVE_RESTORE_LIST:
6197 return match_save_restore_list_operand (arg);
6198
6199 case OP_MDMX_IMM_REG:
6200 return match_mdmx_imm_reg_operand (arg, operand);
6201
6202 case OP_REPEAT_DEST_REG:
6203 return match_tied_reg_operand (arg, arg->dest_regno);
6204
6205 case OP_REPEAT_PREV_REG:
6206 return match_tied_reg_operand (arg, arg->last_regno);
6207
6208 case OP_PC:
6209 return match_pc_operand (arg);
6210
6211 case OP_REG28:
6212 return match_reg28_operand (arg);
6213
6214 case OP_VU0_SUFFIX:
6215 return match_vu0_suffix_operand (arg, operand, FALSE);
6216
6217 case OP_VU0_MATCH_SUFFIX:
6218 return match_vu0_suffix_operand (arg, operand, TRUE);
6219
6220 case OP_IMM_INDEX:
6221 return match_imm_index_operand (arg, operand);
6222
6223 case OP_REG_INDEX:
6224 return match_reg_index_operand (arg, operand);
6225
6226 case OP_SAME_RS_RT:
6227 return match_same_rs_rt_operand (arg, operand);
6228
6229 case OP_CHECK_PREV:
6230 return match_check_prev_operand (arg, operand);
6231
6232 case OP_NON_ZERO_REG:
6233 return match_non_zero_reg_operand (arg, operand);
6234 }
6235 abort ();
6236 }
6237
6238 /* ARG is the state after successfully matching an instruction.
6239 Issue any queued-up warnings. */
6240
6241 static void
6242 check_completed_insn (struct mips_arg_info *arg)
6243 {
6244 if (arg->seen_at)
6245 {
6246 if (AT == ATREG)
6247 as_warn (_("used $at without \".set noat\""));
6248 else
6249 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
6250 }
6251 }
6252
6253 /* Return true if modifying general-purpose register REG needs a delay. */
6254
6255 static bfd_boolean
6256 reg_needs_delay (unsigned int reg)
6257 {
6258 unsigned long prev_pinfo;
6259
6260 prev_pinfo = history[0].insn_mo->pinfo;
6261 if (!mips_opts.noreorder
6262 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
6263 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
6264 && (gpr_write_mask (&history[0]) & (1 << reg)))
6265 return TRUE;
6266
6267 return FALSE;
6268 }
6269
6270 /* Classify an instruction according to the FIX_VR4120_* enumeration.
6271 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6272 by VR4120 errata. */
6273
6274 static unsigned int
6275 classify_vr4120_insn (const char *name)
6276 {
6277 if (strncmp (name, "macc", 4) == 0)
6278 return FIX_VR4120_MACC;
6279 if (strncmp (name, "dmacc", 5) == 0)
6280 return FIX_VR4120_DMACC;
6281 if (strncmp (name, "mult", 4) == 0)
6282 return FIX_VR4120_MULT;
6283 if (strncmp (name, "dmult", 5) == 0)
6284 return FIX_VR4120_DMULT;
6285 if (strstr (name, "div"))
6286 return FIX_VR4120_DIV;
6287 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6288 return FIX_VR4120_MTHILO;
6289 return NUM_FIX_VR4120_CLASSES;
6290 }
6291
6292 #define INSN_ERET 0x42000018
6293 #define INSN_DERET 0x4200001f
6294 #define INSN_DMULT 0x1c
6295 #define INSN_DMULTU 0x1d
6296
6297 /* Return the number of instructions that must separate INSN1 and INSN2,
6298 where INSN1 is the earlier instruction. Return the worst-case value
6299 for any INSN2 if INSN2 is null. */
6300
6301 static unsigned int
6302 insns_between (const struct mips_cl_insn *insn1,
6303 const struct mips_cl_insn *insn2)
6304 {
6305 unsigned long pinfo1, pinfo2;
6306 unsigned int mask;
6307
6308 /* If INFO2 is null, pessimistically assume that all flags are set for
6309 the second instruction. */
6310 pinfo1 = insn1->insn_mo->pinfo;
6311 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
6312
6313 /* For most targets, write-after-read dependencies on the HI and LO
6314 registers must be separated by at least two instructions. */
6315 if (!hilo_interlocks)
6316 {
6317 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6318 return 2;
6319 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6320 return 2;
6321 }
6322
6323 /* If we're working around r7000 errata, there must be two instructions
6324 between an mfhi or mflo and any instruction that uses the result. */
6325 if (mips_7000_hilo_fix
6326 && !mips_opts.micromips
6327 && MF_HILO_INSN (pinfo1)
6328 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
6329 return 2;
6330
6331 /* If we're working around 24K errata, one instruction is required
6332 if an ERET or DERET is followed by a branch instruction. */
6333 if (mips_fix_24k && !mips_opts.micromips)
6334 {
6335 if (insn1->insn_opcode == INSN_ERET
6336 || insn1->insn_opcode == INSN_DERET)
6337 {
6338 if (insn2 == NULL
6339 || insn2->insn_opcode == INSN_ERET
6340 || insn2->insn_opcode == INSN_DERET
6341 || delayed_branch_p (insn2))
6342 return 1;
6343 }
6344 }
6345
6346 /* If we're working around PMC RM7000 errata, there must be three
6347 nops between a dmult and a load instruction. */
6348 if (mips_fix_rm7000 && !mips_opts.micromips)
6349 {
6350 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6351 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6352 {
6353 if (pinfo2 & INSN_LOAD_MEMORY)
6354 return 3;
6355 }
6356 }
6357
6358 /* If working around VR4120 errata, check for combinations that need
6359 a single intervening instruction. */
6360 if (mips_fix_vr4120 && !mips_opts.micromips)
6361 {
6362 unsigned int class1, class2;
6363
6364 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6365 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
6366 {
6367 if (insn2 == NULL)
6368 return 1;
6369 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6370 if (vr4120_conflicts[class1] & (1 << class2))
6371 return 1;
6372 }
6373 }
6374
6375 if (!HAVE_CODE_COMPRESSION)
6376 {
6377 /* Check for GPR or coprocessor load delays. All such delays
6378 are on the RT register. */
6379 /* Itbl support may require additional care here. */
6380 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
6381 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
6382 {
6383 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
6384 return 1;
6385 }
6386
6387 /* Check for generic coprocessor hazards.
6388
6389 This case is not handled very well. There is no special
6390 knowledge of CP0 handling, and the coprocessors other than
6391 the floating point unit are not distinguished at all. */
6392 /* Itbl support may require additional care here. FIXME!
6393 Need to modify this to include knowledge about
6394 user specified delays! */
6395 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
6396 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6397 {
6398 /* Handle cases where INSN1 writes to a known general coprocessor
6399 register. There must be a one instruction delay before INSN2
6400 if INSN2 reads that register, otherwise no delay is needed. */
6401 mask = fpr_write_mask (insn1);
6402 if (mask != 0)
6403 {
6404 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
6405 return 1;
6406 }
6407 else
6408 {
6409 /* Read-after-write dependencies on the control registers
6410 require a two-instruction gap. */
6411 if ((pinfo1 & INSN_WRITE_COND_CODE)
6412 && (pinfo2 & INSN_READ_COND_CODE))
6413 return 2;
6414
6415 /* We don't know exactly what INSN1 does. If INSN2 is
6416 also a coprocessor instruction, assume there must be
6417 a one instruction gap. */
6418 if (pinfo2 & INSN_COP)
6419 return 1;
6420 }
6421 }
6422
6423 /* Check for read-after-write dependencies on the coprocessor
6424 control registers in cases where INSN1 does not need a general
6425 coprocessor delay. This means that INSN1 is a floating point
6426 comparison instruction. */
6427 /* Itbl support may require additional care here. */
6428 else if (!cop_interlocks
6429 && (pinfo1 & INSN_WRITE_COND_CODE)
6430 && (pinfo2 & INSN_READ_COND_CODE))
6431 return 1;
6432 }
6433
6434 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6435 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6436 and pause. */
6437 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6438 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6439 || (insn2 && delayed_branch_p (insn2))))
6440 return 1;
6441
6442 return 0;
6443 }
6444
6445 /* Return the number of nops that would be needed to work around the
6446 VR4130 mflo/mfhi errata if instruction INSN immediately followed
6447 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6448 that are contained within the first IGNORE instructions of HIST. */
6449
6450 static int
6451 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
6452 const struct mips_cl_insn *insn)
6453 {
6454 int i, j;
6455 unsigned int mask;
6456
6457 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6458 are not affected by the errata. */
6459 if (insn != 0
6460 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6461 || strcmp (insn->insn_mo->name, "mtlo") == 0
6462 || strcmp (insn->insn_mo->name, "mthi") == 0))
6463 return 0;
6464
6465 /* Search for the first MFLO or MFHI. */
6466 for (i = 0; i < MAX_VR4130_NOPS; i++)
6467 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
6468 {
6469 /* Extract the destination register. */
6470 mask = gpr_write_mask (&hist[i]);
6471
6472 /* No nops are needed if INSN reads that register. */
6473 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
6474 return 0;
6475
6476 /* ...or if any of the intervening instructions do. */
6477 for (j = 0; j < i; j++)
6478 if (gpr_read_mask (&hist[j]) & mask)
6479 return 0;
6480
6481 if (i >= ignore)
6482 return MAX_VR4130_NOPS - i;
6483 }
6484 return 0;
6485 }
6486
6487 #define BASE_REG_EQ(INSN1, INSN2) \
6488 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
6489 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6490
6491 /* Return the minimum alignment for this store instruction. */
6492
6493 static int
6494 fix_24k_align_to (const struct mips_opcode *mo)
6495 {
6496 if (strcmp (mo->name, "sh") == 0)
6497 return 2;
6498
6499 if (strcmp (mo->name, "swc1") == 0
6500 || strcmp (mo->name, "swc2") == 0
6501 || strcmp (mo->name, "sw") == 0
6502 || strcmp (mo->name, "sc") == 0
6503 || strcmp (mo->name, "s.s") == 0)
6504 return 4;
6505
6506 if (strcmp (mo->name, "sdc1") == 0
6507 || strcmp (mo->name, "sdc2") == 0
6508 || strcmp (mo->name, "s.d") == 0)
6509 return 8;
6510
6511 /* sb, swl, swr */
6512 return 1;
6513 }
6514
6515 struct fix_24k_store_info
6516 {
6517 /* Immediate offset, if any, for this store instruction. */
6518 short off;
6519 /* Alignment required by this store instruction. */
6520 int align_to;
6521 /* True for register offsets. */
6522 int register_offset;
6523 };
6524
6525 /* Comparison function used by qsort. */
6526
6527 static int
6528 fix_24k_sort (const void *a, const void *b)
6529 {
6530 const struct fix_24k_store_info *pos1 = a;
6531 const struct fix_24k_store_info *pos2 = b;
6532
6533 return (pos1->off - pos2->off);
6534 }
6535
6536 /* INSN is a store instruction. Try to record the store information
6537 in STINFO. Return false if the information isn't known. */
6538
6539 static bfd_boolean
6540 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
6541 const struct mips_cl_insn *insn)
6542 {
6543 /* The instruction must have a known offset. */
6544 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6545 return FALSE;
6546
6547 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6548 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6549 return TRUE;
6550 }
6551
6552 /* Return the number of nops that would be needed to work around the 24k
6553 "lost data on stores during refill" errata if instruction INSN
6554 immediately followed the 2 instructions described by HIST.
6555 Ignore hazards that are contained within the first IGNORE
6556 instructions of HIST.
6557
6558 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6559 for the data cache refills and store data. The following describes
6560 the scenario where the store data could be lost.
6561
6562 * A data cache miss, due to either a load or a store, causing fill
6563 data to be supplied by the memory subsystem
6564 * The first three doublewords of fill data are returned and written
6565 into the cache
6566 * A sequence of four stores occurs in consecutive cycles around the
6567 final doubleword of the fill:
6568 * Store A
6569 * Store B
6570 * Store C
6571 * Zero, One or more instructions
6572 * Store D
6573
6574 The four stores A-D must be to different doublewords of the line that
6575 is being filled. The fourth instruction in the sequence above permits
6576 the fill of the final doubleword to be transferred from the FSB into
6577 the cache. In the sequence above, the stores may be either integer
6578 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6579 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6580 different doublewords on the line. If the floating point unit is
6581 running in 1:2 mode, it is not possible to create the sequence above
6582 using only floating point store instructions.
6583
6584 In this case, the cache line being filled is incorrectly marked
6585 invalid, thereby losing the data from any store to the line that
6586 occurs between the original miss and the completion of the five
6587 cycle sequence shown above.
6588
6589 The workarounds are:
6590
6591 * Run the data cache in write-through mode.
6592 * Insert a non-store instruction between
6593 Store A and Store B or Store B and Store C. */
6594
6595 static int
6596 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
6597 const struct mips_cl_insn *insn)
6598 {
6599 struct fix_24k_store_info pos[3];
6600 int align, i, base_offset;
6601
6602 if (ignore >= 2)
6603 return 0;
6604
6605 /* If the previous instruction wasn't a store, there's nothing to
6606 worry about. */
6607 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6608 return 0;
6609
6610 /* If the instructions after the previous one are unknown, we have
6611 to assume the worst. */
6612 if (!insn)
6613 return 1;
6614
6615 /* Check whether we are dealing with three consecutive stores. */
6616 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6617 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6618 return 0;
6619
6620 /* If we don't know the relationship between the store addresses,
6621 assume the worst. */
6622 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
6623 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6624 return 1;
6625
6626 if (!fix_24k_record_store_info (&pos[0], insn)
6627 || !fix_24k_record_store_info (&pos[1], &hist[0])
6628 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6629 return 1;
6630
6631 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6632
6633 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6634 X bytes and such that the base register + X is known to be aligned
6635 to align bytes. */
6636
6637 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6638 align = 8;
6639 else
6640 {
6641 align = pos[0].align_to;
6642 base_offset = pos[0].off;
6643 for (i = 1; i < 3; i++)
6644 if (align < pos[i].align_to)
6645 {
6646 align = pos[i].align_to;
6647 base_offset = pos[i].off;
6648 }
6649 for (i = 0; i < 3; i++)
6650 pos[i].off -= base_offset;
6651 }
6652
6653 pos[0].off &= ~align + 1;
6654 pos[1].off &= ~align + 1;
6655 pos[2].off &= ~align + 1;
6656
6657 /* If any two stores write to the same chunk, they also write to the
6658 same doubleword. The offsets are still sorted at this point. */
6659 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6660 return 0;
6661
6662 /* A range of at least 9 bytes is needed for the stores to be in
6663 non-overlapping doublewords. */
6664 if (pos[2].off - pos[0].off <= 8)
6665 return 0;
6666
6667 if (pos[2].off - pos[1].off >= 24
6668 || pos[1].off - pos[0].off >= 24
6669 || pos[2].off - pos[0].off >= 32)
6670 return 0;
6671
6672 return 1;
6673 }
6674
6675 /* Return the number of nops that would be needed if instruction INSN
6676 immediately followed the MAX_NOPS instructions given by HIST,
6677 where HIST[0] is the most recent instruction. Ignore hazards
6678 between INSN and the first IGNORE instructions in HIST.
6679
6680 If INSN is null, return the worse-case number of nops for any
6681 instruction. */
6682
6683 static int
6684 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
6685 const struct mips_cl_insn *insn)
6686 {
6687 int i, nops, tmp_nops;
6688
6689 nops = 0;
6690 for (i = ignore; i < MAX_DELAY_NOPS; i++)
6691 {
6692 tmp_nops = insns_between (hist + i, insn) - i;
6693 if (tmp_nops > nops)
6694 nops = tmp_nops;
6695 }
6696
6697 if (mips_fix_vr4130 && !mips_opts.micromips)
6698 {
6699 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
6700 if (tmp_nops > nops)
6701 nops = tmp_nops;
6702 }
6703
6704 if (mips_fix_24k && !mips_opts.micromips)
6705 {
6706 tmp_nops = nops_for_24k (ignore, hist, insn);
6707 if (tmp_nops > nops)
6708 nops = tmp_nops;
6709 }
6710
6711 return nops;
6712 }
6713
6714 /* The variable arguments provide NUM_INSNS extra instructions that
6715 might be added to HIST. Return the largest number of nops that
6716 would be needed after the extended sequence, ignoring hazards
6717 in the first IGNORE instructions. */
6718
6719 static int
6720 nops_for_sequence (int num_insns, int ignore,
6721 const struct mips_cl_insn *hist, ...)
6722 {
6723 va_list args;
6724 struct mips_cl_insn buffer[MAX_NOPS];
6725 struct mips_cl_insn *cursor;
6726 int nops;
6727
6728 va_start (args, hist);
6729 cursor = buffer + num_insns;
6730 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
6731 while (cursor > buffer)
6732 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6733
6734 nops = nops_for_insn (ignore, buffer, NULL);
6735 va_end (args);
6736 return nops;
6737 }
6738
6739 /* Like nops_for_insn, but if INSN is a branch, take into account the
6740 worst-case delay for the branch target. */
6741
6742 static int
6743 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
6744 const struct mips_cl_insn *insn)
6745 {
6746 int nops, tmp_nops;
6747
6748 nops = nops_for_insn (ignore, hist, insn);
6749 if (delayed_branch_p (insn))
6750 {
6751 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
6752 hist, insn, get_delay_slot_nop (insn));
6753 if (tmp_nops > nops)
6754 nops = tmp_nops;
6755 }
6756 else if (compact_branch_p (insn))
6757 {
6758 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
6759 if (tmp_nops > nops)
6760 nops = tmp_nops;
6761 }
6762 return nops;
6763 }
6764
6765 /* Fix NOP issue: Replace nops by "or at,at,zero". */
6766
6767 static void
6768 fix_loongson2f_nop (struct mips_cl_insn * ip)
6769 {
6770 gas_assert (!HAVE_CODE_COMPRESSION);
6771 if (strcmp (ip->insn_mo->name, "nop") == 0)
6772 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6773 }
6774
6775 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6776 jr target pc &= 'hffff_ffff_cfff_ffff. */
6777
6778 static void
6779 fix_loongson2f_jump (struct mips_cl_insn * ip)
6780 {
6781 gas_assert (!HAVE_CODE_COMPRESSION);
6782 if (strcmp (ip->insn_mo->name, "j") == 0
6783 || strcmp (ip->insn_mo->name, "jr") == 0
6784 || strcmp (ip->insn_mo->name, "jalr") == 0)
6785 {
6786 int sreg;
6787 expressionS ep;
6788
6789 if (! mips_opts.at)
6790 return;
6791
6792 sreg = EXTRACT_OPERAND (0, RS, *ip);
6793 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6794 return;
6795
6796 ep.X_op = O_constant;
6797 ep.X_add_number = 0xcfff0000;
6798 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6799 ep.X_add_number = 0xffff;
6800 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6801 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6802 }
6803 }
6804
6805 static void
6806 fix_loongson2f (struct mips_cl_insn * ip)
6807 {
6808 if (mips_fix_loongson2f_nop)
6809 fix_loongson2f_nop (ip);
6810
6811 if (mips_fix_loongson2f_jump)
6812 fix_loongson2f_jump (ip);
6813 }
6814
6815 /* IP is a branch that has a delay slot, and we need to fill it
6816 automatically. Return true if we can do that by swapping IP
6817 with the previous instruction.
6818 ADDRESS_EXPR is an operand of the instruction to be used with
6819 RELOC_TYPE. */
6820
6821 static bfd_boolean
6822 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
6823 bfd_reloc_code_real_type *reloc_type)
6824 {
6825 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
6826 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
6827 unsigned int fpr_read, prev_fpr_write;
6828
6829 /* -O2 and above is required for this optimization. */
6830 if (mips_optimize < 2)
6831 return FALSE;
6832
6833 /* If we have seen .set volatile or .set nomove, don't optimize. */
6834 if (mips_opts.nomove)
6835 return FALSE;
6836
6837 /* We can't swap if the previous instruction's position is fixed. */
6838 if (history[0].fixed_p)
6839 return FALSE;
6840
6841 /* If the previous previous insn was in a .set noreorder, we can't
6842 swap. Actually, the MIPS assembler will swap in this situation.
6843 However, gcc configured -with-gnu-as will generate code like
6844
6845 .set noreorder
6846 lw $4,XXX
6847 .set reorder
6848 INSN
6849 bne $4,$0,foo
6850
6851 in which we can not swap the bne and INSN. If gcc is not configured
6852 -with-gnu-as, it does not output the .set pseudo-ops. */
6853 if (history[1].noreorder_p)
6854 return FALSE;
6855
6856 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6857 This means that the previous instruction was a 4-byte one anyhow. */
6858 if (mips_opts.mips16 && history[0].fixp[0])
6859 return FALSE;
6860
6861 /* If the branch is itself the target of a branch, we can not swap.
6862 We cheat on this; all we check for is whether there is a label on
6863 this instruction. If there are any branches to anything other than
6864 a label, users must use .set noreorder. */
6865 if (seg_info (now_seg)->label_list)
6866 return FALSE;
6867
6868 /* If the previous instruction is in a variant frag other than this
6869 branch's one, we cannot do the swap. This does not apply to
6870 MIPS16 code, which uses variant frags for different purposes. */
6871 if (!mips_opts.mips16
6872 && history[0].frag
6873 && history[0].frag->fr_type == rs_machine_dependent)
6874 return FALSE;
6875
6876 /* We do not swap with instructions that cannot architecturally
6877 be placed in a branch delay slot, such as SYNC or ERET. We
6878 also refrain from swapping with a trap instruction, since it
6879 complicates trap handlers to have the trap instruction be in
6880 a delay slot. */
6881 prev_pinfo = history[0].insn_mo->pinfo;
6882 if (prev_pinfo & INSN_NO_DELAY_SLOT)
6883 return FALSE;
6884
6885 /* Check for conflicts between the branch and the instructions
6886 before the candidate delay slot. */
6887 if (nops_for_insn (0, history + 1, ip) > 0)
6888 return FALSE;
6889
6890 /* Check for conflicts between the swapped sequence and the
6891 target of the branch. */
6892 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6893 return FALSE;
6894
6895 /* If the branch reads a register that the previous
6896 instruction sets, we can not swap. */
6897 gpr_read = gpr_read_mask (ip);
6898 prev_gpr_write = gpr_write_mask (&history[0]);
6899 if (gpr_read & prev_gpr_write)
6900 return FALSE;
6901
6902 fpr_read = fpr_read_mask (ip);
6903 prev_fpr_write = fpr_write_mask (&history[0]);
6904 if (fpr_read & prev_fpr_write)
6905 return FALSE;
6906
6907 /* If the branch writes a register that the previous
6908 instruction sets, we can not swap. */
6909 gpr_write = gpr_write_mask (ip);
6910 if (gpr_write & prev_gpr_write)
6911 return FALSE;
6912
6913 /* If the branch writes a register that the previous
6914 instruction reads, we can not swap. */
6915 prev_gpr_read = gpr_read_mask (&history[0]);
6916 if (gpr_write & prev_gpr_read)
6917 return FALSE;
6918
6919 /* If one instruction sets a condition code and the
6920 other one uses a condition code, we can not swap. */
6921 pinfo = ip->insn_mo->pinfo;
6922 if ((pinfo & INSN_READ_COND_CODE)
6923 && (prev_pinfo & INSN_WRITE_COND_CODE))
6924 return FALSE;
6925 if ((pinfo & INSN_WRITE_COND_CODE)
6926 && (prev_pinfo & INSN_READ_COND_CODE))
6927 return FALSE;
6928
6929 /* If the previous instruction uses the PC, we can not swap. */
6930 prev_pinfo2 = history[0].insn_mo->pinfo2;
6931 if (prev_pinfo2 & INSN2_READ_PC)
6932 return FALSE;
6933
6934 /* If the previous instruction has an incorrect size for a fixed
6935 branch delay slot in microMIPS mode, we cannot swap. */
6936 pinfo2 = ip->insn_mo->pinfo2;
6937 if (mips_opts.micromips
6938 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6939 && insn_length (history) != 2)
6940 return FALSE;
6941 if (mips_opts.micromips
6942 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6943 && insn_length (history) != 4)
6944 return FALSE;
6945
6946 /* On R5900 short loops need to be fixed by inserting a nop in
6947 the branch delay slots.
6948 A short loop can be terminated too early. */
6949 if (mips_opts.arch == CPU_R5900
6950 /* Check if instruction has a parameter, ignore "j $31". */
6951 && (address_expr != NULL)
6952 /* Parameter must be 16 bit. */
6953 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6954 /* Branch to same segment. */
6955 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
6956 /* Branch to same code fragment. */
6957 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
6958 /* Can only calculate branch offset if value is known. */
6959 && symbol_constant_p (address_expr->X_add_symbol)
6960 /* Check if branch is really conditional. */
6961 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6962 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6963 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6964 {
6965 int distance;
6966 /* Check if loop is shorter than 6 instructions including
6967 branch and delay slot. */
6968 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
6969 if (distance <= 20)
6970 {
6971 int i;
6972 int rv;
6973
6974 rv = FALSE;
6975 /* When the loop includes branches or jumps,
6976 it is not a short loop. */
6977 for (i = 0; i < (distance / 4); i++)
6978 {
6979 if ((history[i].cleared_p)
6980 || delayed_branch_p (&history[i]))
6981 {
6982 rv = TRUE;
6983 break;
6984 }
6985 }
6986 if (!rv)
6987 {
6988 /* Insert nop after branch to fix short loop. */
6989 return FALSE;
6990 }
6991 }
6992 }
6993
6994 return TRUE;
6995 }
6996
6997 /* Decide how we should add IP to the instruction stream.
6998 ADDRESS_EXPR is an operand of the instruction to be used with
6999 RELOC_TYPE. */
7000
7001 static enum append_method
7002 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
7003 bfd_reloc_code_real_type *reloc_type)
7004 {
7005 /* The relaxed version of a macro sequence must be inherently
7006 hazard-free. */
7007 if (mips_relax.sequence == 2)
7008 return APPEND_ADD;
7009
7010 /* We must not dabble with instructions in a ".set noreorder" block. */
7011 if (mips_opts.noreorder)
7012 return APPEND_ADD;
7013
7014 /* Otherwise, it's our responsibility to fill branch delay slots. */
7015 if (delayed_branch_p (ip))
7016 {
7017 if (!branch_likely_p (ip)
7018 && can_swap_branch_p (ip, address_expr, reloc_type))
7019 return APPEND_SWAP;
7020
7021 if (mips_opts.mips16
7022 && ISA_SUPPORTS_MIPS16E
7023 && gpr_read_mask (ip) != 0)
7024 return APPEND_ADD_COMPACT;
7025
7026 if (mips_opts.micromips
7027 && ((ip->insn_opcode & 0xffe0) == 0x4580
7028 || (!forced_insn_length
7029 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7030 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7031 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7032 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7033 return APPEND_ADD_COMPACT;
7034
7035 return APPEND_ADD_WITH_NOP;
7036 }
7037
7038 return APPEND_ADD;
7039 }
7040
7041 /* IP is an instruction whose opcode we have just changed, END points
7042 to the end of the opcode table processed. Point IP->insn_mo to the
7043 new opcode's definition. */
7044
7045 static void
7046 find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
7047 {
7048 const struct mips_opcode *mo;
7049
7050 for (mo = ip->insn_mo; mo < end; mo++)
7051 if (mo->pinfo != INSN_MACRO
7052 && (ip->insn_opcode & mo->mask) == mo->match)
7053 {
7054 ip->insn_mo = mo;
7055 return;
7056 }
7057 abort ();
7058 }
7059
7060 /* IP is a MIPS16 instruction whose opcode we have just changed.
7061 Point IP->insn_mo to the new opcode's definition. */
7062
7063 static void
7064 find_altered_mips16_opcode (struct mips_cl_insn *ip)
7065 {
7066 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7067 }
7068
7069 /* IP is a microMIPS instruction whose opcode we have just changed.
7070 Point IP->insn_mo to the new opcode's definition. */
7071
7072 static void
7073 find_altered_micromips_opcode (struct mips_cl_insn *ip)
7074 {
7075 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7076 }
7077
7078 /* For microMIPS macros, we need to generate a local number label
7079 as the target of branches. */
7080 #define MICROMIPS_LABEL_CHAR '\037'
7081 static unsigned long micromips_target_label;
7082 static char micromips_target_name[32];
7083
7084 static char *
7085 micromips_label_name (void)
7086 {
7087 char *p = micromips_target_name;
7088 char symbol_name_temporary[24];
7089 unsigned long l;
7090 int i;
7091
7092 if (*p)
7093 return p;
7094
7095 i = 0;
7096 l = micromips_target_label;
7097 #ifdef LOCAL_LABEL_PREFIX
7098 *p++ = LOCAL_LABEL_PREFIX;
7099 #endif
7100 *p++ = 'L';
7101 *p++ = MICROMIPS_LABEL_CHAR;
7102 do
7103 {
7104 symbol_name_temporary[i++] = l % 10 + '0';
7105 l /= 10;
7106 }
7107 while (l != 0);
7108 while (i > 0)
7109 *p++ = symbol_name_temporary[--i];
7110 *p = '\0';
7111
7112 return micromips_target_name;
7113 }
7114
7115 static void
7116 micromips_label_expr (expressionS *label_expr)
7117 {
7118 label_expr->X_op = O_symbol;
7119 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7120 label_expr->X_add_number = 0;
7121 }
7122
7123 static void
7124 micromips_label_inc (void)
7125 {
7126 micromips_target_label++;
7127 *micromips_target_name = '\0';
7128 }
7129
7130 static void
7131 micromips_add_label (void)
7132 {
7133 symbolS *s;
7134
7135 s = colon (micromips_label_name ());
7136 micromips_label_inc ();
7137 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
7138 }
7139
7140 /* If assembling microMIPS code, then return the microMIPS reloc
7141 corresponding to the requested one if any. Otherwise return
7142 the reloc unchanged. */
7143
7144 static bfd_reloc_code_real_type
7145 micromips_map_reloc (bfd_reloc_code_real_type reloc)
7146 {
7147 static const bfd_reloc_code_real_type relocs[][2] =
7148 {
7149 /* Keep sorted incrementally by the left-hand key. */
7150 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7151 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7152 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7153 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7154 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7155 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7156 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7157 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7158 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7159 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7160 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7161 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7162 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7163 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7164 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7165 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7166 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7167 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7168 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7169 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7170 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7171 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7172 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7173 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7174 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7175 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7176 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7177 };
7178 bfd_reloc_code_real_type r;
7179 size_t i;
7180
7181 if (!mips_opts.micromips)
7182 return reloc;
7183 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7184 {
7185 r = relocs[i][0];
7186 if (r > reloc)
7187 return reloc;
7188 if (r == reloc)
7189 return relocs[i][1];
7190 }
7191 return reloc;
7192 }
7193
7194 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7195 Return true on success, storing the resolved value in RESULT. */
7196
7197 static bfd_boolean
7198 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7199 offsetT *result)
7200 {
7201 switch (reloc)
7202 {
7203 case BFD_RELOC_MIPS_HIGHEST:
7204 case BFD_RELOC_MICROMIPS_HIGHEST:
7205 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7206 return TRUE;
7207
7208 case BFD_RELOC_MIPS_HIGHER:
7209 case BFD_RELOC_MICROMIPS_HIGHER:
7210 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7211 return TRUE;
7212
7213 case BFD_RELOC_HI16_S:
7214 case BFD_RELOC_HI16_S_PCREL:
7215 case BFD_RELOC_MICROMIPS_HI16_S:
7216 case BFD_RELOC_MIPS16_HI16_S:
7217 *result = ((operand + 0x8000) >> 16) & 0xffff;
7218 return TRUE;
7219
7220 case BFD_RELOC_HI16:
7221 case BFD_RELOC_MICROMIPS_HI16:
7222 case BFD_RELOC_MIPS16_HI16:
7223 *result = (operand >> 16) & 0xffff;
7224 return TRUE;
7225
7226 case BFD_RELOC_LO16:
7227 case BFD_RELOC_LO16_PCREL:
7228 case BFD_RELOC_MICROMIPS_LO16:
7229 case BFD_RELOC_MIPS16_LO16:
7230 *result = operand & 0xffff;
7231 return TRUE;
7232
7233 case BFD_RELOC_UNUSED:
7234 *result = operand;
7235 return TRUE;
7236
7237 default:
7238 return FALSE;
7239 }
7240 }
7241
7242 /* Output an instruction. IP is the instruction information.
7243 ADDRESS_EXPR is an operand of the instruction to be used with
7244 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7245 a macro expansion. */
7246
7247 static void
7248 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
7249 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
7250 {
7251 unsigned long prev_pinfo2, pinfo;
7252 bfd_boolean relaxed_branch = FALSE;
7253 enum append_method method;
7254 bfd_boolean relax32;
7255 int branch_disp;
7256
7257 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
7258 fix_loongson2f (ip);
7259
7260 file_ase_mips16 |= mips_opts.mips16;
7261 file_ase_micromips |= mips_opts.micromips;
7262
7263 prev_pinfo2 = history[0].insn_mo->pinfo2;
7264 pinfo = ip->insn_mo->pinfo;
7265
7266 /* Don't raise alarm about `nods' frags as they'll fill in the right
7267 kind of nop in relaxation if required. */
7268 if (mips_opts.micromips
7269 && !expansionp
7270 && !(history[0].frag
7271 && history[0].frag->fr_type == rs_machine_dependent
7272 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7273 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
7274 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7275 && micromips_insn_length (ip->insn_mo) != 2)
7276 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7277 && micromips_insn_length (ip->insn_mo) != 4)))
7278 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
7279 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
7280
7281 if (address_expr == NULL)
7282 ip->complete_p = 1;
7283 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7284 && reloc_type[1] == BFD_RELOC_UNUSED
7285 && reloc_type[2] == BFD_RELOC_UNUSED
7286 && address_expr->X_op == O_constant)
7287 {
7288 switch (*reloc_type)
7289 {
7290 case BFD_RELOC_MIPS_JMP:
7291 {
7292 int shift;
7293
7294 /* Shift is 2, unusually, for microMIPS JALX. */
7295 shift = (mips_opts.micromips
7296 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
7297 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7298 as_bad (_("jump to misaligned address (0x%lx)"),
7299 (unsigned long) address_expr->X_add_number);
7300 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7301 & 0x3ffffff);
7302 ip->complete_p = 1;
7303 }
7304 break;
7305
7306 case BFD_RELOC_MIPS16_JMP:
7307 if ((address_expr->X_add_number & 3) != 0)
7308 as_bad (_("jump to misaligned address (0x%lx)"),
7309 (unsigned long) address_expr->X_add_number);
7310 ip->insn_opcode |=
7311 (((address_expr->X_add_number & 0x7c0000) << 3)
7312 | ((address_expr->X_add_number & 0xf800000) >> 7)
7313 | ((address_expr->X_add_number & 0x3fffc) >> 2));
7314 ip->complete_p = 1;
7315 break;
7316
7317 case BFD_RELOC_16_PCREL_S2:
7318 {
7319 int shift;
7320
7321 shift = mips_opts.micromips ? 1 : 2;
7322 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7323 as_bad (_("branch to misaligned address (0x%lx)"),
7324 (unsigned long) address_expr->X_add_number);
7325 if (!mips_relax_branch)
7326 {
7327 if ((address_expr->X_add_number + (1 << (shift + 15)))
7328 & ~((1 << (shift + 16)) - 1))
7329 as_bad (_("branch address range overflow (0x%lx)"),
7330 (unsigned long) address_expr->X_add_number);
7331 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7332 & 0xffff);
7333 }
7334 }
7335 break;
7336
7337 case BFD_RELOC_MIPS_21_PCREL_S2:
7338 {
7339 int shift;
7340
7341 shift = 2;
7342 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7343 as_bad (_("branch to misaligned address (0x%lx)"),
7344 (unsigned long) address_expr->X_add_number);
7345 if ((address_expr->X_add_number + (1 << (shift + 20)))
7346 & ~((1 << (shift + 21)) - 1))
7347 as_bad (_("branch address range overflow (0x%lx)"),
7348 (unsigned long) address_expr->X_add_number);
7349 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7350 & 0x1fffff);
7351 }
7352 break;
7353
7354 case BFD_RELOC_MIPS_26_PCREL_S2:
7355 {
7356 int shift;
7357
7358 shift = 2;
7359 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7360 as_bad (_("branch to misaligned address (0x%lx)"),
7361 (unsigned long) address_expr->X_add_number);
7362 if ((address_expr->X_add_number + (1 << (shift + 25)))
7363 & ~((1 << (shift + 26)) - 1))
7364 as_bad (_("branch address range overflow (0x%lx)"),
7365 (unsigned long) address_expr->X_add_number);
7366 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7367 & 0x3ffffff);
7368 }
7369 break;
7370
7371 default:
7372 {
7373 offsetT value;
7374
7375 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7376 &value))
7377 {
7378 ip->insn_opcode |= value & 0xffff;
7379 ip->complete_p = 1;
7380 }
7381 }
7382 break;
7383 }
7384 }
7385
7386 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7387 {
7388 /* There are a lot of optimizations we could do that we don't.
7389 In particular, we do not, in general, reorder instructions.
7390 If you use gcc with optimization, it will reorder
7391 instructions and generally do much more optimization then we
7392 do here; repeating all that work in the assembler would only
7393 benefit hand written assembly code, and does not seem worth
7394 it. */
7395 int nops = (mips_optimize == 0
7396 ? nops_for_insn (0, history, NULL)
7397 : nops_for_insn_or_target (0, history, ip));
7398 if (nops > 0)
7399 {
7400 fragS *old_frag;
7401 unsigned long old_frag_offset;
7402 int i;
7403
7404 old_frag = frag_now;
7405 old_frag_offset = frag_now_fix ();
7406
7407 for (i = 0; i < nops; i++)
7408 add_fixed_insn (NOP_INSN);
7409 insert_into_history (0, nops, NOP_INSN);
7410
7411 if (listing)
7412 {
7413 listing_prev_line ();
7414 /* We may be at the start of a variant frag. In case we
7415 are, make sure there is enough space for the frag
7416 after the frags created by listing_prev_line. The
7417 argument to frag_grow here must be at least as large
7418 as the argument to all other calls to frag_grow in
7419 this file. We don't have to worry about being in the
7420 middle of a variant frag, because the variants insert
7421 all needed nop instructions themselves. */
7422 frag_grow (40);
7423 }
7424
7425 mips_move_text_labels ();
7426
7427 #ifndef NO_ECOFF_DEBUGGING
7428 if (ECOFF_DEBUGGING)
7429 ecoff_fix_loc (old_frag, old_frag_offset);
7430 #endif
7431 }
7432 }
7433 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7434 {
7435 int nops;
7436
7437 /* Work out how many nops in prev_nop_frag are needed by IP,
7438 ignoring hazards generated by the first prev_nop_frag_since
7439 instructions. */
7440 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
7441 gas_assert (nops <= prev_nop_frag_holds);
7442
7443 /* Enforce NOPS as a minimum. */
7444 if (nops > prev_nop_frag_required)
7445 prev_nop_frag_required = nops;
7446
7447 if (prev_nop_frag_holds == prev_nop_frag_required)
7448 {
7449 /* Settle for the current number of nops. Update the history
7450 accordingly (for the benefit of any future .set reorder code). */
7451 prev_nop_frag = NULL;
7452 insert_into_history (prev_nop_frag_since,
7453 prev_nop_frag_holds, NOP_INSN);
7454 }
7455 else
7456 {
7457 /* Allow this instruction to replace one of the nops that was
7458 tentatively added to prev_nop_frag. */
7459 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
7460 prev_nop_frag_holds--;
7461 prev_nop_frag_since++;
7462 }
7463 }
7464
7465 method = get_append_method (ip, address_expr, reloc_type);
7466 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
7467
7468 dwarf2_emit_insn (0);
7469 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7470 so "move" the instruction address accordingly.
7471
7472 Also, it doesn't seem appropriate for the assembler to reorder .loc
7473 entries. If this instruction is a branch that we are going to swap
7474 with the previous instruction, the two instructions should be
7475 treated as a unit, and the debug information for both instructions
7476 should refer to the start of the branch sequence. Using the
7477 current position is certainly wrong when swapping a 32-bit branch
7478 and a 16-bit delay slot, since the current position would then be
7479 in the middle of a branch. */
7480 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
7481
7482 relax32 = (mips_relax_branch
7483 /* Don't try branch relaxation within .set nomacro, or within
7484 .set noat if we use $at for PIC computations. If it turns
7485 out that the branch was out-of-range, we'll get an error. */
7486 && !mips_opts.warn_about_macros
7487 && (mips_opts.at || mips_pic == NO_PIC)
7488 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7489 as they have no complementing branches. */
7490 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
7491
7492 if (!HAVE_CODE_COMPRESSION
7493 && address_expr
7494 && relax32
7495 && *reloc_type == BFD_RELOC_16_PCREL_S2
7496 && delayed_branch_p (ip))
7497 {
7498 relaxed_branch = TRUE;
7499 add_relaxed_insn (ip, (relaxed_branch_length
7500 (NULL, NULL,
7501 uncond_branch_p (ip) ? -1
7502 : branch_likely_p (ip) ? 1
7503 : 0)), 4,
7504 RELAX_BRANCH_ENCODE
7505 (AT, mips_pic != NO_PIC,
7506 uncond_branch_p (ip),
7507 branch_likely_p (ip),
7508 pinfo & INSN_WRITE_GPR_31,
7509 0),
7510 address_expr->X_add_symbol,
7511 address_expr->X_add_number);
7512 *reloc_type = BFD_RELOC_UNUSED;
7513 }
7514 else if (mips_opts.micromips
7515 && address_expr
7516 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7517 || *reloc_type > BFD_RELOC_UNUSED)
7518 && (delayed_branch_p (ip) || compact_branch_p (ip))
7519 /* Don't try branch relaxation when users specify
7520 16-bit/32-bit instructions. */
7521 && !forced_insn_length)
7522 {
7523 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7524 && *reloc_type > BFD_RELOC_UNUSED);
7525 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
7526 int uncond = uncond_branch_p (ip) ? -1 : 0;
7527 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7528 int nods = method == APPEND_ADD_WITH_NOP;
7529 int al = pinfo & INSN_WRITE_GPR_31;
7530 int length32 = nods ? 8 : 4;
7531
7532 gas_assert (address_expr != NULL);
7533 gas_assert (!mips_relax.sequence);
7534
7535 relaxed_branch = TRUE;
7536 if (nods)
7537 method = APPEND_ADD;
7538 if (relax32)
7539 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7540 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
7541 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
7542 mips_pic != NO_PIC,
7543 uncond, compact, al, nods,
7544 relax32, 0, 0),
7545 address_expr->X_add_symbol,
7546 address_expr->X_add_number);
7547 *reloc_type = BFD_RELOC_UNUSED;
7548 }
7549 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
7550 {
7551 bfd_boolean require_unextended;
7552 bfd_boolean require_extended;
7553 symbolS *symbol;
7554 offsetT offset;
7555
7556 if (forced_insn_length != 0)
7557 {
7558 require_unextended = forced_insn_length == 2;
7559 require_extended = forced_insn_length == 4;
7560 }
7561 else
7562 {
7563 require_unextended = (mips_opts.noautoextend
7564 && !mips_opcode_32bit_p (ip->insn_mo));
7565 require_extended = 0;
7566 }
7567
7568 /* We need to set up a variant frag. */
7569 gas_assert (address_expr != NULL);
7570 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7571 symbol created by `make_expr_symbol' may not get a necessary
7572 external relocation produced. */
7573 if (address_expr->X_op == O_symbol)
7574 {
7575 symbol = address_expr->X_add_symbol;
7576 offset = address_expr->X_add_number;
7577 }
7578 else
7579 {
7580 symbol = make_expr_symbol (address_expr);
7581 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
7582 offset = 0;
7583 }
7584 add_relaxed_insn (ip, 12, 0,
7585 RELAX_MIPS16_ENCODE
7586 (*reloc_type - BFD_RELOC_UNUSED,
7587 mips_opts.ase & ASE_MIPS16E2,
7588 mips_pic != NO_PIC,
7589 HAVE_32BIT_SYMBOLS,
7590 mips_opts.warn_about_macros,
7591 require_unextended, require_extended,
7592 delayed_branch_p (&history[0]),
7593 history[0].mips16_absolute_jump_p),
7594 symbol, offset);
7595 }
7596 else if (mips_opts.mips16 && insn_length (ip) == 2)
7597 {
7598 if (!delayed_branch_p (ip))
7599 /* Make sure there is enough room to swap this instruction with
7600 a following jump instruction. */
7601 frag_grow (6);
7602 add_fixed_insn (ip);
7603 }
7604 else
7605 {
7606 if (mips_opts.mips16
7607 && mips_opts.noreorder
7608 && delayed_branch_p (&history[0]))
7609 as_warn (_("extended instruction in delay slot"));
7610
7611 if (mips_relax.sequence)
7612 {
7613 /* If we've reached the end of this frag, turn it into a variant
7614 frag and record the information for the instructions we've
7615 written so far. */
7616 if (frag_room () < 4)
7617 relax_close_frag ();
7618 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
7619 }
7620
7621 if (mips_relax.sequence != 2)
7622 {
7623 if (mips_macro_warning.first_insn_sizes[0] == 0)
7624 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7625 mips_macro_warning.sizes[0] += insn_length (ip);
7626 mips_macro_warning.insns[0]++;
7627 }
7628 if (mips_relax.sequence != 1)
7629 {
7630 if (mips_macro_warning.first_insn_sizes[1] == 0)
7631 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7632 mips_macro_warning.sizes[1] += insn_length (ip);
7633 mips_macro_warning.insns[1]++;
7634 }
7635
7636 if (mips_opts.mips16)
7637 {
7638 ip->fixed_p = 1;
7639 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7640 }
7641 add_fixed_insn (ip);
7642 }
7643
7644 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
7645 {
7646 bfd_reloc_code_real_type final_type[3];
7647 reloc_howto_type *howto0;
7648 reloc_howto_type *howto;
7649 int i;
7650
7651 /* Perform any necessary conversion to microMIPS relocations
7652 and find out how many relocations there actually are. */
7653 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7654 final_type[i] = micromips_map_reloc (reloc_type[i]);
7655
7656 /* In a compound relocation, it is the final (outermost)
7657 operator that determines the relocated field. */
7658 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
7659 if (!howto)
7660 abort ();
7661
7662 if (i > 1)
7663 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
7664 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7665 bfd_get_reloc_size (howto),
7666 address_expr,
7667 howto0 && howto0->pc_relative,
7668 final_type[0]);
7669 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7670 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
7671
7672 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
7673 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
7674 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7675
7676 /* These relocations can have an addend that won't fit in
7677 4 octets for 64bit assembly. */
7678 if (GPR_SIZE == 64
7679 && ! howto->partial_inplace
7680 && (reloc_type[0] == BFD_RELOC_16
7681 || reloc_type[0] == BFD_RELOC_32
7682 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7683 || reloc_type[0] == BFD_RELOC_GPREL16
7684 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7685 || reloc_type[0] == BFD_RELOC_GPREL32
7686 || reloc_type[0] == BFD_RELOC_64
7687 || reloc_type[0] == BFD_RELOC_CTOR
7688 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7689 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7690 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7691 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7692 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7693 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7694 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7695 || hi16_reloc_p (reloc_type[0])
7696 || lo16_reloc_p (reloc_type[0])))
7697 ip->fixp[0]->fx_no_overflow = 1;
7698
7699 /* These relocations can have an addend that won't fit in 2 octets. */
7700 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7701 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7702 ip->fixp[0]->fx_no_overflow = 1;
7703
7704 if (mips_relax.sequence)
7705 {
7706 if (mips_relax.first_fixup == 0)
7707 mips_relax.first_fixup = ip->fixp[0];
7708 }
7709 else if (reloc_needs_lo_p (*reloc_type))
7710 {
7711 struct mips_hi_fixup *hi_fixup;
7712
7713 /* Reuse the last entry if it already has a matching %lo. */
7714 hi_fixup = mips_hi_fixup_list;
7715 if (hi_fixup == 0
7716 || !fixup_has_matching_lo_p (hi_fixup->fixp))
7717 {
7718 hi_fixup = XNEW (struct mips_hi_fixup);
7719 hi_fixup->next = mips_hi_fixup_list;
7720 mips_hi_fixup_list = hi_fixup;
7721 }
7722 hi_fixup->fixp = ip->fixp[0];
7723 hi_fixup->seg = now_seg;
7724 }
7725
7726 /* Add fixups for the second and third relocations, if given.
7727 Note that the ABI allows the second relocation to be
7728 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7729 moment we only use RSS_UNDEF, but we could add support
7730 for the others if it ever becomes necessary. */
7731 for (i = 1; i < 3; i++)
7732 if (reloc_type[i] != BFD_RELOC_UNUSED)
7733 {
7734 ip->fixp[i] = fix_new (ip->frag, ip->where,
7735 ip->fixp[0]->fx_size, NULL, 0,
7736 FALSE, final_type[i]);
7737
7738 /* Use fx_tcbit to mark compound relocs. */
7739 ip->fixp[0]->fx_tcbit = 1;
7740 ip->fixp[i]->fx_tcbit = 1;
7741 }
7742 }
7743
7744 /* Update the register mask information. */
7745 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7746 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
7747
7748 switch (method)
7749 {
7750 case APPEND_ADD:
7751 insert_into_history (0, 1, ip);
7752 break;
7753
7754 case APPEND_ADD_WITH_NOP:
7755 {
7756 struct mips_cl_insn *nop;
7757
7758 insert_into_history (0, 1, ip);
7759 nop = get_delay_slot_nop (ip);
7760 add_fixed_insn (nop);
7761 insert_into_history (0, 1, nop);
7762 if (mips_relax.sequence)
7763 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7764 }
7765 break;
7766
7767 case APPEND_ADD_COMPACT:
7768 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7769 if (mips_opts.mips16)
7770 {
7771 ip->insn_opcode |= 0x0080;
7772 find_altered_mips16_opcode (ip);
7773 }
7774 /* Convert microMIPS instructions. */
7775 else if (mips_opts.micromips)
7776 {
7777 /* jr16->jrc */
7778 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7779 ip->insn_opcode |= 0x0020;
7780 /* b16->bc */
7781 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7782 ip->insn_opcode = 0x40e00000;
7783 /* beqz16->beqzc, bnez16->bnezc */
7784 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7785 {
7786 unsigned long regno;
7787
7788 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7789 regno &= MICROMIPSOP_MASK_MD;
7790 regno = micromips_to_32_reg_d_map[regno];
7791 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7792 | (regno << MICROMIPSOP_SH_RS)
7793 | 0x40a00000) ^ 0x00400000;
7794 }
7795 /* beqz->beqzc, bnez->bnezc */
7796 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7797 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7798 | ((ip->insn_opcode >> 7) & 0x00400000)
7799 | 0x40a00000) ^ 0x00400000;
7800 /* beq $0->beqzc, bne $0->bnezc */
7801 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7802 ip->insn_opcode = (((ip->insn_opcode >>
7803 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
7804 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
7805 | ((ip->insn_opcode >> 7) & 0x00400000)
7806 | 0x40a00000) ^ 0x00400000;
7807 else
7808 abort ();
7809 find_altered_micromips_opcode (ip);
7810 }
7811 else
7812 abort ();
7813 install_insn (ip);
7814 insert_into_history (0, 1, ip);
7815 break;
7816
7817 case APPEND_SWAP:
7818 {
7819 struct mips_cl_insn delay = history[0];
7820
7821 if (relaxed_branch || delay.frag != ip->frag)
7822 {
7823 /* Add the delay slot instruction to the end of the
7824 current frag and shrink the fixed part of the
7825 original frag. If the branch occupies the tail of
7826 the latter, move it backwards to cover the gap. */
7827 delay.frag->fr_fix -= branch_disp;
7828 if (delay.frag == ip->frag)
7829 move_insn (ip, ip->frag, ip->where - branch_disp);
7830 add_fixed_insn (&delay);
7831 }
7832 else
7833 {
7834 /* If this is not a relaxed branch and we are in the
7835 same frag, then just swap the instructions. */
7836 move_insn (ip, delay.frag, delay.where);
7837 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
7838 }
7839 history[0] = *ip;
7840 delay.fixed_p = 1;
7841 insert_into_history (0, 1, &delay);
7842 }
7843 break;
7844 }
7845
7846 /* If we have just completed an unconditional branch, clear the history. */
7847 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7848 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
7849 {
7850 unsigned int i;
7851
7852 mips_no_prev_insn ();
7853
7854 for (i = 0; i < ARRAY_SIZE (history); i++)
7855 history[i].cleared_p = 1;
7856 }
7857
7858 /* We need to emit a label at the end of branch-likely macros. */
7859 if (emit_branch_likely_macro)
7860 {
7861 emit_branch_likely_macro = FALSE;
7862 micromips_add_label ();
7863 }
7864
7865 /* We just output an insn, so the next one doesn't have a label. */
7866 mips_clear_insn_labels ();
7867 }
7868
7869 /* Forget that there was any previous instruction or label.
7870 When BRANCH is true, the branch history is also flushed. */
7871
7872 static void
7873 mips_no_prev_insn (void)
7874 {
7875 prev_nop_frag = NULL;
7876 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
7877 mips_clear_insn_labels ();
7878 }
7879
7880 /* This function must be called before we emit something other than
7881 instructions. It is like mips_no_prev_insn except that it inserts
7882 any NOPS that might be needed by previous instructions. */
7883
7884 void
7885 mips_emit_delays (void)
7886 {
7887 if (! mips_opts.noreorder)
7888 {
7889 int nops = nops_for_insn (0, history, NULL);
7890 if (nops > 0)
7891 {
7892 while (nops-- > 0)
7893 add_fixed_insn (NOP_INSN);
7894 mips_move_text_labels ();
7895 }
7896 }
7897 mips_no_prev_insn ();
7898 }
7899
7900 /* Start a (possibly nested) noreorder block. */
7901
7902 static void
7903 start_noreorder (void)
7904 {
7905 if (mips_opts.noreorder == 0)
7906 {
7907 unsigned int i;
7908 int nops;
7909
7910 /* None of the instructions before the .set noreorder can be moved. */
7911 for (i = 0; i < ARRAY_SIZE (history); i++)
7912 history[i].fixed_p = 1;
7913
7914 /* Insert any nops that might be needed between the .set noreorder
7915 block and the previous instructions. We will later remove any
7916 nops that turn out not to be needed. */
7917 nops = nops_for_insn (0, history, NULL);
7918 if (nops > 0)
7919 {
7920 if (mips_optimize != 0)
7921 {
7922 /* Record the frag which holds the nop instructions, so
7923 that we can remove them if we don't need them. */
7924 frag_grow (nops * NOP_INSN_SIZE);
7925 prev_nop_frag = frag_now;
7926 prev_nop_frag_holds = nops;
7927 prev_nop_frag_required = 0;
7928 prev_nop_frag_since = 0;
7929 }
7930
7931 for (; nops > 0; --nops)
7932 add_fixed_insn (NOP_INSN);
7933
7934 /* Move on to a new frag, so that it is safe to simply
7935 decrease the size of prev_nop_frag. */
7936 frag_wane (frag_now);
7937 frag_new (0);
7938 mips_move_text_labels ();
7939 }
7940 mips_mark_labels ();
7941 mips_clear_insn_labels ();
7942 }
7943 mips_opts.noreorder++;
7944 mips_any_noreorder = 1;
7945 }
7946
7947 /* End a nested noreorder block. */
7948
7949 static void
7950 end_noreorder (void)
7951 {
7952 mips_opts.noreorder--;
7953 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7954 {
7955 /* Commit to inserting prev_nop_frag_required nops and go back to
7956 handling nop insertion the .set reorder way. */
7957 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
7958 * NOP_INSN_SIZE);
7959 insert_into_history (prev_nop_frag_since,
7960 prev_nop_frag_required, NOP_INSN);
7961 prev_nop_frag = NULL;
7962 }
7963 }
7964
7965 /* Sign-extend 32-bit mode constants that have bit 31 set and all
7966 higher bits unset. */
7967
7968 static void
7969 normalize_constant_expr (expressionS *ex)
7970 {
7971 if (ex->X_op == O_constant
7972 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7973 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7974 - 0x80000000);
7975 }
7976
7977 /* Sign-extend 32-bit mode address offsets that have bit 31 set and
7978 all higher bits unset. */
7979
7980 static void
7981 normalize_address_expr (expressionS *ex)
7982 {
7983 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7984 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7985 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7986 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7987 - 0x80000000);
7988 }
7989
7990 /* Try to match TOKENS against OPCODE, storing the result in INSN.
7991 Return true if the match was successful.
7992
7993 OPCODE_EXTRA is a value that should be ORed into the opcode
7994 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
7995 there are more alternatives after OPCODE and SOFT_MATCH is
7996 as for mips_arg_info. */
7997
7998 static bfd_boolean
7999 match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8000 struct mips_operand_token *tokens, unsigned int opcode_extra,
8001 bfd_boolean lax_match, bfd_boolean complete_p)
8002 {
8003 const char *args;
8004 struct mips_arg_info arg;
8005 const struct mips_operand *operand;
8006 char c;
8007
8008 imm_expr.X_op = O_absent;
8009 offset_expr.X_op = O_absent;
8010 offset_reloc[0] = BFD_RELOC_UNUSED;
8011 offset_reloc[1] = BFD_RELOC_UNUSED;
8012 offset_reloc[2] = BFD_RELOC_UNUSED;
8013
8014 create_insn (insn, opcode);
8015 /* When no opcode suffix is specified, assume ".xyzw". */
8016 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8017 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8018 else
8019 insn->insn_opcode |= opcode_extra;
8020 memset (&arg, 0, sizeof (arg));
8021 arg.insn = insn;
8022 arg.token = tokens;
8023 arg.argnum = 1;
8024 arg.last_regno = ILLEGAL_REG;
8025 arg.dest_regno = ILLEGAL_REG;
8026 arg.lax_match = lax_match;
8027 for (args = opcode->args;; ++args)
8028 {
8029 if (arg.token->type == OT_END)
8030 {
8031 /* Handle unary instructions in which only one operand is given.
8032 The source is then the same as the destination. */
8033 if (arg.opnum == 1 && *args == ',')
8034 {
8035 operand = (mips_opts.micromips
8036 ? decode_micromips_operand (args + 1)
8037 : decode_mips_operand (args + 1));
8038 if (operand && mips_optional_operand_p (operand))
8039 {
8040 arg.token = tokens;
8041 arg.argnum = 1;
8042 continue;
8043 }
8044 }
8045
8046 /* Treat elided base registers as $0. */
8047 if (strcmp (args, "(b)") == 0)
8048 args += 3;
8049
8050 if (args[0] == '+')
8051 switch (args[1])
8052 {
8053 case 'K':
8054 case 'N':
8055 /* The register suffix is optional. */
8056 args += 2;
8057 break;
8058 }
8059
8060 /* Fail the match if there were too few operands. */
8061 if (*args)
8062 return FALSE;
8063
8064 /* Successful match. */
8065 if (!complete_p)
8066 return TRUE;
8067 clear_insn_error ();
8068 if (arg.dest_regno == arg.last_regno
8069 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8070 {
8071 if (arg.opnum == 2)
8072 set_insn_error
8073 (0, _("source and destination must be different"));
8074 else if (arg.last_regno == 31)
8075 set_insn_error
8076 (0, _("a destination register must be supplied"));
8077 }
8078 else if (arg.last_regno == 31
8079 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8080 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8081 set_insn_error (0, _("the source register must not be $31"));
8082 check_completed_insn (&arg);
8083 return TRUE;
8084 }
8085
8086 /* Fail the match if the line has too many operands. */
8087 if (*args == 0)
8088 return FALSE;
8089
8090 /* Handle characters that need to match exactly. */
8091 if (*args == '(' || *args == ')' || *args == ',')
8092 {
8093 if (match_char (&arg, *args))
8094 continue;
8095 return FALSE;
8096 }
8097 if (*args == '#')
8098 {
8099 ++args;
8100 if (arg.token->type == OT_DOUBLE_CHAR
8101 && arg.token->u.ch == *args)
8102 {
8103 ++arg.token;
8104 continue;
8105 }
8106 return FALSE;
8107 }
8108
8109 /* Handle special macro operands. Work out the properties of
8110 other operands. */
8111 arg.opnum += 1;
8112 switch (*args)
8113 {
8114 case '-':
8115 switch (args[1])
8116 {
8117 case 'A':
8118 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8119 break;
8120
8121 case 'B':
8122 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8123 break;
8124 }
8125 break;
8126
8127 case '+':
8128 switch (args[1])
8129 {
8130 case 'i':
8131 *offset_reloc = BFD_RELOC_MIPS_JMP;
8132 break;
8133
8134 case '\'':
8135 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8136 break;
8137
8138 case '\"':
8139 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8140 break;
8141 }
8142 break;
8143
8144 case 'I':
8145 if (!match_const_int (&arg, &imm_expr.X_add_number))
8146 return FALSE;
8147 imm_expr.X_op = O_constant;
8148 if (GPR_SIZE == 32)
8149 normalize_constant_expr (&imm_expr);
8150 continue;
8151
8152 case 'A':
8153 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8154 {
8155 /* Assume that the offset has been elided and that what
8156 we saw was a base register. The match will fail later
8157 if that assumption turns out to be wrong. */
8158 offset_expr.X_op = O_constant;
8159 offset_expr.X_add_number = 0;
8160 }
8161 else
8162 {
8163 if (!match_expression (&arg, &offset_expr, offset_reloc))
8164 return FALSE;
8165 normalize_address_expr (&offset_expr);
8166 }
8167 continue;
8168
8169 case 'F':
8170 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8171 8, TRUE))
8172 return FALSE;
8173 continue;
8174
8175 case 'L':
8176 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8177 8, FALSE))
8178 return FALSE;
8179 continue;
8180
8181 case 'f':
8182 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8183 4, TRUE))
8184 return FALSE;
8185 continue;
8186
8187 case 'l':
8188 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8189 4, FALSE))
8190 return FALSE;
8191 continue;
8192
8193 case 'p':
8194 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8195 break;
8196
8197 case 'a':
8198 *offset_reloc = BFD_RELOC_MIPS_JMP;
8199 break;
8200
8201 case 'm':
8202 gas_assert (mips_opts.micromips);
8203 c = args[1];
8204 switch (c)
8205 {
8206 case 'D':
8207 case 'E':
8208 if (!forced_insn_length)
8209 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8210 else if (c == 'D')
8211 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8212 else
8213 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8214 break;
8215 }
8216 break;
8217 }
8218
8219 operand = (mips_opts.micromips
8220 ? decode_micromips_operand (args)
8221 : decode_mips_operand (args));
8222 if (!operand)
8223 abort ();
8224
8225 /* Skip prefixes. */
8226 if (*args == '+' || *args == 'm' || *args == '-')
8227 args++;
8228
8229 if (mips_optional_operand_p (operand)
8230 && args[1] == ','
8231 && (arg.token[0].type != OT_REG
8232 || arg.token[1].type == OT_END))
8233 {
8234 /* Assume that the register has been elided and is the
8235 same as the first operand. */
8236 arg.token = tokens;
8237 arg.argnum = 1;
8238 }
8239
8240 if (!match_operand (&arg, operand))
8241 return FALSE;
8242 }
8243 }
8244
8245 /* Like match_insn, but for MIPS16. */
8246
8247 static bfd_boolean
8248 match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8249 struct mips_operand_token *tokens)
8250 {
8251 const char *args;
8252 const struct mips_operand *operand;
8253 const struct mips_operand *ext_operand;
8254 bfd_boolean pcrel = FALSE;
8255 int required_insn_length;
8256 struct mips_arg_info arg;
8257 int relax_char;
8258
8259 if (forced_insn_length)
8260 required_insn_length = forced_insn_length;
8261 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8262 required_insn_length = 2;
8263 else
8264 required_insn_length = 0;
8265
8266 create_insn (insn, opcode);
8267 imm_expr.X_op = O_absent;
8268 offset_expr.X_op = O_absent;
8269 offset_reloc[0] = BFD_RELOC_UNUSED;
8270 offset_reloc[1] = BFD_RELOC_UNUSED;
8271 offset_reloc[2] = BFD_RELOC_UNUSED;
8272 relax_char = 0;
8273
8274 memset (&arg, 0, sizeof (arg));
8275 arg.insn = insn;
8276 arg.token = tokens;
8277 arg.argnum = 1;
8278 arg.last_regno = ILLEGAL_REG;
8279 arg.dest_regno = ILLEGAL_REG;
8280 relax_char = 0;
8281 for (args = opcode->args;; ++args)
8282 {
8283 int c;
8284
8285 if (arg.token->type == OT_END)
8286 {
8287 offsetT value;
8288
8289 /* Handle unary instructions in which only one operand is given.
8290 The source is then the same as the destination. */
8291 if (arg.opnum == 1 && *args == ',')
8292 {
8293 operand = decode_mips16_operand (args[1], FALSE);
8294 if (operand && mips_optional_operand_p (operand))
8295 {
8296 arg.token = tokens;
8297 arg.argnum = 1;
8298 continue;
8299 }
8300 }
8301
8302 /* Fail the match if there were too few operands. */
8303 if (*args)
8304 return FALSE;
8305
8306 /* Successful match. Stuff the immediate value in now, if
8307 we can. */
8308 clear_insn_error ();
8309 if (opcode->pinfo == INSN_MACRO)
8310 {
8311 gas_assert (relax_char == 0 || relax_char == 'p');
8312 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8313 }
8314 else if (relax_char
8315 && offset_expr.X_op == O_constant
8316 && !pcrel
8317 && calculate_reloc (*offset_reloc,
8318 offset_expr.X_add_number,
8319 &value))
8320 {
8321 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
8322 required_insn_length, &insn->insn_opcode);
8323 offset_expr.X_op = O_absent;
8324 *offset_reloc = BFD_RELOC_UNUSED;
8325 }
8326 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8327 {
8328 if (required_insn_length == 2)
8329 set_insn_error (0, _("invalid unextended operand value"));
8330 else if (!mips_opcode_32bit_p (opcode))
8331 {
8332 forced_insn_length = 4;
8333 insn->insn_opcode |= MIPS16_EXTEND;
8334 }
8335 }
8336 else if (relax_char)
8337 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8338
8339 check_completed_insn (&arg);
8340 return TRUE;
8341 }
8342
8343 /* Fail the match if the line has too many operands. */
8344 if (*args == 0)
8345 return FALSE;
8346
8347 /* Handle characters that need to match exactly. */
8348 if (*args == '(' || *args == ')' || *args == ',')
8349 {
8350 if (match_char (&arg, *args))
8351 continue;
8352 return FALSE;
8353 }
8354
8355 arg.opnum += 1;
8356 c = *args;
8357 switch (c)
8358 {
8359 case 'p':
8360 case 'q':
8361 case 'A':
8362 case 'B':
8363 case 'E':
8364 case 'V':
8365 case 'u':
8366 relax_char = c;
8367 break;
8368
8369 case 'I':
8370 if (!match_const_int (&arg, &imm_expr.X_add_number))
8371 return FALSE;
8372 imm_expr.X_op = O_constant;
8373 if (GPR_SIZE == 32)
8374 normalize_constant_expr (&imm_expr);
8375 continue;
8376
8377 case 'a':
8378 case 'i':
8379 *offset_reloc = BFD_RELOC_MIPS16_JMP;
8380 break;
8381 }
8382
8383 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
8384 if (!operand)
8385 abort ();
8386
8387 if (operand->type == OP_PCREL)
8388 pcrel = TRUE;
8389 else
8390 {
8391 ext_operand = decode_mips16_operand (c, TRUE);
8392 if (operand != ext_operand)
8393 {
8394 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8395 {
8396 offset_expr.X_op = O_constant;
8397 offset_expr.X_add_number = 0;
8398 relax_char = c;
8399 continue;
8400 }
8401
8402 if (!match_expression (&arg, &offset_expr, offset_reloc))
8403 return FALSE;
8404
8405 /* '8' is used for SLTI(U) and has traditionally not
8406 been allowed to take relocation operators. */
8407 if (offset_reloc[0] != BFD_RELOC_UNUSED
8408 && (ext_operand->size != 16 || c == '8'))
8409 {
8410 match_not_constant (&arg);
8411 return FALSE;
8412 }
8413
8414 if (offset_expr.X_op == O_big)
8415 {
8416 match_out_of_range (&arg);
8417 return FALSE;
8418 }
8419
8420 relax_char = c;
8421 continue;
8422 }
8423 }
8424
8425 if (mips_optional_operand_p (operand)
8426 && args[1] == ','
8427 && (arg.token[0].type != OT_REG
8428 || arg.token[1].type == OT_END))
8429 {
8430 /* Assume that the register has been elided and is the
8431 same as the first operand. */
8432 arg.token = tokens;
8433 arg.argnum = 1;
8434 }
8435
8436 if (!match_operand (&arg, operand))
8437 return FALSE;
8438 }
8439 }
8440
8441 /* Record that the current instruction is invalid for the current ISA. */
8442
8443 static void
8444 match_invalid_for_isa (void)
8445 {
8446 set_insn_error_ss
8447 (0, _("opcode not supported on this processor: %s (%s)"),
8448 mips_cpu_info_from_arch (mips_opts.arch)->name,
8449 mips_cpu_info_from_isa (mips_opts.isa)->name);
8450 }
8451
8452 /* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8453 Return true if a definite match or failure was found, storing any match
8454 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8455 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8456 tried and failed to match under normal conditions and now want to try a
8457 more relaxed match. */
8458
8459 static bfd_boolean
8460 match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8461 const struct mips_opcode *past, struct mips_operand_token *tokens,
8462 int opcode_extra, bfd_boolean lax_match)
8463 {
8464 const struct mips_opcode *opcode;
8465 const struct mips_opcode *invalid_delay_slot;
8466 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8467
8468 /* Search for a match, ignoring alternatives that don't satisfy the
8469 current ISA or forced_length. */
8470 invalid_delay_slot = 0;
8471 seen_valid_for_isa = FALSE;
8472 seen_valid_for_size = FALSE;
8473 opcode = first;
8474 do
8475 {
8476 gas_assert (strcmp (opcode->name, first->name) == 0);
8477 if (is_opcode_valid (opcode))
8478 {
8479 seen_valid_for_isa = TRUE;
8480 if (is_size_valid (opcode))
8481 {
8482 bfd_boolean delay_slot_ok;
8483
8484 seen_valid_for_size = TRUE;
8485 delay_slot_ok = is_delay_slot_valid (opcode);
8486 if (match_insn (insn, opcode, tokens, opcode_extra,
8487 lax_match, delay_slot_ok))
8488 {
8489 if (!delay_slot_ok)
8490 {
8491 if (!invalid_delay_slot)
8492 invalid_delay_slot = opcode;
8493 }
8494 else
8495 return TRUE;
8496 }
8497 }
8498 }
8499 ++opcode;
8500 }
8501 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8502
8503 /* If the only matches we found had the wrong length for the delay slot,
8504 pick the first such match. We'll issue an appropriate warning later. */
8505 if (invalid_delay_slot)
8506 {
8507 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8508 lax_match, TRUE))
8509 return TRUE;
8510 abort ();
8511 }
8512
8513 /* Handle the case where we didn't try to match an instruction because
8514 all the alternatives were incompatible with the current ISA. */
8515 if (!seen_valid_for_isa)
8516 {
8517 match_invalid_for_isa ();
8518 return TRUE;
8519 }
8520
8521 /* Handle the case where we didn't try to match an instruction because
8522 all the alternatives were of the wrong size. */
8523 if (!seen_valid_for_size)
8524 {
8525 if (mips_opts.insn32)
8526 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
8527 else
8528 set_insn_error_i
8529 (0, _("unrecognized %d-bit version of microMIPS opcode"),
8530 8 * forced_insn_length);
8531 return TRUE;
8532 }
8533
8534 return FALSE;
8535 }
8536
8537 /* Like match_insns, but for MIPS16. */
8538
8539 static bfd_boolean
8540 match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8541 struct mips_operand_token *tokens)
8542 {
8543 const struct mips_opcode *opcode;
8544 bfd_boolean seen_valid_for_isa;
8545 bfd_boolean seen_valid_for_size;
8546
8547 /* Search for a match, ignoring alternatives that don't satisfy the
8548 current ISA. There are no separate entries for extended forms so
8549 we deal with forced_length later. */
8550 seen_valid_for_isa = FALSE;
8551 seen_valid_for_size = FALSE;
8552 opcode = first;
8553 do
8554 {
8555 gas_assert (strcmp (opcode->name, first->name) == 0);
8556 if (is_opcode_valid_16 (opcode))
8557 {
8558 seen_valid_for_isa = TRUE;
8559 if (is_size_valid_16 (opcode))
8560 {
8561 seen_valid_for_size = TRUE;
8562 if (match_mips16_insn (insn, opcode, tokens))
8563 return TRUE;
8564 }
8565 }
8566 ++opcode;
8567 }
8568 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8569 && strcmp (opcode->name, first->name) == 0);
8570
8571 /* Handle the case where we didn't try to match an instruction because
8572 all the alternatives were incompatible with the current ISA. */
8573 if (!seen_valid_for_isa)
8574 {
8575 match_invalid_for_isa ();
8576 return TRUE;
8577 }
8578
8579 /* Handle the case where we didn't try to match an instruction because
8580 all the alternatives were of the wrong size. */
8581 if (!seen_valid_for_size)
8582 {
8583 if (forced_insn_length == 2)
8584 set_insn_error
8585 (0, _("unrecognized unextended version of MIPS16 opcode"));
8586 else
8587 set_insn_error
8588 (0, _("unrecognized extended version of MIPS16 opcode"));
8589 return TRUE;
8590 }
8591
8592 return FALSE;
8593 }
8594
8595 /* Set up global variables for the start of a new macro. */
8596
8597 static void
8598 macro_start (void)
8599 {
8600 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
8601 memset (&mips_macro_warning.first_insn_sizes, 0,
8602 sizeof (mips_macro_warning.first_insn_sizes));
8603 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
8604 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
8605 && delayed_branch_p (&history[0]));
8606 if (history[0].frag
8607 && history[0].frag->fr_type == rs_machine_dependent
8608 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8609 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8610 mips_macro_warning.delay_slot_length = 0;
8611 else
8612 switch (history[0].insn_mo->pinfo2
8613 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8614 {
8615 case INSN2_BRANCH_DELAY_32BIT:
8616 mips_macro_warning.delay_slot_length = 4;
8617 break;
8618 case INSN2_BRANCH_DELAY_16BIT:
8619 mips_macro_warning.delay_slot_length = 2;
8620 break;
8621 default:
8622 mips_macro_warning.delay_slot_length = 0;
8623 break;
8624 }
8625 mips_macro_warning.first_frag = NULL;
8626 }
8627
8628 /* Given that a macro is longer than one instruction or of the wrong size,
8629 return the appropriate warning for it. Return null if no warning is
8630 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8631 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8632 and RELAX_NOMACRO. */
8633
8634 static const char *
8635 macro_warning (relax_substateT subtype)
8636 {
8637 if (subtype & RELAX_DELAY_SLOT)
8638 return _("macro instruction expanded into multiple instructions"
8639 " in a branch delay slot");
8640 else if (subtype & RELAX_NOMACRO)
8641 return _("macro instruction expanded into multiple instructions");
8642 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8643 | RELAX_DELAY_SLOT_SIZE_SECOND))
8644 return ((subtype & RELAX_DELAY_SLOT_16BIT)
8645 ? _("macro instruction expanded into a wrong size instruction"
8646 " in a 16-bit branch delay slot")
8647 : _("macro instruction expanded into a wrong size instruction"
8648 " in a 32-bit branch delay slot"));
8649 else
8650 return 0;
8651 }
8652
8653 /* Finish up a macro. Emit warnings as appropriate. */
8654
8655 static void
8656 macro_end (void)
8657 {
8658 /* Relaxation warning flags. */
8659 relax_substateT subtype = 0;
8660
8661 /* Check delay slot size requirements. */
8662 if (mips_macro_warning.delay_slot_length == 2)
8663 subtype |= RELAX_DELAY_SLOT_16BIT;
8664 if (mips_macro_warning.delay_slot_length != 0)
8665 {
8666 if (mips_macro_warning.delay_slot_length
8667 != mips_macro_warning.first_insn_sizes[0])
8668 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8669 if (mips_macro_warning.delay_slot_length
8670 != mips_macro_warning.first_insn_sizes[1])
8671 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8672 }
8673
8674 /* Check instruction count requirements. */
8675 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8676 {
8677 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
8678 subtype |= RELAX_SECOND_LONGER;
8679 if (mips_opts.warn_about_macros)
8680 subtype |= RELAX_NOMACRO;
8681 if (mips_macro_warning.delay_slot_p)
8682 subtype |= RELAX_DELAY_SLOT;
8683 }
8684
8685 /* If both alternatives fail to fill a delay slot correctly,
8686 emit the warning now. */
8687 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8688 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8689 {
8690 relax_substateT s;
8691 const char *msg;
8692
8693 s = subtype & (RELAX_DELAY_SLOT_16BIT
8694 | RELAX_DELAY_SLOT_SIZE_FIRST
8695 | RELAX_DELAY_SLOT_SIZE_SECOND);
8696 msg = macro_warning (s);
8697 if (msg != NULL)
8698 as_warn ("%s", msg);
8699 subtype &= ~s;
8700 }
8701
8702 /* If both implementations are longer than 1 instruction, then emit the
8703 warning now. */
8704 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8705 {
8706 relax_substateT s;
8707 const char *msg;
8708
8709 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8710 msg = macro_warning (s);
8711 if (msg != NULL)
8712 as_warn ("%s", msg);
8713 subtype &= ~s;
8714 }
8715
8716 /* If any flags still set, then one implementation might need a warning
8717 and the other either will need one of a different kind or none at all.
8718 Pass any remaining flags over to relaxation. */
8719 if (mips_macro_warning.first_frag != NULL)
8720 mips_macro_warning.first_frag->fr_subtype |= subtype;
8721 }
8722
8723 /* Instruction operand formats used in macros that vary between
8724 standard MIPS and microMIPS code. */
8725
8726 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
8727 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8728 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8729 static const char * const lui_fmt[2] = { "t,u", "s,u" };
8730 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
8731 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
8732 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8733 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8734
8735 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
8736 #define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8737 : cop12_fmt[mips_opts.micromips])
8738 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
8739 #define LUI_FMT (lui_fmt[mips_opts.micromips])
8740 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
8741 #define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8742 : mem12_fmt[mips_opts.micromips])
8743 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
8744 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
8745 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
8746
8747 /* Read a macro's relocation codes from *ARGS and store them in *R.
8748 The first argument in *ARGS will be either the code for a single
8749 relocation or -1 followed by the three codes that make up a
8750 composite relocation. */
8751
8752 static void
8753 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8754 {
8755 int i, next;
8756
8757 next = va_arg (*args, int);
8758 if (next >= 0)
8759 r[0] = (bfd_reloc_code_real_type) next;
8760 else
8761 {
8762 for (i = 0; i < 3; i++)
8763 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8764 /* This function is only used for 16-bit relocation fields.
8765 To make the macro code simpler, treat an unrelocated value
8766 in the same way as BFD_RELOC_LO16. */
8767 if (r[0] == BFD_RELOC_UNUSED)
8768 r[0] = BFD_RELOC_LO16;
8769 }
8770 }
8771
8772 /* Build an instruction created by a macro expansion. This is passed
8773 a pointer to the count of instructions created so far, an
8774 expression, the name of the instruction to build, an operand format
8775 string, and corresponding arguments. */
8776
8777 static void
8778 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
8779 {
8780 const struct mips_opcode *mo = NULL;
8781 bfd_reloc_code_real_type r[3];
8782 const struct mips_opcode *amo;
8783 const struct mips_operand *operand;
8784 struct hash_control *hash;
8785 struct mips_cl_insn insn;
8786 va_list args;
8787 unsigned int uval;
8788
8789 va_start (args, fmt);
8790
8791 if (mips_opts.mips16)
8792 {
8793 mips16_macro_build (ep, name, fmt, &args);
8794 va_end (args);
8795 return;
8796 }
8797
8798 r[0] = BFD_RELOC_UNUSED;
8799 r[1] = BFD_RELOC_UNUSED;
8800 r[2] = BFD_RELOC_UNUSED;
8801 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8802 amo = (struct mips_opcode *) hash_find (hash, name);
8803 gas_assert (amo);
8804 gas_assert (strcmp (name, amo->name) == 0);
8805
8806 do
8807 {
8808 /* Search until we get a match for NAME. It is assumed here that
8809 macros will never generate MDMX, MIPS-3D, or MT instructions.
8810 We try to match an instruction that fulfills the branch delay
8811 slot instruction length requirement (if any) of the previous
8812 instruction. While doing this we record the first instruction
8813 seen that matches all the other conditions and use it anyway
8814 if the requirement cannot be met; we will issue an appropriate
8815 warning later on. */
8816 if (strcmp (fmt, amo->args) == 0
8817 && amo->pinfo != INSN_MACRO
8818 && is_opcode_valid (amo)
8819 && is_size_valid (amo))
8820 {
8821 if (is_delay_slot_valid (amo))
8822 {
8823 mo = amo;
8824 break;
8825 }
8826 else if (!mo)
8827 mo = amo;
8828 }
8829
8830 ++amo;
8831 gas_assert (amo->name);
8832 }
8833 while (strcmp (name, amo->name) == 0);
8834
8835 gas_assert (mo);
8836 create_insn (&insn, mo);
8837 for (; *fmt; ++fmt)
8838 {
8839 switch (*fmt)
8840 {
8841 case ',':
8842 case '(':
8843 case ')':
8844 case 'z':
8845 break;
8846
8847 case 'i':
8848 case 'j':
8849 macro_read_relocs (&args, r);
8850 gas_assert (*r == BFD_RELOC_GPREL16
8851 || *r == BFD_RELOC_MIPS_HIGHER
8852 || *r == BFD_RELOC_HI16_S
8853 || *r == BFD_RELOC_LO16
8854 || *r == BFD_RELOC_MIPS_GOT_OFST);
8855 break;
8856
8857 case 'o':
8858 macro_read_relocs (&args, r);
8859 break;
8860
8861 case 'u':
8862 macro_read_relocs (&args, r);
8863 gas_assert (ep != NULL
8864 && (ep->X_op == O_constant
8865 || (ep->X_op == O_symbol
8866 && (*r == BFD_RELOC_MIPS_HIGHEST
8867 || *r == BFD_RELOC_HI16_S
8868 || *r == BFD_RELOC_HI16
8869 || *r == BFD_RELOC_GPREL16
8870 || *r == BFD_RELOC_MIPS_GOT_HI16
8871 || *r == BFD_RELOC_MIPS_CALL_HI16))));
8872 break;
8873
8874 case 'p':
8875 gas_assert (ep != NULL);
8876
8877 /*
8878 * This allows macro() to pass an immediate expression for
8879 * creating short branches without creating a symbol.
8880 *
8881 * We don't allow branch relaxation for these branches, as
8882 * they should only appear in ".set nomacro" anyway.
8883 */
8884 if (ep->X_op == O_constant)
8885 {
8886 /* For microMIPS we always use relocations for branches.
8887 So we should not resolve immediate values. */
8888 gas_assert (!mips_opts.micromips);
8889
8890 if ((ep->X_add_number & 3) != 0)
8891 as_bad (_("branch to misaligned address (0x%lx)"),
8892 (unsigned long) ep->X_add_number);
8893 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8894 as_bad (_("branch address range overflow (0x%lx)"),
8895 (unsigned long) ep->X_add_number);
8896 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8897 ep = NULL;
8898 }
8899 else
8900 *r = BFD_RELOC_16_PCREL_S2;
8901 break;
8902
8903 case 'a':
8904 gas_assert (ep != NULL);
8905 *r = BFD_RELOC_MIPS_JMP;
8906 break;
8907
8908 default:
8909 operand = (mips_opts.micromips
8910 ? decode_micromips_operand (fmt)
8911 : decode_mips_operand (fmt));
8912 if (!operand)
8913 abort ();
8914
8915 uval = va_arg (args, int);
8916 if (operand->type == OP_CLO_CLZ_DEST)
8917 uval |= (uval << 5);
8918 insn_insert_operand (&insn, operand, uval);
8919
8920 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
8921 ++fmt;
8922 break;
8923 }
8924 }
8925 va_end (args);
8926 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
8927
8928 append_insn (&insn, ep, r, TRUE);
8929 }
8930
8931 static void
8932 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
8933 va_list *args)
8934 {
8935 struct mips_opcode *mo;
8936 struct mips_cl_insn insn;
8937 const struct mips_operand *operand;
8938 bfd_reloc_code_real_type r[3]
8939 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
8940
8941 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
8942 gas_assert (mo);
8943 gas_assert (strcmp (name, mo->name) == 0);
8944
8945 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
8946 {
8947 ++mo;
8948 gas_assert (mo->name);
8949 gas_assert (strcmp (name, mo->name) == 0);
8950 }
8951
8952 create_insn (&insn, mo);
8953 for (; *fmt; ++fmt)
8954 {
8955 int c;
8956
8957 c = *fmt;
8958 switch (c)
8959 {
8960 case ',':
8961 case '(':
8962 case ')':
8963 break;
8964
8965 case '.':
8966 case 'S':
8967 case 'P':
8968 case 'R':
8969 break;
8970
8971 case '<':
8972 case '5':
8973 case 'F':
8974 case 'H':
8975 case 'W':
8976 case 'D':
8977 case 'j':
8978 case '8':
8979 case 'V':
8980 case 'C':
8981 case 'U':
8982 case 'k':
8983 case 'K':
8984 case 'p':
8985 case 'q':
8986 {
8987 offsetT value;
8988
8989 gas_assert (ep != NULL);
8990
8991 if (ep->X_op != O_constant)
8992 *r = (int) BFD_RELOC_UNUSED + c;
8993 else if (calculate_reloc (*r, ep->X_add_number, &value))
8994 {
8995 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
8996 ep = NULL;
8997 *r = BFD_RELOC_UNUSED;
8998 }
8999 }
9000 break;
9001
9002 default:
9003 operand = decode_mips16_operand (c, FALSE);
9004 if (!operand)
9005 abort ();
9006
9007 insn_insert_operand (&insn, operand, va_arg (*args, int));
9008 break;
9009 }
9010 }
9011
9012 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
9013
9014 append_insn (&insn, ep, r, TRUE);
9015 }
9016
9017 /*
9018 * Generate a "jalr" instruction with a relocation hint to the called
9019 * function. This occurs in NewABI PIC code.
9020 */
9021 static void
9022 macro_build_jalr (expressionS *ep, int cprestore)
9023 {
9024 static const bfd_reloc_code_real_type jalr_relocs[2]
9025 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9026 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9027 const char *jalr;
9028 char *f = NULL;
9029
9030 if (MIPS_JALR_HINT_P (ep))
9031 {
9032 frag_grow (8);
9033 f = frag_more (0);
9034 }
9035 if (mips_opts.micromips)
9036 {
9037 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9038 ? "jalr" : "jalrs");
9039 if (MIPS_JALR_HINT_P (ep)
9040 || mips_opts.insn32
9041 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9042 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9043 else
9044 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9045 }
9046 else
9047 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
9048 if (MIPS_JALR_HINT_P (ep))
9049 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
9050 }
9051
9052 /*
9053 * Generate a "lui" instruction.
9054 */
9055 static void
9056 macro_build_lui (expressionS *ep, int regnum)
9057 {
9058 gas_assert (! mips_opts.mips16);
9059
9060 if (ep->X_op != O_constant)
9061 {
9062 gas_assert (ep->X_op == O_symbol);
9063 /* _gp_disp is a special case, used from s_cpload.
9064 __gnu_local_gp is used if mips_no_shared. */
9065 gas_assert (mips_pic == NO_PIC
9066 || (! HAVE_NEWABI
9067 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9068 || (! mips_in_shared
9069 && strcmp (S_GET_NAME (ep->X_add_symbol),
9070 "__gnu_local_gp") == 0));
9071 }
9072
9073 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
9074 }
9075
9076 /* Generate a sequence of instructions to do a load or store from a constant
9077 offset off of a base register (breg) into/from a target register (treg),
9078 using AT if necessary. */
9079 static void
9080 macro_build_ldst_constoffset (expressionS *ep, const char *op,
9081 int treg, int breg, int dbl)
9082 {
9083 gas_assert (ep->X_op == O_constant);
9084
9085 /* Sign-extending 32-bit constants makes their handling easier. */
9086 if (!dbl)
9087 normalize_constant_expr (ep);
9088
9089 /* Right now, this routine can only handle signed 32-bit constants. */
9090 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
9091 as_warn (_("operand overflow"));
9092
9093 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9094 {
9095 /* Signed 16-bit offset will fit in the op. Easy! */
9096 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
9097 }
9098 else
9099 {
9100 /* 32-bit offset, need multiple instructions and AT, like:
9101 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9102 addu $tempreg,$tempreg,$breg
9103 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9104 to handle the complete offset. */
9105 macro_build_lui (ep, AT);
9106 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9107 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
9108
9109 if (!mips_opts.at)
9110 as_bad (_("macro used $at after \".set noat\""));
9111 }
9112 }
9113
9114 /* set_at()
9115 * Generates code to set the $at register to true (one)
9116 * if reg is less than the immediate expression.
9117 */
9118 static void
9119 set_at (int reg, int unsignedp)
9120 {
9121 if (imm_expr.X_add_number >= -0x8000
9122 && imm_expr.X_add_number < 0x8000)
9123 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9124 AT, reg, BFD_RELOC_LO16);
9125 else
9126 {
9127 load_register (AT, &imm_expr, GPR_SIZE == 64);
9128 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
9129 }
9130 }
9131
9132 /* Count the leading zeroes by performing a binary chop. This is a
9133 bulky bit of source, but performance is a LOT better for the
9134 majority of values than a simple loop to count the bits:
9135 for (lcnt = 0; (lcnt < 32); lcnt++)
9136 if ((v) & (1 << (31 - lcnt)))
9137 break;
9138 However it is not code size friendly, and the gain will drop a bit
9139 on certain cached systems.
9140 */
9141 #define COUNT_TOP_ZEROES(v) \
9142 (((v) & ~0xffff) == 0 \
9143 ? ((v) & ~0xff) == 0 \
9144 ? ((v) & ~0xf) == 0 \
9145 ? ((v) & ~0x3) == 0 \
9146 ? ((v) & ~0x1) == 0 \
9147 ? !(v) \
9148 ? 32 \
9149 : 31 \
9150 : 30 \
9151 : ((v) & ~0x7) == 0 \
9152 ? 29 \
9153 : 28 \
9154 : ((v) & ~0x3f) == 0 \
9155 ? ((v) & ~0x1f) == 0 \
9156 ? 27 \
9157 : 26 \
9158 : ((v) & ~0x7f) == 0 \
9159 ? 25 \
9160 : 24 \
9161 : ((v) & ~0xfff) == 0 \
9162 ? ((v) & ~0x3ff) == 0 \
9163 ? ((v) & ~0x1ff) == 0 \
9164 ? 23 \
9165 : 22 \
9166 : ((v) & ~0x7ff) == 0 \
9167 ? 21 \
9168 : 20 \
9169 : ((v) & ~0x3fff) == 0 \
9170 ? ((v) & ~0x1fff) == 0 \
9171 ? 19 \
9172 : 18 \
9173 : ((v) & ~0x7fff) == 0 \
9174 ? 17 \
9175 : 16 \
9176 : ((v) & ~0xffffff) == 0 \
9177 ? ((v) & ~0xfffff) == 0 \
9178 ? ((v) & ~0x3ffff) == 0 \
9179 ? ((v) & ~0x1ffff) == 0 \
9180 ? 15 \
9181 : 14 \
9182 : ((v) & ~0x7ffff) == 0 \
9183 ? 13 \
9184 : 12 \
9185 : ((v) & ~0x3fffff) == 0 \
9186 ? ((v) & ~0x1fffff) == 0 \
9187 ? 11 \
9188 : 10 \
9189 : ((v) & ~0x7fffff) == 0 \
9190 ? 9 \
9191 : 8 \
9192 : ((v) & ~0xfffffff) == 0 \
9193 ? ((v) & ~0x3ffffff) == 0 \
9194 ? ((v) & ~0x1ffffff) == 0 \
9195 ? 7 \
9196 : 6 \
9197 : ((v) & ~0x7ffffff) == 0 \
9198 ? 5 \
9199 : 4 \
9200 : ((v) & ~0x3fffffff) == 0 \
9201 ? ((v) & ~0x1fffffff) == 0 \
9202 ? 3 \
9203 : 2 \
9204 : ((v) & ~0x7fffffff) == 0 \
9205 ? 1 \
9206 : 0)
9207
9208 /* load_register()
9209 * This routine generates the least number of instructions necessary to load
9210 * an absolute expression value into a register.
9211 */
9212 static void
9213 load_register (int reg, expressionS *ep, int dbl)
9214 {
9215 int freg;
9216 expressionS hi32, lo32;
9217
9218 if (ep->X_op != O_big)
9219 {
9220 gas_assert (ep->X_op == O_constant);
9221
9222 /* Sign-extending 32-bit constants makes their handling easier. */
9223 if (!dbl)
9224 normalize_constant_expr (ep);
9225
9226 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
9227 {
9228 /* We can handle 16 bit signed values with an addiu to
9229 $zero. No need to ever use daddiu here, since $zero and
9230 the result are always correct in 32 bit mode. */
9231 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9232 return;
9233 }
9234 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9235 {
9236 /* We can handle 16 bit unsigned values with an ori to
9237 $zero. */
9238 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9239 return;
9240 }
9241 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
9242 {
9243 /* 32 bit values require an lui. */
9244 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9245 if ((ep->X_add_number & 0xffff) != 0)
9246 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9247 return;
9248 }
9249 }
9250
9251 /* The value is larger than 32 bits. */
9252
9253 if (!dbl || GPR_SIZE == 32)
9254 {
9255 char value[32];
9256
9257 sprintf_vma (value, ep->X_add_number);
9258 as_bad (_("number (0x%s) larger than 32 bits"), value);
9259 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9260 return;
9261 }
9262
9263 if (ep->X_op != O_big)
9264 {
9265 hi32 = *ep;
9266 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9267 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9268 hi32.X_add_number &= 0xffffffff;
9269 lo32 = *ep;
9270 lo32.X_add_number &= 0xffffffff;
9271 }
9272 else
9273 {
9274 gas_assert (ep->X_add_number > 2);
9275 if (ep->X_add_number == 3)
9276 generic_bignum[3] = 0;
9277 else if (ep->X_add_number > 4)
9278 as_bad (_("number larger than 64 bits"));
9279 lo32.X_op = O_constant;
9280 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9281 hi32.X_op = O_constant;
9282 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9283 }
9284
9285 if (hi32.X_add_number == 0)
9286 freg = 0;
9287 else
9288 {
9289 int shift, bit;
9290 unsigned long hi, lo;
9291
9292 if (hi32.X_add_number == (offsetT) 0xffffffff)
9293 {
9294 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9295 {
9296 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9297 return;
9298 }
9299 if (lo32.X_add_number & 0x80000000)
9300 {
9301 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9302 if (lo32.X_add_number & 0xffff)
9303 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9304 return;
9305 }
9306 }
9307
9308 /* Check for 16bit shifted constant. We know that hi32 is
9309 non-zero, so start the mask on the first bit of the hi32
9310 value. */
9311 shift = 17;
9312 do
9313 {
9314 unsigned long himask, lomask;
9315
9316 if (shift < 32)
9317 {
9318 himask = 0xffff >> (32 - shift);
9319 lomask = (0xffff << shift) & 0xffffffff;
9320 }
9321 else
9322 {
9323 himask = 0xffff << (shift - 32);
9324 lomask = 0;
9325 }
9326 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9327 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9328 {
9329 expressionS tmp;
9330
9331 tmp.X_op = O_constant;
9332 if (shift < 32)
9333 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9334 | (lo32.X_add_number >> shift));
9335 else
9336 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
9337 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9338 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9339 reg, reg, (shift >= 32) ? shift - 32 : shift);
9340 return;
9341 }
9342 ++shift;
9343 }
9344 while (shift <= (64 - 16));
9345
9346 /* Find the bit number of the lowest one bit, and store the
9347 shifted value in hi/lo. */
9348 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9349 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9350 if (lo != 0)
9351 {
9352 bit = 0;
9353 while ((lo & 1) == 0)
9354 {
9355 lo >>= 1;
9356 ++bit;
9357 }
9358 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9359 hi >>= bit;
9360 }
9361 else
9362 {
9363 bit = 32;
9364 while ((hi & 1) == 0)
9365 {
9366 hi >>= 1;
9367 ++bit;
9368 }
9369 lo = hi;
9370 hi = 0;
9371 }
9372
9373 /* Optimize if the shifted value is a (power of 2) - 1. */
9374 if ((hi == 0 && ((lo + 1) & lo) == 0)
9375 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
9376 {
9377 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
9378 if (shift != 0)
9379 {
9380 expressionS tmp;
9381
9382 /* This instruction will set the register to be all
9383 ones. */
9384 tmp.X_op = O_constant;
9385 tmp.X_add_number = (offsetT) -1;
9386 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9387 if (bit != 0)
9388 {
9389 bit += shift;
9390 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9391 reg, reg, (bit >= 32) ? bit - 32 : bit);
9392 }
9393 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
9394 reg, reg, (shift >= 32) ? shift - 32 : shift);
9395 return;
9396 }
9397 }
9398
9399 /* Sign extend hi32 before calling load_register, because we can
9400 generally get better code when we load a sign extended value. */
9401 if ((hi32.X_add_number & 0x80000000) != 0)
9402 hi32.X_add_number |= ~(offsetT) 0xffffffff;
9403 load_register (reg, &hi32, 0);
9404 freg = reg;
9405 }
9406 if ((lo32.X_add_number & 0xffff0000) == 0)
9407 {
9408 if (freg != 0)
9409 {
9410 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
9411 freg = reg;
9412 }
9413 }
9414 else
9415 {
9416 expressionS mid16;
9417
9418 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
9419 {
9420 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9421 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
9422 return;
9423 }
9424
9425 if (freg != 0)
9426 {
9427 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
9428 freg = reg;
9429 }
9430 mid16 = lo32;
9431 mid16.X_add_number >>= 16;
9432 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9433 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9434 freg = reg;
9435 }
9436 if ((lo32.X_add_number & 0xffff) != 0)
9437 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9438 }
9439
9440 static inline void
9441 load_delay_nop (void)
9442 {
9443 if (!gpr_interlocks)
9444 macro_build (NULL, "nop", "");
9445 }
9446
9447 /* Load an address into a register. */
9448
9449 static void
9450 load_address (int reg, expressionS *ep, int *used_at)
9451 {
9452 if (ep->X_op != O_constant
9453 && ep->X_op != O_symbol)
9454 {
9455 as_bad (_("expression too complex"));
9456 ep->X_op = O_constant;
9457 }
9458
9459 if (ep->X_op == O_constant)
9460 {
9461 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
9462 return;
9463 }
9464
9465 if (mips_pic == NO_PIC)
9466 {
9467 /* If this is a reference to a GP relative symbol, we want
9468 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
9469 Otherwise we want
9470 lui $reg,<sym> (BFD_RELOC_HI16_S)
9471 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9472 If we have an addend, we always use the latter form.
9473
9474 With 64bit address space and a usable $at we want
9475 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9476 lui $at,<sym> (BFD_RELOC_HI16_S)
9477 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9478 daddiu $at,<sym> (BFD_RELOC_LO16)
9479 dsll32 $reg,0
9480 daddu $reg,$reg,$at
9481
9482 If $at is already in use, we use a path which is suboptimal
9483 on superscalar processors.
9484 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9485 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9486 dsll $reg,16
9487 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9488 dsll $reg,16
9489 daddiu $reg,<sym> (BFD_RELOC_LO16)
9490
9491 For GP relative symbols in 64bit address space we can use
9492 the same sequence as in 32bit address space. */
9493 if (HAVE_64BIT_SYMBOLS)
9494 {
9495 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9496 && !nopic_need_relax (ep->X_add_symbol, 1))
9497 {
9498 relax_start (ep->X_add_symbol);
9499 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9500 mips_gp_register, BFD_RELOC_GPREL16);
9501 relax_switch ();
9502 }
9503
9504 if (*used_at == 0 && mips_opts.at)
9505 {
9506 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9507 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
9508 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9509 BFD_RELOC_MIPS_HIGHER);
9510 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
9511 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
9512 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
9513 *used_at = 1;
9514 }
9515 else
9516 {
9517 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9518 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9519 BFD_RELOC_MIPS_HIGHER);
9520 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9521 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
9522 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9523 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
9524 }
9525
9526 if (mips_relax.sequence)
9527 relax_end ();
9528 }
9529 else
9530 {
9531 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9532 && !nopic_need_relax (ep->X_add_symbol, 1))
9533 {
9534 relax_start (ep->X_add_symbol);
9535 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9536 mips_gp_register, BFD_RELOC_GPREL16);
9537 relax_switch ();
9538 }
9539 macro_build_lui (ep, reg);
9540 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9541 reg, reg, BFD_RELOC_LO16);
9542 if (mips_relax.sequence)
9543 relax_end ();
9544 }
9545 }
9546 else if (!mips_big_got)
9547 {
9548 expressionS ex;
9549
9550 /* If this is a reference to an external symbol, we want
9551 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9552 Otherwise we want
9553 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9554 nop
9555 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9556 If there is a constant, it must be added in after.
9557
9558 If we have NewABI, we want
9559 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9560 unless we're referencing a global symbol with a non-zero
9561 offset, in which case cst must be added separately. */
9562 if (HAVE_NEWABI)
9563 {
9564 if (ep->X_add_number)
9565 {
9566 ex.X_add_number = ep->X_add_number;
9567 ep->X_add_number = 0;
9568 relax_start (ep->X_add_symbol);
9569 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9570 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9571 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9572 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9573 ex.X_op = O_constant;
9574 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9575 reg, reg, BFD_RELOC_LO16);
9576 ep->X_add_number = ex.X_add_number;
9577 relax_switch ();
9578 }
9579 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9580 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9581 if (mips_relax.sequence)
9582 relax_end ();
9583 }
9584 else
9585 {
9586 ex.X_add_number = ep->X_add_number;
9587 ep->X_add_number = 0;
9588 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9589 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9590 load_delay_nop ();
9591 relax_start (ep->X_add_symbol);
9592 relax_switch ();
9593 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9594 BFD_RELOC_LO16);
9595 relax_end ();
9596
9597 if (ex.X_add_number != 0)
9598 {
9599 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9600 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9601 ex.X_op = O_constant;
9602 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9603 reg, reg, BFD_RELOC_LO16);
9604 }
9605 }
9606 }
9607 else if (mips_big_got)
9608 {
9609 expressionS ex;
9610
9611 /* This is the large GOT case. If this is a reference to an
9612 external symbol, we want
9613 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9614 addu $reg,$reg,$gp
9615 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
9616
9617 Otherwise, for a reference to a local symbol in old ABI, we want
9618 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9619 nop
9620 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9621 If there is a constant, it must be added in after.
9622
9623 In the NewABI, for local symbols, with or without offsets, we want:
9624 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9625 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
9626 */
9627 if (HAVE_NEWABI)
9628 {
9629 ex.X_add_number = ep->X_add_number;
9630 ep->X_add_number = 0;
9631 relax_start (ep->X_add_symbol);
9632 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9633 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9634 reg, reg, mips_gp_register);
9635 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9636 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9637 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9638 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9639 else if (ex.X_add_number)
9640 {
9641 ex.X_op = O_constant;
9642 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9643 BFD_RELOC_LO16);
9644 }
9645
9646 ep->X_add_number = ex.X_add_number;
9647 relax_switch ();
9648 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9649 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9650 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9651 BFD_RELOC_MIPS_GOT_OFST);
9652 relax_end ();
9653 }
9654 else
9655 {
9656 ex.X_add_number = ep->X_add_number;
9657 ep->X_add_number = 0;
9658 relax_start (ep->X_add_symbol);
9659 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9660 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9661 reg, reg, mips_gp_register);
9662 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9663 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9664 relax_switch ();
9665 if (reg_needs_delay (mips_gp_register))
9666 {
9667 /* We need a nop before loading from $gp. This special
9668 check is required because the lui which starts the main
9669 instruction stream does not refer to $gp, and so will not
9670 insert the nop which may be required. */
9671 macro_build (NULL, "nop", "");
9672 }
9673 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9674 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9675 load_delay_nop ();
9676 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9677 BFD_RELOC_LO16);
9678 relax_end ();
9679
9680 if (ex.X_add_number != 0)
9681 {
9682 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9683 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9684 ex.X_op = O_constant;
9685 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9686 BFD_RELOC_LO16);
9687 }
9688 }
9689 }
9690 else
9691 abort ();
9692
9693 if (!mips_opts.at && *used_at == 1)
9694 as_bad (_("macro used $at after \".set noat\""));
9695 }
9696
9697 /* Move the contents of register SOURCE into register DEST. */
9698
9699 static void
9700 move_register (int dest, int source)
9701 {
9702 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9703 instruction specifically requires a 32-bit one. */
9704 if (mips_opts.micromips
9705 && !mips_opts.insn32
9706 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9707 macro_build (NULL, "move", "mp,mj", dest, source);
9708 else
9709 macro_build (NULL, "or", "d,v,t", dest, source, 0);
9710 }
9711
9712 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
9713 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9714 The two alternatives are:
9715
9716 Global symbol Local symbol
9717 ------------- ------------
9718 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9719 ... ...
9720 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9721
9722 load_got_offset emits the first instruction and add_got_offset
9723 emits the second for a 16-bit offset or add_got_offset_hilo emits
9724 a sequence to add a 32-bit offset using a scratch register. */
9725
9726 static void
9727 load_got_offset (int dest, expressionS *local)
9728 {
9729 expressionS global;
9730
9731 global = *local;
9732 global.X_add_number = 0;
9733
9734 relax_start (local->X_add_symbol);
9735 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9736 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9737 relax_switch ();
9738 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9739 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9740 relax_end ();
9741 }
9742
9743 static void
9744 add_got_offset (int dest, expressionS *local)
9745 {
9746 expressionS global;
9747
9748 global.X_op = O_constant;
9749 global.X_op_symbol = NULL;
9750 global.X_add_symbol = NULL;
9751 global.X_add_number = local->X_add_number;
9752
9753 relax_start (local->X_add_symbol);
9754 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
9755 dest, dest, BFD_RELOC_LO16);
9756 relax_switch ();
9757 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
9758 relax_end ();
9759 }
9760
9761 static void
9762 add_got_offset_hilo (int dest, expressionS *local, int tmp)
9763 {
9764 expressionS global;
9765 int hold_mips_optimize;
9766
9767 global.X_op = O_constant;
9768 global.X_op_symbol = NULL;
9769 global.X_add_symbol = NULL;
9770 global.X_add_number = local->X_add_number;
9771
9772 relax_start (local->X_add_symbol);
9773 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9774 relax_switch ();
9775 /* Set mips_optimize around the lui instruction to avoid
9776 inserting an unnecessary nop after the lw. */
9777 hold_mips_optimize = mips_optimize;
9778 mips_optimize = 2;
9779 macro_build_lui (&global, tmp);
9780 mips_optimize = hold_mips_optimize;
9781 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9782 relax_end ();
9783
9784 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9785 }
9786
9787 /* Emit a sequence of instructions to emulate a branch likely operation.
9788 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9789 is its complementing branch with the original condition negated.
9790 CALL is set if the original branch specified the link operation.
9791 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9792
9793 Code like this is produced in the noreorder mode:
9794
9795 BRNEG <args>, 1f
9796 nop
9797 b <sym>
9798 delay slot (executed only if branch taken)
9799 1:
9800
9801 or, if CALL is set:
9802
9803 BRNEG <args>, 1f
9804 nop
9805 bal <sym>
9806 delay slot (executed only if branch taken)
9807 1:
9808
9809 In the reorder mode the delay slot would be filled with a nop anyway,
9810 so code produced is simply:
9811
9812 BR <args>, <sym>
9813 nop
9814
9815 This function is used when producing code for the microMIPS ASE that
9816 does not implement branch likely instructions in hardware. */
9817
9818 static void
9819 macro_build_branch_likely (const char *br, const char *brneg,
9820 int call, expressionS *ep, const char *fmt,
9821 unsigned int sreg, unsigned int treg)
9822 {
9823 int noreorder = mips_opts.noreorder;
9824 expressionS expr1;
9825
9826 gas_assert (mips_opts.micromips);
9827 start_noreorder ();
9828 if (noreorder)
9829 {
9830 micromips_label_expr (&expr1);
9831 macro_build (&expr1, brneg, fmt, sreg, treg);
9832 macro_build (NULL, "nop", "");
9833 macro_build (ep, call ? "bal" : "b", "p");
9834
9835 /* Set to true so that append_insn adds a label. */
9836 emit_branch_likely_macro = TRUE;
9837 }
9838 else
9839 {
9840 macro_build (ep, br, fmt, sreg, treg);
9841 macro_build (NULL, "nop", "");
9842 }
9843 end_noreorder ();
9844 }
9845
9846 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9847 the condition code tested. EP specifies the branch target. */
9848
9849 static void
9850 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9851 {
9852 const int call = 0;
9853 const char *brneg;
9854 const char *br;
9855
9856 switch (type)
9857 {
9858 case M_BC1FL:
9859 br = "bc1f";
9860 brneg = "bc1t";
9861 break;
9862 case M_BC1TL:
9863 br = "bc1t";
9864 brneg = "bc1f";
9865 break;
9866 case M_BC2FL:
9867 br = "bc2f";
9868 brneg = "bc2t";
9869 break;
9870 case M_BC2TL:
9871 br = "bc2t";
9872 brneg = "bc2f";
9873 break;
9874 default:
9875 abort ();
9876 }
9877 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9878 }
9879
9880 /* Emit a two-argument branch macro specified by TYPE, using SREG as
9881 the register tested. EP specifies the branch target. */
9882
9883 static void
9884 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9885 {
9886 const char *brneg = NULL;
9887 const char *br;
9888 int call = 0;
9889
9890 switch (type)
9891 {
9892 case M_BGEZ:
9893 br = "bgez";
9894 break;
9895 case M_BGEZL:
9896 br = mips_opts.micromips ? "bgez" : "bgezl";
9897 brneg = "bltz";
9898 break;
9899 case M_BGEZALL:
9900 gas_assert (mips_opts.micromips);
9901 br = mips_opts.insn32 ? "bgezal" : "bgezals";
9902 brneg = "bltz";
9903 call = 1;
9904 break;
9905 case M_BGTZ:
9906 br = "bgtz";
9907 break;
9908 case M_BGTZL:
9909 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9910 brneg = "blez";
9911 break;
9912 case M_BLEZ:
9913 br = "blez";
9914 break;
9915 case M_BLEZL:
9916 br = mips_opts.micromips ? "blez" : "blezl";
9917 brneg = "bgtz";
9918 break;
9919 case M_BLTZ:
9920 br = "bltz";
9921 break;
9922 case M_BLTZL:
9923 br = mips_opts.micromips ? "bltz" : "bltzl";
9924 brneg = "bgez";
9925 break;
9926 case M_BLTZALL:
9927 gas_assert (mips_opts.micromips);
9928 br = mips_opts.insn32 ? "bltzal" : "bltzals";
9929 brneg = "bgez";
9930 call = 1;
9931 break;
9932 default:
9933 abort ();
9934 }
9935 if (mips_opts.micromips && brneg)
9936 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9937 else
9938 macro_build (ep, br, "s,p", sreg);
9939 }
9940
9941 /* Emit a three-argument branch macro specified by TYPE, using SREG and
9942 TREG as the registers tested. EP specifies the branch target. */
9943
9944 static void
9945 macro_build_branch_rsrt (int type, expressionS *ep,
9946 unsigned int sreg, unsigned int treg)
9947 {
9948 const char *brneg = NULL;
9949 const int call = 0;
9950 const char *br;
9951
9952 switch (type)
9953 {
9954 case M_BEQ:
9955 case M_BEQ_I:
9956 br = "beq";
9957 break;
9958 case M_BEQL:
9959 case M_BEQL_I:
9960 br = mips_opts.micromips ? "beq" : "beql";
9961 brneg = "bne";
9962 break;
9963 case M_BNE:
9964 case M_BNE_I:
9965 br = "bne";
9966 break;
9967 case M_BNEL:
9968 case M_BNEL_I:
9969 br = mips_opts.micromips ? "bne" : "bnel";
9970 brneg = "beq";
9971 break;
9972 default:
9973 abort ();
9974 }
9975 if (mips_opts.micromips && brneg)
9976 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
9977 else
9978 macro_build (ep, br, "s,t,p", sreg, treg);
9979 }
9980
9981 /* Return the high part that should be loaded in order to make the low
9982 part of VALUE accessible using an offset of OFFBITS bits. */
9983
9984 static offsetT
9985 offset_high_part (offsetT value, unsigned int offbits)
9986 {
9987 offsetT bias;
9988 addressT low_mask;
9989
9990 if (offbits == 0)
9991 return value;
9992 bias = 1 << (offbits - 1);
9993 low_mask = bias * 2 - 1;
9994 return (value + bias) & ~low_mask;
9995 }
9996
9997 /* Return true if the value stored in offset_expr and offset_reloc
9998 fits into a signed offset of OFFBITS bits. RANGE is the maximum
9999 amount that the caller wants to add without inducing overflow
10000 and ALIGN is the known alignment of the value in bytes. */
10001
10002 static bfd_boolean
10003 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10004 {
10005 if (offbits == 16)
10006 {
10007 /* Accept any relocation operator if overflow isn't a concern. */
10008 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10009 return TRUE;
10010
10011 /* These relocations are guaranteed not to overflow in correct links. */
10012 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10013 || gprel16_reloc_p (*offset_reloc))
10014 return TRUE;
10015 }
10016 if (offset_expr.X_op == O_constant
10017 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10018 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10019 return TRUE;
10020 return FALSE;
10021 }
10022
10023 /*
10024 * Build macros
10025 * This routine implements the seemingly endless macro or synthesized
10026 * instructions and addressing modes in the mips assembly language. Many
10027 * of these macros are simple and are similar to each other. These could
10028 * probably be handled by some kind of table or grammar approach instead of
10029 * this verbose method. Others are not simple macros but are more like
10030 * optimizing code generation.
10031 * One interesting optimization is when several store macros appear
10032 * consecutively that would load AT with the upper half of the same address.
10033 * The ensuing load upper instructions are omitted. This implies some kind
10034 * of global optimization. We currently only optimize within a single macro.
10035 * For many of the load and store macros if the address is specified as a
10036 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10037 * first load register 'at' with zero and use it as the base register. The
10038 * mips assembler simply uses register $zero. Just one tiny optimization
10039 * we're missing.
10040 */
10041 static void
10042 macro (struct mips_cl_insn *ip, char *str)
10043 {
10044 const struct mips_operand_array *operands;
10045 unsigned int breg, i;
10046 unsigned int tempreg;
10047 int mask;
10048 int used_at = 0;
10049 expressionS label_expr;
10050 expressionS expr1;
10051 expressionS *ep;
10052 const char *s;
10053 const char *s2;
10054 const char *fmt;
10055 int likely = 0;
10056 int coproc = 0;
10057 int offbits = 16;
10058 int call = 0;
10059 int jals = 0;
10060 int dbl = 0;
10061 int imm = 0;
10062 int ust = 0;
10063 int lp = 0;
10064 bfd_boolean large_offset;
10065 int off;
10066 int hold_mips_optimize;
10067 unsigned int align;
10068 unsigned int op[MAX_OPERANDS];
10069
10070 gas_assert (! mips_opts.mips16);
10071
10072 operands = insn_operands (ip);
10073 for (i = 0; i < MAX_OPERANDS; i++)
10074 if (operands->operand[i])
10075 op[i] = insn_extract_operand (ip, operands->operand[i]);
10076 else
10077 op[i] = -1;
10078
10079 mask = ip->insn_mo->mask;
10080
10081 label_expr.X_op = O_constant;
10082 label_expr.X_op_symbol = NULL;
10083 label_expr.X_add_symbol = NULL;
10084 label_expr.X_add_number = 0;
10085
10086 expr1.X_op = O_constant;
10087 expr1.X_op_symbol = NULL;
10088 expr1.X_add_symbol = NULL;
10089 expr1.X_add_number = 1;
10090 align = 1;
10091
10092 switch (mask)
10093 {
10094 case M_DABS:
10095 dbl = 1;
10096 /* Fall through. */
10097 case M_ABS:
10098 /* bgez $a0,1f
10099 move v0,$a0
10100 sub v0,$zero,$a0
10101 1:
10102 */
10103
10104 start_noreorder ();
10105
10106 if (mips_opts.micromips)
10107 micromips_label_expr (&label_expr);
10108 else
10109 label_expr.X_add_number = 8;
10110 macro_build (&label_expr, "bgez", "s,p", op[1]);
10111 if (op[0] == op[1])
10112 macro_build (NULL, "nop", "");
10113 else
10114 move_register (op[0], op[1]);
10115 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
10116 if (mips_opts.micromips)
10117 micromips_add_label ();
10118
10119 end_noreorder ();
10120 break;
10121
10122 case M_ADD_I:
10123 s = "addi";
10124 s2 = "add";
10125 goto do_addi;
10126 case M_ADDU_I:
10127 s = "addiu";
10128 s2 = "addu";
10129 goto do_addi;
10130 case M_DADD_I:
10131 dbl = 1;
10132 s = "daddi";
10133 s2 = "dadd";
10134 if (!mips_opts.micromips)
10135 goto do_addi;
10136 if (imm_expr.X_add_number >= -0x200
10137 && imm_expr.X_add_number < 0x200)
10138 {
10139 macro_build (NULL, s, "t,r,.", op[0], op[1],
10140 (int) imm_expr.X_add_number);
10141 break;
10142 }
10143 goto do_addi_i;
10144 case M_DADDU_I:
10145 dbl = 1;
10146 s = "daddiu";
10147 s2 = "daddu";
10148 do_addi:
10149 if (imm_expr.X_add_number >= -0x8000
10150 && imm_expr.X_add_number < 0x8000)
10151 {
10152 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
10153 break;
10154 }
10155 do_addi_i:
10156 used_at = 1;
10157 load_register (AT, &imm_expr, dbl);
10158 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10159 break;
10160
10161 case M_AND_I:
10162 s = "andi";
10163 s2 = "and";
10164 goto do_bit;
10165 case M_OR_I:
10166 s = "ori";
10167 s2 = "or";
10168 goto do_bit;
10169 case M_NOR_I:
10170 s = "";
10171 s2 = "nor";
10172 goto do_bit;
10173 case M_XOR_I:
10174 s = "xori";
10175 s2 = "xor";
10176 do_bit:
10177 if (imm_expr.X_add_number >= 0
10178 && imm_expr.X_add_number < 0x10000)
10179 {
10180 if (mask != M_NOR_I)
10181 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
10182 else
10183 {
10184 macro_build (&imm_expr, "ori", "t,r,i",
10185 op[0], op[1], BFD_RELOC_LO16);
10186 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
10187 }
10188 break;
10189 }
10190
10191 used_at = 1;
10192 load_register (AT, &imm_expr, GPR_SIZE == 64);
10193 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10194 break;
10195
10196 case M_BALIGN:
10197 switch (imm_expr.X_add_number)
10198 {
10199 case 0:
10200 macro_build (NULL, "nop", "");
10201 break;
10202 case 2:
10203 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
10204 break;
10205 case 1:
10206 case 3:
10207 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
10208 (int) imm_expr.X_add_number);
10209 break;
10210 default:
10211 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10212 (unsigned long) imm_expr.X_add_number);
10213 break;
10214 }
10215 break;
10216
10217 case M_BC1FL:
10218 case M_BC1TL:
10219 case M_BC2FL:
10220 case M_BC2TL:
10221 gas_assert (mips_opts.micromips);
10222 macro_build_branch_ccl (mask, &offset_expr,
10223 EXTRACT_OPERAND (1, BCC, *ip));
10224 break;
10225
10226 case M_BEQ_I:
10227 case M_BEQL_I:
10228 case M_BNE_I:
10229 case M_BNEL_I:
10230 if (imm_expr.X_add_number == 0)
10231 op[1] = 0;
10232 else
10233 {
10234 op[1] = AT;
10235 used_at = 1;
10236 load_register (op[1], &imm_expr, GPR_SIZE == 64);
10237 }
10238 /* Fall through. */
10239 case M_BEQL:
10240 case M_BNEL:
10241 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
10242 break;
10243
10244 case M_BGEL:
10245 likely = 1;
10246 /* Fall through. */
10247 case M_BGE:
10248 if (op[1] == 0)
10249 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10250 else if (op[0] == 0)
10251 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
10252 else
10253 {
10254 used_at = 1;
10255 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10256 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10257 &offset_expr, AT, ZERO);
10258 }
10259 break;
10260
10261 case M_BGEZL:
10262 case M_BGEZALL:
10263 case M_BGTZL:
10264 case M_BLEZL:
10265 case M_BLTZL:
10266 case M_BLTZALL:
10267 macro_build_branch_rs (mask, &offset_expr, op[0]);
10268 break;
10269
10270 case M_BGTL_I:
10271 likely = 1;
10272 /* Fall through. */
10273 case M_BGT_I:
10274 /* Check for > max integer. */
10275 if (imm_expr.X_add_number >= GPR_SMAX)
10276 {
10277 do_false:
10278 /* Result is always false. */
10279 if (! likely)
10280 macro_build (NULL, "nop", "");
10281 else
10282 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
10283 break;
10284 }
10285 ++imm_expr.X_add_number;
10286 /* FALLTHROUGH */
10287 case M_BGE_I:
10288 case M_BGEL_I:
10289 if (mask == M_BGEL_I)
10290 likely = 1;
10291 if (imm_expr.X_add_number == 0)
10292 {
10293 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
10294 &offset_expr, op[0]);
10295 break;
10296 }
10297 if (imm_expr.X_add_number == 1)
10298 {
10299 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
10300 &offset_expr, op[0]);
10301 break;
10302 }
10303 if (imm_expr.X_add_number <= GPR_SMIN)
10304 {
10305 do_true:
10306 /* result is always true */
10307 as_warn (_("branch %s is always true"), ip->insn_mo->name);
10308 macro_build (&offset_expr, "b", "p");
10309 break;
10310 }
10311 used_at = 1;
10312 set_at (op[0], 0);
10313 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10314 &offset_expr, AT, ZERO);
10315 break;
10316
10317 case M_BGEUL:
10318 likely = 1;
10319 /* Fall through. */
10320 case M_BGEU:
10321 if (op[1] == 0)
10322 goto do_true;
10323 else if (op[0] == 0)
10324 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10325 &offset_expr, ZERO, op[1]);
10326 else
10327 {
10328 used_at = 1;
10329 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10330 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10331 &offset_expr, AT, ZERO);
10332 }
10333 break;
10334
10335 case M_BGTUL_I:
10336 likely = 1;
10337 /* Fall through. */
10338 case M_BGTU_I:
10339 if (op[0] == 0
10340 || (GPR_SIZE == 32
10341 && imm_expr.X_add_number == -1))
10342 goto do_false;
10343 ++imm_expr.X_add_number;
10344 /* FALLTHROUGH */
10345 case M_BGEU_I:
10346 case M_BGEUL_I:
10347 if (mask == M_BGEUL_I)
10348 likely = 1;
10349 if (imm_expr.X_add_number == 0)
10350 goto do_true;
10351 else if (imm_expr.X_add_number == 1)
10352 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10353 &offset_expr, op[0], ZERO);
10354 else
10355 {
10356 used_at = 1;
10357 set_at (op[0], 1);
10358 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10359 &offset_expr, AT, ZERO);
10360 }
10361 break;
10362
10363 case M_BGTL:
10364 likely = 1;
10365 /* Fall through. */
10366 case M_BGT:
10367 if (op[1] == 0)
10368 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10369 else if (op[0] == 0)
10370 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
10371 else
10372 {
10373 used_at = 1;
10374 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10375 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10376 &offset_expr, AT, ZERO);
10377 }
10378 break;
10379
10380 case M_BGTUL:
10381 likely = 1;
10382 /* Fall through. */
10383 case M_BGTU:
10384 if (op[1] == 0)
10385 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10386 &offset_expr, op[0], ZERO);
10387 else if (op[0] == 0)
10388 goto do_false;
10389 else
10390 {
10391 used_at = 1;
10392 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10393 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10394 &offset_expr, AT, ZERO);
10395 }
10396 break;
10397
10398 case M_BLEL:
10399 likely = 1;
10400 /* Fall through. */
10401 case M_BLE:
10402 if (op[1] == 0)
10403 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10404 else if (op[0] == 0)
10405 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
10406 else
10407 {
10408 used_at = 1;
10409 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10410 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10411 &offset_expr, AT, ZERO);
10412 }
10413 break;
10414
10415 case M_BLEL_I:
10416 likely = 1;
10417 /* Fall through. */
10418 case M_BLE_I:
10419 if (imm_expr.X_add_number >= GPR_SMAX)
10420 goto do_true;
10421 ++imm_expr.X_add_number;
10422 /* FALLTHROUGH */
10423 case M_BLT_I:
10424 case M_BLTL_I:
10425 if (mask == M_BLTL_I)
10426 likely = 1;
10427 if (imm_expr.X_add_number == 0)
10428 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10429 else if (imm_expr.X_add_number == 1)
10430 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10431 else
10432 {
10433 used_at = 1;
10434 set_at (op[0], 0);
10435 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10436 &offset_expr, AT, ZERO);
10437 }
10438 break;
10439
10440 case M_BLEUL:
10441 likely = 1;
10442 /* Fall through. */
10443 case M_BLEU:
10444 if (op[1] == 0)
10445 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10446 &offset_expr, op[0], ZERO);
10447 else if (op[0] == 0)
10448 goto do_true;
10449 else
10450 {
10451 used_at = 1;
10452 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10453 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10454 &offset_expr, AT, ZERO);
10455 }
10456 break;
10457
10458 case M_BLEUL_I:
10459 likely = 1;
10460 /* Fall through. */
10461 case M_BLEU_I:
10462 if (op[0] == 0
10463 || (GPR_SIZE == 32
10464 && imm_expr.X_add_number == -1))
10465 goto do_true;
10466 ++imm_expr.X_add_number;
10467 /* FALLTHROUGH */
10468 case M_BLTU_I:
10469 case M_BLTUL_I:
10470 if (mask == M_BLTUL_I)
10471 likely = 1;
10472 if (imm_expr.X_add_number == 0)
10473 goto do_false;
10474 else if (imm_expr.X_add_number == 1)
10475 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10476 &offset_expr, op[0], ZERO);
10477 else
10478 {
10479 used_at = 1;
10480 set_at (op[0], 1);
10481 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10482 &offset_expr, AT, ZERO);
10483 }
10484 break;
10485
10486 case M_BLTL:
10487 likely = 1;
10488 /* Fall through. */
10489 case M_BLT:
10490 if (op[1] == 0)
10491 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10492 else if (op[0] == 0)
10493 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
10494 else
10495 {
10496 used_at = 1;
10497 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10498 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10499 &offset_expr, AT, ZERO);
10500 }
10501 break;
10502
10503 case M_BLTUL:
10504 likely = 1;
10505 /* Fall through. */
10506 case M_BLTU:
10507 if (op[1] == 0)
10508 goto do_false;
10509 else if (op[0] == 0)
10510 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10511 &offset_expr, ZERO, op[1]);
10512 else
10513 {
10514 used_at = 1;
10515 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10516 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10517 &offset_expr, AT, ZERO);
10518 }
10519 break;
10520
10521 case M_DDIV_3:
10522 dbl = 1;
10523 /* Fall through. */
10524 case M_DIV_3:
10525 s = "mflo";
10526 goto do_div3;
10527 case M_DREM_3:
10528 dbl = 1;
10529 /* Fall through. */
10530 case M_REM_3:
10531 s = "mfhi";
10532 do_div3:
10533 if (op[2] == 0)
10534 {
10535 as_warn (_("divide by zero"));
10536 if (mips_trap)
10537 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10538 else
10539 macro_build (NULL, "break", BRK_FMT, 7);
10540 break;
10541 }
10542
10543 start_noreorder ();
10544 if (mips_trap)
10545 {
10546 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10547 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10548 }
10549 else
10550 {
10551 if (mips_opts.micromips)
10552 micromips_label_expr (&label_expr);
10553 else
10554 label_expr.X_add_number = 8;
10555 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10556 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10557 macro_build (NULL, "break", BRK_FMT, 7);
10558 if (mips_opts.micromips)
10559 micromips_add_label ();
10560 }
10561 expr1.X_add_number = -1;
10562 used_at = 1;
10563 load_register (AT, &expr1, dbl);
10564 if (mips_opts.micromips)
10565 micromips_label_expr (&label_expr);
10566 else
10567 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
10568 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
10569 if (dbl)
10570 {
10571 expr1.X_add_number = 1;
10572 load_register (AT, &expr1, dbl);
10573 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
10574 }
10575 else
10576 {
10577 expr1.X_add_number = 0x80000000;
10578 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
10579 }
10580 if (mips_trap)
10581 {
10582 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
10583 /* We want to close the noreorder block as soon as possible, so
10584 that later insns are available for delay slot filling. */
10585 end_noreorder ();
10586 }
10587 else
10588 {
10589 if (mips_opts.micromips)
10590 micromips_label_expr (&label_expr);
10591 else
10592 label_expr.X_add_number = 8;
10593 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
10594 macro_build (NULL, "nop", "");
10595
10596 /* We want to close the noreorder block as soon as possible, so
10597 that later insns are available for delay slot filling. */
10598 end_noreorder ();
10599
10600 macro_build (NULL, "break", BRK_FMT, 6);
10601 }
10602 if (mips_opts.micromips)
10603 micromips_add_label ();
10604 macro_build (NULL, s, MFHL_FMT, op[0]);
10605 break;
10606
10607 case M_DIV_3I:
10608 s = "div";
10609 s2 = "mflo";
10610 goto do_divi;
10611 case M_DIVU_3I:
10612 s = "divu";
10613 s2 = "mflo";
10614 goto do_divi;
10615 case M_REM_3I:
10616 s = "div";
10617 s2 = "mfhi";
10618 goto do_divi;
10619 case M_REMU_3I:
10620 s = "divu";
10621 s2 = "mfhi";
10622 goto do_divi;
10623 case M_DDIV_3I:
10624 dbl = 1;
10625 s = "ddiv";
10626 s2 = "mflo";
10627 goto do_divi;
10628 case M_DDIVU_3I:
10629 dbl = 1;
10630 s = "ddivu";
10631 s2 = "mflo";
10632 goto do_divi;
10633 case M_DREM_3I:
10634 dbl = 1;
10635 s = "ddiv";
10636 s2 = "mfhi";
10637 goto do_divi;
10638 case M_DREMU_3I:
10639 dbl = 1;
10640 s = "ddivu";
10641 s2 = "mfhi";
10642 do_divi:
10643 if (imm_expr.X_add_number == 0)
10644 {
10645 as_warn (_("divide by zero"));
10646 if (mips_trap)
10647 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10648 else
10649 macro_build (NULL, "break", BRK_FMT, 7);
10650 break;
10651 }
10652 if (imm_expr.X_add_number == 1)
10653 {
10654 if (strcmp (s2, "mflo") == 0)
10655 move_register (op[0], op[1]);
10656 else
10657 move_register (op[0], ZERO);
10658 break;
10659 }
10660 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
10661 {
10662 if (strcmp (s2, "mflo") == 0)
10663 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
10664 else
10665 move_register (op[0], ZERO);
10666 break;
10667 }
10668
10669 used_at = 1;
10670 load_register (AT, &imm_expr, dbl);
10671 macro_build (NULL, s, "z,s,t", op[1], AT);
10672 macro_build (NULL, s2, MFHL_FMT, op[0]);
10673 break;
10674
10675 case M_DIVU_3:
10676 s = "divu";
10677 s2 = "mflo";
10678 goto do_divu3;
10679 case M_REMU_3:
10680 s = "divu";
10681 s2 = "mfhi";
10682 goto do_divu3;
10683 case M_DDIVU_3:
10684 s = "ddivu";
10685 s2 = "mflo";
10686 goto do_divu3;
10687 case M_DREMU_3:
10688 s = "ddivu";
10689 s2 = "mfhi";
10690 do_divu3:
10691 start_noreorder ();
10692 if (mips_trap)
10693 {
10694 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10695 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10696 /* We want to close the noreorder block as soon as possible, so
10697 that later insns are available for delay slot filling. */
10698 end_noreorder ();
10699 }
10700 else
10701 {
10702 if (mips_opts.micromips)
10703 micromips_label_expr (&label_expr);
10704 else
10705 label_expr.X_add_number = 8;
10706 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10707 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10708
10709 /* We want to close the noreorder block as soon as possible, so
10710 that later insns are available for delay slot filling. */
10711 end_noreorder ();
10712 macro_build (NULL, "break", BRK_FMT, 7);
10713 if (mips_opts.micromips)
10714 micromips_add_label ();
10715 }
10716 macro_build (NULL, s2, MFHL_FMT, op[0]);
10717 break;
10718
10719 case M_DLCA_AB:
10720 dbl = 1;
10721 /* Fall through. */
10722 case M_LCA_AB:
10723 call = 1;
10724 goto do_la;
10725 case M_DLA_AB:
10726 dbl = 1;
10727 /* Fall through. */
10728 case M_LA_AB:
10729 do_la:
10730 /* Load the address of a symbol into a register. If breg is not
10731 zero, we then add a base register to it. */
10732
10733 breg = op[2];
10734 if (dbl && GPR_SIZE == 32)
10735 as_warn (_("dla used to load 32-bit register; recommend using la "
10736 "instead"));
10737
10738 if (!dbl && HAVE_64BIT_OBJECTS)
10739 as_warn (_("la used to load 64-bit address; recommend using dla "
10740 "instead"));
10741
10742 if (small_offset_p (0, align, 16))
10743 {
10744 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
10745 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10746 break;
10747 }
10748
10749 if (mips_opts.at && (op[0] == breg))
10750 {
10751 tempreg = AT;
10752 used_at = 1;
10753 }
10754 else
10755 tempreg = op[0];
10756
10757 if (offset_expr.X_op != O_symbol
10758 && offset_expr.X_op != O_constant)
10759 {
10760 as_bad (_("expression too complex"));
10761 offset_expr.X_op = O_constant;
10762 }
10763
10764 if (offset_expr.X_op == O_constant)
10765 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
10766 else if (mips_pic == NO_PIC)
10767 {
10768 /* If this is a reference to a GP relative symbol, we want
10769 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
10770 Otherwise we want
10771 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10772 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10773 If we have a constant, we need two instructions anyhow,
10774 so we may as well always use the latter form.
10775
10776 With 64bit address space and a usable $at we want
10777 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10778 lui $at,<sym> (BFD_RELOC_HI16_S)
10779 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10780 daddiu $at,<sym> (BFD_RELOC_LO16)
10781 dsll32 $tempreg,0
10782 daddu $tempreg,$tempreg,$at
10783
10784 If $at is already in use, we use a path which is suboptimal
10785 on superscalar processors.
10786 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10787 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10788 dsll $tempreg,16
10789 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10790 dsll $tempreg,16
10791 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10792
10793 For GP relative symbols in 64bit address space we can use
10794 the same sequence as in 32bit address space. */
10795 if (HAVE_64BIT_SYMBOLS)
10796 {
10797 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10798 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10799 {
10800 relax_start (offset_expr.X_add_symbol);
10801 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10802 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10803 relax_switch ();
10804 }
10805
10806 if (used_at == 0 && mips_opts.at)
10807 {
10808 macro_build (&offset_expr, "lui", LUI_FMT,
10809 tempreg, BFD_RELOC_MIPS_HIGHEST);
10810 macro_build (&offset_expr, "lui", LUI_FMT,
10811 AT, BFD_RELOC_HI16_S);
10812 macro_build (&offset_expr, "daddiu", "t,r,j",
10813 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10814 macro_build (&offset_expr, "daddiu", "t,r,j",
10815 AT, AT, BFD_RELOC_LO16);
10816 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
10817 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
10818 used_at = 1;
10819 }
10820 else
10821 {
10822 macro_build (&offset_expr, "lui", LUI_FMT,
10823 tempreg, BFD_RELOC_MIPS_HIGHEST);
10824 macro_build (&offset_expr, "daddiu", "t,r,j",
10825 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10826 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10827 macro_build (&offset_expr, "daddiu", "t,r,j",
10828 tempreg, tempreg, BFD_RELOC_HI16_S);
10829 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10830 macro_build (&offset_expr, "daddiu", "t,r,j",
10831 tempreg, tempreg, BFD_RELOC_LO16);
10832 }
10833
10834 if (mips_relax.sequence)
10835 relax_end ();
10836 }
10837 else
10838 {
10839 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10840 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10841 {
10842 relax_start (offset_expr.X_add_symbol);
10843 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10844 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10845 relax_switch ();
10846 }
10847 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
10848 as_bad (_("offset too large"));
10849 macro_build_lui (&offset_expr, tempreg);
10850 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10851 tempreg, tempreg, BFD_RELOC_LO16);
10852 if (mips_relax.sequence)
10853 relax_end ();
10854 }
10855 }
10856 else if (!mips_big_got && !HAVE_NEWABI)
10857 {
10858 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10859
10860 /* If this is a reference to an external symbol, and there
10861 is no constant, we want
10862 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10863 or for lca or if tempreg is PIC_CALL_REG
10864 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10865 For a local symbol, we want
10866 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10867 nop
10868 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10869
10870 If we have a small constant, and this is a reference to
10871 an external symbol, we want
10872 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10873 nop
10874 addiu $tempreg,$tempreg,<constant>
10875 For a local symbol, we want the same instruction
10876 sequence, but we output a BFD_RELOC_LO16 reloc on the
10877 addiu instruction.
10878
10879 If we have a large constant, and this is a reference to
10880 an external symbol, we want
10881 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10882 lui $at,<hiconstant>
10883 addiu $at,$at,<loconstant>
10884 addu $tempreg,$tempreg,$at
10885 For a local symbol, we want the same instruction
10886 sequence, but we output a BFD_RELOC_LO16 reloc on the
10887 addiu instruction.
10888 */
10889
10890 if (offset_expr.X_add_number == 0)
10891 {
10892 if (mips_pic == SVR4_PIC
10893 && breg == 0
10894 && (call || tempreg == PIC_CALL_REG))
10895 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10896
10897 relax_start (offset_expr.X_add_symbol);
10898 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10899 lw_reloc_type, mips_gp_register);
10900 if (breg != 0)
10901 {
10902 /* We're going to put in an addu instruction using
10903 tempreg, so we may as well insert the nop right
10904 now. */
10905 load_delay_nop ();
10906 }
10907 relax_switch ();
10908 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10909 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
10910 load_delay_nop ();
10911 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10912 tempreg, tempreg, BFD_RELOC_LO16);
10913 relax_end ();
10914 /* FIXME: If breg == 0, and the next instruction uses
10915 $tempreg, then if this variant case is used an extra
10916 nop will be generated. */
10917 }
10918 else if (offset_expr.X_add_number >= -0x8000
10919 && offset_expr.X_add_number < 0x8000)
10920 {
10921 load_got_offset (tempreg, &offset_expr);
10922 load_delay_nop ();
10923 add_got_offset (tempreg, &offset_expr);
10924 }
10925 else
10926 {
10927 expr1.X_add_number = offset_expr.X_add_number;
10928 offset_expr.X_add_number =
10929 SEXT_16BIT (offset_expr.X_add_number);
10930 load_got_offset (tempreg, &offset_expr);
10931 offset_expr.X_add_number = expr1.X_add_number;
10932 /* If we are going to add in a base register, and the
10933 target register and the base register are the same,
10934 then we are using AT as a temporary register. Since
10935 we want to load the constant into AT, we add our
10936 current AT (from the global offset table) and the
10937 register into the register now, and pretend we were
10938 not using a base register. */
10939 if (breg == op[0])
10940 {
10941 load_delay_nop ();
10942 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10943 op[0], AT, breg);
10944 breg = 0;
10945 tempreg = op[0];
10946 }
10947 add_got_offset_hilo (tempreg, &offset_expr, AT);
10948 used_at = 1;
10949 }
10950 }
10951 else if (!mips_big_got && HAVE_NEWABI)
10952 {
10953 int add_breg_early = 0;
10954
10955 /* If this is a reference to an external, and there is no
10956 constant, or local symbol (*), with or without a
10957 constant, we want
10958 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10959 or for lca or if tempreg is PIC_CALL_REG
10960 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10961
10962 If we have a small constant, and this is a reference to
10963 an external symbol, we want
10964 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10965 addiu $tempreg,$tempreg,<constant>
10966
10967 If we have a large constant, and this is a reference to
10968 an external symbol, we want
10969 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10970 lui $at,<hiconstant>
10971 addiu $at,$at,<loconstant>
10972 addu $tempreg,$tempreg,$at
10973
10974 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
10975 local symbols, even though it introduces an additional
10976 instruction. */
10977
10978 if (offset_expr.X_add_number)
10979 {
10980 expr1.X_add_number = offset_expr.X_add_number;
10981 offset_expr.X_add_number = 0;
10982
10983 relax_start (offset_expr.X_add_symbol);
10984 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10985 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10986
10987 if (expr1.X_add_number >= -0x8000
10988 && expr1.X_add_number < 0x8000)
10989 {
10990 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10991 tempreg, tempreg, BFD_RELOC_LO16);
10992 }
10993 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
10994 {
10995 unsigned int dreg;
10996
10997 /* If we are going to add in a base register, and the
10998 target register and the base register are the same,
10999 then we are using AT as a temporary register. Since
11000 we want to load the constant into AT, we add our
11001 current AT (from the global offset table) and the
11002 register into the register now, and pretend we were
11003 not using a base register. */
11004 if (breg != op[0])
11005 dreg = tempreg;
11006 else
11007 {
11008 gas_assert (tempreg == AT);
11009 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11010 op[0], AT, breg);
11011 dreg = op[0];
11012 add_breg_early = 1;
11013 }
11014
11015 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11016 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11017 dreg, dreg, AT);
11018
11019 used_at = 1;
11020 }
11021 else
11022 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11023
11024 relax_switch ();
11025 offset_expr.X_add_number = expr1.X_add_number;
11026
11027 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11028 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11029 if (add_breg_early)
11030 {
11031 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11032 op[0], tempreg, breg);
11033 breg = 0;
11034 tempreg = op[0];
11035 }
11036 relax_end ();
11037 }
11038 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
11039 {
11040 relax_start (offset_expr.X_add_symbol);
11041 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11042 BFD_RELOC_MIPS_CALL16, mips_gp_register);
11043 relax_switch ();
11044 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11045 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11046 relax_end ();
11047 }
11048 else
11049 {
11050 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11051 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11052 }
11053 }
11054 else if (mips_big_got && !HAVE_NEWABI)
11055 {
11056 int gpdelay;
11057 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11058 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11059 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11060
11061 /* This is the large GOT case. If this is a reference to an
11062 external symbol, and there is no constant, we want
11063 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11064 addu $tempreg,$tempreg,$gp
11065 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11066 or for lca or if tempreg is PIC_CALL_REG
11067 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11068 addu $tempreg,$tempreg,$gp
11069 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11070 For a local symbol, we want
11071 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11072 nop
11073 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11074
11075 If we have a small constant, and this is a reference to
11076 an external symbol, we want
11077 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11078 addu $tempreg,$tempreg,$gp
11079 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11080 nop
11081 addiu $tempreg,$tempreg,<constant>
11082 For a local symbol, we want
11083 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11084 nop
11085 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11086
11087 If we have a large constant, and this is a reference to
11088 an external symbol, we want
11089 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11090 addu $tempreg,$tempreg,$gp
11091 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11092 lui $at,<hiconstant>
11093 addiu $at,$at,<loconstant>
11094 addu $tempreg,$tempreg,$at
11095 For a local symbol, we want
11096 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11097 lui $at,<hiconstant>
11098 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11099 addu $tempreg,$tempreg,$at
11100 */
11101
11102 expr1.X_add_number = offset_expr.X_add_number;
11103 offset_expr.X_add_number = 0;
11104 relax_start (offset_expr.X_add_symbol);
11105 gpdelay = reg_needs_delay (mips_gp_register);
11106 if (expr1.X_add_number == 0 && breg == 0
11107 && (call || tempreg == PIC_CALL_REG))
11108 {
11109 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11110 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11111 }
11112 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11113 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11114 tempreg, tempreg, mips_gp_register);
11115 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11116 tempreg, lw_reloc_type, tempreg);
11117 if (expr1.X_add_number == 0)
11118 {
11119 if (breg != 0)
11120 {
11121 /* We're going to put in an addu instruction using
11122 tempreg, so we may as well insert the nop right
11123 now. */
11124 load_delay_nop ();
11125 }
11126 }
11127 else if (expr1.X_add_number >= -0x8000
11128 && expr1.X_add_number < 0x8000)
11129 {
11130 load_delay_nop ();
11131 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11132 tempreg, tempreg, BFD_RELOC_LO16);
11133 }
11134 else
11135 {
11136 unsigned int dreg;
11137
11138 /* If we are going to add in a base register, and the
11139 target register and the base register are the same,
11140 then we are using AT as a temporary register. Since
11141 we want to load the constant into AT, we add our
11142 current AT (from the global offset table) and the
11143 register into the register now, and pretend we were
11144 not using a base register. */
11145 if (breg != op[0])
11146 dreg = tempreg;
11147 else
11148 {
11149 gas_assert (tempreg == AT);
11150 load_delay_nop ();
11151 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11152 op[0], AT, breg);
11153 dreg = op[0];
11154 }
11155
11156 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11157 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11158
11159 used_at = 1;
11160 }
11161 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
11162 relax_switch ();
11163
11164 if (gpdelay)
11165 {
11166 /* This is needed because this instruction uses $gp, but
11167 the first instruction on the main stream does not. */
11168 macro_build (NULL, "nop", "");
11169 }
11170
11171 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11172 local_reloc_type, mips_gp_register);
11173 if (expr1.X_add_number >= -0x8000
11174 && expr1.X_add_number < 0x8000)
11175 {
11176 load_delay_nop ();
11177 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11178 tempreg, tempreg, BFD_RELOC_LO16);
11179 /* FIXME: If add_number is 0, and there was no base
11180 register, the external symbol case ended with a load,
11181 so if the symbol turns out to not be external, and
11182 the next instruction uses tempreg, an unnecessary nop
11183 will be inserted. */
11184 }
11185 else
11186 {
11187 if (breg == op[0])
11188 {
11189 /* We must add in the base register now, as in the
11190 external symbol case. */
11191 gas_assert (tempreg == AT);
11192 load_delay_nop ();
11193 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11194 op[0], AT, breg);
11195 tempreg = op[0];
11196 /* We set breg to 0 because we have arranged to add
11197 it in in both cases. */
11198 breg = 0;
11199 }
11200
11201 macro_build_lui (&expr1, AT);
11202 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11203 AT, AT, BFD_RELOC_LO16);
11204 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11205 tempreg, tempreg, AT);
11206 used_at = 1;
11207 }
11208 relax_end ();
11209 }
11210 else if (mips_big_got && HAVE_NEWABI)
11211 {
11212 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11213 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11214 int add_breg_early = 0;
11215
11216 /* This is the large GOT case. If this is a reference to an
11217 external symbol, and there is no constant, we want
11218 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11219 add $tempreg,$tempreg,$gp
11220 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11221 or for lca or if tempreg is PIC_CALL_REG
11222 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11223 add $tempreg,$tempreg,$gp
11224 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11225
11226 If we have a small constant, and this is a reference to
11227 an external symbol, we want
11228 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11229 add $tempreg,$tempreg,$gp
11230 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11231 addi $tempreg,$tempreg,<constant>
11232
11233 If we have a large constant, and this is a reference to
11234 an external symbol, we want
11235 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11236 addu $tempreg,$tempreg,$gp
11237 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11238 lui $at,<hiconstant>
11239 addi $at,$at,<loconstant>
11240 add $tempreg,$tempreg,$at
11241
11242 If we have NewABI, and we know it's a local symbol, we want
11243 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11244 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11245 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11246
11247 relax_start (offset_expr.X_add_symbol);
11248
11249 expr1.X_add_number = offset_expr.X_add_number;
11250 offset_expr.X_add_number = 0;
11251
11252 if (expr1.X_add_number == 0 && breg == 0
11253 && (call || tempreg == PIC_CALL_REG))
11254 {
11255 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11256 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11257 }
11258 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11259 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11260 tempreg, tempreg, mips_gp_register);
11261 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11262 tempreg, lw_reloc_type, tempreg);
11263
11264 if (expr1.X_add_number == 0)
11265 ;
11266 else if (expr1.X_add_number >= -0x8000
11267 && expr1.X_add_number < 0x8000)
11268 {
11269 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11270 tempreg, tempreg, BFD_RELOC_LO16);
11271 }
11272 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11273 {
11274 unsigned int dreg;
11275
11276 /* If we are going to add in a base register, and the
11277 target register and the base register are the same,
11278 then we are using AT as a temporary register. Since
11279 we want to load the constant into AT, we add our
11280 current AT (from the global offset table) and the
11281 register into the register now, and pretend we were
11282 not using a base register. */
11283 if (breg != op[0])
11284 dreg = tempreg;
11285 else
11286 {
11287 gas_assert (tempreg == AT);
11288 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11289 op[0], AT, breg);
11290 dreg = op[0];
11291 add_breg_early = 1;
11292 }
11293
11294 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11295 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11296
11297 used_at = 1;
11298 }
11299 else
11300 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11301
11302 relax_switch ();
11303 offset_expr.X_add_number = expr1.X_add_number;
11304 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11305 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11306 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11307 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11308 if (add_breg_early)
11309 {
11310 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11311 op[0], tempreg, breg);
11312 breg = 0;
11313 tempreg = op[0];
11314 }
11315 relax_end ();
11316 }
11317 else
11318 abort ();
11319
11320 if (breg != 0)
11321 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
11322 break;
11323
11324 case M_MSGSND:
11325 gas_assert (!mips_opts.micromips);
11326 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
11327 break;
11328
11329 case M_MSGLD:
11330 gas_assert (!mips_opts.micromips);
11331 macro_build (NULL, "c2", "C", 0x02);
11332 break;
11333
11334 case M_MSGLD_T:
11335 gas_assert (!mips_opts.micromips);
11336 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
11337 break;
11338
11339 case M_MSGWAIT:
11340 gas_assert (!mips_opts.micromips);
11341 macro_build (NULL, "c2", "C", 3);
11342 break;
11343
11344 case M_MSGWAIT_T:
11345 gas_assert (!mips_opts.micromips);
11346 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
11347 break;
11348
11349 case M_J_A:
11350 /* The j instruction may not be used in PIC code, since it
11351 requires an absolute address. We convert it to a b
11352 instruction. */
11353 if (mips_pic == NO_PIC)
11354 macro_build (&offset_expr, "j", "a");
11355 else
11356 macro_build (&offset_expr, "b", "p");
11357 break;
11358
11359 /* The jal instructions must be handled as macros because when
11360 generating PIC code they expand to multi-instruction
11361 sequences. Normally they are simple instructions. */
11362 case M_JALS_1:
11363 op[1] = op[0];
11364 op[0] = RA;
11365 /* Fall through. */
11366 case M_JALS_2:
11367 gas_assert (mips_opts.micromips);
11368 if (mips_opts.insn32)
11369 {
11370 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11371 break;
11372 }
11373 jals = 1;
11374 goto jal;
11375 case M_JAL_1:
11376 op[1] = op[0];
11377 op[0] = RA;
11378 /* Fall through. */
11379 case M_JAL_2:
11380 jal:
11381 if (mips_pic == NO_PIC)
11382 {
11383 s = jals ? "jalrs" : "jalr";
11384 if (mips_opts.micromips
11385 && !mips_opts.insn32
11386 && op[0] == RA
11387 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11388 macro_build (NULL, s, "mj", op[1]);
11389 else
11390 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11391 }
11392 else
11393 {
11394 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11395 && mips_cprestore_offset >= 0);
11396
11397 if (op[1] != PIC_CALL_REG)
11398 as_warn (_("MIPS PIC call to register other than $25"));
11399
11400 s = ((mips_opts.micromips
11401 && !mips_opts.insn32
11402 && (!mips_opts.noreorder || cprestore))
11403 ? "jalrs" : "jalr");
11404 if (mips_opts.micromips
11405 && !mips_opts.insn32
11406 && op[0] == RA
11407 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11408 macro_build (NULL, s, "mj", op[1]);
11409 else
11410 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11411 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
11412 {
11413 if (mips_cprestore_offset < 0)
11414 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11415 else
11416 {
11417 if (!mips_frame_reg_valid)
11418 {
11419 as_warn (_("no .frame pseudo-op used in PIC code"));
11420 /* Quiet this warning. */
11421 mips_frame_reg_valid = 1;
11422 }
11423 if (!mips_cprestore_valid)
11424 {
11425 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11426 /* Quiet this warning. */
11427 mips_cprestore_valid = 1;
11428 }
11429 if (mips_opts.noreorder)
11430 macro_build (NULL, "nop", "");
11431 expr1.X_add_number = mips_cprestore_offset;
11432 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11433 mips_gp_register,
11434 mips_frame_reg,
11435 HAVE_64BIT_ADDRESSES);
11436 }
11437 }
11438 }
11439
11440 break;
11441
11442 case M_JALS_A:
11443 gas_assert (mips_opts.micromips);
11444 if (mips_opts.insn32)
11445 {
11446 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11447 break;
11448 }
11449 jals = 1;
11450 /* Fall through. */
11451 case M_JAL_A:
11452 if (mips_pic == NO_PIC)
11453 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
11454 else if (mips_pic == SVR4_PIC)
11455 {
11456 /* If this is a reference to an external symbol, and we are
11457 using a small GOT, we want
11458 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11459 nop
11460 jalr $ra,$25
11461 nop
11462 lw $gp,cprestore($sp)
11463 The cprestore value is set using the .cprestore
11464 pseudo-op. If we are using a big GOT, we want
11465 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11466 addu $25,$25,$gp
11467 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11468 nop
11469 jalr $ra,$25
11470 nop
11471 lw $gp,cprestore($sp)
11472 If the symbol is not external, we want
11473 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11474 nop
11475 addiu $25,$25,<sym> (BFD_RELOC_LO16)
11476 jalr $ra,$25
11477 nop
11478 lw $gp,cprestore($sp)
11479
11480 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11481 sequences above, minus nops, unless the symbol is local,
11482 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11483 GOT_DISP. */
11484 if (HAVE_NEWABI)
11485 {
11486 if (!mips_big_got)
11487 {
11488 relax_start (offset_expr.X_add_symbol);
11489 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11490 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11491 mips_gp_register);
11492 relax_switch ();
11493 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11494 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
11495 mips_gp_register);
11496 relax_end ();
11497 }
11498 else
11499 {
11500 relax_start (offset_expr.X_add_symbol);
11501 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11502 BFD_RELOC_MIPS_CALL_HI16);
11503 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11504 PIC_CALL_REG, mips_gp_register);
11505 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11506 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11507 PIC_CALL_REG);
11508 relax_switch ();
11509 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11510 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11511 mips_gp_register);
11512 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11513 PIC_CALL_REG, PIC_CALL_REG,
11514 BFD_RELOC_MIPS_GOT_OFST);
11515 relax_end ();
11516 }
11517
11518 macro_build_jalr (&offset_expr, 0);
11519 }
11520 else
11521 {
11522 relax_start (offset_expr.X_add_symbol);
11523 if (!mips_big_got)
11524 {
11525 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11526 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11527 mips_gp_register);
11528 load_delay_nop ();
11529 relax_switch ();
11530 }
11531 else
11532 {
11533 int gpdelay;
11534
11535 gpdelay = reg_needs_delay (mips_gp_register);
11536 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11537 BFD_RELOC_MIPS_CALL_HI16);
11538 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11539 PIC_CALL_REG, mips_gp_register);
11540 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11541 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11542 PIC_CALL_REG);
11543 load_delay_nop ();
11544 relax_switch ();
11545 if (gpdelay)
11546 macro_build (NULL, "nop", "");
11547 }
11548 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11549 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
11550 mips_gp_register);
11551 load_delay_nop ();
11552 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11553 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
11554 relax_end ();
11555 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
11556
11557 if (mips_cprestore_offset < 0)
11558 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11559 else
11560 {
11561 if (!mips_frame_reg_valid)
11562 {
11563 as_warn (_("no .frame pseudo-op used in PIC code"));
11564 /* Quiet this warning. */
11565 mips_frame_reg_valid = 1;
11566 }
11567 if (!mips_cprestore_valid)
11568 {
11569 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11570 /* Quiet this warning. */
11571 mips_cprestore_valid = 1;
11572 }
11573 if (mips_opts.noreorder)
11574 macro_build (NULL, "nop", "");
11575 expr1.X_add_number = mips_cprestore_offset;
11576 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11577 mips_gp_register,
11578 mips_frame_reg,
11579 HAVE_64BIT_ADDRESSES);
11580 }
11581 }
11582 }
11583 else if (mips_pic == VXWORKS_PIC)
11584 as_bad (_("non-PIC jump used in PIC library"));
11585 else
11586 abort ();
11587
11588 break;
11589
11590 case M_LBUE_AB:
11591 s = "lbue";
11592 fmt = "t,+j(b)";
11593 offbits = 9;
11594 goto ld_st;
11595 case M_LHUE_AB:
11596 s = "lhue";
11597 fmt = "t,+j(b)";
11598 offbits = 9;
11599 goto ld_st;
11600 case M_LBE_AB:
11601 s = "lbe";
11602 fmt = "t,+j(b)";
11603 offbits = 9;
11604 goto ld_st;
11605 case M_LHE_AB:
11606 s = "lhe";
11607 fmt = "t,+j(b)";
11608 offbits = 9;
11609 goto ld_st;
11610 case M_LLE_AB:
11611 s = "lle";
11612 fmt = "t,+j(b)";
11613 offbits = 9;
11614 goto ld_st;
11615 case M_LWE_AB:
11616 s = "lwe";
11617 fmt = "t,+j(b)";
11618 offbits = 9;
11619 goto ld_st;
11620 case M_LWLE_AB:
11621 s = "lwle";
11622 fmt = "t,+j(b)";
11623 offbits = 9;
11624 goto ld_st;
11625 case M_LWRE_AB:
11626 s = "lwre";
11627 fmt = "t,+j(b)";
11628 offbits = 9;
11629 goto ld_st;
11630 case M_SBE_AB:
11631 s = "sbe";
11632 fmt = "t,+j(b)";
11633 offbits = 9;
11634 goto ld_st;
11635 case M_SCE_AB:
11636 s = "sce";
11637 fmt = "t,+j(b)";
11638 offbits = 9;
11639 goto ld_st;
11640 case M_SHE_AB:
11641 s = "she";
11642 fmt = "t,+j(b)";
11643 offbits = 9;
11644 goto ld_st;
11645 case M_SWE_AB:
11646 s = "swe";
11647 fmt = "t,+j(b)";
11648 offbits = 9;
11649 goto ld_st;
11650 case M_SWLE_AB:
11651 s = "swle";
11652 fmt = "t,+j(b)";
11653 offbits = 9;
11654 goto ld_st;
11655 case M_SWRE_AB:
11656 s = "swre";
11657 fmt = "t,+j(b)";
11658 offbits = 9;
11659 goto ld_st;
11660 case M_ACLR_AB:
11661 s = "aclr";
11662 fmt = "\\,~(b)";
11663 offbits = 12;
11664 goto ld_st;
11665 case M_ASET_AB:
11666 s = "aset";
11667 fmt = "\\,~(b)";
11668 offbits = 12;
11669 goto ld_st;
11670 case M_LB_AB:
11671 s = "lb";
11672 fmt = "t,o(b)";
11673 goto ld;
11674 case M_LBU_AB:
11675 s = "lbu";
11676 fmt = "t,o(b)";
11677 goto ld;
11678 case M_LH_AB:
11679 s = "lh";
11680 fmt = "t,o(b)";
11681 goto ld;
11682 case M_LHU_AB:
11683 s = "lhu";
11684 fmt = "t,o(b)";
11685 goto ld;
11686 case M_LW_AB:
11687 s = "lw";
11688 fmt = "t,o(b)";
11689 goto ld;
11690 case M_LWC0_AB:
11691 gas_assert (!mips_opts.micromips);
11692 s = "lwc0";
11693 fmt = "E,o(b)";
11694 /* Itbl support may require additional care here. */
11695 coproc = 1;
11696 goto ld_st;
11697 case M_LWC1_AB:
11698 s = "lwc1";
11699 fmt = "T,o(b)";
11700 /* Itbl support may require additional care here. */
11701 coproc = 1;
11702 goto ld_st;
11703 case M_LWC2_AB:
11704 s = "lwc2";
11705 fmt = COP12_FMT;
11706 offbits = (mips_opts.micromips ? 12
11707 : ISA_IS_R6 (mips_opts.isa) ? 11
11708 : 16);
11709 /* Itbl support may require additional care here. */
11710 coproc = 1;
11711 goto ld_st;
11712 case M_LWC3_AB:
11713 gas_assert (!mips_opts.micromips);
11714 s = "lwc3";
11715 fmt = "E,o(b)";
11716 /* Itbl support may require additional care here. */
11717 coproc = 1;
11718 goto ld_st;
11719 case M_LWL_AB:
11720 s = "lwl";
11721 fmt = MEM12_FMT;
11722 offbits = (mips_opts.micromips ? 12 : 16);
11723 goto ld_st;
11724 case M_LWR_AB:
11725 s = "lwr";
11726 fmt = MEM12_FMT;
11727 offbits = (mips_opts.micromips ? 12 : 16);
11728 goto ld_st;
11729 case M_LDC1_AB:
11730 s = "ldc1";
11731 fmt = "T,o(b)";
11732 /* Itbl support may require additional care here. */
11733 coproc = 1;
11734 goto ld_st;
11735 case M_LDC2_AB:
11736 s = "ldc2";
11737 fmt = COP12_FMT;
11738 offbits = (mips_opts.micromips ? 12
11739 : ISA_IS_R6 (mips_opts.isa) ? 11
11740 : 16);
11741 /* Itbl support may require additional care here. */
11742 coproc = 1;
11743 goto ld_st;
11744 case M_LQC2_AB:
11745 s = "lqc2";
11746 fmt = "+7,o(b)";
11747 /* Itbl support may require additional care here. */
11748 coproc = 1;
11749 goto ld_st;
11750 case M_LDC3_AB:
11751 s = "ldc3";
11752 fmt = "E,o(b)";
11753 /* Itbl support may require additional care here. */
11754 coproc = 1;
11755 goto ld_st;
11756 case M_LDL_AB:
11757 s = "ldl";
11758 fmt = MEM12_FMT;
11759 offbits = (mips_opts.micromips ? 12 : 16);
11760 goto ld_st;
11761 case M_LDR_AB:
11762 s = "ldr";
11763 fmt = MEM12_FMT;
11764 offbits = (mips_opts.micromips ? 12 : 16);
11765 goto ld_st;
11766 case M_LL_AB:
11767 s = "ll";
11768 fmt = LL_SC_FMT;
11769 offbits = (mips_opts.micromips ? 12
11770 : ISA_IS_R6 (mips_opts.isa) ? 9
11771 : 16);
11772 goto ld;
11773 case M_LLD_AB:
11774 s = "lld";
11775 fmt = LL_SC_FMT;
11776 offbits = (mips_opts.micromips ? 12
11777 : ISA_IS_R6 (mips_opts.isa) ? 9
11778 : 16);
11779 goto ld;
11780 case M_LWU_AB:
11781 s = "lwu";
11782 fmt = MEM12_FMT;
11783 offbits = (mips_opts.micromips ? 12 : 16);
11784 goto ld;
11785 case M_LWP_AB:
11786 gas_assert (mips_opts.micromips);
11787 s = "lwp";
11788 fmt = "t,~(b)";
11789 offbits = 12;
11790 lp = 1;
11791 goto ld;
11792 case M_LDP_AB:
11793 gas_assert (mips_opts.micromips);
11794 s = "ldp";
11795 fmt = "t,~(b)";
11796 offbits = 12;
11797 lp = 1;
11798 goto ld;
11799 case M_LWM_AB:
11800 gas_assert (mips_opts.micromips);
11801 s = "lwm";
11802 fmt = "n,~(b)";
11803 offbits = 12;
11804 goto ld_st;
11805 case M_LDM_AB:
11806 gas_assert (mips_opts.micromips);
11807 s = "ldm";
11808 fmt = "n,~(b)";
11809 offbits = 12;
11810 goto ld_st;
11811
11812 ld:
11813 /* We don't want to use $0 as tempreg. */
11814 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
11815 goto ld_st;
11816 else
11817 tempreg = op[0] + lp;
11818 goto ld_noat;
11819
11820 case M_SB_AB:
11821 s = "sb";
11822 fmt = "t,o(b)";
11823 goto ld_st;
11824 case M_SH_AB:
11825 s = "sh";
11826 fmt = "t,o(b)";
11827 goto ld_st;
11828 case M_SW_AB:
11829 s = "sw";
11830 fmt = "t,o(b)";
11831 goto ld_st;
11832 case M_SWC0_AB:
11833 gas_assert (!mips_opts.micromips);
11834 s = "swc0";
11835 fmt = "E,o(b)";
11836 /* Itbl support may require additional care here. */
11837 coproc = 1;
11838 goto ld_st;
11839 case M_SWC1_AB:
11840 s = "swc1";
11841 fmt = "T,o(b)";
11842 /* Itbl support may require additional care here. */
11843 coproc = 1;
11844 goto ld_st;
11845 case M_SWC2_AB:
11846 s = "swc2";
11847 fmt = COP12_FMT;
11848 offbits = (mips_opts.micromips ? 12
11849 : ISA_IS_R6 (mips_opts.isa) ? 11
11850 : 16);
11851 /* Itbl support may require additional care here. */
11852 coproc = 1;
11853 goto ld_st;
11854 case M_SWC3_AB:
11855 gas_assert (!mips_opts.micromips);
11856 s = "swc3";
11857 fmt = "E,o(b)";
11858 /* Itbl support may require additional care here. */
11859 coproc = 1;
11860 goto ld_st;
11861 case M_SWL_AB:
11862 s = "swl";
11863 fmt = MEM12_FMT;
11864 offbits = (mips_opts.micromips ? 12 : 16);
11865 goto ld_st;
11866 case M_SWR_AB:
11867 s = "swr";
11868 fmt = MEM12_FMT;
11869 offbits = (mips_opts.micromips ? 12 : 16);
11870 goto ld_st;
11871 case M_SC_AB:
11872 s = "sc";
11873 fmt = LL_SC_FMT;
11874 offbits = (mips_opts.micromips ? 12
11875 : ISA_IS_R6 (mips_opts.isa) ? 9
11876 : 16);
11877 goto ld_st;
11878 case M_SCD_AB:
11879 s = "scd";
11880 fmt = LL_SC_FMT;
11881 offbits = (mips_opts.micromips ? 12
11882 : ISA_IS_R6 (mips_opts.isa) ? 9
11883 : 16);
11884 goto ld_st;
11885 case M_CACHE_AB:
11886 s = "cache";
11887 fmt = (mips_opts.micromips ? "k,~(b)"
11888 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11889 : "k,o(b)");
11890 offbits = (mips_opts.micromips ? 12
11891 : ISA_IS_R6 (mips_opts.isa) ? 9
11892 : 16);
11893 goto ld_st;
11894 case M_CACHEE_AB:
11895 s = "cachee";
11896 fmt = "k,+j(b)";
11897 offbits = 9;
11898 goto ld_st;
11899 case M_PREF_AB:
11900 s = "pref";
11901 fmt = (mips_opts.micromips ? "k,~(b)"
11902 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11903 : "k,o(b)");
11904 offbits = (mips_opts.micromips ? 12
11905 : ISA_IS_R6 (mips_opts.isa) ? 9
11906 : 16);
11907 goto ld_st;
11908 case M_PREFE_AB:
11909 s = "prefe";
11910 fmt = "k,+j(b)";
11911 offbits = 9;
11912 goto ld_st;
11913 case M_SDC1_AB:
11914 s = "sdc1";
11915 fmt = "T,o(b)";
11916 coproc = 1;
11917 /* Itbl support may require additional care here. */
11918 goto ld_st;
11919 case M_SDC2_AB:
11920 s = "sdc2";
11921 fmt = COP12_FMT;
11922 offbits = (mips_opts.micromips ? 12
11923 : ISA_IS_R6 (mips_opts.isa) ? 11
11924 : 16);
11925 /* Itbl support may require additional care here. */
11926 coproc = 1;
11927 goto ld_st;
11928 case M_SQC2_AB:
11929 s = "sqc2";
11930 fmt = "+7,o(b)";
11931 /* Itbl support may require additional care here. */
11932 coproc = 1;
11933 goto ld_st;
11934 case M_SDC3_AB:
11935 gas_assert (!mips_opts.micromips);
11936 s = "sdc3";
11937 fmt = "E,o(b)";
11938 /* Itbl support may require additional care here. */
11939 coproc = 1;
11940 goto ld_st;
11941 case M_SDL_AB:
11942 s = "sdl";
11943 fmt = MEM12_FMT;
11944 offbits = (mips_opts.micromips ? 12 : 16);
11945 goto ld_st;
11946 case M_SDR_AB:
11947 s = "sdr";
11948 fmt = MEM12_FMT;
11949 offbits = (mips_opts.micromips ? 12 : 16);
11950 goto ld_st;
11951 case M_SWP_AB:
11952 gas_assert (mips_opts.micromips);
11953 s = "swp";
11954 fmt = "t,~(b)";
11955 offbits = 12;
11956 goto ld_st;
11957 case M_SDP_AB:
11958 gas_assert (mips_opts.micromips);
11959 s = "sdp";
11960 fmt = "t,~(b)";
11961 offbits = 12;
11962 goto ld_st;
11963 case M_SWM_AB:
11964 gas_assert (mips_opts.micromips);
11965 s = "swm";
11966 fmt = "n,~(b)";
11967 offbits = 12;
11968 goto ld_st;
11969 case M_SDM_AB:
11970 gas_assert (mips_opts.micromips);
11971 s = "sdm";
11972 fmt = "n,~(b)";
11973 offbits = 12;
11974
11975 ld_st:
11976 tempreg = AT;
11977 ld_noat:
11978 breg = op[2];
11979 if (small_offset_p (0, align, 16))
11980 {
11981 /* The first case exists for M_LD_AB and M_SD_AB, which are
11982 macros for o32 but which should act like normal instructions
11983 otherwise. */
11984 if (offbits == 16)
11985 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
11986 offset_reloc[1], offset_reloc[2], breg);
11987 else if (small_offset_p (0, align, offbits))
11988 {
11989 if (offbits == 0)
11990 macro_build (NULL, s, fmt, op[0], breg);
11991 else
11992 macro_build (NULL, s, fmt, op[0],
11993 (int) offset_expr.X_add_number, breg);
11994 }
11995 else
11996 {
11997 if (tempreg == AT)
11998 used_at = 1;
11999 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12000 tempreg, breg, -1, offset_reloc[0],
12001 offset_reloc[1], offset_reloc[2]);
12002 if (offbits == 0)
12003 macro_build (NULL, s, fmt, op[0], tempreg);
12004 else
12005 macro_build (NULL, s, fmt, op[0], 0, tempreg);
12006 }
12007 break;
12008 }
12009
12010 if (tempreg == AT)
12011 used_at = 1;
12012
12013 if (offset_expr.X_op != O_constant
12014 && offset_expr.X_op != O_symbol)
12015 {
12016 as_bad (_("expression too complex"));
12017 offset_expr.X_op = O_constant;
12018 }
12019
12020 if (HAVE_32BIT_ADDRESSES
12021 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12022 {
12023 char value [32];
12024
12025 sprintf_vma (value, offset_expr.X_add_number);
12026 as_bad (_("number (0x%s) larger than 32 bits"), value);
12027 }
12028
12029 /* A constant expression in PIC code can be handled just as it
12030 is in non PIC code. */
12031 if (offset_expr.X_op == O_constant)
12032 {
12033 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12034 offbits == 0 ? 16 : offbits);
12035 offset_expr.X_add_number -= expr1.X_add_number;
12036
12037 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12038 if (breg != 0)
12039 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12040 tempreg, tempreg, breg);
12041 if (offbits == 0)
12042 {
12043 if (offset_expr.X_add_number != 0)
12044 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
12045 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
12046 macro_build (NULL, s, fmt, op[0], tempreg);
12047 }
12048 else if (offbits == 16)
12049 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12050 else
12051 macro_build (NULL, s, fmt, op[0],
12052 (int) offset_expr.X_add_number, tempreg);
12053 }
12054 else if (offbits != 16)
12055 {
12056 /* The offset field is too narrow to be used for a low-part
12057 relocation, so load the whole address into the auxiliary
12058 register. */
12059 load_address (tempreg, &offset_expr, &used_at);
12060 if (breg != 0)
12061 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12062 tempreg, tempreg, breg);
12063 if (offbits == 0)
12064 macro_build (NULL, s, fmt, op[0], tempreg);
12065 else
12066 macro_build (NULL, s, fmt, op[0], 0, tempreg);
12067 }
12068 else if (mips_pic == NO_PIC)
12069 {
12070 /* If this is a reference to a GP relative symbol, and there
12071 is no base register, we want
12072 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12073 Otherwise, if there is no base register, we want
12074 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12075 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12076 If we have a constant, we need two instructions anyhow,
12077 so we always use the latter form.
12078
12079 If we have a base register, and this is a reference to a
12080 GP relative symbol, we want
12081 addu $tempreg,$breg,$gp
12082 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
12083 Otherwise we want
12084 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12085 addu $tempreg,$tempreg,$breg
12086 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12087 With a constant we always use the latter case.
12088
12089 With 64bit address space and no base register and $at usable,
12090 we want
12091 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12092 lui $at,<sym> (BFD_RELOC_HI16_S)
12093 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12094 dsll32 $tempreg,0
12095 daddu $tempreg,$at
12096 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12097 If we have a base register, we want
12098 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12099 lui $at,<sym> (BFD_RELOC_HI16_S)
12100 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12101 daddu $at,$breg
12102 dsll32 $tempreg,0
12103 daddu $tempreg,$at
12104 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12105
12106 Without $at we can't generate the optimal path for superscalar
12107 processors here since this would require two temporary registers.
12108 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12109 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12110 dsll $tempreg,16
12111 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12112 dsll $tempreg,16
12113 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12114 If we have a base register, we want
12115 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12116 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12117 dsll $tempreg,16
12118 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12119 dsll $tempreg,16
12120 daddu $tempreg,$tempreg,$breg
12121 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12122
12123 For GP relative symbols in 64bit address space we can use
12124 the same sequence as in 32bit address space. */
12125 if (HAVE_64BIT_SYMBOLS)
12126 {
12127 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12128 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12129 {
12130 relax_start (offset_expr.X_add_symbol);
12131 if (breg == 0)
12132 {
12133 macro_build (&offset_expr, s, fmt, op[0],
12134 BFD_RELOC_GPREL16, mips_gp_register);
12135 }
12136 else
12137 {
12138 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12139 tempreg, breg, mips_gp_register);
12140 macro_build (&offset_expr, s, fmt, op[0],
12141 BFD_RELOC_GPREL16, tempreg);
12142 }
12143 relax_switch ();
12144 }
12145
12146 if (used_at == 0 && mips_opts.at)
12147 {
12148 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12149 BFD_RELOC_MIPS_HIGHEST);
12150 macro_build (&offset_expr, "lui", LUI_FMT, AT,
12151 BFD_RELOC_HI16_S);
12152 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12153 tempreg, BFD_RELOC_MIPS_HIGHER);
12154 if (breg != 0)
12155 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
12156 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
12157 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
12158 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
12159 tempreg);
12160 used_at = 1;
12161 }
12162 else
12163 {
12164 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12165 BFD_RELOC_MIPS_HIGHEST);
12166 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12167 tempreg, BFD_RELOC_MIPS_HIGHER);
12168 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12169 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12170 tempreg, BFD_RELOC_HI16_S);
12171 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12172 if (breg != 0)
12173 macro_build (NULL, "daddu", "d,v,t",
12174 tempreg, tempreg, breg);
12175 macro_build (&offset_expr, s, fmt, op[0],
12176 BFD_RELOC_LO16, tempreg);
12177 }
12178
12179 if (mips_relax.sequence)
12180 relax_end ();
12181 break;
12182 }
12183
12184 if (breg == 0)
12185 {
12186 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12187 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12188 {
12189 relax_start (offset_expr.X_add_symbol);
12190 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
12191 mips_gp_register);
12192 relax_switch ();
12193 }
12194 macro_build_lui (&offset_expr, tempreg);
12195 macro_build (&offset_expr, s, fmt, op[0],
12196 BFD_RELOC_LO16, tempreg);
12197 if (mips_relax.sequence)
12198 relax_end ();
12199 }
12200 else
12201 {
12202 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12203 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12204 {
12205 relax_start (offset_expr.X_add_symbol);
12206 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12207 tempreg, breg, mips_gp_register);
12208 macro_build (&offset_expr, s, fmt, op[0],
12209 BFD_RELOC_GPREL16, tempreg);
12210 relax_switch ();
12211 }
12212 macro_build_lui (&offset_expr, tempreg);
12213 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12214 tempreg, tempreg, breg);
12215 macro_build (&offset_expr, s, fmt, op[0],
12216 BFD_RELOC_LO16, tempreg);
12217 if (mips_relax.sequence)
12218 relax_end ();
12219 }
12220 }
12221 else if (!mips_big_got)
12222 {
12223 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
12224
12225 /* If this is a reference to an external symbol, we want
12226 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12227 nop
12228 <op> op[0],0($tempreg)
12229 Otherwise we want
12230 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12231 nop
12232 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12233 <op> op[0],0($tempreg)
12234
12235 For NewABI, we want
12236 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12237 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
12238
12239 If there is a base register, we add it to $tempreg before
12240 the <op>. If there is a constant, we stick it in the
12241 <op> instruction. We don't handle constants larger than
12242 16 bits, because we have no way to load the upper 16 bits
12243 (actually, we could handle them for the subset of cases
12244 in which we are not using $at). */
12245 gas_assert (offset_expr.X_op == O_symbol);
12246 if (HAVE_NEWABI)
12247 {
12248 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12249 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12250 if (breg != 0)
12251 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12252 tempreg, tempreg, breg);
12253 macro_build (&offset_expr, s, fmt, op[0],
12254 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12255 break;
12256 }
12257 expr1.X_add_number = offset_expr.X_add_number;
12258 offset_expr.X_add_number = 0;
12259 if (expr1.X_add_number < -0x8000
12260 || expr1.X_add_number >= 0x8000)
12261 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12262 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12263 lw_reloc_type, mips_gp_register);
12264 load_delay_nop ();
12265 relax_start (offset_expr.X_add_symbol);
12266 relax_switch ();
12267 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12268 tempreg, BFD_RELOC_LO16);
12269 relax_end ();
12270 if (breg != 0)
12271 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12272 tempreg, tempreg, breg);
12273 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12274 }
12275 else if (mips_big_got && !HAVE_NEWABI)
12276 {
12277 int gpdelay;
12278
12279 /* If this is a reference to an external symbol, we want
12280 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12281 addu $tempreg,$tempreg,$gp
12282 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12283 <op> op[0],0($tempreg)
12284 Otherwise we want
12285 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12286 nop
12287 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12288 <op> op[0],0($tempreg)
12289 If there is a base register, we add it to $tempreg before
12290 the <op>. If there is a constant, we stick it in the
12291 <op> instruction. We don't handle constants larger than
12292 16 bits, because we have no way to load the upper 16 bits
12293 (actually, we could handle them for the subset of cases
12294 in which we are not using $at). */
12295 gas_assert (offset_expr.X_op == O_symbol);
12296 expr1.X_add_number = offset_expr.X_add_number;
12297 offset_expr.X_add_number = 0;
12298 if (expr1.X_add_number < -0x8000
12299 || expr1.X_add_number >= 0x8000)
12300 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12301 gpdelay = reg_needs_delay (mips_gp_register);
12302 relax_start (offset_expr.X_add_symbol);
12303 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12304 BFD_RELOC_MIPS_GOT_HI16);
12305 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12306 mips_gp_register);
12307 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12308 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12309 relax_switch ();
12310 if (gpdelay)
12311 macro_build (NULL, "nop", "");
12312 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12313 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12314 load_delay_nop ();
12315 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12316 tempreg, BFD_RELOC_LO16);
12317 relax_end ();
12318
12319 if (breg != 0)
12320 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12321 tempreg, tempreg, breg);
12322 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12323 }
12324 else if (mips_big_got && HAVE_NEWABI)
12325 {
12326 /* If this is a reference to an external symbol, we want
12327 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12328 add $tempreg,$tempreg,$gp
12329 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12330 <op> op[0],<ofst>($tempreg)
12331 Otherwise, for local symbols, we want:
12332 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12333 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
12334 gas_assert (offset_expr.X_op == O_symbol);
12335 expr1.X_add_number = offset_expr.X_add_number;
12336 offset_expr.X_add_number = 0;
12337 if (expr1.X_add_number < -0x8000
12338 || expr1.X_add_number >= 0x8000)
12339 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12340 relax_start (offset_expr.X_add_symbol);
12341 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12342 BFD_RELOC_MIPS_GOT_HI16);
12343 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12344 mips_gp_register);
12345 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12346 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12347 if (breg != 0)
12348 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12349 tempreg, tempreg, breg);
12350 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12351
12352 relax_switch ();
12353 offset_expr.X_add_number = expr1.X_add_number;
12354 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12355 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12356 if (breg != 0)
12357 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12358 tempreg, tempreg, breg);
12359 macro_build (&offset_expr, s, fmt, op[0],
12360 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12361 relax_end ();
12362 }
12363 else
12364 abort ();
12365
12366 break;
12367
12368 case M_JRADDIUSP:
12369 gas_assert (mips_opts.micromips);
12370 gas_assert (mips_opts.insn32);
12371 start_noreorder ();
12372 macro_build (NULL, "jr", "s", RA);
12373 expr1.X_add_number = op[0] << 2;
12374 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12375 end_noreorder ();
12376 break;
12377
12378 case M_JRC:
12379 gas_assert (mips_opts.micromips);
12380 gas_assert (mips_opts.insn32);
12381 macro_build (NULL, "jr", "s", op[0]);
12382 if (mips_opts.noreorder)
12383 macro_build (NULL, "nop", "");
12384 break;
12385
12386 case M_LI:
12387 case M_LI_S:
12388 load_register (op[0], &imm_expr, 0);
12389 break;
12390
12391 case M_DLI:
12392 load_register (op[0], &imm_expr, 1);
12393 break;
12394
12395 case M_LI_SS:
12396 if (imm_expr.X_op == O_constant)
12397 {
12398 used_at = 1;
12399 load_register (AT, &imm_expr, 0);
12400 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12401 break;
12402 }
12403 else
12404 {
12405 gas_assert (imm_expr.X_op == O_absent
12406 && offset_expr.X_op == O_symbol
12407 && strcmp (segment_name (S_GET_SEGMENT
12408 (offset_expr.X_add_symbol)),
12409 ".lit4") == 0
12410 && offset_expr.X_add_number == 0);
12411 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
12412 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
12413 break;
12414 }
12415
12416 case M_LI_D:
12417 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12418 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12419 order 32 bits of the value and the low order 32 bits are either
12420 zero or in OFFSET_EXPR. */
12421 if (imm_expr.X_op == O_constant)
12422 {
12423 if (GPR_SIZE == 64)
12424 load_register (op[0], &imm_expr, 1);
12425 else
12426 {
12427 int hreg, lreg;
12428
12429 if (target_big_endian)
12430 {
12431 hreg = op[0];
12432 lreg = op[0] + 1;
12433 }
12434 else
12435 {
12436 hreg = op[0] + 1;
12437 lreg = op[0];
12438 }
12439
12440 if (hreg <= 31)
12441 load_register (hreg, &imm_expr, 0);
12442 if (lreg <= 31)
12443 {
12444 if (offset_expr.X_op == O_absent)
12445 move_register (lreg, 0);
12446 else
12447 {
12448 gas_assert (offset_expr.X_op == O_constant);
12449 load_register (lreg, &offset_expr, 0);
12450 }
12451 }
12452 }
12453 break;
12454 }
12455 gas_assert (imm_expr.X_op == O_absent);
12456
12457 /* We know that sym is in the .rdata section. First we get the
12458 upper 16 bits of the address. */
12459 if (mips_pic == NO_PIC)
12460 {
12461 macro_build_lui (&offset_expr, AT);
12462 used_at = 1;
12463 }
12464 else
12465 {
12466 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12467 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12468 used_at = 1;
12469 }
12470
12471 /* Now we load the register(s). */
12472 if (GPR_SIZE == 64)
12473 {
12474 used_at = 1;
12475 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12476 BFD_RELOC_LO16, AT);
12477 }
12478 else
12479 {
12480 used_at = 1;
12481 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12482 BFD_RELOC_LO16, AT);
12483 if (op[0] != RA)
12484 {
12485 /* FIXME: How in the world do we deal with the possible
12486 overflow here? */
12487 offset_expr.X_add_number += 4;
12488 macro_build (&offset_expr, "lw", "t,o(b)",
12489 op[0] + 1, BFD_RELOC_LO16, AT);
12490 }
12491 }
12492 break;
12493
12494 case M_LI_DD:
12495 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12496 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12497 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12498 the value and the low order 32 bits are either zero or in
12499 OFFSET_EXPR. */
12500 if (imm_expr.X_op == O_constant)
12501 {
12502 used_at = 1;
12503 load_register (AT, &imm_expr, FPR_SIZE == 64);
12504 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12505 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
12506 else
12507 {
12508 if (ISA_HAS_MXHC1 (mips_opts.isa))
12509 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12510 else if (FPR_SIZE != 32)
12511 as_bad (_("Unable to generate `%s' compliant code "
12512 "without mthc1"),
12513 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12514 else
12515 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
12516 if (offset_expr.X_op == O_absent)
12517 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
12518 else
12519 {
12520 gas_assert (offset_expr.X_op == O_constant);
12521 load_register (AT, &offset_expr, 0);
12522 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12523 }
12524 }
12525 break;
12526 }
12527
12528 gas_assert (imm_expr.X_op == O_absent
12529 && offset_expr.X_op == O_symbol
12530 && offset_expr.X_add_number == 0);
12531 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12532 if (strcmp (s, ".lit8") == 0)
12533 {
12534 op[2] = mips_gp_register;
12535 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12536 offset_reloc[1] = BFD_RELOC_UNUSED;
12537 offset_reloc[2] = BFD_RELOC_UNUSED;
12538 }
12539 else
12540 {
12541 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
12542 used_at = 1;
12543 if (mips_pic != NO_PIC)
12544 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12545 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12546 else
12547 {
12548 /* FIXME: This won't work for a 64 bit address. */
12549 macro_build_lui (&offset_expr, AT);
12550 }
12551
12552 op[2] = AT;
12553 offset_reloc[0] = BFD_RELOC_LO16;
12554 offset_reloc[1] = BFD_RELOC_UNUSED;
12555 offset_reloc[2] = BFD_RELOC_UNUSED;
12556 }
12557 align = 8;
12558 /* Fall through */
12559
12560 case M_L_DAB:
12561 /*
12562 * The MIPS assembler seems to check for X_add_number not
12563 * being double aligned and generating:
12564 * lui at,%hi(foo+1)
12565 * addu at,at,v1
12566 * addiu at,at,%lo(foo+1)
12567 * lwc1 f2,0(at)
12568 * lwc1 f3,4(at)
12569 * But, the resulting address is the same after relocation so why
12570 * generate the extra instruction?
12571 */
12572 /* Itbl support may require additional care here. */
12573 coproc = 1;
12574 fmt = "T,o(b)";
12575 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12576 {
12577 s = "ldc1";
12578 goto ld_st;
12579 }
12580 s = "lwc1";
12581 goto ldd_std;
12582
12583 case M_S_DAB:
12584 gas_assert (!mips_opts.micromips);
12585 /* Itbl support may require additional care here. */
12586 coproc = 1;
12587 fmt = "T,o(b)";
12588 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12589 {
12590 s = "sdc1";
12591 goto ld_st;
12592 }
12593 s = "swc1";
12594 goto ldd_std;
12595
12596 case M_LQ_AB:
12597 fmt = "t,o(b)";
12598 s = "lq";
12599 goto ld;
12600
12601 case M_SQ_AB:
12602 fmt = "t,o(b)";
12603 s = "sq";
12604 goto ld_st;
12605
12606 case M_LD_AB:
12607 fmt = "t,o(b)";
12608 if (GPR_SIZE == 64)
12609 {
12610 s = "ld";
12611 goto ld;
12612 }
12613 s = "lw";
12614 goto ldd_std;
12615
12616 case M_SD_AB:
12617 fmt = "t,o(b)";
12618 if (GPR_SIZE == 64)
12619 {
12620 s = "sd";
12621 goto ld_st;
12622 }
12623 s = "sw";
12624
12625 ldd_std:
12626 /* Even on a big endian machine $fn comes before $fn+1. We have
12627 to adjust when loading from memory. We set coproc if we must
12628 load $fn+1 first. */
12629 /* Itbl support may require additional care here. */
12630 if (!target_big_endian)
12631 coproc = 0;
12632
12633 breg = op[2];
12634 if (small_offset_p (0, align, 16))
12635 {
12636 ep = &offset_expr;
12637 if (!small_offset_p (4, align, 16))
12638 {
12639 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12640 -1, offset_reloc[0], offset_reloc[1],
12641 offset_reloc[2]);
12642 expr1.X_add_number = 0;
12643 ep = &expr1;
12644 breg = AT;
12645 used_at = 1;
12646 offset_reloc[0] = BFD_RELOC_LO16;
12647 offset_reloc[1] = BFD_RELOC_UNUSED;
12648 offset_reloc[2] = BFD_RELOC_UNUSED;
12649 }
12650 if (strcmp (s, "lw") == 0 && op[0] == breg)
12651 {
12652 ep->X_add_number += 4;
12653 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
12654 offset_reloc[1], offset_reloc[2], breg);
12655 ep->X_add_number -= 4;
12656 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
12657 offset_reloc[1], offset_reloc[2], breg);
12658 }
12659 else
12660 {
12661 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
12662 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12663 breg);
12664 ep->X_add_number += 4;
12665 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
12666 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12667 breg);
12668 }
12669 break;
12670 }
12671
12672 if (offset_expr.X_op != O_symbol
12673 && offset_expr.X_op != O_constant)
12674 {
12675 as_bad (_("expression too complex"));
12676 offset_expr.X_op = O_constant;
12677 }
12678
12679 if (HAVE_32BIT_ADDRESSES
12680 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12681 {
12682 char value [32];
12683
12684 sprintf_vma (value, offset_expr.X_add_number);
12685 as_bad (_("number (0x%s) larger than 32 bits"), value);
12686 }
12687
12688 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
12689 {
12690 /* If this is a reference to a GP relative symbol, we want
12691 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12692 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
12693 If we have a base register, we use this
12694 addu $at,$breg,$gp
12695 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12696 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
12697 If this is not a GP relative symbol, we want
12698 lui $at,<sym> (BFD_RELOC_HI16_S)
12699 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12700 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12701 If there is a base register, we add it to $at after the
12702 lui instruction. If there is a constant, we always use
12703 the last case. */
12704 if (offset_expr.X_op == O_symbol
12705 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12706 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12707 {
12708 relax_start (offset_expr.X_add_symbol);
12709 if (breg == 0)
12710 {
12711 tempreg = mips_gp_register;
12712 }
12713 else
12714 {
12715 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12716 AT, breg, mips_gp_register);
12717 tempreg = AT;
12718 used_at = 1;
12719 }
12720
12721 /* Itbl support may require additional care here. */
12722 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12723 BFD_RELOC_GPREL16, tempreg);
12724 offset_expr.X_add_number += 4;
12725
12726 /* Set mips_optimize to 2 to avoid inserting an
12727 undesired nop. */
12728 hold_mips_optimize = mips_optimize;
12729 mips_optimize = 2;
12730 /* Itbl support may require additional care here. */
12731 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12732 BFD_RELOC_GPREL16, tempreg);
12733 mips_optimize = hold_mips_optimize;
12734
12735 relax_switch ();
12736
12737 offset_expr.X_add_number -= 4;
12738 }
12739 used_at = 1;
12740 if (offset_high_part (offset_expr.X_add_number, 16)
12741 != offset_high_part (offset_expr.X_add_number + 4, 16))
12742 {
12743 load_address (AT, &offset_expr, &used_at);
12744 offset_expr.X_op = O_constant;
12745 offset_expr.X_add_number = 0;
12746 }
12747 else
12748 macro_build_lui (&offset_expr, AT);
12749 if (breg != 0)
12750 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12751 /* Itbl support may require additional care here. */
12752 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12753 BFD_RELOC_LO16, AT);
12754 /* FIXME: How do we handle overflow here? */
12755 offset_expr.X_add_number += 4;
12756 /* Itbl support may require additional care here. */
12757 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12758 BFD_RELOC_LO16, AT);
12759 if (mips_relax.sequence)
12760 relax_end ();
12761 }
12762 else if (!mips_big_got)
12763 {
12764 /* If this is a reference to an external symbol, we want
12765 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12766 nop
12767 <op> op[0],0($at)
12768 <op> op[0]+1,4($at)
12769 Otherwise we want
12770 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12771 nop
12772 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12773 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12774 If there is a base register we add it to $at before the
12775 lwc1 instructions. If there is a constant we include it
12776 in the lwc1 instructions. */
12777 used_at = 1;
12778 expr1.X_add_number = offset_expr.X_add_number;
12779 if (expr1.X_add_number < -0x8000
12780 || expr1.X_add_number >= 0x8000 - 4)
12781 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12782 load_got_offset (AT, &offset_expr);
12783 load_delay_nop ();
12784 if (breg != 0)
12785 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12786
12787 /* Set mips_optimize to 2 to avoid inserting an undesired
12788 nop. */
12789 hold_mips_optimize = mips_optimize;
12790 mips_optimize = 2;
12791
12792 /* Itbl support may require additional care here. */
12793 relax_start (offset_expr.X_add_symbol);
12794 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12795 BFD_RELOC_LO16, AT);
12796 expr1.X_add_number += 4;
12797 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12798 BFD_RELOC_LO16, AT);
12799 relax_switch ();
12800 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12801 BFD_RELOC_LO16, AT);
12802 offset_expr.X_add_number += 4;
12803 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12804 BFD_RELOC_LO16, AT);
12805 relax_end ();
12806
12807 mips_optimize = hold_mips_optimize;
12808 }
12809 else if (mips_big_got)
12810 {
12811 int gpdelay;
12812
12813 /* If this is a reference to an external symbol, we want
12814 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12815 addu $at,$at,$gp
12816 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12817 nop
12818 <op> op[0],0($at)
12819 <op> op[0]+1,4($at)
12820 Otherwise we want
12821 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12822 nop
12823 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12824 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12825 If there is a base register we add it to $at before the
12826 lwc1 instructions. If there is a constant we include it
12827 in the lwc1 instructions. */
12828 used_at = 1;
12829 expr1.X_add_number = offset_expr.X_add_number;
12830 offset_expr.X_add_number = 0;
12831 if (expr1.X_add_number < -0x8000
12832 || expr1.X_add_number >= 0x8000 - 4)
12833 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12834 gpdelay = reg_needs_delay (mips_gp_register);
12835 relax_start (offset_expr.X_add_symbol);
12836 macro_build (&offset_expr, "lui", LUI_FMT,
12837 AT, BFD_RELOC_MIPS_GOT_HI16);
12838 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12839 AT, AT, mips_gp_register);
12840 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
12841 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
12842 load_delay_nop ();
12843 if (breg != 0)
12844 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12845 /* Itbl support may require additional care here. */
12846 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12847 BFD_RELOC_LO16, AT);
12848 expr1.X_add_number += 4;
12849
12850 /* Set mips_optimize to 2 to avoid inserting an undesired
12851 nop. */
12852 hold_mips_optimize = mips_optimize;
12853 mips_optimize = 2;
12854 /* Itbl support may require additional care here. */
12855 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12856 BFD_RELOC_LO16, AT);
12857 mips_optimize = hold_mips_optimize;
12858 expr1.X_add_number -= 4;
12859
12860 relax_switch ();
12861 offset_expr.X_add_number = expr1.X_add_number;
12862 if (gpdelay)
12863 macro_build (NULL, "nop", "");
12864 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12865 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12866 load_delay_nop ();
12867 if (breg != 0)
12868 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12869 /* Itbl support may require additional care here. */
12870 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12871 BFD_RELOC_LO16, AT);
12872 offset_expr.X_add_number += 4;
12873
12874 /* Set mips_optimize to 2 to avoid inserting an undesired
12875 nop. */
12876 hold_mips_optimize = mips_optimize;
12877 mips_optimize = 2;
12878 /* Itbl support may require additional care here. */
12879 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12880 BFD_RELOC_LO16, AT);
12881 mips_optimize = hold_mips_optimize;
12882 relax_end ();
12883 }
12884 else
12885 abort ();
12886
12887 break;
12888
12889 case M_SAA_AB:
12890 s = "saa";
12891 goto saa_saad;
12892 case M_SAAD_AB:
12893 s = "saad";
12894 saa_saad:
12895 gas_assert (!mips_opts.micromips);
12896 offbits = 0;
12897 fmt = "t,(b)";
12898 goto ld_st;
12899
12900 /* New code added to support COPZ instructions.
12901 This code builds table entries out of the macros in mip_opcodes.
12902 R4000 uses interlocks to handle coproc delays.
12903 Other chips (like the R3000) require nops to be inserted for delays.
12904
12905 FIXME: Currently, we require that the user handle delays.
12906 In order to fill delay slots for non-interlocked chips,
12907 we must have a way to specify delays based on the coprocessor.
12908 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12909 What are the side-effects of the cop instruction?
12910 What cache support might we have and what are its effects?
12911 Both coprocessor & memory require delays. how long???
12912 What registers are read/set/modified?
12913
12914 If an itbl is provided to interpret cop instructions,
12915 this knowledge can be encoded in the itbl spec. */
12916
12917 case M_COP0:
12918 s = "c0";
12919 goto copz;
12920 case M_COP1:
12921 s = "c1";
12922 goto copz;
12923 case M_COP2:
12924 s = "c2";
12925 goto copz;
12926 case M_COP3:
12927 s = "c3";
12928 copz:
12929 gas_assert (!mips_opts.micromips);
12930 /* For now we just do C (same as Cz). The parameter will be
12931 stored in insn_opcode by mips_ip. */
12932 macro_build (NULL, s, "C", (int) ip->insn_opcode);
12933 break;
12934
12935 case M_MOVE:
12936 move_register (op[0], op[1]);
12937 break;
12938
12939 case M_MOVEP:
12940 gas_assert (mips_opts.micromips);
12941 gas_assert (mips_opts.insn32);
12942 move_register (micromips_to_32_reg_h_map1[op[0]],
12943 micromips_to_32_reg_m_map[op[1]]);
12944 move_register (micromips_to_32_reg_h_map2[op[0]],
12945 micromips_to_32_reg_n_map[op[2]]);
12946 break;
12947
12948 case M_DMUL:
12949 dbl = 1;
12950 /* Fall through. */
12951 case M_MUL:
12952 if (mips_opts.arch == CPU_R5900)
12953 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12954 op[2]);
12955 else
12956 {
12957 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12958 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12959 }
12960 break;
12961
12962 case M_DMUL_I:
12963 dbl = 1;
12964 /* Fall through. */
12965 case M_MUL_I:
12966 /* The MIPS assembler some times generates shifts and adds. I'm
12967 not trying to be that fancy. GCC should do this for us
12968 anyway. */
12969 used_at = 1;
12970 load_register (AT, &imm_expr, dbl);
12971 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
12972 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12973 break;
12974
12975 case M_DMULO_I:
12976 dbl = 1;
12977 /* Fall through. */
12978 case M_MULO_I:
12979 imm = 1;
12980 goto do_mulo;
12981
12982 case M_DMULO:
12983 dbl = 1;
12984 /* Fall through. */
12985 case M_MULO:
12986 do_mulo:
12987 start_noreorder ();
12988 used_at = 1;
12989 if (imm)
12990 load_register (AT, &imm_expr, dbl);
12991 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
12992 op[1], imm ? AT : op[2]);
12993 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12994 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
12995 macro_build (NULL, "mfhi", MFHL_FMT, AT);
12996 if (mips_trap)
12997 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
12998 else
12999 {
13000 if (mips_opts.micromips)
13001 micromips_label_expr (&label_expr);
13002 else
13003 label_expr.X_add_number = 8;
13004 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
13005 macro_build (NULL, "nop", "");
13006 macro_build (NULL, "break", BRK_FMT, 6);
13007 if (mips_opts.micromips)
13008 micromips_add_label ();
13009 }
13010 end_noreorder ();
13011 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13012 break;
13013
13014 case M_DMULOU_I:
13015 dbl = 1;
13016 /* Fall through. */
13017 case M_MULOU_I:
13018 imm = 1;
13019 goto do_mulou;
13020
13021 case M_DMULOU:
13022 dbl = 1;
13023 /* Fall through. */
13024 case M_MULOU:
13025 do_mulou:
13026 start_noreorder ();
13027 used_at = 1;
13028 if (imm)
13029 load_register (AT, &imm_expr, dbl);
13030 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
13031 op[1], imm ? AT : op[2]);
13032 macro_build (NULL, "mfhi", MFHL_FMT, AT);
13033 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13034 if (mips_trap)
13035 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
13036 else
13037 {
13038 if (mips_opts.micromips)
13039 micromips_label_expr (&label_expr);
13040 else
13041 label_expr.X_add_number = 8;
13042 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
13043 macro_build (NULL, "nop", "");
13044 macro_build (NULL, "break", BRK_FMT, 6);
13045 if (mips_opts.micromips)
13046 micromips_add_label ();
13047 }
13048 end_noreorder ();
13049 break;
13050
13051 case M_DROL:
13052 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13053 {
13054 if (op[0] == op[1])
13055 {
13056 tempreg = AT;
13057 used_at = 1;
13058 }
13059 else
13060 tempreg = op[0];
13061 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13062 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
13063 break;
13064 }
13065 used_at = 1;
13066 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13067 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13068 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13069 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13070 break;
13071
13072 case M_ROL:
13073 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13074 {
13075 if (op[0] == op[1])
13076 {
13077 tempreg = AT;
13078 used_at = 1;
13079 }
13080 else
13081 tempreg = op[0];
13082 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13083 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
13084 break;
13085 }
13086 used_at = 1;
13087 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13088 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13089 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13090 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13091 break;
13092
13093 case M_DROL_I:
13094 {
13095 unsigned int rot;
13096 const char *l;
13097 const char *rr;
13098
13099 rot = imm_expr.X_add_number & 0x3f;
13100 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13101 {
13102 rot = (64 - rot) & 0x3f;
13103 if (rot >= 32)
13104 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13105 else
13106 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13107 break;
13108 }
13109 if (rot == 0)
13110 {
13111 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13112 break;
13113 }
13114 l = (rot < 0x20) ? "dsll" : "dsll32";
13115 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
13116 rot &= 0x1f;
13117 used_at = 1;
13118 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13119 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13120 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13121 }
13122 break;
13123
13124 case M_ROL_I:
13125 {
13126 unsigned int rot;
13127
13128 rot = imm_expr.X_add_number & 0x1f;
13129 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13130 {
13131 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13132 (32 - rot) & 0x1f);
13133 break;
13134 }
13135 if (rot == 0)
13136 {
13137 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13138 break;
13139 }
13140 used_at = 1;
13141 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13142 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13143 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13144 }
13145 break;
13146
13147 case M_DROR:
13148 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13149 {
13150 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
13151 break;
13152 }
13153 used_at = 1;
13154 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13155 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13156 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13157 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13158 break;
13159
13160 case M_ROR:
13161 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13162 {
13163 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
13164 break;
13165 }
13166 used_at = 1;
13167 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13168 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13169 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13170 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13171 break;
13172
13173 case M_DROR_I:
13174 {
13175 unsigned int rot;
13176 const char *l;
13177 const char *rr;
13178
13179 rot = imm_expr.X_add_number & 0x3f;
13180 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13181 {
13182 if (rot >= 32)
13183 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13184 else
13185 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13186 break;
13187 }
13188 if (rot == 0)
13189 {
13190 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13191 break;
13192 }
13193 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
13194 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13195 rot &= 0x1f;
13196 used_at = 1;
13197 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13198 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13199 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13200 }
13201 break;
13202
13203 case M_ROR_I:
13204 {
13205 unsigned int rot;
13206
13207 rot = imm_expr.X_add_number & 0x1f;
13208 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13209 {
13210 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
13211 break;
13212 }
13213 if (rot == 0)
13214 {
13215 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13216 break;
13217 }
13218 used_at = 1;
13219 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13220 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13221 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13222 }
13223 break;
13224
13225 case M_SEQ:
13226 if (op[1] == 0)
13227 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13228 else if (op[2] == 0)
13229 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13230 else
13231 {
13232 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13233 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13234 }
13235 break;
13236
13237 case M_SEQ_I:
13238 if (imm_expr.X_add_number == 0)
13239 {
13240 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13241 break;
13242 }
13243 if (op[1] == 0)
13244 {
13245 as_warn (_("instruction %s: result is always false"),
13246 ip->insn_mo->name);
13247 move_register (op[0], 0);
13248 break;
13249 }
13250 if (CPU_HAS_SEQ (mips_opts.arch)
13251 && -512 <= imm_expr.X_add_number
13252 && imm_expr.X_add_number < 512)
13253 {
13254 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
13255 (int) imm_expr.X_add_number);
13256 break;
13257 }
13258 if (imm_expr.X_add_number >= 0
13259 && imm_expr.X_add_number < 0x10000)
13260 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
13261 else if (imm_expr.X_add_number > -0x8000
13262 && imm_expr.X_add_number < 0)
13263 {
13264 imm_expr.X_add_number = -imm_expr.X_add_number;
13265 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13266 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13267 }
13268 else if (CPU_HAS_SEQ (mips_opts.arch))
13269 {
13270 used_at = 1;
13271 load_register (AT, &imm_expr, GPR_SIZE == 64);
13272 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
13273 break;
13274 }
13275 else
13276 {
13277 load_register (AT, &imm_expr, GPR_SIZE == 64);
13278 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13279 used_at = 1;
13280 }
13281 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13282 break;
13283
13284 case M_SGE: /* X >= Y <==> not (X < Y) */
13285 s = "slt";
13286 goto sge;
13287 case M_SGEU:
13288 s = "sltu";
13289 sge:
13290 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13291 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13292 break;
13293
13294 case M_SGE_I: /* X >= I <==> not (X < I) */
13295 case M_SGEU_I:
13296 if (imm_expr.X_add_number >= -0x8000
13297 && imm_expr.X_add_number < 0x8000)
13298 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13299 op[0], op[1], BFD_RELOC_LO16);
13300 else
13301 {
13302 load_register (AT, &imm_expr, GPR_SIZE == 64);
13303 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
13304 op[0], op[1], AT);
13305 used_at = 1;
13306 }
13307 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13308 break;
13309
13310 case M_SGT: /* X > Y <==> Y < X */
13311 s = "slt";
13312 goto sgt;
13313 case M_SGTU:
13314 s = "sltu";
13315 sgt:
13316 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13317 break;
13318
13319 case M_SGT_I: /* X > I <==> I < X */
13320 s = "slt";
13321 goto sgti;
13322 case M_SGTU_I:
13323 s = "sltu";
13324 sgti:
13325 used_at = 1;
13326 load_register (AT, &imm_expr, GPR_SIZE == 64);
13327 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13328 break;
13329
13330 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
13331 s = "slt";
13332 goto sle;
13333 case M_SLEU:
13334 s = "sltu";
13335 sle:
13336 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13337 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13338 break;
13339
13340 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
13341 s = "slt";
13342 goto slei;
13343 case M_SLEU_I:
13344 s = "sltu";
13345 slei:
13346 used_at = 1;
13347 load_register (AT, &imm_expr, GPR_SIZE == 64);
13348 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13349 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13350 break;
13351
13352 case M_SLT_I:
13353 if (imm_expr.X_add_number >= -0x8000
13354 && imm_expr.X_add_number < 0x8000)
13355 {
13356 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13357 BFD_RELOC_LO16);
13358 break;
13359 }
13360 used_at = 1;
13361 load_register (AT, &imm_expr, GPR_SIZE == 64);
13362 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
13363 break;
13364
13365 case M_SLTU_I:
13366 if (imm_expr.X_add_number >= -0x8000
13367 && imm_expr.X_add_number < 0x8000)
13368 {
13369 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
13370 BFD_RELOC_LO16);
13371 break;
13372 }
13373 used_at = 1;
13374 load_register (AT, &imm_expr, GPR_SIZE == 64);
13375 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
13376 break;
13377
13378 case M_SNE:
13379 if (op[1] == 0)
13380 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13381 else if (op[2] == 0)
13382 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13383 else
13384 {
13385 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13386 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13387 }
13388 break;
13389
13390 case M_SNE_I:
13391 if (imm_expr.X_add_number == 0)
13392 {
13393 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13394 break;
13395 }
13396 if (op[1] == 0)
13397 {
13398 as_warn (_("instruction %s: result is always true"),
13399 ip->insn_mo->name);
13400 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
13401 op[0], 0, BFD_RELOC_LO16);
13402 break;
13403 }
13404 if (CPU_HAS_SEQ (mips_opts.arch)
13405 && -512 <= imm_expr.X_add_number
13406 && imm_expr.X_add_number < 512)
13407 {
13408 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
13409 (int) imm_expr.X_add_number);
13410 break;
13411 }
13412 if (imm_expr.X_add_number >= 0
13413 && imm_expr.X_add_number < 0x10000)
13414 {
13415 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13416 BFD_RELOC_LO16);
13417 }
13418 else if (imm_expr.X_add_number > -0x8000
13419 && imm_expr.X_add_number < 0)
13420 {
13421 imm_expr.X_add_number = -imm_expr.X_add_number;
13422 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13423 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13424 }
13425 else if (CPU_HAS_SEQ (mips_opts.arch))
13426 {
13427 used_at = 1;
13428 load_register (AT, &imm_expr, GPR_SIZE == 64);
13429 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
13430 break;
13431 }
13432 else
13433 {
13434 load_register (AT, &imm_expr, GPR_SIZE == 64);
13435 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13436 used_at = 1;
13437 }
13438 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13439 break;
13440
13441 case M_SUB_I:
13442 s = "addi";
13443 s2 = "sub";
13444 goto do_subi;
13445 case M_SUBU_I:
13446 s = "addiu";
13447 s2 = "subu";
13448 goto do_subi;
13449 case M_DSUB_I:
13450 dbl = 1;
13451 s = "daddi";
13452 s2 = "dsub";
13453 if (!mips_opts.micromips)
13454 goto do_subi;
13455 if (imm_expr.X_add_number > -0x200
13456 && imm_expr.X_add_number <= 0x200)
13457 {
13458 macro_build (NULL, s, "t,r,.", op[0], op[1],
13459 (int) -imm_expr.X_add_number);
13460 break;
13461 }
13462 goto do_subi_i;
13463 case M_DSUBU_I:
13464 dbl = 1;
13465 s = "daddiu";
13466 s2 = "dsubu";
13467 do_subi:
13468 if (imm_expr.X_add_number > -0x8000
13469 && imm_expr.X_add_number <= 0x8000)
13470 {
13471 imm_expr.X_add_number = -imm_expr.X_add_number;
13472 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13473 break;
13474 }
13475 do_subi_i:
13476 used_at = 1;
13477 load_register (AT, &imm_expr, dbl);
13478 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
13479 break;
13480
13481 case M_TEQ_I:
13482 s = "teq";
13483 goto trap;
13484 case M_TGE_I:
13485 s = "tge";
13486 goto trap;
13487 case M_TGEU_I:
13488 s = "tgeu";
13489 goto trap;
13490 case M_TLT_I:
13491 s = "tlt";
13492 goto trap;
13493 case M_TLTU_I:
13494 s = "tltu";
13495 goto trap;
13496 case M_TNE_I:
13497 s = "tne";
13498 trap:
13499 used_at = 1;
13500 load_register (AT, &imm_expr, GPR_SIZE == 64);
13501 macro_build (NULL, s, "s,t", op[0], AT);
13502 break;
13503
13504 case M_TRUNCWS:
13505 case M_TRUNCWD:
13506 gas_assert (!mips_opts.micromips);
13507 gas_assert (mips_opts.isa == ISA_MIPS1);
13508 used_at = 1;
13509
13510 /*
13511 * Is the double cfc1 instruction a bug in the mips assembler;
13512 * or is there a reason for it?
13513 */
13514 start_noreorder ();
13515 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13516 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13517 macro_build (NULL, "nop", "");
13518 expr1.X_add_number = 3;
13519 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
13520 expr1.X_add_number = 2;
13521 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13522 macro_build (NULL, "ctc1", "t,G", AT, RA);
13523 macro_build (NULL, "nop", "");
13524 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
13525 op[0], op[1]);
13526 macro_build (NULL, "ctc1", "t,G", op[2], RA);
13527 macro_build (NULL, "nop", "");
13528 end_noreorder ();
13529 break;
13530
13531 case M_ULH_AB:
13532 s = "lb";
13533 s2 = "lbu";
13534 off = 1;
13535 goto uld_st;
13536 case M_ULHU_AB:
13537 s = "lbu";
13538 s2 = "lbu";
13539 off = 1;
13540 goto uld_st;
13541 case M_ULW_AB:
13542 s = "lwl";
13543 s2 = "lwr";
13544 offbits = (mips_opts.micromips ? 12 : 16);
13545 off = 3;
13546 goto uld_st;
13547 case M_ULD_AB:
13548 s = "ldl";
13549 s2 = "ldr";
13550 offbits = (mips_opts.micromips ? 12 : 16);
13551 off = 7;
13552 goto uld_st;
13553 case M_USH_AB:
13554 s = "sb";
13555 s2 = "sb";
13556 off = 1;
13557 ust = 1;
13558 goto uld_st;
13559 case M_USW_AB:
13560 s = "swl";
13561 s2 = "swr";
13562 offbits = (mips_opts.micromips ? 12 : 16);
13563 off = 3;
13564 ust = 1;
13565 goto uld_st;
13566 case M_USD_AB:
13567 s = "sdl";
13568 s2 = "sdr";
13569 offbits = (mips_opts.micromips ? 12 : 16);
13570 off = 7;
13571 ust = 1;
13572
13573 uld_st:
13574 breg = op[2];
13575 large_offset = !small_offset_p (off, align, offbits);
13576 ep = &offset_expr;
13577 expr1.X_add_number = 0;
13578 if (large_offset)
13579 {
13580 used_at = 1;
13581 tempreg = AT;
13582 if (small_offset_p (0, align, 16))
13583 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13584 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13585 else
13586 {
13587 load_address (tempreg, ep, &used_at);
13588 if (breg != 0)
13589 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13590 tempreg, tempreg, breg);
13591 }
13592 offset_reloc[0] = BFD_RELOC_LO16;
13593 offset_reloc[1] = BFD_RELOC_UNUSED;
13594 offset_reloc[2] = BFD_RELOC_UNUSED;
13595 breg = tempreg;
13596 tempreg = op[0];
13597 ep = &expr1;
13598 }
13599 else if (!ust && op[0] == breg)
13600 {
13601 used_at = 1;
13602 tempreg = AT;
13603 }
13604 else
13605 tempreg = op[0];
13606
13607 if (off == 1)
13608 goto ulh_sh;
13609
13610 if (!target_big_endian)
13611 ep->X_add_number += off;
13612 if (offbits == 12)
13613 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
13614 else
13615 macro_build (ep, s, "t,o(b)", tempreg, -1,
13616 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13617
13618 if (!target_big_endian)
13619 ep->X_add_number -= off;
13620 else
13621 ep->X_add_number += off;
13622 if (offbits == 12)
13623 macro_build (NULL, s2, "t,~(b)",
13624 tempreg, (int) ep->X_add_number, breg);
13625 else
13626 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13627 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13628
13629 /* If necessary, move the result in tempreg to the final destination. */
13630 if (!ust && op[0] != tempreg)
13631 {
13632 /* Protect second load's delay slot. */
13633 load_delay_nop ();
13634 move_register (op[0], tempreg);
13635 }
13636 break;
13637
13638 ulh_sh:
13639 used_at = 1;
13640 if (target_big_endian == ust)
13641 ep->X_add_number += off;
13642 tempreg = ust || large_offset ? op[0] : AT;
13643 macro_build (ep, s, "t,o(b)", tempreg, -1,
13644 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13645
13646 /* For halfword transfers we need a temporary register to shuffle
13647 bytes. Unfortunately for M_USH_A we have none available before
13648 the next store as AT holds the base address. We deal with this
13649 case by clobbering TREG and then restoring it as with ULH. */
13650 tempreg = ust == large_offset ? op[0] : AT;
13651 if (ust)
13652 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
13653
13654 if (target_big_endian == ust)
13655 ep->X_add_number -= off;
13656 else
13657 ep->X_add_number += off;
13658 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13659 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13660
13661 /* For M_USH_A re-retrieve the LSB. */
13662 if (ust && large_offset)
13663 {
13664 if (target_big_endian)
13665 ep->X_add_number += off;
13666 else
13667 ep->X_add_number -= off;
13668 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13669 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
13670 }
13671 /* For ULH and M_USH_A OR the LSB in. */
13672 if (!ust || large_offset)
13673 {
13674 tempreg = !large_offset ? AT : op[0];
13675 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
13676 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13677 }
13678 break;
13679
13680 default:
13681 /* FIXME: Check if this is one of the itbl macros, since they
13682 are added dynamically. */
13683 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
13684 break;
13685 }
13686 if (!mips_opts.at && used_at)
13687 as_bad (_("macro used $at after \".set noat\""));
13688 }
13689
13690 /* Implement macros in mips16 mode. */
13691
13692 static void
13693 mips16_macro (struct mips_cl_insn *ip)
13694 {
13695 const struct mips_operand_array *operands;
13696 int mask;
13697 int tmp;
13698 expressionS expr1;
13699 int dbl;
13700 const char *s, *s2, *s3;
13701 unsigned int op[MAX_OPERANDS];
13702 unsigned int i;
13703
13704 mask = ip->insn_mo->mask;
13705
13706 operands = insn_operands (ip);
13707 for (i = 0; i < MAX_OPERANDS; i++)
13708 if (operands->operand[i])
13709 op[i] = insn_extract_operand (ip, operands->operand[i]);
13710 else
13711 op[i] = -1;
13712
13713 expr1.X_op = O_constant;
13714 expr1.X_op_symbol = NULL;
13715 expr1.X_add_symbol = NULL;
13716 expr1.X_add_number = 1;
13717
13718 dbl = 0;
13719
13720 switch (mask)
13721 {
13722 default:
13723 abort ();
13724
13725 case M_DDIV_3:
13726 dbl = 1;
13727 /* Fall through. */
13728 case M_DIV_3:
13729 s = "mflo";
13730 goto do_div3;
13731 case M_DREM_3:
13732 dbl = 1;
13733 /* Fall through. */
13734 case M_REM_3:
13735 s = "mfhi";
13736 do_div3:
13737 start_noreorder ();
13738 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
13739 expr1.X_add_number = 2;
13740 macro_build (&expr1, "bnez", "x,p", op[2]);
13741 macro_build (NULL, "break", "6", 7);
13742
13743 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13744 since that causes an overflow. We should do that as well,
13745 but I don't see how to do the comparisons without a temporary
13746 register. */
13747 end_noreorder ();
13748 macro_build (NULL, s, "x", op[0]);
13749 break;
13750
13751 case M_DIVU_3:
13752 s = "divu";
13753 s2 = "mflo";
13754 goto do_divu3;
13755 case M_REMU_3:
13756 s = "divu";
13757 s2 = "mfhi";
13758 goto do_divu3;
13759 case M_DDIVU_3:
13760 s = "ddivu";
13761 s2 = "mflo";
13762 goto do_divu3;
13763 case M_DREMU_3:
13764 s = "ddivu";
13765 s2 = "mfhi";
13766 do_divu3:
13767 start_noreorder ();
13768 macro_build (NULL, s, ".,x,y", op[1], op[2]);
13769 expr1.X_add_number = 2;
13770 macro_build (&expr1, "bnez", "x,p", op[2]);
13771 macro_build (NULL, "break", "6", 7);
13772 end_noreorder ();
13773 macro_build (NULL, s2, "x", op[0]);
13774 break;
13775
13776 case M_DMUL:
13777 dbl = 1;
13778 /* Fall through. */
13779 case M_MUL:
13780 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13781 macro_build (NULL, "mflo", "x", op[0]);
13782 break;
13783
13784 case M_DSUBU_I:
13785 dbl = 1;
13786 goto do_subu;
13787 case M_SUBU_I:
13788 do_subu:
13789 imm_expr.X_add_number = -imm_expr.X_add_number;
13790 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
13791 break;
13792
13793 case M_SUBU_I_2:
13794 imm_expr.X_add_number = -imm_expr.X_add_number;
13795 macro_build (&imm_expr, "addiu", "x,k", op[0]);
13796 break;
13797
13798 case M_DSUBU_I_2:
13799 imm_expr.X_add_number = -imm_expr.X_add_number;
13800 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
13801 break;
13802
13803 case M_BEQ:
13804 s = "cmp";
13805 s2 = "bteqz";
13806 goto do_branch;
13807 case M_BNE:
13808 s = "cmp";
13809 s2 = "btnez";
13810 goto do_branch;
13811 case M_BLT:
13812 s = "slt";
13813 s2 = "btnez";
13814 goto do_branch;
13815 case M_BLTU:
13816 s = "sltu";
13817 s2 = "btnez";
13818 goto do_branch;
13819 case M_BLE:
13820 s = "slt";
13821 s2 = "bteqz";
13822 goto do_reverse_branch;
13823 case M_BLEU:
13824 s = "sltu";
13825 s2 = "bteqz";
13826 goto do_reverse_branch;
13827 case M_BGE:
13828 s = "slt";
13829 s2 = "bteqz";
13830 goto do_branch;
13831 case M_BGEU:
13832 s = "sltu";
13833 s2 = "bteqz";
13834 goto do_branch;
13835 case M_BGT:
13836 s = "slt";
13837 s2 = "btnez";
13838 goto do_reverse_branch;
13839 case M_BGTU:
13840 s = "sltu";
13841 s2 = "btnez";
13842
13843 do_reverse_branch:
13844 tmp = op[1];
13845 op[1] = op[0];
13846 op[0] = tmp;
13847
13848 do_branch:
13849 macro_build (NULL, s, "x,y", op[0], op[1]);
13850 macro_build (&offset_expr, s2, "p");
13851 break;
13852
13853 case M_BEQ_I:
13854 s = "cmpi";
13855 s2 = "bteqz";
13856 s3 = "x,U";
13857 goto do_branch_i;
13858 case M_BNE_I:
13859 s = "cmpi";
13860 s2 = "btnez";
13861 s3 = "x,U";
13862 goto do_branch_i;
13863 case M_BLT_I:
13864 s = "slti";
13865 s2 = "btnez";
13866 s3 = "x,8";
13867 goto do_branch_i;
13868 case M_BLTU_I:
13869 s = "sltiu";
13870 s2 = "btnez";
13871 s3 = "x,8";
13872 goto do_branch_i;
13873 case M_BLE_I:
13874 s = "slti";
13875 s2 = "btnez";
13876 s3 = "x,8";
13877 goto do_addone_branch_i;
13878 case M_BLEU_I:
13879 s = "sltiu";
13880 s2 = "btnez";
13881 s3 = "x,8";
13882 goto do_addone_branch_i;
13883 case M_BGE_I:
13884 s = "slti";
13885 s2 = "bteqz";
13886 s3 = "x,8";
13887 goto do_branch_i;
13888 case M_BGEU_I:
13889 s = "sltiu";
13890 s2 = "bteqz";
13891 s3 = "x,8";
13892 goto do_branch_i;
13893 case M_BGT_I:
13894 s = "slti";
13895 s2 = "bteqz";
13896 s3 = "x,8";
13897 goto do_addone_branch_i;
13898 case M_BGTU_I:
13899 s = "sltiu";
13900 s2 = "bteqz";
13901 s3 = "x,8";
13902
13903 do_addone_branch_i:
13904 ++imm_expr.X_add_number;
13905
13906 do_branch_i:
13907 macro_build (&imm_expr, s, s3, op[0]);
13908 macro_build (&offset_expr, s2, "p");
13909 break;
13910
13911 case M_ABS:
13912 expr1.X_add_number = 0;
13913 macro_build (&expr1, "slti", "x,8", op[1]);
13914 if (op[0] != op[1])
13915 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
13916 expr1.X_add_number = 2;
13917 macro_build (&expr1, "bteqz", "p");
13918 macro_build (NULL, "neg", "x,w", op[0], op[0]);
13919 break;
13920 }
13921 }
13922
13923 /* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13924 opcode bits in *OPCODE_EXTRA. */
13925
13926 static struct mips_opcode *
13927 mips_lookup_insn (struct hash_control *hash, const char *start,
13928 ssize_t length, unsigned int *opcode_extra)
13929 {
13930 char *name, *dot, *p;
13931 unsigned int mask, suffix;
13932 ssize_t opend;
13933 struct mips_opcode *insn;
13934
13935 /* Make a copy of the instruction so that we can fiddle with it. */
13936 name = xstrndup (start, length);
13937
13938 /* Look up the instruction as-is. */
13939 insn = (struct mips_opcode *) hash_find (hash, name);
13940 if (insn)
13941 goto end;
13942
13943 dot = strchr (name, '.');
13944 if (dot && dot[1])
13945 {
13946 /* Try to interpret the text after the dot as a VU0 channel suffix. */
13947 p = mips_parse_vu0_channels (dot + 1, &mask);
13948 if (*p == 0 && mask != 0)
13949 {
13950 *dot = 0;
13951 insn = (struct mips_opcode *) hash_find (hash, name);
13952 *dot = '.';
13953 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13954 {
13955 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
13956 goto end;
13957 }
13958 }
13959 }
13960
13961 if (mips_opts.micromips)
13962 {
13963 /* See if there's an instruction size override suffix,
13964 either `16' or `32', at the end of the mnemonic proper,
13965 that defines the operation, i.e. before the first `.'
13966 character if any. Strip it and retry. */
13967 opend = dot != NULL ? dot - name : length;
13968 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13969 suffix = 2;
13970 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13971 suffix = 4;
13972 else
13973 suffix = 0;
13974 if (suffix)
13975 {
13976 memmove (name + opend - 2, name + opend, length - opend + 1);
13977 insn = (struct mips_opcode *) hash_find (hash, name);
13978 if (insn)
13979 {
13980 forced_insn_length = suffix;
13981 goto end;
13982 }
13983 }
13984 }
13985
13986 insn = NULL;
13987 end:
13988 free (name);
13989 return insn;
13990 }
13991
13992 /* Assemble an instruction into its binary format. If the instruction
13993 is a macro, set imm_expr and offset_expr to the values associated
13994 with "I" and "A" operands respectively. Otherwise store the value
13995 of the relocatable field (if any) in offset_expr. In both cases
13996 set offset_reloc to the relocation operators applied to offset_expr. */
13997
13998 static void
13999 mips_ip (char *str, struct mips_cl_insn *insn)
14000 {
14001 const struct mips_opcode *first, *past;
14002 struct hash_control *hash;
14003 char format;
14004 size_t end;
14005 struct mips_operand_token *tokens;
14006 unsigned int opcode_extra;
14007
14008 if (mips_opts.micromips)
14009 {
14010 hash = micromips_op_hash;
14011 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14012 }
14013 else
14014 {
14015 hash = op_hash;
14016 past = &mips_opcodes[NUMOPCODES];
14017 }
14018 forced_insn_length = 0;
14019 opcode_extra = 0;
14020
14021 /* We first try to match an instruction up to a space or to the end. */
14022 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14023 continue;
14024
14025 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14026 if (first == NULL)
14027 {
14028 set_insn_error (0, _("unrecognized opcode"));
14029 return;
14030 }
14031
14032 if (strcmp (first->name, "li.s") == 0)
14033 format = 'f';
14034 else if (strcmp (first->name, "li.d") == 0)
14035 format = 'd';
14036 else
14037 format = 0;
14038 tokens = mips_parse_arguments (str + end, format);
14039 if (!tokens)
14040 return;
14041
14042 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14043 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
14044 set_insn_error (0, _("invalid operands"));
14045
14046 obstack_free (&mips_operand_tokens, tokens);
14047 }
14048
14049 /* As for mips_ip, but used when assembling MIPS16 code.
14050 Also set forced_insn_length to the resulting instruction size in
14051 bytes if the user explicitly requested a small or extended instruction. */
14052
14053 static void
14054 mips16_ip (char *str, struct mips_cl_insn *insn)
14055 {
14056 char *end, *s, c;
14057 struct mips_opcode *first;
14058 struct mips_operand_token *tokens;
14059 unsigned int l;
14060
14061 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
14062 ;
14063 end = s;
14064 c = *end;
14065
14066 l = 0;
14067 switch (c)
14068 {
14069 case '\0':
14070 break;
14071
14072 case ' ':
14073 s++;
14074 break;
14075
14076 case '.':
14077 s++;
14078 if (*s == 't')
14079 {
14080 l = 2;
14081 s++;
14082 }
14083 else if (*s == 'e')
14084 {
14085 l = 4;
14086 s++;
14087 }
14088 if (*s == '\0')
14089 break;
14090 else if (*s++ == ' ')
14091 break;
14092 set_insn_error (0, _("unrecognized opcode"));
14093 return;
14094 }
14095 forced_insn_length = l;
14096
14097 *end = 0;
14098 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
14099 *end = c;
14100
14101 if (!first)
14102 {
14103 set_insn_error (0, _("unrecognized opcode"));
14104 return;
14105 }
14106
14107 tokens = mips_parse_arguments (s, 0);
14108 if (!tokens)
14109 return;
14110
14111 if (!match_mips16_insns (insn, first, tokens))
14112 set_insn_error (0, _("invalid operands"));
14113
14114 obstack_free (&mips_operand_tokens, tokens);
14115 }
14116
14117 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14118 NBITS is the number of significant bits in VAL. */
14119
14120 static unsigned long
14121 mips16_immed_extend (offsetT val, unsigned int nbits)
14122 {
14123 int extval;
14124
14125 extval = 0;
14126 val &= (1U << nbits) - 1;
14127 if (nbits == 16 || nbits == 9)
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 if (nbits == 6)
14138 {
14139 extval = ((val & 0x1f) << 6) | (val & 0x20);
14140 val = 0;
14141 }
14142 return (extval << 16) | val;
14143 }
14144
14145 /* Like decode_mips16_operand, but require the operand to be defined and
14146 require it to be an integer. */
14147
14148 static const struct mips_int_operand *
14149 mips16_immed_operand (int type, bfd_boolean extended_p)
14150 {
14151 const struct mips_operand *operand;
14152
14153 operand = decode_mips16_operand (type, extended_p);
14154 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14155 abort ();
14156 return (const struct mips_int_operand *) operand;
14157 }
14158
14159 /* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14160
14161 static bfd_boolean
14162 mips16_immed_in_range_p (const struct mips_int_operand *operand,
14163 bfd_reloc_code_real_type reloc, offsetT sval)
14164 {
14165 int min_val, max_val;
14166
14167 min_val = mips_int_operand_min (operand);
14168 max_val = mips_int_operand_max (operand);
14169 if (reloc != BFD_RELOC_UNUSED)
14170 {
14171 if (min_val < 0)
14172 sval = SEXT_16BIT (sval);
14173 else
14174 sval &= 0xffff;
14175 }
14176
14177 return (sval >= min_val
14178 && sval <= max_val
14179 && (sval & ((1 << operand->shift) - 1)) == 0);
14180 }
14181
14182 /* Install immediate value VAL into MIPS16 instruction *INSN,
14183 extending it if necessary. The instruction in *INSN may
14184 already be extended.
14185
14186 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14187 if none. In the former case, VAL is a 16-bit number with no
14188 defined signedness.
14189
14190 TYPE is the type of the immediate field. USER_INSN_LENGTH
14191 is the length that the user requested, or 0 if none. */
14192
14193 static void
14194 mips16_immed (const char *file, unsigned int line, int type,
14195 bfd_reloc_code_real_type reloc, offsetT val,
14196 unsigned int user_insn_length, unsigned long *insn)
14197 {
14198 const struct mips_int_operand *operand;
14199 unsigned int uval, length;
14200
14201 operand = mips16_immed_operand (type, FALSE);
14202 if (!mips16_immed_in_range_p (operand, reloc, val))
14203 {
14204 /* We need an extended instruction. */
14205 if (user_insn_length == 2)
14206 as_bad_where (file, line, _("invalid unextended operand value"));
14207 else
14208 *insn |= MIPS16_EXTEND;
14209 }
14210 else if (user_insn_length == 4)
14211 {
14212 /* The operand doesn't force an unextended instruction to be extended.
14213 Warn if the user wanted an extended instruction anyway. */
14214 *insn |= MIPS16_EXTEND;
14215 as_warn_where (file, line,
14216 _("extended operand requested but not required"));
14217 }
14218
14219 length = mips16_opcode_length (*insn);
14220 if (length == 4)
14221 {
14222 operand = mips16_immed_operand (type, TRUE);
14223 if (!mips16_immed_in_range_p (operand, reloc, val))
14224 as_bad_where (file, line,
14225 _("operand value out of range for instruction"));
14226 }
14227 uval = ((unsigned int) val >> operand->shift) - operand->bias;
14228 if (length == 2 || operand->root.lsb != 0)
14229 *insn = mips_insert_operand (&operand->root, *insn, uval);
14230 else
14231 *insn |= mips16_immed_extend (uval, operand->root.size);
14232 }
14233 \f
14234 struct percent_op_match
14235 {
14236 const char *str;
14237 bfd_reloc_code_real_type reloc;
14238 };
14239
14240 static const struct percent_op_match mips_percent_op[] =
14241 {
14242 {"%lo", BFD_RELOC_LO16},
14243 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14244 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14245 {"%call16", BFD_RELOC_MIPS_CALL16},
14246 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14247 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14248 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14249 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14250 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14251 {"%got", BFD_RELOC_MIPS_GOT16},
14252 {"%gp_rel", BFD_RELOC_GPREL16},
14253 {"%gprel", BFD_RELOC_GPREL16},
14254 {"%half", BFD_RELOC_16},
14255 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14256 {"%higher", BFD_RELOC_MIPS_HIGHER},
14257 {"%neg", BFD_RELOC_MIPS_SUB},
14258 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14259 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14260 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14261 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14262 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14263 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14264 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14265 {"%hi", BFD_RELOC_HI16_S},
14266 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14267 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
14268 };
14269
14270 static const struct percent_op_match mips16_percent_op[] =
14271 {
14272 {"%lo", BFD_RELOC_MIPS16_LO16},
14273 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
14274 {"%gprel", BFD_RELOC_MIPS16_GPREL},
14275 {"%got", BFD_RELOC_MIPS16_GOT16},
14276 {"%call16", BFD_RELOC_MIPS16_CALL16},
14277 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14278 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14279 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14280 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14281 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14282 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14283 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14284 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14285 };
14286
14287
14288 /* Return true if *STR points to a relocation operator. When returning true,
14289 move *STR over the operator and store its relocation code in *RELOC.
14290 Leave both *STR and *RELOC alone when returning false. */
14291
14292 static bfd_boolean
14293 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14294 {
14295 const struct percent_op_match *percent_op;
14296 size_t limit, i;
14297
14298 if (mips_opts.mips16)
14299 {
14300 percent_op = mips16_percent_op;
14301 limit = ARRAY_SIZE (mips16_percent_op);
14302 }
14303 else
14304 {
14305 percent_op = mips_percent_op;
14306 limit = ARRAY_SIZE (mips_percent_op);
14307 }
14308
14309 for (i = 0; i < limit; i++)
14310 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14311 {
14312 int len = strlen (percent_op[i].str);
14313
14314 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14315 continue;
14316
14317 *str += strlen (percent_op[i].str);
14318 *reloc = percent_op[i].reloc;
14319
14320 /* Check whether the output BFD supports this relocation.
14321 If not, issue an error and fall back on something safe. */
14322 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14323 {
14324 as_bad (_("relocation %s isn't supported by the current ABI"),
14325 percent_op[i].str);
14326 *reloc = BFD_RELOC_UNUSED;
14327 }
14328 return TRUE;
14329 }
14330 return FALSE;
14331 }
14332
14333
14334 /* Parse string STR as a 16-bit relocatable operand. Store the
14335 expression in *EP and the relocations in the array starting
14336 at RELOC. Return the number of relocation operators used.
14337
14338 On exit, EXPR_END points to the first character after the expression. */
14339
14340 static size_t
14341 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14342 char *str)
14343 {
14344 bfd_reloc_code_real_type reversed_reloc[3];
14345 size_t reloc_index, i;
14346 int crux_depth, str_depth;
14347 char *crux;
14348
14349 /* Search for the start of the main expression, recoding relocations
14350 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14351 of the main expression and with CRUX_DEPTH containing the number
14352 of open brackets at that point. */
14353 reloc_index = -1;
14354 str_depth = 0;
14355 do
14356 {
14357 reloc_index++;
14358 crux = str;
14359 crux_depth = str_depth;
14360
14361 /* Skip over whitespace and brackets, keeping count of the number
14362 of brackets. */
14363 while (*str == ' ' || *str == '\t' || *str == '(')
14364 if (*str++ == '(')
14365 str_depth++;
14366 }
14367 while (*str == '%'
14368 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14369 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14370
14371 my_getExpression (ep, crux);
14372 str = expr_end;
14373
14374 /* Match every open bracket. */
14375 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14376 if (*str++ == ')')
14377 crux_depth--;
14378
14379 if (crux_depth > 0)
14380 as_bad (_("unclosed '('"));
14381
14382 expr_end = str;
14383
14384 if (reloc_index != 0)
14385 {
14386 prev_reloc_op_frag = frag_now;
14387 for (i = 0; i < reloc_index; i++)
14388 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14389 }
14390
14391 return reloc_index;
14392 }
14393
14394 static void
14395 my_getExpression (expressionS *ep, char *str)
14396 {
14397 char *save_in;
14398
14399 save_in = input_line_pointer;
14400 input_line_pointer = str;
14401 expression (ep);
14402 expr_end = input_line_pointer;
14403 input_line_pointer = save_in;
14404 }
14405
14406 const char *
14407 md_atof (int type, char *litP, int *sizeP)
14408 {
14409 return ieee_md_atof (type, litP, sizeP, target_big_endian);
14410 }
14411
14412 void
14413 md_number_to_chars (char *buf, valueT val, int n)
14414 {
14415 if (target_big_endian)
14416 number_to_chars_bigendian (buf, val, n);
14417 else
14418 number_to_chars_littleendian (buf, val, n);
14419 }
14420 \f
14421 static int support_64bit_objects(void)
14422 {
14423 const char **list, **l;
14424 int yes;
14425
14426 list = bfd_target_list ();
14427 for (l = list; *l != NULL; l++)
14428 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14429 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14430 break;
14431 yes = (*l != NULL);
14432 free (list);
14433 return yes;
14434 }
14435
14436 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14437 NEW_VALUE. Warn if another value was already specified. Note:
14438 we have to defer parsing the -march and -mtune arguments in order
14439 to handle 'from-abi' correctly, since the ABI might be specified
14440 in a later argument. */
14441
14442 static void
14443 mips_set_option_string (const char **string_ptr, const char *new_value)
14444 {
14445 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14446 as_warn (_("a different %s was already specified, is now %s"),
14447 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14448 new_value);
14449
14450 *string_ptr = new_value;
14451 }
14452
14453 int
14454 md_parse_option (int c, const char *arg)
14455 {
14456 unsigned int i;
14457
14458 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14459 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14460 {
14461 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
14462 c == mips_ases[i].option_on);
14463 return 1;
14464 }
14465
14466 switch (c)
14467 {
14468 case OPTION_CONSTRUCT_FLOATS:
14469 mips_disable_float_construction = 0;
14470 break;
14471
14472 case OPTION_NO_CONSTRUCT_FLOATS:
14473 mips_disable_float_construction = 1;
14474 break;
14475
14476 case OPTION_TRAP:
14477 mips_trap = 1;
14478 break;
14479
14480 case OPTION_BREAK:
14481 mips_trap = 0;
14482 break;
14483
14484 case OPTION_EB:
14485 target_big_endian = 1;
14486 break;
14487
14488 case OPTION_EL:
14489 target_big_endian = 0;
14490 break;
14491
14492 case 'O':
14493 if (arg == NULL)
14494 mips_optimize = 1;
14495 else if (arg[0] == '0')
14496 mips_optimize = 0;
14497 else if (arg[0] == '1')
14498 mips_optimize = 1;
14499 else
14500 mips_optimize = 2;
14501 break;
14502
14503 case 'g':
14504 if (arg == NULL)
14505 mips_debug = 2;
14506 else
14507 mips_debug = atoi (arg);
14508 break;
14509
14510 case OPTION_MIPS1:
14511 file_mips_opts.isa = ISA_MIPS1;
14512 break;
14513
14514 case OPTION_MIPS2:
14515 file_mips_opts.isa = ISA_MIPS2;
14516 break;
14517
14518 case OPTION_MIPS3:
14519 file_mips_opts.isa = ISA_MIPS3;
14520 break;
14521
14522 case OPTION_MIPS4:
14523 file_mips_opts.isa = ISA_MIPS4;
14524 break;
14525
14526 case OPTION_MIPS5:
14527 file_mips_opts.isa = ISA_MIPS5;
14528 break;
14529
14530 case OPTION_MIPS32:
14531 file_mips_opts.isa = ISA_MIPS32;
14532 break;
14533
14534 case OPTION_MIPS32R2:
14535 file_mips_opts.isa = ISA_MIPS32R2;
14536 break;
14537
14538 case OPTION_MIPS32R3:
14539 file_mips_opts.isa = ISA_MIPS32R3;
14540 break;
14541
14542 case OPTION_MIPS32R5:
14543 file_mips_opts.isa = ISA_MIPS32R5;
14544 break;
14545
14546 case OPTION_MIPS32R6:
14547 file_mips_opts.isa = ISA_MIPS32R6;
14548 break;
14549
14550 case OPTION_MIPS64R2:
14551 file_mips_opts.isa = ISA_MIPS64R2;
14552 break;
14553
14554 case OPTION_MIPS64R3:
14555 file_mips_opts.isa = ISA_MIPS64R3;
14556 break;
14557
14558 case OPTION_MIPS64R5:
14559 file_mips_opts.isa = ISA_MIPS64R5;
14560 break;
14561
14562 case OPTION_MIPS64R6:
14563 file_mips_opts.isa = ISA_MIPS64R6;
14564 break;
14565
14566 case OPTION_MIPS64:
14567 file_mips_opts.isa = ISA_MIPS64;
14568 break;
14569
14570 case OPTION_MTUNE:
14571 mips_set_option_string (&mips_tune_string, arg);
14572 break;
14573
14574 case OPTION_MARCH:
14575 mips_set_option_string (&mips_arch_string, arg);
14576 break;
14577
14578 case OPTION_M4650:
14579 mips_set_option_string (&mips_arch_string, "4650");
14580 mips_set_option_string (&mips_tune_string, "4650");
14581 break;
14582
14583 case OPTION_NO_M4650:
14584 break;
14585
14586 case OPTION_M4010:
14587 mips_set_option_string (&mips_arch_string, "4010");
14588 mips_set_option_string (&mips_tune_string, "4010");
14589 break;
14590
14591 case OPTION_NO_M4010:
14592 break;
14593
14594 case OPTION_M4100:
14595 mips_set_option_string (&mips_arch_string, "4100");
14596 mips_set_option_string (&mips_tune_string, "4100");
14597 break;
14598
14599 case OPTION_NO_M4100:
14600 break;
14601
14602 case OPTION_M3900:
14603 mips_set_option_string (&mips_arch_string, "3900");
14604 mips_set_option_string (&mips_tune_string, "3900");
14605 break;
14606
14607 case OPTION_NO_M3900:
14608 break;
14609
14610 case OPTION_MICROMIPS:
14611 if (file_mips_opts.mips16 == 1)
14612 {
14613 as_bad (_("-mmicromips cannot be used with -mips16"));
14614 return 0;
14615 }
14616 file_mips_opts.micromips = 1;
14617 mips_no_prev_insn ();
14618 break;
14619
14620 case OPTION_NO_MICROMIPS:
14621 file_mips_opts.micromips = 0;
14622 mips_no_prev_insn ();
14623 break;
14624
14625 case OPTION_MIPS16:
14626 if (file_mips_opts.micromips == 1)
14627 {
14628 as_bad (_("-mips16 cannot be used with -micromips"));
14629 return 0;
14630 }
14631 file_mips_opts.mips16 = 1;
14632 mips_no_prev_insn ();
14633 break;
14634
14635 case OPTION_NO_MIPS16:
14636 file_mips_opts.mips16 = 0;
14637 mips_no_prev_insn ();
14638 break;
14639
14640 case OPTION_FIX_24K:
14641 mips_fix_24k = 1;
14642 break;
14643
14644 case OPTION_NO_FIX_24K:
14645 mips_fix_24k = 0;
14646 break;
14647
14648 case OPTION_FIX_RM7000:
14649 mips_fix_rm7000 = 1;
14650 break;
14651
14652 case OPTION_NO_FIX_RM7000:
14653 mips_fix_rm7000 = 0;
14654 break;
14655
14656 case OPTION_FIX_LOONGSON2F_JUMP:
14657 mips_fix_loongson2f_jump = TRUE;
14658 break;
14659
14660 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14661 mips_fix_loongson2f_jump = FALSE;
14662 break;
14663
14664 case OPTION_FIX_LOONGSON2F_NOP:
14665 mips_fix_loongson2f_nop = TRUE;
14666 break;
14667
14668 case OPTION_NO_FIX_LOONGSON2F_NOP:
14669 mips_fix_loongson2f_nop = FALSE;
14670 break;
14671
14672 case OPTION_FIX_VR4120:
14673 mips_fix_vr4120 = 1;
14674 break;
14675
14676 case OPTION_NO_FIX_VR4120:
14677 mips_fix_vr4120 = 0;
14678 break;
14679
14680 case OPTION_FIX_VR4130:
14681 mips_fix_vr4130 = 1;
14682 break;
14683
14684 case OPTION_NO_FIX_VR4130:
14685 mips_fix_vr4130 = 0;
14686 break;
14687
14688 case OPTION_FIX_CN63XXP1:
14689 mips_fix_cn63xxp1 = TRUE;
14690 break;
14691
14692 case OPTION_NO_FIX_CN63XXP1:
14693 mips_fix_cn63xxp1 = FALSE;
14694 break;
14695
14696 case OPTION_RELAX_BRANCH:
14697 mips_relax_branch = 1;
14698 break;
14699
14700 case OPTION_NO_RELAX_BRANCH:
14701 mips_relax_branch = 0;
14702 break;
14703
14704 case OPTION_IGNORE_BRANCH_ISA:
14705 mips_ignore_branch_isa = TRUE;
14706 break;
14707
14708 case OPTION_NO_IGNORE_BRANCH_ISA:
14709 mips_ignore_branch_isa = FALSE;
14710 break;
14711
14712 case OPTION_INSN32:
14713 file_mips_opts.insn32 = TRUE;
14714 break;
14715
14716 case OPTION_NO_INSN32:
14717 file_mips_opts.insn32 = FALSE;
14718 break;
14719
14720 case OPTION_MSHARED:
14721 mips_in_shared = TRUE;
14722 break;
14723
14724 case OPTION_MNO_SHARED:
14725 mips_in_shared = FALSE;
14726 break;
14727
14728 case OPTION_MSYM32:
14729 file_mips_opts.sym32 = TRUE;
14730 break;
14731
14732 case OPTION_MNO_SYM32:
14733 file_mips_opts.sym32 = FALSE;
14734 break;
14735
14736 /* When generating ELF code, we permit -KPIC and -call_shared to
14737 select SVR4_PIC, and -non_shared to select no PIC. This is
14738 intended to be compatible with Irix 5. */
14739 case OPTION_CALL_SHARED:
14740 mips_pic = SVR4_PIC;
14741 mips_abicalls = TRUE;
14742 break;
14743
14744 case OPTION_CALL_NONPIC:
14745 mips_pic = NO_PIC;
14746 mips_abicalls = TRUE;
14747 break;
14748
14749 case OPTION_NON_SHARED:
14750 mips_pic = NO_PIC;
14751 mips_abicalls = FALSE;
14752 break;
14753
14754 /* The -xgot option tells the assembler to use 32 bit offsets
14755 when accessing the got in SVR4_PIC mode. It is for Irix
14756 compatibility. */
14757 case OPTION_XGOT:
14758 mips_big_got = 1;
14759 break;
14760
14761 case 'G':
14762 g_switch_value = atoi (arg);
14763 g_switch_seen = 1;
14764 break;
14765
14766 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14767 and -mabi=64. */
14768 case OPTION_32:
14769 mips_abi = O32_ABI;
14770 break;
14771
14772 case OPTION_N32:
14773 mips_abi = N32_ABI;
14774 break;
14775
14776 case OPTION_64:
14777 mips_abi = N64_ABI;
14778 if (!support_64bit_objects())
14779 as_fatal (_("no compiled in support for 64 bit object file format"));
14780 break;
14781
14782 case OPTION_GP32:
14783 file_mips_opts.gp = 32;
14784 break;
14785
14786 case OPTION_GP64:
14787 file_mips_opts.gp = 64;
14788 break;
14789
14790 case OPTION_FP32:
14791 file_mips_opts.fp = 32;
14792 break;
14793
14794 case OPTION_FPXX:
14795 file_mips_opts.fp = 0;
14796 break;
14797
14798 case OPTION_FP64:
14799 file_mips_opts.fp = 64;
14800 break;
14801
14802 case OPTION_ODD_SPREG:
14803 file_mips_opts.oddspreg = 1;
14804 break;
14805
14806 case OPTION_NO_ODD_SPREG:
14807 file_mips_opts.oddspreg = 0;
14808 break;
14809
14810 case OPTION_SINGLE_FLOAT:
14811 file_mips_opts.single_float = 1;
14812 break;
14813
14814 case OPTION_DOUBLE_FLOAT:
14815 file_mips_opts.single_float = 0;
14816 break;
14817
14818 case OPTION_SOFT_FLOAT:
14819 file_mips_opts.soft_float = 1;
14820 break;
14821
14822 case OPTION_HARD_FLOAT:
14823 file_mips_opts.soft_float = 0;
14824 break;
14825
14826 case OPTION_MABI:
14827 if (strcmp (arg, "32") == 0)
14828 mips_abi = O32_ABI;
14829 else if (strcmp (arg, "o64") == 0)
14830 mips_abi = O64_ABI;
14831 else if (strcmp (arg, "n32") == 0)
14832 mips_abi = N32_ABI;
14833 else if (strcmp (arg, "64") == 0)
14834 {
14835 mips_abi = N64_ABI;
14836 if (! support_64bit_objects())
14837 as_fatal (_("no compiled in support for 64 bit object file "
14838 "format"));
14839 }
14840 else if (strcmp (arg, "eabi") == 0)
14841 mips_abi = EABI_ABI;
14842 else
14843 {
14844 as_fatal (_("invalid abi -mabi=%s"), arg);
14845 return 0;
14846 }
14847 break;
14848
14849 case OPTION_M7000_HILO_FIX:
14850 mips_7000_hilo_fix = TRUE;
14851 break;
14852
14853 case OPTION_MNO_7000_HILO_FIX:
14854 mips_7000_hilo_fix = FALSE;
14855 break;
14856
14857 case OPTION_MDEBUG:
14858 mips_flag_mdebug = TRUE;
14859 break;
14860
14861 case OPTION_NO_MDEBUG:
14862 mips_flag_mdebug = FALSE;
14863 break;
14864
14865 case OPTION_PDR:
14866 mips_flag_pdr = TRUE;
14867 break;
14868
14869 case OPTION_NO_PDR:
14870 mips_flag_pdr = FALSE;
14871 break;
14872
14873 case OPTION_MVXWORKS_PIC:
14874 mips_pic = VXWORKS_PIC;
14875 break;
14876
14877 case OPTION_NAN:
14878 if (strcmp (arg, "2008") == 0)
14879 mips_nan2008 = 1;
14880 else if (strcmp (arg, "legacy") == 0)
14881 mips_nan2008 = 0;
14882 else
14883 {
14884 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
14885 return 0;
14886 }
14887 break;
14888
14889 default:
14890 return 0;
14891 }
14892
14893 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14894
14895 return 1;
14896 }
14897 \f
14898 /* Set up globals to tune for the ISA or processor described by INFO. */
14899
14900 static void
14901 mips_set_tune (const struct mips_cpu_info *info)
14902 {
14903 if (info != 0)
14904 mips_tune = info->cpu;
14905 }
14906
14907
14908 void
14909 mips_after_parse_args (void)
14910 {
14911 const struct mips_cpu_info *arch_info = 0;
14912 const struct mips_cpu_info *tune_info = 0;
14913
14914 /* GP relative stuff not working for PE */
14915 if (strncmp (TARGET_OS, "pe", 2) == 0)
14916 {
14917 if (g_switch_seen && g_switch_value != 0)
14918 as_bad (_("-G not supported in this configuration"));
14919 g_switch_value = 0;
14920 }
14921
14922 if (mips_abi == NO_ABI)
14923 mips_abi = MIPS_DEFAULT_ABI;
14924
14925 /* The following code determines the architecture.
14926 Similar code was added to GCC 3.3 (see override_options() in
14927 config/mips/mips.c). The GAS and GCC code should be kept in sync
14928 as much as possible. */
14929
14930 if (mips_arch_string != 0)
14931 arch_info = mips_parse_cpu ("-march", mips_arch_string);
14932
14933 if (file_mips_opts.isa != ISA_UNKNOWN)
14934 {
14935 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
14936 ISA level specified by -mipsN, while arch_info->isa contains
14937 the -march selection (if any). */
14938 if (arch_info != 0)
14939 {
14940 /* -march takes precedence over -mipsN, since it is more descriptive.
14941 There's no harm in specifying both as long as the ISA levels
14942 are the same. */
14943 if (file_mips_opts.isa != arch_info->isa)
14944 as_bad (_("-%s conflicts with the other architecture options,"
14945 " which imply -%s"),
14946 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
14947 mips_cpu_info_from_isa (arch_info->isa)->name);
14948 }
14949 else
14950 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
14951 }
14952
14953 if (arch_info == 0)
14954 {
14955 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
14956 gas_assert (arch_info);
14957 }
14958
14959 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
14960 as_bad (_("-march=%s is not compatible with the selected ABI"),
14961 arch_info->name);
14962
14963 file_mips_opts.arch = arch_info->cpu;
14964 file_mips_opts.isa = arch_info->isa;
14965
14966 /* Set up initial mips_opts state. */
14967 mips_opts = file_mips_opts;
14968
14969 /* The register size inference code is now placed in
14970 file_mips_check_options. */
14971
14972 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
14973 processor. */
14974 if (mips_tune_string != 0)
14975 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
14976
14977 if (tune_info == 0)
14978 mips_set_tune (arch_info);
14979 else
14980 mips_set_tune (tune_info);
14981
14982 if (mips_flag_mdebug < 0)
14983 mips_flag_mdebug = 0;
14984 }
14985 \f
14986 void
14987 mips_init_after_args (void)
14988 {
14989 /* initialize opcodes */
14990 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
14991 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
14992 }
14993
14994 long
14995 md_pcrel_from (fixS *fixP)
14996 {
14997 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14998 switch (fixP->fx_r_type)
14999 {
15000 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15001 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15002 /* Return the address of the delay slot. */
15003 return addr + 2;
15004
15005 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15006 case BFD_RELOC_MICROMIPS_JMP:
15007 case BFD_RELOC_MIPS16_16_PCREL_S1:
15008 case BFD_RELOC_16_PCREL_S2:
15009 case BFD_RELOC_MIPS_21_PCREL_S2:
15010 case BFD_RELOC_MIPS_26_PCREL_S2:
15011 case BFD_RELOC_MIPS_JMP:
15012 /* Return the address of the delay slot. */
15013 return addr + 4;
15014
15015 case BFD_RELOC_MIPS_18_PCREL_S3:
15016 /* Return the aligned address of the doubleword containing
15017 the instruction. */
15018 return addr & ~7;
15019
15020 default:
15021 return addr;
15022 }
15023 }
15024
15025 /* This is called before the symbol table is processed. In order to
15026 work with gcc when using mips-tfile, we must keep all local labels.
15027 However, in other cases, we want to discard them. If we were
15028 called with -g, but we didn't see any debugging information, it may
15029 mean that gcc is smuggling debugging information through to
15030 mips-tfile, in which case we must generate all local labels. */
15031
15032 void
15033 mips_frob_file_before_adjust (void)
15034 {
15035 #ifndef NO_ECOFF_DEBUGGING
15036 if (ECOFF_DEBUGGING
15037 && mips_debug != 0
15038 && ! ecoff_debugging_seen)
15039 flag_keep_locals = 1;
15040 #endif
15041 }
15042
15043 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15044 the corresponding LO16 reloc. This is called before md_apply_fix and
15045 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15046 relocation operators.
15047
15048 For our purposes, a %lo() expression matches a %got() or %hi()
15049 expression if:
15050
15051 (a) it refers to the same symbol; and
15052 (b) the offset applied in the %lo() expression is no lower than
15053 the offset applied in the %got() or %hi().
15054
15055 (b) allows us to cope with code like:
15056
15057 lui $4,%hi(foo)
15058 lh $4,%lo(foo+2)($4)
15059
15060 ...which is legal on RELA targets, and has a well-defined behaviour
15061 if the user knows that adding 2 to "foo" will not induce a carry to
15062 the high 16 bits.
15063
15064 When several %lo()s match a particular %got() or %hi(), we use the
15065 following rules to distinguish them:
15066
15067 (1) %lo()s with smaller offsets are a better match than %lo()s with
15068 higher offsets.
15069
15070 (2) %lo()s with no matching %got() or %hi() are better than those
15071 that already have a matching %got() or %hi().
15072
15073 (3) later %lo()s are better than earlier %lo()s.
15074
15075 These rules are applied in order.
15076
15077 (1) means, among other things, that %lo()s with identical offsets are
15078 chosen if they exist.
15079
15080 (2) means that we won't associate several high-part relocations with
15081 the same low-part relocation unless there's no alternative. Having
15082 several high parts for the same low part is a GNU extension; this rule
15083 allows careful users to avoid it.
15084
15085 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15086 with the last high-part relocation being at the front of the list.
15087 It therefore makes sense to choose the last matching low-part
15088 relocation, all other things being equal. It's also easier
15089 to code that way. */
15090
15091 void
15092 mips_frob_file (void)
15093 {
15094 struct mips_hi_fixup *l;
15095 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15096
15097 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15098 {
15099 segment_info_type *seginfo;
15100 bfd_boolean matched_lo_p;
15101 fixS **hi_pos, **lo_pos, **pos;
15102
15103 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15104
15105 /* If a GOT16 relocation turns out to be against a global symbol,
15106 there isn't supposed to be a matching LO. Ignore %gots against
15107 constants; we'll report an error for those later. */
15108 if (got16_reloc_p (l->fixp->fx_r_type)
15109 && !(l->fixp->fx_addsy
15110 && pic_need_relax (l->fixp->fx_addsy)))
15111 continue;
15112
15113 /* Check quickly whether the next fixup happens to be a matching %lo. */
15114 if (fixup_has_matching_lo_p (l->fixp))
15115 continue;
15116
15117 seginfo = seg_info (l->seg);
15118
15119 /* Set HI_POS to the position of this relocation in the chain.
15120 Set LO_POS to the position of the chosen low-part relocation.
15121 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15122 relocation that matches an immediately-preceding high-part
15123 relocation. */
15124 hi_pos = NULL;
15125 lo_pos = NULL;
15126 matched_lo_p = FALSE;
15127 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15128
15129 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15130 {
15131 if (*pos == l->fixp)
15132 hi_pos = pos;
15133
15134 if ((*pos)->fx_r_type == looking_for_rtype
15135 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15136 && (*pos)->fx_offset >= l->fixp->fx_offset
15137 && (lo_pos == NULL
15138 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15139 || (!matched_lo_p
15140 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15141 lo_pos = pos;
15142
15143 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15144 && fixup_has_matching_lo_p (*pos));
15145 }
15146
15147 /* If we found a match, remove the high-part relocation from its
15148 current position and insert it before the low-part relocation.
15149 Make the offsets match so that fixup_has_matching_lo_p()
15150 will return true.
15151
15152 We don't warn about unmatched high-part relocations since some
15153 versions of gcc have been known to emit dead "lui ...%hi(...)"
15154 instructions. */
15155 if (lo_pos != NULL)
15156 {
15157 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15158 if (l->fixp->fx_next != *lo_pos)
15159 {
15160 *hi_pos = l->fixp->fx_next;
15161 l->fixp->fx_next = *lo_pos;
15162 *lo_pos = l->fixp;
15163 }
15164 }
15165 }
15166 }
15167
15168 int
15169 mips_force_relocation (fixS *fixp)
15170 {
15171 if (generic_force_reloc (fixp))
15172 return 1;
15173
15174 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15175 so that the linker relaxation can update targets. */
15176 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15177 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15178 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15179 return 1;
15180
15181 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15182 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15183 microMIPS symbols so that we can do cross-mode branch diagnostics
15184 and BAL to JALX conversion by the linker. */
15185 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15186 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15187 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15188 && fixp->fx_addsy
15189 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15190 return 1;
15191
15192 /* We want all PC-relative relocations to be kept for R6 relaxation. */
15193 if (ISA_IS_R6 (file_mips_opts.isa)
15194 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15195 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15196 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15197 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15198 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15199 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15200 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15201 return 1;
15202
15203 return 0;
15204 }
15205
15206 /* Implement TC_FORCE_RELOCATION_ABS. */
15207
15208 bfd_boolean
15209 mips_force_relocation_abs (fixS *fixp)
15210 {
15211 if (generic_force_reloc (fixp))
15212 return TRUE;
15213
15214 /* These relocations do not have enough bits in the in-place addend
15215 to hold an arbitrary absolute section's offset. */
15216 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15217 return TRUE;
15218
15219 return FALSE;
15220 }
15221
15222 /* Read the instruction associated with RELOC from BUF. */
15223
15224 static unsigned int
15225 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15226 {
15227 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15228 return read_compressed_insn (buf, 4);
15229 else
15230 return read_insn (buf);
15231 }
15232
15233 /* Write instruction INSN to BUF, given that it has been relocated
15234 by RELOC. */
15235
15236 static void
15237 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15238 unsigned long insn)
15239 {
15240 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15241 write_compressed_insn (buf, insn, 4);
15242 else
15243 write_insn (buf, insn);
15244 }
15245
15246 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15247 to a symbol in another ISA mode, which cannot be converted to JALX. */
15248
15249 static bfd_boolean
15250 fix_bad_cross_mode_jump_p (fixS *fixP)
15251 {
15252 unsigned long opcode;
15253 int other;
15254 char *buf;
15255
15256 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15257 return FALSE;
15258
15259 other = S_GET_OTHER (fixP->fx_addsy);
15260 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15261 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15262 switch (fixP->fx_r_type)
15263 {
15264 case BFD_RELOC_MIPS_JMP:
15265 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15266 case BFD_RELOC_MICROMIPS_JMP:
15267 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15268 default:
15269 return FALSE;
15270 }
15271 }
15272
15273 /* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15274 jump to a symbol in the same ISA mode. */
15275
15276 static bfd_boolean
15277 fix_bad_same_mode_jalx_p (fixS *fixP)
15278 {
15279 unsigned long opcode;
15280 int other;
15281 char *buf;
15282
15283 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15284 return FALSE;
15285
15286 other = S_GET_OTHER (fixP->fx_addsy);
15287 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15288 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15289 switch (fixP->fx_r_type)
15290 {
15291 case BFD_RELOC_MIPS_JMP:
15292 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15293 case BFD_RELOC_MIPS16_JMP:
15294 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15295 case BFD_RELOC_MICROMIPS_JMP:
15296 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15297 default:
15298 return FALSE;
15299 }
15300 }
15301
15302 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15303 to a symbol whose value plus addend is not aligned according to the
15304 ultimate (after linker relaxation) jump instruction's immediate field
15305 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15306 regular MIPS code, to (1 << 2). */
15307
15308 static bfd_boolean
15309 fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15310 {
15311 bfd_boolean micro_to_mips_p;
15312 valueT val;
15313 int other;
15314
15315 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15316 return FALSE;
15317
15318 other = S_GET_OTHER (fixP->fx_addsy);
15319 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15320 val += fixP->fx_offset;
15321 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15322 && !ELF_ST_IS_MICROMIPS (other));
15323 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15324 != ELF_ST_IS_COMPRESSED (other));
15325 }
15326
15327 /* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15328 to a symbol whose annotation indicates another ISA mode. For absolute
15329 symbols check the ISA bit instead.
15330
15331 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15332 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15333 MIPS symbols and associated with BAL instructions as these instructions
15334 may be converted to JALX by the linker. */
15335
15336 static bfd_boolean
15337 fix_bad_cross_mode_branch_p (fixS *fixP)
15338 {
15339 bfd_boolean absolute_p;
15340 unsigned long opcode;
15341 asection *symsec;
15342 valueT val;
15343 int other;
15344 char *buf;
15345
15346 if (mips_ignore_branch_isa)
15347 return FALSE;
15348
15349 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15350 return FALSE;
15351
15352 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15353 absolute_p = bfd_is_abs_section (symsec);
15354
15355 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15356 other = S_GET_OTHER (fixP->fx_addsy);
15357
15358 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15359 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15360 switch (fixP->fx_r_type)
15361 {
15362 case BFD_RELOC_16_PCREL_S2:
15363 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15364 && opcode != 0x0411);
15365 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15366 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15367 && opcode != 0x4060);
15368 case BFD_RELOC_MIPS_21_PCREL_S2:
15369 case BFD_RELOC_MIPS_26_PCREL_S2:
15370 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15371 case BFD_RELOC_MIPS16_16_PCREL_S1:
15372 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15373 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15374 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15375 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15376 default:
15377 abort ();
15378 }
15379 }
15380
15381 /* Return TRUE if the symbol plus addend associated with a regular MIPS
15382 branch instruction pointed to by FIXP is not aligned according to the
15383 branch instruction's immediate field requirement. We need the addend
15384 to preserve the ISA bit and also the sum must not have bit 2 set. We
15385 must explicitly OR in the ISA bit from symbol annotation as the bit
15386 won't be set in the symbol's value then. */
15387
15388 static bfd_boolean
15389 fix_bad_misaligned_branch_p (fixS *fixP)
15390 {
15391 bfd_boolean absolute_p;
15392 asection *symsec;
15393 valueT isa_bit;
15394 valueT val;
15395 valueT off;
15396 int other;
15397
15398 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15399 return FALSE;
15400
15401 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15402 absolute_p = bfd_is_abs_section (symsec);
15403
15404 val = S_GET_VALUE (fixP->fx_addsy);
15405 other = S_GET_OTHER (fixP->fx_addsy);
15406 off = fixP->fx_offset;
15407
15408 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15409 val |= ELF_ST_IS_COMPRESSED (other);
15410 val += off;
15411 return (val & 0x3) != isa_bit;
15412 }
15413
15414 /* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15415 and its calculated value VAL. */
15416
15417 static void
15418 fix_validate_branch (fixS *fixP, valueT val)
15419 {
15420 if (fixP->fx_done && (val & 0x3) != 0)
15421 as_bad_where (fixP->fx_file, fixP->fx_line,
15422 _("branch to misaligned address (0x%lx)"),
15423 (long) (val + md_pcrel_from (fixP)));
15424 else if (fix_bad_cross_mode_branch_p (fixP))
15425 as_bad_where (fixP->fx_file, fixP->fx_line,
15426 _("branch to a symbol in another ISA mode"));
15427 else if (fix_bad_misaligned_branch_p (fixP))
15428 as_bad_where (fixP->fx_file, fixP->fx_line,
15429 _("branch to misaligned address (0x%lx)"),
15430 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15431 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15432 as_bad_where (fixP->fx_file, fixP->fx_line,
15433 _("cannot encode misaligned addend "
15434 "in the relocatable field (0x%lx)"),
15435 (long) fixP->fx_offset);
15436 }
15437
15438 /* Apply a fixup to the object file. */
15439
15440 void
15441 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15442 {
15443 char *buf;
15444 unsigned long insn;
15445 reloc_howto_type *howto;
15446
15447 if (fixP->fx_pcrel)
15448 switch (fixP->fx_r_type)
15449 {
15450 case BFD_RELOC_16_PCREL_S2:
15451 case BFD_RELOC_MIPS16_16_PCREL_S1:
15452 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15453 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15454 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15455 case BFD_RELOC_32_PCREL:
15456 case BFD_RELOC_MIPS_21_PCREL_S2:
15457 case BFD_RELOC_MIPS_26_PCREL_S2:
15458 case BFD_RELOC_MIPS_18_PCREL_S3:
15459 case BFD_RELOC_MIPS_19_PCREL_S2:
15460 case BFD_RELOC_HI16_S_PCREL:
15461 case BFD_RELOC_LO16_PCREL:
15462 break;
15463
15464 case BFD_RELOC_32:
15465 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15466 break;
15467
15468 default:
15469 as_bad_where (fixP->fx_file, fixP->fx_line,
15470 _("PC-relative reference to a different section"));
15471 break;
15472 }
15473
15474 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15475 that have no MIPS ELF equivalent. */
15476 if (fixP->fx_r_type != BFD_RELOC_8)
15477 {
15478 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15479 if (!howto)
15480 return;
15481 }
15482
15483 gas_assert (fixP->fx_size == 2
15484 || fixP->fx_size == 4
15485 || fixP->fx_r_type == BFD_RELOC_8
15486 || fixP->fx_r_type == BFD_RELOC_16
15487 || fixP->fx_r_type == BFD_RELOC_64
15488 || fixP->fx_r_type == BFD_RELOC_CTOR
15489 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15490 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15491 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15492 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15493 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15494 || fixP->fx_r_type == BFD_RELOC_NONE);
15495
15496 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15497
15498 /* Don't treat parts of a composite relocation as done. There are two
15499 reasons for this:
15500
15501 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15502 should nevertheless be emitted if the first part is.
15503
15504 (2) In normal usage, composite relocations are never assembly-time
15505 constants. The easiest way of dealing with the pathological
15506 exceptions is to generate a relocation against STN_UNDEF and
15507 leave everything up to the linker. */
15508 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15509 fixP->fx_done = 1;
15510
15511 switch (fixP->fx_r_type)
15512 {
15513 case BFD_RELOC_MIPS_TLS_GD:
15514 case BFD_RELOC_MIPS_TLS_LDM:
15515 case BFD_RELOC_MIPS_TLS_DTPREL32:
15516 case BFD_RELOC_MIPS_TLS_DTPREL64:
15517 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15518 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15519 case BFD_RELOC_MIPS_TLS_GOTTPREL:
15520 case BFD_RELOC_MIPS_TLS_TPREL32:
15521 case BFD_RELOC_MIPS_TLS_TPREL64:
15522 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15523 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15524 case BFD_RELOC_MICROMIPS_TLS_GD:
15525 case BFD_RELOC_MICROMIPS_TLS_LDM:
15526 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15527 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15528 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15529 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15530 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15531 case BFD_RELOC_MIPS16_TLS_GD:
15532 case BFD_RELOC_MIPS16_TLS_LDM:
15533 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15534 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15535 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15536 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15537 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15538 if (fixP->fx_addsy)
15539 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15540 else
15541 as_bad_where (fixP->fx_file, fixP->fx_line,
15542 _("TLS relocation against a constant"));
15543 break;
15544
15545 case BFD_RELOC_MIPS_JMP:
15546 case BFD_RELOC_MIPS16_JMP:
15547 case BFD_RELOC_MICROMIPS_JMP:
15548 {
15549 int shift;
15550
15551 gas_assert (!fixP->fx_done);
15552
15553 /* Shift is 2, unusually, for microMIPS JALX. */
15554 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15555 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15556 shift = 1;
15557 else
15558 shift = 2;
15559
15560 if (fix_bad_cross_mode_jump_p (fixP))
15561 as_bad_where (fixP->fx_file, fixP->fx_line,
15562 _("jump to a symbol in another ISA mode"));
15563 else if (fix_bad_same_mode_jalx_p (fixP))
15564 as_bad_where (fixP->fx_file, fixP->fx_line,
15565 _("JALX to a symbol in the same ISA mode"));
15566 else if (fix_bad_misaligned_jump_p (fixP, shift))
15567 as_bad_where (fixP->fx_file, fixP->fx_line,
15568 _("jump to misaligned address (0x%lx)"),
15569 (long) (S_GET_VALUE (fixP->fx_addsy)
15570 + fixP->fx_offset));
15571 else if (HAVE_IN_PLACE_ADDENDS
15572 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15573 as_bad_where (fixP->fx_file, fixP->fx_line,
15574 _("cannot encode misaligned addend "
15575 "in the relocatable field (0x%lx)"),
15576 (long) fixP->fx_offset);
15577 }
15578 /* Fall through. */
15579
15580 case BFD_RELOC_MIPS_SHIFT5:
15581 case BFD_RELOC_MIPS_SHIFT6:
15582 case BFD_RELOC_MIPS_GOT_DISP:
15583 case BFD_RELOC_MIPS_GOT_PAGE:
15584 case BFD_RELOC_MIPS_GOT_OFST:
15585 case BFD_RELOC_MIPS_SUB:
15586 case BFD_RELOC_MIPS_INSERT_A:
15587 case BFD_RELOC_MIPS_INSERT_B:
15588 case BFD_RELOC_MIPS_DELETE:
15589 case BFD_RELOC_MIPS_HIGHEST:
15590 case BFD_RELOC_MIPS_HIGHER:
15591 case BFD_RELOC_MIPS_SCN_DISP:
15592 case BFD_RELOC_MIPS_REL16:
15593 case BFD_RELOC_MIPS_RELGOT:
15594 case BFD_RELOC_MIPS_JALR:
15595 case BFD_RELOC_HI16:
15596 case BFD_RELOC_HI16_S:
15597 case BFD_RELOC_LO16:
15598 case BFD_RELOC_GPREL16:
15599 case BFD_RELOC_MIPS_LITERAL:
15600 case BFD_RELOC_MIPS_CALL16:
15601 case BFD_RELOC_MIPS_GOT16:
15602 case BFD_RELOC_GPREL32:
15603 case BFD_RELOC_MIPS_GOT_HI16:
15604 case BFD_RELOC_MIPS_GOT_LO16:
15605 case BFD_RELOC_MIPS_CALL_HI16:
15606 case BFD_RELOC_MIPS_CALL_LO16:
15607 case BFD_RELOC_HI16_S_PCREL:
15608 case BFD_RELOC_LO16_PCREL:
15609 case BFD_RELOC_MIPS16_GPREL:
15610 case BFD_RELOC_MIPS16_GOT16:
15611 case BFD_RELOC_MIPS16_CALL16:
15612 case BFD_RELOC_MIPS16_HI16:
15613 case BFD_RELOC_MIPS16_HI16_S:
15614 case BFD_RELOC_MIPS16_LO16:
15615 case BFD_RELOC_MICROMIPS_GOT_DISP:
15616 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15617 case BFD_RELOC_MICROMIPS_GOT_OFST:
15618 case BFD_RELOC_MICROMIPS_SUB:
15619 case BFD_RELOC_MICROMIPS_HIGHEST:
15620 case BFD_RELOC_MICROMIPS_HIGHER:
15621 case BFD_RELOC_MICROMIPS_SCN_DISP:
15622 case BFD_RELOC_MICROMIPS_JALR:
15623 case BFD_RELOC_MICROMIPS_HI16:
15624 case BFD_RELOC_MICROMIPS_HI16_S:
15625 case BFD_RELOC_MICROMIPS_LO16:
15626 case BFD_RELOC_MICROMIPS_GPREL16:
15627 case BFD_RELOC_MICROMIPS_LITERAL:
15628 case BFD_RELOC_MICROMIPS_CALL16:
15629 case BFD_RELOC_MICROMIPS_GOT16:
15630 case BFD_RELOC_MICROMIPS_GOT_HI16:
15631 case BFD_RELOC_MICROMIPS_GOT_LO16:
15632 case BFD_RELOC_MICROMIPS_CALL_HI16:
15633 case BFD_RELOC_MICROMIPS_CALL_LO16:
15634 case BFD_RELOC_MIPS_EH:
15635 if (fixP->fx_done)
15636 {
15637 offsetT value;
15638
15639 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15640 {
15641 insn = read_reloc_insn (buf, fixP->fx_r_type);
15642 if (mips16_reloc_p (fixP->fx_r_type))
15643 insn |= mips16_immed_extend (value, 16);
15644 else
15645 insn |= (value & 0xffff);
15646 write_reloc_insn (buf, fixP->fx_r_type, insn);
15647 }
15648 else
15649 as_bad_where (fixP->fx_file, fixP->fx_line,
15650 _("unsupported constant in relocation"));
15651 }
15652 break;
15653
15654 case BFD_RELOC_64:
15655 /* This is handled like BFD_RELOC_32, but we output a sign
15656 extended value if we are only 32 bits. */
15657 if (fixP->fx_done)
15658 {
15659 if (8 <= sizeof (valueT))
15660 md_number_to_chars (buf, *valP, 8);
15661 else
15662 {
15663 valueT hiv;
15664
15665 if ((*valP & 0x80000000) != 0)
15666 hiv = 0xffffffff;
15667 else
15668 hiv = 0;
15669 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15670 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
15671 }
15672 }
15673 break;
15674
15675 case BFD_RELOC_RVA:
15676 case BFD_RELOC_32:
15677 case BFD_RELOC_32_PCREL:
15678 case BFD_RELOC_16:
15679 case BFD_RELOC_8:
15680 /* If we are deleting this reloc entry, we must fill in the
15681 value now. This can happen if we have a .word which is not
15682 resolved when it appears but is later defined. */
15683 if (fixP->fx_done)
15684 md_number_to_chars (buf, *valP, fixP->fx_size);
15685 break;
15686
15687 case BFD_RELOC_MIPS_21_PCREL_S2:
15688 fix_validate_branch (fixP, *valP);
15689 if (!fixP->fx_done)
15690 break;
15691
15692 if (*valP + 0x400000 <= 0x7fffff)
15693 {
15694 insn = read_insn (buf);
15695 insn |= (*valP >> 2) & 0x1fffff;
15696 write_insn (buf, insn);
15697 }
15698 else
15699 as_bad_where (fixP->fx_file, fixP->fx_line,
15700 _("branch out of range"));
15701 break;
15702
15703 case BFD_RELOC_MIPS_26_PCREL_S2:
15704 fix_validate_branch (fixP, *valP);
15705 if (!fixP->fx_done)
15706 break;
15707
15708 if (*valP + 0x8000000 <= 0xfffffff)
15709 {
15710 insn = read_insn (buf);
15711 insn |= (*valP >> 2) & 0x3ffffff;
15712 write_insn (buf, insn);
15713 }
15714 else
15715 as_bad_where (fixP->fx_file, fixP->fx_line,
15716 _("branch out of range"));
15717 break;
15718
15719 case BFD_RELOC_MIPS_18_PCREL_S3:
15720 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
15721 as_bad_where (fixP->fx_file, fixP->fx_line,
15722 _("PC-relative access using misaligned symbol (%lx)"),
15723 (long) S_GET_VALUE (fixP->fx_addsy));
15724 if ((fixP->fx_offset & 0x7) != 0)
15725 as_bad_where (fixP->fx_file, fixP->fx_line,
15726 _("PC-relative access using misaligned offset (%lx)"),
15727 (long) fixP->fx_offset);
15728 if (!fixP->fx_done)
15729 break;
15730
15731 if (*valP + 0x100000 <= 0x1fffff)
15732 {
15733 insn = read_insn (buf);
15734 insn |= (*valP >> 3) & 0x3ffff;
15735 write_insn (buf, insn);
15736 }
15737 else
15738 as_bad_where (fixP->fx_file, fixP->fx_line,
15739 _("PC-relative access out of range"));
15740 break;
15741
15742 case BFD_RELOC_MIPS_19_PCREL_S2:
15743 if ((*valP & 0x3) != 0)
15744 as_bad_where (fixP->fx_file, fixP->fx_line,
15745 _("PC-relative access to misaligned address (%lx)"),
15746 (long) *valP);
15747 if (!fixP->fx_done)
15748 break;
15749
15750 if (*valP + 0x100000 <= 0x1fffff)
15751 {
15752 insn = read_insn (buf);
15753 insn |= (*valP >> 2) & 0x7ffff;
15754 write_insn (buf, insn);
15755 }
15756 else
15757 as_bad_where (fixP->fx_file, fixP->fx_line,
15758 _("PC-relative access out of range"));
15759 break;
15760
15761 case BFD_RELOC_16_PCREL_S2:
15762 fix_validate_branch (fixP, *valP);
15763
15764 /* We need to save the bits in the instruction since fixup_segment()
15765 might be deleting the relocation entry (i.e., a branch within
15766 the current segment). */
15767 if (! fixP->fx_done)
15768 break;
15769
15770 /* Update old instruction data. */
15771 insn = read_insn (buf);
15772
15773 if (*valP + 0x20000 <= 0x3ffff)
15774 {
15775 insn |= (*valP >> 2) & 0xffff;
15776 write_insn (buf, insn);
15777 }
15778 else if (fixP->fx_tcbit2
15779 && fixP->fx_done
15780 && fixP->fx_frag->fr_address >= text_section->vma
15781 && (fixP->fx_frag->fr_address
15782 < text_section->vma + bfd_get_section_size (text_section))
15783 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15784 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15785 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15786 {
15787 /* The branch offset is too large. If this is an
15788 unconditional branch, and we are not generating PIC code,
15789 we can convert it to an absolute jump instruction. */
15790 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15791 insn = 0x0c000000; /* jal */
15792 else
15793 insn = 0x08000000; /* j */
15794 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15795 fixP->fx_done = 0;
15796 fixP->fx_addsy = section_symbol (text_section);
15797 *valP += md_pcrel_from (fixP);
15798 write_insn (buf, insn);
15799 }
15800 else
15801 {
15802 /* If we got here, we have branch-relaxation disabled,
15803 and there's nothing we can do to fix this instruction
15804 without turning it into a longer sequence. */
15805 as_bad_where (fixP->fx_file, fixP->fx_line,
15806 _("branch out of range"));
15807 }
15808 break;
15809
15810 case BFD_RELOC_MIPS16_16_PCREL_S1:
15811 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15812 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15813 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15814 gas_assert (!fixP->fx_done);
15815 if (fix_bad_cross_mode_branch_p (fixP))
15816 as_bad_where (fixP->fx_file, fixP->fx_line,
15817 _("branch to a symbol in another ISA mode"));
15818 else if (fixP->fx_addsy
15819 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15820 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15821 && (fixP->fx_offset & 0x1) != 0)
15822 as_bad_where (fixP->fx_file, fixP->fx_line,
15823 _("branch to misaligned address (0x%lx)"),
15824 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15825 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15826 as_bad_where (fixP->fx_file, fixP->fx_line,
15827 _("cannot encode misaligned addend "
15828 "in the relocatable field (0x%lx)"),
15829 (long) fixP->fx_offset);
15830 break;
15831
15832 case BFD_RELOC_VTABLE_INHERIT:
15833 fixP->fx_done = 0;
15834 if (fixP->fx_addsy
15835 && !S_IS_DEFINED (fixP->fx_addsy)
15836 && !S_IS_WEAK (fixP->fx_addsy))
15837 S_SET_WEAK (fixP->fx_addsy);
15838 break;
15839
15840 case BFD_RELOC_NONE:
15841 case BFD_RELOC_VTABLE_ENTRY:
15842 fixP->fx_done = 0;
15843 break;
15844
15845 default:
15846 abort ();
15847 }
15848
15849 /* Remember value for tc_gen_reloc. */
15850 fixP->fx_addnumber = *valP;
15851 }
15852
15853 static symbolS *
15854 get_symbol (void)
15855 {
15856 int c;
15857 char *name;
15858 symbolS *p;
15859
15860 c = get_symbol_name (&name);
15861 p = (symbolS *) symbol_find_or_make (name);
15862 (void) restore_line_pointer (c);
15863 return p;
15864 }
15865
15866 /* Align the current frag to a given power of two. If a particular
15867 fill byte should be used, FILL points to an integer that contains
15868 that byte, otherwise FILL is null.
15869
15870 This function used to have the comment:
15871
15872 The MIPS assembler also automatically adjusts any preceding label.
15873
15874 The implementation therefore applied the adjustment to a maximum of
15875 one label. However, other label adjustments are applied to batches
15876 of labels, and adjusting just one caused problems when new labels
15877 were added for the sake of debugging or unwind information.
15878 We therefore adjust all preceding labels (given as LABELS) instead. */
15879
15880 static void
15881 mips_align (int to, int *fill, struct insn_label_list *labels)
15882 {
15883 mips_emit_delays ();
15884 mips_record_compressed_mode ();
15885 if (fill == NULL && subseg_text_p (now_seg))
15886 frag_align_code (to, 0);
15887 else
15888 frag_align (to, fill ? *fill : 0, 0);
15889 record_alignment (now_seg, to);
15890 mips_move_labels (labels, FALSE);
15891 }
15892
15893 /* Align to a given power of two. .align 0 turns off the automatic
15894 alignment used by the data creating pseudo-ops. */
15895
15896 static void
15897 s_align (int x ATTRIBUTE_UNUSED)
15898 {
15899 int temp, fill_value, *fill_ptr;
15900 long max_alignment = 28;
15901
15902 /* o Note that the assembler pulls down any immediately preceding label
15903 to the aligned address.
15904 o It's not documented but auto alignment is reinstated by
15905 a .align pseudo instruction.
15906 o Note also that after auto alignment is turned off the mips assembler
15907 issues an error on attempt to assemble an improperly aligned data item.
15908 We don't. */
15909
15910 temp = get_absolute_expression ();
15911 if (temp > max_alignment)
15912 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
15913 else if (temp < 0)
15914 {
15915 as_warn (_("alignment negative, 0 assumed"));
15916 temp = 0;
15917 }
15918 if (*input_line_pointer == ',')
15919 {
15920 ++input_line_pointer;
15921 fill_value = get_absolute_expression ();
15922 fill_ptr = &fill_value;
15923 }
15924 else
15925 fill_ptr = 0;
15926 if (temp)
15927 {
15928 segment_info_type *si = seg_info (now_seg);
15929 struct insn_label_list *l = si->label_list;
15930 /* Auto alignment should be switched on by next section change. */
15931 auto_align = 1;
15932 mips_align (temp, fill_ptr, l);
15933 }
15934 else
15935 {
15936 auto_align = 0;
15937 }
15938
15939 demand_empty_rest_of_line ();
15940 }
15941
15942 static void
15943 s_change_sec (int sec)
15944 {
15945 segT seg;
15946
15947 /* The ELF backend needs to know that we are changing sections, so
15948 that .previous works correctly. We could do something like check
15949 for an obj_section_change_hook macro, but that might be confusing
15950 as it would not be appropriate to use it in the section changing
15951 functions in read.c, since obj-elf.c intercepts those. FIXME:
15952 This should be cleaner, somehow. */
15953 obj_elf_section_change_hook ();
15954
15955 mips_emit_delays ();
15956
15957 switch (sec)
15958 {
15959 case 't':
15960 s_text (0);
15961 break;
15962 case 'd':
15963 s_data (0);
15964 break;
15965 case 'b':
15966 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15967 demand_empty_rest_of_line ();
15968 break;
15969
15970 case 'r':
15971 seg = subseg_new (RDATA_SECTION_NAME,
15972 (subsegT) get_absolute_expression ());
15973 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15974 | SEC_READONLY | SEC_RELOC
15975 | SEC_DATA));
15976 if (strncmp (TARGET_OS, "elf", 3) != 0)
15977 record_alignment (seg, 4);
15978 demand_empty_rest_of_line ();
15979 break;
15980
15981 case 's':
15982 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
15983 bfd_set_section_flags (stdoutput, seg,
15984 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
15985 if (strncmp (TARGET_OS, "elf", 3) != 0)
15986 record_alignment (seg, 4);
15987 demand_empty_rest_of_line ();
15988 break;
15989
15990 case 'B':
15991 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
15992 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15993 if (strncmp (TARGET_OS, "elf", 3) != 0)
15994 record_alignment (seg, 4);
15995 demand_empty_rest_of_line ();
15996 break;
15997 }
15998
15999 auto_align = 1;
16000 }
16001
16002 void
16003 s_change_section (int ignore ATTRIBUTE_UNUSED)
16004 {
16005 char *saved_ilp;
16006 char *section_name;
16007 char c, endc;
16008 char next_c = 0;
16009 int section_type;
16010 int section_flag;
16011 int section_entry_size;
16012 int section_alignment;
16013
16014 saved_ilp = input_line_pointer;
16015 endc = get_symbol_name (&section_name);
16016 c = (endc == '"' ? input_line_pointer[1] : endc);
16017 if (c)
16018 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
16019
16020 /* Do we have .section Name<,"flags">? */
16021 if (c != ',' || (c == ',' && next_c == '"'))
16022 {
16023 /* Just after name is now '\0'. */
16024 (void) restore_line_pointer (endc);
16025 input_line_pointer = saved_ilp;
16026 obj_elf_section (ignore);
16027 return;
16028 }
16029
16030 section_name = xstrdup (section_name);
16031 c = restore_line_pointer (endc);
16032
16033 input_line_pointer++;
16034
16035 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16036 if (c == ',')
16037 section_type = get_absolute_expression ();
16038 else
16039 section_type = 0;
16040
16041 if (*input_line_pointer++ == ',')
16042 section_flag = get_absolute_expression ();
16043 else
16044 section_flag = 0;
16045
16046 if (*input_line_pointer++ == ',')
16047 section_entry_size = get_absolute_expression ();
16048 else
16049 section_entry_size = 0;
16050
16051 if (*input_line_pointer++ == ',')
16052 section_alignment = get_absolute_expression ();
16053 else
16054 section_alignment = 0;
16055
16056 /* FIXME: really ignore? */
16057 (void) section_alignment;
16058
16059 /* When using the generic form of .section (as implemented by obj-elf.c),
16060 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16061 traditionally had to fall back on the more common @progbits instead.
16062
16063 There's nothing really harmful in this, since bfd will correct
16064 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
16065 means that, for backwards compatibility, the special_section entries
16066 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16067
16068 Even so, we shouldn't force users of the MIPS .section syntax to
16069 incorrectly label the sections as SHT_PROGBITS. The best compromise
16070 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16071 generic type-checking code. */
16072 if (section_type == SHT_MIPS_DWARF)
16073 section_type = SHT_PROGBITS;
16074
16075 obj_elf_change_section (section_name, section_type, 0, section_flag,
16076 section_entry_size, 0, 0, 0);
16077
16078 if (now_seg->name != section_name)
16079 free (section_name);
16080 }
16081
16082 void
16083 mips_enable_auto_align (void)
16084 {
16085 auto_align = 1;
16086 }
16087
16088 static void
16089 s_cons (int log_size)
16090 {
16091 segment_info_type *si = seg_info (now_seg);
16092 struct insn_label_list *l = si->label_list;
16093
16094 mips_emit_delays ();
16095 if (log_size > 0 && auto_align)
16096 mips_align (log_size, 0, l);
16097 cons (1 << log_size);
16098 mips_clear_insn_labels ();
16099 }
16100
16101 static void
16102 s_float_cons (int type)
16103 {
16104 segment_info_type *si = seg_info (now_seg);
16105 struct insn_label_list *l = si->label_list;
16106
16107 mips_emit_delays ();
16108
16109 if (auto_align)
16110 {
16111 if (type == 'd')
16112 mips_align (3, 0, l);
16113 else
16114 mips_align (2, 0, l);
16115 }
16116
16117 float_cons (type);
16118 mips_clear_insn_labels ();
16119 }
16120
16121 /* Handle .globl. We need to override it because on Irix 5 you are
16122 permitted to say
16123 .globl foo .text
16124 where foo is an undefined symbol, to mean that foo should be
16125 considered to be the address of a function. */
16126
16127 static void
16128 s_mips_globl (int x ATTRIBUTE_UNUSED)
16129 {
16130 char *name;
16131 int c;
16132 symbolS *symbolP;
16133 flagword flag;
16134
16135 do
16136 {
16137 c = get_symbol_name (&name);
16138 symbolP = symbol_find_or_make (name);
16139 S_SET_EXTERNAL (symbolP);
16140
16141 *input_line_pointer = c;
16142 SKIP_WHITESPACE_AFTER_NAME ();
16143
16144 /* On Irix 5, every global symbol that is not explicitly labelled as
16145 being a function is apparently labelled as being an object. */
16146 flag = BSF_OBJECT;
16147
16148 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16149 && (*input_line_pointer != ','))
16150 {
16151 char *secname;
16152 asection *sec;
16153
16154 c = get_symbol_name (&secname);
16155 sec = bfd_get_section_by_name (stdoutput, secname);
16156 if (sec == NULL)
16157 as_bad (_("%s: no such section"), secname);
16158 (void) restore_line_pointer (c);
16159
16160 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16161 flag = BSF_FUNCTION;
16162 }
16163
16164 symbol_get_bfdsym (symbolP)->flags |= flag;
16165
16166 c = *input_line_pointer;
16167 if (c == ',')
16168 {
16169 input_line_pointer++;
16170 SKIP_WHITESPACE ();
16171 if (is_end_of_line[(unsigned char) *input_line_pointer])
16172 c = '\n';
16173 }
16174 }
16175 while (c == ',');
16176
16177 demand_empty_rest_of_line ();
16178 }
16179
16180 static void
16181 s_option (int x ATTRIBUTE_UNUSED)
16182 {
16183 char *opt;
16184 char c;
16185
16186 c = get_symbol_name (&opt);
16187
16188 if (*opt == 'O')
16189 {
16190 /* FIXME: What does this mean? */
16191 }
16192 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
16193 {
16194 int i;
16195
16196 i = atoi (opt + 3);
16197 if (i != 0 && i != 2)
16198 as_bad (_(".option pic%d not supported"), i);
16199 else if (mips_pic == VXWORKS_PIC)
16200 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16201 else if (i == 0)
16202 mips_pic = NO_PIC;
16203 else if (i == 2)
16204 {
16205 mips_pic = SVR4_PIC;
16206 mips_abicalls = TRUE;
16207 }
16208
16209 if (mips_pic == SVR4_PIC)
16210 {
16211 if (g_switch_seen && g_switch_value != 0)
16212 as_warn (_("-G may not be used with SVR4 PIC code"));
16213 g_switch_value = 0;
16214 bfd_set_gp_size (stdoutput, 0);
16215 }
16216 }
16217 else
16218 as_warn (_("unrecognized option \"%s\""), opt);
16219
16220 (void) restore_line_pointer (c);
16221 demand_empty_rest_of_line ();
16222 }
16223
16224 /* This structure is used to hold a stack of .set values. */
16225
16226 struct mips_option_stack
16227 {
16228 struct mips_option_stack *next;
16229 struct mips_set_options options;
16230 };
16231
16232 static struct mips_option_stack *mips_opts_stack;
16233
16234 /* Return status for .set/.module option handling. */
16235
16236 enum code_option_type
16237 {
16238 /* Unrecognized option. */
16239 OPTION_TYPE_BAD = -1,
16240
16241 /* Ordinary option. */
16242 OPTION_TYPE_NORMAL,
16243
16244 /* ISA changing option. */
16245 OPTION_TYPE_ISA
16246 };
16247
16248 /* Handle common .set/.module options. Return status indicating option
16249 type. */
16250
16251 static enum code_option_type
16252 parse_code_option (char * name)
16253 {
16254 bfd_boolean isa_set = FALSE;
16255 const struct mips_ase *ase;
16256
16257 if (strncmp (name, "at=", 3) == 0)
16258 {
16259 char *s = name + 3;
16260
16261 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16262 as_bad (_("unrecognized register name `%s'"), s);
16263 }
16264 else if (strcmp (name, "at") == 0)
16265 mips_opts.at = ATREG;
16266 else if (strcmp (name, "noat") == 0)
16267 mips_opts.at = ZERO;
16268 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16269 mips_opts.nomove = 0;
16270 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16271 mips_opts.nomove = 1;
16272 else if (strcmp (name, "bopt") == 0)
16273 mips_opts.nobopt = 0;
16274 else if (strcmp (name, "nobopt") == 0)
16275 mips_opts.nobopt = 1;
16276 else if (strcmp (name, "gp=32") == 0)
16277 mips_opts.gp = 32;
16278 else if (strcmp (name, "gp=64") == 0)
16279 mips_opts.gp = 64;
16280 else if (strcmp (name, "fp=32") == 0)
16281 mips_opts.fp = 32;
16282 else if (strcmp (name, "fp=xx") == 0)
16283 mips_opts.fp = 0;
16284 else if (strcmp (name, "fp=64") == 0)
16285 mips_opts.fp = 64;
16286 else if (strcmp (name, "softfloat") == 0)
16287 mips_opts.soft_float = 1;
16288 else if (strcmp (name, "hardfloat") == 0)
16289 mips_opts.soft_float = 0;
16290 else if (strcmp (name, "singlefloat") == 0)
16291 mips_opts.single_float = 1;
16292 else if (strcmp (name, "doublefloat") == 0)
16293 mips_opts.single_float = 0;
16294 else if (strcmp (name, "nooddspreg") == 0)
16295 mips_opts.oddspreg = 0;
16296 else if (strcmp (name, "oddspreg") == 0)
16297 mips_opts.oddspreg = 1;
16298 else if (strcmp (name, "mips16") == 0
16299 || strcmp (name, "MIPS-16") == 0)
16300 mips_opts.mips16 = 1;
16301 else if (strcmp (name, "nomips16") == 0
16302 || strcmp (name, "noMIPS-16") == 0)
16303 mips_opts.mips16 = 0;
16304 else if (strcmp (name, "micromips") == 0)
16305 mips_opts.micromips = 1;
16306 else if (strcmp (name, "nomicromips") == 0)
16307 mips_opts.micromips = 0;
16308 else if (name[0] == 'n'
16309 && name[1] == 'o'
16310 && (ase = mips_lookup_ase (name + 2)))
16311 mips_set_ase (ase, &mips_opts, FALSE);
16312 else if ((ase = mips_lookup_ase (name)))
16313 mips_set_ase (ase, &mips_opts, TRUE);
16314 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16315 {
16316 /* Permit the user to change the ISA and architecture on the fly.
16317 Needless to say, misuse can cause serious problems. */
16318 if (strncmp (name, "arch=", 5) == 0)
16319 {
16320 const struct mips_cpu_info *p;
16321
16322 p = mips_parse_cpu ("internal use", name + 5);
16323 if (!p)
16324 as_bad (_("unknown architecture %s"), name + 5);
16325 else
16326 {
16327 mips_opts.arch = p->cpu;
16328 mips_opts.isa = p->isa;
16329 isa_set = TRUE;
16330 }
16331 }
16332 else if (strncmp (name, "mips", 4) == 0)
16333 {
16334 const struct mips_cpu_info *p;
16335
16336 p = mips_parse_cpu ("internal use", name);
16337 if (!p)
16338 as_bad (_("unknown ISA level %s"), name + 4);
16339 else
16340 {
16341 mips_opts.arch = p->cpu;
16342 mips_opts.isa = p->isa;
16343 isa_set = TRUE;
16344 }
16345 }
16346 else
16347 as_bad (_("unknown ISA or architecture %s"), name);
16348 }
16349 else if (strcmp (name, "autoextend") == 0)
16350 mips_opts.noautoextend = 0;
16351 else if (strcmp (name, "noautoextend") == 0)
16352 mips_opts.noautoextend = 1;
16353 else if (strcmp (name, "insn32") == 0)
16354 mips_opts.insn32 = TRUE;
16355 else if (strcmp (name, "noinsn32") == 0)
16356 mips_opts.insn32 = FALSE;
16357 else if (strcmp (name, "sym32") == 0)
16358 mips_opts.sym32 = TRUE;
16359 else if (strcmp (name, "nosym32") == 0)
16360 mips_opts.sym32 = FALSE;
16361 else
16362 return OPTION_TYPE_BAD;
16363
16364 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
16365 }
16366
16367 /* Handle the .set pseudo-op. */
16368
16369 static void
16370 s_mipsset (int x ATTRIBUTE_UNUSED)
16371 {
16372 enum code_option_type type = OPTION_TYPE_NORMAL;
16373 char *name = input_line_pointer, ch;
16374
16375 file_mips_check_options ();
16376
16377 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16378 ++input_line_pointer;
16379 ch = *input_line_pointer;
16380 *input_line_pointer = '\0';
16381
16382 if (strchr (name, ','))
16383 {
16384 /* Generic ".set" directive; use the generic handler. */
16385 *input_line_pointer = ch;
16386 input_line_pointer = name;
16387 s_set (0);
16388 return;
16389 }
16390
16391 if (strcmp (name, "reorder") == 0)
16392 {
16393 if (mips_opts.noreorder)
16394 end_noreorder ();
16395 }
16396 else if (strcmp (name, "noreorder") == 0)
16397 {
16398 if (!mips_opts.noreorder)
16399 start_noreorder ();
16400 }
16401 else if (strcmp (name, "macro") == 0)
16402 mips_opts.warn_about_macros = 0;
16403 else if (strcmp (name, "nomacro") == 0)
16404 {
16405 if (mips_opts.noreorder == 0)
16406 as_bad (_("`noreorder' must be set before `nomacro'"));
16407 mips_opts.warn_about_macros = 1;
16408 }
16409 else if (strcmp (name, "gp=default") == 0)
16410 mips_opts.gp = file_mips_opts.gp;
16411 else if (strcmp (name, "fp=default") == 0)
16412 mips_opts.fp = file_mips_opts.fp;
16413 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16414 {
16415 mips_opts.isa = file_mips_opts.isa;
16416 mips_opts.arch = file_mips_opts.arch;
16417 mips_opts.gp = file_mips_opts.gp;
16418 mips_opts.fp = file_mips_opts.fp;
16419 }
16420 else if (strcmp (name, "push") == 0)
16421 {
16422 struct mips_option_stack *s;
16423
16424 s = XNEW (struct mips_option_stack);
16425 s->next = mips_opts_stack;
16426 s->options = mips_opts;
16427 mips_opts_stack = s;
16428 }
16429 else if (strcmp (name, "pop") == 0)
16430 {
16431 struct mips_option_stack *s;
16432
16433 s = mips_opts_stack;
16434 if (s == NULL)
16435 as_bad (_(".set pop with no .set push"));
16436 else
16437 {
16438 /* If we're changing the reorder mode we need to handle
16439 delay slots correctly. */
16440 if (s->options.noreorder && ! mips_opts.noreorder)
16441 start_noreorder ();
16442 else if (! s->options.noreorder && mips_opts.noreorder)
16443 end_noreorder ();
16444
16445 mips_opts = s->options;
16446 mips_opts_stack = s->next;
16447 free (s);
16448 }
16449 }
16450 else
16451 {
16452 type = parse_code_option (name);
16453 if (type == OPTION_TYPE_BAD)
16454 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16455 }
16456
16457 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16458 registers based on what is supported by the arch/cpu. */
16459 if (type == OPTION_TYPE_ISA)
16460 {
16461 switch (mips_opts.isa)
16462 {
16463 case 0:
16464 break;
16465 case ISA_MIPS1:
16466 /* MIPS I cannot support FPXX. */
16467 mips_opts.fp = 32;
16468 /* fall-through. */
16469 case ISA_MIPS2:
16470 case ISA_MIPS32:
16471 case ISA_MIPS32R2:
16472 case ISA_MIPS32R3:
16473 case ISA_MIPS32R5:
16474 mips_opts.gp = 32;
16475 if (mips_opts.fp != 0)
16476 mips_opts.fp = 32;
16477 break;
16478 case ISA_MIPS32R6:
16479 mips_opts.gp = 32;
16480 mips_opts.fp = 64;
16481 break;
16482 case ISA_MIPS3:
16483 case ISA_MIPS4:
16484 case ISA_MIPS5:
16485 case ISA_MIPS64:
16486 case ISA_MIPS64R2:
16487 case ISA_MIPS64R3:
16488 case ISA_MIPS64R5:
16489 case ISA_MIPS64R6:
16490 mips_opts.gp = 64;
16491 if (mips_opts.fp != 0)
16492 {
16493 if (mips_opts.arch == CPU_R5900)
16494 mips_opts.fp = 32;
16495 else
16496 mips_opts.fp = 64;
16497 }
16498 break;
16499 default:
16500 as_bad (_("unknown ISA level %s"), name + 4);
16501 break;
16502 }
16503 }
16504
16505 mips_check_options (&mips_opts, FALSE);
16506
16507 mips_check_isa_supports_ases ();
16508 *input_line_pointer = ch;
16509 demand_empty_rest_of_line ();
16510 }
16511
16512 /* Handle the .module pseudo-op. */
16513
16514 static void
16515 s_module (int ignore ATTRIBUTE_UNUSED)
16516 {
16517 char *name = input_line_pointer, ch;
16518
16519 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16520 ++input_line_pointer;
16521 ch = *input_line_pointer;
16522 *input_line_pointer = '\0';
16523
16524 if (!file_mips_opts_checked)
16525 {
16526 if (parse_code_option (name) == OPTION_TYPE_BAD)
16527 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16528
16529 /* Update module level settings from mips_opts. */
16530 file_mips_opts = mips_opts;
16531 }
16532 else
16533 as_bad (_(".module is not permitted after generating code"));
16534
16535 *input_line_pointer = ch;
16536 demand_empty_rest_of_line ();
16537 }
16538
16539 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
16540 .option pic2. It means to generate SVR4 PIC calls. */
16541
16542 static void
16543 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16544 {
16545 mips_pic = SVR4_PIC;
16546 mips_abicalls = TRUE;
16547
16548 if (g_switch_seen && g_switch_value != 0)
16549 as_warn (_("-G may not be used with SVR4 PIC code"));
16550 g_switch_value = 0;
16551
16552 bfd_set_gp_size (stdoutput, 0);
16553 demand_empty_rest_of_line ();
16554 }
16555
16556 /* Handle the .cpload pseudo-op. This is used when generating SVR4
16557 PIC code. It sets the $gp register for the function based on the
16558 function address, which is in the register named in the argument.
16559 This uses a relocation against _gp_disp, which is handled specially
16560 by the linker. The result is:
16561 lui $gp,%hi(_gp_disp)
16562 addiu $gp,$gp,%lo(_gp_disp)
16563 addu $gp,$gp,.cpload argument
16564 The .cpload argument is normally $25 == $t9.
16565
16566 The -mno-shared option changes this to:
16567 lui $gp,%hi(__gnu_local_gp)
16568 addiu $gp,$gp,%lo(__gnu_local_gp)
16569 and the argument is ignored. This saves an instruction, but the
16570 resulting code is not position independent; it uses an absolute
16571 address for __gnu_local_gp. Thus code assembled with -mno-shared
16572 can go into an ordinary executable, but not into a shared library. */
16573
16574 static void
16575 s_cpload (int ignore ATTRIBUTE_UNUSED)
16576 {
16577 expressionS ex;
16578 int reg;
16579 int in_shared;
16580
16581 file_mips_check_options ();
16582
16583 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16584 .cpload is ignored. */
16585 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16586 {
16587 s_ignore (0);
16588 return;
16589 }
16590
16591 if (mips_opts.mips16)
16592 {
16593 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16594 ignore_rest_of_line ();
16595 return;
16596 }
16597
16598 /* .cpload should be in a .set noreorder section. */
16599 if (mips_opts.noreorder == 0)
16600 as_warn (_(".cpload not in noreorder section"));
16601
16602 reg = tc_get_register (0);
16603
16604 /* If we need to produce a 64-bit address, we are better off using
16605 the default instruction sequence. */
16606 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16607
16608 ex.X_op = O_symbol;
16609 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16610 "__gnu_local_gp");
16611 ex.X_op_symbol = NULL;
16612 ex.X_add_number = 0;
16613
16614 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16615 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16616
16617 mips_mark_labels ();
16618 mips_assembling_insn = TRUE;
16619
16620 macro_start ();
16621 macro_build_lui (&ex, mips_gp_register);
16622 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16623 mips_gp_register, BFD_RELOC_LO16);
16624 if (in_shared)
16625 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16626 mips_gp_register, reg);
16627 macro_end ();
16628
16629 mips_assembling_insn = FALSE;
16630 demand_empty_rest_of_line ();
16631 }
16632
16633 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16634 .cpsetup $reg1, offset|$reg2, label
16635
16636 If offset is given, this results in:
16637 sd $gp, offset($sp)
16638 lui $gp, %hi(%neg(%gp_rel(label)))
16639 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16640 daddu $gp, $gp, $reg1
16641
16642 If $reg2 is given, this results in:
16643 or $reg2, $gp, $0
16644 lui $gp, %hi(%neg(%gp_rel(label)))
16645 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16646 daddu $gp, $gp, $reg1
16647 $reg1 is normally $25 == $t9.
16648
16649 The -mno-shared option replaces the last three instructions with
16650 lui $gp,%hi(_gp)
16651 addiu $gp,$gp,%lo(_gp) */
16652
16653 static void
16654 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16655 {
16656 expressionS ex_off;
16657 expressionS ex_sym;
16658 int reg1;
16659
16660 file_mips_check_options ();
16661
16662 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16663 We also need NewABI support. */
16664 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16665 {
16666 s_ignore (0);
16667 return;
16668 }
16669
16670 if (mips_opts.mips16)
16671 {
16672 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16673 ignore_rest_of_line ();
16674 return;
16675 }
16676
16677 reg1 = tc_get_register (0);
16678 SKIP_WHITESPACE ();
16679 if (*input_line_pointer != ',')
16680 {
16681 as_bad (_("missing argument separator ',' for .cpsetup"));
16682 return;
16683 }
16684 else
16685 ++input_line_pointer;
16686 SKIP_WHITESPACE ();
16687 if (*input_line_pointer == '$')
16688 {
16689 mips_cpreturn_register = tc_get_register (0);
16690 mips_cpreturn_offset = -1;
16691 }
16692 else
16693 {
16694 mips_cpreturn_offset = get_absolute_expression ();
16695 mips_cpreturn_register = -1;
16696 }
16697 SKIP_WHITESPACE ();
16698 if (*input_line_pointer != ',')
16699 {
16700 as_bad (_("missing argument separator ',' for .cpsetup"));
16701 return;
16702 }
16703 else
16704 ++input_line_pointer;
16705 SKIP_WHITESPACE ();
16706 expression (&ex_sym);
16707
16708 mips_mark_labels ();
16709 mips_assembling_insn = TRUE;
16710
16711 macro_start ();
16712 if (mips_cpreturn_register == -1)
16713 {
16714 ex_off.X_op = O_constant;
16715 ex_off.X_add_symbol = NULL;
16716 ex_off.X_op_symbol = NULL;
16717 ex_off.X_add_number = mips_cpreturn_offset;
16718
16719 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16720 BFD_RELOC_LO16, SP);
16721 }
16722 else
16723 move_register (mips_cpreturn_register, mips_gp_register);
16724
16725 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16726 {
16727 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16728 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16729 BFD_RELOC_HI16_S);
16730
16731 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16732 mips_gp_register, -1, BFD_RELOC_GPREL16,
16733 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16734
16735 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16736 mips_gp_register, reg1);
16737 }
16738 else
16739 {
16740 expressionS ex;
16741
16742 ex.X_op = O_symbol;
16743 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16744 ex.X_op_symbol = NULL;
16745 ex.X_add_number = 0;
16746
16747 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16748 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16749
16750 macro_build_lui (&ex, mips_gp_register);
16751 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16752 mips_gp_register, BFD_RELOC_LO16);
16753 }
16754
16755 macro_end ();
16756
16757 mips_assembling_insn = FALSE;
16758 demand_empty_rest_of_line ();
16759 }
16760
16761 static void
16762 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16763 {
16764 file_mips_check_options ();
16765
16766 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16767 .cplocal is ignored. */
16768 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16769 {
16770 s_ignore (0);
16771 return;
16772 }
16773
16774 if (mips_opts.mips16)
16775 {
16776 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16777 ignore_rest_of_line ();
16778 return;
16779 }
16780
16781 mips_gp_register = tc_get_register (0);
16782 demand_empty_rest_of_line ();
16783 }
16784
16785 /* Handle the .cprestore pseudo-op. This stores $gp into a given
16786 offset from $sp. The offset is remembered, and after making a PIC
16787 call $gp is restored from that location. */
16788
16789 static void
16790 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16791 {
16792 expressionS ex;
16793
16794 file_mips_check_options ();
16795
16796 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16797 .cprestore is ignored. */
16798 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16799 {
16800 s_ignore (0);
16801 return;
16802 }
16803
16804 if (mips_opts.mips16)
16805 {
16806 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16807 ignore_rest_of_line ();
16808 return;
16809 }
16810
16811 mips_cprestore_offset = get_absolute_expression ();
16812 mips_cprestore_valid = 1;
16813
16814 ex.X_op = O_constant;
16815 ex.X_add_symbol = NULL;
16816 ex.X_op_symbol = NULL;
16817 ex.X_add_number = mips_cprestore_offset;
16818
16819 mips_mark_labels ();
16820 mips_assembling_insn = TRUE;
16821
16822 macro_start ();
16823 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16824 SP, HAVE_64BIT_ADDRESSES);
16825 macro_end ();
16826
16827 mips_assembling_insn = FALSE;
16828 demand_empty_rest_of_line ();
16829 }
16830
16831 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16832 was given in the preceding .cpsetup, it results in:
16833 ld $gp, offset($sp)
16834
16835 If a register $reg2 was given there, it results in:
16836 or $gp, $reg2, $0 */
16837
16838 static void
16839 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16840 {
16841 expressionS ex;
16842
16843 file_mips_check_options ();
16844
16845 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16846 We also need NewABI support. */
16847 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16848 {
16849 s_ignore (0);
16850 return;
16851 }
16852
16853 if (mips_opts.mips16)
16854 {
16855 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16856 ignore_rest_of_line ();
16857 return;
16858 }
16859
16860 mips_mark_labels ();
16861 mips_assembling_insn = TRUE;
16862
16863 macro_start ();
16864 if (mips_cpreturn_register == -1)
16865 {
16866 ex.X_op = O_constant;
16867 ex.X_add_symbol = NULL;
16868 ex.X_op_symbol = NULL;
16869 ex.X_add_number = mips_cpreturn_offset;
16870
16871 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16872 }
16873 else
16874 move_register (mips_gp_register, mips_cpreturn_register);
16875
16876 macro_end ();
16877
16878 mips_assembling_insn = FALSE;
16879 demand_empty_rest_of_line ();
16880 }
16881
16882 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16883 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16884 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16885 debug information or MIPS16 TLS. */
16886
16887 static void
16888 s_tls_rel_directive (const size_t bytes, const char *dirstr,
16889 bfd_reloc_code_real_type rtype)
16890 {
16891 expressionS ex;
16892 char *p;
16893
16894 expression (&ex);
16895
16896 if (ex.X_op != O_symbol)
16897 {
16898 as_bad (_("unsupported use of %s"), dirstr);
16899 ignore_rest_of_line ();
16900 }
16901
16902 p = frag_more (bytes);
16903 md_number_to_chars (p, 0, bytes);
16904 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
16905 demand_empty_rest_of_line ();
16906 mips_clear_insn_labels ();
16907 }
16908
16909 /* Handle .dtprelword. */
16910
16911 static void
16912 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16913 {
16914 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
16915 }
16916
16917 /* Handle .dtpreldword. */
16918
16919 static void
16920 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16921 {
16922 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16923 }
16924
16925 /* Handle .tprelword. */
16926
16927 static void
16928 s_tprelword (int ignore ATTRIBUTE_UNUSED)
16929 {
16930 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16931 }
16932
16933 /* Handle .tpreldword. */
16934
16935 static void
16936 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16937 {
16938 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
16939 }
16940
16941 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16942 code. It sets the offset to use in gp_rel relocations. */
16943
16944 static void
16945 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
16946 {
16947 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16948 We also need NewABI support. */
16949 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16950 {
16951 s_ignore (0);
16952 return;
16953 }
16954
16955 mips_gprel_offset = get_absolute_expression ();
16956
16957 demand_empty_rest_of_line ();
16958 }
16959
16960 /* Handle the .gpword pseudo-op. This is used when generating PIC
16961 code. It generates a 32 bit GP relative reloc. */
16962
16963 static void
16964 s_gpword (int ignore ATTRIBUTE_UNUSED)
16965 {
16966 segment_info_type *si;
16967 struct insn_label_list *l;
16968 expressionS ex;
16969 char *p;
16970
16971 /* When not generating PIC code, this is treated as .word. */
16972 if (mips_pic != SVR4_PIC)
16973 {
16974 s_cons (2);
16975 return;
16976 }
16977
16978 si = seg_info (now_seg);
16979 l = si->label_list;
16980 mips_emit_delays ();
16981 if (auto_align)
16982 mips_align (2, 0, l);
16983
16984 expression (&ex);
16985 mips_clear_insn_labels ();
16986
16987 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16988 {
16989 as_bad (_("unsupported use of .gpword"));
16990 ignore_rest_of_line ();
16991 }
16992
16993 p = frag_more (4);
16994 md_number_to_chars (p, 0, 4);
16995 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16996 BFD_RELOC_GPREL32);
16997
16998 demand_empty_rest_of_line ();
16999 }
17000
17001 static void
17002 s_gpdword (int ignore ATTRIBUTE_UNUSED)
17003 {
17004 segment_info_type *si;
17005 struct insn_label_list *l;
17006 expressionS ex;
17007 char *p;
17008
17009 /* When not generating PIC code, this is treated as .dword. */
17010 if (mips_pic != SVR4_PIC)
17011 {
17012 s_cons (3);
17013 return;
17014 }
17015
17016 si = seg_info (now_seg);
17017 l = si->label_list;
17018 mips_emit_delays ();
17019 if (auto_align)
17020 mips_align (3, 0, l);
17021
17022 expression (&ex);
17023 mips_clear_insn_labels ();
17024
17025 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17026 {
17027 as_bad (_("unsupported use of .gpdword"));
17028 ignore_rest_of_line ();
17029 }
17030
17031 p = frag_more (8);
17032 md_number_to_chars (p, 0, 8);
17033 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17034 BFD_RELOC_GPREL32)->fx_tcbit = 1;
17035
17036 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
17037 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17038 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
17039
17040 demand_empty_rest_of_line ();
17041 }
17042
17043 /* Handle the .ehword pseudo-op. This is used when generating unwinding
17044 tables. It generates a R_MIPS_EH reloc. */
17045
17046 static void
17047 s_ehword (int ignore ATTRIBUTE_UNUSED)
17048 {
17049 expressionS ex;
17050 char *p;
17051
17052 mips_emit_delays ();
17053
17054 expression (&ex);
17055 mips_clear_insn_labels ();
17056
17057 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17058 {
17059 as_bad (_("unsupported use of .ehword"));
17060 ignore_rest_of_line ();
17061 }
17062
17063 p = frag_more (4);
17064 md_number_to_chars (p, 0, 4);
17065 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17066 BFD_RELOC_32_PCREL);
17067
17068 demand_empty_rest_of_line ();
17069 }
17070
17071 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
17072 tables in SVR4 PIC code. */
17073
17074 static void
17075 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17076 {
17077 int reg;
17078
17079 file_mips_check_options ();
17080
17081 /* This is ignored when not generating SVR4 PIC code. */
17082 if (mips_pic != SVR4_PIC)
17083 {
17084 s_ignore (0);
17085 return;
17086 }
17087
17088 mips_mark_labels ();
17089 mips_assembling_insn = TRUE;
17090
17091 /* Add $gp to the register named as an argument. */
17092 macro_start ();
17093 reg = tc_get_register (0);
17094 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17095 macro_end ();
17096
17097 mips_assembling_insn = FALSE;
17098 demand_empty_rest_of_line ();
17099 }
17100
17101 /* Handle the .insn pseudo-op. This marks instruction labels in
17102 mips16/micromips mode. This permits the linker to handle them specially,
17103 such as generating jalx instructions when needed. We also make
17104 them odd for the duration of the assembly, in order to generate the
17105 right sort of code. We will make them even in the adjust_symtab
17106 routine, while leaving them marked. This is convenient for the
17107 debugger and the disassembler. The linker knows to make them odd
17108 again. */
17109
17110 static void
17111 s_insn (int ignore ATTRIBUTE_UNUSED)
17112 {
17113 file_mips_check_options ();
17114 file_ase_mips16 |= mips_opts.mips16;
17115 file_ase_micromips |= mips_opts.micromips;
17116
17117 mips_mark_labels ();
17118
17119 demand_empty_rest_of_line ();
17120 }
17121
17122 /* Handle the .nan pseudo-op. */
17123
17124 static void
17125 s_nan (int ignore ATTRIBUTE_UNUSED)
17126 {
17127 static const char str_legacy[] = "legacy";
17128 static const char str_2008[] = "2008";
17129 size_t i;
17130
17131 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17132
17133 if (i == sizeof (str_2008) - 1
17134 && memcmp (input_line_pointer, str_2008, i) == 0)
17135 mips_nan2008 = 1;
17136 else if (i == sizeof (str_legacy) - 1
17137 && memcmp (input_line_pointer, str_legacy, i) == 0)
17138 {
17139 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17140 mips_nan2008 = 0;
17141 else
17142 as_bad (_("`%s' does not support legacy NaN"),
17143 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17144 }
17145 else
17146 as_bad (_("bad .nan directive"));
17147
17148 input_line_pointer += i;
17149 demand_empty_rest_of_line ();
17150 }
17151
17152 /* Handle a .stab[snd] directive. Ideally these directives would be
17153 implemented in a transparent way, so that removing them would not
17154 have any effect on the generated instructions. However, s_stab
17155 internally changes the section, so in practice we need to decide
17156 now whether the preceding label marks compressed code. We do not
17157 support changing the compression mode of a label after a .stab*
17158 directive, such as in:
17159
17160 foo:
17161 .stabs ...
17162 .set mips16
17163
17164 so the current mode wins. */
17165
17166 static void
17167 s_mips_stab (int type)
17168 {
17169 file_mips_check_options ();
17170 mips_mark_labels ();
17171 s_stab (type);
17172 }
17173
17174 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
17175
17176 static void
17177 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17178 {
17179 char *name;
17180 int c;
17181 symbolS *symbolP;
17182 expressionS exp;
17183
17184 c = get_symbol_name (&name);
17185 symbolP = symbol_find_or_make (name);
17186 S_SET_WEAK (symbolP);
17187 *input_line_pointer = c;
17188
17189 SKIP_WHITESPACE_AFTER_NAME ();
17190
17191 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17192 {
17193 if (S_IS_DEFINED (symbolP))
17194 {
17195 as_bad (_("ignoring attempt to redefine symbol %s"),
17196 S_GET_NAME (symbolP));
17197 ignore_rest_of_line ();
17198 return;
17199 }
17200
17201 if (*input_line_pointer == ',')
17202 {
17203 ++input_line_pointer;
17204 SKIP_WHITESPACE ();
17205 }
17206
17207 expression (&exp);
17208 if (exp.X_op != O_symbol)
17209 {
17210 as_bad (_("bad .weakext directive"));
17211 ignore_rest_of_line ();
17212 return;
17213 }
17214 symbol_set_value_expression (symbolP, &exp);
17215 }
17216
17217 demand_empty_rest_of_line ();
17218 }
17219
17220 /* Parse a register string into a number. Called from the ECOFF code
17221 to parse .frame. The argument is non-zero if this is the frame
17222 register, so that we can record it in mips_frame_reg. */
17223
17224 int
17225 tc_get_register (int frame)
17226 {
17227 unsigned int reg;
17228
17229 SKIP_WHITESPACE ();
17230 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17231 reg = 0;
17232 if (frame)
17233 {
17234 mips_frame_reg = reg != 0 ? reg : SP;
17235 mips_frame_reg_valid = 1;
17236 mips_cprestore_valid = 0;
17237 }
17238 return reg;
17239 }
17240
17241 valueT
17242 md_section_align (asection *seg, valueT addr)
17243 {
17244 int align = bfd_get_section_alignment (stdoutput, seg);
17245
17246 /* We don't need to align ELF sections to the full alignment.
17247 However, Irix 5 may prefer that we align them at least to a 16
17248 byte boundary. We don't bother to align the sections if we
17249 are targeted for an embedded system. */
17250 if (strncmp (TARGET_OS, "elf", 3) == 0)
17251 return addr;
17252 if (align > 4)
17253 align = 4;
17254
17255 return ((addr + (1 << align) - 1) & -(1 << align));
17256 }
17257
17258 /* Utility routine, called from above as well. If called while the
17259 input file is still being read, it's only an approximation. (For
17260 example, a symbol may later become defined which appeared to be
17261 undefined earlier.) */
17262
17263 static int
17264 nopic_need_relax (symbolS *sym, int before_relaxing)
17265 {
17266 if (sym == 0)
17267 return 0;
17268
17269 if (g_switch_value > 0)
17270 {
17271 const char *symname;
17272 int change;
17273
17274 /* Find out whether this symbol can be referenced off the $gp
17275 register. It can be if it is smaller than the -G size or if
17276 it is in the .sdata or .sbss section. Certain symbols can
17277 not be referenced off the $gp, although it appears as though
17278 they can. */
17279 symname = S_GET_NAME (sym);
17280 if (symname != (const char *) NULL
17281 && (strcmp (symname, "eprol") == 0
17282 || strcmp (symname, "etext") == 0
17283 || strcmp (symname, "_gp") == 0
17284 || strcmp (symname, "edata") == 0
17285 || strcmp (symname, "_fbss") == 0
17286 || strcmp (symname, "_fdata") == 0
17287 || strcmp (symname, "_ftext") == 0
17288 || strcmp (symname, "end") == 0
17289 || strcmp (symname, "_gp_disp") == 0))
17290 change = 1;
17291 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17292 && (0
17293 #ifndef NO_ECOFF_DEBUGGING
17294 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17295 && (symbol_get_obj (sym)->ecoff_extern_size
17296 <= g_switch_value))
17297 #endif
17298 /* We must defer this decision until after the whole
17299 file has been read, since there might be a .extern
17300 after the first use of this symbol. */
17301 || (before_relaxing
17302 #ifndef NO_ECOFF_DEBUGGING
17303 && symbol_get_obj (sym)->ecoff_extern_size == 0
17304 #endif
17305 && S_GET_VALUE (sym) == 0)
17306 || (S_GET_VALUE (sym) != 0
17307 && S_GET_VALUE (sym) <= g_switch_value)))
17308 change = 0;
17309 else
17310 {
17311 const char *segname;
17312
17313 segname = segment_name (S_GET_SEGMENT (sym));
17314 gas_assert (strcmp (segname, ".lit8") != 0
17315 && strcmp (segname, ".lit4") != 0);
17316 change = (strcmp (segname, ".sdata") != 0
17317 && strcmp (segname, ".sbss") != 0
17318 && strncmp (segname, ".sdata.", 7) != 0
17319 && strncmp (segname, ".sbss.", 6) != 0
17320 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17321 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17322 }
17323 return change;
17324 }
17325 else
17326 /* We are not optimizing for the $gp register. */
17327 return 1;
17328 }
17329
17330
17331 /* Return true if the given symbol should be considered local for SVR4 PIC. */
17332
17333 static bfd_boolean
17334 pic_need_relax (symbolS *sym)
17335 {
17336 asection *symsec;
17337
17338 /* Handle the case of a symbol equated to another symbol. */
17339 while (symbol_equated_reloc_p (sym))
17340 {
17341 symbolS *n;
17342
17343 /* It's possible to get a loop here in a badly written program. */
17344 n = symbol_get_value_expression (sym)->X_add_symbol;
17345 if (n == sym)
17346 break;
17347 sym = n;
17348 }
17349
17350 if (symbol_section_p (sym))
17351 return TRUE;
17352
17353 symsec = S_GET_SEGMENT (sym);
17354
17355 /* This must duplicate the test in adjust_reloc_syms. */
17356 return (!bfd_is_und_section (symsec)
17357 && !bfd_is_abs_section (symsec)
17358 && !bfd_is_com_section (symsec)
17359 /* A global or weak symbol is treated as external. */
17360 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
17361 }
17362 \f
17363 /* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17364 convert a section-relative value VAL to the equivalent PC-relative
17365 value. */
17366
17367 static offsetT
17368 mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17369 offsetT val, long stretch)
17370 {
17371 fragS *sym_frag;
17372 addressT addr;
17373
17374 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17375
17376 sym_frag = symbol_get_frag (fragp->fr_symbol);
17377
17378 /* If the relax_marker of the symbol fragment differs from the
17379 relax_marker of this fragment, we have not yet adjusted the
17380 symbol fragment fr_address. We want to add in STRETCH in
17381 order to get a better estimate of the address. This
17382 particularly matters because of the shift bits. */
17383 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17384 {
17385 fragS *f;
17386
17387 /* Adjust stretch for any alignment frag. Note that if have
17388 been expanding the earlier code, the symbol may be
17389 defined in what appears to be an earlier frag. FIXME:
17390 This doesn't handle the fr_subtype field, which specifies
17391 a maximum number of bytes to skip when doing an
17392 alignment. */
17393 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17394 {
17395 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17396 {
17397 if (stretch < 0)
17398 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17399 else
17400 stretch &= ~((1 << (int) f->fr_offset) - 1);
17401 if (stretch == 0)
17402 break;
17403 }
17404 }
17405 if (f != NULL)
17406 val += stretch;
17407 }
17408
17409 addr = fragp->fr_address + fragp->fr_fix;
17410
17411 /* The base address rules are complicated. The base address of
17412 a branch is the following instruction. The base address of a
17413 PC relative load or add is the instruction itself, but if it
17414 is in a delay slot (in which case it can not be extended) use
17415 the address of the instruction whose delay slot it is in. */
17416 if (pcrel_op->include_isa_bit)
17417 {
17418 addr += 2;
17419
17420 /* If we are currently assuming that this frag should be
17421 extended, then the current address is two bytes higher. */
17422 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17423 addr += 2;
17424
17425 /* Ignore the low bit in the target, since it will be set
17426 for a text label. */
17427 val &= -2;
17428 }
17429 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17430 addr -= 4;
17431 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17432 addr -= 2;
17433
17434 val -= addr & -(1 << pcrel_op->align_log2);
17435
17436 return val;
17437 }
17438
17439 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17440 extended opcode. SEC is the section the frag is in. */
17441
17442 static int
17443 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17444 {
17445 const struct mips_int_operand *operand;
17446 offsetT val;
17447 segT symsec;
17448 int type;
17449
17450 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17451 return 0;
17452 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17453 return 1;
17454
17455 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17456 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17457 operand = mips16_immed_operand (type, FALSE);
17458 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17459 || (operand->root.type == OP_PCREL
17460 ? sec != symsec
17461 : !bfd_is_abs_section (symsec)))
17462 return 1;
17463
17464 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17465
17466 if (operand->root.type == OP_PCREL)
17467 {
17468 const struct mips_pcrel_operand *pcrel_op;
17469 offsetT maxtiny;
17470
17471 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
17472 return 1;
17473
17474 pcrel_op = (const struct mips_pcrel_operand *) operand;
17475 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17476
17477 /* If any of the shifted bits are set, we must use an extended
17478 opcode. If the address depends on the size of this
17479 instruction, this can lead to a loop, so we arrange to always
17480 use an extended opcode. */
17481 if ((val & ((1 << operand->shift) - 1)) != 0)
17482 {
17483 fragp->fr_subtype =
17484 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17485 return 1;
17486 }
17487
17488 /* If we are about to mark a frag as extended because the value
17489 is precisely the next value above maxtiny, then there is a
17490 chance of an infinite loop as in the following code:
17491 la $4,foo
17492 .skip 1020
17493 .align 2
17494 foo:
17495 In this case when the la is extended, foo is 0x3fc bytes
17496 away, so the la can be shrunk, but then foo is 0x400 away, so
17497 the la must be extended. To avoid this loop, we mark the
17498 frag as extended if it was small, and is about to become
17499 extended with the next value above maxtiny. */
17500 maxtiny = mips_int_operand_max (operand);
17501 if (val == maxtiny + (1 << operand->shift)
17502 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17503 {
17504 fragp->fr_subtype =
17505 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17506 return 1;
17507 }
17508 }
17509
17510 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17511 }
17512
17513 /* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17514 macro expansion. SEC is the section the frag is in. We only
17515 support PC-relative instructions (LA, DLA, LW, LD) here, in
17516 non-PIC code using 32-bit addressing. */
17517
17518 static int
17519 mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17520 {
17521 const struct mips_pcrel_operand *pcrel_op;
17522 const struct mips_int_operand *operand;
17523 offsetT val;
17524 segT symsec;
17525 int type;
17526
17527 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17528
17529 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17530 return 0;
17531 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17532 return 0;
17533
17534 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17535 switch (type)
17536 {
17537 case 'A':
17538 case 'B':
17539 case 'E':
17540 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17541 if (bfd_is_abs_section (symsec))
17542 return 1;
17543 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17544 return 0;
17545 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17546 return 1;
17547
17548 operand = mips16_immed_operand (type, TRUE);
17549 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17550 pcrel_op = (const struct mips_pcrel_operand *) operand;
17551 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17552
17553 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17554
17555 default:
17556 return 0;
17557 }
17558 }
17559
17560 /* Compute the length of a branch sequence, and adjust the
17561 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17562 worst-case length is computed, with UPDATE being used to indicate
17563 whether an unconditional (-1), branch-likely (+1) or regular (0)
17564 branch is to be computed. */
17565 static int
17566 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17567 {
17568 bfd_boolean toofar;
17569 int length;
17570
17571 if (fragp
17572 && S_IS_DEFINED (fragp->fr_symbol)
17573 && !S_IS_WEAK (fragp->fr_symbol)
17574 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17575 {
17576 addressT addr;
17577 offsetT val;
17578
17579 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17580
17581 addr = fragp->fr_address + fragp->fr_fix + 4;
17582
17583 val -= addr;
17584
17585 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17586 }
17587 else
17588 /* If the symbol is not defined or it's in a different segment,
17589 we emit the long sequence. */
17590 toofar = TRUE;
17591
17592 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17593 fragp->fr_subtype
17594 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17595 RELAX_BRANCH_PIC (fragp->fr_subtype),
17596 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17597 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17598 RELAX_BRANCH_LINK (fragp->fr_subtype),
17599 toofar);
17600
17601 length = 4;
17602 if (toofar)
17603 {
17604 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17605 length += 8;
17606
17607 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
17608 {
17609 /* Additional space for PIC loading of target address. */
17610 length += 8;
17611 if (mips_opts.isa == ISA_MIPS1)
17612 /* Additional space for $at-stabilizing nop. */
17613 length += 4;
17614 }
17615
17616 /* If branch is conditional. */
17617 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17618 length += 8;
17619 }
17620
17621 return length;
17622 }
17623
17624 /* Get a FRAG's branch instruction delay slot size, either from the
17625 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17626 or SHORT_INSN_SIZE otherwise. */
17627
17628 static int
17629 frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17630 {
17631 char *buf = fragp->fr_literal + fragp->fr_fix;
17632
17633 if (al)
17634 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17635 else
17636 return short_insn_size;
17637 }
17638
17639 /* Compute the length of a branch sequence, and adjust the
17640 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17641 worst-case length is computed, with UPDATE being used to indicate
17642 whether an unconditional (-1), or regular (0) branch is to be
17643 computed. */
17644
17645 static int
17646 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17647 {
17648 bfd_boolean insn32 = TRUE;
17649 bfd_boolean nods = TRUE;
17650 bfd_boolean pic = TRUE;
17651 bfd_boolean al = TRUE;
17652 int short_insn_size;
17653 bfd_boolean toofar;
17654 int length;
17655
17656 if (fragp)
17657 {
17658 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17659 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
17660 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
17661 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17662 }
17663 short_insn_size = insn32 ? 4 : 2;
17664
17665 if (fragp
17666 && S_IS_DEFINED (fragp->fr_symbol)
17667 && !S_IS_WEAK (fragp->fr_symbol)
17668 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17669 {
17670 addressT addr;
17671 offsetT val;
17672
17673 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17674 /* Ignore the low bit in the target, since it will be set
17675 for a text label. */
17676 if ((val & 1) != 0)
17677 --val;
17678
17679 addr = fragp->fr_address + fragp->fr_fix + 4;
17680
17681 val -= addr;
17682
17683 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17684 }
17685 else
17686 /* If the symbol is not defined or it's in a different segment,
17687 we emit the long sequence. */
17688 toofar = TRUE;
17689
17690 if (fragp && update
17691 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17692 fragp->fr_subtype = (toofar
17693 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17694 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17695
17696 length = 4;
17697 if (toofar)
17698 {
17699 bfd_boolean compact_known = fragp != NULL;
17700 bfd_boolean compact = FALSE;
17701 bfd_boolean uncond;
17702
17703 if (fragp)
17704 {
17705 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17706 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17707 }
17708 else
17709 uncond = update < 0;
17710
17711 /* If label is out of range, we turn branch <br>:
17712
17713 <br> label # 4 bytes
17714 0:
17715
17716 into:
17717
17718 j label # 4 bytes
17719 nop # 2/4 bytes if
17720 # compact && (!PIC || insn32)
17721 0:
17722 */
17723 if ((!pic || insn32) && (!compact_known || compact))
17724 length += short_insn_size;
17725
17726 /* If assembling PIC code, we further turn:
17727
17728 j label # 4 bytes
17729
17730 into:
17731
17732 lw/ld at, %got(label)(gp) # 4 bytes
17733 d/addiu at, %lo(label) # 4 bytes
17734 jr/c at # 2/4 bytes
17735 */
17736 if (pic)
17737 length += 4 + short_insn_size;
17738
17739 /* Add an extra nop if the jump has no compact form and we need
17740 to fill the delay slot. */
17741 if ((!pic || al) && nods)
17742 length += (fragp
17743 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
17744 : short_insn_size);
17745
17746 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17747
17748 <brneg> 0f # 4 bytes
17749 nop # 2/4 bytes if !compact
17750 */
17751 if (!uncond)
17752 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
17753 }
17754 else if (nods)
17755 {
17756 /* Add an extra nop to fill the delay slot. */
17757 gas_assert (fragp);
17758 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
17759 }
17760
17761 return length;
17762 }
17763
17764 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17765 bit accordingly. */
17766
17767 static int
17768 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17769 {
17770 bfd_boolean toofar;
17771
17772 if (fragp
17773 && S_IS_DEFINED (fragp->fr_symbol)
17774 && !S_IS_WEAK (fragp->fr_symbol)
17775 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17776 {
17777 addressT addr;
17778 offsetT val;
17779 int type;
17780
17781 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17782 /* Ignore the low bit in the target, since it will be set
17783 for a text label. */
17784 if ((val & 1) != 0)
17785 --val;
17786
17787 /* Assume this is a 2-byte branch. */
17788 addr = fragp->fr_address + fragp->fr_fix + 2;
17789
17790 /* We try to avoid the infinite loop by not adding 2 more bytes for
17791 long branches. */
17792
17793 val -= addr;
17794
17795 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17796 if (type == 'D')
17797 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17798 else if (type == 'E')
17799 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17800 else
17801 abort ();
17802 }
17803 else
17804 /* If the symbol is not defined or it's in a different segment,
17805 we emit a normal 32-bit branch. */
17806 toofar = TRUE;
17807
17808 if (fragp && update
17809 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17810 fragp->fr_subtype
17811 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17812 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17813
17814 if (toofar)
17815 return 4;
17816
17817 return 2;
17818 }
17819
17820 /* Estimate the size of a frag before relaxing. Unless this is the
17821 mips16, we are not really relaxing here, and the final size is
17822 encoded in the subtype information. For the mips16, we have to
17823 decide whether we are using an extended opcode or not. */
17824
17825 int
17826 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17827 {
17828 int change;
17829
17830 if (RELAX_BRANCH_P (fragp->fr_subtype))
17831 {
17832
17833 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17834
17835 return fragp->fr_var;
17836 }
17837
17838 if (RELAX_MIPS16_P (fragp->fr_subtype))
17839 {
17840 /* We don't want to modify the EXTENDED bit here; it might get us
17841 into infinite loops. We change it only in mips_relax_frag(). */
17842 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
17843 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
17844 else
17845 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
17846 }
17847
17848 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17849 {
17850 int length = 4;
17851
17852 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17853 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17854 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17855 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17856 fragp->fr_var = length;
17857
17858 return length;
17859 }
17860
17861 if (mips_pic == VXWORKS_PIC)
17862 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17863 change = 0;
17864 else if (RELAX_PIC (fragp->fr_subtype))
17865 change = pic_need_relax (fragp->fr_symbol);
17866 else
17867 change = nopic_need_relax (fragp->fr_symbol, 0);
17868
17869 if (change)
17870 {
17871 fragp->fr_subtype |= RELAX_USE_SECOND;
17872 return -RELAX_FIRST (fragp->fr_subtype);
17873 }
17874 else
17875 return -RELAX_SECOND (fragp->fr_subtype);
17876 }
17877
17878 /* This is called to see whether a reloc against a defined symbol
17879 should be converted into a reloc against a section. */
17880
17881 int
17882 mips_fix_adjustable (fixS *fixp)
17883 {
17884 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17885 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17886 return 0;
17887
17888 if (fixp->fx_addsy == NULL)
17889 return 1;
17890
17891 /* Allow relocs used for EH tables. */
17892 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17893 return 1;
17894
17895 /* If symbol SYM is in a mergeable section, relocations of the form
17896 SYM + 0 can usually be made section-relative. The mergeable data
17897 is then identified by the section offset rather than by the symbol.
17898
17899 However, if we're generating REL LO16 relocations, the offset is split
17900 between the LO16 and partnering high part relocation. The linker will
17901 need to recalculate the complete offset in order to correctly identify
17902 the merge data.
17903
17904 The linker has traditionally not looked for the partnering high part
17905 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17906 placed anywhere. Rather than break backwards compatibility by changing
17907 this, it seems better not to force the issue, and instead keep the
17908 original symbol. This will work with either linker behavior. */
17909 if ((lo16_reloc_p (fixp->fx_r_type)
17910 || reloc_needs_lo_p (fixp->fx_r_type))
17911 && HAVE_IN_PLACE_ADDENDS
17912 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17913 return 0;
17914
17915 /* There is no place to store an in-place offset for JALR relocations. */
17916 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
17917 return 0;
17918
17919 /* Likewise an in-range offset of limited PC-relative relocations may
17920 overflow the in-place relocatable field if recalculated against the
17921 start address of the symbol's containing section.
17922
17923 Also, PC relative relocations for MIPS R6 need to be symbol rather than
17924 section relative to allow linker relaxations to be performed later on. */
17925 if (limited_pcrel_reloc_p (fixp->fx_r_type)
17926 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
17927 return 0;
17928
17929 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17930 to a floating-point stub. The same is true for non-R_MIPS16_26
17931 relocations against MIPS16 functions; in this case, the stub becomes
17932 the function's canonical address.
17933
17934 Floating-point stubs are stored in unique .mips16.call.* or
17935 .mips16.fn.* sections. If a stub T for function F is in section S,
17936 the first relocation in section S must be against F; this is how the
17937 linker determines the target function. All relocations that might
17938 resolve to T must also be against F. We therefore have the following
17939 restrictions, which are given in an intentionally-redundant way:
17940
17941 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17942 symbols.
17943
17944 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17945 if that stub might be used.
17946
17947 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17948 symbols.
17949
17950 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17951 that stub might be used.
17952
17953 There is a further restriction:
17954
17955 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17956 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
17957 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
17958 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
17959 against MIPS16 or microMIPS symbols because we need to keep the
17960 MIPS16 or microMIPS symbol for the purpose of mode mismatch
17961 detection and JAL or BAL to JALX instruction conversion in the
17962 linker.
17963
17964 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17965 against a MIPS16 symbol. We deal with (5) by additionally leaving
17966 alone any jump and branch relocations against a microMIPS symbol.
17967
17968 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17969 relocation against some symbol R, no relocation against R may be
17970 reduced. (Note that this deals with (2) as well as (1) because
17971 relocations against global symbols will never be reduced on ELF
17972 targets.) This approach is a little simpler than trying to detect
17973 stub sections, and gives the "all or nothing" per-symbol consistency
17974 that we have for MIPS16 symbols. */
17975 if (fixp->fx_subsy == NULL
17976 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
17977 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17978 && (jmp_reloc_p (fixp->fx_r_type)
17979 || b_reloc_p (fixp->fx_r_type)))
17980 || *symbol_get_tc (fixp->fx_addsy)))
17981 return 0;
17982
17983 return 1;
17984 }
17985
17986 /* Translate internal representation of relocation info to BFD target
17987 format. */
17988
17989 arelent **
17990 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
17991 {
17992 static arelent *retval[4];
17993 arelent *reloc;
17994 bfd_reloc_code_real_type code;
17995
17996 memset (retval, 0, sizeof(retval));
17997 reloc = retval[0] = XCNEW (arelent);
17998 reloc->sym_ptr_ptr = XNEW (asymbol *);
17999 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18000 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18001
18002 if (fixp->fx_pcrel)
18003 {
18004 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18005 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
18006 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18007 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
18008 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18009 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18010 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18011 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18012 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18013 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18014 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18015 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
18016
18017 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18018 Relocations want only the symbol offset. */
18019 switch (fixp->fx_r_type)
18020 {
18021 case BFD_RELOC_MIPS_18_PCREL_S3:
18022 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18023 break;
18024 default:
18025 reloc->addend = fixp->fx_addnumber + reloc->address;
18026 break;
18027 }
18028 }
18029 else if (HAVE_IN_PLACE_ADDENDS
18030 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18031 && (read_compressed_insn (fixp->fx_frag->fr_literal
18032 + fixp->fx_where, 4) >> 26) == 0x3c)
18033 {
18034 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18035 addend accordingly. */
18036 reloc->addend = fixp->fx_addnumber >> 1;
18037 }
18038 else
18039 reloc->addend = fixp->fx_addnumber;
18040
18041 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18042 entry to be used in the relocation's section offset. */
18043 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18044 {
18045 reloc->address = reloc->addend;
18046 reloc->addend = 0;
18047 }
18048
18049 code = fixp->fx_r_type;
18050
18051 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
18052 if (reloc->howto == NULL)
18053 {
18054 as_bad_where (fixp->fx_file, fixp->fx_line,
18055 _("cannot represent %s relocation in this object file"
18056 " format"),
18057 bfd_get_reloc_code_name (code));
18058 retval[0] = NULL;
18059 }
18060
18061 return retval;
18062 }
18063
18064 /* Relax a machine dependent frag. This returns the amount by which
18065 the current size of the frag should change. */
18066
18067 int
18068 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
18069 {
18070 if (RELAX_BRANCH_P (fragp->fr_subtype))
18071 {
18072 offsetT old_var = fragp->fr_var;
18073
18074 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
18075
18076 return fragp->fr_var - old_var;
18077 }
18078
18079 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18080 {
18081 offsetT old_var = fragp->fr_var;
18082 offsetT new_var = 4;
18083
18084 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18085 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18086 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18087 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18088 fragp->fr_var = new_var;
18089
18090 return new_var - old_var;
18091 }
18092
18093 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18094 return 0;
18095
18096 if (!mips16_extended_frag (fragp, sec, stretch))
18097 {
18098 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18099 {
18100 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18101 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
18102 }
18103 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18104 {
18105 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18106 return -2;
18107 }
18108 else
18109 return 0;
18110 }
18111 else if (!mips16_macro_frag (fragp, sec, stretch))
18112 {
18113 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18114 {
18115 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18116 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18117 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
18118 }
18119 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18120 {
18121 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18122 return 2;
18123 }
18124 else
18125 return 0;
18126 }
18127 else
18128 {
18129 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18130 return 0;
18131 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18132 {
18133 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18134 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18135 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
18136 }
18137 else
18138 {
18139 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18140 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
18141 }
18142 }
18143
18144 return 0;
18145 }
18146
18147 /* Convert a machine dependent frag. */
18148
18149 void
18150 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
18151 {
18152 if (RELAX_BRANCH_P (fragp->fr_subtype))
18153 {
18154 char *buf;
18155 unsigned long insn;
18156 fixS *fixp;
18157
18158 buf = fragp->fr_literal + fragp->fr_fix;
18159 insn = read_insn (buf);
18160
18161 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18162 {
18163 /* We generate a fixup instead of applying it right now
18164 because, if there are linker relaxations, we're going to
18165 need the relocations. */
18166 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18167 fragp->fr_symbol, fragp->fr_offset,
18168 TRUE, BFD_RELOC_16_PCREL_S2);
18169 fixp->fx_file = fragp->fr_file;
18170 fixp->fx_line = fragp->fr_line;
18171
18172 buf = write_insn (buf, insn);
18173 }
18174 else
18175 {
18176 int i;
18177
18178 as_warn_where (fragp->fr_file, fragp->fr_line,
18179 _("relaxed out-of-range branch into a jump"));
18180
18181 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18182 goto uncond;
18183
18184 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18185 {
18186 /* Reverse the branch. */
18187 switch ((insn >> 28) & 0xf)
18188 {
18189 case 4:
18190 if ((insn & 0xff000000) == 0x47000000
18191 || (insn & 0xff600000) == 0x45600000)
18192 {
18193 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18194 reversed by tweaking bit 23. */
18195 insn ^= 0x00800000;
18196 }
18197 else
18198 {
18199 /* bc[0-3][tf]l? instructions can have the condition
18200 reversed by tweaking a single TF bit, and their
18201 opcodes all have 0x4???????. */
18202 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18203 insn ^= 0x00010000;
18204 }
18205 break;
18206
18207 case 0:
18208 /* bltz 0x04000000 bgez 0x04010000
18209 bltzal 0x04100000 bgezal 0x04110000 */
18210 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18211 insn ^= 0x00010000;
18212 break;
18213
18214 case 1:
18215 /* beq 0x10000000 bne 0x14000000
18216 blez 0x18000000 bgtz 0x1c000000 */
18217 insn ^= 0x04000000;
18218 break;
18219
18220 default:
18221 abort ();
18222 }
18223 }
18224
18225 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18226 {
18227 /* Clear the and-link bit. */
18228 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18229
18230 /* bltzal 0x04100000 bgezal 0x04110000
18231 bltzall 0x04120000 bgezall 0x04130000 */
18232 insn &= ~0x00100000;
18233 }
18234
18235 /* Branch over the branch (if the branch was likely) or the
18236 full jump (not likely case). Compute the offset from the
18237 current instruction to branch to. */
18238 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18239 i = 16;
18240 else
18241 {
18242 /* How many bytes in instructions we've already emitted? */
18243 i = buf - fragp->fr_literal - fragp->fr_fix;
18244 /* How many bytes in instructions from here to the end? */
18245 i = fragp->fr_var - i;
18246 }
18247 /* Convert to instruction count. */
18248 i >>= 2;
18249 /* Branch counts from the next instruction. */
18250 i--;
18251 insn |= i;
18252 /* Branch over the jump. */
18253 buf = write_insn (buf, insn);
18254
18255 /* nop */
18256 buf = write_insn (buf, 0);
18257
18258 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18259 {
18260 /* beql $0, $0, 2f */
18261 insn = 0x50000000;
18262 /* Compute the PC offset from the current instruction to
18263 the end of the variable frag. */
18264 /* How many bytes in instructions we've already emitted? */
18265 i = buf - fragp->fr_literal - fragp->fr_fix;
18266 /* How many bytes in instructions from here to the end? */
18267 i = fragp->fr_var - i;
18268 /* Convert to instruction count. */
18269 i >>= 2;
18270 /* Don't decrement i, because we want to branch over the
18271 delay slot. */
18272 insn |= i;
18273
18274 buf = write_insn (buf, insn);
18275 buf = write_insn (buf, 0);
18276 }
18277
18278 uncond:
18279 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
18280 {
18281 /* j or jal. */
18282 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18283 ? 0x0c000000 : 0x08000000);
18284
18285 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18286 fragp->fr_symbol, fragp->fr_offset,
18287 FALSE, BFD_RELOC_MIPS_JMP);
18288 fixp->fx_file = fragp->fr_file;
18289 fixp->fx_line = fragp->fr_line;
18290
18291 buf = write_insn (buf, insn);
18292 }
18293 else
18294 {
18295 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18296
18297 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
18298 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18299 insn |= at << OP_SH_RT;
18300
18301 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18302 fragp->fr_symbol, fragp->fr_offset,
18303 FALSE, BFD_RELOC_MIPS_GOT16);
18304 fixp->fx_file = fragp->fr_file;
18305 fixp->fx_line = fragp->fr_line;
18306
18307 buf = write_insn (buf, insn);
18308
18309 if (mips_opts.isa == ISA_MIPS1)
18310 /* nop */
18311 buf = write_insn (buf, 0);
18312
18313 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
18314 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18315 insn |= at << OP_SH_RS | at << OP_SH_RT;
18316
18317 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18318 fragp->fr_symbol, fragp->fr_offset,
18319 FALSE, BFD_RELOC_LO16);
18320 fixp->fx_file = fragp->fr_file;
18321 fixp->fx_line = fragp->fr_line;
18322
18323 buf = write_insn (buf, insn);
18324
18325 /* j(al)r $at. */
18326 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18327 insn = 0x0000f809;
18328 else
18329 insn = 0x00000008;
18330 insn |= at << OP_SH_RS;
18331
18332 buf = write_insn (buf, insn);
18333 }
18334 }
18335
18336 fragp->fr_fix += fragp->fr_var;
18337 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18338 return;
18339 }
18340
18341 /* Relax microMIPS branches. */
18342 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18343 {
18344 char *buf = fragp->fr_literal + fragp->fr_fix;
18345 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18346 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18347 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18348 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18349 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18350 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18351 bfd_boolean short_ds;
18352 unsigned long insn;
18353 fixS *fixp;
18354
18355 fragp->fr_fix += fragp->fr_var;
18356
18357 /* Handle 16-bit branches that fit or are forced to fit. */
18358 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18359 {
18360 /* We generate a fixup instead of applying it right now,
18361 because if there is linker relaxation, we're going to
18362 need the relocations. */
18363 switch (type)
18364 {
18365 case 'D':
18366 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18367 fragp->fr_symbol, fragp->fr_offset,
18368 TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18369 break;
18370 case 'E':
18371 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18372 fragp->fr_symbol, fragp->fr_offset,
18373 TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18374 break;
18375 default:
18376 abort ();
18377 }
18378
18379 fixp->fx_file = fragp->fr_file;
18380 fixp->fx_line = fragp->fr_line;
18381
18382 /* These relocations can have an addend that won't fit in
18383 2 octets. */
18384 fixp->fx_no_overflow = 1;
18385
18386 return;
18387 }
18388
18389 /* Handle 32-bit branches that fit or are forced to fit. */
18390 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18391 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18392 {
18393 /* We generate a fixup instead of applying it right now,
18394 because if there is linker relaxation, we're going to
18395 need the relocations. */
18396 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18397 fragp->fr_symbol, fragp->fr_offset,
18398 TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18399 fixp->fx_file = fragp->fr_file;
18400 fixp->fx_line = fragp->fr_line;
18401
18402 if (type == 0)
18403 {
18404 insn = read_compressed_insn (buf, 4);
18405 buf += 4;
18406
18407 if (nods)
18408 {
18409 /* Check the short-delay-slot bit. */
18410 if (!al || (insn & 0x02000000) != 0)
18411 buf = write_compressed_insn (buf, 0x0c00, 2);
18412 else
18413 buf = write_compressed_insn (buf, 0x00000000, 4);
18414 }
18415
18416 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18417 return;
18418 }
18419 }
18420
18421 /* Relax 16-bit branches to 32-bit branches. */
18422 if (type != 0)
18423 {
18424 insn = read_compressed_insn (buf, 2);
18425
18426 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18427 insn = 0x94000000; /* beq */
18428 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18429 {
18430 unsigned long regno;
18431
18432 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18433 regno = micromips_to_32_reg_d_map [regno];
18434 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18435 insn |= regno << MICROMIPSOP_SH_RS;
18436 }
18437 else
18438 abort ();
18439
18440 /* Nothing else to do, just write it out. */
18441 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18442 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18443 {
18444 buf = write_compressed_insn (buf, insn, 4);
18445 if (nods)
18446 buf = write_compressed_insn (buf, 0x0c00, 2);
18447 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18448 return;
18449 }
18450 }
18451 else
18452 insn = read_compressed_insn (buf, 4);
18453
18454 /* Relax 32-bit branches to a sequence of instructions. */
18455 as_warn_where (fragp->fr_file, fragp->fr_line,
18456 _("relaxed out-of-range branch into a jump"));
18457
18458 /* Set the short-delay-slot bit. */
18459 short_ds = !al || (insn & 0x02000000) != 0;
18460
18461 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18462 {
18463 symbolS *l;
18464
18465 /* Reverse the branch. */
18466 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18467 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18468 insn ^= 0x20000000;
18469 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18470 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18471 || (insn & 0xffe00000) == 0x40800000 /* blez */
18472 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18473 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18474 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18475 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18476 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18477 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18478 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18479 insn ^= 0x00400000;
18480 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18481 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18482 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18483 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18484 insn ^= 0x00200000;
18485 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18486 BNZ.df */
18487 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18488 BNZ.V */
18489 insn ^= 0x00800000;
18490 else
18491 abort ();
18492
18493 if (al)
18494 {
18495 /* Clear the and-link and short-delay-slot bits. */
18496 gas_assert ((insn & 0xfda00000) == 0x40200000);
18497
18498 /* bltzal 0x40200000 bgezal 0x40600000 */
18499 /* bltzals 0x42200000 bgezals 0x42600000 */
18500 insn &= ~0x02200000;
18501 }
18502
18503 /* Make a label at the end for use with the branch. */
18504 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18505 micromips_label_inc ();
18506 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18507
18508 /* Refer to it. */
18509 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18510 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18511 fixp->fx_file = fragp->fr_file;
18512 fixp->fx_line = fragp->fr_line;
18513
18514 /* Branch over the jump. */
18515 buf = write_compressed_insn (buf, insn, 4);
18516
18517 if (!compact)
18518 {
18519 /* nop */
18520 if (insn32)
18521 buf = write_compressed_insn (buf, 0x00000000, 4);
18522 else
18523 buf = write_compressed_insn (buf, 0x0c00, 2);
18524 }
18525 }
18526
18527 if (!pic)
18528 {
18529 unsigned long jal = (short_ds || nods
18530 ? 0x74000000 : 0xf4000000); /* jal/s */
18531
18532 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18533 insn = al ? jal : 0xd4000000;
18534
18535 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18536 fragp->fr_symbol, fragp->fr_offset,
18537 FALSE, BFD_RELOC_MICROMIPS_JMP);
18538 fixp->fx_file = fragp->fr_file;
18539 fixp->fx_line = fragp->fr_line;
18540
18541 buf = write_compressed_insn (buf, insn, 4);
18542
18543 if (compact || nods)
18544 {
18545 /* nop */
18546 if (insn32)
18547 buf = write_compressed_insn (buf, 0x00000000, 4);
18548 else
18549 buf = write_compressed_insn (buf, 0x0c00, 2);
18550 }
18551 }
18552 else
18553 {
18554 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18555
18556 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18557 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18558 insn |= at << MICROMIPSOP_SH_RT;
18559
18560 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18561 fragp->fr_symbol, fragp->fr_offset,
18562 FALSE, BFD_RELOC_MICROMIPS_GOT16);
18563 fixp->fx_file = fragp->fr_file;
18564 fixp->fx_line = fragp->fr_line;
18565
18566 buf = write_compressed_insn (buf, insn, 4);
18567
18568 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18569 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18570 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18571
18572 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18573 fragp->fr_symbol, fragp->fr_offset,
18574 FALSE, BFD_RELOC_MICROMIPS_LO16);
18575 fixp->fx_file = fragp->fr_file;
18576 fixp->fx_line = fragp->fr_line;
18577
18578 buf = write_compressed_insn (buf, insn, 4);
18579
18580 if (insn32)
18581 {
18582 /* jr/jalr $at */
18583 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18584 insn |= at << MICROMIPSOP_SH_RS;
18585
18586 buf = write_compressed_insn (buf, insn, 4);
18587
18588 if (compact || nods)
18589 /* nop */
18590 buf = write_compressed_insn (buf, 0x00000000, 4);
18591 }
18592 else
18593 {
18594 /* jr/jrc/jalr/jalrs $at */
18595 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18596 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
18597
18598 insn = al ? jalr : jr;
18599 insn |= at << MICROMIPSOP_SH_MJ;
18600
18601 buf = write_compressed_insn (buf, insn, 2);
18602 if (al && nods)
18603 {
18604 /* nop */
18605 if (short_ds)
18606 buf = write_compressed_insn (buf, 0x0c00, 2);
18607 else
18608 buf = write_compressed_insn (buf, 0x00000000, 4);
18609 }
18610 }
18611 }
18612
18613 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18614 return;
18615 }
18616
18617 if (RELAX_MIPS16_P (fragp->fr_subtype))
18618 {
18619 int type;
18620 const struct mips_int_operand *operand;
18621 offsetT val;
18622 char *buf;
18623 unsigned int user_length;
18624 bfd_boolean need_reloc;
18625 unsigned long insn;
18626 bfd_boolean mac;
18627 bfd_boolean ext;
18628 segT symsec;
18629
18630 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18631 operand = mips16_immed_operand (type, FALSE);
18632
18633 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
18634 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18635 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
18636
18637 symsec = S_GET_SEGMENT (fragp->fr_symbol);
18638 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
18639 || (operand->root.type == OP_PCREL && !mac
18640 ? asec != symsec
18641 : !bfd_is_abs_section (symsec)));
18642
18643 if (operand->root.type == OP_PCREL && !mac)
18644 {
18645 const struct mips_pcrel_operand *pcrel_op;
18646
18647 pcrel_op = (const struct mips_pcrel_operand *) operand;
18648
18649 if (pcrel_op->include_isa_bit && !need_reloc)
18650 {
18651 if (!mips_ignore_branch_isa
18652 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
18653 as_bad_where (fragp->fr_file, fragp->fr_line,
18654 _("branch to a symbol in another ISA mode"));
18655 else if ((fragp->fr_offset & 0x1) != 0)
18656 as_bad_where (fragp->fr_file, fragp->fr_line,
18657 _("branch to misaligned address (0x%lx)"),
18658 (long) val);
18659 }
18660
18661 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
18662
18663 /* Make sure the section winds up with the alignment we have
18664 assumed. */
18665 if (operand->shift > 0)
18666 record_alignment (asec, operand->shift);
18667 }
18668
18669 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18670 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18671 {
18672 if (mac)
18673 as_warn_where (fragp->fr_file, fragp->fr_line,
18674 _("macro instruction expanded into multiple "
18675 "instructions in a branch delay slot"));
18676 else if (ext)
18677 as_warn_where (fragp->fr_file, fragp->fr_line,
18678 _("extended instruction in a branch delay slot"));
18679 }
18680 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
18681 as_warn_where (fragp->fr_file, fragp->fr_line,
18682 _("macro instruction expanded into multiple "
18683 "instructions"));
18684
18685 buf = fragp->fr_literal + fragp->fr_fix;
18686
18687 insn = read_compressed_insn (buf, 2);
18688 if (ext)
18689 insn |= MIPS16_EXTEND;
18690
18691 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18692 user_length = 4;
18693 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18694 user_length = 2;
18695 else
18696 user_length = 0;
18697
18698 if (mac)
18699 {
18700 unsigned long reg;
18701 unsigned long new;
18702 unsigned long op;
18703 bfd_boolean e2;
18704
18705 gas_assert (type == 'A' || type == 'B' || type == 'E');
18706 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
18707
18708 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
18709
18710 if (need_reloc)
18711 {
18712 fixS *fixp;
18713
18714 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
18715
18716 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18717 fragp->fr_symbol, fragp->fr_offset,
18718 FALSE, BFD_RELOC_MIPS16_HI16_S);
18719 fixp->fx_file = fragp->fr_file;
18720 fixp->fx_line = fragp->fr_line;
18721
18722 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
18723 fragp->fr_symbol, fragp->fr_offset,
18724 FALSE, BFD_RELOC_MIPS16_LO16);
18725 fixp->fx_file = fragp->fr_file;
18726 fixp->fx_line = fragp->fr_line;
18727
18728 val = 0;
18729 }
18730
18731 switch (insn & 0xf800)
18732 {
18733 case 0x0800: /* ADDIU */
18734 reg = (insn >> 8) & 0x7;
18735 op = 0xf0004800 | (reg << 8);
18736 break;
18737 case 0xb000: /* LW */
18738 reg = (insn >> 8) & 0x7;
18739 op = 0xf0009800 | (reg << 8) | (reg << 5);
18740 break;
18741 case 0xf800: /* I64 */
18742 reg = (insn >> 5) & 0x7;
18743 switch (insn & 0x0700)
18744 {
18745 case 0x0400: /* LD */
18746 op = 0xf0003800 | (reg << 8) | (reg << 5);
18747 break;
18748 case 0x0600: /* DADDIU */
18749 op = 0xf000fd00 | (reg << 5);
18750 break;
18751 default:
18752 abort ();
18753 }
18754 break;
18755 default:
18756 abort ();
18757 }
18758
18759 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
18760 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
18761 buf = write_compressed_insn (buf, new, 4);
18762 if (!e2)
18763 {
18764 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
18765 buf = write_compressed_insn (buf, new, 4);
18766 }
18767 op |= mips16_immed_extend (val, 16);
18768 buf = write_compressed_insn (buf, op, 4);
18769
18770 fragp->fr_fix += e2 ? 8 : 12;
18771 }
18772 else
18773 {
18774 unsigned int length = ext ? 4 : 2;
18775
18776 if (need_reloc)
18777 {
18778 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
18779 fixS *fixp;
18780
18781 switch (type)
18782 {
18783 case 'p':
18784 case 'q':
18785 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
18786 break;
18787 default:
18788 break;
18789 }
18790 if (mac || reloc == BFD_RELOC_NONE)
18791 as_bad_where (fragp->fr_file, fragp->fr_line,
18792 _("unsupported relocation"));
18793 else if (ext)
18794 {
18795 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18796 fragp->fr_symbol, fragp->fr_offset,
18797 TRUE, reloc);
18798 fixp->fx_file = fragp->fr_file;
18799 fixp->fx_line = fragp->fr_line;
18800 }
18801 else
18802 as_bad_where (fragp->fr_file, fragp->fr_line,
18803 _("invalid unextended operand value"));
18804 }
18805 else
18806 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18807 BFD_RELOC_UNUSED, val, user_length, &insn);
18808
18809 gas_assert (mips16_opcode_length (insn) == length);
18810 write_compressed_insn (buf, insn, length);
18811 fragp->fr_fix += length;
18812 }
18813 }
18814 else
18815 {
18816 relax_substateT subtype = fragp->fr_subtype;
18817 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18818 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18819 int first, second;
18820 fixS *fixp;
18821
18822 first = RELAX_FIRST (subtype);
18823 second = RELAX_SECOND (subtype);
18824 fixp = (fixS *) fragp->fr_opcode;
18825
18826 /* If the delay slot chosen does not match the size of the instruction,
18827 then emit a warning. */
18828 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18829 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18830 {
18831 relax_substateT s;
18832 const char *msg;
18833
18834 s = subtype & (RELAX_DELAY_SLOT_16BIT
18835 | RELAX_DELAY_SLOT_SIZE_FIRST
18836 | RELAX_DELAY_SLOT_SIZE_SECOND);
18837 msg = macro_warning (s);
18838 if (msg != NULL)
18839 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18840 subtype &= ~s;
18841 }
18842
18843 /* Possibly emit a warning if we've chosen the longer option. */
18844 if (use_second == second_longer)
18845 {
18846 relax_substateT s;
18847 const char *msg;
18848
18849 s = (subtype
18850 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18851 msg = macro_warning (s);
18852 if (msg != NULL)
18853 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18854 subtype &= ~s;
18855 }
18856
18857 /* Go through all the fixups for the first sequence. Disable them
18858 (by marking them as done) if we're going to use the second
18859 sequence instead. */
18860 while (fixp
18861 && fixp->fx_frag == fragp
18862 && fixp->fx_where < fragp->fr_fix - second)
18863 {
18864 if (subtype & RELAX_USE_SECOND)
18865 fixp->fx_done = 1;
18866 fixp = fixp->fx_next;
18867 }
18868
18869 /* Go through the fixups for the second sequence. Disable them if
18870 we're going to use the first sequence, otherwise adjust their
18871 addresses to account for the relaxation. */
18872 while (fixp && fixp->fx_frag == fragp)
18873 {
18874 if (subtype & RELAX_USE_SECOND)
18875 fixp->fx_where -= first;
18876 else
18877 fixp->fx_done = 1;
18878 fixp = fixp->fx_next;
18879 }
18880
18881 /* Now modify the frag contents. */
18882 if (subtype & RELAX_USE_SECOND)
18883 {
18884 char *start;
18885
18886 start = fragp->fr_literal + fragp->fr_fix - first - second;
18887 memmove (start, start + first, second);
18888 fragp->fr_fix -= first;
18889 }
18890 else
18891 fragp->fr_fix -= second;
18892 }
18893 }
18894
18895 /* This function is called after the relocs have been generated.
18896 We've been storing mips16 text labels as odd. Here we convert them
18897 back to even for the convenience of the debugger. */
18898
18899 void
18900 mips_frob_file_after_relocs (void)
18901 {
18902 asymbol **syms;
18903 unsigned int count, i;
18904
18905 syms = bfd_get_outsymbols (stdoutput);
18906 count = bfd_get_symcount (stdoutput);
18907 for (i = 0; i < count; i++, syms++)
18908 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18909 && ((*syms)->value & 1) != 0)
18910 {
18911 (*syms)->value &= ~1;
18912 /* If the symbol has an odd size, it was probably computed
18913 incorrectly, so adjust that as well. */
18914 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18915 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18916 }
18917 }
18918
18919 /* This function is called whenever a label is defined, including fake
18920 labels instantiated off the dot special symbol. It is used when
18921 handling branch delays; if a branch has a label, we assume we cannot
18922 move it. This also bumps the value of the symbol by 1 in compressed
18923 code. */
18924
18925 static void
18926 mips_record_label (symbolS *sym)
18927 {
18928 segment_info_type *si = seg_info (now_seg);
18929 struct insn_label_list *l;
18930
18931 if (free_insn_labels == NULL)
18932 l = XNEW (struct insn_label_list);
18933 else
18934 {
18935 l = free_insn_labels;
18936 free_insn_labels = l->next;
18937 }
18938
18939 l->label = sym;
18940 l->next = si->label_list;
18941 si->label_list = l;
18942 }
18943
18944 /* This function is called as tc_frob_label() whenever a label is defined
18945 and adds a DWARF-2 record we only want for true labels. */
18946
18947 void
18948 mips_define_label (symbolS *sym)
18949 {
18950 mips_record_label (sym);
18951 dwarf2_emit_label (sym);
18952 }
18953
18954 /* This function is called by tc_new_dot_label whenever a new dot symbol
18955 is defined. */
18956
18957 void
18958 mips_add_dot_label (symbolS *sym)
18959 {
18960 mips_record_label (sym);
18961 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18962 mips_compressed_mark_label (sym);
18963 }
18964 \f
18965 /* Converting ASE flags from internal to .MIPS.abiflags values. */
18966 static unsigned int
18967 mips_convert_ase_flags (int ase)
18968 {
18969 unsigned int ext_ases = 0;
18970
18971 if (ase & ASE_DSP)
18972 ext_ases |= AFL_ASE_DSP;
18973 if (ase & ASE_DSPR2)
18974 ext_ases |= AFL_ASE_DSPR2;
18975 if (ase & ASE_DSPR3)
18976 ext_ases |= AFL_ASE_DSPR3;
18977 if (ase & ASE_EVA)
18978 ext_ases |= AFL_ASE_EVA;
18979 if (ase & ASE_MCU)
18980 ext_ases |= AFL_ASE_MCU;
18981 if (ase & ASE_MDMX)
18982 ext_ases |= AFL_ASE_MDMX;
18983 if (ase & ASE_MIPS3D)
18984 ext_ases |= AFL_ASE_MIPS3D;
18985 if (ase & ASE_MT)
18986 ext_ases |= AFL_ASE_MT;
18987 if (ase & ASE_SMARTMIPS)
18988 ext_ases |= AFL_ASE_SMARTMIPS;
18989 if (ase & ASE_VIRT)
18990 ext_ases |= AFL_ASE_VIRT;
18991 if (ase & ASE_MSA)
18992 ext_ases |= AFL_ASE_MSA;
18993 if (ase & ASE_XPA)
18994 ext_ases |= AFL_ASE_XPA;
18995 if (ase & ASE_MIPS16E2)
18996 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
18997 if (ase & ASE_CRC)
18998 ext_ases |= AFL_ASE_CRC;
18999 if (ase & ASE_GINV)
19000 ext_ases |= AFL_ASE_GINV;
19001
19002 return ext_ases;
19003 }
19004 /* Some special processing for a MIPS ELF file. */
19005
19006 void
19007 mips_elf_final_processing (void)
19008 {
19009 int fpabi;
19010 Elf_Internal_ABIFlags_v0 flags;
19011
19012 flags.version = 0;
19013 flags.isa_rev = 0;
19014 switch (file_mips_opts.isa)
19015 {
19016 case INSN_ISA1:
19017 flags.isa_level = 1;
19018 break;
19019 case INSN_ISA2:
19020 flags.isa_level = 2;
19021 break;
19022 case INSN_ISA3:
19023 flags.isa_level = 3;
19024 break;
19025 case INSN_ISA4:
19026 flags.isa_level = 4;
19027 break;
19028 case INSN_ISA5:
19029 flags.isa_level = 5;
19030 break;
19031 case INSN_ISA32:
19032 flags.isa_level = 32;
19033 flags.isa_rev = 1;
19034 break;
19035 case INSN_ISA32R2:
19036 flags.isa_level = 32;
19037 flags.isa_rev = 2;
19038 break;
19039 case INSN_ISA32R3:
19040 flags.isa_level = 32;
19041 flags.isa_rev = 3;
19042 break;
19043 case INSN_ISA32R5:
19044 flags.isa_level = 32;
19045 flags.isa_rev = 5;
19046 break;
19047 case INSN_ISA32R6:
19048 flags.isa_level = 32;
19049 flags.isa_rev = 6;
19050 break;
19051 case INSN_ISA64:
19052 flags.isa_level = 64;
19053 flags.isa_rev = 1;
19054 break;
19055 case INSN_ISA64R2:
19056 flags.isa_level = 64;
19057 flags.isa_rev = 2;
19058 break;
19059 case INSN_ISA64R3:
19060 flags.isa_level = 64;
19061 flags.isa_rev = 3;
19062 break;
19063 case INSN_ISA64R5:
19064 flags.isa_level = 64;
19065 flags.isa_rev = 5;
19066 break;
19067 case INSN_ISA64R6:
19068 flags.isa_level = 64;
19069 flags.isa_rev = 6;
19070 break;
19071 }
19072
19073 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19074 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19075 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19076 : (file_mips_opts.fp == 64) ? AFL_REG_64
19077 : AFL_REG_32;
19078 flags.cpr2_size = AFL_REG_NONE;
19079 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19080 Tag_GNU_MIPS_ABI_FP);
19081 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19082 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19083 if (file_ase_mips16)
19084 flags.ases |= AFL_ASE_MIPS16;
19085 if (file_ase_micromips)
19086 flags.ases |= AFL_ASE_MICROMIPS;
19087 flags.flags1 = 0;
19088 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19089 || file_mips_opts.fp == 64)
19090 && file_mips_opts.oddspreg)
19091 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19092 flags.flags2 = 0;
19093
19094 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19095 ((Elf_External_ABIFlags_v0 *)
19096 mips_flags_frag));
19097
19098 /* Write out the register information. */
19099 if (mips_abi != N64_ABI)
19100 {
19101 Elf32_RegInfo s;
19102
19103 s.ri_gprmask = mips_gprmask;
19104 s.ri_cprmask[0] = mips_cprmask[0];
19105 s.ri_cprmask[1] = mips_cprmask[1];
19106 s.ri_cprmask[2] = mips_cprmask[2];
19107 s.ri_cprmask[3] = mips_cprmask[3];
19108 /* The gp_value field is set by the MIPS ELF backend. */
19109
19110 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19111 ((Elf32_External_RegInfo *)
19112 mips_regmask_frag));
19113 }
19114 else
19115 {
19116 Elf64_Internal_RegInfo s;
19117
19118 s.ri_gprmask = mips_gprmask;
19119 s.ri_pad = 0;
19120 s.ri_cprmask[0] = mips_cprmask[0];
19121 s.ri_cprmask[1] = mips_cprmask[1];
19122 s.ri_cprmask[2] = mips_cprmask[2];
19123 s.ri_cprmask[3] = mips_cprmask[3];
19124 /* The gp_value field is set by the MIPS ELF backend. */
19125
19126 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19127 ((Elf64_External_RegInfo *)
19128 mips_regmask_frag));
19129 }
19130
19131 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19132 sort of BFD interface for this. */
19133 if (mips_any_noreorder)
19134 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19135 if (mips_pic != NO_PIC)
19136 {
19137 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
19138 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19139 }
19140 if (mips_abicalls)
19141 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19142
19143 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19144 defined at present; this might need to change in future. */
19145 if (file_ase_mips16)
19146 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
19147 if (file_ase_micromips)
19148 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
19149 if (file_mips_opts.ase & ASE_MDMX)
19150 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
19151
19152 /* Set the MIPS ELF ABI flags. */
19153 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
19154 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
19155 else if (mips_abi == O64_ABI)
19156 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
19157 else if (mips_abi == EABI_ABI)
19158 {
19159 if (file_mips_opts.gp == 64)
19160 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19161 else
19162 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19163 }
19164
19165 /* Nothing to do for N32_ABI or N64_ABI. */
19166
19167 if (mips_32bitmode)
19168 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
19169
19170 if (mips_nan2008 == 1)
19171 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19172
19173 /* 32 bit code with 64 bit FP registers. */
19174 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19175 Tag_GNU_MIPS_ABI_FP);
19176 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
19177 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
19178 }
19179 \f
19180 typedef struct proc {
19181 symbolS *func_sym;
19182 symbolS *func_end_sym;
19183 unsigned long reg_mask;
19184 unsigned long reg_offset;
19185 unsigned long fpreg_mask;
19186 unsigned long fpreg_offset;
19187 unsigned long frame_offset;
19188 unsigned long frame_reg;
19189 unsigned long pc_reg;
19190 } procS;
19191
19192 static procS cur_proc;
19193 static procS *cur_proc_ptr;
19194 static int numprocs;
19195
19196 /* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19197 as "2", and a normal nop as "0". */
19198
19199 #define NOP_OPCODE_MIPS 0
19200 #define NOP_OPCODE_MIPS16 1
19201 #define NOP_OPCODE_MICROMIPS 2
19202
19203 char
19204 mips_nop_opcode (void)
19205 {
19206 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19207 return NOP_OPCODE_MICROMIPS;
19208 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19209 return NOP_OPCODE_MIPS16;
19210 else
19211 return NOP_OPCODE_MIPS;
19212 }
19213
19214 /* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19215 32-bit microMIPS NOPs here (if applicable). */
19216
19217 void
19218 mips_handle_align (fragS *fragp)
19219 {
19220 char nop_opcode;
19221 char *p;
19222 int bytes, size, excess;
19223 valueT opcode;
19224
19225 if (fragp->fr_type != rs_align_code)
19226 return;
19227
19228 p = fragp->fr_literal + fragp->fr_fix;
19229 nop_opcode = *p;
19230 switch (nop_opcode)
19231 {
19232 case NOP_OPCODE_MICROMIPS:
19233 opcode = micromips_nop32_insn.insn_opcode;
19234 size = 4;
19235 break;
19236 case NOP_OPCODE_MIPS16:
19237 opcode = mips16_nop_insn.insn_opcode;
19238 size = 2;
19239 break;
19240 case NOP_OPCODE_MIPS:
19241 default:
19242 opcode = nop_insn.insn_opcode;
19243 size = 4;
19244 break;
19245 }
19246
19247 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19248 excess = bytes % size;
19249
19250 /* Handle the leading part if we're not inserting a whole number of
19251 instructions, and make it the end of the fixed part of the frag.
19252 Try to fit in a short microMIPS NOP if applicable and possible,
19253 and use zeroes otherwise. */
19254 gas_assert (excess < 4);
19255 fragp->fr_fix += excess;
19256 switch (excess)
19257 {
19258 case 3:
19259 *p++ = '\0';
19260 /* Fall through. */
19261 case 2:
19262 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
19263 {
19264 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
19265 break;
19266 }
19267 *p++ = '\0';
19268 /* Fall through. */
19269 case 1:
19270 *p++ = '\0';
19271 /* Fall through. */
19272 case 0:
19273 break;
19274 }
19275
19276 md_number_to_chars (p, opcode, size);
19277 fragp->fr_var = size;
19278 }
19279
19280 static long
19281 get_number (void)
19282 {
19283 int negative = 0;
19284 long val = 0;
19285
19286 if (*input_line_pointer == '-')
19287 {
19288 ++input_line_pointer;
19289 negative = 1;
19290 }
19291 if (!ISDIGIT (*input_line_pointer))
19292 as_bad (_("expected simple number"));
19293 if (input_line_pointer[0] == '0')
19294 {
19295 if (input_line_pointer[1] == 'x')
19296 {
19297 input_line_pointer += 2;
19298 while (ISXDIGIT (*input_line_pointer))
19299 {
19300 val <<= 4;
19301 val |= hex_value (*input_line_pointer++);
19302 }
19303 return negative ? -val : val;
19304 }
19305 else
19306 {
19307 ++input_line_pointer;
19308 while (ISDIGIT (*input_line_pointer))
19309 {
19310 val <<= 3;
19311 val |= *input_line_pointer++ - '0';
19312 }
19313 return negative ? -val : val;
19314 }
19315 }
19316 if (!ISDIGIT (*input_line_pointer))
19317 {
19318 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19319 *input_line_pointer, *input_line_pointer);
19320 as_warn (_("invalid number"));
19321 return -1;
19322 }
19323 while (ISDIGIT (*input_line_pointer))
19324 {
19325 val *= 10;
19326 val += *input_line_pointer++ - '0';
19327 }
19328 return negative ? -val : val;
19329 }
19330
19331 /* The .file directive; just like the usual .file directive, but there
19332 is an initial number which is the ECOFF file index. In the non-ECOFF
19333 case .file implies DWARF-2. */
19334
19335 static void
19336 s_mips_file (int x ATTRIBUTE_UNUSED)
19337 {
19338 static int first_file_directive = 0;
19339
19340 if (ECOFF_DEBUGGING)
19341 {
19342 get_number ();
19343 s_app_file (0);
19344 }
19345 else
19346 {
19347 char *filename;
19348
19349 filename = dwarf2_directive_filename ();
19350
19351 /* Versions of GCC up to 3.1 start files with a ".file"
19352 directive even for stabs output. Make sure that this
19353 ".file" is handled. Note that you need a version of GCC
19354 after 3.1 in order to support DWARF-2 on MIPS. */
19355 if (filename != NULL && ! first_file_directive)
19356 {
19357 (void) new_logical_line (filename, -1);
19358 s_app_file_string (filename, 0);
19359 }
19360 first_file_directive = 1;
19361 }
19362 }
19363
19364 /* The .loc directive, implying DWARF-2. */
19365
19366 static void
19367 s_mips_loc (int x ATTRIBUTE_UNUSED)
19368 {
19369 if (!ECOFF_DEBUGGING)
19370 dwarf2_directive_loc (0);
19371 }
19372
19373 /* The .end directive. */
19374
19375 static void
19376 s_mips_end (int x ATTRIBUTE_UNUSED)
19377 {
19378 symbolS *p;
19379
19380 /* Following functions need their own .frame and .cprestore directives. */
19381 mips_frame_reg_valid = 0;
19382 mips_cprestore_valid = 0;
19383
19384 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19385 {
19386 p = get_symbol ();
19387 demand_empty_rest_of_line ();
19388 }
19389 else
19390 p = NULL;
19391
19392 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19393 as_warn (_(".end not in text section"));
19394
19395 if (!cur_proc_ptr)
19396 {
19397 as_warn (_(".end directive without a preceding .ent directive"));
19398 demand_empty_rest_of_line ();
19399 return;
19400 }
19401
19402 if (p != NULL)
19403 {
19404 gas_assert (S_GET_NAME (p));
19405 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
19406 as_warn (_(".end symbol does not match .ent symbol"));
19407
19408 if (debug_type == DEBUG_STABS)
19409 stabs_generate_asm_endfunc (S_GET_NAME (p),
19410 S_GET_NAME (p));
19411 }
19412 else
19413 as_warn (_(".end directive missing or unknown symbol"));
19414
19415 /* Create an expression to calculate the size of the function. */
19416 if (p && cur_proc_ptr)
19417 {
19418 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19419 expressionS *exp = XNEW (expressionS);
19420
19421 obj->size = exp;
19422 exp->X_op = O_subtract;
19423 exp->X_add_symbol = symbol_temp_new_now ();
19424 exp->X_op_symbol = p;
19425 exp->X_add_number = 0;
19426
19427 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19428 }
19429
19430 #ifdef md_flush_pending_output
19431 md_flush_pending_output ();
19432 #endif
19433
19434 /* Generate a .pdr section. */
19435 if (!ECOFF_DEBUGGING && mips_flag_pdr)
19436 {
19437 segT saved_seg = now_seg;
19438 subsegT saved_subseg = now_subseg;
19439 expressionS exp;
19440 char *fragp;
19441
19442 gas_assert (pdr_seg);
19443 subseg_set (pdr_seg, 0);
19444
19445 /* Write the symbol. */
19446 exp.X_op = O_symbol;
19447 exp.X_add_symbol = p;
19448 exp.X_add_number = 0;
19449 emit_expr (&exp, 4);
19450
19451 fragp = frag_more (7 * 4);
19452
19453 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19454 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19455 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19456 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19457 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19458 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19459 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19460
19461 subseg_set (saved_seg, saved_subseg);
19462 }
19463
19464 cur_proc_ptr = NULL;
19465 }
19466
19467 /* The .aent and .ent directives. */
19468
19469 static void
19470 s_mips_ent (int aent)
19471 {
19472 symbolS *symbolP;
19473
19474 symbolP = get_symbol ();
19475 if (*input_line_pointer == ',')
19476 ++input_line_pointer;
19477 SKIP_WHITESPACE ();
19478 if (ISDIGIT (*input_line_pointer)
19479 || *input_line_pointer == '-')
19480 get_number ();
19481
19482 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19483 as_warn (_(".ent or .aent not in text section"));
19484
19485 if (!aent && cur_proc_ptr)
19486 as_warn (_("missing .end"));
19487
19488 if (!aent)
19489 {
19490 /* This function needs its own .frame and .cprestore directives. */
19491 mips_frame_reg_valid = 0;
19492 mips_cprestore_valid = 0;
19493
19494 cur_proc_ptr = &cur_proc;
19495 memset (cur_proc_ptr, '\0', sizeof (procS));
19496
19497 cur_proc_ptr->func_sym = symbolP;
19498
19499 ++numprocs;
19500
19501 if (debug_type == DEBUG_STABS)
19502 stabs_generate_asm_func (S_GET_NAME (symbolP),
19503 S_GET_NAME (symbolP));
19504 }
19505
19506 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19507
19508 demand_empty_rest_of_line ();
19509 }
19510
19511 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19512 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19513 s_mips_frame is used so that we can set the PDR information correctly.
19514 We can't use the ecoff routines because they make reference to the ecoff
19515 symbol table (in the mdebug section). */
19516
19517 static void
19518 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19519 {
19520 if (ECOFF_DEBUGGING)
19521 s_ignore (ignore);
19522 else
19523 {
19524 long val;
19525
19526 if (cur_proc_ptr == (procS *) NULL)
19527 {
19528 as_warn (_(".frame outside of .ent"));
19529 demand_empty_rest_of_line ();
19530 return;
19531 }
19532
19533 cur_proc_ptr->frame_reg = tc_get_register (1);
19534
19535 SKIP_WHITESPACE ();
19536 if (*input_line_pointer++ != ','
19537 || get_absolute_expression_and_terminator (&val) != ',')
19538 {
19539 as_warn (_("bad .frame directive"));
19540 --input_line_pointer;
19541 demand_empty_rest_of_line ();
19542 return;
19543 }
19544
19545 cur_proc_ptr->frame_offset = val;
19546 cur_proc_ptr->pc_reg = tc_get_register (0);
19547
19548 demand_empty_rest_of_line ();
19549 }
19550 }
19551
19552 /* The .fmask and .mask directives. If the mdebug section is present
19553 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19554 embedded targets, s_mips_mask is used so that we can set the PDR
19555 information correctly. We can't use the ecoff routines because they
19556 make reference to the ecoff symbol table (in the mdebug section). */
19557
19558 static void
19559 s_mips_mask (int reg_type)
19560 {
19561 if (ECOFF_DEBUGGING)
19562 s_ignore (reg_type);
19563 else
19564 {
19565 long mask, off;
19566
19567 if (cur_proc_ptr == (procS *) NULL)
19568 {
19569 as_warn (_(".mask/.fmask outside of .ent"));
19570 demand_empty_rest_of_line ();
19571 return;
19572 }
19573
19574 if (get_absolute_expression_and_terminator (&mask) != ',')
19575 {
19576 as_warn (_("bad .mask/.fmask directive"));
19577 --input_line_pointer;
19578 demand_empty_rest_of_line ();
19579 return;
19580 }
19581
19582 off = get_absolute_expression ();
19583
19584 if (reg_type == 'F')
19585 {
19586 cur_proc_ptr->fpreg_mask = mask;
19587 cur_proc_ptr->fpreg_offset = off;
19588 }
19589 else
19590 {
19591 cur_proc_ptr->reg_mask = mask;
19592 cur_proc_ptr->reg_offset = off;
19593 }
19594
19595 demand_empty_rest_of_line ();
19596 }
19597 }
19598
19599 /* A table describing all the processors gas knows about. Names are
19600 matched in the order listed.
19601
19602 To ease comparison, please keep this table in the same order as
19603 gcc's mips_cpu_info_table[]. */
19604 static const struct mips_cpu_info mips_cpu_info_table[] =
19605 {
19606 /* Entries for generic ISAs */
19607 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19608 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19609 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19610 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19611 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19612 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19613 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19614 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19615 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
19616 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
19617 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19618 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
19619 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19620 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
19621 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
19622
19623 /* MIPS I */
19624 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19625 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19626 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
19627
19628 /* MIPS II */
19629 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
19630
19631 /* MIPS III */
19632 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19633 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19634 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19635 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19636 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19637 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19638 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19639 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19640 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19641 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19642 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19643 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19644 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
19645 /* ST Microelectronics Loongson 2E and 2F cores */
19646 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19647 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
19648
19649 /* MIPS IV */
19650 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19651 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19652 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19653 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19654 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19655 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19656 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19657 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19658 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19659 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19660 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19661 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19662 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19663 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19664 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
19665
19666 /* MIPS 32 */
19667 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19668 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19669 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19670 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19671
19672 /* MIPS 32 Release 2 */
19673 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19674 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19675 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19676 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19677 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19678 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19679 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19680 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19681 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19682 ISA_MIPS32R2, CPU_MIPS32R2 },
19683 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19684 ISA_MIPS32R2, CPU_MIPS32R2 },
19685 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19686 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19687 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19688 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19689 /* Deprecated forms of the above. */
19690 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19691 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19692 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
19693 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19694 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19695 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19696 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19697 /* Deprecated forms of the above. */
19698 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19699 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19700 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
19701 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19702 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19703 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19704 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19705 /* Deprecated forms of the above. */
19706 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19707 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19708 /* 34Kn is a 34kc without DSP. */
19709 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19710 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19711 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19712 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19713 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19714 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19715 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19716 /* Deprecated forms of the above. */
19717 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19718 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19719 /* 1004K cores are multiprocessor versions of the 34K. */
19720 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19721 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19722 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19723 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19724 /* interaptiv is the new name for 1004kf */
19725 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19726 { "interaptiv-mr2", 0,
19727 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
19728 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
19729 /* M5100 family */
19730 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19731 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19732 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
19733 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
19734
19735 /* MIPS 64 */
19736 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19737 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19738 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19739 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19740
19741 /* Broadcom SB-1 CPU core */
19742 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19743 /* Broadcom SB-1A CPU core */
19744 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19745
19746 { "loongson3a", 0, 0, ISA_MIPS64R2, CPU_LOONGSON_3A },
19747
19748 /* MIPS 64 Release 2 */
19749
19750 /* Cavium Networks Octeon CPU core */
19751 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19752 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19753 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
19754 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
19755
19756 /* RMI Xlr */
19757 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
19758
19759 /* Broadcom XLP.
19760 XLP is mostly like XLR, with the prominent exception that it is
19761 MIPS64R2 rather than MIPS64. */
19762 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
19763
19764 /* MIPS 64 Release 6 */
19765 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
19766 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
19767
19768 /* End marker */
19769 { NULL, 0, 0, 0, 0 }
19770 };
19771
19772
19773 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19774 with a final "000" replaced by "k". Ignore case.
19775
19776 Note: this function is shared between GCC and GAS. */
19777
19778 static bfd_boolean
19779 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19780 {
19781 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19782 given++, canonical++;
19783
19784 return ((*given == 0 && *canonical == 0)
19785 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19786 }
19787
19788
19789 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19790 CPU name. We've traditionally allowed a lot of variation here.
19791
19792 Note: this function is shared between GCC and GAS. */
19793
19794 static bfd_boolean
19795 mips_matching_cpu_name_p (const char *canonical, const char *given)
19796 {
19797 /* First see if the name matches exactly, or with a final "000"
19798 turned into "k". */
19799 if (mips_strict_matching_cpu_name_p (canonical, given))
19800 return TRUE;
19801
19802 /* If not, try comparing based on numerical designation alone.
19803 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19804 if (TOLOWER (*given) == 'r')
19805 given++;
19806 if (!ISDIGIT (*given))
19807 return FALSE;
19808
19809 /* Skip over some well-known prefixes in the canonical name,
19810 hoping to find a number there too. */
19811 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19812 canonical += 2;
19813 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19814 canonical += 2;
19815 else if (TOLOWER (canonical[0]) == 'r')
19816 canonical += 1;
19817
19818 return mips_strict_matching_cpu_name_p (canonical, given);
19819 }
19820
19821
19822 /* Parse an option that takes the name of a processor as its argument.
19823 OPTION is the name of the option and CPU_STRING is the argument.
19824 Return the corresponding processor enumeration if the CPU_STRING is
19825 recognized, otherwise report an error and return null.
19826
19827 A similar function exists in GCC. */
19828
19829 static const struct mips_cpu_info *
19830 mips_parse_cpu (const char *option, const char *cpu_string)
19831 {
19832 const struct mips_cpu_info *p;
19833
19834 /* 'from-abi' selects the most compatible architecture for the given
19835 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19836 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19837 version. Look first at the -mgp options, if given, otherwise base
19838 the choice on MIPS_DEFAULT_64BIT.
19839
19840 Treat NO_ABI like the EABIs. One reason to do this is that the
19841 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19842 architecture. This code picks MIPS I for 'mips' and MIPS III for
19843 'mips64', just as we did in the days before 'from-abi'. */
19844 if (strcasecmp (cpu_string, "from-abi") == 0)
19845 {
19846 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19847 return mips_cpu_info_from_isa (ISA_MIPS1);
19848
19849 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19850 return mips_cpu_info_from_isa (ISA_MIPS3);
19851
19852 if (file_mips_opts.gp >= 0)
19853 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
19854 ? ISA_MIPS1 : ISA_MIPS3);
19855
19856 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19857 ? ISA_MIPS3
19858 : ISA_MIPS1);
19859 }
19860
19861 /* 'default' has traditionally been a no-op. Probably not very useful. */
19862 if (strcasecmp (cpu_string, "default") == 0)
19863 return 0;
19864
19865 for (p = mips_cpu_info_table; p->name != 0; p++)
19866 if (mips_matching_cpu_name_p (p->name, cpu_string))
19867 return p;
19868
19869 as_bad (_("bad value (%s) for %s"), cpu_string, option);
19870 return 0;
19871 }
19872
19873 /* Return the canonical processor information for ISA (a member of the
19874 ISA_MIPS* enumeration). */
19875
19876 static const struct mips_cpu_info *
19877 mips_cpu_info_from_isa (int isa)
19878 {
19879 int i;
19880
19881 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19882 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19883 && isa == mips_cpu_info_table[i].isa)
19884 return (&mips_cpu_info_table[i]);
19885
19886 return NULL;
19887 }
19888
19889 static const struct mips_cpu_info *
19890 mips_cpu_info_from_arch (int arch)
19891 {
19892 int i;
19893
19894 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19895 if (arch == mips_cpu_info_table[i].cpu)
19896 return (&mips_cpu_info_table[i]);
19897
19898 return NULL;
19899 }
19900 \f
19901 static void
19902 show (FILE *stream, const char *string, int *col_p, int *first_p)
19903 {
19904 if (*first_p)
19905 {
19906 fprintf (stream, "%24s", "");
19907 *col_p = 24;
19908 }
19909 else
19910 {
19911 fprintf (stream, ", ");
19912 *col_p += 2;
19913 }
19914
19915 if (*col_p + strlen (string) > 72)
19916 {
19917 fprintf (stream, "\n%24s", "");
19918 *col_p = 24;
19919 }
19920
19921 fprintf (stream, "%s", string);
19922 *col_p += strlen (string);
19923
19924 *first_p = 0;
19925 }
19926
19927 void
19928 md_show_usage (FILE *stream)
19929 {
19930 int column, first;
19931 size_t i;
19932
19933 fprintf (stream, _("\
19934 MIPS options:\n\
19935 -EB generate big endian output\n\
19936 -EL generate little endian output\n\
19937 -g, -g2 do not remove unneeded NOPs or swap branches\n\
19938 -G NUM allow referencing objects up to NUM bytes\n\
19939 implicitly with the gp register [default 8]\n"));
19940 fprintf (stream, _("\
19941 -mips1 generate MIPS ISA I instructions\n\
19942 -mips2 generate MIPS ISA II instructions\n\
19943 -mips3 generate MIPS ISA III instructions\n\
19944 -mips4 generate MIPS ISA IV instructions\n\
19945 -mips5 generate MIPS ISA V instructions\n\
19946 -mips32 generate MIPS32 ISA instructions\n\
19947 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
19948 -mips32r3 generate MIPS32 release 3 ISA instructions\n\
19949 -mips32r5 generate MIPS32 release 5 ISA instructions\n\
19950 -mips32r6 generate MIPS32 release 6 ISA instructions\n\
19951 -mips64 generate MIPS64 ISA instructions\n\
19952 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
19953 -mips64r3 generate MIPS64 release 3 ISA instructions\n\
19954 -mips64r5 generate MIPS64 release 5 ISA instructions\n\
19955 -mips64r6 generate MIPS64 release 6 ISA instructions\n\
19956 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19957
19958 first = 1;
19959
19960 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19961 show (stream, mips_cpu_info_table[i].name, &column, &first);
19962 show (stream, "from-abi", &column, &first);
19963 fputc ('\n', stream);
19964
19965 fprintf (stream, _("\
19966 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19967 -no-mCPU don't generate code specific to CPU.\n\
19968 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19969
19970 first = 1;
19971
19972 show (stream, "3900", &column, &first);
19973 show (stream, "4010", &column, &first);
19974 show (stream, "4100", &column, &first);
19975 show (stream, "4650", &column, &first);
19976 fputc ('\n', stream);
19977
19978 fprintf (stream, _("\
19979 -mips16 generate mips16 instructions\n\
19980 -no-mips16 do not generate mips16 instructions\n"));
19981 fprintf (stream, _("\
19982 -mmips16e2 generate MIPS16e2 instructions\n\
19983 -mno-mips16e2 do not generate MIPS16e2 instructions\n"));
19984 fprintf (stream, _("\
19985 -mmicromips generate microMIPS instructions\n\
19986 -mno-micromips do not generate microMIPS instructions\n"));
19987 fprintf (stream, _("\
19988 -msmartmips generate smartmips instructions\n\
19989 -mno-smartmips do not generate smartmips instructions\n"));
19990 fprintf (stream, _("\
19991 -mdsp generate DSP instructions\n\
19992 -mno-dsp do not generate DSP instructions\n"));
19993 fprintf (stream, _("\
19994 -mdspr2 generate DSP R2 instructions\n\
19995 -mno-dspr2 do not generate DSP R2 instructions\n"));
19996 fprintf (stream, _("\
19997 -mdspr3 generate DSP R3 instructions\n\
19998 -mno-dspr3 do not generate DSP R3 instructions\n"));
19999 fprintf (stream, _("\
20000 -mmt generate MT instructions\n\
20001 -mno-mt do not generate MT instructions\n"));
20002 fprintf (stream, _("\
20003 -mmcu generate MCU instructions\n\
20004 -mno-mcu do not generate MCU instructions\n"));
20005 fprintf (stream, _("\
20006 -mmsa generate MSA instructions\n\
20007 -mno-msa do not generate MSA instructions\n"));
20008 fprintf (stream, _("\
20009 -mxpa generate eXtended Physical Address (XPA) instructions\n\
20010 -mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20011 fprintf (stream, _("\
20012 -mvirt generate Virtualization instructions\n\
20013 -mno-virt do not generate Virtualization instructions\n"));
20014 fprintf (stream, _("\
20015 -mcrc generate CRC instructions\n\
20016 -mno-crc do not generate CRC instructions\n"));
20017 fprintf (stream, _("\
20018 -mginv generate Global INValidate (GINV) instructions\n\
20019 -mno-ginv do not generate Global INValidate instructions\n"));
20020 fprintf (stream, _("\
20021 -minsn32 only generate 32-bit microMIPS instructions\n\
20022 -mno-insn32 generate all microMIPS instructions\n"));
20023 fprintf (stream, _("\
20024 -mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20025 -mfix-loongson2f-nop work around Loongson2F NOP errata\n\
20026 -mfix-vr4120 work around certain VR4120 errata\n\
20027 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
20028 -mfix-24k insert a nop after ERET and DERET instructions\n\
20029 -mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
20030 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20031 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
20032 -msym32 assume all symbols have 32-bit values\n\
20033 -O0 do not remove unneeded NOPs, do not swap branches\n\
20034 -O, -O1 remove unneeded NOPs, do not swap branches\n\
20035 -O2 remove unneeded NOPs and swap branches\n\
20036 --trap, --no-break trap exception on div by 0 and mult overflow\n\
20037 --break, --no-trap break exception on div by 0 and mult overflow\n"));
20038 fprintf (stream, _("\
20039 -mhard-float allow floating-point instructions\n\
20040 -msoft-float do not allow floating-point instructions\n\
20041 -msingle-float only allow 32-bit floating-point operations\n\
20042 -mdouble-float allow 32-bit and 64-bit floating-point operations\n\
20043 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
20044 --[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
20045 -mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20046 -mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
20047 -mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20048
20049 first = 1;
20050
20051 show (stream, "legacy", &column, &first);
20052 show (stream, "2008", &column, &first);
20053
20054 fputc ('\n', stream);
20055
20056 fprintf (stream, _("\
20057 -KPIC, -call_shared generate SVR4 position independent code\n\
20058 -call_nonpic generate non-PIC code that can operate with DSOs\n\
20059 -mvxworks-pic generate VxWorks position independent code\n\
20060 -non_shared do not generate code that can operate with DSOs\n\
20061 -xgot assume a 32 bit GOT\n\
20062 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
20063 -mshared, -mno-shared disable/enable .cpload optimization for\n\
20064 position dependent (non shared) code\n\
20065 -mabi=ABI create ABI conformant object file for:\n"));
20066
20067 first = 1;
20068
20069 show (stream, "32", &column, &first);
20070 show (stream, "o64", &column, &first);
20071 show (stream, "n32", &column, &first);
20072 show (stream, "64", &column, &first);
20073 show (stream, "eabi", &column, &first);
20074
20075 fputc ('\n', stream);
20076
20077 fprintf (stream, _("\
20078 -32 create o32 ABI object file%s\n"),
20079 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20080 fprintf (stream, _("\
20081 -n32 create n32 ABI object file%s\n"),
20082 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20083 fprintf (stream, _("\
20084 -64 create 64 ABI object file%s\n"),
20085 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
20086 }
20087
20088 #ifdef TE_IRIX
20089 enum dwarf2_format
20090 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
20091 {
20092 if (HAVE_64BIT_SYMBOLS)
20093 return dwarf2_format_64bit_irix;
20094 else
20095 return dwarf2_format_32bit;
20096 }
20097 #endif
20098
20099 int
20100 mips_dwarf2_addr_size (void)
20101 {
20102 if (HAVE_64BIT_OBJECTS)
20103 return 8;
20104 else
20105 return 4;
20106 }
20107
20108 /* Standard calling conventions leave the CFA at SP on entry. */
20109 void
20110 mips_cfi_frame_initial_instructions (void)
20111 {
20112 cfi_add_CFA_def_cfa_register (SP);
20113 }
20114
20115 int
20116 tc_mips_regname_to_dw2regnum (char *regname)
20117 {
20118 unsigned int regnum = -1;
20119 unsigned int reg;
20120
20121 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20122 regnum = reg;
20123
20124 return regnum;
20125 }
20126
20127 /* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20128 Given a symbolic attribute NAME, return the proper integer value.
20129 Returns -1 if the attribute is not known. */
20130
20131 int
20132 mips_convert_symbolic_attribute (const char *name)
20133 {
20134 static const struct
20135 {
20136 const char * name;
20137 const int tag;
20138 }
20139 attribute_table[] =
20140 {
20141 #define T(tag) {#tag, tag}
20142 T (Tag_GNU_MIPS_ABI_FP),
20143 T (Tag_GNU_MIPS_ABI_MSA),
20144 #undef T
20145 };
20146 unsigned int i;
20147
20148 if (name == NULL)
20149 return -1;
20150
20151 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20152 if (streq (name, attribute_table[i].name))
20153 return attribute_table[i].tag;
20154
20155 return -1;
20156 }
20157
20158 void
20159 md_mips_end (void)
20160 {
20161 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20162
20163 mips_emit_delays ();
20164 if (cur_proc_ptr)
20165 as_warn (_("missing .end at end of assembly"));
20166
20167 /* Just in case no code was emitted, do the consistency check. */
20168 file_mips_check_options ();
20169
20170 /* Set a floating-point ABI if the user did not. */
20171 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20172 {
20173 /* Perform consistency checks on the floating-point ABI. */
20174 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20175 Tag_GNU_MIPS_ABI_FP);
20176 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20177 check_fpabi (fpabi);
20178 }
20179 else
20180 {
20181 /* Soft-float gets precedence over single-float, the two options should
20182 not be used together so this should not matter. */
20183 if (file_mips_opts.soft_float == 1)
20184 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20185 /* Single-float gets precedence over all double_float cases. */
20186 else if (file_mips_opts.single_float == 1)
20187 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20188 else
20189 {
20190 switch (file_mips_opts.fp)
20191 {
20192 case 32:
20193 if (file_mips_opts.gp == 32)
20194 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20195 break;
20196 case 0:
20197 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20198 break;
20199 case 64:
20200 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20201 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20202 else if (file_mips_opts.gp == 32)
20203 fpabi = Val_GNU_MIPS_ABI_FP_64;
20204 else
20205 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20206 break;
20207 }
20208 }
20209
20210 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20211 Tag_GNU_MIPS_ABI_FP, fpabi);
20212 }
20213 }
20214
20215 /* Returns the relocation type required for a particular CFI encoding. */
20216
20217 bfd_reloc_code_real_type
20218 mips_cfi_reloc_for_encoding (int encoding)
20219 {
20220 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20221 return BFD_RELOC_32_PCREL;
20222 else return BFD_RELOC_NONE;
20223 }