This set of changes clarifies the conditions for the R5900 short loop fix and extends...
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright (C) 1993-2018 Free Software Foundation, Inc.
3 Contributed by the OSF and Ralph Campbell.
4 Written by Keith Knowles and Ralph Campbell, working independently.
5 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6 Support.
7
8 This file is part of GAS.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
24
25 #include "as.h"
26 #include "config.h"
27 #include "subsegs.h"
28 #include "safe-ctype.h"
29
30 #include "opcode/mips.h"
31 #include "itbl-ops.h"
32 #include "dwarf2dbg.h"
33 #include "dw2gencfi.h"
34
35 /* Check assumptions made in this file. */
36 typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
37 typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
38
39 #ifdef DEBUG
40 #define DBG(x) printf x
41 #else
42 #define DBG(x)
43 #endif
44
45 #define streq(a, b) (strcmp (a, b) == 0)
46
47 #define SKIP_SPACE_TABS(S) \
48 do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49
50 /* Clean up namespace so we can include obj-elf.h too. */
51 static int mips_output_flavor (void);
52 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
53 #undef OBJ_PROCESS_STAB
54 #undef OUTPUT_FLAVOR
55 #undef S_GET_ALIGN
56 #undef S_GET_SIZE
57 #undef S_SET_ALIGN
58 #undef S_SET_SIZE
59 #undef obj_frob_file
60 #undef obj_frob_file_after_relocs
61 #undef obj_frob_symbol
62 #undef obj_pop_insert
63 #undef obj_sec_sym_ok_for_reloc
64 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
65
66 #include "obj-elf.h"
67 /* Fix any of them that we actually care about. */
68 #undef OUTPUT_FLAVOR
69 #define OUTPUT_FLAVOR mips_output_flavor()
70
71 #include "elf/mips.h"
72
73 #ifndef ECOFF_DEBUGGING
74 #define NO_ECOFF_DEBUGGING
75 #define ECOFF_DEBUGGING 0
76 #endif
77
78 int mips_flag_mdebug = -1;
79
80 /* Control generation of .pdr sections. Off by default on IRIX: the native
81 linker doesn't know about and discards them, but relocations against them
82 remain, leading to rld crashes. */
83 #ifdef TE_IRIX
84 int mips_flag_pdr = FALSE;
85 #else
86 int mips_flag_pdr = TRUE;
87 #endif
88
89 #include "ecoff.h"
90
91 static char *mips_regmask_frag;
92 static char *mips_flags_frag;
93
94 #define ZERO 0
95 #define ATREG 1
96 #define S0 16
97 #define S7 23
98 #define TREG 24
99 #define PIC_CALL_REG 25
100 #define KT0 26
101 #define KT1 27
102 #define GP 28
103 #define SP 29
104 #define FP 30
105 #define RA 31
106
107 #define ILLEGAL_REG (32)
108
109 #define AT mips_opts.at
110
111 extern int target_big_endian;
112
113 /* The name of the readonly data section. */
114 #define RDATA_SECTION_NAME ".rodata"
115
116 /* Ways in which an instruction can be "appended" to the output. */
117 enum append_method {
118 /* Just add it normally. */
119 APPEND_ADD,
120
121 /* Add it normally and then add a nop. */
122 APPEND_ADD_WITH_NOP,
123
124 /* Turn an instruction with a delay slot into a "compact" version. */
125 APPEND_ADD_COMPACT,
126
127 /* Insert the instruction before the last one. */
128 APPEND_SWAP
129 };
130
131 /* Information about an instruction, including its format, operands
132 and fixups. */
133 struct mips_cl_insn
134 {
135 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
136 const struct mips_opcode *insn_mo;
137
138 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
139 a copy of INSN_MO->match with the operands filled in. If we have
140 decided to use an extended MIPS16 instruction, this includes the
141 extension. */
142 unsigned long insn_opcode;
143
144 /* The frag that contains the instruction. */
145 struct frag *frag;
146
147 /* The offset into FRAG of the first instruction byte. */
148 long where;
149
150 /* The relocs associated with the instruction, if any. */
151 fixS *fixp[3];
152
153 /* True if this entry cannot be moved from its current position. */
154 unsigned int fixed_p : 1;
155
156 /* True if this instruction occurred in a .set noreorder block. */
157 unsigned int noreorder_p : 1;
158
159 /* True for mips16 instructions that jump to an absolute address. */
160 unsigned int mips16_absolute_jump_p : 1;
161
162 /* True if this instruction is complete. */
163 unsigned int complete_p : 1;
164
165 /* True if this instruction is cleared from history by unconditional
166 branch. */
167 unsigned int cleared_p : 1;
168 };
169
170 /* The ABI to use. */
171 enum mips_abi_level
172 {
173 NO_ABI = 0,
174 O32_ABI,
175 O64_ABI,
176 N32_ABI,
177 N64_ABI,
178 EABI_ABI
179 };
180
181 /* MIPS ABI we are using for this output file. */
182 static enum mips_abi_level mips_abi = NO_ABI;
183
184 /* Whether or not we have code that can call pic code. */
185 int mips_abicalls = FALSE;
186
187 /* Whether or not we have code which can be put into a shared
188 library. */
189 static bfd_boolean mips_in_shared = TRUE;
190
191 /* This is the set of options which may be modified by the .set
192 pseudo-op. We use a struct so that .set push and .set pop are more
193 reliable. */
194
195 struct mips_set_options
196 {
197 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
198 if it has not been initialized. Changed by `.set mipsN', and the
199 -mipsN command line option, and the default CPU. */
200 int isa;
201 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
202 <asename>', by command line options, and based on the default
203 architecture. */
204 int ase;
205 /* Whether we are assembling for the mips16 processor. 0 if we are
206 not, 1 if we are, and -1 if the value has not been initialized.
207 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
208 -nomips16 command line options, and the default CPU. */
209 int mips16;
210 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
211 1 if we are, and -1 if the value has not been initialized. Changed
212 by `.set micromips' and `.set nomicromips', and the -mmicromips
213 and -mno-micromips command line options, and the default CPU. */
214 int micromips;
215 /* Non-zero if we should not reorder instructions. Changed by `.set
216 reorder' and `.set noreorder'. */
217 int noreorder;
218 /* Non-zero if we should not permit the register designated "assembler
219 temporary" to be used in instructions. The value is the register
220 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
221 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
222 unsigned int at;
223 /* Non-zero if we should warn when a macro instruction expands into
224 more than one machine instruction. Changed by `.set nomacro' and
225 `.set macro'. */
226 int warn_about_macros;
227 /* Non-zero if we should not move instructions. Changed by `.set
228 move', `.set volatile', `.set nomove', and `.set novolatile'. */
229 int nomove;
230 /* Non-zero if we should not optimize branches by moving the target
231 of the branch into the delay slot. Actually, we don't perform
232 this optimization anyhow. Changed by `.set bopt' and `.set
233 nobopt'. */
234 int nobopt;
235 /* Non-zero if we should not autoextend mips16 instructions.
236 Changed by `.set autoextend' and `.set noautoextend'. */
237 int noautoextend;
238 /* True if we should only emit 32-bit microMIPS instructions.
239 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
240 and -mno-insn32 command line options. */
241 bfd_boolean insn32;
242 /* Restrict general purpose registers and floating point registers
243 to 32 bit. This is initially determined when -mgp32 or -mfp32
244 is passed but can changed if the assembler code uses .set mipsN. */
245 int gp;
246 int fp;
247 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
248 command line option, and the default CPU. */
249 int arch;
250 /* True if ".set sym32" is in effect. */
251 bfd_boolean sym32;
252 /* True if floating-point operations are not allowed. Changed by .set
253 softfloat or .set hardfloat, by command line options -msoft-float or
254 -mhard-float. The default is false. */
255 bfd_boolean soft_float;
256
257 /* True if only single-precision floating-point operations are allowed.
258 Changed by .set singlefloat or .set doublefloat, command-line options
259 -msingle-float or -mdouble-float. The default is false. */
260 bfd_boolean single_float;
261
262 /* 1 if single-precision operations on odd-numbered registers are
263 allowed. */
264 int oddspreg;
265 };
266
267 /* Specifies whether module level options have been checked yet. */
268 static bfd_boolean file_mips_opts_checked = FALSE;
269
270 /* Do we support nan2008? 0 if we don't, 1 if we do, and -1 if the
271 value has not been initialized. Changed by `.nan legacy' and
272 `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
273 options, and the default CPU. */
274 static int mips_nan2008 = -1;
275
276 /* This is the struct we use to hold the module level set of options.
277 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
278 fp fields to -1 to indicate that they have not been initialized. */
279
280 static struct mips_set_options file_mips_opts =
281 {
282 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
283 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
284 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
285 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
286 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
287 };
288
289 /* This is similar to file_mips_opts, but for the current set of options. */
290
291 static struct mips_set_options mips_opts =
292 {
293 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
294 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
295 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
296 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
297 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
298 };
299
300 /* Which bits of file_ase were explicitly set or cleared by ASE options. */
301 static unsigned int file_ase_explicit;
302
303 /* These variables are filled in with the masks of registers used.
304 The object format code reads them and puts them in the appropriate
305 place. */
306 unsigned long mips_gprmask;
307 unsigned long mips_cprmask[4];
308
309 /* True if any MIPS16 code was produced. */
310 static int file_ase_mips16;
311
312 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
313 || mips_opts.isa == ISA_MIPS32R2 \
314 || mips_opts.isa == ISA_MIPS32R3 \
315 || mips_opts.isa == ISA_MIPS32R5 \
316 || mips_opts.isa == ISA_MIPS64 \
317 || mips_opts.isa == ISA_MIPS64R2 \
318 || mips_opts.isa == ISA_MIPS64R3 \
319 || mips_opts.isa == ISA_MIPS64R5)
320
321 /* True if any microMIPS code was produced. */
322 static int file_ase_micromips;
323
324 /* True if we want to create R_MIPS_JALR for jalr $25. */
325 #ifdef TE_IRIX
326 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
327 #else
328 /* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
329 because there's no place for any addend, the only acceptable
330 expression is a bare symbol. */
331 #define MIPS_JALR_HINT_P(EXPR) \
332 (!HAVE_IN_PLACE_ADDENDS \
333 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
334 #endif
335
336 /* The argument of the -march= flag. The architecture we are assembling. */
337 static const char *mips_arch_string;
338
339 /* The argument of the -mtune= flag. The architecture for which we
340 are optimizing. */
341 static int mips_tune = CPU_UNKNOWN;
342 static const char *mips_tune_string;
343
344 /* True when generating 32-bit code for a 64-bit processor. */
345 static int mips_32bitmode = 0;
346
347 /* True if the given ABI requires 32-bit registers. */
348 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
349
350 /* Likewise 64-bit registers. */
351 #define ABI_NEEDS_64BIT_REGS(ABI) \
352 ((ABI) == N32_ABI \
353 || (ABI) == N64_ABI \
354 || (ABI) == O64_ABI)
355
356 #define ISA_IS_R6(ISA) \
357 ((ISA) == ISA_MIPS32R6 \
358 || (ISA) == ISA_MIPS64R6)
359
360 /* Return true if ISA supports 64 bit wide gp registers. */
361 #define ISA_HAS_64BIT_REGS(ISA) \
362 ((ISA) == ISA_MIPS3 \
363 || (ISA) == ISA_MIPS4 \
364 || (ISA) == ISA_MIPS5 \
365 || (ISA) == ISA_MIPS64 \
366 || (ISA) == ISA_MIPS64R2 \
367 || (ISA) == ISA_MIPS64R3 \
368 || (ISA) == ISA_MIPS64R5 \
369 || (ISA) == ISA_MIPS64R6)
370
371 /* Return true if ISA supports 64 bit wide float registers. */
372 #define ISA_HAS_64BIT_FPRS(ISA) \
373 ((ISA) == ISA_MIPS3 \
374 || (ISA) == ISA_MIPS4 \
375 || (ISA) == ISA_MIPS5 \
376 || (ISA) == ISA_MIPS32R2 \
377 || (ISA) == ISA_MIPS32R3 \
378 || (ISA) == ISA_MIPS32R5 \
379 || (ISA) == ISA_MIPS32R6 \
380 || (ISA) == ISA_MIPS64 \
381 || (ISA) == ISA_MIPS64R2 \
382 || (ISA) == ISA_MIPS64R3 \
383 || (ISA) == ISA_MIPS64R5 \
384 || (ISA) == ISA_MIPS64R6)
385
386 /* Return true if ISA supports 64-bit right rotate (dror et al.)
387 instructions. */
388 #define ISA_HAS_DROR(ISA) \
389 ((ISA) == ISA_MIPS64R2 \
390 || (ISA) == ISA_MIPS64R3 \
391 || (ISA) == ISA_MIPS64R5 \
392 || (ISA) == ISA_MIPS64R6 \
393 || (mips_opts.micromips \
394 && ISA_HAS_64BIT_REGS (ISA)) \
395 )
396
397 /* Return true if ISA supports 32-bit right rotate (ror et al.)
398 instructions. */
399 #define ISA_HAS_ROR(ISA) \
400 ((ISA) == ISA_MIPS32R2 \
401 || (ISA) == ISA_MIPS32R3 \
402 || (ISA) == ISA_MIPS32R5 \
403 || (ISA) == ISA_MIPS32R6 \
404 || (ISA) == ISA_MIPS64R2 \
405 || (ISA) == ISA_MIPS64R3 \
406 || (ISA) == ISA_MIPS64R5 \
407 || (ISA) == ISA_MIPS64R6 \
408 || (mips_opts.ase & ASE_SMARTMIPS) \
409 || mips_opts.micromips \
410 )
411
412 /* Return true if ISA supports single-precision floats in odd registers. */
413 #define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
414 (((ISA) == ISA_MIPS32 \
415 || (ISA) == ISA_MIPS32R2 \
416 || (ISA) == ISA_MIPS32R3 \
417 || (ISA) == ISA_MIPS32R5 \
418 || (ISA) == ISA_MIPS32R6 \
419 || (ISA) == ISA_MIPS64 \
420 || (ISA) == ISA_MIPS64R2 \
421 || (ISA) == ISA_MIPS64R3 \
422 || (ISA) == ISA_MIPS64R5 \
423 || (ISA) == ISA_MIPS64R6 \
424 || (CPU) == CPU_R5900) \
425 && ((CPU) != CPU_GS464 \
426 || (CPU) != CPU_GS464E \
427 || (CPU) != CPU_GS264E))
428
429 /* Return true if ISA supports move to/from high part of a 64-bit
430 floating-point register. */
431 #define ISA_HAS_MXHC1(ISA) \
432 ((ISA) == ISA_MIPS32R2 \
433 || (ISA) == ISA_MIPS32R3 \
434 || (ISA) == ISA_MIPS32R5 \
435 || (ISA) == ISA_MIPS32R6 \
436 || (ISA) == ISA_MIPS64R2 \
437 || (ISA) == ISA_MIPS64R3 \
438 || (ISA) == ISA_MIPS64R5 \
439 || (ISA) == ISA_MIPS64R6)
440
441 /* Return true if ISA supports legacy NAN. */
442 #define ISA_HAS_LEGACY_NAN(ISA) \
443 ((ISA) == ISA_MIPS1 \
444 || (ISA) == ISA_MIPS2 \
445 || (ISA) == ISA_MIPS3 \
446 || (ISA) == ISA_MIPS4 \
447 || (ISA) == ISA_MIPS5 \
448 || (ISA) == ISA_MIPS32 \
449 || (ISA) == ISA_MIPS32R2 \
450 || (ISA) == ISA_MIPS32R3 \
451 || (ISA) == ISA_MIPS32R5 \
452 || (ISA) == ISA_MIPS64 \
453 || (ISA) == ISA_MIPS64R2 \
454 || (ISA) == ISA_MIPS64R3 \
455 || (ISA) == ISA_MIPS64R5)
456
457 #define GPR_SIZE \
458 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
459 ? 32 \
460 : mips_opts.gp)
461
462 #define FPR_SIZE \
463 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
464 ? 32 \
465 : mips_opts.fp)
466
467 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
468
469 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
470
471 /* True if relocations are stored in-place. */
472 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
473
474 /* The ABI-derived address size. */
475 #define HAVE_64BIT_ADDRESSES \
476 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
477 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
478
479 /* The size of symbolic constants (i.e., expressions of the form
480 "SYMBOL" or "SYMBOL + OFFSET"). */
481 #define HAVE_32BIT_SYMBOLS \
482 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
483 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
484
485 /* Addresses are loaded in different ways, depending on the address size
486 in use. The n32 ABI Documentation also mandates the use of additions
487 with overflow checking, but existing implementations don't follow it. */
488 #define ADDRESS_ADD_INSN \
489 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
490
491 #define ADDRESS_ADDI_INSN \
492 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
493
494 #define ADDRESS_LOAD_INSN \
495 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
496
497 #define ADDRESS_STORE_INSN \
498 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
499
500 /* Return true if the given CPU supports the MIPS16 ASE. */
501 #define CPU_HAS_MIPS16(cpu) \
502 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
503 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
504
505 /* Return true if the given CPU supports the microMIPS ASE. */
506 #define CPU_HAS_MICROMIPS(cpu) 0
507
508 /* True if CPU has a dror instruction. */
509 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
510
511 /* True if CPU has a ror instruction. */
512 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
513
514 /* True if CPU is in the Octeon family */
515 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
516 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
517
518 /* True if CPU has seq/sne and seqi/snei instructions. */
519 #define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
520
521 /* True, if CPU has support for ldc1 and sdc1. */
522 #define CPU_HAS_LDC1_SDC1(CPU) \
523 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
524
525 /* True if mflo and mfhi can be immediately followed by instructions
526 which write to the HI and LO registers.
527
528 According to MIPS specifications, MIPS ISAs I, II, and III need
529 (at least) two instructions between the reads of HI/LO and
530 instructions which write them, and later ISAs do not. Contradicting
531 the MIPS specifications, some MIPS IV processor user manuals (e.g.
532 the UM for the NEC Vr5000) document needing the instructions between
533 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
534 MIPS64 and later ISAs to have the interlocks, plus any specific
535 earlier-ISA CPUs for which CPU documentation declares that the
536 instructions are really interlocked. */
537 #define hilo_interlocks \
538 (mips_opts.isa == ISA_MIPS32 \
539 || mips_opts.isa == ISA_MIPS32R2 \
540 || mips_opts.isa == ISA_MIPS32R3 \
541 || mips_opts.isa == ISA_MIPS32R5 \
542 || mips_opts.isa == ISA_MIPS32R6 \
543 || mips_opts.isa == ISA_MIPS64 \
544 || mips_opts.isa == ISA_MIPS64R2 \
545 || mips_opts.isa == ISA_MIPS64R3 \
546 || mips_opts.isa == ISA_MIPS64R5 \
547 || mips_opts.isa == ISA_MIPS64R6 \
548 || mips_opts.arch == CPU_R4010 \
549 || mips_opts.arch == CPU_R5900 \
550 || mips_opts.arch == CPU_R10000 \
551 || mips_opts.arch == CPU_R12000 \
552 || mips_opts.arch == CPU_R14000 \
553 || mips_opts.arch == CPU_R16000 \
554 || mips_opts.arch == CPU_RM7000 \
555 || mips_opts.arch == CPU_VR5500 \
556 || mips_opts.micromips \
557 )
558
559 /* Whether the processor uses hardware interlocks to protect reads
560 from the GPRs after they are loaded from memory, and thus does not
561 require nops to be inserted. This applies to instructions marked
562 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
563 level I and microMIPS mode instructions are always interlocked. */
564 #define gpr_interlocks \
565 (mips_opts.isa != ISA_MIPS1 \
566 || mips_opts.arch == CPU_R3900 \
567 || mips_opts.arch == CPU_R5900 \
568 || mips_opts.micromips \
569 )
570
571 /* Whether the processor uses hardware interlocks to avoid delays
572 required by coprocessor instructions, and thus does not require
573 nops to be inserted. This applies to instructions marked
574 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
575 instructions marked INSN_WRITE_COND_CODE and ones marked
576 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
577 levels I, II, and III and microMIPS mode instructions are always
578 interlocked. */
579 /* Itbl support may require additional care here. */
580 #define cop_interlocks \
581 ((mips_opts.isa != ISA_MIPS1 \
582 && mips_opts.isa != ISA_MIPS2 \
583 && mips_opts.isa != ISA_MIPS3) \
584 || mips_opts.arch == CPU_R4300 \
585 || mips_opts.micromips \
586 )
587
588 /* Whether the processor uses hardware interlocks to protect reads
589 from coprocessor registers after they are loaded from memory, and
590 thus does not require nops to be inserted. This applies to
591 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
592 requires at MIPS ISA level I and microMIPS mode instructions are
593 always interlocked. */
594 #define cop_mem_interlocks \
595 (mips_opts.isa != ISA_MIPS1 \
596 || mips_opts.micromips \
597 )
598
599 /* Is this a mfhi or mflo instruction? */
600 #define MF_HILO_INSN(PINFO) \
601 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
602
603 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
604 has been selected. This implies, in particular, that addresses of text
605 labels have their LSB set. */
606 #define HAVE_CODE_COMPRESSION \
607 ((mips_opts.mips16 | mips_opts.micromips) != 0)
608
609 /* The minimum and maximum signed values that can be stored in a GPR. */
610 #define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
611 #define GPR_SMIN (-GPR_SMAX - 1)
612
613 /* MIPS PIC level. */
614
615 enum mips_pic_level mips_pic;
616
617 /* 1 if we should generate 32 bit offsets from the $gp register in
618 SVR4_PIC mode. Currently has no meaning in other modes. */
619 static int mips_big_got = 0;
620
621 /* 1 if trap instructions should used for overflow rather than break
622 instructions. */
623 static int mips_trap = 0;
624
625 /* 1 if double width floating point constants should not be constructed
626 by assembling two single width halves into two single width floating
627 point registers which just happen to alias the double width destination
628 register. On some architectures this aliasing can be disabled by a bit
629 in the status register, and the setting of this bit cannot be determined
630 automatically at assemble time. */
631 static int mips_disable_float_construction;
632
633 /* Non-zero if any .set noreorder directives were used. */
634
635 static int mips_any_noreorder;
636
637 /* Non-zero if nops should be inserted when the register referenced in
638 an mfhi/mflo instruction is read in the next two instructions. */
639 static int mips_7000_hilo_fix;
640
641 /* The size of objects in the small data section. */
642 static unsigned int g_switch_value = 8;
643 /* Whether the -G option was used. */
644 static int g_switch_seen = 0;
645
646 #define N_RMASK 0xc4
647 #define N_VFP 0xd4
648
649 /* If we can determine in advance that GP optimization won't be
650 possible, we can skip the relaxation stuff that tries to produce
651 GP-relative references. This makes delay slot optimization work
652 better.
653
654 This function can only provide a guess, but it seems to work for
655 gcc output. It needs to guess right for gcc, otherwise gcc
656 will put what it thinks is a GP-relative instruction in a branch
657 delay slot.
658
659 I don't know if a fix is needed for the SVR4_PIC mode. I've only
660 fixed it for the non-PIC mode. KR 95/04/07 */
661 static int nopic_need_relax (symbolS *, int);
662
663 /* handle of the OPCODE hash table */
664 static struct hash_control *op_hash = NULL;
665
666 /* The opcode hash table we use for the mips16. */
667 static struct hash_control *mips16_op_hash = NULL;
668
669 /* The opcode hash table we use for the microMIPS ASE. */
670 static struct hash_control *micromips_op_hash = NULL;
671
672 /* This array holds the chars that always start a comment. If the
673 pre-processor is disabled, these aren't very useful */
674 const char comment_chars[] = "#";
675
676 /* This array holds the chars that only start a comment at the beginning of
677 a line. If the line seems to have the form '# 123 filename'
678 .line and .file directives will appear in the pre-processed output */
679 /* Note that input_file.c hand checks for '#' at the beginning of the
680 first line of the input file. This is because the compiler outputs
681 #NO_APP at the beginning of its output. */
682 /* Also note that C style comments are always supported. */
683 const char line_comment_chars[] = "#";
684
685 /* This array holds machine specific line separator characters. */
686 const char line_separator_chars[] = ";";
687
688 /* Chars that can be used to separate mant from exp in floating point nums */
689 const char EXP_CHARS[] = "eE";
690
691 /* Chars that mean this number is a floating point constant */
692 /* As in 0f12.456 */
693 /* or 0d1.2345e12 */
694 const char FLT_CHARS[] = "rRsSfFdDxXpP";
695
696 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
697 changed in read.c . Ideally it shouldn't have to know about it at all,
698 but nothing is ideal around here.
699 */
700
701 /* Types of printf format used for instruction-related error messages.
702 "I" means int ("%d") and "S" means string ("%s"). */
703 enum mips_insn_error_format {
704 ERR_FMT_PLAIN,
705 ERR_FMT_I,
706 ERR_FMT_SS,
707 };
708
709 /* Information about an error that was found while assembling the current
710 instruction. */
711 struct mips_insn_error {
712 /* We sometimes need to match an instruction against more than one
713 opcode table entry. Errors found during this matching are reported
714 against a particular syntactic argument rather than against the
715 instruction as a whole. We grade these messages so that errors
716 against argument N have a greater priority than an error against
717 any argument < N, since the former implies that arguments up to N
718 were acceptable and that the opcode entry was therefore a closer match.
719 If several matches report an error against the same argument,
720 we only use that error if it is the same in all cases.
721
722 min_argnum is the minimum argument number for which an error message
723 should be accepted. It is 0 if MSG is against the instruction as
724 a whole. */
725 int min_argnum;
726
727 /* The printf()-style message, including its format and arguments. */
728 enum mips_insn_error_format format;
729 const char *msg;
730 union {
731 int i;
732 const char *ss[2];
733 } u;
734 };
735
736 /* The error that should be reported for the current instruction. */
737 static struct mips_insn_error insn_error;
738
739 static int auto_align = 1;
740
741 /* When outputting SVR4 PIC code, the assembler needs to know the
742 offset in the stack frame from which to restore the $gp register.
743 This is set by the .cprestore pseudo-op, and saved in this
744 variable. */
745 static offsetT mips_cprestore_offset = -1;
746
747 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
748 more optimizations, it can use a register value instead of a memory-saved
749 offset and even an other register than $gp as global pointer. */
750 static offsetT mips_cpreturn_offset = -1;
751 static int mips_cpreturn_register = -1;
752 static int mips_gp_register = GP;
753 static int mips_gprel_offset = 0;
754
755 /* Whether mips_cprestore_offset has been set in the current function
756 (or whether it has already been warned about, if not). */
757 static int mips_cprestore_valid = 0;
758
759 /* This is the register which holds the stack frame, as set by the
760 .frame pseudo-op. This is needed to implement .cprestore. */
761 static int mips_frame_reg = SP;
762
763 /* Whether mips_frame_reg has been set in the current function
764 (or whether it has already been warned about, if not). */
765 static int mips_frame_reg_valid = 0;
766
767 /* To output NOP instructions correctly, we need to keep information
768 about the previous two instructions. */
769
770 /* Whether we are optimizing. The default value of 2 means to remove
771 unneeded NOPs and swap branch instructions when possible. A value
772 of 1 means to not swap branches. A value of 0 means to always
773 insert NOPs. */
774 static int mips_optimize = 2;
775
776 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
777 equivalent to seeing no -g option at all. */
778 static int mips_debug = 0;
779
780 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
781 #define MAX_VR4130_NOPS 4
782
783 /* The maximum number of NOPs needed to fill delay slots. */
784 #define MAX_DELAY_NOPS 2
785
786 /* The maximum number of NOPs needed for any purpose. */
787 #define MAX_NOPS 4
788
789 /* A list of previous instructions, with index 0 being the most recent.
790 We need to look back MAX_NOPS instructions when filling delay slots
791 or working around processor errata. We need to look back one
792 instruction further if we're thinking about using history[0] to
793 fill a branch delay slot. */
794 static struct mips_cl_insn history[1 + MAX_NOPS];
795
796 /* Arrays of operands for each instruction. */
797 #define MAX_OPERANDS 6
798 struct mips_operand_array {
799 const struct mips_operand *operand[MAX_OPERANDS];
800 };
801 static struct mips_operand_array *mips_operands;
802 static struct mips_operand_array *mips16_operands;
803 static struct mips_operand_array *micromips_operands;
804
805 /* Nop instructions used by emit_nop. */
806 static struct mips_cl_insn nop_insn;
807 static struct mips_cl_insn mips16_nop_insn;
808 static struct mips_cl_insn micromips_nop16_insn;
809 static struct mips_cl_insn micromips_nop32_insn;
810
811 /* The appropriate nop for the current mode. */
812 #define NOP_INSN (mips_opts.mips16 \
813 ? &mips16_nop_insn \
814 : (mips_opts.micromips \
815 ? (mips_opts.insn32 \
816 ? &micromips_nop32_insn \
817 : &micromips_nop16_insn) \
818 : &nop_insn))
819
820 /* The size of NOP_INSN in bytes. */
821 #define NOP_INSN_SIZE ((mips_opts.mips16 \
822 || (mips_opts.micromips && !mips_opts.insn32)) \
823 ? 2 : 4)
824
825 /* If this is set, it points to a frag holding nop instructions which
826 were inserted before the start of a noreorder section. If those
827 nops turn out to be unnecessary, the size of the frag can be
828 decreased. */
829 static fragS *prev_nop_frag;
830
831 /* The number of nop instructions we created in prev_nop_frag. */
832 static int prev_nop_frag_holds;
833
834 /* The number of nop instructions that we know we need in
835 prev_nop_frag. */
836 static int prev_nop_frag_required;
837
838 /* The number of instructions we've seen since prev_nop_frag. */
839 static int prev_nop_frag_since;
840
841 /* Relocations against symbols are sometimes done in two parts, with a HI
842 relocation and a LO relocation. Each relocation has only 16 bits of
843 space to store an addend. This means that in order for the linker to
844 handle carries correctly, it must be able to locate both the HI and
845 the LO relocation. This means that the relocations must appear in
846 order in the relocation table.
847
848 In order to implement this, we keep track of each unmatched HI
849 relocation. We then sort them so that they immediately precede the
850 corresponding LO relocation. */
851
852 struct mips_hi_fixup
853 {
854 /* Next HI fixup. */
855 struct mips_hi_fixup *next;
856 /* This fixup. */
857 fixS *fixp;
858 /* The section this fixup is in. */
859 segT seg;
860 };
861
862 /* The list of unmatched HI relocs. */
863
864 static struct mips_hi_fixup *mips_hi_fixup_list;
865
866 /* The frag containing the last explicit relocation operator.
867 Null if explicit relocations have not been used. */
868
869 static fragS *prev_reloc_op_frag;
870
871 /* Map mips16 register numbers to normal MIPS register numbers. */
872
873 static const unsigned int mips16_to_32_reg_map[] =
874 {
875 16, 17, 2, 3, 4, 5, 6, 7
876 };
877
878 /* Map microMIPS register numbers to normal MIPS register numbers. */
879
880 #define micromips_to_32_reg_d_map mips16_to_32_reg_map
881
882 /* The microMIPS registers with type h. */
883 static const unsigned int micromips_to_32_reg_h_map1[] =
884 {
885 5, 5, 6, 4, 4, 4, 4, 4
886 };
887 static const unsigned int micromips_to_32_reg_h_map2[] =
888 {
889 6, 7, 7, 21, 22, 5, 6, 7
890 };
891
892 /* The microMIPS registers with type m. */
893 static const unsigned int micromips_to_32_reg_m_map[] =
894 {
895 0, 17, 2, 3, 16, 18, 19, 20
896 };
897
898 #define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
899
900 /* Classifies the kind of instructions we're interested in when
901 implementing -mfix-vr4120. */
902 enum fix_vr4120_class
903 {
904 FIX_VR4120_MACC,
905 FIX_VR4120_DMACC,
906 FIX_VR4120_MULT,
907 FIX_VR4120_DMULT,
908 FIX_VR4120_DIV,
909 FIX_VR4120_MTHILO,
910 NUM_FIX_VR4120_CLASSES
911 };
912
913 /* ...likewise -mfix-loongson2f-jump. */
914 static bfd_boolean mips_fix_loongson2f_jump;
915
916 /* ...likewise -mfix-loongson2f-nop. */
917 static bfd_boolean mips_fix_loongson2f_nop;
918
919 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
920 static bfd_boolean mips_fix_loongson2f;
921
922 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
923 there must be at least one other instruction between an instruction
924 of type X and an instruction of type Y. */
925 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
926
927 /* True if -mfix-vr4120 is in force. */
928 static int mips_fix_vr4120;
929
930 /* ...likewise -mfix-vr4130. */
931 static int mips_fix_vr4130;
932
933 /* ...likewise -mfix-24k. */
934 static int mips_fix_24k;
935
936 /* ...likewise -mfix-rm7000 */
937 static int mips_fix_rm7000;
938
939 /* ...likewise -mfix-cn63xxp1 */
940 static bfd_boolean mips_fix_cn63xxp1;
941
942 /* We don't relax branches by default, since this causes us to expand
943 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
944 fail to compute the offset before expanding the macro to the most
945 efficient expansion. */
946
947 static int mips_relax_branch;
948
949 /* TRUE if checks are suppressed for invalid branches between ISA modes.
950 Needed for broken assembly produced by some GCC versions and some
951 sloppy code out there, where branches to data labels are present. */
952 static bfd_boolean mips_ignore_branch_isa;
953 \f
954 /* The expansion of many macros depends on the type of symbol that
955 they refer to. For example, when generating position-dependent code,
956 a macro that refers to a symbol may have two different expansions,
957 one which uses GP-relative addresses and one which uses absolute
958 addresses. When generating SVR4-style PIC, a macro may have
959 different expansions for local and global symbols.
960
961 We handle these situations by generating both sequences and putting
962 them in variant frags. In position-dependent code, the first sequence
963 will be the GP-relative one and the second sequence will be the
964 absolute one. In SVR4 PIC, the first sequence will be for global
965 symbols and the second will be for local symbols.
966
967 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
968 SECOND are the lengths of the two sequences in bytes. These fields
969 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
970 the subtype has the following flags:
971
972 RELAX_PIC
973 Set if generating PIC code.
974
975 RELAX_USE_SECOND
976 Set if it has been decided that we should use the second
977 sequence instead of the first.
978
979 RELAX_SECOND_LONGER
980 Set in the first variant frag if the macro's second implementation
981 is longer than its first. This refers to the macro as a whole,
982 not an individual relaxation.
983
984 RELAX_NOMACRO
985 Set in the first variant frag if the macro appeared in a .set nomacro
986 block and if one alternative requires a warning but the other does not.
987
988 RELAX_DELAY_SLOT
989 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
990 delay slot.
991
992 RELAX_DELAY_SLOT_16BIT
993 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
994 16-bit instruction.
995
996 RELAX_DELAY_SLOT_SIZE_FIRST
997 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
998 the macro is of the wrong size for the branch delay slot.
999
1000 RELAX_DELAY_SLOT_SIZE_SECOND
1001 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1002 the macro is of the wrong size for the branch delay slot.
1003
1004 The frag's "opcode" points to the first fixup for relaxable code.
1005
1006 Relaxable macros are generated using a sequence such as:
1007
1008 relax_start (SYMBOL);
1009 ... generate first expansion ...
1010 relax_switch ();
1011 ... generate second expansion ...
1012 relax_end ();
1013
1014 The code and fixups for the unwanted alternative are discarded
1015 by md_convert_frag. */
1016 #define RELAX_ENCODE(FIRST, SECOND, PIC) \
1017 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
1018
1019 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1020 #define RELAX_SECOND(X) ((X) & 0xff)
1021 #define RELAX_PIC(X) (((X) & 0x10000) != 0)
1022 #define RELAX_USE_SECOND 0x20000
1023 #define RELAX_SECOND_LONGER 0x40000
1024 #define RELAX_NOMACRO 0x80000
1025 #define RELAX_DELAY_SLOT 0x100000
1026 #define RELAX_DELAY_SLOT_16BIT 0x200000
1027 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1028 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
1029
1030 /* Branch without likely bit. If label is out of range, we turn:
1031
1032 beq reg1, reg2, label
1033 delay slot
1034
1035 into
1036
1037 bne reg1, reg2, 0f
1038 nop
1039 j label
1040 0: delay slot
1041
1042 with the following opcode replacements:
1043
1044 beq <-> bne
1045 blez <-> bgtz
1046 bltz <-> bgez
1047 bc1f <-> bc1t
1048
1049 bltzal <-> bgezal (with jal label instead of j label)
1050
1051 Even though keeping the delay slot instruction in the delay slot of
1052 the branch would be more efficient, it would be very tricky to do
1053 correctly, because we'd have to introduce a variable frag *after*
1054 the delay slot instruction, and expand that instead. Let's do it
1055 the easy way for now, even if the branch-not-taken case now costs
1056 one additional instruction. Out-of-range branches are not supposed
1057 to be common, anyway.
1058
1059 Branch likely. If label is out of range, we turn:
1060
1061 beql reg1, reg2, label
1062 delay slot (annulled if branch not taken)
1063
1064 into
1065
1066 beql reg1, reg2, 1f
1067 nop
1068 beql $0, $0, 2f
1069 nop
1070 1: j[al] label
1071 delay slot (executed only if branch taken)
1072 2:
1073
1074 It would be possible to generate a shorter sequence by losing the
1075 likely bit, generating something like:
1076
1077 bne reg1, reg2, 0f
1078 nop
1079 j[al] label
1080 delay slot (executed only if branch taken)
1081 0:
1082
1083 beql -> bne
1084 bnel -> beq
1085 blezl -> bgtz
1086 bgtzl -> blez
1087 bltzl -> bgez
1088 bgezl -> bltz
1089 bc1fl -> bc1t
1090 bc1tl -> bc1f
1091
1092 bltzall -> bgezal (with jal label instead of j label)
1093 bgezall -> bltzal (ditto)
1094
1095
1096 but it's not clear that it would actually improve performance. */
1097 #define RELAX_BRANCH_ENCODE(at, pic, \
1098 uncond, likely, link, toofar) \
1099 ((relax_substateT) \
1100 (0xc0000000 \
1101 | ((at) & 0x1f) \
1102 | ((pic) ? 0x20 : 0) \
1103 | ((toofar) ? 0x40 : 0) \
1104 | ((link) ? 0x80 : 0) \
1105 | ((likely) ? 0x100 : 0) \
1106 | ((uncond) ? 0x200 : 0)))
1107 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1108 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1109 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1110 #define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1111 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1112 #define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
1113 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1114
1115 /* For mips16 code, we use an entirely different form of relaxation.
1116 mips16 supports two versions of most instructions which take
1117 immediate values: a small one which takes some small value, and a
1118 larger one which takes a 16 bit value. Since branches also follow
1119 this pattern, relaxing these values is required.
1120
1121 We can assemble both mips16 and normal MIPS code in a single
1122 object. Therefore, we need to support this type of relaxation at
1123 the same time that we support the relaxation described above. We
1124 use the high bit of the subtype field to distinguish these cases.
1125
1126 The information we store for this type of relaxation is the
1127 argument code found in the opcode file for this relocation, whether
1128 the user explicitly requested a small or extended form, and whether
1129 the relocation is in a jump or jal delay slot. That tells us the
1130 size of the value, and how it should be stored. We also store
1131 whether the fragment is considered to be extended or not. We also
1132 store whether this is known to be a branch to a different section,
1133 whether we have tried to relax this frag yet, and whether we have
1134 ever extended a PC relative fragment because of a shift count. */
1135 #define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
1136 small, ext, \
1137 dslot, jal_dslot) \
1138 (0x80000000 \
1139 | ((type) & 0xff) \
1140 | ((e2) ? 0x100 : 0) \
1141 | ((pic) ? 0x200 : 0) \
1142 | ((sym32) ? 0x400 : 0) \
1143 | ((nomacro) ? 0x800 : 0) \
1144 | ((small) ? 0x1000 : 0) \
1145 | ((ext) ? 0x2000 : 0) \
1146 | ((dslot) ? 0x4000 : 0) \
1147 | ((jal_dslot) ? 0x8000 : 0))
1148
1149 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1150 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1151 #define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1152 #define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1153 #define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1154 #define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1155 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1156 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1157 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1158 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1159
1160 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1161 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1162 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1163 #define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1164 #define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1165 #define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1166 #define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1167 #define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1168 #define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
1169
1170 /* For microMIPS code, we use relaxation similar to one we use for
1171 MIPS16 code. Some instructions that take immediate values support
1172 two encodings: a small one which takes some small value, and a
1173 larger one which takes a 16 bit value. As some branches also follow
1174 this pattern, relaxing these values is required.
1175
1176 We can assemble both microMIPS and normal MIPS code in a single
1177 object. Therefore, we need to support this type of relaxation at
1178 the same time that we support the relaxation described above. We
1179 use one of the high bits of the subtype field to distinguish these
1180 cases.
1181
1182 The information we store for this type of relaxation is the argument
1183 code found in the opcode file for this relocation, the register
1184 selected as the assembler temporary, whether in the 32-bit
1185 instruction mode, whether the branch is unconditional, whether it is
1186 compact, whether there is no delay-slot instruction available to fill
1187 in, whether it stores the link address implicitly in $ra, whether
1188 relaxation of out-of-range 32-bit branches to a sequence of
1189 instructions is enabled, and whether the displacement of a branch is
1190 too large to fit as an immediate argument of a 16-bit and a 32-bit
1191 branch, respectively. */
1192 #define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
1193 uncond, compact, link, nods, \
1194 relax32, toofar16, toofar32) \
1195 (0x40000000 \
1196 | ((type) & 0xff) \
1197 | (((at) & 0x1f) << 8) \
1198 | ((insn32) ? 0x2000 : 0) \
1199 | ((pic) ? 0x4000 : 0) \
1200 | ((uncond) ? 0x8000 : 0) \
1201 | ((compact) ? 0x10000 : 0) \
1202 | ((link) ? 0x20000 : 0) \
1203 | ((nods) ? 0x40000 : 0) \
1204 | ((relax32) ? 0x80000 : 0) \
1205 | ((toofar16) ? 0x100000 : 0) \
1206 | ((toofar32) ? 0x200000 : 0))
1207 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1208 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1209 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1210 #define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
1211 #define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1212 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1213 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1214 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1215 #define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1216 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1217
1218 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1219 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1220 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1221 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1222 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1223 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
1224
1225 /* Sign-extend 16-bit value X. */
1226 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1227
1228 /* Is the given value a sign-extended 32-bit value? */
1229 #define IS_SEXT_32BIT_NUM(x) \
1230 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1231 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1232
1233 /* Is the given value a sign-extended 16-bit value? */
1234 #define IS_SEXT_16BIT_NUM(x) \
1235 (((x) &~ (offsetT) 0x7fff) == 0 \
1236 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1237
1238 /* Is the given value a sign-extended 12-bit value? */
1239 #define IS_SEXT_12BIT_NUM(x) \
1240 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1241
1242 /* Is the given value a sign-extended 9-bit value? */
1243 #define IS_SEXT_9BIT_NUM(x) \
1244 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1245
1246 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
1247 #define IS_ZEXT_32BIT_NUM(x) \
1248 (((x) &~ (offsetT) 0xffffffff) == 0 \
1249 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1250
1251 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1252 SHIFT places. */
1253 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1254 (((STRUCT) >> (SHIFT)) & (MASK))
1255
1256 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
1257 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1258 (!(MICROMIPS) \
1259 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1260 : EXTRACT_BITS ((INSN).insn_opcode, \
1261 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1262 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1263 EXTRACT_BITS ((INSN).insn_opcode, \
1264 MIPS16OP_MASK_##FIELD, \
1265 MIPS16OP_SH_##FIELD)
1266
1267 /* The MIPS16 EXTEND opcode, shifted left 16 places. */
1268 #define MIPS16_EXTEND (0xf000U << 16)
1269 \f
1270 /* Whether or not we are emitting a branch-likely macro. */
1271 static bfd_boolean emit_branch_likely_macro = FALSE;
1272
1273 /* Global variables used when generating relaxable macros. See the
1274 comment above RELAX_ENCODE for more details about how relaxation
1275 is used. */
1276 static struct {
1277 /* 0 if we're not emitting a relaxable macro.
1278 1 if we're emitting the first of the two relaxation alternatives.
1279 2 if we're emitting the second alternative. */
1280 int sequence;
1281
1282 /* The first relaxable fixup in the current frag. (In other words,
1283 the first fixup that refers to relaxable code.) */
1284 fixS *first_fixup;
1285
1286 /* sizes[0] says how many bytes of the first alternative are stored in
1287 the current frag. Likewise sizes[1] for the second alternative. */
1288 unsigned int sizes[2];
1289
1290 /* The symbol on which the choice of sequence depends. */
1291 symbolS *symbol;
1292 } mips_relax;
1293 \f
1294 /* Global variables used to decide whether a macro needs a warning. */
1295 static struct {
1296 /* True if the macro is in a branch delay slot. */
1297 bfd_boolean delay_slot_p;
1298
1299 /* Set to the length in bytes required if the macro is in a delay slot
1300 that requires a specific length of instruction, otherwise zero. */
1301 unsigned int delay_slot_length;
1302
1303 /* For relaxable macros, sizes[0] is the length of the first alternative
1304 in bytes and sizes[1] is the length of the second alternative.
1305 For non-relaxable macros, both elements give the length of the
1306 macro in bytes. */
1307 unsigned int sizes[2];
1308
1309 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1310 instruction of the first alternative in bytes and first_insn_sizes[1]
1311 is the length of the first instruction of the second alternative.
1312 For non-relaxable macros, both elements give the length of the first
1313 instruction in bytes.
1314
1315 Set to zero if we haven't yet seen the first instruction. */
1316 unsigned int first_insn_sizes[2];
1317
1318 /* For relaxable macros, insns[0] is the number of instructions for the
1319 first alternative and insns[1] is the number of instructions for the
1320 second alternative.
1321
1322 For non-relaxable macros, both elements give the number of
1323 instructions for the macro. */
1324 unsigned int insns[2];
1325
1326 /* The first variant frag for this macro. */
1327 fragS *first_frag;
1328 } mips_macro_warning;
1329 \f
1330 /* Prototypes for static functions. */
1331
1332 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1333
1334 static void append_insn
1335 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1336 bfd_boolean expansionp);
1337 static void mips_no_prev_insn (void);
1338 static void macro_build (expressionS *, const char *, const char *, ...);
1339 static void mips16_macro_build
1340 (expressionS *, const char *, const char *, va_list *);
1341 static void load_register (int, expressionS *, int);
1342 static void macro_start (void);
1343 static void macro_end (void);
1344 static void macro (struct mips_cl_insn *ip, char *str);
1345 static void mips16_macro (struct mips_cl_insn * ip);
1346 static void mips_ip (char *str, struct mips_cl_insn * ip);
1347 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1348 static unsigned long mips16_immed_extend (offsetT, unsigned int);
1349 static void mips16_immed
1350 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1351 unsigned int, unsigned long *);
1352 static size_t my_getSmallExpression
1353 (expressionS *, bfd_reloc_code_real_type *, char *);
1354 static void my_getExpression (expressionS *, char *);
1355 static void s_align (int);
1356 static void s_change_sec (int);
1357 static void s_change_section (int);
1358 static void s_cons (int);
1359 static void s_float_cons (int);
1360 static void s_mips_globl (int);
1361 static void s_option (int);
1362 static void s_mipsset (int);
1363 static void s_abicalls (int);
1364 static void s_cpload (int);
1365 static void s_cpsetup (int);
1366 static void s_cplocal (int);
1367 static void s_cprestore (int);
1368 static void s_cpreturn (int);
1369 static void s_dtprelword (int);
1370 static void s_dtpreldword (int);
1371 static void s_tprelword (int);
1372 static void s_tpreldword (int);
1373 static void s_gpvalue (int);
1374 static void s_gpword (int);
1375 static void s_gpdword (int);
1376 static void s_ehword (int);
1377 static void s_cpadd (int);
1378 static void s_insn (int);
1379 static void s_nan (int);
1380 static void s_module (int);
1381 static void s_mips_ent (int);
1382 static void s_mips_end (int);
1383 static void s_mips_frame (int);
1384 static void s_mips_mask (int reg_type);
1385 static void s_mips_stab (int);
1386 static void s_mips_weakext (int);
1387 static void s_mips_file (int);
1388 static void s_mips_loc (int);
1389 static bfd_boolean pic_need_relax (symbolS *);
1390 static int relaxed_branch_length (fragS *, asection *, int);
1391 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1392 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1393 static void file_mips_check_options (void);
1394
1395 /* Table and functions used to map between CPU/ISA names, and
1396 ISA levels, and CPU numbers. */
1397
1398 struct mips_cpu_info
1399 {
1400 const char *name; /* CPU or ISA name. */
1401 int flags; /* MIPS_CPU_* flags. */
1402 int ase; /* Set of ASEs implemented by the CPU. */
1403 int isa; /* ISA level. */
1404 int cpu; /* CPU number (default CPU if ISA). */
1405 };
1406
1407 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1408
1409 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1410 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1411 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1412 \f
1413 /* Command-line options. */
1414 const char *md_shortopts = "O::g::G:";
1415
1416 enum options
1417 {
1418 OPTION_MARCH = OPTION_MD_BASE,
1419 OPTION_MTUNE,
1420 OPTION_MIPS1,
1421 OPTION_MIPS2,
1422 OPTION_MIPS3,
1423 OPTION_MIPS4,
1424 OPTION_MIPS5,
1425 OPTION_MIPS32,
1426 OPTION_MIPS64,
1427 OPTION_MIPS32R2,
1428 OPTION_MIPS32R3,
1429 OPTION_MIPS32R5,
1430 OPTION_MIPS32R6,
1431 OPTION_MIPS64R2,
1432 OPTION_MIPS64R3,
1433 OPTION_MIPS64R5,
1434 OPTION_MIPS64R6,
1435 OPTION_MIPS16,
1436 OPTION_NO_MIPS16,
1437 OPTION_MIPS3D,
1438 OPTION_NO_MIPS3D,
1439 OPTION_MDMX,
1440 OPTION_NO_MDMX,
1441 OPTION_DSP,
1442 OPTION_NO_DSP,
1443 OPTION_MT,
1444 OPTION_NO_MT,
1445 OPTION_VIRT,
1446 OPTION_NO_VIRT,
1447 OPTION_MSA,
1448 OPTION_NO_MSA,
1449 OPTION_SMARTMIPS,
1450 OPTION_NO_SMARTMIPS,
1451 OPTION_DSPR2,
1452 OPTION_NO_DSPR2,
1453 OPTION_DSPR3,
1454 OPTION_NO_DSPR3,
1455 OPTION_EVA,
1456 OPTION_NO_EVA,
1457 OPTION_XPA,
1458 OPTION_NO_XPA,
1459 OPTION_MICROMIPS,
1460 OPTION_NO_MICROMIPS,
1461 OPTION_MCU,
1462 OPTION_NO_MCU,
1463 OPTION_MIPS16E2,
1464 OPTION_NO_MIPS16E2,
1465 OPTION_CRC,
1466 OPTION_NO_CRC,
1467 OPTION_M4650,
1468 OPTION_NO_M4650,
1469 OPTION_M4010,
1470 OPTION_NO_M4010,
1471 OPTION_M4100,
1472 OPTION_NO_M4100,
1473 OPTION_M3900,
1474 OPTION_NO_M3900,
1475 OPTION_M7000_HILO_FIX,
1476 OPTION_MNO_7000_HILO_FIX,
1477 OPTION_FIX_24K,
1478 OPTION_NO_FIX_24K,
1479 OPTION_FIX_RM7000,
1480 OPTION_NO_FIX_RM7000,
1481 OPTION_FIX_LOONGSON2F_JUMP,
1482 OPTION_NO_FIX_LOONGSON2F_JUMP,
1483 OPTION_FIX_LOONGSON2F_NOP,
1484 OPTION_NO_FIX_LOONGSON2F_NOP,
1485 OPTION_FIX_VR4120,
1486 OPTION_NO_FIX_VR4120,
1487 OPTION_FIX_VR4130,
1488 OPTION_NO_FIX_VR4130,
1489 OPTION_FIX_CN63XXP1,
1490 OPTION_NO_FIX_CN63XXP1,
1491 OPTION_TRAP,
1492 OPTION_BREAK,
1493 OPTION_EB,
1494 OPTION_EL,
1495 OPTION_FP32,
1496 OPTION_GP32,
1497 OPTION_CONSTRUCT_FLOATS,
1498 OPTION_NO_CONSTRUCT_FLOATS,
1499 OPTION_FP64,
1500 OPTION_FPXX,
1501 OPTION_GP64,
1502 OPTION_RELAX_BRANCH,
1503 OPTION_NO_RELAX_BRANCH,
1504 OPTION_IGNORE_BRANCH_ISA,
1505 OPTION_NO_IGNORE_BRANCH_ISA,
1506 OPTION_INSN32,
1507 OPTION_NO_INSN32,
1508 OPTION_MSHARED,
1509 OPTION_MNO_SHARED,
1510 OPTION_MSYM32,
1511 OPTION_MNO_SYM32,
1512 OPTION_SOFT_FLOAT,
1513 OPTION_HARD_FLOAT,
1514 OPTION_SINGLE_FLOAT,
1515 OPTION_DOUBLE_FLOAT,
1516 OPTION_32,
1517 OPTION_CALL_SHARED,
1518 OPTION_CALL_NONPIC,
1519 OPTION_NON_SHARED,
1520 OPTION_XGOT,
1521 OPTION_MABI,
1522 OPTION_N32,
1523 OPTION_64,
1524 OPTION_MDEBUG,
1525 OPTION_NO_MDEBUG,
1526 OPTION_PDR,
1527 OPTION_NO_PDR,
1528 OPTION_MVXWORKS_PIC,
1529 OPTION_NAN,
1530 OPTION_ODD_SPREG,
1531 OPTION_NO_ODD_SPREG,
1532 OPTION_GINV,
1533 OPTION_NO_GINV,
1534 OPTION_LOONGSON_MMI,
1535 OPTION_NO_LOONGSON_MMI,
1536 OPTION_LOONGSON_CAM,
1537 OPTION_NO_LOONGSON_CAM,
1538 OPTION_LOONGSON_EXT,
1539 OPTION_NO_LOONGSON_EXT,
1540 OPTION_LOONGSON_EXT2,
1541 OPTION_NO_LOONGSON_EXT2,
1542 OPTION_END_OF_ENUM
1543 };
1544
1545 struct option md_longopts[] =
1546 {
1547 /* Options which specify architecture. */
1548 {"march", required_argument, NULL, OPTION_MARCH},
1549 {"mtune", required_argument, NULL, OPTION_MTUNE},
1550 {"mips0", no_argument, NULL, OPTION_MIPS1},
1551 {"mips1", no_argument, NULL, OPTION_MIPS1},
1552 {"mips2", no_argument, NULL, OPTION_MIPS2},
1553 {"mips3", no_argument, NULL, OPTION_MIPS3},
1554 {"mips4", no_argument, NULL, OPTION_MIPS4},
1555 {"mips5", no_argument, NULL, OPTION_MIPS5},
1556 {"mips32", no_argument, NULL, OPTION_MIPS32},
1557 {"mips64", no_argument, NULL, OPTION_MIPS64},
1558 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1559 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1560 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
1561 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
1562 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1563 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1564 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
1565 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
1566
1567 /* Options which specify Application Specific Extensions (ASEs). */
1568 {"mips16", no_argument, NULL, OPTION_MIPS16},
1569 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1570 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1571 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1572 {"mdmx", no_argument, NULL, OPTION_MDMX},
1573 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1574 {"mdsp", no_argument, NULL, OPTION_DSP},
1575 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1576 {"mmt", no_argument, NULL, OPTION_MT},
1577 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1578 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1579 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1580 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1581 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1582 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1583 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
1584 {"meva", no_argument, NULL, OPTION_EVA},
1585 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1586 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1587 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1588 {"mmcu", no_argument, NULL, OPTION_MCU},
1589 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1590 {"mvirt", no_argument, NULL, OPTION_VIRT},
1591 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1592 {"mmsa", no_argument, NULL, OPTION_MSA},
1593 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
1594 {"mxpa", no_argument, NULL, OPTION_XPA},
1595 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
1596 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1597 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
1598 {"mcrc", no_argument, NULL, OPTION_CRC},
1599 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
1600 {"mginv", no_argument, NULL, OPTION_GINV},
1601 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
1602 {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1603 {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
1604 {"mloongson-cam", no_argument, NULL, OPTION_LOONGSON_CAM},
1605 {"mno-loongson-cam", no_argument, NULL, OPTION_NO_LOONGSON_CAM},
1606 {"mloongson-ext", no_argument, NULL, OPTION_LOONGSON_EXT},
1607 {"mno-loongson-ext", no_argument, NULL, OPTION_NO_LOONGSON_EXT},
1608 {"mloongson-ext2", no_argument, NULL, OPTION_LOONGSON_EXT2},
1609 {"mno-loongson-ext2", no_argument, NULL, OPTION_NO_LOONGSON_EXT2},
1610
1611 /* Old-style architecture options. Don't add more of these. */
1612 {"m4650", no_argument, NULL, OPTION_M4650},
1613 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1614 {"m4010", no_argument, NULL, OPTION_M4010},
1615 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1616 {"m4100", no_argument, NULL, OPTION_M4100},
1617 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1618 {"m3900", no_argument, NULL, OPTION_M3900},
1619 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1620
1621 /* Options which enable bug fixes. */
1622 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1623 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1624 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1625 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1626 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1627 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1628 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1629 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1630 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1631 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1632 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1633 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1634 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1635 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1636 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
1637 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1638 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1639
1640 /* Miscellaneous options. */
1641 {"trap", no_argument, NULL, OPTION_TRAP},
1642 {"no-break", no_argument, NULL, OPTION_TRAP},
1643 {"break", no_argument, NULL, OPTION_BREAK},
1644 {"no-trap", no_argument, NULL, OPTION_BREAK},
1645 {"EB", no_argument, NULL, OPTION_EB},
1646 {"EL", no_argument, NULL, OPTION_EL},
1647 {"mfp32", no_argument, NULL, OPTION_FP32},
1648 {"mgp32", no_argument, NULL, OPTION_GP32},
1649 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1650 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1651 {"mfp64", no_argument, NULL, OPTION_FP64},
1652 {"mfpxx", no_argument, NULL, OPTION_FPXX},
1653 {"mgp64", no_argument, NULL, OPTION_GP64},
1654 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1655 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1656 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1657 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
1658 {"minsn32", no_argument, NULL, OPTION_INSN32},
1659 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1660 {"mshared", no_argument, NULL, OPTION_MSHARED},
1661 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1662 {"msym32", no_argument, NULL, OPTION_MSYM32},
1663 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1664 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1665 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1666 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1667 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1668 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1669 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
1670
1671 /* Strictly speaking this next option is ELF specific,
1672 but we allow it for other ports as well in order to
1673 make testing easier. */
1674 {"32", no_argument, NULL, OPTION_32},
1675
1676 /* ELF-specific options. */
1677 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1678 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1679 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1680 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1681 {"xgot", no_argument, NULL, OPTION_XGOT},
1682 {"mabi", required_argument, NULL, OPTION_MABI},
1683 {"n32", no_argument, NULL, OPTION_N32},
1684 {"64", no_argument, NULL, OPTION_64},
1685 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1686 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1687 {"mpdr", no_argument, NULL, OPTION_PDR},
1688 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1689 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1690 {"mnan", required_argument, NULL, OPTION_NAN},
1691
1692 {NULL, no_argument, NULL, 0}
1693 };
1694 size_t md_longopts_size = sizeof (md_longopts);
1695 \f
1696 /* Information about either an Application Specific Extension or an
1697 optional architecture feature that, for simplicity, we treat in the
1698 same way as an ASE. */
1699 struct mips_ase
1700 {
1701 /* The name of the ASE, used in both the command-line and .set options. */
1702 const char *name;
1703
1704 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1705 and 64-bit architectures, the flags here refer to the subset that
1706 is available on both. */
1707 unsigned int flags;
1708
1709 /* The ASE_* flag used for instructions that are available on 64-bit
1710 architectures but that are not included in FLAGS. */
1711 unsigned int flags64;
1712
1713 /* The command-line options that turn the ASE on and off. */
1714 int option_on;
1715 int option_off;
1716
1717 /* The minimum required architecture revisions for MIPS32, MIPS64,
1718 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1719 int mips32_rev;
1720 int mips64_rev;
1721 int micromips32_rev;
1722 int micromips64_rev;
1723
1724 /* The architecture where the ASE was removed or -1 if the extension has not
1725 been removed. */
1726 int rem_rev;
1727 };
1728
1729 /* A table of all supported ASEs. */
1730 static const struct mips_ase mips_ases[] = {
1731 { "dsp", ASE_DSP, ASE_DSP64,
1732 OPTION_DSP, OPTION_NO_DSP,
1733 2, 2, 2, 2,
1734 -1 },
1735
1736 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1737 OPTION_DSPR2, OPTION_NO_DSPR2,
1738 2, 2, 2, 2,
1739 -1 },
1740
1741 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1742 OPTION_DSPR3, OPTION_NO_DSPR3,
1743 6, 6, -1, -1,
1744 -1 },
1745
1746 { "eva", ASE_EVA, 0,
1747 OPTION_EVA, OPTION_NO_EVA,
1748 2, 2, 2, 2,
1749 -1 },
1750
1751 { "mcu", ASE_MCU, 0,
1752 OPTION_MCU, OPTION_NO_MCU,
1753 2, 2, 2, 2,
1754 -1 },
1755
1756 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1757 { "mdmx", ASE_MDMX, 0,
1758 OPTION_MDMX, OPTION_NO_MDMX,
1759 -1, 1, -1, -1,
1760 6 },
1761
1762 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1763 { "mips3d", ASE_MIPS3D, 0,
1764 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1765 2, 1, -1, -1,
1766 6 },
1767
1768 { "mt", ASE_MT, 0,
1769 OPTION_MT, OPTION_NO_MT,
1770 2, 2, -1, -1,
1771 -1 },
1772
1773 { "smartmips", ASE_SMARTMIPS, 0,
1774 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1775 1, -1, -1, -1,
1776 6 },
1777
1778 { "virt", ASE_VIRT, ASE_VIRT64,
1779 OPTION_VIRT, OPTION_NO_VIRT,
1780 2, 2, 2, 2,
1781 -1 },
1782
1783 { "msa", ASE_MSA, ASE_MSA64,
1784 OPTION_MSA, OPTION_NO_MSA,
1785 2, 2, 2, 2,
1786 -1 },
1787
1788 { "xpa", ASE_XPA, 0,
1789 OPTION_XPA, OPTION_NO_XPA,
1790 2, 2, 2, 2,
1791 -1 },
1792
1793 { "mips16e2", ASE_MIPS16E2, 0,
1794 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1795 2, 2, -1, -1,
1796 6 },
1797
1798 { "crc", ASE_CRC, ASE_CRC64,
1799 OPTION_CRC, OPTION_NO_CRC,
1800 6, 6, -1, -1,
1801 -1 },
1802
1803 { "ginv", ASE_GINV, 0,
1804 OPTION_GINV, OPTION_NO_GINV,
1805 6, 6, 6, 6,
1806 -1 },
1807
1808 { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1809 OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1810 0, 0, -1, -1,
1811 -1 },
1812
1813 { "loongson-cam", ASE_LOONGSON_CAM, 0,
1814 OPTION_LOONGSON_CAM, OPTION_NO_LOONGSON_CAM,
1815 0, 0, -1, -1,
1816 -1 },
1817
1818 { "loongson-ext", ASE_LOONGSON_EXT, 0,
1819 OPTION_LOONGSON_EXT, OPTION_NO_LOONGSON_EXT,
1820 0, 0, -1, -1,
1821 -1 },
1822
1823 { "loongson-ext2", ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2, 0,
1824 OPTION_LOONGSON_EXT2, OPTION_NO_LOONGSON_EXT2,
1825 0, 0, -1, -1,
1826 -1 },
1827 };
1828
1829 /* The set of ASEs that require -mfp64. */
1830 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
1831
1832 /* Groups of ASE_* flags that represent different revisions of an ASE. */
1833 static const unsigned int mips_ase_groups[] = {
1834 ASE_DSP | ASE_DSPR2 | ASE_DSPR3,
1835 ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2
1836 };
1837 \f
1838 /* Pseudo-op table.
1839
1840 The following pseudo-ops from the Kane and Heinrich MIPS book
1841 should be defined here, but are currently unsupported: .alias,
1842 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1843
1844 The following pseudo-ops from the Kane and Heinrich MIPS book are
1845 specific to the type of debugging information being generated, and
1846 should be defined by the object format: .aent, .begin, .bend,
1847 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1848 .vreg.
1849
1850 The following pseudo-ops from the Kane and Heinrich MIPS book are
1851 not MIPS CPU specific, but are also not specific to the object file
1852 format. This file is probably the best place to define them, but
1853 they are not currently supported: .asm0, .endr, .lab, .struct. */
1854
1855 static const pseudo_typeS mips_pseudo_table[] =
1856 {
1857 /* MIPS specific pseudo-ops. */
1858 {"option", s_option, 0},
1859 {"set", s_mipsset, 0},
1860 {"rdata", s_change_sec, 'r'},
1861 {"sdata", s_change_sec, 's'},
1862 {"livereg", s_ignore, 0},
1863 {"abicalls", s_abicalls, 0},
1864 {"cpload", s_cpload, 0},
1865 {"cpsetup", s_cpsetup, 0},
1866 {"cplocal", s_cplocal, 0},
1867 {"cprestore", s_cprestore, 0},
1868 {"cpreturn", s_cpreturn, 0},
1869 {"dtprelword", s_dtprelword, 0},
1870 {"dtpreldword", s_dtpreldword, 0},
1871 {"tprelword", s_tprelword, 0},
1872 {"tpreldword", s_tpreldword, 0},
1873 {"gpvalue", s_gpvalue, 0},
1874 {"gpword", s_gpword, 0},
1875 {"gpdword", s_gpdword, 0},
1876 {"ehword", s_ehword, 0},
1877 {"cpadd", s_cpadd, 0},
1878 {"insn", s_insn, 0},
1879 {"nan", s_nan, 0},
1880 {"module", s_module, 0},
1881
1882 /* Relatively generic pseudo-ops that happen to be used on MIPS
1883 chips. */
1884 {"asciiz", stringer, 8 + 1},
1885 {"bss", s_change_sec, 'b'},
1886 {"err", s_err, 0},
1887 {"half", s_cons, 1},
1888 {"dword", s_cons, 3},
1889 {"weakext", s_mips_weakext, 0},
1890 {"origin", s_org, 0},
1891 {"repeat", s_rept, 0},
1892
1893 /* For MIPS this is non-standard, but we define it for consistency. */
1894 {"sbss", s_change_sec, 'B'},
1895
1896 /* These pseudo-ops are defined in read.c, but must be overridden
1897 here for one reason or another. */
1898 {"align", s_align, 0},
1899 {"byte", s_cons, 0},
1900 {"data", s_change_sec, 'd'},
1901 {"double", s_float_cons, 'd'},
1902 {"float", s_float_cons, 'f'},
1903 {"globl", s_mips_globl, 0},
1904 {"global", s_mips_globl, 0},
1905 {"hword", s_cons, 1},
1906 {"int", s_cons, 2},
1907 {"long", s_cons, 2},
1908 {"octa", s_cons, 4},
1909 {"quad", s_cons, 3},
1910 {"section", s_change_section, 0},
1911 {"short", s_cons, 1},
1912 {"single", s_float_cons, 'f'},
1913 {"stabd", s_mips_stab, 'd'},
1914 {"stabn", s_mips_stab, 'n'},
1915 {"stabs", s_mips_stab, 's'},
1916 {"text", s_change_sec, 't'},
1917 {"word", s_cons, 2},
1918
1919 { "extern", ecoff_directive_extern, 0},
1920
1921 { NULL, NULL, 0 },
1922 };
1923
1924 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1925 {
1926 /* These pseudo-ops should be defined by the object file format.
1927 However, a.out doesn't support them, so we have versions here. */
1928 {"aent", s_mips_ent, 1},
1929 {"bgnb", s_ignore, 0},
1930 {"end", s_mips_end, 0},
1931 {"endb", s_ignore, 0},
1932 {"ent", s_mips_ent, 0},
1933 {"file", s_mips_file, 0},
1934 {"fmask", s_mips_mask, 'F'},
1935 {"frame", s_mips_frame, 0},
1936 {"loc", s_mips_loc, 0},
1937 {"mask", s_mips_mask, 'R'},
1938 {"verstamp", s_ignore, 0},
1939 { NULL, NULL, 0 },
1940 };
1941
1942 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1943 purpose of the `.dc.a' internal pseudo-op. */
1944
1945 int
1946 mips_address_bytes (void)
1947 {
1948 file_mips_check_options ();
1949 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1950 }
1951
1952 extern void pop_insert (const pseudo_typeS *);
1953
1954 void
1955 mips_pop_insert (void)
1956 {
1957 pop_insert (mips_pseudo_table);
1958 if (! ECOFF_DEBUGGING)
1959 pop_insert (mips_nonecoff_pseudo_table);
1960 }
1961 \f
1962 /* Symbols labelling the current insn. */
1963
1964 struct insn_label_list
1965 {
1966 struct insn_label_list *next;
1967 symbolS *label;
1968 };
1969
1970 static struct insn_label_list *free_insn_labels;
1971 #define label_list tc_segment_info_data.labels
1972
1973 static void mips_clear_insn_labels (void);
1974 static void mips_mark_labels (void);
1975 static void mips_compressed_mark_labels (void);
1976
1977 static inline void
1978 mips_clear_insn_labels (void)
1979 {
1980 struct insn_label_list **pl;
1981 segment_info_type *si;
1982
1983 if (now_seg)
1984 {
1985 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1986 ;
1987
1988 si = seg_info (now_seg);
1989 *pl = si->label_list;
1990 si->label_list = NULL;
1991 }
1992 }
1993
1994 /* Mark instruction labels in MIPS16/microMIPS mode. */
1995
1996 static inline void
1997 mips_mark_labels (void)
1998 {
1999 if (HAVE_CODE_COMPRESSION)
2000 mips_compressed_mark_labels ();
2001 }
2002 \f
2003 static char *expr_end;
2004
2005 /* An expression in a macro instruction. This is set by mips_ip and
2006 mips16_ip and when populated is always an O_constant. */
2007
2008 static expressionS imm_expr;
2009
2010 /* The relocatable field in an instruction and the relocs associated
2011 with it. These variables are used for instructions like LUI and
2012 JAL as well as true offsets. They are also used for address
2013 operands in macros. */
2014
2015 static expressionS offset_expr;
2016 static bfd_reloc_code_real_type offset_reloc[3]
2017 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2018
2019 /* This is set to the resulting size of the instruction to be produced
2020 by mips16_ip if an explicit extension is used or by mips_ip if an
2021 explicit size is supplied. */
2022
2023 static unsigned int forced_insn_length;
2024
2025 /* True if we are assembling an instruction. All dot symbols defined during
2026 this time should be treated as code labels. */
2027
2028 static bfd_boolean mips_assembling_insn;
2029
2030 /* The pdr segment for per procedure frame/regmask info. Not used for
2031 ECOFF debugging. */
2032
2033 static segT pdr_seg;
2034
2035 /* The default target format to use. */
2036
2037 #if defined (TE_FreeBSD)
2038 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2039 #elif defined (TE_TMIPS)
2040 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2041 #else
2042 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2043 #endif
2044
2045 const char *
2046 mips_target_format (void)
2047 {
2048 switch (OUTPUT_FLAVOR)
2049 {
2050 case bfd_target_elf_flavour:
2051 #ifdef TE_VXWORKS
2052 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2053 return (target_big_endian
2054 ? "elf32-bigmips-vxworks"
2055 : "elf32-littlemips-vxworks");
2056 #endif
2057 return (target_big_endian
2058 ? (HAVE_64BIT_OBJECTS
2059 ? ELF_TARGET ("elf64-", "big")
2060 : (HAVE_NEWABI
2061 ? ELF_TARGET ("elf32-n", "big")
2062 : ELF_TARGET ("elf32-", "big")))
2063 : (HAVE_64BIT_OBJECTS
2064 ? ELF_TARGET ("elf64-", "little")
2065 : (HAVE_NEWABI
2066 ? ELF_TARGET ("elf32-n", "little")
2067 : ELF_TARGET ("elf32-", "little"))));
2068 default:
2069 abort ();
2070 return NULL;
2071 }
2072 }
2073
2074 /* Return the ISA revision that is currently in use, or 0 if we are
2075 generating code for MIPS V or below. */
2076
2077 static int
2078 mips_isa_rev (void)
2079 {
2080 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2081 return 2;
2082
2083 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2084 return 3;
2085
2086 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2087 return 5;
2088
2089 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2090 return 6;
2091
2092 /* microMIPS implies revision 2 or above. */
2093 if (mips_opts.micromips)
2094 return 2;
2095
2096 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2097 return 1;
2098
2099 return 0;
2100 }
2101
2102 /* Return the mask of all ASEs that are revisions of those in FLAGS. */
2103
2104 static unsigned int
2105 mips_ase_mask (unsigned int flags)
2106 {
2107 unsigned int i;
2108
2109 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2110 if (flags & mips_ase_groups[i])
2111 flags |= mips_ase_groups[i];
2112 return flags;
2113 }
2114
2115 /* Check whether the current ISA supports ASE. Issue a warning if
2116 appropriate. */
2117
2118 static void
2119 mips_check_isa_supports_ase (const struct mips_ase *ase)
2120 {
2121 const char *base;
2122 int min_rev, size;
2123 static unsigned int warned_isa;
2124 static unsigned int warned_fp32;
2125
2126 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2127 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2128 else
2129 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2130 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2131 && (warned_isa & ase->flags) != ase->flags)
2132 {
2133 warned_isa |= ase->flags;
2134 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2135 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2136 if (min_rev < 0)
2137 as_warn (_("the %d-bit %s architecture does not support the"
2138 " `%s' extension"), size, base, ase->name);
2139 else
2140 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
2141 ase->name, base, size, min_rev);
2142 }
2143 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2144 && (warned_isa & ase->flags) != ase->flags)
2145 {
2146 warned_isa |= ase->flags;
2147 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2148 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2149 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2150 ase->name, base, size, ase->rem_rev);
2151 }
2152
2153 if ((ase->flags & FP64_ASES)
2154 && mips_opts.fp != 64
2155 && (warned_fp32 & ase->flags) != ase->flags)
2156 {
2157 warned_fp32 |= ase->flags;
2158 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
2159 }
2160 }
2161
2162 /* Check all enabled ASEs to see whether they are supported by the
2163 chosen architecture. */
2164
2165 static void
2166 mips_check_isa_supports_ases (void)
2167 {
2168 unsigned int i, mask;
2169
2170 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2171 {
2172 mask = mips_ase_mask (mips_ases[i].flags);
2173 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2174 mips_check_isa_supports_ase (&mips_ases[i]);
2175 }
2176 }
2177
2178 /* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2179 that were affected. */
2180
2181 static unsigned int
2182 mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2183 bfd_boolean enabled_p)
2184 {
2185 unsigned int mask;
2186
2187 mask = mips_ase_mask (ase->flags);
2188 opts->ase &= ~mask;
2189
2190 /* Clear combination ASE flags, which need to be recalculated based on
2191 updated regular ASE settings. */
2192 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT);
2193
2194 if (enabled_p)
2195 opts->ase |= ase->flags;
2196
2197 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2198 instructions which are only valid when both ASEs are enabled.
2199 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2200 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2201 {
2202 opts->ase |= ASE_XPA_VIRT;
2203 mask |= ASE_XPA_VIRT;
2204 }
2205 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2206 {
2207 opts->ase |= ASE_MIPS16E2_MT;
2208 mask |= ASE_MIPS16E2_MT;
2209 }
2210
2211 return mask;
2212 }
2213
2214 /* Return the ASE called NAME, or null if none. */
2215
2216 static const struct mips_ase *
2217 mips_lookup_ase (const char *name)
2218 {
2219 unsigned int i;
2220
2221 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2222 if (strcmp (name, mips_ases[i].name) == 0)
2223 return &mips_ases[i];
2224 return NULL;
2225 }
2226
2227 /* Return the length of a microMIPS instruction in bytes. If bits of
2228 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2229 otherwise it is a 32-bit instruction. */
2230
2231 static inline unsigned int
2232 micromips_insn_length (const struct mips_opcode *mo)
2233 {
2234 return mips_opcode_32bit_p (mo) ? 4 : 2;
2235 }
2236
2237 /* Return the length of MIPS16 instruction OPCODE. */
2238
2239 static inline unsigned int
2240 mips16_opcode_length (unsigned long opcode)
2241 {
2242 return (opcode >> 16) == 0 ? 2 : 4;
2243 }
2244
2245 /* Return the length of instruction INSN. */
2246
2247 static inline unsigned int
2248 insn_length (const struct mips_cl_insn *insn)
2249 {
2250 if (mips_opts.micromips)
2251 return micromips_insn_length (insn->insn_mo);
2252 else if (mips_opts.mips16)
2253 return mips16_opcode_length (insn->insn_opcode);
2254 else
2255 return 4;
2256 }
2257
2258 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2259
2260 static void
2261 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2262 {
2263 size_t i;
2264
2265 insn->insn_mo = mo;
2266 insn->insn_opcode = mo->match;
2267 insn->frag = NULL;
2268 insn->where = 0;
2269 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2270 insn->fixp[i] = NULL;
2271 insn->fixed_p = (mips_opts.noreorder > 0);
2272 insn->noreorder_p = (mips_opts.noreorder > 0);
2273 insn->mips16_absolute_jump_p = 0;
2274 insn->complete_p = 0;
2275 insn->cleared_p = 0;
2276 }
2277
2278 /* Get a list of all the operands in INSN. */
2279
2280 static const struct mips_operand_array *
2281 insn_operands (const struct mips_cl_insn *insn)
2282 {
2283 if (insn->insn_mo >= &mips_opcodes[0]
2284 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2285 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2286
2287 if (insn->insn_mo >= &mips16_opcodes[0]
2288 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2289 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2290
2291 if (insn->insn_mo >= &micromips_opcodes[0]
2292 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2293 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2294
2295 abort ();
2296 }
2297
2298 /* Get a description of operand OPNO of INSN. */
2299
2300 static const struct mips_operand *
2301 insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2302 {
2303 const struct mips_operand_array *operands;
2304
2305 operands = insn_operands (insn);
2306 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2307 abort ();
2308 return operands->operand[opno];
2309 }
2310
2311 /* Install UVAL as the value of OPERAND in INSN. */
2312
2313 static inline void
2314 insn_insert_operand (struct mips_cl_insn *insn,
2315 const struct mips_operand *operand, unsigned int uval)
2316 {
2317 if (mips_opts.mips16
2318 && operand->type == OP_INT && operand->lsb == 0
2319 && mips_opcode_32bit_p (insn->insn_mo))
2320 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2321 else
2322 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2323 }
2324
2325 /* Extract the value of OPERAND from INSN. */
2326
2327 static inline unsigned
2328 insn_extract_operand (const struct mips_cl_insn *insn,
2329 const struct mips_operand *operand)
2330 {
2331 return mips_extract_operand (operand, insn->insn_opcode);
2332 }
2333
2334 /* Record the current MIPS16/microMIPS mode in now_seg. */
2335
2336 static void
2337 mips_record_compressed_mode (void)
2338 {
2339 segment_info_type *si;
2340
2341 si = seg_info (now_seg);
2342 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2343 si->tc_segment_info_data.mips16 = mips_opts.mips16;
2344 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2345 si->tc_segment_info_data.micromips = mips_opts.micromips;
2346 }
2347
2348 /* Read a standard MIPS instruction from BUF. */
2349
2350 static unsigned long
2351 read_insn (char *buf)
2352 {
2353 if (target_big_endian)
2354 return bfd_getb32 ((bfd_byte *) buf);
2355 else
2356 return bfd_getl32 ((bfd_byte *) buf);
2357 }
2358
2359 /* Write standard MIPS instruction INSN to BUF. Return a pointer to
2360 the next byte. */
2361
2362 static char *
2363 write_insn (char *buf, unsigned int insn)
2364 {
2365 md_number_to_chars (buf, insn, 4);
2366 return buf + 4;
2367 }
2368
2369 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2370 has length LENGTH. */
2371
2372 static unsigned long
2373 read_compressed_insn (char *buf, unsigned int length)
2374 {
2375 unsigned long insn;
2376 unsigned int i;
2377
2378 insn = 0;
2379 for (i = 0; i < length; i += 2)
2380 {
2381 insn <<= 16;
2382 if (target_big_endian)
2383 insn |= bfd_getb16 ((char *) buf);
2384 else
2385 insn |= bfd_getl16 ((char *) buf);
2386 buf += 2;
2387 }
2388 return insn;
2389 }
2390
2391 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2392 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2393
2394 static char *
2395 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2396 {
2397 unsigned int i;
2398
2399 for (i = 0; i < length; i += 2)
2400 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2401 return buf + length;
2402 }
2403
2404 /* Install INSN at the location specified by its "frag" and "where" fields. */
2405
2406 static void
2407 install_insn (const struct mips_cl_insn *insn)
2408 {
2409 char *f = insn->frag->fr_literal + insn->where;
2410 if (HAVE_CODE_COMPRESSION)
2411 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2412 else
2413 write_insn (f, insn->insn_opcode);
2414 mips_record_compressed_mode ();
2415 }
2416
2417 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2418 and install the opcode in the new location. */
2419
2420 static void
2421 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2422 {
2423 size_t i;
2424
2425 insn->frag = frag;
2426 insn->where = where;
2427 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2428 if (insn->fixp[i] != NULL)
2429 {
2430 insn->fixp[i]->fx_frag = frag;
2431 insn->fixp[i]->fx_where = where;
2432 }
2433 install_insn (insn);
2434 }
2435
2436 /* Add INSN to the end of the output. */
2437
2438 static void
2439 add_fixed_insn (struct mips_cl_insn *insn)
2440 {
2441 char *f = frag_more (insn_length (insn));
2442 move_insn (insn, frag_now, f - frag_now->fr_literal);
2443 }
2444
2445 /* Start a variant frag and move INSN to the start of the variant part,
2446 marking it as fixed. The other arguments are as for frag_var. */
2447
2448 static void
2449 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2450 relax_substateT subtype, symbolS *symbol, offsetT offset)
2451 {
2452 frag_grow (max_chars);
2453 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2454 insn->fixed_p = 1;
2455 frag_var (rs_machine_dependent, max_chars, var,
2456 subtype, symbol, offset, NULL);
2457 }
2458
2459 /* Insert N copies of INSN into the history buffer, starting at
2460 position FIRST. Neither FIRST nor N need to be clipped. */
2461
2462 static void
2463 insert_into_history (unsigned int first, unsigned int n,
2464 const struct mips_cl_insn *insn)
2465 {
2466 if (mips_relax.sequence != 2)
2467 {
2468 unsigned int i;
2469
2470 for (i = ARRAY_SIZE (history); i-- > first;)
2471 if (i >= first + n)
2472 history[i] = history[i - n];
2473 else
2474 history[i] = *insn;
2475 }
2476 }
2477
2478 /* Clear the error in insn_error. */
2479
2480 static void
2481 clear_insn_error (void)
2482 {
2483 memset (&insn_error, 0, sizeof (insn_error));
2484 }
2485
2486 /* Possibly record error message MSG for the current instruction.
2487 If the error is about a particular argument, ARGNUM is the 1-based
2488 number of that argument, otherwise it is 0. FORMAT is the format
2489 of MSG. Return true if MSG was used, false if the current message
2490 was kept. */
2491
2492 static bfd_boolean
2493 set_insn_error_format (int argnum, enum mips_insn_error_format format,
2494 const char *msg)
2495 {
2496 if (argnum == 0)
2497 {
2498 /* Give priority to errors against specific arguments, and to
2499 the first whole-instruction message. */
2500 if (insn_error.msg)
2501 return FALSE;
2502 }
2503 else
2504 {
2505 /* Keep insn_error if it is against a later argument. */
2506 if (argnum < insn_error.min_argnum)
2507 return FALSE;
2508
2509 /* If both errors are against the same argument but are different,
2510 give up on reporting a specific error for this argument.
2511 See the comment about mips_insn_error for details. */
2512 if (argnum == insn_error.min_argnum
2513 && insn_error.msg
2514 && strcmp (insn_error.msg, msg) != 0)
2515 {
2516 insn_error.msg = 0;
2517 insn_error.min_argnum += 1;
2518 return FALSE;
2519 }
2520 }
2521 insn_error.min_argnum = argnum;
2522 insn_error.format = format;
2523 insn_error.msg = msg;
2524 return TRUE;
2525 }
2526
2527 /* Record an instruction error with no % format fields. ARGNUM and MSG are
2528 as for set_insn_error_format. */
2529
2530 static void
2531 set_insn_error (int argnum, const char *msg)
2532 {
2533 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2534 }
2535
2536 /* Record an instruction error with one %d field I. ARGNUM and MSG are
2537 as for set_insn_error_format. */
2538
2539 static void
2540 set_insn_error_i (int argnum, const char *msg, int i)
2541 {
2542 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2543 insn_error.u.i = i;
2544 }
2545
2546 /* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2547 are as for set_insn_error_format. */
2548
2549 static void
2550 set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2551 {
2552 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2553 {
2554 insn_error.u.ss[0] = s1;
2555 insn_error.u.ss[1] = s2;
2556 }
2557 }
2558
2559 /* Report the error in insn_error, which is against assembly code STR. */
2560
2561 static void
2562 report_insn_error (const char *str)
2563 {
2564 const char *msg = concat (insn_error.msg, " `%s'", NULL);
2565
2566 switch (insn_error.format)
2567 {
2568 case ERR_FMT_PLAIN:
2569 as_bad (msg, str);
2570 break;
2571
2572 case ERR_FMT_I:
2573 as_bad (msg, insn_error.u.i, str);
2574 break;
2575
2576 case ERR_FMT_SS:
2577 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2578 break;
2579 }
2580
2581 free ((char *) msg);
2582 }
2583
2584 /* Initialize vr4120_conflicts. There is a bit of duplication here:
2585 the idea is to make it obvious at a glance that each errata is
2586 included. */
2587
2588 static void
2589 init_vr4120_conflicts (void)
2590 {
2591 #define CONFLICT(FIRST, SECOND) \
2592 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2593
2594 /* Errata 21 - [D]DIV[U] after [D]MACC */
2595 CONFLICT (MACC, DIV);
2596 CONFLICT (DMACC, DIV);
2597
2598 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2599 CONFLICT (DMULT, DMULT);
2600 CONFLICT (DMULT, DMACC);
2601 CONFLICT (DMACC, DMULT);
2602 CONFLICT (DMACC, DMACC);
2603
2604 /* Errata 24 - MT{LO,HI} after [D]MACC */
2605 CONFLICT (MACC, MTHILO);
2606 CONFLICT (DMACC, MTHILO);
2607
2608 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2609 instruction is executed immediately after a MACC or DMACC
2610 instruction, the result of [either instruction] is incorrect." */
2611 CONFLICT (MACC, MULT);
2612 CONFLICT (MACC, DMULT);
2613 CONFLICT (DMACC, MULT);
2614 CONFLICT (DMACC, DMULT);
2615
2616 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2617 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2618 DDIV or DDIVU instruction, the result of the MACC or
2619 DMACC instruction is incorrect.". */
2620 CONFLICT (DMULT, MACC);
2621 CONFLICT (DMULT, DMACC);
2622 CONFLICT (DIV, MACC);
2623 CONFLICT (DIV, DMACC);
2624
2625 #undef CONFLICT
2626 }
2627
2628 struct regname {
2629 const char *name;
2630 unsigned int num;
2631 };
2632
2633 #define RNUM_MASK 0x00000ff
2634 #define RTYPE_MASK 0x0ffff00
2635 #define RTYPE_NUM 0x0000100
2636 #define RTYPE_FPU 0x0000200
2637 #define RTYPE_FCC 0x0000400
2638 #define RTYPE_VEC 0x0000800
2639 #define RTYPE_GP 0x0001000
2640 #define RTYPE_CP0 0x0002000
2641 #define RTYPE_PC 0x0004000
2642 #define RTYPE_ACC 0x0008000
2643 #define RTYPE_CCC 0x0010000
2644 #define RTYPE_VI 0x0020000
2645 #define RTYPE_VF 0x0040000
2646 #define RTYPE_R5900_I 0x0080000
2647 #define RTYPE_R5900_Q 0x0100000
2648 #define RTYPE_R5900_R 0x0200000
2649 #define RTYPE_R5900_ACC 0x0400000
2650 #define RTYPE_MSA 0x0800000
2651 #define RWARN 0x8000000
2652
2653 #define GENERIC_REGISTER_NUMBERS \
2654 {"$0", RTYPE_NUM | 0}, \
2655 {"$1", RTYPE_NUM | 1}, \
2656 {"$2", RTYPE_NUM | 2}, \
2657 {"$3", RTYPE_NUM | 3}, \
2658 {"$4", RTYPE_NUM | 4}, \
2659 {"$5", RTYPE_NUM | 5}, \
2660 {"$6", RTYPE_NUM | 6}, \
2661 {"$7", RTYPE_NUM | 7}, \
2662 {"$8", RTYPE_NUM | 8}, \
2663 {"$9", RTYPE_NUM | 9}, \
2664 {"$10", RTYPE_NUM | 10}, \
2665 {"$11", RTYPE_NUM | 11}, \
2666 {"$12", RTYPE_NUM | 12}, \
2667 {"$13", RTYPE_NUM | 13}, \
2668 {"$14", RTYPE_NUM | 14}, \
2669 {"$15", RTYPE_NUM | 15}, \
2670 {"$16", RTYPE_NUM | 16}, \
2671 {"$17", RTYPE_NUM | 17}, \
2672 {"$18", RTYPE_NUM | 18}, \
2673 {"$19", RTYPE_NUM | 19}, \
2674 {"$20", RTYPE_NUM | 20}, \
2675 {"$21", RTYPE_NUM | 21}, \
2676 {"$22", RTYPE_NUM | 22}, \
2677 {"$23", RTYPE_NUM | 23}, \
2678 {"$24", RTYPE_NUM | 24}, \
2679 {"$25", RTYPE_NUM | 25}, \
2680 {"$26", RTYPE_NUM | 26}, \
2681 {"$27", RTYPE_NUM | 27}, \
2682 {"$28", RTYPE_NUM | 28}, \
2683 {"$29", RTYPE_NUM | 29}, \
2684 {"$30", RTYPE_NUM | 30}, \
2685 {"$31", RTYPE_NUM | 31}
2686
2687 #define FPU_REGISTER_NAMES \
2688 {"$f0", RTYPE_FPU | 0}, \
2689 {"$f1", RTYPE_FPU | 1}, \
2690 {"$f2", RTYPE_FPU | 2}, \
2691 {"$f3", RTYPE_FPU | 3}, \
2692 {"$f4", RTYPE_FPU | 4}, \
2693 {"$f5", RTYPE_FPU | 5}, \
2694 {"$f6", RTYPE_FPU | 6}, \
2695 {"$f7", RTYPE_FPU | 7}, \
2696 {"$f8", RTYPE_FPU | 8}, \
2697 {"$f9", RTYPE_FPU | 9}, \
2698 {"$f10", RTYPE_FPU | 10}, \
2699 {"$f11", RTYPE_FPU | 11}, \
2700 {"$f12", RTYPE_FPU | 12}, \
2701 {"$f13", RTYPE_FPU | 13}, \
2702 {"$f14", RTYPE_FPU | 14}, \
2703 {"$f15", RTYPE_FPU | 15}, \
2704 {"$f16", RTYPE_FPU | 16}, \
2705 {"$f17", RTYPE_FPU | 17}, \
2706 {"$f18", RTYPE_FPU | 18}, \
2707 {"$f19", RTYPE_FPU | 19}, \
2708 {"$f20", RTYPE_FPU | 20}, \
2709 {"$f21", RTYPE_FPU | 21}, \
2710 {"$f22", RTYPE_FPU | 22}, \
2711 {"$f23", RTYPE_FPU | 23}, \
2712 {"$f24", RTYPE_FPU | 24}, \
2713 {"$f25", RTYPE_FPU | 25}, \
2714 {"$f26", RTYPE_FPU | 26}, \
2715 {"$f27", RTYPE_FPU | 27}, \
2716 {"$f28", RTYPE_FPU | 28}, \
2717 {"$f29", RTYPE_FPU | 29}, \
2718 {"$f30", RTYPE_FPU | 30}, \
2719 {"$f31", RTYPE_FPU | 31}
2720
2721 #define FPU_CONDITION_CODE_NAMES \
2722 {"$fcc0", RTYPE_FCC | 0}, \
2723 {"$fcc1", RTYPE_FCC | 1}, \
2724 {"$fcc2", RTYPE_FCC | 2}, \
2725 {"$fcc3", RTYPE_FCC | 3}, \
2726 {"$fcc4", RTYPE_FCC | 4}, \
2727 {"$fcc5", RTYPE_FCC | 5}, \
2728 {"$fcc6", RTYPE_FCC | 6}, \
2729 {"$fcc7", RTYPE_FCC | 7}
2730
2731 #define COPROC_CONDITION_CODE_NAMES \
2732 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2733 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2734 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2735 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2736 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2737 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2738 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2739 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2740
2741 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2742 {"$a4", RTYPE_GP | 8}, \
2743 {"$a5", RTYPE_GP | 9}, \
2744 {"$a6", RTYPE_GP | 10}, \
2745 {"$a7", RTYPE_GP | 11}, \
2746 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2747 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2748 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2749 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2750 {"$t0", RTYPE_GP | 12}, \
2751 {"$t1", RTYPE_GP | 13}, \
2752 {"$t2", RTYPE_GP | 14}, \
2753 {"$t3", RTYPE_GP | 15}
2754
2755 #define O32_SYMBOLIC_REGISTER_NAMES \
2756 {"$t0", RTYPE_GP | 8}, \
2757 {"$t1", RTYPE_GP | 9}, \
2758 {"$t2", RTYPE_GP | 10}, \
2759 {"$t3", RTYPE_GP | 11}, \
2760 {"$t4", RTYPE_GP | 12}, \
2761 {"$t5", RTYPE_GP | 13}, \
2762 {"$t6", RTYPE_GP | 14}, \
2763 {"$t7", RTYPE_GP | 15}, \
2764 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2765 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2766 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2767 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2768
2769 /* Remaining symbolic register names */
2770 #define SYMBOLIC_REGISTER_NAMES \
2771 {"$zero", RTYPE_GP | 0}, \
2772 {"$at", RTYPE_GP | 1}, \
2773 {"$AT", RTYPE_GP | 1}, \
2774 {"$v0", RTYPE_GP | 2}, \
2775 {"$v1", RTYPE_GP | 3}, \
2776 {"$a0", RTYPE_GP | 4}, \
2777 {"$a1", RTYPE_GP | 5}, \
2778 {"$a2", RTYPE_GP | 6}, \
2779 {"$a3", RTYPE_GP | 7}, \
2780 {"$s0", RTYPE_GP | 16}, \
2781 {"$s1", RTYPE_GP | 17}, \
2782 {"$s2", RTYPE_GP | 18}, \
2783 {"$s3", RTYPE_GP | 19}, \
2784 {"$s4", RTYPE_GP | 20}, \
2785 {"$s5", RTYPE_GP | 21}, \
2786 {"$s6", RTYPE_GP | 22}, \
2787 {"$s7", RTYPE_GP | 23}, \
2788 {"$t8", RTYPE_GP | 24}, \
2789 {"$t9", RTYPE_GP | 25}, \
2790 {"$k0", RTYPE_GP | 26}, \
2791 {"$kt0", RTYPE_GP | 26}, \
2792 {"$k1", RTYPE_GP | 27}, \
2793 {"$kt1", RTYPE_GP | 27}, \
2794 {"$gp", RTYPE_GP | 28}, \
2795 {"$sp", RTYPE_GP | 29}, \
2796 {"$s8", RTYPE_GP | 30}, \
2797 {"$fp", RTYPE_GP | 30}, \
2798 {"$ra", RTYPE_GP | 31}
2799
2800 #define MIPS16_SPECIAL_REGISTER_NAMES \
2801 {"$pc", RTYPE_PC | 0}
2802
2803 #define MDMX_VECTOR_REGISTER_NAMES \
2804 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2805 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2806 {"$v2", RTYPE_VEC | 2}, \
2807 {"$v3", RTYPE_VEC | 3}, \
2808 {"$v4", RTYPE_VEC | 4}, \
2809 {"$v5", RTYPE_VEC | 5}, \
2810 {"$v6", RTYPE_VEC | 6}, \
2811 {"$v7", RTYPE_VEC | 7}, \
2812 {"$v8", RTYPE_VEC | 8}, \
2813 {"$v9", RTYPE_VEC | 9}, \
2814 {"$v10", RTYPE_VEC | 10}, \
2815 {"$v11", RTYPE_VEC | 11}, \
2816 {"$v12", RTYPE_VEC | 12}, \
2817 {"$v13", RTYPE_VEC | 13}, \
2818 {"$v14", RTYPE_VEC | 14}, \
2819 {"$v15", RTYPE_VEC | 15}, \
2820 {"$v16", RTYPE_VEC | 16}, \
2821 {"$v17", RTYPE_VEC | 17}, \
2822 {"$v18", RTYPE_VEC | 18}, \
2823 {"$v19", RTYPE_VEC | 19}, \
2824 {"$v20", RTYPE_VEC | 20}, \
2825 {"$v21", RTYPE_VEC | 21}, \
2826 {"$v22", RTYPE_VEC | 22}, \
2827 {"$v23", RTYPE_VEC | 23}, \
2828 {"$v24", RTYPE_VEC | 24}, \
2829 {"$v25", RTYPE_VEC | 25}, \
2830 {"$v26", RTYPE_VEC | 26}, \
2831 {"$v27", RTYPE_VEC | 27}, \
2832 {"$v28", RTYPE_VEC | 28}, \
2833 {"$v29", RTYPE_VEC | 29}, \
2834 {"$v30", RTYPE_VEC | 30}, \
2835 {"$v31", RTYPE_VEC | 31}
2836
2837 #define R5900_I_NAMES \
2838 {"$I", RTYPE_R5900_I | 0}
2839
2840 #define R5900_Q_NAMES \
2841 {"$Q", RTYPE_R5900_Q | 0}
2842
2843 #define R5900_R_NAMES \
2844 {"$R", RTYPE_R5900_R | 0}
2845
2846 #define R5900_ACC_NAMES \
2847 {"$ACC", RTYPE_R5900_ACC | 0 }
2848
2849 #define MIPS_DSP_ACCUMULATOR_NAMES \
2850 {"$ac0", RTYPE_ACC | 0}, \
2851 {"$ac1", RTYPE_ACC | 1}, \
2852 {"$ac2", RTYPE_ACC | 2}, \
2853 {"$ac3", RTYPE_ACC | 3}
2854
2855 static const struct regname reg_names[] = {
2856 GENERIC_REGISTER_NUMBERS,
2857 FPU_REGISTER_NAMES,
2858 FPU_CONDITION_CODE_NAMES,
2859 COPROC_CONDITION_CODE_NAMES,
2860
2861 /* The $txx registers depends on the abi,
2862 these will be added later into the symbol table from
2863 one of the tables below once mips_abi is set after
2864 parsing of arguments from the command line. */
2865 SYMBOLIC_REGISTER_NAMES,
2866
2867 MIPS16_SPECIAL_REGISTER_NAMES,
2868 MDMX_VECTOR_REGISTER_NAMES,
2869 R5900_I_NAMES,
2870 R5900_Q_NAMES,
2871 R5900_R_NAMES,
2872 R5900_ACC_NAMES,
2873 MIPS_DSP_ACCUMULATOR_NAMES,
2874 {0, 0}
2875 };
2876
2877 static const struct regname reg_names_o32[] = {
2878 O32_SYMBOLIC_REGISTER_NAMES,
2879 {0, 0}
2880 };
2881
2882 static const struct regname reg_names_n32n64[] = {
2883 N32N64_SYMBOLIC_REGISTER_NAMES,
2884 {0, 0}
2885 };
2886
2887 /* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2888 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2889 of these register symbols, return the associated vector register,
2890 otherwise return SYMVAL itself. */
2891
2892 static unsigned int
2893 mips_prefer_vec_regno (unsigned int symval)
2894 {
2895 if ((symval & -2) == (RTYPE_GP | 2))
2896 return RTYPE_VEC | (symval & 1);
2897 return symval;
2898 }
2899
2900 /* Return true if string [S, E) is a valid register name, storing its
2901 symbol value in *SYMVAL_PTR if so. */
2902
2903 static bfd_boolean
2904 mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
2905 {
2906 char save_c;
2907 symbolS *symbol;
2908
2909 /* Terminate name. */
2910 save_c = *e;
2911 *e = '\0';
2912
2913 /* Look up the name. */
2914 symbol = symbol_find (s);
2915 *e = save_c;
2916
2917 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2918 return FALSE;
2919
2920 *symval_ptr = S_GET_VALUE (symbol);
2921 return TRUE;
2922 }
2923
2924 /* Return true if the string at *SPTR is a valid register name. Allow it
2925 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2926 is nonnull.
2927
2928 When returning true, move *SPTR past the register, store the
2929 register's symbol value in *SYMVAL_PTR and the channel mask in
2930 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2931 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2932 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2933
2934 static bfd_boolean
2935 mips_parse_register (char **sptr, unsigned int *symval_ptr,
2936 unsigned int *channels_ptr)
2937 {
2938 char *s, *e, *m;
2939 const char *q;
2940 unsigned int channels, symval, bit;
2941
2942 /* Find end of name. */
2943 s = e = *sptr;
2944 if (is_name_beginner (*e))
2945 ++e;
2946 while (is_part_of_name (*e))
2947 ++e;
2948
2949 channels = 0;
2950 if (!mips_parse_register_1 (s, e, &symval))
2951 {
2952 if (!channels_ptr)
2953 return FALSE;
2954
2955 /* Eat characters from the end of the string that are valid
2956 channel suffixes. The preceding register must be $ACC or
2957 end with a digit, so there is no ambiguity. */
2958 bit = 1;
2959 m = e;
2960 for (q = "wzyx"; *q; q++, bit <<= 1)
2961 if (m > s && m[-1] == *q)
2962 {
2963 --m;
2964 channels |= bit;
2965 }
2966
2967 if (channels == 0
2968 || !mips_parse_register_1 (s, m, &symval)
2969 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2970 return FALSE;
2971 }
2972
2973 *sptr = e;
2974 *symval_ptr = symval;
2975 if (channels_ptr)
2976 *channels_ptr = channels;
2977 return TRUE;
2978 }
2979
2980 /* Check if SPTR points at a valid register specifier according to TYPES.
2981 If so, then return 1, advance S to consume the specifier and store
2982 the register's number in REGNOP, otherwise return 0. */
2983
2984 static int
2985 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2986 {
2987 unsigned int regno;
2988
2989 if (mips_parse_register (s, &regno, NULL))
2990 {
2991 if (types & RTYPE_VEC)
2992 regno = mips_prefer_vec_regno (regno);
2993 if (regno & types)
2994 regno &= RNUM_MASK;
2995 else
2996 regno = ~0;
2997 }
2998 else
2999 {
3000 if (types & RWARN)
3001 as_warn (_("unrecognized register name `%s'"), *s);
3002 regno = ~0;
3003 }
3004 if (regnop)
3005 *regnop = regno;
3006 return regno <= RNUM_MASK;
3007 }
3008
3009 /* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
3010 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
3011
3012 static char *
3013 mips_parse_vu0_channels (char *s, unsigned int *channels)
3014 {
3015 unsigned int i;
3016
3017 *channels = 0;
3018 for (i = 0; i < 4; i++)
3019 if (*s == "xyzw"[i])
3020 {
3021 *channels |= 1 << (3 - i);
3022 ++s;
3023 }
3024 return s;
3025 }
3026
3027 /* Token types for parsed operand lists. */
3028 enum mips_operand_token_type {
3029 /* A plain register, e.g. $f2. */
3030 OT_REG,
3031
3032 /* A 4-bit XYZW channel mask. */
3033 OT_CHANNELS,
3034
3035 /* A constant vector index, e.g. [1]. */
3036 OT_INTEGER_INDEX,
3037
3038 /* A register vector index, e.g. [$2]. */
3039 OT_REG_INDEX,
3040
3041 /* A continuous range of registers, e.g. $s0-$s4. */
3042 OT_REG_RANGE,
3043
3044 /* A (possibly relocated) expression. */
3045 OT_INTEGER,
3046
3047 /* A floating-point value. */
3048 OT_FLOAT,
3049
3050 /* A single character. This can be '(', ')' or ',', but '(' only appears
3051 before OT_REGs. */
3052 OT_CHAR,
3053
3054 /* A doubled character, either "--" or "++". */
3055 OT_DOUBLE_CHAR,
3056
3057 /* The end of the operand list. */
3058 OT_END
3059 };
3060
3061 /* A parsed operand token. */
3062 struct mips_operand_token
3063 {
3064 /* The type of token. */
3065 enum mips_operand_token_type type;
3066 union
3067 {
3068 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
3069 unsigned int regno;
3070
3071 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3072 unsigned int channels;
3073
3074 /* The integer value of an OT_INTEGER_INDEX. */
3075 addressT index;
3076
3077 /* The two register symbol values involved in an OT_REG_RANGE. */
3078 struct {
3079 unsigned int regno1;
3080 unsigned int regno2;
3081 } reg_range;
3082
3083 /* The value of an OT_INTEGER. The value is represented as an
3084 expression and the relocation operators that were applied to
3085 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3086 relocation operators were used. */
3087 struct {
3088 expressionS value;
3089 bfd_reloc_code_real_type relocs[3];
3090 } integer;
3091
3092 /* The binary data for an OT_FLOAT constant, and the number of bytes
3093 in the constant. */
3094 struct {
3095 unsigned char data[8];
3096 int length;
3097 } flt;
3098
3099 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
3100 char ch;
3101 } u;
3102 };
3103
3104 /* An obstack used to construct lists of mips_operand_tokens. */
3105 static struct obstack mips_operand_tokens;
3106
3107 /* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3108
3109 static void
3110 mips_add_token (struct mips_operand_token *token,
3111 enum mips_operand_token_type type)
3112 {
3113 token->type = type;
3114 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3115 }
3116
3117 /* Check whether S is '(' followed by a register name. Add OT_CHAR
3118 and OT_REG tokens for them if so, and return a pointer to the first
3119 unconsumed character. Return null otherwise. */
3120
3121 static char *
3122 mips_parse_base_start (char *s)
3123 {
3124 struct mips_operand_token token;
3125 unsigned int regno, channels;
3126 bfd_boolean decrement_p;
3127
3128 if (*s != '(')
3129 return 0;
3130
3131 ++s;
3132 SKIP_SPACE_TABS (s);
3133
3134 /* Only match "--" as part of a base expression. In other contexts "--X"
3135 is a double negative. */
3136 decrement_p = (s[0] == '-' && s[1] == '-');
3137 if (decrement_p)
3138 {
3139 s += 2;
3140 SKIP_SPACE_TABS (s);
3141 }
3142
3143 /* Allow a channel specifier because that leads to better error messages
3144 than treating something like "$vf0x++" as an expression. */
3145 if (!mips_parse_register (&s, &regno, &channels))
3146 return 0;
3147
3148 token.u.ch = '(';
3149 mips_add_token (&token, OT_CHAR);
3150
3151 if (decrement_p)
3152 {
3153 token.u.ch = '-';
3154 mips_add_token (&token, OT_DOUBLE_CHAR);
3155 }
3156
3157 token.u.regno = regno;
3158 mips_add_token (&token, OT_REG);
3159
3160 if (channels)
3161 {
3162 token.u.channels = channels;
3163 mips_add_token (&token, OT_CHANNELS);
3164 }
3165
3166 /* For consistency, only match "++" as part of base expressions too. */
3167 SKIP_SPACE_TABS (s);
3168 if (s[0] == '+' && s[1] == '+')
3169 {
3170 s += 2;
3171 token.u.ch = '+';
3172 mips_add_token (&token, OT_DOUBLE_CHAR);
3173 }
3174
3175 return s;
3176 }
3177
3178 /* Parse one or more tokens from S. Return a pointer to the first
3179 unconsumed character on success. Return null if an error was found
3180 and store the error text in insn_error. FLOAT_FORMAT is as for
3181 mips_parse_arguments. */
3182
3183 static char *
3184 mips_parse_argument_token (char *s, char float_format)
3185 {
3186 char *end, *save_in;
3187 const char *err;
3188 unsigned int regno1, regno2, channels;
3189 struct mips_operand_token token;
3190
3191 /* First look for "($reg", since we want to treat that as an
3192 OT_CHAR and OT_REG rather than an expression. */
3193 end = mips_parse_base_start (s);
3194 if (end)
3195 return end;
3196
3197 /* Handle other characters that end up as OT_CHARs. */
3198 if (*s == ')' || *s == ',')
3199 {
3200 token.u.ch = *s;
3201 mips_add_token (&token, OT_CHAR);
3202 ++s;
3203 return s;
3204 }
3205
3206 /* Handle tokens that start with a register. */
3207 if (mips_parse_register (&s, &regno1, &channels))
3208 {
3209 if (channels)
3210 {
3211 /* A register and a VU0 channel suffix. */
3212 token.u.regno = regno1;
3213 mips_add_token (&token, OT_REG);
3214
3215 token.u.channels = channels;
3216 mips_add_token (&token, OT_CHANNELS);
3217 return s;
3218 }
3219
3220 SKIP_SPACE_TABS (s);
3221 if (*s == '-')
3222 {
3223 /* A register range. */
3224 ++s;
3225 SKIP_SPACE_TABS (s);
3226 if (!mips_parse_register (&s, &regno2, NULL))
3227 {
3228 set_insn_error (0, _("invalid register range"));
3229 return 0;
3230 }
3231
3232 token.u.reg_range.regno1 = regno1;
3233 token.u.reg_range.regno2 = regno2;
3234 mips_add_token (&token, OT_REG_RANGE);
3235 return s;
3236 }
3237
3238 /* Add the register itself. */
3239 token.u.regno = regno1;
3240 mips_add_token (&token, OT_REG);
3241
3242 /* Check for a vector index. */
3243 if (*s == '[')
3244 {
3245 ++s;
3246 SKIP_SPACE_TABS (s);
3247 if (mips_parse_register (&s, &token.u.regno, NULL))
3248 mips_add_token (&token, OT_REG_INDEX);
3249 else
3250 {
3251 expressionS element;
3252
3253 my_getExpression (&element, s);
3254 if (element.X_op != O_constant)
3255 {
3256 set_insn_error (0, _("vector element must be constant"));
3257 return 0;
3258 }
3259 s = expr_end;
3260 token.u.index = element.X_add_number;
3261 mips_add_token (&token, OT_INTEGER_INDEX);
3262 }
3263 SKIP_SPACE_TABS (s);
3264 if (*s != ']')
3265 {
3266 set_insn_error (0, _("missing `]'"));
3267 return 0;
3268 }
3269 ++s;
3270 }
3271 return s;
3272 }
3273
3274 if (float_format)
3275 {
3276 /* First try to treat expressions as floats. */
3277 save_in = input_line_pointer;
3278 input_line_pointer = s;
3279 err = md_atof (float_format, (char *) token.u.flt.data,
3280 &token.u.flt.length);
3281 end = input_line_pointer;
3282 input_line_pointer = save_in;
3283 if (err && *err)
3284 {
3285 set_insn_error (0, err);
3286 return 0;
3287 }
3288 if (s != end)
3289 {
3290 mips_add_token (&token, OT_FLOAT);
3291 return end;
3292 }
3293 }
3294
3295 /* Treat everything else as an integer expression. */
3296 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3297 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3298 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3299 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3300 s = expr_end;
3301 mips_add_token (&token, OT_INTEGER);
3302 return s;
3303 }
3304
3305 /* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3306 if expressions should be treated as 32-bit floating-point constants,
3307 'd' if they should be treated as 64-bit floating-point constants,
3308 or 0 if they should be treated as integer expressions (the usual case).
3309
3310 Return a list of tokens on success, otherwise return 0. The caller
3311 must obstack_free the list after use. */
3312
3313 static struct mips_operand_token *
3314 mips_parse_arguments (char *s, char float_format)
3315 {
3316 struct mips_operand_token token;
3317
3318 SKIP_SPACE_TABS (s);
3319 while (*s)
3320 {
3321 s = mips_parse_argument_token (s, float_format);
3322 if (!s)
3323 {
3324 obstack_free (&mips_operand_tokens,
3325 obstack_finish (&mips_operand_tokens));
3326 return 0;
3327 }
3328 SKIP_SPACE_TABS (s);
3329 }
3330 mips_add_token (&token, OT_END);
3331 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
3332 }
3333
3334 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3335 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
3336
3337 static bfd_boolean
3338 is_opcode_valid (const struct mips_opcode *mo)
3339 {
3340 int isa = mips_opts.isa;
3341 int ase = mips_opts.ase;
3342 int fp_s, fp_d;
3343 unsigned int i;
3344
3345 if (ISA_HAS_64BIT_REGS (isa))
3346 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3347 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3348 ase |= mips_ases[i].flags64;
3349
3350 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
3351 return FALSE;
3352
3353 /* Check whether the instruction or macro requires single-precision or
3354 double-precision floating-point support. Note that this information is
3355 stored differently in the opcode table for insns and macros. */
3356 if (mo->pinfo == INSN_MACRO)
3357 {
3358 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3359 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3360 }
3361 else
3362 {
3363 fp_s = mo->pinfo & FP_S;
3364 fp_d = mo->pinfo & FP_D;
3365 }
3366
3367 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3368 return FALSE;
3369
3370 if (fp_s && mips_opts.soft_float)
3371 return FALSE;
3372
3373 return TRUE;
3374 }
3375
3376 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
3377 selected ISA and architecture. */
3378
3379 static bfd_boolean
3380 is_opcode_valid_16 (const struct mips_opcode *mo)
3381 {
3382 int isa = mips_opts.isa;
3383 int ase = mips_opts.ase;
3384 unsigned int i;
3385
3386 if (ISA_HAS_64BIT_REGS (isa))
3387 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3388 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3389 ase |= mips_ases[i].flags64;
3390
3391 return opcode_is_member (mo, isa, ase, mips_opts.arch);
3392 }
3393
3394 /* Return TRUE if the size of the microMIPS opcode MO matches one
3395 explicitly requested. Always TRUE in the standard MIPS mode.
3396 Use is_size_valid_16 for MIPS16 opcodes. */
3397
3398 static bfd_boolean
3399 is_size_valid (const struct mips_opcode *mo)
3400 {
3401 if (!mips_opts.micromips)
3402 return TRUE;
3403
3404 if (mips_opts.insn32)
3405 {
3406 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3407 return FALSE;
3408 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3409 return FALSE;
3410 }
3411 if (!forced_insn_length)
3412 return TRUE;
3413 if (mo->pinfo == INSN_MACRO)
3414 return FALSE;
3415 return forced_insn_length == micromips_insn_length (mo);
3416 }
3417
3418 /* Return TRUE if the size of the MIPS16 opcode MO matches one
3419 explicitly requested. */
3420
3421 static bfd_boolean
3422 is_size_valid_16 (const struct mips_opcode *mo)
3423 {
3424 if (!forced_insn_length)
3425 return TRUE;
3426 if (mo->pinfo == INSN_MACRO)
3427 return FALSE;
3428 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3429 return FALSE;
3430 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3431 return FALSE;
3432 return TRUE;
3433 }
3434
3435 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
3436 of the preceding instruction. Always TRUE in the standard MIPS mode.
3437
3438 We don't accept macros in 16-bit delay slots to avoid a case where
3439 a macro expansion fails because it relies on a preceding 32-bit real
3440 instruction to have matched and does not handle the operands correctly.
3441 The only macros that may expand to 16-bit instructions are JAL that
3442 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3443 and BGT (that likewise cannot be placed in a delay slot) that decay to
3444 a NOP. In all these cases the macros precede any corresponding real
3445 instruction definitions in the opcode table, so they will match in the
3446 second pass where the size of the delay slot is ignored and therefore
3447 produce correct code. */
3448
3449 static bfd_boolean
3450 is_delay_slot_valid (const struct mips_opcode *mo)
3451 {
3452 if (!mips_opts.micromips)
3453 return TRUE;
3454
3455 if (mo->pinfo == INSN_MACRO)
3456 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
3457 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3458 && micromips_insn_length (mo) != 4)
3459 return FALSE;
3460 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3461 && micromips_insn_length (mo) != 2)
3462 return FALSE;
3463
3464 return TRUE;
3465 }
3466
3467 /* For consistency checking, verify that all bits of OPCODE are specified
3468 either by the match/mask part of the instruction definition, or by the
3469 operand list. Also build up a list of operands in OPERANDS.
3470
3471 INSN_BITS says which bits of the instruction are significant.
3472 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3473 provides the mips_operand description of each operand. DECODE_OPERAND
3474 is null for MIPS16 instructions. */
3475
3476 static int
3477 validate_mips_insn (const struct mips_opcode *opcode,
3478 unsigned long insn_bits,
3479 const struct mips_operand *(*decode_operand) (const char *),
3480 struct mips_operand_array *operands)
3481 {
3482 const char *s;
3483 unsigned long used_bits, doubled, undefined, opno, mask;
3484 const struct mips_operand *operand;
3485
3486 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3487 if ((mask & opcode->match) != opcode->match)
3488 {
3489 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3490 opcode->name, opcode->args);
3491 return 0;
3492 }
3493 used_bits = 0;
3494 opno = 0;
3495 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3496 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
3497 for (s = opcode->args; *s; ++s)
3498 switch (*s)
3499 {
3500 case ',':
3501 case '(':
3502 case ')':
3503 break;
3504
3505 case '#':
3506 s++;
3507 break;
3508
3509 default:
3510 if (!decode_operand)
3511 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
3512 else
3513 operand = decode_operand (s);
3514 if (!operand && opcode->pinfo != INSN_MACRO)
3515 {
3516 as_bad (_("internal: unknown operand type: %s %s"),
3517 opcode->name, opcode->args);
3518 return 0;
3519 }
3520 gas_assert (opno < MAX_OPERANDS);
3521 operands->operand[opno] = operand;
3522 if (!decode_operand && operand
3523 && operand->type == OP_INT && operand->lsb == 0
3524 && mips_opcode_32bit_p (opcode))
3525 used_bits |= mips16_immed_extend (-1, operand->size);
3526 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
3527 {
3528 used_bits = mips_insert_operand (operand, used_bits, -1);
3529 if (operand->type == OP_MDMX_IMM_REG)
3530 /* Bit 5 is the format selector (OB vs QH). The opcode table
3531 has separate entries for each format. */
3532 used_bits &= ~(1 << (operand->lsb + 5));
3533 if (operand->type == OP_ENTRY_EXIT_LIST)
3534 used_bits &= ~(mask & 0x700);
3535 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3536 operand field that cannot be fully described with LSB/SIZE. */
3537 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3538 used_bits &= ~0x6000;
3539 }
3540 /* Skip prefix characters. */
3541 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
3542 ++s;
3543 opno += 1;
3544 break;
3545 }
3546 doubled = used_bits & mask & insn_bits;
3547 if (doubled)
3548 {
3549 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3550 " %s %s"), doubled, opcode->name, opcode->args);
3551 return 0;
3552 }
3553 used_bits |= mask;
3554 undefined = ~used_bits & insn_bits;
3555 if (opcode->pinfo != INSN_MACRO && undefined)
3556 {
3557 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3558 undefined, opcode->name, opcode->args);
3559 return 0;
3560 }
3561 used_bits &= ~insn_bits;
3562 if (used_bits)
3563 {
3564 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3565 used_bits, opcode->name, opcode->args);
3566 return 0;
3567 }
3568 return 1;
3569 }
3570
3571 /* The MIPS16 version of validate_mips_insn. */
3572
3573 static int
3574 validate_mips16_insn (const struct mips_opcode *opcode,
3575 struct mips_operand_array *operands)
3576 {
3577 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
3578
3579 return validate_mips_insn (opcode, insn_bits, 0, operands);
3580 }
3581
3582 /* The microMIPS version of validate_mips_insn. */
3583
3584 static int
3585 validate_micromips_insn (const struct mips_opcode *opc,
3586 struct mips_operand_array *operands)
3587 {
3588 unsigned long insn_bits;
3589 unsigned long major;
3590 unsigned int length;
3591
3592 if (opc->pinfo == INSN_MACRO)
3593 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3594 operands);
3595
3596 length = micromips_insn_length (opc);
3597 if (length != 2 && length != 4)
3598 {
3599 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
3600 "%s %s"), length, opc->name, opc->args);
3601 return 0;
3602 }
3603 major = opc->match >> (10 + 8 * (length - 2));
3604 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3605 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3606 {
3607 as_bad (_("internal error: bad microMIPS opcode "
3608 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3609 return 0;
3610 }
3611
3612 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3613 insn_bits = 1 << 4 * length;
3614 insn_bits <<= 4 * length;
3615 insn_bits -= 1;
3616 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3617 operands);
3618 }
3619
3620 /* This function is called once, at assembler startup time. It should set up
3621 all the tables, etc. that the MD part of the assembler will need. */
3622
3623 void
3624 md_begin (void)
3625 {
3626 const char *retval = NULL;
3627 int i = 0;
3628 int broken = 0;
3629
3630 if (mips_pic != NO_PIC)
3631 {
3632 if (g_switch_seen && g_switch_value != 0)
3633 as_bad (_("-G may not be used in position-independent code"));
3634 g_switch_value = 0;
3635 }
3636 else if (mips_abicalls)
3637 {
3638 if (g_switch_seen && g_switch_value != 0)
3639 as_bad (_("-G may not be used with abicalls"));
3640 g_switch_value = 0;
3641 }
3642
3643 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3644 as_warn (_("could not set architecture and machine"));
3645
3646 op_hash = hash_new ();
3647
3648 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
3649 for (i = 0; i < NUMOPCODES;)
3650 {
3651 const char *name = mips_opcodes[i].name;
3652
3653 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
3654 if (retval != NULL)
3655 {
3656 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3657 mips_opcodes[i].name, retval);
3658 /* Probably a memory allocation problem? Give up now. */
3659 as_fatal (_("broken assembler, no assembly attempted"));
3660 }
3661 do
3662 {
3663 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3664 decode_mips_operand, &mips_operands[i]))
3665 broken = 1;
3666 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3667 {
3668 create_insn (&nop_insn, mips_opcodes + i);
3669 if (mips_fix_loongson2f_nop)
3670 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3671 nop_insn.fixed_p = 1;
3672 }
3673 ++i;
3674 }
3675 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3676 }
3677
3678 mips16_op_hash = hash_new ();
3679 mips16_operands = XCNEWVEC (struct mips_operand_array,
3680 bfd_mips16_num_opcodes);
3681
3682 i = 0;
3683 while (i < bfd_mips16_num_opcodes)
3684 {
3685 const char *name = mips16_opcodes[i].name;
3686
3687 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
3688 if (retval != NULL)
3689 as_fatal (_("internal: can't hash `%s': %s"),
3690 mips16_opcodes[i].name, retval);
3691 do
3692 {
3693 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3694 broken = 1;
3695 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3696 {
3697 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3698 mips16_nop_insn.fixed_p = 1;
3699 }
3700 ++i;
3701 }
3702 while (i < bfd_mips16_num_opcodes
3703 && strcmp (mips16_opcodes[i].name, name) == 0);
3704 }
3705
3706 micromips_op_hash = hash_new ();
3707 micromips_operands = XCNEWVEC (struct mips_operand_array,
3708 bfd_micromips_num_opcodes);
3709
3710 i = 0;
3711 while (i < bfd_micromips_num_opcodes)
3712 {
3713 const char *name = micromips_opcodes[i].name;
3714
3715 retval = hash_insert (micromips_op_hash, name,
3716 (void *) &micromips_opcodes[i]);
3717 if (retval != NULL)
3718 as_fatal (_("internal: can't hash `%s': %s"),
3719 micromips_opcodes[i].name, retval);
3720 do
3721 {
3722 struct mips_cl_insn *micromips_nop_insn;
3723
3724 if (!validate_micromips_insn (&micromips_opcodes[i],
3725 &micromips_operands[i]))
3726 broken = 1;
3727
3728 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3729 {
3730 if (micromips_insn_length (micromips_opcodes + i) == 2)
3731 micromips_nop_insn = &micromips_nop16_insn;
3732 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3733 micromips_nop_insn = &micromips_nop32_insn;
3734 else
3735 continue;
3736
3737 if (micromips_nop_insn->insn_mo == NULL
3738 && strcmp (name, "nop") == 0)
3739 {
3740 create_insn (micromips_nop_insn, micromips_opcodes + i);
3741 micromips_nop_insn->fixed_p = 1;
3742 }
3743 }
3744 }
3745 while (++i < bfd_micromips_num_opcodes
3746 && strcmp (micromips_opcodes[i].name, name) == 0);
3747 }
3748
3749 if (broken)
3750 as_fatal (_("broken assembler, no assembly attempted"));
3751
3752 /* We add all the general register names to the symbol table. This
3753 helps us detect invalid uses of them. */
3754 for (i = 0; reg_names[i].name; i++)
3755 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
3756 reg_names[i].num, /* & RNUM_MASK, */
3757 &zero_address_frag));
3758 if (HAVE_NEWABI)
3759 for (i = 0; reg_names_n32n64[i].name; i++)
3760 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
3761 reg_names_n32n64[i].num, /* & RNUM_MASK, */
3762 &zero_address_frag));
3763 else
3764 for (i = 0; reg_names_o32[i].name; i++)
3765 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
3766 reg_names_o32[i].num, /* & RNUM_MASK, */
3767 &zero_address_frag));
3768
3769 for (i = 0; i < 32; i++)
3770 {
3771 char regname[6];
3772
3773 /* R5900 VU0 floating-point register. */
3774 sprintf (regname, "$vf%d", i);
3775 symbol_table_insert (symbol_new (regname, reg_section,
3776 RTYPE_VF | i, &zero_address_frag));
3777
3778 /* R5900 VU0 integer register. */
3779 sprintf (regname, "$vi%d", i);
3780 symbol_table_insert (symbol_new (regname, reg_section,
3781 RTYPE_VI | i, &zero_address_frag));
3782
3783 /* MSA register. */
3784 sprintf (regname, "$w%d", i);
3785 symbol_table_insert (symbol_new (regname, reg_section,
3786 RTYPE_MSA | i, &zero_address_frag));
3787 }
3788
3789 obstack_init (&mips_operand_tokens);
3790
3791 mips_no_prev_insn ();
3792
3793 mips_gprmask = 0;
3794 mips_cprmask[0] = 0;
3795 mips_cprmask[1] = 0;
3796 mips_cprmask[2] = 0;
3797 mips_cprmask[3] = 0;
3798
3799 /* set the default alignment for the text section (2**2) */
3800 record_alignment (text_section, 2);
3801
3802 bfd_set_gp_size (stdoutput, g_switch_value);
3803
3804 /* On a native system other than VxWorks, sections must be aligned
3805 to 16 byte boundaries. When configured for an embedded ELF
3806 target, we don't bother. */
3807 if (strncmp (TARGET_OS, "elf", 3) != 0
3808 && strncmp (TARGET_OS, "vxworks", 7) != 0)
3809 {
3810 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3811 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3812 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3813 }
3814
3815 /* Create a .reginfo section for register masks and a .mdebug
3816 section for debugging information. */
3817 {
3818 segT seg;
3819 subsegT subseg;
3820 flagword flags;
3821 segT sec;
3822
3823 seg = now_seg;
3824 subseg = now_subseg;
3825
3826 /* The ABI says this section should be loaded so that the
3827 running program can access it. However, we don't load it
3828 if we are configured for an embedded target */
3829 flags = SEC_READONLY | SEC_DATA;
3830 if (strncmp (TARGET_OS, "elf", 3) != 0)
3831 flags |= SEC_ALLOC | SEC_LOAD;
3832
3833 if (mips_abi != N64_ABI)
3834 {
3835 sec = subseg_new (".reginfo", (subsegT) 0);
3836
3837 bfd_set_section_flags (stdoutput, sec, flags);
3838 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
3839
3840 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3841 }
3842 else
3843 {
3844 /* The 64-bit ABI uses a .MIPS.options section rather than
3845 .reginfo section. */
3846 sec = subseg_new (".MIPS.options", (subsegT) 0);
3847 bfd_set_section_flags (stdoutput, sec, flags);
3848 bfd_set_section_alignment (stdoutput, sec, 3);
3849
3850 /* Set up the option header. */
3851 {
3852 Elf_Internal_Options opthdr;
3853 char *f;
3854
3855 opthdr.kind = ODK_REGINFO;
3856 opthdr.size = (sizeof (Elf_External_Options)
3857 + sizeof (Elf64_External_RegInfo));
3858 opthdr.section = 0;
3859 opthdr.info = 0;
3860 f = frag_more (sizeof (Elf_External_Options));
3861 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3862 (Elf_External_Options *) f);
3863
3864 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3865 }
3866 }
3867
3868 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3869 bfd_set_section_flags (stdoutput, sec,
3870 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3871 bfd_set_section_alignment (stdoutput, sec, 3);
3872 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3873
3874 if (ECOFF_DEBUGGING)
3875 {
3876 sec = subseg_new (".mdebug", (subsegT) 0);
3877 (void) bfd_set_section_flags (stdoutput, sec,
3878 SEC_HAS_CONTENTS | SEC_READONLY);
3879 (void) bfd_set_section_alignment (stdoutput, sec, 2);
3880 }
3881 else if (mips_flag_pdr)
3882 {
3883 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3884 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3885 SEC_READONLY | SEC_RELOC
3886 | SEC_DEBUGGING);
3887 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3888 }
3889
3890 subseg_set (seg, subseg);
3891 }
3892
3893 if (mips_fix_vr4120)
3894 init_vr4120_conflicts ();
3895 }
3896
3897 static inline void
3898 fpabi_incompatible_with (int fpabi, const char *what)
3899 {
3900 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3901 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3902 }
3903
3904 static inline void
3905 fpabi_requires (int fpabi, const char *what)
3906 {
3907 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3908 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3909 }
3910
3911 /* Check -mabi and register sizes against the specified FP ABI. */
3912 static void
3913 check_fpabi (int fpabi)
3914 {
3915 switch (fpabi)
3916 {
3917 case Val_GNU_MIPS_ABI_FP_DOUBLE:
3918 if (file_mips_opts.soft_float)
3919 fpabi_incompatible_with (fpabi, "softfloat");
3920 else if (file_mips_opts.single_float)
3921 fpabi_incompatible_with (fpabi, "singlefloat");
3922 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3923 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3924 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3925 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
3926 break;
3927
3928 case Val_GNU_MIPS_ABI_FP_XX:
3929 if (mips_abi != O32_ABI)
3930 fpabi_requires (fpabi, "-mabi=32");
3931 else if (file_mips_opts.soft_float)
3932 fpabi_incompatible_with (fpabi, "softfloat");
3933 else if (file_mips_opts.single_float)
3934 fpabi_incompatible_with (fpabi, "singlefloat");
3935 else if (file_mips_opts.fp != 0)
3936 fpabi_requires (fpabi, "fp=xx");
3937 break;
3938
3939 case Val_GNU_MIPS_ABI_FP_64A:
3940 case Val_GNU_MIPS_ABI_FP_64:
3941 if (mips_abi != O32_ABI)
3942 fpabi_requires (fpabi, "-mabi=32");
3943 else if (file_mips_opts.soft_float)
3944 fpabi_incompatible_with (fpabi, "softfloat");
3945 else if (file_mips_opts.single_float)
3946 fpabi_incompatible_with (fpabi, "singlefloat");
3947 else if (file_mips_opts.fp != 64)
3948 fpabi_requires (fpabi, "fp=64");
3949 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3950 fpabi_incompatible_with (fpabi, "nooddspreg");
3951 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3952 fpabi_requires (fpabi, "nooddspreg");
3953 break;
3954
3955 case Val_GNU_MIPS_ABI_FP_SINGLE:
3956 if (file_mips_opts.soft_float)
3957 fpabi_incompatible_with (fpabi, "softfloat");
3958 else if (!file_mips_opts.single_float)
3959 fpabi_requires (fpabi, "singlefloat");
3960 break;
3961
3962 case Val_GNU_MIPS_ABI_FP_SOFT:
3963 if (!file_mips_opts.soft_float)
3964 fpabi_requires (fpabi, "softfloat");
3965 break;
3966
3967 case Val_GNU_MIPS_ABI_FP_OLD_64:
3968 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3969 Tag_GNU_MIPS_ABI_FP, fpabi);
3970 break;
3971
3972 case Val_GNU_MIPS_ABI_FP_NAN2008:
3973 /* Silently ignore compatibility value. */
3974 break;
3975
3976 default:
3977 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3978 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3979 break;
3980 }
3981 }
3982
3983 /* Perform consistency checks on the current options. */
3984
3985 static void
3986 mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3987 {
3988 /* Check the size of integer registers agrees with the ABI and ISA. */
3989 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3990 as_bad (_("`gp=64' used with a 32-bit processor"));
3991 else if (abi_checks
3992 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3993 as_bad (_("`gp=32' used with a 64-bit ABI"));
3994 else if (abi_checks
3995 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3996 as_bad (_("`gp=64' used with a 32-bit ABI"));
3997
3998 /* Check the size of the float registers agrees with the ABI and ISA. */
3999 switch (opts->fp)
4000 {
4001 case 0:
4002 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
4003 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
4004 else if (opts->single_float == 1)
4005 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
4006 break;
4007 case 64:
4008 if (!ISA_HAS_64BIT_FPRS (opts->isa))
4009 as_bad (_("`fp=64' used with a 32-bit fpu"));
4010 else if (abi_checks
4011 && ABI_NEEDS_32BIT_REGS (mips_abi)
4012 && !ISA_HAS_MXHC1 (opts->isa))
4013 as_warn (_("`fp=64' used with a 32-bit ABI"));
4014 break;
4015 case 32:
4016 if (abi_checks
4017 && ABI_NEEDS_64BIT_REGS (mips_abi))
4018 as_warn (_("`fp=32' used with a 64-bit ABI"));
4019 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
4020 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
4021 break;
4022 default:
4023 as_bad (_("Unknown size of floating point registers"));
4024 break;
4025 }
4026
4027 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
4028 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4029
4030 if (opts->micromips == 1 && opts->mips16 == 1)
4031 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
4032 else if (ISA_IS_R6 (opts->isa)
4033 && (opts->micromips == 1
4034 || opts->mips16 == 1))
4035 as_fatal (_("`%s' cannot be used with `%s'"),
4036 opts->micromips ? "micromips" : "mips16",
4037 mips_cpu_info_from_isa (opts->isa)->name);
4038
4039 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4040 as_fatal (_("branch relaxation is not supported in `%s'"),
4041 mips_cpu_info_from_isa (opts->isa)->name);
4042 }
4043
4044 /* Perform consistency checks on the module level options exactly once.
4045 This is a deferred check that happens:
4046 at the first .set directive
4047 or, at the first pseudo op that generates code (inc .dc.a)
4048 or, at the first instruction
4049 or, at the end. */
4050
4051 static void
4052 file_mips_check_options (void)
4053 {
4054 const struct mips_cpu_info *arch_info = 0;
4055
4056 if (file_mips_opts_checked)
4057 return;
4058
4059 /* The following code determines the register size.
4060 Similar code was added to GCC 3.3 (see override_options() in
4061 config/mips/mips.c). The GAS and GCC code should be kept in sync
4062 as much as possible. */
4063
4064 if (file_mips_opts.gp < 0)
4065 {
4066 /* Infer the integer register size from the ABI and processor.
4067 Restrict ourselves to 32-bit registers if that's all the
4068 processor has, or if the ABI cannot handle 64-bit registers. */
4069 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4070 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4071 ? 32 : 64;
4072 }
4073
4074 if (file_mips_opts.fp < 0)
4075 {
4076 /* No user specified float register size.
4077 ??? GAS treats single-float processors as though they had 64-bit
4078 float registers (although it complains when double-precision
4079 instructions are used). As things stand, saying they have 32-bit
4080 registers would lead to spurious "register must be even" messages.
4081 So here we assume float registers are never smaller than the
4082 integer ones. */
4083 if (file_mips_opts.gp == 64)
4084 /* 64-bit integer registers implies 64-bit float registers. */
4085 file_mips_opts.fp = 64;
4086 else if ((file_mips_opts.ase & FP64_ASES)
4087 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4088 /* Handle ASEs that require 64-bit float registers, if possible. */
4089 file_mips_opts.fp = 64;
4090 else if (ISA_IS_R6 (mips_opts.isa))
4091 /* R6 implies 64-bit float registers. */
4092 file_mips_opts.fp = 64;
4093 else
4094 /* 32-bit float registers. */
4095 file_mips_opts.fp = 32;
4096 }
4097
4098 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
4099
4100 /* Disable operations on odd-numbered floating-point registers by default
4101 when using the FPXX ABI. */
4102 if (file_mips_opts.oddspreg < 0)
4103 {
4104 if (file_mips_opts.fp == 0)
4105 file_mips_opts.oddspreg = 0;
4106 else
4107 file_mips_opts.oddspreg = 1;
4108 }
4109
4110 /* End of GCC-shared inference code. */
4111
4112 /* This flag is set when we have a 64-bit capable CPU but use only
4113 32-bit wide registers. Note that EABI does not use it. */
4114 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4115 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4116 || mips_abi == O32_ABI))
4117 mips_32bitmode = 1;
4118
4119 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4120 as_bad (_("trap exception not supported at ISA 1"));
4121
4122 /* If the selected architecture includes support for ASEs, enable
4123 generation of code for them. */
4124 if (file_mips_opts.mips16 == -1)
4125 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4126 if (file_mips_opts.micromips == -1)
4127 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4128 ? 1 : 0;
4129
4130 if (mips_nan2008 == -1)
4131 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4132 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4133 as_fatal (_("`%s' does not support legacy NaN"),
4134 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4135
4136 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4137 being selected implicitly. */
4138 if (file_mips_opts.fp != 64)
4139 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4140
4141 /* If the user didn't explicitly select or deselect a particular ASE,
4142 use the default setting for the CPU. */
4143 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4144
4145 /* Set up the current options. These may change throughout assembly. */
4146 mips_opts = file_mips_opts;
4147
4148 mips_check_isa_supports_ases ();
4149 mips_check_options (&file_mips_opts, TRUE);
4150 file_mips_opts_checked = TRUE;
4151
4152 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4153 as_warn (_("could not set architecture and machine"));
4154 }
4155
4156 void
4157 md_assemble (char *str)
4158 {
4159 struct mips_cl_insn insn;
4160 bfd_reloc_code_real_type unused_reloc[3]
4161 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
4162
4163 file_mips_check_options ();
4164
4165 imm_expr.X_op = O_absent;
4166 offset_expr.X_op = O_absent;
4167 offset_reloc[0] = BFD_RELOC_UNUSED;
4168 offset_reloc[1] = BFD_RELOC_UNUSED;
4169 offset_reloc[2] = BFD_RELOC_UNUSED;
4170
4171 mips_mark_labels ();
4172 mips_assembling_insn = TRUE;
4173 clear_insn_error ();
4174
4175 if (mips_opts.mips16)
4176 mips16_ip (str, &insn);
4177 else
4178 {
4179 mips_ip (str, &insn);
4180 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4181 str, insn.insn_opcode));
4182 }
4183
4184 if (insn_error.msg)
4185 report_insn_error (str);
4186 else if (insn.insn_mo->pinfo == INSN_MACRO)
4187 {
4188 macro_start ();
4189 if (mips_opts.mips16)
4190 mips16_macro (&insn);
4191 else
4192 macro (&insn, str);
4193 macro_end ();
4194 }
4195 else
4196 {
4197 if (offset_expr.X_op != O_absent)
4198 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
4199 else
4200 append_insn (&insn, NULL, unused_reloc, FALSE);
4201 }
4202
4203 mips_assembling_insn = FALSE;
4204 }
4205
4206 /* Convenience functions for abstracting away the differences between
4207 MIPS16 and non-MIPS16 relocations. */
4208
4209 static inline bfd_boolean
4210 mips16_reloc_p (bfd_reloc_code_real_type reloc)
4211 {
4212 switch (reloc)
4213 {
4214 case BFD_RELOC_MIPS16_JMP:
4215 case BFD_RELOC_MIPS16_GPREL:
4216 case BFD_RELOC_MIPS16_GOT16:
4217 case BFD_RELOC_MIPS16_CALL16:
4218 case BFD_RELOC_MIPS16_HI16_S:
4219 case BFD_RELOC_MIPS16_HI16:
4220 case BFD_RELOC_MIPS16_LO16:
4221 case BFD_RELOC_MIPS16_16_PCREL_S1:
4222 return TRUE;
4223
4224 default:
4225 return FALSE;
4226 }
4227 }
4228
4229 static inline bfd_boolean
4230 micromips_reloc_p (bfd_reloc_code_real_type reloc)
4231 {
4232 switch (reloc)
4233 {
4234 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4235 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4236 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4237 case BFD_RELOC_MICROMIPS_GPREL16:
4238 case BFD_RELOC_MICROMIPS_JMP:
4239 case BFD_RELOC_MICROMIPS_HI16:
4240 case BFD_RELOC_MICROMIPS_HI16_S:
4241 case BFD_RELOC_MICROMIPS_LO16:
4242 case BFD_RELOC_MICROMIPS_LITERAL:
4243 case BFD_RELOC_MICROMIPS_GOT16:
4244 case BFD_RELOC_MICROMIPS_CALL16:
4245 case BFD_RELOC_MICROMIPS_GOT_HI16:
4246 case BFD_RELOC_MICROMIPS_GOT_LO16:
4247 case BFD_RELOC_MICROMIPS_CALL_HI16:
4248 case BFD_RELOC_MICROMIPS_CALL_LO16:
4249 case BFD_RELOC_MICROMIPS_SUB:
4250 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4251 case BFD_RELOC_MICROMIPS_GOT_OFST:
4252 case BFD_RELOC_MICROMIPS_GOT_DISP:
4253 case BFD_RELOC_MICROMIPS_HIGHEST:
4254 case BFD_RELOC_MICROMIPS_HIGHER:
4255 case BFD_RELOC_MICROMIPS_SCN_DISP:
4256 case BFD_RELOC_MICROMIPS_JALR:
4257 return TRUE;
4258
4259 default:
4260 return FALSE;
4261 }
4262 }
4263
4264 static inline bfd_boolean
4265 jmp_reloc_p (bfd_reloc_code_real_type reloc)
4266 {
4267 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4268 }
4269
4270 static inline bfd_boolean
4271 b_reloc_p (bfd_reloc_code_real_type reloc)
4272 {
4273 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4274 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4275 || reloc == BFD_RELOC_16_PCREL_S2
4276 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
4277 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4278 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4279 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4280 }
4281
4282 static inline bfd_boolean
4283 got16_reloc_p (bfd_reloc_code_real_type reloc)
4284 {
4285 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
4286 || reloc == BFD_RELOC_MICROMIPS_GOT16);
4287 }
4288
4289 static inline bfd_boolean
4290 hi16_reloc_p (bfd_reloc_code_real_type reloc)
4291 {
4292 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
4293 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
4294 }
4295
4296 static inline bfd_boolean
4297 lo16_reloc_p (bfd_reloc_code_real_type reloc)
4298 {
4299 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
4300 || reloc == BFD_RELOC_MICROMIPS_LO16);
4301 }
4302
4303 static inline bfd_boolean
4304 jalr_reloc_p (bfd_reloc_code_real_type reloc)
4305 {
4306 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
4307 }
4308
4309 static inline bfd_boolean
4310 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4311 {
4312 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4313 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4314 }
4315
4316 /* Return true if RELOC is a PC-relative relocation that does not have
4317 full address range. */
4318
4319 static inline bfd_boolean
4320 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4321 {
4322 switch (reloc)
4323 {
4324 case BFD_RELOC_16_PCREL_S2:
4325 case BFD_RELOC_MIPS16_16_PCREL_S1:
4326 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4327 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4328 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4329 case BFD_RELOC_MIPS_21_PCREL_S2:
4330 case BFD_RELOC_MIPS_26_PCREL_S2:
4331 case BFD_RELOC_MIPS_18_PCREL_S3:
4332 case BFD_RELOC_MIPS_19_PCREL_S2:
4333 return TRUE;
4334
4335 case BFD_RELOC_32_PCREL:
4336 case BFD_RELOC_HI16_S_PCREL:
4337 case BFD_RELOC_LO16_PCREL:
4338 return HAVE_64BIT_ADDRESSES;
4339
4340 default:
4341 return FALSE;
4342 }
4343 }
4344
4345 /* Return true if the given relocation might need a matching %lo().
4346 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4347 need a matching %lo() when applied to local symbols. */
4348
4349 static inline bfd_boolean
4350 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
4351 {
4352 return (HAVE_IN_PLACE_ADDENDS
4353 && (hi16_reloc_p (reloc)
4354 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4355 all GOT16 relocations evaluate to "G". */
4356 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4357 }
4358
4359 /* Return the type of %lo() reloc needed by RELOC, given that
4360 reloc_needs_lo_p. */
4361
4362 static inline bfd_reloc_code_real_type
4363 matching_lo_reloc (bfd_reloc_code_real_type reloc)
4364 {
4365 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4366 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4367 : BFD_RELOC_LO16));
4368 }
4369
4370 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
4371 relocation. */
4372
4373 static inline bfd_boolean
4374 fixup_has_matching_lo_p (fixS *fixp)
4375 {
4376 return (fixp->fx_next != NULL
4377 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
4378 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4379 && fixp->fx_offset == fixp->fx_next->fx_offset);
4380 }
4381
4382 /* Move all labels in LABELS to the current insertion point. TEXT_P
4383 says whether the labels refer to text or data. */
4384
4385 static void
4386 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
4387 {
4388 struct insn_label_list *l;
4389 valueT val;
4390
4391 for (l = labels; l != NULL; l = l->next)
4392 {
4393 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
4394 symbol_set_frag (l->label, frag_now);
4395 val = (valueT) frag_now_fix ();
4396 /* MIPS16/microMIPS text labels are stored as odd. */
4397 if (text_p && HAVE_CODE_COMPRESSION)
4398 ++val;
4399 S_SET_VALUE (l->label, val);
4400 }
4401 }
4402
4403 /* Move all labels in insn_labels to the current insertion point
4404 and treat them as text labels. */
4405
4406 static void
4407 mips_move_text_labels (void)
4408 {
4409 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4410 }
4411
4412 /* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4413
4414 static bfd_boolean
4415 s_is_linkonce (symbolS *sym, segT from_seg)
4416 {
4417 bfd_boolean linkonce = FALSE;
4418 segT symseg = S_GET_SEGMENT (sym);
4419
4420 if (symseg != from_seg && !S_IS_LOCAL (sym))
4421 {
4422 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4423 linkonce = TRUE;
4424 /* The GNU toolchain uses an extension for ELF: a section
4425 beginning with the magic string .gnu.linkonce is a
4426 linkonce section. */
4427 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4428 sizeof ".gnu.linkonce" - 1) == 0)
4429 linkonce = TRUE;
4430 }
4431 return linkonce;
4432 }
4433
4434 /* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
4435 linker to handle them specially, such as generating jalx instructions
4436 when needed. We also make them odd for the duration of the assembly,
4437 in order to generate the right sort of code. We will make them even
4438 in the adjust_symtab routine, while leaving them marked. This is
4439 convenient for the debugger and the disassembler. The linker knows
4440 to make them odd again. */
4441
4442 static void
4443 mips_compressed_mark_label (symbolS *label)
4444 {
4445 gas_assert (HAVE_CODE_COMPRESSION);
4446
4447 if (mips_opts.mips16)
4448 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4449 else
4450 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
4451 if ((S_GET_VALUE (label) & 1) == 0
4452 /* Don't adjust the address if the label is global or weak, or
4453 in a link-once section, since we'll be emitting symbol reloc
4454 references to it which will be patched up by the linker, and
4455 the final value of the symbol may or may not be MIPS16/microMIPS. */
4456 && !S_IS_WEAK (label)
4457 && !S_IS_EXTERNAL (label)
4458 && !s_is_linkonce (label, now_seg))
4459 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4460 }
4461
4462 /* Mark preceding MIPS16 or microMIPS instruction labels. */
4463
4464 static void
4465 mips_compressed_mark_labels (void)
4466 {
4467 struct insn_label_list *l;
4468
4469 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4470 mips_compressed_mark_label (l->label);
4471 }
4472
4473 /* End the current frag. Make it a variant frag and record the
4474 relaxation info. */
4475
4476 static void
4477 relax_close_frag (void)
4478 {
4479 mips_macro_warning.first_frag = frag_now;
4480 frag_var (rs_machine_dependent, 0, 0,
4481 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4482 mips_pic != NO_PIC),
4483 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4484
4485 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4486 mips_relax.first_fixup = 0;
4487 }
4488
4489 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
4490 See the comment above RELAX_ENCODE for more details. */
4491
4492 static void
4493 relax_start (symbolS *symbol)
4494 {
4495 gas_assert (mips_relax.sequence == 0);
4496 mips_relax.sequence = 1;
4497 mips_relax.symbol = symbol;
4498 }
4499
4500 /* Start generating the second version of a relaxable sequence.
4501 See the comment above RELAX_ENCODE for more details. */
4502
4503 static void
4504 relax_switch (void)
4505 {
4506 gas_assert (mips_relax.sequence == 1);
4507 mips_relax.sequence = 2;
4508 }
4509
4510 /* End the current relaxable sequence. */
4511
4512 static void
4513 relax_end (void)
4514 {
4515 gas_assert (mips_relax.sequence == 2);
4516 relax_close_frag ();
4517 mips_relax.sequence = 0;
4518 }
4519
4520 /* Return true if IP is a delayed branch or jump. */
4521
4522 static inline bfd_boolean
4523 delayed_branch_p (const struct mips_cl_insn *ip)
4524 {
4525 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4526 | INSN_COND_BRANCH_DELAY
4527 | INSN_COND_BRANCH_LIKELY)) != 0;
4528 }
4529
4530 /* Return true if IP is a compact branch or jump. */
4531
4532 static inline bfd_boolean
4533 compact_branch_p (const struct mips_cl_insn *ip)
4534 {
4535 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4536 | INSN2_COND_BRANCH)) != 0;
4537 }
4538
4539 /* Return true if IP is an unconditional branch or jump. */
4540
4541 static inline bfd_boolean
4542 uncond_branch_p (const struct mips_cl_insn *ip)
4543 {
4544 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
4545 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
4546 }
4547
4548 /* Return true if IP is a branch-likely instruction. */
4549
4550 static inline bfd_boolean
4551 branch_likely_p (const struct mips_cl_insn *ip)
4552 {
4553 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4554 }
4555
4556 /* Return the type of nop that should be used to fill the delay slot
4557 of delayed branch IP. */
4558
4559 static struct mips_cl_insn *
4560 get_delay_slot_nop (const struct mips_cl_insn *ip)
4561 {
4562 if (mips_opts.micromips
4563 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4564 return &micromips_nop32_insn;
4565 return NOP_INSN;
4566 }
4567
4568 /* Return a mask that has bit N set if OPCODE reads the register(s)
4569 in operand N. */
4570
4571 static unsigned int
4572 insn_read_mask (const struct mips_opcode *opcode)
4573 {
4574 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4575 }
4576
4577 /* Return a mask that has bit N set if OPCODE writes to the register(s)
4578 in operand N. */
4579
4580 static unsigned int
4581 insn_write_mask (const struct mips_opcode *opcode)
4582 {
4583 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4584 }
4585
4586 /* Return a mask of the registers specified by operand OPERAND of INSN.
4587 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4588 is set. */
4589
4590 static unsigned int
4591 operand_reg_mask (const struct mips_cl_insn *insn,
4592 const struct mips_operand *operand,
4593 unsigned int type_mask)
4594 {
4595 unsigned int uval, vsel;
4596
4597 switch (operand->type)
4598 {
4599 case OP_INT:
4600 case OP_MAPPED_INT:
4601 case OP_MSB:
4602 case OP_PCREL:
4603 case OP_PERF_REG:
4604 case OP_ADDIUSP_INT:
4605 case OP_ENTRY_EXIT_LIST:
4606 case OP_REPEAT_DEST_REG:
4607 case OP_REPEAT_PREV_REG:
4608 case OP_PC:
4609 case OP_VU0_SUFFIX:
4610 case OP_VU0_MATCH_SUFFIX:
4611 case OP_IMM_INDEX:
4612 abort ();
4613
4614 case OP_REG28:
4615 return 1 << 28;
4616
4617 case OP_REG:
4618 case OP_OPTIONAL_REG:
4619 {
4620 const struct mips_reg_operand *reg_op;
4621
4622 reg_op = (const struct mips_reg_operand *) operand;
4623 if (!(type_mask & (1 << reg_op->reg_type)))
4624 return 0;
4625 uval = insn_extract_operand (insn, operand);
4626 return 1 << mips_decode_reg_operand (reg_op, uval);
4627 }
4628
4629 case OP_REG_PAIR:
4630 {
4631 const struct mips_reg_pair_operand *pair_op;
4632
4633 pair_op = (const struct mips_reg_pair_operand *) operand;
4634 if (!(type_mask & (1 << pair_op->reg_type)))
4635 return 0;
4636 uval = insn_extract_operand (insn, operand);
4637 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4638 }
4639
4640 case OP_CLO_CLZ_DEST:
4641 if (!(type_mask & (1 << OP_REG_GP)))
4642 return 0;
4643 uval = insn_extract_operand (insn, operand);
4644 return (1 << (uval & 31)) | (1 << (uval >> 5));
4645
4646 case OP_SAME_RS_RT:
4647 if (!(type_mask & (1 << OP_REG_GP)))
4648 return 0;
4649 uval = insn_extract_operand (insn, operand);
4650 gas_assert ((uval & 31) == (uval >> 5));
4651 return 1 << (uval & 31);
4652
4653 case OP_CHECK_PREV:
4654 case OP_NON_ZERO_REG:
4655 if (!(type_mask & (1 << OP_REG_GP)))
4656 return 0;
4657 uval = insn_extract_operand (insn, operand);
4658 return 1 << (uval & 31);
4659
4660 case OP_LWM_SWM_LIST:
4661 abort ();
4662
4663 case OP_SAVE_RESTORE_LIST:
4664 abort ();
4665
4666 case OP_MDMX_IMM_REG:
4667 if (!(type_mask & (1 << OP_REG_VEC)))
4668 return 0;
4669 uval = insn_extract_operand (insn, operand);
4670 vsel = uval >> 5;
4671 if ((vsel & 0x18) == 0x18)
4672 return 0;
4673 return 1 << (uval & 31);
4674
4675 case OP_REG_INDEX:
4676 if (!(type_mask & (1 << OP_REG_GP)))
4677 return 0;
4678 return 1 << insn_extract_operand (insn, operand);
4679 }
4680 abort ();
4681 }
4682
4683 /* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4684 where bit N of OPNO_MASK is set if operand N should be included.
4685 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4686 is set. */
4687
4688 static unsigned int
4689 insn_reg_mask (const struct mips_cl_insn *insn,
4690 unsigned int type_mask, unsigned int opno_mask)
4691 {
4692 unsigned int opno, reg_mask;
4693
4694 opno = 0;
4695 reg_mask = 0;
4696 while (opno_mask != 0)
4697 {
4698 if (opno_mask & 1)
4699 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4700 opno_mask >>= 1;
4701 opno += 1;
4702 }
4703 return reg_mask;
4704 }
4705
4706 /* Return the mask of core registers that IP reads. */
4707
4708 static unsigned int
4709 gpr_read_mask (const struct mips_cl_insn *ip)
4710 {
4711 unsigned long pinfo, pinfo2;
4712 unsigned int mask;
4713
4714 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4715 pinfo = ip->insn_mo->pinfo;
4716 pinfo2 = ip->insn_mo->pinfo2;
4717 if (pinfo & INSN_UDI)
4718 {
4719 /* UDI instructions have traditionally been assumed to read RS
4720 and RT. */
4721 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4722 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4723 }
4724 if (pinfo & INSN_READ_GPR_24)
4725 mask |= 1 << 24;
4726 if (pinfo2 & INSN2_READ_GPR_16)
4727 mask |= 1 << 16;
4728 if (pinfo2 & INSN2_READ_SP)
4729 mask |= 1 << SP;
4730 if (pinfo2 & INSN2_READ_GPR_31)
4731 mask |= 1 << 31;
4732 /* Don't include register 0. */
4733 return mask & ~1;
4734 }
4735
4736 /* Return the mask of core registers that IP writes. */
4737
4738 static unsigned int
4739 gpr_write_mask (const struct mips_cl_insn *ip)
4740 {
4741 unsigned long pinfo, pinfo2;
4742 unsigned int mask;
4743
4744 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4745 pinfo = ip->insn_mo->pinfo;
4746 pinfo2 = ip->insn_mo->pinfo2;
4747 if (pinfo & INSN_WRITE_GPR_24)
4748 mask |= 1 << 24;
4749 if (pinfo & INSN_WRITE_GPR_31)
4750 mask |= 1 << 31;
4751 if (pinfo & INSN_UDI)
4752 /* UDI instructions have traditionally been assumed to write to RD. */
4753 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4754 if (pinfo2 & INSN2_WRITE_SP)
4755 mask |= 1 << SP;
4756 /* Don't include register 0. */
4757 return mask & ~1;
4758 }
4759
4760 /* Return the mask of floating-point registers that IP reads. */
4761
4762 static unsigned int
4763 fpr_read_mask (const struct mips_cl_insn *ip)
4764 {
4765 unsigned long pinfo;
4766 unsigned int mask;
4767
4768 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4769 | (1 << OP_REG_MSA)),
4770 insn_read_mask (ip->insn_mo));
4771 pinfo = ip->insn_mo->pinfo;
4772 /* Conservatively treat all operands to an FP_D instruction are doubles.
4773 (This is overly pessimistic for things like cvt.d.s.) */
4774 if (FPR_SIZE != 64 && (pinfo & FP_D))
4775 mask |= mask << 1;
4776 return mask;
4777 }
4778
4779 /* Return the mask of floating-point registers that IP writes. */
4780
4781 static unsigned int
4782 fpr_write_mask (const struct mips_cl_insn *ip)
4783 {
4784 unsigned long pinfo;
4785 unsigned int mask;
4786
4787 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4788 | (1 << OP_REG_MSA)),
4789 insn_write_mask (ip->insn_mo));
4790 pinfo = ip->insn_mo->pinfo;
4791 /* Conservatively treat all operands to an FP_D instruction are doubles.
4792 (This is overly pessimistic for things like cvt.s.d.) */
4793 if (FPR_SIZE != 64 && (pinfo & FP_D))
4794 mask |= mask << 1;
4795 return mask;
4796 }
4797
4798 /* Operand OPNUM of INSN is an odd-numbered floating-point register.
4799 Check whether that is allowed. */
4800
4801 static bfd_boolean
4802 mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4803 {
4804 const char *s = insn->name;
4805 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4806 || FPR_SIZE == 64)
4807 && mips_opts.oddspreg;
4808
4809 if (insn->pinfo == INSN_MACRO)
4810 /* Let a macro pass, we'll catch it later when it is expanded. */
4811 return TRUE;
4812
4813 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4814 otherwise it depends on oddspreg. */
4815 if ((insn->pinfo & FP_S)
4816 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
4817 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
4818 return FPR_SIZE == 32 || oddspreg;
4819
4820 /* Allow odd registers for single-precision ops and double-precision if the
4821 floating-point registers are 64-bit wide. */
4822 switch (insn->pinfo & (FP_S | FP_D))
4823 {
4824 case FP_S:
4825 case 0:
4826 return oddspreg;
4827 case FP_D:
4828 return FPR_SIZE == 64;
4829 default:
4830 break;
4831 }
4832
4833 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4834 s = strchr (insn->name, '.');
4835 if (s != NULL && opnum == 2)
4836 s = strchr (s + 1, '.');
4837 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4838 return oddspreg;
4839
4840 return FPR_SIZE == 64;
4841 }
4842
4843 /* Information about an instruction argument that we're trying to match. */
4844 struct mips_arg_info
4845 {
4846 /* The instruction so far. */
4847 struct mips_cl_insn *insn;
4848
4849 /* The first unconsumed operand token. */
4850 struct mips_operand_token *token;
4851
4852 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4853 int opnum;
4854
4855 /* The 1-based argument number, for error reporting. This does not
4856 count elided optional registers, etc.. */
4857 int argnum;
4858
4859 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4860 unsigned int last_regno;
4861
4862 /* If the first operand was an OP_REG, this is the register that it
4863 specified, otherwise it is ILLEGAL_REG. */
4864 unsigned int dest_regno;
4865
4866 /* The value of the last OP_INT operand. Only used for OP_MSB,
4867 where it gives the lsb position. */
4868 unsigned int last_op_int;
4869
4870 /* If true, match routines should assume that no later instruction
4871 alternative matches and should therefore be as accommodating as
4872 possible. Match routines should not report errors if something
4873 is only invalid for !LAX_MATCH. */
4874 bfd_boolean lax_match;
4875
4876 /* True if a reference to the current AT register was seen. */
4877 bfd_boolean seen_at;
4878 };
4879
4880 /* Record that the argument is out of range. */
4881
4882 static void
4883 match_out_of_range (struct mips_arg_info *arg)
4884 {
4885 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4886 }
4887
4888 /* Record that the argument isn't constant but needs to be. */
4889
4890 static void
4891 match_not_constant (struct mips_arg_info *arg)
4892 {
4893 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4894 arg->argnum);
4895 }
4896
4897 /* Try to match an OT_CHAR token for character CH. Consume the token
4898 and return true on success, otherwise return false. */
4899
4900 static bfd_boolean
4901 match_char (struct mips_arg_info *arg, char ch)
4902 {
4903 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4904 {
4905 ++arg->token;
4906 if (ch == ',')
4907 arg->argnum += 1;
4908 return TRUE;
4909 }
4910 return FALSE;
4911 }
4912
4913 /* Try to get an expression from the next tokens in ARG. Consume the
4914 tokens and return true on success, storing the expression value in
4915 VALUE and relocation types in R. */
4916
4917 static bfd_boolean
4918 match_expression (struct mips_arg_info *arg, expressionS *value,
4919 bfd_reloc_code_real_type *r)
4920 {
4921 /* If the next token is a '(' that was parsed as being part of a base
4922 expression, assume we have an elided offset. The later match will fail
4923 if this turns out to be wrong. */
4924 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4925 {
4926 value->X_op = O_constant;
4927 value->X_add_number = 0;
4928 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4929 return TRUE;
4930 }
4931
4932 /* Reject register-based expressions such as "0+$2" and "(($2))".
4933 For plain registers the default error seems more appropriate. */
4934 if (arg->token->type == OT_INTEGER
4935 && arg->token->u.integer.value.X_op == O_register)
4936 {
4937 set_insn_error (arg->argnum, _("register value used as expression"));
4938 return FALSE;
4939 }
4940
4941 if (arg->token->type == OT_INTEGER)
4942 {
4943 *value = arg->token->u.integer.value;
4944 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4945 ++arg->token;
4946 return TRUE;
4947 }
4948
4949 set_insn_error_i
4950 (arg->argnum, _("operand %d must be an immediate expression"),
4951 arg->argnum);
4952 return FALSE;
4953 }
4954
4955 /* Try to get a constant expression from the next tokens in ARG. Consume
4956 the tokens and return true on success, storing the constant value
4957 in *VALUE. */
4958
4959 static bfd_boolean
4960 match_const_int (struct mips_arg_info *arg, offsetT *value)
4961 {
4962 expressionS ex;
4963 bfd_reloc_code_real_type r[3];
4964
4965 if (!match_expression (arg, &ex, r))
4966 return FALSE;
4967
4968 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
4969 *value = ex.X_add_number;
4970 else
4971 {
4972 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
4973 match_out_of_range (arg);
4974 else
4975 match_not_constant (arg);
4976 return FALSE;
4977 }
4978 return TRUE;
4979 }
4980
4981 /* Return the RTYPE_* flags for a register operand of type TYPE that
4982 appears in instruction OPCODE. */
4983
4984 static unsigned int
4985 convert_reg_type (const struct mips_opcode *opcode,
4986 enum mips_reg_operand_type type)
4987 {
4988 switch (type)
4989 {
4990 case OP_REG_GP:
4991 return RTYPE_NUM | RTYPE_GP;
4992
4993 case OP_REG_FP:
4994 /* Allow vector register names for MDMX if the instruction is a 64-bit
4995 FPR load, store or move (including moves to and from GPRs). */
4996 if ((mips_opts.ase & ASE_MDMX)
4997 && (opcode->pinfo & FP_D)
4998 && (opcode->pinfo & (INSN_COPROC_MOVE
4999 | INSN_COPROC_MEMORY_DELAY
5000 | INSN_LOAD_COPROC
5001 | INSN_LOAD_MEMORY
5002 | INSN_STORE_MEMORY)))
5003 return RTYPE_FPU | RTYPE_VEC;
5004 return RTYPE_FPU;
5005
5006 case OP_REG_CCC:
5007 if (opcode->pinfo & (FP_D | FP_S))
5008 return RTYPE_CCC | RTYPE_FCC;
5009 return RTYPE_CCC;
5010
5011 case OP_REG_VEC:
5012 if (opcode->membership & INSN_5400)
5013 return RTYPE_FPU;
5014 return RTYPE_FPU | RTYPE_VEC;
5015
5016 case OP_REG_ACC:
5017 return RTYPE_ACC;
5018
5019 case OP_REG_COPRO:
5020 if (opcode->name[strlen (opcode->name) - 1] == '0')
5021 return RTYPE_NUM | RTYPE_CP0;
5022 return RTYPE_NUM;
5023
5024 case OP_REG_HW:
5025 return RTYPE_NUM;
5026
5027 case OP_REG_VI:
5028 return RTYPE_NUM | RTYPE_VI;
5029
5030 case OP_REG_VF:
5031 return RTYPE_NUM | RTYPE_VF;
5032
5033 case OP_REG_R5900_I:
5034 return RTYPE_R5900_I;
5035
5036 case OP_REG_R5900_Q:
5037 return RTYPE_R5900_Q;
5038
5039 case OP_REG_R5900_R:
5040 return RTYPE_R5900_R;
5041
5042 case OP_REG_R5900_ACC:
5043 return RTYPE_R5900_ACC;
5044
5045 case OP_REG_MSA:
5046 return RTYPE_MSA;
5047
5048 case OP_REG_MSA_CTRL:
5049 return RTYPE_NUM;
5050 }
5051 abort ();
5052 }
5053
5054 /* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5055
5056 static void
5057 check_regno (struct mips_arg_info *arg,
5058 enum mips_reg_operand_type type, unsigned int regno)
5059 {
5060 if (AT && type == OP_REG_GP && regno == AT)
5061 arg->seen_at = TRUE;
5062
5063 if (type == OP_REG_FP
5064 && (regno & 1) != 0
5065 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
5066 {
5067 /* This was a warning prior to introducing O32 FPXX and FP64 support
5068 so maintain a warning for FP32 but raise an error for the new
5069 cases. */
5070 if (FPR_SIZE == 32)
5071 as_warn (_("float register should be even, was %d"), regno);
5072 else
5073 as_bad (_("float register should be even, was %d"), regno);
5074 }
5075
5076 if (type == OP_REG_CCC)
5077 {
5078 const char *name;
5079 size_t length;
5080
5081 name = arg->insn->insn_mo->name;
5082 length = strlen (name);
5083 if ((regno & 1) != 0
5084 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5085 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
5086 as_warn (_("condition code register should be even for %s, was %d"),
5087 name, regno);
5088
5089 if ((regno & 3) != 0
5090 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
5091 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
5092 name, regno);
5093 }
5094 }
5095
5096 /* ARG is a register with symbol value SYMVAL. Try to interpret it as
5097 a register of type TYPE. Return true on success, storing the register
5098 number in *REGNO and warning about any dubious uses. */
5099
5100 static bfd_boolean
5101 match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5102 unsigned int symval, unsigned int *regno)
5103 {
5104 if (type == OP_REG_VEC)
5105 symval = mips_prefer_vec_regno (symval);
5106 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5107 return FALSE;
5108
5109 *regno = symval & RNUM_MASK;
5110 check_regno (arg, type, *regno);
5111 return TRUE;
5112 }
5113
5114 /* Try to interpret the next token in ARG as a register of type TYPE.
5115 Consume the token and return true on success, storing the register
5116 number in *REGNO. Return false on failure. */
5117
5118 static bfd_boolean
5119 match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5120 unsigned int *regno)
5121 {
5122 if (arg->token->type == OT_REG
5123 && match_regno (arg, type, arg->token->u.regno, regno))
5124 {
5125 ++arg->token;
5126 return TRUE;
5127 }
5128 return FALSE;
5129 }
5130
5131 /* Try to interpret the next token in ARG as a range of registers of type TYPE.
5132 Consume the token and return true on success, storing the register numbers
5133 in *REGNO1 and *REGNO2. Return false on failure. */
5134
5135 static bfd_boolean
5136 match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5137 unsigned int *regno1, unsigned int *regno2)
5138 {
5139 if (match_reg (arg, type, regno1))
5140 {
5141 *regno2 = *regno1;
5142 return TRUE;
5143 }
5144 if (arg->token->type == OT_REG_RANGE
5145 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5146 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5147 && *regno1 <= *regno2)
5148 {
5149 ++arg->token;
5150 return TRUE;
5151 }
5152 return FALSE;
5153 }
5154
5155 /* OP_INT matcher. */
5156
5157 static bfd_boolean
5158 match_int_operand (struct mips_arg_info *arg,
5159 const struct mips_operand *operand_base)
5160 {
5161 const struct mips_int_operand *operand;
5162 unsigned int uval;
5163 int min_val, max_val, factor;
5164 offsetT sval;
5165
5166 operand = (const struct mips_int_operand *) operand_base;
5167 factor = 1 << operand->shift;
5168 min_val = mips_int_operand_min (operand);
5169 max_val = mips_int_operand_max (operand);
5170
5171 if (operand_base->lsb == 0
5172 && operand_base->size == 16
5173 && operand->shift == 0
5174 && operand->bias == 0
5175 && (operand->max_val == 32767 || operand->max_val == 65535))
5176 {
5177 /* The operand can be relocated. */
5178 if (!match_expression (arg, &offset_expr, offset_reloc))
5179 return FALSE;
5180
5181 if (offset_expr.X_op == O_big)
5182 {
5183 match_out_of_range (arg);
5184 return FALSE;
5185 }
5186
5187 if (offset_reloc[0] != BFD_RELOC_UNUSED)
5188 /* Relocation operators were used. Accept the argument and
5189 leave the relocation value in offset_expr and offset_relocs
5190 for the caller to process. */
5191 return TRUE;
5192
5193 if (offset_expr.X_op != O_constant)
5194 {
5195 /* Accept non-constant operands if no later alternative matches,
5196 leaving it for the caller to process. */
5197 if (!arg->lax_match)
5198 {
5199 match_not_constant (arg);
5200 return FALSE;
5201 }
5202 offset_reloc[0] = BFD_RELOC_LO16;
5203 return TRUE;
5204 }
5205
5206 /* Clear the global state; we're going to install the operand
5207 ourselves. */
5208 sval = offset_expr.X_add_number;
5209 offset_expr.X_op = O_absent;
5210
5211 /* For compatibility with older assemblers, we accept
5212 0x8000-0xffff as signed 16-bit numbers when only
5213 signed numbers are allowed. */
5214 if (sval > max_val)
5215 {
5216 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5217 if (!arg->lax_match && sval <= max_val)
5218 {
5219 match_out_of_range (arg);
5220 return FALSE;
5221 }
5222 }
5223 }
5224 else
5225 {
5226 if (!match_const_int (arg, &sval))
5227 return FALSE;
5228 }
5229
5230 arg->last_op_int = sval;
5231
5232 if (sval < min_val || sval > max_val || sval % factor)
5233 {
5234 match_out_of_range (arg);
5235 return FALSE;
5236 }
5237
5238 uval = (unsigned int) sval >> operand->shift;
5239 uval -= operand->bias;
5240
5241 /* Handle -mfix-cn63xxp1. */
5242 if (arg->opnum == 1
5243 && mips_fix_cn63xxp1
5244 && !mips_opts.micromips
5245 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5246 switch (uval)
5247 {
5248 case 5:
5249 case 25:
5250 case 26:
5251 case 27:
5252 case 28:
5253 case 29:
5254 case 30:
5255 case 31:
5256 /* These are ok. */
5257 break;
5258
5259 default:
5260 /* The rest must be changed to 28. */
5261 uval = 28;
5262 break;
5263 }
5264
5265 insn_insert_operand (arg->insn, operand_base, uval);
5266 return TRUE;
5267 }
5268
5269 /* OP_MAPPED_INT matcher. */
5270
5271 static bfd_boolean
5272 match_mapped_int_operand (struct mips_arg_info *arg,
5273 const struct mips_operand *operand_base)
5274 {
5275 const struct mips_mapped_int_operand *operand;
5276 unsigned int uval, num_vals;
5277 offsetT sval;
5278
5279 operand = (const struct mips_mapped_int_operand *) operand_base;
5280 if (!match_const_int (arg, &sval))
5281 return FALSE;
5282
5283 num_vals = 1 << operand_base->size;
5284 for (uval = 0; uval < num_vals; uval++)
5285 if (operand->int_map[uval] == sval)
5286 break;
5287 if (uval == num_vals)
5288 {
5289 match_out_of_range (arg);
5290 return FALSE;
5291 }
5292
5293 insn_insert_operand (arg->insn, operand_base, uval);
5294 return TRUE;
5295 }
5296
5297 /* OP_MSB matcher. */
5298
5299 static bfd_boolean
5300 match_msb_operand (struct mips_arg_info *arg,
5301 const struct mips_operand *operand_base)
5302 {
5303 const struct mips_msb_operand *operand;
5304 int min_val, max_val, max_high;
5305 offsetT size, sval, high;
5306
5307 operand = (const struct mips_msb_operand *) operand_base;
5308 min_val = operand->bias;
5309 max_val = min_val + (1 << operand_base->size) - 1;
5310 max_high = operand->opsize;
5311
5312 if (!match_const_int (arg, &size))
5313 return FALSE;
5314
5315 high = size + arg->last_op_int;
5316 sval = operand->add_lsb ? high : size;
5317
5318 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5319 {
5320 match_out_of_range (arg);
5321 return FALSE;
5322 }
5323 insn_insert_operand (arg->insn, operand_base, sval - min_val);
5324 return TRUE;
5325 }
5326
5327 /* OP_REG matcher. */
5328
5329 static bfd_boolean
5330 match_reg_operand (struct mips_arg_info *arg,
5331 const struct mips_operand *operand_base)
5332 {
5333 const struct mips_reg_operand *operand;
5334 unsigned int regno, uval, num_vals;
5335
5336 operand = (const struct mips_reg_operand *) operand_base;
5337 if (!match_reg (arg, operand->reg_type, &regno))
5338 return FALSE;
5339
5340 if (operand->reg_map)
5341 {
5342 num_vals = 1 << operand->root.size;
5343 for (uval = 0; uval < num_vals; uval++)
5344 if (operand->reg_map[uval] == regno)
5345 break;
5346 if (num_vals == uval)
5347 return FALSE;
5348 }
5349 else
5350 uval = regno;
5351
5352 arg->last_regno = regno;
5353 if (arg->opnum == 1)
5354 arg->dest_regno = regno;
5355 insn_insert_operand (arg->insn, operand_base, uval);
5356 return TRUE;
5357 }
5358
5359 /* OP_REG_PAIR matcher. */
5360
5361 static bfd_boolean
5362 match_reg_pair_operand (struct mips_arg_info *arg,
5363 const struct mips_operand *operand_base)
5364 {
5365 const struct mips_reg_pair_operand *operand;
5366 unsigned int regno1, regno2, uval, num_vals;
5367
5368 operand = (const struct mips_reg_pair_operand *) operand_base;
5369 if (!match_reg (arg, operand->reg_type, &regno1)
5370 || !match_char (arg, ',')
5371 || !match_reg (arg, operand->reg_type, &regno2))
5372 return FALSE;
5373
5374 num_vals = 1 << operand_base->size;
5375 for (uval = 0; uval < num_vals; uval++)
5376 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5377 break;
5378 if (uval == num_vals)
5379 return FALSE;
5380
5381 insn_insert_operand (arg->insn, operand_base, uval);
5382 return TRUE;
5383 }
5384
5385 /* OP_PCREL matcher. The caller chooses the relocation type. */
5386
5387 static bfd_boolean
5388 match_pcrel_operand (struct mips_arg_info *arg)
5389 {
5390 bfd_reloc_code_real_type r[3];
5391
5392 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
5393 }
5394
5395 /* OP_PERF_REG matcher. */
5396
5397 static bfd_boolean
5398 match_perf_reg_operand (struct mips_arg_info *arg,
5399 const struct mips_operand *operand)
5400 {
5401 offsetT sval;
5402
5403 if (!match_const_int (arg, &sval))
5404 return FALSE;
5405
5406 if (sval != 0
5407 && (sval != 1
5408 || (mips_opts.arch == CPU_R5900
5409 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5410 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5411 {
5412 set_insn_error (arg->argnum, _("invalid performance register"));
5413 return FALSE;
5414 }
5415
5416 insn_insert_operand (arg->insn, operand, sval);
5417 return TRUE;
5418 }
5419
5420 /* OP_ADDIUSP matcher. */
5421
5422 static bfd_boolean
5423 match_addiusp_operand (struct mips_arg_info *arg,
5424 const struct mips_operand *operand)
5425 {
5426 offsetT sval;
5427 unsigned int uval;
5428
5429 if (!match_const_int (arg, &sval))
5430 return FALSE;
5431
5432 if (sval % 4)
5433 {
5434 match_out_of_range (arg);
5435 return FALSE;
5436 }
5437
5438 sval /= 4;
5439 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
5440 {
5441 match_out_of_range (arg);
5442 return FALSE;
5443 }
5444
5445 uval = (unsigned int) sval;
5446 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5447 insn_insert_operand (arg->insn, operand, uval);
5448 return TRUE;
5449 }
5450
5451 /* OP_CLO_CLZ_DEST matcher. */
5452
5453 static bfd_boolean
5454 match_clo_clz_dest_operand (struct mips_arg_info *arg,
5455 const struct mips_operand *operand)
5456 {
5457 unsigned int regno;
5458
5459 if (!match_reg (arg, OP_REG_GP, &regno))
5460 return FALSE;
5461
5462 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5463 return TRUE;
5464 }
5465
5466 /* OP_CHECK_PREV matcher. */
5467
5468 static bfd_boolean
5469 match_check_prev_operand (struct mips_arg_info *arg,
5470 const struct mips_operand *operand_base)
5471 {
5472 const struct mips_check_prev_operand *operand;
5473 unsigned int regno;
5474
5475 operand = (const struct mips_check_prev_operand *) operand_base;
5476
5477 if (!match_reg (arg, OP_REG_GP, &regno))
5478 return FALSE;
5479
5480 if (!operand->zero_ok && regno == 0)
5481 return FALSE;
5482
5483 if ((operand->less_than_ok && regno < arg->last_regno)
5484 || (operand->greater_than_ok && regno > arg->last_regno)
5485 || (operand->equal_ok && regno == arg->last_regno))
5486 {
5487 arg->last_regno = regno;
5488 insn_insert_operand (arg->insn, operand_base, regno);
5489 return TRUE;
5490 }
5491
5492 return FALSE;
5493 }
5494
5495 /* OP_SAME_RS_RT matcher. */
5496
5497 static bfd_boolean
5498 match_same_rs_rt_operand (struct mips_arg_info *arg,
5499 const struct mips_operand *operand)
5500 {
5501 unsigned int regno;
5502
5503 if (!match_reg (arg, OP_REG_GP, &regno))
5504 return FALSE;
5505
5506 if (regno == 0)
5507 {
5508 set_insn_error (arg->argnum, _("the source register must not be $0"));
5509 return FALSE;
5510 }
5511
5512 arg->last_regno = regno;
5513
5514 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5515 return TRUE;
5516 }
5517
5518 /* OP_LWM_SWM_LIST matcher. */
5519
5520 static bfd_boolean
5521 match_lwm_swm_list_operand (struct mips_arg_info *arg,
5522 const struct mips_operand *operand)
5523 {
5524 unsigned int reglist, sregs, ra, regno1, regno2;
5525 struct mips_arg_info reset;
5526
5527 reglist = 0;
5528 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5529 return FALSE;
5530 do
5531 {
5532 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5533 {
5534 reglist |= 1 << FP;
5535 regno2 = S7;
5536 }
5537 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5538 reset = *arg;
5539 }
5540 while (match_char (arg, ',')
5541 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5542 *arg = reset;
5543
5544 if (operand->size == 2)
5545 {
5546 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5547
5548 s0, ra
5549 s0, s1, ra, s2, s3
5550 s0-s2, ra
5551
5552 and any permutations of these. */
5553 if ((reglist & 0xfff1ffff) != 0x80010000)
5554 return FALSE;
5555
5556 sregs = (reglist >> 17) & 7;
5557 ra = 0;
5558 }
5559 else
5560 {
5561 /* The list must include at least one of ra and s0-sN,
5562 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5563 which are $23 and $30 respectively.) E.g.:
5564
5565 ra
5566 s0
5567 ra, s0, s1, s2
5568 s0-s8
5569 s0-s5, ra
5570
5571 and any permutations of these. */
5572 if ((reglist & 0x3f00ffff) != 0)
5573 return FALSE;
5574
5575 ra = (reglist >> 27) & 0x10;
5576 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5577 }
5578 sregs += 1;
5579 if ((sregs & -sregs) != sregs)
5580 return FALSE;
5581
5582 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5583 return TRUE;
5584 }
5585
5586 /* OP_ENTRY_EXIT_LIST matcher. */
5587
5588 static unsigned int
5589 match_entry_exit_operand (struct mips_arg_info *arg,
5590 const struct mips_operand *operand)
5591 {
5592 unsigned int mask;
5593 bfd_boolean is_exit;
5594
5595 /* The format is the same for both ENTRY and EXIT, but the constraints
5596 are different. */
5597 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5598 mask = (is_exit ? 7 << 3 : 0);
5599 do
5600 {
5601 unsigned int regno1, regno2;
5602 bfd_boolean is_freg;
5603
5604 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5605 is_freg = FALSE;
5606 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5607 is_freg = TRUE;
5608 else
5609 return FALSE;
5610
5611 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5612 {
5613 mask &= ~(7 << 3);
5614 mask |= (5 + regno2) << 3;
5615 }
5616 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5617 mask |= (regno2 - 3) << 3;
5618 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5619 mask |= (regno2 - 15) << 1;
5620 else if (regno1 == RA && regno2 == RA)
5621 mask |= 1;
5622 else
5623 return FALSE;
5624 }
5625 while (match_char (arg, ','));
5626
5627 insn_insert_operand (arg->insn, operand, mask);
5628 return TRUE;
5629 }
5630
5631 /* Encode regular MIPS SAVE/RESTORE instruction operands according to
5632 the argument register mask AMASK, the number of static registers
5633 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5634 respectively, and the frame size FRAME_SIZE. */
5635
5636 static unsigned int
5637 mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5638 unsigned int ra, unsigned int s0, unsigned int s1,
5639 unsigned int frame_size)
5640 {
5641 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5642 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5643 }
5644
5645 /* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5646 argument register mask AMASK, the number of static registers saved
5647 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5648 respectively, and the frame size FRAME_SIZE. */
5649
5650 static unsigned int
5651 mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5652 unsigned int ra, unsigned int s0, unsigned int s1,
5653 unsigned int frame_size)
5654 {
5655 unsigned int args;
5656
5657 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5658 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5659 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5660 | ((frame_size & 0xf0) << 16));
5661 return args;
5662 }
5663
5664 /* OP_SAVE_RESTORE_LIST matcher. */
5665
5666 static bfd_boolean
5667 match_save_restore_list_operand (struct mips_arg_info *arg)
5668 {
5669 unsigned int opcode, args, statics, sregs;
5670 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
5671 unsigned int arg_mask, ra, s0, s1;
5672 offsetT frame_size;
5673
5674 opcode = arg->insn->insn_opcode;
5675 frame_size = 0;
5676 num_frame_sizes = 0;
5677 args = 0;
5678 statics = 0;
5679 sregs = 0;
5680 ra = 0;
5681 s0 = 0;
5682 s1 = 0;
5683 do
5684 {
5685 unsigned int regno1, regno2;
5686
5687 if (arg->token->type == OT_INTEGER)
5688 {
5689 /* Handle the frame size. */
5690 if (!match_const_int (arg, &frame_size))
5691 return FALSE;
5692 num_frame_sizes += 1;
5693 }
5694 else
5695 {
5696 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5697 return FALSE;
5698
5699 while (regno1 <= regno2)
5700 {
5701 if (regno1 >= 4 && regno1 <= 7)
5702 {
5703 if (num_frame_sizes == 0)
5704 /* args $a0-$a3 */
5705 args |= 1 << (regno1 - 4);
5706 else
5707 /* statics $a0-$a3 */
5708 statics |= 1 << (regno1 - 4);
5709 }
5710 else if (regno1 >= 16 && regno1 <= 23)
5711 /* $s0-$s7 */
5712 sregs |= 1 << (regno1 - 16);
5713 else if (regno1 == 30)
5714 /* $s8 */
5715 sregs |= 1 << 8;
5716 else if (regno1 == 31)
5717 /* Add $ra to insn. */
5718 ra = 1;
5719 else
5720 return FALSE;
5721 regno1 += 1;
5722 if (regno1 == 24)
5723 regno1 = 30;
5724 }
5725 }
5726 }
5727 while (match_char (arg, ','));
5728
5729 /* Encode args/statics combination. */
5730 if (args & statics)
5731 return FALSE;
5732 else if (args == 0xf)
5733 /* All $a0-$a3 are args. */
5734 arg_mask = MIPS_SVRS_ALL_ARGS;
5735 else if (statics == 0xf)
5736 /* All $a0-$a3 are statics. */
5737 arg_mask = MIPS_SVRS_ALL_STATICS;
5738 else
5739 {
5740 /* Count arg registers. */
5741 num_args = 0;
5742 while (args & 0x1)
5743 {
5744 args >>= 1;
5745 num_args += 1;
5746 }
5747 if (args != 0)
5748 return FALSE;
5749
5750 /* Count static registers. */
5751 num_statics = 0;
5752 while (statics & 0x8)
5753 {
5754 statics = (statics << 1) & 0xf;
5755 num_statics += 1;
5756 }
5757 if (statics != 0)
5758 return FALSE;
5759
5760 /* Encode args/statics. */
5761 arg_mask = (num_args << 2) | num_statics;
5762 }
5763
5764 /* Encode $s0/$s1. */
5765 if (sregs & (1 << 0)) /* $s0 */
5766 s0 = 1;
5767 if (sregs & (1 << 1)) /* $s1 */
5768 s1 = 1;
5769 sregs >>= 2;
5770
5771 /* Encode $s2-$s8. */
5772 num_sregs = 0;
5773 while (sregs & 1)
5774 {
5775 sregs >>= 1;
5776 num_sregs += 1;
5777 }
5778 if (sregs != 0)
5779 return FALSE;
5780
5781 /* Encode frame size. */
5782 if (num_frame_sizes == 0)
5783 {
5784 set_insn_error (arg->argnum, _("missing frame size"));
5785 return FALSE;
5786 }
5787 if (num_frame_sizes > 1)
5788 {
5789 set_insn_error (arg->argnum, _("frame size specified twice"));
5790 return FALSE;
5791 }
5792 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5793 {
5794 set_insn_error (arg->argnum, _("invalid frame size"));
5795 return FALSE;
5796 }
5797 frame_size /= 8;
5798
5799 /* Finally build the instruction. */
5800 if (mips_opts.mips16)
5801 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5802 frame_size);
5803 else if (!mips_opts.micromips)
5804 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5805 frame_size);
5806 else
5807 abort ();
5808
5809 arg->insn->insn_opcode = opcode;
5810 return TRUE;
5811 }
5812
5813 /* OP_MDMX_IMM_REG matcher. */
5814
5815 static bfd_boolean
5816 match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
5817 const struct mips_operand *operand)
5818 {
5819 unsigned int regno, uval;
5820 bfd_boolean is_qh;
5821 const struct mips_opcode *opcode;
5822
5823 /* The mips_opcode records whether this is an octobyte or quadhalf
5824 instruction. Start out with that bit in place. */
5825 opcode = arg->insn->insn_mo;
5826 uval = mips_extract_operand (operand, opcode->match);
5827 is_qh = (uval != 0);
5828
5829 if (arg->token->type == OT_REG)
5830 {
5831 if ((opcode->membership & INSN_5400)
5832 && strcmp (opcode->name, "rzu.ob") == 0)
5833 {
5834 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5835 arg->argnum);
5836 return FALSE;
5837 }
5838
5839 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5840 return FALSE;
5841 ++arg->token;
5842
5843 /* Check whether this is a vector register or a broadcast of
5844 a single element. */
5845 if (arg->token->type == OT_INTEGER_INDEX)
5846 {
5847 if (arg->token->u.index > (is_qh ? 3 : 7))
5848 {
5849 set_insn_error (arg->argnum, _("invalid element selector"));
5850 return FALSE;
5851 }
5852 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5853 ++arg->token;
5854 }
5855 else
5856 {
5857 /* A full vector. */
5858 if ((opcode->membership & INSN_5400)
5859 && (strcmp (opcode->name, "sll.ob") == 0
5860 || strcmp (opcode->name, "srl.ob") == 0))
5861 {
5862 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5863 arg->argnum);
5864 return FALSE;
5865 }
5866
5867 if (is_qh)
5868 uval |= MDMX_FMTSEL_VEC_QH << 5;
5869 else
5870 uval |= MDMX_FMTSEL_VEC_OB << 5;
5871 }
5872 uval |= regno;
5873 }
5874 else
5875 {
5876 offsetT sval;
5877
5878 if (!match_const_int (arg, &sval))
5879 return FALSE;
5880 if (sval < 0 || sval > 31)
5881 {
5882 match_out_of_range (arg);
5883 return FALSE;
5884 }
5885 uval |= (sval & 31);
5886 if (is_qh)
5887 uval |= MDMX_FMTSEL_IMM_QH << 5;
5888 else
5889 uval |= MDMX_FMTSEL_IMM_OB << 5;
5890 }
5891 insn_insert_operand (arg->insn, operand, uval);
5892 return TRUE;
5893 }
5894
5895 /* OP_IMM_INDEX matcher. */
5896
5897 static bfd_boolean
5898 match_imm_index_operand (struct mips_arg_info *arg,
5899 const struct mips_operand *operand)
5900 {
5901 unsigned int max_val;
5902
5903 if (arg->token->type != OT_INTEGER_INDEX)
5904 return FALSE;
5905
5906 max_val = (1 << operand->size) - 1;
5907 if (arg->token->u.index > max_val)
5908 {
5909 match_out_of_range (arg);
5910 return FALSE;
5911 }
5912 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5913 ++arg->token;
5914 return TRUE;
5915 }
5916
5917 /* OP_REG_INDEX matcher. */
5918
5919 static bfd_boolean
5920 match_reg_index_operand (struct mips_arg_info *arg,
5921 const struct mips_operand *operand)
5922 {
5923 unsigned int regno;
5924
5925 if (arg->token->type != OT_REG_INDEX)
5926 return FALSE;
5927
5928 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5929 return FALSE;
5930
5931 insn_insert_operand (arg->insn, operand, regno);
5932 ++arg->token;
5933 return TRUE;
5934 }
5935
5936 /* OP_PC matcher. */
5937
5938 static bfd_boolean
5939 match_pc_operand (struct mips_arg_info *arg)
5940 {
5941 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5942 {
5943 ++arg->token;
5944 return TRUE;
5945 }
5946 return FALSE;
5947 }
5948
5949 /* OP_REG28 matcher. */
5950
5951 static bfd_boolean
5952 match_reg28_operand (struct mips_arg_info *arg)
5953 {
5954 unsigned int regno;
5955
5956 if (arg->token->type == OT_REG
5957 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5958 && regno == GP)
5959 {
5960 ++arg->token;
5961 return TRUE;
5962 }
5963 return FALSE;
5964 }
5965
5966 /* OP_NON_ZERO_REG matcher. */
5967
5968 static bfd_boolean
5969 match_non_zero_reg_operand (struct mips_arg_info *arg,
5970 const struct mips_operand *operand)
5971 {
5972 unsigned int regno;
5973
5974 if (!match_reg (arg, OP_REG_GP, &regno))
5975 return FALSE;
5976
5977 if (regno == 0)
5978 return FALSE;
5979
5980 arg->last_regno = regno;
5981 insn_insert_operand (arg->insn, operand, regno);
5982 return TRUE;
5983 }
5984
5985 /* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5986 register that we need to match. */
5987
5988 static bfd_boolean
5989 match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
5990 {
5991 unsigned int regno;
5992
5993 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
5994 }
5995
5996 /* Try to match a floating-point constant from ARG for LI.S or LI.D.
5997 LENGTH is the length of the value in bytes (4 for float, 8 for double)
5998 and USING_GPRS says whether the destination is a GPR rather than an FPR.
5999
6000 Return the constant in IMM and OFFSET as follows:
6001
6002 - If the constant should be loaded via memory, set IMM to O_absent and
6003 OFFSET to the memory address.
6004
6005 - Otherwise, if the constant should be loaded into two 32-bit registers,
6006 set IMM to the O_constant to load into the high register and OFFSET
6007 to the corresponding value for the low register.
6008
6009 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6010
6011 These constants only appear as the last operand in an instruction,
6012 and every instruction that accepts them in any variant accepts them
6013 in all variants. This means we don't have to worry about backing out
6014 any changes if the instruction does not match. We just match
6015 unconditionally and report an error if the constant is invalid. */
6016
6017 static bfd_boolean
6018 match_float_constant (struct mips_arg_info *arg, expressionS *imm,
6019 expressionS *offset, int length, bfd_boolean using_gprs)
6020 {
6021 char *p;
6022 segT seg, new_seg;
6023 subsegT subseg;
6024 const char *newname;
6025 unsigned char *data;
6026
6027 /* Where the constant is placed is based on how the MIPS assembler
6028 does things:
6029
6030 length == 4 && using_gprs -- immediate value only
6031 length == 8 && using_gprs -- .rdata or immediate value
6032 length == 4 && !using_gprs -- .lit4 or immediate value
6033 length == 8 && !using_gprs -- .lit8 or immediate value
6034
6035 The .lit4 and .lit8 sections are only used if permitted by the
6036 -G argument. */
6037 if (arg->token->type != OT_FLOAT)
6038 {
6039 set_insn_error (arg->argnum, _("floating-point expression required"));
6040 return FALSE;
6041 }
6042
6043 gas_assert (arg->token->u.flt.length == length);
6044 data = arg->token->u.flt.data;
6045 ++arg->token;
6046
6047 /* Handle 32-bit constants for which an immediate value is best. */
6048 if (length == 4
6049 && (using_gprs
6050 || g_switch_value < 4
6051 || (data[0] == 0 && data[1] == 0)
6052 || (data[2] == 0 && data[3] == 0)))
6053 {
6054 imm->X_op = O_constant;
6055 if (!target_big_endian)
6056 imm->X_add_number = bfd_getl32 (data);
6057 else
6058 imm->X_add_number = bfd_getb32 (data);
6059 offset->X_op = O_absent;
6060 return TRUE;
6061 }
6062
6063 /* Handle 64-bit constants for which an immediate value is best. */
6064 if (length == 8
6065 && !mips_disable_float_construction
6066 /* Constants can only be constructed in GPRs and copied to FPRs if the
6067 GPRs are at least as wide as the FPRs or MTHC1 is available.
6068 Unlike most tests for 32-bit floating-point registers this check
6069 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6070 permit 64-bit moves without MXHC1.
6071 Force the constant into memory otherwise. */
6072 && (using_gprs
6073 || GPR_SIZE == 64
6074 || ISA_HAS_MXHC1 (mips_opts.isa)
6075 || FPR_SIZE == 32)
6076 && ((data[0] == 0 && data[1] == 0)
6077 || (data[2] == 0 && data[3] == 0))
6078 && ((data[4] == 0 && data[5] == 0)
6079 || (data[6] == 0 && data[7] == 0)))
6080 {
6081 /* The value is simple enough to load with a couple of instructions.
6082 If using 32-bit registers, set IMM to the high order 32 bits and
6083 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6084 64 bit constant. */
6085 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
6086 {
6087 imm->X_op = O_constant;
6088 offset->X_op = O_constant;
6089 if (!target_big_endian)
6090 {
6091 imm->X_add_number = bfd_getl32 (data + 4);
6092 offset->X_add_number = bfd_getl32 (data);
6093 }
6094 else
6095 {
6096 imm->X_add_number = bfd_getb32 (data);
6097 offset->X_add_number = bfd_getb32 (data + 4);
6098 }
6099 if (offset->X_add_number == 0)
6100 offset->X_op = O_absent;
6101 }
6102 else
6103 {
6104 imm->X_op = O_constant;
6105 if (!target_big_endian)
6106 imm->X_add_number = bfd_getl64 (data);
6107 else
6108 imm->X_add_number = bfd_getb64 (data);
6109 offset->X_op = O_absent;
6110 }
6111 return TRUE;
6112 }
6113
6114 /* Switch to the right section. */
6115 seg = now_seg;
6116 subseg = now_subseg;
6117 if (length == 4)
6118 {
6119 gas_assert (!using_gprs && g_switch_value >= 4);
6120 newname = ".lit4";
6121 }
6122 else
6123 {
6124 if (using_gprs || g_switch_value < 8)
6125 newname = RDATA_SECTION_NAME;
6126 else
6127 newname = ".lit8";
6128 }
6129
6130 new_seg = subseg_new (newname, (subsegT) 0);
6131 bfd_set_section_flags (stdoutput, new_seg,
6132 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6133 frag_align (length == 4 ? 2 : 3, 0, 0);
6134 if (strncmp (TARGET_OS, "elf", 3) != 0)
6135 record_alignment (new_seg, 4);
6136 else
6137 record_alignment (new_seg, length == 4 ? 2 : 3);
6138 if (seg == now_seg)
6139 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
6140
6141 /* Set the argument to the current address in the section. */
6142 imm->X_op = O_absent;
6143 offset->X_op = O_symbol;
6144 offset->X_add_symbol = symbol_temp_new_now ();
6145 offset->X_add_number = 0;
6146
6147 /* Put the floating point number into the section. */
6148 p = frag_more (length);
6149 memcpy (p, data, length);
6150
6151 /* Switch back to the original section. */
6152 subseg_set (seg, subseg);
6153 return TRUE;
6154 }
6155
6156 /* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6157 them. */
6158
6159 static bfd_boolean
6160 match_vu0_suffix_operand (struct mips_arg_info *arg,
6161 const struct mips_operand *operand,
6162 bfd_boolean match_p)
6163 {
6164 unsigned int uval;
6165
6166 /* The operand can be an XYZW mask or a single 2-bit channel index
6167 (with X being 0). */
6168 gas_assert (operand->size == 2 || operand->size == 4);
6169
6170 /* The suffix can be omitted when it is already part of the opcode. */
6171 if (arg->token->type != OT_CHANNELS)
6172 return match_p;
6173
6174 uval = arg->token->u.channels;
6175 if (operand->size == 2)
6176 {
6177 /* Check that a single bit is set and convert it into a 2-bit index. */
6178 if ((uval & -uval) != uval)
6179 return FALSE;
6180 uval = 4 - ffs (uval);
6181 }
6182
6183 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6184 return FALSE;
6185
6186 ++arg->token;
6187 if (!match_p)
6188 insn_insert_operand (arg->insn, operand, uval);
6189 return TRUE;
6190 }
6191
6192 /* Try to match a token from ARG against OPERAND. Consume the token
6193 and return true on success, otherwise return false. */
6194
6195 static bfd_boolean
6196 match_operand (struct mips_arg_info *arg,
6197 const struct mips_operand *operand)
6198 {
6199 switch (operand->type)
6200 {
6201 case OP_INT:
6202 return match_int_operand (arg, operand);
6203
6204 case OP_MAPPED_INT:
6205 return match_mapped_int_operand (arg, operand);
6206
6207 case OP_MSB:
6208 return match_msb_operand (arg, operand);
6209
6210 case OP_REG:
6211 case OP_OPTIONAL_REG:
6212 return match_reg_operand (arg, operand);
6213
6214 case OP_REG_PAIR:
6215 return match_reg_pair_operand (arg, operand);
6216
6217 case OP_PCREL:
6218 return match_pcrel_operand (arg);
6219
6220 case OP_PERF_REG:
6221 return match_perf_reg_operand (arg, operand);
6222
6223 case OP_ADDIUSP_INT:
6224 return match_addiusp_operand (arg, operand);
6225
6226 case OP_CLO_CLZ_DEST:
6227 return match_clo_clz_dest_operand (arg, operand);
6228
6229 case OP_LWM_SWM_LIST:
6230 return match_lwm_swm_list_operand (arg, operand);
6231
6232 case OP_ENTRY_EXIT_LIST:
6233 return match_entry_exit_operand (arg, operand);
6234
6235 case OP_SAVE_RESTORE_LIST:
6236 return match_save_restore_list_operand (arg);
6237
6238 case OP_MDMX_IMM_REG:
6239 return match_mdmx_imm_reg_operand (arg, operand);
6240
6241 case OP_REPEAT_DEST_REG:
6242 return match_tied_reg_operand (arg, arg->dest_regno);
6243
6244 case OP_REPEAT_PREV_REG:
6245 return match_tied_reg_operand (arg, arg->last_regno);
6246
6247 case OP_PC:
6248 return match_pc_operand (arg);
6249
6250 case OP_REG28:
6251 return match_reg28_operand (arg);
6252
6253 case OP_VU0_SUFFIX:
6254 return match_vu0_suffix_operand (arg, operand, FALSE);
6255
6256 case OP_VU0_MATCH_SUFFIX:
6257 return match_vu0_suffix_operand (arg, operand, TRUE);
6258
6259 case OP_IMM_INDEX:
6260 return match_imm_index_operand (arg, operand);
6261
6262 case OP_REG_INDEX:
6263 return match_reg_index_operand (arg, operand);
6264
6265 case OP_SAME_RS_RT:
6266 return match_same_rs_rt_operand (arg, operand);
6267
6268 case OP_CHECK_PREV:
6269 return match_check_prev_operand (arg, operand);
6270
6271 case OP_NON_ZERO_REG:
6272 return match_non_zero_reg_operand (arg, operand);
6273 }
6274 abort ();
6275 }
6276
6277 /* ARG is the state after successfully matching an instruction.
6278 Issue any queued-up warnings. */
6279
6280 static void
6281 check_completed_insn (struct mips_arg_info *arg)
6282 {
6283 if (arg->seen_at)
6284 {
6285 if (AT == ATREG)
6286 as_warn (_("used $at without \".set noat\""));
6287 else
6288 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
6289 }
6290 }
6291
6292 /* Return true if modifying general-purpose register REG needs a delay. */
6293
6294 static bfd_boolean
6295 reg_needs_delay (unsigned int reg)
6296 {
6297 unsigned long prev_pinfo;
6298
6299 prev_pinfo = history[0].insn_mo->pinfo;
6300 if (!mips_opts.noreorder
6301 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
6302 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
6303 && (gpr_write_mask (&history[0]) & (1 << reg)))
6304 return TRUE;
6305
6306 return FALSE;
6307 }
6308
6309 /* Classify an instruction according to the FIX_VR4120_* enumeration.
6310 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6311 by VR4120 errata. */
6312
6313 static unsigned int
6314 classify_vr4120_insn (const char *name)
6315 {
6316 if (strncmp (name, "macc", 4) == 0)
6317 return FIX_VR4120_MACC;
6318 if (strncmp (name, "dmacc", 5) == 0)
6319 return FIX_VR4120_DMACC;
6320 if (strncmp (name, "mult", 4) == 0)
6321 return FIX_VR4120_MULT;
6322 if (strncmp (name, "dmult", 5) == 0)
6323 return FIX_VR4120_DMULT;
6324 if (strstr (name, "div"))
6325 return FIX_VR4120_DIV;
6326 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6327 return FIX_VR4120_MTHILO;
6328 return NUM_FIX_VR4120_CLASSES;
6329 }
6330
6331 #define INSN_ERET 0x42000018
6332 #define INSN_DERET 0x4200001f
6333 #define INSN_DMULT 0x1c
6334 #define INSN_DMULTU 0x1d
6335
6336 /* Return the number of instructions that must separate INSN1 and INSN2,
6337 where INSN1 is the earlier instruction. Return the worst-case value
6338 for any INSN2 if INSN2 is null. */
6339
6340 static unsigned int
6341 insns_between (const struct mips_cl_insn *insn1,
6342 const struct mips_cl_insn *insn2)
6343 {
6344 unsigned long pinfo1, pinfo2;
6345 unsigned int mask;
6346
6347 /* If INFO2 is null, pessimistically assume that all flags are set for
6348 the second instruction. */
6349 pinfo1 = insn1->insn_mo->pinfo;
6350 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
6351
6352 /* For most targets, write-after-read dependencies on the HI and LO
6353 registers must be separated by at least two instructions. */
6354 if (!hilo_interlocks)
6355 {
6356 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6357 return 2;
6358 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6359 return 2;
6360 }
6361
6362 /* If we're working around r7000 errata, there must be two instructions
6363 between an mfhi or mflo and any instruction that uses the result. */
6364 if (mips_7000_hilo_fix
6365 && !mips_opts.micromips
6366 && MF_HILO_INSN (pinfo1)
6367 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
6368 return 2;
6369
6370 /* If we're working around 24K errata, one instruction is required
6371 if an ERET or DERET is followed by a branch instruction. */
6372 if (mips_fix_24k && !mips_opts.micromips)
6373 {
6374 if (insn1->insn_opcode == INSN_ERET
6375 || insn1->insn_opcode == INSN_DERET)
6376 {
6377 if (insn2 == NULL
6378 || insn2->insn_opcode == INSN_ERET
6379 || insn2->insn_opcode == INSN_DERET
6380 || delayed_branch_p (insn2))
6381 return 1;
6382 }
6383 }
6384
6385 /* If we're working around PMC RM7000 errata, there must be three
6386 nops between a dmult and a load instruction. */
6387 if (mips_fix_rm7000 && !mips_opts.micromips)
6388 {
6389 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6390 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6391 {
6392 if (pinfo2 & INSN_LOAD_MEMORY)
6393 return 3;
6394 }
6395 }
6396
6397 /* If working around VR4120 errata, check for combinations that need
6398 a single intervening instruction. */
6399 if (mips_fix_vr4120 && !mips_opts.micromips)
6400 {
6401 unsigned int class1, class2;
6402
6403 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6404 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
6405 {
6406 if (insn2 == NULL)
6407 return 1;
6408 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6409 if (vr4120_conflicts[class1] & (1 << class2))
6410 return 1;
6411 }
6412 }
6413
6414 if (!HAVE_CODE_COMPRESSION)
6415 {
6416 /* Check for GPR or coprocessor load delays. All such delays
6417 are on the RT register. */
6418 /* Itbl support may require additional care here. */
6419 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
6420 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
6421 {
6422 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
6423 return 1;
6424 }
6425
6426 /* Check for generic coprocessor hazards.
6427
6428 This case is not handled very well. There is no special
6429 knowledge of CP0 handling, and the coprocessors other than
6430 the floating point unit are not distinguished at all. */
6431 /* Itbl support may require additional care here. FIXME!
6432 Need to modify this to include knowledge about
6433 user specified delays! */
6434 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
6435 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6436 {
6437 /* Handle cases where INSN1 writes to a known general coprocessor
6438 register. There must be a one instruction delay before INSN2
6439 if INSN2 reads that register, otherwise no delay is needed. */
6440 mask = fpr_write_mask (insn1);
6441 if (mask != 0)
6442 {
6443 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
6444 return 1;
6445 }
6446 else
6447 {
6448 /* Read-after-write dependencies on the control registers
6449 require a two-instruction gap. */
6450 if ((pinfo1 & INSN_WRITE_COND_CODE)
6451 && (pinfo2 & INSN_READ_COND_CODE))
6452 return 2;
6453
6454 /* We don't know exactly what INSN1 does. If INSN2 is
6455 also a coprocessor instruction, assume there must be
6456 a one instruction gap. */
6457 if (pinfo2 & INSN_COP)
6458 return 1;
6459 }
6460 }
6461
6462 /* Check for read-after-write dependencies on the coprocessor
6463 control registers in cases where INSN1 does not need a general
6464 coprocessor delay. This means that INSN1 is a floating point
6465 comparison instruction. */
6466 /* Itbl support may require additional care here. */
6467 else if (!cop_interlocks
6468 && (pinfo1 & INSN_WRITE_COND_CODE)
6469 && (pinfo2 & INSN_READ_COND_CODE))
6470 return 1;
6471 }
6472
6473 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6474 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6475 and pause. */
6476 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6477 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6478 || (insn2 && delayed_branch_p (insn2))))
6479 return 1;
6480
6481 return 0;
6482 }
6483
6484 /* Return the number of nops that would be needed to work around the
6485 VR4130 mflo/mfhi errata if instruction INSN immediately followed
6486 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6487 that are contained within the first IGNORE instructions of HIST. */
6488
6489 static int
6490 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
6491 const struct mips_cl_insn *insn)
6492 {
6493 int i, j;
6494 unsigned int mask;
6495
6496 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6497 are not affected by the errata. */
6498 if (insn != 0
6499 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6500 || strcmp (insn->insn_mo->name, "mtlo") == 0
6501 || strcmp (insn->insn_mo->name, "mthi") == 0))
6502 return 0;
6503
6504 /* Search for the first MFLO or MFHI. */
6505 for (i = 0; i < MAX_VR4130_NOPS; i++)
6506 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
6507 {
6508 /* Extract the destination register. */
6509 mask = gpr_write_mask (&hist[i]);
6510
6511 /* No nops are needed if INSN reads that register. */
6512 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
6513 return 0;
6514
6515 /* ...or if any of the intervening instructions do. */
6516 for (j = 0; j < i; j++)
6517 if (gpr_read_mask (&hist[j]) & mask)
6518 return 0;
6519
6520 if (i >= ignore)
6521 return MAX_VR4130_NOPS - i;
6522 }
6523 return 0;
6524 }
6525
6526 #define BASE_REG_EQ(INSN1, INSN2) \
6527 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
6528 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6529
6530 /* Return the minimum alignment for this store instruction. */
6531
6532 static int
6533 fix_24k_align_to (const struct mips_opcode *mo)
6534 {
6535 if (strcmp (mo->name, "sh") == 0)
6536 return 2;
6537
6538 if (strcmp (mo->name, "swc1") == 0
6539 || strcmp (mo->name, "swc2") == 0
6540 || strcmp (mo->name, "sw") == 0
6541 || strcmp (mo->name, "sc") == 0
6542 || strcmp (mo->name, "s.s") == 0)
6543 return 4;
6544
6545 if (strcmp (mo->name, "sdc1") == 0
6546 || strcmp (mo->name, "sdc2") == 0
6547 || strcmp (mo->name, "s.d") == 0)
6548 return 8;
6549
6550 /* sb, swl, swr */
6551 return 1;
6552 }
6553
6554 struct fix_24k_store_info
6555 {
6556 /* Immediate offset, if any, for this store instruction. */
6557 short off;
6558 /* Alignment required by this store instruction. */
6559 int align_to;
6560 /* True for register offsets. */
6561 int register_offset;
6562 };
6563
6564 /* Comparison function used by qsort. */
6565
6566 static int
6567 fix_24k_sort (const void *a, const void *b)
6568 {
6569 const struct fix_24k_store_info *pos1 = a;
6570 const struct fix_24k_store_info *pos2 = b;
6571
6572 return (pos1->off - pos2->off);
6573 }
6574
6575 /* INSN is a store instruction. Try to record the store information
6576 in STINFO. Return false if the information isn't known. */
6577
6578 static bfd_boolean
6579 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
6580 const struct mips_cl_insn *insn)
6581 {
6582 /* The instruction must have a known offset. */
6583 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6584 return FALSE;
6585
6586 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6587 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6588 return TRUE;
6589 }
6590
6591 /* Return the number of nops that would be needed to work around the 24k
6592 "lost data on stores during refill" errata if instruction INSN
6593 immediately followed the 2 instructions described by HIST.
6594 Ignore hazards that are contained within the first IGNORE
6595 instructions of HIST.
6596
6597 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6598 for the data cache refills and store data. The following describes
6599 the scenario where the store data could be lost.
6600
6601 * A data cache miss, due to either a load or a store, causing fill
6602 data to be supplied by the memory subsystem
6603 * The first three doublewords of fill data are returned and written
6604 into the cache
6605 * A sequence of four stores occurs in consecutive cycles around the
6606 final doubleword of the fill:
6607 * Store A
6608 * Store B
6609 * Store C
6610 * Zero, One or more instructions
6611 * Store D
6612
6613 The four stores A-D must be to different doublewords of the line that
6614 is being filled. The fourth instruction in the sequence above permits
6615 the fill of the final doubleword to be transferred from the FSB into
6616 the cache. In the sequence above, the stores may be either integer
6617 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6618 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6619 different doublewords on the line. If the floating point unit is
6620 running in 1:2 mode, it is not possible to create the sequence above
6621 using only floating point store instructions.
6622
6623 In this case, the cache line being filled is incorrectly marked
6624 invalid, thereby losing the data from any store to the line that
6625 occurs between the original miss and the completion of the five
6626 cycle sequence shown above.
6627
6628 The workarounds are:
6629
6630 * Run the data cache in write-through mode.
6631 * Insert a non-store instruction between
6632 Store A and Store B or Store B and Store C. */
6633
6634 static int
6635 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
6636 const struct mips_cl_insn *insn)
6637 {
6638 struct fix_24k_store_info pos[3];
6639 int align, i, base_offset;
6640
6641 if (ignore >= 2)
6642 return 0;
6643
6644 /* If the previous instruction wasn't a store, there's nothing to
6645 worry about. */
6646 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6647 return 0;
6648
6649 /* If the instructions after the previous one are unknown, we have
6650 to assume the worst. */
6651 if (!insn)
6652 return 1;
6653
6654 /* Check whether we are dealing with three consecutive stores. */
6655 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6656 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6657 return 0;
6658
6659 /* If we don't know the relationship between the store addresses,
6660 assume the worst. */
6661 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
6662 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6663 return 1;
6664
6665 if (!fix_24k_record_store_info (&pos[0], insn)
6666 || !fix_24k_record_store_info (&pos[1], &hist[0])
6667 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6668 return 1;
6669
6670 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6671
6672 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6673 X bytes and such that the base register + X is known to be aligned
6674 to align bytes. */
6675
6676 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6677 align = 8;
6678 else
6679 {
6680 align = pos[0].align_to;
6681 base_offset = pos[0].off;
6682 for (i = 1; i < 3; i++)
6683 if (align < pos[i].align_to)
6684 {
6685 align = pos[i].align_to;
6686 base_offset = pos[i].off;
6687 }
6688 for (i = 0; i < 3; i++)
6689 pos[i].off -= base_offset;
6690 }
6691
6692 pos[0].off &= ~align + 1;
6693 pos[1].off &= ~align + 1;
6694 pos[2].off &= ~align + 1;
6695
6696 /* If any two stores write to the same chunk, they also write to the
6697 same doubleword. The offsets are still sorted at this point. */
6698 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6699 return 0;
6700
6701 /* A range of at least 9 bytes is needed for the stores to be in
6702 non-overlapping doublewords. */
6703 if (pos[2].off - pos[0].off <= 8)
6704 return 0;
6705
6706 if (pos[2].off - pos[1].off >= 24
6707 || pos[1].off - pos[0].off >= 24
6708 || pos[2].off - pos[0].off >= 32)
6709 return 0;
6710
6711 return 1;
6712 }
6713
6714 /* Return the number of nops that would be needed if instruction INSN
6715 immediately followed the MAX_NOPS instructions given by HIST,
6716 where HIST[0] is the most recent instruction. Ignore hazards
6717 between INSN and the first IGNORE instructions in HIST.
6718
6719 If INSN is null, return the worse-case number of nops for any
6720 instruction. */
6721
6722 static int
6723 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
6724 const struct mips_cl_insn *insn)
6725 {
6726 int i, nops, tmp_nops;
6727
6728 nops = 0;
6729 for (i = ignore; i < MAX_DELAY_NOPS; i++)
6730 {
6731 tmp_nops = insns_between (hist + i, insn) - i;
6732 if (tmp_nops > nops)
6733 nops = tmp_nops;
6734 }
6735
6736 if (mips_fix_vr4130 && !mips_opts.micromips)
6737 {
6738 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
6739 if (tmp_nops > nops)
6740 nops = tmp_nops;
6741 }
6742
6743 if (mips_fix_24k && !mips_opts.micromips)
6744 {
6745 tmp_nops = nops_for_24k (ignore, hist, insn);
6746 if (tmp_nops > nops)
6747 nops = tmp_nops;
6748 }
6749
6750 return nops;
6751 }
6752
6753 /* The variable arguments provide NUM_INSNS extra instructions that
6754 might be added to HIST. Return the largest number of nops that
6755 would be needed after the extended sequence, ignoring hazards
6756 in the first IGNORE instructions. */
6757
6758 static int
6759 nops_for_sequence (int num_insns, int ignore,
6760 const struct mips_cl_insn *hist, ...)
6761 {
6762 va_list args;
6763 struct mips_cl_insn buffer[MAX_NOPS];
6764 struct mips_cl_insn *cursor;
6765 int nops;
6766
6767 va_start (args, hist);
6768 cursor = buffer + num_insns;
6769 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
6770 while (cursor > buffer)
6771 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6772
6773 nops = nops_for_insn (ignore, buffer, NULL);
6774 va_end (args);
6775 return nops;
6776 }
6777
6778 /* Like nops_for_insn, but if INSN is a branch, take into account the
6779 worst-case delay for the branch target. */
6780
6781 static int
6782 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
6783 const struct mips_cl_insn *insn)
6784 {
6785 int nops, tmp_nops;
6786
6787 nops = nops_for_insn (ignore, hist, insn);
6788 if (delayed_branch_p (insn))
6789 {
6790 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
6791 hist, insn, get_delay_slot_nop (insn));
6792 if (tmp_nops > nops)
6793 nops = tmp_nops;
6794 }
6795 else if (compact_branch_p (insn))
6796 {
6797 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
6798 if (tmp_nops > nops)
6799 nops = tmp_nops;
6800 }
6801 return nops;
6802 }
6803
6804 /* Fix NOP issue: Replace nops by "or at,at,zero". */
6805
6806 static void
6807 fix_loongson2f_nop (struct mips_cl_insn * ip)
6808 {
6809 gas_assert (!HAVE_CODE_COMPRESSION);
6810 if (strcmp (ip->insn_mo->name, "nop") == 0)
6811 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6812 }
6813
6814 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6815 jr target pc &= 'hffff_ffff_cfff_ffff. */
6816
6817 static void
6818 fix_loongson2f_jump (struct mips_cl_insn * ip)
6819 {
6820 gas_assert (!HAVE_CODE_COMPRESSION);
6821 if (strcmp (ip->insn_mo->name, "j") == 0
6822 || strcmp (ip->insn_mo->name, "jr") == 0
6823 || strcmp (ip->insn_mo->name, "jalr") == 0)
6824 {
6825 int sreg;
6826 expressionS ep;
6827
6828 if (! mips_opts.at)
6829 return;
6830
6831 sreg = EXTRACT_OPERAND (0, RS, *ip);
6832 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6833 return;
6834
6835 ep.X_op = O_constant;
6836 ep.X_add_number = 0xcfff0000;
6837 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6838 ep.X_add_number = 0xffff;
6839 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6840 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6841 }
6842 }
6843
6844 static void
6845 fix_loongson2f (struct mips_cl_insn * ip)
6846 {
6847 if (mips_fix_loongson2f_nop)
6848 fix_loongson2f_nop (ip);
6849
6850 if (mips_fix_loongson2f_jump)
6851 fix_loongson2f_jump (ip);
6852 }
6853
6854 /* IP is a branch that has a delay slot, and we need to fill it
6855 automatically. Return true if we can do that by swapping IP
6856 with the previous instruction.
6857 ADDRESS_EXPR is an operand of the instruction to be used with
6858 RELOC_TYPE. */
6859
6860 static bfd_boolean
6861 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
6862 bfd_reloc_code_real_type *reloc_type)
6863 {
6864 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
6865 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
6866 unsigned int fpr_read, prev_fpr_write;
6867
6868 /* -O2 and above is required for this optimization. */
6869 if (mips_optimize < 2)
6870 return FALSE;
6871
6872 /* If we have seen .set volatile or .set nomove, don't optimize. */
6873 if (mips_opts.nomove)
6874 return FALSE;
6875
6876 /* We can't swap if the previous instruction's position is fixed. */
6877 if (history[0].fixed_p)
6878 return FALSE;
6879
6880 /* If the previous previous insn was in a .set noreorder, we can't
6881 swap. Actually, the MIPS assembler will swap in this situation.
6882 However, gcc configured -with-gnu-as will generate code like
6883
6884 .set noreorder
6885 lw $4,XXX
6886 .set reorder
6887 INSN
6888 bne $4,$0,foo
6889
6890 in which we can not swap the bne and INSN. If gcc is not configured
6891 -with-gnu-as, it does not output the .set pseudo-ops. */
6892 if (history[1].noreorder_p)
6893 return FALSE;
6894
6895 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6896 This means that the previous instruction was a 4-byte one anyhow. */
6897 if (mips_opts.mips16 && history[0].fixp[0])
6898 return FALSE;
6899
6900 /* If the branch is itself the target of a branch, we can not swap.
6901 We cheat on this; all we check for is whether there is a label on
6902 this instruction. If there are any branches to anything other than
6903 a label, users must use .set noreorder. */
6904 if (seg_info (now_seg)->label_list)
6905 return FALSE;
6906
6907 /* If the previous instruction is in a variant frag other than this
6908 branch's one, we cannot do the swap. This does not apply to
6909 MIPS16 code, which uses variant frags for different purposes. */
6910 if (!mips_opts.mips16
6911 && history[0].frag
6912 && history[0].frag->fr_type == rs_machine_dependent)
6913 return FALSE;
6914
6915 /* We do not swap with instructions that cannot architecturally
6916 be placed in a branch delay slot, such as SYNC or ERET. We
6917 also refrain from swapping with a trap instruction, since it
6918 complicates trap handlers to have the trap instruction be in
6919 a delay slot. */
6920 prev_pinfo = history[0].insn_mo->pinfo;
6921 if (prev_pinfo & INSN_NO_DELAY_SLOT)
6922 return FALSE;
6923
6924 /* Check for conflicts between the branch and the instructions
6925 before the candidate delay slot. */
6926 if (nops_for_insn (0, history + 1, ip) > 0)
6927 return FALSE;
6928
6929 /* Check for conflicts between the swapped sequence and the
6930 target of the branch. */
6931 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6932 return FALSE;
6933
6934 /* If the branch reads a register that the previous
6935 instruction sets, we can not swap. */
6936 gpr_read = gpr_read_mask (ip);
6937 prev_gpr_write = gpr_write_mask (&history[0]);
6938 if (gpr_read & prev_gpr_write)
6939 return FALSE;
6940
6941 fpr_read = fpr_read_mask (ip);
6942 prev_fpr_write = fpr_write_mask (&history[0]);
6943 if (fpr_read & prev_fpr_write)
6944 return FALSE;
6945
6946 /* If the branch writes a register that the previous
6947 instruction sets, we can not swap. */
6948 gpr_write = gpr_write_mask (ip);
6949 if (gpr_write & prev_gpr_write)
6950 return FALSE;
6951
6952 /* If the branch writes a register that the previous
6953 instruction reads, we can not swap. */
6954 prev_gpr_read = gpr_read_mask (&history[0]);
6955 if (gpr_write & prev_gpr_read)
6956 return FALSE;
6957
6958 /* If one instruction sets a condition code and the
6959 other one uses a condition code, we can not swap. */
6960 pinfo = ip->insn_mo->pinfo;
6961 if ((pinfo & INSN_READ_COND_CODE)
6962 && (prev_pinfo & INSN_WRITE_COND_CODE))
6963 return FALSE;
6964 if ((pinfo & INSN_WRITE_COND_CODE)
6965 && (prev_pinfo & INSN_READ_COND_CODE))
6966 return FALSE;
6967
6968 /* If the previous instruction uses the PC, we can not swap. */
6969 prev_pinfo2 = history[0].insn_mo->pinfo2;
6970 if (prev_pinfo2 & INSN2_READ_PC)
6971 return FALSE;
6972
6973 /* If the previous instruction has an incorrect size for a fixed
6974 branch delay slot in microMIPS mode, we cannot swap. */
6975 pinfo2 = ip->insn_mo->pinfo2;
6976 if (mips_opts.micromips
6977 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6978 && insn_length (history) != 2)
6979 return FALSE;
6980 if (mips_opts.micromips
6981 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6982 && insn_length (history) != 4)
6983 return FALSE;
6984
6985 /* On the R5900 short loops need to be fixed by inserting a NOP in the
6986 branch delay slot.
6987
6988 The short loop bug under certain conditions causes loops to execute
6989 only once or twice. We must ensure that the assembler never
6990 generates loops that satisfy all of the following conditions:
6991
6992 - a loop consists of less than or equal to six instructions
6993 (including the branch delay slot);
6994 - a loop contains only one conditional branch instruction at the end
6995 of the loop;
6996 - a loop does not contain any other branch or jump instructions;
6997 - a branch delay slot of the loop is not NOP (EE 2.9 or later).
6998
6999 We need to do this because of a hardware bug in the R5900 chip. */
7000 if (mips_opts.arch == CPU_R5900
7001 /* Check if instruction has a parameter, ignore "j $31". */
7002 && (address_expr != NULL)
7003 /* Parameter must be 16 bit. */
7004 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
7005 /* Branch to same segment. */
7006 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
7007 /* Branch to same code fragment. */
7008 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
7009 /* Can only calculate branch offset if value is known. */
7010 && symbol_constant_p (address_expr->X_add_symbol)
7011 /* Check if branch is really conditional. */
7012 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
7013 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
7014 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7015 {
7016 int distance;
7017 /* Check if loop is shorter than or equal to 6 instructions
7018 including branch and delay slot. */
7019 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
7020 if (distance <= 20)
7021 {
7022 int i;
7023 int rv;
7024
7025 rv = FALSE;
7026 /* When the loop includes branches or jumps,
7027 it is not a short loop. */
7028 for (i = 0; i < (distance / 4); i++)
7029 {
7030 if ((history[i].cleared_p)
7031 || delayed_branch_p (&history[i]))
7032 {
7033 rv = TRUE;
7034 break;
7035 }
7036 }
7037 if (!rv)
7038 {
7039 /* Insert nop after branch to fix short loop. */
7040 return FALSE;
7041 }
7042 }
7043 }
7044
7045 return TRUE;
7046 }
7047
7048 /* Decide how we should add IP to the instruction stream.
7049 ADDRESS_EXPR is an operand of the instruction to be used with
7050 RELOC_TYPE. */
7051
7052 static enum append_method
7053 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
7054 bfd_reloc_code_real_type *reloc_type)
7055 {
7056 /* The relaxed version of a macro sequence must be inherently
7057 hazard-free. */
7058 if (mips_relax.sequence == 2)
7059 return APPEND_ADD;
7060
7061 /* We must not dabble with instructions in a ".set noreorder" block. */
7062 if (mips_opts.noreorder)
7063 return APPEND_ADD;
7064
7065 /* Otherwise, it's our responsibility to fill branch delay slots. */
7066 if (delayed_branch_p (ip))
7067 {
7068 if (!branch_likely_p (ip)
7069 && can_swap_branch_p (ip, address_expr, reloc_type))
7070 return APPEND_SWAP;
7071
7072 if (mips_opts.mips16
7073 && ISA_SUPPORTS_MIPS16E
7074 && gpr_read_mask (ip) != 0)
7075 return APPEND_ADD_COMPACT;
7076
7077 if (mips_opts.micromips
7078 && ((ip->insn_opcode & 0xffe0) == 0x4580
7079 || (!forced_insn_length
7080 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7081 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7082 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7083 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7084 return APPEND_ADD_COMPACT;
7085
7086 return APPEND_ADD_WITH_NOP;
7087 }
7088
7089 return APPEND_ADD;
7090 }
7091
7092 /* IP is an instruction whose opcode we have just changed, END points
7093 to the end of the opcode table processed. Point IP->insn_mo to the
7094 new opcode's definition. */
7095
7096 static void
7097 find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
7098 {
7099 const struct mips_opcode *mo;
7100
7101 for (mo = ip->insn_mo; mo < end; mo++)
7102 if (mo->pinfo != INSN_MACRO
7103 && (ip->insn_opcode & mo->mask) == mo->match)
7104 {
7105 ip->insn_mo = mo;
7106 return;
7107 }
7108 abort ();
7109 }
7110
7111 /* IP is a MIPS16 instruction whose opcode we have just changed.
7112 Point IP->insn_mo to the new opcode's definition. */
7113
7114 static void
7115 find_altered_mips16_opcode (struct mips_cl_insn *ip)
7116 {
7117 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7118 }
7119
7120 /* IP is a microMIPS instruction whose opcode we have just changed.
7121 Point IP->insn_mo to the new opcode's definition. */
7122
7123 static void
7124 find_altered_micromips_opcode (struct mips_cl_insn *ip)
7125 {
7126 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7127 }
7128
7129 /* For microMIPS macros, we need to generate a local number label
7130 as the target of branches. */
7131 #define MICROMIPS_LABEL_CHAR '\037'
7132 static unsigned long micromips_target_label;
7133 static char micromips_target_name[32];
7134
7135 static char *
7136 micromips_label_name (void)
7137 {
7138 char *p = micromips_target_name;
7139 char symbol_name_temporary[24];
7140 unsigned long l;
7141 int i;
7142
7143 if (*p)
7144 return p;
7145
7146 i = 0;
7147 l = micromips_target_label;
7148 #ifdef LOCAL_LABEL_PREFIX
7149 *p++ = LOCAL_LABEL_PREFIX;
7150 #endif
7151 *p++ = 'L';
7152 *p++ = MICROMIPS_LABEL_CHAR;
7153 do
7154 {
7155 symbol_name_temporary[i++] = l % 10 + '0';
7156 l /= 10;
7157 }
7158 while (l != 0);
7159 while (i > 0)
7160 *p++ = symbol_name_temporary[--i];
7161 *p = '\0';
7162
7163 return micromips_target_name;
7164 }
7165
7166 static void
7167 micromips_label_expr (expressionS *label_expr)
7168 {
7169 label_expr->X_op = O_symbol;
7170 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7171 label_expr->X_add_number = 0;
7172 }
7173
7174 static void
7175 micromips_label_inc (void)
7176 {
7177 micromips_target_label++;
7178 *micromips_target_name = '\0';
7179 }
7180
7181 static void
7182 micromips_add_label (void)
7183 {
7184 symbolS *s;
7185
7186 s = colon (micromips_label_name ());
7187 micromips_label_inc ();
7188 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
7189 }
7190
7191 /* If assembling microMIPS code, then return the microMIPS reloc
7192 corresponding to the requested one if any. Otherwise return
7193 the reloc unchanged. */
7194
7195 static bfd_reloc_code_real_type
7196 micromips_map_reloc (bfd_reloc_code_real_type reloc)
7197 {
7198 static const bfd_reloc_code_real_type relocs[][2] =
7199 {
7200 /* Keep sorted incrementally by the left-hand key. */
7201 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7202 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7203 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7204 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7205 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7206 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7207 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7208 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7209 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7210 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7211 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7212 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7213 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7214 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7215 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7216 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7217 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7218 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7219 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7220 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7221 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7222 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7223 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7224 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7225 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7226 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7227 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7228 };
7229 bfd_reloc_code_real_type r;
7230 size_t i;
7231
7232 if (!mips_opts.micromips)
7233 return reloc;
7234 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7235 {
7236 r = relocs[i][0];
7237 if (r > reloc)
7238 return reloc;
7239 if (r == reloc)
7240 return relocs[i][1];
7241 }
7242 return reloc;
7243 }
7244
7245 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7246 Return true on success, storing the resolved value in RESULT. */
7247
7248 static bfd_boolean
7249 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7250 offsetT *result)
7251 {
7252 switch (reloc)
7253 {
7254 case BFD_RELOC_MIPS_HIGHEST:
7255 case BFD_RELOC_MICROMIPS_HIGHEST:
7256 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7257 return TRUE;
7258
7259 case BFD_RELOC_MIPS_HIGHER:
7260 case BFD_RELOC_MICROMIPS_HIGHER:
7261 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7262 return TRUE;
7263
7264 case BFD_RELOC_HI16_S:
7265 case BFD_RELOC_HI16_S_PCREL:
7266 case BFD_RELOC_MICROMIPS_HI16_S:
7267 case BFD_RELOC_MIPS16_HI16_S:
7268 *result = ((operand + 0x8000) >> 16) & 0xffff;
7269 return TRUE;
7270
7271 case BFD_RELOC_HI16:
7272 case BFD_RELOC_MICROMIPS_HI16:
7273 case BFD_RELOC_MIPS16_HI16:
7274 *result = (operand >> 16) & 0xffff;
7275 return TRUE;
7276
7277 case BFD_RELOC_LO16:
7278 case BFD_RELOC_LO16_PCREL:
7279 case BFD_RELOC_MICROMIPS_LO16:
7280 case BFD_RELOC_MIPS16_LO16:
7281 *result = operand & 0xffff;
7282 return TRUE;
7283
7284 case BFD_RELOC_UNUSED:
7285 *result = operand;
7286 return TRUE;
7287
7288 default:
7289 return FALSE;
7290 }
7291 }
7292
7293 /* Output an instruction. IP is the instruction information.
7294 ADDRESS_EXPR is an operand of the instruction to be used with
7295 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7296 a macro expansion. */
7297
7298 static void
7299 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
7300 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
7301 {
7302 unsigned long prev_pinfo2, pinfo;
7303 bfd_boolean relaxed_branch = FALSE;
7304 enum append_method method;
7305 bfd_boolean relax32;
7306 int branch_disp;
7307
7308 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
7309 fix_loongson2f (ip);
7310
7311 file_ase_mips16 |= mips_opts.mips16;
7312 file_ase_micromips |= mips_opts.micromips;
7313
7314 prev_pinfo2 = history[0].insn_mo->pinfo2;
7315 pinfo = ip->insn_mo->pinfo;
7316
7317 /* Don't raise alarm about `nods' frags as they'll fill in the right
7318 kind of nop in relaxation if required. */
7319 if (mips_opts.micromips
7320 && !expansionp
7321 && !(history[0].frag
7322 && history[0].frag->fr_type == rs_machine_dependent
7323 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7324 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
7325 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7326 && micromips_insn_length (ip->insn_mo) != 2)
7327 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7328 && micromips_insn_length (ip->insn_mo) != 4)))
7329 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
7330 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
7331
7332 if (address_expr == NULL)
7333 ip->complete_p = 1;
7334 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7335 && reloc_type[1] == BFD_RELOC_UNUSED
7336 && reloc_type[2] == BFD_RELOC_UNUSED
7337 && address_expr->X_op == O_constant)
7338 {
7339 switch (*reloc_type)
7340 {
7341 case BFD_RELOC_MIPS_JMP:
7342 {
7343 int shift;
7344
7345 /* Shift is 2, unusually, for microMIPS JALX. */
7346 shift = (mips_opts.micromips
7347 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
7348 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7349 as_bad (_("jump to misaligned address (0x%lx)"),
7350 (unsigned long) address_expr->X_add_number);
7351 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7352 & 0x3ffffff);
7353 ip->complete_p = 1;
7354 }
7355 break;
7356
7357 case BFD_RELOC_MIPS16_JMP:
7358 if ((address_expr->X_add_number & 3) != 0)
7359 as_bad (_("jump to misaligned address (0x%lx)"),
7360 (unsigned long) address_expr->X_add_number);
7361 ip->insn_opcode |=
7362 (((address_expr->X_add_number & 0x7c0000) << 3)
7363 | ((address_expr->X_add_number & 0xf800000) >> 7)
7364 | ((address_expr->X_add_number & 0x3fffc) >> 2));
7365 ip->complete_p = 1;
7366 break;
7367
7368 case BFD_RELOC_16_PCREL_S2:
7369 {
7370 int shift;
7371
7372 shift = mips_opts.micromips ? 1 : 2;
7373 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7374 as_bad (_("branch to misaligned address (0x%lx)"),
7375 (unsigned long) address_expr->X_add_number);
7376 if (!mips_relax_branch)
7377 {
7378 if ((address_expr->X_add_number + (1 << (shift + 15)))
7379 & ~((1 << (shift + 16)) - 1))
7380 as_bad (_("branch address range overflow (0x%lx)"),
7381 (unsigned long) address_expr->X_add_number);
7382 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7383 & 0xffff);
7384 }
7385 }
7386 break;
7387
7388 case BFD_RELOC_MIPS_21_PCREL_S2:
7389 {
7390 int shift;
7391
7392 shift = 2;
7393 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7394 as_bad (_("branch to misaligned address (0x%lx)"),
7395 (unsigned long) address_expr->X_add_number);
7396 if ((address_expr->X_add_number + (1 << (shift + 20)))
7397 & ~((1 << (shift + 21)) - 1))
7398 as_bad (_("branch address range overflow (0x%lx)"),
7399 (unsigned long) address_expr->X_add_number);
7400 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7401 & 0x1fffff);
7402 }
7403 break;
7404
7405 case BFD_RELOC_MIPS_26_PCREL_S2:
7406 {
7407 int shift;
7408
7409 shift = 2;
7410 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7411 as_bad (_("branch to misaligned address (0x%lx)"),
7412 (unsigned long) address_expr->X_add_number);
7413 if ((address_expr->X_add_number + (1 << (shift + 25)))
7414 & ~((1 << (shift + 26)) - 1))
7415 as_bad (_("branch address range overflow (0x%lx)"),
7416 (unsigned long) address_expr->X_add_number);
7417 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7418 & 0x3ffffff);
7419 }
7420 break;
7421
7422 default:
7423 {
7424 offsetT value;
7425
7426 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7427 &value))
7428 {
7429 ip->insn_opcode |= value & 0xffff;
7430 ip->complete_p = 1;
7431 }
7432 }
7433 break;
7434 }
7435 }
7436
7437 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7438 {
7439 /* There are a lot of optimizations we could do that we don't.
7440 In particular, we do not, in general, reorder instructions.
7441 If you use gcc with optimization, it will reorder
7442 instructions and generally do much more optimization then we
7443 do here; repeating all that work in the assembler would only
7444 benefit hand written assembly code, and does not seem worth
7445 it. */
7446 int nops = (mips_optimize == 0
7447 ? nops_for_insn (0, history, NULL)
7448 : nops_for_insn_or_target (0, history, ip));
7449 if (nops > 0)
7450 {
7451 fragS *old_frag;
7452 unsigned long old_frag_offset;
7453 int i;
7454
7455 old_frag = frag_now;
7456 old_frag_offset = frag_now_fix ();
7457
7458 for (i = 0; i < nops; i++)
7459 add_fixed_insn (NOP_INSN);
7460 insert_into_history (0, nops, NOP_INSN);
7461
7462 if (listing)
7463 {
7464 listing_prev_line ();
7465 /* We may be at the start of a variant frag. In case we
7466 are, make sure there is enough space for the frag
7467 after the frags created by listing_prev_line. The
7468 argument to frag_grow here must be at least as large
7469 as the argument to all other calls to frag_grow in
7470 this file. We don't have to worry about being in the
7471 middle of a variant frag, because the variants insert
7472 all needed nop instructions themselves. */
7473 frag_grow (40);
7474 }
7475
7476 mips_move_text_labels ();
7477
7478 #ifndef NO_ECOFF_DEBUGGING
7479 if (ECOFF_DEBUGGING)
7480 ecoff_fix_loc (old_frag, old_frag_offset);
7481 #endif
7482 }
7483 }
7484 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7485 {
7486 int nops;
7487
7488 /* Work out how many nops in prev_nop_frag are needed by IP,
7489 ignoring hazards generated by the first prev_nop_frag_since
7490 instructions. */
7491 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
7492 gas_assert (nops <= prev_nop_frag_holds);
7493
7494 /* Enforce NOPS as a minimum. */
7495 if (nops > prev_nop_frag_required)
7496 prev_nop_frag_required = nops;
7497
7498 if (prev_nop_frag_holds == prev_nop_frag_required)
7499 {
7500 /* Settle for the current number of nops. Update the history
7501 accordingly (for the benefit of any future .set reorder code). */
7502 prev_nop_frag = NULL;
7503 insert_into_history (prev_nop_frag_since,
7504 prev_nop_frag_holds, NOP_INSN);
7505 }
7506 else
7507 {
7508 /* Allow this instruction to replace one of the nops that was
7509 tentatively added to prev_nop_frag. */
7510 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
7511 prev_nop_frag_holds--;
7512 prev_nop_frag_since++;
7513 }
7514 }
7515
7516 method = get_append_method (ip, address_expr, reloc_type);
7517 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
7518
7519 dwarf2_emit_insn (0);
7520 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7521 so "move" the instruction address accordingly.
7522
7523 Also, it doesn't seem appropriate for the assembler to reorder .loc
7524 entries. If this instruction is a branch that we are going to swap
7525 with the previous instruction, the two instructions should be
7526 treated as a unit, and the debug information for both instructions
7527 should refer to the start of the branch sequence. Using the
7528 current position is certainly wrong when swapping a 32-bit branch
7529 and a 16-bit delay slot, since the current position would then be
7530 in the middle of a branch. */
7531 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
7532
7533 relax32 = (mips_relax_branch
7534 /* Don't try branch relaxation within .set nomacro, or within
7535 .set noat if we use $at for PIC computations. If it turns
7536 out that the branch was out-of-range, we'll get an error. */
7537 && !mips_opts.warn_about_macros
7538 && (mips_opts.at || mips_pic == NO_PIC)
7539 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7540 as they have no complementing branches. */
7541 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
7542
7543 if (!HAVE_CODE_COMPRESSION
7544 && address_expr
7545 && relax32
7546 && *reloc_type == BFD_RELOC_16_PCREL_S2
7547 && delayed_branch_p (ip))
7548 {
7549 relaxed_branch = TRUE;
7550 add_relaxed_insn (ip, (relaxed_branch_length
7551 (NULL, NULL,
7552 uncond_branch_p (ip) ? -1
7553 : branch_likely_p (ip) ? 1
7554 : 0)), 4,
7555 RELAX_BRANCH_ENCODE
7556 (AT, mips_pic != NO_PIC,
7557 uncond_branch_p (ip),
7558 branch_likely_p (ip),
7559 pinfo & INSN_WRITE_GPR_31,
7560 0),
7561 address_expr->X_add_symbol,
7562 address_expr->X_add_number);
7563 *reloc_type = BFD_RELOC_UNUSED;
7564 }
7565 else if (mips_opts.micromips
7566 && address_expr
7567 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7568 || *reloc_type > BFD_RELOC_UNUSED)
7569 && (delayed_branch_p (ip) || compact_branch_p (ip))
7570 /* Don't try branch relaxation when users specify
7571 16-bit/32-bit instructions. */
7572 && !forced_insn_length)
7573 {
7574 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7575 && *reloc_type > BFD_RELOC_UNUSED);
7576 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
7577 int uncond = uncond_branch_p (ip) ? -1 : 0;
7578 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7579 int nods = method == APPEND_ADD_WITH_NOP;
7580 int al = pinfo & INSN_WRITE_GPR_31;
7581 int length32 = nods ? 8 : 4;
7582
7583 gas_assert (address_expr != NULL);
7584 gas_assert (!mips_relax.sequence);
7585
7586 relaxed_branch = TRUE;
7587 if (nods)
7588 method = APPEND_ADD;
7589 if (relax32)
7590 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7591 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
7592 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
7593 mips_pic != NO_PIC,
7594 uncond, compact, al, nods,
7595 relax32, 0, 0),
7596 address_expr->X_add_symbol,
7597 address_expr->X_add_number);
7598 *reloc_type = BFD_RELOC_UNUSED;
7599 }
7600 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
7601 {
7602 bfd_boolean require_unextended;
7603 bfd_boolean require_extended;
7604 symbolS *symbol;
7605 offsetT offset;
7606
7607 if (forced_insn_length != 0)
7608 {
7609 require_unextended = forced_insn_length == 2;
7610 require_extended = forced_insn_length == 4;
7611 }
7612 else
7613 {
7614 require_unextended = (mips_opts.noautoextend
7615 && !mips_opcode_32bit_p (ip->insn_mo));
7616 require_extended = 0;
7617 }
7618
7619 /* We need to set up a variant frag. */
7620 gas_assert (address_expr != NULL);
7621 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7622 symbol created by `make_expr_symbol' may not get a necessary
7623 external relocation produced. */
7624 if (address_expr->X_op == O_symbol)
7625 {
7626 symbol = address_expr->X_add_symbol;
7627 offset = address_expr->X_add_number;
7628 }
7629 else
7630 {
7631 symbol = make_expr_symbol (address_expr);
7632 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
7633 offset = 0;
7634 }
7635 add_relaxed_insn (ip, 12, 0,
7636 RELAX_MIPS16_ENCODE
7637 (*reloc_type - BFD_RELOC_UNUSED,
7638 mips_opts.ase & ASE_MIPS16E2,
7639 mips_pic != NO_PIC,
7640 HAVE_32BIT_SYMBOLS,
7641 mips_opts.warn_about_macros,
7642 require_unextended, require_extended,
7643 delayed_branch_p (&history[0]),
7644 history[0].mips16_absolute_jump_p),
7645 symbol, offset);
7646 }
7647 else if (mips_opts.mips16 && insn_length (ip) == 2)
7648 {
7649 if (!delayed_branch_p (ip))
7650 /* Make sure there is enough room to swap this instruction with
7651 a following jump instruction. */
7652 frag_grow (6);
7653 add_fixed_insn (ip);
7654 }
7655 else
7656 {
7657 if (mips_opts.mips16
7658 && mips_opts.noreorder
7659 && delayed_branch_p (&history[0]))
7660 as_warn (_("extended instruction in delay slot"));
7661
7662 if (mips_relax.sequence)
7663 {
7664 /* If we've reached the end of this frag, turn it into a variant
7665 frag and record the information for the instructions we've
7666 written so far. */
7667 if (frag_room () < 4)
7668 relax_close_frag ();
7669 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
7670 }
7671
7672 if (mips_relax.sequence != 2)
7673 {
7674 if (mips_macro_warning.first_insn_sizes[0] == 0)
7675 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7676 mips_macro_warning.sizes[0] += insn_length (ip);
7677 mips_macro_warning.insns[0]++;
7678 }
7679 if (mips_relax.sequence != 1)
7680 {
7681 if (mips_macro_warning.first_insn_sizes[1] == 0)
7682 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7683 mips_macro_warning.sizes[1] += insn_length (ip);
7684 mips_macro_warning.insns[1]++;
7685 }
7686
7687 if (mips_opts.mips16)
7688 {
7689 ip->fixed_p = 1;
7690 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7691 }
7692 add_fixed_insn (ip);
7693 }
7694
7695 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
7696 {
7697 bfd_reloc_code_real_type final_type[3];
7698 reloc_howto_type *howto0;
7699 reloc_howto_type *howto;
7700 int i;
7701
7702 /* Perform any necessary conversion to microMIPS relocations
7703 and find out how many relocations there actually are. */
7704 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7705 final_type[i] = micromips_map_reloc (reloc_type[i]);
7706
7707 /* In a compound relocation, it is the final (outermost)
7708 operator that determines the relocated field. */
7709 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
7710 if (!howto)
7711 abort ();
7712
7713 if (i > 1)
7714 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
7715 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7716 bfd_get_reloc_size (howto),
7717 address_expr,
7718 howto0 && howto0->pc_relative,
7719 final_type[0]);
7720 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7721 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
7722
7723 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
7724 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
7725 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7726
7727 /* These relocations can have an addend that won't fit in
7728 4 octets for 64bit assembly. */
7729 if (GPR_SIZE == 64
7730 && ! howto->partial_inplace
7731 && (reloc_type[0] == BFD_RELOC_16
7732 || reloc_type[0] == BFD_RELOC_32
7733 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7734 || reloc_type[0] == BFD_RELOC_GPREL16
7735 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7736 || reloc_type[0] == BFD_RELOC_GPREL32
7737 || reloc_type[0] == BFD_RELOC_64
7738 || reloc_type[0] == BFD_RELOC_CTOR
7739 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7740 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7741 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7742 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7743 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7744 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7745 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7746 || hi16_reloc_p (reloc_type[0])
7747 || lo16_reloc_p (reloc_type[0])))
7748 ip->fixp[0]->fx_no_overflow = 1;
7749
7750 /* These relocations can have an addend that won't fit in 2 octets. */
7751 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7752 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7753 ip->fixp[0]->fx_no_overflow = 1;
7754
7755 if (mips_relax.sequence)
7756 {
7757 if (mips_relax.first_fixup == 0)
7758 mips_relax.first_fixup = ip->fixp[0];
7759 }
7760 else if (reloc_needs_lo_p (*reloc_type))
7761 {
7762 struct mips_hi_fixup *hi_fixup;
7763
7764 /* Reuse the last entry if it already has a matching %lo. */
7765 hi_fixup = mips_hi_fixup_list;
7766 if (hi_fixup == 0
7767 || !fixup_has_matching_lo_p (hi_fixup->fixp))
7768 {
7769 hi_fixup = XNEW (struct mips_hi_fixup);
7770 hi_fixup->next = mips_hi_fixup_list;
7771 mips_hi_fixup_list = hi_fixup;
7772 }
7773 hi_fixup->fixp = ip->fixp[0];
7774 hi_fixup->seg = now_seg;
7775 }
7776
7777 /* Add fixups for the second and third relocations, if given.
7778 Note that the ABI allows the second relocation to be
7779 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7780 moment we only use RSS_UNDEF, but we could add support
7781 for the others if it ever becomes necessary. */
7782 for (i = 1; i < 3; i++)
7783 if (reloc_type[i] != BFD_RELOC_UNUSED)
7784 {
7785 ip->fixp[i] = fix_new (ip->frag, ip->where,
7786 ip->fixp[0]->fx_size, NULL, 0,
7787 FALSE, final_type[i]);
7788
7789 /* Use fx_tcbit to mark compound relocs. */
7790 ip->fixp[0]->fx_tcbit = 1;
7791 ip->fixp[i]->fx_tcbit = 1;
7792 }
7793 }
7794
7795 /* Update the register mask information. */
7796 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7797 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
7798
7799 switch (method)
7800 {
7801 case APPEND_ADD:
7802 insert_into_history (0, 1, ip);
7803 break;
7804
7805 case APPEND_ADD_WITH_NOP:
7806 {
7807 struct mips_cl_insn *nop;
7808
7809 insert_into_history (0, 1, ip);
7810 nop = get_delay_slot_nop (ip);
7811 add_fixed_insn (nop);
7812 insert_into_history (0, 1, nop);
7813 if (mips_relax.sequence)
7814 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7815 }
7816 break;
7817
7818 case APPEND_ADD_COMPACT:
7819 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7820 if (mips_opts.mips16)
7821 {
7822 ip->insn_opcode |= 0x0080;
7823 find_altered_mips16_opcode (ip);
7824 }
7825 /* Convert microMIPS instructions. */
7826 else if (mips_opts.micromips)
7827 {
7828 /* jr16->jrc */
7829 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7830 ip->insn_opcode |= 0x0020;
7831 /* b16->bc */
7832 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7833 ip->insn_opcode = 0x40e00000;
7834 /* beqz16->beqzc, bnez16->bnezc */
7835 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7836 {
7837 unsigned long regno;
7838
7839 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7840 regno &= MICROMIPSOP_MASK_MD;
7841 regno = micromips_to_32_reg_d_map[regno];
7842 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7843 | (regno << MICROMIPSOP_SH_RS)
7844 | 0x40a00000) ^ 0x00400000;
7845 }
7846 /* beqz->beqzc, bnez->bnezc */
7847 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7848 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7849 | ((ip->insn_opcode >> 7) & 0x00400000)
7850 | 0x40a00000) ^ 0x00400000;
7851 /* beq $0->beqzc, bne $0->bnezc */
7852 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7853 ip->insn_opcode = (((ip->insn_opcode >>
7854 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
7855 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
7856 | ((ip->insn_opcode >> 7) & 0x00400000)
7857 | 0x40a00000) ^ 0x00400000;
7858 else
7859 abort ();
7860 find_altered_micromips_opcode (ip);
7861 }
7862 else
7863 abort ();
7864 install_insn (ip);
7865 insert_into_history (0, 1, ip);
7866 break;
7867
7868 case APPEND_SWAP:
7869 {
7870 struct mips_cl_insn delay = history[0];
7871
7872 if (relaxed_branch || delay.frag != ip->frag)
7873 {
7874 /* Add the delay slot instruction to the end of the
7875 current frag and shrink the fixed part of the
7876 original frag. If the branch occupies the tail of
7877 the latter, move it backwards to cover the gap. */
7878 delay.frag->fr_fix -= branch_disp;
7879 if (delay.frag == ip->frag)
7880 move_insn (ip, ip->frag, ip->where - branch_disp);
7881 add_fixed_insn (&delay);
7882 }
7883 else
7884 {
7885 /* If this is not a relaxed branch and we are in the
7886 same frag, then just swap the instructions. */
7887 move_insn (ip, delay.frag, delay.where);
7888 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
7889 }
7890 history[0] = *ip;
7891 delay.fixed_p = 1;
7892 insert_into_history (0, 1, &delay);
7893 }
7894 break;
7895 }
7896
7897 /* If we have just completed an unconditional branch, clear the history. */
7898 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7899 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
7900 {
7901 unsigned int i;
7902
7903 mips_no_prev_insn ();
7904
7905 for (i = 0; i < ARRAY_SIZE (history); i++)
7906 history[i].cleared_p = 1;
7907 }
7908
7909 /* We need to emit a label at the end of branch-likely macros. */
7910 if (emit_branch_likely_macro)
7911 {
7912 emit_branch_likely_macro = FALSE;
7913 micromips_add_label ();
7914 }
7915
7916 /* We just output an insn, so the next one doesn't have a label. */
7917 mips_clear_insn_labels ();
7918 }
7919
7920 /* Forget that there was any previous instruction or label.
7921 When BRANCH is true, the branch history is also flushed. */
7922
7923 static void
7924 mips_no_prev_insn (void)
7925 {
7926 prev_nop_frag = NULL;
7927 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
7928 mips_clear_insn_labels ();
7929 }
7930
7931 /* This function must be called before we emit something other than
7932 instructions. It is like mips_no_prev_insn except that it inserts
7933 any NOPS that might be needed by previous instructions. */
7934
7935 void
7936 mips_emit_delays (void)
7937 {
7938 if (! mips_opts.noreorder)
7939 {
7940 int nops = nops_for_insn (0, history, NULL);
7941 if (nops > 0)
7942 {
7943 while (nops-- > 0)
7944 add_fixed_insn (NOP_INSN);
7945 mips_move_text_labels ();
7946 }
7947 }
7948 mips_no_prev_insn ();
7949 }
7950
7951 /* Start a (possibly nested) noreorder block. */
7952
7953 static void
7954 start_noreorder (void)
7955 {
7956 if (mips_opts.noreorder == 0)
7957 {
7958 unsigned int i;
7959 int nops;
7960
7961 /* None of the instructions before the .set noreorder can be moved. */
7962 for (i = 0; i < ARRAY_SIZE (history); i++)
7963 history[i].fixed_p = 1;
7964
7965 /* Insert any nops that might be needed between the .set noreorder
7966 block and the previous instructions. We will later remove any
7967 nops that turn out not to be needed. */
7968 nops = nops_for_insn (0, history, NULL);
7969 if (nops > 0)
7970 {
7971 if (mips_optimize != 0)
7972 {
7973 /* Record the frag which holds the nop instructions, so
7974 that we can remove them if we don't need them. */
7975 frag_grow (nops * NOP_INSN_SIZE);
7976 prev_nop_frag = frag_now;
7977 prev_nop_frag_holds = nops;
7978 prev_nop_frag_required = 0;
7979 prev_nop_frag_since = 0;
7980 }
7981
7982 for (; nops > 0; --nops)
7983 add_fixed_insn (NOP_INSN);
7984
7985 /* Move on to a new frag, so that it is safe to simply
7986 decrease the size of prev_nop_frag. */
7987 frag_wane (frag_now);
7988 frag_new (0);
7989 mips_move_text_labels ();
7990 }
7991 mips_mark_labels ();
7992 mips_clear_insn_labels ();
7993 }
7994 mips_opts.noreorder++;
7995 mips_any_noreorder = 1;
7996 }
7997
7998 /* End a nested noreorder block. */
7999
8000 static void
8001 end_noreorder (void)
8002 {
8003 mips_opts.noreorder--;
8004 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
8005 {
8006 /* Commit to inserting prev_nop_frag_required nops and go back to
8007 handling nop insertion the .set reorder way. */
8008 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
8009 * NOP_INSN_SIZE);
8010 insert_into_history (prev_nop_frag_since,
8011 prev_nop_frag_required, NOP_INSN);
8012 prev_nop_frag = NULL;
8013 }
8014 }
8015
8016 /* Sign-extend 32-bit mode constants that have bit 31 set and all
8017 higher bits unset. */
8018
8019 static void
8020 normalize_constant_expr (expressionS *ex)
8021 {
8022 if (ex->X_op == O_constant
8023 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8024 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8025 - 0x80000000);
8026 }
8027
8028 /* Sign-extend 32-bit mode address offsets that have bit 31 set and
8029 all higher bits unset. */
8030
8031 static void
8032 normalize_address_expr (expressionS *ex)
8033 {
8034 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8035 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8036 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8037 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8038 - 0x80000000);
8039 }
8040
8041 /* Try to match TOKENS against OPCODE, storing the result in INSN.
8042 Return true if the match was successful.
8043
8044 OPCODE_EXTRA is a value that should be ORed into the opcode
8045 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
8046 there are more alternatives after OPCODE and SOFT_MATCH is
8047 as for mips_arg_info. */
8048
8049 static bfd_boolean
8050 match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8051 struct mips_operand_token *tokens, unsigned int opcode_extra,
8052 bfd_boolean lax_match, bfd_boolean complete_p)
8053 {
8054 const char *args;
8055 struct mips_arg_info arg;
8056 const struct mips_operand *operand;
8057 char c;
8058
8059 imm_expr.X_op = O_absent;
8060 offset_expr.X_op = O_absent;
8061 offset_reloc[0] = BFD_RELOC_UNUSED;
8062 offset_reloc[1] = BFD_RELOC_UNUSED;
8063 offset_reloc[2] = BFD_RELOC_UNUSED;
8064
8065 create_insn (insn, opcode);
8066 /* When no opcode suffix is specified, assume ".xyzw". */
8067 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8068 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8069 else
8070 insn->insn_opcode |= opcode_extra;
8071 memset (&arg, 0, sizeof (arg));
8072 arg.insn = insn;
8073 arg.token = tokens;
8074 arg.argnum = 1;
8075 arg.last_regno = ILLEGAL_REG;
8076 arg.dest_regno = ILLEGAL_REG;
8077 arg.lax_match = lax_match;
8078 for (args = opcode->args;; ++args)
8079 {
8080 if (arg.token->type == OT_END)
8081 {
8082 /* Handle unary instructions in which only one operand is given.
8083 The source is then the same as the destination. */
8084 if (arg.opnum == 1 && *args == ',')
8085 {
8086 operand = (mips_opts.micromips
8087 ? decode_micromips_operand (args + 1)
8088 : decode_mips_operand (args + 1));
8089 if (operand && mips_optional_operand_p (operand))
8090 {
8091 arg.token = tokens;
8092 arg.argnum = 1;
8093 continue;
8094 }
8095 }
8096
8097 /* Treat elided base registers as $0. */
8098 if (strcmp (args, "(b)") == 0)
8099 args += 3;
8100
8101 if (args[0] == '+')
8102 switch (args[1])
8103 {
8104 case 'K':
8105 case 'N':
8106 /* The register suffix is optional. */
8107 args += 2;
8108 break;
8109 }
8110
8111 /* Fail the match if there were too few operands. */
8112 if (*args)
8113 return FALSE;
8114
8115 /* Successful match. */
8116 if (!complete_p)
8117 return TRUE;
8118 clear_insn_error ();
8119 if (arg.dest_regno == arg.last_regno
8120 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8121 {
8122 if (arg.opnum == 2)
8123 set_insn_error
8124 (0, _("source and destination must be different"));
8125 else if (arg.last_regno == 31)
8126 set_insn_error
8127 (0, _("a destination register must be supplied"));
8128 }
8129 else if (arg.last_regno == 31
8130 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8131 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8132 set_insn_error (0, _("the source register must not be $31"));
8133 check_completed_insn (&arg);
8134 return TRUE;
8135 }
8136
8137 /* Fail the match if the line has too many operands. */
8138 if (*args == 0)
8139 return FALSE;
8140
8141 /* Handle characters that need to match exactly. */
8142 if (*args == '(' || *args == ')' || *args == ',')
8143 {
8144 if (match_char (&arg, *args))
8145 continue;
8146 return FALSE;
8147 }
8148 if (*args == '#')
8149 {
8150 ++args;
8151 if (arg.token->type == OT_DOUBLE_CHAR
8152 && arg.token->u.ch == *args)
8153 {
8154 ++arg.token;
8155 continue;
8156 }
8157 return FALSE;
8158 }
8159
8160 /* Handle special macro operands. Work out the properties of
8161 other operands. */
8162 arg.opnum += 1;
8163 switch (*args)
8164 {
8165 case '-':
8166 switch (args[1])
8167 {
8168 case 'A':
8169 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8170 break;
8171
8172 case 'B':
8173 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8174 break;
8175 }
8176 break;
8177
8178 case '+':
8179 switch (args[1])
8180 {
8181 case 'i':
8182 *offset_reloc = BFD_RELOC_MIPS_JMP;
8183 break;
8184
8185 case '\'':
8186 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8187 break;
8188
8189 case '\"':
8190 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8191 break;
8192 }
8193 break;
8194
8195 case 'I':
8196 if (!match_const_int (&arg, &imm_expr.X_add_number))
8197 return FALSE;
8198 imm_expr.X_op = O_constant;
8199 if (GPR_SIZE == 32)
8200 normalize_constant_expr (&imm_expr);
8201 continue;
8202
8203 case 'A':
8204 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8205 {
8206 /* Assume that the offset has been elided and that what
8207 we saw was a base register. The match will fail later
8208 if that assumption turns out to be wrong. */
8209 offset_expr.X_op = O_constant;
8210 offset_expr.X_add_number = 0;
8211 }
8212 else
8213 {
8214 if (!match_expression (&arg, &offset_expr, offset_reloc))
8215 return FALSE;
8216 normalize_address_expr (&offset_expr);
8217 }
8218 continue;
8219
8220 case 'F':
8221 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8222 8, TRUE))
8223 return FALSE;
8224 continue;
8225
8226 case 'L':
8227 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8228 8, FALSE))
8229 return FALSE;
8230 continue;
8231
8232 case 'f':
8233 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8234 4, TRUE))
8235 return FALSE;
8236 continue;
8237
8238 case 'l':
8239 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8240 4, FALSE))
8241 return FALSE;
8242 continue;
8243
8244 case 'p':
8245 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8246 break;
8247
8248 case 'a':
8249 *offset_reloc = BFD_RELOC_MIPS_JMP;
8250 break;
8251
8252 case 'm':
8253 gas_assert (mips_opts.micromips);
8254 c = args[1];
8255 switch (c)
8256 {
8257 case 'D':
8258 case 'E':
8259 if (!forced_insn_length)
8260 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8261 else if (c == 'D')
8262 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8263 else
8264 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8265 break;
8266 }
8267 break;
8268 }
8269
8270 operand = (mips_opts.micromips
8271 ? decode_micromips_operand (args)
8272 : decode_mips_operand (args));
8273 if (!operand)
8274 abort ();
8275
8276 /* Skip prefixes. */
8277 if (*args == '+' || *args == 'm' || *args == '-')
8278 args++;
8279
8280 if (mips_optional_operand_p (operand)
8281 && args[1] == ','
8282 && (arg.token[0].type != OT_REG
8283 || arg.token[1].type == OT_END))
8284 {
8285 /* Assume that the register has been elided and is the
8286 same as the first operand. */
8287 arg.token = tokens;
8288 arg.argnum = 1;
8289 }
8290
8291 if (!match_operand (&arg, operand))
8292 return FALSE;
8293 }
8294 }
8295
8296 /* Like match_insn, but for MIPS16. */
8297
8298 static bfd_boolean
8299 match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8300 struct mips_operand_token *tokens)
8301 {
8302 const char *args;
8303 const struct mips_operand *operand;
8304 const struct mips_operand *ext_operand;
8305 bfd_boolean pcrel = FALSE;
8306 int required_insn_length;
8307 struct mips_arg_info arg;
8308 int relax_char;
8309
8310 if (forced_insn_length)
8311 required_insn_length = forced_insn_length;
8312 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8313 required_insn_length = 2;
8314 else
8315 required_insn_length = 0;
8316
8317 create_insn (insn, opcode);
8318 imm_expr.X_op = O_absent;
8319 offset_expr.X_op = O_absent;
8320 offset_reloc[0] = BFD_RELOC_UNUSED;
8321 offset_reloc[1] = BFD_RELOC_UNUSED;
8322 offset_reloc[2] = BFD_RELOC_UNUSED;
8323 relax_char = 0;
8324
8325 memset (&arg, 0, sizeof (arg));
8326 arg.insn = insn;
8327 arg.token = tokens;
8328 arg.argnum = 1;
8329 arg.last_regno = ILLEGAL_REG;
8330 arg.dest_regno = ILLEGAL_REG;
8331 relax_char = 0;
8332 for (args = opcode->args;; ++args)
8333 {
8334 int c;
8335
8336 if (arg.token->type == OT_END)
8337 {
8338 offsetT value;
8339
8340 /* Handle unary instructions in which only one operand is given.
8341 The source is then the same as the destination. */
8342 if (arg.opnum == 1 && *args == ',')
8343 {
8344 operand = decode_mips16_operand (args[1], FALSE);
8345 if (operand && mips_optional_operand_p (operand))
8346 {
8347 arg.token = tokens;
8348 arg.argnum = 1;
8349 continue;
8350 }
8351 }
8352
8353 /* Fail the match if there were too few operands. */
8354 if (*args)
8355 return FALSE;
8356
8357 /* Successful match. Stuff the immediate value in now, if
8358 we can. */
8359 clear_insn_error ();
8360 if (opcode->pinfo == INSN_MACRO)
8361 {
8362 gas_assert (relax_char == 0 || relax_char == 'p');
8363 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8364 }
8365 else if (relax_char
8366 && offset_expr.X_op == O_constant
8367 && !pcrel
8368 && calculate_reloc (*offset_reloc,
8369 offset_expr.X_add_number,
8370 &value))
8371 {
8372 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
8373 required_insn_length, &insn->insn_opcode);
8374 offset_expr.X_op = O_absent;
8375 *offset_reloc = BFD_RELOC_UNUSED;
8376 }
8377 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8378 {
8379 if (required_insn_length == 2)
8380 set_insn_error (0, _("invalid unextended operand value"));
8381 else if (!mips_opcode_32bit_p (opcode))
8382 {
8383 forced_insn_length = 4;
8384 insn->insn_opcode |= MIPS16_EXTEND;
8385 }
8386 }
8387 else if (relax_char)
8388 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8389
8390 check_completed_insn (&arg);
8391 return TRUE;
8392 }
8393
8394 /* Fail the match if the line has too many operands. */
8395 if (*args == 0)
8396 return FALSE;
8397
8398 /* Handle characters that need to match exactly. */
8399 if (*args == '(' || *args == ')' || *args == ',')
8400 {
8401 if (match_char (&arg, *args))
8402 continue;
8403 return FALSE;
8404 }
8405
8406 arg.opnum += 1;
8407 c = *args;
8408 switch (c)
8409 {
8410 case 'p':
8411 case 'q':
8412 case 'A':
8413 case 'B':
8414 case 'E':
8415 case 'V':
8416 case 'u':
8417 relax_char = c;
8418 break;
8419
8420 case 'I':
8421 if (!match_const_int (&arg, &imm_expr.X_add_number))
8422 return FALSE;
8423 imm_expr.X_op = O_constant;
8424 if (GPR_SIZE == 32)
8425 normalize_constant_expr (&imm_expr);
8426 continue;
8427
8428 case 'a':
8429 case 'i':
8430 *offset_reloc = BFD_RELOC_MIPS16_JMP;
8431 break;
8432 }
8433
8434 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
8435 if (!operand)
8436 abort ();
8437
8438 if (operand->type == OP_PCREL)
8439 pcrel = TRUE;
8440 else
8441 {
8442 ext_operand = decode_mips16_operand (c, TRUE);
8443 if (operand != ext_operand)
8444 {
8445 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8446 {
8447 offset_expr.X_op = O_constant;
8448 offset_expr.X_add_number = 0;
8449 relax_char = c;
8450 continue;
8451 }
8452
8453 if (!match_expression (&arg, &offset_expr, offset_reloc))
8454 return FALSE;
8455
8456 /* '8' is used for SLTI(U) and has traditionally not
8457 been allowed to take relocation operators. */
8458 if (offset_reloc[0] != BFD_RELOC_UNUSED
8459 && (ext_operand->size != 16 || c == '8'))
8460 {
8461 match_not_constant (&arg);
8462 return FALSE;
8463 }
8464
8465 if (offset_expr.X_op == O_big)
8466 {
8467 match_out_of_range (&arg);
8468 return FALSE;
8469 }
8470
8471 relax_char = c;
8472 continue;
8473 }
8474 }
8475
8476 if (mips_optional_operand_p (operand)
8477 && args[1] == ','
8478 && (arg.token[0].type != OT_REG
8479 || arg.token[1].type == OT_END))
8480 {
8481 /* Assume that the register has been elided and is the
8482 same as the first operand. */
8483 arg.token = tokens;
8484 arg.argnum = 1;
8485 }
8486
8487 if (!match_operand (&arg, operand))
8488 return FALSE;
8489 }
8490 }
8491
8492 /* Record that the current instruction is invalid for the current ISA. */
8493
8494 static void
8495 match_invalid_for_isa (void)
8496 {
8497 set_insn_error_ss
8498 (0, _("opcode not supported on this processor: %s (%s)"),
8499 mips_cpu_info_from_arch (mips_opts.arch)->name,
8500 mips_cpu_info_from_isa (mips_opts.isa)->name);
8501 }
8502
8503 /* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8504 Return true if a definite match or failure was found, storing any match
8505 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8506 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8507 tried and failed to match under normal conditions and now want to try a
8508 more relaxed match. */
8509
8510 static bfd_boolean
8511 match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8512 const struct mips_opcode *past, struct mips_operand_token *tokens,
8513 int opcode_extra, bfd_boolean lax_match)
8514 {
8515 const struct mips_opcode *opcode;
8516 const struct mips_opcode *invalid_delay_slot;
8517 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8518
8519 /* Search for a match, ignoring alternatives that don't satisfy the
8520 current ISA or forced_length. */
8521 invalid_delay_slot = 0;
8522 seen_valid_for_isa = FALSE;
8523 seen_valid_for_size = FALSE;
8524 opcode = first;
8525 do
8526 {
8527 gas_assert (strcmp (opcode->name, first->name) == 0);
8528 if (is_opcode_valid (opcode))
8529 {
8530 seen_valid_for_isa = TRUE;
8531 if (is_size_valid (opcode))
8532 {
8533 bfd_boolean delay_slot_ok;
8534
8535 seen_valid_for_size = TRUE;
8536 delay_slot_ok = is_delay_slot_valid (opcode);
8537 if (match_insn (insn, opcode, tokens, opcode_extra,
8538 lax_match, delay_slot_ok))
8539 {
8540 if (!delay_slot_ok)
8541 {
8542 if (!invalid_delay_slot)
8543 invalid_delay_slot = opcode;
8544 }
8545 else
8546 return TRUE;
8547 }
8548 }
8549 }
8550 ++opcode;
8551 }
8552 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8553
8554 /* If the only matches we found had the wrong length for the delay slot,
8555 pick the first such match. We'll issue an appropriate warning later. */
8556 if (invalid_delay_slot)
8557 {
8558 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8559 lax_match, TRUE))
8560 return TRUE;
8561 abort ();
8562 }
8563
8564 /* Handle the case where we didn't try to match an instruction because
8565 all the alternatives were incompatible with the current ISA. */
8566 if (!seen_valid_for_isa)
8567 {
8568 match_invalid_for_isa ();
8569 return TRUE;
8570 }
8571
8572 /* Handle the case where we didn't try to match an instruction because
8573 all the alternatives were of the wrong size. */
8574 if (!seen_valid_for_size)
8575 {
8576 if (mips_opts.insn32)
8577 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
8578 else
8579 set_insn_error_i
8580 (0, _("unrecognized %d-bit version of microMIPS opcode"),
8581 8 * forced_insn_length);
8582 return TRUE;
8583 }
8584
8585 return FALSE;
8586 }
8587
8588 /* Like match_insns, but for MIPS16. */
8589
8590 static bfd_boolean
8591 match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8592 struct mips_operand_token *tokens)
8593 {
8594 const struct mips_opcode *opcode;
8595 bfd_boolean seen_valid_for_isa;
8596 bfd_boolean seen_valid_for_size;
8597
8598 /* Search for a match, ignoring alternatives that don't satisfy the
8599 current ISA. There are no separate entries for extended forms so
8600 we deal with forced_length later. */
8601 seen_valid_for_isa = FALSE;
8602 seen_valid_for_size = FALSE;
8603 opcode = first;
8604 do
8605 {
8606 gas_assert (strcmp (opcode->name, first->name) == 0);
8607 if (is_opcode_valid_16 (opcode))
8608 {
8609 seen_valid_for_isa = TRUE;
8610 if (is_size_valid_16 (opcode))
8611 {
8612 seen_valid_for_size = TRUE;
8613 if (match_mips16_insn (insn, opcode, tokens))
8614 return TRUE;
8615 }
8616 }
8617 ++opcode;
8618 }
8619 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8620 && strcmp (opcode->name, first->name) == 0);
8621
8622 /* Handle the case where we didn't try to match an instruction because
8623 all the alternatives were incompatible with the current ISA. */
8624 if (!seen_valid_for_isa)
8625 {
8626 match_invalid_for_isa ();
8627 return TRUE;
8628 }
8629
8630 /* Handle the case where we didn't try to match an instruction because
8631 all the alternatives were of the wrong size. */
8632 if (!seen_valid_for_size)
8633 {
8634 if (forced_insn_length == 2)
8635 set_insn_error
8636 (0, _("unrecognized unextended version of MIPS16 opcode"));
8637 else
8638 set_insn_error
8639 (0, _("unrecognized extended version of MIPS16 opcode"));
8640 return TRUE;
8641 }
8642
8643 return FALSE;
8644 }
8645
8646 /* Set up global variables for the start of a new macro. */
8647
8648 static void
8649 macro_start (void)
8650 {
8651 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
8652 memset (&mips_macro_warning.first_insn_sizes, 0,
8653 sizeof (mips_macro_warning.first_insn_sizes));
8654 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
8655 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
8656 && delayed_branch_p (&history[0]));
8657 if (history[0].frag
8658 && history[0].frag->fr_type == rs_machine_dependent
8659 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8660 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8661 mips_macro_warning.delay_slot_length = 0;
8662 else
8663 switch (history[0].insn_mo->pinfo2
8664 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8665 {
8666 case INSN2_BRANCH_DELAY_32BIT:
8667 mips_macro_warning.delay_slot_length = 4;
8668 break;
8669 case INSN2_BRANCH_DELAY_16BIT:
8670 mips_macro_warning.delay_slot_length = 2;
8671 break;
8672 default:
8673 mips_macro_warning.delay_slot_length = 0;
8674 break;
8675 }
8676 mips_macro_warning.first_frag = NULL;
8677 }
8678
8679 /* Given that a macro is longer than one instruction or of the wrong size,
8680 return the appropriate warning for it. Return null if no warning is
8681 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8682 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8683 and RELAX_NOMACRO. */
8684
8685 static const char *
8686 macro_warning (relax_substateT subtype)
8687 {
8688 if (subtype & RELAX_DELAY_SLOT)
8689 return _("macro instruction expanded into multiple instructions"
8690 " in a branch delay slot");
8691 else if (subtype & RELAX_NOMACRO)
8692 return _("macro instruction expanded into multiple instructions");
8693 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8694 | RELAX_DELAY_SLOT_SIZE_SECOND))
8695 return ((subtype & RELAX_DELAY_SLOT_16BIT)
8696 ? _("macro instruction expanded into a wrong size instruction"
8697 " in a 16-bit branch delay slot")
8698 : _("macro instruction expanded into a wrong size instruction"
8699 " in a 32-bit branch delay slot"));
8700 else
8701 return 0;
8702 }
8703
8704 /* Finish up a macro. Emit warnings as appropriate. */
8705
8706 static void
8707 macro_end (void)
8708 {
8709 /* Relaxation warning flags. */
8710 relax_substateT subtype = 0;
8711
8712 /* Check delay slot size requirements. */
8713 if (mips_macro_warning.delay_slot_length == 2)
8714 subtype |= RELAX_DELAY_SLOT_16BIT;
8715 if (mips_macro_warning.delay_slot_length != 0)
8716 {
8717 if (mips_macro_warning.delay_slot_length
8718 != mips_macro_warning.first_insn_sizes[0])
8719 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8720 if (mips_macro_warning.delay_slot_length
8721 != mips_macro_warning.first_insn_sizes[1])
8722 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8723 }
8724
8725 /* Check instruction count requirements. */
8726 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8727 {
8728 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
8729 subtype |= RELAX_SECOND_LONGER;
8730 if (mips_opts.warn_about_macros)
8731 subtype |= RELAX_NOMACRO;
8732 if (mips_macro_warning.delay_slot_p)
8733 subtype |= RELAX_DELAY_SLOT;
8734 }
8735
8736 /* If both alternatives fail to fill a delay slot correctly,
8737 emit the warning now. */
8738 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8739 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8740 {
8741 relax_substateT s;
8742 const char *msg;
8743
8744 s = subtype & (RELAX_DELAY_SLOT_16BIT
8745 | RELAX_DELAY_SLOT_SIZE_FIRST
8746 | RELAX_DELAY_SLOT_SIZE_SECOND);
8747 msg = macro_warning (s);
8748 if (msg != NULL)
8749 as_warn ("%s", msg);
8750 subtype &= ~s;
8751 }
8752
8753 /* If both implementations are longer than 1 instruction, then emit the
8754 warning now. */
8755 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8756 {
8757 relax_substateT s;
8758 const char *msg;
8759
8760 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8761 msg = macro_warning (s);
8762 if (msg != NULL)
8763 as_warn ("%s", msg);
8764 subtype &= ~s;
8765 }
8766
8767 /* If any flags still set, then one implementation might need a warning
8768 and the other either will need one of a different kind or none at all.
8769 Pass any remaining flags over to relaxation. */
8770 if (mips_macro_warning.first_frag != NULL)
8771 mips_macro_warning.first_frag->fr_subtype |= subtype;
8772 }
8773
8774 /* Instruction operand formats used in macros that vary between
8775 standard MIPS and microMIPS code. */
8776
8777 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
8778 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8779 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8780 static const char * const lui_fmt[2] = { "t,u", "s,u" };
8781 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
8782 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
8783 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8784 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8785
8786 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
8787 #define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8788 : cop12_fmt[mips_opts.micromips])
8789 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
8790 #define LUI_FMT (lui_fmt[mips_opts.micromips])
8791 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
8792 #define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8793 : mem12_fmt[mips_opts.micromips])
8794 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
8795 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
8796 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
8797
8798 /* Read a macro's relocation codes from *ARGS and store them in *R.
8799 The first argument in *ARGS will be either the code for a single
8800 relocation or -1 followed by the three codes that make up a
8801 composite relocation. */
8802
8803 static void
8804 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8805 {
8806 int i, next;
8807
8808 next = va_arg (*args, int);
8809 if (next >= 0)
8810 r[0] = (bfd_reloc_code_real_type) next;
8811 else
8812 {
8813 for (i = 0; i < 3; i++)
8814 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8815 /* This function is only used for 16-bit relocation fields.
8816 To make the macro code simpler, treat an unrelocated value
8817 in the same way as BFD_RELOC_LO16. */
8818 if (r[0] == BFD_RELOC_UNUSED)
8819 r[0] = BFD_RELOC_LO16;
8820 }
8821 }
8822
8823 /* Build an instruction created by a macro expansion. This is passed
8824 a pointer to the count of instructions created so far, an
8825 expression, the name of the instruction to build, an operand format
8826 string, and corresponding arguments. */
8827
8828 static void
8829 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
8830 {
8831 const struct mips_opcode *mo = NULL;
8832 bfd_reloc_code_real_type r[3];
8833 const struct mips_opcode *amo;
8834 const struct mips_operand *operand;
8835 struct hash_control *hash;
8836 struct mips_cl_insn insn;
8837 va_list args;
8838 unsigned int uval;
8839
8840 va_start (args, fmt);
8841
8842 if (mips_opts.mips16)
8843 {
8844 mips16_macro_build (ep, name, fmt, &args);
8845 va_end (args);
8846 return;
8847 }
8848
8849 r[0] = BFD_RELOC_UNUSED;
8850 r[1] = BFD_RELOC_UNUSED;
8851 r[2] = BFD_RELOC_UNUSED;
8852 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8853 amo = (struct mips_opcode *) hash_find (hash, name);
8854 gas_assert (amo);
8855 gas_assert (strcmp (name, amo->name) == 0);
8856
8857 do
8858 {
8859 /* Search until we get a match for NAME. It is assumed here that
8860 macros will never generate MDMX, MIPS-3D, or MT instructions.
8861 We try to match an instruction that fulfills the branch delay
8862 slot instruction length requirement (if any) of the previous
8863 instruction. While doing this we record the first instruction
8864 seen that matches all the other conditions and use it anyway
8865 if the requirement cannot be met; we will issue an appropriate
8866 warning later on. */
8867 if (strcmp (fmt, amo->args) == 0
8868 && amo->pinfo != INSN_MACRO
8869 && is_opcode_valid (amo)
8870 && is_size_valid (amo))
8871 {
8872 if (is_delay_slot_valid (amo))
8873 {
8874 mo = amo;
8875 break;
8876 }
8877 else if (!mo)
8878 mo = amo;
8879 }
8880
8881 ++amo;
8882 gas_assert (amo->name);
8883 }
8884 while (strcmp (name, amo->name) == 0);
8885
8886 gas_assert (mo);
8887 create_insn (&insn, mo);
8888 for (; *fmt; ++fmt)
8889 {
8890 switch (*fmt)
8891 {
8892 case ',':
8893 case '(':
8894 case ')':
8895 case 'z':
8896 break;
8897
8898 case 'i':
8899 case 'j':
8900 macro_read_relocs (&args, r);
8901 gas_assert (*r == BFD_RELOC_GPREL16
8902 || *r == BFD_RELOC_MIPS_HIGHER
8903 || *r == BFD_RELOC_HI16_S
8904 || *r == BFD_RELOC_LO16
8905 || *r == BFD_RELOC_MIPS_GOT_OFST
8906 || (mips_opts.micromips
8907 && (*r == BFD_RELOC_16
8908 || *r == BFD_RELOC_MIPS_GOT16
8909 || *r == BFD_RELOC_MIPS_CALL16
8910 || *r == BFD_RELOC_MIPS_GOT_HI16
8911 || *r == BFD_RELOC_MIPS_GOT_LO16
8912 || *r == BFD_RELOC_MIPS_CALL_HI16
8913 || *r == BFD_RELOC_MIPS_CALL_LO16
8914 || *r == BFD_RELOC_MIPS_SUB
8915 || *r == BFD_RELOC_MIPS_GOT_PAGE
8916 || *r == BFD_RELOC_MIPS_HIGHEST
8917 || *r == BFD_RELOC_MIPS_GOT_DISP
8918 || *r == BFD_RELOC_MIPS_TLS_GD
8919 || *r == BFD_RELOC_MIPS_TLS_LDM
8920 || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
8921 || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
8922 || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
8923 || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
8924 || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
8925 break;
8926
8927 case 'o':
8928 macro_read_relocs (&args, r);
8929 break;
8930
8931 case 'u':
8932 macro_read_relocs (&args, r);
8933 gas_assert (ep != NULL
8934 && (ep->X_op == O_constant
8935 || (ep->X_op == O_symbol
8936 && (*r == BFD_RELOC_MIPS_HIGHEST
8937 || *r == BFD_RELOC_HI16_S
8938 || *r == BFD_RELOC_HI16
8939 || *r == BFD_RELOC_GPREL16
8940 || *r == BFD_RELOC_MIPS_GOT_HI16
8941 || *r == BFD_RELOC_MIPS_CALL_HI16))));
8942 break;
8943
8944 case 'p':
8945 gas_assert (ep != NULL);
8946
8947 /*
8948 * This allows macro() to pass an immediate expression for
8949 * creating short branches without creating a symbol.
8950 *
8951 * We don't allow branch relaxation for these branches, as
8952 * they should only appear in ".set nomacro" anyway.
8953 */
8954 if (ep->X_op == O_constant)
8955 {
8956 /* For microMIPS we always use relocations for branches.
8957 So we should not resolve immediate values. */
8958 gas_assert (!mips_opts.micromips);
8959
8960 if ((ep->X_add_number & 3) != 0)
8961 as_bad (_("branch to misaligned address (0x%lx)"),
8962 (unsigned long) ep->X_add_number);
8963 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8964 as_bad (_("branch address range overflow (0x%lx)"),
8965 (unsigned long) ep->X_add_number);
8966 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8967 ep = NULL;
8968 }
8969 else
8970 *r = BFD_RELOC_16_PCREL_S2;
8971 break;
8972
8973 case 'a':
8974 gas_assert (ep != NULL);
8975 *r = BFD_RELOC_MIPS_JMP;
8976 break;
8977
8978 default:
8979 operand = (mips_opts.micromips
8980 ? decode_micromips_operand (fmt)
8981 : decode_mips_operand (fmt));
8982 if (!operand)
8983 abort ();
8984
8985 uval = va_arg (args, int);
8986 if (operand->type == OP_CLO_CLZ_DEST)
8987 uval |= (uval << 5);
8988 insn_insert_operand (&insn, operand, uval);
8989
8990 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
8991 ++fmt;
8992 break;
8993 }
8994 }
8995 va_end (args);
8996 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
8997
8998 append_insn (&insn, ep, r, TRUE);
8999 }
9000
9001 static void
9002 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
9003 va_list *args)
9004 {
9005 struct mips_opcode *mo;
9006 struct mips_cl_insn insn;
9007 const struct mips_operand *operand;
9008 bfd_reloc_code_real_type r[3]
9009 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
9010
9011 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9012 gas_assert (mo);
9013 gas_assert (strcmp (name, mo->name) == 0);
9014
9015 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
9016 {
9017 ++mo;
9018 gas_assert (mo->name);
9019 gas_assert (strcmp (name, mo->name) == 0);
9020 }
9021
9022 create_insn (&insn, mo);
9023 for (; *fmt; ++fmt)
9024 {
9025 int c;
9026
9027 c = *fmt;
9028 switch (c)
9029 {
9030 case ',':
9031 case '(':
9032 case ')':
9033 break;
9034
9035 case '.':
9036 case 'S':
9037 case 'P':
9038 case 'R':
9039 break;
9040
9041 case '<':
9042 case '5':
9043 case 'F':
9044 case 'H':
9045 case 'W':
9046 case 'D':
9047 case 'j':
9048 case '8':
9049 case 'V':
9050 case 'C':
9051 case 'U':
9052 case 'k':
9053 case 'K':
9054 case 'p':
9055 case 'q':
9056 {
9057 offsetT value;
9058
9059 gas_assert (ep != NULL);
9060
9061 if (ep->X_op != O_constant)
9062 *r = (int) BFD_RELOC_UNUSED + c;
9063 else if (calculate_reloc (*r, ep->X_add_number, &value))
9064 {
9065 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
9066 ep = NULL;
9067 *r = BFD_RELOC_UNUSED;
9068 }
9069 }
9070 break;
9071
9072 default:
9073 operand = decode_mips16_operand (c, FALSE);
9074 if (!operand)
9075 abort ();
9076
9077 insn_insert_operand (&insn, operand, va_arg (*args, int));
9078 break;
9079 }
9080 }
9081
9082 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
9083
9084 append_insn (&insn, ep, r, TRUE);
9085 }
9086
9087 /*
9088 * Generate a "jalr" instruction with a relocation hint to the called
9089 * function. This occurs in NewABI PIC code.
9090 */
9091 static void
9092 macro_build_jalr (expressionS *ep, int cprestore)
9093 {
9094 static const bfd_reloc_code_real_type jalr_relocs[2]
9095 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9096 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9097 const char *jalr;
9098 char *f = NULL;
9099
9100 if (MIPS_JALR_HINT_P (ep))
9101 {
9102 frag_grow (8);
9103 f = frag_more (0);
9104 }
9105 if (mips_opts.micromips)
9106 {
9107 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9108 ? "jalr" : "jalrs");
9109 if (MIPS_JALR_HINT_P (ep)
9110 || mips_opts.insn32
9111 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9112 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9113 else
9114 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9115 }
9116 else
9117 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
9118 if (MIPS_JALR_HINT_P (ep))
9119 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
9120 }
9121
9122 /*
9123 * Generate a "lui" instruction.
9124 */
9125 static void
9126 macro_build_lui (expressionS *ep, int regnum)
9127 {
9128 gas_assert (! mips_opts.mips16);
9129
9130 if (ep->X_op != O_constant)
9131 {
9132 gas_assert (ep->X_op == O_symbol);
9133 /* _gp_disp is a special case, used from s_cpload.
9134 __gnu_local_gp is used if mips_no_shared. */
9135 gas_assert (mips_pic == NO_PIC
9136 || (! HAVE_NEWABI
9137 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9138 || (! mips_in_shared
9139 && strcmp (S_GET_NAME (ep->X_add_symbol),
9140 "__gnu_local_gp") == 0));
9141 }
9142
9143 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
9144 }
9145
9146 /* Generate a sequence of instructions to do a load or store from a constant
9147 offset off of a base register (breg) into/from a target register (treg),
9148 using AT if necessary. */
9149 static void
9150 macro_build_ldst_constoffset (expressionS *ep, const char *op,
9151 int treg, int breg, int dbl)
9152 {
9153 gas_assert (ep->X_op == O_constant);
9154
9155 /* Sign-extending 32-bit constants makes their handling easier. */
9156 if (!dbl)
9157 normalize_constant_expr (ep);
9158
9159 /* Right now, this routine can only handle signed 32-bit constants. */
9160 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
9161 as_warn (_("operand overflow"));
9162
9163 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9164 {
9165 /* Signed 16-bit offset will fit in the op. Easy! */
9166 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
9167 }
9168 else
9169 {
9170 /* 32-bit offset, need multiple instructions and AT, like:
9171 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9172 addu $tempreg,$tempreg,$breg
9173 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9174 to handle the complete offset. */
9175 macro_build_lui (ep, AT);
9176 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9177 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
9178
9179 if (!mips_opts.at)
9180 as_bad (_("macro used $at after \".set noat\""));
9181 }
9182 }
9183
9184 /* set_at()
9185 * Generates code to set the $at register to true (one)
9186 * if reg is less than the immediate expression.
9187 */
9188 static void
9189 set_at (int reg, int unsignedp)
9190 {
9191 if (imm_expr.X_add_number >= -0x8000
9192 && imm_expr.X_add_number < 0x8000)
9193 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9194 AT, reg, BFD_RELOC_LO16);
9195 else
9196 {
9197 load_register (AT, &imm_expr, GPR_SIZE == 64);
9198 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
9199 }
9200 }
9201
9202 /* Count the leading zeroes by performing a binary chop. This is a
9203 bulky bit of source, but performance is a LOT better for the
9204 majority of values than a simple loop to count the bits:
9205 for (lcnt = 0; (lcnt < 32); lcnt++)
9206 if ((v) & (1 << (31 - lcnt)))
9207 break;
9208 However it is not code size friendly, and the gain will drop a bit
9209 on certain cached systems.
9210 */
9211 #define COUNT_TOP_ZEROES(v) \
9212 (((v) & ~0xffff) == 0 \
9213 ? ((v) & ~0xff) == 0 \
9214 ? ((v) & ~0xf) == 0 \
9215 ? ((v) & ~0x3) == 0 \
9216 ? ((v) & ~0x1) == 0 \
9217 ? !(v) \
9218 ? 32 \
9219 : 31 \
9220 : 30 \
9221 : ((v) & ~0x7) == 0 \
9222 ? 29 \
9223 : 28 \
9224 : ((v) & ~0x3f) == 0 \
9225 ? ((v) & ~0x1f) == 0 \
9226 ? 27 \
9227 : 26 \
9228 : ((v) & ~0x7f) == 0 \
9229 ? 25 \
9230 : 24 \
9231 : ((v) & ~0xfff) == 0 \
9232 ? ((v) & ~0x3ff) == 0 \
9233 ? ((v) & ~0x1ff) == 0 \
9234 ? 23 \
9235 : 22 \
9236 : ((v) & ~0x7ff) == 0 \
9237 ? 21 \
9238 : 20 \
9239 : ((v) & ~0x3fff) == 0 \
9240 ? ((v) & ~0x1fff) == 0 \
9241 ? 19 \
9242 : 18 \
9243 : ((v) & ~0x7fff) == 0 \
9244 ? 17 \
9245 : 16 \
9246 : ((v) & ~0xffffff) == 0 \
9247 ? ((v) & ~0xfffff) == 0 \
9248 ? ((v) & ~0x3ffff) == 0 \
9249 ? ((v) & ~0x1ffff) == 0 \
9250 ? 15 \
9251 : 14 \
9252 : ((v) & ~0x7ffff) == 0 \
9253 ? 13 \
9254 : 12 \
9255 : ((v) & ~0x3fffff) == 0 \
9256 ? ((v) & ~0x1fffff) == 0 \
9257 ? 11 \
9258 : 10 \
9259 : ((v) & ~0x7fffff) == 0 \
9260 ? 9 \
9261 : 8 \
9262 : ((v) & ~0xfffffff) == 0 \
9263 ? ((v) & ~0x3ffffff) == 0 \
9264 ? ((v) & ~0x1ffffff) == 0 \
9265 ? 7 \
9266 : 6 \
9267 : ((v) & ~0x7ffffff) == 0 \
9268 ? 5 \
9269 : 4 \
9270 : ((v) & ~0x3fffffff) == 0 \
9271 ? ((v) & ~0x1fffffff) == 0 \
9272 ? 3 \
9273 : 2 \
9274 : ((v) & ~0x7fffffff) == 0 \
9275 ? 1 \
9276 : 0)
9277
9278 /* load_register()
9279 * This routine generates the least number of instructions necessary to load
9280 * an absolute expression value into a register.
9281 */
9282 static void
9283 load_register (int reg, expressionS *ep, int dbl)
9284 {
9285 int freg;
9286 expressionS hi32, lo32;
9287
9288 if (ep->X_op != O_big)
9289 {
9290 gas_assert (ep->X_op == O_constant);
9291
9292 /* Sign-extending 32-bit constants makes their handling easier. */
9293 if (!dbl)
9294 normalize_constant_expr (ep);
9295
9296 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
9297 {
9298 /* We can handle 16 bit signed values with an addiu to
9299 $zero. No need to ever use daddiu here, since $zero and
9300 the result are always correct in 32 bit mode. */
9301 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9302 return;
9303 }
9304 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9305 {
9306 /* We can handle 16 bit unsigned values with an ori to
9307 $zero. */
9308 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9309 return;
9310 }
9311 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
9312 {
9313 /* 32 bit values require an lui. */
9314 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9315 if ((ep->X_add_number & 0xffff) != 0)
9316 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9317 return;
9318 }
9319 }
9320
9321 /* The value is larger than 32 bits. */
9322
9323 if (!dbl || GPR_SIZE == 32)
9324 {
9325 char value[32];
9326
9327 sprintf_vma (value, ep->X_add_number);
9328 as_bad (_("number (0x%s) larger than 32 bits"), value);
9329 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9330 return;
9331 }
9332
9333 if (ep->X_op != O_big)
9334 {
9335 hi32 = *ep;
9336 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9337 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9338 hi32.X_add_number &= 0xffffffff;
9339 lo32 = *ep;
9340 lo32.X_add_number &= 0xffffffff;
9341 }
9342 else
9343 {
9344 gas_assert (ep->X_add_number > 2);
9345 if (ep->X_add_number == 3)
9346 generic_bignum[3] = 0;
9347 else if (ep->X_add_number > 4)
9348 as_bad (_("number larger than 64 bits"));
9349 lo32.X_op = O_constant;
9350 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9351 hi32.X_op = O_constant;
9352 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9353 }
9354
9355 if (hi32.X_add_number == 0)
9356 freg = 0;
9357 else
9358 {
9359 int shift, bit;
9360 unsigned long hi, lo;
9361
9362 if (hi32.X_add_number == (offsetT) 0xffffffff)
9363 {
9364 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9365 {
9366 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9367 return;
9368 }
9369 if (lo32.X_add_number & 0x80000000)
9370 {
9371 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9372 if (lo32.X_add_number & 0xffff)
9373 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9374 return;
9375 }
9376 }
9377
9378 /* Check for 16bit shifted constant. We know that hi32 is
9379 non-zero, so start the mask on the first bit of the hi32
9380 value. */
9381 shift = 17;
9382 do
9383 {
9384 unsigned long himask, lomask;
9385
9386 if (shift < 32)
9387 {
9388 himask = 0xffff >> (32 - shift);
9389 lomask = (0xffff << shift) & 0xffffffff;
9390 }
9391 else
9392 {
9393 himask = 0xffff << (shift - 32);
9394 lomask = 0;
9395 }
9396 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9397 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9398 {
9399 expressionS tmp;
9400
9401 tmp.X_op = O_constant;
9402 if (shift < 32)
9403 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9404 | (lo32.X_add_number >> shift));
9405 else
9406 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
9407 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9408 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9409 reg, reg, (shift >= 32) ? shift - 32 : shift);
9410 return;
9411 }
9412 ++shift;
9413 }
9414 while (shift <= (64 - 16));
9415
9416 /* Find the bit number of the lowest one bit, and store the
9417 shifted value in hi/lo. */
9418 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9419 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9420 if (lo != 0)
9421 {
9422 bit = 0;
9423 while ((lo & 1) == 0)
9424 {
9425 lo >>= 1;
9426 ++bit;
9427 }
9428 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9429 hi >>= bit;
9430 }
9431 else
9432 {
9433 bit = 32;
9434 while ((hi & 1) == 0)
9435 {
9436 hi >>= 1;
9437 ++bit;
9438 }
9439 lo = hi;
9440 hi = 0;
9441 }
9442
9443 /* Optimize if the shifted value is a (power of 2) - 1. */
9444 if ((hi == 0 && ((lo + 1) & lo) == 0)
9445 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
9446 {
9447 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
9448 if (shift != 0)
9449 {
9450 expressionS tmp;
9451
9452 /* This instruction will set the register to be all
9453 ones. */
9454 tmp.X_op = O_constant;
9455 tmp.X_add_number = (offsetT) -1;
9456 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9457 if (bit != 0)
9458 {
9459 bit += shift;
9460 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9461 reg, reg, (bit >= 32) ? bit - 32 : bit);
9462 }
9463 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
9464 reg, reg, (shift >= 32) ? shift - 32 : shift);
9465 return;
9466 }
9467 }
9468
9469 /* Sign extend hi32 before calling load_register, because we can
9470 generally get better code when we load a sign extended value. */
9471 if ((hi32.X_add_number & 0x80000000) != 0)
9472 hi32.X_add_number |= ~(offsetT) 0xffffffff;
9473 load_register (reg, &hi32, 0);
9474 freg = reg;
9475 }
9476 if ((lo32.X_add_number & 0xffff0000) == 0)
9477 {
9478 if (freg != 0)
9479 {
9480 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
9481 freg = reg;
9482 }
9483 }
9484 else
9485 {
9486 expressionS mid16;
9487
9488 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
9489 {
9490 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9491 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
9492 return;
9493 }
9494
9495 if (freg != 0)
9496 {
9497 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
9498 freg = reg;
9499 }
9500 mid16 = lo32;
9501 mid16.X_add_number >>= 16;
9502 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9503 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9504 freg = reg;
9505 }
9506 if ((lo32.X_add_number & 0xffff) != 0)
9507 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9508 }
9509
9510 static inline void
9511 load_delay_nop (void)
9512 {
9513 if (!gpr_interlocks)
9514 macro_build (NULL, "nop", "");
9515 }
9516
9517 /* Load an address into a register. */
9518
9519 static void
9520 load_address (int reg, expressionS *ep, int *used_at)
9521 {
9522 if (ep->X_op != O_constant
9523 && ep->X_op != O_symbol)
9524 {
9525 as_bad (_("expression too complex"));
9526 ep->X_op = O_constant;
9527 }
9528
9529 if (ep->X_op == O_constant)
9530 {
9531 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
9532 return;
9533 }
9534
9535 if (mips_pic == NO_PIC)
9536 {
9537 /* If this is a reference to a GP relative symbol, we want
9538 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
9539 Otherwise we want
9540 lui $reg,<sym> (BFD_RELOC_HI16_S)
9541 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9542 If we have an addend, we always use the latter form.
9543
9544 With 64bit address space and a usable $at we want
9545 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9546 lui $at,<sym> (BFD_RELOC_HI16_S)
9547 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9548 daddiu $at,<sym> (BFD_RELOC_LO16)
9549 dsll32 $reg,0
9550 daddu $reg,$reg,$at
9551
9552 If $at is already in use, we use a path which is suboptimal
9553 on superscalar processors.
9554 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9555 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9556 dsll $reg,16
9557 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9558 dsll $reg,16
9559 daddiu $reg,<sym> (BFD_RELOC_LO16)
9560
9561 For GP relative symbols in 64bit address space we can use
9562 the same sequence as in 32bit address space. */
9563 if (HAVE_64BIT_SYMBOLS)
9564 {
9565 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9566 && !nopic_need_relax (ep->X_add_symbol, 1))
9567 {
9568 relax_start (ep->X_add_symbol);
9569 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9570 mips_gp_register, BFD_RELOC_GPREL16);
9571 relax_switch ();
9572 }
9573
9574 if (*used_at == 0 && mips_opts.at)
9575 {
9576 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9577 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
9578 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9579 BFD_RELOC_MIPS_HIGHER);
9580 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
9581 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
9582 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
9583 *used_at = 1;
9584 }
9585 else
9586 {
9587 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9588 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9589 BFD_RELOC_MIPS_HIGHER);
9590 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9591 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
9592 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9593 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
9594 }
9595
9596 if (mips_relax.sequence)
9597 relax_end ();
9598 }
9599 else
9600 {
9601 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9602 && !nopic_need_relax (ep->X_add_symbol, 1))
9603 {
9604 relax_start (ep->X_add_symbol);
9605 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9606 mips_gp_register, BFD_RELOC_GPREL16);
9607 relax_switch ();
9608 }
9609 macro_build_lui (ep, reg);
9610 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9611 reg, reg, BFD_RELOC_LO16);
9612 if (mips_relax.sequence)
9613 relax_end ();
9614 }
9615 }
9616 else if (!mips_big_got)
9617 {
9618 expressionS ex;
9619
9620 /* If this is a reference to an external symbol, we want
9621 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9622 Otherwise we want
9623 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9624 nop
9625 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9626 If there is a constant, it must be added in after.
9627
9628 If we have NewABI, we want
9629 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9630 unless we're referencing a global symbol with a non-zero
9631 offset, in which case cst must be added separately. */
9632 if (HAVE_NEWABI)
9633 {
9634 if (ep->X_add_number)
9635 {
9636 ex.X_add_number = ep->X_add_number;
9637 ep->X_add_number = 0;
9638 relax_start (ep->X_add_symbol);
9639 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9640 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9641 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9642 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9643 ex.X_op = O_constant;
9644 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9645 reg, reg, BFD_RELOC_LO16);
9646 ep->X_add_number = ex.X_add_number;
9647 relax_switch ();
9648 }
9649 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9650 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9651 if (mips_relax.sequence)
9652 relax_end ();
9653 }
9654 else
9655 {
9656 ex.X_add_number = ep->X_add_number;
9657 ep->X_add_number = 0;
9658 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9659 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9660 load_delay_nop ();
9661 relax_start (ep->X_add_symbol);
9662 relax_switch ();
9663 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9664 BFD_RELOC_LO16);
9665 relax_end ();
9666
9667 if (ex.X_add_number != 0)
9668 {
9669 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9670 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9671 ex.X_op = O_constant;
9672 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9673 reg, reg, BFD_RELOC_LO16);
9674 }
9675 }
9676 }
9677 else if (mips_big_got)
9678 {
9679 expressionS ex;
9680
9681 /* This is the large GOT case. If this is a reference to an
9682 external symbol, we want
9683 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9684 addu $reg,$reg,$gp
9685 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
9686
9687 Otherwise, for a reference to a local symbol in old ABI, we want
9688 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9689 nop
9690 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9691 If there is a constant, it must be added in after.
9692
9693 In the NewABI, for local symbols, with or without offsets, we want:
9694 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9695 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
9696 */
9697 if (HAVE_NEWABI)
9698 {
9699 ex.X_add_number = ep->X_add_number;
9700 ep->X_add_number = 0;
9701 relax_start (ep->X_add_symbol);
9702 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9703 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9704 reg, reg, mips_gp_register);
9705 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9706 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9707 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9708 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9709 else if (ex.X_add_number)
9710 {
9711 ex.X_op = O_constant;
9712 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9713 BFD_RELOC_LO16);
9714 }
9715
9716 ep->X_add_number = ex.X_add_number;
9717 relax_switch ();
9718 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9719 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9720 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9721 BFD_RELOC_MIPS_GOT_OFST);
9722 relax_end ();
9723 }
9724 else
9725 {
9726 ex.X_add_number = ep->X_add_number;
9727 ep->X_add_number = 0;
9728 relax_start (ep->X_add_symbol);
9729 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9730 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9731 reg, reg, mips_gp_register);
9732 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9733 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9734 relax_switch ();
9735 if (reg_needs_delay (mips_gp_register))
9736 {
9737 /* We need a nop before loading from $gp. This special
9738 check is required because the lui which starts the main
9739 instruction stream does not refer to $gp, and so will not
9740 insert the nop which may be required. */
9741 macro_build (NULL, "nop", "");
9742 }
9743 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9744 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9745 load_delay_nop ();
9746 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9747 BFD_RELOC_LO16);
9748 relax_end ();
9749
9750 if (ex.X_add_number != 0)
9751 {
9752 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9753 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9754 ex.X_op = O_constant;
9755 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9756 BFD_RELOC_LO16);
9757 }
9758 }
9759 }
9760 else
9761 abort ();
9762
9763 if (!mips_opts.at && *used_at == 1)
9764 as_bad (_("macro used $at after \".set noat\""));
9765 }
9766
9767 /* Move the contents of register SOURCE into register DEST. */
9768
9769 static void
9770 move_register (int dest, int source)
9771 {
9772 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9773 instruction specifically requires a 32-bit one. */
9774 if (mips_opts.micromips
9775 && !mips_opts.insn32
9776 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9777 macro_build (NULL, "move", "mp,mj", dest, source);
9778 else
9779 macro_build (NULL, "or", "d,v,t", dest, source, 0);
9780 }
9781
9782 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
9783 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9784 The two alternatives are:
9785
9786 Global symbol Local symbol
9787 ------------- ------------
9788 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9789 ... ...
9790 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9791
9792 load_got_offset emits the first instruction and add_got_offset
9793 emits the second for a 16-bit offset or add_got_offset_hilo emits
9794 a sequence to add a 32-bit offset using a scratch register. */
9795
9796 static void
9797 load_got_offset (int dest, expressionS *local)
9798 {
9799 expressionS global;
9800
9801 global = *local;
9802 global.X_add_number = 0;
9803
9804 relax_start (local->X_add_symbol);
9805 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9806 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9807 relax_switch ();
9808 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9809 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9810 relax_end ();
9811 }
9812
9813 static void
9814 add_got_offset (int dest, expressionS *local)
9815 {
9816 expressionS global;
9817
9818 global.X_op = O_constant;
9819 global.X_op_symbol = NULL;
9820 global.X_add_symbol = NULL;
9821 global.X_add_number = local->X_add_number;
9822
9823 relax_start (local->X_add_symbol);
9824 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
9825 dest, dest, BFD_RELOC_LO16);
9826 relax_switch ();
9827 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
9828 relax_end ();
9829 }
9830
9831 static void
9832 add_got_offset_hilo (int dest, expressionS *local, int tmp)
9833 {
9834 expressionS global;
9835 int hold_mips_optimize;
9836
9837 global.X_op = O_constant;
9838 global.X_op_symbol = NULL;
9839 global.X_add_symbol = NULL;
9840 global.X_add_number = local->X_add_number;
9841
9842 relax_start (local->X_add_symbol);
9843 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9844 relax_switch ();
9845 /* Set mips_optimize around the lui instruction to avoid
9846 inserting an unnecessary nop after the lw. */
9847 hold_mips_optimize = mips_optimize;
9848 mips_optimize = 2;
9849 macro_build_lui (&global, tmp);
9850 mips_optimize = hold_mips_optimize;
9851 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9852 relax_end ();
9853
9854 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9855 }
9856
9857 /* Emit a sequence of instructions to emulate a branch likely operation.
9858 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9859 is its complementing branch with the original condition negated.
9860 CALL is set if the original branch specified the link operation.
9861 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9862
9863 Code like this is produced in the noreorder mode:
9864
9865 BRNEG <args>, 1f
9866 nop
9867 b <sym>
9868 delay slot (executed only if branch taken)
9869 1:
9870
9871 or, if CALL is set:
9872
9873 BRNEG <args>, 1f
9874 nop
9875 bal <sym>
9876 delay slot (executed only if branch taken)
9877 1:
9878
9879 In the reorder mode the delay slot would be filled with a nop anyway,
9880 so code produced is simply:
9881
9882 BR <args>, <sym>
9883 nop
9884
9885 This function is used when producing code for the microMIPS ASE that
9886 does not implement branch likely instructions in hardware. */
9887
9888 static void
9889 macro_build_branch_likely (const char *br, const char *brneg,
9890 int call, expressionS *ep, const char *fmt,
9891 unsigned int sreg, unsigned int treg)
9892 {
9893 int noreorder = mips_opts.noreorder;
9894 expressionS expr1;
9895
9896 gas_assert (mips_opts.micromips);
9897 start_noreorder ();
9898 if (noreorder)
9899 {
9900 micromips_label_expr (&expr1);
9901 macro_build (&expr1, brneg, fmt, sreg, treg);
9902 macro_build (NULL, "nop", "");
9903 macro_build (ep, call ? "bal" : "b", "p");
9904
9905 /* Set to true so that append_insn adds a label. */
9906 emit_branch_likely_macro = TRUE;
9907 }
9908 else
9909 {
9910 macro_build (ep, br, fmt, sreg, treg);
9911 macro_build (NULL, "nop", "");
9912 }
9913 end_noreorder ();
9914 }
9915
9916 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9917 the condition code tested. EP specifies the branch target. */
9918
9919 static void
9920 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9921 {
9922 const int call = 0;
9923 const char *brneg;
9924 const char *br;
9925
9926 switch (type)
9927 {
9928 case M_BC1FL:
9929 br = "bc1f";
9930 brneg = "bc1t";
9931 break;
9932 case M_BC1TL:
9933 br = "bc1t";
9934 brneg = "bc1f";
9935 break;
9936 case M_BC2FL:
9937 br = "bc2f";
9938 brneg = "bc2t";
9939 break;
9940 case M_BC2TL:
9941 br = "bc2t";
9942 brneg = "bc2f";
9943 break;
9944 default:
9945 abort ();
9946 }
9947 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9948 }
9949
9950 /* Emit a two-argument branch macro specified by TYPE, using SREG as
9951 the register tested. EP specifies the branch target. */
9952
9953 static void
9954 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9955 {
9956 const char *brneg = NULL;
9957 const char *br;
9958 int call = 0;
9959
9960 switch (type)
9961 {
9962 case M_BGEZ:
9963 br = "bgez";
9964 break;
9965 case M_BGEZL:
9966 br = mips_opts.micromips ? "bgez" : "bgezl";
9967 brneg = "bltz";
9968 break;
9969 case M_BGEZALL:
9970 gas_assert (mips_opts.micromips);
9971 br = mips_opts.insn32 ? "bgezal" : "bgezals";
9972 brneg = "bltz";
9973 call = 1;
9974 break;
9975 case M_BGTZ:
9976 br = "bgtz";
9977 break;
9978 case M_BGTZL:
9979 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9980 brneg = "blez";
9981 break;
9982 case M_BLEZ:
9983 br = "blez";
9984 break;
9985 case M_BLEZL:
9986 br = mips_opts.micromips ? "blez" : "blezl";
9987 brneg = "bgtz";
9988 break;
9989 case M_BLTZ:
9990 br = "bltz";
9991 break;
9992 case M_BLTZL:
9993 br = mips_opts.micromips ? "bltz" : "bltzl";
9994 brneg = "bgez";
9995 break;
9996 case M_BLTZALL:
9997 gas_assert (mips_opts.micromips);
9998 br = mips_opts.insn32 ? "bltzal" : "bltzals";
9999 brneg = "bgez";
10000 call = 1;
10001 break;
10002 default:
10003 abort ();
10004 }
10005 if (mips_opts.micromips && brneg)
10006 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
10007 else
10008 macro_build (ep, br, "s,p", sreg);
10009 }
10010
10011 /* Emit a three-argument branch macro specified by TYPE, using SREG and
10012 TREG as the registers tested. EP specifies the branch target. */
10013
10014 static void
10015 macro_build_branch_rsrt (int type, expressionS *ep,
10016 unsigned int sreg, unsigned int treg)
10017 {
10018 const char *brneg = NULL;
10019 const int call = 0;
10020 const char *br;
10021
10022 switch (type)
10023 {
10024 case M_BEQ:
10025 case M_BEQ_I:
10026 br = "beq";
10027 break;
10028 case M_BEQL:
10029 case M_BEQL_I:
10030 br = mips_opts.micromips ? "beq" : "beql";
10031 brneg = "bne";
10032 break;
10033 case M_BNE:
10034 case M_BNE_I:
10035 br = "bne";
10036 break;
10037 case M_BNEL:
10038 case M_BNEL_I:
10039 br = mips_opts.micromips ? "bne" : "bnel";
10040 brneg = "beq";
10041 break;
10042 default:
10043 abort ();
10044 }
10045 if (mips_opts.micromips && brneg)
10046 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10047 else
10048 macro_build (ep, br, "s,t,p", sreg, treg);
10049 }
10050
10051 /* Return the high part that should be loaded in order to make the low
10052 part of VALUE accessible using an offset of OFFBITS bits. */
10053
10054 static offsetT
10055 offset_high_part (offsetT value, unsigned int offbits)
10056 {
10057 offsetT bias;
10058 addressT low_mask;
10059
10060 if (offbits == 0)
10061 return value;
10062 bias = 1 << (offbits - 1);
10063 low_mask = bias * 2 - 1;
10064 return (value + bias) & ~low_mask;
10065 }
10066
10067 /* Return true if the value stored in offset_expr and offset_reloc
10068 fits into a signed offset of OFFBITS bits. RANGE is the maximum
10069 amount that the caller wants to add without inducing overflow
10070 and ALIGN is the known alignment of the value in bytes. */
10071
10072 static bfd_boolean
10073 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10074 {
10075 if (offbits == 16)
10076 {
10077 /* Accept any relocation operator if overflow isn't a concern. */
10078 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10079 return TRUE;
10080
10081 /* These relocations are guaranteed not to overflow in correct links. */
10082 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10083 || gprel16_reloc_p (*offset_reloc))
10084 return TRUE;
10085 }
10086 if (offset_expr.X_op == O_constant
10087 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10088 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10089 return TRUE;
10090 return FALSE;
10091 }
10092
10093 /*
10094 * Build macros
10095 * This routine implements the seemingly endless macro or synthesized
10096 * instructions and addressing modes in the mips assembly language. Many
10097 * of these macros are simple and are similar to each other. These could
10098 * probably be handled by some kind of table or grammar approach instead of
10099 * this verbose method. Others are not simple macros but are more like
10100 * optimizing code generation.
10101 * One interesting optimization is when several store macros appear
10102 * consecutively that would load AT with the upper half of the same address.
10103 * The ensuing load upper instructions are omitted. This implies some kind
10104 * of global optimization. We currently only optimize within a single macro.
10105 * For many of the load and store macros if the address is specified as a
10106 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10107 * first load register 'at' with zero and use it as the base register. The
10108 * mips assembler simply uses register $zero. Just one tiny optimization
10109 * we're missing.
10110 */
10111 static void
10112 macro (struct mips_cl_insn *ip, char *str)
10113 {
10114 const struct mips_operand_array *operands;
10115 unsigned int breg, i;
10116 unsigned int tempreg;
10117 int mask;
10118 int used_at = 0;
10119 expressionS label_expr;
10120 expressionS expr1;
10121 expressionS *ep;
10122 const char *s;
10123 const char *s2;
10124 const char *fmt;
10125 int likely = 0;
10126 int coproc = 0;
10127 int offbits = 16;
10128 int call = 0;
10129 int jals = 0;
10130 int dbl = 0;
10131 int imm = 0;
10132 int ust = 0;
10133 int lp = 0;
10134 bfd_boolean large_offset;
10135 int off;
10136 int hold_mips_optimize;
10137 unsigned int align;
10138 unsigned int op[MAX_OPERANDS];
10139
10140 gas_assert (! mips_opts.mips16);
10141
10142 operands = insn_operands (ip);
10143 for (i = 0; i < MAX_OPERANDS; i++)
10144 if (operands->operand[i])
10145 op[i] = insn_extract_operand (ip, operands->operand[i]);
10146 else
10147 op[i] = -1;
10148
10149 mask = ip->insn_mo->mask;
10150
10151 label_expr.X_op = O_constant;
10152 label_expr.X_op_symbol = NULL;
10153 label_expr.X_add_symbol = NULL;
10154 label_expr.X_add_number = 0;
10155
10156 expr1.X_op = O_constant;
10157 expr1.X_op_symbol = NULL;
10158 expr1.X_add_symbol = NULL;
10159 expr1.X_add_number = 1;
10160 align = 1;
10161
10162 switch (mask)
10163 {
10164 case M_DABS:
10165 dbl = 1;
10166 /* Fall through. */
10167 case M_ABS:
10168 /* bgez $a0,1f
10169 move v0,$a0
10170 sub v0,$zero,$a0
10171 1:
10172 */
10173
10174 start_noreorder ();
10175
10176 if (mips_opts.micromips)
10177 micromips_label_expr (&label_expr);
10178 else
10179 label_expr.X_add_number = 8;
10180 macro_build (&label_expr, "bgez", "s,p", op[1]);
10181 if (op[0] == op[1])
10182 macro_build (NULL, "nop", "");
10183 else
10184 move_register (op[0], op[1]);
10185 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
10186 if (mips_opts.micromips)
10187 micromips_add_label ();
10188
10189 end_noreorder ();
10190 break;
10191
10192 case M_ADD_I:
10193 s = "addi";
10194 s2 = "add";
10195 goto do_addi;
10196 case M_ADDU_I:
10197 s = "addiu";
10198 s2 = "addu";
10199 goto do_addi;
10200 case M_DADD_I:
10201 dbl = 1;
10202 s = "daddi";
10203 s2 = "dadd";
10204 if (!mips_opts.micromips)
10205 goto do_addi;
10206 if (imm_expr.X_add_number >= -0x200
10207 && imm_expr.X_add_number < 0x200)
10208 {
10209 macro_build (NULL, s, "t,r,.", op[0], op[1],
10210 (int) imm_expr.X_add_number);
10211 break;
10212 }
10213 goto do_addi_i;
10214 case M_DADDU_I:
10215 dbl = 1;
10216 s = "daddiu";
10217 s2 = "daddu";
10218 do_addi:
10219 if (imm_expr.X_add_number >= -0x8000
10220 && imm_expr.X_add_number < 0x8000)
10221 {
10222 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
10223 break;
10224 }
10225 do_addi_i:
10226 used_at = 1;
10227 load_register (AT, &imm_expr, dbl);
10228 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10229 break;
10230
10231 case M_AND_I:
10232 s = "andi";
10233 s2 = "and";
10234 goto do_bit;
10235 case M_OR_I:
10236 s = "ori";
10237 s2 = "or";
10238 goto do_bit;
10239 case M_NOR_I:
10240 s = "";
10241 s2 = "nor";
10242 goto do_bit;
10243 case M_XOR_I:
10244 s = "xori";
10245 s2 = "xor";
10246 do_bit:
10247 if (imm_expr.X_add_number >= 0
10248 && imm_expr.X_add_number < 0x10000)
10249 {
10250 if (mask != M_NOR_I)
10251 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
10252 else
10253 {
10254 macro_build (&imm_expr, "ori", "t,r,i",
10255 op[0], op[1], BFD_RELOC_LO16);
10256 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
10257 }
10258 break;
10259 }
10260
10261 used_at = 1;
10262 load_register (AT, &imm_expr, GPR_SIZE == 64);
10263 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10264 break;
10265
10266 case M_BALIGN:
10267 switch (imm_expr.X_add_number)
10268 {
10269 case 0:
10270 macro_build (NULL, "nop", "");
10271 break;
10272 case 2:
10273 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
10274 break;
10275 case 1:
10276 case 3:
10277 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
10278 (int) imm_expr.X_add_number);
10279 break;
10280 default:
10281 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10282 (unsigned long) imm_expr.X_add_number);
10283 break;
10284 }
10285 break;
10286
10287 case M_BC1FL:
10288 case M_BC1TL:
10289 case M_BC2FL:
10290 case M_BC2TL:
10291 gas_assert (mips_opts.micromips);
10292 macro_build_branch_ccl (mask, &offset_expr,
10293 EXTRACT_OPERAND (1, BCC, *ip));
10294 break;
10295
10296 case M_BEQ_I:
10297 case M_BEQL_I:
10298 case M_BNE_I:
10299 case M_BNEL_I:
10300 if (imm_expr.X_add_number == 0)
10301 op[1] = 0;
10302 else
10303 {
10304 op[1] = AT;
10305 used_at = 1;
10306 load_register (op[1], &imm_expr, GPR_SIZE == 64);
10307 }
10308 /* Fall through. */
10309 case M_BEQL:
10310 case M_BNEL:
10311 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
10312 break;
10313
10314 case M_BGEL:
10315 likely = 1;
10316 /* Fall through. */
10317 case M_BGE:
10318 if (op[1] == 0)
10319 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10320 else if (op[0] == 0)
10321 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
10322 else
10323 {
10324 used_at = 1;
10325 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10326 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10327 &offset_expr, AT, ZERO);
10328 }
10329 break;
10330
10331 case M_BGEZL:
10332 case M_BGEZALL:
10333 case M_BGTZL:
10334 case M_BLEZL:
10335 case M_BLTZL:
10336 case M_BLTZALL:
10337 macro_build_branch_rs (mask, &offset_expr, op[0]);
10338 break;
10339
10340 case M_BGTL_I:
10341 likely = 1;
10342 /* Fall through. */
10343 case M_BGT_I:
10344 /* Check for > max integer. */
10345 if (imm_expr.X_add_number >= GPR_SMAX)
10346 {
10347 do_false:
10348 /* Result is always false. */
10349 if (! likely)
10350 macro_build (NULL, "nop", "");
10351 else
10352 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
10353 break;
10354 }
10355 ++imm_expr.X_add_number;
10356 /* FALLTHROUGH */
10357 case M_BGE_I:
10358 case M_BGEL_I:
10359 if (mask == M_BGEL_I)
10360 likely = 1;
10361 if (imm_expr.X_add_number == 0)
10362 {
10363 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
10364 &offset_expr, op[0]);
10365 break;
10366 }
10367 if (imm_expr.X_add_number == 1)
10368 {
10369 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
10370 &offset_expr, op[0]);
10371 break;
10372 }
10373 if (imm_expr.X_add_number <= GPR_SMIN)
10374 {
10375 do_true:
10376 /* result is always true */
10377 as_warn (_("branch %s is always true"), ip->insn_mo->name);
10378 macro_build (&offset_expr, "b", "p");
10379 break;
10380 }
10381 used_at = 1;
10382 set_at (op[0], 0);
10383 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10384 &offset_expr, AT, ZERO);
10385 break;
10386
10387 case M_BGEUL:
10388 likely = 1;
10389 /* Fall through. */
10390 case M_BGEU:
10391 if (op[1] == 0)
10392 goto do_true;
10393 else if (op[0] == 0)
10394 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10395 &offset_expr, ZERO, op[1]);
10396 else
10397 {
10398 used_at = 1;
10399 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10400 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10401 &offset_expr, AT, ZERO);
10402 }
10403 break;
10404
10405 case M_BGTUL_I:
10406 likely = 1;
10407 /* Fall through. */
10408 case M_BGTU_I:
10409 if (op[0] == 0
10410 || (GPR_SIZE == 32
10411 && imm_expr.X_add_number == -1))
10412 goto do_false;
10413 ++imm_expr.X_add_number;
10414 /* FALLTHROUGH */
10415 case M_BGEU_I:
10416 case M_BGEUL_I:
10417 if (mask == M_BGEUL_I)
10418 likely = 1;
10419 if (imm_expr.X_add_number == 0)
10420 goto do_true;
10421 else if (imm_expr.X_add_number == 1)
10422 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10423 &offset_expr, op[0], ZERO);
10424 else
10425 {
10426 used_at = 1;
10427 set_at (op[0], 1);
10428 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10429 &offset_expr, AT, ZERO);
10430 }
10431 break;
10432
10433 case M_BGTL:
10434 likely = 1;
10435 /* Fall through. */
10436 case M_BGT:
10437 if (op[1] == 0)
10438 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10439 else if (op[0] == 0)
10440 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
10441 else
10442 {
10443 used_at = 1;
10444 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10445 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10446 &offset_expr, AT, ZERO);
10447 }
10448 break;
10449
10450 case M_BGTUL:
10451 likely = 1;
10452 /* Fall through. */
10453 case M_BGTU:
10454 if (op[1] == 0)
10455 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10456 &offset_expr, op[0], ZERO);
10457 else if (op[0] == 0)
10458 goto do_false;
10459 else
10460 {
10461 used_at = 1;
10462 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10463 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10464 &offset_expr, AT, ZERO);
10465 }
10466 break;
10467
10468 case M_BLEL:
10469 likely = 1;
10470 /* Fall through. */
10471 case M_BLE:
10472 if (op[1] == 0)
10473 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10474 else if (op[0] == 0)
10475 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
10476 else
10477 {
10478 used_at = 1;
10479 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10480 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10481 &offset_expr, AT, ZERO);
10482 }
10483 break;
10484
10485 case M_BLEL_I:
10486 likely = 1;
10487 /* Fall through. */
10488 case M_BLE_I:
10489 if (imm_expr.X_add_number >= GPR_SMAX)
10490 goto do_true;
10491 ++imm_expr.X_add_number;
10492 /* FALLTHROUGH */
10493 case M_BLT_I:
10494 case M_BLTL_I:
10495 if (mask == M_BLTL_I)
10496 likely = 1;
10497 if (imm_expr.X_add_number == 0)
10498 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10499 else if (imm_expr.X_add_number == 1)
10500 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10501 else
10502 {
10503 used_at = 1;
10504 set_at (op[0], 0);
10505 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10506 &offset_expr, AT, ZERO);
10507 }
10508 break;
10509
10510 case M_BLEUL:
10511 likely = 1;
10512 /* Fall through. */
10513 case M_BLEU:
10514 if (op[1] == 0)
10515 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10516 &offset_expr, op[0], ZERO);
10517 else if (op[0] == 0)
10518 goto do_true;
10519 else
10520 {
10521 used_at = 1;
10522 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10523 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10524 &offset_expr, AT, ZERO);
10525 }
10526 break;
10527
10528 case M_BLEUL_I:
10529 likely = 1;
10530 /* Fall through. */
10531 case M_BLEU_I:
10532 if (op[0] == 0
10533 || (GPR_SIZE == 32
10534 && imm_expr.X_add_number == -1))
10535 goto do_true;
10536 ++imm_expr.X_add_number;
10537 /* FALLTHROUGH */
10538 case M_BLTU_I:
10539 case M_BLTUL_I:
10540 if (mask == M_BLTUL_I)
10541 likely = 1;
10542 if (imm_expr.X_add_number == 0)
10543 goto do_false;
10544 else if (imm_expr.X_add_number == 1)
10545 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10546 &offset_expr, op[0], ZERO);
10547 else
10548 {
10549 used_at = 1;
10550 set_at (op[0], 1);
10551 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10552 &offset_expr, AT, ZERO);
10553 }
10554 break;
10555
10556 case M_BLTL:
10557 likely = 1;
10558 /* Fall through. */
10559 case M_BLT:
10560 if (op[1] == 0)
10561 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10562 else if (op[0] == 0)
10563 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
10564 else
10565 {
10566 used_at = 1;
10567 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10568 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10569 &offset_expr, AT, ZERO);
10570 }
10571 break;
10572
10573 case M_BLTUL:
10574 likely = 1;
10575 /* Fall through. */
10576 case M_BLTU:
10577 if (op[1] == 0)
10578 goto do_false;
10579 else if (op[0] == 0)
10580 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10581 &offset_expr, ZERO, op[1]);
10582 else
10583 {
10584 used_at = 1;
10585 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10586 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10587 &offset_expr, AT, ZERO);
10588 }
10589 break;
10590
10591 case M_DDIV_3:
10592 dbl = 1;
10593 /* Fall through. */
10594 case M_DIV_3:
10595 s = "mflo";
10596 goto do_div3;
10597 case M_DREM_3:
10598 dbl = 1;
10599 /* Fall through. */
10600 case M_REM_3:
10601 s = "mfhi";
10602 do_div3:
10603 if (op[2] == 0)
10604 {
10605 as_warn (_("divide by zero"));
10606 if (mips_trap)
10607 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10608 else
10609 macro_build (NULL, "break", BRK_FMT, 7);
10610 break;
10611 }
10612
10613 start_noreorder ();
10614 if (mips_trap)
10615 {
10616 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10617 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10618 }
10619 else
10620 {
10621 if (mips_opts.micromips)
10622 micromips_label_expr (&label_expr);
10623 else
10624 label_expr.X_add_number = 8;
10625 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10626 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10627 macro_build (NULL, "break", BRK_FMT, 7);
10628 if (mips_opts.micromips)
10629 micromips_add_label ();
10630 }
10631 expr1.X_add_number = -1;
10632 used_at = 1;
10633 load_register (AT, &expr1, dbl);
10634 if (mips_opts.micromips)
10635 micromips_label_expr (&label_expr);
10636 else
10637 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
10638 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
10639 if (dbl)
10640 {
10641 expr1.X_add_number = 1;
10642 load_register (AT, &expr1, dbl);
10643 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
10644 }
10645 else
10646 {
10647 expr1.X_add_number = 0x80000000;
10648 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
10649 }
10650 if (mips_trap)
10651 {
10652 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
10653 /* We want to close the noreorder block as soon as possible, so
10654 that later insns are available for delay slot filling. */
10655 end_noreorder ();
10656 }
10657 else
10658 {
10659 if (mips_opts.micromips)
10660 micromips_label_expr (&label_expr);
10661 else
10662 label_expr.X_add_number = 8;
10663 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
10664 macro_build (NULL, "nop", "");
10665
10666 /* We want to close the noreorder block as soon as possible, so
10667 that later insns are available for delay slot filling. */
10668 end_noreorder ();
10669
10670 macro_build (NULL, "break", BRK_FMT, 6);
10671 }
10672 if (mips_opts.micromips)
10673 micromips_add_label ();
10674 macro_build (NULL, s, MFHL_FMT, op[0]);
10675 break;
10676
10677 case M_DIV_3I:
10678 s = "div";
10679 s2 = "mflo";
10680 goto do_divi;
10681 case M_DIVU_3I:
10682 s = "divu";
10683 s2 = "mflo";
10684 goto do_divi;
10685 case M_REM_3I:
10686 s = "div";
10687 s2 = "mfhi";
10688 goto do_divi;
10689 case M_REMU_3I:
10690 s = "divu";
10691 s2 = "mfhi";
10692 goto do_divi;
10693 case M_DDIV_3I:
10694 dbl = 1;
10695 s = "ddiv";
10696 s2 = "mflo";
10697 goto do_divi;
10698 case M_DDIVU_3I:
10699 dbl = 1;
10700 s = "ddivu";
10701 s2 = "mflo";
10702 goto do_divi;
10703 case M_DREM_3I:
10704 dbl = 1;
10705 s = "ddiv";
10706 s2 = "mfhi";
10707 goto do_divi;
10708 case M_DREMU_3I:
10709 dbl = 1;
10710 s = "ddivu";
10711 s2 = "mfhi";
10712 do_divi:
10713 if (imm_expr.X_add_number == 0)
10714 {
10715 as_warn (_("divide by zero"));
10716 if (mips_trap)
10717 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10718 else
10719 macro_build (NULL, "break", BRK_FMT, 7);
10720 break;
10721 }
10722 if (imm_expr.X_add_number == 1)
10723 {
10724 if (strcmp (s2, "mflo") == 0)
10725 move_register (op[0], op[1]);
10726 else
10727 move_register (op[0], ZERO);
10728 break;
10729 }
10730 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
10731 {
10732 if (strcmp (s2, "mflo") == 0)
10733 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
10734 else
10735 move_register (op[0], ZERO);
10736 break;
10737 }
10738
10739 used_at = 1;
10740 load_register (AT, &imm_expr, dbl);
10741 macro_build (NULL, s, "z,s,t", op[1], AT);
10742 macro_build (NULL, s2, MFHL_FMT, op[0]);
10743 break;
10744
10745 case M_DIVU_3:
10746 s = "divu";
10747 s2 = "mflo";
10748 goto do_divu3;
10749 case M_REMU_3:
10750 s = "divu";
10751 s2 = "mfhi";
10752 goto do_divu3;
10753 case M_DDIVU_3:
10754 s = "ddivu";
10755 s2 = "mflo";
10756 goto do_divu3;
10757 case M_DREMU_3:
10758 s = "ddivu";
10759 s2 = "mfhi";
10760 do_divu3:
10761 start_noreorder ();
10762 if (mips_trap)
10763 {
10764 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10765 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10766 /* We want to close the noreorder block as soon as possible, so
10767 that later insns are available for delay slot filling. */
10768 end_noreorder ();
10769 }
10770 else
10771 {
10772 if (mips_opts.micromips)
10773 micromips_label_expr (&label_expr);
10774 else
10775 label_expr.X_add_number = 8;
10776 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10777 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10778
10779 /* We want to close the noreorder block as soon as possible, so
10780 that later insns are available for delay slot filling. */
10781 end_noreorder ();
10782 macro_build (NULL, "break", BRK_FMT, 7);
10783 if (mips_opts.micromips)
10784 micromips_add_label ();
10785 }
10786 macro_build (NULL, s2, MFHL_FMT, op[0]);
10787 break;
10788
10789 case M_DLCA_AB:
10790 dbl = 1;
10791 /* Fall through. */
10792 case M_LCA_AB:
10793 call = 1;
10794 goto do_la;
10795 case M_DLA_AB:
10796 dbl = 1;
10797 /* Fall through. */
10798 case M_LA_AB:
10799 do_la:
10800 /* Load the address of a symbol into a register. If breg is not
10801 zero, we then add a base register to it. */
10802
10803 breg = op[2];
10804 if (dbl && GPR_SIZE == 32)
10805 as_warn (_("dla used to load 32-bit register; recommend using la "
10806 "instead"));
10807
10808 if (!dbl && HAVE_64BIT_OBJECTS)
10809 as_warn (_("la used to load 64-bit address; recommend using dla "
10810 "instead"));
10811
10812 if (small_offset_p (0, align, 16))
10813 {
10814 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
10815 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10816 break;
10817 }
10818
10819 if (mips_opts.at && (op[0] == breg))
10820 {
10821 tempreg = AT;
10822 used_at = 1;
10823 }
10824 else
10825 tempreg = op[0];
10826
10827 if (offset_expr.X_op != O_symbol
10828 && offset_expr.X_op != O_constant)
10829 {
10830 as_bad (_("expression too complex"));
10831 offset_expr.X_op = O_constant;
10832 }
10833
10834 if (offset_expr.X_op == O_constant)
10835 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
10836 else if (mips_pic == NO_PIC)
10837 {
10838 /* If this is a reference to a GP relative symbol, we want
10839 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
10840 Otherwise we want
10841 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10842 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10843 If we have a constant, we need two instructions anyhow,
10844 so we may as well always use the latter form.
10845
10846 With 64bit address space and a usable $at we want
10847 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10848 lui $at,<sym> (BFD_RELOC_HI16_S)
10849 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10850 daddiu $at,<sym> (BFD_RELOC_LO16)
10851 dsll32 $tempreg,0
10852 daddu $tempreg,$tempreg,$at
10853
10854 If $at is already in use, we use a path which is suboptimal
10855 on superscalar processors.
10856 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10857 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10858 dsll $tempreg,16
10859 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10860 dsll $tempreg,16
10861 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10862
10863 For GP relative symbols in 64bit address space we can use
10864 the same sequence as in 32bit address space. */
10865 if (HAVE_64BIT_SYMBOLS)
10866 {
10867 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10868 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10869 {
10870 relax_start (offset_expr.X_add_symbol);
10871 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10872 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10873 relax_switch ();
10874 }
10875
10876 if (used_at == 0 && mips_opts.at)
10877 {
10878 macro_build (&offset_expr, "lui", LUI_FMT,
10879 tempreg, BFD_RELOC_MIPS_HIGHEST);
10880 macro_build (&offset_expr, "lui", LUI_FMT,
10881 AT, BFD_RELOC_HI16_S);
10882 macro_build (&offset_expr, "daddiu", "t,r,j",
10883 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10884 macro_build (&offset_expr, "daddiu", "t,r,j",
10885 AT, AT, BFD_RELOC_LO16);
10886 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
10887 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
10888 used_at = 1;
10889 }
10890 else
10891 {
10892 macro_build (&offset_expr, "lui", LUI_FMT,
10893 tempreg, BFD_RELOC_MIPS_HIGHEST);
10894 macro_build (&offset_expr, "daddiu", "t,r,j",
10895 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10896 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10897 macro_build (&offset_expr, "daddiu", "t,r,j",
10898 tempreg, tempreg, BFD_RELOC_HI16_S);
10899 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10900 macro_build (&offset_expr, "daddiu", "t,r,j",
10901 tempreg, tempreg, BFD_RELOC_LO16);
10902 }
10903
10904 if (mips_relax.sequence)
10905 relax_end ();
10906 }
10907 else
10908 {
10909 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10910 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10911 {
10912 relax_start (offset_expr.X_add_symbol);
10913 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10914 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10915 relax_switch ();
10916 }
10917 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
10918 as_bad (_("offset too large"));
10919 macro_build_lui (&offset_expr, tempreg);
10920 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10921 tempreg, tempreg, BFD_RELOC_LO16);
10922 if (mips_relax.sequence)
10923 relax_end ();
10924 }
10925 }
10926 else if (!mips_big_got && !HAVE_NEWABI)
10927 {
10928 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10929
10930 /* If this is a reference to an external symbol, and there
10931 is no constant, we want
10932 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10933 or for lca or if tempreg is PIC_CALL_REG
10934 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10935 For a local symbol, we want
10936 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10937 nop
10938 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10939
10940 If we have a small constant, and this is a reference to
10941 an external symbol, we want
10942 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10943 nop
10944 addiu $tempreg,$tempreg,<constant>
10945 For a local symbol, we want the same instruction
10946 sequence, but we output a BFD_RELOC_LO16 reloc on the
10947 addiu instruction.
10948
10949 If we have a large constant, and this is a reference to
10950 an external symbol, we want
10951 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10952 lui $at,<hiconstant>
10953 addiu $at,$at,<loconstant>
10954 addu $tempreg,$tempreg,$at
10955 For a local symbol, we want the same instruction
10956 sequence, but we output a BFD_RELOC_LO16 reloc on the
10957 addiu instruction.
10958 */
10959
10960 if (offset_expr.X_add_number == 0)
10961 {
10962 if (mips_pic == SVR4_PIC
10963 && breg == 0
10964 && (call || tempreg == PIC_CALL_REG))
10965 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10966
10967 relax_start (offset_expr.X_add_symbol);
10968 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10969 lw_reloc_type, mips_gp_register);
10970 if (breg != 0)
10971 {
10972 /* We're going to put in an addu instruction using
10973 tempreg, so we may as well insert the nop right
10974 now. */
10975 load_delay_nop ();
10976 }
10977 relax_switch ();
10978 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10979 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
10980 load_delay_nop ();
10981 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10982 tempreg, tempreg, BFD_RELOC_LO16);
10983 relax_end ();
10984 /* FIXME: If breg == 0, and the next instruction uses
10985 $tempreg, then if this variant case is used an extra
10986 nop will be generated. */
10987 }
10988 else if (offset_expr.X_add_number >= -0x8000
10989 && offset_expr.X_add_number < 0x8000)
10990 {
10991 load_got_offset (tempreg, &offset_expr);
10992 load_delay_nop ();
10993 add_got_offset (tempreg, &offset_expr);
10994 }
10995 else
10996 {
10997 expr1.X_add_number = offset_expr.X_add_number;
10998 offset_expr.X_add_number =
10999 SEXT_16BIT (offset_expr.X_add_number);
11000 load_got_offset (tempreg, &offset_expr);
11001 offset_expr.X_add_number = expr1.X_add_number;
11002 /* If we are going to add in a base register, and the
11003 target register and the base register are the same,
11004 then we are using AT as a temporary register. Since
11005 we want to load the constant into AT, we add our
11006 current AT (from the global offset table) and the
11007 register into the register now, and pretend we were
11008 not using a base register. */
11009 if (breg == op[0])
11010 {
11011 load_delay_nop ();
11012 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11013 op[0], AT, breg);
11014 breg = 0;
11015 tempreg = op[0];
11016 }
11017 add_got_offset_hilo (tempreg, &offset_expr, AT);
11018 used_at = 1;
11019 }
11020 }
11021 else if (!mips_big_got && HAVE_NEWABI)
11022 {
11023 int add_breg_early = 0;
11024
11025 /* If this is a reference to an external, and there is no
11026 constant, or local symbol (*), with or without a
11027 constant, we want
11028 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11029 or for lca or if tempreg is PIC_CALL_REG
11030 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11031
11032 If we have a small constant, and this is a reference to
11033 an external symbol, we want
11034 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11035 addiu $tempreg,$tempreg,<constant>
11036
11037 If we have a large constant, and this is a reference to
11038 an external symbol, we want
11039 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11040 lui $at,<hiconstant>
11041 addiu $at,$at,<loconstant>
11042 addu $tempreg,$tempreg,$at
11043
11044 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11045 local symbols, even though it introduces an additional
11046 instruction. */
11047
11048 if (offset_expr.X_add_number)
11049 {
11050 expr1.X_add_number = offset_expr.X_add_number;
11051 offset_expr.X_add_number = 0;
11052
11053 relax_start (offset_expr.X_add_symbol);
11054 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11055 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11056
11057 if (expr1.X_add_number >= -0x8000
11058 && expr1.X_add_number < 0x8000)
11059 {
11060 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11061 tempreg, tempreg, BFD_RELOC_LO16);
11062 }
11063 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11064 {
11065 unsigned int dreg;
11066
11067 /* If we are going to add in a base register, and the
11068 target register and the base register are the same,
11069 then we are using AT as a temporary register. Since
11070 we want to load the constant into AT, we add our
11071 current AT (from the global offset table) and the
11072 register into the register now, and pretend we were
11073 not using a base register. */
11074 if (breg != op[0])
11075 dreg = tempreg;
11076 else
11077 {
11078 gas_assert (tempreg == AT);
11079 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11080 op[0], AT, breg);
11081 dreg = op[0];
11082 add_breg_early = 1;
11083 }
11084
11085 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11086 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11087 dreg, dreg, AT);
11088
11089 used_at = 1;
11090 }
11091 else
11092 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11093
11094 relax_switch ();
11095 offset_expr.X_add_number = expr1.X_add_number;
11096
11097 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11098 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11099 if (add_breg_early)
11100 {
11101 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11102 op[0], tempreg, breg);
11103 breg = 0;
11104 tempreg = op[0];
11105 }
11106 relax_end ();
11107 }
11108 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
11109 {
11110 relax_start (offset_expr.X_add_symbol);
11111 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11112 BFD_RELOC_MIPS_CALL16, mips_gp_register);
11113 relax_switch ();
11114 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11115 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11116 relax_end ();
11117 }
11118 else
11119 {
11120 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11121 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11122 }
11123 }
11124 else if (mips_big_got && !HAVE_NEWABI)
11125 {
11126 int gpdelay;
11127 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11128 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11129 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11130
11131 /* This is the large GOT case. If this is a reference to an
11132 external symbol, and there is no constant, we want
11133 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11134 addu $tempreg,$tempreg,$gp
11135 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11136 or for lca or if tempreg is PIC_CALL_REG
11137 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11138 addu $tempreg,$tempreg,$gp
11139 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11140 For a local symbol, we want
11141 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11142 nop
11143 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11144
11145 If we have a small constant, and this is a reference to
11146 an external symbol, we want
11147 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11148 addu $tempreg,$tempreg,$gp
11149 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11150 nop
11151 addiu $tempreg,$tempreg,<constant>
11152 For a local symbol, we want
11153 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11154 nop
11155 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11156
11157 If we have a large constant, and this is a reference to
11158 an external symbol, we want
11159 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11160 addu $tempreg,$tempreg,$gp
11161 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11162 lui $at,<hiconstant>
11163 addiu $at,$at,<loconstant>
11164 addu $tempreg,$tempreg,$at
11165 For a local symbol, we want
11166 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11167 lui $at,<hiconstant>
11168 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11169 addu $tempreg,$tempreg,$at
11170 */
11171
11172 expr1.X_add_number = offset_expr.X_add_number;
11173 offset_expr.X_add_number = 0;
11174 relax_start (offset_expr.X_add_symbol);
11175 gpdelay = reg_needs_delay (mips_gp_register);
11176 if (expr1.X_add_number == 0 && breg == 0
11177 && (call || tempreg == PIC_CALL_REG))
11178 {
11179 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11180 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11181 }
11182 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11183 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11184 tempreg, tempreg, mips_gp_register);
11185 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11186 tempreg, lw_reloc_type, tempreg);
11187 if (expr1.X_add_number == 0)
11188 {
11189 if (breg != 0)
11190 {
11191 /* We're going to put in an addu instruction using
11192 tempreg, so we may as well insert the nop right
11193 now. */
11194 load_delay_nop ();
11195 }
11196 }
11197 else if (expr1.X_add_number >= -0x8000
11198 && expr1.X_add_number < 0x8000)
11199 {
11200 load_delay_nop ();
11201 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11202 tempreg, tempreg, BFD_RELOC_LO16);
11203 }
11204 else
11205 {
11206 unsigned int dreg;
11207
11208 /* If we are going to add in a base register, and the
11209 target register and the base register are the same,
11210 then we are using AT as a temporary register. Since
11211 we want to load the constant into AT, we add our
11212 current AT (from the global offset table) and the
11213 register into the register now, and pretend we were
11214 not using a base register. */
11215 if (breg != op[0])
11216 dreg = tempreg;
11217 else
11218 {
11219 gas_assert (tempreg == AT);
11220 load_delay_nop ();
11221 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11222 op[0], AT, breg);
11223 dreg = op[0];
11224 }
11225
11226 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11227 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11228
11229 used_at = 1;
11230 }
11231 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
11232 relax_switch ();
11233
11234 if (gpdelay)
11235 {
11236 /* This is needed because this instruction uses $gp, but
11237 the first instruction on the main stream does not. */
11238 macro_build (NULL, "nop", "");
11239 }
11240
11241 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11242 local_reloc_type, mips_gp_register);
11243 if (expr1.X_add_number >= -0x8000
11244 && expr1.X_add_number < 0x8000)
11245 {
11246 load_delay_nop ();
11247 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11248 tempreg, tempreg, BFD_RELOC_LO16);
11249 /* FIXME: If add_number is 0, and there was no base
11250 register, the external symbol case ended with a load,
11251 so if the symbol turns out to not be external, and
11252 the next instruction uses tempreg, an unnecessary nop
11253 will be inserted. */
11254 }
11255 else
11256 {
11257 if (breg == op[0])
11258 {
11259 /* We must add in the base register now, as in the
11260 external symbol case. */
11261 gas_assert (tempreg == AT);
11262 load_delay_nop ();
11263 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11264 op[0], AT, breg);
11265 tempreg = op[0];
11266 /* We set breg to 0 because we have arranged to add
11267 it in in both cases. */
11268 breg = 0;
11269 }
11270
11271 macro_build_lui (&expr1, AT);
11272 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11273 AT, AT, BFD_RELOC_LO16);
11274 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11275 tempreg, tempreg, AT);
11276 used_at = 1;
11277 }
11278 relax_end ();
11279 }
11280 else if (mips_big_got && HAVE_NEWABI)
11281 {
11282 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11283 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11284 int add_breg_early = 0;
11285
11286 /* This is the large GOT case. If this is a reference to an
11287 external symbol, and there is no constant, we want
11288 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11289 add $tempreg,$tempreg,$gp
11290 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11291 or for lca or if tempreg is PIC_CALL_REG
11292 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11293 add $tempreg,$tempreg,$gp
11294 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11295
11296 If we have a small constant, and this is a reference to
11297 an external symbol, we want
11298 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11299 add $tempreg,$tempreg,$gp
11300 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11301 addi $tempreg,$tempreg,<constant>
11302
11303 If we have a large constant, and this is a reference to
11304 an external symbol, we want
11305 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11306 addu $tempreg,$tempreg,$gp
11307 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11308 lui $at,<hiconstant>
11309 addi $at,$at,<loconstant>
11310 add $tempreg,$tempreg,$at
11311
11312 If we have NewABI, and we know it's a local symbol, we want
11313 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11314 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11315 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11316
11317 relax_start (offset_expr.X_add_symbol);
11318
11319 expr1.X_add_number = offset_expr.X_add_number;
11320 offset_expr.X_add_number = 0;
11321
11322 if (expr1.X_add_number == 0 && breg == 0
11323 && (call || tempreg == PIC_CALL_REG))
11324 {
11325 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11326 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11327 }
11328 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11329 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11330 tempreg, tempreg, mips_gp_register);
11331 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11332 tempreg, lw_reloc_type, tempreg);
11333
11334 if (expr1.X_add_number == 0)
11335 ;
11336 else if (expr1.X_add_number >= -0x8000
11337 && expr1.X_add_number < 0x8000)
11338 {
11339 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11340 tempreg, tempreg, BFD_RELOC_LO16);
11341 }
11342 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11343 {
11344 unsigned int dreg;
11345
11346 /* If we are going to add in a base register, and the
11347 target register and the base register are the same,
11348 then we are using AT as a temporary register. Since
11349 we want to load the constant into AT, we add our
11350 current AT (from the global offset table) and the
11351 register into the register now, and pretend we were
11352 not using a base register. */
11353 if (breg != op[0])
11354 dreg = tempreg;
11355 else
11356 {
11357 gas_assert (tempreg == AT);
11358 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11359 op[0], AT, breg);
11360 dreg = op[0];
11361 add_breg_early = 1;
11362 }
11363
11364 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11365 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11366
11367 used_at = 1;
11368 }
11369 else
11370 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11371
11372 relax_switch ();
11373 offset_expr.X_add_number = expr1.X_add_number;
11374 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11375 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11376 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11377 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11378 if (add_breg_early)
11379 {
11380 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11381 op[0], tempreg, breg);
11382 breg = 0;
11383 tempreg = op[0];
11384 }
11385 relax_end ();
11386 }
11387 else
11388 abort ();
11389
11390 if (breg != 0)
11391 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
11392 break;
11393
11394 case M_MSGSND:
11395 gas_assert (!mips_opts.micromips);
11396 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
11397 break;
11398
11399 case M_MSGLD:
11400 gas_assert (!mips_opts.micromips);
11401 macro_build (NULL, "c2", "C", 0x02);
11402 break;
11403
11404 case M_MSGLD_T:
11405 gas_assert (!mips_opts.micromips);
11406 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
11407 break;
11408
11409 case M_MSGWAIT:
11410 gas_assert (!mips_opts.micromips);
11411 macro_build (NULL, "c2", "C", 3);
11412 break;
11413
11414 case M_MSGWAIT_T:
11415 gas_assert (!mips_opts.micromips);
11416 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
11417 break;
11418
11419 case M_J_A:
11420 /* The j instruction may not be used in PIC code, since it
11421 requires an absolute address. We convert it to a b
11422 instruction. */
11423 if (mips_pic == NO_PIC)
11424 macro_build (&offset_expr, "j", "a");
11425 else
11426 macro_build (&offset_expr, "b", "p");
11427 break;
11428
11429 /* The jal instructions must be handled as macros because when
11430 generating PIC code they expand to multi-instruction
11431 sequences. Normally they are simple instructions. */
11432 case M_JALS_1:
11433 op[1] = op[0];
11434 op[0] = RA;
11435 /* Fall through. */
11436 case M_JALS_2:
11437 gas_assert (mips_opts.micromips);
11438 if (mips_opts.insn32)
11439 {
11440 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11441 break;
11442 }
11443 jals = 1;
11444 goto jal;
11445 case M_JAL_1:
11446 op[1] = op[0];
11447 op[0] = RA;
11448 /* Fall through. */
11449 case M_JAL_2:
11450 jal:
11451 if (mips_pic == NO_PIC)
11452 {
11453 s = jals ? "jalrs" : "jalr";
11454 if (mips_opts.micromips
11455 && !mips_opts.insn32
11456 && op[0] == RA
11457 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11458 macro_build (NULL, s, "mj", op[1]);
11459 else
11460 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11461 }
11462 else
11463 {
11464 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11465 && mips_cprestore_offset >= 0);
11466
11467 if (op[1] != PIC_CALL_REG)
11468 as_warn (_("MIPS PIC call to register other than $25"));
11469
11470 s = ((mips_opts.micromips
11471 && !mips_opts.insn32
11472 && (!mips_opts.noreorder || cprestore))
11473 ? "jalrs" : "jalr");
11474 if (mips_opts.micromips
11475 && !mips_opts.insn32
11476 && op[0] == RA
11477 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11478 macro_build (NULL, s, "mj", op[1]);
11479 else
11480 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11481 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
11482 {
11483 if (mips_cprestore_offset < 0)
11484 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11485 else
11486 {
11487 if (!mips_frame_reg_valid)
11488 {
11489 as_warn (_("no .frame pseudo-op used in PIC code"));
11490 /* Quiet this warning. */
11491 mips_frame_reg_valid = 1;
11492 }
11493 if (!mips_cprestore_valid)
11494 {
11495 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11496 /* Quiet this warning. */
11497 mips_cprestore_valid = 1;
11498 }
11499 if (mips_opts.noreorder)
11500 macro_build (NULL, "nop", "");
11501 expr1.X_add_number = mips_cprestore_offset;
11502 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11503 mips_gp_register,
11504 mips_frame_reg,
11505 HAVE_64BIT_ADDRESSES);
11506 }
11507 }
11508 }
11509
11510 break;
11511
11512 case M_JALS_A:
11513 gas_assert (mips_opts.micromips);
11514 if (mips_opts.insn32)
11515 {
11516 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11517 break;
11518 }
11519 jals = 1;
11520 /* Fall through. */
11521 case M_JAL_A:
11522 if (mips_pic == NO_PIC)
11523 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
11524 else if (mips_pic == SVR4_PIC)
11525 {
11526 /* If this is a reference to an external symbol, and we are
11527 using a small GOT, we want
11528 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11529 nop
11530 jalr $ra,$25
11531 nop
11532 lw $gp,cprestore($sp)
11533 The cprestore value is set using the .cprestore
11534 pseudo-op. If we are using a big GOT, we want
11535 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11536 addu $25,$25,$gp
11537 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11538 nop
11539 jalr $ra,$25
11540 nop
11541 lw $gp,cprestore($sp)
11542 If the symbol is not external, we want
11543 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11544 nop
11545 addiu $25,$25,<sym> (BFD_RELOC_LO16)
11546 jalr $ra,$25
11547 nop
11548 lw $gp,cprestore($sp)
11549
11550 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11551 sequences above, minus nops, unless the symbol is local,
11552 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11553 GOT_DISP. */
11554 if (HAVE_NEWABI)
11555 {
11556 if (!mips_big_got)
11557 {
11558 relax_start (offset_expr.X_add_symbol);
11559 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11560 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11561 mips_gp_register);
11562 relax_switch ();
11563 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11564 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
11565 mips_gp_register);
11566 relax_end ();
11567 }
11568 else
11569 {
11570 relax_start (offset_expr.X_add_symbol);
11571 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11572 BFD_RELOC_MIPS_CALL_HI16);
11573 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11574 PIC_CALL_REG, mips_gp_register);
11575 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11576 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11577 PIC_CALL_REG);
11578 relax_switch ();
11579 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11580 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11581 mips_gp_register);
11582 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11583 PIC_CALL_REG, PIC_CALL_REG,
11584 BFD_RELOC_MIPS_GOT_OFST);
11585 relax_end ();
11586 }
11587
11588 macro_build_jalr (&offset_expr, 0);
11589 }
11590 else
11591 {
11592 relax_start (offset_expr.X_add_symbol);
11593 if (!mips_big_got)
11594 {
11595 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11596 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11597 mips_gp_register);
11598 load_delay_nop ();
11599 relax_switch ();
11600 }
11601 else
11602 {
11603 int gpdelay;
11604
11605 gpdelay = reg_needs_delay (mips_gp_register);
11606 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11607 BFD_RELOC_MIPS_CALL_HI16);
11608 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11609 PIC_CALL_REG, mips_gp_register);
11610 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11611 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11612 PIC_CALL_REG);
11613 load_delay_nop ();
11614 relax_switch ();
11615 if (gpdelay)
11616 macro_build (NULL, "nop", "");
11617 }
11618 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11619 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
11620 mips_gp_register);
11621 load_delay_nop ();
11622 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11623 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
11624 relax_end ();
11625 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
11626
11627 if (mips_cprestore_offset < 0)
11628 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11629 else
11630 {
11631 if (!mips_frame_reg_valid)
11632 {
11633 as_warn (_("no .frame pseudo-op used in PIC code"));
11634 /* Quiet this warning. */
11635 mips_frame_reg_valid = 1;
11636 }
11637 if (!mips_cprestore_valid)
11638 {
11639 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11640 /* Quiet this warning. */
11641 mips_cprestore_valid = 1;
11642 }
11643 if (mips_opts.noreorder)
11644 macro_build (NULL, "nop", "");
11645 expr1.X_add_number = mips_cprestore_offset;
11646 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11647 mips_gp_register,
11648 mips_frame_reg,
11649 HAVE_64BIT_ADDRESSES);
11650 }
11651 }
11652 }
11653 else if (mips_pic == VXWORKS_PIC)
11654 as_bad (_("non-PIC jump used in PIC library"));
11655 else
11656 abort ();
11657
11658 break;
11659
11660 case M_LBUE_AB:
11661 s = "lbue";
11662 fmt = "t,+j(b)";
11663 offbits = 9;
11664 goto ld_st;
11665 case M_LHUE_AB:
11666 s = "lhue";
11667 fmt = "t,+j(b)";
11668 offbits = 9;
11669 goto ld_st;
11670 case M_LBE_AB:
11671 s = "lbe";
11672 fmt = "t,+j(b)";
11673 offbits = 9;
11674 goto ld_st;
11675 case M_LHE_AB:
11676 s = "lhe";
11677 fmt = "t,+j(b)";
11678 offbits = 9;
11679 goto ld_st;
11680 case M_LLE_AB:
11681 s = "lle";
11682 fmt = "t,+j(b)";
11683 offbits = 9;
11684 goto ld_st;
11685 case M_LWE_AB:
11686 s = "lwe";
11687 fmt = "t,+j(b)";
11688 offbits = 9;
11689 goto ld_st;
11690 case M_LWLE_AB:
11691 s = "lwle";
11692 fmt = "t,+j(b)";
11693 offbits = 9;
11694 goto ld_st;
11695 case M_LWRE_AB:
11696 s = "lwre";
11697 fmt = "t,+j(b)";
11698 offbits = 9;
11699 goto ld_st;
11700 case M_SBE_AB:
11701 s = "sbe";
11702 fmt = "t,+j(b)";
11703 offbits = 9;
11704 goto ld_st;
11705 case M_SCE_AB:
11706 s = "sce";
11707 fmt = "t,+j(b)";
11708 offbits = 9;
11709 goto ld_st;
11710 case M_SHE_AB:
11711 s = "she";
11712 fmt = "t,+j(b)";
11713 offbits = 9;
11714 goto ld_st;
11715 case M_SWE_AB:
11716 s = "swe";
11717 fmt = "t,+j(b)";
11718 offbits = 9;
11719 goto ld_st;
11720 case M_SWLE_AB:
11721 s = "swle";
11722 fmt = "t,+j(b)";
11723 offbits = 9;
11724 goto ld_st;
11725 case M_SWRE_AB:
11726 s = "swre";
11727 fmt = "t,+j(b)";
11728 offbits = 9;
11729 goto ld_st;
11730 case M_ACLR_AB:
11731 s = "aclr";
11732 fmt = "\\,~(b)";
11733 offbits = 12;
11734 goto ld_st;
11735 case M_ASET_AB:
11736 s = "aset";
11737 fmt = "\\,~(b)";
11738 offbits = 12;
11739 goto ld_st;
11740 case M_LB_AB:
11741 s = "lb";
11742 fmt = "t,o(b)";
11743 goto ld;
11744 case M_LBU_AB:
11745 s = "lbu";
11746 fmt = "t,o(b)";
11747 goto ld;
11748 case M_LH_AB:
11749 s = "lh";
11750 fmt = "t,o(b)";
11751 goto ld;
11752 case M_LHU_AB:
11753 s = "lhu";
11754 fmt = "t,o(b)";
11755 goto ld;
11756 case M_LW_AB:
11757 s = "lw";
11758 fmt = "t,o(b)";
11759 goto ld;
11760 case M_LWC0_AB:
11761 gas_assert (!mips_opts.micromips);
11762 s = "lwc0";
11763 fmt = "E,o(b)";
11764 /* Itbl support may require additional care here. */
11765 coproc = 1;
11766 goto ld_st;
11767 case M_LWC1_AB:
11768 s = "lwc1";
11769 fmt = "T,o(b)";
11770 /* Itbl support may require additional care here. */
11771 coproc = 1;
11772 goto ld_st;
11773 case M_LWC2_AB:
11774 s = "lwc2";
11775 fmt = COP12_FMT;
11776 offbits = (mips_opts.micromips ? 12
11777 : ISA_IS_R6 (mips_opts.isa) ? 11
11778 : 16);
11779 /* Itbl support may require additional care here. */
11780 coproc = 1;
11781 goto ld_st;
11782 case M_LWC3_AB:
11783 gas_assert (!mips_opts.micromips);
11784 s = "lwc3";
11785 fmt = "E,o(b)";
11786 /* Itbl support may require additional care here. */
11787 coproc = 1;
11788 goto ld_st;
11789 case M_LWL_AB:
11790 s = "lwl";
11791 fmt = MEM12_FMT;
11792 offbits = (mips_opts.micromips ? 12 : 16);
11793 goto ld_st;
11794 case M_LWR_AB:
11795 s = "lwr";
11796 fmt = MEM12_FMT;
11797 offbits = (mips_opts.micromips ? 12 : 16);
11798 goto ld_st;
11799 case M_LDC1_AB:
11800 s = "ldc1";
11801 fmt = "T,o(b)";
11802 /* Itbl support may require additional care here. */
11803 coproc = 1;
11804 goto ld_st;
11805 case M_LDC2_AB:
11806 s = "ldc2";
11807 fmt = COP12_FMT;
11808 offbits = (mips_opts.micromips ? 12
11809 : ISA_IS_R6 (mips_opts.isa) ? 11
11810 : 16);
11811 /* Itbl support may require additional care here. */
11812 coproc = 1;
11813 goto ld_st;
11814 case M_LQC2_AB:
11815 s = "lqc2";
11816 fmt = "+7,o(b)";
11817 /* Itbl support may require additional care here. */
11818 coproc = 1;
11819 goto ld_st;
11820 case M_LDC3_AB:
11821 s = "ldc3";
11822 fmt = "E,o(b)";
11823 /* Itbl support may require additional care here. */
11824 coproc = 1;
11825 goto ld_st;
11826 case M_LDL_AB:
11827 s = "ldl";
11828 fmt = MEM12_FMT;
11829 offbits = (mips_opts.micromips ? 12 : 16);
11830 goto ld_st;
11831 case M_LDR_AB:
11832 s = "ldr";
11833 fmt = MEM12_FMT;
11834 offbits = (mips_opts.micromips ? 12 : 16);
11835 goto ld_st;
11836 case M_LL_AB:
11837 s = "ll";
11838 fmt = LL_SC_FMT;
11839 offbits = (mips_opts.micromips ? 12
11840 : ISA_IS_R6 (mips_opts.isa) ? 9
11841 : 16);
11842 goto ld;
11843 case M_LLD_AB:
11844 s = "lld";
11845 fmt = LL_SC_FMT;
11846 offbits = (mips_opts.micromips ? 12
11847 : ISA_IS_R6 (mips_opts.isa) ? 9
11848 : 16);
11849 goto ld;
11850 case M_LWU_AB:
11851 s = "lwu";
11852 fmt = MEM12_FMT;
11853 offbits = (mips_opts.micromips ? 12 : 16);
11854 goto ld;
11855 case M_LWP_AB:
11856 gas_assert (mips_opts.micromips);
11857 s = "lwp";
11858 fmt = "t,~(b)";
11859 offbits = 12;
11860 lp = 1;
11861 goto ld;
11862 case M_LDP_AB:
11863 gas_assert (mips_opts.micromips);
11864 s = "ldp";
11865 fmt = "t,~(b)";
11866 offbits = 12;
11867 lp = 1;
11868 goto ld;
11869 case M_LWM_AB:
11870 gas_assert (mips_opts.micromips);
11871 s = "lwm";
11872 fmt = "n,~(b)";
11873 offbits = 12;
11874 goto ld_st;
11875 case M_LDM_AB:
11876 gas_assert (mips_opts.micromips);
11877 s = "ldm";
11878 fmt = "n,~(b)";
11879 offbits = 12;
11880 goto ld_st;
11881
11882 ld:
11883 /* We don't want to use $0 as tempreg. */
11884 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
11885 goto ld_st;
11886 else
11887 tempreg = op[0] + lp;
11888 goto ld_noat;
11889
11890 case M_SB_AB:
11891 s = "sb";
11892 fmt = "t,o(b)";
11893 goto ld_st;
11894 case M_SH_AB:
11895 s = "sh";
11896 fmt = "t,o(b)";
11897 goto ld_st;
11898 case M_SW_AB:
11899 s = "sw";
11900 fmt = "t,o(b)";
11901 goto ld_st;
11902 case M_SWC0_AB:
11903 gas_assert (!mips_opts.micromips);
11904 s = "swc0";
11905 fmt = "E,o(b)";
11906 /* Itbl support may require additional care here. */
11907 coproc = 1;
11908 goto ld_st;
11909 case M_SWC1_AB:
11910 s = "swc1";
11911 fmt = "T,o(b)";
11912 /* Itbl support may require additional care here. */
11913 coproc = 1;
11914 goto ld_st;
11915 case M_SWC2_AB:
11916 s = "swc2";
11917 fmt = COP12_FMT;
11918 offbits = (mips_opts.micromips ? 12
11919 : ISA_IS_R6 (mips_opts.isa) ? 11
11920 : 16);
11921 /* Itbl support may require additional care here. */
11922 coproc = 1;
11923 goto ld_st;
11924 case M_SWC3_AB:
11925 gas_assert (!mips_opts.micromips);
11926 s = "swc3";
11927 fmt = "E,o(b)";
11928 /* Itbl support may require additional care here. */
11929 coproc = 1;
11930 goto ld_st;
11931 case M_SWL_AB:
11932 s = "swl";
11933 fmt = MEM12_FMT;
11934 offbits = (mips_opts.micromips ? 12 : 16);
11935 goto ld_st;
11936 case M_SWR_AB:
11937 s = "swr";
11938 fmt = MEM12_FMT;
11939 offbits = (mips_opts.micromips ? 12 : 16);
11940 goto ld_st;
11941 case M_SC_AB:
11942 s = "sc";
11943 fmt = LL_SC_FMT;
11944 offbits = (mips_opts.micromips ? 12
11945 : ISA_IS_R6 (mips_opts.isa) ? 9
11946 : 16);
11947 goto ld_st;
11948 case M_SCD_AB:
11949 s = "scd";
11950 fmt = LL_SC_FMT;
11951 offbits = (mips_opts.micromips ? 12
11952 : ISA_IS_R6 (mips_opts.isa) ? 9
11953 : 16);
11954 goto ld_st;
11955 case M_CACHE_AB:
11956 s = "cache";
11957 fmt = (mips_opts.micromips ? "k,~(b)"
11958 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11959 : "k,o(b)");
11960 offbits = (mips_opts.micromips ? 12
11961 : ISA_IS_R6 (mips_opts.isa) ? 9
11962 : 16);
11963 goto ld_st;
11964 case M_CACHEE_AB:
11965 s = "cachee";
11966 fmt = "k,+j(b)";
11967 offbits = 9;
11968 goto ld_st;
11969 case M_PREF_AB:
11970 s = "pref";
11971 fmt = (mips_opts.micromips ? "k,~(b)"
11972 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11973 : "k,o(b)");
11974 offbits = (mips_opts.micromips ? 12
11975 : ISA_IS_R6 (mips_opts.isa) ? 9
11976 : 16);
11977 goto ld_st;
11978 case M_PREFE_AB:
11979 s = "prefe";
11980 fmt = "k,+j(b)";
11981 offbits = 9;
11982 goto ld_st;
11983 case M_SDC1_AB:
11984 s = "sdc1";
11985 fmt = "T,o(b)";
11986 coproc = 1;
11987 /* Itbl support may require additional care here. */
11988 goto ld_st;
11989 case M_SDC2_AB:
11990 s = "sdc2";
11991 fmt = COP12_FMT;
11992 offbits = (mips_opts.micromips ? 12
11993 : ISA_IS_R6 (mips_opts.isa) ? 11
11994 : 16);
11995 /* Itbl support may require additional care here. */
11996 coproc = 1;
11997 goto ld_st;
11998 case M_SQC2_AB:
11999 s = "sqc2";
12000 fmt = "+7,o(b)";
12001 /* Itbl support may require additional care here. */
12002 coproc = 1;
12003 goto ld_st;
12004 case M_SDC3_AB:
12005 gas_assert (!mips_opts.micromips);
12006 s = "sdc3";
12007 fmt = "E,o(b)";
12008 /* Itbl support may require additional care here. */
12009 coproc = 1;
12010 goto ld_st;
12011 case M_SDL_AB:
12012 s = "sdl";
12013 fmt = MEM12_FMT;
12014 offbits = (mips_opts.micromips ? 12 : 16);
12015 goto ld_st;
12016 case M_SDR_AB:
12017 s = "sdr";
12018 fmt = MEM12_FMT;
12019 offbits = (mips_opts.micromips ? 12 : 16);
12020 goto ld_st;
12021 case M_SWP_AB:
12022 gas_assert (mips_opts.micromips);
12023 s = "swp";
12024 fmt = "t,~(b)";
12025 offbits = 12;
12026 goto ld_st;
12027 case M_SDP_AB:
12028 gas_assert (mips_opts.micromips);
12029 s = "sdp";
12030 fmt = "t,~(b)";
12031 offbits = 12;
12032 goto ld_st;
12033 case M_SWM_AB:
12034 gas_assert (mips_opts.micromips);
12035 s = "swm";
12036 fmt = "n,~(b)";
12037 offbits = 12;
12038 goto ld_st;
12039 case M_SDM_AB:
12040 gas_assert (mips_opts.micromips);
12041 s = "sdm";
12042 fmt = "n,~(b)";
12043 offbits = 12;
12044
12045 ld_st:
12046 tempreg = AT;
12047 ld_noat:
12048 breg = op[2];
12049 if (small_offset_p (0, align, 16))
12050 {
12051 /* The first case exists for M_LD_AB and M_SD_AB, which are
12052 macros for o32 but which should act like normal instructions
12053 otherwise. */
12054 if (offbits == 16)
12055 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
12056 offset_reloc[1], offset_reloc[2], breg);
12057 else if (small_offset_p (0, align, offbits))
12058 {
12059 if (offbits == 0)
12060 macro_build (NULL, s, fmt, op[0], breg);
12061 else
12062 macro_build (NULL, s, fmt, op[0],
12063 (int) offset_expr.X_add_number, breg);
12064 }
12065 else
12066 {
12067 if (tempreg == AT)
12068 used_at = 1;
12069 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12070 tempreg, breg, -1, offset_reloc[0],
12071 offset_reloc[1], offset_reloc[2]);
12072 if (offbits == 0)
12073 macro_build (NULL, s, fmt, op[0], tempreg);
12074 else
12075 macro_build (NULL, s, fmt, op[0], 0, tempreg);
12076 }
12077 break;
12078 }
12079
12080 if (tempreg == AT)
12081 used_at = 1;
12082
12083 if (offset_expr.X_op != O_constant
12084 && offset_expr.X_op != O_symbol)
12085 {
12086 as_bad (_("expression too complex"));
12087 offset_expr.X_op = O_constant;
12088 }
12089
12090 if (HAVE_32BIT_ADDRESSES
12091 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12092 {
12093 char value [32];
12094
12095 sprintf_vma (value, offset_expr.X_add_number);
12096 as_bad (_("number (0x%s) larger than 32 bits"), value);
12097 }
12098
12099 /* A constant expression in PIC code can be handled just as it
12100 is in non PIC code. */
12101 if (offset_expr.X_op == O_constant)
12102 {
12103 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12104 offbits == 0 ? 16 : offbits);
12105 offset_expr.X_add_number -= expr1.X_add_number;
12106
12107 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12108 if (breg != 0)
12109 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12110 tempreg, tempreg, breg);
12111 if (offbits == 0)
12112 {
12113 if (offset_expr.X_add_number != 0)
12114 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
12115 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
12116 macro_build (NULL, s, fmt, op[0], tempreg);
12117 }
12118 else if (offbits == 16)
12119 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12120 else
12121 macro_build (NULL, s, fmt, op[0],
12122 (int) offset_expr.X_add_number, tempreg);
12123 }
12124 else if (offbits != 16)
12125 {
12126 /* The offset field is too narrow to be used for a low-part
12127 relocation, so load the whole address into the auxiliary
12128 register. */
12129 load_address (tempreg, &offset_expr, &used_at);
12130 if (breg != 0)
12131 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12132 tempreg, tempreg, breg);
12133 if (offbits == 0)
12134 macro_build (NULL, s, fmt, op[0], tempreg);
12135 else
12136 macro_build (NULL, s, fmt, op[0], 0, tempreg);
12137 }
12138 else if (mips_pic == NO_PIC)
12139 {
12140 /* If this is a reference to a GP relative symbol, and there
12141 is no base register, we want
12142 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12143 Otherwise, if there is no base register, we want
12144 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12145 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12146 If we have a constant, we need two instructions anyhow,
12147 so we always use the latter form.
12148
12149 If we have a base register, and this is a reference to a
12150 GP relative symbol, we want
12151 addu $tempreg,$breg,$gp
12152 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
12153 Otherwise we want
12154 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12155 addu $tempreg,$tempreg,$breg
12156 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12157 With a constant we always use the latter case.
12158
12159 With 64bit address space and no base register and $at usable,
12160 we want
12161 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12162 lui $at,<sym> (BFD_RELOC_HI16_S)
12163 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12164 dsll32 $tempreg,0
12165 daddu $tempreg,$at
12166 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12167 If we have a base register, we want
12168 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12169 lui $at,<sym> (BFD_RELOC_HI16_S)
12170 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12171 daddu $at,$breg
12172 dsll32 $tempreg,0
12173 daddu $tempreg,$at
12174 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12175
12176 Without $at we can't generate the optimal path for superscalar
12177 processors here since this would require two temporary registers.
12178 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12179 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12180 dsll $tempreg,16
12181 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12182 dsll $tempreg,16
12183 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12184 If we have a base register, we want
12185 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12186 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12187 dsll $tempreg,16
12188 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12189 dsll $tempreg,16
12190 daddu $tempreg,$tempreg,$breg
12191 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12192
12193 For GP relative symbols in 64bit address space we can use
12194 the same sequence as in 32bit address space. */
12195 if (HAVE_64BIT_SYMBOLS)
12196 {
12197 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12198 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12199 {
12200 relax_start (offset_expr.X_add_symbol);
12201 if (breg == 0)
12202 {
12203 macro_build (&offset_expr, s, fmt, op[0],
12204 BFD_RELOC_GPREL16, mips_gp_register);
12205 }
12206 else
12207 {
12208 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12209 tempreg, breg, mips_gp_register);
12210 macro_build (&offset_expr, s, fmt, op[0],
12211 BFD_RELOC_GPREL16, tempreg);
12212 }
12213 relax_switch ();
12214 }
12215
12216 if (used_at == 0 && mips_opts.at)
12217 {
12218 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12219 BFD_RELOC_MIPS_HIGHEST);
12220 macro_build (&offset_expr, "lui", LUI_FMT, AT,
12221 BFD_RELOC_HI16_S);
12222 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12223 tempreg, BFD_RELOC_MIPS_HIGHER);
12224 if (breg != 0)
12225 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
12226 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
12227 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
12228 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
12229 tempreg);
12230 used_at = 1;
12231 }
12232 else
12233 {
12234 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12235 BFD_RELOC_MIPS_HIGHEST);
12236 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12237 tempreg, BFD_RELOC_MIPS_HIGHER);
12238 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12239 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12240 tempreg, BFD_RELOC_HI16_S);
12241 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12242 if (breg != 0)
12243 macro_build (NULL, "daddu", "d,v,t",
12244 tempreg, tempreg, breg);
12245 macro_build (&offset_expr, s, fmt, op[0],
12246 BFD_RELOC_LO16, tempreg);
12247 }
12248
12249 if (mips_relax.sequence)
12250 relax_end ();
12251 break;
12252 }
12253
12254 if (breg == 0)
12255 {
12256 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12257 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12258 {
12259 relax_start (offset_expr.X_add_symbol);
12260 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
12261 mips_gp_register);
12262 relax_switch ();
12263 }
12264 macro_build_lui (&offset_expr, tempreg);
12265 macro_build (&offset_expr, s, fmt, op[0],
12266 BFD_RELOC_LO16, tempreg);
12267 if (mips_relax.sequence)
12268 relax_end ();
12269 }
12270 else
12271 {
12272 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12273 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12274 {
12275 relax_start (offset_expr.X_add_symbol);
12276 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12277 tempreg, breg, mips_gp_register);
12278 macro_build (&offset_expr, s, fmt, op[0],
12279 BFD_RELOC_GPREL16, tempreg);
12280 relax_switch ();
12281 }
12282 macro_build_lui (&offset_expr, tempreg);
12283 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12284 tempreg, tempreg, breg);
12285 macro_build (&offset_expr, s, fmt, op[0],
12286 BFD_RELOC_LO16, tempreg);
12287 if (mips_relax.sequence)
12288 relax_end ();
12289 }
12290 }
12291 else if (!mips_big_got)
12292 {
12293 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
12294
12295 /* If this is a reference to an external symbol, we want
12296 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12297 nop
12298 <op> op[0],0($tempreg)
12299 Otherwise we want
12300 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12301 nop
12302 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12303 <op> op[0],0($tempreg)
12304
12305 For NewABI, we want
12306 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12307 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
12308
12309 If there is a base register, we add it to $tempreg before
12310 the <op>. If there is a constant, we stick it in the
12311 <op> instruction. We don't handle constants larger than
12312 16 bits, because we have no way to load the upper 16 bits
12313 (actually, we could handle them for the subset of cases
12314 in which we are not using $at). */
12315 gas_assert (offset_expr.X_op == O_symbol);
12316 if (HAVE_NEWABI)
12317 {
12318 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12319 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12320 if (breg != 0)
12321 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12322 tempreg, tempreg, breg);
12323 macro_build (&offset_expr, s, fmt, op[0],
12324 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12325 break;
12326 }
12327 expr1.X_add_number = offset_expr.X_add_number;
12328 offset_expr.X_add_number = 0;
12329 if (expr1.X_add_number < -0x8000
12330 || expr1.X_add_number >= 0x8000)
12331 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12332 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12333 lw_reloc_type, mips_gp_register);
12334 load_delay_nop ();
12335 relax_start (offset_expr.X_add_symbol);
12336 relax_switch ();
12337 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12338 tempreg, BFD_RELOC_LO16);
12339 relax_end ();
12340 if (breg != 0)
12341 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12342 tempreg, tempreg, breg);
12343 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12344 }
12345 else if (mips_big_got && !HAVE_NEWABI)
12346 {
12347 int gpdelay;
12348
12349 /* If this is a reference to an external symbol, we want
12350 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12351 addu $tempreg,$tempreg,$gp
12352 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12353 <op> op[0],0($tempreg)
12354 Otherwise we want
12355 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12356 nop
12357 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12358 <op> op[0],0($tempreg)
12359 If there is a base register, we add it to $tempreg before
12360 the <op>. If there is a constant, we stick it in the
12361 <op> instruction. We don't handle constants larger than
12362 16 bits, because we have no way to load the upper 16 bits
12363 (actually, we could handle them for the subset of cases
12364 in which we are not using $at). */
12365 gas_assert (offset_expr.X_op == O_symbol);
12366 expr1.X_add_number = offset_expr.X_add_number;
12367 offset_expr.X_add_number = 0;
12368 if (expr1.X_add_number < -0x8000
12369 || expr1.X_add_number >= 0x8000)
12370 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12371 gpdelay = reg_needs_delay (mips_gp_register);
12372 relax_start (offset_expr.X_add_symbol);
12373 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12374 BFD_RELOC_MIPS_GOT_HI16);
12375 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12376 mips_gp_register);
12377 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12378 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12379 relax_switch ();
12380 if (gpdelay)
12381 macro_build (NULL, "nop", "");
12382 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12383 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12384 load_delay_nop ();
12385 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12386 tempreg, BFD_RELOC_LO16);
12387 relax_end ();
12388
12389 if (breg != 0)
12390 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12391 tempreg, tempreg, breg);
12392 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12393 }
12394 else if (mips_big_got && HAVE_NEWABI)
12395 {
12396 /* If this is a reference to an external symbol, we want
12397 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12398 add $tempreg,$tempreg,$gp
12399 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12400 <op> op[0],<ofst>($tempreg)
12401 Otherwise, for local symbols, we want:
12402 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12403 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
12404 gas_assert (offset_expr.X_op == O_symbol);
12405 expr1.X_add_number = offset_expr.X_add_number;
12406 offset_expr.X_add_number = 0;
12407 if (expr1.X_add_number < -0x8000
12408 || expr1.X_add_number >= 0x8000)
12409 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12410 relax_start (offset_expr.X_add_symbol);
12411 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12412 BFD_RELOC_MIPS_GOT_HI16);
12413 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12414 mips_gp_register);
12415 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12416 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12417 if (breg != 0)
12418 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12419 tempreg, tempreg, breg);
12420 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12421
12422 relax_switch ();
12423 offset_expr.X_add_number = expr1.X_add_number;
12424 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12425 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12426 if (breg != 0)
12427 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12428 tempreg, tempreg, breg);
12429 macro_build (&offset_expr, s, fmt, op[0],
12430 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12431 relax_end ();
12432 }
12433 else
12434 abort ();
12435
12436 break;
12437
12438 case M_JRADDIUSP:
12439 gas_assert (mips_opts.micromips);
12440 gas_assert (mips_opts.insn32);
12441 start_noreorder ();
12442 macro_build (NULL, "jr", "s", RA);
12443 expr1.X_add_number = op[0] << 2;
12444 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12445 end_noreorder ();
12446 break;
12447
12448 case M_JRC:
12449 gas_assert (mips_opts.micromips);
12450 gas_assert (mips_opts.insn32);
12451 macro_build (NULL, "jr", "s", op[0]);
12452 if (mips_opts.noreorder)
12453 macro_build (NULL, "nop", "");
12454 break;
12455
12456 case M_LI:
12457 case M_LI_S:
12458 load_register (op[0], &imm_expr, 0);
12459 break;
12460
12461 case M_DLI:
12462 load_register (op[0], &imm_expr, 1);
12463 break;
12464
12465 case M_LI_SS:
12466 if (imm_expr.X_op == O_constant)
12467 {
12468 used_at = 1;
12469 load_register (AT, &imm_expr, 0);
12470 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12471 break;
12472 }
12473 else
12474 {
12475 gas_assert (imm_expr.X_op == O_absent
12476 && offset_expr.X_op == O_symbol
12477 && strcmp (segment_name (S_GET_SEGMENT
12478 (offset_expr.X_add_symbol)),
12479 ".lit4") == 0
12480 && offset_expr.X_add_number == 0);
12481 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
12482 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
12483 break;
12484 }
12485
12486 case M_LI_D:
12487 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12488 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12489 order 32 bits of the value and the low order 32 bits are either
12490 zero or in OFFSET_EXPR. */
12491 if (imm_expr.X_op == O_constant)
12492 {
12493 if (GPR_SIZE == 64)
12494 load_register (op[0], &imm_expr, 1);
12495 else
12496 {
12497 int hreg, lreg;
12498
12499 if (target_big_endian)
12500 {
12501 hreg = op[0];
12502 lreg = op[0] + 1;
12503 }
12504 else
12505 {
12506 hreg = op[0] + 1;
12507 lreg = op[0];
12508 }
12509
12510 if (hreg <= 31)
12511 load_register (hreg, &imm_expr, 0);
12512 if (lreg <= 31)
12513 {
12514 if (offset_expr.X_op == O_absent)
12515 move_register (lreg, 0);
12516 else
12517 {
12518 gas_assert (offset_expr.X_op == O_constant);
12519 load_register (lreg, &offset_expr, 0);
12520 }
12521 }
12522 }
12523 break;
12524 }
12525 gas_assert (imm_expr.X_op == O_absent);
12526
12527 /* We know that sym is in the .rdata section. First we get the
12528 upper 16 bits of the address. */
12529 if (mips_pic == NO_PIC)
12530 {
12531 macro_build_lui (&offset_expr, AT);
12532 used_at = 1;
12533 }
12534 else
12535 {
12536 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12537 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12538 used_at = 1;
12539 }
12540
12541 /* Now we load the register(s). */
12542 if (GPR_SIZE == 64)
12543 {
12544 used_at = 1;
12545 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12546 BFD_RELOC_LO16, AT);
12547 }
12548 else
12549 {
12550 used_at = 1;
12551 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12552 BFD_RELOC_LO16, AT);
12553 if (op[0] != RA)
12554 {
12555 /* FIXME: How in the world do we deal with the possible
12556 overflow here? */
12557 offset_expr.X_add_number += 4;
12558 macro_build (&offset_expr, "lw", "t,o(b)",
12559 op[0] + 1, BFD_RELOC_LO16, AT);
12560 }
12561 }
12562 break;
12563
12564 case M_LI_DD:
12565 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12566 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12567 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12568 the value and the low order 32 bits are either zero or in
12569 OFFSET_EXPR. */
12570 if (imm_expr.X_op == O_constant)
12571 {
12572 used_at = 1;
12573 load_register (AT, &imm_expr, FPR_SIZE == 64);
12574 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12575 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
12576 else
12577 {
12578 if (ISA_HAS_MXHC1 (mips_opts.isa))
12579 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12580 else if (FPR_SIZE != 32)
12581 as_bad (_("Unable to generate `%s' compliant code "
12582 "without mthc1"),
12583 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12584 else
12585 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
12586 if (offset_expr.X_op == O_absent)
12587 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
12588 else
12589 {
12590 gas_assert (offset_expr.X_op == O_constant);
12591 load_register (AT, &offset_expr, 0);
12592 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12593 }
12594 }
12595 break;
12596 }
12597
12598 gas_assert (imm_expr.X_op == O_absent
12599 && offset_expr.X_op == O_symbol
12600 && offset_expr.X_add_number == 0);
12601 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12602 if (strcmp (s, ".lit8") == 0)
12603 {
12604 op[2] = mips_gp_register;
12605 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12606 offset_reloc[1] = BFD_RELOC_UNUSED;
12607 offset_reloc[2] = BFD_RELOC_UNUSED;
12608 }
12609 else
12610 {
12611 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
12612 used_at = 1;
12613 if (mips_pic != NO_PIC)
12614 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12615 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12616 else
12617 {
12618 /* FIXME: This won't work for a 64 bit address. */
12619 macro_build_lui (&offset_expr, AT);
12620 }
12621
12622 op[2] = AT;
12623 offset_reloc[0] = BFD_RELOC_LO16;
12624 offset_reloc[1] = BFD_RELOC_UNUSED;
12625 offset_reloc[2] = BFD_RELOC_UNUSED;
12626 }
12627 align = 8;
12628 /* Fall through */
12629
12630 case M_L_DAB:
12631 /*
12632 * The MIPS assembler seems to check for X_add_number not
12633 * being double aligned and generating:
12634 * lui at,%hi(foo+1)
12635 * addu at,at,v1
12636 * addiu at,at,%lo(foo+1)
12637 * lwc1 f2,0(at)
12638 * lwc1 f3,4(at)
12639 * But, the resulting address is the same after relocation so why
12640 * generate the extra instruction?
12641 */
12642 /* Itbl support may require additional care here. */
12643 coproc = 1;
12644 fmt = "T,o(b)";
12645 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12646 {
12647 s = "ldc1";
12648 goto ld_st;
12649 }
12650 s = "lwc1";
12651 goto ldd_std;
12652
12653 case M_S_DAB:
12654 gas_assert (!mips_opts.micromips);
12655 /* Itbl support may require additional care here. */
12656 coproc = 1;
12657 fmt = "T,o(b)";
12658 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12659 {
12660 s = "sdc1";
12661 goto ld_st;
12662 }
12663 s = "swc1";
12664 goto ldd_std;
12665
12666 case M_LQ_AB:
12667 fmt = "t,o(b)";
12668 s = "lq";
12669 goto ld;
12670
12671 case M_SQ_AB:
12672 fmt = "t,o(b)";
12673 s = "sq";
12674 goto ld_st;
12675
12676 case M_LD_AB:
12677 fmt = "t,o(b)";
12678 if (GPR_SIZE == 64)
12679 {
12680 s = "ld";
12681 goto ld;
12682 }
12683 s = "lw";
12684 goto ldd_std;
12685
12686 case M_SD_AB:
12687 fmt = "t,o(b)";
12688 if (GPR_SIZE == 64)
12689 {
12690 s = "sd";
12691 goto ld_st;
12692 }
12693 s = "sw";
12694
12695 ldd_std:
12696 /* Even on a big endian machine $fn comes before $fn+1. We have
12697 to adjust when loading from memory. We set coproc if we must
12698 load $fn+1 first. */
12699 /* Itbl support may require additional care here. */
12700 if (!target_big_endian)
12701 coproc = 0;
12702
12703 breg = op[2];
12704 if (small_offset_p (0, align, 16))
12705 {
12706 ep = &offset_expr;
12707 if (!small_offset_p (4, align, 16))
12708 {
12709 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12710 -1, offset_reloc[0], offset_reloc[1],
12711 offset_reloc[2]);
12712 expr1.X_add_number = 0;
12713 ep = &expr1;
12714 breg = AT;
12715 used_at = 1;
12716 offset_reloc[0] = BFD_RELOC_LO16;
12717 offset_reloc[1] = BFD_RELOC_UNUSED;
12718 offset_reloc[2] = BFD_RELOC_UNUSED;
12719 }
12720 if (strcmp (s, "lw") == 0 && op[0] == breg)
12721 {
12722 ep->X_add_number += 4;
12723 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
12724 offset_reloc[1], offset_reloc[2], breg);
12725 ep->X_add_number -= 4;
12726 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
12727 offset_reloc[1], offset_reloc[2], breg);
12728 }
12729 else
12730 {
12731 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
12732 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12733 breg);
12734 ep->X_add_number += 4;
12735 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
12736 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12737 breg);
12738 }
12739 break;
12740 }
12741
12742 if (offset_expr.X_op != O_symbol
12743 && offset_expr.X_op != O_constant)
12744 {
12745 as_bad (_("expression too complex"));
12746 offset_expr.X_op = O_constant;
12747 }
12748
12749 if (HAVE_32BIT_ADDRESSES
12750 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12751 {
12752 char value [32];
12753
12754 sprintf_vma (value, offset_expr.X_add_number);
12755 as_bad (_("number (0x%s) larger than 32 bits"), value);
12756 }
12757
12758 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
12759 {
12760 /* If this is a reference to a GP relative symbol, we want
12761 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12762 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
12763 If we have a base register, we use this
12764 addu $at,$breg,$gp
12765 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12766 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
12767 If this is not a GP relative symbol, we want
12768 lui $at,<sym> (BFD_RELOC_HI16_S)
12769 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12770 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12771 If there is a base register, we add it to $at after the
12772 lui instruction. If there is a constant, we always use
12773 the last case. */
12774 if (offset_expr.X_op == O_symbol
12775 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12776 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12777 {
12778 relax_start (offset_expr.X_add_symbol);
12779 if (breg == 0)
12780 {
12781 tempreg = mips_gp_register;
12782 }
12783 else
12784 {
12785 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12786 AT, breg, mips_gp_register);
12787 tempreg = AT;
12788 used_at = 1;
12789 }
12790
12791 /* Itbl support may require additional care here. */
12792 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12793 BFD_RELOC_GPREL16, tempreg);
12794 offset_expr.X_add_number += 4;
12795
12796 /* Set mips_optimize to 2 to avoid inserting an
12797 undesired nop. */
12798 hold_mips_optimize = mips_optimize;
12799 mips_optimize = 2;
12800 /* Itbl support may require additional care here. */
12801 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12802 BFD_RELOC_GPREL16, tempreg);
12803 mips_optimize = hold_mips_optimize;
12804
12805 relax_switch ();
12806
12807 offset_expr.X_add_number -= 4;
12808 }
12809 used_at = 1;
12810 if (offset_high_part (offset_expr.X_add_number, 16)
12811 != offset_high_part (offset_expr.X_add_number + 4, 16))
12812 {
12813 load_address (AT, &offset_expr, &used_at);
12814 offset_expr.X_op = O_constant;
12815 offset_expr.X_add_number = 0;
12816 }
12817 else
12818 macro_build_lui (&offset_expr, AT);
12819 if (breg != 0)
12820 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12821 /* Itbl support may require additional care here. */
12822 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12823 BFD_RELOC_LO16, AT);
12824 /* FIXME: How do we handle overflow here? */
12825 offset_expr.X_add_number += 4;
12826 /* Itbl support may require additional care here. */
12827 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12828 BFD_RELOC_LO16, AT);
12829 if (mips_relax.sequence)
12830 relax_end ();
12831 }
12832 else if (!mips_big_got)
12833 {
12834 /* If this is a reference to an external symbol, we want
12835 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12836 nop
12837 <op> op[0],0($at)
12838 <op> op[0]+1,4($at)
12839 Otherwise we want
12840 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12841 nop
12842 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12843 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12844 If there is a base register we add it to $at before the
12845 lwc1 instructions. If there is a constant we include it
12846 in the lwc1 instructions. */
12847 used_at = 1;
12848 expr1.X_add_number = offset_expr.X_add_number;
12849 if (expr1.X_add_number < -0x8000
12850 || expr1.X_add_number >= 0x8000 - 4)
12851 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12852 load_got_offset (AT, &offset_expr);
12853 load_delay_nop ();
12854 if (breg != 0)
12855 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12856
12857 /* Set mips_optimize to 2 to avoid inserting an undesired
12858 nop. */
12859 hold_mips_optimize = mips_optimize;
12860 mips_optimize = 2;
12861
12862 /* Itbl support may require additional care here. */
12863 relax_start (offset_expr.X_add_symbol);
12864 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12865 BFD_RELOC_LO16, AT);
12866 expr1.X_add_number += 4;
12867 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12868 BFD_RELOC_LO16, AT);
12869 relax_switch ();
12870 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12871 BFD_RELOC_LO16, AT);
12872 offset_expr.X_add_number += 4;
12873 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12874 BFD_RELOC_LO16, AT);
12875 relax_end ();
12876
12877 mips_optimize = hold_mips_optimize;
12878 }
12879 else if (mips_big_got)
12880 {
12881 int gpdelay;
12882
12883 /* If this is a reference to an external symbol, we want
12884 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12885 addu $at,$at,$gp
12886 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12887 nop
12888 <op> op[0],0($at)
12889 <op> op[0]+1,4($at)
12890 Otherwise we want
12891 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12892 nop
12893 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12894 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12895 If there is a base register we add it to $at before the
12896 lwc1 instructions. If there is a constant we include it
12897 in the lwc1 instructions. */
12898 used_at = 1;
12899 expr1.X_add_number = offset_expr.X_add_number;
12900 offset_expr.X_add_number = 0;
12901 if (expr1.X_add_number < -0x8000
12902 || expr1.X_add_number >= 0x8000 - 4)
12903 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12904 gpdelay = reg_needs_delay (mips_gp_register);
12905 relax_start (offset_expr.X_add_symbol);
12906 macro_build (&offset_expr, "lui", LUI_FMT,
12907 AT, BFD_RELOC_MIPS_GOT_HI16);
12908 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12909 AT, AT, mips_gp_register);
12910 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
12911 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
12912 load_delay_nop ();
12913 if (breg != 0)
12914 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12915 /* Itbl support may require additional care here. */
12916 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12917 BFD_RELOC_LO16, AT);
12918 expr1.X_add_number += 4;
12919
12920 /* Set mips_optimize to 2 to avoid inserting an undesired
12921 nop. */
12922 hold_mips_optimize = mips_optimize;
12923 mips_optimize = 2;
12924 /* Itbl support may require additional care here. */
12925 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12926 BFD_RELOC_LO16, AT);
12927 mips_optimize = hold_mips_optimize;
12928 expr1.X_add_number -= 4;
12929
12930 relax_switch ();
12931 offset_expr.X_add_number = expr1.X_add_number;
12932 if (gpdelay)
12933 macro_build (NULL, "nop", "");
12934 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12935 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12936 load_delay_nop ();
12937 if (breg != 0)
12938 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12939 /* Itbl support may require additional care here. */
12940 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12941 BFD_RELOC_LO16, AT);
12942 offset_expr.X_add_number += 4;
12943
12944 /* Set mips_optimize to 2 to avoid inserting an undesired
12945 nop. */
12946 hold_mips_optimize = mips_optimize;
12947 mips_optimize = 2;
12948 /* Itbl support may require additional care here. */
12949 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12950 BFD_RELOC_LO16, AT);
12951 mips_optimize = hold_mips_optimize;
12952 relax_end ();
12953 }
12954 else
12955 abort ();
12956
12957 break;
12958
12959 case M_SAA_AB:
12960 s = "saa";
12961 goto saa_saad;
12962 case M_SAAD_AB:
12963 s = "saad";
12964 saa_saad:
12965 gas_assert (!mips_opts.micromips);
12966 offbits = 0;
12967 fmt = "t,(b)";
12968 goto ld_st;
12969
12970 /* New code added to support COPZ instructions.
12971 This code builds table entries out of the macros in mip_opcodes.
12972 R4000 uses interlocks to handle coproc delays.
12973 Other chips (like the R3000) require nops to be inserted for delays.
12974
12975 FIXME: Currently, we require that the user handle delays.
12976 In order to fill delay slots for non-interlocked chips,
12977 we must have a way to specify delays based on the coprocessor.
12978 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12979 What are the side-effects of the cop instruction?
12980 What cache support might we have and what are its effects?
12981 Both coprocessor & memory require delays. how long???
12982 What registers are read/set/modified?
12983
12984 If an itbl is provided to interpret cop instructions,
12985 this knowledge can be encoded in the itbl spec. */
12986
12987 case M_COP0:
12988 s = "c0";
12989 goto copz;
12990 case M_COP1:
12991 s = "c1";
12992 goto copz;
12993 case M_COP2:
12994 s = "c2";
12995 goto copz;
12996 case M_COP3:
12997 s = "c3";
12998 copz:
12999 gas_assert (!mips_opts.micromips);
13000 /* For now we just do C (same as Cz). The parameter will be
13001 stored in insn_opcode by mips_ip. */
13002 macro_build (NULL, s, "C", (int) ip->insn_opcode);
13003 break;
13004
13005 case M_MOVE:
13006 move_register (op[0], op[1]);
13007 break;
13008
13009 case M_MOVEP:
13010 gas_assert (mips_opts.micromips);
13011 gas_assert (mips_opts.insn32);
13012 move_register (micromips_to_32_reg_h_map1[op[0]],
13013 micromips_to_32_reg_m_map[op[1]]);
13014 move_register (micromips_to_32_reg_h_map2[op[0]],
13015 micromips_to_32_reg_n_map[op[2]]);
13016 break;
13017
13018 case M_DMUL:
13019 dbl = 1;
13020 /* Fall through. */
13021 case M_MUL:
13022 if (mips_opts.arch == CPU_R5900)
13023 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13024 op[2]);
13025 else
13026 {
13027 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13028 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13029 }
13030 break;
13031
13032 case M_DMUL_I:
13033 dbl = 1;
13034 /* Fall through. */
13035 case M_MUL_I:
13036 /* The MIPS assembler some times generates shifts and adds. I'm
13037 not trying to be that fancy. GCC should do this for us
13038 anyway. */
13039 used_at = 1;
13040 load_register (AT, &imm_expr, dbl);
13041 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13042 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13043 break;
13044
13045 case M_DMULO_I:
13046 dbl = 1;
13047 /* Fall through. */
13048 case M_MULO_I:
13049 imm = 1;
13050 goto do_mulo;
13051
13052 case M_DMULO:
13053 dbl = 1;
13054 /* Fall through. */
13055 case M_MULO:
13056 do_mulo:
13057 start_noreorder ();
13058 used_at = 1;
13059 if (imm)
13060 load_register (AT, &imm_expr, dbl);
13061 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13062 op[1], imm ? AT : op[2]);
13063 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13064 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
13065 macro_build (NULL, "mfhi", MFHL_FMT, AT);
13066 if (mips_trap)
13067 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
13068 else
13069 {
13070 if (mips_opts.micromips)
13071 micromips_label_expr (&label_expr);
13072 else
13073 label_expr.X_add_number = 8;
13074 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
13075 macro_build (NULL, "nop", "");
13076 macro_build (NULL, "break", BRK_FMT, 6);
13077 if (mips_opts.micromips)
13078 micromips_add_label ();
13079 }
13080 end_noreorder ();
13081 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13082 break;
13083
13084 case M_DMULOU_I:
13085 dbl = 1;
13086 /* Fall through. */
13087 case M_MULOU_I:
13088 imm = 1;
13089 goto do_mulou;
13090
13091 case M_DMULOU:
13092 dbl = 1;
13093 /* Fall through. */
13094 case M_MULOU:
13095 do_mulou:
13096 start_noreorder ();
13097 used_at = 1;
13098 if (imm)
13099 load_register (AT, &imm_expr, dbl);
13100 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
13101 op[1], imm ? AT : op[2]);
13102 macro_build (NULL, "mfhi", MFHL_FMT, AT);
13103 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13104 if (mips_trap)
13105 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
13106 else
13107 {
13108 if (mips_opts.micromips)
13109 micromips_label_expr (&label_expr);
13110 else
13111 label_expr.X_add_number = 8;
13112 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
13113 macro_build (NULL, "nop", "");
13114 macro_build (NULL, "break", BRK_FMT, 6);
13115 if (mips_opts.micromips)
13116 micromips_add_label ();
13117 }
13118 end_noreorder ();
13119 break;
13120
13121 case M_DROL:
13122 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13123 {
13124 if (op[0] == op[1])
13125 {
13126 tempreg = AT;
13127 used_at = 1;
13128 }
13129 else
13130 tempreg = op[0];
13131 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13132 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
13133 break;
13134 }
13135 used_at = 1;
13136 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13137 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13138 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13139 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13140 break;
13141
13142 case M_ROL:
13143 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13144 {
13145 if (op[0] == op[1])
13146 {
13147 tempreg = AT;
13148 used_at = 1;
13149 }
13150 else
13151 tempreg = op[0];
13152 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13153 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
13154 break;
13155 }
13156 used_at = 1;
13157 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13158 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13159 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13160 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13161 break;
13162
13163 case M_DROL_I:
13164 {
13165 unsigned int rot;
13166 const char *l;
13167 const char *rr;
13168
13169 rot = imm_expr.X_add_number & 0x3f;
13170 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13171 {
13172 rot = (64 - rot) & 0x3f;
13173 if (rot >= 32)
13174 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13175 else
13176 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13177 break;
13178 }
13179 if (rot == 0)
13180 {
13181 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13182 break;
13183 }
13184 l = (rot < 0x20) ? "dsll" : "dsll32";
13185 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
13186 rot &= 0x1f;
13187 used_at = 1;
13188 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13189 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13190 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13191 }
13192 break;
13193
13194 case M_ROL_I:
13195 {
13196 unsigned int rot;
13197
13198 rot = imm_expr.X_add_number & 0x1f;
13199 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13200 {
13201 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13202 (32 - rot) & 0x1f);
13203 break;
13204 }
13205 if (rot == 0)
13206 {
13207 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13208 break;
13209 }
13210 used_at = 1;
13211 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13212 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13213 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13214 }
13215 break;
13216
13217 case M_DROR:
13218 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13219 {
13220 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
13221 break;
13222 }
13223 used_at = 1;
13224 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13225 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13226 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13227 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13228 break;
13229
13230 case M_ROR:
13231 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13232 {
13233 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
13234 break;
13235 }
13236 used_at = 1;
13237 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13238 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13239 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13240 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13241 break;
13242
13243 case M_DROR_I:
13244 {
13245 unsigned int rot;
13246 const char *l;
13247 const char *rr;
13248
13249 rot = imm_expr.X_add_number & 0x3f;
13250 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13251 {
13252 if (rot >= 32)
13253 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13254 else
13255 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13256 break;
13257 }
13258 if (rot == 0)
13259 {
13260 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13261 break;
13262 }
13263 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
13264 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13265 rot &= 0x1f;
13266 used_at = 1;
13267 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13268 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13269 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13270 }
13271 break;
13272
13273 case M_ROR_I:
13274 {
13275 unsigned int rot;
13276
13277 rot = imm_expr.X_add_number & 0x1f;
13278 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13279 {
13280 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
13281 break;
13282 }
13283 if (rot == 0)
13284 {
13285 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13286 break;
13287 }
13288 used_at = 1;
13289 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13290 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13291 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13292 }
13293 break;
13294
13295 case M_SEQ:
13296 if (op[1] == 0)
13297 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13298 else if (op[2] == 0)
13299 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13300 else
13301 {
13302 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13303 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13304 }
13305 break;
13306
13307 case M_SEQ_I:
13308 if (imm_expr.X_add_number == 0)
13309 {
13310 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13311 break;
13312 }
13313 if (op[1] == 0)
13314 {
13315 as_warn (_("instruction %s: result is always false"),
13316 ip->insn_mo->name);
13317 move_register (op[0], 0);
13318 break;
13319 }
13320 if (CPU_HAS_SEQ (mips_opts.arch)
13321 && -512 <= imm_expr.X_add_number
13322 && imm_expr.X_add_number < 512)
13323 {
13324 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
13325 (int) imm_expr.X_add_number);
13326 break;
13327 }
13328 if (imm_expr.X_add_number >= 0
13329 && imm_expr.X_add_number < 0x10000)
13330 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
13331 else if (imm_expr.X_add_number > -0x8000
13332 && imm_expr.X_add_number < 0)
13333 {
13334 imm_expr.X_add_number = -imm_expr.X_add_number;
13335 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13336 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13337 }
13338 else if (CPU_HAS_SEQ (mips_opts.arch))
13339 {
13340 used_at = 1;
13341 load_register (AT, &imm_expr, GPR_SIZE == 64);
13342 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
13343 break;
13344 }
13345 else
13346 {
13347 load_register (AT, &imm_expr, GPR_SIZE == 64);
13348 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13349 used_at = 1;
13350 }
13351 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13352 break;
13353
13354 case M_SGE: /* X >= Y <==> not (X < Y) */
13355 s = "slt";
13356 goto sge;
13357 case M_SGEU:
13358 s = "sltu";
13359 sge:
13360 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13361 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13362 break;
13363
13364 case M_SGE_I: /* X >= I <==> not (X < I) */
13365 case M_SGEU_I:
13366 if (imm_expr.X_add_number >= -0x8000
13367 && imm_expr.X_add_number < 0x8000)
13368 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13369 op[0], op[1], BFD_RELOC_LO16);
13370 else
13371 {
13372 load_register (AT, &imm_expr, GPR_SIZE == 64);
13373 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
13374 op[0], op[1], AT);
13375 used_at = 1;
13376 }
13377 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13378 break;
13379
13380 case M_SGT: /* X > Y <==> Y < X */
13381 s = "slt";
13382 goto sgt;
13383 case M_SGTU:
13384 s = "sltu";
13385 sgt:
13386 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13387 break;
13388
13389 case M_SGT_I: /* X > I <==> I < X */
13390 s = "slt";
13391 goto sgti;
13392 case M_SGTU_I:
13393 s = "sltu";
13394 sgti:
13395 used_at = 1;
13396 load_register (AT, &imm_expr, GPR_SIZE == 64);
13397 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13398 break;
13399
13400 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
13401 s = "slt";
13402 goto sle;
13403 case M_SLEU:
13404 s = "sltu";
13405 sle:
13406 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13407 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13408 break;
13409
13410 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
13411 s = "slt";
13412 goto slei;
13413 case M_SLEU_I:
13414 s = "sltu";
13415 slei:
13416 used_at = 1;
13417 load_register (AT, &imm_expr, GPR_SIZE == 64);
13418 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13419 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13420 break;
13421
13422 case M_SLT_I:
13423 if (imm_expr.X_add_number >= -0x8000
13424 && imm_expr.X_add_number < 0x8000)
13425 {
13426 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13427 BFD_RELOC_LO16);
13428 break;
13429 }
13430 used_at = 1;
13431 load_register (AT, &imm_expr, GPR_SIZE == 64);
13432 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
13433 break;
13434
13435 case M_SLTU_I:
13436 if (imm_expr.X_add_number >= -0x8000
13437 && imm_expr.X_add_number < 0x8000)
13438 {
13439 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
13440 BFD_RELOC_LO16);
13441 break;
13442 }
13443 used_at = 1;
13444 load_register (AT, &imm_expr, GPR_SIZE == 64);
13445 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
13446 break;
13447
13448 case M_SNE:
13449 if (op[1] == 0)
13450 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13451 else if (op[2] == 0)
13452 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13453 else
13454 {
13455 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13456 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13457 }
13458 break;
13459
13460 case M_SNE_I:
13461 if (imm_expr.X_add_number == 0)
13462 {
13463 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13464 break;
13465 }
13466 if (op[1] == 0)
13467 {
13468 as_warn (_("instruction %s: result is always true"),
13469 ip->insn_mo->name);
13470 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
13471 op[0], 0, BFD_RELOC_LO16);
13472 break;
13473 }
13474 if (CPU_HAS_SEQ (mips_opts.arch)
13475 && -512 <= imm_expr.X_add_number
13476 && imm_expr.X_add_number < 512)
13477 {
13478 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
13479 (int) imm_expr.X_add_number);
13480 break;
13481 }
13482 if (imm_expr.X_add_number >= 0
13483 && imm_expr.X_add_number < 0x10000)
13484 {
13485 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13486 BFD_RELOC_LO16);
13487 }
13488 else if (imm_expr.X_add_number > -0x8000
13489 && imm_expr.X_add_number < 0)
13490 {
13491 imm_expr.X_add_number = -imm_expr.X_add_number;
13492 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13493 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13494 }
13495 else if (CPU_HAS_SEQ (mips_opts.arch))
13496 {
13497 used_at = 1;
13498 load_register (AT, &imm_expr, GPR_SIZE == 64);
13499 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
13500 break;
13501 }
13502 else
13503 {
13504 load_register (AT, &imm_expr, GPR_SIZE == 64);
13505 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13506 used_at = 1;
13507 }
13508 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13509 break;
13510
13511 case M_SUB_I:
13512 s = "addi";
13513 s2 = "sub";
13514 goto do_subi;
13515 case M_SUBU_I:
13516 s = "addiu";
13517 s2 = "subu";
13518 goto do_subi;
13519 case M_DSUB_I:
13520 dbl = 1;
13521 s = "daddi";
13522 s2 = "dsub";
13523 if (!mips_opts.micromips)
13524 goto do_subi;
13525 if (imm_expr.X_add_number > -0x200
13526 && imm_expr.X_add_number <= 0x200)
13527 {
13528 macro_build (NULL, s, "t,r,.", op[0], op[1],
13529 (int) -imm_expr.X_add_number);
13530 break;
13531 }
13532 goto do_subi_i;
13533 case M_DSUBU_I:
13534 dbl = 1;
13535 s = "daddiu";
13536 s2 = "dsubu";
13537 do_subi:
13538 if (imm_expr.X_add_number > -0x8000
13539 && imm_expr.X_add_number <= 0x8000)
13540 {
13541 imm_expr.X_add_number = -imm_expr.X_add_number;
13542 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13543 break;
13544 }
13545 do_subi_i:
13546 used_at = 1;
13547 load_register (AT, &imm_expr, dbl);
13548 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
13549 break;
13550
13551 case M_TEQ_I:
13552 s = "teq";
13553 goto trap;
13554 case M_TGE_I:
13555 s = "tge";
13556 goto trap;
13557 case M_TGEU_I:
13558 s = "tgeu";
13559 goto trap;
13560 case M_TLT_I:
13561 s = "tlt";
13562 goto trap;
13563 case M_TLTU_I:
13564 s = "tltu";
13565 goto trap;
13566 case M_TNE_I:
13567 s = "tne";
13568 trap:
13569 used_at = 1;
13570 load_register (AT, &imm_expr, GPR_SIZE == 64);
13571 macro_build (NULL, s, "s,t", op[0], AT);
13572 break;
13573
13574 case M_TRUNCWS:
13575 case M_TRUNCWD:
13576 gas_assert (!mips_opts.micromips);
13577 gas_assert (mips_opts.isa == ISA_MIPS1);
13578 used_at = 1;
13579
13580 /*
13581 * Is the double cfc1 instruction a bug in the mips assembler;
13582 * or is there a reason for it?
13583 */
13584 start_noreorder ();
13585 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13586 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13587 macro_build (NULL, "nop", "");
13588 expr1.X_add_number = 3;
13589 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
13590 expr1.X_add_number = 2;
13591 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13592 macro_build (NULL, "ctc1", "t,G", AT, RA);
13593 macro_build (NULL, "nop", "");
13594 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
13595 op[0], op[1]);
13596 macro_build (NULL, "ctc1", "t,G", op[2], RA);
13597 macro_build (NULL, "nop", "");
13598 end_noreorder ();
13599 break;
13600
13601 case M_ULH_AB:
13602 s = "lb";
13603 s2 = "lbu";
13604 off = 1;
13605 goto uld_st;
13606 case M_ULHU_AB:
13607 s = "lbu";
13608 s2 = "lbu";
13609 off = 1;
13610 goto uld_st;
13611 case M_ULW_AB:
13612 s = "lwl";
13613 s2 = "lwr";
13614 offbits = (mips_opts.micromips ? 12 : 16);
13615 off = 3;
13616 goto uld_st;
13617 case M_ULD_AB:
13618 s = "ldl";
13619 s2 = "ldr";
13620 offbits = (mips_opts.micromips ? 12 : 16);
13621 off = 7;
13622 goto uld_st;
13623 case M_USH_AB:
13624 s = "sb";
13625 s2 = "sb";
13626 off = 1;
13627 ust = 1;
13628 goto uld_st;
13629 case M_USW_AB:
13630 s = "swl";
13631 s2 = "swr";
13632 offbits = (mips_opts.micromips ? 12 : 16);
13633 off = 3;
13634 ust = 1;
13635 goto uld_st;
13636 case M_USD_AB:
13637 s = "sdl";
13638 s2 = "sdr";
13639 offbits = (mips_opts.micromips ? 12 : 16);
13640 off = 7;
13641 ust = 1;
13642
13643 uld_st:
13644 breg = op[2];
13645 large_offset = !small_offset_p (off, align, offbits);
13646 ep = &offset_expr;
13647 expr1.X_add_number = 0;
13648 if (large_offset)
13649 {
13650 used_at = 1;
13651 tempreg = AT;
13652 if (small_offset_p (0, align, 16))
13653 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13654 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13655 else
13656 {
13657 load_address (tempreg, ep, &used_at);
13658 if (breg != 0)
13659 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13660 tempreg, tempreg, breg);
13661 }
13662 offset_reloc[0] = BFD_RELOC_LO16;
13663 offset_reloc[1] = BFD_RELOC_UNUSED;
13664 offset_reloc[2] = BFD_RELOC_UNUSED;
13665 breg = tempreg;
13666 tempreg = op[0];
13667 ep = &expr1;
13668 }
13669 else if (!ust && op[0] == breg)
13670 {
13671 used_at = 1;
13672 tempreg = AT;
13673 }
13674 else
13675 tempreg = op[0];
13676
13677 if (off == 1)
13678 goto ulh_sh;
13679
13680 if (!target_big_endian)
13681 ep->X_add_number += off;
13682 if (offbits == 12)
13683 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
13684 else
13685 macro_build (ep, s, "t,o(b)", tempreg, -1,
13686 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13687
13688 if (!target_big_endian)
13689 ep->X_add_number -= off;
13690 else
13691 ep->X_add_number += off;
13692 if (offbits == 12)
13693 macro_build (NULL, s2, "t,~(b)",
13694 tempreg, (int) ep->X_add_number, breg);
13695 else
13696 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13697 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13698
13699 /* If necessary, move the result in tempreg to the final destination. */
13700 if (!ust && op[0] != tempreg)
13701 {
13702 /* Protect second load's delay slot. */
13703 load_delay_nop ();
13704 move_register (op[0], tempreg);
13705 }
13706 break;
13707
13708 ulh_sh:
13709 used_at = 1;
13710 if (target_big_endian == ust)
13711 ep->X_add_number += off;
13712 tempreg = ust || large_offset ? op[0] : AT;
13713 macro_build (ep, s, "t,o(b)", tempreg, -1,
13714 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13715
13716 /* For halfword transfers we need a temporary register to shuffle
13717 bytes. Unfortunately for M_USH_A we have none available before
13718 the next store as AT holds the base address. We deal with this
13719 case by clobbering TREG and then restoring it as with ULH. */
13720 tempreg = ust == large_offset ? op[0] : AT;
13721 if (ust)
13722 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
13723
13724 if (target_big_endian == ust)
13725 ep->X_add_number -= off;
13726 else
13727 ep->X_add_number += off;
13728 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13729 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13730
13731 /* For M_USH_A re-retrieve the LSB. */
13732 if (ust && large_offset)
13733 {
13734 if (target_big_endian)
13735 ep->X_add_number += off;
13736 else
13737 ep->X_add_number -= off;
13738 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13739 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
13740 }
13741 /* For ULH and M_USH_A OR the LSB in. */
13742 if (!ust || large_offset)
13743 {
13744 tempreg = !large_offset ? AT : op[0];
13745 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
13746 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13747 }
13748 break;
13749
13750 default:
13751 /* FIXME: Check if this is one of the itbl macros, since they
13752 are added dynamically. */
13753 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
13754 break;
13755 }
13756 if (!mips_opts.at && used_at)
13757 as_bad (_("macro used $at after \".set noat\""));
13758 }
13759
13760 /* Implement macros in mips16 mode. */
13761
13762 static void
13763 mips16_macro (struct mips_cl_insn *ip)
13764 {
13765 const struct mips_operand_array *operands;
13766 int mask;
13767 int tmp;
13768 expressionS expr1;
13769 int dbl;
13770 const char *s, *s2, *s3;
13771 unsigned int op[MAX_OPERANDS];
13772 unsigned int i;
13773
13774 mask = ip->insn_mo->mask;
13775
13776 operands = insn_operands (ip);
13777 for (i = 0; i < MAX_OPERANDS; i++)
13778 if (operands->operand[i])
13779 op[i] = insn_extract_operand (ip, operands->operand[i]);
13780 else
13781 op[i] = -1;
13782
13783 expr1.X_op = O_constant;
13784 expr1.X_op_symbol = NULL;
13785 expr1.X_add_symbol = NULL;
13786 expr1.X_add_number = 1;
13787
13788 dbl = 0;
13789
13790 switch (mask)
13791 {
13792 default:
13793 abort ();
13794
13795 case M_DDIV_3:
13796 dbl = 1;
13797 /* Fall through. */
13798 case M_DIV_3:
13799 s = "mflo";
13800 goto do_div3;
13801 case M_DREM_3:
13802 dbl = 1;
13803 /* Fall through. */
13804 case M_REM_3:
13805 s = "mfhi";
13806 do_div3:
13807 start_noreorder ();
13808 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
13809 expr1.X_add_number = 2;
13810 macro_build (&expr1, "bnez", "x,p", op[2]);
13811 macro_build (NULL, "break", "6", 7);
13812
13813 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13814 since that causes an overflow. We should do that as well,
13815 but I don't see how to do the comparisons without a temporary
13816 register. */
13817 end_noreorder ();
13818 macro_build (NULL, s, "x", op[0]);
13819 break;
13820
13821 case M_DIVU_3:
13822 s = "divu";
13823 s2 = "mflo";
13824 goto do_divu3;
13825 case M_REMU_3:
13826 s = "divu";
13827 s2 = "mfhi";
13828 goto do_divu3;
13829 case M_DDIVU_3:
13830 s = "ddivu";
13831 s2 = "mflo";
13832 goto do_divu3;
13833 case M_DREMU_3:
13834 s = "ddivu";
13835 s2 = "mfhi";
13836 do_divu3:
13837 start_noreorder ();
13838 macro_build (NULL, s, ".,x,y", op[1], op[2]);
13839 expr1.X_add_number = 2;
13840 macro_build (&expr1, "bnez", "x,p", op[2]);
13841 macro_build (NULL, "break", "6", 7);
13842 end_noreorder ();
13843 macro_build (NULL, s2, "x", op[0]);
13844 break;
13845
13846 case M_DMUL:
13847 dbl = 1;
13848 /* Fall through. */
13849 case M_MUL:
13850 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13851 macro_build (NULL, "mflo", "x", op[0]);
13852 break;
13853
13854 case M_DSUBU_I:
13855 dbl = 1;
13856 goto do_subu;
13857 case M_SUBU_I:
13858 do_subu:
13859 imm_expr.X_add_number = -imm_expr.X_add_number;
13860 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
13861 break;
13862
13863 case M_SUBU_I_2:
13864 imm_expr.X_add_number = -imm_expr.X_add_number;
13865 macro_build (&imm_expr, "addiu", "x,k", op[0]);
13866 break;
13867
13868 case M_DSUBU_I_2:
13869 imm_expr.X_add_number = -imm_expr.X_add_number;
13870 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
13871 break;
13872
13873 case M_BEQ:
13874 s = "cmp";
13875 s2 = "bteqz";
13876 goto do_branch;
13877 case M_BNE:
13878 s = "cmp";
13879 s2 = "btnez";
13880 goto do_branch;
13881 case M_BLT:
13882 s = "slt";
13883 s2 = "btnez";
13884 goto do_branch;
13885 case M_BLTU:
13886 s = "sltu";
13887 s2 = "btnez";
13888 goto do_branch;
13889 case M_BLE:
13890 s = "slt";
13891 s2 = "bteqz";
13892 goto do_reverse_branch;
13893 case M_BLEU:
13894 s = "sltu";
13895 s2 = "bteqz";
13896 goto do_reverse_branch;
13897 case M_BGE:
13898 s = "slt";
13899 s2 = "bteqz";
13900 goto do_branch;
13901 case M_BGEU:
13902 s = "sltu";
13903 s2 = "bteqz";
13904 goto do_branch;
13905 case M_BGT:
13906 s = "slt";
13907 s2 = "btnez";
13908 goto do_reverse_branch;
13909 case M_BGTU:
13910 s = "sltu";
13911 s2 = "btnez";
13912
13913 do_reverse_branch:
13914 tmp = op[1];
13915 op[1] = op[0];
13916 op[0] = tmp;
13917
13918 do_branch:
13919 macro_build (NULL, s, "x,y", op[0], op[1]);
13920 macro_build (&offset_expr, s2, "p");
13921 break;
13922
13923 case M_BEQ_I:
13924 s = "cmpi";
13925 s2 = "bteqz";
13926 s3 = "x,U";
13927 goto do_branch_i;
13928 case M_BNE_I:
13929 s = "cmpi";
13930 s2 = "btnez";
13931 s3 = "x,U";
13932 goto do_branch_i;
13933 case M_BLT_I:
13934 s = "slti";
13935 s2 = "btnez";
13936 s3 = "x,8";
13937 goto do_branch_i;
13938 case M_BLTU_I:
13939 s = "sltiu";
13940 s2 = "btnez";
13941 s3 = "x,8";
13942 goto do_branch_i;
13943 case M_BLE_I:
13944 s = "slti";
13945 s2 = "btnez";
13946 s3 = "x,8";
13947 goto do_addone_branch_i;
13948 case M_BLEU_I:
13949 s = "sltiu";
13950 s2 = "btnez";
13951 s3 = "x,8";
13952 goto do_addone_branch_i;
13953 case M_BGE_I:
13954 s = "slti";
13955 s2 = "bteqz";
13956 s3 = "x,8";
13957 goto do_branch_i;
13958 case M_BGEU_I:
13959 s = "sltiu";
13960 s2 = "bteqz";
13961 s3 = "x,8";
13962 goto do_branch_i;
13963 case M_BGT_I:
13964 s = "slti";
13965 s2 = "bteqz";
13966 s3 = "x,8";
13967 goto do_addone_branch_i;
13968 case M_BGTU_I:
13969 s = "sltiu";
13970 s2 = "bteqz";
13971 s3 = "x,8";
13972
13973 do_addone_branch_i:
13974 ++imm_expr.X_add_number;
13975
13976 do_branch_i:
13977 macro_build (&imm_expr, s, s3, op[0]);
13978 macro_build (&offset_expr, s2, "p");
13979 break;
13980
13981 case M_ABS:
13982 expr1.X_add_number = 0;
13983 macro_build (&expr1, "slti", "x,8", op[1]);
13984 if (op[0] != op[1])
13985 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
13986 expr1.X_add_number = 2;
13987 macro_build (&expr1, "bteqz", "p");
13988 macro_build (NULL, "neg", "x,w", op[0], op[0]);
13989 break;
13990 }
13991 }
13992
13993 /* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13994 opcode bits in *OPCODE_EXTRA. */
13995
13996 static struct mips_opcode *
13997 mips_lookup_insn (struct hash_control *hash, const char *start,
13998 ssize_t length, unsigned int *opcode_extra)
13999 {
14000 char *name, *dot, *p;
14001 unsigned int mask, suffix;
14002 ssize_t opend;
14003 struct mips_opcode *insn;
14004
14005 /* Make a copy of the instruction so that we can fiddle with it. */
14006 name = xstrndup (start, length);
14007
14008 /* Look up the instruction as-is. */
14009 insn = (struct mips_opcode *) hash_find (hash, name);
14010 if (insn)
14011 goto end;
14012
14013 dot = strchr (name, '.');
14014 if (dot && dot[1])
14015 {
14016 /* Try to interpret the text after the dot as a VU0 channel suffix. */
14017 p = mips_parse_vu0_channels (dot + 1, &mask);
14018 if (*p == 0 && mask != 0)
14019 {
14020 *dot = 0;
14021 insn = (struct mips_opcode *) hash_find (hash, name);
14022 *dot = '.';
14023 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14024 {
14025 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
14026 goto end;
14027 }
14028 }
14029 }
14030
14031 if (mips_opts.micromips)
14032 {
14033 /* See if there's an instruction size override suffix,
14034 either `16' or `32', at the end of the mnemonic proper,
14035 that defines the operation, i.e. before the first `.'
14036 character if any. Strip it and retry. */
14037 opend = dot != NULL ? dot - name : length;
14038 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14039 suffix = 2;
14040 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
14041 suffix = 4;
14042 else
14043 suffix = 0;
14044 if (suffix)
14045 {
14046 memmove (name + opend - 2, name + opend, length - opend + 1);
14047 insn = (struct mips_opcode *) hash_find (hash, name);
14048 if (insn)
14049 {
14050 forced_insn_length = suffix;
14051 goto end;
14052 }
14053 }
14054 }
14055
14056 insn = NULL;
14057 end:
14058 free (name);
14059 return insn;
14060 }
14061
14062 /* Assemble an instruction into its binary format. If the instruction
14063 is a macro, set imm_expr and offset_expr to the values associated
14064 with "I" and "A" operands respectively. Otherwise store the value
14065 of the relocatable field (if any) in offset_expr. In both cases
14066 set offset_reloc to the relocation operators applied to offset_expr. */
14067
14068 static void
14069 mips_ip (char *str, struct mips_cl_insn *insn)
14070 {
14071 const struct mips_opcode *first, *past;
14072 struct hash_control *hash;
14073 char format;
14074 size_t end;
14075 struct mips_operand_token *tokens;
14076 unsigned int opcode_extra;
14077
14078 if (mips_opts.micromips)
14079 {
14080 hash = micromips_op_hash;
14081 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14082 }
14083 else
14084 {
14085 hash = op_hash;
14086 past = &mips_opcodes[NUMOPCODES];
14087 }
14088 forced_insn_length = 0;
14089 opcode_extra = 0;
14090
14091 /* We first try to match an instruction up to a space or to the end. */
14092 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14093 continue;
14094
14095 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14096 if (first == NULL)
14097 {
14098 set_insn_error (0, _("unrecognized opcode"));
14099 return;
14100 }
14101
14102 if (strcmp (first->name, "li.s") == 0)
14103 format = 'f';
14104 else if (strcmp (first->name, "li.d") == 0)
14105 format = 'd';
14106 else
14107 format = 0;
14108 tokens = mips_parse_arguments (str + end, format);
14109 if (!tokens)
14110 return;
14111
14112 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14113 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
14114 set_insn_error (0, _("invalid operands"));
14115
14116 obstack_free (&mips_operand_tokens, tokens);
14117 }
14118
14119 /* As for mips_ip, but used when assembling MIPS16 code.
14120 Also set forced_insn_length to the resulting instruction size in
14121 bytes if the user explicitly requested a small or extended instruction. */
14122
14123 static void
14124 mips16_ip (char *str, struct mips_cl_insn *insn)
14125 {
14126 char *end, *s, c;
14127 struct mips_opcode *first;
14128 struct mips_operand_token *tokens;
14129 unsigned int l;
14130
14131 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
14132 ;
14133 end = s;
14134 c = *end;
14135
14136 l = 0;
14137 switch (c)
14138 {
14139 case '\0':
14140 break;
14141
14142 case ' ':
14143 s++;
14144 break;
14145
14146 case '.':
14147 s++;
14148 if (*s == 't')
14149 {
14150 l = 2;
14151 s++;
14152 }
14153 else if (*s == 'e')
14154 {
14155 l = 4;
14156 s++;
14157 }
14158 if (*s == '\0')
14159 break;
14160 else if (*s++ == ' ')
14161 break;
14162 set_insn_error (0, _("unrecognized opcode"));
14163 return;
14164 }
14165 forced_insn_length = l;
14166
14167 *end = 0;
14168 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
14169 *end = c;
14170
14171 if (!first)
14172 {
14173 set_insn_error (0, _("unrecognized opcode"));
14174 return;
14175 }
14176
14177 tokens = mips_parse_arguments (s, 0);
14178 if (!tokens)
14179 return;
14180
14181 if (!match_mips16_insns (insn, first, tokens))
14182 set_insn_error (0, _("invalid operands"));
14183
14184 obstack_free (&mips_operand_tokens, tokens);
14185 }
14186
14187 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14188 NBITS is the number of significant bits in VAL. */
14189
14190 static unsigned long
14191 mips16_immed_extend (offsetT val, unsigned int nbits)
14192 {
14193 int extval;
14194
14195 extval = 0;
14196 val &= (1U << nbits) - 1;
14197 if (nbits == 16 || nbits == 9)
14198 {
14199 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14200 val &= 0x1f;
14201 }
14202 else if (nbits == 15)
14203 {
14204 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14205 val &= 0xf;
14206 }
14207 else if (nbits == 6)
14208 {
14209 extval = ((val & 0x1f) << 6) | (val & 0x20);
14210 val = 0;
14211 }
14212 return (extval << 16) | val;
14213 }
14214
14215 /* Like decode_mips16_operand, but require the operand to be defined and
14216 require it to be an integer. */
14217
14218 static const struct mips_int_operand *
14219 mips16_immed_operand (int type, bfd_boolean extended_p)
14220 {
14221 const struct mips_operand *operand;
14222
14223 operand = decode_mips16_operand (type, extended_p);
14224 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14225 abort ();
14226 return (const struct mips_int_operand *) operand;
14227 }
14228
14229 /* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14230
14231 static bfd_boolean
14232 mips16_immed_in_range_p (const struct mips_int_operand *operand,
14233 bfd_reloc_code_real_type reloc, offsetT sval)
14234 {
14235 int min_val, max_val;
14236
14237 min_val = mips_int_operand_min (operand);
14238 max_val = mips_int_operand_max (operand);
14239 if (reloc != BFD_RELOC_UNUSED)
14240 {
14241 if (min_val < 0)
14242 sval = SEXT_16BIT (sval);
14243 else
14244 sval &= 0xffff;
14245 }
14246
14247 return (sval >= min_val
14248 && sval <= max_val
14249 && (sval & ((1 << operand->shift) - 1)) == 0);
14250 }
14251
14252 /* Install immediate value VAL into MIPS16 instruction *INSN,
14253 extending it if necessary. The instruction in *INSN may
14254 already be extended.
14255
14256 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14257 if none. In the former case, VAL is a 16-bit number with no
14258 defined signedness.
14259
14260 TYPE is the type of the immediate field. USER_INSN_LENGTH
14261 is the length that the user requested, or 0 if none. */
14262
14263 static void
14264 mips16_immed (const char *file, unsigned int line, int type,
14265 bfd_reloc_code_real_type reloc, offsetT val,
14266 unsigned int user_insn_length, unsigned long *insn)
14267 {
14268 const struct mips_int_operand *operand;
14269 unsigned int uval, length;
14270
14271 operand = mips16_immed_operand (type, FALSE);
14272 if (!mips16_immed_in_range_p (operand, reloc, val))
14273 {
14274 /* We need an extended instruction. */
14275 if (user_insn_length == 2)
14276 as_bad_where (file, line, _("invalid unextended operand value"));
14277 else
14278 *insn |= MIPS16_EXTEND;
14279 }
14280 else if (user_insn_length == 4)
14281 {
14282 /* The operand doesn't force an unextended instruction to be extended.
14283 Warn if the user wanted an extended instruction anyway. */
14284 *insn |= MIPS16_EXTEND;
14285 as_warn_where (file, line,
14286 _("extended operand requested but not required"));
14287 }
14288
14289 length = mips16_opcode_length (*insn);
14290 if (length == 4)
14291 {
14292 operand = mips16_immed_operand (type, TRUE);
14293 if (!mips16_immed_in_range_p (operand, reloc, val))
14294 as_bad_where (file, line,
14295 _("operand value out of range for instruction"));
14296 }
14297 uval = ((unsigned int) val >> operand->shift) - operand->bias;
14298 if (length == 2 || operand->root.lsb != 0)
14299 *insn = mips_insert_operand (&operand->root, *insn, uval);
14300 else
14301 *insn |= mips16_immed_extend (uval, operand->root.size);
14302 }
14303 \f
14304 struct percent_op_match
14305 {
14306 const char *str;
14307 bfd_reloc_code_real_type reloc;
14308 };
14309
14310 static const struct percent_op_match mips_percent_op[] =
14311 {
14312 {"%lo", BFD_RELOC_LO16},
14313 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14314 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14315 {"%call16", BFD_RELOC_MIPS_CALL16},
14316 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14317 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14318 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14319 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14320 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14321 {"%got", BFD_RELOC_MIPS_GOT16},
14322 {"%gp_rel", BFD_RELOC_GPREL16},
14323 {"%gprel", BFD_RELOC_GPREL16},
14324 {"%half", BFD_RELOC_16},
14325 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14326 {"%higher", BFD_RELOC_MIPS_HIGHER},
14327 {"%neg", BFD_RELOC_MIPS_SUB},
14328 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14329 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14330 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14331 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14332 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14333 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14334 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14335 {"%hi", BFD_RELOC_HI16_S},
14336 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14337 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
14338 };
14339
14340 static const struct percent_op_match mips16_percent_op[] =
14341 {
14342 {"%lo", BFD_RELOC_MIPS16_LO16},
14343 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
14344 {"%gprel", BFD_RELOC_MIPS16_GPREL},
14345 {"%got", BFD_RELOC_MIPS16_GOT16},
14346 {"%call16", BFD_RELOC_MIPS16_CALL16},
14347 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14348 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14349 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14350 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14351 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14352 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14353 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14354 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14355 };
14356
14357
14358 /* Return true if *STR points to a relocation operator. When returning true,
14359 move *STR over the operator and store its relocation code in *RELOC.
14360 Leave both *STR and *RELOC alone when returning false. */
14361
14362 static bfd_boolean
14363 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14364 {
14365 const struct percent_op_match *percent_op;
14366 size_t limit, i;
14367
14368 if (mips_opts.mips16)
14369 {
14370 percent_op = mips16_percent_op;
14371 limit = ARRAY_SIZE (mips16_percent_op);
14372 }
14373 else
14374 {
14375 percent_op = mips_percent_op;
14376 limit = ARRAY_SIZE (mips_percent_op);
14377 }
14378
14379 for (i = 0; i < limit; i++)
14380 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14381 {
14382 int len = strlen (percent_op[i].str);
14383
14384 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14385 continue;
14386
14387 *str += strlen (percent_op[i].str);
14388 *reloc = percent_op[i].reloc;
14389
14390 /* Check whether the output BFD supports this relocation.
14391 If not, issue an error and fall back on something safe. */
14392 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14393 {
14394 as_bad (_("relocation %s isn't supported by the current ABI"),
14395 percent_op[i].str);
14396 *reloc = BFD_RELOC_UNUSED;
14397 }
14398 return TRUE;
14399 }
14400 return FALSE;
14401 }
14402
14403
14404 /* Parse string STR as a 16-bit relocatable operand. Store the
14405 expression in *EP and the relocations in the array starting
14406 at RELOC. Return the number of relocation operators used.
14407
14408 On exit, EXPR_END points to the first character after the expression. */
14409
14410 static size_t
14411 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14412 char *str)
14413 {
14414 bfd_reloc_code_real_type reversed_reloc[3];
14415 size_t reloc_index, i;
14416 int crux_depth, str_depth;
14417 char *crux;
14418
14419 /* Search for the start of the main expression, recoding relocations
14420 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14421 of the main expression and with CRUX_DEPTH containing the number
14422 of open brackets at that point. */
14423 reloc_index = -1;
14424 str_depth = 0;
14425 do
14426 {
14427 reloc_index++;
14428 crux = str;
14429 crux_depth = str_depth;
14430
14431 /* Skip over whitespace and brackets, keeping count of the number
14432 of brackets. */
14433 while (*str == ' ' || *str == '\t' || *str == '(')
14434 if (*str++ == '(')
14435 str_depth++;
14436 }
14437 while (*str == '%'
14438 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14439 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14440
14441 my_getExpression (ep, crux);
14442 str = expr_end;
14443
14444 /* Match every open bracket. */
14445 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14446 if (*str++ == ')')
14447 crux_depth--;
14448
14449 if (crux_depth > 0)
14450 as_bad (_("unclosed '('"));
14451
14452 expr_end = str;
14453
14454 if (reloc_index != 0)
14455 {
14456 prev_reloc_op_frag = frag_now;
14457 for (i = 0; i < reloc_index; i++)
14458 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14459 }
14460
14461 return reloc_index;
14462 }
14463
14464 static void
14465 my_getExpression (expressionS *ep, char *str)
14466 {
14467 char *save_in;
14468
14469 save_in = input_line_pointer;
14470 input_line_pointer = str;
14471 expression (ep);
14472 expr_end = input_line_pointer;
14473 input_line_pointer = save_in;
14474 }
14475
14476 const char *
14477 md_atof (int type, char *litP, int *sizeP)
14478 {
14479 return ieee_md_atof (type, litP, sizeP, target_big_endian);
14480 }
14481
14482 void
14483 md_number_to_chars (char *buf, valueT val, int n)
14484 {
14485 if (target_big_endian)
14486 number_to_chars_bigendian (buf, val, n);
14487 else
14488 number_to_chars_littleendian (buf, val, n);
14489 }
14490 \f
14491 static int support_64bit_objects(void)
14492 {
14493 const char **list, **l;
14494 int yes;
14495
14496 list = bfd_target_list ();
14497 for (l = list; *l != NULL; l++)
14498 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14499 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14500 break;
14501 yes = (*l != NULL);
14502 free (list);
14503 return yes;
14504 }
14505
14506 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14507 NEW_VALUE. Warn if another value was already specified. Note:
14508 we have to defer parsing the -march and -mtune arguments in order
14509 to handle 'from-abi' correctly, since the ABI might be specified
14510 in a later argument. */
14511
14512 static void
14513 mips_set_option_string (const char **string_ptr, const char *new_value)
14514 {
14515 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14516 as_warn (_("a different %s was already specified, is now %s"),
14517 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14518 new_value);
14519
14520 *string_ptr = new_value;
14521 }
14522
14523 int
14524 md_parse_option (int c, const char *arg)
14525 {
14526 unsigned int i;
14527
14528 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14529 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14530 {
14531 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
14532 c == mips_ases[i].option_on);
14533 return 1;
14534 }
14535
14536 switch (c)
14537 {
14538 case OPTION_CONSTRUCT_FLOATS:
14539 mips_disable_float_construction = 0;
14540 break;
14541
14542 case OPTION_NO_CONSTRUCT_FLOATS:
14543 mips_disable_float_construction = 1;
14544 break;
14545
14546 case OPTION_TRAP:
14547 mips_trap = 1;
14548 break;
14549
14550 case OPTION_BREAK:
14551 mips_trap = 0;
14552 break;
14553
14554 case OPTION_EB:
14555 target_big_endian = 1;
14556 break;
14557
14558 case OPTION_EL:
14559 target_big_endian = 0;
14560 break;
14561
14562 case 'O':
14563 if (arg == NULL)
14564 mips_optimize = 1;
14565 else if (arg[0] == '0')
14566 mips_optimize = 0;
14567 else if (arg[0] == '1')
14568 mips_optimize = 1;
14569 else
14570 mips_optimize = 2;
14571 break;
14572
14573 case 'g':
14574 if (arg == NULL)
14575 mips_debug = 2;
14576 else
14577 mips_debug = atoi (arg);
14578 break;
14579
14580 case OPTION_MIPS1:
14581 file_mips_opts.isa = ISA_MIPS1;
14582 break;
14583
14584 case OPTION_MIPS2:
14585 file_mips_opts.isa = ISA_MIPS2;
14586 break;
14587
14588 case OPTION_MIPS3:
14589 file_mips_opts.isa = ISA_MIPS3;
14590 break;
14591
14592 case OPTION_MIPS4:
14593 file_mips_opts.isa = ISA_MIPS4;
14594 break;
14595
14596 case OPTION_MIPS5:
14597 file_mips_opts.isa = ISA_MIPS5;
14598 break;
14599
14600 case OPTION_MIPS32:
14601 file_mips_opts.isa = ISA_MIPS32;
14602 break;
14603
14604 case OPTION_MIPS32R2:
14605 file_mips_opts.isa = ISA_MIPS32R2;
14606 break;
14607
14608 case OPTION_MIPS32R3:
14609 file_mips_opts.isa = ISA_MIPS32R3;
14610 break;
14611
14612 case OPTION_MIPS32R5:
14613 file_mips_opts.isa = ISA_MIPS32R5;
14614 break;
14615
14616 case OPTION_MIPS32R6:
14617 file_mips_opts.isa = ISA_MIPS32R6;
14618 break;
14619
14620 case OPTION_MIPS64R2:
14621 file_mips_opts.isa = ISA_MIPS64R2;
14622 break;
14623
14624 case OPTION_MIPS64R3:
14625 file_mips_opts.isa = ISA_MIPS64R3;
14626 break;
14627
14628 case OPTION_MIPS64R5:
14629 file_mips_opts.isa = ISA_MIPS64R5;
14630 break;
14631
14632 case OPTION_MIPS64R6:
14633 file_mips_opts.isa = ISA_MIPS64R6;
14634 break;
14635
14636 case OPTION_MIPS64:
14637 file_mips_opts.isa = ISA_MIPS64;
14638 break;
14639
14640 case OPTION_MTUNE:
14641 mips_set_option_string (&mips_tune_string, arg);
14642 break;
14643
14644 case OPTION_MARCH:
14645 mips_set_option_string (&mips_arch_string, arg);
14646 break;
14647
14648 case OPTION_M4650:
14649 mips_set_option_string (&mips_arch_string, "4650");
14650 mips_set_option_string (&mips_tune_string, "4650");
14651 break;
14652
14653 case OPTION_NO_M4650:
14654 break;
14655
14656 case OPTION_M4010:
14657 mips_set_option_string (&mips_arch_string, "4010");
14658 mips_set_option_string (&mips_tune_string, "4010");
14659 break;
14660
14661 case OPTION_NO_M4010:
14662 break;
14663
14664 case OPTION_M4100:
14665 mips_set_option_string (&mips_arch_string, "4100");
14666 mips_set_option_string (&mips_tune_string, "4100");
14667 break;
14668
14669 case OPTION_NO_M4100:
14670 break;
14671
14672 case OPTION_M3900:
14673 mips_set_option_string (&mips_arch_string, "3900");
14674 mips_set_option_string (&mips_tune_string, "3900");
14675 break;
14676
14677 case OPTION_NO_M3900:
14678 break;
14679
14680 case OPTION_MICROMIPS:
14681 if (file_mips_opts.mips16 == 1)
14682 {
14683 as_bad (_("-mmicromips cannot be used with -mips16"));
14684 return 0;
14685 }
14686 file_mips_opts.micromips = 1;
14687 mips_no_prev_insn ();
14688 break;
14689
14690 case OPTION_NO_MICROMIPS:
14691 file_mips_opts.micromips = 0;
14692 mips_no_prev_insn ();
14693 break;
14694
14695 case OPTION_MIPS16:
14696 if (file_mips_opts.micromips == 1)
14697 {
14698 as_bad (_("-mips16 cannot be used with -micromips"));
14699 return 0;
14700 }
14701 file_mips_opts.mips16 = 1;
14702 mips_no_prev_insn ();
14703 break;
14704
14705 case OPTION_NO_MIPS16:
14706 file_mips_opts.mips16 = 0;
14707 mips_no_prev_insn ();
14708 break;
14709
14710 case OPTION_FIX_24K:
14711 mips_fix_24k = 1;
14712 break;
14713
14714 case OPTION_NO_FIX_24K:
14715 mips_fix_24k = 0;
14716 break;
14717
14718 case OPTION_FIX_RM7000:
14719 mips_fix_rm7000 = 1;
14720 break;
14721
14722 case OPTION_NO_FIX_RM7000:
14723 mips_fix_rm7000 = 0;
14724 break;
14725
14726 case OPTION_FIX_LOONGSON2F_JUMP:
14727 mips_fix_loongson2f_jump = TRUE;
14728 break;
14729
14730 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14731 mips_fix_loongson2f_jump = FALSE;
14732 break;
14733
14734 case OPTION_FIX_LOONGSON2F_NOP:
14735 mips_fix_loongson2f_nop = TRUE;
14736 break;
14737
14738 case OPTION_NO_FIX_LOONGSON2F_NOP:
14739 mips_fix_loongson2f_nop = FALSE;
14740 break;
14741
14742 case OPTION_FIX_VR4120:
14743 mips_fix_vr4120 = 1;
14744 break;
14745
14746 case OPTION_NO_FIX_VR4120:
14747 mips_fix_vr4120 = 0;
14748 break;
14749
14750 case OPTION_FIX_VR4130:
14751 mips_fix_vr4130 = 1;
14752 break;
14753
14754 case OPTION_NO_FIX_VR4130:
14755 mips_fix_vr4130 = 0;
14756 break;
14757
14758 case OPTION_FIX_CN63XXP1:
14759 mips_fix_cn63xxp1 = TRUE;
14760 break;
14761
14762 case OPTION_NO_FIX_CN63XXP1:
14763 mips_fix_cn63xxp1 = FALSE;
14764 break;
14765
14766 case OPTION_RELAX_BRANCH:
14767 mips_relax_branch = 1;
14768 break;
14769
14770 case OPTION_NO_RELAX_BRANCH:
14771 mips_relax_branch = 0;
14772 break;
14773
14774 case OPTION_IGNORE_BRANCH_ISA:
14775 mips_ignore_branch_isa = TRUE;
14776 break;
14777
14778 case OPTION_NO_IGNORE_BRANCH_ISA:
14779 mips_ignore_branch_isa = FALSE;
14780 break;
14781
14782 case OPTION_INSN32:
14783 file_mips_opts.insn32 = TRUE;
14784 break;
14785
14786 case OPTION_NO_INSN32:
14787 file_mips_opts.insn32 = FALSE;
14788 break;
14789
14790 case OPTION_MSHARED:
14791 mips_in_shared = TRUE;
14792 break;
14793
14794 case OPTION_MNO_SHARED:
14795 mips_in_shared = FALSE;
14796 break;
14797
14798 case OPTION_MSYM32:
14799 file_mips_opts.sym32 = TRUE;
14800 break;
14801
14802 case OPTION_MNO_SYM32:
14803 file_mips_opts.sym32 = FALSE;
14804 break;
14805
14806 /* When generating ELF code, we permit -KPIC and -call_shared to
14807 select SVR4_PIC, and -non_shared to select no PIC. This is
14808 intended to be compatible with Irix 5. */
14809 case OPTION_CALL_SHARED:
14810 mips_pic = SVR4_PIC;
14811 mips_abicalls = TRUE;
14812 break;
14813
14814 case OPTION_CALL_NONPIC:
14815 mips_pic = NO_PIC;
14816 mips_abicalls = TRUE;
14817 break;
14818
14819 case OPTION_NON_SHARED:
14820 mips_pic = NO_PIC;
14821 mips_abicalls = FALSE;
14822 break;
14823
14824 /* The -xgot option tells the assembler to use 32 bit offsets
14825 when accessing the got in SVR4_PIC mode. It is for Irix
14826 compatibility. */
14827 case OPTION_XGOT:
14828 mips_big_got = 1;
14829 break;
14830
14831 case 'G':
14832 g_switch_value = atoi (arg);
14833 g_switch_seen = 1;
14834 break;
14835
14836 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14837 and -mabi=64. */
14838 case OPTION_32:
14839 mips_abi = O32_ABI;
14840 break;
14841
14842 case OPTION_N32:
14843 mips_abi = N32_ABI;
14844 break;
14845
14846 case OPTION_64:
14847 mips_abi = N64_ABI;
14848 if (!support_64bit_objects())
14849 as_fatal (_("no compiled in support for 64 bit object file format"));
14850 break;
14851
14852 case OPTION_GP32:
14853 file_mips_opts.gp = 32;
14854 break;
14855
14856 case OPTION_GP64:
14857 file_mips_opts.gp = 64;
14858 break;
14859
14860 case OPTION_FP32:
14861 file_mips_opts.fp = 32;
14862 break;
14863
14864 case OPTION_FPXX:
14865 file_mips_opts.fp = 0;
14866 break;
14867
14868 case OPTION_FP64:
14869 file_mips_opts.fp = 64;
14870 break;
14871
14872 case OPTION_ODD_SPREG:
14873 file_mips_opts.oddspreg = 1;
14874 break;
14875
14876 case OPTION_NO_ODD_SPREG:
14877 file_mips_opts.oddspreg = 0;
14878 break;
14879
14880 case OPTION_SINGLE_FLOAT:
14881 file_mips_opts.single_float = 1;
14882 break;
14883
14884 case OPTION_DOUBLE_FLOAT:
14885 file_mips_opts.single_float = 0;
14886 break;
14887
14888 case OPTION_SOFT_FLOAT:
14889 file_mips_opts.soft_float = 1;
14890 break;
14891
14892 case OPTION_HARD_FLOAT:
14893 file_mips_opts.soft_float = 0;
14894 break;
14895
14896 case OPTION_MABI:
14897 if (strcmp (arg, "32") == 0)
14898 mips_abi = O32_ABI;
14899 else if (strcmp (arg, "o64") == 0)
14900 mips_abi = O64_ABI;
14901 else if (strcmp (arg, "n32") == 0)
14902 mips_abi = N32_ABI;
14903 else if (strcmp (arg, "64") == 0)
14904 {
14905 mips_abi = N64_ABI;
14906 if (! support_64bit_objects())
14907 as_fatal (_("no compiled in support for 64 bit object file "
14908 "format"));
14909 }
14910 else if (strcmp (arg, "eabi") == 0)
14911 mips_abi = EABI_ABI;
14912 else
14913 {
14914 as_fatal (_("invalid abi -mabi=%s"), arg);
14915 return 0;
14916 }
14917 break;
14918
14919 case OPTION_M7000_HILO_FIX:
14920 mips_7000_hilo_fix = TRUE;
14921 break;
14922
14923 case OPTION_MNO_7000_HILO_FIX:
14924 mips_7000_hilo_fix = FALSE;
14925 break;
14926
14927 case OPTION_MDEBUG:
14928 mips_flag_mdebug = TRUE;
14929 break;
14930
14931 case OPTION_NO_MDEBUG:
14932 mips_flag_mdebug = FALSE;
14933 break;
14934
14935 case OPTION_PDR:
14936 mips_flag_pdr = TRUE;
14937 break;
14938
14939 case OPTION_NO_PDR:
14940 mips_flag_pdr = FALSE;
14941 break;
14942
14943 case OPTION_MVXWORKS_PIC:
14944 mips_pic = VXWORKS_PIC;
14945 break;
14946
14947 case OPTION_NAN:
14948 if (strcmp (arg, "2008") == 0)
14949 mips_nan2008 = 1;
14950 else if (strcmp (arg, "legacy") == 0)
14951 mips_nan2008 = 0;
14952 else
14953 {
14954 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
14955 return 0;
14956 }
14957 break;
14958
14959 default:
14960 return 0;
14961 }
14962
14963 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14964
14965 return 1;
14966 }
14967 \f
14968 /* Set up globals to tune for the ISA or processor described by INFO. */
14969
14970 static void
14971 mips_set_tune (const struct mips_cpu_info *info)
14972 {
14973 if (info != 0)
14974 mips_tune = info->cpu;
14975 }
14976
14977
14978 void
14979 mips_after_parse_args (void)
14980 {
14981 const struct mips_cpu_info *arch_info = 0;
14982 const struct mips_cpu_info *tune_info = 0;
14983
14984 /* GP relative stuff not working for PE */
14985 if (strncmp (TARGET_OS, "pe", 2) == 0)
14986 {
14987 if (g_switch_seen && g_switch_value != 0)
14988 as_bad (_("-G not supported in this configuration"));
14989 g_switch_value = 0;
14990 }
14991
14992 if (mips_abi == NO_ABI)
14993 mips_abi = MIPS_DEFAULT_ABI;
14994
14995 /* The following code determines the architecture.
14996 Similar code was added to GCC 3.3 (see override_options() in
14997 config/mips/mips.c). The GAS and GCC code should be kept in sync
14998 as much as possible. */
14999
15000 if (mips_arch_string != 0)
15001 arch_info = mips_parse_cpu ("-march", mips_arch_string);
15002
15003 if (file_mips_opts.isa != ISA_UNKNOWN)
15004 {
15005 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
15006 ISA level specified by -mipsN, while arch_info->isa contains
15007 the -march selection (if any). */
15008 if (arch_info != 0)
15009 {
15010 /* -march takes precedence over -mipsN, since it is more descriptive.
15011 There's no harm in specifying both as long as the ISA levels
15012 are the same. */
15013 if (file_mips_opts.isa != arch_info->isa)
15014 as_bad (_("-%s conflicts with the other architecture options,"
15015 " which imply -%s"),
15016 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
15017 mips_cpu_info_from_isa (arch_info->isa)->name);
15018 }
15019 else
15020 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
15021 }
15022
15023 if (arch_info == 0)
15024 {
15025 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15026 gas_assert (arch_info);
15027 }
15028
15029 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15030 as_bad (_("-march=%s is not compatible with the selected ABI"),
15031 arch_info->name);
15032
15033 file_mips_opts.arch = arch_info->cpu;
15034 file_mips_opts.isa = arch_info->isa;
15035
15036 /* Set up initial mips_opts state. */
15037 mips_opts = file_mips_opts;
15038
15039 /* The register size inference code is now placed in
15040 file_mips_check_options. */
15041
15042 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15043 processor. */
15044 if (mips_tune_string != 0)
15045 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15046
15047 if (tune_info == 0)
15048 mips_set_tune (arch_info);
15049 else
15050 mips_set_tune (tune_info);
15051
15052 if (mips_flag_mdebug < 0)
15053 mips_flag_mdebug = 0;
15054 }
15055 \f
15056 void
15057 mips_init_after_args (void)
15058 {
15059 /* initialize opcodes */
15060 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15061 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15062 }
15063
15064 long
15065 md_pcrel_from (fixS *fixP)
15066 {
15067 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15068 switch (fixP->fx_r_type)
15069 {
15070 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15071 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15072 /* Return the address of the delay slot. */
15073 return addr + 2;
15074
15075 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15076 case BFD_RELOC_MICROMIPS_JMP:
15077 case BFD_RELOC_MIPS16_16_PCREL_S1:
15078 case BFD_RELOC_16_PCREL_S2:
15079 case BFD_RELOC_MIPS_21_PCREL_S2:
15080 case BFD_RELOC_MIPS_26_PCREL_S2:
15081 case BFD_RELOC_MIPS_JMP:
15082 /* Return the address of the delay slot. */
15083 return addr + 4;
15084
15085 case BFD_RELOC_MIPS_18_PCREL_S3:
15086 /* Return the aligned address of the doubleword containing
15087 the instruction. */
15088 return addr & ~7;
15089
15090 default:
15091 return addr;
15092 }
15093 }
15094
15095 /* This is called before the symbol table is processed. In order to
15096 work with gcc when using mips-tfile, we must keep all local labels.
15097 However, in other cases, we want to discard them. If we were
15098 called with -g, but we didn't see any debugging information, it may
15099 mean that gcc is smuggling debugging information through to
15100 mips-tfile, in which case we must generate all local labels. */
15101
15102 void
15103 mips_frob_file_before_adjust (void)
15104 {
15105 #ifndef NO_ECOFF_DEBUGGING
15106 if (ECOFF_DEBUGGING
15107 && mips_debug != 0
15108 && ! ecoff_debugging_seen)
15109 flag_keep_locals = 1;
15110 #endif
15111 }
15112
15113 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15114 the corresponding LO16 reloc. This is called before md_apply_fix and
15115 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15116 relocation operators.
15117
15118 For our purposes, a %lo() expression matches a %got() or %hi()
15119 expression if:
15120
15121 (a) it refers to the same symbol; and
15122 (b) the offset applied in the %lo() expression is no lower than
15123 the offset applied in the %got() or %hi().
15124
15125 (b) allows us to cope with code like:
15126
15127 lui $4,%hi(foo)
15128 lh $4,%lo(foo+2)($4)
15129
15130 ...which is legal on RELA targets, and has a well-defined behaviour
15131 if the user knows that adding 2 to "foo" will not induce a carry to
15132 the high 16 bits.
15133
15134 When several %lo()s match a particular %got() or %hi(), we use the
15135 following rules to distinguish them:
15136
15137 (1) %lo()s with smaller offsets are a better match than %lo()s with
15138 higher offsets.
15139
15140 (2) %lo()s with no matching %got() or %hi() are better than those
15141 that already have a matching %got() or %hi().
15142
15143 (3) later %lo()s are better than earlier %lo()s.
15144
15145 These rules are applied in order.
15146
15147 (1) means, among other things, that %lo()s with identical offsets are
15148 chosen if they exist.
15149
15150 (2) means that we won't associate several high-part relocations with
15151 the same low-part relocation unless there's no alternative. Having
15152 several high parts for the same low part is a GNU extension; this rule
15153 allows careful users to avoid it.
15154
15155 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15156 with the last high-part relocation being at the front of the list.
15157 It therefore makes sense to choose the last matching low-part
15158 relocation, all other things being equal. It's also easier
15159 to code that way. */
15160
15161 void
15162 mips_frob_file (void)
15163 {
15164 struct mips_hi_fixup *l;
15165 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15166
15167 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15168 {
15169 segment_info_type *seginfo;
15170 bfd_boolean matched_lo_p;
15171 fixS **hi_pos, **lo_pos, **pos;
15172
15173 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15174
15175 /* If a GOT16 relocation turns out to be against a global symbol,
15176 there isn't supposed to be a matching LO. Ignore %gots against
15177 constants; we'll report an error for those later. */
15178 if (got16_reloc_p (l->fixp->fx_r_type)
15179 && !(l->fixp->fx_addsy
15180 && pic_need_relax (l->fixp->fx_addsy)))
15181 continue;
15182
15183 /* Check quickly whether the next fixup happens to be a matching %lo. */
15184 if (fixup_has_matching_lo_p (l->fixp))
15185 continue;
15186
15187 seginfo = seg_info (l->seg);
15188
15189 /* Set HI_POS to the position of this relocation in the chain.
15190 Set LO_POS to the position of the chosen low-part relocation.
15191 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15192 relocation that matches an immediately-preceding high-part
15193 relocation. */
15194 hi_pos = NULL;
15195 lo_pos = NULL;
15196 matched_lo_p = FALSE;
15197 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15198
15199 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15200 {
15201 if (*pos == l->fixp)
15202 hi_pos = pos;
15203
15204 if ((*pos)->fx_r_type == looking_for_rtype
15205 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15206 && (*pos)->fx_offset >= l->fixp->fx_offset
15207 && (lo_pos == NULL
15208 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15209 || (!matched_lo_p
15210 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15211 lo_pos = pos;
15212
15213 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15214 && fixup_has_matching_lo_p (*pos));
15215 }
15216
15217 /* If we found a match, remove the high-part relocation from its
15218 current position and insert it before the low-part relocation.
15219 Make the offsets match so that fixup_has_matching_lo_p()
15220 will return true.
15221
15222 We don't warn about unmatched high-part relocations since some
15223 versions of gcc have been known to emit dead "lui ...%hi(...)"
15224 instructions. */
15225 if (lo_pos != NULL)
15226 {
15227 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15228 if (l->fixp->fx_next != *lo_pos)
15229 {
15230 *hi_pos = l->fixp->fx_next;
15231 l->fixp->fx_next = *lo_pos;
15232 *lo_pos = l->fixp;
15233 }
15234 }
15235 }
15236 }
15237
15238 int
15239 mips_force_relocation (fixS *fixp)
15240 {
15241 if (generic_force_reloc (fixp))
15242 return 1;
15243
15244 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15245 so that the linker relaxation can update targets. */
15246 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15247 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15248 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15249 return 1;
15250
15251 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15252 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15253 microMIPS symbols so that we can do cross-mode branch diagnostics
15254 and BAL to JALX conversion by the linker. */
15255 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15256 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15257 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15258 && fixp->fx_addsy
15259 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15260 return 1;
15261
15262 /* We want all PC-relative relocations to be kept for R6 relaxation. */
15263 if (ISA_IS_R6 (file_mips_opts.isa)
15264 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15265 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15266 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15267 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15268 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15269 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15270 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15271 return 1;
15272
15273 return 0;
15274 }
15275
15276 /* Implement TC_FORCE_RELOCATION_ABS. */
15277
15278 bfd_boolean
15279 mips_force_relocation_abs (fixS *fixp)
15280 {
15281 if (generic_force_reloc (fixp))
15282 return TRUE;
15283
15284 /* These relocations do not have enough bits in the in-place addend
15285 to hold an arbitrary absolute section's offset. */
15286 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15287 return TRUE;
15288
15289 return FALSE;
15290 }
15291
15292 /* Read the instruction associated with RELOC from BUF. */
15293
15294 static unsigned int
15295 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15296 {
15297 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15298 return read_compressed_insn (buf, 4);
15299 else
15300 return read_insn (buf);
15301 }
15302
15303 /* Write instruction INSN to BUF, given that it has been relocated
15304 by RELOC. */
15305
15306 static void
15307 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15308 unsigned long insn)
15309 {
15310 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15311 write_compressed_insn (buf, insn, 4);
15312 else
15313 write_insn (buf, insn);
15314 }
15315
15316 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15317 to a symbol in another ISA mode, which cannot be converted to JALX. */
15318
15319 static bfd_boolean
15320 fix_bad_cross_mode_jump_p (fixS *fixP)
15321 {
15322 unsigned long opcode;
15323 int other;
15324 char *buf;
15325
15326 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15327 return FALSE;
15328
15329 other = S_GET_OTHER (fixP->fx_addsy);
15330 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15331 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15332 switch (fixP->fx_r_type)
15333 {
15334 case BFD_RELOC_MIPS_JMP:
15335 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15336 case BFD_RELOC_MICROMIPS_JMP:
15337 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15338 default:
15339 return FALSE;
15340 }
15341 }
15342
15343 /* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15344 jump to a symbol in the same ISA mode. */
15345
15346 static bfd_boolean
15347 fix_bad_same_mode_jalx_p (fixS *fixP)
15348 {
15349 unsigned long opcode;
15350 int other;
15351 char *buf;
15352
15353 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15354 return FALSE;
15355
15356 other = S_GET_OTHER (fixP->fx_addsy);
15357 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15358 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15359 switch (fixP->fx_r_type)
15360 {
15361 case BFD_RELOC_MIPS_JMP:
15362 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15363 case BFD_RELOC_MIPS16_JMP:
15364 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15365 case BFD_RELOC_MICROMIPS_JMP:
15366 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15367 default:
15368 return FALSE;
15369 }
15370 }
15371
15372 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15373 to a symbol whose value plus addend is not aligned according to the
15374 ultimate (after linker relaxation) jump instruction's immediate field
15375 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15376 regular MIPS code, to (1 << 2). */
15377
15378 static bfd_boolean
15379 fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15380 {
15381 bfd_boolean micro_to_mips_p;
15382 valueT val;
15383 int other;
15384
15385 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15386 return FALSE;
15387
15388 other = S_GET_OTHER (fixP->fx_addsy);
15389 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15390 val += fixP->fx_offset;
15391 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15392 && !ELF_ST_IS_MICROMIPS (other));
15393 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15394 != ELF_ST_IS_COMPRESSED (other));
15395 }
15396
15397 /* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15398 to a symbol whose annotation indicates another ISA mode. For absolute
15399 symbols check the ISA bit instead.
15400
15401 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15402 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15403 MIPS symbols and associated with BAL instructions as these instructions
15404 may be converted to JALX by the linker. */
15405
15406 static bfd_boolean
15407 fix_bad_cross_mode_branch_p (fixS *fixP)
15408 {
15409 bfd_boolean absolute_p;
15410 unsigned long opcode;
15411 asection *symsec;
15412 valueT val;
15413 int other;
15414 char *buf;
15415
15416 if (mips_ignore_branch_isa)
15417 return FALSE;
15418
15419 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15420 return FALSE;
15421
15422 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15423 absolute_p = bfd_is_abs_section (symsec);
15424
15425 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15426 other = S_GET_OTHER (fixP->fx_addsy);
15427
15428 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15429 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15430 switch (fixP->fx_r_type)
15431 {
15432 case BFD_RELOC_16_PCREL_S2:
15433 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15434 && opcode != 0x0411);
15435 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15436 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15437 && opcode != 0x4060);
15438 case BFD_RELOC_MIPS_21_PCREL_S2:
15439 case BFD_RELOC_MIPS_26_PCREL_S2:
15440 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15441 case BFD_RELOC_MIPS16_16_PCREL_S1:
15442 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15443 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15444 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15445 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15446 default:
15447 abort ();
15448 }
15449 }
15450
15451 /* Return TRUE if the symbol plus addend associated with a regular MIPS
15452 branch instruction pointed to by FIXP is not aligned according to the
15453 branch instruction's immediate field requirement. We need the addend
15454 to preserve the ISA bit and also the sum must not have bit 2 set. We
15455 must explicitly OR in the ISA bit from symbol annotation as the bit
15456 won't be set in the symbol's value then. */
15457
15458 static bfd_boolean
15459 fix_bad_misaligned_branch_p (fixS *fixP)
15460 {
15461 bfd_boolean absolute_p;
15462 asection *symsec;
15463 valueT isa_bit;
15464 valueT val;
15465 valueT off;
15466 int other;
15467
15468 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15469 return FALSE;
15470
15471 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15472 absolute_p = bfd_is_abs_section (symsec);
15473
15474 val = S_GET_VALUE (fixP->fx_addsy);
15475 other = S_GET_OTHER (fixP->fx_addsy);
15476 off = fixP->fx_offset;
15477
15478 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15479 val |= ELF_ST_IS_COMPRESSED (other);
15480 val += off;
15481 return (val & 0x3) != isa_bit;
15482 }
15483
15484 /* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15485 and its calculated value VAL. */
15486
15487 static void
15488 fix_validate_branch (fixS *fixP, valueT val)
15489 {
15490 if (fixP->fx_done && (val & 0x3) != 0)
15491 as_bad_where (fixP->fx_file, fixP->fx_line,
15492 _("branch to misaligned address (0x%lx)"),
15493 (long) (val + md_pcrel_from (fixP)));
15494 else if (fix_bad_cross_mode_branch_p (fixP))
15495 as_bad_where (fixP->fx_file, fixP->fx_line,
15496 _("branch to a symbol in another ISA mode"));
15497 else if (fix_bad_misaligned_branch_p (fixP))
15498 as_bad_where (fixP->fx_file, fixP->fx_line,
15499 _("branch to misaligned address (0x%lx)"),
15500 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15501 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15502 as_bad_where (fixP->fx_file, fixP->fx_line,
15503 _("cannot encode misaligned addend "
15504 "in the relocatable field (0x%lx)"),
15505 (long) fixP->fx_offset);
15506 }
15507
15508 /* Apply a fixup to the object file. */
15509
15510 void
15511 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15512 {
15513 char *buf;
15514 unsigned long insn;
15515 reloc_howto_type *howto;
15516
15517 if (fixP->fx_pcrel)
15518 switch (fixP->fx_r_type)
15519 {
15520 case BFD_RELOC_16_PCREL_S2:
15521 case BFD_RELOC_MIPS16_16_PCREL_S1:
15522 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15523 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15524 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15525 case BFD_RELOC_32_PCREL:
15526 case BFD_RELOC_MIPS_21_PCREL_S2:
15527 case BFD_RELOC_MIPS_26_PCREL_S2:
15528 case BFD_RELOC_MIPS_18_PCREL_S3:
15529 case BFD_RELOC_MIPS_19_PCREL_S2:
15530 case BFD_RELOC_HI16_S_PCREL:
15531 case BFD_RELOC_LO16_PCREL:
15532 break;
15533
15534 case BFD_RELOC_32:
15535 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15536 break;
15537
15538 default:
15539 as_bad_where (fixP->fx_file, fixP->fx_line,
15540 _("PC-relative reference to a different section"));
15541 break;
15542 }
15543
15544 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15545 that have no MIPS ELF equivalent. */
15546 if (fixP->fx_r_type != BFD_RELOC_8)
15547 {
15548 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15549 if (!howto)
15550 return;
15551 }
15552
15553 gas_assert (fixP->fx_size == 2
15554 || fixP->fx_size == 4
15555 || fixP->fx_r_type == BFD_RELOC_8
15556 || fixP->fx_r_type == BFD_RELOC_16
15557 || fixP->fx_r_type == BFD_RELOC_64
15558 || fixP->fx_r_type == BFD_RELOC_CTOR
15559 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15560 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15561 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15562 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15563 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15564 || fixP->fx_r_type == BFD_RELOC_NONE);
15565
15566 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15567
15568 /* Don't treat parts of a composite relocation as done. There are two
15569 reasons for this:
15570
15571 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15572 should nevertheless be emitted if the first part is.
15573
15574 (2) In normal usage, composite relocations are never assembly-time
15575 constants. The easiest way of dealing with the pathological
15576 exceptions is to generate a relocation against STN_UNDEF and
15577 leave everything up to the linker. */
15578 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15579 fixP->fx_done = 1;
15580
15581 switch (fixP->fx_r_type)
15582 {
15583 case BFD_RELOC_MIPS_TLS_GD:
15584 case BFD_RELOC_MIPS_TLS_LDM:
15585 case BFD_RELOC_MIPS_TLS_DTPREL32:
15586 case BFD_RELOC_MIPS_TLS_DTPREL64:
15587 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15588 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15589 case BFD_RELOC_MIPS_TLS_GOTTPREL:
15590 case BFD_RELOC_MIPS_TLS_TPREL32:
15591 case BFD_RELOC_MIPS_TLS_TPREL64:
15592 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15593 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15594 case BFD_RELOC_MICROMIPS_TLS_GD:
15595 case BFD_RELOC_MICROMIPS_TLS_LDM:
15596 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15597 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15598 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15599 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15600 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15601 case BFD_RELOC_MIPS16_TLS_GD:
15602 case BFD_RELOC_MIPS16_TLS_LDM:
15603 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15604 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15605 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15606 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15607 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15608 if (fixP->fx_addsy)
15609 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15610 else
15611 as_bad_where (fixP->fx_file, fixP->fx_line,
15612 _("TLS relocation against a constant"));
15613 break;
15614
15615 case BFD_RELOC_MIPS_JMP:
15616 case BFD_RELOC_MIPS16_JMP:
15617 case BFD_RELOC_MICROMIPS_JMP:
15618 {
15619 int shift;
15620
15621 gas_assert (!fixP->fx_done);
15622
15623 /* Shift is 2, unusually, for microMIPS JALX. */
15624 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15625 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15626 shift = 1;
15627 else
15628 shift = 2;
15629
15630 if (fix_bad_cross_mode_jump_p (fixP))
15631 as_bad_where (fixP->fx_file, fixP->fx_line,
15632 _("jump to a symbol in another ISA mode"));
15633 else if (fix_bad_same_mode_jalx_p (fixP))
15634 as_bad_where (fixP->fx_file, fixP->fx_line,
15635 _("JALX to a symbol in the same ISA mode"));
15636 else if (fix_bad_misaligned_jump_p (fixP, shift))
15637 as_bad_where (fixP->fx_file, fixP->fx_line,
15638 _("jump to misaligned address (0x%lx)"),
15639 (long) (S_GET_VALUE (fixP->fx_addsy)
15640 + fixP->fx_offset));
15641 else if (HAVE_IN_PLACE_ADDENDS
15642 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15643 as_bad_where (fixP->fx_file, fixP->fx_line,
15644 _("cannot encode misaligned addend "
15645 "in the relocatable field (0x%lx)"),
15646 (long) fixP->fx_offset);
15647 }
15648 /* Fall through. */
15649
15650 case BFD_RELOC_MIPS_SHIFT5:
15651 case BFD_RELOC_MIPS_SHIFT6:
15652 case BFD_RELOC_MIPS_GOT_DISP:
15653 case BFD_RELOC_MIPS_GOT_PAGE:
15654 case BFD_RELOC_MIPS_GOT_OFST:
15655 case BFD_RELOC_MIPS_SUB:
15656 case BFD_RELOC_MIPS_INSERT_A:
15657 case BFD_RELOC_MIPS_INSERT_B:
15658 case BFD_RELOC_MIPS_DELETE:
15659 case BFD_RELOC_MIPS_HIGHEST:
15660 case BFD_RELOC_MIPS_HIGHER:
15661 case BFD_RELOC_MIPS_SCN_DISP:
15662 case BFD_RELOC_MIPS_REL16:
15663 case BFD_RELOC_MIPS_RELGOT:
15664 case BFD_RELOC_MIPS_JALR:
15665 case BFD_RELOC_HI16:
15666 case BFD_RELOC_HI16_S:
15667 case BFD_RELOC_LO16:
15668 case BFD_RELOC_GPREL16:
15669 case BFD_RELOC_MIPS_LITERAL:
15670 case BFD_RELOC_MIPS_CALL16:
15671 case BFD_RELOC_MIPS_GOT16:
15672 case BFD_RELOC_GPREL32:
15673 case BFD_RELOC_MIPS_GOT_HI16:
15674 case BFD_RELOC_MIPS_GOT_LO16:
15675 case BFD_RELOC_MIPS_CALL_HI16:
15676 case BFD_RELOC_MIPS_CALL_LO16:
15677 case BFD_RELOC_HI16_S_PCREL:
15678 case BFD_RELOC_LO16_PCREL:
15679 case BFD_RELOC_MIPS16_GPREL:
15680 case BFD_RELOC_MIPS16_GOT16:
15681 case BFD_RELOC_MIPS16_CALL16:
15682 case BFD_RELOC_MIPS16_HI16:
15683 case BFD_RELOC_MIPS16_HI16_S:
15684 case BFD_RELOC_MIPS16_LO16:
15685 case BFD_RELOC_MICROMIPS_GOT_DISP:
15686 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15687 case BFD_RELOC_MICROMIPS_GOT_OFST:
15688 case BFD_RELOC_MICROMIPS_SUB:
15689 case BFD_RELOC_MICROMIPS_HIGHEST:
15690 case BFD_RELOC_MICROMIPS_HIGHER:
15691 case BFD_RELOC_MICROMIPS_SCN_DISP:
15692 case BFD_RELOC_MICROMIPS_JALR:
15693 case BFD_RELOC_MICROMIPS_HI16:
15694 case BFD_RELOC_MICROMIPS_HI16_S:
15695 case BFD_RELOC_MICROMIPS_LO16:
15696 case BFD_RELOC_MICROMIPS_GPREL16:
15697 case BFD_RELOC_MICROMIPS_LITERAL:
15698 case BFD_RELOC_MICROMIPS_CALL16:
15699 case BFD_RELOC_MICROMIPS_GOT16:
15700 case BFD_RELOC_MICROMIPS_GOT_HI16:
15701 case BFD_RELOC_MICROMIPS_GOT_LO16:
15702 case BFD_RELOC_MICROMIPS_CALL_HI16:
15703 case BFD_RELOC_MICROMIPS_CALL_LO16:
15704 case BFD_RELOC_MIPS_EH:
15705 if (fixP->fx_done)
15706 {
15707 offsetT value;
15708
15709 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15710 {
15711 insn = read_reloc_insn (buf, fixP->fx_r_type);
15712 if (mips16_reloc_p (fixP->fx_r_type))
15713 insn |= mips16_immed_extend (value, 16);
15714 else
15715 insn |= (value & 0xffff);
15716 write_reloc_insn (buf, fixP->fx_r_type, insn);
15717 }
15718 else
15719 as_bad_where (fixP->fx_file, fixP->fx_line,
15720 _("unsupported constant in relocation"));
15721 }
15722 break;
15723
15724 case BFD_RELOC_64:
15725 /* This is handled like BFD_RELOC_32, but we output a sign
15726 extended value if we are only 32 bits. */
15727 if (fixP->fx_done)
15728 {
15729 if (8 <= sizeof (valueT))
15730 md_number_to_chars (buf, *valP, 8);
15731 else
15732 {
15733 valueT hiv;
15734
15735 if ((*valP & 0x80000000) != 0)
15736 hiv = 0xffffffff;
15737 else
15738 hiv = 0;
15739 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15740 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
15741 }
15742 }
15743 break;
15744
15745 case BFD_RELOC_RVA:
15746 case BFD_RELOC_32:
15747 case BFD_RELOC_32_PCREL:
15748 case BFD_RELOC_16:
15749 case BFD_RELOC_8:
15750 /* If we are deleting this reloc entry, we must fill in the
15751 value now. This can happen if we have a .word which is not
15752 resolved when it appears but is later defined. */
15753 if (fixP->fx_done)
15754 md_number_to_chars (buf, *valP, fixP->fx_size);
15755 break;
15756
15757 case BFD_RELOC_MIPS_21_PCREL_S2:
15758 fix_validate_branch (fixP, *valP);
15759 if (!fixP->fx_done)
15760 break;
15761
15762 if (*valP + 0x400000 <= 0x7fffff)
15763 {
15764 insn = read_insn (buf);
15765 insn |= (*valP >> 2) & 0x1fffff;
15766 write_insn (buf, insn);
15767 }
15768 else
15769 as_bad_where (fixP->fx_file, fixP->fx_line,
15770 _("branch out of range"));
15771 break;
15772
15773 case BFD_RELOC_MIPS_26_PCREL_S2:
15774 fix_validate_branch (fixP, *valP);
15775 if (!fixP->fx_done)
15776 break;
15777
15778 if (*valP + 0x8000000 <= 0xfffffff)
15779 {
15780 insn = read_insn (buf);
15781 insn |= (*valP >> 2) & 0x3ffffff;
15782 write_insn (buf, insn);
15783 }
15784 else
15785 as_bad_where (fixP->fx_file, fixP->fx_line,
15786 _("branch out of range"));
15787 break;
15788
15789 case BFD_RELOC_MIPS_18_PCREL_S3:
15790 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
15791 as_bad_where (fixP->fx_file, fixP->fx_line,
15792 _("PC-relative access using misaligned symbol (%lx)"),
15793 (long) S_GET_VALUE (fixP->fx_addsy));
15794 if ((fixP->fx_offset & 0x7) != 0)
15795 as_bad_where (fixP->fx_file, fixP->fx_line,
15796 _("PC-relative access using misaligned offset (%lx)"),
15797 (long) fixP->fx_offset);
15798 if (!fixP->fx_done)
15799 break;
15800
15801 if (*valP + 0x100000 <= 0x1fffff)
15802 {
15803 insn = read_insn (buf);
15804 insn |= (*valP >> 3) & 0x3ffff;
15805 write_insn (buf, insn);
15806 }
15807 else
15808 as_bad_where (fixP->fx_file, fixP->fx_line,
15809 _("PC-relative access out of range"));
15810 break;
15811
15812 case BFD_RELOC_MIPS_19_PCREL_S2:
15813 if ((*valP & 0x3) != 0)
15814 as_bad_where (fixP->fx_file, fixP->fx_line,
15815 _("PC-relative access to misaligned address (%lx)"),
15816 (long) *valP);
15817 if (!fixP->fx_done)
15818 break;
15819
15820 if (*valP + 0x100000 <= 0x1fffff)
15821 {
15822 insn = read_insn (buf);
15823 insn |= (*valP >> 2) & 0x7ffff;
15824 write_insn (buf, insn);
15825 }
15826 else
15827 as_bad_where (fixP->fx_file, fixP->fx_line,
15828 _("PC-relative access out of range"));
15829 break;
15830
15831 case BFD_RELOC_16_PCREL_S2:
15832 fix_validate_branch (fixP, *valP);
15833
15834 /* We need to save the bits in the instruction since fixup_segment()
15835 might be deleting the relocation entry (i.e., a branch within
15836 the current segment). */
15837 if (! fixP->fx_done)
15838 break;
15839
15840 /* Update old instruction data. */
15841 insn = read_insn (buf);
15842
15843 if (*valP + 0x20000 <= 0x3ffff)
15844 {
15845 insn |= (*valP >> 2) & 0xffff;
15846 write_insn (buf, insn);
15847 }
15848 else if (fixP->fx_tcbit2
15849 && fixP->fx_done
15850 && fixP->fx_frag->fr_address >= text_section->vma
15851 && (fixP->fx_frag->fr_address
15852 < text_section->vma + bfd_get_section_size (text_section))
15853 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15854 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15855 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15856 {
15857 /* The branch offset is too large. If this is an
15858 unconditional branch, and we are not generating PIC code,
15859 we can convert it to an absolute jump instruction. */
15860 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15861 insn = 0x0c000000; /* jal */
15862 else
15863 insn = 0x08000000; /* j */
15864 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15865 fixP->fx_done = 0;
15866 fixP->fx_addsy = section_symbol (text_section);
15867 *valP += md_pcrel_from (fixP);
15868 write_insn (buf, insn);
15869 }
15870 else
15871 {
15872 /* If we got here, we have branch-relaxation disabled,
15873 and there's nothing we can do to fix this instruction
15874 without turning it into a longer sequence. */
15875 as_bad_where (fixP->fx_file, fixP->fx_line,
15876 _("branch out of range"));
15877 }
15878 break;
15879
15880 case BFD_RELOC_MIPS16_16_PCREL_S1:
15881 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15882 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15883 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15884 gas_assert (!fixP->fx_done);
15885 if (fix_bad_cross_mode_branch_p (fixP))
15886 as_bad_where (fixP->fx_file, fixP->fx_line,
15887 _("branch to a symbol in another ISA mode"));
15888 else if (fixP->fx_addsy
15889 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15890 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15891 && (fixP->fx_offset & 0x1) != 0)
15892 as_bad_where (fixP->fx_file, fixP->fx_line,
15893 _("branch to misaligned address (0x%lx)"),
15894 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15895 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15896 as_bad_where (fixP->fx_file, fixP->fx_line,
15897 _("cannot encode misaligned addend "
15898 "in the relocatable field (0x%lx)"),
15899 (long) fixP->fx_offset);
15900 break;
15901
15902 case BFD_RELOC_VTABLE_INHERIT:
15903 fixP->fx_done = 0;
15904 if (fixP->fx_addsy
15905 && !S_IS_DEFINED (fixP->fx_addsy)
15906 && !S_IS_WEAK (fixP->fx_addsy))
15907 S_SET_WEAK (fixP->fx_addsy);
15908 break;
15909
15910 case BFD_RELOC_NONE:
15911 case BFD_RELOC_VTABLE_ENTRY:
15912 fixP->fx_done = 0;
15913 break;
15914
15915 default:
15916 abort ();
15917 }
15918
15919 /* Remember value for tc_gen_reloc. */
15920 fixP->fx_addnumber = *valP;
15921 }
15922
15923 static symbolS *
15924 get_symbol (void)
15925 {
15926 int c;
15927 char *name;
15928 symbolS *p;
15929
15930 c = get_symbol_name (&name);
15931 p = (symbolS *) symbol_find_or_make (name);
15932 (void) restore_line_pointer (c);
15933 return p;
15934 }
15935
15936 /* Align the current frag to a given power of two. If a particular
15937 fill byte should be used, FILL points to an integer that contains
15938 that byte, otherwise FILL is null.
15939
15940 This function used to have the comment:
15941
15942 The MIPS assembler also automatically adjusts any preceding label.
15943
15944 The implementation therefore applied the adjustment to a maximum of
15945 one label. However, other label adjustments are applied to batches
15946 of labels, and adjusting just one caused problems when new labels
15947 were added for the sake of debugging or unwind information.
15948 We therefore adjust all preceding labels (given as LABELS) instead. */
15949
15950 static void
15951 mips_align (int to, int *fill, struct insn_label_list *labels)
15952 {
15953 mips_emit_delays ();
15954 mips_record_compressed_mode ();
15955 if (fill == NULL && subseg_text_p (now_seg))
15956 frag_align_code (to, 0);
15957 else
15958 frag_align (to, fill ? *fill : 0, 0);
15959 record_alignment (now_seg, to);
15960 mips_move_labels (labels, FALSE);
15961 }
15962
15963 /* Align to a given power of two. .align 0 turns off the automatic
15964 alignment used by the data creating pseudo-ops. */
15965
15966 static void
15967 s_align (int x ATTRIBUTE_UNUSED)
15968 {
15969 int temp, fill_value, *fill_ptr;
15970 long max_alignment = 28;
15971
15972 /* o Note that the assembler pulls down any immediately preceding label
15973 to the aligned address.
15974 o It's not documented but auto alignment is reinstated by
15975 a .align pseudo instruction.
15976 o Note also that after auto alignment is turned off the mips assembler
15977 issues an error on attempt to assemble an improperly aligned data item.
15978 We don't. */
15979
15980 temp = get_absolute_expression ();
15981 if (temp > max_alignment)
15982 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
15983 else if (temp < 0)
15984 {
15985 as_warn (_("alignment negative, 0 assumed"));
15986 temp = 0;
15987 }
15988 if (*input_line_pointer == ',')
15989 {
15990 ++input_line_pointer;
15991 fill_value = get_absolute_expression ();
15992 fill_ptr = &fill_value;
15993 }
15994 else
15995 fill_ptr = 0;
15996 if (temp)
15997 {
15998 segment_info_type *si = seg_info (now_seg);
15999 struct insn_label_list *l = si->label_list;
16000 /* Auto alignment should be switched on by next section change. */
16001 auto_align = 1;
16002 mips_align (temp, fill_ptr, l);
16003 }
16004 else
16005 {
16006 auto_align = 0;
16007 }
16008
16009 demand_empty_rest_of_line ();
16010 }
16011
16012 static void
16013 s_change_sec (int sec)
16014 {
16015 segT seg;
16016
16017 /* The ELF backend needs to know that we are changing sections, so
16018 that .previous works correctly. We could do something like check
16019 for an obj_section_change_hook macro, but that might be confusing
16020 as it would not be appropriate to use it in the section changing
16021 functions in read.c, since obj-elf.c intercepts those. FIXME:
16022 This should be cleaner, somehow. */
16023 obj_elf_section_change_hook ();
16024
16025 mips_emit_delays ();
16026
16027 switch (sec)
16028 {
16029 case 't':
16030 s_text (0);
16031 break;
16032 case 'd':
16033 s_data (0);
16034 break;
16035 case 'b':
16036 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16037 demand_empty_rest_of_line ();
16038 break;
16039
16040 case 'r':
16041 seg = subseg_new (RDATA_SECTION_NAME,
16042 (subsegT) get_absolute_expression ());
16043 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16044 | SEC_READONLY | SEC_RELOC
16045 | SEC_DATA));
16046 if (strncmp (TARGET_OS, "elf", 3) != 0)
16047 record_alignment (seg, 4);
16048 demand_empty_rest_of_line ();
16049 break;
16050
16051 case 's':
16052 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
16053 bfd_set_section_flags (stdoutput, seg,
16054 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16055 if (strncmp (TARGET_OS, "elf", 3) != 0)
16056 record_alignment (seg, 4);
16057 demand_empty_rest_of_line ();
16058 break;
16059
16060 case 'B':
16061 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16062 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16063 if (strncmp (TARGET_OS, "elf", 3) != 0)
16064 record_alignment (seg, 4);
16065 demand_empty_rest_of_line ();
16066 break;
16067 }
16068
16069 auto_align = 1;
16070 }
16071
16072 void
16073 s_change_section (int ignore ATTRIBUTE_UNUSED)
16074 {
16075 char *saved_ilp;
16076 char *section_name;
16077 char c, endc;
16078 char next_c = 0;
16079 int section_type;
16080 int section_flag;
16081 int section_entry_size;
16082 int section_alignment;
16083
16084 saved_ilp = input_line_pointer;
16085 endc = get_symbol_name (&section_name);
16086 c = (endc == '"' ? input_line_pointer[1] : endc);
16087 if (c)
16088 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
16089
16090 /* Do we have .section Name<,"flags">? */
16091 if (c != ',' || (c == ',' && next_c == '"'))
16092 {
16093 /* Just after name is now '\0'. */
16094 (void) restore_line_pointer (endc);
16095 input_line_pointer = saved_ilp;
16096 obj_elf_section (ignore);
16097 return;
16098 }
16099
16100 section_name = xstrdup (section_name);
16101 c = restore_line_pointer (endc);
16102
16103 input_line_pointer++;
16104
16105 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16106 if (c == ',')
16107 section_type = get_absolute_expression ();
16108 else
16109 section_type = 0;
16110
16111 if (*input_line_pointer++ == ',')
16112 section_flag = get_absolute_expression ();
16113 else
16114 section_flag = 0;
16115
16116 if (*input_line_pointer++ == ',')
16117 section_entry_size = get_absolute_expression ();
16118 else
16119 section_entry_size = 0;
16120
16121 if (*input_line_pointer++ == ',')
16122 section_alignment = get_absolute_expression ();
16123 else
16124 section_alignment = 0;
16125
16126 /* FIXME: really ignore? */
16127 (void) section_alignment;
16128
16129 /* When using the generic form of .section (as implemented by obj-elf.c),
16130 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16131 traditionally had to fall back on the more common @progbits instead.
16132
16133 There's nothing really harmful in this, since bfd will correct
16134 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
16135 means that, for backwards compatibility, the special_section entries
16136 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16137
16138 Even so, we shouldn't force users of the MIPS .section syntax to
16139 incorrectly label the sections as SHT_PROGBITS. The best compromise
16140 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16141 generic type-checking code. */
16142 if (section_type == SHT_MIPS_DWARF)
16143 section_type = SHT_PROGBITS;
16144
16145 obj_elf_change_section (section_name, section_type, 0, section_flag,
16146 section_entry_size, 0, 0, 0);
16147
16148 if (now_seg->name != section_name)
16149 free (section_name);
16150 }
16151
16152 void
16153 mips_enable_auto_align (void)
16154 {
16155 auto_align = 1;
16156 }
16157
16158 static void
16159 s_cons (int log_size)
16160 {
16161 segment_info_type *si = seg_info (now_seg);
16162 struct insn_label_list *l = si->label_list;
16163
16164 mips_emit_delays ();
16165 if (log_size > 0 && auto_align)
16166 mips_align (log_size, 0, l);
16167 cons (1 << log_size);
16168 mips_clear_insn_labels ();
16169 }
16170
16171 static void
16172 s_float_cons (int type)
16173 {
16174 segment_info_type *si = seg_info (now_seg);
16175 struct insn_label_list *l = si->label_list;
16176
16177 mips_emit_delays ();
16178
16179 if (auto_align)
16180 {
16181 if (type == 'd')
16182 mips_align (3, 0, l);
16183 else
16184 mips_align (2, 0, l);
16185 }
16186
16187 float_cons (type);
16188 mips_clear_insn_labels ();
16189 }
16190
16191 /* Handle .globl. We need to override it because on Irix 5 you are
16192 permitted to say
16193 .globl foo .text
16194 where foo is an undefined symbol, to mean that foo should be
16195 considered to be the address of a function. */
16196
16197 static void
16198 s_mips_globl (int x ATTRIBUTE_UNUSED)
16199 {
16200 char *name;
16201 int c;
16202 symbolS *symbolP;
16203 flagword flag;
16204
16205 do
16206 {
16207 c = get_symbol_name (&name);
16208 symbolP = symbol_find_or_make (name);
16209 S_SET_EXTERNAL (symbolP);
16210
16211 *input_line_pointer = c;
16212 SKIP_WHITESPACE_AFTER_NAME ();
16213
16214 /* On Irix 5, every global symbol that is not explicitly labelled as
16215 being a function is apparently labelled as being an object. */
16216 flag = BSF_OBJECT;
16217
16218 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16219 && (*input_line_pointer != ','))
16220 {
16221 char *secname;
16222 asection *sec;
16223
16224 c = get_symbol_name (&secname);
16225 sec = bfd_get_section_by_name (stdoutput, secname);
16226 if (sec == NULL)
16227 as_bad (_("%s: no such section"), secname);
16228 (void) restore_line_pointer (c);
16229
16230 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16231 flag = BSF_FUNCTION;
16232 }
16233
16234 symbol_get_bfdsym (symbolP)->flags |= flag;
16235
16236 c = *input_line_pointer;
16237 if (c == ',')
16238 {
16239 input_line_pointer++;
16240 SKIP_WHITESPACE ();
16241 if (is_end_of_line[(unsigned char) *input_line_pointer])
16242 c = '\n';
16243 }
16244 }
16245 while (c == ',');
16246
16247 demand_empty_rest_of_line ();
16248 }
16249
16250 static void
16251 s_option (int x ATTRIBUTE_UNUSED)
16252 {
16253 char *opt;
16254 char c;
16255
16256 c = get_symbol_name (&opt);
16257
16258 if (*opt == 'O')
16259 {
16260 /* FIXME: What does this mean? */
16261 }
16262 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
16263 {
16264 int i;
16265
16266 i = atoi (opt + 3);
16267 if (i != 0 && i != 2)
16268 as_bad (_(".option pic%d not supported"), i);
16269 else if (mips_pic == VXWORKS_PIC)
16270 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16271 else if (i == 0)
16272 mips_pic = NO_PIC;
16273 else if (i == 2)
16274 {
16275 mips_pic = SVR4_PIC;
16276 mips_abicalls = TRUE;
16277 }
16278
16279 if (mips_pic == SVR4_PIC)
16280 {
16281 if (g_switch_seen && g_switch_value != 0)
16282 as_warn (_("-G may not be used with SVR4 PIC code"));
16283 g_switch_value = 0;
16284 bfd_set_gp_size (stdoutput, 0);
16285 }
16286 }
16287 else
16288 as_warn (_("unrecognized option \"%s\""), opt);
16289
16290 (void) restore_line_pointer (c);
16291 demand_empty_rest_of_line ();
16292 }
16293
16294 /* This structure is used to hold a stack of .set values. */
16295
16296 struct mips_option_stack
16297 {
16298 struct mips_option_stack *next;
16299 struct mips_set_options options;
16300 };
16301
16302 static struct mips_option_stack *mips_opts_stack;
16303
16304 /* Return status for .set/.module option handling. */
16305
16306 enum code_option_type
16307 {
16308 /* Unrecognized option. */
16309 OPTION_TYPE_BAD = -1,
16310
16311 /* Ordinary option. */
16312 OPTION_TYPE_NORMAL,
16313
16314 /* ISA changing option. */
16315 OPTION_TYPE_ISA
16316 };
16317
16318 /* Handle common .set/.module options. Return status indicating option
16319 type. */
16320
16321 static enum code_option_type
16322 parse_code_option (char * name)
16323 {
16324 bfd_boolean isa_set = FALSE;
16325 const struct mips_ase *ase;
16326
16327 if (strncmp (name, "at=", 3) == 0)
16328 {
16329 char *s = name + 3;
16330
16331 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16332 as_bad (_("unrecognized register name `%s'"), s);
16333 }
16334 else if (strcmp (name, "at") == 0)
16335 mips_opts.at = ATREG;
16336 else if (strcmp (name, "noat") == 0)
16337 mips_opts.at = ZERO;
16338 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16339 mips_opts.nomove = 0;
16340 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16341 mips_opts.nomove = 1;
16342 else if (strcmp (name, "bopt") == 0)
16343 mips_opts.nobopt = 0;
16344 else if (strcmp (name, "nobopt") == 0)
16345 mips_opts.nobopt = 1;
16346 else if (strcmp (name, "gp=32") == 0)
16347 mips_opts.gp = 32;
16348 else if (strcmp (name, "gp=64") == 0)
16349 mips_opts.gp = 64;
16350 else if (strcmp (name, "fp=32") == 0)
16351 mips_opts.fp = 32;
16352 else if (strcmp (name, "fp=xx") == 0)
16353 mips_opts.fp = 0;
16354 else if (strcmp (name, "fp=64") == 0)
16355 mips_opts.fp = 64;
16356 else if (strcmp (name, "softfloat") == 0)
16357 mips_opts.soft_float = 1;
16358 else if (strcmp (name, "hardfloat") == 0)
16359 mips_opts.soft_float = 0;
16360 else if (strcmp (name, "singlefloat") == 0)
16361 mips_opts.single_float = 1;
16362 else if (strcmp (name, "doublefloat") == 0)
16363 mips_opts.single_float = 0;
16364 else if (strcmp (name, "nooddspreg") == 0)
16365 mips_opts.oddspreg = 0;
16366 else if (strcmp (name, "oddspreg") == 0)
16367 mips_opts.oddspreg = 1;
16368 else if (strcmp (name, "mips16") == 0
16369 || strcmp (name, "MIPS-16") == 0)
16370 mips_opts.mips16 = 1;
16371 else if (strcmp (name, "nomips16") == 0
16372 || strcmp (name, "noMIPS-16") == 0)
16373 mips_opts.mips16 = 0;
16374 else if (strcmp (name, "micromips") == 0)
16375 mips_opts.micromips = 1;
16376 else if (strcmp (name, "nomicromips") == 0)
16377 mips_opts.micromips = 0;
16378 else if (name[0] == 'n'
16379 && name[1] == 'o'
16380 && (ase = mips_lookup_ase (name + 2)))
16381 mips_set_ase (ase, &mips_opts, FALSE);
16382 else if ((ase = mips_lookup_ase (name)))
16383 mips_set_ase (ase, &mips_opts, TRUE);
16384 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16385 {
16386 /* Permit the user to change the ISA and architecture on the fly.
16387 Needless to say, misuse can cause serious problems. */
16388 if (strncmp (name, "arch=", 5) == 0)
16389 {
16390 const struct mips_cpu_info *p;
16391
16392 p = mips_parse_cpu ("internal use", name + 5);
16393 if (!p)
16394 as_bad (_("unknown architecture %s"), name + 5);
16395 else
16396 {
16397 mips_opts.arch = p->cpu;
16398 mips_opts.isa = p->isa;
16399 isa_set = TRUE;
16400 }
16401 }
16402 else if (strncmp (name, "mips", 4) == 0)
16403 {
16404 const struct mips_cpu_info *p;
16405
16406 p = mips_parse_cpu ("internal use", name);
16407 if (!p)
16408 as_bad (_("unknown ISA level %s"), name + 4);
16409 else
16410 {
16411 mips_opts.arch = p->cpu;
16412 mips_opts.isa = p->isa;
16413 isa_set = TRUE;
16414 }
16415 }
16416 else
16417 as_bad (_("unknown ISA or architecture %s"), name);
16418 }
16419 else if (strcmp (name, "autoextend") == 0)
16420 mips_opts.noautoextend = 0;
16421 else if (strcmp (name, "noautoextend") == 0)
16422 mips_opts.noautoextend = 1;
16423 else if (strcmp (name, "insn32") == 0)
16424 mips_opts.insn32 = TRUE;
16425 else if (strcmp (name, "noinsn32") == 0)
16426 mips_opts.insn32 = FALSE;
16427 else if (strcmp (name, "sym32") == 0)
16428 mips_opts.sym32 = TRUE;
16429 else if (strcmp (name, "nosym32") == 0)
16430 mips_opts.sym32 = FALSE;
16431 else
16432 return OPTION_TYPE_BAD;
16433
16434 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
16435 }
16436
16437 /* Handle the .set pseudo-op. */
16438
16439 static void
16440 s_mipsset (int x ATTRIBUTE_UNUSED)
16441 {
16442 enum code_option_type type = OPTION_TYPE_NORMAL;
16443 char *name = input_line_pointer, ch;
16444
16445 file_mips_check_options ();
16446
16447 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16448 ++input_line_pointer;
16449 ch = *input_line_pointer;
16450 *input_line_pointer = '\0';
16451
16452 if (strchr (name, ','))
16453 {
16454 /* Generic ".set" directive; use the generic handler. */
16455 *input_line_pointer = ch;
16456 input_line_pointer = name;
16457 s_set (0);
16458 return;
16459 }
16460
16461 if (strcmp (name, "reorder") == 0)
16462 {
16463 if (mips_opts.noreorder)
16464 end_noreorder ();
16465 }
16466 else if (strcmp (name, "noreorder") == 0)
16467 {
16468 if (!mips_opts.noreorder)
16469 start_noreorder ();
16470 }
16471 else if (strcmp (name, "macro") == 0)
16472 mips_opts.warn_about_macros = 0;
16473 else if (strcmp (name, "nomacro") == 0)
16474 {
16475 if (mips_opts.noreorder == 0)
16476 as_bad (_("`noreorder' must be set before `nomacro'"));
16477 mips_opts.warn_about_macros = 1;
16478 }
16479 else if (strcmp (name, "gp=default") == 0)
16480 mips_opts.gp = file_mips_opts.gp;
16481 else if (strcmp (name, "fp=default") == 0)
16482 mips_opts.fp = file_mips_opts.fp;
16483 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16484 {
16485 mips_opts.isa = file_mips_opts.isa;
16486 mips_opts.arch = file_mips_opts.arch;
16487 mips_opts.gp = file_mips_opts.gp;
16488 mips_opts.fp = file_mips_opts.fp;
16489 }
16490 else if (strcmp (name, "push") == 0)
16491 {
16492 struct mips_option_stack *s;
16493
16494 s = XNEW (struct mips_option_stack);
16495 s->next = mips_opts_stack;
16496 s->options = mips_opts;
16497 mips_opts_stack = s;
16498 }
16499 else if (strcmp (name, "pop") == 0)
16500 {
16501 struct mips_option_stack *s;
16502
16503 s = mips_opts_stack;
16504 if (s == NULL)
16505 as_bad (_(".set pop with no .set push"));
16506 else
16507 {
16508 /* If we're changing the reorder mode we need to handle
16509 delay slots correctly. */
16510 if (s->options.noreorder && ! mips_opts.noreorder)
16511 start_noreorder ();
16512 else if (! s->options.noreorder && mips_opts.noreorder)
16513 end_noreorder ();
16514
16515 mips_opts = s->options;
16516 mips_opts_stack = s->next;
16517 free (s);
16518 }
16519 }
16520 else
16521 {
16522 type = parse_code_option (name);
16523 if (type == OPTION_TYPE_BAD)
16524 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16525 }
16526
16527 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16528 registers based on what is supported by the arch/cpu. */
16529 if (type == OPTION_TYPE_ISA)
16530 {
16531 switch (mips_opts.isa)
16532 {
16533 case 0:
16534 break;
16535 case ISA_MIPS1:
16536 /* MIPS I cannot support FPXX. */
16537 mips_opts.fp = 32;
16538 /* fall-through. */
16539 case ISA_MIPS2:
16540 case ISA_MIPS32:
16541 case ISA_MIPS32R2:
16542 case ISA_MIPS32R3:
16543 case ISA_MIPS32R5:
16544 mips_opts.gp = 32;
16545 if (mips_opts.fp != 0)
16546 mips_opts.fp = 32;
16547 break;
16548 case ISA_MIPS32R6:
16549 mips_opts.gp = 32;
16550 mips_opts.fp = 64;
16551 break;
16552 case ISA_MIPS3:
16553 case ISA_MIPS4:
16554 case ISA_MIPS5:
16555 case ISA_MIPS64:
16556 case ISA_MIPS64R2:
16557 case ISA_MIPS64R3:
16558 case ISA_MIPS64R5:
16559 case ISA_MIPS64R6:
16560 mips_opts.gp = 64;
16561 if (mips_opts.fp != 0)
16562 {
16563 if (mips_opts.arch == CPU_R5900)
16564 mips_opts.fp = 32;
16565 else
16566 mips_opts.fp = 64;
16567 }
16568 break;
16569 default:
16570 as_bad (_("unknown ISA level %s"), name + 4);
16571 break;
16572 }
16573 }
16574
16575 mips_check_options (&mips_opts, FALSE);
16576
16577 mips_check_isa_supports_ases ();
16578 *input_line_pointer = ch;
16579 demand_empty_rest_of_line ();
16580 }
16581
16582 /* Handle the .module pseudo-op. */
16583
16584 static void
16585 s_module (int ignore ATTRIBUTE_UNUSED)
16586 {
16587 char *name = input_line_pointer, ch;
16588
16589 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16590 ++input_line_pointer;
16591 ch = *input_line_pointer;
16592 *input_line_pointer = '\0';
16593
16594 if (!file_mips_opts_checked)
16595 {
16596 if (parse_code_option (name) == OPTION_TYPE_BAD)
16597 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16598
16599 /* Update module level settings from mips_opts. */
16600 file_mips_opts = mips_opts;
16601 }
16602 else
16603 as_bad (_(".module is not permitted after generating code"));
16604
16605 *input_line_pointer = ch;
16606 demand_empty_rest_of_line ();
16607 }
16608
16609 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
16610 .option pic2. It means to generate SVR4 PIC calls. */
16611
16612 static void
16613 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16614 {
16615 mips_pic = SVR4_PIC;
16616 mips_abicalls = TRUE;
16617
16618 if (g_switch_seen && g_switch_value != 0)
16619 as_warn (_("-G may not be used with SVR4 PIC code"));
16620 g_switch_value = 0;
16621
16622 bfd_set_gp_size (stdoutput, 0);
16623 demand_empty_rest_of_line ();
16624 }
16625
16626 /* Handle the .cpload pseudo-op. This is used when generating SVR4
16627 PIC code. It sets the $gp register for the function based on the
16628 function address, which is in the register named in the argument.
16629 This uses a relocation against _gp_disp, which is handled specially
16630 by the linker. The result is:
16631 lui $gp,%hi(_gp_disp)
16632 addiu $gp,$gp,%lo(_gp_disp)
16633 addu $gp,$gp,.cpload argument
16634 The .cpload argument is normally $25 == $t9.
16635
16636 The -mno-shared option changes this to:
16637 lui $gp,%hi(__gnu_local_gp)
16638 addiu $gp,$gp,%lo(__gnu_local_gp)
16639 and the argument is ignored. This saves an instruction, but the
16640 resulting code is not position independent; it uses an absolute
16641 address for __gnu_local_gp. Thus code assembled with -mno-shared
16642 can go into an ordinary executable, but not into a shared library. */
16643
16644 static void
16645 s_cpload (int ignore ATTRIBUTE_UNUSED)
16646 {
16647 expressionS ex;
16648 int reg;
16649 int in_shared;
16650
16651 file_mips_check_options ();
16652
16653 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16654 .cpload is ignored. */
16655 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16656 {
16657 s_ignore (0);
16658 return;
16659 }
16660
16661 if (mips_opts.mips16)
16662 {
16663 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16664 ignore_rest_of_line ();
16665 return;
16666 }
16667
16668 /* .cpload should be in a .set noreorder section. */
16669 if (mips_opts.noreorder == 0)
16670 as_warn (_(".cpload not in noreorder section"));
16671
16672 reg = tc_get_register (0);
16673
16674 /* If we need to produce a 64-bit address, we are better off using
16675 the default instruction sequence. */
16676 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16677
16678 ex.X_op = O_symbol;
16679 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16680 "__gnu_local_gp");
16681 ex.X_op_symbol = NULL;
16682 ex.X_add_number = 0;
16683
16684 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16685 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16686
16687 mips_mark_labels ();
16688 mips_assembling_insn = TRUE;
16689
16690 macro_start ();
16691 macro_build_lui (&ex, mips_gp_register);
16692 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16693 mips_gp_register, BFD_RELOC_LO16);
16694 if (in_shared)
16695 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16696 mips_gp_register, reg);
16697 macro_end ();
16698
16699 mips_assembling_insn = FALSE;
16700 demand_empty_rest_of_line ();
16701 }
16702
16703 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16704 .cpsetup $reg1, offset|$reg2, label
16705
16706 If offset is given, this results in:
16707 sd $gp, offset($sp)
16708 lui $gp, %hi(%neg(%gp_rel(label)))
16709 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16710 daddu $gp, $gp, $reg1
16711
16712 If $reg2 is given, this results in:
16713 or $reg2, $gp, $0
16714 lui $gp, %hi(%neg(%gp_rel(label)))
16715 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16716 daddu $gp, $gp, $reg1
16717 $reg1 is normally $25 == $t9.
16718
16719 The -mno-shared option replaces the last three instructions with
16720 lui $gp,%hi(_gp)
16721 addiu $gp,$gp,%lo(_gp) */
16722
16723 static void
16724 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16725 {
16726 expressionS ex_off;
16727 expressionS ex_sym;
16728 int reg1;
16729
16730 file_mips_check_options ();
16731
16732 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16733 We also need NewABI support. */
16734 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16735 {
16736 s_ignore (0);
16737 return;
16738 }
16739
16740 if (mips_opts.mips16)
16741 {
16742 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16743 ignore_rest_of_line ();
16744 return;
16745 }
16746
16747 reg1 = tc_get_register (0);
16748 SKIP_WHITESPACE ();
16749 if (*input_line_pointer != ',')
16750 {
16751 as_bad (_("missing argument separator ',' for .cpsetup"));
16752 return;
16753 }
16754 else
16755 ++input_line_pointer;
16756 SKIP_WHITESPACE ();
16757 if (*input_line_pointer == '$')
16758 {
16759 mips_cpreturn_register = tc_get_register (0);
16760 mips_cpreturn_offset = -1;
16761 }
16762 else
16763 {
16764 mips_cpreturn_offset = get_absolute_expression ();
16765 mips_cpreturn_register = -1;
16766 }
16767 SKIP_WHITESPACE ();
16768 if (*input_line_pointer != ',')
16769 {
16770 as_bad (_("missing argument separator ',' for .cpsetup"));
16771 return;
16772 }
16773 else
16774 ++input_line_pointer;
16775 SKIP_WHITESPACE ();
16776 expression (&ex_sym);
16777
16778 mips_mark_labels ();
16779 mips_assembling_insn = TRUE;
16780
16781 macro_start ();
16782 if (mips_cpreturn_register == -1)
16783 {
16784 ex_off.X_op = O_constant;
16785 ex_off.X_add_symbol = NULL;
16786 ex_off.X_op_symbol = NULL;
16787 ex_off.X_add_number = mips_cpreturn_offset;
16788
16789 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16790 BFD_RELOC_LO16, SP);
16791 }
16792 else
16793 move_register (mips_cpreturn_register, mips_gp_register);
16794
16795 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16796 {
16797 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16798 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16799 BFD_RELOC_HI16_S);
16800
16801 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16802 mips_gp_register, -1, BFD_RELOC_GPREL16,
16803 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16804
16805 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16806 mips_gp_register, reg1);
16807 }
16808 else
16809 {
16810 expressionS ex;
16811
16812 ex.X_op = O_symbol;
16813 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16814 ex.X_op_symbol = NULL;
16815 ex.X_add_number = 0;
16816
16817 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16818 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16819
16820 macro_build_lui (&ex, mips_gp_register);
16821 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16822 mips_gp_register, BFD_RELOC_LO16);
16823 }
16824
16825 macro_end ();
16826
16827 mips_assembling_insn = FALSE;
16828 demand_empty_rest_of_line ();
16829 }
16830
16831 static void
16832 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16833 {
16834 file_mips_check_options ();
16835
16836 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16837 .cplocal is ignored. */
16838 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16839 {
16840 s_ignore (0);
16841 return;
16842 }
16843
16844 if (mips_opts.mips16)
16845 {
16846 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16847 ignore_rest_of_line ();
16848 return;
16849 }
16850
16851 mips_gp_register = tc_get_register (0);
16852 demand_empty_rest_of_line ();
16853 }
16854
16855 /* Handle the .cprestore pseudo-op. This stores $gp into a given
16856 offset from $sp. The offset is remembered, and after making a PIC
16857 call $gp is restored from that location. */
16858
16859 static void
16860 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16861 {
16862 expressionS ex;
16863
16864 file_mips_check_options ();
16865
16866 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16867 .cprestore is ignored. */
16868 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16869 {
16870 s_ignore (0);
16871 return;
16872 }
16873
16874 if (mips_opts.mips16)
16875 {
16876 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16877 ignore_rest_of_line ();
16878 return;
16879 }
16880
16881 mips_cprestore_offset = get_absolute_expression ();
16882 mips_cprestore_valid = 1;
16883
16884 ex.X_op = O_constant;
16885 ex.X_add_symbol = NULL;
16886 ex.X_op_symbol = NULL;
16887 ex.X_add_number = mips_cprestore_offset;
16888
16889 mips_mark_labels ();
16890 mips_assembling_insn = TRUE;
16891
16892 macro_start ();
16893 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16894 SP, HAVE_64BIT_ADDRESSES);
16895 macro_end ();
16896
16897 mips_assembling_insn = FALSE;
16898 demand_empty_rest_of_line ();
16899 }
16900
16901 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16902 was given in the preceding .cpsetup, it results in:
16903 ld $gp, offset($sp)
16904
16905 If a register $reg2 was given there, it results in:
16906 or $gp, $reg2, $0 */
16907
16908 static void
16909 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16910 {
16911 expressionS ex;
16912
16913 file_mips_check_options ();
16914
16915 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16916 We also need NewABI support. */
16917 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16918 {
16919 s_ignore (0);
16920 return;
16921 }
16922
16923 if (mips_opts.mips16)
16924 {
16925 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16926 ignore_rest_of_line ();
16927 return;
16928 }
16929
16930 mips_mark_labels ();
16931 mips_assembling_insn = TRUE;
16932
16933 macro_start ();
16934 if (mips_cpreturn_register == -1)
16935 {
16936 ex.X_op = O_constant;
16937 ex.X_add_symbol = NULL;
16938 ex.X_op_symbol = NULL;
16939 ex.X_add_number = mips_cpreturn_offset;
16940
16941 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16942 }
16943 else
16944 move_register (mips_gp_register, mips_cpreturn_register);
16945
16946 macro_end ();
16947
16948 mips_assembling_insn = FALSE;
16949 demand_empty_rest_of_line ();
16950 }
16951
16952 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16953 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16954 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16955 debug information or MIPS16 TLS. */
16956
16957 static void
16958 s_tls_rel_directive (const size_t bytes, const char *dirstr,
16959 bfd_reloc_code_real_type rtype)
16960 {
16961 expressionS ex;
16962 char *p;
16963
16964 expression (&ex);
16965
16966 if (ex.X_op != O_symbol)
16967 {
16968 as_bad (_("unsupported use of %s"), dirstr);
16969 ignore_rest_of_line ();
16970 }
16971
16972 p = frag_more (bytes);
16973 md_number_to_chars (p, 0, bytes);
16974 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
16975 demand_empty_rest_of_line ();
16976 mips_clear_insn_labels ();
16977 }
16978
16979 /* Handle .dtprelword. */
16980
16981 static void
16982 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16983 {
16984 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
16985 }
16986
16987 /* Handle .dtpreldword. */
16988
16989 static void
16990 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16991 {
16992 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16993 }
16994
16995 /* Handle .tprelword. */
16996
16997 static void
16998 s_tprelword (int ignore ATTRIBUTE_UNUSED)
16999 {
17000 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17001 }
17002
17003 /* Handle .tpreldword. */
17004
17005 static void
17006 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17007 {
17008 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
17009 }
17010
17011 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17012 code. It sets the offset to use in gp_rel relocations. */
17013
17014 static void
17015 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
17016 {
17017 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17018 We also need NewABI support. */
17019 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17020 {
17021 s_ignore (0);
17022 return;
17023 }
17024
17025 mips_gprel_offset = get_absolute_expression ();
17026
17027 demand_empty_rest_of_line ();
17028 }
17029
17030 /* Handle the .gpword pseudo-op. This is used when generating PIC
17031 code. It generates a 32 bit GP relative reloc. */
17032
17033 static void
17034 s_gpword (int ignore ATTRIBUTE_UNUSED)
17035 {
17036 segment_info_type *si;
17037 struct insn_label_list *l;
17038 expressionS ex;
17039 char *p;
17040
17041 /* When not generating PIC code, this is treated as .word. */
17042 if (mips_pic != SVR4_PIC)
17043 {
17044 s_cons (2);
17045 return;
17046 }
17047
17048 si = seg_info (now_seg);
17049 l = si->label_list;
17050 mips_emit_delays ();
17051 if (auto_align)
17052 mips_align (2, 0, l);
17053
17054 expression (&ex);
17055 mips_clear_insn_labels ();
17056
17057 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17058 {
17059 as_bad (_("unsupported use of .gpword"));
17060 ignore_rest_of_line ();
17061 }
17062
17063 p = frag_more (4);
17064 md_number_to_chars (p, 0, 4);
17065 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17066 BFD_RELOC_GPREL32);
17067
17068 demand_empty_rest_of_line ();
17069 }
17070
17071 static void
17072 s_gpdword (int ignore ATTRIBUTE_UNUSED)
17073 {
17074 segment_info_type *si;
17075 struct insn_label_list *l;
17076 expressionS ex;
17077 char *p;
17078
17079 /* When not generating PIC code, this is treated as .dword. */
17080 if (mips_pic != SVR4_PIC)
17081 {
17082 s_cons (3);
17083 return;
17084 }
17085
17086 si = seg_info (now_seg);
17087 l = si->label_list;
17088 mips_emit_delays ();
17089 if (auto_align)
17090 mips_align (3, 0, l);
17091
17092 expression (&ex);
17093 mips_clear_insn_labels ();
17094
17095 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17096 {
17097 as_bad (_("unsupported use of .gpdword"));
17098 ignore_rest_of_line ();
17099 }
17100
17101 p = frag_more (8);
17102 md_number_to_chars (p, 0, 8);
17103 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17104 BFD_RELOC_GPREL32)->fx_tcbit = 1;
17105
17106 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
17107 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17108 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
17109
17110 demand_empty_rest_of_line ();
17111 }
17112
17113 /* Handle the .ehword pseudo-op. This is used when generating unwinding
17114 tables. It generates a R_MIPS_EH reloc. */
17115
17116 static void
17117 s_ehword (int ignore ATTRIBUTE_UNUSED)
17118 {
17119 expressionS ex;
17120 char *p;
17121
17122 mips_emit_delays ();
17123
17124 expression (&ex);
17125 mips_clear_insn_labels ();
17126
17127 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17128 {
17129 as_bad (_("unsupported use of .ehword"));
17130 ignore_rest_of_line ();
17131 }
17132
17133 p = frag_more (4);
17134 md_number_to_chars (p, 0, 4);
17135 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17136 BFD_RELOC_32_PCREL);
17137
17138 demand_empty_rest_of_line ();
17139 }
17140
17141 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
17142 tables in SVR4 PIC code. */
17143
17144 static void
17145 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17146 {
17147 int reg;
17148
17149 file_mips_check_options ();
17150
17151 /* This is ignored when not generating SVR4 PIC code. */
17152 if (mips_pic != SVR4_PIC)
17153 {
17154 s_ignore (0);
17155 return;
17156 }
17157
17158 mips_mark_labels ();
17159 mips_assembling_insn = TRUE;
17160
17161 /* Add $gp to the register named as an argument. */
17162 macro_start ();
17163 reg = tc_get_register (0);
17164 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17165 macro_end ();
17166
17167 mips_assembling_insn = FALSE;
17168 demand_empty_rest_of_line ();
17169 }
17170
17171 /* Handle the .insn pseudo-op. This marks instruction labels in
17172 mips16/micromips mode. This permits the linker to handle them specially,
17173 such as generating jalx instructions when needed. We also make
17174 them odd for the duration of the assembly, in order to generate the
17175 right sort of code. We will make them even in the adjust_symtab
17176 routine, while leaving them marked. This is convenient for the
17177 debugger and the disassembler. The linker knows to make them odd
17178 again. */
17179
17180 static void
17181 s_insn (int ignore ATTRIBUTE_UNUSED)
17182 {
17183 file_mips_check_options ();
17184 file_ase_mips16 |= mips_opts.mips16;
17185 file_ase_micromips |= mips_opts.micromips;
17186
17187 mips_mark_labels ();
17188
17189 demand_empty_rest_of_line ();
17190 }
17191
17192 /* Handle the .nan pseudo-op. */
17193
17194 static void
17195 s_nan (int ignore ATTRIBUTE_UNUSED)
17196 {
17197 static const char str_legacy[] = "legacy";
17198 static const char str_2008[] = "2008";
17199 size_t i;
17200
17201 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17202
17203 if (i == sizeof (str_2008) - 1
17204 && memcmp (input_line_pointer, str_2008, i) == 0)
17205 mips_nan2008 = 1;
17206 else if (i == sizeof (str_legacy) - 1
17207 && memcmp (input_line_pointer, str_legacy, i) == 0)
17208 {
17209 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17210 mips_nan2008 = 0;
17211 else
17212 as_bad (_("`%s' does not support legacy NaN"),
17213 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17214 }
17215 else
17216 as_bad (_("bad .nan directive"));
17217
17218 input_line_pointer += i;
17219 demand_empty_rest_of_line ();
17220 }
17221
17222 /* Handle a .stab[snd] directive. Ideally these directives would be
17223 implemented in a transparent way, so that removing them would not
17224 have any effect on the generated instructions. However, s_stab
17225 internally changes the section, so in practice we need to decide
17226 now whether the preceding label marks compressed code. We do not
17227 support changing the compression mode of a label after a .stab*
17228 directive, such as in:
17229
17230 foo:
17231 .stabs ...
17232 .set mips16
17233
17234 so the current mode wins. */
17235
17236 static void
17237 s_mips_stab (int type)
17238 {
17239 file_mips_check_options ();
17240 mips_mark_labels ();
17241 s_stab (type);
17242 }
17243
17244 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
17245
17246 static void
17247 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17248 {
17249 char *name;
17250 int c;
17251 symbolS *symbolP;
17252 expressionS exp;
17253
17254 c = get_symbol_name (&name);
17255 symbolP = symbol_find_or_make (name);
17256 S_SET_WEAK (symbolP);
17257 *input_line_pointer = c;
17258
17259 SKIP_WHITESPACE_AFTER_NAME ();
17260
17261 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17262 {
17263 if (S_IS_DEFINED (symbolP))
17264 {
17265 as_bad (_("ignoring attempt to redefine symbol %s"),
17266 S_GET_NAME (symbolP));
17267 ignore_rest_of_line ();
17268 return;
17269 }
17270
17271 if (*input_line_pointer == ',')
17272 {
17273 ++input_line_pointer;
17274 SKIP_WHITESPACE ();
17275 }
17276
17277 expression (&exp);
17278 if (exp.X_op != O_symbol)
17279 {
17280 as_bad (_("bad .weakext directive"));
17281 ignore_rest_of_line ();
17282 return;
17283 }
17284 symbol_set_value_expression (symbolP, &exp);
17285 }
17286
17287 demand_empty_rest_of_line ();
17288 }
17289
17290 /* Parse a register string into a number. Called from the ECOFF code
17291 to parse .frame. The argument is non-zero if this is the frame
17292 register, so that we can record it in mips_frame_reg. */
17293
17294 int
17295 tc_get_register (int frame)
17296 {
17297 unsigned int reg;
17298
17299 SKIP_WHITESPACE ();
17300 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17301 reg = 0;
17302 if (frame)
17303 {
17304 mips_frame_reg = reg != 0 ? reg : SP;
17305 mips_frame_reg_valid = 1;
17306 mips_cprestore_valid = 0;
17307 }
17308 return reg;
17309 }
17310
17311 valueT
17312 md_section_align (asection *seg, valueT addr)
17313 {
17314 int align = bfd_get_section_alignment (stdoutput, seg);
17315
17316 /* We don't need to align ELF sections to the full alignment.
17317 However, Irix 5 may prefer that we align them at least to a 16
17318 byte boundary. We don't bother to align the sections if we
17319 are targeted for an embedded system. */
17320 if (strncmp (TARGET_OS, "elf", 3) == 0)
17321 return addr;
17322 if (align > 4)
17323 align = 4;
17324
17325 return ((addr + (1 << align) - 1) & -(1 << align));
17326 }
17327
17328 /* Utility routine, called from above as well. If called while the
17329 input file is still being read, it's only an approximation. (For
17330 example, a symbol may later become defined which appeared to be
17331 undefined earlier.) */
17332
17333 static int
17334 nopic_need_relax (symbolS *sym, int before_relaxing)
17335 {
17336 if (sym == 0)
17337 return 0;
17338
17339 if (g_switch_value > 0)
17340 {
17341 const char *symname;
17342 int change;
17343
17344 /* Find out whether this symbol can be referenced off the $gp
17345 register. It can be if it is smaller than the -G size or if
17346 it is in the .sdata or .sbss section. Certain symbols can
17347 not be referenced off the $gp, although it appears as though
17348 they can. */
17349 symname = S_GET_NAME (sym);
17350 if (symname != (const char *) NULL
17351 && (strcmp (symname, "eprol") == 0
17352 || strcmp (symname, "etext") == 0
17353 || strcmp (symname, "_gp") == 0
17354 || strcmp (symname, "edata") == 0
17355 || strcmp (symname, "_fbss") == 0
17356 || strcmp (symname, "_fdata") == 0
17357 || strcmp (symname, "_ftext") == 0
17358 || strcmp (symname, "end") == 0
17359 || strcmp (symname, "_gp_disp") == 0))
17360 change = 1;
17361 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17362 && (0
17363 #ifndef NO_ECOFF_DEBUGGING
17364 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17365 && (symbol_get_obj (sym)->ecoff_extern_size
17366 <= g_switch_value))
17367 #endif
17368 /* We must defer this decision until after the whole
17369 file has been read, since there might be a .extern
17370 after the first use of this symbol. */
17371 || (before_relaxing
17372 #ifndef NO_ECOFF_DEBUGGING
17373 && symbol_get_obj (sym)->ecoff_extern_size == 0
17374 #endif
17375 && S_GET_VALUE (sym) == 0)
17376 || (S_GET_VALUE (sym) != 0
17377 && S_GET_VALUE (sym) <= g_switch_value)))
17378 change = 0;
17379 else
17380 {
17381 const char *segname;
17382
17383 segname = segment_name (S_GET_SEGMENT (sym));
17384 gas_assert (strcmp (segname, ".lit8") != 0
17385 && strcmp (segname, ".lit4") != 0);
17386 change = (strcmp (segname, ".sdata") != 0
17387 && strcmp (segname, ".sbss") != 0
17388 && strncmp (segname, ".sdata.", 7) != 0
17389 && strncmp (segname, ".sbss.", 6) != 0
17390 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17391 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17392 }
17393 return change;
17394 }
17395 else
17396 /* We are not optimizing for the $gp register. */
17397 return 1;
17398 }
17399
17400
17401 /* Return true if the given symbol should be considered local for SVR4 PIC. */
17402
17403 static bfd_boolean
17404 pic_need_relax (symbolS *sym)
17405 {
17406 asection *symsec;
17407
17408 /* Handle the case of a symbol equated to another symbol. */
17409 while (symbol_equated_reloc_p (sym))
17410 {
17411 symbolS *n;
17412
17413 /* It's possible to get a loop here in a badly written program. */
17414 n = symbol_get_value_expression (sym)->X_add_symbol;
17415 if (n == sym)
17416 break;
17417 sym = n;
17418 }
17419
17420 if (symbol_section_p (sym))
17421 return TRUE;
17422
17423 symsec = S_GET_SEGMENT (sym);
17424
17425 /* This must duplicate the test in adjust_reloc_syms. */
17426 return (!bfd_is_und_section (symsec)
17427 && !bfd_is_abs_section (symsec)
17428 && !bfd_is_com_section (symsec)
17429 /* A global or weak symbol is treated as external. */
17430 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
17431 }
17432 \f
17433 /* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17434 convert a section-relative value VAL to the equivalent PC-relative
17435 value. */
17436
17437 static offsetT
17438 mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17439 offsetT val, long stretch)
17440 {
17441 fragS *sym_frag;
17442 addressT addr;
17443
17444 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17445
17446 sym_frag = symbol_get_frag (fragp->fr_symbol);
17447
17448 /* If the relax_marker of the symbol fragment differs from the
17449 relax_marker of this fragment, we have not yet adjusted the
17450 symbol fragment fr_address. We want to add in STRETCH in
17451 order to get a better estimate of the address. This
17452 particularly matters because of the shift bits. */
17453 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17454 {
17455 fragS *f;
17456
17457 /* Adjust stretch for any alignment frag. Note that if have
17458 been expanding the earlier code, the symbol may be
17459 defined in what appears to be an earlier frag. FIXME:
17460 This doesn't handle the fr_subtype field, which specifies
17461 a maximum number of bytes to skip when doing an
17462 alignment. */
17463 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17464 {
17465 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17466 {
17467 if (stretch < 0)
17468 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17469 else
17470 stretch &= ~((1 << (int) f->fr_offset) - 1);
17471 if (stretch == 0)
17472 break;
17473 }
17474 }
17475 if (f != NULL)
17476 val += stretch;
17477 }
17478
17479 addr = fragp->fr_address + fragp->fr_fix;
17480
17481 /* The base address rules are complicated. The base address of
17482 a branch is the following instruction. The base address of a
17483 PC relative load or add is the instruction itself, but if it
17484 is in a delay slot (in which case it can not be extended) use
17485 the address of the instruction whose delay slot it is in. */
17486 if (pcrel_op->include_isa_bit)
17487 {
17488 addr += 2;
17489
17490 /* If we are currently assuming that this frag should be
17491 extended, then the current address is two bytes higher. */
17492 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17493 addr += 2;
17494
17495 /* Ignore the low bit in the target, since it will be set
17496 for a text label. */
17497 val &= -2;
17498 }
17499 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17500 addr -= 4;
17501 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17502 addr -= 2;
17503
17504 val -= addr & -(1 << pcrel_op->align_log2);
17505
17506 return val;
17507 }
17508
17509 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17510 extended opcode. SEC is the section the frag is in. */
17511
17512 static int
17513 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17514 {
17515 const struct mips_int_operand *operand;
17516 offsetT val;
17517 segT symsec;
17518 int type;
17519
17520 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17521 return 0;
17522 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17523 return 1;
17524
17525 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17526 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17527 operand = mips16_immed_operand (type, FALSE);
17528 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17529 || (operand->root.type == OP_PCREL
17530 ? sec != symsec
17531 : !bfd_is_abs_section (symsec)))
17532 return 1;
17533
17534 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17535
17536 if (operand->root.type == OP_PCREL)
17537 {
17538 const struct mips_pcrel_operand *pcrel_op;
17539 offsetT maxtiny;
17540
17541 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
17542 return 1;
17543
17544 pcrel_op = (const struct mips_pcrel_operand *) operand;
17545 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17546
17547 /* If any of the shifted bits are set, we must use an extended
17548 opcode. If the address depends on the size of this
17549 instruction, this can lead to a loop, so we arrange to always
17550 use an extended opcode. */
17551 if ((val & ((1 << operand->shift) - 1)) != 0)
17552 {
17553 fragp->fr_subtype =
17554 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17555 return 1;
17556 }
17557
17558 /* If we are about to mark a frag as extended because the value
17559 is precisely the next value above maxtiny, then there is a
17560 chance of an infinite loop as in the following code:
17561 la $4,foo
17562 .skip 1020
17563 .align 2
17564 foo:
17565 In this case when the la is extended, foo is 0x3fc bytes
17566 away, so the la can be shrunk, but then foo is 0x400 away, so
17567 the la must be extended. To avoid this loop, we mark the
17568 frag as extended if it was small, and is about to become
17569 extended with the next value above maxtiny. */
17570 maxtiny = mips_int_operand_max (operand);
17571 if (val == maxtiny + (1 << operand->shift)
17572 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17573 {
17574 fragp->fr_subtype =
17575 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17576 return 1;
17577 }
17578 }
17579
17580 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17581 }
17582
17583 /* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17584 macro expansion. SEC is the section the frag is in. We only
17585 support PC-relative instructions (LA, DLA, LW, LD) here, in
17586 non-PIC code using 32-bit addressing. */
17587
17588 static int
17589 mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17590 {
17591 const struct mips_pcrel_operand *pcrel_op;
17592 const struct mips_int_operand *operand;
17593 offsetT val;
17594 segT symsec;
17595 int type;
17596
17597 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17598
17599 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17600 return 0;
17601 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17602 return 0;
17603
17604 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17605 switch (type)
17606 {
17607 case 'A':
17608 case 'B':
17609 case 'E':
17610 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17611 if (bfd_is_abs_section (symsec))
17612 return 1;
17613 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17614 return 0;
17615 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17616 return 1;
17617
17618 operand = mips16_immed_operand (type, TRUE);
17619 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17620 pcrel_op = (const struct mips_pcrel_operand *) operand;
17621 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17622
17623 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17624
17625 default:
17626 return 0;
17627 }
17628 }
17629
17630 /* Compute the length of a branch sequence, and adjust the
17631 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17632 worst-case length is computed, with UPDATE being used to indicate
17633 whether an unconditional (-1), branch-likely (+1) or regular (0)
17634 branch is to be computed. */
17635 static int
17636 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17637 {
17638 bfd_boolean toofar;
17639 int length;
17640
17641 if (fragp
17642 && S_IS_DEFINED (fragp->fr_symbol)
17643 && !S_IS_WEAK (fragp->fr_symbol)
17644 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17645 {
17646 addressT addr;
17647 offsetT val;
17648
17649 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17650
17651 addr = fragp->fr_address + fragp->fr_fix + 4;
17652
17653 val -= addr;
17654
17655 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17656 }
17657 else
17658 /* If the symbol is not defined or it's in a different segment,
17659 we emit the long sequence. */
17660 toofar = TRUE;
17661
17662 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17663 fragp->fr_subtype
17664 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17665 RELAX_BRANCH_PIC (fragp->fr_subtype),
17666 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17667 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17668 RELAX_BRANCH_LINK (fragp->fr_subtype),
17669 toofar);
17670
17671 length = 4;
17672 if (toofar)
17673 {
17674 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17675 length += 8;
17676
17677 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
17678 {
17679 /* Additional space for PIC loading of target address. */
17680 length += 8;
17681 if (mips_opts.isa == ISA_MIPS1)
17682 /* Additional space for $at-stabilizing nop. */
17683 length += 4;
17684 }
17685
17686 /* If branch is conditional. */
17687 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17688 length += 8;
17689 }
17690
17691 return length;
17692 }
17693
17694 /* Get a FRAG's branch instruction delay slot size, either from the
17695 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17696 or SHORT_INSN_SIZE otherwise. */
17697
17698 static int
17699 frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17700 {
17701 char *buf = fragp->fr_literal + fragp->fr_fix;
17702
17703 if (al)
17704 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17705 else
17706 return short_insn_size;
17707 }
17708
17709 /* Compute the length of a branch sequence, and adjust the
17710 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17711 worst-case length is computed, with UPDATE being used to indicate
17712 whether an unconditional (-1), or regular (0) branch is to be
17713 computed. */
17714
17715 static int
17716 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17717 {
17718 bfd_boolean insn32 = TRUE;
17719 bfd_boolean nods = TRUE;
17720 bfd_boolean pic = TRUE;
17721 bfd_boolean al = TRUE;
17722 int short_insn_size;
17723 bfd_boolean toofar;
17724 int length;
17725
17726 if (fragp)
17727 {
17728 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17729 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
17730 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
17731 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17732 }
17733 short_insn_size = insn32 ? 4 : 2;
17734
17735 if (fragp
17736 && S_IS_DEFINED (fragp->fr_symbol)
17737 && !S_IS_WEAK (fragp->fr_symbol)
17738 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17739 {
17740 addressT addr;
17741 offsetT val;
17742
17743 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17744 /* Ignore the low bit in the target, since it will be set
17745 for a text label. */
17746 if ((val & 1) != 0)
17747 --val;
17748
17749 addr = fragp->fr_address + fragp->fr_fix + 4;
17750
17751 val -= addr;
17752
17753 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17754 }
17755 else
17756 /* If the symbol is not defined or it's in a different segment,
17757 we emit the long sequence. */
17758 toofar = TRUE;
17759
17760 if (fragp && update
17761 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17762 fragp->fr_subtype = (toofar
17763 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17764 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17765
17766 length = 4;
17767 if (toofar)
17768 {
17769 bfd_boolean compact_known = fragp != NULL;
17770 bfd_boolean compact = FALSE;
17771 bfd_boolean uncond;
17772
17773 if (fragp)
17774 {
17775 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17776 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17777 }
17778 else
17779 uncond = update < 0;
17780
17781 /* If label is out of range, we turn branch <br>:
17782
17783 <br> label # 4 bytes
17784 0:
17785
17786 into:
17787
17788 j label # 4 bytes
17789 nop # 2/4 bytes if
17790 # compact && (!PIC || insn32)
17791 0:
17792 */
17793 if ((!pic || insn32) && (!compact_known || compact))
17794 length += short_insn_size;
17795
17796 /* If assembling PIC code, we further turn:
17797
17798 j label # 4 bytes
17799
17800 into:
17801
17802 lw/ld at, %got(label)(gp) # 4 bytes
17803 d/addiu at, %lo(label) # 4 bytes
17804 jr/c at # 2/4 bytes
17805 */
17806 if (pic)
17807 length += 4 + short_insn_size;
17808
17809 /* Add an extra nop if the jump has no compact form and we need
17810 to fill the delay slot. */
17811 if ((!pic || al) && nods)
17812 length += (fragp
17813 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
17814 : short_insn_size);
17815
17816 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17817
17818 <brneg> 0f # 4 bytes
17819 nop # 2/4 bytes if !compact
17820 */
17821 if (!uncond)
17822 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
17823 }
17824 else if (nods)
17825 {
17826 /* Add an extra nop to fill the delay slot. */
17827 gas_assert (fragp);
17828 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
17829 }
17830
17831 return length;
17832 }
17833
17834 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17835 bit accordingly. */
17836
17837 static int
17838 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17839 {
17840 bfd_boolean toofar;
17841
17842 if (fragp
17843 && S_IS_DEFINED (fragp->fr_symbol)
17844 && !S_IS_WEAK (fragp->fr_symbol)
17845 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17846 {
17847 addressT addr;
17848 offsetT val;
17849 int type;
17850
17851 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17852 /* Ignore the low bit in the target, since it will be set
17853 for a text label. */
17854 if ((val & 1) != 0)
17855 --val;
17856
17857 /* Assume this is a 2-byte branch. */
17858 addr = fragp->fr_address + fragp->fr_fix + 2;
17859
17860 /* We try to avoid the infinite loop by not adding 2 more bytes for
17861 long branches. */
17862
17863 val -= addr;
17864
17865 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17866 if (type == 'D')
17867 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17868 else if (type == 'E')
17869 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17870 else
17871 abort ();
17872 }
17873 else
17874 /* If the symbol is not defined or it's in a different segment,
17875 we emit a normal 32-bit branch. */
17876 toofar = TRUE;
17877
17878 if (fragp && update
17879 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17880 fragp->fr_subtype
17881 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17882 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17883
17884 if (toofar)
17885 return 4;
17886
17887 return 2;
17888 }
17889
17890 /* Estimate the size of a frag before relaxing. Unless this is the
17891 mips16, we are not really relaxing here, and the final size is
17892 encoded in the subtype information. For the mips16, we have to
17893 decide whether we are using an extended opcode or not. */
17894
17895 int
17896 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17897 {
17898 int change;
17899
17900 if (RELAX_BRANCH_P (fragp->fr_subtype))
17901 {
17902
17903 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17904
17905 return fragp->fr_var;
17906 }
17907
17908 if (RELAX_MIPS16_P (fragp->fr_subtype))
17909 {
17910 /* We don't want to modify the EXTENDED bit here; it might get us
17911 into infinite loops. We change it only in mips_relax_frag(). */
17912 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
17913 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
17914 else
17915 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
17916 }
17917
17918 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17919 {
17920 int length = 4;
17921
17922 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17923 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17924 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17925 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17926 fragp->fr_var = length;
17927
17928 return length;
17929 }
17930
17931 if (mips_pic == VXWORKS_PIC)
17932 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17933 change = 0;
17934 else if (RELAX_PIC (fragp->fr_subtype))
17935 change = pic_need_relax (fragp->fr_symbol);
17936 else
17937 change = nopic_need_relax (fragp->fr_symbol, 0);
17938
17939 if (change)
17940 {
17941 fragp->fr_subtype |= RELAX_USE_SECOND;
17942 return -RELAX_FIRST (fragp->fr_subtype);
17943 }
17944 else
17945 return -RELAX_SECOND (fragp->fr_subtype);
17946 }
17947
17948 /* This is called to see whether a reloc against a defined symbol
17949 should be converted into a reloc against a section. */
17950
17951 int
17952 mips_fix_adjustable (fixS *fixp)
17953 {
17954 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17955 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17956 return 0;
17957
17958 if (fixp->fx_addsy == NULL)
17959 return 1;
17960
17961 /* Allow relocs used for EH tables. */
17962 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17963 return 1;
17964
17965 /* If symbol SYM is in a mergeable section, relocations of the form
17966 SYM + 0 can usually be made section-relative. The mergeable data
17967 is then identified by the section offset rather than by the symbol.
17968
17969 However, if we're generating REL LO16 relocations, the offset is split
17970 between the LO16 and partnering high part relocation. The linker will
17971 need to recalculate the complete offset in order to correctly identify
17972 the merge data.
17973
17974 The linker has traditionally not looked for the partnering high part
17975 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17976 placed anywhere. Rather than break backwards compatibility by changing
17977 this, it seems better not to force the issue, and instead keep the
17978 original symbol. This will work with either linker behavior. */
17979 if ((lo16_reloc_p (fixp->fx_r_type)
17980 || reloc_needs_lo_p (fixp->fx_r_type))
17981 && HAVE_IN_PLACE_ADDENDS
17982 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17983 return 0;
17984
17985 /* There is no place to store an in-place offset for JALR relocations. */
17986 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
17987 return 0;
17988
17989 /* Likewise an in-range offset of limited PC-relative relocations may
17990 overflow the in-place relocatable field if recalculated against the
17991 start address of the symbol's containing section.
17992
17993 Also, PC relative relocations for MIPS R6 need to be symbol rather than
17994 section relative to allow linker relaxations to be performed later on. */
17995 if (limited_pcrel_reloc_p (fixp->fx_r_type)
17996 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
17997 return 0;
17998
17999 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18000 to a floating-point stub. The same is true for non-R_MIPS16_26
18001 relocations against MIPS16 functions; in this case, the stub becomes
18002 the function's canonical address.
18003
18004 Floating-point stubs are stored in unique .mips16.call.* or
18005 .mips16.fn.* sections. If a stub T for function F is in section S,
18006 the first relocation in section S must be against F; this is how the
18007 linker determines the target function. All relocations that might
18008 resolve to T must also be against F. We therefore have the following
18009 restrictions, which are given in an intentionally-redundant way:
18010
18011 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18012 symbols.
18013
18014 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18015 if that stub might be used.
18016
18017 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18018 symbols.
18019
18020 4. We cannot reduce a stub's relocations against MIPS16 symbols if
18021 that stub might be used.
18022
18023 There is a further restriction:
18024
18025 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
18026 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
18027 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18028 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18029 against MIPS16 or microMIPS symbols because we need to keep the
18030 MIPS16 or microMIPS symbol for the purpose of mode mismatch
18031 detection and JAL or BAL to JALX instruction conversion in the
18032 linker.
18033
18034 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
18035 against a MIPS16 symbol. We deal with (5) by additionally leaving
18036 alone any jump and branch relocations against a microMIPS symbol.
18037
18038 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18039 relocation against some symbol R, no relocation against R may be
18040 reduced. (Note that this deals with (2) as well as (1) because
18041 relocations against global symbols will never be reduced on ELF
18042 targets.) This approach is a little simpler than trying to detect
18043 stub sections, and gives the "all or nothing" per-symbol consistency
18044 that we have for MIPS16 symbols. */
18045 if (fixp->fx_subsy == NULL
18046 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
18047 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
18048 && (jmp_reloc_p (fixp->fx_r_type)
18049 || b_reloc_p (fixp->fx_r_type)))
18050 || *symbol_get_tc (fixp->fx_addsy)))
18051 return 0;
18052
18053 return 1;
18054 }
18055
18056 /* Translate internal representation of relocation info to BFD target
18057 format. */
18058
18059 arelent **
18060 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
18061 {
18062 static arelent *retval[4];
18063 arelent *reloc;
18064 bfd_reloc_code_real_type code;
18065
18066 memset (retval, 0, sizeof(retval));
18067 reloc = retval[0] = XCNEW (arelent);
18068 reloc->sym_ptr_ptr = XNEW (asymbol *);
18069 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18070 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18071
18072 if (fixp->fx_pcrel)
18073 {
18074 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18075 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
18076 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18077 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
18078 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18079 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18080 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18081 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18082 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18083 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18084 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18085 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
18086
18087 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18088 Relocations want only the symbol offset. */
18089 switch (fixp->fx_r_type)
18090 {
18091 case BFD_RELOC_MIPS_18_PCREL_S3:
18092 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18093 break;
18094 default:
18095 reloc->addend = fixp->fx_addnumber + reloc->address;
18096 break;
18097 }
18098 }
18099 else if (HAVE_IN_PLACE_ADDENDS
18100 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18101 && (read_compressed_insn (fixp->fx_frag->fr_literal
18102 + fixp->fx_where, 4) >> 26) == 0x3c)
18103 {
18104 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18105 addend accordingly. */
18106 reloc->addend = fixp->fx_addnumber >> 1;
18107 }
18108 else
18109 reloc->addend = fixp->fx_addnumber;
18110
18111 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18112 entry to be used in the relocation's section offset. */
18113 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18114 {
18115 reloc->address = reloc->addend;
18116 reloc->addend = 0;
18117 }
18118
18119 code = fixp->fx_r_type;
18120
18121 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
18122 if (reloc->howto == NULL)
18123 {
18124 as_bad_where (fixp->fx_file, fixp->fx_line,
18125 _("cannot represent %s relocation in this object file"
18126 " format"),
18127 bfd_get_reloc_code_name (code));
18128 retval[0] = NULL;
18129 }
18130
18131 return retval;
18132 }
18133
18134 /* Relax a machine dependent frag. This returns the amount by which
18135 the current size of the frag should change. */
18136
18137 int
18138 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
18139 {
18140 if (RELAX_BRANCH_P (fragp->fr_subtype))
18141 {
18142 offsetT old_var = fragp->fr_var;
18143
18144 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
18145
18146 return fragp->fr_var - old_var;
18147 }
18148
18149 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18150 {
18151 offsetT old_var = fragp->fr_var;
18152 offsetT new_var = 4;
18153
18154 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18155 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18156 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18157 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18158 fragp->fr_var = new_var;
18159
18160 return new_var - old_var;
18161 }
18162
18163 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18164 return 0;
18165
18166 if (!mips16_extended_frag (fragp, sec, stretch))
18167 {
18168 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18169 {
18170 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18171 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
18172 }
18173 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18174 {
18175 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18176 return -2;
18177 }
18178 else
18179 return 0;
18180 }
18181 else if (!mips16_macro_frag (fragp, sec, stretch))
18182 {
18183 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18184 {
18185 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18186 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18187 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
18188 }
18189 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18190 {
18191 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18192 return 2;
18193 }
18194 else
18195 return 0;
18196 }
18197 else
18198 {
18199 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18200 return 0;
18201 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18202 {
18203 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18204 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18205 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
18206 }
18207 else
18208 {
18209 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18210 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
18211 }
18212 }
18213
18214 return 0;
18215 }
18216
18217 /* Convert a machine dependent frag. */
18218
18219 void
18220 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
18221 {
18222 if (RELAX_BRANCH_P (fragp->fr_subtype))
18223 {
18224 char *buf;
18225 unsigned long insn;
18226 fixS *fixp;
18227
18228 buf = fragp->fr_literal + fragp->fr_fix;
18229 insn = read_insn (buf);
18230
18231 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18232 {
18233 /* We generate a fixup instead of applying it right now
18234 because, if there are linker relaxations, we're going to
18235 need the relocations. */
18236 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18237 fragp->fr_symbol, fragp->fr_offset,
18238 TRUE, BFD_RELOC_16_PCREL_S2);
18239 fixp->fx_file = fragp->fr_file;
18240 fixp->fx_line = fragp->fr_line;
18241
18242 buf = write_insn (buf, insn);
18243 }
18244 else
18245 {
18246 int i;
18247
18248 as_warn_where (fragp->fr_file, fragp->fr_line,
18249 _("relaxed out-of-range branch into a jump"));
18250
18251 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18252 goto uncond;
18253
18254 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18255 {
18256 /* Reverse the branch. */
18257 switch ((insn >> 28) & 0xf)
18258 {
18259 case 4:
18260 if ((insn & 0xff000000) == 0x47000000
18261 || (insn & 0xff600000) == 0x45600000)
18262 {
18263 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18264 reversed by tweaking bit 23. */
18265 insn ^= 0x00800000;
18266 }
18267 else
18268 {
18269 /* bc[0-3][tf]l? instructions can have the condition
18270 reversed by tweaking a single TF bit, and their
18271 opcodes all have 0x4???????. */
18272 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18273 insn ^= 0x00010000;
18274 }
18275 break;
18276
18277 case 0:
18278 /* bltz 0x04000000 bgez 0x04010000
18279 bltzal 0x04100000 bgezal 0x04110000 */
18280 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18281 insn ^= 0x00010000;
18282 break;
18283
18284 case 1:
18285 /* beq 0x10000000 bne 0x14000000
18286 blez 0x18000000 bgtz 0x1c000000 */
18287 insn ^= 0x04000000;
18288 break;
18289
18290 default:
18291 abort ();
18292 }
18293 }
18294
18295 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18296 {
18297 /* Clear the and-link bit. */
18298 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18299
18300 /* bltzal 0x04100000 bgezal 0x04110000
18301 bltzall 0x04120000 bgezall 0x04130000 */
18302 insn &= ~0x00100000;
18303 }
18304
18305 /* Branch over the branch (if the branch was likely) or the
18306 full jump (not likely case). Compute the offset from the
18307 current instruction to branch to. */
18308 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18309 i = 16;
18310 else
18311 {
18312 /* How many bytes in instructions we've already emitted? */
18313 i = buf - fragp->fr_literal - fragp->fr_fix;
18314 /* How many bytes in instructions from here to the end? */
18315 i = fragp->fr_var - i;
18316 }
18317 /* Convert to instruction count. */
18318 i >>= 2;
18319 /* Branch counts from the next instruction. */
18320 i--;
18321 insn |= i;
18322 /* Branch over the jump. */
18323 buf = write_insn (buf, insn);
18324
18325 /* nop */
18326 buf = write_insn (buf, 0);
18327
18328 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18329 {
18330 /* beql $0, $0, 2f */
18331 insn = 0x50000000;
18332 /* Compute the PC offset from the current instruction to
18333 the end of the variable frag. */
18334 /* How many bytes in instructions we've already emitted? */
18335 i = buf - fragp->fr_literal - fragp->fr_fix;
18336 /* How many bytes in instructions from here to the end? */
18337 i = fragp->fr_var - i;
18338 /* Convert to instruction count. */
18339 i >>= 2;
18340 /* Don't decrement i, because we want to branch over the
18341 delay slot. */
18342 insn |= i;
18343
18344 buf = write_insn (buf, insn);
18345 buf = write_insn (buf, 0);
18346 }
18347
18348 uncond:
18349 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
18350 {
18351 /* j or jal. */
18352 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18353 ? 0x0c000000 : 0x08000000);
18354
18355 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18356 fragp->fr_symbol, fragp->fr_offset,
18357 FALSE, BFD_RELOC_MIPS_JMP);
18358 fixp->fx_file = fragp->fr_file;
18359 fixp->fx_line = fragp->fr_line;
18360
18361 buf = write_insn (buf, insn);
18362 }
18363 else
18364 {
18365 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18366
18367 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
18368 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18369 insn |= at << OP_SH_RT;
18370
18371 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18372 fragp->fr_symbol, fragp->fr_offset,
18373 FALSE, BFD_RELOC_MIPS_GOT16);
18374 fixp->fx_file = fragp->fr_file;
18375 fixp->fx_line = fragp->fr_line;
18376
18377 buf = write_insn (buf, insn);
18378
18379 if (mips_opts.isa == ISA_MIPS1)
18380 /* nop */
18381 buf = write_insn (buf, 0);
18382
18383 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
18384 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18385 insn |= at << OP_SH_RS | at << OP_SH_RT;
18386
18387 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18388 fragp->fr_symbol, fragp->fr_offset,
18389 FALSE, BFD_RELOC_LO16);
18390 fixp->fx_file = fragp->fr_file;
18391 fixp->fx_line = fragp->fr_line;
18392
18393 buf = write_insn (buf, insn);
18394
18395 /* j(al)r $at. */
18396 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18397 insn = 0x0000f809;
18398 else
18399 insn = 0x00000008;
18400 insn |= at << OP_SH_RS;
18401
18402 buf = write_insn (buf, insn);
18403 }
18404 }
18405
18406 fragp->fr_fix += fragp->fr_var;
18407 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18408 return;
18409 }
18410
18411 /* Relax microMIPS branches. */
18412 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18413 {
18414 char *buf = fragp->fr_literal + fragp->fr_fix;
18415 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18416 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18417 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18418 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18419 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18420 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18421 bfd_boolean short_ds;
18422 unsigned long insn;
18423 fixS *fixp;
18424
18425 fragp->fr_fix += fragp->fr_var;
18426
18427 /* Handle 16-bit branches that fit or are forced to fit. */
18428 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18429 {
18430 /* We generate a fixup instead of applying it right now,
18431 because if there is linker relaxation, we're going to
18432 need the relocations. */
18433 switch (type)
18434 {
18435 case 'D':
18436 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18437 fragp->fr_symbol, fragp->fr_offset,
18438 TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18439 break;
18440 case 'E':
18441 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18442 fragp->fr_symbol, fragp->fr_offset,
18443 TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18444 break;
18445 default:
18446 abort ();
18447 }
18448
18449 fixp->fx_file = fragp->fr_file;
18450 fixp->fx_line = fragp->fr_line;
18451
18452 /* These relocations can have an addend that won't fit in
18453 2 octets. */
18454 fixp->fx_no_overflow = 1;
18455
18456 return;
18457 }
18458
18459 /* Handle 32-bit branches that fit or are forced to fit. */
18460 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18461 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18462 {
18463 /* We generate a fixup instead of applying it right now,
18464 because if there is linker relaxation, we're going to
18465 need the relocations. */
18466 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18467 fragp->fr_symbol, fragp->fr_offset,
18468 TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18469 fixp->fx_file = fragp->fr_file;
18470 fixp->fx_line = fragp->fr_line;
18471
18472 if (type == 0)
18473 {
18474 insn = read_compressed_insn (buf, 4);
18475 buf += 4;
18476
18477 if (nods)
18478 {
18479 /* Check the short-delay-slot bit. */
18480 if (!al || (insn & 0x02000000) != 0)
18481 buf = write_compressed_insn (buf, 0x0c00, 2);
18482 else
18483 buf = write_compressed_insn (buf, 0x00000000, 4);
18484 }
18485
18486 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18487 return;
18488 }
18489 }
18490
18491 /* Relax 16-bit branches to 32-bit branches. */
18492 if (type != 0)
18493 {
18494 insn = read_compressed_insn (buf, 2);
18495
18496 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18497 insn = 0x94000000; /* beq */
18498 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18499 {
18500 unsigned long regno;
18501
18502 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18503 regno = micromips_to_32_reg_d_map [regno];
18504 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18505 insn |= regno << MICROMIPSOP_SH_RS;
18506 }
18507 else
18508 abort ();
18509
18510 /* Nothing else to do, just write it out. */
18511 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18512 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18513 {
18514 buf = write_compressed_insn (buf, insn, 4);
18515 if (nods)
18516 buf = write_compressed_insn (buf, 0x0c00, 2);
18517 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18518 return;
18519 }
18520 }
18521 else
18522 insn = read_compressed_insn (buf, 4);
18523
18524 /* Relax 32-bit branches to a sequence of instructions. */
18525 as_warn_where (fragp->fr_file, fragp->fr_line,
18526 _("relaxed out-of-range branch into a jump"));
18527
18528 /* Set the short-delay-slot bit. */
18529 short_ds = !al || (insn & 0x02000000) != 0;
18530
18531 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18532 {
18533 symbolS *l;
18534
18535 /* Reverse the branch. */
18536 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18537 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18538 insn ^= 0x20000000;
18539 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18540 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18541 || (insn & 0xffe00000) == 0x40800000 /* blez */
18542 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18543 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18544 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18545 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18546 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18547 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18548 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18549 insn ^= 0x00400000;
18550 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18551 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18552 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18553 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18554 insn ^= 0x00200000;
18555 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18556 BNZ.df */
18557 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18558 BNZ.V */
18559 insn ^= 0x00800000;
18560 else
18561 abort ();
18562
18563 if (al)
18564 {
18565 /* Clear the and-link and short-delay-slot bits. */
18566 gas_assert ((insn & 0xfda00000) == 0x40200000);
18567
18568 /* bltzal 0x40200000 bgezal 0x40600000 */
18569 /* bltzals 0x42200000 bgezals 0x42600000 */
18570 insn &= ~0x02200000;
18571 }
18572
18573 /* Make a label at the end for use with the branch. */
18574 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18575 micromips_label_inc ();
18576 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18577
18578 /* Refer to it. */
18579 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18580 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18581 fixp->fx_file = fragp->fr_file;
18582 fixp->fx_line = fragp->fr_line;
18583
18584 /* Branch over the jump. */
18585 buf = write_compressed_insn (buf, insn, 4);
18586
18587 if (!compact)
18588 {
18589 /* nop */
18590 if (insn32)
18591 buf = write_compressed_insn (buf, 0x00000000, 4);
18592 else
18593 buf = write_compressed_insn (buf, 0x0c00, 2);
18594 }
18595 }
18596
18597 if (!pic)
18598 {
18599 unsigned long jal = (short_ds || nods
18600 ? 0x74000000 : 0xf4000000); /* jal/s */
18601
18602 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18603 insn = al ? jal : 0xd4000000;
18604
18605 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18606 fragp->fr_symbol, fragp->fr_offset,
18607 FALSE, BFD_RELOC_MICROMIPS_JMP);
18608 fixp->fx_file = fragp->fr_file;
18609 fixp->fx_line = fragp->fr_line;
18610
18611 buf = write_compressed_insn (buf, insn, 4);
18612
18613 if (compact || nods)
18614 {
18615 /* nop */
18616 if (insn32)
18617 buf = write_compressed_insn (buf, 0x00000000, 4);
18618 else
18619 buf = write_compressed_insn (buf, 0x0c00, 2);
18620 }
18621 }
18622 else
18623 {
18624 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18625
18626 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18627 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18628 insn |= at << MICROMIPSOP_SH_RT;
18629
18630 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18631 fragp->fr_symbol, fragp->fr_offset,
18632 FALSE, BFD_RELOC_MICROMIPS_GOT16);
18633 fixp->fx_file = fragp->fr_file;
18634 fixp->fx_line = fragp->fr_line;
18635
18636 buf = write_compressed_insn (buf, insn, 4);
18637
18638 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18639 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18640 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18641
18642 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18643 fragp->fr_symbol, fragp->fr_offset,
18644 FALSE, BFD_RELOC_MICROMIPS_LO16);
18645 fixp->fx_file = fragp->fr_file;
18646 fixp->fx_line = fragp->fr_line;
18647
18648 buf = write_compressed_insn (buf, insn, 4);
18649
18650 if (insn32)
18651 {
18652 /* jr/jalr $at */
18653 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18654 insn |= at << MICROMIPSOP_SH_RS;
18655
18656 buf = write_compressed_insn (buf, insn, 4);
18657
18658 if (compact || nods)
18659 /* nop */
18660 buf = write_compressed_insn (buf, 0x00000000, 4);
18661 }
18662 else
18663 {
18664 /* jr/jrc/jalr/jalrs $at */
18665 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18666 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
18667
18668 insn = al ? jalr : jr;
18669 insn |= at << MICROMIPSOP_SH_MJ;
18670
18671 buf = write_compressed_insn (buf, insn, 2);
18672 if (al && nods)
18673 {
18674 /* nop */
18675 if (short_ds)
18676 buf = write_compressed_insn (buf, 0x0c00, 2);
18677 else
18678 buf = write_compressed_insn (buf, 0x00000000, 4);
18679 }
18680 }
18681 }
18682
18683 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18684 return;
18685 }
18686
18687 if (RELAX_MIPS16_P (fragp->fr_subtype))
18688 {
18689 int type;
18690 const struct mips_int_operand *operand;
18691 offsetT val;
18692 char *buf;
18693 unsigned int user_length;
18694 bfd_boolean need_reloc;
18695 unsigned long insn;
18696 bfd_boolean mac;
18697 bfd_boolean ext;
18698 segT symsec;
18699
18700 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18701 operand = mips16_immed_operand (type, FALSE);
18702
18703 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
18704 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18705 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
18706
18707 symsec = S_GET_SEGMENT (fragp->fr_symbol);
18708 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
18709 || (operand->root.type == OP_PCREL && !mac
18710 ? asec != symsec
18711 : !bfd_is_abs_section (symsec)));
18712
18713 if (operand->root.type == OP_PCREL && !mac)
18714 {
18715 const struct mips_pcrel_operand *pcrel_op;
18716
18717 pcrel_op = (const struct mips_pcrel_operand *) operand;
18718
18719 if (pcrel_op->include_isa_bit && !need_reloc)
18720 {
18721 if (!mips_ignore_branch_isa
18722 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
18723 as_bad_where (fragp->fr_file, fragp->fr_line,
18724 _("branch to a symbol in another ISA mode"));
18725 else if ((fragp->fr_offset & 0x1) != 0)
18726 as_bad_where (fragp->fr_file, fragp->fr_line,
18727 _("branch to misaligned address (0x%lx)"),
18728 (long) val);
18729 }
18730
18731 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
18732
18733 /* Make sure the section winds up with the alignment we have
18734 assumed. */
18735 if (operand->shift > 0)
18736 record_alignment (asec, operand->shift);
18737 }
18738
18739 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18740 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18741 {
18742 if (mac)
18743 as_warn_where (fragp->fr_file, fragp->fr_line,
18744 _("macro instruction expanded into multiple "
18745 "instructions in a branch delay slot"));
18746 else if (ext)
18747 as_warn_where (fragp->fr_file, fragp->fr_line,
18748 _("extended instruction in a branch delay slot"));
18749 }
18750 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
18751 as_warn_where (fragp->fr_file, fragp->fr_line,
18752 _("macro instruction expanded into multiple "
18753 "instructions"));
18754
18755 buf = fragp->fr_literal + fragp->fr_fix;
18756
18757 insn = read_compressed_insn (buf, 2);
18758 if (ext)
18759 insn |= MIPS16_EXTEND;
18760
18761 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18762 user_length = 4;
18763 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18764 user_length = 2;
18765 else
18766 user_length = 0;
18767
18768 if (mac)
18769 {
18770 unsigned long reg;
18771 unsigned long new;
18772 unsigned long op;
18773 bfd_boolean e2;
18774
18775 gas_assert (type == 'A' || type == 'B' || type == 'E');
18776 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
18777
18778 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
18779
18780 if (need_reloc)
18781 {
18782 fixS *fixp;
18783
18784 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
18785
18786 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18787 fragp->fr_symbol, fragp->fr_offset,
18788 FALSE, BFD_RELOC_MIPS16_HI16_S);
18789 fixp->fx_file = fragp->fr_file;
18790 fixp->fx_line = fragp->fr_line;
18791
18792 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
18793 fragp->fr_symbol, fragp->fr_offset,
18794 FALSE, BFD_RELOC_MIPS16_LO16);
18795 fixp->fx_file = fragp->fr_file;
18796 fixp->fx_line = fragp->fr_line;
18797
18798 val = 0;
18799 }
18800
18801 switch (insn & 0xf800)
18802 {
18803 case 0x0800: /* ADDIU */
18804 reg = (insn >> 8) & 0x7;
18805 op = 0xf0004800 | (reg << 8);
18806 break;
18807 case 0xb000: /* LW */
18808 reg = (insn >> 8) & 0x7;
18809 op = 0xf0009800 | (reg << 8) | (reg << 5);
18810 break;
18811 case 0xf800: /* I64 */
18812 reg = (insn >> 5) & 0x7;
18813 switch (insn & 0x0700)
18814 {
18815 case 0x0400: /* LD */
18816 op = 0xf0003800 | (reg << 8) | (reg << 5);
18817 break;
18818 case 0x0600: /* DADDIU */
18819 op = 0xf000fd00 | (reg << 5);
18820 break;
18821 default:
18822 abort ();
18823 }
18824 break;
18825 default:
18826 abort ();
18827 }
18828
18829 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
18830 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
18831 buf = write_compressed_insn (buf, new, 4);
18832 if (!e2)
18833 {
18834 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
18835 buf = write_compressed_insn (buf, new, 4);
18836 }
18837 op |= mips16_immed_extend (val, 16);
18838 buf = write_compressed_insn (buf, op, 4);
18839
18840 fragp->fr_fix += e2 ? 8 : 12;
18841 }
18842 else
18843 {
18844 unsigned int length = ext ? 4 : 2;
18845
18846 if (need_reloc)
18847 {
18848 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
18849 fixS *fixp;
18850
18851 switch (type)
18852 {
18853 case 'p':
18854 case 'q':
18855 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
18856 break;
18857 default:
18858 break;
18859 }
18860 if (mac || reloc == BFD_RELOC_NONE)
18861 as_bad_where (fragp->fr_file, fragp->fr_line,
18862 _("unsupported relocation"));
18863 else if (ext)
18864 {
18865 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18866 fragp->fr_symbol, fragp->fr_offset,
18867 TRUE, reloc);
18868 fixp->fx_file = fragp->fr_file;
18869 fixp->fx_line = fragp->fr_line;
18870 }
18871 else
18872 as_bad_where (fragp->fr_file, fragp->fr_line,
18873 _("invalid unextended operand value"));
18874 }
18875 else
18876 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18877 BFD_RELOC_UNUSED, val, user_length, &insn);
18878
18879 gas_assert (mips16_opcode_length (insn) == length);
18880 write_compressed_insn (buf, insn, length);
18881 fragp->fr_fix += length;
18882 }
18883 }
18884 else
18885 {
18886 relax_substateT subtype = fragp->fr_subtype;
18887 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18888 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18889 int first, second;
18890 fixS *fixp;
18891
18892 first = RELAX_FIRST (subtype);
18893 second = RELAX_SECOND (subtype);
18894 fixp = (fixS *) fragp->fr_opcode;
18895
18896 /* If the delay slot chosen does not match the size of the instruction,
18897 then emit a warning. */
18898 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18899 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18900 {
18901 relax_substateT s;
18902 const char *msg;
18903
18904 s = subtype & (RELAX_DELAY_SLOT_16BIT
18905 | RELAX_DELAY_SLOT_SIZE_FIRST
18906 | RELAX_DELAY_SLOT_SIZE_SECOND);
18907 msg = macro_warning (s);
18908 if (msg != NULL)
18909 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18910 subtype &= ~s;
18911 }
18912
18913 /* Possibly emit a warning if we've chosen the longer option. */
18914 if (use_second == second_longer)
18915 {
18916 relax_substateT s;
18917 const char *msg;
18918
18919 s = (subtype
18920 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18921 msg = macro_warning (s);
18922 if (msg != NULL)
18923 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18924 subtype &= ~s;
18925 }
18926
18927 /* Go through all the fixups for the first sequence. Disable them
18928 (by marking them as done) if we're going to use the second
18929 sequence instead. */
18930 while (fixp
18931 && fixp->fx_frag == fragp
18932 && fixp->fx_where < fragp->fr_fix - second)
18933 {
18934 if (subtype & RELAX_USE_SECOND)
18935 fixp->fx_done = 1;
18936 fixp = fixp->fx_next;
18937 }
18938
18939 /* Go through the fixups for the second sequence. Disable them if
18940 we're going to use the first sequence, otherwise adjust their
18941 addresses to account for the relaxation. */
18942 while (fixp && fixp->fx_frag == fragp)
18943 {
18944 if (subtype & RELAX_USE_SECOND)
18945 fixp->fx_where -= first;
18946 else
18947 fixp->fx_done = 1;
18948 fixp = fixp->fx_next;
18949 }
18950
18951 /* Now modify the frag contents. */
18952 if (subtype & RELAX_USE_SECOND)
18953 {
18954 char *start;
18955
18956 start = fragp->fr_literal + fragp->fr_fix - first - second;
18957 memmove (start, start + first, second);
18958 fragp->fr_fix -= first;
18959 }
18960 else
18961 fragp->fr_fix -= second;
18962 }
18963 }
18964
18965 /* This function is called after the relocs have been generated.
18966 We've been storing mips16 text labels as odd. Here we convert them
18967 back to even for the convenience of the debugger. */
18968
18969 void
18970 mips_frob_file_after_relocs (void)
18971 {
18972 asymbol **syms;
18973 unsigned int count, i;
18974
18975 syms = bfd_get_outsymbols (stdoutput);
18976 count = bfd_get_symcount (stdoutput);
18977 for (i = 0; i < count; i++, syms++)
18978 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18979 && ((*syms)->value & 1) != 0)
18980 {
18981 (*syms)->value &= ~1;
18982 /* If the symbol has an odd size, it was probably computed
18983 incorrectly, so adjust that as well. */
18984 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18985 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18986 }
18987 }
18988
18989 /* This function is called whenever a label is defined, including fake
18990 labels instantiated off the dot special symbol. It is used when
18991 handling branch delays; if a branch has a label, we assume we cannot
18992 move it. This also bumps the value of the symbol by 1 in compressed
18993 code. */
18994
18995 static void
18996 mips_record_label (symbolS *sym)
18997 {
18998 segment_info_type *si = seg_info (now_seg);
18999 struct insn_label_list *l;
19000
19001 if (free_insn_labels == NULL)
19002 l = XNEW (struct insn_label_list);
19003 else
19004 {
19005 l = free_insn_labels;
19006 free_insn_labels = l->next;
19007 }
19008
19009 l->label = sym;
19010 l->next = si->label_list;
19011 si->label_list = l;
19012 }
19013
19014 /* This function is called as tc_frob_label() whenever a label is defined
19015 and adds a DWARF-2 record we only want for true labels. */
19016
19017 void
19018 mips_define_label (symbolS *sym)
19019 {
19020 mips_record_label (sym);
19021 dwarf2_emit_label (sym);
19022 }
19023
19024 /* This function is called by tc_new_dot_label whenever a new dot symbol
19025 is defined. */
19026
19027 void
19028 mips_add_dot_label (symbolS *sym)
19029 {
19030 mips_record_label (sym);
19031 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19032 mips_compressed_mark_label (sym);
19033 }
19034 \f
19035 /* Converting ASE flags from internal to .MIPS.abiflags values. */
19036 static unsigned int
19037 mips_convert_ase_flags (int ase)
19038 {
19039 unsigned int ext_ases = 0;
19040
19041 if (ase & ASE_DSP)
19042 ext_ases |= AFL_ASE_DSP;
19043 if (ase & ASE_DSPR2)
19044 ext_ases |= AFL_ASE_DSPR2;
19045 if (ase & ASE_DSPR3)
19046 ext_ases |= AFL_ASE_DSPR3;
19047 if (ase & ASE_EVA)
19048 ext_ases |= AFL_ASE_EVA;
19049 if (ase & ASE_MCU)
19050 ext_ases |= AFL_ASE_MCU;
19051 if (ase & ASE_MDMX)
19052 ext_ases |= AFL_ASE_MDMX;
19053 if (ase & ASE_MIPS3D)
19054 ext_ases |= AFL_ASE_MIPS3D;
19055 if (ase & ASE_MT)
19056 ext_ases |= AFL_ASE_MT;
19057 if (ase & ASE_SMARTMIPS)
19058 ext_ases |= AFL_ASE_SMARTMIPS;
19059 if (ase & ASE_VIRT)
19060 ext_ases |= AFL_ASE_VIRT;
19061 if (ase & ASE_MSA)
19062 ext_ases |= AFL_ASE_MSA;
19063 if (ase & ASE_XPA)
19064 ext_ases |= AFL_ASE_XPA;
19065 if (ase & ASE_MIPS16E2)
19066 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
19067 if (ase & ASE_CRC)
19068 ext_ases |= AFL_ASE_CRC;
19069 if (ase & ASE_GINV)
19070 ext_ases |= AFL_ASE_GINV;
19071 if (ase & ASE_LOONGSON_MMI)
19072 ext_ases |= AFL_ASE_LOONGSON_MMI;
19073 if (ase & ASE_LOONGSON_CAM)
19074 ext_ases |= AFL_ASE_LOONGSON_CAM;
19075 if (ase & ASE_LOONGSON_EXT)
19076 ext_ases |= AFL_ASE_LOONGSON_EXT;
19077 if (ase & ASE_LOONGSON_EXT2)
19078 ext_ases |= AFL_ASE_LOONGSON_EXT2;
19079
19080 return ext_ases;
19081 }
19082 /* Some special processing for a MIPS ELF file. */
19083
19084 void
19085 mips_elf_final_processing (void)
19086 {
19087 int fpabi;
19088 Elf_Internal_ABIFlags_v0 flags;
19089
19090 flags.version = 0;
19091 flags.isa_rev = 0;
19092 switch (file_mips_opts.isa)
19093 {
19094 case INSN_ISA1:
19095 flags.isa_level = 1;
19096 break;
19097 case INSN_ISA2:
19098 flags.isa_level = 2;
19099 break;
19100 case INSN_ISA3:
19101 flags.isa_level = 3;
19102 break;
19103 case INSN_ISA4:
19104 flags.isa_level = 4;
19105 break;
19106 case INSN_ISA5:
19107 flags.isa_level = 5;
19108 break;
19109 case INSN_ISA32:
19110 flags.isa_level = 32;
19111 flags.isa_rev = 1;
19112 break;
19113 case INSN_ISA32R2:
19114 flags.isa_level = 32;
19115 flags.isa_rev = 2;
19116 break;
19117 case INSN_ISA32R3:
19118 flags.isa_level = 32;
19119 flags.isa_rev = 3;
19120 break;
19121 case INSN_ISA32R5:
19122 flags.isa_level = 32;
19123 flags.isa_rev = 5;
19124 break;
19125 case INSN_ISA32R6:
19126 flags.isa_level = 32;
19127 flags.isa_rev = 6;
19128 break;
19129 case INSN_ISA64:
19130 flags.isa_level = 64;
19131 flags.isa_rev = 1;
19132 break;
19133 case INSN_ISA64R2:
19134 flags.isa_level = 64;
19135 flags.isa_rev = 2;
19136 break;
19137 case INSN_ISA64R3:
19138 flags.isa_level = 64;
19139 flags.isa_rev = 3;
19140 break;
19141 case INSN_ISA64R5:
19142 flags.isa_level = 64;
19143 flags.isa_rev = 5;
19144 break;
19145 case INSN_ISA64R6:
19146 flags.isa_level = 64;
19147 flags.isa_rev = 6;
19148 break;
19149 }
19150
19151 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19152 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19153 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19154 : (file_mips_opts.fp == 64) ? AFL_REG_64
19155 : AFL_REG_32;
19156 flags.cpr2_size = AFL_REG_NONE;
19157 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19158 Tag_GNU_MIPS_ABI_FP);
19159 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19160 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19161 if (file_ase_mips16)
19162 flags.ases |= AFL_ASE_MIPS16;
19163 if (file_ase_micromips)
19164 flags.ases |= AFL_ASE_MICROMIPS;
19165 flags.flags1 = 0;
19166 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19167 || file_mips_opts.fp == 64)
19168 && file_mips_opts.oddspreg)
19169 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19170 flags.flags2 = 0;
19171
19172 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19173 ((Elf_External_ABIFlags_v0 *)
19174 mips_flags_frag));
19175
19176 /* Write out the register information. */
19177 if (mips_abi != N64_ABI)
19178 {
19179 Elf32_RegInfo s;
19180
19181 s.ri_gprmask = mips_gprmask;
19182 s.ri_cprmask[0] = mips_cprmask[0];
19183 s.ri_cprmask[1] = mips_cprmask[1];
19184 s.ri_cprmask[2] = mips_cprmask[2];
19185 s.ri_cprmask[3] = mips_cprmask[3];
19186 /* The gp_value field is set by the MIPS ELF backend. */
19187
19188 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19189 ((Elf32_External_RegInfo *)
19190 mips_regmask_frag));
19191 }
19192 else
19193 {
19194 Elf64_Internal_RegInfo s;
19195
19196 s.ri_gprmask = mips_gprmask;
19197 s.ri_pad = 0;
19198 s.ri_cprmask[0] = mips_cprmask[0];
19199 s.ri_cprmask[1] = mips_cprmask[1];
19200 s.ri_cprmask[2] = mips_cprmask[2];
19201 s.ri_cprmask[3] = mips_cprmask[3];
19202 /* The gp_value field is set by the MIPS ELF backend. */
19203
19204 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19205 ((Elf64_External_RegInfo *)
19206 mips_regmask_frag));
19207 }
19208
19209 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19210 sort of BFD interface for this. */
19211 if (mips_any_noreorder)
19212 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19213 if (mips_pic != NO_PIC)
19214 {
19215 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
19216 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19217 }
19218 if (mips_abicalls)
19219 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19220
19221 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19222 defined at present; this might need to change in future. */
19223 if (file_ase_mips16)
19224 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
19225 if (file_ase_micromips)
19226 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
19227 if (file_mips_opts.ase & ASE_MDMX)
19228 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
19229
19230 /* Set the MIPS ELF ABI flags. */
19231 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
19232 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
19233 else if (mips_abi == O64_ABI)
19234 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
19235 else if (mips_abi == EABI_ABI)
19236 {
19237 if (file_mips_opts.gp == 64)
19238 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19239 else
19240 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19241 }
19242
19243 /* Nothing to do for N32_ABI or N64_ABI. */
19244
19245 if (mips_32bitmode)
19246 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
19247
19248 if (mips_nan2008 == 1)
19249 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19250
19251 /* 32 bit code with 64 bit FP registers. */
19252 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19253 Tag_GNU_MIPS_ABI_FP);
19254 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
19255 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
19256 }
19257 \f
19258 typedef struct proc {
19259 symbolS *func_sym;
19260 symbolS *func_end_sym;
19261 unsigned long reg_mask;
19262 unsigned long reg_offset;
19263 unsigned long fpreg_mask;
19264 unsigned long fpreg_offset;
19265 unsigned long frame_offset;
19266 unsigned long frame_reg;
19267 unsigned long pc_reg;
19268 } procS;
19269
19270 static procS cur_proc;
19271 static procS *cur_proc_ptr;
19272 static int numprocs;
19273
19274 /* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19275 as "2", and a normal nop as "0". */
19276
19277 #define NOP_OPCODE_MIPS 0
19278 #define NOP_OPCODE_MIPS16 1
19279 #define NOP_OPCODE_MICROMIPS 2
19280
19281 char
19282 mips_nop_opcode (void)
19283 {
19284 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19285 return NOP_OPCODE_MICROMIPS;
19286 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19287 return NOP_OPCODE_MIPS16;
19288 else
19289 return NOP_OPCODE_MIPS;
19290 }
19291
19292 /* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19293 32-bit microMIPS NOPs here (if applicable). */
19294
19295 void
19296 mips_handle_align (fragS *fragp)
19297 {
19298 char nop_opcode;
19299 char *p;
19300 int bytes, size, excess;
19301 valueT opcode;
19302
19303 if (fragp->fr_type != rs_align_code)
19304 return;
19305
19306 p = fragp->fr_literal + fragp->fr_fix;
19307 nop_opcode = *p;
19308 switch (nop_opcode)
19309 {
19310 case NOP_OPCODE_MICROMIPS:
19311 opcode = micromips_nop32_insn.insn_opcode;
19312 size = 4;
19313 break;
19314 case NOP_OPCODE_MIPS16:
19315 opcode = mips16_nop_insn.insn_opcode;
19316 size = 2;
19317 break;
19318 case NOP_OPCODE_MIPS:
19319 default:
19320 opcode = nop_insn.insn_opcode;
19321 size = 4;
19322 break;
19323 }
19324
19325 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19326 excess = bytes % size;
19327
19328 /* Handle the leading part if we're not inserting a whole number of
19329 instructions, and make it the end of the fixed part of the frag.
19330 Try to fit in a short microMIPS NOP if applicable and possible,
19331 and use zeroes otherwise. */
19332 gas_assert (excess < 4);
19333 fragp->fr_fix += excess;
19334 switch (excess)
19335 {
19336 case 3:
19337 *p++ = '\0';
19338 /* Fall through. */
19339 case 2:
19340 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
19341 {
19342 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
19343 break;
19344 }
19345 *p++ = '\0';
19346 /* Fall through. */
19347 case 1:
19348 *p++ = '\0';
19349 /* Fall through. */
19350 case 0:
19351 break;
19352 }
19353
19354 md_number_to_chars (p, opcode, size);
19355 fragp->fr_var = size;
19356 }
19357
19358 static long
19359 get_number (void)
19360 {
19361 int negative = 0;
19362 long val = 0;
19363
19364 if (*input_line_pointer == '-')
19365 {
19366 ++input_line_pointer;
19367 negative = 1;
19368 }
19369 if (!ISDIGIT (*input_line_pointer))
19370 as_bad (_("expected simple number"));
19371 if (input_line_pointer[0] == '0')
19372 {
19373 if (input_line_pointer[1] == 'x')
19374 {
19375 input_line_pointer += 2;
19376 while (ISXDIGIT (*input_line_pointer))
19377 {
19378 val <<= 4;
19379 val |= hex_value (*input_line_pointer++);
19380 }
19381 return negative ? -val : val;
19382 }
19383 else
19384 {
19385 ++input_line_pointer;
19386 while (ISDIGIT (*input_line_pointer))
19387 {
19388 val <<= 3;
19389 val |= *input_line_pointer++ - '0';
19390 }
19391 return negative ? -val : val;
19392 }
19393 }
19394 if (!ISDIGIT (*input_line_pointer))
19395 {
19396 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19397 *input_line_pointer, *input_line_pointer);
19398 as_warn (_("invalid number"));
19399 return -1;
19400 }
19401 while (ISDIGIT (*input_line_pointer))
19402 {
19403 val *= 10;
19404 val += *input_line_pointer++ - '0';
19405 }
19406 return negative ? -val : val;
19407 }
19408
19409 /* The .file directive; just like the usual .file directive, but there
19410 is an initial number which is the ECOFF file index. In the non-ECOFF
19411 case .file implies DWARF-2. */
19412
19413 static void
19414 s_mips_file (int x ATTRIBUTE_UNUSED)
19415 {
19416 static int first_file_directive = 0;
19417
19418 if (ECOFF_DEBUGGING)
19419 {
19420 get_number ();
19421 s_app_file (0);
19422 }
19423 else
19424 {
19425 char *filename;
19426
19427 filename = dwarf2_directive_filename ();
19428
19429 /* Versions of GCC up to 3.1 start files with a ".file"
19430 directive even for stabs output. Make sure that this
19431 ".file" is handled. Note that you need a version of GCC
19432 after 3.1 in order to support DWARF-2 on MIPS. */
19433 if (filename != NULL && ! first_file_directive)
19434 {
19435 (void) new_logical_line (filename, -1);
19436 s_app_file_string (filename, 0);
19437 }
19438 first_file_directive = 1;
19439 }
19440 }
19441
19442 /* The .loc directive, implying DWARF-2. */
19443
19444 static void
19445 s_mips_loc (int x ATTRIBUTE_UNUSED)
19446 {
19447 if (!ECOFF_DEBUGGING)
19448 dwarf2_directive_loc (0);
19449 }
19450
19451 /* The .end directive. */
19452
19453 static void
19454 s_mips_end (int x ATTRIBUTE_UNUSED)
19455 {
19456 symbolS *p;
19457
19458 /* Following functions need their own .frame and .cprestore directives. */
19459 mips_frame_reg_valid = 0;
19460 mips_cprestore_valid = 0;
19461
19462 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19463 {
19464 p = get_symbol ();
19465 demand_empty_rest_of_line ();
19466 }
19467 else
19468 p = NULL;
19469
19470 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19471 as_warn (_(".end not in text section"));
19472
19473 if (!cur_proc_ptr)
19474 {
19475 as_warn (_(".end directive without a preceding .ent directive"));
19476 demand_empty_rest_of_line ();
19477 return;
19478 }
19479
19480 if (p != NULL)
19481 {
19482 gas_assert (S_GET_NAME (p));
19483 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
19484 as_warn (_(".end symbol does not match .ent symbol"));
19485
19486 if (debug_type == DEBUG_STABS)
19487 stabs_generate_asm_endfunc (S_GET_NAME (p),
19488 S_GET_NAME (p));
19489 }
19490 else
19491 as_warn (_(".end directive missing or unknown symbol"));
19492
19493 /* Create an expression to calculate the size of the function. */
19494 if (p && cur_proc_ptr)
19495 {
19496 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19497 expressionS *exp = XNEW (expressionS);
19498
19499 obj->size = exp;
19500 exp->X_op = O_subtract;
19501 exp->X_add_symbol = symbol_temp_new_now ();
19502 exp->X_op_symbol = p;
19503 exp->X_add_number = 0;
19504
19505 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19506 }
19507
19508 #ifdef md_flush_pending_output
19509 md_flush_pending_output ();
19510 #endif
19511
19512 /* Generate a .pdr section. */
19513 if (!ECOFF_DEBUGGING && mips_flag_pdr)
19514 {
19515 segT saved_seg = now_seg;
19516 subsegT saved_subseg = now_subseg;
19517 expressionS exp;
19518 char *fragp;
19519
19520 gas_assert (pdr_seg);
19521 subseg_set (pdr_seg, 0);
19522
19523 /* Write the symbol. */
19524 exp.X_op = O_symbol;
19525 exp.X_add_symbol = p;
19526 exp.X_add_number = 0;
19527 emit_expr (&exp, 4);
19528
19529 fragp = frag_more (7 * 4);
19530
19531 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19532 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19533 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19534 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19535 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19536 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19537 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19538
19539 subseg_set (saved_seg, saved_subseg);
19540 }
19541
19542 cur_proc_ptr = NULL;
19543 }
19544
19545 /* The .aent and .ent directives. */
19546
19547 static void
19548 s_mips_ent (int aent)
19549 {
19550 symbolS *symbolP;
19551
19552 symbolP = get_symbol ();
19553 if (*input_line_pointer == ',')
19554 ++input_line_pointer;
19555 SKIP_WHITESPACE ();
19556 if (ISDIGIT (*input_line_pointer)
19557 || *input_line_pointer == '-')
19558 get_number ();
19559
19560 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19561 as_warn (_(".ent or .aent not in text section"));
19562
19563 if (!aent && cur_proc_ptr)
19564 as_warn (_("missing .end"));
19565
19566 if (!aent)
19567 {
19568 /* This function needs its own .frame and .cprestore directives. */
19569 mips_frame_reg_valid = 0;
19570 mips_cprestore_valid = 0;
19571
19572 cur_proc_ptr = &cur_proc;
19573 memset (cur_proc_ptr, '\0', sizeof (procS));
19574
19575 cur_proc_ptr->func_sym = symbolP;
19576
19577 ++numprocs;
19578
19579 if (debug_type == DEBUG_STABS)
19580 stabs_generate_asm_func (S_GET_NAME (symbolP),
19581 S_GET_NAME (symbolP));
19582 }
19583
19584 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19585
19586 demand_empty_rest_of_line ();
19587 }
19588
19589 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19590 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19591 s_mips_frame is used so that we can set the PDR information correctly.
19592 We can't use the ecoff routines because they make reference to the ecoff
19593 symbol table (in the mdebug section). */
19594
19595 static void
19596 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19597 {
19598 if (ECOFF_DEBUGGING)
19599 s_ignore (ignore);
19600 else
19601 {
19602 long val;
19603
19604 if (cur_proc_ptr == (procS *) NULL)
19605 {
19606 as_warn (_(".frame outside of .ent"));
19607 demand_empty_rest_of_line ();
19608 return;
19609 }
19610
19611 cur_proc_ptr->frame_reg = tc_get_register (1);
19612
19613 SKIP_WHITESPACE ();
19614 if (*input_line_pointer++ != ','
19615 || get_absolute_expression_and_terminator (&val) != ',')
19616 {
19617 as_warn (_("bad .frame directive"));
19618 --input_line_pointer;
19619 demand_empty_rest_of_line ();
19620 return;
19621 }
19622
19623 cur_proc_ptr->frame_offset = val;
19624 cur_proc_ptr->pc_reg = tc_get_register (0);
19625
19626 demand_empty_rest_of_line ();
19627 }
19628 }
19629
19630 /* The .fmask and .mask directives. If the mdebug section is present
19631 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19632 embedded targets, s_mips_mask is used so that we can set the PDR
19633 information correctly. We can't use the ecoff routines because they
19634 make reference to the ecoff symbol table (in the mdebug section). */
19635
19636 static void
19637 s_mips_mask (int reg_type)
19638 {
19639 if (ECOFF_DEBUGGING)
19640 s_ignore (reg_type);
19641 else
19642 {
19643 long mask, off;
19644
19645 if (cur_proc_ptr == (procS *) NULL)
19646 {
19647 as_warn (_(".mask/.fmask outside of .ent"));
19648 demand_empty_rest_of_line ();
19649 return;
19650 }
19651
19652 if (get_absolute_expression_and_terminator (&mask) != ',')
19653 {
19654 as_warn (_("bad .mask/.fmask directive"));
19655 --input_line_pointer;
19656 demand_empty_rest_of_line ();
19657 return;
19658 }
19659
19660 off = get_absolute_expression ();
19661
19662 if (reg_type == 'F')
19663 {
19664 cur_proc_ptr->fpreg_mask = mask;
19665 cur_proc_ptr->fpreg_offset = off;
19666 }
19667 else
19668 {
19669 cur_proc_ptr->reg_mask = mask;
19670 cur_proc_ptr->reg_offset = off;
19671 }
19672
19673 demand_empty_rest_of_line ();
19674 }
19675 }
19676
19677 /* A table describing all the processors gas knows about. Names are
19678 matched in the order listed.
19679
19680 To ease comparison, please keep this table in the same order as
19681 gcc's mips_cpu_info_table[]. */
19682 static const struct mips_cpu_info mips_cpu_info_table[] =
19683 {
19684 /* Entries for generic ISAs */
19685 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19686 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19687 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19688 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19689 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19690 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19691 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19692 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19693 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
19694 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
19695 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19696 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
19697 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19698 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
19699 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
19700
19701 /* MIPS I */
19702 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19703 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19704 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
19705
19706 /* MIPS II */
19707 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
19708
19709 /* MIPS III */
19710 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19711 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19712 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19713 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19714 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19715 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19716 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19717 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19718 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19719 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19720 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19721 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19722 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
19723 /* ST Microelectronics Loongson 2E and 2F cores */
19724 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19725 { "loongson2f", 0, ASE_LOONGSON_MMI, ISA_MIPS3, CPU_LOONGSON_2F },
19726
19727 /* MIPS IV */
19728 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19729 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19730 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19731 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19732 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19733 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19734 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19735 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19736 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19737 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19738 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19739 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19740 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19741 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19742 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
19743
19744 /* MIPS 32 */
19745 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19746 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19747 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19748 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19749
19750 /* MIPS 32 Release 2 */
19751 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19752 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19753 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19754 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19755 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19756 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19757 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19758 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19759 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19760 ISA_MIPS32R2, CPU_MIPS32R2 },
19761 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19762 ISA_MIPS32R2, CPU_MIPS32R2 },
19763 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19764 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19765 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19766 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19767 /* Deprecated forms of the above. */
19768 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19769 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19770 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
19771 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19772 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19773 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19774 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19775 /* Deprecated forms of the above. */
19776 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19777 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19778 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
19779 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19780 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19781 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19782 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19783 /* Deprecated forms of the above. */
19784 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19785 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19786 /* 34Kn is a 34kc without DSP. */
19787 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19788 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19789 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19790 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19791 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19792 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19793 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19794 /* Deprecated forms of the above. */
19795 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19796 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19797 /* 1004K cores are multiprocessor versions of the 34K. */
19798 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19799 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19800 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19801 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19802 /* interaptiv is the new name for 1004kf */
19803 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19804 { "interaptiv-mr2", 0,
19805 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
19806 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
19807 /* M5100 family */
19808 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19809 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19810 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
19811 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
19812
19813 /* MIPS 64 */
19814 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19815 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19816 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19817 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19818
19819 /* Broadcom SB-1 CPU core */
19820 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19821 /* Broadcom SB-1A CPU core */
19822 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19823
19824 /* MIPS 64 Release 2 */
19825 /* Loongson CPU core */
19826 /* -march=loongson3a is an alias of -march=gs464 for compatibility */
19827 { "loongson3a", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
19828 ISA_MIPS64R2, CPU_GS464 },
19829 { "gs464", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
19830 ISA_MIPS64R2, CPU_GS464 },
19831 { "gs464e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
19832 | ASE_LOONGSON_EXT2, ISA_MIPS64R2, CPU_GS464E },
19833 { "gs264e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
19834 | ASE_LOONGSON_EXT2 | ASE_MSA | ASE_MSA64, ISA_MIPS64R2, CPU_GS264E },
19835
19836 /* Cavium Networks Octeon CPU core */
19837 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19838 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19839 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
19840 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
19841
19842 /* RMI Xlr */
19843 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
19844
19845 /* Broadcom XLP.
19846 XLP is mostly like XLR, with the prominent exception that it is
19847 MIPS64R2 rather than MIPS64. */
19848 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
19849
19850 /* MIPS 64 Release 6 */
19851 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
19852 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
19853
19854 /* End marker */
19855 { NULL, 0, 0, 0, 0 }
19856 };
19857
19858
19859 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19860 with a final "000" replaced by "k". Ignore case.
19861
19862 Note: this function is shared between GCC and GAS. */
19863
19864 static bfd_boolean
19865 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19866 {
19867 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19868 given++, canonical++;
19869
19870 return ((*given == 0 && *canonical == 0)
19871 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19872 }
19873
19874
19875 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19876 CPU name. We've traditionally allowed a lot of variation here.
19877
19878 Note: this function is shared between GCC and GAS. */
19879
19880 static bfd_boolean
19881 mips_matching_cpu_name_p (const char *canonical, const char *given)
19882 {
19883 /* First see if the name matches exactly, or with a final "000"
19884 turned into "k". */
19885 if (mips_strict_matching_cpu_name_p (canonical, given))
19886 return TRUE;
19887
19888 /* If not, try comparing based on numerical designation alone.
19889 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19890 if (TOLOWER (*given) == 'r')
19891 given++;
19892 if (!ISDIGIT (*given))
19893 return FALSE;
19894
19895 /* Skip over some well-known prefixes in the canonical name,
19896 hoping to find a number there too. */
19897 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19898 canonical += 2;
19899 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19900 canonical += 2;
19901 else if (TOLOWER (canonical[0]) == 'r')
19902 canonical += 1;
19903
19904 return mips_strict_matching_cpu_name_p (canonical, given);
19905 }
19906
19907
19908 /* Parse an option that takes the name of a processor as its argument.
19909 OPTION is the name of the option and CPU_STRING is the argument.
19910 Return the corresponding processor enumeration if the CPU_STRING is
19911 recognized, otherwise report an error and return null.
19912
19913 A similar function exists in GCC. */
19914
19915 static const struct mips_cpu_info *
19916 mips_parse_cpu (const char *option, const char *cpu_string)
19917 {
19918 const struct mips_cpu_info *p;
19919
19920 /* 'from-abi' selects the most compatible architecture for the given
19921 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19922 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19923 version. Look first at the -mgp options, if given, otherwise base
19924 the choice on MIPS_DEFAULT_64BIT.
19925
19926 Treat NO_ABI like the EABIs. One reason to do this is that the
19927 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19928 architecture. This code picks MIPS I for 'mips' and MIPS III for
19929 'mips64', just as we did in the days before 'from-abi'. */
19930 if (strcasecmp (cpu_string, "from-abi") == 0)
19931 {
19932 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19933 return mips_cpu_info_from_isa (ISA_MIPS1);
19934
19935 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19936 return mips_cpu_info_from_isa (ISA_MIPS3);
19937
19938 if (file_mips_opts.gp >= 0)
19939 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
19940 ? ISA_MIPS1 : ISA_MIPS3);
19941
19942 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19943 ? ISA_MIPS3
19944 : ISA_MIPS1);
19945 }
19946
19947 /* 'default' has traditionally been a no-op. Probably not very useful. */
19948 if (strcasecmp (cpu_string, "default") == 0)
19949 return 0;
19950
19951 for (p = mips_cpu_info_table; p->name != 0; p++)
19952 if (mips_matching_cpu_name_p (p->name, cpu_string))
19953 return p;
19954
19955 as_bad (_("bad value (%s) for %s"), cpu_string, option);
19956 return 0;
19957 }
19958
19959 /* Return the canonical processor information for ISA (a member of the
19960 ISA_MIPS* enumeration). */
19961
19962 static const struct mips_cpu_info *
19963 mips_cpu_info_from_isa (int isa)
19964 {
19965 int i;
19966
19967 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19968 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19969 && isa == mips_cpu_info_table[i].isa)
19970 return (&mips_cpu_info_table[i]);
19971
19972 return NULL;
19973 }
19974
19975 static const struct mips_cpu_info *
19976 mips_cpu_info_from_arch (int arch)
19977 {
19978 int i;
19979
19980 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19981 if (arch == mips_cpu_info_table[i].cpu)
19982 return (&mips_cpu_info_table[i]);
19983
19984 return NULL;
19985 }
19986 \f
19987 static void
19988 show (FILE *stream, const char *string, int *col_p, int *first_p)
19989 {
19990 if (*first_p)
19991 {
19992 fprintf (stream, "%24s", "");
19993 *col_p = 24;
19994 }
19995 else
19996 {
19997 fprintf (stream, ", ");
19998 *col_p += 2;
19999 }
20000
20001 if (*col_p + strlen (string) > 72)
20002 {
20003 fprintf (stream, "\n%24s", "");
20004 *col_p = 24;
20005 }
20006
20007 fprintf (stream, "%s", string);
20008 *col_p += strlen (string);
20009
20010 *first_p = 0;
20011 }
20012
20013 void
20014 md_show_usage (FILE *stream)
20015 {
20016 int column, first;
20017 size_t i;
20018
20019 fprintf (stream, _("\
20020 MIPS options:\n\
20021 -EB generate big endian output\n\
20022 -EL generate little endian output\n\
20023 -g, -g2 do not remove unneeded NOPs or swap branches\n\
20024 -G NUM allow referencing objects up to NUM bytes\n\
20025 implicitly with the gp register [default 8]\n"));
20026 fprintf (stream, _("\
20027 -mips1 generate MIPS ISA I instructions\n\
20028 -mips2 generate MIPS ISA II instructions\n\
20029 -mips3 generate MIPS ISA III instructions\n\
20030 -mips4 generate MIPS ISA IV instructions\n\
20031 -mips5 generate MIPS ISA V instructions\n\
20032 -mips32 generate MIPS32 ISA instructions\n\
20033 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
20034 -mips32r3 generate MIPS32 release 3 ISA instructions\n\
20035 -mips32r5 generate MIPS32 release 5 ISA instructions\n\
20036 -mips32r6 generate MIPS32 release 6 ISA instructions\n\
20037 -mips64 generate MIPS64 ISA instructions\n\
20038 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
20039 -mips64r3 generate MIPS64 release 3 ISA instructions\n\
20040 -mips64r5 generate MIPS64 release 5 ISA instructions\n\
20041 -mips64r6 generate MIPS64 release 6 ISA instructions\n\
20042 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
20043
20044 first = 1;
20045
20046 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20047 show (stream, mips_cpu_info_table[i].name, &column, &first);
20048 show (stream, "from-abi", &column, &first);
20049 fputc ('\n', stream);
20050
20051 fprintf (stream, _("\
20052 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20053 -no-mCPU don't generate code specific to CPU.\n\
20054 For -mCPU and -no-mCPU, CPU must be one of:\n"));
20055
20056 first = 1;
20057
20058 show (stream, "3900", &column, &first);
20059 show (stream, "4010", &column, &first);
20060 show (stream, "4100", &column, &first);
20061 show (stream, "4650", &column, &first);
20062 fputc ('\n', stream);
20063
20064 fprintf (stream, _("\
20065 -mips16 generate mips16 instructions\n\
20066 -no-mips16 do not generate mips16 instructions\n"));
20067 fprintf (stream, _("\
20068 -mmips16e2 generate MIPS16e2 instructions\n\
20069 -mno-mips16e2 do not generate MIPS16e2 instructions\n"));
20070 fprintf (stream, _("\
20071 -mmicromips generate microMIPS instructions\n\
20072 -mno-micromips do not generate microMIPS instructions\n"));
20073 fprintf (stream, _("\
20074 -msmartmips generate smartmips instructions\n\
20075 -mno-smartmips do not generate smartmips instructions\n"));
20076 fprintf (stream, _("\
20077 -mdsp generate DSP instructions\n\
20078 -mno-dsp do not generate DSP instructions\n"));
20079 fprintf (stream, _("\
20080 -mdspr2 generate DSP R2 instructions\n\
20081 -mno-dspr2 do not generate DSP R2 instructions\n"));
20082 fprintf (stream, _("\
20083 -mdspr3 generate DSP R3 instructions\n\
20084 -mno-dspr3 do not generate DSP R3 instructions\n"));
20085 fprintf (stream, _("\
20086 -mmt generate MT instructions\n\
20087 -mno-mt do not generate MT instructions\n"));
20088 fprintf (stream, _("\
20089 -mmcu generate MCU instructions\n\
20090 -mno-mcu do not generate MCU instructions\n"));
20091 fprintf (stream, _("\
20092 -mmsa generate MSA instructions\n\
20093 -mno-msa do not generate MSA instructions\n"));
20094 fprintf (stream, _("\
20095 -mxpa generate eXtended Physical Address (XPA) instructions\n\
20096 -mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20097 fprintf (stream, _("\
20098 -mvirt generate Virtualization instructions\n\
20099 -mno-virt do not generate Virtualization instructions\n"));
20100 fprintf (stream, _("\
20101 -mcrc generate CRC instructions\n\
20102 -mno-crc do not generate CRC instructions\n"));
20103 fprintf (stream, _("\
20104 -mginv generate Global INValidate (GINV) instructions\n\
20105 -mno-ginv do not generate Global INValidate instructions\n"));
20106 fprintf (stream, _("\
20107 -mloongson-mmi generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20108 -mno-loongson-mmi do not generate Loongson MultiMedia extensions Instructions\n"));
20109 fprintf (stream, _("\
20110 -mloongson-cam generate Loongson Content Address Memory (CAM) instructions\n\
20111 -mno-loongson-cam do not generate Loongson Content Address Memory Instructions\n"));
20112 fprintf (stream, _("\
20113 -mloongson-ext generate Loongson EXTensions (EXT) instructions\n\
20114 -mno-loongson-ext do not generate Loongson EXTensions Instructions\n"));
20115 fprintf (stream, _("\
20116 -mloongson-ext2 generate Loongson EXTensions R2 (EXT2) instructions\n\
20117 -mno-loongson-ext2 do not generate Loongson EXTensions R2 Instructions\n"));
20118 fprintf (stream, _("\
20119 -minsn32 only generate 32-bit microMIPS instructions\n\
20120 -mno-insn32 generate all microMIPS instructions\n"));
20121 fprintf (stream, _("\
20122 -mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20123 -mfix-loongson2f-nop work around Loongson2F NOP errata\n\
20124 -mfix-vr4120 work around certain VR4120 errata\n\
20125 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
20126 -mfix-24k insert a nop after ERET and DERET instructions\n\
20127 -mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
20128 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20129 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
20130 -msym32 assume all symbols have 32-bit values\n\
20131 -O0 do not remove unneeded NOPs, do not swap branches\n\
20132 -O, -O1 remove unneeded NOPs, do not swap branches\n\
20133 -O2 remove unneeded NOPs and swap branches\n\
20134 --trap, --no-break trap exception on div by 0 and mult overflow\n\
20135 --break, --no-trap break exception on div by 0 and mult overflow\n"));
20136 fprintf (stream, _("\
20137 -mhard-float allow floating-point instructions\n\
20138 -msoft-float do not allow floating-point instructions\n\
20139 -msingle-float only allow 32-bit floating-point operations\n\
20140 -mdouble-float allow 32-bit and 64-bit floating-point operations\n\
20141 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
20142 --[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
20143 -mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20144 -mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
20145 -mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20146
20147 first = 1;
20148
20149 show (stream, "legacy", &column, &first);
20150 show (stream, "2008", &column, &first);
20151
20152 fputc ('\n', stream);
20153
20154 fprintf (stream, _("\
20155 -KPIC, -call_shared generate SVR4 position independent code\n\
20156 -call_nonpic generate non-PIC code that can operate with DSOs\n\
20157 -mvxworks-pic generate VxWorks position independent code\n\
20158 -non_shared do not generate code that can operate with DSOs\n\
20159 -xgot assume a 32 bit GOT\n\
20160 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
20161 -mshared, -mno-shared disable/enable .cpload optimization for\n\
20162 position dependent (non shared) code\n\
20163 -mabi=ABI create ABI conformant object file for:\n"));
20164
20165 first = 1;
20166
20167 show (stream, "32", &column, &first);
20168 show (stream, "o64", &column, &first);
20169 show (stream, "n32", &column, &first);
20170 show (stream, "64", &column, &first);
20171 show (stream, "eabi", &column, &first);
20172
20173 fputc ('\n', stream);
20174
20175 fprintf (stream, _("\
20176 -32 create o32 ABI object file%s\n"),
20177 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20178 fprintf (stream, _("\
20179 -n32 create n32 ABI object file%s\n"),
20180 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20181 fprintf (stream, _("\
20182 -64 create 64 ABI object file%s\n"),
20183 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
20184 }
20185
20186 #ifdef TE_IRIX
20187 enum dwarf2_format
20188 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
20189 {
20190 if (HAVE_64BIT_SYMBOLS)
20191 return dwarf2_format_64bit_irix;
20192 else
20193 return dwarf2_format_32bit;
20194 }
20195 #endif
20196
20197 int
20198 mips_dwarf2_addr_size (void)
20199 {
20200 if (HAVE_64BIT_OBJECTS)
20201 return 8;
20202 else
20203 return 4;
20204 }
20205
20206 /* Standard calling conventions leave the CFA at SP on entry. */
20207 void
20208 mips_cfi_frame_initial_instructions (void)
20209 {
20210 cfi_add_CFA_def_cfa_register (SP);
20211 }
20212
20213 int
20214 tc_mips_regname_to_dw2regnum (char *regname)
20215 {
20216 unsigned int regnum = -1;
20217 unsigned int reg;
20218
20219 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20220 regnum = reg;
20221
20222 return regnum;
20223 }
20224
20225 /* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20226 Given a symbolic attribute NAME, return the proper integer value.
20227 Returns -1 if the attribute is not known. */
20228
20229 int
20230 mips_convert_symbolic_attribute (const char *name)
20231 {
20232 static const struct
20233 {
20234 const char * name;
20235 const int tag;
20236 }
20237 attribute_table[] =
20238 {
20239 #define T(tag) {#tag, tag}
20240 T (Tag_GNU_MIPS_ABI_FP),
20241 T (Tag_GNU_MIPS_ABI_MSA),
20242 #undef T
20243 };
20244 unsigned int i;
20245
20246 if (name == NULL)
20247 return -1;
20248
20249 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20250 if (streq (name, attribute_table[i].name))
20251 return attribute_table[i].tag;
20252
20253 return -1;
20254 }
20255
20256 void
20257 md_mips_end (void)
20258 {
20259 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20260
20261 mips_emit_delays ();
20262 if (cur_proc_ptr)
20263 as_warn (_("missing .end at end of assembly"));
20264
20265 /* Just in case no code was emitted, do the consistency check. */
20266 file_mips_check_options ();
20267
20268 /* Set a floating-point ABI if the user did not. */
20269 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20270 {
20271 /* Perform consistency checks on the floating-point ABI. */
20272 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20273 Tag_GNU_MIPS_ABI_FP);
20274 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20275 check_fpabi (fpabi);
20276 }
20277 else
20278 {
20279 /* Soft-float gets precedence over single-float, the two options should
20280 not be used together so this should not matter. */
20281 if (file_mips_opts.soft_float == 1)
20282 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20283 /* Single-float gets precedence over all double_float cases. */
20284 else if (file_mips_opts.single_float == 1)
20285 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20286 else
20287 {
20288 switch (file_mips_opts.fp)
20289 {
20290 case 32:
20291 if (file_mips_opts.gp == 32)
20292 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20293 break;
20294 case 0:
20295 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20296 break;
20297 case 64:
20298 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20299 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20300 else if (file_mips_opts.gp == 32)
20301 fpabi = Val_GNU_MIPS_ABI_FP_64;
20302 else
20303 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20304 break;
20305 }
20306 }
20307
20308 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20309 Tag_GNU_MIPS_ABI_FP, fpabi);
20310 }
20311 }
20312
20313 /* Returns the relocation type required for a particular CFI encoding. */
20314
20315 bfd_reloc_code_real_type
20316 mips_cfi_reloc_for_encoding (int encoding)
20317 {
20318 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20319 return BFD_RELOC_32_PCREL;
20320 else return BFD_RELOC_NONE;
20321 }
This page took 0.456537 seconds and 5 git commands to generate.