MIPS16/GAS: Factor out duplicate symbol value conversion code
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright (C) 1993-2017 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, small, ext, dslot, jal_dslot) \
1134 (0x80000000 \
1135 | ((type) & 0xff) \
1136 | ((small) ? 0x100 : 0) \
1137 | ((ext) ? 0x200 : 0) \
1138 | ((dslot) ? 0x400 : 0) \
1139 | ((jal_dslot) ? 0x800 : 0))
1140 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1141 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1142 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1143 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1144 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1145 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1146 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1147 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1148 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1149 #define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x2000) != 0)
1150 #define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x2000)
1151 #define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x2000)
1152
1153 /* For microMIPS code, we use relaxation similar to one we use for
1154 MIPS16 code. Some instructions that take immediate values support
1155 two encodings: a small one which takes some small value, and a
1156 larger one which takes a 16 bit value. As some branches also follow
1157 this pattern, relaxing these values is required.
1158
1159 We can assemble both microMIPS and normal MIPS code in a single
1160 object. Therefore, we need to support this type of relaxation at
1161 the same time that we support the relaxation described above. We
1162 use one of the high bits of the subtype field to distinguish these
1163 cases.
1164
1165 The information we store for this type of relaxation is the argument
1166 code found in the opcode file for this relocation, the register
1167 selected as the assembler temporary, whether in the 32-bit
1168 instruction mode, whether the branch is unconditional, whether it is
1169 compact, whether there is no delay-slot instruction available to fill
1170 in, whether it stores the link address implicitly in $ra, whether
1171 relaxation of out-of-range 32-bit branches to a sequence of
1172 instructions is enabled, and whether the displacement of a branch is
1173 too large to fit as an immediate argument of a 16-bit and a 32-bit
1174 branch, respectively. */
1175 #define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
1176 uncond, compact, link, nods, \
1177 relax32, toofar16, toofar32) \
1178 (0x40000000 \
1179 | ((type) & 0xff) \
1180 | (((at) & 0x1f) << 8) \
1181 | ((insn32) ? 0x2000 : 0) \
1182 | ((pic) ? 0x4000 : 0) \
1183 | ((uncond) ? 0x8000 : 0) \
1184 | ((compact) ? 0x10000 : 0) \
1185 | ((link) ? 0x20000 : 0) \
1186 | ((nods) ? 0x40000 : 0) \
1187 | ((relax32) ? 0x80000 : 0) \
1188 | ((toofar16) ? 0x100000 : 0) \
1189 | ((toofar32) ? 0x200000 : 0))
1190 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1191 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1192 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1193 #define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
1194 #define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1195 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1196 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1197 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1198 #define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1199 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1200
1201 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1202 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1203 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1204 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1205 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1206 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
1207
1208 /* Sign-extend 16-bit value X. */
1209 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1210
1211 /* Is the given value a sign-extended 32-bit value? */
1212 #define IS_SEXT_32BIT_NUM(x) \
1213 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1214 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1215
1216 /* Is the given value a sign-extended 16-bit value? */
1217 #define IS_SEXT_16BIT_NUM(x) \
1218 (((x) &~ (offsetT) 0x7fff) == 0 \
1219 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1220
1221 /* Is the given value a sign-extended 12-bit value? */
1222 #define IS_SEXT_12BIT_NUM(x) \
1223 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1224
1225 /* Is the given value a sign-extended 9-bit value? */
1226 #define IS_SEXT_9BIT_NUM(x) \
1227 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1228
1229 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
1230 #define IS_ZEXT_32BIT_NUM(x) \
1231 (((x) &~ (offsetT) 0xffffffff) == 0 \
1232 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1233
1234 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1235 SHIFT places. */
1236 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1237 (((STRUCT) >> (SHIFT)) & (MASK))
1238
1239 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
1240 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1241 (!(MICROMIPS) \
1242 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1243 : EXTRACT_BITS ((INSN).insn_opcode, \
1244 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1245 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1246 EXTRACT_BITS ((INSN).insn_opcode, \
1247 MIPS16OP_MASK_##FIELD, \
1248 MIPS16OP_SH_##FIELD)
1249
1250 /* The MIPS16 EXTEND opcode, shifted left 16 places. */
1251 #define MIPS16_EXTEND (0xf000U << 16)
1252 \f
1253 /* Whether or not we are emitting a branch-likely macro. */
1254 static bfd_boolean emit_branch_likely_macro = FALSE;
1255
1256 /* Global variables used when generating relaxable macros. See the
1257 comment above RELAX_ENCODE for more details about how relaxation
1258 is used. */
1259 static struct {
1260 /* 0 if we're not emitting a relaxable macro.
1261 1 if we're emitting the first of the two relaxation alternatives.
1262 2 if we're emitting the second alternative. */
1263 int sequence;
1264
1265 /* The first relaxable fixup in the current frag. (In other words,
1266 the first fixup that refers to relaxable code.) */
1267 fixS *first_fixup;
1268
1269 /* sizes[0] says how many bytes of the first alternative are stored in
1270 the current frag. Likewise sizes[1] for the second alternative. */
1271 unsigned int sizes[2];
1272
1273 /* The symbol on which the choice of sequence depends. */
1274 symbolS *symbol;
1275 } mips_relax;
1276 \f
1277 /* Global variables used to decide whether a macro needs a warning. */
1278 static struct {
1279 /* True if the macro is in a branch delay slot. */
1280 bfd_boolean delay_slot_p;
1281
1282 /* Set to the length in bytes required if the macro is in a delay slot
1283 that requires a specific length of instruction, otherwise zero. */
1284 unsigned int delay_slot_length;
1285
1286 /* For relaxable macros, sizes[0] is the length of the first alternative
1287 in bytes and sizes[1] is the length of the second alternative.
1288 For non-relaxable macros, both elements give the length of the
1289 macro in bytes. */
1290 unsigned int sizes[2];
1291
1292 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1293 instruction of the first alternative in bytes and first_insn_sizes[1]
1294 is the length of the first instruction of the second alternative.
1295 For non-relaxable macros, both elements give the length of the first
1296 instruction in bytes.
1297
1298 Set to zero if we haven't yet seen the first instruction. */
1299 unsigned int first_insn_sizes[2];
1300
1301 /* For relaxable macros, insns[0] is the number of instructions for the
1302 first alternative and insns[1] is the number of instructions for the
1303 second alternative.
1304
1305 For non-relaxable macros, both elements give the number of
1306 instructions for the macro. */
1307 unsigned int insns[2];
1308
1309 /* The first variant frag for this macro. */
1310 fragS *first_frag;
1311 } mips_macro_warning;
1312 \f
1313 /* Prototypes for static functions. */
1314
1315 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1316
1317 static void append_insn
1318 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1319 bfd_boolean expansionp);
1320 static void mips_no_prev_insn (void);
1321 static void macro_build (expressionS *, const char *, const char *, ...);
1322 static void mips16_macro_build
1323 (expressionS *, const char *, const char *, va_list *);
1324 static void load_register (int, expressionS *, int);
1325 static void macro_start (void);
1326 static void macro_end (void);
1327 static void macro (struct mips_cl_insn *ip, char *str);
1328 static void mips16_macro (struct mips_cl_insn * ip);
1329 static void mips_ip (char *str, struct mips_cl_insn * ip);
1330 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1331 static void mips16_immed
1332 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1333 unsigned int, unsigned long *);
1334 static size_t my_getSmallExpression
1335 (expressionS *, bfd_reloc_code_real_type *, char *);
1336 static void my_getExpression (expressionS *, char *);
1337 static void s_align (int);
1338 static void s_change_sec (int);
1339 static void s_change_section (int);
1340 static void s_cons (int);
1341 static void s_float_cons (int);
1342 static void s_mips_globl (int);
1343 static void s_option (int);
1344 static void s_mipsset (int);
1345 static void s_abicalls (int);
1346 static void s_cpload (int);
1347 static void s_cpsetup (int);
1348 static void s_cplocal (int);
1349 static void s_cprestore (int);
1350 static void s_cpreturn (int);
1351 static void s_dtprelword (int);
1352 static void s_dtpreldword (int);
1353 static void s_tprelword (int);
1354 static void s_tpreldword (int);
1355 static void s_gpvalue (int);
1356 static void s_gpword (int);
1357 static void s_gpdword (int);
1358 static void s_ehword (int);
1359 static void s_cpadd (int);
1360 static void s_insn (int);
1361 static void s_nan (int);
1362 static void s_module (int);
1363 static void s_mips_ent (int);
1364 static void s_mips_end (int);
1365 static void s_mips_frame (int);
1366 static void s_mips_mask (int reg_type);
1367 static void s_mips_stab (int);
1368 static void s_mips_weakext (int);
1369 static void s_mips_file (int);
1370 static void s_mips_loc (int);
1371 static bfd_boolean pic_need_relax (symbolS *);
1372 static int relaxed_branch_length (fragS *, asection *, int);
1373 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1374 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1375 static void file_mips_check_options (void);
1376
1377 /* Table and functions used to map between CPU/ISA names, and
1378 ISA levels, and CPU numbers. */
1379
1380 struct mips_cpu_info
1381 {
1382 const char *name; /* CPU or ISA name. */
1383 int flags; /* MIPS_CPU_* flags. */
1384 int ase; /* Set of ASEs implemented by the CPU. */
1385 int isa; /* ISA level. */
1386 int cpu; /* CPU number (default CPU if ISA). */
1387 };
1388
1389 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1390
1391 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1392 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1393 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1394 \f
1395 /* Command-line options. */
1396 const char *md_shortopts = "O::g::G:";
1397
1398 enum options
1399 {
1400 OPTION_MARCH = OPTION_MD_BASE,
1401 OPTION_MTUNE,
1402 OPTION_MIPS1,
1403 OPTION_MIPS2,
1404 OPTION_MIPS3,
1405 OPTION_MIPS4,
1406 OPTION_MIPS5,
1407 OPTION_MIPS32,
1408 OPTION_MIPS64,
1409 OPTION_MIPS32R2,
1410 OPTION_MIPS32R3,
1411 OPTION_MIPS32R5,
1412 OPTION_MIPS32R6,
1413 OPTION_MIPS64R2,
1414 OPTION_MIPS64R3,
1415 OPTION_MIPS64R5,
1416 OPTION_MIPS64R6,
1417 OPTION_MIPS16,
1418 OPTION_NO_MIPS16,
1419 OPTION_MIPS3D,
1420 OPTION_NO_MIPS3D,
1421 OPTION_MDMX,
1422 OPTION_NO_MDMX,
1423 OPTION_DSP,
1424 OPTION_NO_DSP,
1425 OPTION_MT,
1426 OPTION_NO_MT,
1427 OPTION_VIRT,
1428 OPTION_NO_VIRT,
1429 OPTION_MSA,
1430 OPTION_NO_MSA,
1431 OPTION_SMARTMIPS,
1432 OPTION_NO_SMARTMIPS,
1433 OPTION_DSPR2,
1434 OPTION_NO_DSPR2,
1435 OPTION_DSPR3,
1436 OPTION_NO_DSPR3,
1437 OPTION_EVA,
1438 OPTION_NO_EVA,
1439 OPTION_XPA,
1440 OPTION_NO_XPA,
1441 OPTION_MICROMIPS,
1442 OPTION_NO_MICROMIPS,
1443 OPTION_MCU,
1444 OPTION_NO_MCU,
1445 OPTION_COMPAT_ARCH_BASE,
1446 OPTION_M4650,
1447 OPTION_NO_M4650,
1448 OPTION_M4010,
1449 OPTION_NO_M4010,
1450 OPTION_M4100,
1451 OPTION_NO_M4100,
1452 OPTION_M3900,
1453 OPTION_NO_M3900,
1454 OPTION_M7000_HILO_FIX,
1455 OPTION_MNO_7000_HILO_FIX,
1456 OPTION_FIX_24K,
1457 OPTION_NO_FIX_24K,
1458 OPTION_FIX_RM7000,
1459 OPTION_NO_FIX_RM7000,
1460 OPTION_FIX_LOONGSON2F_JUMP,
1461 OPTION_NO_FIX_LOONGSON2F_JUMP,
1462 OPTION_FIX_LOONGSON2F_NOP,
1463 OPTION_NO_FIX_LOONGSON2F_NOP,
1464 OPTION_FIX_VR4120,
1465 OPTION_NO_FIX_VR4120,
1466 OPTION_FIX_VR4130,
1467 OPTION_NO_FIX_VR4130,
1468 OPTION_FIX_CN63XXP1,
1469 OPTION_NO_FIX_CN63XXP1,
1470 OPTION_TRAP,
1471 OPTION_BREAK,
1472 OPTION_EB,
1473 OPTION_EL,
1474 OPTION_FP32,
1475 OPTION_GP32,
1476 OPTION_CONSTRUCT_FLOATS,
1477 OPTION_NO_CONSTRUCT_FLOATS,
1478 OPTION_FP64,
1479 OPTION_FPXX,
1480 OPTION_GP64,
1481 OPTION_RELAX_BRANCH,
1482 OPTION_NO_RELAX_BRANCH,
1483 OPTION_IGNORE_BRANCH_ISA,
1484 OPTION_NO_IGNORE_BRANCH_ISA,
1485 OPTION_INSN32,
1486 OPTION_NO_INSN32,
1487 OPTION_MSHARED,
1488 OPTION_MNO_SHARED,
1489 OPTION_MSYM32,
1490 OPTION_MNO_SYM32,
1491 OPTION_SOFT_FLOAT,
1492 OPTION_HARD_FLOAT,
1493 OPTION_SINGLE_FLOAT,
1494 OPTION_DOUBLE_FLOAT,
1495 OPTION_32,
1496 OPTION_CALL_SHARED,
1497 OPTION_CALL_NONPIC,
1498 OPTION_NON_SHARED,
1499 OPTION_XGOT,
1500 OPTION_MABI,
1501 OPTION_N32,
1502 OPTION_64,
1503 OPTION_MDEBUG,
1504 OPTION_NO_MDEBUG,
1505 OPTION_PDR,
1506 OPTION_NO_PDR,
1507 OPTION_MVXWORKS_PIC,
1508 OPTION_NAN,
1509 OPTION_ODD_SPREG,
1510 OPTION_NO_ODD_SPREG,
1511 OPTION_END_OF_ENUM
1512 };
1513
1514 struct option md_longopts[] =
1515 {
1516 /* Options which specify architecture. */
1517 {"march", required_argument, NULL, OPTION_MARCH},
1518 {"mtune", required_argument, NULL, OPTION_MTUNE},
1519 {"mips0", no_argument, NULL, OPTION_MIPS1},
1520 {"mips1", no_argument, NULL, OPTION_MIPS1},
1521 {"mips2", no_argument, NULL, OPTION_MIPS2},
1522 {"mips3", no_argument, NULL, OPTION_MIPS3},
1523 {"mips4", no_argument, NULL, OPTION_MIPS4},
1524 {"mips5", no_argument, NULL, OPTION_MIPS5},
1525 {"mips32", no_argument, NULL, OPTION_MIPS32},
1526 {"mips64", no_argument, NULL, OPTION_MIPS64},
1527 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1528 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1529 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
1530 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
1531 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1532 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1533 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
1534 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
1535
1536 /* Options which specify Application Specific Extensions (ASEs). */
1537 {"mips16", no_argument, NULL, OPTION_MIPS16},
1538 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1539 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1540 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1541 {"mdmx", no_argument, NULL, OPTION_MDMX},
1542 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1543 {"mdsp", no_argument, NULL, OPTION_DSP},
1544 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1545 {"mmt", no_argument, NULL, OPTION_MT},
1546 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1547 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1548 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1549 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1550 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1551 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1552 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
1553 {"meva", no_argument, NULL, OPTION_EVA},
1554 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1555 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1556 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1557 {"mmcu", no_argument, NULL, OPTION_MCU},
1558 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1559 {"mvirt", no_argument, NULL, OPTION_VIRT},
1560 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1561 {"mmsa", no_argument, NULL, OPTION_MSA},
1562 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
1563 {"mxpa", no_argument, NULL, OPTION_XPA},
1564 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
1565
1566 /* Old-style architecture options. Don't add more of these. */
1567 {"m4650", no_argument, NULL, OPTION_M4650},
1568 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1569 {"m4010", no_argument, NULL, OPTION_M4010},
1570 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1571 {"m4100", no_argument, NULL, OPTION_M4100},
1572 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1573 {"m3900", no_argument, NULL, OPTION_M3900},
1574 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1575
1576 /* Options which enable bug fixes. */
1577 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1578 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1579 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1580 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1581 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1582 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1583 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1584 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1585 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1586 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1587 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1588 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1589 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1590 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1591 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
1592 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1593 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1594
1595 /* Miscellaneous options. */
1596 {"trap", no_argument, NULL, OPTION_TRAP},
1597 {"no-break", no_argument, NULL, OPTION_TRAP},
1598 {"break", no_argument, NULL, OPTION_BREAK},
1599 {"no-trap", no_argument, NULL, OPTION_BREAK},
1600 {"EB", no_argument, NULL, OPTION_EB},
1601 {"EL", no_argument, NULL, OPTION_EL},
1602 {"mfp32", no_argument, NULL, OPTION_FP32},
1603 {"mgp32", no_argument, NULL, OPTION_GP32},
1604 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1605 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1606 {"mfp64", no_argument, NULL, OPTION_FP64},
1607 {"mfpxx", no_argument, NULL, OPTION_FPXX},
1608 {"mgp64", no_argument, NULL, OPTION_GP64},
1609 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1610 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1611 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1612 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
1613 {"minsn32", no_argument, NULL, OPTION_INSN32},
1614 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1615 {"mshared", no_argument, NULL, OPTION_MSHARED},
1616 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1617 {"msym32", no_argument, NULL, OPTION_MSYM32},
1618 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1619 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1620 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1621 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1622 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1623 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1624 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
1625
1626 /* Strictly speaking this next option is ELF specific,
1627 but we allow it for other ports as well in order to
1628 make testing easier. */
1629 {"32", no_argument, NULL, OPTION_32},
1630
1631 /* ELF-specific options. */
1632 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1633 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1634 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1635 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1636 {"xgot", no_argument, NULL, OPTION_XGOT},
1637 {"mabi", required_argument, NULL, OPTION_MABI},
1638 {"n32", no_argument, NULL, OPTION_N32},
1639 {"64", no_argument, NULL, OPTION_64},
1640 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1641 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1642 {"mpdr", no_argument, NULL, OPTION_PDR},
1643 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1644 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1645 {"mnan", required_argument, NULL, OPTION_NAN},
1646
1647 {NULL, no_argument, NULL, 0}
1648 };
1649 size_t md_longopts_size = sizeof (md_longopts);
1650 \f
1651 /* Information about either an Application Specific Extension or an
1652 optional architecture feature that, for simplicity, we treat in the
1653 same way as an ASE. */
1654 struct mips_ase
1655 {
1656 /* The name of the ASE, used in both the command-line and .set options. */
1657 const char *name;
1658
1659 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1660 and 64-bit architectures, the flags here refer to the subset that
1661 is available on both. */
1662 unsigned int flags;
1663
1664 /* The ASE_* flag used for instructions that are available on 64-bit
1665 architectures but that are not included in FLAGS. */
1666 unsigned int flags64;
1667
1668 /* The command-line options that turn the ASE on and off. */
1669 int option_on;
1670 int option_off;
1671
1672 /* The minimum required architecture revisions for MIPS32, MIPS64,
1673 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1674 int mips32_rev;
1675 int mips64_rev;
1676 int micromips32_rev;
1677 int micromips64_rev;
1678
1679 /* The architecture where the ASE was removed or -1 if the extension has not
1680 been removed. */
1681 int rem_rev;
1682 };
1683
1684 /* A table of all supported ASEs. */
1685 static const struct mips_ase mips_ases[] = {
1686 { "dsp", ASE_DSP, ASE_DSP64,
1687 OPTION_DSP, OPTION_NO_DSP,
1688 2, 2, 2, 2,
1689 -1 },
1690
1691 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1692 OPTION_DSPR2, OPTION_NO_DSPR2,
1693 2, 2, 2, 2,
1694 -1 },
1695
1696 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1697 OPTION_DSPR3, OPTION_NO_DSPR3,
1698 6, 6, -1, -1,
1699 -1 },
1700
1701 { "eva", ASE_EVA, 0,
1702 OPTION_EVA, OPTION_NO_EVA,
1703 2, 2, 2, 2,
1704 -1 },
1705
1706 { "mcu", ASE_MCU, 0,
1707 OPTION_MCU, OPTION_NO_MCU,
1708 2, 2, 2, 2,
1709 -1 },
1710
1711 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1712 { "mdmx", ASE_MDMX, 0,
1713 OPTION_MDMX, OPTION_NO_MDMX,
1714 -1, 1, -1, -1,
1715 6 },
1716
1717 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1718 { "mips3d", ASE_MIPS3D, 0,
1719 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1720 2, 1, -1, -1,
1721 6 },
1722
1723 { "mt", ASE_MT, 0,
1724 OPTION_MT, OPTION_NO_MT,
1725 2, 2, -1, -1,
1726 -1 },
1727
1728 { "smartmips", ASE_SMARTMIPS, 0,
1729 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1730 1, -1, -1, -1,
1731 6 },
1732
1733 { "virt", ASE_VIRT, ASE_VIRT64,
1734 OPTION_VIRT, OPTION_NO_VIRT,
1735 2, 2, 2, 2,
1736 -1 },
1737
1738 { "msa", ASE_MSA, ASE_MSA64,
1739 OPTION_MSA, OPTION_NO_MSA,
1740 2, 2, 2, 2,
1741 -1 },
1742
1743 { "xpa", ASE_XPA, 0,
1744 OPTION_XPA, OPTION_NO_XPA,
1745 2, 2, -1, -1,
1746 -1 },
1747 };
1748
1749 /* The set of ASEs that require -mfp64. */
1750 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
1751
1752 /* Groups of ASE_* flags that represent different revisions of an ASE. */
1753 static const unsigned int mips_ase_groups[] = {
1754 ASE_DSP | ASE_DSPR2 | ASE_DSPR3
1755 };
1756 \f
1757 /* Pseudo-op table.
1758
1759 The following pseudo-ops from the Kane and Heinrich MIPS book
1760 should be defined here, but are currently unsupported: .alias,
1761 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1762
1763 The following pseudo-ops from the Kane and Heinrich MIPS book are
1764 specific to the type of debugging information being generated, and
1765 should be defined by the object format: .aent, .begin, .bend,
1766 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1767 .vreg.
1768
1769 The following pseudo-ops from the Kane and Heinrich MIPS book are
1770 not MIPS CPU specific, but are also not specific to the object file
1771 format. This file is probably the best place to define them, but
1772 they are not currently supported: .asm0, .endr, .lab, .struct. */
1773
1774 static const pseudo_typeS mips_pseudo_table[] =
1775 {
1776 /* MIPS specific pseudo-ops. */
1777 {"option", s_option, 0},
1778 {"set", s_mipsset, 0},
1779 {"rdata", s_change_sec, 'r'},
1780 {"sdata", s_change_sec, 's'},
1781 {"livereg", s_ignore, 0},
1782 {"abicalls", s_abicalls, 0},
1783 {"cpload", s_cpload, 0},
1784 {"cpsetup", s_cpsetup, 0},
1785 {"cplocal", s_cplocal, 0},
1786 {"cprestore", s_cprestore, 0},
1787 {"cpreturn", s_cpreturn, 0},
1788 {"dtprelword", s_dtprelword, 0},
1789 {"dtpreldword", s_dtpreldword, 0},
1790 {"tprelword", s_tprelword, 0},
1791 {"tpreldword", s_tpreldword, 0},
1792 {"gpvalue", s_gpvalue, 0},
1793 {"gpword", s_gpword, 0},
1794 {"gpdword", s_gpdword, 0},
1795 {"ehword", s_ehword, 0},
1796 {"cpadd", s_cpadd, 0},
1797 {"insn", s_insn, 0},
1798 {"nan", s_nan, 0},
1799 {"module", s_module, 0},
1800
1801 /* Relatively generic pseudo-ops that happen to be used on MIPS
1802 chips. */
1803 {"asciiz", stringer, 8 + 1},
1804 {"bss", s_change_sec, 'b'},
1805 {"err", s_err, 0},
1806 {"half", s_cons, 1},
1807 {"dword", s_cons, 3},
1808 {"weakext", s_mips_weakext, 0},
1809 {"origin", s_org, 0},
1810 {"repeat", s_rept, 0},
1811
1812 /* For MIPS this is non-standard, but we define it for consistency. */
1813 {"sbss", s_change_sec, 'B'},
1814
1815 /* These pseudo-ops are defined in read.c, but must be overridden
1816 here for one reason or another. */
1817 {"align", s_align, 0},
1818 {"byte", s_cons, 0},
1819 {"data", s_change_sec, 'd'},
1820 {"double", s_float_cons, 'd'},
1821 {"float", s_float_cons, 'f'},
1822 {"globl", s_mips_globl, 0},
1823 {"global", s_mips_globl, 0},
1824 {"hword", s_cons, 1},
1825 {"int", s_cons, 2},
1826 {"long", s_cons, 2},
1827 {"octa", s_cons, 4},
1828 {"quad", s_cons, 3},
1829 {"section", s_change_section, 0},
1830 {"short", s_cons, 1},
1831 {"single", s_float_cons, 'f'},
1832 {"stabd", s_mips_stab, 'd'},
1833 {"stabn", s_mips_stab, 'n'},
1834 {"stabs", s_mips_stab, 's'},
1835 {"text", s_change_sec, 't'},
1836 {"word", s_cons, 2},
1837
1838 { "extern", ecoff_directive_extern, 0},
1839
1840 { NULL, NULL, 0 },
1841 };
1842
1843 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1844 {
1845 /* These pseudo-ops should be defined by the object file format.
1846 However, a.out doesn't support them, so we have versions here. */
1847 {"aent", s_mips_ent, 1},
1848 {"bgnb", s_ignore, 0},
1849 {"end", s_mips_end, 0},
1850 {"endb", s_ignore, 0},
1851 {"ent", s_mips_ent, 0},
1852 {"file", s_mips_file, 0},
1853 {"fmask", s_mips_mask, 'F'},
1854 {"frame", s_mips_frame, 0},
1855 {"loc", s_mips_loc, 0},
1856 {"mask", s_mips_mask, 'R'},
1857 {"verstamp", s_ignore, 0},
1858 { NULL, NULL, 0 },
1859 };
1860
1861 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1862 purpose of the `.dc.a' internal pseudo-op. */
1863
1864 int
1865 mips_address_bytes (void)
1866 {
1867 file_mips_check_options ();
1868 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1869 }
1870
1871 extern void pop_insert (const pseudo_typeS *);
1872
1873 void
1874 mips_pop_insert (void)
1875 {
1876 pop_insert (mips_pseudo_table);
1877 if (! ECOFF_DEBUGGING)
1878 pop_insert (mips_nonecoff_pseudo_table);
1879 }
1880 \f
1881 /* Symbols labelling the current insn. */
1882
1883 struct insn_label_list
1884 {
1885 struct insn_label_list *next;
1886 symbolS *label;
1887 };
1888
1889 static struct insn_label_list *free_insn_labels;
1890 #define label_list tc_segment_info_data.labels
1891
1892 static void mips_clear_insn_labels (void);
1893 static void mips_mark_labels (void);
1894 static void mips_compressed_mark_labels (void);
1895
1896 static inline void
1897 mips_clear_insn_labels (void)
1898 {
1899 struct insn_label_list **pl;
1900 segment_info_type *si;
1901
1902 if (now_seg)
1903 {
1904 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1905 ;
1906
1907 si = seg_info (now_seg);
1908 *pl = si->label_list;
1909 si->label_list = NULL;
1910 }
1911 }
1912
1913 /* Mark instruction labels in MIPS16/microMIPS mode. */
1914
1915 static inline void
1916 mips_mark_labels (void)
1917 {
1918 if (HAVE_CODE_COMPRESSION)
1919 mips_compressed_mark_labels ();
1920 }
1921 \f
1922 static char *expr_end;
1923
1924 /* An expression in a macro instruction. This is set by mips_ip and
1925 mips16_ip and when populated is always an O_constant. */
1926
1927 static expressionS imm_expr;
1928
1929 /* The relocatable field in an instruction and the relocs associated
1930 with it. These variables are used for instructions like LUI and
1931 JAL as well as true offsets. They are also used for address
1932 operands in macros. */
1933
1934 static expressionS offset_expr;
1935 static bfd_reloc_code_real_type offset_reloc[3]
1936 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1937
1938 /* This is set to the resulting size of the instruction to be produced
1939 by mips16_ip if an explicit extension is used or by mips_ip if an
1940 explicit size is supplied. */
1941
1942 static unsigned int forced_insn_length;
1943
1944 /* True if we are assembling an instruction. All dot symbols defined during
1945 this time should be treated as code labels. */
1946
1947 static bfd_boolean mips_assembling_insn;
1948
1949 /* The pdr segment for per procedure frame/regmask info. Not used for
1950 ECOFF debugging. */
1951
1952 static segT pdr_seg;
1953
1954 /* The default target format to use. */
1955
1956 #if defined (TE_FreeBSD)
1957 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1958 #elif defined (TE_TMIPS)
1959 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1960 #else
1961 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1962 #endif
1963
1964 const char *
1965 mips_target_format (void)
1966 {
1967 switch (OUTPUT_FLAVOR)
1968 {
1969 case bfd_target_elf_flavour:
1970 #ifdef TE_VXWORKS
1971 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1972 return (target_big_endian
1973 ? "elf32-bigmips-vxworks"
1974 : "elf32-littlemips-vxworks");
1975 #endif
1976 return (target_big_endian
1977 ? (HAVE_64BIT_OBJECTS
1978 ? ELF_TARGET ("elf64-", "big")
1979 : (HAVE_NEWABI
1980 ? ELF_TARGET ("elf32-n", "big")
1981 : ELF_TARGET ("elf32-", "big")))
1982 : (HAVE_64BIT_OBJECTS
1983 ? ELF_TARGET ("elf64-", "little")
1984 : (HAVE_NEWABI
1985 ? ELF_TARGET ("elf32-n", "little")
1986 : ELF_TARGET ("elf32-", "little"))));
1987 default:
1988 abort ();
1989 return NULL;
1990 }
1991 }
1992
1993 /* Return the ISA revision that is currently in use, or 0 if we are
1994 generating code for MIPS V or below. */
1995
1996 static int
1997 mips_isa_rev (void)
1998 {
1999 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2000 return 2;
2001
2002 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2003 return 3;
2004
2005 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2006 return 5;
2007
2008 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2009 return 6;
2010
2011 /* microMIPS implies revision 2 or above. */
2012 if (mips_opts.micromips)
2013 return 2;
2014
2015 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2016 return 1;
2017
2018 return 0;
2019 }
2020
2021 /* Return the mask of all ASEs that are revisions of those in FLAGS. */
2022
2023 static unsigned int
2024 mips_ase_mask (unsigned int flags)
2025 {
2026 unsigned int i;
2027
2028 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2029 if (flags & mips_ase_groups[i])
2030 flags |= mips_ase_groups[i];
2031 return flags;
2032 }
2033
2034 /* Check whether the current ISA supports ASE. Issue a warning if
2035 appropriate. */
2036
2037 static void
2038 mips_check_isa_supports_ase (const struct mips_ase *ase)
2039 {
2040 const char *base;
2041 int min_rev, size;
2042 static unsigned int warned_isa;
2043 static unsigned int warned_fp32;
2044
2045 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2046 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2047 else
2048 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2049 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2050 && (warned_isa & ase->flags) != ase->flags)
2051 {
2052 warned_isa |= ase->flags;
2053 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2054 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2055 if (min_rev < 0)
2056 as_warn (_("the %d-bit %s architecture does not support the"
2057 " `%s' extension"), size, base, ase->name);
2058 else
2059 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
2060 ase->name, base, size, min_rev);
2061 }
2062 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2063 && (warned_isa & ase->flags) != ase->flags)
2064 {
2065 warned_isa |= ase->flags;
2066 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2067 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2068 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2069 ase->name, base, size, ase->rem_rev);
2070 }
2071
2072 if ((ase->flags & FP64_ASES)
2073 && mips_opts.fp != 64
2074 && (warned_fp32 & ase->flags) != ase->flags)
2075 {
2076 warned_fp32 |= ase->flags;
2077 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
2078 }
2079 }
2080
2081 /* Check all enabled ASEs to see whether they are supported by the
2082 chosen architecture. */
2083
2084 static void
2085 mips_check_isa_supports_ases (void)
2086 {
2087 unsigned int i, mask;
2088
2089 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2090 {
2091 mask = mips_ase_mask (mips_ases[i].flags);
2092 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2093 mips_check_isa_supports_ase (&mips_ases[i]);
2094 }
2095 }
2096
2097 /* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2098 that were affected. */
2099
2100 static unsigned int
2101 mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2102 bfd_boolean enabled_p)
2103 {
2104 unsigned int mask;
2105
2106 mask = mips_ase_mask (ase->flags);
2107 opts->ase &= ~mask;
2108 if (enabled_p)
2109 opts->ase |= ase->flags;
2110 return mask;
2111 }
2112
2113 /* Return the ASE called NAME, or null if none. */
2114
2115 static const struct mips_ase *
2116 mips_lookup_ase (const char *name)
2117 {
2118 unsigned int i;
2119
2120 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2121 if (strcmp (name, mips_ases[i].name) == 0)
2122 return &mips_ases[i];
2123 return NULL;
2124 }
2125
2126 /* Return the length of a microMIPS instruction in bytes. If bits of
2127 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2128 otherwise it is a 32-bit instruction. */
2129
2130 static inline unsigned int
2131 micromips_insn_length (const struct mips_opcode *mo)
2132 {
2133 return mips_opcode_32bit_p (mo) ? 4 : 2;
2134 }
2135
2136 /* Return the length of MIPS16 instruction OPCODE. */
2137
2138 static inline unsigned int
2139 mips16_opcode_length (unsigned long opcode)
2140 {
2141 return (opcode >> 16) == 0 ? 2 : 4;
2142 }
2143
2144 /* Return the length of instruction INSN. */
2145
2146 static inline unsigned int
2147 insn_length (const struct mips_cl_insn *insn)
2148 {
2149 if (mips_opts.micromips)
2150 return micromips_insn_length (insn->insn_mo);
2151 else if (mips_opts.mips16)
2152 return mips16_opcode_length (insn->insn_opcode);
2153 else
2154 return 4;
2155 }
2156
2157 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2158
2159 static void
2160 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2161 {
2162 size_t i;
2163
2164 insn->insn_mo = mo;
2165 insn->insn_opcode = mo->match;
2166 insn->frag = NULL;
2167 insn->where = 0;
2168 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2169 insn->fixp[i] = NULL;
2170 insn->fixed_p = (mips_opts.noreorder > 0);
2171 insn->noreorder_p = (mips_opts.noreorder > 0);
2172 insn->mips16_absolute_jump_p = 0;
2173 insn->complete_p = 0;
2174 insn->cleared_p = 0;
2175 }
2176
2177 /* Get a list of all the operands in INSN. */
2178
2179 static const struct mips_operand_array *
2180 insn_operands (const struct mips_cl_insn *insn)
2181 {
2182 if (insn->insn_mo >= &mips_opcodes[0]
2183 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2184 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2185
2186 if (insn->insn_mo >= &mips16_opcodes[0]
2187 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2188 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2189
2190 if (insn->insn_mo >= &micromips_opcodes[0]
2191 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2192 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2193
2194 abort ();
2195 }
2196
2197 /* Get a description of operand OPNO of INSN. */
2198
2199 static const struct mips_operand *
2200 insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2201 {
2202 const struct mips_operand_array *operands;
2203
2204 operands = insn_operands (insn);
2205 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2206 abort ();
2207 return operands->operand[opno];
2208 }
2209
2210 /* Install UVAL as the value of OPERAND in INSN. */
2211
2212 static inline void
2213 insn_insert_operand (struct mips_cl_insn *insn,
2214 const struct mips_operand *operand, unsigned int uval)
2215 {
2216 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2217 }
2218
2219 /* Extract the value of OPERAND from INSN. */
2220
2221 static inline unsigned
2222 insn_extract_operand (const struct mips_cl_insn *insn,
2223 const struct mips_operand *operand)
2224 {
2225 return mips_extract_operand (operand, insn->insn_opcode);
2226 }
2227
2228 /* Record the current MIPS16/microMIPS mode in now_seg. */
2229
2230 static void
2231 mips_record_compressed_mode (void)
2232 {
2233 segment_info_type *si;
2234
2235 si = seg_info (now_seg);
2236 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2237 si->tc_segment_info_data.mips16 = mips_opts.mips16;
2238 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2239 si->tc_segment_info_data.micromips = mips_opts.micromips;
2240 }
2241
2242 /* Read a standard MIPS instruction from BUF. */
2243
2244 static unsigned long
2245 read_insn (char *buf)
2246 {
2247 if (target_big_endian)
2248 return bfd_getb32 ((bfd_byte *) buf);
2249 else
2250 return bfd_getl32 ((bfd_byte *) buf);
2251 }
2252
2253 /* Write standard MIPS instruction INSN to BUF. Return a pointer to
2254 the next byte. */
2255
2256 static char *
2257 write_insn (char *buf, unsigned int insn)
2258 {
2259 md_number_to_chars (buf, insn, 4);
2260 return buf + 4;
2261 }
2262
2263 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2264 has length LENGTH. */
2265
2266 static unsigned long
2267 read_compressed_insn (char *buf, unsigned int length)
2268 {
2269 unsigned long insn;
2270 unsigned int i;
2271
2272 insn = 0;
2273 for (i = 0; i < length; i += 2)
2274 {
2275 insn <<= 16;
2276 if (target_big_endian)
2277 insn |= bfd_getb16 ((char *) buf);
2278 else
2279 insn |= bfd_getl16 ((char *) buf);
2280 buf += 2;
2281 }
2282 return insn;
2283 }
2284
2285 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2286 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2287
2288 static char *
2289 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2290 {
2291 unsigned int i;
2292
2293 for (i = 0; i < length; i += 2)
2294 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2295 return buf + length;
2296 }
2297
2298 /* Install INSN at the location specified by its "frag" and "where" fields. */
2299
2300 static void
2301 install_insn (const struct mips_cl_insn *insn)
2302 {
2303 char *f = insn->frag->fr_literal + insn->where;
2304 if (HAVE_CODE_COMPRESSION)
2305 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2306 else
2307 write_insn (f, insn->insn_opcode);
2308 mips_record_compressed_mode ();
2309 }
2310
2311 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2312 and install the opcode in the new location. */
2313
2314 static void
2315 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2316 {
2317 size_t i;
2318
2319 insn->frag = frag;
2320 insn->where = where;
2321 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2322 if (insn->fixp[i] != NULL)
2323 {
2324 insn->fixp[i]->fx_frag = frag;
2325 insn->fixp[i]->fx_where = where;
2326 }
2327 install_insn (insn);
2328 }
2329
2330 /* Add INSN to the end of the output. */
2331
2332 static void
2333 add_fixed_insn (struct mips_cl_insn *insn)
2334 {
2335 char *f = frag_more (insn_length (insn));
2336 move_insn (insn, frag_now, f - frag_now->fr_literal);
2337 }
2338
2339 /* Start a variant frag and move INSN to the start of the variant part,
2340 marking it as fixed. The other arguments are as for frag_var. */
2341
2342 static void
2343 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2344 relax_substateT subtype, symbolS *symbol, offsetT offset)
2345 {
2346 frag_grow (max_chars);
2347 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2348 insn->fixed_p = 1;
2349 frag_var (rs_machine_dependent, max_chars, var,
2350 subtype, symbol, offset, NULL);
2351 }
2352
2353 /* Insert N copies of INSN into the history buffer, starting at
2354 position FIRST. Neither FIRST nor N need to be clipped. */
2355
2356 static void
2357 insert_into_history (unsigned int first, unsigned int n,
2358 const struct mips_cl_insn *insn)
2359 {
2360 if (mips_relax.sequence != 2)
2361 {
2362 unsigned int i;
2363
2364 for (i = ARRAY_SIZE (history); i-- > first;)
2365 if (i >= first + n)
2366 history[i] = history[i - n];
2367 else
2368 history[i] = *insn;
2369 }
2370 }
2371
2372 /* Clear the error in insn_error. */
2373
2374 static void
2375 clear_insn_error (void)
2376 {
2377 memset (&insn_error, 0, sizeof (insn_error));
2378 }
2379
2380 /* Possibly record error message MSG for the current instruction.
2381 If the error is about a particular argument, ARGNUM is the 1-based
2382 number of that argument, otherwise it is 0. FORMAT is the format
2383 of MSG. Return true if MSG was used, false if the current message
2384 was kept. */
2385
2386 static bfd_boolean
2387 set_insn_error_format (int argnum, enum mips_insn_error_format format,
2388 const char *msg)
2389 {
2390 if (argnum == 0)
2391 {
2392 /* Give priority to errors against specific arguments, and to
2393 the first whole-instruction message. */
2394 if (insn_error.msg)
2395 return FALSE;
2396 }
2397 else
2398 {
2399 /* Keep insn_error if it is against a later argument. */
2400 if (argnum < insn_error.min_argnum)
2401 return FALSE;
2402
2403 /* If both errors are against the same argument but are different,
2404 give up on reporting a specific error for this argument.
2405 See the comment about mips_insn_error for details. */
2406 if (argnum == insn_error.min_argnum
2407 && insn_error.msg
2408 && strcmp (insn_error.msg, msg) != 0)
2409 {
2410 insn_error.msg = 0;
2411 insn_error.min_argnum += 1;
2412 return FALSE;
2413 }
2414 }
2415 insn_error.min_argnum = argnum;
2416 insn_error.format = format;
2417 insn_error.msg = msg;
2418 return TRUE;
2419 }
2420
2421 /* Record an instruction error with no % format fields. ARGNUM and MSG are
2422 as for set_insn_error_format. */
2423
2424 static void
2425 set_insn_error (int argnum, const char *msg)
2426 {
2427 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2428 }
2429
2430 /* Record an instruction error with one %d field I. ARGNUM and MSG are
2431 as for set_insn_error_format. */
2432
2433 static void
2434 set_insn_error_i (int argnum, const char *msg, int i)
2435 {
2436 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2437 insn_error.u.i = i;
2438 }
2439
2440 /* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2441 are as for set_insn_error_format. */
2442
2443 static void
2444 set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2445 {
2446 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2447 {
2448 insn_error.u.ss[0] = s1;
2449 insn_error.u.ss[1] = s2;
2450 }
2451 }
2452
2453 /* Report the error in insn_error, which is against assembly code STR. */
2454
2455 static void
2456 report_insn_error (const char *str)
2457 {
2458 const char *msg = concat (insn_error.msg, " `%s'", NULL);
2459
2460 switch (insn_error.format)
2461 {
2462 case ERR_FMT_PLAIN:
2463 as_bad (msg, str);
2464 break;
2465
2466 case ERR_FMT_I:
2467 as_bad (msg, insn_error.u.i, str);
2468 break;
2469
2470 case ERR_FMT_SS:
2471 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2472 break;
2473 }
2474
2475 free ((char *) msg);
2476 }
2477
2478 /* Initialize vr4120_conflicts. There is a bit of duplication here:
2479 the idea is to make it obvious at a glance that each errata is
2480 included. */
2481
2482 static void
2483 init_vr4120_conflicts (void)
2484 {
2485 #define CONFLICT(FIRST, SECOND) \
2486 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2487
2488 /* Errata 21 - [D]DIV[U] after [D]MACC */
2489 CONFLICT (MACC, DIV);
2490 CONFLICT (DMACC, DIV);
2491
2492 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2493 CONFLICT (DMULT, DMULT);
2494 CONFLICT (DMULT, DMACC);
2495 CONFLICT (DMACC, DMULT);
2496 CONFLICT (DMACC, DMACC);
2497
2498 /* Errata 24 - MT{LO,HI} after [D]MACC */
2499 CONFLICT (MACC, MTHILO);
2500 CONFLICT (DMACC, MTHILO);
2501
2502 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2503 instruction is executed immediately after a MACC or DMACC
2504 instruction, the result of [either instruction] is incorrect." */
2505 CONFLICT (MACC, MULT);
2506 CONFLICT (MACC, DMULT);
2507 CONFLICT (DMACC, MULT);
2508 CONFLICT (DMACC, DMULT);
2509
2510 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2511 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2512 DDIV or DDIVU instruction, the result of the MACC or
2513 DMACC instruction is incorrect.". */
2514 CONFLICT (DMULT, MACC);
2515 CONFLICT (DMULT, DMACC);
2516 CONFLICT (DIV, MACC);
2517 CONFLICT (DIV, DMACC);
2518
2519 #undef CONFLICT
2520 }
2521
2522 struct regname {
2523 const char *name;
2524 unsigned int num;
2525 };
2526
2527 #define RNUM_MASK 0x00000ff
2528 #define RTYPE_MASK 0x0ffff00
2529 #define RTYPE_NUM 0x0000100
2530 #define RTYPE_FPU 0x0000200
2531 #define RTYPE_FCC 0x0000400
2532 #define RTYPE_VEC 0x0000800
2533 #define RTYPE_GP 0x0001000
2534 #define RTYPE_CP0 0x0002000
2535 #define RTYPE_PC 0x0004000
2536 #define RTYPE_ACC 0x0008000
2537 #define RTYPE_CCC 0x0010000
2538 #define RTYPE_VI 0x0020000
2539 #define RTYPE_VF 0x0040000
2540 #define RTYPE_R5900_I 0x0080000
2541 #define RTYPE_R5900_Q 0x0100000
2542 #define RTYPE_R5900_R 0x0200000
2543 #define RTYPE_R5900_ACC 0x0400000
2544 #define RTYPE_MSA 0x0800000
2545 #define RWARN 0x8000000
2546
2547 #define GENERIC_REGISTER_NUMBERS \
2548 {"$0", RTYPE_NUM | 0}, \
2549 {"$1", RTYPE_NUM | 1}, \
2550 {"$2", RTYPE_NUM | 2}, \
2551 {"$3", RTYPE_NUM | 3}, \
2552 {"$4", RTYPE_NUM | 4}, \
2553 {"$5", RTYPE_NUM | 5}, \
2554 {"$6", RTYPE_NUM | 6}, \
2555 {"$7", RTYPE_NUM | 7}, \
2556 {"$8", RTYPE_NUM | 8}, \
2557 {"$9", RTYPE_NUM | 9}, \
2558 {"$10", RTYPE_NUM | 10}, \
2559 {"$11", RTYPE_NUM | 11}, \
2560 {"$12", RTYPE_NUM | 12}, \
2561 {"$13", RTYPE_NUM | 13}, \
2562 {"$14", RTYPE_NUM | 14}, \
2563 {"$15", RTYPE_NUM | 15}, \
2564 {"$16", RTYPE_NUM | 16}, \
2565 {"$17", RTYPE_NUM | 17}, \
2566 {"$18", RTYPE_NUM | 18}, \
2567 {"$19", RTYPE_NUM | 19}, \
2568 {"$20", RTYPE_NUM | 20}, \
2569 {"$21", RTYPE_NUM | 21}, \
2570 {"$22", RTYPE_NUM | 22}, \
2571 {"$23", RTYPE_NUM | 23}, \
2572 {"$24", RTYPE_NUM | 24}, \
2573 {"$25", RTYPE_NUM | 25}, \
2574 {"$26", RTYPE_NUM | 26}, \
2575 {"$27", RTYPE_NUM | 27}, \
2576 {"$28", RTYPE_NUM | 28}, \
2577 {"$29", RTYPE_NUM | 29}, \
2578 {"$30", RTYPE_NUM | 30}, \
2579 {"$31", RTYPE_NUM | 31}
2580
2581 #define FPU_REGISTER_NAMES \
2582 {"$f0", RTYPE_FPU | 0}, \
2583 {"$f1", RTYPE_FPU | 1}, \
2584 {"$f2", RTYPE_FPU | 2}, \
2585 {"$f3", RTYPE_FPU | 3}, \
2586 {"$f4", RTYPE_FPU | 4}, \
2587 {"$f5", RTYPE_FPU | 5}, \
2588 {"$f6", RTYPE_FPU | 6}, \
2589 {"$f7", RTYPE_FPU | 7}, \
2590 {"$f8", RTYPE_FPU | 8}, \
2591 {"$f9", RTYPE_FPU | 9}, \
2592 {"$f10", RTYPE_FPU | 10}, \
2593 {"$f11", RTYPE_FPU | 11}, \
2594 {"$f12", RTYPE_FPU | 12}, \
2595 {"$f13", RTYPE_FPU | 13}, \
2596 {"$f14", RTYPE_FPU | 14}, \
2597 {"$f15", RTYPE_FPU | 15}, \
2598 {"$f16", RTYPE_FPU | 16}, \
2599 {"$f17", RTYPE_FPU | 17}, \
2600 {"$f18", RTYPE_FPU | 18}, \
2601 {"$f19", RTYPE_FPU | 19}, \
2602 {"$f20", RTYPE_FPU | 20}, \
2603 {"$f21", RTYPE_FPU | 21}, \
2604 {"$f22", RTYPE_FPU | 22}, \
2605 {"$f23", RTYPE_FPU | 23}, \
2606 {"$f24", RTYPE_FPU | 24}, \
2607 {"$f25", RTYPE_FPU | 25}, \
2608 {"$f26", RTYPE_FPU | 26}, \
2609 {"$f27", RTYPE_FPU | 27}, \
2610 {"$f28", RTYPE_FPU | 28}, \
2611 {"$f29", RTYPE_FPU | 29}, \
2612 {"$f30", RTYPE_FPU | 30}, \
2613 {"$f31", RTYPE_FPU | 31}
2614
2615 #define FPU_CONDITION_CODE_NAMES \
2616 {"$fcc0", RTYPE_FCC | 0}, \
2617 {"$fcc1", RTYPE_FCC | 1}, \
2618 {"$fcc2", RTYPE_FCC | 2}, \
2619 {"$fcc3", RTYPE_FCC | 3}, \
2620 {"$fcc4", RTYPE_FCC | 4}, \
2621 {"$fcc5", RTYPE_FCC | 5}, \
2622 {"$fcc6", RTYPE_FCC | 6}, \
2623 {"$fcc7", RTYPE_FCC | 7}
2624
2625 #define COPROC_CONDITION_CODE_NAMES \
2626 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2627 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2628 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2629 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2630 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2631 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2632 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2633 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2634
2635 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2636 {"$a4", RTYPE_GP | 8}, \
2637 {"$a5", RTYPE_GP | 9}, \
2638 {"$a6", RTYPE_GP | 10}, \
2639 {"$a7", RTYPE_GP | 11}, \
2640 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2641 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2642 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2643 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2644 {"$t0", RTYPE_GP | 12}, \
2645 {"$t1", RTYPE_GP | 13}, \
2646 {"$t2", RTYPE_GP | 14}, \
2647 {"$t3", RTYPE_GP | 15}
2648
2649 #define O32_SYMBOLIC_REGISTER_NAMES \
2650 {"$t0", RTYPE_GP | 8}, \
2651 {"$t1", RTYPE_GP | 9}, \
2652 {"$t2", RTYPE_GP | 10}, \
2653 {"$t3", RTYPE_GP | 11}, \
2654 {"$t4", RTYPE_GP | 12}, \
2655 {"$t5", RTYPE_GP | 13}, \
2656 {"$t6", RTYPE_GP | 14}, \
2657 {"$t7", RTYPE_GP | 15}, \
2658 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2659 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2660 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2661 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2662
2663 /* Remaining symbolic register names */
2664 #define SYMBOLIC_REGISTER_NAMES \
2665 {"$zero", RTYPE_GP | 0}, \
2666 {"$at", RTYPE_GP | 1}, \
2667 {"$AT", RTYPE_GP | 1}, \
2668 {"$v0", RTYPE_GP | 2}, \
2669 {"$v1", RTYPE_GP | 3}, \
2670 {"$a0", RTYPE_GP | 4}, \
2671 {"$a1", RTYPE_GP | 5}, \
2672 {"$a2", RTYPE_GP | 6}, \
2673 {"$a3", RTYPE_GP | 7}, \
2674 {"$s0", RTYPE_GP | 16}, \
2675 {"$s1", RTYPE_GP | 17}, \
2676 {"$s2", RTYPE_GP | 18}, \
2677 {"$s3", RTYPE_GP | 19}, \
2678 {"$s4", RTYPE_GP | 20}, \
2679 {"$s5", RTYPE_GP | 21}, \
2680 {"$s6", RTYPE_GP | 22}, \
2681 {"$s7", RTYPE_GP | 23}, \
2682 {"$t8", RTYPE_GP | 24}, \
2683 {"$t9", RTYPE_GP | 25}, \
2684 {"$k0", RTYPE_GP | 26}, \
2685 {"$kt0", RTYPE_GP | 26}, \
2686 {"$k1", RTYPE_GP | 27}, \
2687 {"$kt1", RTYPE_GP | 27}, \
2688 {"$gp", RTYPE_GP | 28}, \
2689 {"$sp", RTYPE_GP | 29}, \
2690 {"$s8", RTYPE_GP | 30}, \
2691 {"$fp", RTYPE_GP | 30}, \
2692 {"$ra", RTYPE_GP | 31}
2693
2694 #define MIPS16_SPECIAL_REGISTER_NAMES \
2695 {"$pc", RTYPE_PC | 0}
2696
2697 #define MDMX_VECTOR_REGISTER_NAMES \
2698 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2699 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2700 {"$v2", RTYPE_VEC | 2}, \
2701 {"$v3", RTYPE_VEC | 3}, \
2702 {"$v4", RTYPE_VEC | 4}, \
2703 {"$v5", RTYPE_VEC | 5}, \
2704 {"$v6", RTYPE_VEC | 6}, \
2705 {"$v7", RTYPE_VEC | 7}, \
2706 {"$v8", RTYPE_VEC | 8}, \
2707 {"$v9", RTYPE_VEC | 9}, \
2708 {"$v10", RTYPE_VEC | 10}, \
2709 {"$v11", RTYPE_VEC | 11}, \
2710 {"$v12", RTYPE_VEC | 12}, \
2711 {"$v13", RTYPE_VEC | 13}, \
2712 {"$v14", RTYPE_VEC | 14}, \
2713 {"$v15", RTYPE_VEC | 15}, \
2714 {"$v16", RTYPE_VEC | 16}, \
2715 {"$v17", RTYPE_VEC | 17}, \
2716 {"$v18", RTYPE_VEC | 18}, \
2717 {"$v19", RTYPE_VEC | 19}, \
2718 {"$v20", RTYPE_VEC | 20}, \
2719 {"$v21", RTYPE_VEC | 21}, \
2720 {"$v22", RTYPE_VEC | 22}, \
2721 {"$v23", RTYPE_VEC | 23}, \
2722 {"$v24", RTYPE_VEC | 24}, \
2723 {"$v25", RTYPE_VEC | 25}, \
2724 {"$v26", RTYPE_VEC | 26}, \
2725 {"$v27", RTYPE_VEC | 27}, \
2726 {"$v28", RTYPE_VEC | 28}, \
2727 {"$v29", RTYPE_VEC | 29}, \
2728 {"$v30", RTYPE_VEC | 30}, \
2729 {"$v31", RTYPE_VEC | 31}
2730
2731 #define R5900_I_NAMES \
2732 {"$I", RTYPE_R5900_I | 0}
2733
2734 #define R5900_Q_NAMES \
2735 {"$Q", RTYPE_R5900_Q | 0}
2736
2737 #define R5900_R_NAMES \
2738 {"$R", RTYPE_R5900_R | 0}
2739
2740 #define R5900_ACC_NAMES \
2741 {"$ACC", RTYPE_R5900_ACC | 0 }
2742
2743 #define MIPS_DSP_ACCUMULATOR_NAMES \
2744 {"$ac0", RTYPE_ACC | 0}, \
2745 {"$ac1", RTYPE_ACC | 1}, \
2746 {"$ac2", RTYPE_ACC | 2}, \
2747 {"$ac3", RTYPE_ACC | 3}
2748
2749 static const struct regname reg_names[] = {
2750 GENERIC_REGISTER_NUMBERS,
2751 FPU_REGISTER_NAMES,
2752 FPU_CONDITION_CODE_NAMES,
2753 COPROC_CONDITION_CODE_NAMES,
2754
2755 /* The $txx registers depends on the abi,
2756 these will be added later into the symbol table from
2757 one of the tables below once mips_abi is set after
2758 parsing of arguments from the command line. */
2759 SYMBOLIC_REGISTER_NAMES,
2760
2761 MIPS16_SPECIAL_REGISTER_NAMES,
2762 MDMX_VECTOR_REGISTER_NAMES,
2763 R5900_I_NAMES,
2764 R5900_Q_NAMES,
2765 R5900_R_NAMES,
2766 R5900_ACC_NAMES,
2767 MIPS_DSP_ACCUMULATOR_NAMES,
2768 {0, 0}
2769 };
2770
2771 static const struct regname reg_names_o32[] = {
2772 O32_SYMBOLIC_REGISTER_NAMES,
2773 {0, 0}
2774 };
2775
2776 static const struct regname reg_names_n32n64[] = {
2777 N32N64_SYMBOLIC_REGISTER_NAMES,
2778 {0, 0}
2779 };
2780
2781 /* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2782 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2783 of these register symbols, return the associated vector register,
2784 otherwise return SYMVAL itself. */
2785
2786 static unsigned int
2787 mips_prefer_vec_regno (unsigned int symval)
2788 {
2789 if ((symval & -2) == (RTYPE_GP | 2))
2790 return RTYPE_VEC | (symval & 1);
2791 return symval;
2792 }
2793
2794 /* Return true if string [S, E) is a valid register name, storing its
2795 symbol value in *SYMVAL_PTR if so. */
2796
2797 static bfd_boolean
2798 mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
2799 {
2800 char save_c;
2801 symbolS *symbol;
2802
2803 /* Terminate name. */
2804 save_c = *e;
2805 *e = '\0';
2806
2807 /* Look up the name. */
2808 symbol = symbol_find (s);
2809 *e = save_c;
2810
2811 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2812 return FALSE;
2813
2814 *symval_ptr = S_GET_VALUE (symbol);
2815 return TRUE;
2816 }
2817
2818 /* Return true if the string at *SPTR is a valid register name. Allow it
2819 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2820 is nonnull.
2821
2822 When returning true, move *SPTR past the register, store the
2823 register's symbol value in *SYMVAL_PTR and the channel mask in
2824 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2825 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2826 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2827
2828 static bfd_boolean
2829 mips_parse_register (char **sptr, unsigned int *symval_ptr,
2830 unsigned int *channels_ptr)
2831 {
2832 char *s, *e, *m;
2833 const char *q;
2834 unsigned int channels, symval, bit;
2835
2836 /* Find end of name. */
2837 s = e = *sptr;
2838 if (is_name_beginner (*e))
2839 ++e;
2840 while (is_part_of_name (*e))
2841 ++e;
2842
2843 channels = 0;
2844 if (!mips_parse_register_1 (s, e, &symval))
2845 {
2846 if (!channels_ptr)
2847 return FALSE;
2848
2849 /* Eat characters from the end of the string that are valid
2850 channel suffixes. The preceding register must be $ACC or
2851 end with a digit, so there is no ambiguity. */
2852 bit = 1;
2853 m = e;
2854 for (q = "wzyx"; *q; q++, bit <<= 1)
2855 if (m > s && m[-1] == *q)
2856 {
2857 --m;
2858 channels |= bit;
2859 }
2860
2861 if (channels == 0
2862 || !mips_parse_register_1 (s, m, &symval)
2863 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2864 return FALSE;
2865 }
2866
2867 *sptr = e;
2868 *symval_ptr = symval;
2869 if (channels_ptr)
2870 *channels_ptr = channels;
2871 return TRUE;
2872 }
2873
2874 /* Check if SPTR points at a valid register specifier according to TYPES.
2875 If so, then return 1, advance S to consume the specifier and store
2876 the register's number in REGNOP, otherwise return 0. */
2877
2878 static int
2879 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2880 {
2881 unsigned int regno;
2882
2883 if (mips_parse_register (s, &regno, NULL))
2884 {
2885 if (types & RTYPE_VEC)
2886 regno = mips_prefer_vec_regno (regno);
2887 if (regno & types)
2888 regno &= RNUM_MASK;
2889 else
2890 regno = ~0;
2891 }
2892 else
2893 {
2894 if (types & RWARN)
2895 as_warn (_("unrecognized register name `%s'"), *s);
2896 regno = ~0;
2897 }
2898 if (regnop)
2899 *regnop = regno;
2900 return regno <= RNUM_MASK;
2901 }
2902
2903 /* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2904 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2905
2906 static char *
2907 mips_parse_vu0_channels (char *s, unsigned int *channels)
2908 {
2909 unsigned int i;
2910
2911 *channels = 0;
2912 for (i = 0; i < 4; i++)
2913 if (*s == "xyzw"[i])
2914 {
2915 *channels |= 1 << (3 - i);
2916 ++s;
2917 }
2918 return s;
2919 }
2920
2921 /* Token types for parsed operand lists. */
2922 enum mips_operand_token_type {
2923 /* A plain register, e.g. $f2. */
2924 OT_REG,
2925
2926 /* A 4-bit XYZW channel mask. */
2927 OT_CHANNELS,
2928
2929 /* A constant vector index, e.g. [1]. */
2930 OT_INTEGER_INDEX,
2931
2932 /* A register vector index, e.g. [$2]. */
2933 OT_REG_INDEX,
2934
2935 /* A continuous range of registers, e.g. $s0-$s4. */
2936 OT_REG_RANGE,
2937
2938 /* A (possibly relocated) expression. */
2939 OT_INTEGER,
2940
2941 /* A floating-point value. */
2942 OT_FLOAT,
2943
2944 /* A single character. This can be '(', ')' or ',', but '(' only appears
2945 before OT_REGs. */
2946 OT_CHAR,
2947
2948 /* A doubled character, either "--" or "++". */
2949 OT_DOUBLE_CHAR,
2950
2951 /* The end of the operand list. */
2952 OT_END
2953 };
2954
2955 /* A parsed operand token. */
2956 struct mips_operand_token
2957 {
2958 /* The type of token. */
2959 enum mips_operand_token_type type;
2960 union
2961 {
2962 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
2963 unsigned int regno;
2964
2965 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
2966 unsigned int channels;
2967
2968 /* The integer value of an OT_INTEGER_INDEX. */
2969 addressT index;
2970
2971 /* The two register symbol values involved in an OT_REG_RANGE. */
2972 struct {
2973 unsigned int regno1;
2974 unsigned int regno2;
2975 } reg_range;
2976
2977 /* The value of an OT_INTEGER. The value is represented as an
2978 expression and the relocation operators that were applied to
2979 that expression. The reloc entries are BFD_RELOC_UNUSED if no
2980 relocation operators were used. */
2981 struct {
2982 expressionS value;
2983 bfd_reloc_code_real_type relocs[3];
2984 } integer;
2985
2986 /* The binary data for an OT_FLOAT constant, and the number of bytes
2987 in the constant. */
2988 struct {
2989 unsigned char data[8];
2990 int length;
2991 } flt;
2992
2993 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
2994 char ch;
2995 } u;
2996 };
2997
2998 /* An obstack used to construct lists of mips_operand_tokens. */
2999 static struct obstack mips_operand_tokens;
3000
3001 /* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3002
3003 static void
3004 mips_add_token (struct mips_operand_token *token,
3005 enum mips_operand_token_type type)
3006 {
3007 token->type = type;
3008 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3009 }
3010
3011 /* Check whether S is '(' followed by a register name. Add OT_CHAR
3012 and OT_REG tokens for them if so, and return a pointer to the first
3013 unconsumed character. Return null otherwise. */
3014
3015 static char *
3016 mips_parse_base_start (char *s)
3017 {
3018 struct mips_operand_token token;
3019 unsigned int regno, channels;
3020 bfd_boolean decrement_p;
3021
3022 if (*s != '(')
3023 return 0;
3024
3025 ++s;
3026 SKIP_SPACE_TABS (s);
3027
3028 /* Only match "--" as part of a base expression. In other contexts "--X"
3029 is a double negative. */
3030 decrement_p = (s[0] == '-' && s[1] == '-');
3031 if (decrement_p)
3032 {
3033 s += 2;
3034 SKIP_SPACE_TABS (s);
3035 }
3036
3037 /* Allow a channel specifier because that leads to better error messages
3038 than treating something like "$vf0x++" as an expression. */
3039 if (!mips_parse_register (&s, &regno, &channels))
3040 return 0;
3041
3042 token.u.ch = '(';
3043 mips_add_token (&token, OT_CHAR);
3044
3045 if (decrement_p)
3046 {
3047 token.u.ch = '-';
3048 mips_add_token (&token, OT_DOUBLE_CHAR);
3049 }
3050
3051 token.u.regno = regno;
3052 mips_add_token (&token, OT_REG);
3053
3054 if (channels)
3055 {
3056 token.u.channels = channels;
3057 mips_add_token (&token, OT_CHANNELS);
3058 }
3059
3060 /* For consistency, only match "++" as part of base expressions too. */
3061 SKIP_SPACE_TABS (s);
3062 if (s[0] == '+' && s[1] == '+')
3063 {
3064 s += 2;
3065 token.u.ch = '+';
3066 mips_add_token (&token, OT_DOUBLE_CHAR);
3067 }
3068
3069 return s;
3070 }
3071
3072 /* Parse one or more tokens from S. Return a pointer to the first
3073 unconsumed character on success. Return null if an error was found
3074 and store the error text in insn_error. FLOAT_FORMAT is as for
3075 mips_parse_arguments. */
3076
3077 static char *
3078 mips_parse_argument_token (char *s, char float_format)
3079 {
3080 char *end, *save_in;
3081 const char *err;
3082 unsigned int regno1, regno2, channels;
3083 struct mips_operand_token token;
3084
3085 /* First look for "($reg", since we want to treat that as an
3086 OT_CHAR and OT_REG rather than an expression. */
3087 end = mips_parse_base_start (s);
3088 if (end)
3089 return end;
3090
3091 /* Handle other characters that end up as OT_CHARs. */
3092 if (*s == ')' || *s == ',')
3093 {
3094 token.u.ch = *s;
3095 mips_add_token (&token, OT_CHAR);
3096 ++s;
3097 return s;
3098 }
3099
3100 /* Handle tokens that start with a register. */
3101 if (mips_parse_register (&s, &regno1, &channels))
3102 {
3103 if (channels)
3104 {
3105 /* A register and a VU0 channel suffix. */
3106 token.u.regno = regno1;
3107 mips_add_token (&token, OT_REG);
3108
3109 token.u.channels = channels;
3110 mips_add_token (&token, OT_CHANNELS);
3111 return s;
3112 }
3113
3114 SKIP_SPACE_TABS (s);
3115 if (*s == '-')
3116 {
3117 /* A register range. */
3118 ++s;
3119 SKIP_SPACE_TABS (s);
3120 if (!mips_parse_register (&s, &regno2, NULL))
3121 {
3122 set_insn_error (0, _("invalid register range"));
3123 return 0;
3124 }
3125
3126 token.u.reg_range.regno1 = regno1;
3127 token.u.reg_range.regno2 = regno2;
3128 mips_add_token (&token, OT_REG_RANGE);
3129 return s;
3130 }
3131
3132 /* Add the register itself. */
3133 token.u.regno = regno1;
3134 mips_add_token (&token, OT_REG);
3135
3136 /* Check for a vector index. */
3137 if (*s == '[')
3138 {
3139 ++s;
3140 SKIP_SPACE_TABS (s);
3141 if (mips_parse_register (&s, &token.u.regno, NULL))
3142 mips_add_token (&token, OT_REG_INDEX);
3143 else
3144 {
3145 expressionS element;
3146
3147 my_getExpression (&element, s);
3148 if (element.X_op != O_constant)
3149 {
3150 set_insn_error (0, _("vector element must be constant"));
3151 return 0;
3152 }
3153 s = expr_end;
3154 token.u.index = element.X_add_number;
3155 mips_add_token (&token, OT_INTEGER_INDEX);
3156 }
3157 SKIP_SPACE_TABS (s);
3158 if (*s != ']')
3159 {
3160 set_insn_error (0, _("missing `]'"));
3161 return 0;
3162 }
3163 ++s;
3164 }
3165 return s;
3166 }
3167
3168 if (float_format)
3169 {
3170 /* First try to treat expressions as floats. */
3171 save_in = input_line_pointer;
3172 input_line_pointer = s;
3173 err = md_atof (float_format, (char *) token.u.flt.data,
3174 &token.u.flt.length);
3175 end = input_line_pointer;
3176 input_line_pointer = save_in;
3177 if (err && *err)
3178 {
3179 set_insn_error (0, err);
3180 return 0;
3181 }
3182 if (s != end)
3183 {
3184 mips_add_token (&token, OT_FLOAT);
3185 return end;
3186 }
3187 }
3188
3189 /* Treat everything else as an integer expression. */
3190 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3191 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3192 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3193 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3194 s = expr_end;
3195 mips_add_token (&token, OT_INTEGER);
3196 return s;
3197 }
3198
3199 /* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3200 if expressions should be treated as 32-bit floating-point constants,
3201 'd' if they should be treated as 64-bit floating-point constants,
3202 or 0 if they should be treated as integer expressions (the usual case).
3203
3204 Return a list of tokens on success, otherwise return 0. The caller
3205 must obstack_free the list after use. */
3206
3207 static struct mips_operand_token *
3208 mips_parse_arguments (char *s, char float_format)
3209 {
3210 struct mips_operand_token token;
3211
3212 SKIP_SPACE_TABS (s);
3213 while (*s)
3214 {
3215 s = mips_parse_argument_token (s, float_format);
3216 if (!s)
3217 {
3218 obstack_free (&mips_operand_tokens,
3219 obstack_finish (&mips_operand_tokens));
3220 return 0;
3221 }
3222 SKIP_SPACE_TABS (s);
3223 }
3224 mips_add_token (&token, OT_END);
3225 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
3226 }
3227
3228 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3229 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
3230
3231 static bfd_boolean
3232 is_opcode_valid (const struct mips_opcode *mo)
3233 {
3234 int isa = mips_opts.isa;
3235 int ase = mips_opts.ase;
3236 int fp_s, fp_d;
3237 unsigned int i;
3238
3239 if (ISA_HAS_64BIT_REGS (isa))
3240 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3241 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3242 ase |= mips_ases[i].flags64;
3243
3244 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
3245 return FALSE;
3246
3247 /* Check whether the instruction or macro requires single-precision or
3248 double-precision floating-point support. Note that this information is
3249 stored differently in the opcode table for insns and macros. */
3250 if (mo->pinfo == INSN_MACRO)
3251 {
3252 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3253 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3254 }
3255 else
3256 {
3257 fp_s = mo->pinfo & FP_S;
3258 fp_d = mo->pinfo & FP_D;
3259 }
3260
3261 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3262 return FALSE;
3263
3264 if (fp_s && mips_opts.soft_float)
3265 return FALSE;
3266
3267 return TRUE;
3268 }
3269
3270 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
3271 selected ISA and architecture. */
3272
3273 static bfd_boolean
3274 is_opcode_valid_16 (const struct mips_opcode *mo)
3275 {
3276 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
3277 }
3278
3279 /* Return TRUE if the size of the microMIPS opcode MO matches one
3280 explicitly requested. Always TRUE in the standard MIPS mode.
3281 Use is_size_valid_16 for MIPS16 opcodes. */
3282
3283 static bfd_boolean
3284 is_size_valid (const struct mips_opcode *mo)
3285 {
3286 if (!mips_opts.micromips)
3287 return TRUE;
3288
3289 if (mips_opts.insn32)
3290 {
3291 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3292 return FALSE;
3293 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3294 return FALSE;
3295 }
3296 if (!forced_insn_length)
3297 return TRUE;
3298 if (mo->pinfo == INSN_MACRO)
3299 return FALSE;
3300 return forced_insn_length == micromips_insn_length (mo);
3301 }
3302
3303 /* Return TRUE if the size of the MIPS16 opcode MO matches one
3304 explicitly requested. */
3305
3306 static bfd_boolean
3307 is_size_valid_16 (const struct mips_opcode *mo)
3308 {
3309 if (!forced_insn_length)
3310 return TRUE;
3311 if (mo->pinfo == INSN_MACRO)
3312 return FALSE;
3313 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3314 return FALSE;
3315 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3316 return FALSE;
3317 return TRUE;
3318 }
3319
3320 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
3321 of the preceding instruction. Always TRUE in the standard MIPS mode.
3322
3323 We don't accept macros in 16-bit delay slots to avoid a case where
3324 a macro expansion fails because it relies on a preceding 32-bit real
3325 instruction to have matched and does not handle the operands correctly.
3326 The only macros that may expand to 16-bit instructions are JAL that
3327 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3328 and BGT (that likewise cannot be placed in a delay slot) that decay to
3329 a NOP. In all these cases the macros precede any corresponding real
3330 instruction definitions in the opcode table, so they will match in the
3331 second pass where the size of the delay slot is ignored and therefore
3332 produce correct code. */
3333
3334 static bfd_boolean
3335 is_delay_slot_valid (const struct mips_opcode *mo)
3336 {
3337 if (!mips_opts.micromips)
3338 return TRUE;
3339
3340 if (mo->pinfo == INSN_MACRO)
3341 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
3342 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3343 && micromips_insn_length (mo) != 4)
3344 return FALSE;
3345 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3346 && micromips_insn_length (mo) != 2)
3347 return FALSE;
3348
3349 return TRUE;
3350 }
3351
3352 /* For consistency checking, verify that all bits of OPCODE are specified
3353 either by the match/mask part of the instruction definition, or by the
3354 operand list. Also build up a list of operands in OPERANDS.
3355
3356 INSN_BITS says which bits of the instruction are significant.
3357 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3358 provides the mips_operand description of each operand. DECODE_OPERAND
3359 is null for MIPS16 instructions. */
3360
3361 static int
3362 validate_mips_insn (const struct mips_opcode *opcode,
3363 unsigned long insn_bits,
3364 const struct mips_operand *(*decode_operand) (const char *),
3365 struct mips_operand_array *operands)
3366 {
3367 const char *s;
3368 unsigned long used_bits, doubled, undefined, opno, mask;
3369 const struct mips_operand *operand;
3370
3371 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3372 if ((mask & opcode->match) != opcode->match)
3373 {
3374 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3375 opcode->name, opcode->args);
3376 return 0;
3377 }
3378 used_bits = 0;
3379 opno = 0;
3380 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3381 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
3382 for (s = opcode->args; *s; ++s)
3383 switch (*s)
3384 {
3385 case ',':
3386 case '(':
3387 case ')':
3388 break;
3389
3390 case '#':
3391 s++;
3392 break;
3393
3394 default:
3395 if (!decode_operand)
3396 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
3397 else
3398 operand = decode_operand (s);
3399 if (!operand && opcode->pinfo != INSN_MACRO)
3400 {
3401 as_bad (_("internal: unknown operand type: %s %s"),
3402 opcode->name, opcode->args);
3403 return 0;
3404 }
3405 gas_assert (opno < MAX_OPERANDS);
3406 operands->operand[opno] = operand;
3407 if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
3408 {
3409 used_bits = mips_insert_operand (operand, used_bits, -1);
3410 if (operand->type == OP_MDMX_IMM_REG)
3411 /* Bit 5 is the format selector (OB vs QH). The opcode table
3412 has separate entries for each format. */
3413 used_bits &= ~(1 << (operand->lsb + 5));
3414 if (operand->type == OP_ENTRY_EXIT_LIST)
3415 used_bits &= ~(mask & 0x700);
3416 }
3417 /* Skip prefix characters. */
3418 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
3419 ++s;
3420 opno += 1;
3421 break;
3422 }
3423 doubled = used_bits & mask & insn_bits;
3424 if (doubled)
3425 {
3426 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3427 " %s %s"), doubled, opcode->name, opcode->args);
3428 return 0;
3429 }
3430 used_bits |= mask;
3431 undefined = ~used_bits & insn_bits;
3432 if (opcode->pinfo != INSN_MACRO && undefined)
3433 {
3434 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3435 undefined, opcode->name, opcode->args);
3436 return 0;
3437 }
3438 used_bits &= ~insn_bits;
3439 if (used_bits)
3440 {
3441 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3442 used_bits, opcode->name, opcode->args);
3443 return 0;
3444 }
3445 return 1;
3446 }
3447
3448 /* The MIPS16 version of validate_mips_insn. */
3449
3450 static int
3451 validate_mips16_insn (const struct mips_opcode *opcode,
3452 struct mips_operand_array *operands)
3453 {
3454 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
3455
3456 return validate_mips_insn (opcode, insn_bits, 0, operands);
3457 }
3458
3459 /* The microMIPS version of validate_mips_insn. */
3460
3461 static int
3462 validate_micromips_insn (const struct mips_opcode *opc,
3463 struct mips_operand_array *operands)
3464 {
3465 unsigned long insn_bits;
3466 unsigned long major;
3467 unsigned int length;
3468
3469 if (opc->pinfo == INSN_MACRO)
3470 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3471 operands);
3472
3473 length = micromips_insn_length (opc);
3474 if (length != 2 && length != 4)
3475 {
3476 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
3477 "%s %s"), length, opc->name, opc->args);
3478 return 0;
3479 }
3480 major = opc->match >> (10 + 8 * (length - 2));
3481 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3482 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3483 {
3484 as_bad (_("internal error: bad microMIPS opcode "
3485 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3486 return 0;
3487 }
3488
3489 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3490 insn_bits = 1 << 4 * length;
3491 insn_bits <<= 4 * length;
3492 insn_bits -= 1;
3493 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3494 operands);
3495 }
3496
3497 /* This function is called once, at assembler startup time. It should set up
3498 all the tables, etc. that the MD part of the assembler will need. */
3499
3500 void
3501 md_begin (void)
3502 {
3503 const char *retval = NULL;
3504 int i = 0;
3505 int broken = 0;
3506
3507 if (mips_pic != NO_PIC)
3508 {
3509 if (g_switch_seen && g_switch_value != 0)
3510 as_bad (_("-G may not be used in position-independent code"));
3511 g_switch_value = 0;
3512 }
3513 else if (mips_abicalls)
3514 {
3515 if (g_switch_seen && g_switch_value != 0)
3516 as_bad (_("-G may not be used with abicalls"));
3517 g_switch_value = 0;
3518 }
3519
3520 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3521 as_warn (_("could not set architecture and machine"));
3522
3523 op_hash = hash_new ();
3524
3525 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
3526 for (i = 0; i < NUMOPCODES;)
3527 {
3528 const char *name = mips_opcodes[i].name;
3529
3530 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
3531 if (retval != NULL)
3532 {
3533 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3534 mips_opcodes[i].name, retval);
3535 /* Probably a memory allocation problem? Give up now. */
3536 as_fatal (_("broken assembler, no assembly attempted"));
3537 }
3538 do
3539 {
3540 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3541 decode_mips_operand, &mips_operands[i]))
3542 broken = 1;
3543 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3544 {
3545 create_insn (&nop_insn, mips_opcodes + i);
3546 if (mips_fix_loongson2f_nop)
3547 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3548 nop_insn.fixed_p = 1;
3549 }
3550 ++i;
3551 }
3552 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3553 }
3554
3555 mips16_op_hash = hash_new ();
3556 mips16_operands = XCNEWVEC (struct mips_operand_array,
3557 bfd_mips16_num_opcodes);
3558
3559 i = 0;
3560 while (i < bfd_mips16_num_opcodes)
3561 {
3562 const char *name = mips16_opcodes[i].name;
3563
3564 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
3565 if (retval != NULL)
3566 as_fatal (_("internal: can't hash `%s': %s"),
3567 mips16_opcodes[i].name, retval);
3568 do
3569 {
3570 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3571 broken = 1;
3572 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3573 {
3574 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3575 mips16_nop_insn.fixed_p = 1;
3576 }
3577 ++i;
3578 }
3579 while (i < bfd_mips16_num_opcodes
3580 && strcmp (mips16_opcodes[i].name, name) == 0);
3581 }
3582
3583 micromips_op_hash = hash_new ();
3584 micromips_operands = XCNEWVEC (struct mips_operand_array,
3585 bfd_micromips_num_opcodes);
3586
3587 i = 0;
3588 while (i < bfd_micromips_num_opcodes)
3589 {
3590 const char *name = micromips_opcodes[i].name;
3591
3592 retval = hash_insert (micromips_op_hash, name,
3593 (void *) &micromips_opcodes[i]);
3594 if (retval != NULL)
3595 as_fatal (_("internal: can't hash `%s': %s"),
3596 micromips_opcodes[i].name, retval);
3597 do
3598 {
3599 struct mips_cl_insn *micromips_nop_insn;
3600
3601 if (!validate_micromips_insn (&micromips_opcodes[i],
3602 &micromips_operands[i]))
3603 broken = 1;
3604
3605 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3606 {
3607 if (micromips_insn_length (micromips_opcodes + i) == 2)
3608 micromips_nop_insn = &micromips_nop16_insn;
3609 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3610 micromips_nop_insn = &micromips_nop32_insn;
3611 else
3612 continue;
3613
3614 if (micromips_nop_insn->insn_mo == NULL
3615 && strcmp (name, "nop") == 0)
3616 {
3617 create_insn (micromips_nop_insn, micromips_opcodes + i);
3618 micromips_nop_insn->fixed_p = 1;
3619 }
3620 }
3621 }
3622 while (++i < bfd_micromips_num_opcodes
3623 && strcmp (micromips_opcodes[i].name, name) == 0);
3624 }
3625
3626 if (broken)
3627 as_fatal (_("broken assembler, no assembly attempted"));
3628
3629 /* We add all the general register names to the symbol table. This
3630 helps us detect invalid uses of them. */
3631 for (i = 0; reg_names[i].name; i++)
3632 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
3633 reg_names[i].num, /* & RNUM_MASK, */
3634 &zero_address_frag));
3635 if (HAVE_NEWABI)
3636 for (i = 0; reg_names_n32n64[i].name; i++)
3637 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
3638 reg_names_n32n64[i].num, /* & RNUM_MASK, */
3639 &zero_address_frag));
3640 else
3641 for (i = 0; reg_names_o32[i].name; i++)
3642 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
3643 reg_names_o32[i].num, /* & RNUM_MASK, */
3644 &zero_address_frag));
3645
3646 for (i = 0; i < 32; i++)
3647 {
3648 char regname[6];
3649
3650 /* R5900 VU0 floating-point register. */
3651 sprintf (regname, "$vf%d", i);
3652 symbol_table_insert (symbol_new (regname, reg_section,
3653 RTYPE_VF | i, &zero_address_frag));
3654
3655 /* R5900 VU0 integer register. */
3656 sprintf (regname, "$vi%d", i);
3657 symbol_table_insert (symbol_new (regname, reg_section,
3658 RTYPE_VI | i, &zero_address_frag));
3659
3660 /* MSA register. */
3661 sprintf (regname, "$w%d", i);
3662 symbol_table_insert (symbol_new (regname, reg_section,
3663 RTYPE_MSA | i, &zero_address_frag));
3664 }
3665
3666 obstack_init (&mips_operand_tokens);
3667
3668 mips_no_prev_insn ();
3669
3670 mips_gprmask = 0;
3671 mips_cprmask[0] = 0;
3672 mips_cprmask[1] = 0;
3673 mips_cprmask[2] = 0;
3674 mips_cprmask[3] = 0;
3675
3676 /* set the default alignment for the text section (2**2) */
3677 record_alignment (text_section, 2);
3678
3679 bfd_set_gp_size (stdoutput, g_switch_value);
3680
3681 /* On a native system other than VxWorks, sections must be aligned
3682 to 16 byte boundaries. When configured for an embedded ELF
3683 target, we don't bother. */
3684 if (strncmp (TARGET_OS, "elf", 3) != 0
3685 && strncmp (TARGET_OS, "vxworks", 7) != 0)
3686 {
3687 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3688 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3689 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3690 }
3691
3692 /* Create a .reginfo section for register masks and a .mdebug
3693 section for debugging information. */
3694 {
3695 segT seg;
3696 subsegT subseg;
3697 flagword flags;
3698 segT sec;
3699
3700 seg = now_seg;
3701 subseg = now_subseg;
3702
3703 /* The ABI says this section should be loaded so that the
3704 running program can access it. However, we don't load it
3705 if we are configured for an embedded target */
3706 flags = SEC_READONLY | SEC_DATA;
3707 if (strncmp (TARGET_OS, "elf", 3) != 0)
3708 flags |= SEC_ALLOC | SEC_LOAD;
3709
3710 if (mips_abi != N64_ABI)
3711 {
3712 sec = subseg_new (".reginfo", (subsegT) 0);
3713
3714 bfd_set_section_flags (stdoutput, sec, flags);
3715 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
3716
3717 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3718 }
3719 else
3720 {
3721 /* The 64-bit ABI uses a .MIPS.options section rather than
3722 .reginfo section. */
3723 sec = subseg_new (".MIPS.options", (subsegT) 0);
3724 bfd_set_section_flags (stdoutput, sec, flags);
3725 bfd_set_section_alignment (stdoutput, sec, 3);
3726
3727 /* Set up the option header. */
3728 {
3729 Elf_Internal_Options opthdr;
3730 char *f;
3731
3732 opthdr.kind = ODK_REGINFO;
3733 opthdr.size = (sizeof (Elf_External_Options)
3734 + sizeof (Elf64_External_RegInfo));
3735 opthdr.section = 0;
3736 opthdr.info = 0;
3737 f = frag_more (sizeof (Elf_External_Options));
3738 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3739 (Elf_External_Options *) f);
3740
3741 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3742 }
3743 }
3744
3745 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3746 bfd_set_section_flags (stdoutput, sec,
3747 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3748 bfd_set_section_alignment (stdoutput, sec, 3);
3749 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3750
3751 if (ECOFF_DEBUGGING)
3752 {
3753 sec = subseg_new (".mdebug", (subsegT) 0);
3754 (void) bfd_set_section_flags (stdoutput, sec,
3755 SEC_HAS_CONTENTS | SEC_READONLY);
3756 (void) bfd_set_section_alignment (stdoutput, sec, 2);
3757 }
3758 else if (mips_flag_pdr)
3759 {
3760 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3761 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3762 SEC_READONLY | SEC_RELOC
3763 | SEC_DEBUGGING);
3764 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3765 }
3766
3767 subseg_set (seg, subseg);
3768 }
3769
3770 if (mips_fix_vr4120)
3771 init_vr4120_conflicts ();
3772 }
3773
3774 static inline void
3775 fpabi_incompatible_with (int fpabi, const char *what)
3776 {
3777 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3778 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3779 }
3780
3781 static inline void
3782 fpabi_requires (int fpabi, const char *what)
3783 {
3784 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3785 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3786 }
3787
3788 /* Check -mabi and register sizes against the specified FP ABI. */
3789 static void
3790 check_fpabi (int fpabi)
3791 {
3792 switch (fpabi)
3793 {
3794 case Val_GNU_MIPS_ABI_FP_DOUBLE:
3795 if (file_mips_opts.soft_float)
3796 fpabi_incompatible_with (fpabi, "softfloat");
3797 else if (file_mips_opts.single_float)
3798 fpabi_incompatible_with (fpabi, "singlefloat");
3799 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3800 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3801 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3802 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
3803 break;
3804
3805 case Val_GNU_MIPS_ABI_FP_XX:
3806 if (mips_abi != O32_ABI)
3807 fpabi_requires (fpabi, "-mabi=32");
3808 else if (file_mips_opts.soft_float)
3809 fpabi_incompatible_with (fpabi, "softfloat");
3810 else if (file_mips_opts.single_float)
3811 fpabi_incompatible_with (fpabi, "singlefloat");
3812 else if (file_mips_opts.fp != 0)
3813 fpabi_requires (fpabi, "fp=xx");
3814 break;
3815
3816 case Val_GNU_MIPS_ABI_FP_64A:
3817 case Val_GNU_MIPS_ABI_FP_64:
3818 if (mips_abi != O32_ABI)
3819 fpabi_requires (fpabi, "-mabi=32");
3820 else if (file_mips_opts.soft_float)
3821 fpabi_incompatible_with (fpabi, "softfloat");
3822 else if (file_mips_opts.single_float)
3823 fpabi_incompatible_with (fpabi, "singlefloat");
3824 else if (file_mips_opts.fp != 64)
3825 fpabi_requires (fpabi, "fp=64");
3826 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3827 fpabi_incompatible_with (fpabi, "nooddspreg");
3828 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3829 fpabi_requires (fpabi, "nooddspreg");
3830 break;
3831
3832 case Val_GNU_MIPS_ABI_FP_SINGLE:
3833 if (file_mips_opts.soft_float)
3834 fpabi_incompatible_with (fpabi, "softfloat");
3835 else if (!file_mips_opts.single_float)
3836 fpabi_requires (fpabi, "singlefloat");
3837 break;
3838
3839 case Val_GNU_MIPS_ABI_FP_SOFT:
3840 if (!file_mips_opts.soft_float)
3841 fpabi_requires (fpabi, "softfloat");
3842 break;
3843
3844 case Val_GNU_MIPS_ABI_FP_OLD_64:
3845 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3846 Tag_GNU_MIPS_ABI_FP, fpabi);
3847 break;
3848
3849 case Val_GNU_MIPS_ABI_FP_NAN2008:
3850 /* Silently ignore compatibility value. */
3851 break;
3852
3853 default:
3854 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3855 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3856 break;
3857 }
3858 }
3859
3860 /* Perform consistency checks on the current options. */
3861
3862 static void
3863 mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3864 {
3865 /* Check the size of integer registers agrees with the ABI and ISA. */
3866 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3867 as_bad (_("`gp=64' used with a 32-bit processor"));
3868 else if (abi_checks
3869 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3870 as_bad (_("`gp=32' used with a 64-bit ABI"));
3871 else if (abi_checks
3872 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3873 as_bad (_("`gp=64' used with a 32-bit ABI"));
3874
3875 /* Check the size of the float registers agrees with the ABI and ISA. */
3876 switch (opts->fp)
3877 {
3878 case 0:
3879 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
3880 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
3881 else if (opts->single_float == 1)
3882 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
3883 break;
3884 case 64:
3885 if (!ISA_HAS_64BIT_FPRS (opts->isa))
3886 as_bad (_("`fp=64' used with a 32-bit fpu"));
3887 else if (abi_checks
3888 && ABI_NEEDS_32BIT_REGS (mips_abi)
3889 && !ISA_HAS_MXHC1 (opts->isa))
3890 as_warn (_("`fp=64' used with a 32-bit ABI"));
3891 break;
3892 case 32:
3893 if (abi_checks
3894 && ABI_NEEDS_64BIT_REGS (mips_abi))
3895 as_warn (_("`fp=32' used with a 64-bit ABI"));
3896 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
3897 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
3898 break;
3899 default:
3900 as_bad (_("Unknown size of floating point registers"));
3901 break;
3902 }
3903
3904 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
3905 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
3906
3907 if (opts->micromips == 1 && opts->mips16 == 1)
3908 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
3909 else if (ISA_IS_R6 (opts->isa)
3910 && (opts->micromips == 1
3911 || opts->mips16 == 1))
3912 as_fatal (_("`%s' cannot be used with `%s'"),
3913 opts->micromips ? "micromips" : "mips16",
3914 mips_cpu_info_from_isa (opts->isa)->name);
3915
3916 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
3917 as_fatal (_("branch relaxation is not supported in `%s'"),
3918 mips_cpu_info_from_isa (opts->isa)->name);
3919 }
3920
3921 /* Perform consistency checks on the module level options exactly once.
3922 This is a deferred check that happens:
3923 at the first .set directive
3924 or, at the first pseudo op that generates code (inc .dc.a)
3925 or, at the first instruction
3926 or, at the end. */
3927
3928 static void
3929 file_mips_check_options (void)
3930 {
3931 const struct mips_cpu_info *arch_info = 0;
3932
3933 if (file_mips_opts_checked)
3934 return;
3935
3936 /* The following code determines the register size.
3937 Similar code was added to GCC 3.3 (see override_options() in
3938 config/mips/mips.c). The GAS and GCC code should be kept in sync
3939 as much as possible. */
3940
3941 if (file_mips_opts.gp < 0)
3942 {
3943 /* Infer the integer register size from the ABI and processor.
3944 Restrict ourselves to 32-bit registers if that's all the
3945 processor has, or if the ABI cannot handle 64-bit registers. */
3946 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
3947 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
3948 ? 32 : 64;
3949 }
3950
3951 if (file_mips_opts.fp < 0)
3952 {
3953 /* No user specified float register size.
3954 ??? GAS treats single-float processors as though they had 64-bit
3955 float registers (although it complains when double-precision
3956 instructions are used). As things stand, saying they have 32-bit
3957 registers would lead to spurious "register must be even" messages.
3958 So here we assume float registers are never smaller than the
3959 integer ones. */
3960 if (file_mips_opts.gp == 64)
3961 /* 64-bit integer registers implies 64-bit float registers. */
3962 file_mips_opts.fp = 64;
3963 else if ((file_mips_opts.ase & FP64_ASES)
3964 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
3965 /* Handle ASEs that require 64-bit float registers, if possible. */
3966 file_mips_opts.fp = 64;
3967 else if (ISA_IS_R6 (mips_opts.isa))
3968 /* R6 implies 64-bit float registers. */
3969 file_mips_opts.fp = 64;
3970 else
3971 /* 32-bit float registers. */
3972 file_mips_opts.fp = 32;
3973 }
3974
3975 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
3976
3977 /* Disable operations on odd-numbered floating-point registers by default
3978 when using the FPXX ABI. */
3979 if (file_mips_opts.oddspreg < 0)
3980 {
3981 if (file_mips_opts.fp == 0)
3982 file_mips_opts.oddspreg = 0;
3983 else
3984 file_mips_opts.oddspreg = 1;
3985 }
3986
3987 /* End of GCC-shared inference code. */
3988
3989 /* This flag is set when we have a 64-bit capable CPU but use only
3990 32-bit wide registers. Note that EABI does not use it. */
3991 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
3992 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
3993 || mips_abi == O32_ABI))
3994 mips_32bitmode = 1;
3995
3996 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
3997 as_bad (_("trap exception not supported at ISA 1"));
3998
3999 /* If the selected architecture includes support for ASEs, enable
4000 generation of code for them. */
4001 if (file_mips_opts.mips16 == -1)
4002 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4003 if (file_mips_opts.micromips == -1)
4004 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4005 ? 1 : 0;
4006
4007 if (mips_nan2008 == -1)
4008 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4009 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4010 as_fatal (_("`%s' does not support legacy NaN"),
4011 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4012
4013 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4014 being selected implicitly. */
4015 if (file_mips_opts.fp != 64)
4016 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4017
4018 /* If the user didn't explicitly select or deselect a particular ASE,
4019 use the default setting for the CPU. */
4020 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4021
4022 /* Set up the current options. These may change throughout assembly. */
4023 mips_opts = file_mips_opts;
4024
4025 mips_check_isa_supports_ases ();
4026 mips_check_options (&file_mips_opts, TRUE);
4027 file_mips_opts_checked = TRUE;
4028
4029 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4030 as_warn (_("could not set architecture and machine"));
4031 }
4032
4033 void
4034 md_assemble (char *str)
4035 {
4036 struct mips_cl_insn insn;
4037 bfd_reloc_code_real_type unused_reloc[3]
4038 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
4039
4040 file_mips_check_options ();
4041
4042 imm_expr.X_op = O_absent;
4043 offset_expr.X_op = O_absent;
4044 offset_reloc[0] = BFD_RELOC_UNUSED;
4045 offset_reloc[1] = BFD_RELOC_UNUSED;
4046 offset_reloc[2] = BFD_RELOC_UNUSED;
4047
4048 mips_mark_labels ();
4049 mips_assembling_insn = TRUE;
4050 clear_insn_error ();
4051
4052 if (mips_opts.mips16)
4053 mips16_ip (str, &insn);
4054 else
4055 {
4056 mips_ip (str, &insn);
4057 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4058 str, insn.insn_opcode));
4059 }
4060
4061 if (insn_error.msg)
4062 report_insn_error (str);
4063 else if (insn.insn_mo->pinfo == INSN_MACRO)
4064 {
4065 macro_start ();
4066 if (mips_opts.mips16)
4067 mips16_macro (&insn);
4068 else
4069 macro (&insn, str);
4070 macro_end ();
4071 }
4072 else
4073 {
4074 if (offset_expr.X_op != O_absent)
4075 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
4076 else
4077 append_insn (&insn, NULL, unused_reloc, FALSE);
4078 }
4079
4080 mips_assembling_insn = FALSE;
4081 }
4082
4083 /* Convenience functions for abstracting away the differences between
4084 MIPS16 and non-MIPS16 relocations. */
4085
4086 static inline bfd_boolean
4087 mips16_reloc_p (bfd_reloc_code_real_type reloc)
4088 {
4089 switch (reloc)
4090 {
4091 case BFD_RELOC_MIPS16_JMP:
4092 case BFD_RELOC_MIPS16_GPREL:
4093 case BFD_RELOC_MIPS16_GOT16:
4094 case BFD_RELOC_MIPS16_CALL16:
4095 case BFD_RELOC_MIPS16_HI16_S:
4096 case BFD_RELOC_MIPS16_HI16:
4097 case BFD_RELOC_MIPS16_LO16:
4098 case BFD_RELOC_MIPS16_16_PCREL_S1:
4099 return TRUE;
4100
4101 default:
4102 return FALSE;
4103 }
4104 }
4105
4106 static inline bfd_boolean
4107 micromips_reloc_p (bfd_reloc_code_real_type reloc)
4108 {
4109 switch (reloc)
4110 {
4111 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4112 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4113 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4114 case BFD_RELOC_MICROMIPS_GPREL16:
4115 case BFD_RELOC_MICROMIPS_JMP:
4116 case BFD_RELOC_MICROMIPS_HI16:
4117 case BFD_RELOC_MICROMIPS_HI16_S:
4118 case BFD_RELOC_MICROMIPS_LO16:
4119 case BFD_RELOC_MICROMIPS_LITERAL:
4120 case BFD_RELOC_MICROMIPS_GOT16:
4121 case BFD_RELOC_MICROMIPS_CALL16:
4122 case BFD_RELOC_MICROMIPS_GOT_HI16:
4123 case BFD_RELOC_MICROMIPS_GOT_LO16:
4124 case BFD_RELOC_MICROMIPS_CALL_HI16:
4125 case BFD_RELOC_MICROMIPS_CALL_LO16:
4126 case BFD_RELOC_MICROMIPS_SUB:
4127 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4128 case BFD_RELOC_MICROMIPS_GOT_OFST:
4129 case BFD_RELOC_MICROMIPS_GOT_DISP:
4130 case BFD_RELOC_MICROMIPS_HIGHEST:
4131 case BFD_RELOC_MICROMIPS_HIGHER:
4132 case BFD_RELOC_MICROMIPS_SCN_DISP:
4133 case BFD_RELOC_MICROMIPS_JALR:
4134 return TRUE;
4135
4136 default:
4137 return FALSE;
4138 }
4139 }
4140
4141 static inline bfd_boolean
4142 jmp_reloc_p (bfd_reloc_code_real_type reloc)
4143 {
4144 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4145 }
4146
4147 static inline bfd_boolean
4148 b_reloc_p (bfd_reloc_code_real_type reloc)
4149 {
4150 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4151 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4152 || reloc == BFD_RELOC_16_PCREL_S2
4153 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
4154 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4155 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4156 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4157 }
4158
4159 static inline bfd_boolean
4160 got16_reloc_p (bfd_reloc_code_real_type reloc)
4161 {
4162 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
4163 || reloc == BFD_RELOC_MICROMIPS_GOT16);
4164 }
4165
4166 static inline bfd_boolean
4167 hi16_reloc_p (bfd_reloc_code_real_type reloc)
4168 {
4169 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
4170 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
4171 }
4172
4173 static inline bfd_boolean
4174 lo16_reloc_p (bfd_reloc_code_real_type reloc)
4175 {
4176 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
4177 || reloc == BFD_RELOC_MICROMIPS_LO16);
4178 }
4179
4180 static inline bfd_boolean
4181 jalr_reloc_p (bfd_reloc_code_real_type reloc)
4182 {
4183 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
4184 }
4185
4186 static inline bfd_boolean
4187 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4188 {
4189 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4190 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4191 }
4192
4193 /* Return true if RELOC is a PC-relative relocation that does not have
4194 full address range. */
4195
4196 static inline bfd_boolean
4197 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4198 {
4199 switch (reloc)
4200 {
4201 case BFD_RELOC_16_PCREL_S2:
4202 case BFD_RELOC_MIPS16_16_PCREL_S1:
4203 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4204 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4205 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4206 case BFD_RELOC_MIPS_21_PCREL_S2:
4207 case BFD_RELOC_MIPS_26_PCREL_S2:
4208 case BFD_RELOC_MIPS_18_PCREL_S3:
4209 case BFD_RELOC_MIPS_19_PCREL_S2:
4210 return TRUE;
4211
4212 case BFD_RELOC_32_PCREL:
4213 case BFD_RELOC_HI16_S_PCREL:
4214 case BFD_RELOC_LO16_PCREL:
4215 return HAVE_64BIT_ADDRESSES;
4216
4217 default:
4218 return FALSE;
4219 }
4220 }
4221
4222 /* Return true if the given relocation might need a matching %lo().
4223 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4224 need a matching %lo() when applied to local symbols. */
4225
4226 static inline bfd_boolean
4227 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
4228 {
4229 return (HAVE_IN_PLACE_ADDENDS
4230 && (hi16_reloc_p (reloc)
4231 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4232 all GOT16 relocations evaluate to "G". */
4233 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4234 }
4235
4236 /* Return the type of %lo() reloc needed by RELOC, given that
4237 reloc_needs_lo_p. */
4238
4239 static inline bfd_reloc_code_real_type
4240 matching_lo_reloc (bfd_reloc_code_real_type reloc)
4241 {
4242 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4243 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4244 : BFD_RELOC_LO16));
4245 }
4246
4247 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
4248 relocation. */
4249
4250 static inline bfd_boolean
4251 fixup_has_matching_lo_p (fixS *fixp)
4252 {
4253 return (fixp->fx_next != NULL
4254 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
4255 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4256 && fixp->fx_offset == fixp->fx_next->fx_offset);
4257 }
4258
4259 /* Move all labels in LABELS to the current insertion point. TEXT_P
4260 says whether the labels refer to text or data. */
4261
4262 static void
4263 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
4264 {
4265 struct insn_label_list *l;
4266 valueT val;
4267
4268 for (l = labels; l != NULL; l = l->next)
4269 {
4270 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
4271 symbol_set_frag (l->label, frag_now);
4272 val = (valueT) frag_now_fix ();
4273 /* MIPS16/microMIPS text labels are stored as odd. */
4274 if (text_p && HAVE_CODE_COMPRESSION)
4275 ++val;
4276 S_SET_VALUE (l->label, val);
4277 }
4278 }
4279
4280 /* Move all labels in insn_labels to the current insertion point
4281 and treat them as text labels. */
4282
4283 static void
4284 mips_move_text_labels (void)
4285 {
4286 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4287 }
4288
4289 /* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4290
4291 static bfd_boolean
4292 s_is_linkonce (symbolS *sym, segT from_seg)
4293 {
4294 bfd_boolean linkonce = FALSE;
4295 segT symseg = S_GET_SEGMENT (sym);
4296
4297 if (symseg != from_seg && !S_IS_LOCAL (sym))
4298 {
4299 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4300 linkonce = TRUE;
4301 /* The GNU toolchain uses an extension for ELF: a section
4302 beginning with the magic string .gnu.linkonce is a
4303 linkonce section. */
4304 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4305 sizeof ".gnu.linkonce" - 1) == 0)
4306 linkonce = TRUE;
4307 }
4308 return linkonce;
4309 }
4310
4311 /* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
4312 linker to handle them specially, such as generating jalx instructions
4313 when needed. We also make them odd for the duration of the assembly,
4314 in order to generate the right sort of code. We will make them even
4315 in the adjust_symtab routine, while leaving them marked. This is
4316 convenient for the debugger and the disassembler. The linker knows
4317 to make them odd again. */
4318
4319 static void
4320 mips_compressed_mark_label (symbolS *label)
4321 {
4322 gas_assert (HAVE_CODE_COMPRESSION);
4323
4324 if (mips_opts.mips16)
4325 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4326 else
4327 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
4328 if ((S_GET_VALUE (label) & 1) == 0
4329 /* Don't adjust the address if the label is global or weak, or
4330 in a link-once section, since we'll be emitting symbol reloc
4331 references to it which will be patched up by the linker, and
4332 the final value of the symbol may or may not be MIPS16/microMIPS. */
4333 && !S_IS_WEAK (label)
4334 && !S_IS_EXTERNAL (label)
4335 && !s_is_linkonce (label, now_seg))
4336 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4337 }
4338
4339 /* Mark preceding MIPS16 or microMIPS instruction labels. */
4340
4341 static void
4342 mips_compressed_mark_labels (void)
4343 {
4344 struct insn_label_list *l;
4345
4346 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4347 mips_compressed_mark_label (l->label);
4348 }
4349
4350 /* End the current frag. Make it a variant frag and record the
4351 relaxation info. */
4352
4353 static void
4354 relax_close_frag (void)
4355 {
4356 mips_macro_warning.first_frag = frag_now;
4357 frag_var (rs_machine_dependent, 0, 0,
4358 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4359 mips_pic != NO_PIC),
4360 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4361
4362 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4363 mips_relax.first_fixup = 0;
4364 }
4365
4366 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
4367 See the comment above RELAX_ENCODE for more details. */
4368
4369 static void
4370 relax_start (symbolS *symbol)
4371 {
4372 gas_assert (mips_relax.sequence == 0);
4373 mips_relax.sequence = 1;
4374 mips_relax.symbol = symbol;
4375 }
4376
4377 /* Start generating the second version of a relaxable sequence.
4378 See the comment above RELAX_ENCODE for more details. */
4379
4380 static void
4381 relax_switch (void)
4382 {
4383 gas_assert (mips_relax.sequence == 1);
4384 mips_relax.sequence = 2;
4385 }
4386
4387 /* End the current relaxable sequence. */
4388
4389 static void
4390 relax_end (void)
4391 {
4392 gas_assert (mips_relax.sequence == 2);
4393 relax_close_frag ();
4394 mips_relax.sequence = 0;
4395 }
4396
4397 /* Return true if IP is a delayed branch or jump. */
4398
4399 static inline bfd_boolean
4400 delayed_branch_p (const struct mips_cl_insn *ip)
4401 {
4402 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4403 | INSN_COND_BRANCH_DELAY
4404 | INSN_COND_BRANCH_LIKELY)) != 0;
4405 }
4406
4407 /* Return true if IP is a compact branch or jump. */
4408
4409 static inline bfd_boolean
4410 compact_branch_p (const struct mips_cl_insn *ip)
4411 {
4412 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4413 | INSN2_COND_BRANCH)) != 0;
4414 }
4415
4416 /* Return true if IP is an unconditional branch or jump. */
4417
4418 static inline bfd_boolean
4419 uncond_branch_p (const struct mips_cl_insn *ip)
4420 {
4421 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
4422 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
4423 }
4424
4425 /* Return true if IP is a branch-likely instruction. */
4426
4427 static inline bfd_boolean
4428 branch_likely_p (const struct mips_cl_insn *ip)
4429 {
4430 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4431 }
4432
4433 /* Return the type of nop that should be used to fill the delay slot
4434 of delayed branch IP. */
4435
4436 static struct mips_cl_insn *
4437 get_delay_slot_nop (const struct mips_cl_insn *ip)
4438 {
4439 if (mips_opts.micromips
4440 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4441 return &micromips_nop32_insn;
4442 return NOP_INSN;
4443 }
4444
4445 /* Return a mask that has bit N set if OPCODE reads the register(s)
4446 in operand N. */
4447
4448 static unsigned int
4449 insn_read_mask (const struct mips_opcode *opcode)
4450 {
4451 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4452 }
4453
4454 /* Return a mask that has bit N set if OPCODE writes to the register(s)
4455 in operand N. */
4456
4457 static unsigned int
4458 insn_write_mask (const struct mips_opcode *opcode)
4459 {
4460 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4461 }
4462
4463 /* Return a mask of the registers specified by operand OPERAND of INSN.
4464 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4465 is set. */
4466
4467 static unsigned int
4468 operand_reg_mask (const struct mips_cl_insn *insn,
4469 const struct mips_operand *operand,
4470 unsigned int type_mask)
4471 {
4472 unsigned int uval, vsel;
4473
4474 switch (operand->type)
4475 {
4476 case OP_INT:
4477 case OP_MAPPED_INT:
4478 case OP_MSB:
4479 case OP_PCREL:
4480 case OP_PERF_REG:
4481 case OP_ADDIUSP_INT:
4482 case OP_ENTRY_EXIT_LIST:
4483 case OP_REPEAT_DEST_REG:
4484 case OP_REPEAT_PREV_REG:
4485 case OP_PC:
4486 case OP_VU0_SUFFIX:
4487 case OP_VU0_MATCH_SUFFIX:
4488 case OP_IMM_INDEX:
4489 abort ();
4490
4491 case OP_REG:
4492 case OP_OPTIONAL_REG:
4493 {
4494 const struct mips_reg_operand *reg_op;
4495
4496 reg_op = (const struct mips_reg_operand *) operand;
4497 if (!(type_mask & (1 << reg_op->reg_type)))
4498 return 0;
4499 uval = insn_extract_operand (insn, operand);
4500 return 1 << mips_decode_reg_operand (reg_op, uval);
4501 }
4502
4503 case OP_REG_PAIR:
4504 {
4505 const struct mips_reg_pair_operand *pair_op;
4506
4507 pair_op = (const struct mips_reg_pair_operand *) operand;
4508 if (!(type_mask & (1 << pair_op->reg_type)))
4509 return 0;
4510 uval = insn_extract_operand (insn, operand);
4511 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4512 }
4513
4514 case OP_CLO_CLZ_DEST:
4515 if (!(type_mask & (1 << OP_REG_GP)))
4516 return 0;
4517 uval = insn_extract_operand (insn, operand);
4518 return (1 << (uval & 31)) | (1 << (uval >> 5));
4519
4520 case OP_SAME_RS_RT:
4521 if (!(type_mask & (1 << OP_REG_GP)))
4522 return 0;
4523 uval = insn_extract_operand (insn, operand);
4524 gas_assert ((uval & 31) == (uval >> 5));
4525 return 1 << (uval & 31);
4526
4527 case OP_CHECK_PREV:
4528 case OP_NON_ZERO_REG:
4529 if (!(type_mask & (1 << OP_REG_GP)))
4530 return 0;
4531 uval = insn_extract_operand (insn, operand);
4532 return 1 << (uval & 31);
4533
4534 case OP_LWM_SWM_LIST:
4535 abort ();
4536
4537 case OP_SAVE_RESTORE_LIST:
4538 abort ();
4539
4540 case OP_MDMX_IMM_REG:
4541 if (!(type_mask & (1 << OP_REG_VEC)))
4542 return 0;
4543 uval = insn_extract_operand (insn, operand);
4544 vsel = uval >> 5;
4545 if ((vsel & 0x18) == 0x18)
4546 return 0;
4547 return 1 << (uval & 31);
4548
4549 case OP_REG_INDEX:
4550 if (!(type_mask & (1 << OP_REG_GP)))
4551 return 0;
4552 return 1 << insn_extract_operand (insn, operand);
4553 }
4554 abort ();
4555 }
4556
4557 /* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4558 where bit N of OPNO_MASK is set if operand N should be included.
4559 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4560 is set. */
4561
4562 static unsigned int
4563 insn_reg_mask (const struct mips_cl_insn *insn,
4564 unsigned int type_mask, unsigned int opno_mask)
4565 {
4566 unsigned int opno, reg_mask;
4567
4568 opno = 0;
4569 reg_mask = 0;
4570 while (opno_mask != 0)
4571 {
4572 if (opno_mask & 1)
4573 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4574 opno_mask >>= 1;
4575 opno += 1;
4576 }
4577 return reg_mask;
4578 }
4579
4580 /* Return the mask of core registers that IP reads. */
4581
4582 static unsigned int
4583 gpr_read_mask (const struct mips_cl_insn *ip)
4584 {
4585 unsigned long pinfo, pinfo2;
4586 unsigned int mask;
4587
4588 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4589 pinfo = ip->insn_mo->pinfo;
4590 pinfo2 = ip->insn_mo->pinfo2;
4591 if (pinfo & INSN_UDI)
4592 {
4593 /* UDI instructions have traditionally been assumed to read RS
4594 and RT. */
4595 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4596 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4597 }
4598 if (pinfo & INSN_READ_GPR_24)
4599 mask |= 1 << 24;
4600 if (pinfo2 & INSN2_READ_GPR_16)
4601 mask |= 1 << 16;
4602 if (pinfo2 & INSN2_READ_SP)
4603 mask |= 1 << SP;
4604 if (pinfo2 & INSN2_READ_GPR_31)
4605 mask |= 1 << 31;
4606 /* Don't include register 0. */
4607 return mask & ~1;
4608 }
4609
4610 /* Return the mask of core registers that IP writes. */
4611
4612 static unsigned int
4613 gpr_write_mask (const struct mips_cl_insn *ip)
4614 {
4615 unsigned long pinfo, pinfo2;
4616 unsigned int mask;
4617
4618 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4619 pinfo = ip->insn_mo->pinfo;
4620 pinfo2 = ip->insn_mo->pinfo2;
4621 if (pinfo & INSN_WRITE_GPR_24)
4622 mask |= 1 << 24;
4623 if (pinfo & INSN_WRITE_GPR_31)
4624 mask |= 1 << 31;
4625 if (pinfo & INSN_UDI)
4626 /* UDI instructions have traditionally been assumed to write to RD. */
4627 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4628 if (pinfo2 & INSN2_WRITE_SP)
4629 mask |= 1 << SP;
4630 /* Don't include register 0. */
4631 return mask & ~1;
4632 }
4633
4634 /* Return the mask of floating-point registers that IP reads. */
4635
4636 static unsigned int
4637 fpr_read_mask (const struct mips_cl_insn *ip)
4638 {
4639 unsigned long pinfo;
4640 unsigned int mask;
4641
4642 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4643 | (1 << OP_REG_MSA)),
4644 insn_read_mask (ip->insn_mo));
4645 pinfo = ip->insn_mo->pinfo;
4646 /* Conservatively treat all operands to an FP_D instruction are doubles.
4647 (This is overly pessimistic for things like cvt.d.s.) */
4648 if (FPR_SIZE != 64 && (pinfo & FP_D))
4649 mask |= mask << 1;
4650 return mask;
4651 }
4652
4653 /* Return the mask of floating-point registers that IP writes. */
4654
4655 static unsigned int
4656 fpr_write_mask (const struct mips_cl_insn *ip)
4657 {
4658 unsigned long pinfo;
4659 unsigned int mask;
4660
4661 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4662 | (1 << OP_REG_MSA)),
4663 insn_write_mask (ip->insn_mo));
4664 pinfo = ip->insn_mo->pinfo;
4665 /* Conservatively treat all operands to an FP_D instruction are doubles.
4666 (This is overly pessimistic for things like cvt.s.d.) */
4667 if (FPR_SIZE != 64 && (pinfo & FP_D))
4668 mask |= mask << 1;
4669 return mask;
4670 }
4671
4672 /* Operand OPNUM of INSN is an odd-numbered floating-point register.
4673 Check whether that is allowed. */
4674
4675 static bfd_boolean
4676 mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4677 {
4678 const char *s = insn->name;
4679 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4680 || FPR_SIZE == 64)
4681 && mips_opts.oddspreg;
4682
4683 if (insn->pinfo == INSN_MACRO)
4684 /* Let a macro pass, we'll catch it later when it is expanded. */
4685 return TRUE;
4686
4687 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4688 otherwise it depends on oddspreg. */
4689 if ((insn->pinfo & FP_S)
4690 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
4691 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
4692 return FPR_SIZE == 32 || oddspreg;
4693
4694 /* Allow odd registers for single-precision ops and double-precision if the
4695 floating-point registers are 64-bit wide. */
4696 switch (insn->pinfo & (FP_S | FP_D))
4697 {
4698 case FP_S:
4699 case 0:
4700 return oddspreg;
4701 case FP_D:
4702 return FPR_SIZE == 64;
4703 default:
4704 break;
4705 }
4706
4707 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4708 s = strchr (insn->name, '.');
4709 if (s != NULL && opnum == 2)
4710 s = strchr (s + 1, '.');
4711 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4712 return oddspreg;
4713
4714 return FPR_SIZE == 64;
4715 }
4716
4717 /* Information about an instruction argument that we're trying to match. */
4718 struct mips_arg_info
4719 {
4720 /* The instruction so far. */
4721 struct mips_cl_insn *insn;
4722
4723 /* The first unconsumed operand token. */
4724 struct mips_operand_token *token;
4725
4726 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4727 int opnum;
4728
4729 /* The 1-based argument number, for error reporting. This does not
4730 count elided optional registers, etc.. */
4731 int argnum;
4732
4733 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4734 unsigned int last_regno;
4735
4736 /* If the first operand was an OP_REG, this is the register that it
4737 specified, otherwise it is ILLEGAL_REG. */
4738 unsigned int dest_regno;
4739
4740 /* The value of the last OP_INT operand. Only used for OP_MSB,
4741 where it gives the lsb position. */
4742 unsigned int last_op_int;
4743
4744 /* If true, match routines should assume that no later instruction
4745 alternative matches and should therefore be as accommodating as
4746 possible. Match routines should not report errors if something
4747 is only invalid for !LAX_MATCH. */
4748 bfd_boolean lax_match;
4749
4750 /* True if a reference to the current AT register was seen. */
4751 bfd_boolean seen_at;
4752 };
4753
4754 /* Record that the argument is out of range. */
4755
4756 static void
4757 match_out_of_range (struct mips_arg_info *arg)
4758 {
4759 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4760 }
4761
4762 /* Record that the argument isn't constant but needs to be. */
4763
4764 static void
4765 match_not_constant (struct mips_arg_info *arg)
4766 {
4767 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4768 arg->argnum);
4769 }
4770
4771 /* Try to match an OT_CHAR token for character CH. Consume the token
4772 and return true on success, otherwise return false. */
4773
4774 static bfd_boolean
4775 match_char (struct mips_arg_info *arg, char ch)
4776 {
4777 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4778 {
4779 ++arg->token;
4780 if (ch == ',')
4781 arg->argnum += 1;
4782 return TRUE;
4783 }
4784 return FALSE;
4785 }
4786
4787 /* Try to get an expression from the next tokens in ARG. Consume the
4788 tokens and return true on success, storing the expression value in
4789 VALUE and relocation types in R. */
4790
4791 static bfd_boolean
4792 match_expression (struct mips_arg_info *arg, expressionS *value,
4793 bfd_reloc_code_real_type *r)
4794 {
4795 /* If the next token is a '(' that was parsed as being part of a base
4796 expression, assume we have an elided offset. The later match will fail
4797 if this turns out to be wrong. */
4798 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4799 {
4800 value->X_op = O_constant;
4801 value->X_add_number = 0;
4802 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4803 return TRUE;
4804 }
4805
4806 /* Reject register-based expressions such as "0+$2" and "(($2))".
4807 For plain registers the default error seems more appropriate. */
4808 if (arg->token->type == OT_INTEGER
4809 && arg->token->u.integer.value.X_op == O_register)
4810 {
4811 set_insn_error (arg->argnum, _("register value used as expression"));
4812 return FALSE;
4813 }
4814
4815 if (arg->token->type == OT_INTEGER)
4816 {
4817 *value = arg->token->u.integer.value;
4818 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4819 ++arg->token;
4820 return TRUE;
4821 }
4822
4823 set_insn_error_i
4824 (arg->argnum, _("operand %d must be an immediate expression"),
4825 arg->argnum);
4826 return FALSE;
4827 }
4828
4829 /* Try to get a constant expression from the next tokens in ARG. Consume
4830 the tokens and return return true on success, storing the constant value
4831 in *VALUE. Use FALLBACK as the value if the match succeeded with an
4832 error. */
4833
4834 static bfd_boolean
4835 match_const_int (struct mips_arg_info *arg, offsetT *value)
4836 {
4837 expressionS ex;
4838 bfd_reloc_code_real_type r[3];
4839
4840 if (!match_expression (arg, &ex, r))
4841 return FALSE;
4842
4843 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
4844 *value = ex.X_add_number;
4845 else
4846 {
4847 match_not_constant (arg);
4848 return FALSE;
4849 }
4850 return TRUE;
4851 }
4852
4853 /* Return the RTYPE_* flags for a register operand of type TYPE that
4854 appears in instruction OPCODE. */
4855
4856 static unsigned int
4857 convert_reg_type (const struct mips_opcode *opcode,
4858 enum mips_reg_operand_type type)
4859 {
4860 switch (type)
4861 {
4862 case OP_REG_GP:
4863 return RTYPE_NUM | RTYPE_GP;
4864
4865 case OP_REG_FP:
4866 /* Allow vector register names for MDMX if the instruction is a 64-bit
4867 FPR load, store or move (including moves to and from GPRs). */
4868 if ((mips_opts.ase & ASE_MDMX)
4869 && (opcode->pinfo & FP_D)
4870 && (opcode->pinfo & (INSN_COPROC_MOVE
4871 | INSN_COPROC_MEMORY_DELAY
4872 | INSN_LOAD_COPROC
4873 | INSN_LOAD_MEMORY
4874 | INSN_STORE_MEMORY)))
4875 return RTYPE_FPU | RTYPE_VEC;
4876 return RTYPE_FPU;
4877
4878 case OP_REG_CCC:
4879 if (opcode->pinfo & (FP_D | FP_S))
4880 return RTYPE_CCC | RTYPE_FCC;
4881 return RTYPE_CCC;
4882
4883 case OP_REG_VEC:
4884 if (opcode->membership & INSN_5400)
4885 return RTYPE_FPU;
4886 return RTYPE_FPU | RTYPE_VEC;
4887
4888 case OP_REG_ACC:
4889 return RTYPE_ACC;
4890
4891 case OP_REG_COPRO:
4892 if (opcode->name[strlen (opcode->name) - 1] == '0')
4893 return RTYPE_NUM | RTYPE_CP0;
4894 return RTYPE_NUM;
4895
4896 case OP_REG_HW:
4897 return RTYPE_NUM;
4898
4899 case OP_REG_VI:
4900 return RTYPE_NUM | RTYPE_VI;
4901
4902 case OP_REG_VF:
4903 return RTYPE_NUM | RTYPE_VF;
4904
4905 case OP_REG_R5900_I:
4906 return RTYPE_R5900_I;
4907
4908 case OP_REG_R5900_Q:
4909 return RTYPE_R5900_Q;
4910
4911 case OP_REG_R5900_R:
4912 return RTYPE_R5900_R;
4913
4914 case OP_REG_R5900_ACC:
4915 return RTYPE_R5900_ACC;
4916
4917 case OP_REG_MSA:
4918 return RTYPE_MSA;
4919
4920 case OP_REG_MSA_CTRL:
4921 return RTYPE_NUM;
4922 }
4923 abort ();
4924 }
4925
4926 /* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4927
4928 static void
4929 check_regno (struct mips_arg_info *arg,
4930 enum mips_reg_operand_type type, unsigned int regno)
4931 {
4932 if (AT && type == OP_REG_GP && regno == AT)
4933 arg->seen_at = TRUE;
4934
4935 if (type == OP_REG_FP
4936 && (regno & 1) != 0
4937 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
4938 {
4939 /* This was a warning prior to introducing O32 FPXX and FP64 support
4940 so maintain a warning for FP32 but raise an error for the new
4941 cases. */
4942 if (FPR_SIZE == 32)
4943 as_warn (_("float register should be even, was %d"), regno);
4944 else
4945 as_bad (_("float register should be even, was %d"), regno);
4946 }
4947
4948 if (type == OP_REG_CCC)
4949 {
4950 const char *name;
4951 size_t length;
4952
4953 name = arg->insn->insn_mo->name;
4954 length = strlen (name);
4955 if ((regno & 1) != 0
4956 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4957 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
4958 as_warn (_("condition code register should be even for %s, was %d"),
4959 name, regno);
4960
4961 if ((regno & 3) != 0
4962 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
4963 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
4964 name, regno);
4965 }
4966 }
4967
4968 /* ARG is a register with symbol value SYMVAL. Try to interpret it as
4969 a register of type TYPE. Return true on success, storing the register
4970 number in *REGNO and warning about any dubious uses. */
4971
4972 static bfd_boolean
4973 match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4974 unsigned int symval, unsigned int *regno)
4975 {
4976 if (type == OP_REG_VEC)
4977 symval = mips_prefer_vec_regno (symval);
4978 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4979 return FALSE;
4980
4981 *regno = symval & RNUM_MASK;
4982 check_regno (arg, type, *regno);
4983 return TRUE;
4984 }
4985
4986 /* Try to interpret the next token in ARG as a register of type TYPE.
4987 Consume the token and return true on success, storing the register
4988 number in *REGNO. Return false on failure. */
4989
4990 static bfd_boolean
4991 match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4992 unsigned int *regno)
4993 {
4994 if (arg->token->type == OT_REG
4995 && match_regno (arg, type, arg->token->u.regno, regno))
4996 {
4997 ++arg->token;
4998 return TRUE;
4999 }
5000 return FALSE;
5001 }
5002
5003 /* Try to interpret the next token in ARG as a range of registers of type TYPE.
5004 Consume the token and return true on success, storing the register numbers
5005 in *REGNO1 and *REGNO2. Return false on failure. */
5006
5007 static bfd_boolean
5008 match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5009 unsigned int *regno1, unsigned int *regno2)
5010 {
5011 if (match_reg (arg, type, regno1))
5012 {
5013 *regno2 = *regno1;
5014 return TRUE;
5015 }
5016 if (arg->token->type == OT_REG_RANGE
5017 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5018 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5019 && *regno1 <= *regno2)
5020 {
5021 ++arg->token;
5022 return TRUE;
5023 }
5024 return FALSE;
5025 }
5026
5027 /* OP_INT matcher. */
5028
5029 static bfd_boolean
5030 match_int_operand (struct mips_arg_info *arg,
5031 const struct mips_operand *operand_base)
5032 {
5033 const struct mips_int_operand *operand;
5034 unsigned int uval;
5035 int min_val, max_val, factor;
5036 offsetT sval;
5037
5038 operand = (const struct mips_int_operand *) operand_base;
5039 factor = 1 << operand->shift;
5040 min_val = mips_int_operand_min (operand);
5041 max_val = mips_int_operand_max (operand);
5042
5043 if (operand_base->lsb == 0
5044 && operand_base->size == 16
5045 && operand->shift == 0
5046 && operand->bias == 0
5047 && (operand->max_val == 32767 || operand->max_val == 65535))
5048 {
5049 /* The operand can be relocated. */
5050 if (!match_expression (arg, &offset_expr, offset_reloc))
5051 return FALSE;
5052
5053 if (offset_reloc[0] != BFD_RELOC_UNUSED)
5054 /* Relocation operators were used. Accept the argument and
5055 leave the relocation value in offset_expr and offset_relocs
5056 for the caller to process. */
5057 return TRUE;
5058
5059 if (offset_expr.X_op != O_constant)
5060 {
5061 /* Accept non-constant operands if no later alternative matches,
5062 leaving it for the caller to process. */
5063 if (!arg->lax_match)
5064 return FALSE;
5065 offset_reloc[0] = BFD_RELOC_LO16;
5066 return TRUE;
5067 }
5068
5069 /* Clear the global state; we're going to install the operand
5070 ourselves. */
5071 sval = offset_expr.X_add_number;
5072 offset_expr.X_op = O_absent;
5073
5074 /* For compatibility with older assemblers, we accept
5075 0x8000-0xffff as signed 16-bit numbers when only
5076 signed numbers are allowed. */
5077 if (sval > max_val)
5078 {
5079 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5080 if (!arg->lax_match && sval <= max_val)
5081 return FALSE;
5082 }
5083 }
5084 else
5085 {
5086 if (!match_const_int (arg, &sval))
5087 return FALSE;
5088 }
5089
5090 arg->last_op_int = sval;
5091
5092 if (sval < min_val || sval > max_val || sval % factor)
5093 {
5094 match_out_of_range (arg);
5095 return FALSE;
5096 }
5097
5098 uval = (unsigned int) sval >> operand->shift;
5099 uval -= operand->bias;
5100
5101 /* Handle -mfix-cn63xxp1. */
5102 if (arg->opnum == 1
5103 && mips_fix_cn63xxp1
5104 && !mips_opts.micromips
5105 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5106 switch (uval)
5107 {
5108 case 5:
5109 case 25:
5110 case 26:
5111 case 27:
5112 case 28:
5113 case 29:
5114 case 30:
5115 case 31:
5116 /* These are ok. */
5117 break;
5118
5119 default:
5120 /* The rest must be changed to 28. */
5121 uval = 28;
5122 break;
5123 }
5124
5125 insn_insert_operand (arg->insn, operand_base, uval);
5126 return TRUE;
5127 }
5128
5129 /* OP_MAPPED_INT matcher. */
5130
5131 static bfd_boolean
5132 match_mapped_int_operand (struct mips_arg_info *arg,
5133 const struct mips_operand *operand_base)
5134 {
5135 const struct mips_mapped_int_operand *operand;
5136 unsigned int uval, num_vals;
5137 offsetT sval;
5138
5139 operand = (const struct mips_mapped_int_operand *) operand_base;
5140 if (!match_const_int (arg, &sval))
5141 return FALSE;
5142
5143 num_vals = 1 << operand_base->size;
5144 for (uval = 0; uval < num_vals; uval++)
5145 if (operand->int_map[uval] == sval)
5146 break;
5147 if (uval == num_vals)
5148 {
5149 match_out_of_range (arg);
5150 return FALSE;
5151 }
5152
5153 insn_insert_operand (arg->insn, operand_base, uval);
5154 return TRUE;
5155 }
5156
5157 /* OP_MSB matcher. */
5158
5159 static bfd_boolean
5160 match_msb_operand (struct mips_arg_info *arg,
5161 const struct mips_operand *operand_base)
5162 {
5163 const struct mips_msb_operand *operand;
5164 int min_val, max_val, max_high;
5165 offsetT size, sval, high;
5166
5167 operand = (const struct mips_msb_operand *) operand_base;
5168 min_val = operand->bias;
5169 max_val = min_val + (1 << operand_base->size) - 1;
5170 max_high = operand->opsize;
5171
5172 if (!match_const_int (arg, &size))
5173 return FALSE;
5174
5175 high = size + arg->last_op_int;
5176 sval = operand->add_lsb ? high : size;
5177
5178 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5179 {
5180 match_out_of_range (arg);
5181 return FALSE;
5182 }
5183 insn_insert_operand (arg->insn, operand_base, sval - min_val);
5184 return TRUE;
5185 }
5186
5187 /* OP_REG matcher. */
5188
5189 static bfd_boolean
5190 match_reg_operand (struct mips_arg_info *arg,
5191 const struct mips_operand *operand_base)
5192 {
5193 const struct mips_reg_operand *operand;
5194 unsigned int regno, uval, num_vals;
5195
5196 operand = (const struct mips_reg_operand *) operand_base;
5197 if (!match_reg (arg, operand->reg_type, &regno))
5198 return FALSE;
5199
5200 if (operand->reg_map)
5201 {
5202 num_vals = 1 << operand->root.size;
5203 for (uval = 0; uval < num_vals; uval++)
5204 if (operand->reg_map[uval] == regno)
5205 break;
5206 if (num_vals == uval)
5207 return FALSE;
5208 }
5209 else
5210 uval = regno;
5211
5212 arg->last_regno = regno;
5213 if (arg->opnum == 1)
5214 arg->dest_regno = regno;
5215 insn_insert_operand (arg->insn, operand_base, uval);
5216 return TRUE;
5217 }
5218
5219 /* OP_REG_PAIR matcher. */
5220
5221 static bfd_boolean
5222 match_reg_pair_operand (struct mips_arg_info *arg,
5223 const struct mips_operand *operand_base)
5224 {
5225 const struct mips_reg_pair_operand *operand;
5226 unsigned int regno1, regno2, uval, num_vals;
5227
5228 operand = (const struct mips_reg_pair_operand *) operand_base;
5229 if (!match_reg (arg, operand->reg_type, &regno1)
5230 || !match_char (arg, ',')
5231 || !match_reg (arg, operand->reg_type, &regno2))
5232 return FALSE;
5233
5234 num_vals = 1 << operand_base->size;
5235 for (uval = 0; uval < num_vals; uval++)
5236 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5237 break;
5238 if (uval == num_vals)
5239 return FALSE;
5240
5241 insn_insert_operand (arg->insn, operand_base, uval);
5242 return TRUE;
5243 }
5244
5245 /* OP_PCREL matcher. The caller chooses the relocation type. */
5246
5247 static bfd_boolean
5248 match_pcrel_operand (struct mips_arg_info *arg)
5249 {
5250 bfd_reloc_code_real_type r[3];
5251
5252 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
5253 }
5254
5255 /* OP_PERF_REG matcher. */
5256
5257 static bfd_boolean
5258 match_perf_reg_operand (struct mips_arg_info *arg,
5259 const struct mips_operand *operand)
5260 {
5261 offsetT sval;
5262
5263 if (!match_const_int (arg, &sval))
5264 return FALSE;
5265
5266 if (sval != 0
5267 && (sval != 1
5268 || (mips_opts.arch == CPU_R5900
5269 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5270 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5271 {
5272 set_insn_error (arg->argnum, _("invalid performance register"));
5273 return FALSE;
5274 }
5275
5276 insn_insert_operand (arg->insn, operand, sval);
5277 return TRUE;
5278 }
5279
5280 /* OP_ADDIUSP matcher. */
5281
5282 static bfd_boolean
5283 match_addiusp_operand (struct mips_arg_info *arg,
5284 const struct mips_operand *operand)
5285 {
5286 offsetT sval;
5287 unsigned int uval;
5288
5289 if (!match_const_int (arg, &sval))
5290 return FALSE;
5291
5292 if (sval % 4)
5293 {
5294 match_out_of_range (arg);
5295 return FALSE;
5296 }
5297
5298 sval /= 4;
5299 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
5300 {
5301 match_out_of_range (arg);
5302 return FALSE;
5303 }
5304
5305 uval = (unsigned int) sval;
5306 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5307 insn_insert_operand (arg->insn, operand, uval);
5308 return TRUE;
5309 }
5310
5311 /* OP_CLO_CLZ_DEST matcher. */
5312
5313 static bfd_boolean
5314 match_clo_clz_dest_operand (struct mips_arg_info *arg,
5315 const struct mips_operand *operand)
5316 {
5317 unsigned int regno;
5318
5319 if (!match_reg (arg, OP_REG_GP, &regno))
5320 return FALSE;
5321
5322 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5323 return TRUE;
5324 }
5325
5326 /* OP_CHECK_PREV matcher. */
5327
5328 static bfd_boolean
5329 match_check_prev_operand (struct mips_arg_info *arg,
5330 const struct mips_operand *operand_base)
5331 {
5332 const struct mips_check_prev_operand *operand;
5333 unsigned int regno;
5334
5335 operand = (const struct mips_check_prev_operand *) operand_base;
5336
5337 if (!match_reg (arg, OP_REG_GP, &regno))
5338 return FALSE;
5339
5340 if (!operand->zero_ok && regno == 0)
5341 return FALSE;
5342
5343 if ((operand->less_than_ok && regno < arg->last_regno)
5344 || (operand->greater_than_ok && regno > arg->last_regno)
5345 || (operand->equal_ok && regno == arg->last_regno))
5346 {
5347 arg->last_regno = regno;
5348 insn_insert_operand (arg->insn, operand_base, regno);
5349 return TRUE;
5350 }
5351
5352 return FALSE;
5353 }
5354
5355 /* OP_SAME_RS_RT matcher. */
5356
5357 static bfd_boolean
5358 match_same_rs_rt_operand (struct mips_arg_info *arg,
5359 const struct mips_operand *operand)
5360 {
5361 unsigned int regno;
5362
5363 if (!match_reg (arg, OP_REG_GP, &regno))
5364 return FALSE;
5365
5366 if (regno == 0)
5367 {
5368 set_insn_error (arg->argnum, _("the source register must not be $0"));
5369 return FALSE;
5370 }
5371
5372 arg->last_regno = regno;
5373
5374 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5375 return TRUE;
5376 }
5377
5378 /* OP_LWM_SWM_LIST matcher. */
5379
5380 static bfd_boolean
5381 match_lwm_swm_list_operand (struct mips_arg_info *arg,
5382 const struct mips_operand *operand)
5383 {
5384 unsigned int reglist, sregs, ra, regno1, regno2;
5385 struct mips_arg_info reset;
5386
5387 reglist = 0;
5388 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5389 return FALSE;
5390 do
5391 {
5392 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5393 {
5394 reglist |= 1 << FP;
5395 regno2 = S7;
5396 }
5397 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5398 reset = *arg;
5399 }
5400 while (match_char (arg, ',')
5401 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5402 *arg = reset;
5403
5404 if (operand->size == 2)
5405 {
5406 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5407
5408 s0, ra
5409 s0, s1, ra, s2, s3
5410 s0-s2, ra
5411
5412 and any permutations of these. */
5413 if ((reglist & 0xfff1ffff) != 0x80010000)
5414 return FALSE;
5415
5416 sregs = (reglist >> 17) & 7;
5417 ra = 0;
5418 }
5419 else
5420 {
5421 /* The list must include at least one of ra and s0-sN,
5422 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5423 which are $23 and $30 respectively.) E.g.:
5424
5425 ra
5426 s0
5427 ra, s0, s1, s2
5428 s0-s8
5429 s0-s5, ra
5430
5431 and any permutations of these. */
5432 if ((reglist & 0x3f00ffff) != 0)
5433 return FALSE;
5434
5435 ra = (reglist >> 27) & 0x10;
5436 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5437 }
5438 sregs += 1;
5439 if ((sregs & -sregs) != sregs)
5440 return FALSE;
5441
5442 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5443 return TRUE;
5444 }
5445
5446 /* OP_ENTRY_EXIT_LIST matcher. */
5447
5448 static unsigned int
5449 match_entry_exit_operand (struct mips_arg_info *arg,
5450 const struct mips_operand *operand)
5451 {
5452 unsigned int mask;
5453 bfd_boolean is_exit;
5454
5455 /* The format is the same for both ENTRY and EXIT, but the constraints
5456 are different. */
5457 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5458 mask = (is_exit ? 7 << 3 : 0);
5459 do
5460 {
5461 unsigned int regno1, regno2;
5462 bfd_boolean is_freg;
5463
5464 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5465 is_freg = FALSE;
5466 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5467 is_freg = TRUE;
5468 else
5469 return FALSE;
5470
5471 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5472 {
5473 mask &= ~(7 << 3);
5474 mask |= (5 + regno2) << 3;
5475 }
5476 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5477 mask |= (regno2 - 3) << 3;
5478 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5479 mask |= (regno2 - 15) << 1;
5480 else if (regno1 == RA && regno2 == RA)
5481 mask |= 1;
5482 else
5483 return FALSE;
5484 }
5485 while (match_char (arg, ','));
5486
5487 insn_insert_operand (arg->insn, operand, mask);
5488 return TRUE;
5489 }
5490
5491 /* OP_SAVE_RESTORE_LIST matcher. */
5492
5493 static bfd_boolean
5494 match_save_restore_list_operand (struct mips_arg_info *arg)
5495 {
5496 unsigned int opcode, args, statics, sregs;
5497 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
5498 offsetT frame_size;
5499
5500 opcode = arg->insn->insn_opcode;
5501 frame_size = 0;
5502 num_frame_sizes = 0;
5503 args = 0;
5504 statics = 0;
5505 sregs = 0;
5506 do
5507 {
5508 unsigned int regno1, regno2;
5509
5510 if (arg->token->type == OT_INTEGER)
5511 {
5512 /* Handle the frame size. */
5513 if (!match_const_int (arg, &frame_size))
5514 return FALSE;
5515 num_frame_sizes += 1;
5516 }
5517 else
5518 {
5519 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5520 return FALSE;
5521
5522 while (regno1 <= regno2)
5523 {
5524 if (regno1 >= 4 && regno1 <= 7)
5525 {
5526 if (num_frame_sizes == 0)
5527 /* args $a0-$a3 */
5528 args |= 1 << (regno1 - 4);
5529 else
5530 /* statics $a0-$a3 */
5531 statics |= 1 << (regno1 - 4);
5532 }
5533 else if (regno1 >= 16 && regno1 <= 23)
5534 /* $s0-$s7 */
5535 sregs |= 1 << (regno1 - 16);
5536 else if (regno1 == 30)
5537 /* $s8 */
5538 sregs |= 1 << 8;
5539 else if (regno1 == 31)
5540 /* Add $ra to insn. */
5541 opcode |= 0x40;
5542 else
5543 return FALSE;
5544 regno1 += 1;
5545 if (regno1 == 24)
5546 regno1 = 30;
5547 }
5548 }
5549 }
5550 while (match_char (arg, ','));
5551
5552 /* Encode args/statics combination. */
5553 if (args & statics)
5554 return FALSE;
5555 else if (args == 0xf)
5556 /* All $a0-$a3 are args. */
5557 opcode |= MIPS16_ALL_ARGS << 16;
5558 else if (statics == 0xf)
5559 /* All $a0-$a3 are statics. */
5560 opcode |= MIPS16_ALL_STATICS << 16;
5561 else
5562 {
5563 /* Count arg registers. */
5564 num_args = 0;
5565 while (args & 0x1)
5566 {
5567 args >>= 1;
5568 num_args += 1;
5569 }
5570 if (args != 0)
5571 return FALSE;
5572
5573 /* Count static registers. */
5574 num_statics = 0;
5575 while (statics & 0x8)
5576 {
5577 statics = (statics << 1) & 0xf;
5578 num_statics += 1;
5579 }
5580 if (statics != 0)
5581 return FALSE;
5582
5583 /* Encode args/statics. */
5584 opcode |= ((num_args << 2) | num_statics) << 16;
5585 }
5586
5587 /* Encode $s0/$s1. */
5588 if (sregs & (1 << 0)) /* $s0 */
5589 opcode |= 0x20;
5590 if (sregs & (1 << 1)) /* $s1 */
5591 opcode |= 0x10;
5592 sregs >>= 2;
5593
5594 /* Encode $s2-$s8. */
5595 num_sregs = 0;
5596 while (sregs & 1)
5597 {
5598 sregs >>= 1;
5599 num_sregs += 1;
5600 }
5601 if (sregs != 0)
5602 return FALSE;
5603 opcode |= num_sregs << 24;
5604
5605 /* Encode frame size. */
5606 if (num_frame_sizes == 0)
5607 {
5608 set_insn_error (arg->argnum, _("missing frame size"));
5609 return FALSE;
5610 }
5611 if (num_frame_sizes > 1)
5612 {
5613 set_insn_error (arg->argnum, _("frame size specified twice"));
5614 return FALSE;
5615 }
5616 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5617 {
5618 set_insn_error (arg->argnum, _("invalid frame size"));
5619 return FALSE;
5620 }
5621 if (frame_size != 128 || (opcode >> 16) != 0)
5622 {
5623 frame_size /= 8;
5624 opcode |= (((frame_size & 0xf0) << 16)
5625 | (frame_size & 0x0f));
5626 }
5627
5628 /* Finally build the instruction. */
5629 if ((opcode >> 16) != 0 || frame_size == 0)
5630 opcode |= MIPS16_EXTEND;
5631 arg->insn->insn_opcode = opcode;
5632 return TRUE;
5633 }
5634
5635 /* OP_MDMX_IMM_REG matcher. */
5636
5637 static bfd_boolean
5638 match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
5639 const struct mips_operand *operand)
5640 {
5641 unsigned int regno, uval;
5642 bfd_boolean is_qh;
5643 const struct mips_opcode *opcode;
5644
5645 /* The mips_opcode records whether this is an octobyte or quadhalf
5646 instruction. Start out with that bit in place. */
5647 opcode = arg->insn->insn_mo;
5648 uval = mips_extract_operand (operand, opcode->match);
5649 is_qh = (uval != 0);
5650
5651 if (arg->token->type == OT_REG)
5652 {
5653 if ((opcode->membership & INSN_5400)
5654 && strcmp (opcode->name, "rzu.ob") == 0)
5655 {
5656 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5657 arg->argnum);
5658 return FALSE;
5659 }
5660
5661 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5662 return FALSE;
5663 ++arg->token;
5664
5665 /* Check whether this is a vector register or a broadcast of
5666 a single element. */
5667 if (arg->token->type == OT_INTEGER_INDEX)
5668 {
5669 if (arg->token->u.index > (is_qh ? 3 : 7))
5670 {
5671 set_insn_error (arg->argnum, _("invalid element selector"));
5672 return FALSE;
5673 }
5674 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5675 ++arg->token;
5676 }
5677 else
5678 {
5679 /* A full vector. */
5680 if ((opcode->membership & INSN_5400)
5681 && (strcmp (opcode->name, "sll.ob") == 0
5682 || strcmp (opcode->name, "srl.ob") == 0))
5683 {
5684 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5685 arg->argnum);
5686 return FALSE;
5687 }
5688
5689 if (is_qh)
5690 uval |= MDMX_FMTSEL_VEC_QH << 5;
5691 else
5692 uval |= MDMX_FMTSEL_VEC_OB << 5;
5693 }
5694 uval |= regno;
5695 }
5696 else
5697 {
5698 offsetT sval;
5699
5700 if (!match_const_int (arg, &sval))
5701 return FALSE;
5702 if (sval < 0 || sval > 31)
5703 {
5704 match_out_of_range (arg);
5705 return FALSE;
5706 }
5707 uval |= (sval & 31);
5708 if (is_qh)
5709 uval |= MDMX_FMTSEL_IMM_QH << 5;
5710 else
5711 uval |= MDMX_FMTSEL_IMM_OB << 5;
5712 }
5713 insn_insert_operand (arg->insn, operand, uval);
5714 return TRUE;
5715 }
5716
5717 /* OP_IMM_INDEX matcher. */
5718
5719 static bfd_boolean
5720 match_imm_index_operand (struct mips_arg_info *arg,
5721 const struct mips_operand *operand)
5722 {
5723 unsigned int max_val;
5724
5725 if (arg->token->type != OT_INTEGER_INDEX)
5726 return FALSE;
5727
5728 max_val = (1 << operand->size) - 1;
5729 if (arg->token->u.index > max_val)
5730 {
5731 match_out_of_range (arg);
5732 return FALSE;
5733 }
5734 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5735 ++arg->token;
5736 return TRUE;
5737 }
5738
5739 /* OP_REG_INDEX matcher. */
5740
5741 static bfd_boolean
5742 match_reg_index_operand (struct mips_arg_info *arg,
5743 const struct mips_operand *operand)
5744 {
5745 unsigned int regno;
5746
5747 if (arg->token->type != OT_REG_INDEX)
5748 return FALSE;
5749
5750 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5751 return FALSE;
5752
5753 insn_insert_operand (arg->insn, operand, regno);
5754 ++arg->token;
5755 return TRUE;
5756 }
5757
5758 /* OP_PC matcher. */
5759
5760 static bfd_boolean
5761 match_pc_operand (struct mips_arg_info *arg)
5762 {
5763 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5764 {
5765 ++arg->token;
5766 return TRUE;
5767 }
5768 return FALSE;
5769 }
5770
5771 /* OP_NON_ZERO_REG matcher. */
5772
5773 static bfd_boolean
5774 match_non_zero_reg_operand (struct mips_arg_info *arg,
5775 const struct mips_operand *operand)
5776 {
5777 unsigned int regno;
5778
5779 if (!match_reg (arg, OP_REG_GP, &regno))
5780 return FALSE;
5781
5782 if (regno == 0)
5783 return FALSE;
5784
5785 arg->last_regno = regno;
5786 insn_insert_operand (arg->insn, operand, regno);
5787 return TRUE;
5788 }
5789
5790 /* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5791 register that we need to match. */
5792
5793 static bfd_boolean
5794 match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
5795 {
5796 unsigned int regno;
5797
5798 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
5799 }
5800
5801 /* Read a floating-point constant from S for LI.S or LI.D. LENGTH is
5802 the length of the value in bytes (4 for float, 8 for double) and
5803 USING_GPRS says whether the destination is a GPR rather than an FPR.
5804
5805 Return the constant in IMM and OFFSET as follows:
5806
5807 - If the constant should be loaded via memory, set IMM to O_absent and
5808 OFFSET to the memory address.
5809
5810 - Otherwise, if the constant should be loaded into two 32-bit registers,
5811 set IMM to the O_constant to load into the high register and OFFSET
5812 to the corresponding value for the low register.
5813
5814 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5815
5816 These constants only appear as the last operand in an instruction,
5817 and every instruction that accepts them in any variant accepts them
5818 in all variants. This means we don't have to worry about backing out
5819 any changes if the instruction does not match. We just match
5820 unconditionally and report an error if the constant is invalid. */
5821
5822 static bfd_boolean
5823 match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5824 expressionS *offset, int length, bfd_boolean using_gprs)
5825 {
5826 char *p;
5827 segT seg, new_seg;
5828 subsegT subseg;
5829 const char *newname;
5830 unsigned char *data;
5831
5832 /* Where the constant is placed is based on how the MIPS assembler
5833 does things:
5834
5835 length == 4 && using_gprs -- immediate value only
5836 length == 8 && using_gprs -- .rdata or immediate value
5837 length == 4 && !using_gprs -- .lit4 or immediate value
5838 length == 8 && !using_gprs -- .lit8 or immediate value
5839
5840 The .lit4 and .lit8 sections are only used if permitted by the
5841 -G argument. */
5842 if (arg->token->type != OT_FLOAT)
5843 {
5844 set_insn_error (arg->argnum, _("floating-point expression required"));
5845 return FALSE;
5846 }
5847
5848 gas_assert (arg->token->u.flt.length == length);
5849 data = arg->token->u.flt.data;
5850 ++arg->token;
5851
5852 /* Handle 32-bit constants for which an immediate value is best. */
5853 if (length == 4
5854 && (using_gprs
5855 || g_switch_value < 4
5856 || (data[0] == 0 && data[1] == 0)
5857 || (data[2] == 0 && data[3] == 0)))
5858 {
5859 imm->X_op = O_constant;
5860 if (!target_big_endian)
5861 imm->X_add_number = bfd_getl32 (data);
5862 else
5863 imm->X_add_number = bfd_getb32 (data);
5864 offset->X_op = O_absent;
5865 return TRUE;
5866 }
5867
5868 /* Handle 64-bit constants for which an immediate value is best. */
5869 if (length == 8
5870 && !mips_disable_float_construction
5871 /* Constants can only be constructed in GPRs and copied to FPRs if the
5872 GPRs are at least as wide as the FPRs or MTHC1 is available.
5873 Unlike most tests for 32-bit floating-point registers this check
5874 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
5875 permit 64-bit moves without MXHC1.
5876 Force the constant into memory otherwise. */
5877 && (using_gprs
5878 || GPR_SIZE == 64
5879 || ISA_HAS_MXHC1 (mips_opts.isa)
5880 || FPR_SIZE == 32)
5881 && ((data[0] == 0 && data[1] == 0)
5882 || (data[2] == 0 && data[3] == 0))
5883 && ((data[4] == 0 && data[5] == 0)
5884 || (data[6] == 0 && data[7] == 0)))
5885 {
5886 /* The value is simple enough to load with a couple of instructions.
5887 If using 32-bit registers, set IMM to the high order 32 bits and
5888 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
5889 64 bit constant. */
5890 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
5891 {
5892 imm->X_op = O_constant;
5893 offset->X_op = O_constant;
5894 if (!target_big_endian)
5895 {
5896 imm->X_add_number = bfd_getl32 (data + 4);
5897 offset->X_add_number = bfd_getl32 (data);
5898 }
5899 else
5900 {
5901 imm->X_add_number = bfd_getb32 (data);
5902 offset->X_add_number = bfd_getb32 (data + 4);
5903 }
5904 if (offset->X_add_number == 0)
5905 offset->X_op = O_absent;
5906 }
5907 else
5908 {
5909 imm->X_op = O_constant;
5910 if (!target_big_endian)
5911 imm->X_add_number = bfd_getl64 (data);
5912 else
5913 imm->X_add_number = bfd_getb64 (data);
5914 offset->X_op = O_absent;
5915 }
5916 return TRUE;
5917 }
5918
5919 /* Switch to the right section. */
5920 seg = now_seg;
5921 subseg = now_subseg;
5922 if (length == 4)
5923 {
5924 gas_assert (!using_gprs && g_switch_value >= 4);
5925 newname = ".lit4";
5926 }
5927 else
5928 {
5929 if (using_gprs || g_switch_value < 8)
5930 newname = RDATA_SECTION_NAME;
5931 else
5932 newname = ".lit8";
5933 }
5934
5935 new_seg = subseg_new (newname, (subsegT) 0);
5936 bfd_set_section_flags (stdoutput, new_seg,
5937 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
5938 frag_align (length == 4 ? 2 : 3, 0, 0);
5939 if (strncmp (TARGET_OS, "elf", 3) != 0)
5940 record_alignment (new_seg, 4);
5941 else
5942 record_alignment (new_seg, length == 4 ? 2 : 3);
5943 if (seg == now_seg)
5944 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
5945
5946 /* Set the argument to the current address in the section. */
5947 imm->X_op = O_absent;
5948 offset->X_op = O_symbol;
5949 offset->X_add_symbol = symbol_temp_new_now ();
5950 offset->X_add_number = 0;
5951
5952 /* Put the floating point number into the section. */
5953 p = frag_more (length);
5954 memcpy (p, data, length);
5955
5956 /* Switch back to the original section. */
5957 subseg_set (seg, subseg);
5958 return TRUE;
5959 }
5960
5961 /* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
5962 them. */
5963
5964 static bfd_boolean
5965 match_vu0_suffix_operand (struct mips_arg_info *arg,
5966 const struct mips_operand *operand,
5967 bfd_boolean match_p)
5968 {
5969 unsigned int uval;
5970
5971 /* The operand can be an XYZW mask or a single 2-bit channel index
5972 (with X being 0). */
5973 gas_assert (operand->size == 2 || operand->size == 4);
5974
5975 /* The suffix can be omitted when it is already part of the opcode. */
5976 if (arg->token->type != OT_CHANNELS)
5977 return match_p;
5978
5979 uval = arg->token->u.channels;
5980 if (operand->size == 2)
5981 {
5982 /* Check that a single bit is set and convert it into a 2-bit index. */
5983 if ((uval & -uval) != uval)
5984 return FALSE;
5985 uval = 4 - ffs (uval);
5986 }
5987
5988 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
5989 return FALSE;
5990
5991 ++arg->token;
5992 if (!match_p)
5993 insn_insert_operand (arg->insn, operand, uval);
5994 return TRUE;
5995 }
5996
5997 /* S is the text seen for ARG. Match it against OPERAND. Return the end
5998 of the argument text if the match is successful, otherwise return null. */
5999
6000 static bfd_boolean
6001 match_operand (struct mips_arg_info *arg,
6002 const struct mips_operand *operand)
6003 {
6004 switch (operand->type)
6005 {
6006 case OP_INT:
6007 return match_int_operand (arg, operand);
6008
6009 case OP_MAPPED_INT:
6010 return match_mapped_int_operand (arg, operand);
6011
6012 case OP_MSB:
6013 return match_msb_operand (arg, operand);
6014
6015 case OP_REG:
6016 case OP_OPTIONAL_REG:
6017 return match_reg_operand (arg, operand);
6018
6019 case OP_REG_PAIR:
6020 return match_reg_pair_operand (arg, operand);
6021
6022 case OP_PCREL:
6023 return match_pcrel_operand (arg);
6024
6025 case OP_PERF_REG:
6026 return match_perf_reg_operand (arg, operand);
6027
6028 case OP_ADDIUSP_INT:
6029 return match_addiusp_operand (arg, operand);
6030
6031 case OP_CLO_CLZ_DEST:
6032 return match_clo_clz_dest_operand (arg, operand);
6033
6034 case OP_LWM_SWM_LIST:
6035 return match_lwm_swm_list_operand (arg, operand);
6036
6037 case OP_ENTRY_EXIT_LIST:
6038 return match_entry_exit_operand (arg, operand);
6039
6040 case OP_SAVE_RESTORE_LIST:
6041 return match_save_restore_list_operand (arg);
6042
6043 case OP_MDMX_IMM_REG:
6044 return match_mdmx_imm_reg_operand (arg, operand);
6045
6046 case OP_REPEAT_DEST_REG:
6047 return match_tied_reg_operand (arg, arg->dest_regno);
6048
6049 case OP_REPEAT_PREV_REG:
6050 return match_tied_reg_operand (arg, arg->last_regno);
6051
6052 case OP_PC:
6053 return match_pc_operand (arg);
6054
6055 case OP_VU0_SUFFIX:
6056 return match_vu0_suffix_operand (arg, operand, FALSE);
6057
6058 case OP_VU0_MATCH_SUFFIX:
6059 return match_vu0_suffix_operand (arg, operand, TRUE);
6060
6061 case OP_IMM_INDEX:
6062 return match_imm_index_operand (arg, operand);
6063
6064 case OP_REG_INDEX:
6065 return match_reg_index_operand (arg, operand);
6066
6067 case OP_SAME_RS_RT:
6068 return match_same_rs_rt_operand (arg, operand);
6069
6070 case OP_CHECK_PREV:
6071 return match_check_prev_operand (arg, operand);
6072
6073 case OP_NON_ZERO_REG:
6074 return match_non_zero_reg_operand (arg, operand);
6075 }
6076 abort ();
6077 }
6078
6079 /* ARG is the state after successfully matching an instruction.
6080 Issue any queued-up warnings. */
6081
6082 static void
6083 check_completed_insn (struct mips_arg_info *arg)
6084 {
6085 if (arg->seen_at)
6086 {
6087 if (AT == ATREG)
6088 as_warn (_("used $at without \".set noat\""));
6089 else
6090 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
6091 }
6092 }
6093
6094 /* Return true if modifying general-purpose register REG needs a delay. */
6095
6096 static bfd_boolean
6097 reg_needs_delay (unsigned int reg)
6098 {
6099 unsigned long prev_pinfo;
6100
6101 prev_pinfo = history[0].insn_mo->pinfo;
6102 if (!mips_opts.noreorder
6103 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
6104 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
6105 && (gpr_write_mask (&history[0]) & (1 << reg)))
6106 return TRUE;
6107
6108 return FALSE;
6109 }
6110
6111 /* Classify an instruction according to the FIX_VR4120_* enumeration.
6112 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6113 by VR4120 errata. */
6114
6115 static unsigned int
6116 classify_vr4120_insn (const char *name)
6117 {
6118 if (strncmp (name, "macc", 4) == 0)
6119 return FIX_VR4120_MACC;
6120 if (strncmp (name, "dmacc", 5) == 0)
6121 return FIX_VR4120_DMACC;
6122 if (strncmp (name, "mult", 4) == 0)
6123 return FIX_VR4120_MULT;
6124 if (strncmp (name, "dmult", 5) == 0)
6125 return FIX_VR4120_DMULT;
6126 if (strstr (name, "div"))
6127 return FIX_VR4120_DIV;
6128 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6129 return FIX_VR4120_MTHILO;
6130 return NUM_FIX_VR4120_CLASSES;
6131 }
6132
6133 #define INSN_ERET 0x42000018
6134 #define INSN_DERET 0x4200001f
6135 #define INSN_DMULT 0x1c
6136 #define INSN_DMULTU 0x1d
6137
6138 /* Return the number of instructions that must separate INSN1 and INSN2,
6139 where INSN1 is the earlier instruction. Return the worst-case value
6140 for any INSN2 if INSN2 is null. */
6141
6142 static unsigned int
6143 insns_between (const struct mips_cl_insn *insn1,
6144 const struct mips_cl_insn *insn2)
6145 {
6146 unsigned long pinfo1, pinfo2;
6147 unsigned int mask;
6148
6149 /* If INFO2 is null, pessimistically assume that all flags are set for
6150 the second instruction. */
6151 pinfo1 = insn1->insn_mo->pinfo;
6152 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
6153
6154 /* For most targets, write-after-read dependencies on the HI and LO
6155 registers must be separated by at least two instructions. */
6156 if (!hilo_interlocks)
6157 {
6158 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6159 return 2;
6160 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6161 return 2;
6162 }
6163
6164 /* If we're working around r7000 errata, there must be two instructions
6165 between an mfhi or mflo and any instruction that uses the result. */
6166 if (mips_7000_hilo_fix
6167 && !mips_opts.micromips
6168 && MF_HILO_INSN (pinfo1)
6169 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
6170 return 2;
6171
6172 /* If we're working around 24K errata, one instruction is required
6173 if an ERET or DERET is followed by a branch instruction. */
6174 if (mips_fix_24k && !mips_opts.micromips)
6175 {
6176 if (insn1->insn_opcode == INSN_ERET
6177 || insn1->insn_opcode == INSN_DERET)
6178 {
6179 if (insn2 == NULL
6180 || insn2->insn_opcode == INSN_ERET
6181 || insn2->insn_opcode == INSN_DERET
6182 || delayed_branch_p (insn2))
6183 return 1;
6184 }
6185 }
6186
6187 /* If we're working around PMC RM7000 errata, there must be three
6188 nops between a dmult and a load instruction. */
6189 if (mips_fix_rm7000 && !mips_opts.micromips)
6190 {
6191 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6192 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6193 {
6194 if (pinfo2 & INSN_LOAD_MEMORY)
6195 return 3;
6196 }
6197 }
6198
6199 /* If working around VR4120 errata, check for combinations that need
6200 a single intervening instruction. */
6201 if (mips_fix_vr4120 && !mips_opts.micromips)
6202 {
6203 unsigned int class1, class2;
6204
6205 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6206 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
6207 {
6208 if (insn2 == NULL)
6209 return 1;
6210 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6211 if (vr4120_conflicts[class1] & (1 << class2))
6212 return 1;
6213 }
6214 }
6215
6216 if (!HAVE_CODE_COMPRESSION)
6217 {
6218 /* Check for GPR or coprocessor load delays. All such delays
6219 are on the RT register. */
6220 /* Itbl support may require additional care here. */
6221 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
6222 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
6223 {
6224 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
6225 return 1;
6226 }
6227
6228 /* Check for generic coprocessor hazards.
6229
6230 This case is not handled very well. There is no special
6231 knowledge of CP0 handling, and the coprocessors other than
6232 the floating point unit are not distinguished at all. */
6233 /* Itbl support may require additional care here. FIXME!
6234 Need to modify this to include knowledge about
6235 user specified delays! */
6236 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
6237 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6238 {
6239 /* Handle cases where INSN1 writes to a known general coprocessor
6240 register. There must be a one instruction delay before INSN2
6241 if INSN2 reads that register, otherwise no delay is needed. */
6242 mask = fpr_write_mask (insn1);
6243 if (mask != 0)
6244 {
6245 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
6246 return 1;
6247 }
6248 else
6249 {
6250 /* Read-after-write dependencies on the control registers
6251 require a two-instruction gap. */
6252 if ((pinfo1 & INSN_WRITE_COND_CODE)
6253 && (pinfo2 & INSN_READ_COND_CODE))
6254 return 2;
6255
6256 /* We don't know exactly what INSN1 does. If INSN2 is
6257 also a coprocessor instruction, assume there must be
6258 a one instruction gap. */
6259 if (pinfo2 & INSN_COP)
6260 return 1;
6261 }
6262 }
6263
6264 /* Check for read-after-write dependencies on the coprocessor
6265 control registers in cases where INSN1 does not need a general
6266 coprocessor delay. This means that INSN1 is a floating point
6267 comparison instruction. */
6268 /* Itbl support may require additional care here. */
6269 else if (!cop_interlocks
6270 && (pinfo1 & INSN_WRITE_COND_CODE)
6271 && (pinfo2 & INSN_READ_COND_CODE))
6272 return 1;
6273 }
6274
6275 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6276 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6277 and pause. */
6278 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6279 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6280 || (insn2 && delayed_branch_p (insn2))))
6281 return 1;
6282
6283 return 0;
6284 }
6285
6286 /* Return the number of nops that would be needed to work around the
6287 VR4130 mflo/mfhi errata if instruction INSN immediately followed
6288 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6289 that are contained within the first IGNORE instructions of HIST. */
6290
6291 static int
6292 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
6293 const struct mips_cl_insn *insn)
6294 {
6295 int i, j;
6296 unsigned int mask;
6297
6298 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6299 are not affected by the errata. */
6300 if (insn != 0
6301 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6302 || strcmp (insn->insn_mo->name, "mtlo") == 0
6303 || strcmp (insn->insn_mo->name, "mthi") == 0))
6304 return 0;
6305
6306 /* Search for the first MFLO or MFHI. */
6307 for (i = 0; i < MAX_VR4130_NOPS; i++)
6308 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
6309 {
6310 /* Extract the destination register. */
6311 mask = gpr_write_mask (&hist[i]);
6312
6313 /* No nops are needed if INSN reads that register. */
6314 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
6315 return 0;
6316
6317 /* ...or if any of the intervening instructions do. */
6318 for (j = 0; j < i; j++)
6319 if (gpr_read_mask (&hist[j]) & mask)
6320 return 0;
6321
6322 if (i >= ignore)
6323 return MAX_VR4130_NOPS - i;
6324 }
6325 return 0;
6326 }
6327
6328 #define BASE_REG_EQ(INSN1, INSN2) \
6329 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
6330 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6331
6332 /* Return the minimum alignment for this store instruction. */
6333
6334 static int
6335 fix_24k_align_to (const struct mips_opcode *mo)
6336 {
6337 if (strcmp (mo->name, "sh") == 0)
6338 return 2;
6339
6340 if (strcmp (mo->name, "swc1") == 0
6341 || strcmp (mo->name, "swc2") == 0
6342 || strcmp (mo->name, "sw") == 0
6343 || strcmp (mo->name, "sc") == 0
6344 || strcmp (mo->name, "s.s") == 0)
6345 return 4;
6346
6347 if (strcmp (mo->name, "sdc1") == 0
6348 || strcmp (mo->name, "sdc2") == 0
6349 || strcmp (mo->name, "s.d") == 0)
6350 return 8;
6351
6352 /* sb, swl, swr */
6353 return 1;
6354 }
6355
6356 struct fix_24k_store_info
6357 {
6358 /* Immediate offset, if any, for this store instruction. */
6359 short off;
6360 /* Alignment required by this store instruction. */
6361 int align_to;
6362 /* True for register offsets. */
6363 int register_offset;
6364 };
6365
6366 /* Comparison function used by qsort. */
6367
6368 static int
6369 fix_24k_sort (const void *a, const void *b)
6370 {
6371 const struct fix_24k_store_info *pos1 = a;
6372 const struct fix_24k_store_info *pos2 = b;
6373
6374 return (pos1->off - pos2->off);
6375 }
6376
6377 /* INSN is a store instruction. Try to record the store information
6378 in STINFO. Return false if the information isn't known. */
6379
6380 static bfd_boolean
6381 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
6382 const struct mips_cl_insn *insn)
6383 {
6384 /* The instruction must have a known offset. */
6385 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6386 return FALSE;
6387
6388 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6389 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6390 return TRUE;
6391 }
6392
6393 /* Return the number of nops that would be needed to work around the 24k
6394 "lost data on stores during refill" errata if instruction INSN
6395 immediately followed the 2 instructions described by HIST.
6396 Ignore hazards that are contained within the first IGNORE
6397 instructions of HIST.
6398
6399 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6400 for the data cache refills and store data. The following describes
6401 the scenario where the store data could be lost.
6402
6403 * A data cache miss, due to either a load or a store, causing fill
6404 data to be supplied by the memory subsystem
6405 * The first three doublewords of fill data are returned and written
6406 into the cache
6407 * A sequence of four stores occurs in consecutive cycles around the
6408 final doubleword of the fill:
6409 * Store A
6410 * Store B
6411 * Store C
6412 * Zero, One or more instructions
6413 * Store D
6414
6415 The four stores A-D must be to different doublewords of the line that
6416 is being filled. The fourth instruction in the sequence above permits
6417 the fill of the final doubleword to be transferred from the FSB into
6418 the cache. In the sequence above, the stores may be either integer
6419 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6420 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6421 different doublewords on the line. If the floating point unit is
6422 running in 1:2 mode, it is not possible to create the sequence above
6423 using only floating point store instructions.
6424
6425 In this case, the cache line being filled is incorrectly marked
6426 invalid, thereby losing the data from any store to the line that
6427 occurs between the original miss and the completion of the five
6428 cycle sequence shown above.
6429
6430 The workarounds are:
6431
6432 * Run the data cache in write-through mode.
6433 * Insert a non-store instruction between
6434 Store A and Store B or Store B and Store C. */
6435
6436 static int
6437 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
6438 const struct mips_cl_insn *insn)
6439 {
6440 struct fix_24k_store_info pos[3];
6441 int align, i, base_offset;
6442
6443 if (ignore >= 2)
6444 return 0;
6445
6446 /* If the previous instruction wasn't a store, there's nothing to
6447 worry about. */
6448 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6449 return 0;
6450
6451 /* If the instructions after the previous one are unknown, we have
6452 to assume the worst. */
6453 if (!insn)
6454 return 1;
6455
6456 /* Check whether we are dealing with three consecutive stores. */
6457 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6458 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6459 return 0;
6460
6461 /* If we don't know the relationship between the store addresses,
6462 assume the worst. */
6463 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
6464 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6465 return 1;
6466
6467 if (!fix_24k_record_store_info (&pos[0], insn)
6468 || !fix_24k_record_store_info (&pos[1], &hist[0])
6469 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6470 return 1;
6471
6472 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6473
6474 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6475 X bytes and such that the base register + X is known to be aligned
6476 to align bytes. */
6477
6478 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6479 align = 8;
6480 else
6481 {
6482 align = pos[0].align_to;
6483 base_offset = pos[0].off;
6484 for (i = 1; i < 3; i++)
6485 if (align < pos[i].align_to)
6486 {
6487 align = pos[i].align_to;
6488 base_offset = pos[i].off;
6489 }
6490 for (i = 0; i < 3; i++)
6491 pos[i].off -= base_offset;
6492 }
6493
6494 pos[0].off &= ~align + 1;
6495 pos[1].off &= ~align + 1;
6496 pos[2].off &= ~align + 1;
6497
6498 /* If any two stores write to the same chunk, they also write to the
6499 same doubleword. The offsets are still sorted at this point. */
6500 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6501 return 0;
6502
6503 /* A range of at least 9 bytes is needed for the stores to be in
6504 non-overlapping doublewords. */
6505 if (pos[2].off - pos[0].off <= 8)
6506 return 0;
6507
6508 if (pos[2].off - pos[1].off >= 24
6509 || pos[1].off - pos[0].off >= 24
6510 || pos[2].off - pos[0].off >= 32)
6511 return 0;
6512
6513 return 1;
6514 }
6515
6516 /* Return the number of nops that would be needed if instruction INSN
6517 immediately followed the MAX_NOPS instructions given by HIST,
6518 where HIST[0] is the most recent instruction. Ignore hazards
6519 between INSN and the first IGNORE instructions in HIST.
6520
6521 If INSN is null, return the worse-case number of nops for any
6522 instruction. */
6523
6524 static int
6525 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
6526 const struct mips_cl_insn *insn)
6527 {
6528 int i, nops, tmp_nops;
6529
6530 nops = 0;
6531 for (i = ignore; i < MAX_DELAY_NOPS; i++)
6532 {
6533 tmp_nops = insns_between (hist + i, insn) - i;
6534 if (tmp_nops > nops)
6535 nops = tmp_nops;
6536 }
6537
6538 if (mips_fix_vr4130 && !mips_opts.micromips)
6539 {
6540 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
6541 if (tmp_nops > nops)
6542 nops = tmp_nops;
6543 }
6544
6545 if (mips_fix_24k && !mips_opts.micromips)
6546 {
6547 tmp_nops = nops_for_24k (ignore, hist, insn);
6548 if (tmp_nops > nops)
6549 nops = tmp_nops;
6550 }
6551
6552 return nops;
6553 }
6554
6555 /* The variable arguments provide NUM_INSNS extra instructions that
6556 might be added to HIST. Return the largest number of nops that
6557 would be needed after the extended sequence, ignoring hazards
6558 in the first IGNORE instructions. */
6559
6560 static int
6561 nops_for_sequence (int num_insns, int ignore,
6562 const struct mips_cl_insn *hist, ...)
6563 {
6564 va_list args;
6565 struct mips_cl_insn buffer[MAX_NOPS];
6566 struct mips_cl_insn *cursor;
6567 int nops;
6568
6569 va_start (args, hist);
6570 cursor = buffer + num_insns;
6571 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
6572 while (cursor > buffer)
6573 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6574
6575 nops = nops_for_insn (ignore, buffer, NULL);
6576 va_end (args);
6577 return nops;
6578 }
6579
6580 /* Like nops_for_insn, but if INSN is a branch, take into account the
6581 worst-case delay for the branch target. */
6582
6583 static int
6584 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
6585 const struct mips_cl_insn *insn)
6586 {
6587 int nops, tmp_nops;
6588
6589 nops = nops_for_insn (ignore, hist, insn);
6590 if (delayed_branch_p (insn))
6591 {
6592 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
6593 hist, insn, get_delay_slot_nop (insn));
6594 if (tmp_nops > nops)
6595 nops = tmp_nops;
6596 }
6597 else if (compact_branch_p (insn))
6598 {
6599 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
6600 if (tmp_nops > nops)
6601 nops = tmp_nops;
6602 }
6603 return nops;
6604 }
6605
6606 /* Fix NOP issue: Replace nops by "or at,at,zero". */
6607
6608 static void
6609 fix_loongson2f_nop (struct mips_cl_insn * ip)
6610 {
6611 gas_assert (!HAVE_CODE_COMPRESSION);
6612 if (strcmp (ip->insn_mo->name, "nop") == 0)
6613 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6614 }
6615
6616 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6617 jr target pc &= 'hffff_ffff_cfff_ffff. */
6618
6619 static void
6620 fix_loongson2f_jump (struct mips_cl_insn * ip)
6621 {
6622 gas_assert (!HAVE_CODE_COMPRESSION);
6623 if (strcmp (ip->insn_mo->name, "j") == 0
6624 || strcmp (ip->insn_mo->name, "jr") == 0
6625 || strcmp (ip->insn_mo->name, "jalr") == 0)
6626 {
6627 int sreg;
6628 expressionS ep;
6629
6630 if (! mips_opts.at)
6631 return;
6632
6633 sreg = EXTRACT_OPERAND (0, RS, *ip);
6634 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6635 return;
6636
6637 ep.X_op = O_constant;
6638 ep.X_add_number = 0xcfff0000;
6639 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6640 ep.X_add_number = 0xffff;
6641 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6642 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6643 }
6644 }
6645
6646 static void
6647 fix_loongson2f (struct mips_cl_insn * ip)
6648 {
6649 if (mips_fix_loongson2f_nop)
6650 fix_loongson2f_nop (ip);
6651
6652 if (mips_fix_loongson2f_jump)
6653 fix_loongson2f_jump (ip);
6654 }
6655
6656 /* IP is a branch that has a delay slot, and we need to fill it
6657 automatically. Return true if we can do that by swapping IP
6658 with the previous instruction.
6659 ADDRESS_EXPR is an operand of the instruction to be used with
6660 RELOC_TYPE. */
6661
6662 static bfd_boolean
6663 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
6664 bfd_reloc_code_real_type *reloc_type)
6665 {
6666 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
6667 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
6668 unsigned int fpr_read, prev_fpr_write;
6669
6670 /* -O2 and above is required for this optimization. */
6671 if (mips_optimize < 2)
6672 return FALSE;
6673
6674 /* If we have seen .set volatile or .set nomove, don't optimize. */
6675 if (mips_opts.nomove)
6676 return FALSE;
6677
6678 /* We can't swap if the previous instruction's position is fixed. */
6679 if (history[0].fixed_p)
6680 return FALSE;
6681
6682 /* If the previous previous insn was in a .set noreorder, we can't
6683 swap. Actually, the MIPS assembler will swap in this situation.
6684 However, gcc configured -with-gnu-as will generate code like
6685
6686 .set noreorder
6687 lw $4,XXX
6688 .set reorder
6689 INSN
6690 bne $4,$0,foo
6691
6692 in which we can not swap the bne and INSN. If gcc is not configured
6693 -with-gnu-as, it does not output the .set pseudo-ops. */
6694 if (history[1].noreorder_p)
6695 return FALSE;
6696
6697 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6698 This means that the previous instruction was a 4-byte one anyhow. */
6699 if (mips_opts.mips16 && history[0].fixp[0])
6700 return FALSE;
6701
6702 /* If the branch is itself the target of a branch, we can not swap.
6703 We cheat on this; all we check for is whether there is a label on
6704 this instruction. If there are any branches to anything other than
6705 a label, users must use .set noreorder. */
6706 if (seg_info (now_seg)->label_list)
6707 return FALSE;
6708
6709 /* If the previous instruction is in a variant frag other than this
6710 branch's one, we cannot do the swap. This does not apply to
6711 MIPS16 code, which uses variant frags for different purposes. */
6712 if (!mips_opts.mips16
6713 && history[0].frag
6714 && history[0].frag->fr_type == rs_machine_dependent)
6715 return FALSE;
6716
6717 /* We do not swap with instructions that cannot architecturally
6718 be placed in a branch delay slot, such as SYNC or ERET. We
6719 also refrain from swapping with a trap instruction, since it
6720 complicates trap handlers to have the trap instruction be in
6721 a delay slot. */
6722 prev_pinfo = history[0].insn_mo->pinfo;
6723 if (prev_pinfo & INSN_NO_DELAY_SLOT)
6724 return FALSE;
6725
6726 /* Check for conflicts between the branch and the instructions
6727 before the candidate delay slot. */
6728 if (nops_for_insn (0, history + 1, ip) > 0)
6729 return FALSE;
6730
6731 /* Check for conflicts between the swapped sequence and the
6732 target of the branch. */
6733 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6734 return FALSE;
6735
6736 /* If the branch reads a register that the previous
6737 instruction sets, we can not swap. */
6738 gpr_read = gpr_read_mask (ip);
6739 prev_gpr_write = gpr_write_mask (&history[0]);
6740 if (gpr_read & prev_gpr_write)
6741 return FALSE;
6742
6743 fpr_read = fpr_read_mask (ip);
6744 prev_fpr_write = fpr_write_mask (&history[0]);
6745 if (fpr_read & prev_fpr_write)
6746 return FALSE;
6747
6748 /* If the branch writes a register that the previous
6749 instruction sets, we can not swap. */
6750 gpr_write = gpr_write_mask (ip);
6751 if (gpr_write & prev_gpr_write)
6752 return FALSE;
6753
6754 /* If the branch writes a register that the previous
6755 instruction reads, we can not swap. */
6756 prev_gpr_read = gpr_read_mask (&history[0]);
6757 if (gpr_write & prev_gpr_read)
6758 return FALSE;
6759
6760 /* If one instruction sets a condition code and the
6761 other one uses a condition code, we can not swap. */
6762 pinfo = ip->insn_mo->pinfo;
6763 if ((pinfo & INSN_READ_COND_CODE)
6764 && (prev_pinfo & INSN_WRITE_COND_CODE))
6765 return FALSE;
6766 if ((pinfo & INSN_WRITE_COND_CODE)
6767 && (prev_pinfo & INSN_READ_COND_CODE))
6768 return FALSE;
6769
6770 /* If the previous instruction uses the PC, we can not swap. */
6771 prev_pinfo2 = history[0].insn_mo->pinfo2;
6772 if (prev_pinfo2 & INSN2_READ_PC)
6773 return FALSE;
6774
6775 /* If the previous instruction has an incorrect size for a fixed
6776 branch delay slot in microMIPS mode, we cannot swap. */
6777 pinfo2 = ip->insn_mo->pinfo2;
6778 if (mips_opts.micromips
6779 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6780 && insn_length (history) != 2)
6781 return FALSE;
6782 if (mips_opts.micromips
6783 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6784 && insn_length (history) != 4)
6785 return FALSE;
6786
6787 /* On R5900 short loops need to be fixed by inserting a nop in
6788 the branch delay slots.
6789 A short loop can be terminated too early. */
6790 if (mips_opts.arch == CPU_R5900
6791 /* Check if instruction has a parameter, ignore "j $31". */
6792 && (address_expr != NULL)
6793 /* Parameter must be 16 bit. */
6794 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6795 /* Branch to same segment. */
6796 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
6797 /* Branch to same code fragment. */
6798 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
6799 /* Can only calculate branch offset if value is known. */
6800 && symbol_constant_p (address_expr->X_add_symbol)
6801 /* Check if branch is really conditional. */
6802 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6803 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6804 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6805 {
6806 int distance;
6807 /* Check if loop is shorter than 6 instructions including
6808 branch and delay slot. */
6809 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
6810 if (distance <= 20)
6811 {
6812 int i;
6813 int rv;
6814
6815 rv = FALSE;
6816 /* When the loop includes branches or jumps,
6817 it is not a short loop. */
6818 for (i = 0; i < (distance / 4); i++)
6819 {
6820 if ((history[i].cleared_p)
6821 || delayed_branch_p (&history[i]))
6822 {
6823 rv = TRUE;
6824 break;
6825 }
6826 }
6827 if (rv == FALSE)
6828 {
6829 /* Insert nop after branch to fix short loop. */
6830 return FALSE;
6831 }
6832 }
6833 }
6834
6835 return TRUE;
6836 }
6837
6838 /* Decide how we should add IP to the instruction stream.
6839 ADDRESS_EXPR is an operand of the instruction to be used with
6840 RELOC_TYPE. */
6841
6842 static enum append_method
6843 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
6844 bfd_reloc_code_real_type *reloc_type)
6845 {
6846 /* The relaxed version of a macro sequence must be inherently
6847 hazard-free. */
6848 if (mips_relax.sequence == 2)
6849 return APPEND_ADD;
6850
6851 /* We must not dabble with instructions in a ".set noreorder" block. */
6852 if (mips_opts.noreorder)
6853 return APPEND_ADD;
6854
6855 /* Otherwise, it's our responsibility to fill branch delay slots. */
6856 if (delayed_branch_p (ip))
6857 {
6858 if (!branch_likely_p (ip)
6859 && can_swap_branch_p (ip, address_expr, reloc_type))
6860 return APPEND_SWAP;
6861
6862 if (mips_opts.mips16
6863 && ISA_SUPPORTS_MIPS16E
6864 && gpr_read_mask (ip) != 0)
6865 return APPEND_ADD_COMPACT;
6866
6867 if (mips_opts.micromips
6868 && ((ip->insn_opcode & 0xffe0) == 0x4580
6869 || (!forced_insn_length
6870 && ((ip->insn_opcode & 0xfc00) == 0xcc00
6871 || (ip->insn_opcode & 0xdc00) == 0x8c00))
6872 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
6873 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
6874 return APPEND_ADD_COMPACT;
6875
6876 return APPEND_ADD_WITH_NOP;
6877 }
6878
6879 return APPEND_ADD;
6880 }
6881
6882 /* IP is an instruction whose opcode we have just changed, END points
6883 to the end of the opcode table processed. Point IP->insn_mo to the
6884 new opcode's definition. */
6885
6886 static void
6887 find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
6888 {
6889 const struct mips_opcode *mo;
6890
6891 for (mo = ip->insn_mo; mo < end; mo++)
6892 if (mo->pinfo != INSN_MACRO
6893 && (ip->insn_opcode & mo->mask) == mo->match)
6894 {
6895 ip->insn_mo = mo;
6896 return;
6897 }
6898 abort ();
6899 }
6900
6901 /* IP is a MIPS16 instruction whose opcode we have just changed.
6902 Point IP->insn_mo to the new opcode's definition. */
6903
6904 static void
6905 find_altered_mips16_opcode (struct mips_cl_insn *ip)
6906 {
6907 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
6908 }
6909
6910 /* IP is a microMIPS instruction whose opcode we have just changed.
6911 Point IP->insn_mo to the new opcode's definition. */
6912
6913 static void
6914 find_altered_micromips_opcode (struct mips_cl_insn *ip)
6915 {
6916 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
6917 }
6918
6919 /* For microMIPS macros, we need to generate a local number label
6920 as the target of branches. */
6921 #define MICROMIPS_LABEL_CHAR '\037'
6922 static unsigned long micromips_target_label;
6923 static char micromips_target_name[32];
6924
6925 static char *
6926 micromips_label_name (void)
6927 {
6928 char *p = micromips_target_name;
6929 char symbol_name_temporary[24];
6930 unsigned long l;
6931 int i;
6932
6933 if (*p)
6934 return p;
6935
6936 i = 0;
6937 l = micromips_target_label;
6938 #ifdef LOCAL_LABEL_PREFIX
6939 *p++ = LOCAL_LABEL_PREFIX;
6940 #endif
6941 *p++ = 'L';
6942 *p++ = MICROMIPS_LABEL_CHAR;
6943 do
6944 {
6945 symbol_name_temporary[i++] = l % 10 + '0';
6946 l /= 10;
6947 }
6948 while (l != 0);
6949 while (i > 0)
6950 *p++ = symbol_name_temporary[--i];
6951 *p = '\0';
6952
6953 return micromips_target_name;
6954 }
6955
6956 static void
6957 micromips_label_expr (expressionS *label_expr)
6958 {
6959 label_expr->X_op = O_symbol;
6960 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
6961 label_expr->X_add_number = 0;
6962 }
6963
6964 static void
6965 micromips_label_inc (void)
6966 {
6967 micromips_target_label++;
6968 *micromips_target_name = '\0';
6969 }
6970
6971 static void
6972 micromips_add_label (void)
6973 {
6974 symbolS *s;
6975
6976 s = colon (micromips_label_name ());
6977 micromips_label_inc ();
6978 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
6979 }
6980
6981 /* If assembling microMIPS code, then return the microMIPS reloc
6982 corresponding to the requested one if any. Otherwise return
6983 the reloc unchanged. */
6984
6985 static bfd_reloc_code_real_type
6986 micromips_map_reloc (bfd_reloc_code_real_type reloc)
6987 {
6988 static const bfd_reloc_code_real_type relocs[][2] =
6989 {
6990 /* Keep sorted incrementally by the left-hand key. */
6991 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
6992 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
6993 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
6994 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
6995 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
6996 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
6997 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
6998 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
6999 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7000 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7001 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7002 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7003 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7004 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7005 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7006 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7007 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7008 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7009 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7010 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7011 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7012 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7013 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7014 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7015 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7016 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7017 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7018 };
7019 bfd_reloc_code_real_type r;
7020 size_t i;
7021
7022 if (!mips_opts.micromips)
7023 return reloc;
7024 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7025 {
7026 r = relocs[i][0];
7027 if (r > reloc)
7028 return reloc;
7029 if (r == reloc)
7030 return relocs[i][1];
7031 }
7032 return reloc;
7033 }
7034
7035 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7036 Return true on success, storing the resolved value in RESULT. */
7037
7038 static bfd_boolean
7039 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7040 offsetT *result)
7041 {
7042 switch (reloc)
7043 {
7044 case BFD_RELOC_MIPS_HIGHEST:
7045 case BFD_RELOC_MICROMIPS_HIGHEST:
7046 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7047 return TRUE;
7048
7049 case BFD_RELOC_MIPS_HIGHER:
7050 case BFD_RELOC_MICROMIPS_HIGHER:
7051 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7052 return TRUE;
7053
7054 case BFD_RELOC_HI16_S:
7055 case BFD_RELOC_HI16_S_PCREL:
7056 case BFD_RELOC_MICROMIPS_HI16_S:
7057 case BFD_RELOC_MIPS16_HI16_S:
7058 *result = ((operand + 0x8000) >> 16) & 0xffff;
7059 return TRUE;
7060
7061 case BFD_RELOC_HI16:
7062 case BFD_RELOC_MICROMIPS_HI16:
7063 case BFD_RELOC_MIPS16_HI16:
7064 *result = (operand >> 16) & 0xffff;
7065 return TRUE;
7066
7067 case BFD_RELOC_LO16:
7068 case BFD_RELOC_LO16_PCREL:
7069 case BFD_RELOC_MICROMIPS_LO16:
7070 case BFD_RELOC_MIPS16_LO16:
7071 *result = operand & 0xffff;
7072 return TRUE;
7073
7074 case BFD_RELOC_UNUSED:
7075 *result = operand;
7076 return TRUE;
7077
7078 default:
7079 return FALSE;
7080 }
7081 }
7082
7083 /* Output an instruction. IP is the instruction information.
7084 ADDRESS_EXPR is an operand of the instruction to be used with
7085 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7086 a macro expansion. */
7087
7088 static void
7089 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
7090 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
7091 {
7092 unsigned long prev_pinfo2, pinfo;
7093 bfd_boolean relaxed_branch = FALSE;
7094 enum append_method method;
7095 bfd_boolean relax32;
7096 int branch_disp;
7097
7098 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
7099 fix_loongson2f (ip);
7100
7101 file_ase_mips16 |= mips_opts.mips16;
7102 file_ase_micromips |= mips_opts.micromips;
7103
7104 prev_pinfo2 = history[0].insn_mo->pinfo2;
7105 pinfo = ip->insn_mo->pinfo;
7106
7107 /* Don't raise alarm about `nods' frags as they'll fill in the right
7108 kind of nop in relaxation if required. */
7109 if (mips_opts.micromips
7110 && !expansionp
7111 && !(history[0].frag
7112 && history[0].frag->fr_type == rs_machine_dependent
7113 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7114 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
7115 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7116 && micromips_insn_length (ip->insn_mo) != 2)
7117 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7118 && micromips_insn_length (ip->insn_mo) != 4)))
7119 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
7120 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
7121
7122 if (address_expr == NULL)
7123 ip->complete_p = 1;
7124 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7125 && reloc_type[1] == BFD_RELOC_UNUSED
7126 && reloc_type[2] == BFD_RELOC_UNUSED
7127 && address_expr->X_op == O_constant)
7128 {
7129 switch (*reloc_type)
7130 {
7131 case BFD_RELOC_MIPS_JMP:
7132 {
7133 int shift;
7134
7135 /* Shift is 2, unusually, for microMIPS JALX. */
7136 shift = (mips_opts.micromips
7137 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
7138 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7139 as_bad (_("jump to misaligned address (0x%lx)"),
7140 (unsigned long) address_expr->X_add_number);
7141 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7142 & 0x3ffffff);
7143 ip->complete_p = 1;
7144 }
7145 break;
7146
7147 case BFD_RELOC_MIPS16_JMP:
7148 if ((address_expr->X_add_number & 3) != 0)
7149 as_bad (_("jump to misaligned address (0x%lx)"),
7150 (unsigned long) address_expr->X_add_number);
7151 ip->insn_opcode |=
7152 (((address_expr->X_add_number & 0x7c0000) << 3)
7153 | ((address_expr->X_add_number & 0xf800000) >> 7)
7154 | ((address_expr->X_add_number & 0x3fffc) >> 2));
7155 ip->complete_p = 1;
7156 break;
7157
7158 case BFD_RELOC_16_PCREL_S2:
7159 {
7160 int shift;
7161
7162 shift = mips_opts.micromips ? 1 : 2;
7163 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7164 as_bad (_("branch to misaligned address (0x%lx)"),
7165 (unsigned long) address_expr->X_add_number);
7166 if (!mips_relax_branch)
7167 {
7168 if ((address_expr->X_add_number + (1 << (shift + 15)))
7169 & ~((1 << (shift + 16)) - 1))
7170 as_bad (_("branch address range overflow (0x%lx)"),
7171 (unsigned long) address_expr->X_add_number);
7172 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7173 & 0xffff);
7174 }
7175 }
7176 break;
7177
7178 case BFD_RELOC_MIPS_21_PCREL_S2:
7179 {
7180 int shift;
7181
7182 shift = 2;
7183 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7184 as_bad (_("branch to misaligned address (0x%lx)"),
7185 (unsigned long) address_expr->X_add_number);
7186 if ((address_expr->X_add_number + (1 << (shift + 20)))
7187 & ~((1 << (shift + 21)) - 1))
7188 as_bad (_("branch address range overflow (0x%lx)"),
7189 (unsigned long) address_expr->X_add_number);
7190 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7191 & 0x1fffff);
7192 }
7193 break;
7194
7195 case BFD_RELOC_MIPS_26_PCREL_S2:
7196 {
7197 int shift;
7198
7199 shift = 2;
7200 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7201 as_bad (_("branch to misaligned address (0x%lx)"),
7202 (unsigned long) address_expr->X_add_number);
7203 if ((address_expr->X_add_number + (1 << (shift + 25)))
7204 & ~((1 << (shift + 26)) - 1))
7205 as_bad (_("branch address range overflow (0x%lx)"),
7206 (unsigned long) address_expr->X_add_number);
7207 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7208 & 0x3ffffff);
7209 }
7210 break;
7211
7212 default:
7213 {
7214 offsetT value;
7215
7216 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7217 &value))
7218 {
7219 ip->insn_opcode |= value & 0xffff;
7220 ip->complete_p = 1;
7221 }
7222 }
7223 break;
7224 }
7225 }
7226
7227 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7228 {
7229 /* There are a lot of optimizations we could do that we don't.
7230 In particular, we do not, in general, reorder instructions.
7231 If you use gcc with optimization, it will reorder
7232 instructions and generally do much more optimization then we
7233 do here; repeating all that work in the assembler would only
7234 benefit hand written assembly code, and does not seem worth
7235 it. */
7236 int nops = (mips_optimize == 0
7237 ? nops_for_insn (0, history, NULL)
7238 : nops_for_insn_or_target (0, history, ip));
7239 if (nops > 0)
7240 {
7241 fragS *old_frag;
7242 unsigned long old_frag_offset;
7243 int i;
7244
7245 old_frag = frag_now;
7246 old_frag_offset = frag_now_fix ();
7247
7248 for (i = 0; i < nops; i++)
7249 add_fixed_insn (NOP_INSN);
7250 insert_into_history (0, nops, NOP_INSN);
7251
7252 if (listing)
7253 {
7254 listing_prev_line ();
7255 /* We may be at the start of a variant frag. In case we
7256 are, make sure there is enough space for the frag
7257 after the frags created by listing_prev_line. The
7258 argument to frag_grow here must be at least as large
7259 as the argument to all other calls to frag_grow in
7260 this file. We don't have to worry about being in the
7261 middle of a variant frag, because the variants insert
7262 all needed nop instructions themselves. */
7263 frag_grow (40);
7264 }
7265
7266 mips_move_text_labels ();
7267
7268 #ifndef NO_ECOFF_DEBUGGING
7269 if (ECOFF_DEBUGGING)
7270 ecoff_fix_loc (old_frag, old_frag_offset);
7271 #endif
7272 }
7273 }
7274 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7275 {
7276 int nops;
7277
7278 /* Work out how many nops in prev_nop_frag are needed by IP,
7279 ignoring hazards generated by the first prev_nop_frag_since
7280 instructions. */
7281 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
7282 gas_assert (nops <= prev_nop_frag_holds);
7283
7284 /* Enforce NOPS as a minimum. */
7285 if (nops > prev_nop_frag_required)
7286 prev_nop_frag_required = nops;
7287
7288 if (prev_nop_frag_holds == prev_nop_frag_required)
7289 {
7290 /* Settle for the current number of nops. Update the history
7291 accordingly (for the benefit of any future .set reorder code). */
7292 prev_nop_frag = NULL;
7293 insert_into_history (prev_nop_frag_since,
7294 prev_nop_frag_holds, NOP_INSN);
7295 }
7296 else
7297 {
7298 /* Allow this instruction to replace one of the nops that was
7299 tentatively added to prev_nop_frag. */
7300 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
7301 prev_nop_frag_holds--;
7302 prev_nop_frag_since++;
7303 }
7304 }
7305
7306 method = get_append_method (ip, address_expr, reloc_type);
7307 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
7308
7309 dwarf2_emit_insn (0);
7310 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7311 so "move" the instruction address accordingly.
7312
7313 Also, it doesn't seem appropriate for the assembler to reorder .loc
7314 entries. If this instruction is a branch that we are going to swap
7315 with the previous instruction, the two instructions should be
7316 treated as a unit, and the debug information for both instructions
7317 should refer to the start of the branch sequence. Using the
7318 current position is certainly wrong when swapping a 32-bit branch
7319 and a 16-bit delay slot, since the current position would then be
7320 in the middle of a branch. */
7321 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
7322
7323 relax32 = (mips_relax_branch
7324 /* Don't try branch relaxation within .set nomacro, or within
7325 .set noat if we use $at for PIC computations. If it turns
7326 out that the branch was out-of-range, we'll get an error. */
7327 && !mips_opts.warn_about_macros
7328 && (mips_opts.at || mips_pic == NO_PIC)
7329 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7330 as they have no complementing branches. */
7331 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
7332
7333 if (!HAVE_CODE_COMPRESSION
7334 && address_expr
7335 && relax32
7336 && *reloc_type == BFD_RELOC_16_PCREL_S2
7337 && delayed_branch_p (ip))
7338 {
7339 relaxed_branch = TRUE;
7340 add_relaxed_insn (ip, (relaxed_branch_length
7341 (NULL, NULL,
7342 uncond_branch_p (ip) ? -1
7343 : branch_likely_p (ip) ? 1
7344 : 0)), 4,
7345 RELAX_BRANCH_ENCODE
7346 (AT, mips_pic != NO_PIC,
7347 uncond_branch_p (ip),
7348 branch_likely_p (ip),
7349 pinfo & INSN_WRITE_GPR_31,
7350 0),
7351 address_expr->X_add_symbol,
7352 address_expr->X_add_number);
7353 *reloc_type = BFD_RELOC_UNUSED;
7354 }
7355 else if (mips_opts.micromips
7356 && address_expr
7357 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7358 || *reloc_type > BFD_RELOC_UNUSED)
7359 && (delayed_branch_p (ip) || compact_branch_p (ip))
7360 /* Don't try branch relaxation when users specify
7361 16-bit/32-bit instructions. */
7362 && !forced_insn_length)
7363 {
7364 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7365 && *reloc_type > BFD_RELOC_UNUSED);
7366 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
7367 int uncond = uncond_branch_p (ip) ? -1 : 0;
7368 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7369 int nods = method == APPEND_ADD_WITH_NOP;
7370 int al = pinfo & INSN_WRITE_GPR_31;
7371 int length32 = nods ? 8 : 4;
7372
7373 gas_assert (address_expr != NULL);
7374 gas_assert (!mips_relax.sequence);
7375
7376 relaxed_branch = TRUE;
7377 if (nods)
7378 method = APPEND_ADD;
7379 if (relax32)
7380 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7381 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
7382 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
7383 mips_pic != NO_PIC,
7384 uncond, compact, al, nods,
7385 relax32, 0, 0),
7386 address_expr->X_add_symbol,
7387 address_expr->X_add_number);
7388 *reloc_type = BFD_RELOC_UNUSED;
7389 }
7390 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
7391 {
7392 bfd_boolean require_unextended;
7393 bfd_boolean require_extended;
7394 symbolS *symbol;
7395 offsetT offset;
7396
7397 if (forced_insn_length != 0)
7398 {
7399 require_unextended = forced_insn_length == 2;
7400 require_extended = forced_insn_length == 4;
7401 }
7402 else
7403 {
7404 require_unextended = (mips_opts.noautoextend
7405 && !mips_opcode_32bit_p (ip->insn_mo));
7406 require_extended = 0;
7407 }
7408
7409 /* We need to set up a variant frag. */
7410 gas_assert (address_expr != NULL);
7411 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7412 symbol created by `make_expr_symbol' may not get a necessary
7413 external relocation produced. */
7414 if (address_expr->X_op == O_symbol)
7415 {
7416 symbol = address_expr->X_add_symbol;
7417 offset = address_expr->X_add_number;
7418 }
7419 else
7420 {
7421 symbol = make_expr_symbol (address_expr);
7422 offset = 0;
7423 }
7424 add_relaxed_insn (ip, 4, 0,
7425 RELAX_MIPS16_ENCODE
7426 (*reloc_type - BFD_RELOC_UNUSED,
7427 require_unextended, require_extended,
7428 delayed_branch_p (&history[0]),
7429 history[0].mips16_absolute_jump_p),
7430 symbol, offset);
7431 }
7432 else if (mips_opts.mips16 && insn_length (ip) == 2)
7433 {
7434 if (!delayed_branch_p (ip))
7435 /* Make sure there is enough room to swap this instruction with
7436 a following jump instruction. */
7437 frag_grow (6);
7438 add_fixed_insn (ip);
7439 }
7440 else
7441 {
7442 if (mips_opts.mips16
7443 && mips_opts.noreorder
7444 && delayed_branch_p (&history[0]))
7445 as_warn (_("extended instruction in delay slot"));
7446
7447 if (mips_relax.sequence)
7448 {
7449 /* If we've reached the end of this frag, turn it into a variant
7450 frag and record the information for the instructions we've
7451 written so far. */
7452 if (frag_room () < 4)
7453 relax_close_frag ();
7454 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
7455 }
7456
7457 if (mips_relax.sequence != 2)
7458 {
7459 if (mips_macro_warning.first_insn_sizes[0] == 0)
7460 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7461 mips_macro_warning.sizes[0] += insn_length (ip);
7462 mips_macro_warning.insns[0]++;
7463 }
7464 if (mips_relax.sequence != 1)
7465 {
7466 if (mips_macro_warning.first_insn_sizes[1] == 0)
7467 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7468 mips_macro_warning.sizes[1] += insn_length (ip);
7469 mips_macro_warning.insns[1]++;
7470 }
7471
7472 if (mips_opts.mips16)
7473 {
7474 ip->fixed_p = 1;
7475 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7476 }
7477 add_fixed_insn (ip);
7478 }
7479
7480 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
7481 {
7482 bfd_reloc_code_real_type final_type[3];
7483 reloc_howto_type *howto0;
7484 reloc_howto_type *howto;
7485 int i;
7486
7487 /* Perform any necessary conversion to microMIPS relocations
7488 and find out how many relocations there actually are. */
7489 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7490 final_type[i] = micromips_map_reloc (reloc_type[i]);
7491
7492 /* In a compound relocation, it is the final (outermost)
7493 operator that determines the relocated field. */
7494 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
7495 if (!howto)
7496 abort ();
7497
7498 if (i > 1)
7499 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
7500 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7501 bfd_get_reloc_size (howto),
7502 address_expr,
7503 howto0 && howto0->pc_relative,
7504 final_type[0]);
7505 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7506 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
7507
7508 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
7509 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
7510 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7511
7512 /* These relocations can have an addend that won't fit in
7513 4 octets for 64bit assembly. */
7514 if (GPR_SIZE == 64
7515 && ! howto->partial_inplace
7516 && (reloc_type[0] == BFD_RELOC_16
7517 || reloc_type[0] == BFD_RELOC_32
7518 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7519 || reloc_type[0] == BFD_RELOC_GPREL16
7520 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7521 || reloc_type[0] == BFD_RELOC_GPREL32
7522 || reloc_type[0] == BFD_RELOC_64
7523 || reloc_type[0] == BFD_RELOC_CTOR
7524 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7525 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7526 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7527 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7528 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7529 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7530 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7531 || hi16_reloc_p (reloc_type[0])
7532 || lo16_reloc_p (reloc_type[0])))
7533 ip->fixp[0]->fx_no_overflow = 1;
7534
7535 /* These relocations can have an addend that won't fit in 2 octets. */
7536 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7537 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7538 ip->fixp[0]->fx_no_overflow = 1;
7539
7540 if (mips_relax.sequence)
7541 {
7542 if (mips_relax.first_fixup == 0)
7543 mips_relax.first_fixup = ip->fixp[0];
7544 }
7545 else if (reloc_needs_lo_p (*reloc_type))
7546 {
7547 struct mips_hi_fixup *hi_fixup;
7548
7549 /* Reuse the last entry if it already has a matching %lo. */
7550 hi_fixup = mips_hi_fixup_list;
7551 if (hi_fixup == 0
7552 || !fixup_has_matching_lo_p (hi_fixup->fixp))
7553 {
7554 hi_fixup = XNEW (struct mips_hi_fixup);
7555 hi_fixup->next = mips_hi_fixup_list;
7556 mips_hi_fixup_list = hi_fixup;
7557 }
7558 hi_fixup->fixp = ip->fixp[0];
7559 hi_fixup->seg = now_seg;
7560 }
7561
7562 /* Add fixups for the second and third relocations, if given.
7563 Note that the ABI allows the second relocation to be
7564 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7565 moment we only use RSS_UNDEF, but we could add support
7566 for the others if it ever becomes necessary. */
7567 for (i = 1; i < 3; i++)
7568 if (reloc_type[i] != BFD_RELOC_UNUSED)
7569 {
7570 ip->fixp[i] = fix_new (ip->frag, ip->where,
7571 ip->fixp[0]->fx_size, NULL, 0,
7572 FALSE, final_type[i]);
7573
7574 /* Use fx_tcbit to mark compound relocs. */
7575 ip->fixp[0]->fx_tcbit = 1;
7576 ip->fixp[i]->fx_tcbit = 1;
7577 }
7578 }
7579
7580 /* Update the register mask information. */
7581 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7582 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
7583
7584 switch (method)
7585 {
7586 case APPEND_ADD:
7587 insert_into_history (0, 1, ip);
7588 break;
7589
7590 case APPEND_ADD_WITH_NOP:
7591 {
7592 struct mips_cl_insn *nop;
7593
7594 insert_into_history (0, 1, ip);
7595 nop = get_delay_slot_nop (ip);
7596 add_fixed_insn (nop);
7597 insert_into_history (0, 1, nop);
7598 if (mips_relax.sequence)
7599 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7600 }
7601 break;
7602
7603 case APPEND_ADD_COMPACT:
7604 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7605 if (mips_opts.mips16)
7606 {
7607 ip->insn_opcode |= 0x0080;
7608 find_altered_mips16_opcode (ip);
7609 }
7610 /* Convert microMIPS instructions. */
7611 else if (mips_opts.micromips)
7612 {
7613 /* jr16->jrc */
7614 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7615 ip->insn_opcode |= 0x0020;
7616 /* b16->bc */
7617 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7618 ip->insn_opcode = 0x40e00000;
7619 /* beqz16->beqzc, bnez16->bnezc */
7620 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7621 {
7622 unsigned long regno;
7623
7624 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7625 regno &= MICROMIPSOP_MASK_MD;
7626 regno = micromips_to_32_reg_d_map[regno];
7627 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7628 | (regno << MICROMIPSOP_SH_RS)
7629 | 0x40a00000) ^ 0x00400000;
7630 }
7631 /* beqz->beqzc, bnez->bnezc */
7632 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7633 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7634 | ((ip->insn_opcode >> 7) & 0x00400000)
7635 | 0x40a00000) ^ 0x00400000;
7636 /* beq $0->beqzc, bne $0->bnezc */
7637 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7638 ip->insn_opcode = (((ip->insn_opcode >>
7639 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
7640 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
7641 | ((ip->insn_opcode >> 7) & 0x00400000)
7642 | 0x40a00000) ^ 0x00400000;
7643 else
7644 abort ();
7645 find_altered_micromips_opcode (ip);
7646 }
7647 else
7648 abort ();
7649 install_insn (ip);
7650 insert_into_history (0, 1, ip);
7651 break;
7652
7653 case APPEND_SWAP:
7654 {
7655 struct mips_cl_insn delay = history[0];
7656
7657 if (relaxed_branch || delay.frag != ip->frag)
7658 {
7659 /* Add the delay slot instruction to the end of the
7660 current frag and shrink the fixed part of the
7661 original frag. If the branch occupies the tail of
7662 the latter, move it backwards to cover the gap. */
7663 delay.frag->fr_fix -= branch_disp;
7664 if (delay.frag == ip->frag)
7665 move_insn (ip, ip->frag, ip->where - branch_disp);
7666 add_fixed_insn (&delay);
7667 }
7668 else
7669 {
7670 /* If this is not a relaxed branch and we are in the
7671 same frag, then just swap the instructions. */
7672 move_insn (ip, delay.frag, delay.where);
7673 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
7674 }
7675 history[0] = *ip;
7676 delay.fixed_p = 1;
7677 insert_into_history (0, 1, &delay);
7678 }
7679 break;
7680 }
7681
7682 /* If we have just completed an unconditional branch, clear the history. */
7683 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7684 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
7685 {
7686 unsigned int i;
7687
7688 mips_no_prev_insn ();
7689
7690 for (i = 0; i < ARRAY_SIZE (history); i++)
7691 history[i].cleared_p = 1;
7692 }
7693
7694 /* We need to emit a label at the end of branch-likely macros. */
7695 if (emit_branch_likely_macro)
7696 {
7697 emit_branch_likely_macro = FALSE;
7698 micromips_add_label ();
7699 }
7700
7701 /* We just output an insn, so the next one doesn't have a label. */
7702 mips_clear_insn_labels ();
7703 }
7704
7705 /* Forget that there was any previous instruction or label.
7706 When BRANCH is true, the branch history is also flushed. */
7707
7708 static void
7709 mips_no_prev_insn (void)
7710 {
7711 prev_nop_frag = NULL;
7712 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
7713 mips_clear_insn_labels ();
7714 }
7715
7716 /* This function must be called before we emit something other than
7717 instructions. It is like mips_no_prev_insn except that it inserts
7718 any NOPS that might be needed by previous instructions. */
7719
7720 void
7721 mips_emit_delays (void)
7722 {
7723 if (! mips_opts.noreorder)
7724 {
7725 int nops = nops_for_insn (0, history, NULL);
7726 if (nops > 0)
7727 {
7728 while (nops-- > 0)
7729 add_fixed_insn (NOP_INSN);
7730 mips_move_text_labels ();
7731 }
7732 }
7733 mips_no_prev_insn ();
7734 }
7735
7736 /* Start a (possibly nested) noreorder block. */
7737
7738 static void
7739 start_noreorder (void)
7740 {
7741 if (mips_opts.noreorder == 0)
7742 {
7743 unsigned int i;
7744 int nops;
7745
7746 /* None of the instructions before the .set noreorder can be moved. */
7747 for (i = 0; i < ARRAY_SIZE (history); i++)
7748 history[i].fixed_p = 1;
7749
7750 /* Insert any nops that might be needed between the .set noreorder
7751 block and the previous instructions. We will later remove any
7752 nops that turn out not to be needed. */
7753 nops = nops_for_insn (0, history, NULL);
7754 if (nops > 0)
7755 {
7756 if (mips_optimize != 0)
7757 {
7758 /* Record the frag which holds the nop instructions, so
7759 that we can remove them if we don't need them. */
7760 frag_grow (nops * NOP_INSN_SIZE);
7761 prev_nop_frag = frag_now;
7762 prev_nop_frag_holds = nops;
7763 prev_nop_frag_required = 0;
7764 prev_nop_frag_since = 0;
7765 }
7766
7767 for (; nops > 0; --nops)
7768 add_fixed_insn (NOP_INSN);
7769
7770 /* Move on to a new frag, so that it is safe to simply
7771 decrease the size of prev_nop_frag. */
7772 frag_wane (frag_now);
7773 frag_new (0);
7774 mips_move_text_labels ();
7775 }
7776 mips_mark_labels ();
7777 mips_clear_insn_labels ();
7778 }
7779 mips_opts.noreorder++;
7780 mips_any_noreorder = 1;
7781 }
7782
7783 /* End a nested noreorder block. */
7784
7785 static void
7786 end_noreorder (void)
7787 {
7788 mips_opts.noreorder--;
7789 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7790 {
7791 /* Commit to inserting prev_nop_frag_required nops and go back to
7792 handling nop insertion the .set reorder way. */
7793 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
7794 * NOP_INSN_SIZE);
7795 insert_into_history (prev_nop_frag_since,
7796 prev_nop_frag_required, NOP_INSN);
7797 prev_nop_frag = NULL;
7798 }
7799 }
7800
7801 /* Sign-extend 32-bit mode constants that have bit 31 set and all
7802 higher bits unset. */
7803
7804 static void
7805 normalize_constant_expr (expressionS *ex)
7806 {
7807 if (ex->X_op == O_constant
7808 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7809 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7810 - 0x80000000);
7811 }
7812
7813 /* Sign-extend 32-bit mode address offsets that have bit 31 set and
7814 all higher bits unset. */
7815
7816 static void
7817 normalize_address_expr (expressionS *ex)
7818 {
7819 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7820 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7821 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7822 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7823 - 0x80000000);
7824 }
7825
7826 /* Try to match TOKENS against OPCODE, storing the result in INSN.
7827 Return true if the match was successful.
7828
7829 OPCODE_EXTRA is a value that should be ORed into the opcode
7830 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
7831 there are more alternatives after OPCODE and SOFT_MATCH is
7832 as for mips_arg_info. */
7833
7834 static bfd_boolean
7835 match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7836 struct mips_operand_token *tokens, unsigned int opcode_extra,
7837 bfd_boolean lax_match, bfd_boolean complete_p)
7838 {
7839 const char *args;
7840 struct mips_arg_info arg;
7841 const struct mips_operand *operand;
7842 char c;
7843
7844 imm_expr.X_op = O_absent;
7845 offset_expr.X_op = O_absent;
7846 offset_reloc[0] = BFD_RELOC_UNUSED;
7847 offset_reloc[1] = BFD_RELOC_UNUSED;
7848 offset_reloc[2] = BFD_RELOC_UNUSED;
7849
7850 create_insn (insn, opcode);
7851 /* When no opcode suffix is specified, assume ".xyzw". */
7852 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
7853 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
7854 else
7855 insn->insn_opcode |= opcode_extra;
7856 memset (&arg, 0, sizeof (arg));
7857 arg.insn = insn;
7858 arg.token = tokens;
7859 arg.argnum = 1;
7860 arg.last_regno = ILLEGAL_REG;
7861 arg.dest_regno = ILLEGAL_REG;
7862 arg.lax_match = lax_match;
7863 for (args = opcode->args;; ++args)
7864 {
7865 if (arg.token->type == OT_END)
7866 {
7867 /* Handle unary instructions in which only one operand is given.
7868 The source is then the same as the destination. */
7869 if (arg.opnum == 1 && *args == ',')
7870 {
7871 operand = (mips_opts.micromips
7872 ? decode_micromips_operand (args + 1)
7873 : decode_mips_operand (args + 1));
7874 if (operand && mips_optional_operand_p (operand))
7875 {
7876 arg.token = tokens;
7877 arg.argnum = 1;
7878 continue;
7879 }
7880 }
7881
7882 /* Treat elided base registers as $0. */
7883 if (strcmp (args, "(b)") == 0)
7884 args += 3;
7885
7886 if (args[0] == '+')
7887 switch (args[1])
7888 {
7889 case 'K':
7890 case 'N':
7891 /* The register suffix is optional. */
7892 args += 2;
7893 break;
7894 }
7895
7896 /* Fail the match if there were too few operands. */
7897 if (*args)
7898 return FALSE;
7899
7900 /* Successful match. */
7901 if (!complete_p)
7902 return TRUE;
7903 clear_insn_error ();
7904 if (arg.dest_regno == arg.last_regno
7905 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
7906 {
7907 if (arg.opnum == 2)
7908 set_insn_error
7909 (0, _("source and destination must be different"));
7910 else if (arg.last_regno == 31)
7911 set_insn_error
7912 (0, _("a destination register must be supplied"));
7913 }
7914 else if (arg.last_regno == 31
7915 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
7916 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
7917 set_insn_error (0, _("the source register must not be $31"));
7918 check_completed_insn (&arg);
7919 return TRUE;
7920 }
7921
7922 /* Fail the match if the line has too many operands. */
7923 if (*args == 0)
7924 return FALSE;
7925
7926 /* Handle characters that need to match exactly. */
7927 if (*args == '(' || *args == ')' || *args == ',')
7928 {
7929 if (match_char (&arg, *args))
7930 continue;
7931 return FALSE;
7932 }
7933 if (*args == '#')
7934 {
7935 ++args;
7936 if (arg.token->type == OT_DOUBLE_CHAR
7937 && arg.token->u.ch == *args)
7938 {
7939 ++arg.token;
7940 continue;
7941 }
7942 return FALSE;
7943 }
7944
7945 /* Handle special macro operands. Work out the properties of
7946 other operands. */
7947 arg.opnum += 1;
7948 switch (*args)
7949 {
7950 case '-':
7951 switch (args[1])
7952 {
7953 case 'A':
7954 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
7955 break;
7956
7957 case 'B':
7958 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
7959 break;
7960 }
7961 break;
7962
7963 case '+':
7964 switch (args[1])
7965 {
7966 case 'i':
7967 *offset_reloc = BFD_RELOC_MIPS_JMP;
7968 break;
7969
7970 case '\'':
7971 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
7972 break;
7973
7974 case '\"':
7975 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
7976 break;
7977 }
7978 break;
7979
7980 case 'I':
7981 if (!match_const_int (&arg, &imm_expr.X_add_number))
7982 return FALSE;
7983 imm_expr.X_op = O_constant;
7984 if (GPR_SIZE == 32)
7985 normalize_constant_expr (&imm_expr);
7986 continue;
7987
7988 case 'A':
7989 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
7990 {
7991 /* Assume that the offset has been elided and that what
7992 we saw was a base register. The match will fail later
7993 if that assumption turns out to be wrong. */
7994 offset_expr.X_op = O_constant;
7995 offset_expr.X_add_number = 0;
7996 }
7997 else
7998 {
7999 if (!match_expression (&arg, &offset_expr, offset_reloc))
8000 return FALSE;
8001 normalize_address_expr (&offset_expr);
8002 }
8003 continue;
8004
8005 case 'F':
8006 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8007 8, TRUE))
8008 return FALSE;
8009 continue;
8010
8011 case 'L':
8012 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8013 8, FALSE))
8014 return FALSE;
8015 continue;
8016
8017 case 'f':
8018 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8019 4, TRUE))
8020 return FALSE;
8021 continue;
8022
8023 case 'l':
8024 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8025 4, FALSE))
8026 return FALSE;
8027 continue;
8028
8029 case 'p':
8030 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8031 break;
8032
8033 case 'a':
8034 *offset_reloc = BFD_RELOC_MIPS_JMP;
8035 break;
8036
8037 case 'm':
8038 gas_assert (mips_opts.micromips);
8039 c = args[1];
8040 switch (c)
8041 {
8042 case 'D':
8043 case 'E':
8044 if (!forced_insn_length)
8045 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8046 else if (c == 'D')
8047 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8048 else
8049 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8050 break;
8051 }
8052 break;
8053 }
8054
8055 operand = (mips_opts.micromips
8056 ? decode_micromips_operand (args)
8057 : decode_mips_operand (args));
8058 if (!operand)
8059 abort ();
8060
8061 /* Skip prefixes. */
8062 if (*args == '+' || *args == 'm' || *args == '-')
8063 args++;
8064
8065 if (mips_optional_operand_p (operand)
8066 && args[1] == ','
8067 && (arg.token[0].type != OT_REG
8068 || arg.token[1].type == OT_END))
8069 {
8070 /* Assume that the register has been elided and is the
8071 same as the first operand. */
8072 arg.token = tokens;
8073 arg.argnum = 1;
8074 }
8075
8076 if (!match_operand (&arg, operand))
8077 return FALSE;
8078 }
8079 }
8080
8081 /* Like match_insn, but for MIPS16. */
8082
8083 static bfd_boolean
8084 match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8085 struct mips_operand_token *tokens)
8086 {
8087 const char *args;
8088 const struct mips_operand *operand;
8089 const struct mips_operand *ext_operand;
8090 int required_insn_length;
8091 struct mips_arg_info arg;
8092 int relax_char;
8093
8094 if (forced_insn_length)
8095 required_insn_length = forced_insn_length;
8096 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8097 required_insn_length = 2;
8098 else
8099 required_insn_length = 0;
8100
8101 create_insn (insn, opcode);
8102 imm_expr.X_op = O_absent;
8103 offset_expr.X_op = O_absent;
8104 offset_reloc[0] = BFD_RELOC_UNUSED;
8105 offset_reloc[1] = BFD_RELOC_UNUSED;
8106 offset_reloc[2] = BFD_RELOC_UNUSED;
8107 relax_char = 0;
8108
8109 memset (&arg, 0, sizeof (arg));
8110 arg.insn = insn;
8111 arg.token = tokens;
8112 arg.argnum = 1;
8113 arg.last_regno = ILLEGAL_REG;
8114 arg.dest_regno = ILLEGAL_REG;
8115 relax_char = 0;
8116 for (args = opcode->args;; ++args)
8117 {
8118 int c;
8119
8120 if (arg.token->type == OT_END)
8121 {
8122 offsetT value;
8123
8124 /* Handle unary instructions in which only one operand is given.
8125 The source is then the same as the destination. */
8126 if (arg.opnum == 1 && *args == ',')
8127 {
8128 operand = decode_mips16_operand (args[1], FALSE);
8129 if (operand && mips_optional_operand_p (operand))
8130 {
8131 arg.token = tokens;
8132 arg.argnum = 1;
8133 continue;
8134 }
8135 }
8136
8137 /* Fail the match if there were too few operands. */
8138 if (*args)
8139 return FALSE;
8140
8141 /* Successful match. Stuff the immediate value in now, if
8142 we can. */
8143 clear_insn_error ();
8144 if (opcode->pinfo == INSN_MACRO)
8145 {
8146 gas_assert (relax_char == 0 || relax_char == 'p');
8147 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8148 }
8149 else if (relax_char
8150 && offset_expr.X_op == O_constant
8151 && calculate_reloc (*offset_reloc,
8152 offset_expr.X_add_number,
8153 &value))
8154 {
8155 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
8156 required_insn_length, &insn->insn_opcode);
8157 offset_expr.X_op = O_absent;
8158 *offset_reloc = BFD_RELOC_UNUSED;
8159 }
8160 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8161 {
8162 if (required_insn_length == 2)
8163 set_insn_error (0, _("invalid unextended operand value"));
8164 else
8165 {
8166 forced_insn_length = 4;
8167 insn->insn_opcode |= MIPS16_EXTEND;
8168 }
8169 }
8170 else if (relax_char)
8171 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8172
8173 check_completed_insn (&arg);
8174 return TRUE;
8175 }
8176
8177 /* Fail the match if the line has too many operands. */
8178 if (*args == 0)
8179 return FALSE;
8180
8181 /* Handle characters that need to match exactly. */
8182 if (*args == '(' || *args == ')' || *args == ',')
8183 {
8184 if (match_char (&arg, *args))
8185 continue;
8186 return FALSE;
8187 }
8188
8189 arg.opnum += 1;
8190 c = *args;
8191 switch (c)
8192 {
8193 case 'p':
8194 case 'q':
8195 case 'A':
8196 case 'B':
8197 case 'E':
8198 relax_char = c;
8199 break;
8200
8201 case 'I':
8202 if (!match_const_int (&arg, &imm_expr.X_add_number))
8203 return FALSE;
8204 imm_expr.X_op = O_constant;
8205 if (GPR_SIZE == 32)
8206 normalize_constant_expr (&imm_expr);
8207 continue;
8208
8209 case 'a':
8210 case 'i':
8211 *offset_reloc = BFD_RELOC_MIPS16_JMP;
8212 break;
8213 }
8214
8215 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
8216 if (!operand)
8217 abort ();
8218
8219 if (operand->type != OP_PCREL)
8220 {
8221 ext_operand = decode_mips16_operand (c, TRUE);
8222 if (operand != ext_operand)
8223 {
8224 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8225 {
8226 offset_expr.X_op = O_constant;
8227 offset_expr.X_add_number = 0;
8228 relax_char = c;
8229 continue;
8230 }
8231
8232 /* We need the OT_INTEGER check because some MIPS16
8233 immediate variants are listed before the register ones. */
8234 if (arg.token->type != OT_INTEGER
8235 || !match_expression (&arg, &offset_expr, offset_reloc))
8236 return FALSE;
8237
8238 /* '8' is used for SLTI(U) and has traditionally not
8239 been allowed to take relocation operators. */
8240 if (offset_reloc[0] != BFD_RELOC_UNUSED
8241 && (ext_operand->size != 16 || c == '8'))
8242 return FALSE;
8243
8244 relax_char = c;
8245 continue;
8246 }
8247 }
8248
8249 if (mips_optional_operand_p (operand)
8250 && args[1] == ','
8251 && (arg.token[0].type != OT_REG
8252 || arg.token[1].type == OT_END))
8253 {
8254 /* Assume that the register has been elided and is the
8255 same as the first operand. */
8256 arg.token = tokens;
8257 arg.argnum = 1;
8258 }
8259
8260 if (!match_operand (&arg, operand))
8261 return FALSE;
8262 }
8263 }
8264
8265 /* Record that the current instruction is invalid for the current ISA. */
8266
8267 static void
8268 match_invalid_for_isa (void)
8269 {
8270 set_insn_error_ss
8271 (0, _("opcode not supported on this processor: %s (%s)"),
8272 mips_cpu_info_from_arch (mips_opts.arch)->name,
8273 mips_cpu_info_from_isa (mips_opts.isa)->name);
8274 }
8275
8276 /* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8277 Return true if a definite match or failure was found, storing any match
8278 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8279 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8280 tried and failed to match under normal conditions and now want to try a
8281 more relaxed match. */
8282
8283 static bfd_boolean
8284 match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8285 const struct mips_opcode *past, struct mips_operand_token *tokens,
8286 int opcode_extra, bfd_boolean lax_match)
8287 {
8288 const struct mips_opcode *opcode;
8289 const struct mips_opcode *invalid_delay_slot;
8290 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8291
8292 /* Search for a match, ignoring alternatives that don't satisfy the
8293 current ISA or forced_length. */
8294 invalid_delay_slot = 0;
8295 seen_valid_for_isa = FALSE;
8296 seen_valid_for_size = FALSE;
8297 opcode = first;
8298 do
8299 {
8300 gas_assert (strcmp (opcode->name, first->name) == 0);
8301 if (is_opcode_valid (opcode))
8302 {
8303 seen_valid_for_isa = TRUE;
8304 if (is_size_valid (opcode))
8305 {
8306 bfd_boolean delay_slot_ok;
8307
8308 seen_valid_for_size = TRUE;
8309 delay_slot_ok = is_delay_slot_valid (opcode);
8310 if (match_insn (insn, opcode, tokens, opcode_extra,
8311 lax_match, delay_slot_ok))
8312 {
8313 if (!delay_slot_ok)
8314 {
8315 if (!invalid_delay_slot)
8316 invalid_delay_slot = opcode;
8317 }
8318 else
8319 return TRUE;
8320 }
8321 }
8322 }
8323 ++opcode;
8324 }
8325 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8326
8327 /* If the only matches we found had the wrong length for the delay slot,
8328 pick the first such match. We'll issue an appropriate warning later. */
8329 if (invalid_delay_slot)
8330 {
8331 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8332 lax_match, TRUE))
8333 return TRUE;
8334 abort ();
8335 }
8336
8337 /* Handle the case where we didn't try to match an instruction because
8338 all the alternatives were incompatible with the current ISA. */
8339 if (!seen_valid_for_isa)
8340 {
8341 match_invalid_for_isa ();
8342 return TRUE;
8343 }
8344
8345 /* Handle the case where we didn't try to match an instruction because
8346 all the alternatives were of the wrong size. */
8347 if (!seen_valid_for_size)
8348 {
8349 if (mips_opts.insn32)
8350 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
8351 else
8352 set_insn_error_i
8353 (0, _("unrecognized %d-bit version of microMIPS opcode"),
8354 8 * forced_insn_length);
8355 return TRUE;
8356 }
8357
8358 return FALSE;
8359 }
8360
8361 /* Like match_insns, but for MIPS16. */
8362
8363 static bfd_boolean
8364 match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8365 struct mips_operand_token *tokens)
8366 {
8367 const struct mips_opcode *opcode;
8368 bfd_boolean seen_valid_for_isa;
8369 bfd_boolean seen_valid_for_size;
8370
8371 /* Search for a match, ignoring alternatives that don't satisfy the
8372 current ISA. There are no separate entries for extended forms so
8373 we deal with forced_length later. */
8374 seen_valid_for_isa = FALSE;
8375 seen_valid_for_size = FALSE;
8376 opcode = first;
8377 do
8378 {
8379 gas_assert (strcmp (opcode->name, first->name) == 0);
8380 if (is_opcode_valid_16 (opcode))
8381 {
8382 seen_valid_for_isa = TRUE;
8383 if (is_size_valid_16 (opcode))
8384 {
8385 seen_valid_for_size = TRUE;
8386 if (match_mips16_insn (insn, opcode, tokens))
8387 return TRUE;
8388 }
8389 }
8390 ++opcode;
8391 }
8392 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8393 && strcmp (opcode->name, first->name) == 0);
8394
8395 /* Handle the case where we didn't try to match an instruction because
8396 all the alternatives were incompatible with the current ISA. */
8397 if (!seen_valid_for_isa)
8398 {
8399 match_invalid_for_isa ();
8400 return TRUE;
8401 }
8402
8403 /* Handle the case where we didn't try to match an instruction because
8404 all the alternatives were of the wrong size. */
8405 if (!seen_valid_for_size)
8406 {
8407 if (forced_insn_length == 2)
8408 set_insn_error
8409 (0, _("unrecognized unextended version of MIPS16 opcode"));
8410 else
8411 set_insn_error
8412 (0, _("unrecognized extended version of MIPS16 opcode"));
8413 return TRUE;
8414 }
8415
8416 return FALSE;
8417 }
8418
8419 /* Set up global variables for the start of a new macro. */
8420
8421 static void
8422 macro_start (void)
8423 {
8424 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
8425 memset (&mips_macro_warning.first_insn_sizes, 0,
8426 sizeof (mips_macro_warning.first_insn_sizes));
8427 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
8428 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
8429 && delayed_branch_p (&history[0]));
8430 if (history[0].frag
8431 && history[0].frag->fr_type == rs_machine_dependent
8432 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8433 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8434 mips_macro_warning.delay_slot_length = 0;
8435 else
8436 switch (history[0].insn_mo->pinfo2
8437 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8438 {
8439 case INSN2_BRANCH_DELAY_32BIT:
8440 mips_macro_warning.delay_slot_length = 4;
8441 break;
8442 case INSN2_BRANCH_DELAY_16BIT:
8443 mips_macro_warning.delay_slot_length = 2;
8444 break;
8445 default:
8446 mips_macro_warning.delay_slot_length = 0;
8447 break;
8448 }
8449 mips_macro_warning.first_frag = NULL;
8450 }
8451
8452 /* Given that a macro is longer than one instruction or of the wrong size,
8453 return the appropriate warning for it. Return null if no warning is
8454 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8455 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8456 and RELAX_NOMACRO. */
8457
8458 static const char *
8459 macro_warning (relax_substateT subtype)
8460 {
8461 if (subtype & RELAX_DELAY_SLOT)
8462 return _("macro instruction expanded into multiple instructions"
8463 " in a branch delay slot");
8464 else if (subtype & RELAX_NOMACRO)
8465 return _("macro instruction expanded into multiple instructions");
8466 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8467 | RELAX_DELAY_SLOT_SIZE_SECOND))
8468 return ((subtype & RELAX_DELAY_SLOT_16BIT)
8469 ? _("macro instruction expanded into a wrong size instruction"
8470 " in a 16-bit branch delay slot")
8471 : _("macro instruction expanded into a wrong size instruction"
8472 " in a 32-bit branch delay slot"));
8473 else
8474 return 0;
8475 }
8476
8477 /* Finish up a macro. Emit warnings as appropriate. */
8478
8479 static void
8480 macro_end (void)
8481 {
8482 /* Relaxation warning flags. */
8483 relax_substateT subtype = 0;
8484
8485 /* Check delay slot size requirements. */
8486 if (mips_macro_warning.delay_slot_length == 2)
8487 subtype |= RELAX_DELAY_SLOT_16BIT;
8488 if (mips_macro_warning.delay_slot_length != 0)
8489 {
8490 if (mips_macro_warning.delay_slot_length
8491 != mips_macro_warning.first_insn_sizes[0])
8492 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8493 if (mips_macro_warning.delay_slot_length
8494 != mips_macro_warning.first_insn_sizes[1])
8495 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8496 }
8497
8498 /* Check instruction count requirements. */
8499 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8500 {
8501 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
8502 subtype |= RELAX_SECOND_LONGER;
8503 if (mips_opts.warn_about_macros)
8504 subtype |= RELAX_NOMACRO;
8505 if (mips_macro_warning.delay_slot_p)
8506 subtype |= RELAX_DELAY_SLOT;
8507 }
8508
8509 /* If both alternatives fail to fill a delay slot correctly,
8510 emit the warning now. */
8511 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8512 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8513 {
8514 relax_substateT s;
8515 const char *msg;
8516
8517 s = subtype & (RELAX_DELAY_SLOT_16BIT
8518 | RELAX_DELAY_SLOT_SIZE_FIRST
8519 | RELAX_DELAY_SLOT_SIZE_SECOND);
8520 msg = macro_warning (s);
8521 if (msg != NULL)
8522 as_warn ("%s", msg);
8523 subtype &= ~s;
8524 }
8525
8526 /* If both implementations are longer than 1 instruction, then emit the
8527 warning now. */
8528 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8529 {
8530 relax_substateT s;
8531 const char *msg;
8532
8533 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8534 msg = macro_warning (s);
8535 if (msg != NULL)
8536 as_warn ("%s", msg);
8537 subtype &= ~s;
8538 }
8539
8540 /* If any flags still set, then one implementation might need a warning
8541 and the other either will need one of a different kind or none at all.
8542 Pass any remaining flags over to relaxation. */
8543 if (mips_macro_warning.first_frag != NULL)
8544 mips_macro_warning.first_frag->fr_subtype |= subtype;
8545 }
8546
8547 /* Instruction operand formats used in macros that vary between
8548 standard MIPS and microMIPS code. */
8549
8550 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
8551 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8552 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8553 static const char * const lui_fmt[2] = { "t,u", "s,u" };
8554 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
8555 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
8556 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8557 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8558
8559 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
8560 #define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8561 : cop12_fmt[mips_opts.micromips])
8562 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
8563 #define LUI_FMT (lui_fmt[mips_opts.micromips])
8564 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
8565 #define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8566 : mem12_fmt[mips_opts.micromips])
8567 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
8568 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
8569 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
8570
8571 /* Read a macro's relocation codes from *ARGS and store them in *R.
8572 The first argument in *ARGS will be either the code for a single
8573 relocation or -1 followed by the three codes that make up a
8574 composite relocation. */
8575
8576 static void
8577 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8578 {
8579 int i, next;
8580
8581 next = va_arg (*args, int);
8582 if (next >= 0)
8583 r[0] = (bfd_reloc_code_real_type) next;
8584 else
8585 {
8586 for (i = 0; i < 3; i++)
8587 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8588 /* This function is only used for 16-bit relocation fields.
8589 To make the macro code simpler, treat an unrelocated value
8590 in the same way as BFD_RELOC_LO16. */
8591 if (r[0] == BFD_RELOC_UNUSED)
8592 r[0] = BFD_RELOC_LO16;
8593 }
8594 }
8595
8596 /* Build an instruction created by a macro expansion. This is passed
8597 a pointer to the count of instructions created so far, an
8598 expression, the name of the instruction to build, an operand format
8599 string, and corresponding arguments. */
8600
8601 static void
8602 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
8603 {
8604 const struct mips_opcode *mo = NULL;
8605 bfd_reloc_code_real_type r[3];
8606 const struct mips_opcode *amo;
8607 const struct mips_operand *operand;
8608 struct hash_control *hash;
8609 struct mips_cl_insn insn;
8610 va_list args;
8611 unsigned int uval;
8612
8613 va_start (args, fmt);
8614
8615 if (mips_opts.mips16)
8616 {
8617 mips16_macro_build (ep, name, fmt, &args);
8618 va_end (args);
8619 return;
8620 }
8621
8622 r[0] = BFD_RELOC_UNUSED;
8623 r[1] = BFD_RELOC_UNUSED;
8624 r[2] = BFD_RELOC_UNUSED;
8625 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8626 amo = (struct mips_opcode *) hash_find (hash, name);
8627 gas_assert (amo);
8628 gas_assert (strcmp (name, amo->name) == 0);
8629
8630 do
8631 {
8632 /* Search until we get a match for NAME. It is assumed here that
8633 macros will never generate MDMX, MIPS-3D, or MT instructions.
8634 We try to match an instruction that fulfills the branch delay
8635 slot instruction length requirement (if any) of the previous
8636 instruction. While doing this we record the first instruction
8637 seen that matches all the other conditions and use it anyway
8638 if the requirement cannot be met; we will issue an appropriate
8639 warning later on. */
8640 if (strcmp (fmt, amo->args) == 0
8641 && amo->pinfo != INSN_MACRO
8642 && is_opcode_valid (amo)
8643 && is_size_valid (amo))
8644 {
8645 if (is_delay_slot_valid (amo))
8646 {
8647 mo = amo;
8648 break;
8649 }
8650 else if (!mo)
8651 mo = amo;
8652 }
8653
8654 ++amo;
8655 gas_assert (amo->name);
8656 }
8657 while (strcmp (name, amo->name) == 0);
8658
8659 gas_assert (mo);
8660 create_insn (&insn, mo);
8661 for (; *fmt; ++fmt)
8662 {
8663 switch (*fmt)
8664 {
8665 case ',':
8666 case '(':
8667 case ')':
8668 case 'z':
8669 break;
8670
8671 case 'i':
8672 case 'j':
8673 macro_read_relocs (&args, r);
8674 gas_assert (*r == BFD_RELOC_GPREL16
8675 || *r == BFD_RELOC_MIPS_HIGHER
8676 || *r == BFD_RELOC_HI16_S
8677 || *r == BFD_RELOC_LO16
8678 || *r == BFD_RELOC_MIPS_GOT_OFST);
8679 break;
8680
8681 case 'o':
8682 macro_read_relocs (&args, r);
8683 break;
8684
8685 case 'u':
8686 macro_read_relocs (&args, r);
8687 gas_assert (ep != NULL
8688 && (ep->X_op == O_constant
8689 || (ep->X_op == O_symbol
8690 && (*r == BFD_RELOC_MIPS_HIGHEST
8691 || *r == BFD_RELOC_HI16_S
8692 || *r == BFD_RELOC_HI16
8693 || *r == BFD_RELOC_GPREL16
8694 || *r == BFD_RELOC_MIPS_GOT_HI16
8695 || *r == BFD_RELOC_MIPS_CALL_HI16))));
8696 break;
8697
8698 case 'p':
8699 gas_assert (ep != NULL);
8700
8701 /*
8702 * This allows macro() to pass an immediate expression for
8703 * creating short branches without creating a symbol.
8704 *
8705 * We don't allow branch relaxation for these branches, as
8706 * they should only appear in ".set nomacro" anyway.
8707 */
8708 if (ep->X_op == O_constant)
8709 {
8710 /* For microMIPS we always use relocations for branches.
8711 So we should not resolve immediate values. */
8712 gas_assert (!mips_opts.micromips);
8713
8714 if ((ep->X_add_number & 3) != 0)
8715 as_bad (_("branch to misaligned address (0x%lx)"),
8716 (unsigned long) ep->X_add_number);
8717 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8718 as_bad (_("branch address range overflow (0x%lx)"),
8719 (unsigned long) ep->X_add_number);
8720 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8721 ep = NULL;
8722 }
8723 else
8724 *r = BFD_RELOC_16_PCREL_S2;
8725 break;
8726
8727 case 'a':
8728 gas_assert (ep != NULL);
8729 *r = BFD_RELOC_MIPS_JMP;
8730 break;
8731
8732 default:
8733 operand = (mips_opts.micromips
8734 ? decode_micromips_operand (fmt)
8735 : decode_mips_operand (fmt));
8736 if (!operand)
8737 abort ();
8738
8739 uval = va_arg (args, int);
8740 if (operand->type == OP_CLO_CLZ_DEST)
8741 uval |= (uval << 5);
8742 insn_insert_operand (&insn, operand, uval);
8743
8744 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
8745 ++fmt;
8746 break;
8747 }
8748 }
8749 va_end (args);
8750 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
8751
8752 append_insn (&insn, ep, r, TRUE);
8753 }
8754
8755 static void
8756 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
8757 va_list *args)
8758 {
8759 struct mips_opcode *mo;
8760 struct mips_cl_insn insn;
8761 const struct mips_operand *operand;
8762 bfd_reloc_code_real_type r[3]
8763 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
8764
8765 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
8766 gas_assert (mo);
8767 gas_assert (strcmp (name, mo->name) == 0);
8768
8769 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
8770 {
8771 ++mo;
8772 gas_assert (mo->name);
8773 gas_assert (strcmp (name, mo->name) == 0);
8774 }
8775
8776 create_insn (&insn, mo);
8777 for (; *fmt; ++fmt)
8778 {
8779 int c;
8780
8781 c = *fmt;
8782 switch (c)
8783 {
8784 case ',':
8785 case '(':
8786 case ')':
8787 break;
8788
8789 case '.':
8790 case 'S':
8791 case 'P':
8792 case 'R':
8793 break;
8794
8795 case '<':
8796 case '5':
8797 case 'F':
8798 case 'H':
8799 case 'W':
8800 case 'D':
8801 case 'j':
8802 case '8':
8803 case 'V':
8804 case 'C':
8805 case 'U':
8806 case 'k':
8807 case 'K':
8808 case 'p':
8809 case 'q':
8810 {
8811 offsetT value;
8812
8813 gas_assert (ep != NULL);
8814
8815 if (ep->X_op != O_constant)
8816 *r = (int) BFD_RELOC_UNUSED + c;
8817 else if (calculate_reloc (*r, ep->X_add_number, &value))
8818 {
8819 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
8820 ep = NULL;
8821 *r = BFD_RELOC_UNUSED;
8822 }
8823 }
8824 break;
8825
8826 default:
8827 operand = decode_mips16_operand (c, FALSE);
8828 if (!operand)
8829 abort ();
8830
8831 insn_insert_operand (&insn, operand, va_arg (*args, int));
8832 break;
8833 }
8834 }
8835
8836 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
8837
8838 append_insn (&insn, ep, r, TRUE);
8839 }
8840
8841 /*
8842 * Generate a "jalr" instruction with a relocation hint to the called
8843 * function. This occurs in NewABI PIC code.
8844 */
8845 static void
8846 macro_build_jalr (expressionS *ep, int cprestore)
8847 {
8848 static const bfd_reloc_code_real_type jalr_relocs[2]
8849 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
8850 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
8851 const char *jalr;
8852 char *f = NULL;
8853
8854 if (MIPS_JALR_HINT_P (ep))
8855 {
8856 frag_grow (8);
8857 f = frag_more (0);
8858 }
8859 if (mips_opts.micromips)
8860 {
8861 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
8862 ? "jalr" : "jalrs");
8863 if (MIPS_JALR_HINT_P (ep)
8864 || mips_opts.insn32
8865 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
8866 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
8867 else
8868 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
8869 }
8870 else
8871 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
8872 if (MIPS_JALR_HINT_P (ep))
8873 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
8874 }
8875
8876 /*
8877 * Generate a "lui" instruction.
8878 */
8879 static void
8880 macro_build_lui (expressionS *ep, int regnum)
8881 {
8882 gas_assert (! mips_opts.mips16);
8883
8884 if (ep->X_op != O_constant)
8885 {
8886 gas_assert (ep->X_op == O_symbol);
8887 /* _gp_disp is a special case, used from s_cpload.
8888 __gnu_local_gp is used if mips_no_shared. */
8889 gas_assert (mips_pic == NO_PIC
8890 || (! HAVE_NEWABI
8891 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
8892 || (! mips_in_shared
8893 && strcmp (S_GET_NAME (ep->X_add_symbol),
8894 "__gnu_local_gp") == 0));
8895 }
8896
8897 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
8898 }
8899
8900 /* Generate a sequence of instructions to do a load or store from a constant
8901 offset off of a base register (breg) into/from a target register (treg),
8902 using AT if necessary. */
8903 static void
8904 macro_build_ldst_constoffset (expressionS *ep, const char *op,
8905 int treg, int breg, int dbl)
8906 {
8907 gas_assert (ep->X_op == O_constant);
8908
8909 /* Sign-extending 32-bit constants makes their handling easier. */
8910 if (!dbl)
8911 normalize_constant_expr (ep);
8912
8913 /* Right now, this routine can only handle signed 32-bit constants. */
8914 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
8915 as_warn (_("operand overflow"));
8916
8917 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
8918 {
8919 /* Signed 16-bit offset will fit in the op. Easy! */
8920 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
8921 }
8922 else
8923 {
8924 /* 32-bit offset, need multiple instructions and AT, like:
8925 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
8926 addu $tempreg,$tempreg,$breg
8927 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
8928 to handle the complete offset. */
8929 macro_build_lui (ep, AT);
8930 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
8931 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
8932
8933 if (!mips_opts.at)
8934 as_bad (_("macro used $at after \".set noat\""));
8935 }
8936 }
8937
8938 /* set_at()
8939 * Generates code to set the $at register to true (one)
8940 * if reg is less than the immediate expression.
8941 */
8942 static void
8943 set_at (int reg, int unsignedp)
8944 {
8945 if (imm_expr.X_add_number >= -0x8000
8946 && imm_expr.X_add_number < 0x8000)
8947 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
8948 AT, reg, BFD_RELOC_LO16);
8949 else
8950 {
8951 load_register (AT, &imm_expr, GPR_SIZE == 64);
8952 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
8953 }
8954 }
8955
8956 /* Count the leading zeroes by performing a binary chop. This is a
8957 bulky bit of source, but performance is a LOT better for the
8958 majority of values than a simple loop to count the bits:
8959 for (lcnt = 0; (lcnt < 32); lcnt++)
8960 if ((v) & (1 << (31 - lcnt)))
8961 break;
8962 However it is not code size friendly, and the gain will drop a bit
8963 on certain cached systems.
8964 */
8965 #define COUNT_TOP_ZEROES(v) \
8966 (((v) & ~0xffff) == 0 \
8967 ? ((v) & ~0xff) == 0 \
8968 ? ((v) & ~0xf) == 0 \
8969 ? ((v) & ~0x3) == 0 \
8970 ? ((v) & ~0x1) == 0 \
8971 ? !(v) \
8972 ? 32 \
8973 : 31 \
8974 : 30 \
8975 : ((v) & ~0x7) == 0 \
8976 ? 29 \
8977 : 28 \
8978 : ((v) & ~0x3f) == 0 \
8979 ? ((v) & ~0x1f) == 0 \
8980 ? 27 \
8981 : 26 \
8982 : ((v) & ~0x7f) == 0 \
8983 ? 25 \
8984 : 24 \
8985 : ((v) & ~0xfff) == 0 \
8986 ? ((v) & ~0x3ff) == 0 \
8987 ? ((v) & ~0x1ff) == 0 \
8988 ? 23 \
8989 : 22 \
8990 : ((v) & ~0x7ff) == 0 \
8991 ? 21 \
8992 : 20 \
8993 : ((v) & ~0x3fff) == 0 \
8994 ? ((v) & ~0x1fff) == 0 \
8995 ? 19 \
8996 : 18 \
8997 : ((v) & ~0x7fff) == 0 \
8998 ? 17 \
8999 : 16 \
9000 : ((v) & ~0xffffff) == 0 \
9001 ? ((v) & ~0xfffff) == 0 \
9002 ? ((v) & ~0x3ffff) == 0 \
9003 ? ((v) & ~0x1ffff) == 0 \
9004 ? 15 \
9005 : 14 \
9006 : ((v) & ~0x7ffff) == 0 \
9007 ? 13 \
9008 : 12 \
9009 : ((v) & ~0x3fffff) == 0 \
9010 ? ((v) & ~0x1fffff) == 0 \
9011 ? 11 \
9012 : 10 \
9013 : ((v) & ~0x7fffff) == 0 \
9014 ? 9 \
9015 : 8 \
9016 : ((v) & ~0xfffffff) == 0 \
9017 ? ((v) & ~0x3ffffff) == 0 \
9018 ? ((v) & ~0x1ffffff) == 0 \
9019 ? 7 \
9020 : 6 \
9021 : ((v) & ~0x7ffffff) == 0 \
9022 ? 5 \
9023 : 4 \
9024 : ((v) & ~0x3fffffff) == 0 \
9025 ? ((v) & ~0x1fffffff) == 0 \
9026 ? 3 \
9027 : 2 \
9028 : ((v) & ~0x7fffffff) == 0 \
9029 ? 1 \
9030 : 0)
9031
9032 /* load_register()
9033 * This routine generates the least number of instructions necessary to load
9034 * an absolute expression value into a register.
9035 */
9036 static void
9037 load_register (int reg, expressionS *ep, int dbl)
9038 {
9039 int freg;
9040 expressionS hi32, lo32;
9041
9042 if (ep->X_op != O_big)
9043 {
9044 gas_assert (ep->X_op == O_constant);
9045
9046 /* Sign-extending 32-bit constants makes their handling easier. */
9047 if (!dbl)
9048 normalize_constant_expr (ep);
9049
9050 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
9051 {
9052 /* We can handle 16 bit signed values with an addiu to
9053 $zero. No need to ever use daddiu here, since $zero and
9054 the result are always correct in 32 bit mode. */
9055 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9056 return;
9057 }
9058 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9059 {
9060 /* We can handle 16 bit unsigned values with an ori to
9061 $zero. */
9062 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9063 return;
9064 }
9065 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
9066 {
9067 /* 32 bit values require an lui. */
9068 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9069 if ((ep->X_add_number & 0xffff) != 0)
9070 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9071 return;
9072 }
9073 }
9074
9075 /* The value is larger than 32 bits. */
9076
9077 if (!dbl || GPR_SIZE == 32)
9078 {
9079 char value[32];
9080
9081 sprintf_vma (value, ep->X_add_number);
9082 as_bad (_("number (0x%s) larger than 32 bits"), value);
9083 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9084 return;
9085 }
9086
9087 if (ep->X_op != O_big)
9088 {
9089 hi32 = *ep;
9090 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9091 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9092 hi32.X_add_number &= 0xffffffff;
9093 lo32 = *ep;
9094 lo32.X_add_number &= 0xffffffff;
9095 }
9096 else
9097 {
9098 gas_assert (ep->X_add_number > 2);
9099 if (ep->X_add_number == 3)
9100 generic_bignum[3] = 0;
9101 else if (ep->X_add_number > 4)
9102 as_bad (_("number larger than 64 bits"));
9103 lo32.X_op = O_constant;
9104 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9105 hi32.X_op = O_constant;
9106 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9107 }
9108
9109 if (hi32.X_add_number == 0)
9110 freg = 0;
9111 else
9112 {
9113 int shift, bit;
9114 unsigned long hi, lo;
9115
9116 if (hi32.X_add_number == (offsetT) 0xffffffff)
9117 {
9118 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9119 {
9120 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9121 return;
9122 }
9123 if (lo32.X_add_number & 0x80000000)
9124 {
9125 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9126 if (lo32.X_add_number & 0xffff)
9127 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9128 return;
9129 }
9130 }
9131
9132 /* Check for 16bit shifted constant. We know that hi32 is
9133 non-zero, so start the mask on the first bit of the hi32
9134 value. */
9135 shift = 17;
9136 do
9137 {
9138 unsigned long himask, lomask;
9139
9140 if (shift < 32)
9141 {
9142 himask = 0xffff >> (32 - shift);
9143 lomask = (0xffff << shift) & 0xffffffff;
9144 }
9145 else
9146 {
9147 himask = 0xffff << (shift - 32);
9148 lomask = 0;
9149 }
9150 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9151 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9152 {
9153 expressionS tmp;
9154
9155 tmp.X_op = O_constant;
9156 if (shift < 32)
9157 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9158 | (lo32.X_add_number >> shift));
9159 else
9160 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
9161 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9162 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9163 reg, reg, (shift >= 32) ? shift - 32 : shift);
9164 return;
9165 }
9166 ++shift;
9167 }
9168 while (shift <= (64 - 16));
9169
9170 /* Find the bit number of the lowest one bit, and store the
9171 shifted value in hi/lo. */
9172 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9173 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9174 if (lo != 0)
9175 {
9176 bit = 0;
9177 while ((lo & 1) == 0)
9178 {
9179 lo >>= 1;
9180 ++bit;
9181 }
9182 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9183 hi >>= bit;
9184 }
9185 else
9186 {
9187 bit = 32;
9188 while ((hi & 1) == 0)
9189 {
9190 hi >>= 1;
9191 ++bit;
9192 }
9193 lo = hi;
9194 hi = 0;
9195 }
9196
9197 /* Optimize if the shifted value is a (power of 2) - 1. */
9198 if ((hi == 0 && ((lo + 1) & lo) == 0)
9199 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
9200 {
9201 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
9202 if (shift != 0)
9203 {
9204 expressionS tmp;
9205
9206 /* This instruction will set the register to be all
9207 ones. */
9208 tmp.X_op = O_constant;
9209 tmp.X_add_number = (offsetT) -1;
9210 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9211 if (bit != 0)
9212 {
9213 bit += shift;
9214 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9215 reg, reg, (bit >= 32) ? bit - 32 : bit);
9216 }
9217 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
9218 reg, reg, (shift >= 32) ? shift - 32 : shift);
9219 return;
9220 }
9221 }
9222
9223 /* Sign extend hi32 before calling load_register, because we can
9224 generally get better code when we load a sign extended value. */
9225 if ((hi32.X_add_number & 0x80000000) != 0)
9226 hi32.X_add_number |= ~(offsetT) 0xffffffff;
9227 load_register (reg, &hi32, 0);
9228 freg = reg;
9229 }
9230 if ((lo32.X_add_number & 0xffff0000) == 0)
9231 {
9232 if (freg != 0)
9233 {
9234 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
9235 freg = reg;
9236 }
9237 }
9238 else
9239 {
9240 expressionS mid16;
9241
9242 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
9243 {
9244 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9245 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
9246 return;
9247 }
9248
9249 if (freg != 0)
9250 {
9251 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
9252 freg = reg;
9253 }
9254 mid16 = lo32;
9255 mid16.X_add_number >>= 16;
9256 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9257 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9258 freg = reg;
9259 }
9260 if ((lo32.X_add_number & 0xffff) != 0)
9261 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9262 }
9263
9264 static inline void
9265 load_delay_nop (void)
9266 {
9267 if (!gpr_interlocks)
9268 macro_build (NULL, "nop", "");
9269 }
9270
9271 /* Load an address into a register. */
9272
9273 static void
9274 load_address (int reg, expressionS *ep, int *used_at)
9275 {
9276 if (ep->X_op != O_constant
9277 && ep->X_op != O_symbol)
9278 {
9279 as_bad (_("expression too complex"));
9280 ep->X_op = O_constant;
9281 }
9282
9283 if (ep->X_op == O_constant)
9284 {
9285 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
9286 return;
9287 }
9288
9289 if (mips_pic == NO_PIC)
9290 {
9291 /* If this is a reference to a GP relative symbol, we want
9292 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
9293 Otherwise we want
9294 lui $reg,<sym> (BFD_RELOC_HI16_S)
9295 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9296 If we have an addend, we always use the latter form.
9297
9298 With 64bit address space and a usable $at we want
9299 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9300 lui $at,<sym> (BFD_RELOC_HI16_S)
9301 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9302 daddiu $at,<sym> (BFD_RELOC_LO16)
9303 dsll32 $reg,0
9304 daddu $reg,$reg,$at
9305
9306 If $at is already in use, we use a path which is suboptimal
9307 on superscalar processors.
9308 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9309 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9310 dsll $reg,16
9311 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9312 dsll $reg,16
9313 daddiu $reg,<sym> (BFD_RELOC_LO16)
9314
9315 For GP relative symbols in 64bit address space we can use
9316 the same sequence as in 32bit address space. */
9317 if (HAVE_64BIT_SYMBOLS)
9318 {
9319 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9320 && !nopic_need_relax (ep->X_add_symbol, 1))
9321 {
9322 relax_start (ep->X_add_symbol);
9323 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9324 mips_gp_register, BFD_RELOC_GPREL16);
9325 relax_switch ();
9326 }
9327
9328 if (*used_at == 0 && mips_opts.at)
9329 {
9330 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9331 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
9332 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9333 BFD_RELOC_MIPS_HIGHER);
9334 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
9335 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
9336 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
9337 *used_at = 1;
9338 }
9339 else
9340 {
9341 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9342 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9343 BFD_RELOC_MIPS_HIGHER);
9344 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9345 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
9346 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9347 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
9348 }
9349
9350 if (mips_relax.sequence)
9351 relax_end ();
9352 }
9353 else
9354 {
9355 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9356 && !nopic_need_relax (ep->X_add_symbol, 1))
9357 {
9358 relax_start (ep->X_add_symbol);
9359 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9360 mips_gp_register, BFD_RELOC_GPREL16);
9361 relax_switch ();
9362 }
9363 macro_build_lui (ep, reg);
9364 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9365 reg, reg, BFD_RELOC_LO16);
9366 if (mips_relax.sequence)
9367 relax_end ();
9368 }
9369 }
9370 else if (!mips_big_got)
9371 {
9372 expressionS ex;
9373
9374 /* If this is a reference to an external symbol, we want
9375 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9376 Otherwise we want
9377 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9378 nop
9379 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9380 If there is a constant, it must be added in after.
9381
9382 If we have NewABI, we want
9383 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9384 unless we're referencing a global symbol with a non-zero
9385 offset, in which case cst must be added separately. */
9386 if (HAVE_NEWABI)
9387 {
9388 if (ep->X_add_number)
9389 {
9390 ex.X_add_number = ep->X_add_number;
9391 ep->X_add_number = 0;
9392 relax_start (ep->X_add_symbol);
9393 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9394 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9395 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9396 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9397 ex.X_op = O_constant;
9398 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9399 reg, reg, BFD_RELOC_LO16);
9400 ep->X_add_number = ex.X_add_number;
9401 relax_switch ();
9402 }
9403 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9404 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9405 if (mips_relax.sequence)
9406 relax_end ();
9407 }
9408 else
9409 {
9410 ex.X_add_number = ep->X_add_number;
9411 ep->X_add_number = 0;
9412 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9413 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9414 load_delay_nop ();
9415 relax_start (ep->X_add_symbol);
9416 relax_switch ();
9417 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9418 BFD_RELOC_LO16);
9419 relax_end ();
9420
9421 if (ex.X_add_number != 0)
9422 {
9423 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9424 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9425 ex.X_op = O_constant;
9426 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9427 reg, reg, BFD_RELOC_LO16);
9428 }
9429 }
9430 }
9431 else if (mips_big_got)
9432 {
9433 expressionS ex;
9434
9435 /* This is the large GOT case. If this is a reference to an
9436 external symbol, we want
9437 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9438 addu $reg,$reg,$gp
9439 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
9440
9441 Otherwise, for a reference to a local symbol in old ABI, we want
9442 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9443 nop
9444 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9445 If there is a constant, it must be added in after.
9446
9447 In the NewABI, for local symbols, with or without offsets, we want:
9448 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9449 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
9450 */
9451 if (HAVE_NEWABI)
9452 {
9453 ex.X_add_number = ep->X_add_number;
9454 ep->X_add_number = 0;
9455 relax_start (ep->X_add_symbol);
9456 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9457 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9458 reg, reg, mips_gp_register);
9459 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9460 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9461 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9462 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9463 else if (ex.X_add_number)
9464 {
9465 ex.X_op = O_constant;
9466 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9467 BFD_RELOC_LO16);
9468 }
9469
9470 ep->X_add_number = ex.X_add_number;
9471 relax_switch ();
9472 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9473 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9474 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9475 BFD_RELOC_MIPS_GOT_OFST);
9476 relax_end ();
9477 }
9478 else
9479 {
9480 ex.X_add_number = ep->X_add_number;
9481 ep->X_add_number = 0;
9482 relax_start (ep->X_add_symbol);
9483 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9484 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9485 reg, reg, mips_gp_register);
9486 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9487 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9488 relax_switch ();
9489 if (reg_needs_delay (mips_gp_register))
9490 {
9491 /* We need a nop before loading from $gp. This special
9492 check is required because the lui which starts the main
9493 instruction stream does not refer to $gp, and so will not
9494 insert the nop which may be required. */
9495 macro_build (NULL, "nop", "");
9496 }
9497 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9498 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9499 load_delay_nop ();
9500 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9501 BFD_RELOC_LO16);
9502 relax_end ();
9503
9504 if (ex.X_add_number != 0)
9505 {
9506 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9507 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9508 ex.X_op = O_constant;
9509 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9510 BFD_RELOC_LO16);
9511 }
9512 }
9513 }
9514 else
9515 abort ();
9516
9517 if (!mips_opts.at && *used_at == 1)
9518 as_bad (_("macro used $at after \".set noat\""));
9519 }
9520
9521 /* Move the contents of register SOURCE into register DEST. */
9522
9523 static void
9524 move_register (int dest, int source)
9525 {
9526 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9527 instruction specifically requires a 32-bit one. */
9528 if (mips_opts.micromips
9529 && !mips_opts.insn32
9530 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9531 macro_build (NULL, "move", "mp,mj", dest, source);
9532 else
9533 macro_build (NULL, "or", "d,v,t", dest, source, 0);
9534 }
9535
9536 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
9537 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9538 The two alternatives are:
9539
9540 Global symbol Local symbol
9541 ------------- ------------
9542 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9543 ... ...
9544 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9545
9546 load_got_offset emits the first instruction and add_got_offset
9547 emits the second for a 16-bit offset or add_got_offset_hilo emits
9548 a sequence to add a 32-bit offset using a scratch register. */
9549
9550 static void
9551 load_got_offset (int dest, expressionS *local)
9552 {
9553 expressionS global;
9554
9555 global = *local;
9556 global.X_add_number = 0;
9557
9558 relax_start (local->X_add_symbol);
9559 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9560 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9561 relax_switch ();
9562 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9563 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9564 relax_end ();
9565 }
9566
9567 static void
9568 add_got_offset (int dest, expressionS *local)
9569 {
9570 expressionS global;
9571
9572 global.X_op = O_constant;
9573 global.X_op_symbol = NULL;
9574 global.X_add_symbol = NULL;
9575 global.X_add_number = local->X_add_number;
9576
9577 relax_start (local->X_add_symbol);
9578 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
9579 dest, dest, BFD_RELOC_LO16);
9580 relax_switch ();
9581 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
9582 relax_end ();
9583 }
9584
9585 static void
9586 add_got_offset_hilo (int dest, expressionS *local, int tmp)
9587 {
9588 expressionS global;
9589 int hold_mips_optimize;
9590
9591 global.X_op = O_constant;
9592 global.X_op_symbol = NULL;
9593 global.X_add_symbol = NULL;
9594 global.X_add_number = local->X_add_number;
9595
9596 relax_start (local->X_add_symbol);
9597 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9598 relax_switch ();
9599 /* Set mips_optimize around the lui instruction to avoid
9600 inserting an unnecessary nop after the lw. */
9601 hold_mips_optimize = mips_optimize;
9602 mips_optimize = 2;
9603 macro_build_lui (&global, tmp);
9604 mips_optimize = hold_mips_optimize;
9605 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9606 relax_end ();
9607
9608 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9609 }
9610
9611 /* Emit a sequence of instructions to emulate a branch likely operation.
9612 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9613 is its complementing branch with the original condition negated.
9614 CALL is set if the original branch specified the link operation.
9615 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9616
9617 Code like this is produced in the noreorder mode:
9618
9619 BRNEG <args>, 1f
9620 nop
9621 b <sym>
9622 delay slot (executed only if branch taken)
9623 1:
9624
9625 or, if CALL is set:
9626
9627 BRNEG <args>, 1f
9628 nop
9629 bal <sym>
9630 delay slot (executed only if branch taken)
9631 1:
9632
9633 In the reorder mode the delay slot would be filled with a nop anyway,
9634 so code produced is simply:
9635
9636 BR <args>, <sym>
9637 nop
9638
9639 This function is used when producing code for the microMIPS ASE that
9640 does not implement branch likely instructions in hardware. */
9641
9642 static void
9643 macro_build_branch_likely (const char *br, const char *brneg,
9644 int call, expressionS *ep, const char *fmt,
9645 unsigned int sreg, unsigned int treg)
9646 {
9647 int noreorder = mips_opts.noreorder;
9648 expressionS expr1;
9649
9650 gas_assert (mips_opts.micromips);
9651 start_noreorder ();
9652 if (noreorder)
9653 {
9654 micromips_label_expr (&expr1);
9655 macro_build (&expr1, brneg, fmt, sreg, treg);
9656 macro_build (NULL, "nop", "");
9657 macro_build (ep, call ? "bal" : "b", "p");
9658
9659 /* Set to true so that append_insn adds a label. */
9660 emit_branch_likely_macro = TRUE;
9661 }
9662 else
9663 {
9664 macro_build (ep, br, fmt, sreg, treg);
9665 macro_build (NULL, "nop", "");
9666 }
9667 end_noreorder ();
9668 }
9669
9670 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9671 the condition code tested. EP specifies the branch target. */
9672
9673 static void
9674 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9675 {
9676 const int call = 0;
9677 const char *brneg;
9678 const char *br;
9679
9680 switch (type)
9681 {
9682 case M_BC1FL:
9683 br = "bc1f";
9684 brneg = "bc1t";
9685 break;
9686 case M_BC1TL:
9687 br = "bc1t";
9688 brneg = "bc1f";
9689 break;
9690 case M_BC2FL:
9691 br = "bc2f";
9692 brneg = "bc2t";
9693 break;
9694 case M_BC2TL:
9695 br = "bc2t";
9696 brneg = "bc2f";
9697 break;
9698 default:
9699 abort ();
9700 }
9701 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9702 }
9703
9704 /* Emit a two-argument branch macro specified by TYPE, using SREG as
9705 the register tested. EP specifies the branch target. */
9706
9707 static void
9708 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9709 {
9710 const char *brneg = NULL;
9711 const char *br;
9712 int call = 0;
9713
9714 switch (type)
9715 {
9716 case M_BGEZ:
9717 br = "bgez";
9718 break;
9719 case M_BGEZL:
9720 br = mips_opts.micromips ? "bgez" : "bgezl";
9721 brneg = "bltz";
9722 break;
9723 case M_BGEZALL:
9724 gas_assert (mips_opts.micromips);
9725 br = mips_opts.insn32 ? "bgezal" : "bgezals";
9726 brneg = "bltz";
9727 call = 1;
9728 break;
9729 case M_BGTZ:
9730 br = "bgtz";
9731 break;
9732 case M_BGTZL:
9733 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9734 brneg = "blez";
9735 break;
9736 case M_BLEZ:
9737 br = "blez";
9738 break;
9739 case M_BLEZL:
9740 br = mips_opts.micromips ? "blez" : "blezl";
9741 brneg = "bgtz";
9742 break;
9743 case M_BLTZ:
9744 br = "bltz";
9745 break;
9746 case M_BLTZL:
9747 br = mips_opts.micromips ? "bltz" : "bltzl";
9748 brneg = "bgez";
9749 break;
9750 case M_BLTZALL:
9751 gas_assert (mips_opts.micromips);
9752 br = mips_opts.insn32 ? "bltzal" : "bltzals";
9753 brneg = "bgez";
9754 call = 1;
9755 break;
9756 default:
9757 abort ();
9758 }
9759 if (mips_opts.micromips && brneg)
9760 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9761 else
9762 macro_build (ep, br, "s,p", sreg);
9763 }
9764
9765 /* Emit a three-argument branch macro specified by TYPE, using SREG and
9766 TREG as the registers tested. EP specifies the branch target. */
9767
9768 static void
9769 macro_build_branch_rsrt (int type, expressionS *ep,
9770 unsigned int sreg, unsigned int treg)
9771 {
9772 const char *brneg = NULL;
9773 const int call = 0;
9774 const char *br;
9775
9776 switch (type)
9777 {
9778 case M_BEQ:
9779 case M_BEQ_I:
9780 br = "beq";
9781 break;
9782 case M_BEQL:
9783 case M_BEQL_I:
9784 br = mips_opts.micromips ? "beq" : "beql";
9785 brneg = "bne";
9786 break;
9787 case M_BNE:
9788 case M_BNE_I:
9789 br = "bne";
9790 break;
9791 case M_BNEL:
9792 case M_BNEL_I:
9793 br = mips_opts.micromips ? "bne" : "bnel";
9794 brneg = "beq";
9795 break;
9796 default:
9797 abort ();
9798 }
9799 if (mips_opts.micromips && brneg)
9800 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
9801 else
9802 macro_build (ep, br, "s,t,p", sreg, treg);
9803 }
9804
9805 /* Return the high part that should be loaded in order to make the low
9806 part of VALUE accessible using an offset of OFFBITS bits. */
9807
9808 static offsetT
9809 offset_high_part (offsetT value, unsigned int offbits)
9810 {
9811 offsetT bias;
9812 addressT low_mask;
9813
9814 if (offbits == 0)
9815 return value;
9816 bias = 1 << (offbits - 1);
9817 low_mask = bias * 2 - 1;
9818 return (value + bias) & ~low_mask;
9819 }
9820
9821 /* Return true if the value stored in offset_expr and offset_reloc
9822 fits into a signed offset of OFFBITS bits. RANGE is the maximum
9823 amount that the caller wants to add without inducing overflow
9824 and ALIGN is the known alignment of the value in bytes. */
9825
9826 static bfd_boolean
9827 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
9828 {
9829 if (offbits == 16)
9830 {
9831 /* Accept any relocation operator if overflow isn't a concern. */
9832 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
9833 return TRUE;
9834
9835 /* These relocations are guaranteed not to overflow in correct links. */
9836 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
9837 || gprel16_reloc_p (*offset_reloc))
9838 return TRUE;
9839 }
9840 if (offset_expr.X_op == O_constant
9841 && offset_high_part (offset_expr.X_add_number, offbits) == 0
9842 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
9843 return TRUE;
9844 return FALSE;
9845 }
9846
9847 /*
9848 * Build macros
9849 * This routine implements the seemingly endless macro or synthesized
9850 * instructions and addressing modes in the mips assembly language. Many
9851 * of these macros are simple and are similar to each other. These could
9852 * probably be handled by some kind of table or grammar approach instead of
9853 * this verbose method. Others are not simple macros but are more like
9854 * optimizing code generation.
9855 * One interesting optimization is when several store macros appear
9856 * consecutively that would load AT with the upper half of the same address.
9857 * The ensuing load upper instructions are omitted. This implies some kind
9858 * of global optimization. We currently only optimize within a single macro.
9859 * For many of the load and store macros if the address is specified as a
9860 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
9861 * first load register 'at' with zero and use it as the base register. The
9862 * mips assembler simply uses register $zero. Just one tiny optimization
9863 * we're missing.
9864 */
9865 static void
9866 macro (struct mips_cl_insn *ip, char *str)
9867 {
9868 const struct mips_operand_array *operands;
9869 unsigned int breg, i;
9870 unsigned int tempreg;
9871 int mask;
9872 int used_at = 0;
9873 expressionS label_expr;
9874 expressionS expr1;
9875 expressionS *ep;
9876 const char *s;
9877 const char *s2;
9878 const char *fmt;
9879 int likely = 0;
9880 int coproc = 0;
9881 int offbits = 16;
9882 int call = 0;
9883 int jals = 0;
9884 int dbl = 0;
9885 int imm = 0;
9886 int ust = 0;
9887 int lp = 0;
9888 bfd_boolean large_offset;
9889 int off;
9890 int hold_mips_optimize;
9891 unsigned int align;
9892 unsigned int op[MAX_OPERANDS];
9893
9894 gas_assert (! mips_opts.mips16);
9895
9896 operands = insn_operands (ip);
9897 for (i = 0; i < MAX_OPERANDS; i++)
9898 if (operands->operand[i])
9899 op[i] = insn_extract_operand (ip, operands->operand[i]);
9900 else
9901 op[i] = -1;
9902
9903 mask = ip->insn_mo->mask;
9904
9905 label_expr.X_op = O_constant;
9906 label_expr.X_op_symbol = NULL;
9907 label_expr.X_add_symbol = NULL;
9908 label_expr.X_add_number = 0;
9909
9910 expr1.X_op = O_constant;
9911 expr1.X_op_symbol = NULL;
9912 expr1.X_add_symbol = NULL;
9913 expr1.X_add_number = 1;
9914 align = 1;
9915
9916 switch (mask)
9917 {
9918 case M_DABS:
9919 dbl = 1;
9920 /* Fall through. */
9921 case M_ABS:
9922 /* bgez $a0,1f
9923 move v0,$a0
9924 sub v0,$zero,$a0
9925 1:
9926 */
9927
9928 start_noreorder ();
9929
9930 if (mips_opts.micromips)
9931 micromips_label_expr (&label_expr);
9932 else
9933 label_expr.X_add_number = 8;
9934 macro_build (&label_expr, "bgez", "s,p", op[1]);
9935 if (op[0] == op[1])
9936 macro_build (NULL, "nop", "");
9937 else
9938 move_register (op[0], op[1]);
9939 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
9940 if (mips_opts.micromips)
9941 micromips_add_label ();
9942
9943 end_noreorder ();
9944 break;
9945
9946 case M_ADD_I:
9947 s = "addi";
9948 s2 = "add";
9949 goto do_addi;
9950 case M_ADDU_I:
9951 s = "addiu";
9952 s2 = "addu";
9953 goto do_addi;
9954 case M_DADD_I:
9955 dbl = 1;
9956 s = "daddi";
9957 s2 = "dadd";
9958 if (!mips_opts.micromips)
9959 goto do_addi;
9960 if (imm_expr.X_add_number >= -0x200
9961 && imm_expr.X_add_number < 0x200)
9962 {
9963 macro_build (NULL, s, "t,r,.", op[0], op[1],
9964 (int) imm_expr.X_add_number);
9965 break;
9966 }
9967 goto do_addi_i;
9968 case M_DADDU_I:
9969 dbl = 1;
9970 s = "daddiu";
9971 s2 = "daddu";
9972 do_addi:
9973 if (imm_expr.X_add_number >= -0x8000
9974 && imm_expr.X_add_number < 0x8000)
9975 {
9976 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
9977 break;
9978 }
9979 do_addi_i:
9980 used_at = 1;
9981 load_register (AT, &imm_expr, dbl);
9982 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
9983 break;
9984
9985 case M_AND_I:
9986 s = "andi";
9987 s2 = "and";
9988 goto do_bit;
9989 case M_OR_I:
9990 s = "ori";
9991 s2 = "or";
9992 goto do_bit;
9993 case M_NOR_I:
9994 s = "";
9995 s2 = "nor";
9996 goto do_bit;
9997 case M_XOR_I:
9998 s = "xori";
9999 s2 = "xor";
10000 do_bit:
10001 if (imm_expr.X_add_number >= 0
10002 && imm_expr.X_add_number < 0x10000)
10003 {
10004 if (mask != M_NOR_I)
10005 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
10006 else
10007 {
10008 macro_build (&imm_expr, "ori", "t,r,i",
10009 op[0], op[1], BFD_RELOC_LO16);
10010 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
10011 }
10012 break;
10013 }
10014
10015 used_at = 1;
10016 load_register (AT, &imm_expr, GPR_SIZE == 64);
10017 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10018 break;
10019
10020 case M_BALIGN:
10021 switch (imm_expr.X_add_number)
10022 {
10023 case 0:
10024 macro_build (NULL, "nop", "");
10025 break;
10026 case 2:
10027 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
10028 break;
10029 case 1:
10030 case 3:
10031 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
10032 (int) imm_expr.X_add_number);
10033 break;
10034 default:
10035 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10036 (unsigned long) imm_expr.X_add_number);
10037 break;
10038 }
10039 break;
10040
10041 case M_BC1FL:
10042 case M_BC1TL:
10043 case M_BC2FL:
10044 case M_BC2TL:
10045 gas_assert (mips_opts.micromips);
10046 macro_build_branch_ccl (mask, &offset_expr,
10047 EXTRACT_OPERAND (1, BCC, *ip));
10048 break;
10049
10050 case M_BEQ_I:
10051 case M_BEQL_I:
10052 case M_BNE_I:
10053 case M_BNEL_I:
10054 if (imm_expr.X_add_number == 0)
10055 op[1] = 0;
10056 else
10057 {
10058 op[1] = AT;
10059 used_at = 1;
10060 load_register (op[1], &imm_expr, GPR_SIZE == 64);
10061 }
10062 /* Fall through. */
10063 case M_BEQL:
10064 case M_BNEL:
10065 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
10066 break;
10067
10068 case M_BGEL:
10069 likely = 1;
10070 /* Fall through. */
10071 case M_BGE:
10072 if (op[1] == 0)
10073 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10074 else if (op[0] == 0)
10075 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
10076 else
10077 {
10078 used_at = 1;
10079 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10080 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10081 &offset_expr, AT, ZERO);
10082 }
10083 break;
10084
10085 case M_BGEZL:
10086 case M_BGEZALL:
10087 case M_BGTZL:
10088 case M_BLEZL:
10089 case M_BLTZL:
10090 case M_BLTZALL:
10091 macro_build_branch_rs (mask, &offset_expr, op[0]);
10092 break;
10093
10094 case M_BGTL_I:
10095 likely = 1;
10096 /* Fall through. */
10097 case M_BGT_I:
10098 /* Check for > max integer. */
10099 if (imm_expr.X_add_number >= GPR_SMAX)
10100 {
10101 do_false:
10102 /* Result is always false. */
10103 if (! likely)
10104 macro_build (NULL, "nop", "");
10105 else
10106 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
10107 break;
10108 }
10109 ++imm_expr.X_add_number;
10110 /* FALLTHROUGH */
10111 case M_BGE_I:
10112 case M_BGEL_I:
10113 if (mask == M_BGEL_I)
10114 likely = 1;
10115 if (imm_expr.X_add_number == 0)
10116 {
10117 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
10118 &offset_expr, op[0]);
10119 break;
10120 }
10121 if (imm_expr.X_add_number == 1)
10122 {
10123 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
10124 &offset_expr, op[0]);
10125 break;
10126 }
10127 if (imm_expr.X_add_number <= GPR_SMIN)
10128 {
10129 do_true:
10130 /* result is always true */
10131 as_warn (_("branch %s is always true"), ip->insn_mo->name);
10132 macro_build (&offset_expr, "b", "p");
10133 break;
10134 }
10135 used_at = 1;
10136 set_at (op[0], 0);
10137 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10138 &offset_expr, AT, ZERO);
10139 break;
10140
10141 case M_BGEUL:
10142 likely = 1;
10143 /* Fall through. */
10144 case M_BGEU:
10145 if (op[1] == 0)
10146 goto do_true;
10147 else if (op[0] == 0)
10148 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10149 &offset_expr, ZERO, op[1]);
10150 else
10151 {
10152 used_at = 1;
10153 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10154 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10155 &offset_expr, AT, ZERO);
10156 }
10157 break;
10158
10159 case M_BGTUL_I:
10160 likely = 1;
10161 /* Fall through. */
10162 case M_BGTU_I:
10163 if (op[0] == 0
10164 || (GPR_SIZE == 32
10165 && imm_expr.X_add_number == -1))
10166 goto do_false;
10167 ++imm_expr.X_add_number;
10168 /* FALLTHROUGH */
10169 case M_BGEU_I:
10170 case M_BGEUL_I:
10171 if (mask == M_BGEUL_I)
10172 likely = 1;
10173 if (imm_expr.X_add_number == 0)
10174 goto do_true;
10175 else if (imm_expr.X_add_number == 1)
10176 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10177 &offset_expr, op[0], ZERO);
10178 else
10179 {
10180 used_at = 1;
10181 set_at (op[0], 1);
10182 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10183 &offset_expr, AT, ZERO);
10184 }
10185 break;
10186
10187 case M_BGTL:
10188 likely = 1;
10189 /* Fall through. */
10190 case M_BGT:
10191 if (op[1] == 0)
10192 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10193 else if (op[0] == 0)
10194 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
10195 else
10196 {
10197 used_at = 1;
10198 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10199 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10200 &offset_expr, AT, ZERO);
10201 }
10202 break;
10203
10204 case M_BGTUL:
10205 likely = 1;
10206 /* Fall through. */
10207 case M_BGTU:
10208 if (op[1] == 0)
10209 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10210 &offset_expr, op[0], ZERO);
10211 else if (op[0] == 0)
10212 goto do_false;
10213 else
10214 {
10215 used_at = 1;
10216 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10217 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10218 &offset_expr, AT, ZERO);
10219 }
10220 break;
10221
10222 case M_BLEL:
10223 likely = 1;
10224 /* Fall through. */
10225 case M_BLE:
10226 if (op[1] == 0)
10227 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10228 else if (op[0] == 0)
10229 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
10230 else
10231 {
10232 used_at = 1;
10233 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10234 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10235 &offset_expr, AT, ZERO);
10236 }
10237 break;
10238
10239 case M_BLEL_I:
10240 likely = 1;
10241 /* Fall through. */
10242 case M_BLE_I:
10243 if (imm_expr.X_add_number >= GPR_SMAX)
10244 goto do_true;
10245 ++imm_expr.X_add_number;
10246 /* FALLTHROUGH */
10247 case M_BLT_I:
10248 case M_BLTL_I:
10249 if (mask == M_BLTL_I)
10250 likely = 1;
10251 if (imm_expr.X_add_number == 0)
10252 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10253 else if (imm_expr.X_add_number == 1)
10254 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10255 else
10256 {
10257 used_at = 1;
10258 set_at (op[0], 0);
10259 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10260 &offset_expr, AT, ZERO);
10261 }
10262 break;
10263
10264 case M_BLEUL:
10265 likely = 1;
10266 /* Fall through. */
10267 case M_BLEU:
10268 if (op[1] == 0)
10269 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10270 &offset_expr, op[0], ZERO);
10271 else if (op[0] == 0)
10272 goto do_true;
10273 else
10274 {
10275 used_at = 1;
10276 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10277 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10278 &offset_expr, AT, ZERO);
10279 }
10280 break;
10281
10282 case M_BLEUL_I:
10283 likely = 1;
10284 /* Fall through. */
10285 case M_BLEU_I:
10286 if (op[0] == 0
10287 || (GPR_SIZE == 32
10288 && imm_expr.X_add_number == -1))
10289 goto do_true;
10290 ++imm_expr.X_add_number;
10291 /* FALLTHROUGH */
10292 case M_BLTU_I:
10293 case M_BLTUL_I:
10294 if (mask == M_BLTUL_I)
10295 likely = 1;
10296 if (imm_expr.X_add_number == 0)
10297 goto do_false;
10298 else if (imm_expr.X_add_number == 1)
10299 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10300 &offset_expr, op[0], ZERO);
10301 else
10302 {
10303 used_at = 1;
10304 set_at (op[0], 1);
10305 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10306 &offset_expr, AT, ZERO);
10307 }
10308 break;
10309
10310 case M_BLTL:
10311 likely = 1;
10312 /* Fall through. */
10313 case M_BLT:
10314 if (op[1] == 0)
10315 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10316 else if (op[0] == 0)
10317 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
10318 else
10319 {
10320 used_at = 1;
10321 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10322 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10323 &offset_expr, AT, ZERO);
10324 }
10325 break;
10326
10327 case M_BLTUL:
10328 likely = 1;
10329 /* Fall through. */
10330 case M_BLTU:
10331 if (op[1] == 0)
10332 goto do_false;
10333 else if (op[0] == 0)
10334 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10335 &offset_expr, ZERO, op[1]);
10336 else
10337 {
10338 used_at = 1;
10339 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10340 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10341 &offset_expr, AT, ZERO);
10342 }
10343 break;
10344
10345 case M_DDIV_3:
10346 dbl = 1;
10347 /* Fall through. */
10348 case M_DIV_3:
10349 s = "mflo";
10350 goto do_div3;
10351 case M_DREM_3:
10352 dbl = 1;
10353 /* Fall through. */
10354 case M_REM_3:
10355 s = "mfhi";
10356 do_div3:
10357 if (op[2] == 0)
10358 {
10359 as_warn (_("divide by zero"));
10360 if (mips_trap)
10361 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10362 else
10363 macro_build (NULL, "break", BRK_FMT, 7);
10364 break;
10365 }
10366
10367 start_noreorder ();
10368 if (mips_trap)
10369 {
10370 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10371 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10372 }
10373 else
10374 {
10375 if (mips_opts.micromips)
10376 micromips_label_expr (&label_expr);
10377 else
10378 label_expr.X_add_number = 8;
10379 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10380 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10381 macro_build (NULL, "break", BRK_FMT, 7);
10382 if (mips_opts.micromips)
10383 micromips_add_label ();
10384 }
10385 expr1.X_add_number = -1;
10386 used_at = 1;
10387 load_register (AT, &expr1, dbl);
10388 if (mips_opts.micromips)
10389 micromips_label_expr (&label_expr);
10390 else
10391 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
10392 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
10393 if (dbl)
10394 {
10395 expr1.X_add_number = 1;
10396 load_register (AT, &expr1, dbl);
10397 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
10398 }
10399 else
10400 {
10401 expr1.X_add_number = 0x80000000;
10402 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
10403 }
10404 if (mips_trap)
10405 {
10406 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
10407 /* We want to close the noreorder block as soon as possible, so
10408 that later insns are available for delay slot filling. */
10409 end_noreorder ();
10410 }
10411 else
10412 {
10413 if (mips_opts.micromips)
10414 micromips_label_expr (&label_expr);
10415 else
10416 label_expr.X_add_number = 8;
10417 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
10418 macro_build (NULL, "nop", "");
10419
10420 /* We want to close the noreorder block as soon as possible, so
10421 that later insns are available for delay slot filling. */
10422 end_noreorder ();
10423
10424 macro_build (NULL, "break", BRK_FMT, 6);
10425 }
10426 if (mips_opts.micromips)
10427 micromips_add_label ();
10428 macro_build (NULL, s, MFHL_FMT, op[0]);
10429 break;
10430
10431 case M_DIV_3I:
10432 s = "div";
10433 s2 = "mflo";
10434 goto do_divi;
10435 case M_DIVU_3I:
10436 s = "divu";
10437 s2 = "mflo";
10438 goto do_divi;
10439 case M_REM_3I:
10440 s = "div";
10441 s2 = "mfhi";
10442 goto do_divi;
10443 case M_REMU_3I:
10444 s = "divu";
10445 s2 = "mfhi";
10446 goto do_divi;
10447 case M_DDIV_3I:
10448 dbl = 1;
10449 s = "ddiv";
10450 s2 = "mflo";
10451 goto do_divi;
10452 case M_DDIVU_3I:
10453 dbl = 1;
10454 s = "ddivu";
10455 s2 = "mflo";
10456 goto do_divi;
10457 case M_DREM_3I:
10458 dbl = 1;
10459 s = "ddiv";
10460 s2 = "mfhi";
10461 goto do_divi;
10462 case M_DREMU_3I:
10463 dbl = 1;
10464 s = "ddivu";
10465 s2 = "mfhi";
10466 do_divi:
10467 if (imm_expr.X_add_number == 0)
10468 {
10469 as_warn (_("divide by zero"));
10470 if (mips_trap)
10471 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10472 else
10473 macro_build (NULL, "break", BRK_FMT, 7);
10474 break;
10475 }
10476 if (imm_expr.X_add_number == 1)
10477 {
10478 if (strcmp (s2, "mflo") == 0)
10479 move_register (op[0], op[1]);
10480 else
10481 move_register (op[0], ZERO);
10482 break;
10483 }
10484 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
10485 {
10486 if (strcmp (s2, "mflo") == 0)
10487 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
10488 else
10489 move_register (op[0], ZERO);
10490 break;
10491 }
10492
10493 used_at = 1;
10494 load_register (AT, &imm_expr, dbl);
10495 macro_build (NULL, s, "z,s,t", op[1], AT);
10496 macro_build (NULL, s2, MFHL_FMT, op[0]);
10497 break;
10498
10499 case M_DIVU_3:
10500 s = "divu";
10501 s2 = "mflo";
10502 goto do_divu3;
10503 case M_REMU_3:
10504 s = "divu";
10505 s2 = "mfhi";
10506 goto do_divu3;
10507 case M_DDIVU_3:
10508 s = "ddivu";
10509 s2 = "mflo";
10510 goto do_divu3;
10511 case M_DREMU_3:
10512 s = "ddivu";
10513 s2 = "mfhi";
10514 do_divu3:
10515 start_noreorder ();
10516 if (mips_trap)
10517 {
10518 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10519 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10520 /* We want to close the noreorder block as soon as possible, so
10521 that later insns are available for delay slot filling. */
10522 end_noreorder ();
10523 }
10524 else
10525 {
10526 if (mips_opts.micromips)
10527 micromips_label_expr (&label_expr);
10528 else
10529 label_expr.X_add_number = 8;
10530 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10531 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10532
10533 /* We want to close the noreorder block as soon as possible, so
10534 that later insns are available for delay slot filling. */
10535 end_noreorder ();
10536 macro_build (NULL, "break", BRK_FMT, 7);
10537 if (mips_opts.micromips)
10538 micromips_add_label ();
10539 }
10540 macro_build (NULL, s2, MFHL_FMT, op[0]);
10541 break;
10542
10543 case M_DLCA_AB:
10544 dbl = 1;
10545 /* Fall through. */
10546 case M_LCA_AB:
10547 call = 1;
10548 goto do_la;
10549 case M_DLA_AB:
10550 dbl = 1;
10551 /* Fall through. */
10552 case M_LA_AB:
10553 do_la:
10554 /* Load the address of a symbol into a register. If breg is not
10555 zero, we then add a base register to it. */
10556
10557 breg = op[2];
10558 if (dbl && GPR_SIZE == 32)
10559 as_warn (_("dla used to load 32-bit register; recommend using la "
10560 "instead"));
10561
10562 if (!dbl && HAVE_64BIT_OBJECTS)
10563 as_warn (_("la used to load 64-bit address; recommend using dla "
10564 "instead"));
10565
10566 if (small_offset_p (0, align, 16))
10567 {
10568 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
10569 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10570 break;
10571 }
10572
10573 if (mips_opts.at && (op[0] == breg))
10574 {
10575 tempreg = AT;
10576 used_at = 1;
10577 }
10578 else
10579 tempreg = op[0];
10580
10581 if (offset_expr.X_op != O_symbol
10582 && offset_expr.X_op != O_constant)
10583 {
10584 as_bad (_("expression too complex"));
10585 offset_expr.X_op = O_constant;
10586 }
10587
10588 if (offset_expr.X_op == O_constant)
10589 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
10590 else if (mips_pic == NO_PIC)
10591 {
10592 /* If this is a reference to a GP relative symbol, we want
10593 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
10594 Otherwise we want
10595 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10596 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10597 If we have a constant, we need two instructions anyhow,
10598 so we may as well always use the latter form.
10599
10600 With 64bit address space and a usable $at we want
10601 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10602 lui $at,<sym> (BFD_RELOC_HI16_S)
10603 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10604 daddiu $at,<sym> (BFD_RELOC_LO16)
10605 dsll32 $tempreg,0
10606 daddu $tempreg,$tempreg,$at
10607
10608 If $at is already in use, we use a path which is suboptimal
10609 on superscalar processors.
10610 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10611 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10612 dsll $tempreg,16
10613 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10614 dsll $tempreg,16
10615 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10616
10617 For GP relative symbols in 64bit address space we can use
10618 the same sequence as in 32bit address space. */
10619 if (HAVE_64BIT_SYMBOLS)
10620 {
10621 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10622 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10623 {
10624 relax_start (offset_expr.X_add_symbol);
10625 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10626 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10627 relax_switch ();
10628 }
10629
10630 if (used_at == 0 && mips_opts.at)
10631 {
10632 macro_build (&offset_expr, "lui", LUI_FMT,
10633 tempreg, BFD_RELOC_MIPS_HIGHEST);
10634 macro_build (&offset_expr, "lui", LUI_FMT,
10635 AT, BFD_RELOC_HI16_S);
10636 macro_build (&offset_expr, "daddiu", "t,r,j",
10637 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10638 macro_build (&offset_expr, "daddiu", "t,r,j",
10639 AT, AT, BFD_RELOC_LO16);
10640 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
10641 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
10642 used_at = 1;
10643 }
10644 else
10645 {
10646 macro_build (&offset_expr, "lui", LUI_FMT,
10647 tempreg, BFD_RELOC_MIPS_HIGHEST);
10648 macro_build (&offset_expr, "daddiu", "t,r,j",
10649 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10650 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10651 macro_build (&offset_expr, "daddiu", "t,r,j",
10652 tempreg, tempreg, BFD_RELOC_HI16_S);
10653 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10654 macro_build (&offset_expr, "daddiu", "t,r,j",
10655 tempreg, tempreg, BFD_RELOC_LO16);
10656 }
10657
10658 if (mips_relax.sequence)
10659 relax_end ();
10660 }
10661 else
10662 {
10663 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10664 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10665 {
10666 relax_start (offset_expr.X_add_symbol);
10667 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10668 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10669 relax_switch ();
10670 }
10671 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
10672 as_bad (_("offset too large"));
10673 macro_build_lui (&offset_expr, tempreg);
10674 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10675 tempreg, tempreg, BFD_RELOC_LO16);
10676 if (mips_relax.sequence)
10677 relax_end ();
10678 }
10679 }
10680 else if (!mips_big_got && !HAVE_NEWABI)
10681 {
10682 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10683
10684 /* If this is a reference to an external symbol, and there
10685 is no constant, we want
10686 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10687 or for lca or if tempreg is PIC_CALL_REG
10688 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10689 For a local symbol, we want
10690 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10691 nop
10692 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10693
10694 If we have a small constant, and this is a reference to
10695 an external symbol, we want
10696 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10697 nop
10698 addiu $tempreg,$tempreg,<constant>
10699 For a local symbol, we want the same instruction
10700 sequence, but we output a BFD_RELOC_LO16 reloc on the
10701 addiu instruction.
10702
10703 If we have a large constant, and this is a reference to
10704 an external symbol, we want
10705 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10706 lui $at,<hiconstant>
10707 addiu $at,$at,<loconstant>
10708 addu $tempreg,$tempreg,$at
10709 For a local symbol, we want the same instruction
10710 sequence, but we output a BFD_RELOC_LO16 reloc on the
10711 addiu instruction.
10712 */
10713
10714 if (offset_expr.X_add_number == 0)
10715 {
10716 if (mips_pic == SVR4_PIC
10717 && breg == 0
10718 && (call || tempreg == PIC_CALL_REG))
10719 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10720
10721 relax_start (offset_expr.X_add_symbol);
10722 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10723 lw_reloc_type, mips_gp_register);
10724 if (breg != 0)
10725 {
10726 /* We're going to put in an addu instruction using
10727 tempreg, so we may as well insert the nop right
10728 now. */
10729 load_delay_nop ();
10730 }
10731 relax_switch ();
10732 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10733 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
10734 load_delay_nop ();
10735 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10736 tempreg, tempreg, BFD_RELOC_LO16);
10737 relax_end ();
10738 /* FIXME: If breg == 0, and the next instruction uses
10739 $tempreg, then if this variant case is used an extra
10740 nop will be generated. */
10741 }
10742 else if (offset_expr.X_add_number >= -0x8000
10743 && offset_expr.X_add_number < 0x8000)
10744 {
10745 load_got_offset (tempreg, &offset_expr);
10746 load_delay_nop ();
10747 add_got_offset (tempreg, &offset_expr);
10748 }
10749 else
10750 {
10751 expr1.X_add_number = offset_expr.X_add_number;
10752 offset_expr.X_add_number =
10753 SEXT_16BIT (offset_expr.X_add_number);
10754 load_got_offset (tempreg, &offset_expr);
10755 offset_expr.X_add_number = expr1.X_add_number;
10756 /* If we are going to add in a base register, and the
10757 target register and the base register are the same,
10758 then we are using AT as a temporary register. Since
10759 we want to load the constant into AT, we add our
10760 current AT (from the global offset table) and the
10761 register into the register now, and pretend we were
10762 not using a base register. */
10763 if (breg == op[0])
10764 {
10765 load_delay_nop ();
10766 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10767 op[0], AT, breg);
10768 breg = 0;
10769 tempreg = op[0];
10770 }
10771 add_got_offset_hilo (tempreg, &offset_expr, AT);
10772 used_at = 1;
10773 }
10774 }
10775 else if (!mips_big_got && HAVE_NEWABI)
10776 {
10777 int add_breg_early = 0;
10778
10779 /* If this is a reference to an external, and there is no
10780 constant, or local symbol (*), with or without a
10781 constant, we want
10782 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10783 or for lca or if tempreg is PIC_CALL_REG
10784 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10785
10786 If we have a small constant, and this is a reference to
10787 an external symbol, we want
10788 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10789 addiu $tempreg,$tempreg,<constant>
10790
10791 If we have a large constant, and this is a reference to
10792 an external symbol, we want
10793 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10794 lui $at,<hiconstant>
10795 addiu $at,$at,<loconstant>
10796 addu $tempreg,$tempreg,$at
10797
10798 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
10799 local symbols, even though it introduces an additional
10800 instruction. */
10801
10802 if (offset_expr.X_add_number)
10803 {
10804 expr1.X_add_number = offset_expr.X_add_number;
10805 offset_expr.X_add_number = 0;
10806
10807 relax_start (offset_expr.X_add_symbol);
10808 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10809 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10810
10811 if (expr1.X_add_number >= -0x8000
10812 && expr1.X_add_number < 0x8000)
10813 {
10814 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10815 tempreg, tempreg, BFD_RELOC_LO16);
10816 }
10817 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
10818 {
10819 unsigned int dreg;
10820
10821 /* If we are going to add in a base register, and the
10822 target register and the base register are the same,
10823 then we are using AT as a temporary register. Since
10824 we want to load the constant into AT, we add our
10825 current AT (from the global offset table) and the
10826 register into the register now, and pretend we were
10827 not using a base register. */
10828 if (breg != op[0])
10829 dreg = tempreg;
10830 else
10831 {
10832 gas_assert (tempreg == AT);
10833 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10834 op[0], AT, breg);
10835 dreg = op[0];
10836 add_breg_early = 1;
10837 }
10838
10839 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
10840 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10841 dreg, dreg, AT);
10842
10843 used_at = 1;
10844 }
10845 else
10846 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10847
10848 relax_switch ();
10849 offset_expr.X_add_number = expr1.X_add_number;
10850
10851 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10852 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10853 if (add_breg_early)
10854 {
10855 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10856 op[0], tempreg, breg);
10857 breg = 0;
10858 tempreg = op[0];
10859 }
10860 relax_end ();
10861 }
10862 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
10863 {
10864 relax_start (offset_expr.X_add_symbol);
10865 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10866 BFD_RELOC_MIPS_CALL16, mips_gp_register);
10867 relax_switch ();
10868 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10869 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10870 relax_end ();
10871 }
10872 else
10873 {
10874 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10875 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10876 }
10877 }
10878 else if (mips_big_got && !HAVE_NEWABI)
10879 {
10880 int gpdelay;
10881 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10882 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
10883 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10884
10885 /* This is the large GOT case. If this is a reference to an
10886 external symbol, and there is no constant, we want
10887 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10888 addu $tempreg,$tempreg,$gp
10889 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10890 or for lca or if tempreg is PIC_CALL_REG
10891 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10892 addu $tempreg,$tempreg,$gp
10893 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
10894 For a local symbol, we want
10895 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10896 nop
10897 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10898
10899 If we have a small constant, and this is a reference to
10900 an external symbol, we want
10901 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10902 addu $tempreg,$tempreg,$gp
10903 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10904 nop
10905 addiu $tempreg,$tempreg,<constant>
10906 For a local symbol, we want
10907 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10908 nop
10909 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
10910
10911 If we have a large constant, and this is a reference to
10912 an external symbol, we want
10913 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10914 addu $tempreg,$tempreg,$gp
10915 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10916 lui $at,<hiconstant>
10917 addiu $at,$at,<loconstant>
10918 addu $tempreg,$tempreg,$at
10919 For a local symbol, we want
10920 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10921 lui $at,<hiconstant>
10922 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
10923 addu $tempreg,$tempreg,$at
10924 */
10925
10926 expr1.X_add_number = offset_expr.X_add_number;
10927 offset_expr.X_add_number = 0;
10928 relax_start (offset_expr.X_add_symbol);
10929 gpdelay = reg_needs_delay (mips_gp_register);
10930 if (expr1.X_add_number == 0 && breg == 0
10931 && (call || tempreg == PIC_CALL_REG))
10932 {
10933 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10934 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10935 }
10936 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
10937 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10938 tempreg, tempreg, mips_gp_register);
10939 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10940 tempreg, lw_reloc_type, tempreg);
10941 if (expr1.X_add_number == 0)
10942 {
10943 if (breg != 0)
10944 {
10945 /* We're going to put in an addu instruction using
10946 tempreg, so we may as well insert the nop right
10947 now. */
10948 load_delay_nop ();
10949 }
10950 }
10951 else if (expr1.X_add_number >= -0x8000
10952 && expr1.X_add_number < 0x8000)
10953 {
10954 load_delay_nop ();
10955 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10956 tempreg, tempreg, BFD_RELOC_LO16);
10957 }
10958 else
10959 {
10960 unsigned int dreg;
10961
10962 /* If we are going to add in a base register, and the
10963 target register and the base register are the same,
10964 then we are using AT as a temporary register. Since
10965 we want to load the constant into AT, we add our
10966 current AT (from the global offset table) and the
10967 register into the register now, and pretend we were
10968 not using a base register. */
10969 if (breg != op[0])
10970 dreg = tempreg;
10971 else
10972 {
10973 gas_assert (tempreg == AT);
10974 load_delay_nop ();
10975 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10976 op[0], AT, breg);
10977 dreg = op[0];
10978 }
10979
10980 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
10981 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
10982
10983 used_at = 1;
10984 }
10985 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
10986 relax_switch ();
10987
10988 if (gpdelay)
10989 {
10990 /* This is needed because this instruction uses $gp, but
10991 the first instruction on the main stream does not. */
10992 macro_build (NULL, "nop", "");
10993 }
10994
10995 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10996 local_reloc_type, mips_gp_register);
10997 if (expr1.X_add_number >= -0x8000
10998 && expr1.X_add_number < 0x8000)
10999 {
11000 load_delay_nop ();
11001 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11002 tempreg, tempreg, BFD_RELOC_LO16);
11003 /* FIXME: If add_number is 0, and there was no base
11004 register, the external symbol case ended with a load,
11005 so if the symbol turns out to not be external, and
11006 the next instruction uses tempreg, an unnecessary nop
11007 will be inserted. */
11008 }
11009 else
11010 {
11011 if (breg == op[0])
11012 {
11013 /* We must add in the base register now, as in the
11014 external symbol case. */
11015 gas_assert (tempreg == AT);
11016 load_delay_nop ();
11017 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11018 op[0], AT, breg);
11019 tempreg = op[0];
11020 /* We set breg to 0 because we have arranged to add
11021 it in in both cases. */
11022 breg = 0;
11023 }
11024
11025 macro_build_lui (&expr1, AT);
11026 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11027 AT, AT, BFD_RELOC_LO16);
11028 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11029 tempreg, tempreg, AT);
11030 used_at = 1;
11031 }
11032 relax_end ();
11033 }
11034 else if (mips_big_got && HAVE_NEWABI)
11035 {
11036 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11037 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11038 int add_breg_early = 0;
11039
11040 /* This is the large GOT case. If this is a reference to an
11041 external symbol, and there is no constant, we want
11042 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11043 add $tempreg,$tempreg,$gp
11044 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11045 or for lca or if tempreg is PIC_CALL_REG
11046 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11047 add $tempreg,$tempreg,$gp
11048 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11049
11050 If we have a small constant, and this is a reference to
11051 an external symbol, we want
11052 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11053 add $tempreg,$tempreg,$gp
11054 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11055 addi $tempreg,$tempreg,<constant>
11056
11057 If we have a large constant, and this is a reference to
11058 an external symbol, we want
11059 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11060 addu $tempreg,$tempreg,$gp
11061 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11062 lui $at,<hiconstant>
11063 addi $at,$at,<loconstant>
11064 add $tempreg,$tempreg,$at
11065
11066 If we have NewABI, and we know it's a local symbol, we want
11067 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11068 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11069 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11070
11071 relax_start (offset_expr.X_add_symbol);
11072
11073 expr1.X_add_number = offset_expr.X_add_number;
11074 offset_expr.X_add_number = 0;
11075
11076 if (expr1.X_add_number == 0 && breg == 0
11077 && (call || tempreg == PIC_CALL_REG))
11078 {
11079 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11080 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11081 }
11082 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11083 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11084 tempreg, tempreg, mips_gp_register);
11085 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11086 tempreg, lw_reloc_type, tempreg);
11087
11088 if (expr1.X_add_number == 0)
11089 ;
11090 else if (expr1.X_add_number >= -0x8000
11091 && expr1.X_add_number < 0x8000)
11092 {
11093 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11094 tempreg, tempreg, BFD_RELOC_LO16);
11095 }
11096 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11097 {
11098 unsigned int dreg;
11099
11100 /* If we are going to add in a base register, and the
11101 target register and the base register are the same,
11102 then we are using AT as a temporary register. Since
11103 we want to load the constant into AT, we add our
11104 current AT (from the global offset table) and the
11105 register into the register now, and pretend we were
11106 not using a base register. */
11107 if (breg != op[0])
11108 dreg = tempreg;
11109 else
11110 {
11111 gas_assert (tempreg == AT);
11112 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11113 op[0], AT, breg);
11114 dreg = op[0];
11115 add_breg_early = 1;
11116 }
11117
11118 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11119 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11120
11121 used_at = 1;
11122 }
11123 else
11124 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11125
11126 relax_switch ();
11127 offset_expr.X_add_number = expr1.X_add_number;
11128 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11129 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11130 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11131 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11132 if (add_breg_early)
11133 {
11134 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11135 op[0], tempreg, breg);
11136 breg = 0;
11137 tempreg = op[0];
11138 }
11139 relax_end ();
11140 }
11141 else
11142 abort ();
11143
11144 if (breg != 0)
11145 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
11146 break;
11147
11148 case M_MSGSND:
11149 gas_assert (!mips_opts.micromips);
11150 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
11151 break;
11152
11153 case M_MSGLD:
11154 gas_assert (!mips_opts.micromips);
11155 macro_build (NULL, "c2", "C", 0x02);
11156 break;
11157
11158 case M_MSGLD_T:
11159 gas_assert (!mips_opts.micromips);
11160 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
11161 break;
11162
11163 case M_MSGWAIT:
11164 gas_assert (!mips_opts.micromips);
11165 macro_build (NULL, "c2", "C", 3);
11166 break;
11167
11168 case M_MSGWAIT_T:
11169 gas_assert (!mips_opts.micromips);
11170 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
11171 break;
11172
11173 case M_J_A:
11174 /* The j instruction may not be used in PIC code, since it
11175 requires an absolute address. We convert it to a b
11176 instruction. */
11177 if (mips_pic == NO_PIC)
11178 macro_build (&offset_expr, "j", "a");
11179 else
11180 macro_build (&offset_expr, "b", "p");
11181 break;
11182
11183 /* The jal instructions must be handled as macros because when
11184 generating PIC code they expand to multi-instruction
11185 sequences. Normally they are simple instructions. */
11186 case M_JALS_1:
11187 op[1] = op[0];
11188 op[0] = RA;
11189 /* Fall through. */
11190 case M_JALS_2:
11191 gas_assert (mips_opts.micromips);
11192 if (mips_opts.insn32)
11193 {
11194 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11195 break;
11196 }
11197 jals = 1;
11198 goto jal;
11199 case M_JAL_1:
11200 op[1] = op[0];
11201 op[0] = RA;
11202 /* Fall through. */
11203 case M_JAL_2:
11204 jal:
11205 if (mips_pic == NO_PIC)
11206 {
11207 s = jals ? "jalrs" : "jalr";
11208 if (mips_opts.micromips
11209 && !mips_opts.insn32
11210 && op[0] == RA
11211 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11212 macro_build (NULL, s, "mj", op[1]);
11213 else
11214 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11215 }
11216 else
11217 {
11218 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11219 && mips_cprestore_offset >= 0);
11220
11221 if (op[1] != PIC_CALL_REG)
11222 as_warn (_("MIPS PIC call to register other than $25"));
11223
11224 s = ((mips_opts.micromips
11225 && !mips_opts.insn32
11226 && (!mips_opts.noreorder || cprestore))
11227 ? "jalrs" : "jalr");
11228 if (mips_opts.micromips
11229 && !mips_opts.insn32
11230 && op[0] == RA
11231 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11232 macro_build (NULL, s, "mj", op[1]);
11233 else
11234 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11235 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
11236 {
11237 if (mips_cprestore_offset < 0)
11238 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11239 else
11240 {
11241 if (!mips_frame_reg_valid)
11242 {
11243 as_warn (_("no .frame pseudo-op used in PIC code"));
11244 /* Quiet this warning. */
11245 mips_frame_reg_valid = 1;
11246 }
11247 if (!mips_cprestore_valid)
11248 {
11249 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11250 /* Quiet this warning. */
11251 mips_cprestore_valid = 1;
11252 }
11253 if (mips_opts.noreorder)
11254 macro_build (NULL, "nop", "");
11255 expr1.X_add_number = mips_cprestore_offset;
11256 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11257 mips_gp_register,
11258 mips_frame_reg,
11259 HAVE_64BIT_ADDRESSES);
11260 }
11261 }
11262 }
11263
11264 break;
11265
11266 case M_JALS_A:
11267 gas_assert (mips_opts.micromips);
11268 if (mips_opts.insn32)
11269 {
11270 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11271 break;
11272 }
11273 jals = 1;
11274 /* Fall through. */
11275 case M_JAL_A:
11276 if (mips_pic == NO_PIC)
11277 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
11278 else if (mips_pic == SVR4_PIC)
11279 {
11280 /* If this is a reference to an external symbol, and we are
11281 using a small GOT, we want
11282 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11283 nop
11284 jalr $ra,$25
11285 nop
11286 lw $gp,cprestore($sp)
11287 The cprestore value is set using the .cprestore
11288 pseudo-op. If we are using a big GOT, we want
11289 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11290 addu $25,$25,$gp
11291 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11292 nop
11293 jalr $ra,$25
11294 nop
11295 lw $gp,cprestore($sp)
11296 If the symbol is not external, we want
11297 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11298 nop
11299 addiu $25,$25,<sym> (BFD_RELOC_LO16)
11300 jalr $ra,$25
11301 nop
11302 lw $gp,cprestore($sp)
11303
11304 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11305 sequences above, minus nops, unless the symbol is local,
11306 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11307 GOT_DISP. */
11308 if (HAVE_NEWABI)
11309 {
11310 if (!mips_big_got)
11311 {
11312 relax_start (offset_expr.X_add_symbol);
11313 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11314 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11315 mips_gp_register);
11316 relax_switch ();
11317 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11318 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
11319 mips_gp_register);
11320 relax_end ();
11321 }
11322 else
11323 {
11324 relax_start (offset_expr.X_add_symbol);
11325 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11326 BFD_RELOC_MIPS_CALL_HI16);
11327 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11328 PIC_CALL_REG, mips_gp_register);
11329 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11330 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11331 PIC_CALL_REG);
11332 relax_switch ();
11333 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11334 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11335 mips_gp_register);
11336 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11337 PIC_CALL_REG, PIC_CALL_REG,
11338 BFD_RELOC_MIPS_GOT_OFST);
11339 relax_end ();
11340 }
11341
11342 macro_build_jalr (&offset_expr, 0);
11343 }
11344 else
11345 {
11346 relax_start (offset_expr.X_add_symbol);
11347 if (!mips_big_got)
11348 {
11349 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11350 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11351 mips_gp_register);
11352 load_delay_nop ();
11353 relax_switch ();
11354 }
11355 else
11356 {
11357 int gpdelay;
11358
11359 gpdelay = reg_needs_delay (mips_gp_register);
11360 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11361 BFD_RELOC_MIPS_CALL_HI16);
11362 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11363 PIC_CALL_REG, mips_gp_register);
11364 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11365 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11366 PIC_CALL_REG);
11367 load_delay_nop ();
11368 relax_switch ();
11369 if (gpdelay)
11370 macro_build (NULL, "nop", "");
11371 }
11372 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11373 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
11374 mips_gp_register);
11375 load_delay_nop ();
11376 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11377 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
11378 relax_end ();
11379 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
11380
11381 if (mips_cprestore_offset < 0)
11382 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11383 else
11384 {
11385 if (!mips_frame_reg_valid)
11386 {
11387 as_warn (_("no .frame pseudo-op used in PIC code"));
11388 /* Quiet this warning. */
11389 mips_frame_reg_valid = 1;
11390 }
11391 if (!mips_cprestore_valid)
11392 {
11393 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11394 /* Quiet this warning. */
11395 mips_cprestore_valid = 1;
11396 }
11397 if (mips_opts.noreorder)
11398 macro_build (NULL, "nop", "");
11399 expr1.X_add_number = mips_cprestore_offset;
11400 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11401 mips_gp_register,
11402 mips_frame_reg,
11403 HAVE_64BIT_ADDRESSES);
11404 }
11405 }
11406 }
11407 else if (mips_pic == VXWORKS_PIC)
11408 as_bad (_("non-PIC jump used in PIC library"));
11409 else
11410 abort ();
11411
11412 break;
11413
11414 case M_LBUE_AB:
11415 s = "lbue";
11416 fmt = "t,+j(b)";
11417 offbits = 9;
11418 goto ld_st;
11419 case M_LHUE_AB:
11420 s = "lhue";
11421 fmt = "t,+j(b)";
11422 offbits = 9;
11423 goto ld_st;
11424 case M_LBE_AB:
11425 s = "lbe";
11426 fmt = "t,+j(b)";
11427 offbits = 9;
11428 goto ld_st;
11429 case M_LHE_AB:
11430 s = "lhe";
11431 fmt = "t,+j(b)";
11432 offbits = 9;
11433 goto ld_st;
11434 case M_LLE_AB:
11435 s = "lle";
11436 fmt = "t,+j(b)";
11437 offbits = 9;
11438 goto ld_st;
11439 case M_LWE_AB:
11440 s = "lwe";
11441 fmt = "t,+j(b)";
11442 offbits = 9;
11443 goto ld_st;
11444 case M_LWLE_AB:
11445 s = "lwle";
11446 fmt = "t,+j(b)";
11447 offbits = 9;
11448 goto ld_st;
11449 case M_LWRE_AB:
11450 s = "lwre";
11451 fmt = "t,+j(b)";
11452 offbits = 9;
11453 goto ld_st;
11454 case M_SBE_AB:
11455 s = "sbe";
11456 fmt = "t,+j(b)";
11457 offbits = 9;
11458 goto ld_st;
11459 case M_SCE_AB:
11460 s = "sce";
11461 fmt = "t,+j(b)";
11462 offbits = 9;
11463 goto ld_st;
11464 case M_SHE_AB:
11465 s = "she";
11466 fmt = "t,+j(b)";
11467 offbits = 9;
11468 goto ld_st;
11469 case M_SWE_AB:
11470 s = "swe";
11471 fmt = "t,+j(b)";
11472 offbits = 9;
11473 goto ld_st;
11474 case M_SWLE_AB:
11475 s = "swle";
11476 fmt = "t,+j(b)";
11477 offbits = 9;
11478 goto ld_st;
11479 case M_SWRE_AB:
11480 s = "swre";
11481 fmt = "t,+j(b)";
11482 offbits = 9;
11483 goto ld_st;
11484 case M_ACLR_AB:
11485 s = "aclr";
11486 fmt = "\\,~(b)";
11487 offbits = 12;
11488 goto ld_st;
11489 case M_ASET_AB:
11490 s = "aset";
11491 fmt = "\\,~(b)";
11492 offbits = 12;
11493 goto ld_st;
11494 case M_LB_AB:
11495 s = "lb";
11496 fmt = "t,o(b)";
11497 goto ld;
11498 case M_LBU_AB:
11499 s = "lbu";
11500 fmt = "t,o(b)";
11501 goto ld;
11502 case M_LH_AB:
11503 s = "lh";
11504 fmt = "t,o(b)";
11505 goto ld;
11506 case M_LHU_AB:
11507 s = "lhu";
11508 fmt = "t,o(b)";
11509 goto ld;
11510 case M_LW_AB:
11511 s = "lw";
11512 fmt = "t,o(b)";
11513 goto ld;
11514 case M_LWC0_AB:
11515 gas_assert (!mips_opts.micromips);
11516 s = "lwc0";
11517 fmt = "E,o(b)";
11518 /* Itbl support may require additional care here. */
11519 coproc = 1;
11520 goto ld_st;
11521 case M_LWC1_AB:
11522 s = "lwc1";
11523 fmt = "T,o(b)";
11524 /* Itbl support may require additional care here. */
11525 coproc = 1;
11526 goto ld_st;
11527 case M_LWC2_AB:
11528 s = "lwc2";
11529 fmt = COP12_FMT;
11530 offbits = (mips_opts.micromips ? 12
11531 : ISA_IS_R6 (mips_opts.isa) ? 11
11532 : 16);
11533 /* Itbl support may require additional care here. */
11534 coproc = 1;
11535 goto ld_st;
11536 case M_LWC3_AB:
11537 gas_assert (!mips_opts.micromips);
11538 s = "lwc3";
11539 fmt = "E,o(b)";
11540 /* Itbl support may require additional care here. */
11541 coproc = 1;
11542 goto ld_st;
11543 case M_LWL_AB:
11544 s = "lwl";
11545 fmt = MEM12_FMT;
11546 offbits = (mips_opts.micromips ? 12 : 16);
11547 goto ld_st;
11548 case M_LWR_AB:
11549 s = "lwr";
11550 fmt = MEM12_FMT;
11551 offbits = (mips_opts.micromips ? 12 : 16);
11552 goto ld_st;
11553 case M_LDC1_AB:
11554 s = "ldc1";
11555 fmt = "T,o(b)";
11556 /* Itbl support may require additional care here. */
11557 coproc = 1;
11558 goto ld_st;
11559 case M_LDC2_AB:
11560 s = "ldc2";
11561 fmt = COP12_FMT;
11562 offbits = (mips_opts.micromips ? 12
11563 : ISA_IS_R6 (mips_opts.isa) ? 11
11564 : 16);
11565 /* Itbl support may require additional care here. */
11566 coproc = 1;
11567 goto ld_st;
11568 case M_LQC2_AB:
11569 s = "lqc2";
11570 fmt = "+7,o(b)";
11571 /* Itbl support may require additional care here. */
11572 coproc = 1;
11573 goto ld_st;
11574 case M_LDC3_AB:
11575 s = "ldc3";
11576 fmt = "E,o(b)";
11577 /* Itbl support may require additional care here. */
11578 coproc = 1;
11579 goto ld_st;
11580 case M_LDL_AB:
11581 s = "ldl";
11582 fmt = MEM12_FMT;
11583 offbits = (mips_opts.micromips ? 12 : 16);
11584 goto ld_st;
11585 case M_LDR_AB:
11586 s = "ldr";
11587 fmt = MEM12_FMT;
11588 offbits = (mips_opts.micromips ? 12 : 16);
11589 goto ld_st;
11590 case M_LL_AB:
11591 s = "ll";
11592 fmt = LL_SC_FMT;
11593 offbits = (mips_opts.micromips ? 12
11594 : ISA_IS_R6 (mips_opts.isa) ? 9
11595 : 16);
11596 goto ld;
11597 case M_LLD_AB:
11598 s = "lld";
11599 fmt = LL_SC_FMT;
11600 offbits = (mips_opts.micromips ? 12
11601 : ISA_IS_R6 (mips_opts.isa) ? 9
11602 : 16);
11603 goto ld;
11604 case M_LWU_AB:
11605 s = "lwu";
11606 fmt = MEM12_FMT;
11607 offbits = (mips_opts.micromips ? 12 : 16);
11608 goto ld;
11609 case M_LWP_AB:
11610 gas_assert (mips_opts.micromips);
11611 s = "lwp";
11612 fmt = "t,~(b)";
11613 offbits = 12;
11614 lp = 1;
11615 goto ld;
11616 case M_LDP_AB:
11617 gas_assert (mips_opts.micromips);
11618 s = "ldp";
11619 fmt = "t,~(b)";
11620 offbits = 12;
11621 lp = 1;
11622 goto ld;
11623 case M_LWM_AB:
11624 gas_assert (mips_opts.micromips);
11625 s = "lwm";
11626 fmt = "n,~(b)";
11627 offbits = 12;
11628 goto ld_st;
11629 case M_LDM_AB:
11630 gas_assert (mips_opts.micromips);
11631 s = "ldm";
11632 fmt = "n,~(b)";
11633 offbits = 12;
11634 goto ld_st;
11635
11636 ld:
11637 /* We don't want to use $0 as tempreg. */
11638 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
11639 goto ld_st;
11640 else
11641 tempreg = op[0] + lp;
11642 goto ld_noat;
11643
11644 case M_SB_AB:
11645 s = "sb";
11646 fmt = "t,o(b)";
11647 goto ld_st;
11648 case M_SH_AB:
11649 s = "sh";
11650 fmt = "t,o(b)";
11651 goto ld_st;
11652 case M_SW_AB:
11653 s = "sw";
11654 fmt = "t,o(b)";
11655 goto ld_st;
11656 case M_SWC0_AB:
11657 gas_assert (!mips_opts.micromips);
11658 s = "swc0";
11659 fmt = "E,o(b)";
11660 /* Itbl support may require additional care here. */
11661 coproc = 1;
11662 goto ld_st;
11663 case M_SWC1_AB:
11664 s = "swc1";
11665 fmt = "T,o(b)";
11666 /* Itbl support may require additional care here. */
11667 coproc = 1;
11668 goto ld_st;
11669 case M_SWC2_AB:
11670 s = "swc2";
11671 fmt = COP12_FMT;
11672 offbits = (mips_opts.micromips ? 12
11673 : ISA_IS_R6 (mips_opts.isa) ? 11
11674 : 16);
11675 /* Itbl support may require additional care here. */
11676 coproc = 1;
11677 goto ld_st;
11678 case M_SWC3_AB:
11679 gas_assert (!mips_opts.micromips);
11680 s = "swc3";
11681 fmt = "E,o(b)";
11682 /* Itbl support may require additional care here. */
11683 coproc = 1;
11684 goto ld_st;
11685 case M_SWL_AB:
11686 s = "swl";
11687 fmt = MEM12_FMT;
11688 offbits = (mips_opts.micromips ? 12 : 16);
11689 goto ld_st;
11690 case M_SWR_AB:
11691 s = "swr";
11692 fmt = MEM12_FMT;
11693 offbits = (mips_opts.micromips ? 12 : 16);
11694 goto ld_st;
11695 case M_SC_AB:
11696 s = "sc";
11697 fmt = LL_SC_FMT;
11698 offbits = (mips_opts.micromips ? 12
11699 : ISA_IS_R6 (mips_opts.isa) ? 9
11700 : 16);
11701 goto ld_st;
11702 case M_SCD_AB:
11703 s = "scd";
11704 fmt = LL_SC_FMT;
11705 offbits = (mips_opts.micromips ? 12
11706 : ISA_IS_R6 (mips_opts.isa) ? 9
11707 : 16);
11708 goto ld_st;
11709 case M_CACHE_AB:
11710 s = "cache";
11711 fmt = (mips_opts.micromips ? "k,~(b)"
11712 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11713 : "k,o(b)");
11714 offbits = (mips_opts.micromips ? 12
11715 : ISA_IS_R6 (mips_opts.isa) ? 9
11716 : 16);
11717 goto ld_st;
11718 case M_CACHEE_AB:
11719 s = "cachee";
11720 fmt = "k,+j(b)";
11721 offbits = 9;
11722 goto ld_st;
11723 case M_PREF_AB:
11724 s = "pref";
11725 fmt = (mips_opts.micromips ? "k,~(b)"
11726 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11727 : "k,o(b)");
11728 offbits = (mips_opts.micromips ? 12
11729 : ISA_IS_R6 (mips_opts.isa) ? 9
11730 : 16);
11731 goto ld_st;
11732 case M_PREFE_AB:
11733 s = "prefe";
11734 fmt = "k,+j(b)";
11735 offbits = 9;
11736 goto ld_st;
11737 case M_SDC1_AB:
11738 s = "sdc1";
11739 fmt = "T,o(b)";
11740 coproc = 1;
11741 /* Itbl support may require additional care here. */
11742 goto ld_st;
11743 case M_SDC2_AB:
11744 s = "sdc2";
11745 fmt = COP12_FMT;
11746 offbits = (mips_opts.micromips ? 12
11747 : ISA_IS_R6 (mips_opts.isa) ? 11
11748 : 16);
11749 /* Itbl support may require additional care here. */
11750 coproc = 1;
11751 goto ld_st;
11752 case M_SQC2_AB:
11753 s = "sqc2";
11754 fmt = "+7,o(b)";
11755 /* Itbl support may require additional care here. */
11756 coproc = 1;
11757 goto ld_st;
11758 case M_SDC3_AB:
11759 gas_assert (!mips_opts.micromips);
11760 s = "sdc3";
11761 fmt = "E,o(b)";
11762 /* Itbl support may require additional care here. */
11763 coproc = 1;
11764 goto ld_st;
11765 case M_SDL_AB:
11766 s = "sdl";
11767 fmt = MEM12_FMT;
11768 offbits = (mips_opts.micromips ? 12 : 16);
11769 goto ld_st;
11770 case M_SDR_AB:
11771 s = "sdr";
11772 fmt = MEM12_FMT;
11773 offbits = (mips_opts.micromips ? 12 : 16);
11774 goto ld_st;
11775 case M_SWP_AB:
11776 gas_assert (mips_opts.micromips);
11777 s = "swp";
11778 fmt = "t,~(b)";
11779 offbits = 12;
11780 goto ld_st;
11781 case M_SDP_AB:
11782 gas_assert (mips_opts.micromips);
11783 s = "sdp";
11784 fmt = "t,~(b)";
11785 offbits = 12;
11786 goto ld_st;
11787 case M_SWM_AB:
11788 gas_assert (mips_opts.micromips);
11789 s = "swm";
11790 fmt = "n,~(b)";
11791 offbits = 12;
11792 goto ld_st;
11793 case M_SDM_AB:
11794 gas_assert (mips_opts.micromips);
11795 s = "sdm";
11796 fmt = "n,~(b)";
11797 offbits = 12;
11798
11799 ld_st:
11800 tempreg = AT;
11801 ld_noat:
11802 breg = op[2];
11803 if (small_offset_p (0, align, 16))
11804 {
11805 /* The first case exists for M_LD_AB and M_SD_AB, which are
11806 macros for o32 but which should act like normal instructions
11807 otherwise. */
11808 if (offbits == 16)
11809 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
11810 offset_reloc[1], offset_reloc[2], breg);
11811 else if (small_offset_p (0, align, offbits))
11812 {
11813 if (offbits == 0)
11814 macro_build (NULL, s, fmt, op[0], breg);
11815 else
11816 macro_build (NULL, s, fmt, op[0],
11817 (int) offset_expr.X_add_number, breg);
11818 }
11819 else
11820 {
11821 if (tempreg == AT)
11822 used_at = 1;
11823 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11824 tempreg, breg, -1, offset_reloc[0],
11825 offset_reloc[1], offset_reloc[2]);
11826 if (offbits == 0)
11827 macro_build (NULL, s, fmt, op[0], tempreg);
11828 else
11829 macro_build (NULL, s, fmt, op[0], 0, tempreg);
11830 }
11831 break;
11832 }
11833
11834 if (tempreg == AT)
11835 used_at = 1;
11836
11837 if (offset_expr.X_op != O_constant
11838 && offset_expr.X_op != O_symbol)
11839 {
11840 as_bad (_("expression too complex"));
11841 offset_expr.X_op = O_constant;
11842 }
11843
11844 if (HAVE_32BIT_ADDRESSES
11845 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
11846 {
11847 char value [32];
11848
11849 sprintf_vma (value, offset_expr.X_add_number);
11850 as_bad (_("number (0x%s) larger than 32 bits"), value);
11851 }
11852
11853 /* A constant expression in PIC code can be handled just as it
11854 is in non PIC code. */
11855 if (offset_expr.X_op == O_constant)
11856 {
11857 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
11858 offbits == 0 ? 16 : offbits);
11859 offset_expr.X_add_number -= expr1.X_add_number;
11860
11861 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
11862 if (breg != 0)
11863 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11864 tempreg, tempreg, breg);
11865 if (offbits == 0)
11866 {
11867 if (offset_expr.X_add_number != 0)
11868 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
11869 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
11870 macro_build (NULL, s, fmt, op[0], tempreg);
11871 }
11872 else if (offbits == 16)
11873 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
11874 else
11875 macro_build (NULL, s, fmt, op[0],
11876 (int) offset_expr.X_add_number, tempreg);
11877 }
11878 else if (offbits != 16)
11879 {
11880 /* The offset field is too narrow to be used for a low-part
11881 relocation, so load the whole address into the auxiliary
11882 register. */
11883 load_address (tempreg, &offset_expr, &used_at);
11884 if (breg != 0)
11885 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11886 tempreg, tempreg, breg);
11887 if (offbits == 0)
11888 macro_build (NULL, s, fmt, op[0], tempreg);
11889 else
11890 macro_build (NULL, s, fmt, op[0], 0, tempreg);
11891 }
11892 else if (mips_pic == NO_PIC)
11893 {
11894 /* If this is a reference to a GP relative symbol, and there
11895 is no base register, we want
11896 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
11897 Otherwise, if there is no base register, we want
11898 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11899 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11900 If we have a constant, we need two instructions anyhow,
11901 so we always use the latter form.
11902
11903 If we have a base register, and this is a reference to a
11904 GP relative symbol, we want
11905 addu $tempreg,$breg,$gp
11906 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
11907 Otherwise we want
11908 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11909 addu $tempreg,$tempreg,$breg
11910 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11911 With a constant we always use the latter case.
11912
11913 With 64bit address space and no base register and $at usable,
11914 we want
11915 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11916 lui $at,<sym> (BFD_RELOC_HI16_S)
11917 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11918 dsll32 $tempreg,0
11919 daddu $tempreg,$at
11920 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11921 If we have a base register, we want
11922 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11923 lui $at,<sym> (BFD_RELOC_HI16_S)
11924 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11925 daddu $at,$breg
11926 dsll32 $tempreg,0
11927 daddu $tempreg,$at
11928 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11929
11930 Without $at we can't generate the optimal path for superscalar
11931 processors here since this would require two temporary registers.
11932 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11933 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11934 dsll $tempreg,16
11935 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11936 dsll $tempreg,16
11937 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11938 If we have a base register, we want
11939 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11940 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11941 dsll $tempreg,16
11942 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11943 dsll $tempreg,16
11944 daddu $tempreg,$tempreg,$breg
11945 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11946
11947 For GP relative symbols in 64bit address space we can use
11948 the same sequence as in 32bit address space. */
11949 if (HAVE_64BIT_SYMBOLS)
11950 {
11951 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11952 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11953 {
11954 relax_start (offset_expr.X_add_symbol);
11955 if (breg == 0)
11956 {
11957 macro_build (&offset_expr, s, fmt, op[0],
11958 BFD_RELOC_GPREL16, mips_gp_register);
11959 }
11960 else
11961 {
11962 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11963 tempreg, breg, mips_gp_register);
11964 macro_build (&offset_expr, s, fmt, op[0],
11965 BFD_RELOC_GPREL16, tempreg);
11966 }
11967 relax_switch ();
11968 }
11969
11970 if (used_at == 0 && mips_opts.at)
11971 {
11972 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
11973 BFD_RELOC_MIPS_HIGHEST);
11974 macro_build (&offset_expr, "lui", LUI_FMT, AT,
11975 BFD_RELOC_HI16_S);
11976 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11977 tempreg, BFD_RELOC_MIPS_HIGHER);
11978 if (breg != 0)
11979 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
11980 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
11981 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
11982 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
11983 tempreg);
11984 used_at = 1;
11985 }
11986 else
11987 {
11988 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
11989 BFD_RELOC_MIPS_HIGHEST);
11990 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11991 tempreg, BFD_RELOC_MIPS_HIGHER);
11992 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11993 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11994 tempreg, BFD_RELOC_HI16_S);
11995 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11996 if (breg != 0)
11997 macro_build (NULL, "daddu", "d,v,t",
11998 tempreg, tempreg, breg);
11999 macro_build (&offset_expr, s, fmt, op[0],
12000 BFD_RELOC_LO16, tempreg);
12001 }
12002
12003 if (mips_relax.sequence)
12004 relax_end ();
12005 break;
12006 }
12007
12008 if (breg == 0)
12009 {
12010 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12011 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12012 {
12013 relax_start (offset_expr.X_add_symbol);
12014 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
12015 mips_gp_register);
12016 relax_switch ();
12017 }
12018 macro_build_lui (&offset_expr, tempreg);
12019 macro_build (&offset_expr, s, fmt, op[0],
12020 BFD_RELOC_LO16, tempreg);
12021 if (mips_relax.sequence)
12022 relax_end ();
12023 }
12024 else
12025 {
12026 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12027 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12028 {
12029 relax_start (offset_expr.X_add_symbol);
12030 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12031 tempreg, breg, mips_gp_register);
12032 macro_build (&offset_expr, s, fmt, op[0],
12033 BFD_RELOC_GPREL16, tempreg);
12034 relax_switch ();
12035 }
12036 macro_build_lui (&offset_expr, tempreg);
12037 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12038 tempreg, tempreg, breg);
12039 macro_build (&offset_expr, s, fmt, op[0],
12040 BFD_RELOC_LO16, tempreg);
12041 if (mips_relax.sequence)
12042 relax_end ();
12043 }
12044 }
12045 else if (!mips_big_got)
12046 {
12047 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
12048
12049 /* If this is a reference to an external symbol, we want
12050 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12051 nop
12052 <op> op[0],0($tempreg)
12053 Otherwise we want
12054 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12055 nop
12056 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12057 <op> op[0],0($tempreg)
12058
12059 For NewABI, we want
12060 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12061 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
12062
12063 If there is a base register, we add it to $tempreg before
12064 the <op>. If there is a constant, we stick it in the
12065 <op> instruction. We don't handle constants larger than
12066 16 bits, because we have no way to load the upper 16 bits
12067 (actually, we could handle them for the subset of cases
12068 in which we are not using $at). */
12069 gas_assert (offset_expr.X_op == O_symbol);
12070 if (HAVE_NEWABI)
12071 {
12072 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12073 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12074 if (breg != 0)
12075 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12076 tempreg, tempreg, breg);
12077 macro_build (&offset_expr, s, fmt, op[0],
12078 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12079 break;
12080 }
12081 expr1.X_add_number = offset_expr.X_add_number;
12082 offset_expr.X_add_number = 0;
12083 if (expr1.X_add_number < -0x8000
12084 || expr1.X_add_number >= 0x8000)
12085 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12086 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12087 lw_reloc_type, mips_gp_register);
12088 load_delay_nop ();
12089 relax_start (offset_expr.X_add_symbol);
12090 relax_switch ();
12091 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12092 tempreg, BFD_RELOC_LO16);
12093 relax_end ();
12094 if (breg != 0)
12095 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12096 tempreg, tempreg, breg);
12097 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12098 }
12099 else if (mips_big_got && !HAVE_NEWABI)
12100 {
12101 int gpdelay;
12102
12103 /* If this is a reference to an external symbol, we want
12104 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12105 addu $tempreg,$tempreg,$gp
12106 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12107 <op> op[0],0($tempreg)
12108 Otherwise we want
12109 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12110 nop
12111 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12112 <op> op[0],0($tempreg)
12113 If there is a base register, we add it to $tempreg before
12114 the <op>. If there is a constant, we stick it in the
12115 <op> instruction. We don't handle constants larger than
12116 16 bits, because we have no way to load the upper 16 bits
12117 (actually, we could handle them for the subset of cases
12118 in which we are not using $at). */
12119 gas_assert (offset_expr.X_op == O_symbol);
12120 expr1.X_add_number = offset_expr.X_add_number;
12121 offset_expr.X_add_number = 0;
12122 if (expr1.X_add_number < -0x8000
12123 || expr1.X_add_number >= 0x8000)
12124 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12125 gpdelay = reg_needs_delay (mips_gp_register);
12126 relax_start (offset_expr.X_add_symbol);
12127 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12128 BFD_RELOC_MIPS_GOT_HI16);
12129 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12130 mips_gp_register);
12131 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12132 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12133 relax_switch ();
12134 if (gpdelay)
12135 macro_build (NULL, "nop", "");
12136 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12137 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12138 load_delay_nop ();
12139 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12140 tempreg, BFD_RELOC_LO16);
12141 relax_end ();
12142
12143 if (breg != 0)
12144 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12145 tempreg, tempreg, breg);
12146 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12147 }
12148 else if (mips_big_got && HAVE_NEWABI)
12149 {
12150 /* If this is a reference to an external symbol, we want
12151 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12152 add $tempreg,$tempreg,$gp
12153 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12154 <op> op[0],<ofst>($tempreg)
12155 Otherwise, for local symbols, we want:
12156 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12157 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
12158 gas_assert (offset_expr.X_op == O_symbol);
12159 expr1.X_add_number = offset_expr.X_add_number;
12160 offset_expr.X_add_number = 0;
12161 if (expr1.X_add_number < -0x8000
12162 || expr1.X_add_number >= 0x8000)
12163 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12164 relax_start (offset_expr.X_add_symbol);
12165 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12166 BFD_RELOC_MIPS_GOT_HI16);
12167 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12168 mips_gp_register);
12169 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12170 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12171 if (breg != 0)
12172 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12173 tempreg, tempreg, breg);
12174 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12175
12176 relax_switch ();
12177 offset_expr.X_add_number = expr1.X_add_number;
12178 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12179 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12180 if (breg != 0)
12181 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12182 tempreg, tempreg, breg);
12183 macro_build (&offset_expr, s, fmt, op[0],
12184 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12185 relax_end ();
12186 }
12187 else
12188 abort ();
12189
12190 break;
12191
12192 case M_JRADDIUSP:
12193 gas_assert (mips_opts.micromips);
12194 gas_assert (mips_opts.insn32);
12195 start_noreorder ();
12196 macro_build (NULL, "jr", "s", RA);
12197 expr1.X_add_number = op[0] << 2;
12198 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12199 end_noreorder ();
12200 break;
12201
12202 case M_JRC:
12203 gas_assert (mips_opts.micromips);
12204 gas_assert (mips_opts.insn32);
12205 macro_build (NULL, "jr", "s", op[0]);
12206 if (mips_opts.noreorder)
12207 macro_build (NULL, "nop", "");
12208 break;
12209
12210 case M_LI:
12211 case M_LI_S:
12212 load_register (op[0], &imm_expr, 0);
12213 break;
12214
12215 case M_DLI:
12216 load_register (op[0], &imm_expr, 1);
12217 break;
12218
12219 case M_LI_SS:
12220 if (imm_expr.X_op == O_constant)
12221 {
12222 used_at = 1;
12223 load_register (AT, &imm_expr, 0);
12224 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12225 break;
12226 }
12227 else
12228 {
12229 gas_assert (imm_expr.X_op == O_absent
12230 && offset_expr.X_op == O_symbol
12231 && strcmp (segment_name (S_GET_SEGMENT
12232 (offset_expr.X_add_symbol)),
12233 ".lit4") == 0
12234 && offset_expr.X_add_number == 0);
12235 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
12236 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
12237 break;
12238 }
12239
12240 case M_LI_D:
12241 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12242 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12243 order 32 bits of the value and the low order 32 bits are either
12244 zero or in OFFSET_EXPR. */
12245 if (imm_expr.X_op == O_constant)
12246 {
12247 if (GPR_SIZE == 64)
12248 load_register (op[0], &imm_expr, 1);
12249 else
12250 {
12251 int hreg, lreg;
12252
12253 if (target_big_endian)
12254 {
12255 hreg = op[0];
12256 lreg = op[0] + 1;
12257 }
12258 else
12259 {
12260 hreg = op[0] + 1;
12261 lreg = op[0];
12262 }
12263
12264 if (hreg <= 31)
12265 load_register (hreg, &imm_expr, 0);
12266 if (lreg <= 31)
12267 {
12268 if (offset_expr.X_op == O_absent)
12269 move_register (lreg, 0);
12270 else
12271 {
12272 gas_assert (offset_expr.X_op == O_constant);
12273 load_register (lreg, &offset_expr, 0);
12274 }
12275 }
12276 }
12277 break;
12278 }
12279 gas_assert (imm_expr.X_op == O_absent);
12280
12281 /* We know that sym is in the .rdata section. First we get the
12282 upper 16 bits of the address. */
12283 if (mips_pic == NO_PIC)
12284 {
12285 macro_build_lui (&offset_expr, AT);
12286 used_at = 1;
12287 }
12288 else
12289 {
12290 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12291 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12292 used_at = 1;
12293 }
12294
12295 /* Now we load the register(s). */
12296 if (GPR_SIZE == 64)
12297 {
12298 used_at = 1;
12299 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12300 BFD_RELOC_LO16, AT);
12301 }
12302 else
12303 {
12304 used_at = 1;
12305 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12306 BFD_RELOC_LO16, AT);
12307 if (op[0] != RA)
12308 {
12309 /* FIXME: How in the world do we deal with the possible
12310 overflow here? */
12311 offset_expr.X_add_number += 4;
12312 macro_build (&offset_expr, "lw", "t,o(b)",
12313 op[0] + 1, BFD_RELOC_LO16, AT);
12314 }
12315 }
12316 break;
12317
12318 case M_LI_DD:
12319 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12320 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12321 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12322 the value and the low order 32 bits are either zero or in
12323 OFFSET_EXPR. */
12324 if (imm_expr.X_op == O_constant)
12325 {
12326 used_at = 1;
12327 load_register (AT, &imm_expr, FPR_SIZE == 64);
12328 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12329 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
12330 else
12331 {
12332 if (ISA_HAS_MXHC1 (mips_opts.isa))
12333 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12334 else if (FPR_SIZE != 32)
12335 as_bad (_("Unable to generate `%s' compliant code "
12336 "without mthc1"),
12337 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12338 else
12339 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
12340 if (offset_expr.X_op == O_absent)
12341 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
12342 else
12343 {
12344 gas_assert (offset_expr.X_op == O_constant);
12345 load_register (AT, &offset_expr, 0);
12346 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12347 }
12348 }
12349 break;
12350 }
12351
12352 gas_assert (imm_expr.X_op == O_absent
12353 && offset_expr.X_op == O_symbol
12354 && offset_expr.X_add_number == 0);
12355 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12356 if (strcmp (s, ".lit8") == 0)
12357 {
12358 op[2] = mips_gp_register;
12359 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12360 offset_reloc[1] = BFD_RELOC_UNUSED;
12361 offset_reloc[2] = BFD_RELOC_UNUSED;
12362 }
12363 else
12364 {
12365 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
12366 used_at = 1;
12367 if (mips_pic != NO_PIC)
12368 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12369 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12370 else
12371 {
12372 /* FIXME: This won't work for a 64 bit address. */
12373 macro_build_lui (&offset_expr, AT);
12374 }
12375
12376 op[2] = AT;
12377 offset_reloc[0] = BFD_RELOC_LO16;
12378 offset_reloc[1] = BFD_RELOC_UNUSED;
12379 offset_reloc[2] = BFD_RELOC_UNUSED;
12380 }
12381 align = 8;
12382 /* Fall through */
12383
12384 case M_L_DAB:
12385 /*
12386 * The MIPS assembler seems to check for X_add_number not
12387 * being double aligned and generating:
12388 * lui at,%hi(foo+1)
12389 * addu at,at,v1
12390 * addiu at,at,%lo(foo+1)
12391 * lwc1 f2,0(at)
12392 * lwc1 f3,4(at)
12393 * But, the resulting address is the same after relocation so why
12394 * generate the extra instruction?
12395 */
12396 /* Itbl support may require additional care here. */
12397 coproc = 1;
12398 fmt = "T,o(b)";
12399 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12400 {
12401 s = "ldc1";
12402 goto ld_st;
12403 }
12404 s = "lwc1";
12405 goto ldd_std;
12406
12407 case M_S_DAB:
12408 gas_assert (!mips_opts.micromips);
12409 /* Itbl support may require additional care here. */
12410 coproc = 1;
12411 fmt = "T,o(b)";
12412 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12413 {
12414 s = "sdc1";
12415 goto ld_st;
12416 }
12417 s = "swc1";
12418 goto ldd_std;
12419
12420 case M_LQ_AB:
12421 fmt = "t,o(b)";
12422 s = "lq";
12423 goto ld;
12424
12425 case M_SQ_AB:
12426 fmt = "t,o(b)";
12427 s = "sq";
12428 goto ld_st;
12429
12430 case M_LD_AB:
12431 fmt = "t,o(b)";
12432 if (GPR_SIZE == 64)
12433 {
12434 s = "ld";
12435 goto ld;
12436 }
12437 s = "lw";
12438 goto ldd_std;
12439
12440 case M_SD_AB:
12441 fmt = "t,o(b)";
12442 if (GPR_SIZE == 64)
12443 {
12444 s = "sd";
12445 goto ld_st;
12446 }
12447 s = "sw";
12448
12449 ldd_std:
12450 /* Even on a big endian machine $fn comes before $fn+1. We have
12451 to adjust when loading from memory. We set coproc if we must
12452 load $fn+1 first. */
12453 /* Itbl support may require additional care here. */
12454 if (!target_big_endian)
12455 coproc = 0;
12456
12457 breg = op[2];
12458 if (small_offset_p (0, align, 16))
12459 {
12460 ep = &offset_expr;
12461 if (!small_offset_p (4, align, 16))
12462 {
12463 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12464 -1, offset_reloc[0], offset_reloc[1],
12465 offset_reloc[2]);
12466 expr1.X_add_number = 0;
12467 ep = &expr1;
12468 breg = AT;
12469 used_at = 1;
12470 offset_reloc[0] = BFD_RELOC_LO16;
12471 offset_reloc[1] = BFD_RELOC_UNUSED;
12472 offset_reloc[2] = BFD_RELOC_UNUSED;
12473 }
12474 if (strcmp (s, "lw") == 0 && op[0] == breg)
12475 {
12476 ep->X_add_number += 4;
12477 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
12478 offset_reloc[1], offset_reloc[2], breg);
12479 ep->X_add_number -= 4;
12480 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
12481 offset_reloc[1], offset_reloc[2], breg);
12482 }
12483 else
12484 {
12485 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
12486 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12487 breg);
12488 ep->X_add_number += 4;
12489 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
12490 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12491 breg);
12492 }
12493 break;
12494 }
12495
12496 if (offset_expr.X_op != O_symbol
12497 && offset_expr.X_op != O_constant)
12498 {
12499 as_bad (_("expression too complex"));
12500 offset_expr.X_op = O_constant;
12501 }
12502
12503 if (HAVE_32BIT_ADDRESSES
12504 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12505 {
12506 char value [32];
12507
12508 sprintf_vma (value, offset_expr.X_add_number);
12509 as_bad (_("number (0x%s) larger than 32 bits"), value);
12510 }
12511
12512 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
12513 {
12514 /* If this is a reference to a GP relative symbol, we want
12515 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12516 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
12517 If we have a base register, we use this
12518 addu $at,$breg,$gp
12519 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12520 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
12521 If this is not a GP relative symbol, we want
12522 lui $at,<sym> (BFD_RELOC_HI16_S)
12523 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12524 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12525 If there is a base register, we add it to $at after the
12526 lui instruction. If there is a constant, we always use
12527 the last case. */
12528 if (offset_expr.X_op == O_symbol
12529 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12530 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12531 {
12532 relax_start (offset_expr.X_add_symbol);
12533 if (breg == 0)
12534 {
12535 tempreg = mips_gp_register;
12536 }
12537 else
12538 {
12539 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12540 AT, breg, mips_gp_register);
12541 tempreg = AT;
12542 used_at = 1;
12543 }
12544
12545 /* Itbl support may require additional care here. */
12546 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12547 BFD_RELOC_GPREL16, tempreg);
12548 offset_expr.X_add_number += 4;
12549
12550 /* Set mips_optimize to 2 to avoid inserting an
12551 undesired nop. */
12552 hold_mips_optimize = mips_optimize;
12553 mips_optimize = 2;
12554 /* Itbl support may require additional care here. */
12555 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12556 BFD_RELOC_GPREL16, tempreg);
12557 mips_optimize = hold_mips_optimize;
12558
12559 relax_switch ();
12560
12561 offset_expr.X_add_number -= 4;
12562 }
12563 used_at = 1;
12564 if (offset_high_part (offset_expr.X_add_number, 16)
12565 != offset_high_part (offset_expr.X_add_number + 4, 16))
12566 {
12567 load_address (AT, &offset_expr, &used_at);
12568 offset_expr.X_op = O_constant;
12569 offset_expr.X_add_number = 0;
12570 }
12571 else
12572 macro_build_lui (&offset_expr, AT);
12573 if (breg != 0)
12574 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12575 /* Itbl support may require additional care here. */
12576 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12577 BFD_RELOC_LO16, AT);
12578 /* FIXME: How do we handle overflow here? */
12579 offset_expr.X_add_number += 4;
12580 /* Itbl support may require additional care here. */
12581 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12582 BFD_RELOC_LO16, AT);
12583 if (mips_relax.sequence)
12584 relax_end ();
12585 }
12586 else if (!mips_big_got)
12587 {
12588 /* If this is a reference to an external symbol, we want
12589 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12590 nop
12591 <op> op[0],0($at)
12592 <op> op[0]+1,4($at)
12593 Otherwise we want
12594 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12595 nop
12596 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12597 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12598 If there is a base register we add it to $at before the
12599 lwc1 instructions. If there is a constant we include it
12600 in the lwc1 instructions. */
12601 used_at = 1;
12602 expr1.X_add_number = offset_expr.X_add_number;
12603 if (expr1.X_add_number < -0x8000
12604 || expr1.X_add_number >= 0x8000 - 4)
12605 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12606 load_got_offset (AT, &offset_expr);
12607 load_delay_nop ();
12608 if (breg != 0)
12609 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12610
12611 /* Set mips_optimize to 2 to avoid inserting an undesired
12612 nop. */
12613 hold_mips_optimize = mips_optimize;
12614 mips_optimize = 2;
12615
12616 /* Itbl support may require additional care here. */
12617 relax_start (offset_expr.X_add_symbol);
12618 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12619 BFD_RELOC_LO16, AT);
12620 expr1.X_add_number += 4;
12621 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12622 BFD_RELOC_LO16, AT);
12623 relax_switch ();
12624 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12625 BFD_RELOC_LO16, AT);
12626 offset_expr.X_add_number += 4;
12627 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12628 BFD_RELOC_LO16, AT);
12629 relax_end ();
12630
12631 mips_optimize = hold_mips_optimize;
12632 }
12633 else if (mips_big_got)
12634 {
12635 int gpdelay;
12636
12637 /* If this is a reference to an external symbol, we want
12638 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12639 addu $at,$at,$gp
12640 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12641 nop
12642 <op> op[0],0($at)
12643 <op> op[0]+1,4($at)
12644 Otherwise we want
12645 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12646 nop
12647 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12648 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12649 If there is a base register we add it to $at before the
12650 lwc1 instructions. If there is a constant we include it
12651 in the lwc1 instructions. */
12652 used_at = 1;
12653 expr1.X_add_number = offset_expr.X_add_number;
12654 offset_expr.X_add_number = 0;
12655 if (expr1.X_add_number < -0x8000
12656 || expr1.X_add_number >= 0x8000 - 4)
12657 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12658 gpdelay = reg_needs_delay (mips_gp_register);
12659 relax_start (offset_expr.X_add_symbol);
12660 macro_build (&offset_expr, "lui", LUI_FMT,
12661 AT, BFD_RELOC_MIPS_GOT_HI16);
12662 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12663 AT, AT, mips_gp_register);
12664 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
12665 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
12666 load_delay_nop ();
12667 if (breg != 0)
12668 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12669 /* Itbl support may require additional care here. */
12670 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12671 BFD_RELOC_LO16, AT);
12672 expr1.X_add_number += 4;
12673
12674 /* Set mips_optimize to 2 to avoid inserting an undesired
12675 nop. */
12676 hold_mips_optimize = mips_optimize;
12677 mips_optimize = 2;
12678 /* Itbl support may require additional care here. */
12679 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12680 BFD_RELOC_LO16, AT);
12681 mips_optimize = hold_mips_optimize;
12682 expr1.X_add_number -= 4;
12683
12684 relax_switch ();
12685 offset_expr.X_add_number = expr1.X_add_number;
12686 if (gpdelay)
12687 macro_build (NULL, "nop", "");
12688 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12689 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12690 load_delay_nop ();
12691 if (breg != 0)
12692 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12693 /* Itbl support may require additional care here. */
12694 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12695 BFD_RELOC_LO16, AT);
12696 offset_expr.X_add_number += 4;
12697
12698 /* Set mips_optimize to 2 to avoid inserting an undesired
12699 nop. */
12700 hold_mips_optimize = mips_optimize;
12701 mips_optimize = 2;
12702 /* Itbl support may require additional care here. */
12703 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12704 BFD_RELOC_LO16, AT);
12705 mips_optimize = hold_mips_optimize;
12706 relax_end ();
12707 }
12708 else
12709 abort ();
12710
12711 break;
12712
12713 case M_SAA_AB:
12714 s = "saa";
12715 goto saa_saad;
12716 case M_SAAD_AB:
12717 s = "saad";
12718 saa_saad:
12719 gas_assert (!mips_opts.micromips);
12720 offbits = 0;
12721 fmt = "t,(b)";
12722 goto ld_st;
12723
12724 /* New code added to support COPZ instructions.
12725 This code builds table entries out of the macros in mip_opcodes.
12726 R4000 uses interlocks to handle coproc delays.
12727 Other chips (like the R3000) require nops to be inserted for delays.
12728
12729 FIXME: Currently, we require that the user handle delays.
12730 In order to fill delay slots for non-interlocked chips,
12731 we must have a way to specify delays based on the coprocessor.
12732 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12733 What are the side-effects of the cop instruction?
12734 What cache support might we have and what are its effects?
12735 Both coprocessor & memory require delays. how long???
12736 What registers are read/set/modified?
12737
12738 If an itbl is provided to interpret cop instructions,
12739 this knowledge can be encoded in the itbl spec. */
12740
12741 case M_COP0:
12742 s = "c0";
12743 goto copz;
12744 case M_COP1:
12745 s = "c1";
12746 goto copz;
12747 case M_COP2:
12748 s = "c2";
12749 goto copz;
12750 case M_COP3:
12751 s = "c3";
12752 copz:
12753 gas_assert (!mips_opts.micromips);
12754 /* For now we just do C (same as Cz). The parameter will be
12755 stored in insn_opcode by mips_ip. */
12756 macro_build (NULL, s, "C", (int) ip->insn_opcode);
12757 break;
12758
12759 case M_MOVE:
12760 move_register (op[0], op[1]);
12761 break;
12762
12763 case M_MOVEP:
12764 gas_assert (mips_opts.micromips);
12765 gas_assert (mips_opts.insn32);
12766 move_register (micromips_to_32_reg_h_map1[op[0]],
12767 micromips_to_32_reg_m_map[op[1]]);
12768 move_register (micromips_to_32_reg_h_map2[op[0]],
12769 micromips_to_32_reg_n_map[op[2]]);
12770 break;
12771
12772 case M_DMUL:
12773 dbl = 1;
12774 /* Fall through. */
12775 case M_MUL:
12776 if (mips_opts.arch == CPU_R5900)
12777 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12778 op[2]);
12779 else
12780 {
12781 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12782 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12783 }
12784 break;
12785
12786 case M_DMUL_I:
12787 dbl = 1;
12788 /* Fall through. */
12789 case M_MUL_I:
12790 /* The MIPS assembler some times generates shifts and adds. I'm
12791 not trying to be that fancy. GCC should do this for us
12792 anyway. */
12793 used_at = 1;
12794 load_register (AT, &imm_expr, dbl);
12795 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
12796 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12797 break;
12798
12799 case M_DMULO_I:
12800 dbl = 1;
12801 /* Fall through. */
12802 case M_MULO_I:
12803 imm = 1;
12804 goto do_mulo;
12805
12806 case M_DMULO:
12807 dbl = 1;
12808 /* Fall through. */
12809 case M_MULO:
12810 do_mulo:
12811 start_noreorder ();
12812 used_at = 1;
12813 if (imm)
12814 load_register (AT, &imm_expr, dbl);
12815 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
12816 op[1], imm ? AT : op[2]);
12817 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12818 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
12819 macro_build (NULL, "mfhi", MFHL_FMT, AT);
12820 if (mips_trap)
12821 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
12822 else
12823 {
12824 if (mips_opts.micromips)
12825 micromips_label_expr (&label_expr);
12826 else
12827 label_expr.X_add_number = 8;
12828 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
12829 macro_build (NULL, "nop", "");
12830 macro_build (NULL, "break", BRK_FMT, 6);
12831 if (mips_opts.micromips)
12832 micromips_add_label ();
12833 }
12834 end_noreorder ();
12835 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12836 break;
12837
12838 case M_DMULOU_I:
12839 dbl = 1;
12840 /* Fall through. */
12841 case M_MULOU_I:
12842 imm = 1;
12843 goto do_mulou;
12844
12845 case M_DMULOU:
12846 dbl = 1;
12847 /* Fall through. */
12848 case M_MULOU:
12849 do_mulou:
12850 start_noreorder ();
12851 used_at = 1;
12852 if (imm)
12853 load_register (AT, &imm_expr, dbl);
12854 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
12855 op[1], imm ? AT : op[2]);
12856 macro_build (NULL, "mfhi", MFHL_FMT, AT);
12857 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12858 if (mips_trap)
12859 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
12860 else
12861 {
12862 if (mips_opts.micromips)
12863 micromips_label_expr (&label_expr);
12864 else
12865 label_expr.X_add_number = 8;
12866 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
12867 macro_build (NULL, "nop", "");
12868 macro_build (NULL, "break", BRK_FMT, 6);
12869 if (mips_opts.micromips)
12870 micromips_add_label ();
12871 }
12872 end_noreorder ();
12873 break;
12874
12875 case M_DROL:
12876 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
12877 {
12878 if (op[0] == op[1])
12879 {
12880 tempreg = AT;
12881 used_at = 1;
12882 }
12883 else
12884 tempreg = op[0];
12885 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
12886 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
12887 break;
12888 }
12889 used_at = 1;
12890 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12891 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
12892 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
12893 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12894 break;
12895
12896 case M_ROL:
12897 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
12898 {
12899 if (op[0] == op[1])
12900 {
12901 tempreg = AT;
12902 used_at = 1;
12903 }
12904 else
12905 tempreg = op[0];
12906 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
12907 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
12908 break;
12909 }
12910 used_at = 1;
12911 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12912 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
12913 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
12914 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12915 break;
12916
12917 case M_DROL_I:
12918 {
12919 unsigned int rot;
12920 const char *l;
12921 const char *rr;
12922
12923 rot = imm_expr.X_add_number & 0x3f;
12924 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
12925 {
12926 rot = (64 - rot) & 0x3f;
12927 if (rot >= 32)
12928 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
12929 else
12930 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
12931 break;
12932 }
12933 if (rot == 0)
12934 {
12935 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
12936 break;
12937 }
12938 l = (rot < 0x20) ? "dsll" : "dsll32";
12939 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
12940 rot &= 0x1f;
12941 used_at = 1;
12942 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
12943 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12944 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12945 }
12946 break;
12947
12948 case M_ROL_I:
12949 {
12950 unsigned int rot;
12951
12952 rot = imm_expr.X_add_number & 0x1f;
12953 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
12954 {
12955 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
12956 (32 - rot) & 0x1f);
12957 break;
12958 }
12959 if (rot == 0)
12960 {
12961 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
12962 break;
12963 }
12964 used_at = 1;
12965 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
12966 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12967 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12968 }
12969 break;
12970
12971 case M_DROR:
12972 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
12973 {
12974 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
12975 break;
12976 }
12977 used_at = 1;
12978 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12979 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
12980 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
12981 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12982 break;
12983
12984 case M_ROR:
12985 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
12986 {
12987 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
12988 break;
12989 }
12990 used_at = 1;
12991 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12992 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
12993 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
12994 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12995 break;
12996
12997 case M_DROR_I:
12998 {
12999 unsigned int rot;
13000 const char *l;
13001 const char *rr;
13002
13003 rot = imm_expr.X_add_number & 0x3f;
13004 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13005 {
13006 if (rot >= 32)
13007 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13008 else
13009 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13010 break;
13011 }
13012 if (rot == 0)
13013 {
13014 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13015 break;
13016 }
13017 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
13018 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13019 rot &= 0x1f;
13020 used_at = 1;
13021 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13022 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13023 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13024 }
13025 break;
13026
13027 case M_ROR_I:
13028 {
13029 unsigned int rot;
13030
13031 rot = imm_expr.X_add_number & 0x1f;
13032 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13033 {
13034 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
13035 break;
13036 }
13037 if (rot == 0)
13038 {
13039 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13040 break;
13041 }
13042 used_at = 1;
13043 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13044 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13045 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13046 }
13047 break;
13048
13049 case M_SEQ:
13050 if (op[1] == 0)
13051 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13052 else if (op[2] == 0)
13053 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13054 else
13055 {
13056 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13057 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13058 }
13059 break;
13060
13061 case M_SEQ_I:
13062 if (imm_expr.X_add_number == 0)
13063 {
13064 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13065 break;
13066 }
13067 if (op[1] == 0)
13068 {
13069 as_warn (_("instruction %s: result is always false"),
13070 ip->insn_mo->name);
13071 move_register (op[0], 0);
13072 break;
13073 }
13074 if (CPU_HAS_SEQ (mips_opts.arch)
13075 && -512 <= imm_expr.X_add_number
13076 && imm_expr.X_add_number < 512)
13077 {
13078 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
13079 (int) imm_expr.X_add_number);
13080 break;
13081 }
13082 if (imm_expr.X_add_number >= 0
13083 && imm_expr.X_add_number < 0x10000)
13084 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
13085 else if (imm_expr.X_add_number > -0x8000
13086 && imm_expr.X_add_number < 0)
13087 {
13088 imm_expr.X_add_number = -imm_expr.X_add_number;
13089 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13090 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13091 }
13092 else if (CPU_HAS_SEQ (mips_opts.arch))
13093 {
13094 used_at = 1;
13095 load_register (AT, &imm_expr, GPR_SIZE == 64);
13096 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
13097 break;
13098 }
13099 else
13100 {
13101 load_register (AT, &imm_expr, GPR_SIZE == 64);
13102 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13103 used_at = 1;
13104 }
13105 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13106 break;
13107
13108 case M_SGE: /* X >= Y <==> not (X < Y) */
13109 s = "slt";
13110 goto sge;
13111 case M_SGEU:
13112 s = "sltu";
13113 sge:
13114 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13115 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13116 break;
13117
13118 case M_SGE_I: /* X >= I <==> not (X < I) */
13119 case M_SGEU_I:
13120 if (imm_expr.X_add_number >= -0x8000
13121 && imm_expr.X_add_number < 0x8000)
13122 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13123 op[0], op[1], BFD_RELOC_LO16);
13124 else
13125 {
13126 load_register (AT, &imm_expr, GPR_SIZE == 64);
13127 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
13128 op[0], op[1], AT);
13129 used_at = 1;
13130 }
13131 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13132 break;
13133
13134 case M_SGT: /* X > Y <==> Y < X */
13135 s = "slt";
13136 goto sgt;
13137 case M_SGTU:
13138 s = "sltu";
13139 sgt:
13140 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13141 break;
13142
13143 case M_SGT_I: /* X > I <==> I < X */
13144 s = "slt";
13145 goto sgti;
13146 case M_SGTU_I:
13147 s = "sltu";
13148 sgti:
13149 used_at = 1;
13150 load_register (AT, &imm_expr, GPR_SIZE == 64);
13151 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13152 break;
13153
13154 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
13155 s = "slt";
13156 goto sle;
13157 case M_SLEU:
13158 s = "sltu";
13159 sle:
13160 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13161 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13162 break;
13163
13164 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
13165 s = "slt";
13166 goto slei;
13167 case M_SLEU_I:
13168 s = "sltu";
13169 slei:
13170 used_at = 1;
13171 load_register (AT, &imm_expr, GPR_SIZE == 64);
13172 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13173 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13174 break;
13175
13176 case M_SLT_I:
13177 if (imm_expr.X_add_number >= -0x8000
13178 && imm_expr.X_add_number < 0x8000)
13179 {
13180 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13181 BFD_RELOC_LO16);
13182 break;
13183 }
13184 used_at = 1;
13185 load_register (AT, &imm_expr, GPR_SIZE == 64);
13186 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
13187 break;
13188
13189 case M_SLTU_I:
13190 if (imm_expr.X_add_number >= -0x8000
13191 && imm_expr.X_add_number < 0x8000)
13192 {
13193 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
13194 BFD_RELOC_LO16);
13195 break;
13196 }
13197 used_at = 1;
13198 load_register (AT, &imm_expr, GPR_SIZE == 64);
13199 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
13200 break;
13201
13202 case M_SNE:
13203 if (op[1] == 0)
13204 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13205 else if (op[2] == 0)
13206 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13207 else
13208 {
13209 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13210 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13211 }
13212 break;
13213
13214 case M_SNE_I:
13215 if (imm_expr.X_add_number == 0)
13216 {
13217 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13218 break;
13219 }
13220 if (op[1] == 0)
13221 {
13222 as_warn (_("instruction %s: result is always true"),
13223 ip->insn_mo->name);
13224 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
13225 op[0], 0, BFD_RELOC_LO16);
13226 break;
13227 }
13228 if (CPU_HAS_SEQ (mips_opts.arch)
13229 && -512 <= imm_expr.X_add_number
13230 && imm_expr.X_add_number < 512)
13231 {
13232 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
13233 (int) imm_expr.X_add_number);
13234 break;
13235 }
13236 if (imm_expr.X_add_number >= 0
13237 && imm_expr.X_add_number < 0x10000)
13238 {
13239 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13240 BFD_RELOC_LO16);
13241 }
13242 else if (imm_expr.X_add_number > -0x8000
13243 && imm_expr.X_add_number < 0)
13244 {
13245 imm_expr.X_add_number = -imm_expr.X_add_number;
13246 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13247 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13248 }
13249 else if (CPU_HAS_SEQ (mips_opts.arch))
13250 {
13251 used_at = 1;
13252 load_register (AT, &imm_expr, GPR_SIZE == 64);
13253 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
13254 break;
13255 }
13256 else
13257 {
13258 load_register (AT, &imm_expr, GPR_SIZE == 64);
13259 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13260 used_at = 1;
13261 }
13262 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13263 break;
13264
13265 case M_SUB_I:
13266 s = "addi";
13267 s2 = "sub";
13268 goto do_subi;
13269 case M_SUBU_I:
13270 s = "addiu";
13271 s2 = "subu";
13272 goto do_subi;
13273 case M_DSUB_I:
13274 dbl = 1;
13275 s = "daddi";
13276 s2 = "dsub";
13277 if (!mips_opts.micromips)
13278 goto do_subi;
13279 if (imm_expr.X_add_number > -0x200
13280 && imm_expr.X_add_number <= 0x200)
13281 {
13282 macro_build (NULL, s, "t,r,.", op[0], op[1],
13283 (int) -imm_expr.X_add_number);
13284 break;
13285 }
13286 goto do_subi_i;
13287 case M_DSUBU_I:
13288 dbl = 1;
13289 s = "daddiu";
13290 s2 = "dsubu";
13291 do_subi:
13292 if (imm_expr.X_add_number > -0x8000
13293 && imm_expr.X_add_number <= 0x8000)
13294 {
13295 imm_expr.X_add_number = -imm_expr.X_add_number;
13296 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13297 break;
13298 }
13299 do_subi_i:
13300 used_at = 1;
13301 load_register (AT, &imm_expr, dbl);
13302 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
13303 break;
13304
13305 case M_TEQ_I:
13306 s = "teq";
13307 goto trap;
13308 case M_TGE_I:
13309 s = "tge";
13310 goto trap;
13311 case M_TGEU_I:
13312 s = "tgeu";
13313 goto trap;
13314 case M_TLT_I:
13315 s = "tlt";
13316 goto trap;
13317 case M_TLTU_I:
13318 s = "tltu";
13319 goto trap;
13320 case M_TNE_I:
13321 s = "tne";
13322 trap:
13323 used_at = 1;
13324 load_register (AT, &imm_expr, GPR_SIZE == 64);
13325 macro_build (NULL, s, "s,t", op[0], AT);
13326 break;
13327
13328 case M_TRUNCWS:
13329 case M_TRUNCWD:
13330 gas_assert (!mips_opts.micromips);
13331 gas_assert (mips_opts.isa == ISA_MIPS1);
13332 used_at = 1;
13333
13334 /*
13335 * Is the double cfc1 instruction a bug in the mips assembler;
13336 * or is there a reason for it?
13337 */
13338 start_noreorder ();
13339 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13340 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13341 macro_build (NULL, "nop", "");
13342 expr1.X_add_number = 3;
13343 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
13344 expr1.X_add_number = 2;
13345 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13346 macro_build (NULL, "ctc1", "t,G", AT, RA);
13347 macro_build (NULL, "nop", "");
13348 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
13349 op[0], op[1]);
13350 macro_build (NULL, "ctc1", "t,G", op[2], RA);
13351 macro_build (NULL, "nop", "");
13352 end_noreorder ();
13353 break;
13354
13355 case M_ULH_AB:
13356 s = "lb";
13357 s2 = "lbu";
13358 off = 1;
13359 goto uld_st;
13360 case M_ULHU_AB:
13361 s = "lbu";
13362 s2 = "lbu";
13363 off = 1;
13364 goto uld_st;
13365 case M_ULW_AB:
13366 s = "lwl";
13367 s2 = "lwr";
13368 offbits = (mips_opts.micromips ? 12 : 16);
13369 off = 3;
13370 goto uld_st;
13371 case M_ULD_AB:
13372 s = "ldl";
13373 s2 = "ldr";
13374 offbits = (mips_opts.micromips ? 12 : 16);
13375 off = 7;
13376 goto uld_st;
13377 case M_USH_AB:
13378 s = "sb";
13379 s2 = "sb";
13380 off = 1;
13381 ust = 1;
13382 goto uld_st;
13383 case M_USW_AB:
13384 s = "swl";
13385 s2 = "swr";
13386 offbits = (mips_opts.micromips ? 12 : 16);
13387 off = 3;
13388 ust = 1;
13389 goto uld_st;
13390 case M_USD_AB:
13391 s = "sdl";
13392 s2 = "sdr";
13393 offbits = (mips_opts.micromips ? 12 : 16);
13394 off = 7;
13395 ust = 1;
13396
13397 uld_st:
13398 breg = op[2];
13399 large_offset = !small_offset_p (off, align, offbits);
13400 ep = &offset_expr;
13401 expr1.X_add_number = 0;
13402 if (large_offset)
13403 {
13404 used_at = 1;
13405 tempreg = AT;
13406 if (small_offset_p (0, align, 16))
13407 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13408 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13409 else
13410 {
13411 load_address (tempreg, ep, &used_at);
13412 if (breg != 0)
13413 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13414 tempreg, tempreg, breg);
13415 }
13416 offset_reloc[0] = BFD_RELOC_LO16;
13417 offset_reloc[1] = BFD_RELOC_UNUSED;
13418 offset_reloc[2] = BFD_RELOC_UNUSED;
13419 breg = tempreg;
13420 tempreg = op[0];
13421 ep = &expr1;
13422 }
13423 else if (!ust && op[0] == breg)
13424 {
13425 used_at = 1;
13426 tempreg = AT;
13427 }
13428 else
13429 tempreg = op[0];
13430
13431 if (off == 1)
13432 goto ulh_sh;
13433
13434 if (!target_big_endian)
13435 ep->X_add_number += off;
13436 if (offbits == 12)
13437 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
13438 else
13439 macro_build (ep, s, "t,o(b)", tempreg, -1,
13440 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13441
13442 if (!target_big_endian)
13443 ep->X_add_number -= off;
13444 else
13445 ep->X_add_number += off;
13446 if (offbits == 12)
13447 macro_build (NULL, s2, "t,~(b)",
13448 tempreg, (int) ep->X_add_number, breg);
13449 else
13450 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13451 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13452
13453 /* If necessary, move the result in tempreg to the final destination. */
13454 if (!ust && op[0] != tempreg)
13455 {
13456 /* Protect second load's delay slot. */
13457 load_delay_nop ();
13458 move_register (op[0], tempreg);
13459 }
13460 break;
13461
13462 ulh_sh:
13463 used_at = 1;
13464 if (target_big_endian == ust)
13465 ep->X_add_number += off;
13466 tempreg = ust || large_offset ? op[0] : AT;
13467 macro_build (ep, s, "t,o(b)", tempreg, -1,
13468 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13469
13470 /* For halfword transfers we need a temporary register to shuffle
13471 bytes. Unfortunately for M_USH_A we have none available before
13472 the next store as AT holds the base address. We deal with this
13473 case by clobbering TREG and then restoring it as with ULH. */
13474 tempreg = ust == large_offset ? op[0] : AT;
13475 if (ust)
13476 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
13477
13478 if (target_big_endian == ust)
13479 ep->X_add_number -= off;
13480 else
13481 ep->X_add_number += off;
13482 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13483 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13484
13485 /* For M_USH_A re-retrieve the LSB. */
13486 if (ust && large_offset)
13487 {
13488 if (target_big_endian)
13489 ep->X_add_number += off;
13490 else
13491 ep->X_add_number -= off;
13492 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13493 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
13494 }
13495 /* For ULH and M_USH_A OR the LSB in. */
13496 if (!ust || large_offset)
13497 {
13498 tempreg = !large_offset ? AT : op[0];
13499 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
13500 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13501 }
13502 break;
13503
13504 default:
13505 /* FIXME: Check if this is one of the itbl macros, since they
13506 are added dynamically. */
13507 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
13508 break;
13509 }
13510 if (!mips_opts.at && used_at)
13511 as_bad (_("macro used $at after \".set noat\""));
13512 }
13513
13514 /* Implement macros in mips16 mode. */
13515
13516 static void
13517 mips16_macro (struct mips_cl_insn *ip)
13518 {
13519 const struct mips_operand_array *operands;
13520 int mask;
13521 int tmp;
13522 expressionS expr1;
13523 int dbl;
13524 const char *s, *s2, *s3;
13525 unsigned int op[MAX_OPERANDS];
13526 unsigned int i;
13527
13528 mask = ip->insn_mo->mask;
13529
13530 operands = insn_operands (ip);
13531 for (i = 0; i < MAX_OPERANDS; i++)
13532 if (operands->operand[i])
13533 op[i] = insn_extract_operand (ip, operands->operand[i]);
13534 else
13535 op[i] = -1;
13536
13537 expr1.X_op = O_constant;
13538 expr1.X_op_symbol = NULL;
13539 expr1.X_add_symbol = NULL;
13540 expr1.X_add_number = 1;
13541
13542 dbl = 0;
13543
13544 switch (mask)
13545 {
13546 default:
13547 abort ();
13548
13549 case M_DDIV_3:
13550 dbl = 1;
13551 /* Fall through. */
13552 case M_DIV_3:
13553 s = "mflo";
13554 goto do_div3;
13555 case M_DREM_3:
13556 dbl = 1;
13557 /* Fall through. */
13558 case M_REM_3:
13559 s = "mfhi";
13560 do_div3:
13561 start_noreorder ();
13562 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
13563 expr1.X_add_number = 2;
13564 macro_build (&expr1, "bnez", "x,p", op[2]);
13565 macro_build (NULL, "break", "6", 7);
13566
13567 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13568 since that causes an overflow. We should do that as well,
13569 but I don't see how to do the comparisons without a temporary
13570 register. */
13571 end_noreorder ();
13572 macro_build (NULL, s, "x", op[0]);
13573 break;
13574
13575 case M_DIVU_3:
13576 s = "divu";
13577 s2 = "mflo";
13578 goto do_divu3;
13579 case M_REMU_3:
13580 s = "divu";
13581 s2 = "mfhi";
13582 goto do_divu3;
13583 case M_DDIVU_3:
13584 s = "ddivu";
13585 s2 = "mflo";
13586 goto do_divu3;
13587 case M_DREMU_3:
13588 s = "ddivu";
13589 s2 = "mfhi";
13590 do_divu3:
13591 start_noreorder ();
13592 macro_build (NULL, s, ".,x,y", op[1], op[2]);
13593 expr1.X_add_number = 2;
13594 macro_build (&expr1, "bnez", "x,p", op[2]);
13595 macro_build (NULL, "break", "6", 7);
13596 end_noreorder ();
13597 macro_build (NULL, s2, "x", op[0]);
13598 break;
13599
13600 case M_DMUL:
13601 dbl = 1;
13602 /* Fall through. */
13603 case M_MUL:
13604 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13605 macro_build (NULL, "mflo", "x", op[0]);
13606 break;
13607
13608 case M_DSUBU_I:
13609 dbl = 1;
13610 goto do_subu;
13611 case M_SUBU_I:
13612 do_subu:
13613 imm_expr.X_add_number = -imm_expr.X_add_number;
13614 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
13615 break;
13616
13617 case M_SUBU_I_2:
13618 imm_expr.X_add_number = -imm_expr.X_add_number;
13619 macro_build (&imm_expr, "addiu", "x,k", op[0]);
13620 break;
13621
13622 case M_DSUBU_I_2:
13623 imm_expr.X_add_number = -imm_expr.X_add_number;
13624 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
13625 break;
13626
13627 case M_BEQ:
13628 s = "cmp";
13629 s2 = "bteqz";
13630 goto do_branch;
13631 case M_BNE:
13632 s = "cmp";
13633 s2 = "btnez";
13634 goto do_branch;
13635 case M_BLT:
13636 s = "slt";
13637 s2 = "btnez";
13638 goto do_branch;
13639 case M_BLTU:
13640 s = "sltu";
13641 s2 = "btnez";
13642 goto do_branch;
13643 case M_BLE:
13644 s = "slt";
13645 s2 = "bteqz";
13646 goto do_reverse_branch;
13647 case M_BLEU:
13648 s = "sltu";
13649 s2 = "bteqz";
13650 goto do_reverse_branch;
13651 case M_BGE:
13652 s = "slt";
13653 s2 = "bteqz";
13654 goto do_branch;
13655 case M_BGEU:
13656 s = "sltu";
13657 s2 = "bteqz";
13658 goto do_branch;
13659 case M_BGT:
13660 s = "slt";
13661 s2 = "btnez";
13662 goto do_reverse_branch;
13663 case M_BGTU:
13664 s = "sltu";
13665 s2 = "btnez";
13666
13667 do_reverse_branch:
13668 tmp = op[1];
13669 op[1] = op[0];
13670 op[0] = tmp;
13671
13672 do_branch:
13673 macro_build (NULL, s, "x,y", op[0], op[1]);
13674 macro_build (&offset_expr, s2, "p");
13675 break;
13676
13677 case M_BEQ_I:
13678 s = "cmpi";
13679 s2 = "bteqz";
13680 s3 = "x,U";
13681 goto do_branch_i;
13682 case M_BNE_I:
13683 s = "cmpi";
13684 s2 = "btnez";
13685 s3 = "x,U";
13686 goto do_branch_i;
13687 case M_BLT_I:
13688 s = "slti";
13689 s2 = "btnez";
13690 s3 = "x,8";
13691 goto do_branch_i;
13692 case M_BLTU_I:
13693 s = "sltiu";
13694 s2 = "btnez";
13695 s3 = "x,8";
13696 goto do_branch_i;
13697 case M_BLE_I:
13698 s = "slti";
13699 s2 = "btnez";
13700 s3 = "x,8";
13701 goto do_addone_branch_i;
13702 case M_BLEU_I:
13703 s = "sltiu";
13704 s2 = "btnez";
13705 s3 = "x,8";
13706 goto do_addone_branch_i;
13707 case M_BGE_I:
13708 s = "slti";
13709 s2 = "bteqz";
13710 s3 = "x,8";
13711 goto do_branch_i;
13712 case M_BGEU_I:
13713 s = "sltiu";
13714 s2 = "bteqz";
13715 s3 = "x,8";
13716 goto do_branch_i;
13717 case M_BGT_I:
13718 s = "slti";
13719 s2 = "bteqz";
13720 s3 = "x,8";
13721 goto do_addone_branch_i;
13722 case M_BGTU_I:
13723 s = "sltiu";
13724 s2 = "bteqz";
13725 s3 = "x,8";
13726
13727 do_addone_branch_i:
13728 ++imm_expr.X_add_number;
13729
13730 do_branch_i:
13731 macro_build (&imm_expr, s, s3, op[0]);
13732 macro_build (&offset_expr, s2, "p");
13733 break;
13734
13735 case M_ABS:
13736 expr1.X_add_number = 0;
13737 macro_build (&expr1, "slti", "x,8", op[1]);
13738 if (op[0] != op[1])
13739 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
13740 expr1.X_add_number = 2;
13741 macro_build (&expr1, "bteqz", "p");
13742 macro_build (NULL, "neg", "x,w", op[0], op[0]);
13743 break;
13744 }
13745 }
13746
13747 /* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13748 opcode bits in *OPCODE_EXTRA. */
13749
13750 static struct mips_opcode *
13751 mips_lookup_insn (struct hash_control *hash, const char *start,
13752 ssize_t length, unsigned int *opcode_extra)
13753 {
13754 char *name, *dot, *p;
13755 unsigned int mask, suffix;
13756 ssize_t opend;
13757 struct mips_opcode *insn;
13758
13759 /* Make a copy of the instruction so that we can fiddle with it. */
13760 name = xstrndup (start, length);
13761
13762 /* Look up the instruction as-is. */
13763 insn = (struct mips_opcode *) hash_find (hash, name);
13764 if (insn)
13765 goto end;
13766
13767 dot = strchr (name, '.');
13768 if (dot && dot[1])
13769 {
13770 /* Try to interpret the text after the dot as a VU0 channel suffix. */
13771 p = mips_parse_vu0_channels (dot + 1, &mask);
13772 if (*p == 0 && mask != 0)
13773 {
13774 *dot = 0;
13775 insn = (struct mips_opcode *) hash_find (hash, name);
13776 *dot = '.';
13777 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13778 {
13779 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
13780 goto end;
13781 }
13782 }
13783 }
13784
13785 if (mips_opts.micromips)
13786 {
13787 /* See if there's an instruction size override suffix,
13788 either `16' or `32', at the end of the mnemonic proper,
13789 that defines the operation, i.e. before the first `.'
13790 character if any. Strip it and retry. */
13791 opend = dot != NULL ? dot - name : length;
13792 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13793 suffix = 2;
13794 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13795 suffix = 4;
13796 else
13797 suffix = 0;
13798 if (suffix)
13799 {
13800 memcpy (name + opend - 2, name + opend, length - opend + 1);
13801 insn = (struct mips_opcode *) hash_find (hash, name);
13802 if (insn)
13803 {
13804 forced_insn_length = suffix;
13805 goto end;
13806 }
13807 }
13808 }
13809
13810 insn = NULL;
13811 end:
13812 free (name);
13813 return insn;
13814 }
13815
13816 /* Assemble an instruction into its binary format. If the instruction
13817 is a macro, set imm_expr and offset_expr to the values associated
13818 with "I" and "A" operands respectively. Otherwise store the value
13819 of the relocatable field (if any) in offset_expr. In both cases
13820 set offset_reloc to the relocation operators applied to offset_expr. */
13821
13822 static void
13823 mips_ip (char *str, struct mips_cl_insn *insn)
13824 {
13825 const struct mips_opcode *first, *past;
13826 struct hash_control *hash;
13827 char format;
13828 size_t end;
13829 struct mips_operand_token *tokens;
13830 unsigned int opcode_extra;
13831
13832 if (mips_opts.micromips)
13833 {
13834 hash = micromips_op_hash;
13835 past = &micromips_opcodes[bfd_micromips_num_opcodes];
13836 }
13837 else
13838 {
13839 hash = op_hash;
13840 past = &mips_opcodes[NUMOPCODES];
13841 }
13842 forced_insn_length = 0;
13843 opcode_extra = 0;
13844
13845 /* We first try to match an instruction up to a space or to the end. */
13846 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
13847 continue;
13848
13849 first = mips_lookup_insn (hash, str, end, &opcode_extra);
13850 if (first == NULL)
13851 {
13852 set_insn_error (0, _("unrecognized opcode"));
13853 return;
13854 }
13855
13856 if (strcmp (first->name, "li.s") == 0)
13857 format = 'f';
13858 else if (strcmp (first->name, "li.d") == 0)
13859 format = 'd';
13860 else
13861 format = 0;
13862 tokens = mips_parse_arguments (str + end, format);
13863 if (!tokens)
13864 return;
13865
13866 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
13867 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
13868 set_insn_error (0, _("invalid operands"));
13869
13870 obstack_free (&mips_operand_tokens, tokens);
13871 }
13872
13873 /* As for mips_ip, but used when assembling MIPS16 code.
13874 Also set forced_insn_length to the resulting instruction size in
13875 bytes if the user explicitly requested a small or extended instruction. */
13876
13877 static void
13878 mips16_ip (char *str, struct mips_cl_insn *insn)
13879 {
13880 char *end, *s, c;
13881 struct mips_opcode *first;
13882 struct mips_operand_token *tokens;
13883 unsigned int l;
13884
13885 for (s = str; ISLOWER (*s); ++s)
13886 ;
13887 end = s;
13888 c = *end;
13889
13890 l = 0;
13891 switch (c)
13892 {
13893 case '\0':
13894 break;
13895
13896 case ' ':
13897 s++;
13898 break;
13899
13900 case '.':
13901 s++;
13902 if (*s == 't')
13903 {
13904 l = 2;
13905 s++;
13906 }
13907 else if (*s == 'e')
13908 {
13909 l = 4;
13910 s++;
13911 }
13912 if (*s == '\0')
13913 break;
13914 else if (*s++ == ' ')
13915 break;
13916 /* Fall through. */
13917 default:
13918 set_insn_error (0, _("unrecognized opcode"));
13919 return;
13920 }
13921 forced_insn_length = l;
13922
13923 *end = 0;
13924 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
13925 *end = c;
13926
13927 if (!first)
13928 {
13929 set_insn_error (0, _("unrecognized opcode"));
13930 return;
13931 }
13932
13933 tokens = mips_parse_arguments (s, 0);
13934 if (!tokens)
13935 return;
13936
13937 if (!match_mips16_insns (insn, first, tokens))
13938 set_insn_error (0, _("invalid operands"));
13939
13940 obstack_free (&mips_operand_tokens, tokens);
13941 }
13942
13943 /* Marshal immediate value VAL for an extended MIPS16 instruction.
13944 NBITS is the number of significant bits in VAL. */
13945
13946 static unsigned long
13947 mips16_immed_extend (offsetT val, unsigned int nbits)
13948 {
13949 int extval;
13950 if (nbits == 16)
13951 {
13952 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
13953 val &= 0x1f;
13954 }
13955 else if (nbits == 15)
13956 {
13957 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
13958 val &= 0xf;
13959 }
13960 else
13961 {
13962 extval = ((val & 0x1f) << 6) | (val & 0x20);
13963 val = 0;
13964 }
13965 return (extval << 16) | val;
13966 }
13967
13968 /* Like decode_mips16_operand, but require the operand to be defined and
13969 require it to be an integer. */
13970
13971 static const struct mips_int_operand *
13972 mips16_immed_operand (int type, bfd_boolean extended_p)
13973 {
13974 const struct mips_operand *operand;
13975
13976 operand = decode_mips16_operand (type, extended_p);
13977 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
13978 abort ();
13979 return (const struct mips_int_operand *) operand;
13980 }
13981
13982 /* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
13983
13984 static bfd_boolean
13985 mips16_immed_in_range_p (const struct mips_int_operand *operand,
13986 bfd_reloc_code_real_type reloc, offsetT sval)
13987 {
13988 int min_val, max_val;
13989
13990 min_val = mips_int_operand_min (operand);
13991 max_val = mips_int_operand_max (operand);
13992 if (reloc != BFD_RELOC_UNUSED)
13993 {
13994 if (min_val < 0)
13995 sval = SEXT_16BIT (sval);
13996 else
13997 sval &= 0xffff;
13998 }
13999
14000 return (sval >= min_val
14001 && sval <= max_val
14002 && (sval & ((1 << operand->shift) - 1)) == 0);
14003 }
14004
14005 /* Install immediate value VAL into MIPS16 instruction *INSN,
14006 extending it if necessary. The instruction in *INSN may
14007 already be extended.
14008
14009 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14010 if none. In the former case, VAL is a 16-bit number with no
14011 defined signedness.
14012
14013 TYPE is the type of the immediate field. USER_INSN_LENGTH
14014 is the length that the user requested, or 0 if none. */
14015
14016 static void
14017 mips16_immed (const char *file, unsigned int line, int type,
14018 bfd_reloc_code_real_type reloc, offsetT val,
14019 unsigned int user_insn_length, unsigned long *insn)
14020 {
14021 const struct mips_int_operand *operand;
14022 unsigned int uval, length;
14023
14024 operand = mips16_immed_operand (type, FALSE);
14025 if (!mips16_immed_in_range_p (operand, reloc, val))
14026 {
14027 /* We need an extended instruction. */
14028 if (user_insn_length == 2)
14029 as_bad_where (file, line, _("invalid unextended operand value"));
14030 else
14031 *insn |= MIPS16_EXTEND;
14032 }
14033 else if (user_insn_length == 4)
14034 {
14035 /* The operand doesn't force an unextended instruction to be extended.
14036 Warn if the user wanted an extended instruction anyway. */
14037 *insn |= MIPS16_EXTEND;
14038 as_warn_where (file, line,
14039 _("extended operand requested but not required"));
14040 }
14041
14042 length = mips16_opcode_length (*insn);
14043 if (length == 4)
14044 {
14045 operand = mips16_immed_operand (type, TRUE);
14046 if (!mips16_immed_in_range_p (operand, reloc, val))
14047 as_bad_where (file, line,
14048 _("operand value out of range for instruction"));
14049 }
14050 uval = ((unsigned int) val >> operand->shift) - operand->bias;
14051 if (length == 2 || operand->root.lsb != 0)
14052 *insn = mips_insert_operand (&operand->root, *insn, uval);
14053 else
14054 *insn |= mips16_immed_extend (uval, operand->root.size);
14055 }
14056 \f
14057 struct percent_op_match
14058 {
14059 const char *str;
14060 bfd_reloc_code_real_type reloc;
14061 };
14062
14063 static const struct percent_op_match mips_percent_op[] =
14064 {
14065 {"%lo", BFD_RELOC_LO16},
14066 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14067 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14068 {"%call16", BFD_RELOC_MIPS_CALL16},
14069 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14070 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14071 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14072 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14073 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14074 {"%got", BFD_RELOC_MIPS_GOT16},
14075 {"%gp_rel", BFD_RELOC_GPREL16},
14076 {"%half", BFD_RELOC_16},
14077 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14078 {"%higher", BFD_RELOC_MIPS_HIGHER},
14079 {"%neg", BFD_RELOC_MIPS_SUB},
14080 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14081 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14082 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14083 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14084 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14085 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14086 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14087 {"%hi", BFD_RELOC_HI16_S},
14088 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14089 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
14090 };
14091
14092 static const struct percent_op_match mips16_percent_op[] =
14093 {
14094 {"%lo", BFD_RELOC_MIPS16_LO16},
14095 {"%gprel", BFD_RELOC_MIPS16_GPREL},
14096 {"%got", BFD_RELOC_MIPS16_GOT16},
14097 {"%call16", BFD_RELOC_MIPS16_CALL16},
14098 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14099 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14100 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14101 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14102 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14103 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14104 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14105 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14106 };
14107
14108
14109 /* Return true if *STR points to a relocation operator. When returning true,
14110 move *STR over the operator and store its relocation code in *RELOC.
14111 Leave both *STR and *RELOC alone when returning false. */
14112
14113 static bfd_boolean
14114 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14115 {
14116 const struct percent_op_match *percent_op;
14117 size_t limit, i;
14118
14119 if (mips_opts.mips16)
14120 {
14121 percent_op = mips16_percent_op;
14122 limit = ARRAY_SIZE (mips16_percent_op);
14123 }
14124 else
14125 {
14126 percent_op = mips_percent_op;
14127 limit = ARRAY_SIZE (mips_percent_op);
14128 }
14129
14130 for (i = 0; i < limit; i++)
14131 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14132 {
14133 int len = strlen (percent_op[i].str);
14134
14135 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14136 continue;
14137
14138 *str += strlen (percent_op[i].str);
14139 *reloc = percent_op[i].reloc;
14140
14141 /* Check whether the output BFD supports this relocation.
14142 If not, issue an error and fall back on something safe. */
14143 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14144 {
14145 as_bad (_("relocation %s isn't supported by the current ABI"),
14146 percent_op[i].str);
14147 *reloc = BFD_RELOC_UNUSED;
14148 }
14149 return TRUE;
14150 }
14151 return FALSE;
14152 }
14153
14154
14155 /* Parse string STR as a 16-bit relocatable operand. Store the
14156 expression in *EP and the relocations in the array starting
14157 at RELOC. Return the number of relocation operators used.
14158
14159 On exit, EXPR_END points to the first character after the expression. */
14160
14161 static size_t
14162 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14163 char *str)
14164 {
14165 bfd_reloc_code_real_type reversed_reloc[3];
14166 size_t reloc_index, i;
14167 int crux_depth, str_depth;
14168 char *crux;
14169
14170 /* Search for the start of the main expression, recoding relocations
14171 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14172 of the main expression and with CRUX_DEPTH containing the number
14173 of open brackets at that point. */
14174 reloc_index = -1;
14175 str_depth = 0;
14176 do
14177 {
14178 reloc_index++;
14179 crux = str;
14180 crux_depth = str_depth;
14181
14182 /* Skip over whitespace and brackets, keeping count of the number
14183 of brackets. */
14184 while (*str == ' ' || *str == '\t' || *str == '(')
14185 if (*str++ == '(')
14186 str_depth++;
14187 }
14188 while (*str == '%'
14189 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14190 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14191
14192 my_getExpression (ep, crux);
14193 str = expr_end;
14194
14195 /* Match every open bracket. */
14196 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14197 if (*str++ == ')')
14198 crux_depth--;
14199
14200 if (crux_depth > 0)
14201 as_bad (_("unclosed '('"));
14202
14203 expr_end = str;
14204
14205 if (reloc_index != 0)
14206 {
14207 prev_reloc_op_frag = frag_now;
14208 for (i = 0; i < reloc_index; i++)
14209 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14210 }
14211
14212 return reloc_index;
14213 }
14214
14215 static void
14216 my_getExpression (expressionS *ep, char *str)
14217 {
14218 char *save_in;
14219
14220 save_in = input_line_pointer;
14221 input_line_pointer = str;
14222 expression (ep);
14223 expr_end = input_line_pointer;
14224 input_line_pointer = save_in;
14225 }
14226
14227 const char *
14228 md_atof (int type, char *litP, int *sizeP)
14229 {
14230 return ieee_md_atof (type, litP, sizeP, target_big_endian);
14231 }
14232
14233 void
14234 md_number_to_chars (char *buf, valueT val, int n)
14235 {
14236 if (target_big_endian)
14237 number_to_chars_bigendian (buf, val, n);
14238 else
14239 number_to_chars_littleendian (buf, val, n);
14240 }
14241 \f
14242 static int support_64bit_objects(void)
14243 {
14244 const char **list, **l;
14245 int yes;
14246
14247 list = bfd_target_list ();
14248 for (l = list; *l != NULL; l++)
14249 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14250 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14251 break;
14252 yes = (*l != NULL);
14253 free (list);
14254 return yes;
14255 }
14256
14257 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14258 NEW_VALUE. Warn if another value was already specified. Note:
14259 we have to defer parsing the -march and -mtune arguments in order
14260 to handle 'from-abi' correctly, since the ABI might be specified
14261 in a later argument. */
14262
14263 static void
14264 mips_set_option_string (const char **string_ptr, const char *new_value)
14265 {
14266 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14267 as_warn (_("a different %s was already specified, is now %s"),
14268 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14269 new_value);
14270
14271 *string_ptr = new_value;
14272 }
14273
14274 int
14275 md_parse_option (int c, const char *arg)
14276 {
14277 unsigned int i;
14278
14279 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14280 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14281 {
14282 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
14283 c == mips_ases[i].option_on);
14284 return 1;
14285 }
14286
14287 switch (c)
14288 {
14289 case OPTION_CONSTRUCT_FLOATS:
14290 mips_disable_float_construction = 0;
14291 break;
14292
14293 case OPTION_NO_CONSTRUCT_FLOATS:
14294 mips_disable_float_construction = 1;
14295 break;
14296
14297 case OPTION_TRAP:
14298 mips_trap = 1;
14299 break;
14300
14301 case OPTION_BREAK:
14302 mips_trap = 0;
14303 break;
14304
14305 case OPTION_EB:
14306 target_big_endian = 1;
14307 break;
14308
14309 case OPTION_EL:
14310 target_big_endian = 0;
14311 break;
14312
14313 case 'O':
14314 if (arg == NULL)
14315 mips_optimize = 1;
14316 else if (arg[0] == '0')
14317 mips_optimize = 0;
14318 else if (arg[0] == '1')
14319 mips_optimize = 1;
14320 else
14321 mips_optimize = 2;
14322 break;
14323
14324 case 'g':
14325 if (arg == NULL)
14326 mips_debug = 2;
14327 else
14328 mips_debug = atoi (arg);
14329 break;
14330
14331 case OPTION_MIPS1:
14332 file_mips_opts.isa = ISA_MIPS1;
14333 break;
14334
14335 case OPTION_MIPS2:
14336 file_mips_opts.isa = ISA_MIPS2;
14337 break;
14338
14339 case OPTION_MIPS3:
14340 file_mips_opts.isa = ISA_MIPS3;
14341 break;
14342
14343 case OPTION_MIPS4:
14344 file_mips_opts.isa = ISA_MIPS4;
14345 break;
14346
14347 case OPTION_MIPS5:
14348 file_mips_opts.isa = ISA_MIPS5;
14349 break;
14350
14351 case OPTION_MIPS32:
14352 file_mips_opts.isa = ISA_MIPS32;
14353 break;
14354
14355 case OPTION_MIPS32R2:
14356 file_mips_opts.isa = ISA_MIPS32R2;
14357 break;
14358
14359 case OPTION_MIPS32R3:
14360 file_mips_opts.isa = ISA_MIPS32R3;
14361 break;
14362
14363 case OPTION_MIPS32R5:
14364 file_mips_opts.isa = ISA_MIPS32R5;
14365 break;
14366
14367 case OPTION_MIPS32R6:
14368 file_mips_opts.isa = ISA_MIPS32R6;
14369 break;
14370
14371 case OPTION_MIPS64R2:
14372 file_mips_opts.isa = ISA_MIPS64R2;
14373 break;
14374
14375 case OPTION_MIPS64R3:
14376 file_mips_opts.isa = ISA_MIPS64R3;
14377 break;
14378
14379 case OPTION_MIPS64R5:
14380 file_mips_opts.isa = ISA_MIPS64R5;
14381 break;
14382
14383 case OPTION_MIPS64R6:
14384 file_mips_opts.isa = ISA_MIPS64R6;
14385 break;
14386
14387 case OPTION_MIPS64:
14388 file_mips_opts.isa = ISA_MIPS64;
14389 break;
14390
14391 case OPTION_MTUNE:
14392 mips_set_option_string (&mips_tune_string, arg);
14393 break;
14394
14395 case OPTION_MARCH:
14396 mips_set_option_string (&mips_arch_string, arg);
14397 break;
14398
14399 case OPTION_M4650:
14400 mips_set_option_string (&mips_arch_string, "4650");
14401 mips_set_option_string (&mips_tune_string, "4650");
14402 break;
14403
14404 case OPTION_NO_M4650:
14405 break;
14406
14407 case OPTION_M4010:
14408 mips_set_option_string (&mips_arch_string, "4010");
14409 mips_set_option_string (&mips_tune_string, "4010");
14410 break;
14411
14412 case OPTION_NO_M4010:
14413 break;
14414
14415 case OPTION_M4100:
14416 mips_set_option_string (&mips_arch_string, "4100");
14417 mips_set_option_string (&mips_tune_string, "4100");
14418 break;
14419
14420 case OPTION_NO_M4100:
14421 break;
14422
14423 case OPTION_M3900:
14424 mips_set_option_string (&mips_arch_string, "3900");
14425 mips_set_option_string (&mips_tune_string, "3900");
14426 break;
14427
14428 case OPTION_NO_M3900:
14429 break;
14430
14431 case OPTION_MICROMIPS:
14432 if (file_mips_opts.mips16 == 1)
14433 {
14434 as_bad (_("-mmicromips cannot be used with -mips16"));
14435 return 0;
14436 }
14437 file_mips_opts.micromips = 1;
14438 mips_no_prev_insn ();
14439 break;
14440
14441 case OPTION_NO_MICROMIPS:
14442 file_mips_opts.micromips = 0;
14443 mips_no_prev_insn ();
14444 break;
14445
14446 case OPTION_MIPS16:
14447 if (file_mips_opts.micromips == 1)
14448 {
14449 as_bad (_("-mips16 cannot be used with -micromips"));
14450 return 0;
14451 }
14452 file_mips_opts.mips16 = 1;
14453 mips_no_prev_insn ();
14454 break;
14455
14456 case OPTION_NO_MIPS16:
14457 file_mips_opts.mips16 = 0;
14458 mips_no_prev_insn ();
14459 break;
14460
14461 case OPTION_FIX_24K:
14462 mips_fix_24k = 1;
14463 break;
14464
14465 case OPTION_NO_FIX_24K:
14466 mips_fix_24k = 0;
14467 break;
14468
14469 case OPTION_FIX_RM7000:
14470 mips_fix_rm7000 = 1;
14471 break;
14472
14473 case OPTION_NO_FIX_RM7000:
14474 mips_fix_rm7000 = 0;
14475 break;
14476
14477 case OPTION_FIX_LOONGSON2F_JUMP:
14478 mips_fix_loongson2f_jump = TRUE;
14479 break;
14480
14481 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14482 mips_fix_loongson2f_jump = FALSE;
14483 break;
14484
14485 case OPTION_FIX_LOONGSON2F_NOP:
14486 mips_fix_loongson2f_nop = TRUE;
14487 break;
14488
14489 case OPTION_NO_FIX_LOONGSON2F_NOP:
14490 mips_fix_loongson2f_nop = FALSE;
14491 break;
14492
14493 case OPTION_FIX_VR4120:
14494 mips_fix_vr4120 = 1;
14495 break;
14496
14497 case OPTION_NO_FIX_VR4120:
14498 mips_fix_vr4120 = 0;
14499 break;
14500
14501 case OPTION_FIX_VR4130:
14502 mips_fix_vr4130 = 1;
14503 break;
14504
14505 case OPTION_NO_FIX_VR4130:
14506 mips_fix_vr4130 = 0;
14507 break;
14508
14509 case OPTION_FIX_CN63XXP1:
14510 mips_fix_cn63xxp1 = TRUE;
14511 break;
14512
14513 case OPTION_NO_FIX_CN63XXP1:
14514 mips_fix_cn63xxp1 = FALSE;
14515 break;
14516
14517 case OPTION_RELAX_BRANCH:
14518 mips_relax_branch = 1;
14519 break;
14520
14521 case OPTION_NO_RELAX_BRANCH:
14522 mips_relax_branch = 0;
14523 break;
14524
14525 case OPTION_IGNORE_BRANCH_ISA:
14526 mips_ignore_branch_isa = TRUE;
14527 break;
14528
14529 case OPTION_NO_IGNORE_BRANCH_ISA:
14530 mips_ignore_branch_isa = FALSE;
14531 break;
14532
14533 case OPTION_INSN32:
14534 file_mips_opts.insn32 = TRUE;
14535 break;
14536
14537 case OPTION_NO_INSN32:
14538 file_mips_opts.insn32 = FALSE;
14539 break;
14540
14541 case OPTION_MSHARED:
14542 mips_in_shared = TRUE;
14543 break;
14544
14545 case OPTION_MNO_SHARED:
14546 mips_in_shared = FALSE;
14547 break;
14548
14549 case OPTION_MSYM32:
14550 file_mips_opts.sym32 = TRUE;
14551 break;
14552
14553 case OPTION_MNO_SYM32:
14554 file_mips_opts.sym32 = FALSE;
14555 break;
14556
14557 /* When generating ELF code, we permit -KPIC and -call_shared to
14558 select SVR4_PIC, and -non_shared to select no PIC. This is
14559 intended to be compatible with Irix 5. */
14560 case OPTION_CALL_SHARED:
14561 mips_pic = SVR4_PIC;
14562 mips_abicalls = TRUE;
14563 break;
14564
14565 case OPTION_CALL_NONPIC:
14566 mips_pic = NO_PIC;
14567 mips_abicalls = TRUE;
14568 break;
14569
14570 case OPTION_NON_SHARED:
14571 mips_pic = NO_PIC;
14572 mips_abicalls = FALSE;
14573 break;
14574
14575 /* The -xgot option tells the assembler to use 32 bit offsets
14576 when accessing the got in SVR4_PIC mode. It is for Irix
14577 compatibility. */
14578 case OPTION_XGOT:
14579 mips_big_got = 1;
14580 break;
14581
14582 case 'G':
14583 g_switch_value = atoi (arg);
14584 g_switch_seen = 1;
14585 break;
14586
14587 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14588 and -mabi=64. */
14589 case OPTION_32:
14590 mips_abi = O32_ABI;
14591 break;
14592
14593 case OPTION_N32:
14594 mips_abi = N32_ABI;
14595 break;
14596
14597 case OPTION_64:
14598 mips_abi = N64_ABI;
14599 if (!support_64bit_objects())
14600 as_fatal (_("no compiled in support for 64 bit object file format"));
14601 break;
14602
14603 case OPTION_GP32:
14604 file_mips_opts.gp = 32;
14605 break;
14606
14607 case OPTION_GP64:
14608 file_mips_opts.gp = 64;
14609 break;
14610
14611 case OPTION_FP32:
14612 file_mips_opts.fp = 32;
14613 break;
14614
14615 case OPTION_FPXX:
14616 file_mips_opts.fp = 0;
14617 break;
14618
14619 case OPTION_FP64:
14620 file_mips_opts.fp = 64;
14621 break;
14622
14623 case OPTION_ODD_SPREG:
14624 file_mips_opts.oddspreg = 1;
14625 break;
14626
14627 case OPTION_NO_ODD_SPREG:
14628 file_mips_opts.oddspreg = 0;
14629 break;
14630
14631 case OPTION_SINGLE_FLOAT:
14632 file_mips_opts.single_float = 1;
14633 break;
14634
14635 case OPTION_DOUBLE_FLOAT:
14636 file_mips_opts.single_float = 0;
14637 break;
14638
14639 case OPTION_SOFT_FLOAT:
14640 file_mips_opts.soft_float = 1;
14641 break;
14642
14643 case OPTION_HARD_FLOAT:
14644 file_mips_opts.soft_float = 0;
14645 break;
14646
14647 case OPTION_MABI:
14648 if (strcmp (arg, "32") == 0)
14649 mips_abi = O32_ABI;
14650 else if (strcmp (arg, "o64") == 0)
14651 mips_abi = O64_ABI;
14652 else if (strcmp (arg, "n32") == 0)
14653 mips_abi = N32_ABI;
14654 else if (strcmp (arg, "64") == 0)
14655 {
14656 mips_abi = N64_ABI;
14657 if (! support_64bit_objects())
14658 as_fatal (_("no compiled in support for 64 bit object file "
14659 "format"));
14660 }
14661 else if (strcmp (arg, "eabi") == 0)
14662 mips_abi = EABI_ABI;
14663 else
14664 {
14665 as_fatal (_("invalid abi -mabi=%s"), arg);
14666 return 0;
14667 }
14668 break;
14669
14670 case OPTION_M7000_HILO_FIX:
14671 mips_7000_hilo_fix = TRUE;
14672 break;
14673
14674 case OPTION_MNO_7000_HILO_FIX:
14675 mips_7000_hilo_fix = FALSE;
14676 break;
14677
14678 case OPTION_MDEBUG:
14679 mips_flag_mdebug = TRUE;
14680 break;
14681
14682 case OPTION_NO_MDEBUG:
14683 mips_flag_mdebug = FALSE;
14684 break;
14685
14686 case OPTION_PDR:
14687 mips_flag_pdr = TRUE;
14688 break;
14689
14690 case OPTION_NO_PDR:
14691 mips_flag_pdr = FALSE;
14692 break;
14693
14694 case OPTION_MVXWORKS_PIC:
14695 mips_pic = VXWORKS_PIC;
14696 break;
14697
14698 case OPTION_NAN:
14699 if (strcmp (arg, "2008") == 0)
14700 mips_nan2008 = 1;
14701 else if (strcmp (arg, "legacy") == 0)
14702 mips_nan2008 = 0;
14703 else
14704 {
14705 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
14706 return 0;
14707 }
14708 break;
14709
14710 default:
14711 return 0;
14712 }
14713
14714 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14715
14716 return 1;
14717 }
14718 \f
14719 /* Set up globals to tune for the ISA or processor described by INFO. */
14720
14721 static void
14722 mips_set_tune (const struct mips_cpu_info *info)
14723 {
14724 if (info != 0)
14725 mips_tune = info->cpu;
14726 }
14727
14728
14729 void
14730 mips_after_parse_args (void)
14731 {
14732 const struct mips_cpu_info *arch_info = 0;
14733 const struct mips_cpu_info *tune_info = 0;
14734
14735 /* GP relative stuff not working for PE */
14736 if (strncmp (TARGET_OS, "pe", 2) == 0)
14737 {
14738 if (g_switch_seen && g_switch_value != 0)
14739 as_bad (_("-G not supported in this configuration"));
14740 g_switch_value = 0;
14741 }
14742
14743 if (mips_abi == NO_ABI)
14744 mips_abi = MIPS_DEFAULT_ABI;
14745
14746 /* The following code determines the architecture.
14747 Similar code was added to GCC 3.3 (see override_options() in
14748 config/mips/mips.c). The GAS and GCC code should be kept in sync
14749 as much as possible. */
14750
14751 if (mips_arch_string != 0)
14752 arch_info = mips_parse_cpu ("-march", mips_arch_string);
14753
14754 if (file_mips_opts.isa != ISA_UNKNOWN)
14755 {
14756 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
14757 ISA level specified by -mipsN, while arch_info->isa contains
14758 the -march selection (if any). */
14759 if (arch_info != 0)
14760 {
14761 /* -march takes precedence over -mipsN, since it is more descriptive.
14762 There's no harm in specifying both as long as the ISA levels
14763 are the same. */
14764 if (file_mips_opts.isa != arch_info->isa)
14765 as_bad (_("-%s conflicts with the other architecture options,"
14766 " which imply -%s"),
14767 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
14768 mips_cpu_info_from_isa (arch_info->isa)->name);
14769 }
14770 else
14771 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
14772 }
14773
14774 if (arch_info == 0)
14775 {
14776 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
14777 gas_assert (arch_info);
14778 }
14779
14780 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
14781 as_bad (_("-march=%s is not compatible with the selected ABI"),
14782 arch_info->name);
14783
14784 file_mips_opts.arch = arch_info->cpu;
14785 file_mips_opts.isa = arch_info->isa;
14786
14787 /* Set up initial mips_opts state. */
14788 mips_opts = file_mips_opts;
14789
14790 /* The register size inference code is now placed in
14791 file_mips_check_options. */
14792
14793 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
14794 processor. */
14795 if (mips_tune_string != 0)
14796 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
14797
14798 if (tune_info == 0)
14799 mips_set_tune (arch_info);
14800 else
14801 mips_set_tune (tune_info);
14802
14803 if (mips_flag_mdebug < 0)
14804 mips_flag_mdebug = 0;
14805 }
14806 \f
14807 void
14808 mips_init_after_args (void)
14809 {
14810 /* initialize opcodes */
14811 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
14812 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
14813 }
14814
14815 long
14816 md_pcrel_from (fixS *fixP)
14817 {
14818 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14819 switch (fixP->fx_r_type)
14820 {
14821 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14822 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14823 /* Return the address of the delay slot. */
14824 return addr + 2;
14825
14826 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14827 case BFD_RELOC_MICROMIPS_JMP:
14828 case BFD_RELOC_MIPS16_16_PCREL_S1:
14829 case BFD_RELOC_16_PCREL_S2:
14830 case BFD_RELOC_MIPS_21_PCREL_S2:
14831 case BFD_RELOC_MIPS_26_PCREL_S2:
14832 case BFD_RELOC_MIPS_JMP:
14833 /* Return the address of the delay slot. */
14834 return addr + 4;
14835
14836 case BFD_RELOC_MIPS_18_PCREL_S3:
14837 /* Return the aligned address of the doubleword containing
14838 the instruction. */
14839 return addr & ~7;
14840
14841 default:
14842 return addr;
14843 }
14844 }
14845
14846 /* This is called before the symbol table is processed. In order to
14847 work with gcc when using mips-tfile, we must keep all local labels.
14848 However, in other cases, we want to discard them. If we were
14849 called with -g, but we didn't see any debugging information, it may
14850 mean that gcc is smuggling debugging information through to
14851 mips-tfile, in which case we must generate all local labels. */
14852
14853 void
14854 mips_frob_file_before_adjust (void)
14855 {
14856 #ifndef NO_ECOFF_DEBUGGING
14857 if (ECOFF_DEBUGGING
14858 && mips_debug != 0
14859 && ! ecoff_debugging_seen)
14860 flag_keep_locals = 1;
14861 #endif
14862 }
14863
14864 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
14865 the corresponding LO16 reloc. This is called before md_apply_fix and
14866 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
14867 relocation operators.
14868
14869 For our purposes, a %lo() expression matches a %got() or %hi()
14870 expression if:
14871
14872 (a) it refers to the same symbol; and
14873 (b) the offset applied in the %lo() expression is no lower than
14874 the offset applied in the %got() or %hi().
14875
14876 (b) allows us to cope with code like:
14877
14878 lui $4,%hi(foo)
14879 lh $4,%lo(foo+2)($4)
14880
14881 ...which is legal on RELA targets, and has a well-defined behaviour
14882 if the user knows that adding 2 to "foo" will not induce a carry to
14883 the high 16 bits.
14884
14885 When several %lo()s match a particular %got() or %hi(), we use the
14886 following rules to distinguish them:
14887
14888 (1) %lo()s with smaller offsets are a better match than %lo()s with
14889 higher offsets.
14890
14891 (2) %lo()s with no matching %got() or %hi() are better than those
14892 that already have a matching %got() or %hi().
14893
14894 (3) later %lo()s are better than earlier %lo()s.
14895
14896 These rules are applied in order.
14897
14898 (1) means, among other things, that %lo()s with identical offsets are
14899 chosen if they exist.
14900
14901 (2) means that we won't associate several high-part relocations with
14902 the same low-part relocation unless there's no alternative. Having
14903 several high parts for the same low part is a GNU extension; this rule
14904 allows careful users to avoid it.
14905
14906 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
14907 with the last high-part relocation being at the front of the list.
14908 It therefore makes sense to choose the last matching low-part
14909 relocation, all other things being equal. It's also easier
14910 to code that way. */
14911
14912 void
14913 mips_frob_file (void)
14914 {
14915 struct mips_hi_fixup *l;
14916 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
14917
14918 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
14919 {
14920 segment_info_type *seginfo;
14921 bfd_boolean matched_lo_p;
14922 fixS **hi_pos, **lo_pos, **pos;
14923
14924 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
14925
14926 /* If a GOT16 relocation turns out to be against a global symbol,
14927 there isn't supposed to be a matching LO. Ignore %gots against
14928 constants; we'll report an error for those later. */
14929 if (got16_reloc_p (l->fixp->fx_r_type)
14930 && !(l->fixp->fx_addsy
14931 && pic_need_relax (l->fixp->fx_addsy)))
14932 continue;
14933
14934 /* Check quickly whether the next fixup happens to be a matching %lo. */
14935 if (fixup_has_matching_lo_p (l->fixp))
14936 continue;
14937
14938 seginfo = seg_info (l->seg);
14939
14940 /* Set HI_POS to the position of this relocation in the chain.
14941 Set LO_POS to the position of the chosen low-part relocation.
14942 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
14943 relocation that matches an immediately-preceding high-part
14944 relocation. */
14945 hi_pos = NULL;
14946 lo_pos = NULL;
14947 matched_lo_p = FALSE;
14948 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
14949
14950 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
14951 {
14952 if (*pos == l->fixp)
14953 hi_pos = pos;
14954
14955 if ((*pos)->fx_r_type == looking_for_rtype
14956 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
14957 && (*pos)->fx_offset >= l->fixp->fx_offset
14958 && (lo_pos == NULL
14959 || (*pos)->fx_offset < (*lo_pos)->fx_offset
14960 || (!matched_lo_p
14961 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
14962 lo_pos = pos;
14963
14964 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
14965 && fixup_has_matching_lo_p (*pos));
14966 }
14967
14968 /* If we found a match, remove the high-part relocation from its
14969 current position and insert it before the low-part relocation.
14970 Make the offsets match so that fixup_has_matching_lo_p()
14971 will return true.
14972
14973 We don't warn about unmatched high-part relocations since some
14974 versions of gcc have been known to emit dead "lui ...%hi(...)"
14975 instructions. */
14976 if (lo_pos != NULL)
14977 {
14978 l->fixp->fx_offset = (*lo_pos)->fx_offset;
14979 if (l->fixp->fx_next != *lo_pos)
14980 {
14981 *hi_pos = l->fixp->fx_next;
14982 l->fixp->fx_next = *lo_pos;
14983 *lo_pos = l->fixp;
14984 }
14985 }
14986 }
14987 }
14988
14989 int
14990 mips_force_relocation (fixS *fixp)
14991 {
14992 if (generic_force_reloc (fixp))
14993 return 1;
14994
14995 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
14996 so that the linker relaxation can update targets. */
14997 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
14998 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
14999 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15000 return 1;
15001
15002 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15003 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15004 microMIPS symbols so that we can do cross-mode branch diagnostics
15005 and BAL to JALX conversion by the linker. */
15006 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15007 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15008 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15009 && fixp->fx_addsy
15010 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15011 return 1;
15012
15013 /* We want all PC-relative relocations to be kept for R6 relaxation. */
15014 if (ISA_IS_R6 (file_mips_opts.isa)
15015 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15016 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15017 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15018 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15019 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15020 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15021 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15022 return 1;
15023
15024 return 0;
15025 }
15026
15027 /* Implement TC_FORCE_RELOCATION_ABS. */
15028
15029 bfd_boolean
15030 mips_force_relocation_abs (fixS *fixp)
15031 {
15032 if (generic_force_reloc (fixp))
15033 return TRUE;
15034
15035 /* These relocations do not have enough bits in the in-place addend
15036 to hold an arbitrary absolute section's offset. */
15037 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15038 return TRUE;
15039
15040 return FALSE;
15041 }
15042
15043 /* Read the instruction associated with RELOC from BUF. */
15044
15045 static unsigned int
15046 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15047 {
15048 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15049 return read_compressed_insn (buf, 4);
15050 else
15051 return read_insn (buf);
15052 }
15053
15054 /* Write instruction INSN to BUF, given that it has been relocated
15055 by RELOC. */
15056
15057 static void
15058 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15059 unsigned long insn)
15060 {
15061 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15062 write_compressed_insn (buf, insn, 4);
15063 else
15064 write_insn (buf, insn);
15065 }
15066
15067 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15068 to a symbol in another ISA mode, which cannot be converted to JALX. */
15069
15070 static bfd_boolean
15071 fix_bad_cross_mode_jump_p (fixS *fixP)
15072 {
15073 unsigned long opcode;
15074 int other;
15075 char *buf;
15076
15077 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15078 return FALSE;
15079
15080 other = S_GET_OTHER (fixP->fx_addsy);
15081 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15082 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15083 switch (fixP->fx_r_type)
15084 {
15085 case BFD_RELOC_MIPS_JMP:
15086 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15087 case BFD_RELOC_MICROMIPS_JMP:
15088 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15089 default:
15090 return FALSE;
15091 }
15092 }
15093
15094 /* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15095 jump to a symbol in the same ISA mode. */
15096
15097 static bfd_boolean
15098 fix_bad_same_mode_jalx_p (fixS *fixP)
15099 {
15100 unsigned long opcode;
15101 int other;
15102 char *buf;
15103
15104 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15105 return FALSE;
15106
15107 other = S_GET_OTHER (fixP->fx_addsy);
15108 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15109 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15110 switch (fixP->fx_r_type)
15111 {
15112 case BFD_RELOC_MIPS_JMP:
15113 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15114 case BFD_RELOC_MIPS16_JMP:
15115 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15116 case BFD_RELOC_MICROMIPS_JMP:
15117 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15118 default:
15119 return FALSE;
15120 }
15121 }
15122
15123 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15124 to a symbol whose value plus addend is not aligned according to the
15125 ultimate (after linker relaxation) jump instruction's immediate field
15126 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15127 regular MIPS code, to (1 << 2). */
15128
15129 static bfd_boolean
15130 fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15131 {
15132 bfd_boolean micro_to_mips_p;
15133 valueT val;
15134 int other;
15135
15136 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15137 return FALSE;
15138
15139 other = S_GET_OTHER (fixP->fx_addsy);
15140 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15141 val += fixP->fx_offset;
15142 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15143 && !ELF_ST_IS_MICROMIPS (other));
15144 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15145 != ELF_ST_IS_COMPRESSED (other));
15146 }
15147
15148 /* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15149 to a symbol whose annotation indicates another ISA mode. For absolute
15150 symbols check the ISA bit instead.
15151
15152 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15153 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15154 MIPS symbols and associated with BAL instructions as these instructions
15155 may be be converted to JALX by the linker. */
15156
15157 static bfd_boolean
15158 fix_bad_cross_mode_branch_p (fixS *fixP)
15159 {
15160 bfd_boolean absolute_p;
15161 unsigned long opcode;
15162 asection *symsec;
15163 valueT val;
15164 int other;
15165 char *buf;
15166
15167 if (mips_ignore_branch_isa)
15168 return FALSE;
15169
15170 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15171 return FALSE;
15172
15173 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15174 absolute_p = bfd_is_abs_section (symsec);
15175
15176 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15177 other = S_GET_OTHER (fixP->fx_addsy);
15178
15179 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15180 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15181 switch (fixP->fx_r_type)
15182 {
15183 case BFD_RELOC_16_PCREL_S2:
15184 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15185 && opcode != 0x0411);
15186 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15187 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15188 && opcode != 0x4060);
15189 case BFD_RELOC_MIPS_21_PCREL_S2:
15190 case BFD_RELOC_MIPS_26_PCREL_S2:
15191 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15192 case BFD_RELOC_MIPS16_16_PCREL_S1:
15193 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15194 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15195 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15196 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15197 default:
15198 abort ();
15199 }
15200 }
15201
15202 /* Return TRUE if the symbol plus addend associated with a regular MIPS
15203 branch instruction pointed to by FIXP is not aligned according to the
15204 branch instruction's immediate field requirement. We need the addend
15205 to preserve the ISA bit and also the sum must not have bit 2 set. We
15206 must explicitly OR in the ISA bit from symbol annotation as the bit
15207 won't be set in the symbol's value then. */
15208
15209 static bfd_boolean
15210 fix_bad_misaligned_branch_p (fixS *fixP)
15211 {
15212 bfd_boolean absolute_p;
15213 asection *symsec;
15214 valueT isa_bit;
15215 valueT val;
15216 valueT off;
15217 int other;
15218
15219 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15220 return FALSE;
15221
15222 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15223 absolute_p = bfd_is_abs_section (symsec);
15224
15225 val = S_GET_VALUE (fixP->fx_addsy);
15226 other = S_GET_OTHER (fixP->fx_addsy);
15227 off = fixP->fx_offset;
15228
15229 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15230 val |= ELF_ST_IS_COMPRESSED (other);
15231 val += off;
15232 return (val & 0x3) != isa_bit;
15233 }
15234
15235 /* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15236 and its calculated value VAL. */
15237
15238 static void
15239 fix_validate_branch (fixS *fixP, valueT val)
15240 {
15241 if (fixP->fx_done && (val & 0x3) != 0)
15242 as_bad_where (fixP->fx_file, fixP->fx_line,
15243 _("branch to misaligned address (0x%lx)"),
15244 (long) (val + md_pcrel_from (fixP)));
15245 else if (fix_bad_cross_mode_branch_p (fixP))
15246 as_bad_where (fixP->fx_file, fixP->fx_line,
15247 _("branch to a symbol in another ISA mode"));
15248 else if (fix_bad_misaligned_branch_p (fixP))
15249 as_bad_where (fixP->fx_file, fixP->fx_line,
15250 _("branch to misaligned address (0x%lx)"),
15251 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15252 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15253 as_bad_where (fixP->fx_file, fixP->fx_line,
15254 _("cannot encode misaligned addend "
15255 "in the relocatable field (0x%lx)"),
15256 (long) fixP->fx_offset);
15257 }
15258
15259 /* Apply a fixup to the object file. */
15260
15261 void
15262 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15263 {
15264 char *buf;
15265 unsigned long insn;
15266 reloc_howto_type *howto;
15267
15268 if (fixP->fx_pcrel)
15269 switch (fixP->fx_r_type)
15270 {
15271 case BFD_RELOC_16_PCREL_S2:
15272 case BFD_RELOC_MIPS16_16_PCREL_S1:
15273 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15274 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15275 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15276 case BFD_RELOC_32_PCREL:
15277 case BFD_RELOC_MIPS_21_PCREL_S2:
15278 case BFD_RELOC_MIPS_26_PCREL_S2:
15279 case BFD_RELOC_MIPS_18_PCREL_S3:
15280 case BFD_RELOC_MIPS_19_PCREL_S2:
15281 case BFD_RELOC_HI16_S_PCREL:
15282 case BFD_RELOC_LO16_PCREL:
15283 break;
15284
15285 case BFD_RELOC_32:
15286 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15287 break;
15288
15289 default:
15290 as_bad_where (fixP->fx_file, fixP->fx_line,
15291 _("PC-relative reference to a different section"));
15292 break;
15293 }
15294
15295 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15296 that have no MIPS ELF equivalent. */
15297 if (fixP->fx_r_type != BFD_RELOC_8)
15298 {
15299 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15300 if (!howto)
15301 return;
15302 }
15303
15304 gas_assert (fixP->fx_size == 2
15305 || fixP->fx_size == 4
15306 || fixP->fx_r_type == BFD_RELOC_8
15307 || fixP->fx_r_type == BFD_RELOC_16
15308 || fixP->fx_r_type == BFD_RELOC_64
15309 || fixP->fx_r_type == BFD_RELOC_CTOR
15310 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15311 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15312 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15313 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15314 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15315 || fixP->fx_r_type == BFD_RELOC_NONE);
15316
15317 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15318
15319 /* Don't treat parts of a composite relocation as done. There are two
15320 reasons for this:
15321
15322 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15323 should nevertheless be emitted if the first part is.
15324
15325 (2) In normal usage, composite relocations are never assembly-time
15326 constants. The easiest way of dealing with the pathological
15327 exceptions is to generate a relocation against STN_UNDEF and
15328 leave everything up to the linker. */
15329 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15330 fixP->fx_done = 1;
15331
15332 switch (fixP->fx_r_type)
15333 {
15334 case BFD_RELOC_MIPS_TLS_GD:
15335 case BFD_RELOC_MIPS_TLS_LDM:
15336 case BFD_RELOC_MIPS_TLS_DTPREL32:
15337 case BFD_RELOC_MIPS_TLS_DTPREL64:
15338 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15339 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15340 case BFD_RELOC_MIPS_TLS_GOTTPREL:
15341 case BFD_RELOC_MIPS_TLS_TPREL32:
15342 case BFD_RELOC_MIPS_TLS_TPREL64:
15343 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15344 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15345 case BFD_RELOC_MICROMIPS_TLS_GD:
15346 case BFD_RELOC_MICROMIPS_TLS_LDM:
15347 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15348 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15349 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15350 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15351 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15352 case BFD_RELOC_MIPS16_TLS_GD:
15353 case BFD_RELOC_MIPS16_TLS_LDM:
15354 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15355 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15356 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15357 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15358 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15359 if (fixP->fx_addsy)
15360 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15361 else
15362 as_bad_where (fixP->fx_file, fixP->fx_line,
15363 _("TLS relocation against a constant"));
15364 break;
15365
15366 case BFD_RELOC_MIPS_JMP:
15367 case BFD_RELOC_MIPS16_JMP:
15368 case BFD_RELOC_MICROMIPS_JMP:
15369 {
15370 int shift;
15371
15372 gas_assert (!fixP->fx_done);
15373
15374 /* Shift is 2, unusually, for microMIPS JALX. */
15375 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15376 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15377 shift = 1;
15378 else
15379 shift = 2;
15380
15381 if (fix_bad_cross_mode_jump_p (fixP))
15382 as_bad_where (fixP->fx_file, fixP->fx_line,
15383 _("jump to a symbol in another ISA mode"));
15384 else if (fix_bad_same_mode_jalx_p (fixP))
15385 as_bad_where (fixP->fx_file, fixP->fx_line,
15386 _("JALX to a symbol in the same ISA mode"));
15387 else if (fix_bad_misaligned_jump_p (fixP, shift))
15388 as_bad_where (fixP->fx_file, fixP->fx_line,
15389 _("jump to misaligned address (0x%lx)"),
15390 (long) (S_GET_VALUE (fixP->fx_addsy)
15391 + fixP->fx_offset));
15392 else if (HAVE_IN_PLACE_ADDENDS
15393 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15394 as_bad_where (fixP->fx_file, fixP->fx_line,
15395 _("cannot encode misaligned addend "
15396 "in the relocatable field (0x%lx)"),
15397 (long) fixP->fx_offset);
15398 }
15399 /* Fall through. */
15400
15401 case BFD_RELOC_MIPS_SHIFT5:
15402 case BFD_RELOC_MIPS_SHIFT6:
15403 case BFD_RELOC_MIPS_GOT_DISP:
15404 case BFD_RELOC_MIPS_GOT_PAGE:
15405 case BFD_RELOC_MIPS_GOT_OFST:
15406 case BFD_RELOC_MIPS_SUB:
15407 case BFD_RELOC_MIPS_INSERT_A:
15408 case BFD_RELOC_MIPS_INSERT_B:
15409 case BFD_RELOC_MIPS_DELETE:
15410 case BFD_RELOC_MIPS_HIGHEST:
15411 case BFD_RELOC_MIPS_HIGHER:
15412 case BFD_RELOC_MIPS_SCN_DISP:
15413 case BFD_RELOC_MIPS_REL16:
15414 case BFD_RELOC_MIPS_RELGOT:
15415 case BFD_RELOC_MIPS_JALR:
15416 case BFD_RELOC_HI16:
15417 case BFD_RELOC_HI16_S:
15418 case BFD_RELOC_LO16:
15419 case BFD_RELOC_GPREL16:
15420 case BFD_RELOC_MIPS_LITERAL:
15421 case BFD_RELOC_MIPS_CALL16:
15422 case BFD_RELOC_MIPS_GOT16:
15423 case BFD_RELOC_GPREL32:
15424 case BFD_RELOC_MIPS_GOT_HI16:
15425 case BFD_RELOC_MIPS_GOT_LO16:
15426 case BFD_RELOC_MIPS_CALL_HI16:
15427 case BFD_RELOC_MIPS_CALL_LO16:
15428 case BFD_RELOC_HI16_S_PCREL:
15429 case BFD_RELOC_LO16_PCREL:
15430 case BFD_RELOC_MIPS16_GPREL:
15431 case BFD_RELOC_MIPS16_GOT16:
15432 case BFD_RELOC_MIPS16_CALL16:
15433 case BFD_RELOC_MIPS16_HI16:
15434 case BFD_RELOC_MIPS16_HI16_S:
15435 case BFD_RELOC_MIPS16_LO16:
15436 case BFD_RELOC_MICROMIPS_GOT_DISP:
15437 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15438 case BFD_RELOC_MICROMIPS_GOT_OFST:
15439 case BFD_RELOC_MICROMIPS_SUB:
15440 case BFD_RELOC_MICROMIPS_HIGHEST:
15441 case BFD_RELOC_MICROMIPS_HIGHER:
15442 case BFD_RELOC_MICROMIPS_SCN_DISP:
15443 case BFD_RELOC_MICROMIPS_JALR:
15444 case BFD_RELOC_MICROMIPS_HI16:
15445 case BFD_RELOC_MICROMIPS_HI16_S:
15446 case BFD_RELOC_MICROMIPS_LO16:
15447 case BFD_RELOC_MICROMIPS_GPREL16:
15448 case BFD_RELOC_MICROMIPS_LITERAL:
15449 case BFD_RELOC_MICROMIPS_CALL16:
15450 case BFD_RELOC_MICROMIPS_GOT16:
15451 case BFD_RELOC_MICROMIPS_GOT_HI16:
15452 case BFD_RELOC_MICROMIPS_GOT_LO16:
15453 case BFD_RELOC_MICROMIPS_CALL_HI16:
15454 case BFD_RELOC_MICROMIPS_CALL_LO16:
15455 case BFD_RELOC_MIPS_EH:
15456 if (fixP->fx_done)
15457 {
15458 offsetT value;
15459
15460 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15461 {
15462 insn = read_reloc_insn (buf, fixP->fx_r_type);
15463 if (mips16_reloc_p (fixP->fx_r_type))
15464 insn |= mips16_immed_extend (value, 16);
15465 else
15466 insn |= (value & 0xffff);
15467 write_reloc_insn (buf, fixP->fx_r_type, insn);
15468 }
15469 else
15470 as_bad_where (fixP->fx_file, fixP->fx_line,
15471 _("unsupported constant in relocation"));
15472 }
15473 break;
15474
15475 case BFD_RELOC_64:
15476 /* This is handled like BFD_RELOC_32, but we output a sign
15477 extended value if we are only 32 bits. */
15478 if (fixP->fx_done)
15479 {
15480 if (8 <= sizeof (valueT))
15481 md_number_to_chars (buf, *valP, 8);
15482 else
15483 {
15484 valueT hiv;
15485
15486 if ((*valP & 0x80000000) != 0)
15487 hiv = 0xffffffff;
15488 else
15489 hiv = 0;
15490 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15491 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
15492 }
15493 }
15494 break;
15495
15496 case BFD_RELOC_RVA:
15497 case BFD_RELOC_32:
15498 case BFD_RELOC_32_PCREL:
15499 case BFD_RELOC_16:
15500 case BFD_RELOC_8:
15501 /* If we are deleting this reloc entry, we must fill in the
15502 value now. This can happen if we have a .word which is not
15503 resolved when it appears but is later defined. */
15504 if (fixP->fx_done)
15505 md_number_to_chars (buf, *valP, fixP->fx_size);
15506 break;
15507
15508 case BFD_RELOC_MIPS_21_PCREL_S2:
15509 fix_validate_branch (fixP, *valP);
15510 if (!fixP->fx_done)
15511 break;
15512
15513 if (*valP + 0x400000 <= 0x7fffff)
15514 {
15515 insn = read_insn (buf);
15516 insn |= (*valP >> 2) & 0x1fffff;
15517 write_insn (buf, insn);
15518 }
15519 else
15520 as_bad_where (fixP->fx_file, fixP->fx_line,
15521 _("branch out of range"));
15522 break;
15523
15524 case BFD_RELOC_MIPS_26_PCREL_S2:
15525 fix_validate_branch (fixP, *valP);
15526 if (!fixP->fx_done)
15527 break;
15528
15529 if (*valP + 0x8000000 <= 0xfffffff)
15530 {
15531 insn = read_insn (buf);
15532 insn |= (*valP >> 2) & 0x3ffffff;
15533 write_insn (buf, insn);
15534 }
15535 else
15536 as_bad_where (fixP->fx_file, fixP->fx_line,
15537 _("branch out of range"));
15538 break;
15539
15540 case BFD_RELOC_MIPS_18_PCREL_S3:
15541 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
15542 as_bad_where (fixP->fx_file, fixP->fx_line,
15543 _("PC-relative access using misaligned symbol (%lx)"),
15544 (long) S_GET_VALUE (fixP->fx_addsy));
15545 if ((fixP->fx_offset & 0x7) != 0)
15546 as_bad_where (fixP->fx_file, fixP->fx_line,
15547 _("PC-relative access using misaligned offset (%lx)"),
15548 (long) fixP->fx_offset);
15549 if (!fixP->fx_done)
15550 break;
15551
15552 if (*valP + 0x100000 <= 0x1fffff)
15553 {
15554 insn = read_insn (buf);
15555 insn |= (*valP >> 3) & 0x3ffff;
15556 write_insn (buf, insn);
15557 }
15558 else
15559 as_bad_where (fixP->fx_file, fixP->fx_line,
15560 _("PC-relative access out of range"));
15561 break;
15562
15563 case BFD_RELOC_MIPS_19_PCREL_S2:
15564 if ((*valP & 0x3) != 0)
15565 as_bad_where (fixP->fx_file, fixP->fx_line,
15566 _("PC-relative access to misaligned address (%lx)"),
15567 (long) *valP);
15568 if (!fixP->fx_done)
15569 break;
15570
15571 if (*valP + 0x100000 <= 0x1fffff)
15572 {
15573 insn = read_insn (buf);
15574 insn |= (*valP >> 2) & 0x7ffff;
15575 write_insn (buf, insn);
15576 }
15577 else
15578 as_bad_where (fixP->fx_file, fixP->fx_line,
15579 _("PC-relative access out of range"));
15580 break;
15581
15582 case BFD_RELOC_16_PCREL_S2:
15583 fix_validate_branch (fixP, *valP);
15584
15585 /* We need to save the bits in the instruction since fixup_segment()
15586 might be deleting the relocation entry (i.e., a branch within
15587 the current segment). */
15588 if (! fixP->fx_done)
15589 break;
15590
15591 /* Update old instruction data. */
15592 insn = read_insn (buf);
15593
15594 if (*valP + 0x20000 <= 0x3ffff)
15595 {
15596 insn |= (*valP >> 2) & 0xffff;
15597 write_insn (buf, insn);
15598 }
15599 else if (fixP->fx_tcbit2
15600 && fixP->fx_done
15601 && fixP->fx_frag->fr_address >= text_section->vma
15602 && (fixP->fx_frag->fr_address
15603 < text_section->vma + bfd_get_section_size (text_section))
15604 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15605 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15606 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15607 {
15608 /* The branch offset is too large. If this is an
15609 unconditional branch, and we are not generating PIC code,
15610 we can convert it to an absolute jump instruction. */
15611 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15612 insn = 0x0c000000; /* jal */
15613 else
15614 insn = 0x08000000; /* j */
15615 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15616 fixP->fx_done = 0;
15617 fixP->fx_addsy = section_symbol (text_section);
15618 *valP += md_pcrel_from (fixP);
15619 write_insn (buf, insn);
15620 }
15621 else
15622 {
15623 /* If we got here, we have branch-relaxation disabled,
15624 and there's nothing we can do to fix this instruction
15625 without turning it into a longer sequence. */
15626 as_bad_where (fixP->fx_file, fixP->fx_line,
15627 _("branch out of range"));
15628 }
15629 break;
15630
15631 case BFD_RELOC_MIPS16_16_PCREL_S1:
15632 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15633 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15634 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15635 gas_assert (!fixP->fx_done);
15636 if (fix_bad_cross_mode_branch_p (fixP))
15637 as_bad_where (fixP->fx_file, fixP->fx_line,
15638 _("branch to a symbol in another ISA mode"));
15639 else if (fixP->fx_addsy
15640 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15641 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15642 && (fixP->fx_offset & 0x1) != 0)
15643 as_bad_where (fixP->fx_file, fixP->fx_line,
15644 _("branch to misaligned address (0x%lx)"),
15645 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15646 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15647 as_bad_where (fixP->fx_file, fixP->fx_line,
15648 _("cannot encode misaligned addend "
15649 "in the relocatable field (0x%lx)"),
15650 (long) fixP->fx_offset);
15651 break;
15652
15653 case BFD_RELOC_VTABLE_INHERIT:
15654 fixP->fx_done = 0;
15655 if (fixP->fx_addsy
15656 && !S_IS_DEFINED (fixP->fx_addsy)
15657 && !S_IS_WEAK (fixP->fx_addsy))
15658 S_SET_WEAK (fixP->fx_addsy);
15659 break;
15660
15661 case BFD_RELOC_NONE:
15662 case BFD_RELOC_VTABLE_ENTRY:
15663 fixP->fx_done = 0;
15664 break;
15665
15666 default:
15667 abort ();
15668 }
15669
15670 /* Remember value for tc_gen_reloc. */
15671 fixP->fx_addnumber = *valP;
15672 }
15673
15674 static symbolS *
15675 get_symbol (void)
15676 {
15677 int c;
15678 char *name;
15679 symbolS *p;
15680
15681 c = get_symbol_name (&name);
15682 p = (symbolS *) symbol_find_or_make (name);
15683 (void) restore_line_pointer (c);
15684 return p;
15685 }
15686
15687 /* Align the current frag to a given power of two. If a particular
15688 fill byte should be used, FILL points to an integer that contains
15689 that byte, otherwise FILL is null.
15690
15691 This function used to have the comment:
15692
15693 The MIPS assembler also automatically adjusts any preceding label.
15694
15695 The implementation therefore applied the adjustment to a maximum of
15696 one label. However, other label adjustments are applied to batches
15697 of labels, and adjusting just one caused problems when new labels
15698 were added for the sake of debugging or unwind information.
15699 We therefore adjust all preceding labels (given as LABELS) instead. */
15700
15701 static void
15702 mips_align (int to, int *fill, struct insn_label_list *labels)
15703 {
15704 mips_emit_delays ();
15705 mips_record_compressed_mode ();
15706 if (fill == NULL && subseg_text_p (now_seg))
15707 frag_align_code (to, 0);
15708 else
15709 frag_align (to, fill ? *fill : 0, 0);
15710 record_alignment (now_seg, to);
15711 mips_move_labels (labels, FALSE);
15712 }
15713
15714 /* Align to a given power of two. .align 0 turns off the automatic
15715 alignment used by the data creating pseudo-ops. */
15716
15717 static void
15718 s_align (int x ATTRIBUTE_UNUSED)
15719 {
15720 int temp, fill_value, *fill_ptr;
15721 long max_alignment = 28;
15722
15723 /* o Note that the assembler pulls down any immediately preceding label
15724 to the aligned address.
15725 o It's not documented but auto alignment is reinstated by
15726 a .align pseudo instruction.
15727 o Note also that after auto alignment is turned off the mips assembler
15728 issues an error on attempt to assemble an improperly aligned data item.
15729 We don't. */
15730
15731 temp = get_absolute_expression ();
15732 if (temp > max_alignment)
15733 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
15734 else if (temp < 0)
15735 {
15736 as_warn (_("alignment negative, 0 assumed"));
15737 temp = 0;
15738 }
15739 if (*input_line_pointer == ',')
15740 {
15741 ++input_line_pointer;
15742 fill_value = get_absolute_expression ();
15743 fill_ptr = &fill_value;
15744 }
15745 else
15746 fill_ptr = 0;
15747 if (temp)
15748 {
15749 segment_info_type *si = seg_info (now_seg);
15750 struct insn_label_list *l = si->label_list;
15751 /* Auto alignment should be switched on by next section change. */
15752 auto_align = 1;
15753 mips_align (temp, fill_ptr, l);
15754 }
15755 else
15756 {
15757 auto_align = 0;
15758 }
15759
15760 demand_empty_rest_of_line ();
15761 }
15762
15763 static void
15764 s_change_sec (int sec)
15765 {
15766 segT seg;
15767
15768 /* The ELF backend needs to know that we are changing sections, so
15769 that .previous works correctly. We could do something like check
15770 for an obj_section_change_hook macro, but that might be confusing
15771 as it would not be appropriate to use it in the section changing
15772 functions in read.c, since obj-elf.c intercepts those. FIXME:
15773 This should be cleaner, somehow. */
15774 obj_elf_section_change_hook ();
15775
15776 mips_emit_delays ();
15777
15778 switch (sec)
15779 {
15780 case 't':
15781 s_text (0);
15782 break;
15783 case 'd':
15784 s_data (0);
15785 break;
15786 case 'b':
15787 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15788 demand_empty_rest_of_line ();
15789 break;
15790
15791 case 'r':
15792 seg = subseg_new (RDATA_SECTION_NAME,
15793 (subsegT) get_absolute_expression ());
15794 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15795 | SEC_READONLY | SEC_RELOC
15796 | SEC_DATA));
15797 if (strncmp (TARGET_OS, "elf", 3) != 0)
15798 record_alignment (seg, 4);
15799 demand_empty_rest_of_line ();
15800 break;
15801
15802 case 's':
15803 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
15804 bfd_set_section_flags (stdoutput, seg,
15805 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
15806 if (strncmp (TARGET_OS, "elf", 3) != 0)
15807 record_alignment (seg, 4);
15808 demand_empty_rest_of_line ();
15809 break;
15810
15811 case 'B':
15812 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
15813 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15814 if (strncmp (TARGET_OS, "elf", 3) != 0)
15815 record_alignment (seg, 4);
15816 demand_empty_rest_of_line ();
15817 break;
15818 }
15819
15820 auto_align = 1;
15821 }
15822
15823 void
15824 s_change_section (int ignore ATTRIBUTE_UNUSED)
15825 {
15826 char *saved_ilp;
15827 char *section_name;
15828 char c, endc;
15829 char next_c = 0;
15830 int section_type;
15831 int section_flag;
15832 int section_entry_size;
15833 int section_alignment;
15834
15835 saved_ilp = input_line_pointer;
15836 endc = get_symbol_name (&section_name);
15837 c = (endc == '"' ? input_line_pointer[1] : endc);
15838 if (c)
15839 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
15840
15841 /* Do we have .section Name<,"flags">? */
15842 if (c != ',' || (c == ',' && next_c == '"'))
15843 {
15844 /* Just after name is now '\0'. */
15845 (void) restore_line_pointer (endc);
15846 input_line_pointer = saved_ilp;
15847 obj_elf_section (ignore);
15848 return;
15849 }
15850
15851 section_name = xstrdup (section_name);
15852 c = restore_line_pointer (endc);
15853
15854 input_line_pointer++;
15855
15856 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
15857 if (c == ',')
15858 section_type = get_absolute_expression ();
15859 else
15860 section_type = 0;
15861
15862 if (*input_line_pointer++ == ',')
15863 section_flag = get_absolute_expression ();
15864 else
15865 section_flag = 0;
15866
15867 if (*input_line_pointer++ == ',')
15868 section_entry_size = get_absolute_expression ();
15869 else
15870 section_entry_size = 0;
15871
15872 if (*input_line_pointer++ == ',')
15873 section_alignment = get_absolute_expression ();
15874 else
15875 section_alignment = 0;
15876
15877 /* FIXME: really ignore? */
15878 (void) section_alignment;
15879
15880 /* When using the generic form of .section (as implemented by obj-elf.c),
15881 there's no way to set the section type to SHT_MIPS_DWARF. Users have
15882 traditionally had to fall back on the more common @progbits instead.
15883
15884 There's nothing really harmful in this, since bfd will correct
15885 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
15886 means that, for backwards compatibility, the special_section entries
15887 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
15888
15889 Even so, we shouldn't force users of the MIPS .section syntax to
15890 incorrectly label the sections as SHT_PROGBITS. The best compromise
15891 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
15892 generic type-checking code. */
15893 if (section_type == SHT_MIPS_DWARF)
15894 section_type = SHT_PROGBITS;
15895
15896 obj_elf_change_section (section_name, section_type, 0, section_flag,
15897 section_entry_size, 0, 0, 0);
15898
15899 if (now_seg->name != section_name)
15900 free (section_name);
15901 }
15902
15903 void
15904 mips_enable_auto_align (void)
15905 {
15906 auto_align = 1;
15907 }
15908
15909 static void
15910 s_cons (int log_size)
15911 {
15912 segment_info_type *si = seg_info (now_seg);
15913 struct insn_label_list *l = si->label_list;
15914
15915 mips_emit_delays ();
15916 if (log_size > 0 && auto_align)
15917 mips_align (log_size, 0, l);
15918 cons (1 << log_size);
15919 mips_clear_insn_labels ();
15920 }
15921
15922 static void
15923 s_float_cons (int type)
15924 {
15925 segment_info_type *si = seg_info (now_seg);
15926 struct insn_label_list *l = si->label_list;
15927
15928 mips_emit_delays ();
15929
15930 if (auto_align)
15931 {
15932 if (type == 'd')
15933 mips_align (3, 0, l);
15934 else
15935 mips_align (2, 0, l);
15936 }
15937
15938 float_cons (type);
15939 mips_clear_insn_labels ();
15940 }
15941
15942 /* Handle .globl. We need to override it because on Irix 5 you are
15943 permitted to say
15944 .globl foo .text
15945 where foo is an undefined symbol, to mean that foo should be
15946 considered to be the address of a function. */
15947
15948 static void
15949 s_mips_globl (int x ATTRIBUTE_UNUSED)
15950 {
15951 char *name;
15952 int c;
15953 symbolS *symbolP;
15954 flagword flag;
15955
15956 do
15957 {
15958 c = get_symbol_name (&name);
15959 symbolP = symbol_find_or_make (name);
15960 S_SET_EXTERNAL (symbolP);
15961
15962 *input_line_pointer = c;
15963 SKIP_WHITESPACE_AFTER_NAME ();
15964
15965 /* On Irix 5, every global symbol that is not explicitly labelled as
15966 being a function is apparently labelled as being an object. */
15967 flag = BSF_OBJECT;
15968
15969 if (!is_end_of_line[(unsigned char) *input_line_pointer]
15970 && (*input_line_pointer != ','))
15971 {
15972 char *secname;
15973 asection *sec;
15974
15975 c = get_symbol_name (&secname);
15976 sec = bfd_get_section_by_name (stdoutput, secname);
15977 if (sec == NULL)
15978 as_bad (_("%s: no such section"), secname);
15979 (void) restore_line_pointer (c);
15980
15981 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
15982 flag = BSF_FUNCTION;
15983 }
15984
15985 symbol_get_bfdsym (symbolP)->flags |= flag;
15986
15987 c = *input_line_pointer;
15988 if (c == ',')
15989 {
15990 input_line_pointer++;
15991 SKIP_WHITESPACE ();
15992 if (is_end_of_line[(unsigned char) *input_line_pointer])
15993 c = '\n';
15994 }
15995 }
15996 while (c == ',');
15997
15998 demand_empty_rest_of_line ();
15999 }
16000
16001 static void
16002 s_option (int x ATTRIBUTE_UNUSED)
16003 {
16004 char *opt;
16005 char c;
16006
16007 c = get_symbol_name (&opt);
16008
16009 if (*opt == 'O')
16010 {
16011 /* FIXME: What does this mean? */
16012 }
16013 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
16014 {
16015 int i;
16016
16017 i = atoi (opt + 3);
16018 if (i != 0 && i != 2)
16019 as_bad (_(".option pic%d not supported"), i);
16020 else if (mips_pic == VXWORKS_PIC)
16021 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16022 else if (i == 0)
16023 mips_pic = NO_PIC;
16024 else if (i == 2)
16025 {
16026 mips_pic = SVR4_PIC;
16027 mips_abicalls = TRUE;
16028 }
16029
16030 if (mips_pic == SVR4_PIC)
16031 {
16032 if (g_switch_seen && g_switch_value != 0)
16033 as_warn (_("-G may not be used with SVR4 PIC code"));
16034 g_switch_value = 0;
16035 bfd_set_gp_size (stdoutput, 0);
16036 }
16037 }
16038 else
16039 as_warn (_("unrecognized option \"%s\""), opt);
16040
16041 (void) restore_line_pointer (c);
16042 demand_empty_rest_of_line ();
16043 }
16044
16045 /* This structure is used to hold a stack of .set values. */
16046
16047 struct mips_option_stack
16048 {
16049 struct mips_option_stack *next;
16050 struct mips_set_options options;
16051 };
16052
16053 static struct mips_option_stack *mips_opts_stack;
16054
16055 /* Return status for .set/.module option handling. */
16056
16057 enum code_option_type
16058 {
16059 /* Unrecognized option. */
16060 OPTION_TYPE_BAD = -1,
16061
16062 /* Ordinary option. */
16063 OPTION_TYPE_NORMAL,
16064
16065 /* ISA changing option. */
16066 OPTION_TYPE_ISA
16067 };
16068
16069 /* Handle common .set/.module options. Return status indicating option
16070 type. */
16071
16072 static enum code_option_type
16073 parse_code_option (char * name)
16074 {
16075 bfd_boolean isa_set = FALSE;
16076 const struct mips_ase *ase;
16077
16078 if (strncmp (name, "at=", 3) == 0)
16079 {
16080 char *s = name + 3;
16081
16082 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16083 as_bad (_("unrecognized register name `%s'"), s);
16084 }
16085 else if (strcmp (name, "at") == 0)
16086 mips_opts.at = ATREG;
16087 else if (strcmp (name, "noat") == 0)
16088 mips_opts.at = ZERO;
16089 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16090 mips_opts.nomove = 0;
16091 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16092 mips_opts.nomove = 1;
16093 else if (strcmp (name, "bopt") == 0)
16094 mips_opts.nobopt = 0;
16095 else if (strcmp (name, "nobopt") == 0)
16096 mips_opts.nobopt = 1;
16097 else if (strcmp (name, "gp=32") == 0)
16098 mips_opts.gp = 32;
16099 else if (strcmp (name, "gp=64") == 0)
16100 mips_opts.gp = 64;
16101 else if (strcmp (name, "fp=32") == 0)
16102 mips_opts.fp = 32;
16103 else if (strcmp (name, "fp=xx") == 0)
16104 mips_opts.fp = 0;
16105 else if (strcmp (name, "fp=64") == 0)
16106 mips_opts.fp = 64;
16107 else if (strcmp (name, "softfloat") == 0)
16108 mips_opts.soft_float = 1;
16109 else if (strcmp (name, "hardfloat") == 0)
16110 mips_opts.soft_float = 0;
16111 else if (strcmp (name, "singlefloat") == 0)
16112 mips_opts.single_float = 1;
16113 else if (strcmp (name, "doublefloat") == 0)
16114 mips_opts.single_float = 0;
16115 else if (strcmp (name, "nooddspreg") == 0)
16116 mips_opts.oddspreg = 0;
16117 else if (strcmp (name, "oddspreg") == 0)
16118 mips_opts.oddspreg = 1;
16119 else if (strcmp (name, "mips16") == 0
16120 || strcmp (name, "MIPS-16") == 0)
16121 mips_opts.mips16 = 1;
16122 else if (strcmp (name, "nomips16") == 0
16123 || strcmp (name, "noMIPS-16") == 0)
16124 mips_opts.mips16 = 0;
16125 else if (strcmp (name, "micromips") == 0)
16126 mips_opts.micromips = 1;
16127 else if (strcmp (name, "nomicromips") == 0)
16128 mips_opts.micromips = 0;
16129 else if (name[0] == 'n'
16130 && name[1] == 'o'
16131 && (ase = mips_lookup_ase (name + 2)))
16132 mips_set_ase (ase, &mips_opts, FALSE);
16133 else if ((ase = mips_lookup_ase (name)))
16134 mips_set_ase (ase, &mips_opts, TRUE);
16135 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16136 {
16137 /* Permit the user to change the ISA and architecture on the fly.
16138 Needless to say, misuse can cause serious problems. */
16139 if (strncmp (name, "arch=", 5) == 0)
16140 {
16141 const struct mips_cpu_info *p;
16142
16143 p = mips_parse_cpu ("internal use", name + 5);
16144 if (!p)
16145 as_bad (_("unknown architecture %s"), name + 5);
16146 else
16147 {
16148 mips_opts.arch = p->cpu;
16149 mips_opts.isa = p->isa;
16150 isa_set = TRUE;
16151 }
16152 }
16153 else if (strncmp (name, "mips", 4) == 0)
16154 {
16155 const struct mips_cpu_info *p;
16156
16157 p = mips_parse_cpu ("internal use", name);
16158 if (!p)
16159 as_bad (_("unknown ISA level %s"), name + 4);
16160 else
16161 {
16162 mips_opts.arch = p->cpu;
16163 mips_opts.isa = p->isa;
16164 isa_set = TRUE;
16165 }
16166 }
16167 else
16168 as_bad (_("unknown ISA or architecture %s"), name);
16169 }
16170 else if (strcmp (name, "autoextend") == 0)
16171 mips_opts.noautoextend = 0;
16172 else if (strcmp (name, "noautoextend") == 0)
16173 mips_opts.noautoextend = 1;
16174 else if (strcmp (name, "insn32") == 0)
16175 mips_opts.insn32 = TRUE;
16176 else if (strcmp (name, "noinsn32") == 0)
16177 mips_opts.insn32 = FALSE;
16178 else if (strcmp (name, "sym32") == 0)
16179 mips_opts.sym32 = TRUE;
16180 else if (strcmp (name, "nosym32") == 0)
16181 mips_opts.sym32 = FALSE;
16182 else
16183 return OPTION_TYPE_BAD;
16184
16185 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
16186 }
16187
16188 /* Handle the .set pseudo-op. */
16189
16190 static void
16191 s_mipsset (int x ATTRIBUTE_UNUSED)
16192 {
16193 enum code_option_type type = OPTION_TYPE_NORMAL;
16194 char *name = input_line_pointer, ch;
16195
16196 file_mips_check_options ();
16197
16198 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16199 ++input_line_pointer;
16200 ch = *input_line_pointer;
16201 *input_line_pointer = '\0';
16202
16203 if (strchr (name, ','))
16204 {
16205 /* Generic ".set" directive; use the generic handler. */
16206 *input_line_pointer = ch;
16207 input_line_pointer = name;
16208 s_set (0);
16209 return;
16210 }
16211
16212 if (strcmp (name, "reorder") == 0)
16213 {
16214 if (mips_opts.noreorder)
16215 end_noreorder ();
16216 }
16217 else if (strcmp (name, "noreorder") == 0)
16218 {
16219 if (!mips_opts.noreorder)
16220 start_noreorder ();
16221 }
16222 else if (strcmp (name, "macro") == 0)
16223 mips_opts.warn_about_macros = 0;
16224 else if (strcmp (name, "nomacro") == 0)
16225 {
16226 if (mips_opts.noreorder == 0)
16227 as_bad (_("`noreorder' must be set before `nomacro'"));
16228 mips_opts.warn_about_macros = 1;
16229 }
16230 else if (strcmp (name, "gp=default") == 0)
16231 mips_opts.gp = file_mips_opts.gp;
16232 else if (strcmp (name, "fp=default") == 0)
16233 mips_opts.fp = file_mips_opts.fp;
16234 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16235 {
16236 mips_opts.isa = file_mips_opts.isa;
16237 mips_opts.arch = file_mips_opts.arch;
16238 mips_opts.gp = file_mips_opts.gp;
16239 mips_opts.fp = file_mips_opts.fp;
16240 }
16241 else if (strcmp (name, "push") == 0)
16242 {
16243 struct mips_option_stack *s;
16244
16245 s = XNEW (struct mips_option_stack);
16246 s->next = mips_opts_stack;
16247 s->options = mips_opts;
16248 mips_opts_stack = s;
16249 }
16250 else if (strcmp (name, "pop") == 0)
16251 {
16252 struct mips_option_stack *s;
16253
16254 s = mips_opts_stack;
16255 if (s == NULL)
16256 as_bad (_(".set pop with no .set push"));
16257 else
16258 {
16259 /* If we're changing the reorder mode we need to handle
16260 delay slots correctly. */
16261 if (s->options.noreorder && ! mips_opts.noreorder)
16262 start_noreorder ();
16263 else if (! s->options.noreorder && mips_opts.noreorder)
16264 end_noreorder ();
16265
16266 mips_opts = s->options;
16267 mips_opts_stack = s->next;
16268 free (s);
16269 }
16270 }
16271 else
16272 {
16273 type = parse_code_option (name);
16274 if (type == OPTION_TYPE_BAD)
16275 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16276 }
16277
16278 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16279 registers based on what is supported by the arch/cpu. */
16280 if (type == OPTION_TYPE_ISA)
16281 {
16282 switch (mips_opts.isa)
16283 {
16284 case 0:
16285 break;
16286 case ISA_MIPS1:
16287 /* MIPS I cannot support FPXX. */
16288 mips_opts.fp = 32;
16289 /* fall-through. */
16290 case ISA_MIPS2:
16291 case ISA_MIPS32:
16292 case ISA_MIPS32R2:
16293 case ISA_MIPS32R3:
16294 case ISA_MIPS32R5:
16295 mips_opts.gp = 32;
16296 if (mips_opts.fp != 0)
16297 mips_opts.fp = 32;
16298 break;
16299 case ISA_MIPS32R6:
16300 mips_opts.gp = 32;
16301 mips_opts.fp = 64;
16302 break;
16303 case ISA_MIPS3:
16304 case ISA_MIPS4:
16305 case ISA_MIPS5:
16306 case ISA_MIPS64:
16307 case ISA_MIPS64R2:
16308 case ISA_MIPS64R3:
16309 case ISA_MIPS64R5:
16310 case ISA_MIPS64R6:
16311 mips_opts.gp = 64;
16312 if (mips_opts.fp != 0)
16313 {
16314 if (mips_opts.arch == CPU_R5900)
16315 mips_opts.fp = 32;
16316 else
16317 mips_opts.fp = 64;
16318 }
16319 break;
16320 default:
16321 as_bad (_("unknown ISA level %s"), name + 4);
16322 break;
16323 }
16324 }
16325
16326 mips_check_options (&mips_opts, FALSE);
16327
16328 mips_check_isa_supports_ases ();
16329 *input_line_pointer = ch;
16330 demand_empty_rest_of_line ();
16331 }
16332
16333 /* Handle the .module pseudo-op. */
16334
16335 static void
16336 s_module (int ignore ATTRIBUTE_UNUSED)
16337 {
16338 char *name = input_line_pointer, ch;
16339
16340 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16341 ++input_line_pointer;
16342 ch = *input_line_pointer;
16343 *input_line_pointer = '\0';
16344
16345 if (!file_mips_opts_checked)
16346 {
16347 if (parse_code_option (name) == OPTION_TYPE_BAD)
16348 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16349
16350 /* Update module level settings from mips_opts. */
16351 file_mips_opts = mips_opts;
16352 }
16353 else
16354 as_bad (_(".module is not permitted after generating code"));
16355
16356 *input_line_pointer = ch;
16357 demand_empty_rest_of_line ();
16358 }
16359
16360 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
16361 .option pic2. It means to generate SVR4 PIC calls. */
16362
16363 static void
16364 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16365 {
16366 mips_pic = SVR4_PIC;
16367 mips_abicalls = TRUE;
16368
16369 if (g_switch_seen && g_switch_value != 0)
16370 as_warn (_("-G may not be used with SVR4 PIC code"));
16371 g_switch_value = 0;
16372
16373 bfd_set_gp_size (stdoutput, 0);
16374 demand_empty_rest_of_line ();
16375 }
16376
16377 /* Handle the .cpload pseudo-op. This is used when generating SVR4
16378 PIC code. It sets the $gp register for the function based on the
16379 function address, which is in the register named in the argument.
16380 This uses a relocation against _gp_disp, which is handled specially
16381 by the linker. The result is:
16382 lui $gp,%hi(_gp_disp)
16383 addiu $gp,$gp,%lo(_gp_disp)
16384 addu $gp,$gp,.cpload argument
16385 The .cpload argument is normally $25 == $t9.
16386
16387 The -mno-shared option changes this to:
16388 lui $gp,%hi(__gnu_local_gp)
16389 addiu $gp,$gp,%lo(__gnu_local_gp)
16390 and the argument is ignored. This saves an instruction, but the
16391 resulting code is not position independent; it uses an absolute
16392 address for __gnu_local_gp. Thus code assembled with -mno-shared
16393 can go into an ordinary executable, but not into a shared library. */
16394
16395 static void
16396 s_cpload (int ignore ATTRIBUTE_UNUSED)
16397 {
16398 expressionS ex;
16399 int reg;
16400 int in_shared;
16401
16402 file_mips_check_options ();
16403
16404 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16405 .cpload is ignored. */
16406 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16407 {
16408 s_ignore (0);
16409 return;
16410 }
16411
16412 if (mips_opts.mips16)
16413 {
16414 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16415 ignore_rest_of_line ();
16416 return;
16417 }
16418
16419 /* .cpload should be in a .set noreorder section. */
16420 if (mips_opts.noreorder == 0)
16421 as_warn (_(".cpload not in noreorder section"));
16422
16423 reg = tc_get_register (0);
16424
16425 /* If we need to produce a 64-bit address, we are better off using
16426 the default instruction sequence. */
16427 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16428
16429 ex.X_op = O_symbol;
16430 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16431 "__gnu_local_gp");
16432 ex.X_op_symbol = NULL;
16433 ex.X_add_number = 0;
16434
16435 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16436 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16437
16438 mips_mark_labels ();
16439 mips_assembling_insn = TRUE;
16440
16441 macro_start ();
16442 macro_build_lui (&ex, mips_gp_register);
16443 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16444 mips_gp_register, BFD_RELOC_LO16);
16445 if (in_shared)
16446 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16447 mips_gp_register, reg);
16448 macro_end ();
16449
16450 mips_assembling_insn = FALSE;
16451 demand_empty_rest_of_line ();
16452 }
16453
16454 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16455 .cpsetup $reg1, offset|$reg2, label
16456
16457 If offset is given, this results in:
16458 sd $gp, offset($sp)
16459 lui $gp, %hi(%neg(%gp_rel(label)))
16460 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16461 daddu $gp, $gp, $reg1
16462
16463 If $reg2 is given, this results in:
16464 or $reg2, $gp, $0
16465 lui $gp, %hi(%neg(%gp_rel(label)))
16466 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16467 daddu $gp, $gp, $reg1
16468 $reg1 is normally $25 == $t9.
16469
16470 The -mno-shared option replaces the last three instructions with
16471 lui $gp,%hi(_gp)
16472 addiu $gp,$gp,%lo(_gp) */
16473
16474 static void
16475 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16476 {
16477 expressionS ex_off;
16478 expressionS ex_sym;
16479 int reg1;
16480
16481 file_mips_check_options ();
16482
16483 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16484 We also need NewABI support. */
16485 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16486 {
16487 s_ignore (0);
16488 return;
16489 }
16490
16491 if (mips_opts.mips16)
16492 {
16493 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16494 ignore_rest_of_line ();
16495 return;
16496 }
16497
16498 reg1 = tc_get_register (0);
16499 SKIP_WHITESPACE ();
16500 if (*input_line_pointer != ',')
16501 {
16502 as_bad (_("missing argument separator ',' for .cpsetup"));
16503 return;
16504 }
16505 else
16506 ++input_line_pointer;
16507 SKIP_WHITESPACE ();
16508 if (*input_line_pointer == '$')
16509 {
16510 mips_cpreturn_register = tc_get_register (0);
16511 mips_cpreturn_offset = -1;
16512 }
16513 else
16514 {
16515 mips_cpreturn_offset = get_absolute_expression ();
16516 mips_cpreturn_register = -1;
16517 }
16518 SKIP_WHITESPACE ();
16519 if (*input_line_pointer != ',')
16520 {
16521 as_bad (_("missing argument separator ',' for .cpsetup"));
16522 return;
16523 }
16524 else
16525 ++input_line_pointer;
16526 SKIP_WHITESPACE ();
16527 expression (&ex_sym);
16528
16529 mips_mark_labels ();
16530 mips_assembling_insn = TRUE;
16531
16532 macro_start ();
16533 if (mips_cpreturn_register == -1)
16534 {
16535 ex_off.X_op = O_constant;
16536 ex_off.X_add_symbol = NULL;
16537 ex_off.X_op_symbol = NULL;
16538 ex_off.X_add_number = mips_cpreturn_offset;
16539
16540 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16541 BFD_RELOC_LO16, SP);
16542 }
16543 else
16544 move_register (mips_cpreturn_register, mips_gp_register);
16545
16546 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16547 {
16548 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16549 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16550 BFD_RELOC_HI16_S);
16551
16552 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16553 mips_gp_register, -1, BFD_RELOC_GPREL16,
16554 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16555
16556 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16557 mips_gp_register, reg1);
16558 }
16559 else
16560 {
16561 expressionS ex;
16562
16563 ex.X_op = O_symbol;
16564 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16565 ex.X_op_symbol = NULL;
16566 ex.X_add_number = 0;
16567
16568 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16569 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16570
16571 macro_build_lui (&ex, mips_gp_register);
16572 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16573 mips_gp_register, BFD_RELOC_LO16);
16574 }
16575
16576 macro_end ();
16577
16578 mips_assembling_insn = FALSE;
16579 demand_empty_rest_of_line ();
16580 }
16581
16582 static void
16583 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16584 {
16585 file_mips_check_options ();
16586
16587 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16588 .cplocal is ignored. */
16589 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16590 {
16591 s_ignore (0);
16592 return;
16593 }
16594
16595 if (mips_opts.mips16)
16596 {
16597 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16598 ignore_rest_of_line ();
16599 return;
16600 }
16601
16602 mips_gp_register = tc_get_register (0);
16603 demand_empty_rest_of_line ();
16604 }
16605
16606 /* Handle the .cprestore pseudo-op. This stores $gp into a given
16607 offset from $sp. The offset is remembered, and after making a PIC
16608 call $gp is restored from that location. */
16609
16610 static void
16611 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16612 {
16613 expressionS ex;
16614
16615 file_mips_check_options ();
16616
16617 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16618 .cprestore is ignored. */
16619 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16620 {
16621 s_ignore (0);
16622 return;
16623 }
16624
16625 if (mips_opts.mips16)
16626 {
16627 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16628 ignore_rest_of_line ();
16629 return;
16630 }
16631
16632 mips_cprestore_offset = get_absolute_expression ();
16633 mips_cprestore_valid = 1;
16634
16635 ex.X_op = O_constant;
16636 ex.X_add_symbol = NULL;
16637 ex.X_op_symbol = NULL;
16638 ex.X_add_number = mips_cprestore_offset;
16639
16640 mips_mark_labels ();
16641 mips_assembling_insn = TRUE;
16642
16643 macro_start ();
16644 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16645 SP, HAVE_64BIT_ADDRESSES);
16646 macro_end ();
16647
16648 mips_assembling_insn = FALSE;
16649 demand_empty_rest_of_line ();
16650 }
16651
16652 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16653 was given in the preceding .cpsetup, it results in:
16654 ld $gp, offset($sp)
16655
16656 If a register $reg2 was given there, it results in:
16657 or $gp, $reg2, $0 */
16658
16659 static void
16660 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16661 {
16662 expressionS ex;
16663
16664 file_mips_check_options ();
16665
16666 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16667 We also need NewABI support. */
16668 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16669 {
16670 s_ignore (0);
16671 return;
16672 }
16673
16674 if (mips_opts.mips16)
16675 {
16676 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16677 ignore_rest_of_line ();
16678 return;
16679 }
16680
16681 mips_mark_labels ();
16682 mips_assembling_insn = TRUE;
16683
16684 macro_start ();
16685 if (mips_cpreturn_register == -1)
16686 {
16687 ex.X_op = O_constant;
16688 ex.X_add_symbol = NULL;
16689 ex.X_op_symbol = NULL;
16690 ex.X_add_number = mips_cpreturn_offset;
16691
16692 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16693 }
16694 else
16695 move_register (mips_gp_register, mips_cpreturn_register);
16696
16697 macro_end ();
16698
16699 mips_assembling_insn = FALSE;
16700 demand_empty_rest_of_line ();
16701 }
16702
16703 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16704 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16705 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16706 debug information or MIPS16 TLS. */
16707
16708 static void
16709 s_tls_rel_directive (const size_t bytes, const char *dirstr,
16710 bfd_reloc_code_real_type rtype)
16711 {
16712 expressionS ex;
16713 char *p;
16714
16715 expression (&ex);
16716
16717 if (ex.X_op != O_symbol)
16718 {
16719 as_bad (_("unsupported use of %s"), dirstr);
16720 ignore_rest_of_line ();
16721 }
16722
16723 p = frag_more (bytes);
16724 md_number_to_chars (p, 0, bytes);
16725 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
16726 demand_empty_rest_of_line ();
16727 mips_clear_insn_labels ();
16728 }
16729
16730 /* Handle .dtprelword. */
16731
16732 static void
16733 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16734 {
16735 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
16736 }
16737
16738 /* Handle .dtpreldword. */
16739
16740 static void
16741 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16742 {
16743 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16744 }
16745
16746 /* Handle .tprelword. */
16747
16748 static void
16749 s_tprelword (int ignore ATTRIBUTE_UNUSED)
16750 {
16751 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16752 }
16753
16754 /* Handle .tpreldword. */
16755
16756 static void
16757 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16758 {
16759 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
16760 }
16761
16762 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16763 code. It sets the offset to use in gp_rel relocations. */
16764
16765 static void
16766 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
16767 {
16768 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16769 We also need NewABI support. */
16770 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16771 {
16772 s_ignore (0);
16773 return;
16774 }
16775
16776 mips_gprel_offset = get_absolute_expression ();
16777
16778 demand_empty_rest_of_line ();
16779 }
16780
16781 /* Handle the .gpword pseudo-op. This is used when generating PIC
16782 code. It generates a 32 bit GP relative reloc. */
16783
16784 static void
16785 s_gpword (int ignore ATTRIBUTE_UNUSED)
16786 {
16787 segment_info_type *si;
16788 struct insn_label_list *l;
16789 expressionS ex;
16790 char *p;
16791
16792 /* When not generating PIC code, this is treated as .word. */
16793 if (mips_pic != SVR4_PIC)
16794 {
16795 s_cons (2);
16796 return;
16797 }
16798
16799 si = seg_info (now_seg);
16800 l = si->label_list;
16801 mips_emit_delays ();
16802 if (auto_align)
16803 mips_align (2, 0, l);
16804
16805 expression (&ex);
16806 mips_clear_insn_labels ();
16807
16808 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16809 {
16810 as_bad (_("unsupported use of .gpword"));
16811 ignore_rest_of_line ();
16812 }
16813
16814 p = frag_more (4);
16815 md_number_to_chars (p, 0, 4);
16816 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16817 BFD_RELOC_GPREL32);
16818
16819 demand_empty_rest_of_line ();
16820 }
16821
16822 static void
16823 s_gpdword (int ignore ATTRIBUTE_UNUSED)
16824 {
16825 segment_info_type *si;
16826 struct insn_label_list *l;
16827 expressionS ex;
16828 char *p;
16829
16830 /* When not generating PIC code, this is treated as .dword. */
16831 if (mips_pic != SVR4_PIC)
16832 {
16833 s_cons (3);
16834 return;
16835 }
16836
16837 si = seg_info (now_seg);
16838 l = si->label_list;
16839 mips_emit_delays ();
16840 if (auto_align)
16841 mips_align (3, 0, l);
16842
16843 expression (&ex);
16844 mips_clear_insn_labels ();
16845
16846 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16847 {
16848 as_bad (_("unsupported use of .gpdword"));
16849 ignore_rest_of_line ();
16850 }
16851
16852 p = frag_more (8);
16853 md_number_to_chars (p, 0, 8);
16854 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16855 BFD_RELOC_GPREL32)->fx_tcbit = 1;
16856
16857 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
16858 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
16859 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
16860
16861 demand_empty_rest_of_line ();
16862 }
16863
16864 /* Handle the .ehword pseudo-op. This is used when generating unwinding
16865 tables. It generates a R_MIPS_EH reloc. */
16866
16867 static void
16868 s_ehword (int ignore ATTRIBUTE_UNUSED)
16869 {
16870 expressionS ex;
16871 char *p;
16872
16873 mips_emit_delays ();
16874
16875 expression (&ex);
16876 mips_clear_insn_labels ();
16877
16878 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16879 {
16880 as_bad (_("unsupported use of .ehword"));
16881 ignore_rest_of_line ();
16882 }
16883
16884 p = frag_more (4);
16885 md_number_to_chars (p, 0, 4);
16886 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16887 BFD_RELOC_32_PCREL);
16888
16889 demand_empty_rest_of_line ();
16890 }
16891
16892 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
16893 tables in SVR4 PIC code. */
16894
16895 static void
16896 s_cpadd (int ignore ATTRIBUTE_UNUSED)
16897 {
16898 int reg;
16899
16900 file_mips_check_options ();
16901
16902 /* This is ignored when not generating SVR4 PIC code. */
16903 if (mips_pic != SVR4_PIC)
16904 {
16905 s_ignore (0);
16906 return;
16907 }
16908
16909 mips_mark_labels ();
16910 mips_assembling_insn = TRUE;
16911
16912 /* Add $gp to the register named as an argument. */
16913 macro_start ();
16914 reg = tc_get_register (0);
16915 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
16916 macro_end ();
16917
16918 mips_assembling_insn = FALSE;
16919 demand_empty_rest_of_line ();
16920 }
16921
16922 /* Handle the .insn pseudo-op. This marks instruction labels in
16923 mips16/micromips mode. This permits the linker to handle them specially,
16924 such as generating jalx instructions when needed. We also make
16925 them odd for the duration of the assembly, in order to generate the
16926 right sort of code. We will make them even in the adjust_symtab
16927 routine, while leaving them marked. This is convenient for the
16928 debugger and the disassembler. The linker knows to make them odd
16929 again. */
16930
16931 static void
16932 s_insn (int ignore ATTRIBUTE_UNUSED)
16933 {
16934 file_mips_check_options ();
16935 file_ase_mips16 |= mips_opts.mips16;
16936 file_ase_micromips |= mips_opts.micromips;
16937
16938 mips_mark_labels ();
16939
16940 demand_empty_rest_of_line ();
16941 }
16942
16943 /* Handle the .nan pseudo-op. */
16944
16945 static void
16946 s_nan (int ignore ATTRIBUTE_UNUSED)
16947 {
16948 static const char str_legacy[] = "legacy";
16949 static const char str_2008[] = "2008";
16950 size_t i;
16951
16952 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
16953
16954 if (i == sizeof (str_2008) - 1
16955 && memcmp (input_line_pointer, str_2008, i) == 0)
16956 mips_nan2008 = 1;
16957 else if (i == sizeof (str_legacy) - 1
16958 && memcmp (input_line_pointer, str_legacy, i) == 0)
16959 {
16960 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
16961 mips_nan2008 = 0;
16962 else
16963 as_bad (_("`%s' does not support legacy NaN"),
16964 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
16965 }
16966 else
16967 as_bad (_("bad .nan directive"));
16968
16969 input_line_pointer += i;
16970 demand_empty_rest_of_line ();
16971 }
16972
16973 /* Handle a .stab[snd] directive. Ideally these directives would be
16974 implemented in a transparent way, so that removing them would not
16975 have any effect on the generated instructions. However, s_stab
16976 internally changes the section, so in practice we need to decide
16977 now whether the preceding label marks compressed code. We do not
16978 support changing the compression mode of a label after a .stab*
16979 directive, such as in:
16980
16981 foo:
16982 .stabs ...
16983 .set mips16
16984
16985 so the current mode wins. */
16986
16987 static void
16988 s_mips_stab (int type)
16989 {
16990 mips_mark_labels ();
16991 s_stab (type);
16992 }
16993
16994 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
16995
16996 static void
16997 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
16998 {
16999 char *name;
17000 int c;
17001 symbolS *symbolP;
17002 expressionS exp;
17003
17004 c = get_symbol_name (&name);
17005 symbolP = symbol_find_or_make (name);
17006 S_SET_WEAK (symbolP);
17007 *input_line_pointer = c;
17008
17009 SKIP_WHITESPACE_AFTER_NAME ();
17010
17011 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17012 {
17013 if (S_IS_DEFINED (symbolP))
17014 {
17015 as_bad (_("ignoring attempt to redefine symbol %s"),
17016 S_GET_NAME (symbolP));
17017 ignore_rest_of_line ();
17018 return;
17019 }
17020
17021 if (*input_line_pointer == ',')
17022 {
17023 ++input_line_pointer;
17024 SKIP_WHITESPACE ();
17025 }
17026
17027 expression (&exp);
17028 if (exp.X_op != O_symbol)
17029 {
17030 as_bad (_("bad .weakext directive"));
17031 ignore_rest_of_line ();
17032 return;
17033 }
17034 symbol_set_value_expression (symbolP, &exp);
17035 }
17036
17037 demand_empty_rest_of_line ();
17038 }
17039
17040 /* Parse a register string into a number. Called from the ECOFF code
17041 to parse .frame. The argument is non-zero if this is the frame
17042 register, so that we can record it in mips_frame_reg. */
17043
17044 int
17045 tc_get_register (int frame)
17046 {
17047 unsigned int reg;
17048
17049 SKIP_WHITESPACE ();
17050 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17051 reg = 0;
17052 if (frame)
17053 {
17054 mips_frame_reg = reg != 0 ? reg : SP;
17055 mips_frame_reg_valid = 1;
17056 mips_cprestore_valid = 0;
17057 }
17058 return reg;
17059 }
17060
17061 valueT
17062 md_section_align (asection *seg, valueT addr)
17063 {
17064 int align = bfd_get_section_alignment (stdoutput, seg);
17065
17066 /* We don't need to align ELF sections to the full alignment.
17067 However, Irix 5 may prefer that we align them at least to a 16
17068 byte boundary. We don't bother to align the sections if we
17069 are targeted for an embedded system. */
17070 if (strncmp (TARGET_OS, "elf", 3) == 0)
17071 return addr;
17072 if (align > 4)
17073 align = 4;
17074
17075 return ((addr + (1 << align) - 1) & -(1 << align));
17076 }
17077
17078 /* Utility routine, called from above as well. If called while the
17079 input file is still being read, it's only an approximation. (For
17080 example, a symbol may later become defined which appeared to be
17081 undefined earlier.) */
17082
17083 static int
17084 nopic_need_relax (symbolS *sym, int before_relaxing)
17085 {
17086 if (sym == 0)
17087 return 0;
17088
17089 if (g_switch_value > 0)
17090 {
17091 const char *symname;
17092 int change;
17093
17094 /* Find out whether this symbol can be referenced off the $gp
17095 register. It can be if it is smaller than the -G size or if
17096 it is in the .sdata or .sbss section. Certain symbols can
17097 not be referenced off the $gp, although it appears as though
17098 they can. */
17099 symname = S_GET_NAME (sym);
17100 if (symname != (const char *) NULL
17101 && (strcmp (symname, "eprol") == 0
17102 || strcmp (symname, "etext") == 0
17103 || strcmp (symname, "_gp") == 0
17104 || strcmp (symname, "edata") == 0
17105 || strcmp (symname, "_fbss") == 0
17106 || strcmp (symname, "_fdata") == 0
17107 || strcmp (symname, "_ftext") == 0
17108 || strcmp (symname, "end") == 0
17109 || strcmp (symname, "_gp_disp") == 0))
17110 change = 1;
17111 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17112 && (0
17113 #ifndef NO_ECOFF_DEBUGGING
17114 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17115 && (symbol_get_obj (sym)->ecoff_extern_size
17116 <= g_switch_value))
17117 #endif
17118 /* We must defer this decision until after the whole
17119 file has been read, since there might be a .extern
17120 after the first use of this symbol. */
17121 || (before_relaxing
17122 #ifndef NO_ECOFF_DEBUGGING
17123 && symbol_get_obj (sym)->ecoff_extern_size == 0
17124 #endif
17125 && S_GET_VALUE (sym) == 0)
17126 || (S_GET_VALUE (sym) != 0
17127 && S_GET_VALUE (sym) <= g_switch_value)))
17128 change = 0;
17129 else
17130 {
17131 const char *segname;
17132
17133 segname = segment_name (S_GET_SEGMENT (sym));
17134 gas_assert (strcmp (segname, ".lit8") != 0
17135 && strcmp (segname, ".lit4") != 0);
17136 change = (strcmp (segname, ".sdata") != 0
17137 && strcmp (segname, ".sbss") != 0
17138 && strncmp (segname, ".sdata.", 7) != 0
17139 && strncmp (segname, ".sbss.", 6) != 0
17140 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17141 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17142 }
17143 return change;
17144 }
17145 else
17146 /* We are not optimizing for the $gp register. */
17147 return 1;
17148 }
17149
17150
17151 /* Return true if the given symbol should be considered local for SVR4 PIC. */
17152
17153 static bfd_boolean
17154 pic_need_relax (symbolS *sym)
17155 {
17156 asection *symsec;
17157
17158 /* Handle the case of a symbol equated to another symbol. */
17159 while (symbol_equated_reloc_p (sym))
17160 {
17161 symbolS *n;
17162
17163 /* It's possible to get a loop here in a badly written program. */
17164 n = symbol_get_value_expression (sym)->X_add_symbol;
17165 if (n == sym)
17166 break;
17167 sym = n;
17168 }
17169
17170 if (symbol_section_p (sym))
17171 return TRUE;
17172
17173 symsec = S_GET_SEGMENT (sym);
17174
17175 /* This must duplicate the test in adjust_reloc_syms. */
17176 return (!bfd_is_und_section (symsec)
17177 && !bfd_is_abs_section (symsec)
17178 && !bfd_is_com_section (symsec)
17179 /* A global or weak symbol is treated as external. */
17180 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
17181 }
17182 \f
17183 /* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17184 convert a section-relative value VAL to the equivalent PC-relative
17185 value. */
17186
17187 static offsetT
17188 mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17189 offsetT val, long stretch)
17190 {
17191 fragS *sym_frag;
17192 addressT addr;
17193
17194 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17195
17196 sym_frag = symbol_get_frag (fragp->fr_symbol);
17197
17198 /* If the relax_marker of the symbol fragment differs from the
17199 relax_marker of this fragment, we have not yet adjusted the
17200 symbol fragment fr_address. We want to add in STRETCH in
17201 order to get a better estimate of the address. This
17202 particularly matters because of the shift bits. */
17203 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17204 {
17205 fragS *f;
17206
17207 /* Adjust stretch for any alignment frag. Note that if have
17208 been expanding the earlier code, the symbol may be
17209 defined in what appears to be an earlier frag. FIXME:
17210 This doesn't handle the fr_subtype field, which specifies
17211 a maximum number of bytes to skip when doing an
17212 alignment. */
17213 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17214 {
17215 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17216 {
17217 if (stretch < 0)
17218 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17219 else
17220 stretch &= ~((1 << (int) f->fr_offset) - 1);
17221 if (stretch == 0)
17222 break;
17223 }
17224 }
17225 if (f != NULL)
17226 val += stretch;
17227 }
17228
17229 addr = fragp->fr_address + fragp->fr_fix;
17230
17231 /* The base address rules are complicated. The base address of
17232 a branch is the following instruction. The base address of a
17233 PC relative load or add is the instruction itself, but if it
17234 is in a delay slot (in which case it can not be extended) use
17235 the address of the instruction whose delay slot it is in. */
17236 if (pcrel_op->include_isa_bit)
17237 {
17238 addr += 2;
17239
17240 /* If we are currently assuming that this frag should be
17241 extended, then the current address is two bytes higher. */
17242 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17243 addr += 2;
17244
17245 /* Ignore the low bit in the target, since it will be set
17246 for a text label. */
17247 val &= -2;
17248 }
17249 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17250 addr -= 4;
17251 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17252 addr -= 2;
17253
17254 val -= addr & -(1 << pcrel_op->align_log2);
17255
17256 return val;
17257 }
17258
17259 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17260 extended opcode. SEC is the section the frag is in. */
17261
17262 static int
17263 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17264 {
17265 const struct mips_int_operand *operand;
17266 offsetT val;
17267 segT symsec;
17268 int type;
17269
17270 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17271 return 0;
17272 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17273 return 1;
17274
17275 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17276 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17277 operand = mips16_immed_operand (type, FALSE);
17278 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17279 || (operand->root.type == OP_PCREL
17280 ? sec != symsec
17281 : !bfd_is_abs_section (symsec)))
17282 return 1;
17283
17284 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17285
17286 if (operand->root.type == OP_PCREL)
17287 {
17288 const struct mips_pcrel_operand *pcrel_op;
17289 offsetT maxtiny;
17290
17291 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
17292 return 1;
17293
17294 pcrel_op = (const struct mips_pcrel_operand *) operand;
17295 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17296
17297 /* If any of the shifted bits are set, we must use an extended
17298 opcode. If the address depends on the size of this
17299 instruction, this can lead to a loop, so we arrange to always
17300 use an extended opcode. */
17301 if ((val & ((1 << operand->shift) - 1)) != 0)
17302 {
17303 fragp->fr_subtype =
17304 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17305 return 1;
17306 }
17307
17308 /* If we are about to mark a frag as extended because the value
17309 is precisely the next value above maxtiny, then there is a
17310 chance of an infinite loop as in the following code:
17311 la $4,foo
17312 .skip 1020
17313 .align 2
17314 foo:
17315 In this case when the la is extended, foo is 0x3fc bytes
17316 away, so the la can be shrunk, but then foo is 0x400 away, so
17317 the la must be extended. To avoid this loop, we mark the
17318 frag as extended if it was small, and is about to become
17319 extended with the next value above maxtiny. */
17320 maxtiny = mips_int_operand_max (operand);
17321 if (val == maxtiny + (1 << operand->shift)
17322 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17323 {
17324 fragp->fr_subtype =
17325 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17326 return 1;
17327 }
17328 }
17329
17330 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17331 }
17332
17333 /* Compute the length of a branch sequence, and adjust the
17334 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17335 worst-case length is computed, with UPDATE being used to indicate
17336 whether an unconditional (-1), branch-likely (+1) or regular (0)
17337 branch is to be computed. */
17338 static int
17339 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17340 {
17341 bfd_boolean toofar;
17342 int length;
17343
17344 if (fragp
17345 && S_IS_DEFINED (fragp->fr_symbol)
17346 && !S_IS_WEAK (fragp->fr_symbol)
17347 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17348 {
17349 addressT addr;
17350 offsetT val;
17351
17352 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17353
17354 addr = fragp->fr_address + fragp->fr_fix + 4;
17355
17356 val -= addr;
17357
17358 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17359 }
17360 else
17361 /* If the symbol is not defined or it's in a different segment,
17362 we emit the long sequence. */
17363 toofar = TRUE;
17364
17365 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17366 fragp->fr_subtype
17367 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17368 RELAX_BRANCH_PIC (fragp->fr_subtype),
17369 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17370 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17371 RELAX_BRANCH_LINK (fragp->fr_subtype),
17372 toofar);
17373
17374 length = 4;
17375 if (toofar)
17376 {
17377 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17378 length += 8;
17379
17380 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
17381 {
17382 /* Additional space for PIC loading of target address. */
17383 length += 8;
17384 if (mips_opts.isa == ISA_MIPS1)
17385 /* Additional space for $at-stabilizing nop. */
17386 length += 4;
17387 }
17388
17389 /* If branch is conditional. */
17390 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17391 length += 8;
17392 }
17393
17394 return length;
17395 }
17396
17397 /* Get a FRAG's branch instruction delay slot size, either from the
17398 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17399 or SHORT_INSN_SIZE otherwise. */
17400
17401 static int
17402 frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17403 {
17404 char *buf = fragp->fr_literal + fragp->fr_fix;
17405
17406 if (al)
17407 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17408 else
17409 return short_insn_size;
17410 }
17411
17412 /* Compute the length of a branch sequence, and adjust the
17413 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17414 worst-case length is computed, with UPDATE being used to indicate
17415 whether an unconditional (-1), or regular (0) branch is to be
17416 computed. */
17417
17418 static int
17419 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17420 {
17421 bfd_boolean insn32 = TRUE;
17422 bfd_boolean nods = TRUE;
17423 bfd_boolean pic = TRUE;
17424 bfd_boolean al = TRUE;
17425 int short_insn_size;
17426 bfd_boolean toofar;
17427 int length;
17428
17429 if (fragp)
17430 {
17431 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17432 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
17433 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
17434 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17435 }
17436 short_insn_size = insn32 ? 4 : 2;
17437
17438 if (fragp
17439 && S_IS_DEFINED (fragp->fr_symbol)
17440 && !S_IS_WEAK (fragp->fr_symbol)
17441 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17442 {
17443 addressT addr;
17444 offsetT val;
17445
17446 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17447 /* Ignore the low bit in the target, since it will be set
17448 for a text label. */
17449 if ((val & 1) != 0)
17450 --val;
17451
17452 addr = fragp->fr_address + fragp->fr_fix + 4;
17453
17454 val -= addr;
17455
17456 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17457 }
17458 else
17459 /* If the symbol is not defined or it's in a different segment,
17460 we emit the long sequence. */
17461 toofar = TRUE;
17462
17463 if (fragp && update
17464 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17465 fragp->fr_subtype = (toofar
17466 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17467 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17468
17469 length = 4;
17470 if (toofar)
17471 {
17472 bfd_boolean compact_known = fragp != NULL;
17473 bfd_boolean compact = FALSE;
17474 bfd_boolean uncond;
17475
17476 if (fragp)
17477 {
17478 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17479 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17480 }
17481 else
17482 uncond = update < 0;
17483
17484 /* If label is out of range, we turn branch <br>:
17485
17486 <br> label # 4 bytes
17487 0:
17488
17489 into:
17490
17491 j label # 4 bytes
17492 nop # 2/4 bytes if
17493 # compact && (!PIC || insn32)
17494 0:
17495 */
17496 if ((!pic || insn32) && (!compact_known || compact))
17497 length += short_insn_size;
17498
17499 /* If assembling PIC code, we further turn:
17500
17501 j label # 4 bytes
17502
17503 into:
17504
17505 lw/ld at, %got(label)(gp) # 4 bytes
17506 d/addiu at, %lo(label) # 4 bytes
17507 jr/c at # 2/4 bytes
17508 */
17509 if (pic)
17510 length += 4 + short_insn_size;
17511
17512 /* Add an extra nop if the jump has no compact form and we need
17513 to fill the delay slot. */
17514 if ((!pic || al) && nods)
17515 length += (fragp
17516 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
17517 : short_insn_size);
17518
17519 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17520
17521 <brneg> 0f # 4 bytes
17522 nop # 2/4 bytes if !compact
17523 */
17524 if (!uncond)
17525 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
17526 }
17527 else if (nods)
17528 {
17529 /* Add an extra nop to fill the delay slot. */
17530 gas_assert (fragp);
17531 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
17532 }
17533
17534 return length;
17535 }
17536
17537 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17538 bit accordingly. */
17539
17540 static int
17541 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17542 {
17543 bfd_boolean toofar;
17544
17545 if (fragp
17546 && S_IS_DEFINED (fragp->fr_symbol)
17547 && !S_IS_WEAK (fragp->fr_symbol)
17548 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17549 {
17550 addressT addr;
17551 offsetT val;
17552 int type;
17553
17554 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17555 /* Ignore the low bit in the target, since it will be set
17556 for a text label. */
17557 if ((val & 1) != 0)
17558 --val;
17559
17560 /* Assume this is a 2-byte branch. */
17561 addr = fragp->fr_address + fragp->fr_fix + 2;
17562
17563 /* We try to avoid the infinite loop by not adding 2 more bytes for
17564 long branches. */
17565
17566 val -= addr;
17567
17568 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17569 if (type == 'D')
17570 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17571 else if (type == 'E')
17572 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17573 else
17574 abort ();
17575 }
17576 else
17577 /* If the symbol is not defined or it's in a different segment,
17578 we emit a normal 32-bit branch. */
17579 toofar = TRUE;
17580
17581 if (fragp && update
17582 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17583 fragp->fr_subtype
17584 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17585 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17586
17587 if (toofar)
17588 return 4;
17589
17590 return 2;
17591 }
17592
17593 /* Estimate the size of a frag before relaxing. Unless this is the
17594 mips16, we are not really relaxing here, and the final size is
17595 encoded in the subtype information. For the mips16, we have to
17596 decide whether we are using an extended opcode or not. */
17597
17598 int
17599 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17600 {
17601 int change;
17602
17603 if (RELAX_BRANCH_P (fragp->fr_subtype))
17604 {
17605
17606 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17607
17608 return fragp->fr_var;
17609 }
17610
17611 if (RELAX_MIPS16_P (fragp->fr_subtype))
17612 /* We don't want to modify the EXTENDED bit here; it might get us
17613 into infinite loops. We change it only in mips_relax_frag(). */
17614 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
17615
17616 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17617 {
17618 int length = 4;
17619
17620 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17621 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17622 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17623 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17624 fragp->fr_var = length;
17625
17626 return length;
17627 }
17628
17629 if (mips_pic == VXWORKS_PIC)
17630 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17631 change = 0;
17632 else if (RELAX_PIC (fragp->fr_subtype))
17633 change = pic_need_relax (fragp->fr_symbol);
17634 else
17635 change = nopic_need_relax (fragp->fr_symbol, 0);
17636
17637 if (change)
17638 {
17639 fragp->fr_subtype |= RELAX_USE_SECOND;
17640 return -RELAX_FIRST (fragp->fr_subtype);
17641 }
17642 else
17643 return -RELAX_SECOND (fragp->fr_subtype);
17644 }
17645
17646 /* This is called to see whether a reloc against a defined symbol
17647 should be converted into a reloc against a section. */
17648
17649 int
17650 mips_fix_adjustable (fixS *fixp)
17651 {
17652 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17653 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17654 return 0;
17655
17656 if (fixp->fx_addsy == NULL)
17657 return 1;
17658
17659 /* Allow relocs used for EH tables. */
17660 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17661 return 1;
17662
17663 /* If symbol SYM is in a mergeable section, relocations of the form
17664 SYM + 0 can usually be made section-relative. The mergeable data
17665 is then identified by the section offset rather than by the symbol.
17666
17667 However, if we're generating REL LO16 relocations, the offset is split
17668 between the LO16 and partnering high part relocation. The linker will
17669 need to recalculate the complete offset in order to correctly identify
17670 the merge data.
17671
17672 The linker has traditionally not looked for the partnering high part
17673 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17674 placed anywhere. Rather than break backwards compatibility by changing
17675 this, it seems better not to force the issue, and instead keep the
17676 original symbol. This will work with either linker behavior. */
17677 if ((lo16_reloc_p (fixp->fx_r_type)
17678 || reloc_needs_lo_p (fixp->fx_r_type))
17679 && HAVE_IN_PLACE_ADDENDS
17680 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17681 return 0;
17682
17683 /* There is no place to store an in-place offset for JALR relocations. */
17684 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
17685 return 0;
17686
17687 /* Likewise an in-range offset of limited PC-relative relocations may
17688 overflow the in-place relocatable field if recalculated against the
17689 start address of the symbol's containing section.
17690
17691 Also, PC relative relocations for MIPS R6 need to be symbol rather than
17692 section relative to allow linker relaxations to be performed later on. */
17693 if (limited_pcrel_reloc_p (fixp->fx_r_type)
17694 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
17695 return 0;
17696
17697 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17698 to a floating-point stub. The same is true for non-R_MIPS16_26
17699 relocations against MIPS16 functions; in this case, the stub becomes
17700 the function's canonical address.
17701
17702 Floating-point stubs are stored in unique .mips16.call.* or
17703 .mips16.fn.* sections. If a stub T for function F is in section S,
17704 the first relocation in section S must be against F; this is how the
17705 linker determines the target function. All relocations that might
17706 resolve to T must also be against F. We therefore have the following
17707 restrictions, which are given in an intentionally-redundant way:
17708
17709 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17710 symbols.
17711
17712 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17713 if that stub might be used.
17714
17715 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17716 symbols.
17717
17718 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17719 that stub might be used.
17720
17721 There is a further restriction:
17722
17723 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17724 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
17725 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
17726 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
17727 against MIPS16 or microMIPS symbols because we need to keep the
17728 MIPS16 or microMIPS symbol for the purpose of mode mismatch
17729 detection and JAL or BAL to JALX instruction conversion in the
17730 linker.
17731
17732 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17733 against a MIPS16 symbol. We deal with (5) by additionally leaving
17734 alone any jump and branch relocations against a microMIPS symbol.
17735
17736 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17737 relocation against some symbol R, no relocation against R may be
17738 reduced. (Note that this deals with (2) as well as (1) because
17739 relocations against global symbols will never be reduced on ELF
17740 targets.) This approach is a little simpler than trying to detect
17741 stub sections, and gives the "all or nothing" per-symbol consistency
17742 that we have for MIPS16 symbols. */
17743 if (fixp->fx_subsy == NULL
17744 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
17745 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17746 && (jmp_reloc_p (fixp->fx_r_type)
17747 || b_reloc_p (fixp->fx_r_type)))
17748 || *symbol_get_tc (fixp->fx_addsy)))
17749 return 0;
17750
17751 return 1;
17752 }
17753
17754 /* Translate internal representation of relocation info to BFD target
17755 format. */
17756
17757 arelent **
17758 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
17759 {
17760 static arelent *retval[4];
17761 arelent *reloc;
17762 bfd_reloc_code_real_type code;
17763
17764 memset (retval, 0, sizeof(retval));
17765 reloc = retval[0] = XCNEW (arelent);
17766 reloc->sym_ptr_ptr = XNEW (asymbol *);
17767 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
17768 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17769
17770 if (fixp->fx_pcrel)
17771 {
17772 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17773 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
17774 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17775 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17776 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
17777 || fixp->fx_r_type == BFD_RELOC_32_PCREL
17778 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
17779 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
17780 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
17781 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
17782 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
17783 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
17784
17785 /* At this point, fx_addnumber is "symbol offset - pcrel address".
17786 Relocations want only the symbol offset. */
17787 switch (fixp->fx_r_type)
17788 {
17789 case BFD_RELOC_MIPS_18_PCREL_S3:
17790 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
17791 break;
17792 default:
17793 reloc->addend = fixp->fx_addnumber + reloc->address;
17794 break;
17795 }
17796 }
17797 else if (HAVE_IN_PLACE_ADDENDS
17798 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
17799 && (read_compressed_insn (fixp->fx_frag->fr_literal
17800 + fixp->fx_where, 4) >> 26) == 0x3c)
17801 {
17802 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
17803 addend accordingly. */
17804 reloc->addend = fixp->fx_addnumber >> 1;
17805 }
17806 else
17807 reloc->addend = fixp->fx_addnumber;
17808
17809 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17810 entry to be used in the relocation's section offset. */
17811 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17812 {
17813 reloc->address = reloc->addend;
17814 reloc->addend = 0;
17815 }
17816
17817 code = fixp->fx_r_type;
17818
17819 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
17820 if (reloc->howto == NULL)
17821 {
17822 as_bad_where (fixp->fx_file, fixp->fx_line,
17823 _("cannot represent %s relocation in this object file"
17824 " format"),
17825 bfd_get_reloc_code_name (code));
17826 retval[0] = NULL;
17827 }
17828
17829 return retval;
17830 }
17831
17832 /* Relax a machine dependent frag. This returns the amount by which
17833 the current size of the frag should change. */
17834
17835 int
17836 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
17837 {
17838 if (RELAX_BRANCH_P (fragp->fr_subtype))
17839 {
17840 offsetT old_var = fragp->fr_var;
17841
17842 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
17843
17844 return fragp->fr_var - old_var;
17845 }
17846
17847 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17848 {
17849 offsetT old_var = fragp->fr_var;
17850 offsetT new_var = 4;
17851
17852 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17853 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17854 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17855 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17856 fragp->fr_var = new_var;
17857
17858 return new_var - old_var;
17859 }
17860
17861 if (! RELAX_MIPS16_P (fragp->fr_subtype))
17862 return 0;
17863
17864 if (mips16_extended_frag (fragp, sec, stretch))
17865 {
17866 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17867 return 0;
17868 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17869 return 2;
17870 }
17871 else
17872 {
17873 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17874 return 0;
17875 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17876 return -2;
17877 }
17878
17879 return 0;
17880 }
17881
17882 /* Convert a machine dependent frag. */
17883
17884 void
17885 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
17886 {
17887 if (RELAX_BRANCH_P (fragp->fr_subtype))
17888 {
17889 char *buf;
17890 unsigned long insn;
17891 expressionS exp;
17892 fixS *fixp;
17893
17894 buf = fragp->fr_literal + fragp->fr_fix;
17895 insn = read_insn (buf);
17896
17897 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17898 {
17899 /* We generate a fixup instead of applying it right now
17900 because, if there are linker relaxations, we're going to
17901 need the relocations. */
17902 exp.X_op = O_symbol;
17903 exp.X_add_symbol = fragp->fr_symbol;
17904 exp.X_add_number = fragp->fr_offset;
17905
17906 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
17907 BFD_RELOC_16_PCREL_S2);
17908 fixp->fx_file = fragp->fr_file;
17909 fixp->fx_line = fragp->fr_line;
17910
17911 buf = write_insn (buf, insn);
17912 }
17913 else
17914 {
17915 int i;
17916
17917 as_warn_where (fragp->fr_file, fragp->fr_line,
17918 _("relaxed out-of-range branch into a jump"));
17919
17920 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
17921 goto uncond;
17922
17923 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17924 {
17925 /* Reverse the branch. */
17926 switch ((insn >> 28) & 0xf)
17927 {
17928 case 4:
17929 if ((insn & 0xff000000) == 0x47000000
17930 || (insn & 0xff600000) == 0x45600000)
17931 {
17932 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
17933 reversed by tweaking bit 23. */
17934 insn ^= 0x00800000;
17935 }
17936 else
17937 {
17938 /* bc[0-3][tf]l? instructions can have the condition
17939 reversed by tweaking a single TF bit, and their
17940 opcodes all have 0x4???????. */
17941 gas_assert ((insn & 0xf3e00000) == 0x41000000);
17942 insn ^= 0x00010000;
17943 }
17944 break;
17945
17946 case 0:
17947 /* bltz 0x04000000 bgez 0x04010000
17948 bltzal 0x04100000 bgezal 0x04110000 */
17949 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
17950 insn ^= 0x00010000;
17951 break;
17952
17953 case 1:
17954 /* beq 0x10000000 bne 0x14000000
17955 blez 0x18000000 bgtz 0x1c000000 */
17956 insn ^= 0x04000000;
17957 break;
17958
17959 default:
17960 abort ();
17961 }
17962 }
17963
17964 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
17965 {
17966 /* Clear the and-link bit. */
17967 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
17968
17969 /* bltzal 0x04100000 bgezal 0x04110000
17970 bltzall 0x04120000 bgezall 0x04130000 */
17971 insn &= ~0x00100000;
17972 }
17973
17974 /* Branch over the branch (if the branch was likely) or the
17975 full jump (not likely case). Compute the offset from the
17976 current instruction to branch to. */
17977 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17978 i = 16;
17979 else
17980 {
17981 /* How many bytes in instructions we've already emitted? */
17982 i = buf - fragp->fr_literal - fragp->fr_fix;
17983 /* How many bytes in instructions from here to the end? */
17984 i = fragp->fr_var - i;
17985 }
17986 /* Convert to instruction count. */
17987 i >>= 2;
17988 /* Branch counts from the next instruction. */
17989 i--;
17990 insn |= i;
17991 /* Branch over the jump. */
17992 buf = write_insn (buf, insn);
17993
17994 /* nop */
17995 buf = write_insn (buf, 0);
17996
17997 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17998 {
17999 /* beql $0, $0, 2f */
18000 insn = 0x50000000;
18001 /* Compute the PC offset from the current instruction to
18002 the end of the variable frag. */
18003 /* How many bytes in instructions we've already emitted? */
18004 i = buf - fragp->fr_literal - fragp->fr_fix;
18005 /* How many bytes in instructions from here to the end? */
18006 i = fragp->fr_var - i;
18007 /* Convert to instruction count. */
18008 i >>= 2;
18009 /* Don't decrement i, because we want to branch over the
18010 delay slot. */
18011 insn |= i;
18012
18013 buf = write_insn (buf, insn);
18014 buf = write_insn (buf, 0);
18015 }
18016
18017 uncond:
18018 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
18019 {
18020 /* j or jal. */
18021 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18022 ? 0x0c000000 : 0x08000000);
18023 exp.X_op = O_symbol;
18024 exp.X_add_symbol = fragp->fr_symbol;
18025 exp.X_add_number = fragp->fr_offset;
18026
18027 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18028 FALSE, BFD_RELOC_MIPS_JMP);
18029 fixp->fx_file = fragp->fr_file;
18030 fixp->fx_line = fragp->fr_line;
18031
18032 buf = write_insn (buf, insn);
18033 }
18034 else
18035 {
18036 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18037
18038 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
18039 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18040 insn |= at << OP_SH_RT;
18041 exp.X_op = O_symbol;
18042 exp.X_add_symbol = fragp->fr_symbol;
18043 exp.X_add_number = fragp->fr_offset;
18044
18045 if (fragp->fr_offset)
18046 {
18047 exp.X_add_symbol = make_expr_symbol (&exp);
18048 exp.X_add_number = 0;
18049 }
18050
18051 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18052 FALSE, BFD_RELOC_MIPS_GOT16);
18053 fixp->fx_file = fragp->fr_file;
18054 fixp->fx_line = fragp->fr_line;
18055
18056 buf = write_insn (buf, insn);
18057
18058 if (mips_opts.isa == ISA_MIPS1)
18059 /* nop */
18060 buf = write_insn (buf, 0);
18061
18062 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
18063 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18064 insn |= at << OP_SH_RS | at << OP_SH_RT;
18065
18066 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18067 FALSE, BFD_RELOC_LO16);
18068 fixp->fx_file = fragp->fr_file;
18069 fixp->fx_line = fragp->fr_line;
18070
18071 buf = write_insn (buf, insn);
18072
18073 /* j(al)r $at. */
18074 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18075 insn = 0x0000f809;
18076 else
18077 insn = 0x00000008;
18078 insn |= at << OP_SH_RS;
18079
18080 buf = write_insn (buf, insn);
18081 }
18082 }
18083
18084 fragp->fr_fix += fragp->fr_var;
18085 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18086 return;
18087 }
18088
18089 /* Relax microMIPS branches. */
18090 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18091 {
18092 char *buf = fragp->fr_literal + fragp->fr_fix;
18093 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18094 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18095 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18096 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18097 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18098 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18099 bfd_boolean short_ds;
18100 unsigned long insn;
18101 expressionS exp;
18102 fixS *fixp;
18103
18104 exp.X_op = O_symbol;
18105 exp.X_add_symbol = fragp->fr_symbol;
18106 exp.X_add_number = fragp->fr_offset;
18107
18108 fragp->fr_fix += fragp->fr_var;
18109
18110 /* Handle 16-bit branches that fit or are forced to fit. */
18111 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18112 {
18113 /* We generate a fixup instead of applying it right now,
18114 because if there is linker relaxation, we're going to
18115 need the relocations. */
18116 if (type == 'D')
18117 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18118 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18119 else if (type == 'E')
18120 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18121 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18122 else
18123 abort ();
18124
18125 fixp->fx_file = fragp->fr_file;
18126 fixp->fx_line = fragp->fr_line;
18127
18128 /* These relocations can have an addend that won't fit in
18129 2 octets. */
18130 fixp->fx_no_overflow = 1;
18131
18132 return;
18133 }
18134
18135 /* Handle 32-bit branches that fit or are forced to fit. */
18136 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18137 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18138 {
18139 /* We generate a fixup instead of applying it right now,
18140 because if there is linker relaxation, we're going to
18141 need the relocations. */
18142 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18143 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18144 fixp->fx_file = fragp->fr_file;
18145 fixp->fx_line = fragp->fr_line;
18146
18147 if (type == 0)
18148 {
18149 insn = read_compressed_insn (buf, 4);
18150 buf += 4;
18151
18152 if (nods)
18153 {
18154 /* Check the short-delay-slot bit. */
18155 if (!al || (insn & 0x02000000) != 0)
18156 buf = write_compressed_insn (buf, 0x0c00, 2);
18157 else
18158 buf = write_compressed_insn (buf, 0x00000000, 4);
18159 }
18160
18161 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18162 return;
18163 }
18164 }
18165
18166 /* Relax 16-bit branches to 32-bit branches. */
18167 if (type != 0)
18168 {
18169 insn = read_compressed_insn (buf, 2);
18170
18171 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18172 insn = 0x94000000; /* beq */
18173 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18174 {
18175 unsigned long regno;
18176
18177 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18178 regno = micromips_to_32_reg_d_map [regno];
18179 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18180 insn |= regno << MICROMIPSOP_SH_RS;
18181 }
18182 else
18183 abort ();
18184
18185 /* Nothing else to do, just write it out. */
18186 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18187 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18188 {
18189 buf = write_compressed_insn (buf, insn, 4);
18190 if (nods)
18191 buf = write_compressed_insn (buf, 0x0c00, 2);
18192 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18193 return;
18194 }
18195 }
18196 else
18197 insn = read_compressed_insn (buf, 4);
18198
18199 /* Relax 32-bit branches to a sequence of instructions. */
18200 as_warn_where (fragp->fr_file, fragp->fr_line,
18201 _("relaxed out-of-range branch into a jump"));
18202
18203 /* Set the short-delay-slot bit. */
18204 short_ds = !al || (insn & 0x02000000) != 0;
18205
18206 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18207 {
18208 symbolS *l;
18209
18210 /* Reverse the branch. */
18211 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18212 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18213 insn ^= 0x20000000;
18214 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18215 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18216 || (insn & 0xffe00000) == 0x40800000 /* blez */
18217 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18218 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18219 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18220 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18221 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18222 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18223 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18224 insn ^= 0x00400000;
18225 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18226 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18227 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18228 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18229 insn ^= 0x00200000;
18230 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18231 BNZ.df */
18232 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18233 BNZ.V */
18234 insn ^= 0x00800000;
18235 else
18236 abort ();
18237
18238 if (al)
18239 {
18240 /* Clear the and-link and short-delay-slot bits. */
18241 gas_assert ((insn & 0xfda00000) == 0x40200000);
18242
18243 /* bltzal 0x40200000 bgezal 0x40600000 */
18244 /* bltzals 0x42200000 bgezals 0x42600000 */
18245 insn &= ~0x02200000;
18246 }
18247
18248 /* Make a label at the end for use with the branch. */
18249 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18250 micromips_label_inc ();
18251 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18252
18253 /* Refer to it. */
18254 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18255 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18256 fixp->fx_file = fragp->fr_file;
18257 fixp->fx_line = fragp->fr_line;
18258
18259 /* Branch over the jump. */
18260 buf = write_compressed_insn (buf, insn, 4);
18261
18262 if (!compact)
18263 {
18264 /* nop */
18265 if (insn32)
18266 buf = write_compressed_insn (buf, 0x00000000, 4);
18267 else
18268 buf = write_compressed_insn (buf, 0x0c00, 2);
18269 }
18270 }
18271
18272 if (!pic)
18273 {
18274 unsigned long jal = (short_ds || nods
18275 ? 0x74000000 : 0xf4000000); /* jal/s */
18276
18277 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18278 insn = al ? jal : 0xd4000000;
18279
18280 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18281 BFD_RELOC_MICROMIPS_JMP);
18282 fixp->fx_file = fragp->fr_file;
18283 fixp->fx_line = fragp->fr_line;
18284
18285 buf = write_compressed_insn (buf, insn, 4);
18286
18287 if (compact || nods)
18288 {
18289 /* nop */
18290 if (insn32)
18291 buf = write_compressed_insn (buf, 0x00000000, 4);
18292 else
18293 buf = write_compressed_insn (buf, 0x0c00, 2);
18294 }
18295 }
18296 else
18297 {
18298 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18299
18300 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18301 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18302 insn |= at << MICROMIPSOP_SH_RT;
18303
18304 if (exp.X_add_number)
18305 {
18306 exp.X_add_symbol = make_expr_symbol (&exp);
18307 exp.X_add_number = 0;
18308 }
18309
18310 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18311 BFD_RELOC_MICROMIPS_GOT16);
18312 fixp->fx_file = fragp->fr_file;
18313 fixp->fx_line = fragp->fr_line;
18314
18315 buf = write_compressed_insn (buf, insn, 4);
18316
18317 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18318 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18319 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18320
18321 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18322 BFD_RELOC_MICROMIPS_LO16);
18323 fixp->fx_file = fragp->fr_file;
18324 fixp->fx_line = fragp->fr_line;
18325
18326 buf = write_compressed_insn (buf, insn, 4);
18327
18328 if (insn32)
18329 {
18330 /* jr/jalr $at */
18331 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18332 insn |= at << MICROMIPSOP_SH_RS;
18333
18334 buf = write_compressed_insn (buf, insn, 4);
18335
18336 if (compact || nods)
18337 /* nop */
18338 buf = write_compressed_insn (buf, 0x00000000, 4);
18339 }
18340 else
18341 {
18342 /* jr/jrc/jalr/jalrs $at */
18343 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18344 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
18345
18346 insn = al ? jalr : jr;
18347 insn |= at << MICROMIPSOP_SH_MJ;
18348
18349 buf = write_compressed_insn (buf, insn, 2);
18350 if (al && nods)
18351 {
18352 /* nop */
18353 if (short_ds)
18354 buf = write_compressed_insn (buf, 0x0c00, 2);
18355 else
18356 buf = write_compressed_insn (buf, 0x00000000, 4);
18357 }
18358 }
18359 }
18360
18361 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18362 return;
18363 }
18364
18365 if (RELAX_MIPS16_P (fragp->fr_subtype))
18366 {
18367 int type;
18368 const struct mips_int_operand *operand;
18369 offsetT val;
18370 char *buf;
18371 unsigned int user_length, length;
18372 bfd_boolean need_reloc;
18373 unsigned long insn;
18374 bfd_boolean ext;
18375 segT symsec;
18376
18377 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18378 operand = mips16_immed_operand (type, FALSE);
18379
18380 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18381 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
18382
18383 symsec = S_GET_SEGMENT (fragp->fr_symbol);
18384 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
18385 || (operand->root.type == OP_PCREL
18386 ? asec != symsec
18387 : !bfd_is_abs_section (symsec)));
18388
18389 if (operand->root.type == OP_PCREL)
18390 {
18391 const struct mips_pcrel_operand *pcrel_op;
18392
18393 pcrel_op = (const struct mips_pcrel_operand *) operand;
18394
18395 if (pcrel_op->include_isa_bit && !need_reloc)
18396 {
18397 if (!ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
18398 as_bad_where (fragp->fr_file, fragp->fr_line,
18399 _("branch to a symbol in another ISA mode"));
18400 else if ((fragp->fr_offset & 0x1) != 0)
18401 as_bad_where (fragp->fr_file, fragp->fr_line,
18402 _("branch to misaligned address (0x%lx)"),
18403 (long) val);
18404 }
18405
18406 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
18407
18408 /* Make sure the section winds up with the alignment we have
18409 assumed. */
18410 if (operand->shift > 0)
18411 record_alignment (asec, operand->shift);
18412 }
18413
18414 if (ext
18415 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18416 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18417 as_warn_where (fragp->fr_file, fragp->fr_line,
18418 _("extended instruction in delay slot"));
18419
18420 buf = fragp->fr_literal + fragp->fr_fix;
18421
18422 insn = read_compressed_insn (buf, 2);
18423 if (ext)
18424 insn |= MIPS16_EXTEND;
18425
18426 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18427 user_length = 4;
18428 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18429 user_length = 2;
18430 else
18431 user_length = 0;
18432
18433 if (need_reloc)
18434 {
18435 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
18436 expressionS exp;
18437 fixS *fixp;
18438
18439 switch (type)
18440 {
18441 case 'p':
18442 case 'q':
18443 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
18444 break;
18445 default:
18446 as_bad_where (fragp->fr_file, fragp->fr_line,
18447 _("unsupported relocation"));
18448 break;
18449 }
18450 if (reloc == BFD_RELOC_NONE)
18451 ;
18452 else if (ext)
18453 {
18454 exp.X_op = O_symbol;
18455 exp.X_add_symbol = fragp->fr_symbol;
18456 exp.X_add_number = fragp->fr_offset;
18457
18458 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18459 TRUE, reloc);
18460
18461 fixp->fx_file = fragp->fr_file;
18462 fixp->fx_line = fragp->fr_line;
18463 }
18464 else
18465 as_bad_where (fragp->fr_file, fragp->fr_line,
18466 _("invalid unextended operand value"));
18467 }
18468 else
18469 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18470 BFD_RELOC_UNUSED, val, user_length, &insn);
18471
18472 length = (ext ? 4 : 2);
18473 gas_assert (mips16_opcode_length (insn) == length);
18474 write_compressed_insn (buf, insn, length);
18475 fragp->fr_fix += length;
18476 }
18477 else
18478 {
18479 relax_substateT subtype = fragp->fr_subtype;
18480 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18481 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18482 int first, second;
18483 fixS *fixp;
18484
18485 first = RELAX_FIRST (subtype);
18486 second = RELAX_SECOND (subtype);
18487 fixp = (fixS *) fragp->fr_opcode;
18488
18489 /* If the delay slot chosen does not match the size of the instruction,
18490 then emit a warning. */
18491 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18492 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18493 {
18494 relax_substateT s;
18495 const char *msg;
18496
18497 s = subtype & (RELAX_DELAY_SLOT_16BIT
18498 | RELAX_DELAY_SLOT_SIZE_FIRST
18499 | RELAX_DELAY_SLOT_SIZE_SECOND);
18500 msg = macro_warning (s);
18501 if (msg != NULL)
18502 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18503 subtype &= ~s;
18504 }
18505
18506 /* Possibly emit a warning if we've chosen the longer option. */
18507 if (use_second == second_longer)
18508 {
18509 relax_substateT s;
18510 const char *msg;
18511
18512 s = (subtype
18513 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18514 msg = macro_warning (s);
18515 if (msg != NULL)
18516 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18517 subtype &= ~s;
18518 }
18519
18520 /* Go through all the fixups for the first sequence. Disable them
18521 (by marking them as done) if we're going to use the second
18522 sequence instead. */
18523 while (fixp
18524 && fixp->fx_frag == fragp
18525 && fixp->fx_where < fragp->fr_fix - second)
18526 {
18527 if (subtype & RELAX_USE_SECOND)
18528 fixp->fx_done = 1;
18529 fixp = fixp->fx_next;
18530 }
18531
18532 /* Go through the fixups for the second sequence. Disable them if
18533 we're going to use the first sequence, otherwise adjust their
18534 addresses to account for the relaxation. */
18535 while (fixp && fixp->fx_frag == fragp)
18536 {
18537 if (subtype & RELAX_USE_SECOND)
18538 fixp->fx_where -= first;
18539 else
18540 fixp->fx_done = 1;
18541 fixp = fixp->fx_next;
18542 }
18543
18544 /* Now modify the frag contents. */
18545 if (subtype & RELAX_USE_SECOND)
18546 {
18547 char *start;
18548
18549 start = fragp->fr_literal + fragp->fr_fix - first - second;
18550 memmove (start, start + first, second);
18551 fragp->fr_fix -= first;
18552 }
18553 else
18554 fragp->fr_fix -= second;
18555 }
18556 }
18557
18558 /* This function is called after the relocs have been generated.
18559 We've been storing mips16 text labels as odd. Here we convert them
18560 back to even for the convenience of the debugger. */
18561
18562 void
18563 mips_frob_file_after_relocs (void)
18564 {
18565 asymbol **syms;
18566 unsigned int count, i;
18567
18568 syms = bfd_get_outsymbols (stdoutput);
18569 count = bfd_get_symcount (stdoutput);
18570 for (i = 0; i < count; i++, syms++)
18571 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18572 && ((*syms)->value & 1) != 0)
18573 {
18574 (*syms)->value &= ~1;
18575 /* If the symbol has an odd size, it was probably computed
18576 incorrectly, so adjust that as well. */
18577 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18578 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18579 }
18580 }
18581
18582 /* This function is called whenever a label is defined, including fake
18583 labels instantiated off the dot special symbol. It is used when
18584 handling branch delays; if a branch has a label, we assume we cannot
18585 move it. This also bumps the value of the symbol by 1 in compressed
18586 code. */
18587
18588 static void
18589 mips_record_label (symbolS *sym)
18590 {
18591 segment_info_type *si = seg_info (now_seg);
18592 struct insn_label_list *l;
18593
18594 if (free_insn_labels == NULL)
18595 l = XNEW (struct insn_label_list);
18596 else
18597 {
18598 l = free_insn_labels;
18599 free_insn_labels = l->next;
18600 }
18601
18602 l->label = sym;
18603 l->next = si->label_list;
18604 si->label_list = l;
18605 }
18606
18607 /* This function is called as tc_frob_label() whenever a label is defined
18608 and adds a DWARF-2 record we only want for true labels. */
18609
18610 void
18611 mips_define_label (symbolS *sym)
18612 {
18613 mips_record_label (sym);
18614 dwarf2_emit_label (sym);
18615 }
18616
18617 /* This function is called by tc_new_dot_label whenever a new dot symbol
18618 is defined. */
18619
18620 void
18621 mips_add_dot_label (symbolS *sym)
18622 {
18623 mips_record_label (sym);
18624 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18625 mips_compressed_mark_label (sym);
18626 }
18627 \f
18628 /* Converting ASE flags from internal to .MIPS.abiflags values. */
18629 static unsigned int
18630 mips_convert_ase_flags (int ase)
18631 {
18632 unsigned int ext_ases = 0;
18633
18634 if (ase & ASE_DSP)
18635 ext_ases |= AFL_ASE_DSP;
18636 if (ase & ASE_DSPR2)
18637 ext_ases |= AFL_ASE_DSPR2;
18638 if (ase & ASE_DSPR3)
18639 ext_ases |= AFL_ASE_DSPR3;
18640 if (ase & ASE_EVA)
18641 ext_ases |= AFL_ASE_EVA;
18642 if (ase & ASE_MCU)
18643 ext_ases |= AFL_ASE_MCU;
18644 if (ase & ASE_MDMX)
18645 ext_ases |= AFL_ASE_MDMX;
18646 if (ase & ASE_MIPS3D)
18647 ext_ases |= AFL_ASE_MIPS3D;
18648 if (ase & ASE_MT)
18649 ext_ases |= AFL_ASE_MT;
18650 if (ase & ASE_SMARTMIPS)
18651 ext_ases |= AFL_ASE_SMARTMIPS;
18652 if (ase & ASE_VIRT)
18653 ext_ases |= AFL_ASE_VIRT;
18654 if (ase & ASE_MSA)
18655 ext_ases |= AFL_ASE_MSA;
18656 if (ase & ASE_XPA)
18657 ext_ases |= AFL_ASE_XPA;
18658
18659 return ext_ases;
18660 }
18661 /* Some special processing for a MIPS ELF file. */
18662
18663 void
18664 mips_elf_final_processing (void)
18665 {
18666 int fpabi;
18667 Elf_Internal_ABIFlags_v0 flags;
18668
18669 flags.version = 0;
18670 flags.isa_rev = 0;
18671 switch (file_mips_opts.isa)
18672 {
18673 case INSN_ISA1:
18674 flags.isa_level = 1;
18675 break;
18676 case INSN_ISA2:
18677 flags.isa_level = 2;
18678 break;
18679 case INSN_ISA3:
18680 flags.isa_level = 3;
18681 break;
18682 case INSN_ISA4:
18683 flags.isa_level = 4;
18684 break;
18685 case INSN_ISA5:
18686 flags.isa_level = 5;
18687 break;
18688 case INSN_ISA32:
18689 flags.isa_level = 32;
18690 flags.isa_rev = 1;
18691 break;
18692 case INSN_ISA32R2:
18693 flags.isa_level = 32;
18694 flags.isa_rev = 2;
18695 break;
18696 case INSN_ISA32R3:
18697 flags.isa_level = 32;
18698 flags.isa_rev = 3;
18699 break;
18700 case INSN_ISA32R5:
18701 flags.isa_level = 32;
18702 flags.isa_rev = 5;
18703 break;
18704 case INSN_ISA32R6:
18705 flags.isa_level = 32;
18706 flags.isa_rev = 6;
18707 break;
18708 case INSN_ISA64:
18709 flags.isa_level = 64;
18710 flags.isa_rev = 1;
18711 break;
18712 case INSN_ISA64R2:
18713 flags.isa_level = 64;
18714 flags.isa_rev = 2;
18715 break;
18716 case INSN_ISA64R3:
18717 flags.isa_level = 64;
18718 flags.isa_rev = 3;
18719 break;
18720 case INSN_ISA64R5:
18721 flags.isa_level = 64;
18722 flags.isa_rev = 5;
18723 break;
18724 case INSN_ISA64R6:
18725 flags.isa_level = 64;
18726 flags.isa_rev = 6;
18727 break;
18728 }
18729
18730 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
18731 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
18732 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
18733 : (file_mips_opts.fp == 64) ? AFL_REG_64
18734 : AFL_REG_32;
18735 flags.cpr2_size = AFL_REG_NONE;
18736 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
18737 Tag_GNU_MIPS_ABI_FP);
18738 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
18739 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
18740 if (file_ase_mips16)
18741 flags.ases |= AFL_ASE_MIPS16;
18742 if (file_ase_micromips)
18743 flags.ases |= AFL_ASE_MICROMIPS;
18744 flags.flags1 = 0;
18745 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
18746 || file_mips_opts.fp == 64)
18747 && file_mips_opts.oddspreg)
18748 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
18749 flags.flags2 = 0;
18750
18751 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
18752 ((Elf_External_ABIFlags_v0 *)
18753 mips_flags_frag));
18754
18755 /* Write out the register information. */
18756 if (mips_abi != N64_ABI)
18757 {
18758 Elf32_RegInfo s;
18759
18760 s.ri_gprmask = mips_gprmask;
18761 s.ri_cprmask[0] = mips_cprmask[0];
18762 s.ri_cprmask[1] = mips_cprmask[1];
18763 s.ri_cprmask[2] = mips_cprmask[2];
18764 s.ri_cprmask[3] = mips_cprmask[3];
18765 /* The gp_value field is set by the MIPS ELF backend. */
18766
18767 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18768 ((Elf32_External_RegInfo *)
18769 mips_regmask_frag));
18770 }
18771 else
18772 {
18773 Elf64_Internal_RegInfo s;
18774
18775 s.ri_gprmask = mips_gprmask;
18776 s.ri_pad = 0;
18777 s.ri_cprmask[0] = mips_cprmask[0];
18778 s.ri_cprmask[1] = mips_cprmask[1];
18779 s.ri_cprmask[2] = mips_cprmask[2];
18780 s.ri_cprmask[3] = mips_cprmask[3];
18781 /* The gp_value field is set by the MIPS ELF backend. */
18782
18783 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18784 ((Elf64_External_RegInfo *)
18785 mips_regmask_frag));
18786 }
18787
18788 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18789 sort of BFD interface for this. */
18790 if (mips_any_noreorder)
18791 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18792 if (mips_pic != NO_PIC)
18793 {
18794 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
18795 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18796 }
18797 if (mips_abicalls)
18798 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18799
18800 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
18801 defined at present; this might need to change in future. */
18802 if (file_ase_mips16)
18803 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
18804 if (file_ase_micromips)
18805 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
18806 if (file_mips_opts.ase & ASE_MDMX)
18807 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
18808
18809 /* Set the MIPS ELF ABI flags. */
18810 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
18811 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
18812 else if (mips_abi == O64_ABI)
18813 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
18814 else if (mips_abi == EABI_ABI)
18815 {
18816 if (file_mips_opts.gp == 64)
18817 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18818 else
18819 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18820 }
18821 else if (mips_abi == N32_ABI)
18822 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18823
18824 /* Nothing to do for N64_ABI. */
18825
18826 if (mips_32bitmode)
18827 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
18828
18829 if (mips_nan2008 == 1)
18830 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
18831
18832 /* 32 bit code with 64 bit FP registers. */
18833 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
18834 Tag_GNU_MIPS_ABI_FP);
18835 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
18836 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
18837 }
18838 \f
18839 typedef struct proc {
18840 symbolS *func_sym;
18841 symbolS *func_end_sym;
18842 unsigned long reg_mask;
18843 unsigned long reg_offset;
18844 unsigned long fpreg_mask;
18845 unsigned long fpreg_offset;
18846 unsigned long frame_offset;
18847 unsigned long frame_reg;
18848 unsigned long pc_reg;
18849 } procS;
18850
18851 static procS cur_proc;
18852 static procS *cur_proc_ptr;
18853 static int numprocs;
18854
18855 /* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
18856 as "2", and a normal nop as "0". */
18857
18858 #define NOP_OPCODE_MIPS 0
18859 #define NOP_OPCODE_MIPS16 1
18860 #define NOP_OPCODE_MICROMIPS 2
18861
18862 char
18863 mips_nop_opcode (void)
18864 {
18865 if (seg_info (now_seg)->tc_segment_info_data.micromips)
18866 return NOP_OPCODE_MICROMIPS;
18867 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18868 return NOP_OPCODE_MIPS16;
18869 else
18870 return NOP_OPCODE_MIPS;
18871 }
18872
18873 /* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
18874 32-bit microMIPS NOPs here (if applicable). */
18875
18876 void
18877 mips_handle_align (fragS *fragp)
18878 {
18879 char nop_opcode;
18880 char *p;
18881 int bytes, size, excess;
18882 valueT opcode;
18883
18884 if (fragp->fr_type != rs_align_code)
18885 return;
18886
18887 p = fragp->fr_literal + fragp->fr_fix;
18888 nop_opcode = *p;
18889 switch (nop_opcode)
18890 {
18891 case NOP_OPCODE_MICROMIPS:
18892 opcode = micromips_nop32_insn.insn_opcode;
18893 size = 4;
18894 break;
18895 case NOP_OPCODE_MIPS16:
18896 opcode = mips16_nop_insn.insn_opcode;
18897 size = 2;
18898 break;
18899 case NOP_OPCODE_MIPS:
18900 default:
18901 opcode = nop_insn.insn_opcode;
18902 size = 4;
18903 break;
18904 }
18905
18906 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18907 excess = bytes % size;
18908
18909 /* Handle the leading part if we're not inserting a whole number of
18910 instructions, and make it the end of the fixed part of the frag.
18911 Try to fit in a short microMIPS NOP if applicable and possible,
18912 and use zeroes otherwise. */
18913 gas_assert (excess < 4);
18914 fragp->fr_fix += excess;
18915 switch (excess)
18916 {
18917 case 3:
18918 *p++ = '\0';
18919 /* Fall through. */
18920 case 2:
18921 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
18922 {
18923 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
18924 break;
18925 }
18926 *p++ = '\0';
18927 /* Fall through. */
18928 case 1:
18929 *p++ = '\0';
18930 /* Fall through. */
18931 case 0:
18932 break;
18933 }
18934
18935 md_number_to_chars (p, opcode, size);
18936 fragp->fr_var = size;
18937 }
18938
18939 static long
18940 get_number (void)
18941 {
18942 int negative = 0;
18943 long val = 0;
18944
18945 if (*input_line_pointer == '-')
18946 {
18947 ++input_line_pointer;
18948 negative = 1;
18949 }
18950 if (!ISDIGIT (*input_line_pointer))
18951 as_bad (_("expected simple number"));
18952 if (input_line_pointer[0] == '0')
18953 {
18954 if (input_line_pointer[1] == 'x')
18955 {
18956 input_line_pointer += 2;
18957 while (ISXDIGIT (*input_line_pointer))
18958 {
18959 val <<= 4;
18960 val |= hex_value (*input_line_pointer++);
18961 }
18962 return negative ? -val : val;
18963 }
18964 else
18965 {
18966 ++input_line_pointer;
18967 while (ISDIGIT (*input_line_pointer))
18968 {
18969 val <<= 3;
18970 val |= *input_line_pointer++ - '0';
18971 }
18972 return negative ? -val : val;
18973 }
18974 }
18975 if (!ISDIGIT (*input_line_pointer))
18976 {
18977 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18978 *input_line_pointer, *input_line_pointer);
18979 as_warn (_("invalid number"));
18980 return -1;
18981 }
18982 while (ISDIGIT (*input_line_pointer))
18983 {
18984 val *= 10;
18985 val += *input_line_pointer++ - '0';
18986 }
18987 return negative ? -val : val;
18988 }
18989
18990 /* The .file directive; just like the usual .file directive, but there
18991 is an initial number which is the ECOFF file index. In the non-ECOFF
18992 case .file implies DWARF-2. */
18993
18994 static void
18995 s_mips_file (int x ATTRIBUTE_UNUSED)
18996 {
18997 static int first_file_directive = 0;
18998
18999 if (ECOFF_DEBUGGING)
19000 {
19001 get_number ();
19002 s_app_file (0);
19003 }
19004 else
19005 {
19006 char *filename;
19007
19008 filename = dwarf2_directive_file (0);
19009
19010 /* Versions of GCC up to 3.1 start files with a ".file"
19011 directive even for stabs output. Make sure that this
19012 ".file" is handled. Note that you need a version of GCC
19013 after 3.1 in order to support DWARF-2 on MIPS. */
19014 if (filename != NULL && ! first_file_directive)
19015 {
19016 (void) new_logical_line (filename, -1);
19017 s_app_file_string (filename, 0);
19018 }
19019 first_file_directive = 1;
19020 }
19021 }
19022
19023 /* The .loc directive, implying DWARF-2. */
19024
19025 static void
19026 s_mips_loc (int x ATTRIBUTE_UNUSED)
19027 {
19028 if (!ECOFF_DEBUGGING)
19029 dwarf2_directive_loc (0);
19030 }
19031
19032 /* The .end directive. */
19033
19034 static void
19035 s_mips_end (int x ATTRIBUTE_UNUSED)
19036 {
19037 symbolS *p;
19038
19039 /* Following functions need their own .frame and .cprestore directives. */
19040 mips_frame_reg_valid = 0;
19041 mips_cprestore_valid = 0;
19042
19043 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19044 {
19045 p = get_symbol ();
19046 demand_empty_rest_of_line ();
19047 }
19048 else
19049 p = NULL;
19050
19051 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19052 as_warn (_(".end not in text section"));
19053
19054 if (!cur_proc_ptr)
19055 {
19056 as_warn (_(".end directive without a preceding .ent directive"));
19057 demand_empty_rest_of_line ();
19058 return;
19059 }
19060
19061 if (p != NULL)
19062 {
19063 gas_assert (S_GET_NAME (p));
19064 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
19065 as_warn (_(".end symbol does not match .ent symbol"));
19066
19067 if (debug_type == DEBUG_STABS)
19068 stabs_generate_asm_endfunc (S_GET_NAME (p),
19069 S_GET_NAME (p));
19070 }
19071 else
19072 as_warn (_(".end directive missing or unknown symbol"));
19073
19074 /* Create an expression to calculate the size of the function. */
19075 if (p && cur_proc_ptr)
19076 {
19077 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19078 expressionS *exp = XNEW (expressionS);
19079
19080 obj->size = exp;
19081 exp->X_op = O_subtract;
19082 exp->X_add_symbol = symbol_temp_new_now ();
19083 exp->X_op_symbol = p;
19084 exp->X_add_number = 0;
19085
19086 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19087 }
19088
19089 #ifdef md_flush_pending_output
19090 md_flush_pending_output ();
19091 #endif
19092
19093 /* Generate a .pdr section. */
19094 if (!ECOFF_DEBUGGING && mips_flag_pdr)
19095 {
19096 segT saved_seg = now_seg;
19097 subsegT saved_subseg = now_subseg;
19098 expressionS exp;
19099 char *fragp;
19100
19101 gas_assert (pdr_seg);
19102 subseg_set (pdr_seg, 0);
19103
19104 /* Write the symbol. */
19105 exp.X_op = O_symbol;
19106 exp.X_add_symbol = p;
19107 exp.X_add_number = 0;
19108 emit_expr (&exp, 4);
19109
19110 fragp = frag_more (7 * 4);
19111
19112 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19113 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19114 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19115 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19116 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19117 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19118 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19119
19120 subseg_set (saved_seg, saved_subseg);
19121 }
19122
19123 cur_proc_ptr = NULL;
19124 }
19125
19126 /* The .aent and .ent directives. */
19127
19128 static void
19129 s_mips_ent (int aent)
19130 {
19131 symbolS *symbolP;
19132
19133 symbolP = get_symbol ();
19134 if (*input_line_pointer == ',')
19135 ++input_line_pointer;
19136 SKIP_WHITESPACE ();
19137 if (ISDIGIT (*input_line_pointer)
19138 || *input_line_pointer == '-')
19139 get_number ();
19140
19141 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19142 as_warn (_(".ent or .aent not in text section"));
19143
19144 if (!aent && cur_proc_ptr)
19145 as_warn (_("missing .end"));
19146
19147 if (!aent)
19148 {
19149 /* This function needs its own .frame and .cprestore directives. */
19150 mips_frame_reg_valid = 0;
19151 mips_cprestore_valid = 0;
19152
19153 cur_proc_ptr = &cur_proc;
19154 memset (cur_proc_ptr, '\0', sizeof (procS));
19155
19156 cur_proc_ptr->func_sym = symbolP;
19157
19158 ++numprocs;
19159
19160 if (debug_type == DEBUG_STABS)
19161 stabs_generate_asm_func (S_GET_NAME (symbolP),
19162 S_GET_NAME (symbolP));
19163 }
19164
19165 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19166
19167 demand_empty_rest_of_line ();
19168 }
19169
19170 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19171 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19172 s_mips_frame is used so that we can set the PDR information correctly.
19173 We can't use the ecoff routines because they make reference to the ecoff
19174 symbol table (in the mdebug section). */
19175
19176 static void
19177 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19178 {
19179 if (ECOFF_DEBUGGING)
19180 s_ignore (ignore);
19181 else
19182 {
19183 long val;
19184
19185 if (cur_proc_ptr == (procS *) NULL)
19186 {
19187 as_warn (_(".frame outside of .ent"));
19188 demand_empty_rest_of_line ();
19189 return;
19190 }
19191
19192 cur_proc_ptr->frame_reg = tc_get_register (1);
19193
19194 SKIP_WHITESPACE ();
19195 if (*input_line_pointer++ != ','
19196 || get_absolute_expression_and_terminator (&val) != ',')
19197 {
19198 as_warn (_("bad .frame directive"));
19199 --input_line_pointer;
19200 demand_empty_rest_of_line ();
19201 return;
19202 }
19203
19204 cur_proc_ptr->frame_offset = val;
19205 cur_proc_ptr->pc_reg = tc_get_register (0);
19206
19207 demand_empty_rest_of_line ();
19208 }
19209 }
19210
19211 /* The .fmask and .mask directives. If the mdebug section is present
19212 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19213 embedded targets, s_mips_mask is used so that we can set the PDR
19214 information correctly. We can't use the ecoff routines because they
19215 make reference to the ecoff symbol table (in the mdebug section). */
19216
19217 static void
19218 s_mips_mask (int reg_type)
19219 {
19220 if (ECOFF_DEBUGGING)
19221 s_ignore (reg_type);
19222 else
19223 {
19224 long mask, off;
19225
19226 if (cur_proc_ptr == (procS *) NULL)
19227 {
19228 as_warn (_(".mask/.fmask outside of .ent"));
19229 demand_empty_rest_of_line ();
19230 return;
19231 }
19232
19233 if (get_absolute_expression_and_terminator (&mask) != ',')
19234 {
19235 as_warn (_("bad .mask/.fmask directive"));
19236 --input_line_pointer;
19237 demand_empty_rest_of_line ();
19238 return;
19239 }
19240
19241 off = get_absolute_expression ();
19242
19243 if (reg_type == 'F')
19244 {
19245 cur_proc_ptr->fpreg_mask = mask;
19246 cur_proc_ptr->fpreg_offset = off;
19247 }
19248 else
19249 {
19250 cur_proc_ptr->reg_mask = mask;
19251 cur_proc_ptr->reg_offset = off;
19252 }
19253
19254 demand_empty_rest_of_line ();
19255 }
19256 }
19257
19258 /* A table describing all the processors gas knows about. Names are
19259 matched in the order listed.
19260
19261 To ease comparison, please keep this table in the same order as
19262 gcc's mips_cpu_info_table[]. */
19263 static const struct mips_cpu_info mips_cpu_info_table[] =
19264 {
19265 /* Entries for generic ISAs */
19266 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19267 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19268 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19269 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19270 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19271 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19272 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19273 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19274 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
19275 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
19276 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19277 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
19278 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19279 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
19280 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
19281
19282 /* MIPS I */
19283 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19284 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19285 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
19286
19287 /* MIPS II */
19288 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
19289
19290 /* MIPS III */
19291 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19292 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19293 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19294 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19295 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19296 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19297 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19298 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19299 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19300 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19301 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19302 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19303 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
19304 /* ST Microelectronics Loongson 2E and 2F cores */
19305 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19306 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
19307
19308 /* MIPS IV */
19309 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19310 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19311 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19312 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19313 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19314 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19315 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19316 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19317 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19318 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19319 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19320 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19321 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19322 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19323 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
19324
19325 /* MIPS 32 */
19326 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19327 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19328 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19329 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19330
19331 /* MIPS 32 Release 2 */
19332 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19333 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19334 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19335 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19336 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19337 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19338 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19339 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19340 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19341 ISA_MIPS32R2, CPU_MIPS32R2 },
19342 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19343 ISA_MIPS32R2, CPU_MIPS32R2 },
19344 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19345 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19346 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19347 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19348 /* Deprecated forms of the above. */
19349 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19350 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19351 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
19352 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19353 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19354 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19355 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19356 /* Deprecated forms of the above. */
19357 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19358 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19359 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
19360 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19361 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19362 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19363 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19364 /* Deprecated forms of the above. */
19365 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19366 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19367 /* 34Kn is a 34kc without DSP. */
19368 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19369 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19370 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19371 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19372 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19373 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19374 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19375 /* Deprecated forms of the above. */
19376 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19377 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19378 /* 1004K cores are multiprocessor versions of the 34K. */
19379 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19380 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19381 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19382 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19383 /* interaptiv is the new name for 1004kf */
19384 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19385 /* M5100 family */
19386 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19387 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19388 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
19389 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
19390
19391 /* MIPS 64 */
19392 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19393 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19394 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19395 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19396
19397 /* Broadcom SB-1 CPU core */
19398 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19399 /* Broadcom SB-1A CPU core */
19400 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19401
19402 { "loongson3a", 0, 0, ISA_MIPS64R2, CPU_LOONGSON_3A },
19403
19404 /* MIPS 64 Release 2 */
19405
19406 /* Cavium Networks Octeon CPU core */
19407 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19408 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19409 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
19410 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
19411
19412 /* RMI Xlr */
19413 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
19414
19415 /* Broadcom XLP.
19416 XLP is mostly like XLR, with the prominent exception that it is
19417 MIPS64R2 rather than MIPS64. */
19418 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
19419
19420 /* MIPS 64 Release 6 */
19421 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
19422 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
19423
19424 /* End marker */
19425 { NULL, 0, 0, 0, 0 }
19426 };
19427
19428
19429 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19430 with a final "000" replaced by "k". Ignore case.
19431
19432 Note: this function is shared between GCC and GAS. */
19433
19434 static bfd_boolean
19435 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19436 {
19437 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19438 given++, canonical++;
19439
19440 return ((*given == 0 && *canonical == 0)
19441 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19442 }
19443
19444
19445 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19446 CPU name. We've traditionally allowed a lot of variation here.
19447
19448 Note: this function is shared between GCC and GAS. */
19449
19450 static bfd_boolean
19451 mips_matching_cpu_name_p (const char *canonical, const char *given)
19452 {
19453 /* First see if the name matches exactly, or with a final "000"
19454 turned into "k". */
19455 if (mips_strict_matching_cpu_name_p (canonical, given))
19456 return TRUE;
19457
19458 /* If not, try comparing based on numerical designation alone.
19459 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19460 if (TOLOWER (*given) == 'r')
19461 given++;
19462 if (!ISDIGIT (*given))
19463 return FALSE;
19464
19465 /* Skip over some well-known prefixes in the canonical name,
19466 hoping to find a number there too. */
19467 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19468 canonical += 2;
19469 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19470 canonical += 2;
19471 else if (TOLOWER (canonical[0]) == 'r')
19472 canonical += 1;
19473
19474 return mips_strict_matching_cpu_name_p (canonical, given);
19475 }
19476
19477
19478 /* Parse an option that takes the name of a processor as its argument.
19479 OPTION is the name of the option and CPU_STRING is the argument.
19480 Return the corresponding processor enumeration if the CPU_STRING is
19481 recognized, otherwise report an error and return null.
19482
19483 A similar function exists in GCC. */
19484
19485 static const struct mips_cpu_info *
19486 mips_parse_cpu (const char *option, const char *cpu_string)
19487 {
19488 const struct mips_cpu_info *p;
19489
19490 /* 'from-abi' selects the most compatible architecture for the given
19491 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19492 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19493 version. Look first at the -mgp options, if given, otherwise base
19494 the choice on MIPS_DEFAULT_64BIT.
19495
19496 Treat NO_ABI like the EABIs. One reason to do this is that the
19497 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19498 architecture. This code picks MIPS I for 'mips' and MIPS III for
19499 'mips64', just as we did in the days before 'from-abi'. */
19500 if (strcasecmp (cpu_string, "from-abi") == 0)
19501 {
19502 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19503 return mips_cpu_info_from_isa (ISA_MIPS1);
19504
19505 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19506 return mips_cpu_info_from_isa (ISA_MIPS3);
19507
19508 if (file_mips_opts.gp >= 0)
19509 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
19510 ? ISA_MIPS1 : ISA_MIPS3);
19511
19512 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19513 ? ISA_MIPS3
19514 : ISA_MIPS1);
19515 }
19516
19517 /* 'default' has traditionally been a no-op. Probably not very useful. */
19518 if (strcasecmp (cpu_string, "default") == 0)
19519 return 0;
19520
19521 for (p = mips_cpu_info_table; p->name != 0; p++)
19522 if (mips_matching_cpu_name_p (p->name, cpu_string))
19523 return p;
19524
19525 as_bad (_("bad value (%s) for %s"), cpu_string, option);
19526 return 0;
19527 }
19528
19529 /* Return the canonical processor information for ISA (a member of the
19530 ISA_MIPS* enumeration). */
19531
19532 static const struct mips_cpu_info *
19533 mips_cpu_info_from_isa (int isa)
19534 {
19535 int i;
19536
19537 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19538 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19539 && isa == mips_cpu_info_table[i].isa)
19540 return (&mips_cpu_info_table[i]);
19541
19542 return NULL;
19543 }
19544
19545 static const struct mips_cpu_info *
19546 mips_cpu_info_from_arch (int arch)
19547 {
19548 int i;
19549
19550 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19551 if (arch == mips_cpu_info_table[i].cpu)
19552 return (&mips_cpu_info_table[i]);
19553
19554 return NULL;
19555 }
19556 \f
19557 static void
19558 show (FILE *stream, const char *string, int *col_p, int *first_p)
19559 {
19560 if (*first_p)
19561 {
19562 fprintf (stream, "%24s", "");
19563 *col_p = 24;
19564 }
19565 else
19566 {
19567 fprintf (stream, ", ");
19568 *col_p += 2;
19569 }
19570
19571 if (*col_p + strlen (string) > 72)
19572 {
19573 fprintf (stream, "\n%24s", "");
19574 *col_p = 24;
19575 }
19576
19577 fprintf (stream, "%s", string);
19578 *col_p += strlen (string);
19579
19580 *first_p = 0;
19581 }
19582
19583 void
19584 md_show_usage (FILE *stream)
19585 {
19586 int column, first;
19587 size_t i;
19588
19589 fprintf (stream, _("\
19590 MIPS options:\n\
19591 -EB generate big endian output\n\
19592 -EL generate little endian output\n\
19593 -g, -g2 do not remove unneeded NOPs or swap branches\n\
19594 -G NUM allow referencing objects up to NUM bytes\n\
19595 implicitly with the gp register [default 8]\n"));
19596 fprintf (stream, _("\
19597 -mips1 generate MIPS ISA I instructions\n\
19598 -mips2 generate MIPS ISA II instructions\n\
19599 -mips3 generate MIPS ISA III instructions\n\
19600 -mips4 generate MIPS ISA IV instructions\n\
19601 -mips5 generate MIPS ISA V instructions\n\
19602 -mips32 generate MIPS32 ISA instructions\n\
19603 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
19604 -mips32r3 generate MIPS32 release 3 ISA instructions\n\
19605 -mips32r5 generate MIPS32 release 5 ISA instructions\n\
19606 -mips32r6 generate MIPS32 release 6 ISA instructions\n\
19607 -mips64 generate MIPS64 ISA instructions\n\
19608 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
19609 -mips64r3 generate MIPS64 release 3 ISA instructions\n\
19610 -mips64r5 generate MIPS64 release 5 ISA instructions\n\
19611 -mips64r6 generate MIPS64 release 6 ISA instructions\n\
19612 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19613
19614 first = 1;
19615
19616 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19617 show (stream, mips_cpu_info_table[i].name, &column, &first);
19618 show (stream, "from-abi", &column, &first);
19619 fputc ('\n', stream);
19620
19621 fprintf (stream, _("\
19622 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19623 -no-mCPU don't generate code specific to CPU.\n\
19624 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19625
19626 first = 1;
19627
19628 show (stream, "3900", &column, &first);
19629 show (stream, "4010", &column, &first);
19630 show (stream, "4100", &column, &first);
19631 show (stream, "4650", &column, &first);
19632 fputc ('\n', stream);
19633
19634 fprintf (stream, _("\
19635 -mips16 generate mips16 instructions\n\
19636 -no-mips16 do not generate mips16 instructions\n"));
19637 fprintf (stream, _("\
19638 -mmicromips generate microMIPS instructions\n\
19639 -mno-micromips do not generate microMIPS instructions\n"));
19640 fprintf (stream, _("\
19641 -msmartmips generate smartmips instructions\n\
19642 -mno-smartmips do not generate smartmips instructions\n"));
19643 fprintf (stream, _("\
19644 -mdsp generate DSP instructions\n\
19645 -mno-dsp do not generate DSP instructions\n"));
19646 fprintf (stream, _("\
19647 -mdspr2 generate DSP R2 instructions\n\
19648 -mno-dspr2 do not generate DSP R2 instructions\n"));
19649 fprintf (stream, _("\
19650 -mdspr3 generate DSP R3 instructions\n\
19651 -mno-dspr3 do not generate DSP R3 instructions\n"));
19652 fprintf (stream, _("\
19653 -mmt generate MT instructions\n\
19654 -mno-mt do not generate MT instructions\n"));
19655 fprintf (stream, _("\
19656 -mmcu generate MCU instructions\n\
19657 -mno-mcu do not generate MCU instructions\n"));
19658 fprintf (stream, _("\
19659 -mmsa generate MSA instructions\n\
19660 -mno-msa do not generate MSA instructions\n"));
19661 fprintf (stream, _("\
19662 -mxpa generate eXtended Physical Address (XPA) instructions\n\
19663 -mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
19664 fprintf (stream, _("\
19665 -mvirt generate Virtualization instructions\n\
19666 -mno-virt do not generate Virtualization instructions\n"));
19667 fprintf (stream, _("\
19668 -minsn32 only generate 32-bit microMIPS instructions\n\
19669 -mno-insn32 generate all microMIPS instructions\n"));
19670 fprintf (stream, _("\
19671 -mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19672 -mfix-loongson2f-nop work around Loongson2F NOP errata\n\
19673 -mfix-vr4120 work around certain VR4120 errata\n\
19674 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
19675 -mfix-24k insert a nop after ERET and DERET instructions\n\
19676 -mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
19677 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19678 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
19679 -msym32 assume all symbols have 32-bit values\n\
19680 -O0 remove unneeded NOPs, do not swap branches\n\
19681 -O remove unneeded NOPs and swap branches\n\
19682 --trap, --no-break trap exception on div by 0 and mult overflow\n\
19683 --break, --no-trap break exception on div by 0 and mult overflow\n"));
19684 fprintf (stream, _("\
19685 -mhard-float allow floating-point instructions\n\
19686 -msoft-float do not allow floating-point instructions\n\
19687 -msingle-float only allow 32-bit floating-point operations\n\
19688 -mdouble-float allow 32-bit and 64-bit floating-point operations\n\
19689 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
19690 --[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
19691 -mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
19692 -mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
19693 -mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
19694
19695 first = 1;
19696
19697 show (stream, "legacy", &column, &first);
19698 show (stream, "2008", &column, &first);
19699
19700 fputc ('\n', stream);
19701
19702 fprintf (stream, _("\
19703 -KPIC, -call_shared generate SVR4 position independent code\n\
19704 -call_nonpic generate non-PIC code that can operate with DSOs\n\
19705 -mvxworks-pic generate VxWorks position independent code\n\
19706 -non_shared do not generate code that can operate with DSOs\n\
19707 -xgot assume a 32 bit GOT\n\
19708 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
19709 -mshared, -mno-shared disable/enable .cpload optimization for\n\
19710 position dependent (non shared) code\n\
19711 -mabi=ABI create ABI conformant object file for:\n"));
19712
19713 first = 1;
19714
19715 show (stream, "32", &column, &first);
19716 show (stream, "o64", &column, &first);
19717 show (stream, "n32", &column, &first);
19718 show (stream, "64", &column, &first);
19719 show (stream, "eabi", &column, &first);
19720
19721 fputc ('\n', stream);
19722
19723 fprintf (stream, _("\
19724 -32 create o32 ABI object file (default)\n\
19725 -n32 create n32 ABI object file\n\
19726 -64 create 64 ABI object file\n"));
19727 }
19728
19729 #ifdef TE_IRIX
19730 enum dwarf2_format
19731 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
19732 {
19733 if (HAVE_64BIT_SYMBOLS)
19734 return dwarf2_format_64bit_irix;
19735 else
19736 return dwarf2_format_32bit;
19737 }
19738 #endif
19739
19740 int
19741 mips_dwarf2_addr_size (void)
19742 {
19743 if (HAVE_64BIT_OBJECTS)
19744 return 8;
19745 else
19746 return 4;
19747 }
19748
19749 /* Standard calling conventions leave the CFA at SP on entry. */
19750 void
19751 mips_cfi_frame_initial_instructions (void)
19752 {
19753 cfi_add_CFA_def_cfa_register (SP);
19754 }
19755
19756 int
19757 tc_mips_regname_to_dw2regnum (char *regname)
19758 {
19759 unsigned int regnum = -1;
19760 unsigned int reg;
19761
19762 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19763 regnum = reg;
19764
19765 return regnum;
19766 }
19767
19768 /* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
19769 Given a symbolic attribute NAME, return the proper integer value.
19770 Returns -1 if the attribute is not known. */
19771
19772 int
19773 mips_convert_symbolic_attribute (const char *name)
19774 {
19775 static const struct
19776 {
19777 const char * name;
19778 const int tag;
19779 }
19780 attribute_table[] =
19781 {
19782 #define T(tag) {#tag, tag}
19783 T (Tag_GNU_MIPS_ABI_FP),
19784 T (Tag_GNU_MIPS_ABI_MSA),
19785 #undef T
19786 };
19787 unsigned int i;
19788
19789 if (name == NULL)
19790 return -1;
19791
19792 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
19793 if (streq (name, attribute_table[i].name))
19794 return attribute_table[i].tag;
19795
19796 return -1;
19797 }
19798
19799 void
19800 md_mips_end (void)
19801 {
19802 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
19803
19804 mips_emit_delays ();
19805 if (cur_proc_ptr)
19806 as_warn (_("missing .end at end of assembly"));
19807
19808 /* Just in case no code was emitted, do the consistency check. */
19809 file_mips_check_options ();
19810
19811 /* Set a floating-point ABI if the user did not. */
19812 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
19813 {
19814 /* Perform consistency checks on the floating-point ABI. */
19815 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19816 Tag_GNU_MIPS_ABI_FP);
19817 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
19818 check_fpabi (fpabi);
19819 }
19820 else
19821 {
19822 /* Soft-float gets precedence over single-float, the two options should
19823 not be used together so this should not matter. */
19824 if (file_mips_opts.soft_float == 1)
19825 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
19826 /* Single-float gets precedence over all double_float cases. */
19827 else if (file_mips_opts.single_float == 1)
19828 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
19829 else
19830 {
19831 switch (file_mips_opts.fp)
19832 {
19833 case 32:
19834 if (file_mips_opts.gp == 32)
19835 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
19836 break;
19837 case 0:
19838 fpabi = Val_GNU_MIPS_ABI_FP_XX;
19839 break;
19840 case 64:
19841 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
19842 fpabi = Val_GNU_MIPS_ABI_FP_64A;
19843 else if (file_mips_opts.gp == 32)
19844 fpabi = Val_GNU_MIPS_ABI_FP_64;
19845 else
19846 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
19847 break;
19848 }
19849 }
19850
19851 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19852 Tag_GNU_MIPS_ABI_FP, fpabi);
19853 }
19854 }
19855
19856 /* Returns the relocation type required for a particular CFI encoding. */
19857
19858 bfd_reloc_code_real_type
19859 mips_cfi_reloc_for_encoding (int encoding)
19860 {
19861 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
19862 return BFD_RELOC_32_PCREL;
19863 else return BFD_RELOC_NONE;
19864 }
This page took 0.567887 seconds and 4 git commands to generate.