MIPS/GAS: Split Loongson MMI Instructions from loongson2f/3a
[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_LOONGSON_3A)
426
427 /* Return true if ISA supports move to/from high part of a 64-bit
428 floating-point register. */
429 #define ISA_HAS_MXHC1(ISA) \
430 ((ISA) == ISA_MIPS32R2 \
431 || (ISA) == ISA_MIPS32R3 \
432 || (ISA) == ISA_MIPS32R5 \
433 || (ISA) == ISA_MIPS32R6 \
434 || (ISA) == ISA_MIPS64R2 \
435 || (ISA) == ISA_MIPS64R3 \
436 || (ISA) == ISA_MIPS64R5 \
437 || (ISA) == ISA_MIPS64R6)
438
439 /* Return true if ISA supports legacy NAN. */
440 #define ISA_HAS_LEGACY_NAN(ISA) \
441 ((ISA) == ISA_MIPS1 \
442 || (ISA) == ISA_MIPS2 \
443 || (ISA) == ISA_MIPS3 \
444 || (ISA) == ISA_MIPS4 \
445 || (ISA) == ISA_MIPS5 \
446 || (ISA) == ISA_MIPS32 \
447 || (ISA) == ISA_MIPS32R2 \
448 || (ISA) == ISA_MIPS32R3 \
449 || (ISA) == ISA_MIPS32R5 \
450 || (ISA) == ISA_MIPS64 \
451 || (ISA) == ISA_MIPS64R2 \
452 || (ISA) == ISA_MIPS64R3 \
453 || (ISA) == ISA_MIPS64R5)
454
455 #define GPR_SIZE \
456 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
457 ? 32 \
458 : mips_opts.gp)
459
460 #define FPR_SIZE \
461 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
462 ? 32 \
463 : mips_opts.fp)
464
465 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
466
467 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
468
469 /* True if relocations are stored in-place. */
470 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
471
472 /* The ABI-derived address size. */
473 #define HAVE_64BIT_ADDRESSES \
474 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
475 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
476
477 /* The size of symbolic constants (i.e., expressions of the form
478 "SYMBOL" or "SYMBOL + OFFSET"). */
479 #define HAVE_32BIT_SYMBOLS \
480 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
481 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
482
483 /* Addresses are loaded in different ways, depending on the address size
484 in use. The n32 ABI Documentation also mandates the use of additions
485 with overflow checking, but existing implementations don't follow it. */
486 #define ADDRESS_ADD_INSN \
487 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
488
489 #define ADDRESS_ADDI_INSN \
490 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
491
492 #define ADDRESS_LOAD_INSN \
493 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
494
495 #define ADDRESS_STORE_INSN \
496 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
497
498 /* Return true if the given CPU supports the MIPS16 ASE. */
499 #define CPU_HAS_MIPS16(cpu) \
500 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
501 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
502
503 /* Return true if the given CPU supports the microMIPS ASE. */
504 #define CPU_HAS_MICROMIPS(cpu) 0
505
506 /* True if CPU has a dror instruction. */
507 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
508
509 /* True if CPU has a ror instruction. */
510 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
511
512 /* True if CPU is in the Octeon family */
513 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
514 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
515
516 /* True if CPU has seq/sne and seqi/snei instructions. */
517 #define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
518
519 /* True, if CPU has support for ldc1 and sdc1. */
520 #define CPU_HAS_LDC1_SDC1(CPU) \
521 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
522
523 /* True if mflo and mfhi can be immediately followed by instructions
524 which write to the HI and LO registers.
525
526 According to MIPS specifications, MIPS ISAs I, II, and III need
527 (at least) two instructions between the reads of HI/LO and
528 instructions which write them, and later ISAs do not. Contradicting
529 the MIPS specifications, some MIPS IV processor user manuals (e.g.
530 the UM for the NEC Vr5000) document needing the instructions between
531 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
532 MIPS64 and later ISAs to have the interlocks, plus any specific
533 earlier-ISA CPUs for which CPU documentation declares that the
534 instructions are really interlocked. */
535 #define hilo_interlocks \
536 (mips_opts.isa == ISA_MIPS32 \
537 || mips_opts.isa == ISA_MIPS32R2 \
538 || mips_opts.isa == ISA_MIPS32R3 \
539 || mips_opts.isa == ISA_MIPS32R5 \
540 || mips_opts.isa == ISA_MIPS32R6 \
541 || mips_opts.isa == ISA_MIPS64 \
542 || mips_opts.isa == ISA_MIPS64R2 \
543 || mips_opts.isa == ISA_MIPS64R3 \
544 || mips_opts.isa == ISA_MIPS64R5 \
545 || mips_opts.isa == ISA_MIPS64R6 \
546 || mips_opts.arch == CPU_R4010 \
547 || mips_opts.arch == CPU_R5900 \
548 || mips_opts.arch == CPU_R10000 \
549 || mips_opts.arch == CPU_R12000 \
550 || mips_opts.arch == CPU_R14000 \
551 || mips_opts.arch == CPU_R16000 \
552 || mips_opts.arch == CPU_RM7000 \
553 || mips_opts.arch == CPU_VR5500 \
554 || mips_opts.micromips \
555 )
556
557 /* Whether the processor uses hardware interlocks to protect reads
558 from the GPRs after they are loaded from memory, and thus does not
559 require nops to be inserted. This applies to instructions marked
560 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
561 level I and microMIPS mode instructions are always interlocked. */
562 #define gpr_interlocks \
563 (mips_opts.isa != ISA_MIPS1 \
564 || mips_opts.arch == CPU_R3900 \
565 || mips_opts.arch == CPU_R5900 \
566 || mips_opts.micromips \
567 )
568
569 /* Whether the processor uses hardware interlocks to avoid delays
570 required by coprocessor instructions, and thus does not require
571 nops to be inserted. This applies to instructions marked
572 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
573 instructions marked INSN_WRITE_COND_CODE and ones marked
574 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
575 levels I, II, and III and microMIPS mode instructions are always
576 interlocked. */
577 /* Itbl support may require additional care here. */
578 #define cop_interlocks \
579 ((mips_opts.isa != ISA_MIPS1 \
580 && mips_opts.isa != ISA_MIPS2 \
581 && mips_opts.isa != ISA_MIPS3) \
582 || mips_opts.arch == CPU_R4300 \
583 || mips_opts.micromips \
584 )
585
586 /* Whether the processor uses hardware interlocks to protect reads
587 from coprocessor registers after they are loaded from memory, and
588 thus does not require nops to be inserted. This applies to
589 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
590 requires at MIPS ISA level I and microMIPS mode instructions are
591 always interlocked. */
592 #define cop_mem_interlocks \
593 (mips_opts.isa != ISA_MIPS1 \
594 || mips_opts.micromips \
595 )
596
597 /* Is this a mfhi or mflo instruction? */
598 #define MF_HILO_INSN(PINFO) \
599 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
600
601 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
602 has been selected. This implies, in particular, that addresses of text
603 labels have their LSB set. */
604 #define HAVE_CODE_COMPRESSION \
605 ((mips_opts.mips16 | mips_opts.micromips) != 0)
606
607 /* The minimum and maximum signed values that can be stored in a GPR. */
608 #define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
609 #define GPR_SMIN (-GPR_SMAX - 1)
610
611 /* MIPS PIC level. */
612
613 enum mips_pic_level mips_pic;
614
615 /* 1 if we should generate 32 bit offsets from the $gp register in
616 SVR4_PIC mode. Currently has no meaning in other modes. */
617 static int mips_big_got = 0;
618
619 /* 1 if trap instructions should used for overflow rather than break
620 instructions. */
621 static int mips_trap = 0;
622
623 /* 1 if double width floating point constants should not be constructed
624 by assembling two single width halves into two single width floating
625 point registers which just happen to alias the double width destination
626 register. On some architectures this aliasing can be disabled by a bit
627 in the status register, and the setting of this bit cannot be determined
628 automatically at assemble time. */
629 static int mips_disable_float_construction;
630
631 /* Non-zero if any .set noreorder directives were used. */
632
633 static int mips_any_noreorder;
634
635 /* Non-zero if nops should be inserted when the register referenced in
636 an mfhi/mflo instruction is read in the next two instructions. */
637 static int mips_7000_hilo_fix;
638
639 /* The size of objects in the small data section. */
640 static unsigned int g_switch_value = 8;
641 /* Whether the -G option was used. */
642 static int g_switch_seen = 0;
643
644 #define N_RMASK 0xc4
645 #define N_VFP 0xd4
646
647 /* If we can determine in advance that GP optimization won't be
648 possible, we can skip the relaxation stuff that tries to produce
649 GP-relative references. This makes delay slot optimization work
650 better.
651
652 This function can only provide a guess, but it seems to work for
653 gcc output. It needs to guess right for gcc, otherwise gcc
654 will put what it thinks is a GP-relative instruction in a branch
655 delay slot.
656
657 I don't know if a fix is needed for the SVR4_PIC mode. I've only
658 fixed it for the non-PIC mode. KR 95/04/07 */
659 static int nopic_need_relax (symbolS *, int);
660
661 /* handle of the OPCODE hash table */
662 static struct hash_control *op_hash = NULL;
663
664 /* The opcode hash table we use for the mips16. */
665 static struct hash_control *mips16_op_hash = NULL;
666
667 /* The opcode hash table we use for the microMIPS ASE. */
668 static struct hash_control *micromips_op_hash = NULL;
669
670 /* This array holds the chars that always start a comment. If the
671 pre-processor is disabled, these aren't very useful */
672 const char comment_chars[] = "#";
673
674 /* This array holds the chars that only start a comment at the beginning of
675 a line. If the line seems to have the form '# 123 filename'
676 .line and .file directives will appear in the pre-processed output */
677 /* Note that input_file.c hand checks for '#' at the beginning of the
678 first line of the input file. This is because the compiler outputs
679 #NO_APP at the beginning of its output. */
680 /* Also note that C style comments are always supported. */
681 const char line_comment_chars[] = "#";
682
683 /* This array holds machine specific line separator characters. */
684 const char line_separator_chars[] = ";";
685
686 /* Chars that can be used to separate mant from exp in floating point nums */
687 const char EXP_CHARS[] = "eE";
688
689 /* Chars that mean this number is a floating point constant */
690 /* As in 0f12.456 */
691 /* or 0d1.2345e12 */
692 const char FLT_CHARS[] = "rRsSfFdDxXpP";
693
694 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
695 changed in read.c . Ideally it shouldn't have to know about it at all,
696 but nothing is ideal around here.
697 */
698
699 /* Types of printf format used for instruction-related error messages.
700 "I" means int ("%d") and "S" means string ("%s"). */
701 enum mips_insn_error_format {
702 ERR_FMT_PLAIN,
703 ERR_FMT_I,
704 ERR_FMT_SS,
705 };
706
707 /* Information about an error that was found while assembling the current
708 instruction. */
709 struct mips_insn_error {
710 /* We sometimes need to match an instruction against more than one
711 opcode table entry. Errors found during this matching are reported
712 against a particular syntactic argument rather than against the
713 instruction as a whole. We grade these messages so that errors
714 against argument N have a greater priority than an error against
715 any argument < N, since the former implies that arguments up to N
716 were acceptable and that the opcode entry was therefore a closer match.
717 If several matches report an error against the same argument,
718 we only use that error if it is the same in all cases.
719
720 min_argnum is the minimum argument number for which an error message
721 should be accepted. It is 0 if MSG is against the instruction as
722 a whole. */
723 int min_argnum;
724
725 /* The printf()-style message, including its format and arguments. */
726 enum mips_insn_error_format format;
727 const char *msg;
728 union {
729 int i;
730 const char *ss[2];
731 } u;
732 };
733
734 /* The error that should be reported for the current instruction. */
735 static struct mips_insn_error insn_error;
736
737 static int auto_align = 1;
738
739 /* When outputting SVR4 PIC code, the assembler needs to know the
740 offset in the stack frame from which to restore the $gp register.
741 This is set by the .cprestore pseudo-op, and saved in this
742 variable. */
743 static offsetT mips_cprestore_offset = -1;
744
745 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
746 more optimizations, it can use a register value instead of a memory-saved
747 offset and even an other register than $gp as global pointer. */
748 static offsetT mips_cpreturn_offset = -1;
749 static int mips_cpreturn_register = -1;
750 static int mips_gp_register = GP;
751 static int mips_gprel_offset = 0;
752
753 /* Whether mips_cprestore_offset has been set in the current function
754 (or whether it has already been warned about, if not). */
755 static int mips_cprestore_valid = 0;
756
757 /* This is the register which holds the stack frame, as set by the
758 .frame pseudo-op. This is needed to implement .cprestore. */
759 static int mips_frame_reg = SP;
760
761 /* Whether mips_frame_reg has been set in the current function
762 (or whether it has already been warned about, if not). */
763 static int mips_frame_reg_valid = 0;
764
765 /* To output NOP instructions correctly, we need to keep information
766 about the previous two instructions. */
767
768 /* Whether we are optimizing. The default value of 2 means to remove
769 unneeded NOPs and swap branch instructions when possible. A value
770 of 1 means to not swap branches. A value of 0 means to always
771 insert NOPs. */
772 static int mips_optimize = 2;
773
774 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
775 equivalent to seeing no -g option at all. */
776 static int mips_debug = 0;
777
778 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
779 #define MAX_VR4130_NOPS 4
780
781 /* The maximum number of NOPs needed to fill delay slots. */
782 #define MAX_DELAY_NOPS 2
783
784 /* The maximum number of NOPs needed for any purpose. */
785 #define MAX_NOPS 4
786
787 /* A list of previous instructions, with index 0 being the most recent.
788 We need to look back MAX_NOPS instructions when filling delay slots
789 or working around processor errata. We need to look back one
790 instruction further if we're thinking about using history[0] to
791 fill a branch delay slot. */
792 static struct mips_cl_insn history[1 + MAX_NOPS];
793
794 /* Arrays of operands for each instruction. */
795 #define MAX_OPERANDS 6
796 struct mips_operand_array {
797 const struct mips_operand *operand[MAX_OPERANDS];
798 };
799 static struct mips_operand_array *mips_operands;
800 static struct mips_operand_array *mips16_operands;
801 static struct mips_operand_array *micromips_operands;
802
803 /* Nop instructions used by emit_nop. */
804 static struct mips_cl_insn nop_insn;
805 static struct mips_cl_insn mips16_nop_insn;
806 static struct mips_cl_insn micromips_nop16_insn;
807 static struct mips_cl_insn micromips_nop32_insn;
808
809 /* The appropriate nop for the current mode. */
810 #define NOP_INSN (mips_opts.mips16 \
811 ? &mips16_nop_insn \
812 : (mips_opts.micromips \
813 ? (mips_opts.insn32 \
814 ? &micromips_nop32_insn \
815 : &micromips_nop16_insn) \
816 : &nop_insn))
817
818 /* The size of NOP_INSN in bytes. */
819 #define NOP_INSN_SIZE ((mips_opts.mips16 \
820 || (mips_opts.micromips && !mips_opts.insn32)) \
821 ? 2 : 4)
822
823 /* If this is set, it points to a frag holding nop instructions which
824 were inserted before the start of a noreorder section. If those
825 nops turn out to be unnecessary, the size of the frag can be
826 decreased. */
827 static fragS *prev_nop_frag;
828
829 /* The number of nop instructions we created in prev_nop_frag. */
830 static int prev_nop_frag_holds;
831
832 /* The number of nop instructions that we know we need in
833 prev_nop_frag. */
834 static int prev_nop_frag_required;
835
836 /* The number of instructions we've seen since prev_nop_frag. */
837 static int prev_nop_frag_since;
838
839 /* Relocations against symbols are sometimes done in two parts, with a HI
840 relocation and a LO relocation. Each relocation has only 16 bits of
841 space to store an addend. This means that in order for the linker to
842 handle carries correctly, it must be able to locate both the HI and
843 the LO relocation. This means that the relocations must appear in
844 order in the relocation table.
845
846 In order to implement this, we keep track of each unmatched HI
847 relocation. We then sort them so that they immediately precede the
848 corresponding LO relocation. */
849
850 struct mips_hi_fixup
851 {
852 /* Next HI fixup. */
853 struct mips_hi_fixup *next;
854 /* This fixup. */
855 fixS *fixp;
856 /* The section this fixup is in. */
857 segT seg;
858 };
859
860 /* The list of unmatched HI relocs. */
861
862 static struct mips_hi_fixup *mips_hi_fixup_list;
863
864 /* The frag containing the last explicit relocation operator.
865 Null if explicit relocations have not been used. */
866
867 static fragS *prev_reloc_op_frag;
868
869 /* Map mips16 register numbers to normal MIPS register numbers. */
870
871 static const unsigned int mips16_to_32_reg_map[] =
872 {
873 16, 17, 2, 3, 4, 5, 6, 7
874 };
875
876 /* Map microMIPS register numbers to normal MIPS register numbers. */
877
878 #define micromips_to_32_reg_d_map mips16_to_32_reg_map
879
880 /* The microMIPS registers with type h. */
881 static const unsigned int micromips_to_32_reg_h_map1[] =
882 {
883 5, 5, 6, 4, 4, 4, 4, 4
884 };
885 static const unsigned int micromips_to_32_reg_h_map2[] =
886 {
887 6, 7, 7, 21, 22, 5, 6, 7
888 };
889
890 /* The microMIPS registers with type m. */
891 static const unsigned int micromips_to_32_reg_m_map[] =
892 {
893 0, 17, 2, 3, 16, 18, 19, 20
894 };
895
896 #define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
897
898 /* Classifies the kind of instructions we're interested in when
899 implementing -mfix-vr4120. */
900 enum fix_vr4120_class
901 {
902 FIX_VR4120_MACC,
903 FIX_VR4120_DMACC,
904 FIX_VR4120_MULT,
905 FIX_VR4120_DMULT,
906 FIX_VR4120_DIV,
907 FIX_VR4120_MTHILO,
908 NUM_FIX_VR4120_CLASSES
909 };
910
911 /* ...likewise -mfix-loongson2f-jump. */
912 static bfd_boolean mips_fix_loongson2f_jump;
913
914 /* ...likewise -mfix-loongson2f-nop. */
915 static bfd_boolean mips_fix_loongson2f_nop;
916
917 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
918 static bfd_boolean mips_fix_loongson2f;
919
920 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
921 there must be at least one other instruction between an instruction
922 of type X and an instruction of type Y. */
923 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
924
925 /* True if -mfix-vr4120 is in force. */
926 static int mips_fix_vr4120;
927
928 /* ...likewise -mfix-vr4130. */
929 static int mips_fix_vr4130;
930
931 /* ...likewise -mfix-24k. */
932 static int mips_fix_24k;
933
934 /* ...likewise -mfix-rm7000 */
935 static int mips_fix_rm7000;
936
937 /* ...likewise -mfix-cn63xxp1 */
938 static bfd_boolean mips_fix_cn63xxp1;
939
940 /* We don't relax branches by default, since this causes us to expand
941 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
942 fail to compute the offset before expanding the macro to the most
943 efficient expansion. */
944
945 static int mips_relax_branch;
946
947 /* TRUE if checks are suppressed for invalid branches between ISA modes.
948 Needed for broken assembly produced by some GCC versions and some
949 sloppy code out there, where branches to data labels are present. */
950 static bfd_boolean mips_ignore_branch_isa;
951 \f
952 /* The expansion of many macros depends on the type of symbol that
953 they refer to. For example, when generating position-dependent code,
954 a macro that refers to a symbol may have two different expansions,
955 one which uses GP-relative addresses and one which uses absolute
956 addresses. When generating SVR4-style PIC, a macro may have
957 different expansions for local and global symbols.
958
959 We handle these situations by generating both sequences and putting
960 them in variant frags. In position-dependent code, the first sequence
961 will be the GP-relative one and the second sequence will be the
962 absolute one. In SVR4 PIC, the first sequence will be for global
963 symbols and the second will be for local symbols.
964
965 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
966 SECOND are the lengths of the two sequences in bytes. These fields
967 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
968 the subtype has the following flags:
969
970 RELAX_PIC
971 Set if generating PIC code.
972
973 RELAX_USE_SECOND
974 Set if it has been decided that we should use the second
975 sequence instead of the first.
976
977 RELAX_SECOND_LONGER
978 Set in the first variant frag if the macro's second implementation
979 is longer than its first. This refers to the macro as a whole,
980 not an individual relaxation.
981
982 RELAX_NOMACRO
983 Set in the first variant frag if the macro appeared in a .set nomacro
984 block and if one alternative requires a warning but the other does not.
985
986 RELAX_DELAY_SLOT
987 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
988 delay slot.
989
990 RELAX_DELAY_SLOT_16BIT
991 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
992 16-bit instruction.
993
994 RELAX_DELAY_SLOT_SIZE_FIRST
995 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
996 the macro is of the wrong size for the branch delay slot.
997
998 RELAX_DELAY_SLOT_SIZE_SECOND
999 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1000 the macro is of the wrong size for the branch delay slot.
1001
1002 The frag's "opcode" points to the first fixup for relaxable code.
1003
1004 Relaxable macros are generated using a sequence such as:
1005
1006 relax_start (SYMBOL);
1007 ... generate first expansion ...
1008 relax_switch ();
1009 ... generate second expansion ...
1010 relax_end ();
1011
1012 The code and fixups for the unwanted alternative are discarded
1013 by md_convert_frag. */
1014 #define RELAX_ENCODE(FIRST, SECOND, PIC) \
1015 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
1016
1017 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1018 #define RELAX_SECOND(X) ((X) & 0xff)
1019 #define RELAX_PIC(X) (((X) & 0x10000) != 0)
1020 #define RELAX_USE_SECOND 0x20000
1021 #define RELAX_SECOND_LONGER 0x40000
1022 #define RELAX_NOMACRO 0x80000
1023 #define RELAX_DELAY_SLOT 0x100000
1024 #define RELAX_DELAY_SLOT_16BIT 0x200000
1025 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1026 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
1027
1028 /* Branch without likely bit. If label is out of range, we turn:
1029
1030 beq reg1, reg2, label
1031 delay slot
1032
1033 into
1034
1035 bne reg1, reg2, 0f
1036 nop
1037 j label
1038 0: delay slot
1039
1040 with the following opcode replacements:
1041
1042 beq <-> bne
1043 blez <-> bgtz
1044 bltz <-> bgez
1045 bc1f <-> bc1t
1046
1047 bltzal <-> bgezal (with jal label instead of j label)
1048
1049 Even though keeping the delay slot instruction in the delay slot of
1050 the branch would be more efficient, it would be very tricky to do
1051 correctly, because we'd have to introduce a variable frag *after*
1052 the delay slot instruction, and expand that instead. Let's do it
1053 the easy way for now, even if the branch-not-taken case now costs
1054 one additional instruction. Out-of-range branches are not supposed
1055 to be common, anyway.
1056
1057 Branch likely. If label is out of range, we turn:
1058
1059 beql reg1, reg2, label
1060 delay slot (annulled if branch not taken)
1061
1062 into
1063
1064 beql reg1, reg2, 1f
1065 nop
1066 beql $0, $0, 2f
1067 nop
1068 1: j[al] label
1069 delay slot (executed only if branch taken)
1070 2:
1071
1072 It would be possible to generate a shorter sequence by losing the
1073 likely bit, generating something like:
1074
1075 bne reg1, reg2, 0f
1076 nop
1077 j[al] label
1078 delay slot (executed only if branch taken)
1079 0:
1080
1081 beql -> bne
1082 bnel -> beq
1083 blezl -> bgtz
1084 bgtzl -> blez
1085 bltzl -> bgez
1086 bgezl -> bltz
1087 bc1fl -> bc1t
1088 bc1tl -> bc1f
1089
1090 bltzall -> bgezal (with jal label instead of j label)
1091 bgezall -> bltzal (ditto)
1092
1093
1094 but it's not clear that it would actually improve performance. */
1095 #define RELAX_BRANCH_ENCODE(at, pic, \
1096 uncond, likely, link, toofar) \
1097 ((relax_substateT) \
1098 (0xc0000000 \
1099 | ((at) & 0x1f) \
1100 | ((pic) ? 0x20 : 0) \
1101 | ((toofar) ? 0x40 : 0) \
1102 | ((link) ? 0x80 : 0) \
1103 | ((likely) ? 0x100 : 0) \
1104 | ((uncond) ? 0x200 : 0)))
1105 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1106 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1107 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1108 #define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1109 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1110 #define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
1111 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1112
1113 /* For mips16 code, we use an entirely different form of relaxation.
1114 mips16 supports two versions of most instructions which take
1115 immediate values: a small one which takes some small value, and a
1116 larger one which takes a 16 bit value. Since branches also follow
1117 this pattern, relaxing these values is required.
1118
1119 We can assemble both mips16 and normal MIPS code in a single
1120 object. Therefore, we need to support this type of relaxation at
1121 the same time that we support the relaxation described above. We
1122 use the high bit of the subtype field to distinguish these cases.
1123
1124 The information we store for this type of relaxation is the
1125 argument code found in the opcode file for this relocation, whether
1126 the user explicitly requested a small or extended form, and whether
1127 the relocation is in a jump or jal delay slot. That tells us the
1128 size of the value, and how it should be stored. We also store
1129 whether the fragment is considered to be extended or not. We also
1130 store whether this is known to be a branch to a different section,
1131 whether we have tried to relax this frag yet, and whether we have
1132 ever extended a PC relative fragment because of a shift count. */
1133 #define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
1134 small, ext, \
1135 dslot, jal_dslot) \
1136 (0x80000000 \
1137 | ((type) & 0xff) \
1138 | ((e2) ? 0x100 : 0) \
1139 | ((pic) ? 0x200 : 0) \
1140 | ((sym32) ? 0x400 : 0) \
1141 | ((nomacro) ? 0x800 : 0) \
1142 | ((small) ? 0x1000 : 0) \
1143 | ((ext) ? 0x2000 : 0) \
1144 | ((dslot) ? 0x4000 : 0) \
1145 | ((jal_dslot) ? 0x8000 : 0))
1146
1147 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1148 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1149 #define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1150 #define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1151 #define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1152 #define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1153 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1154 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1155 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1156 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1157
1158 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1159 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1160 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1161 #define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1162 #define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1163 #define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1164 #define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1165 #define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1166 #define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
1167
1168 /* For microMIPS code, we use relaxation similar to one we use for
1169 MIPS16 code. Some instructions that take immediate values support
1170 two encodings: a small one which takes some small value, and a
1171 larger one which takes a 16 bit value. As some branches also follow
1172 this pattern, relaxing these values is required.
1173
1174 We can assemble both microMIPS and normal MIPS code in a single
1175 object. Therefore, we need to support this type of relaxation at
1176 the same time that we support the relaxation described above. We
1177 use one of the high bits of the subtype field to distinguish these
1178 cases.
1179
1180 The information we store for this type of relaxation is the argument
1181 code found in the opcode file for this relocation, the register
1182 selected as the assembler temporary, whether in the 32-bit
1183 instruction mode, whether the branch is unconditional, whether it is
1184 compact, whether there is no delay-slot instruction available to fill
1185 in, whether it stores the link address implicitly in $ra, whether
1186 relaxation of out-of-range 32-bit branches to a sequence of
1187 instructions is enabled, and whether the displacement of a branch is
1188 too large to fit as an immediate argument of a 16-bit and a 32-bit
1189 branch, respectively. */
1190 #define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
1191 uncond, compact, link, nods, \
1192 relax32, toofar16, toofar32) \
1193 (0x40000000 \
1194 | ((type) & 0xff) \
1195 | (((at) & 0x1f) << 8) \
1196 | ((insn32) ? 0x2000 : 0) \
1197 | ((pic) ? 0x4000 : 0) \
1198 | ((uncond) ? 0x8000 : 0) \
1199 | ((compact) ? 0x10000 : 0) \
1200 | ((link) ? 0x20000 : 0) \
1201 | ((nods) ? 0x40000 : 0) \
1202 | ((relax32) ? 0x80000 : 0) \
1203 | ((toofar16) ? 0x100000 : 0) \
1204 | ((toofar32) ? 0x200000 : 0))
1205 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1206 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1207 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1208 #define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
1209 #define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1210 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1211 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1212 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1213 #define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1214 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1215
1216 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1217 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1218 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1219 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1220 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1221 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
1222
1223 /* Sign-extend 16-bit value X. */
1224 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1225
1226 /* Is the given value a sign-extended 32-bit value? */
1227 #define IS_SEXT_32BIT_NUM(x) \
1228 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1229 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1230
1231 /* Is the given value a sign-extended 16-bit value? */
1232 #define IS_SEXT_16BIT_NUM(x) \
1233 (((x) &~ (offsetT) 0x7fff) == 0 \
1234 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1235
1236 /* Is the given value a sign-extended 12-bit value? */
1237 #define IS_SEXT_12BIT_NUM(x) \
1238 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1239
1240 /* Is the given value a sign-extended 9-bit value? */
1241 #define IS_SEXT_9BIT_NUM(x) \
1242 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1243
1244 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
1245 #define IS_ZEXT_32BIT_NUM(x) \
1246 (((x) &~ (offsetT) 0xffffffff) == 0 \
1247 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1248
1249 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1250 SHIFT places. */
1251 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1252 (((STRUCT) >> (SHIFT)) & (MASK))
1253
1254 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
1255 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1256 (!(MICROMIPS) \
1257 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1258 : EXTRACT_BITS ((INSN).insn_opcode, \
1259 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1260 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1261 EXTRACT_BITS ((INSN).insn_opcode, \
1262 MIPS16OP_MASK_##FIELD, \
1263 MIPS16OP_SH_##FIELD)
1264
1265 /* The MIPS16 EXTEND opcode, shifted left 16 places. */
1266 #define MIPS16_EXTEND (0xf000U << 16)
1267 \f
1268 /* Whether or not we are emitting a branch-likely macro. */
1269 static bfd_boolean emit_branch_likely_macro = FALSE;
1270
1271 /* Global variables used when generating relaxable macros. See the
1272 comment above RELAX_ENCODE for more details about how relaxation
1273 is used. */
1274 static struct {
1275 /* 0 if we're not emitting a relaxable macro.
1276 1 if we're emitting the first of the two relaxation alternatives.
1277 2 if we're emitting the second alternative. */
1278 int sequence;
1279
1280 /* The first relaxable fixup in the current frag. (In other words,
1281 the first fixup that refers to relaxable code.) */
1282 fixS *first_fixup;
1283
1284 /* sizes[0] says how many bytes of the first alternative are stored in
1285 the current frag. Likewise sizes[1] for the second alternative. */
1286 unsigned int sizes[2];
1287
1288 /* The symbol on which the choice of sequence depends. */
1289 symbolS *symbol;
1290 } mips_relax;
1291 \f
1292 /* Global variables used to decide whether a macro needs a warning. */
1293 static struct {
1294 /* True if the macro is in a branch delay slot. */
1295 bfd_boolean delay_slot_p;
1296
1297 /* Set to the length in bytes required if the macro is in a delay slot
1298 that requires a specific length of instruction, otherwise zero. */
1299 unsigned int delay_slot_length;
1300
1301 /* For relaxable macros, sizes[0] is the length of the first alternative
1302 in bytes and sizes[1] is the length of the second alternative.
1303 For non-relaxable macros, both elements give the length of the
1304 macro in bytes. */
1305 unsigned int sizes[2];
1306
1307 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1308 instruction of the first alternative in bytes and first_insn_sizes[1]
1309 is the length of the first instruction of the second alternative.
1310 For non-relaxable macros, both elements give the length of the first
1311 instruction in bytes.
1312
1313 Set to zero if we haven't yet seen the first instruction. */
1314 unsigned int first_insn_sizes[2];
1315
1316 /* For relaxable macros, insns[0] is the number of instructions for the
1317 first alternative and insns[1] is the number of instructions for the
1318 second alternative.
1319
1320 For non-relaxable macros, both elements give the number of
1321 instructions for the macro. */
1322 unsigned int insns[2];
1323
1324 /* The first variant frag for this macro. */
1325 fragS *first_frag;
1326 } mips_macro_warning;
1327 \f
1328 /* Prototypes for static functions. */
1329
1330 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1331
1332 static void append_insn
1333 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1334 bfd_boolean expansionp);
1335 static void mips_no_prev_insn (void);
1336 static void macro_build (expressionS *, const char *, const char *, ...);
1337 static void mips16_macro_build
1338 (expressionS *, const char *, const char *, va_list *);
1339 static void load_register (int, expressionS *, int);
1340 static void macro_start (void);
1341 static void macro_end (void);
1342 static void macro (struct mips_cl_insn *ip, char *str);
1343 static void mips16_macro (struct mips_cl_insn * ip);
1344 static void mips_ip (char *str, struct mips_cl_insn * ip);
1345 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1346 static unsigned long mips16_immed_extend (offsetT, unsigned int);
1347 static void mips16_immed
1348 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1349 unsigned int, unsigned long *);
1350 static size_t my_getSmallExpression
1351 (expressionS *, bfd_reloc_code_real_type *, char *);
1352 static void my_getExpression (expressionS *, char *);
1353 static void s_align (int);
1354 static void s_change_sec (int);
1355 static void s_change_section (int);
1356 static void s_cons (int);
1357 static void s_float_cons (int);
1358 static void s_mips_globl (int);
1359 static void s_option (int);
1360 static void s_mipsset (int);
1361 static void s_abicalls (int);
1362 static void s_cpload (int);
1363 static void s_cpsetup (int);
1364 static void s_cplocal (int);
1365 static void s_cprestore (int);
1366 static void s_cpreturn (int);
1367 static void s_dtprelword (int);
1368 static void s_dtpreldword (int);
1369 static void s_tprelword (int);
1370 static void s_tpreldword (int);
1371 static void s_gpvalue (int);
1372 static void s_gpword (int);
1373 static void s_gpdword (int);
1374 static void s_ehword (int);
1375 static void s_cpadd (int);
1376 static void s_insn (int);
1377 static void s_nan (int);
1378 static void s_module (int);
1379 static void s_mips_ent (int);
1380 static void s_mips_end (int);
1381 static void s_mips_frame (int);
1382 static void s_mips_mask (int reg_type);
1383 static void s_mips_stab (int);
1384 static void s_mips_weakext (int);
1385 static void s_mips_file (int);
1386 static void s_mips_loc (int);
1387 static bfd_boolean pic_need_relax (symbolS *);
1388 static int relaxed_branch_length (fragS *, asection *, int);
1389 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1390 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1391 static void file_mips_check_options (void);
1392
1393 /* Table and functions used to map between CPU/ISA names, and
1394 ISA levels, and CPU numbers. */
1395
1396 struct mips_cpu_info
1397 {
1398 const char *name; /* CPU or ISA name. */
1399 int flags; /* MIPS_CPU_* flags. */
1400 int ase; /* Set of ASEs implemented by the CPU. */
1401 int isa; /* ISA level. */
1402 int cpu; /* CPU number (default CPU if ISA). */
1403 };
1404
1405 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1406
1407 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1408 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1409 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1410 \f
1411 /* Command-line options. */
1412 const char *md_shortopts = "O::g::G:";
1413
1414 enum options
1415 {
1416 OPTION_MARCH = OPTION_MD_BASE,
1417 OPTION_MTUNE,
1418 OPTION_MIPS1,
1419 OPTION_MIPS2,
1420 OPTION_MIPS3,
1421 OPTION_MIPS4,
1422 OPTION_MIPS5,
1423 OPTION_MIPS32,
1424 OPTION_MIPS64,
1425 OPTION_MIPS32R2,
1426 OPTION_MIPS32R3,
1427 OPTION_MIPS32R5,
1428 OPTION_MIPS32R6,
1429 OPTION_MIPS64R2,
1430 OPTION_MIPS64R3,
1431 OPTION_MIPS64R5,
1432 OPTION_MIPS64R6,
1433 OPTION_MIPS16,
1434 OPTION_NO_MIPS16,
1435 OPTION_MIPS3D,
1436 OPTION_NO_MIPS3D,
1437 OPTION_MDMX,
1438 OPTION_NO_MDMX,
1439 OPTION_DSP,
1440 OPTION_NO_DSP,
1441 OPTION_MT,
1442 OPTION_NO_MT,
1443 OPTION_VIRT,
1444 OPTION_NO_VIRT,
1445 OPTION_MSA,
1446 OPTION_NO_MSA,
1447 OPTION_SMARTMIPS,
1448 OPTION_NO_SMARTMIPS,
1449 OPTION_DSPR2,
1450 OPTION_NO_DSPR2,
1451 OPTION_DSPR3,
1452 OPTION_NO_DSPR3,
1453 OPTION_EVA,
1454 OPTION_NO_EVA,
1455 OPTION_XPA,
1456 OPTION_NO_XPA,
1457 OPTION_MICROMIPS,
1458 OPTION_NO_MICROMIPS,
1459 OPTION_MCU,
1460 OPTION_NO_MCU,
1461 OPTION_MIPS16E2,
1462 OPTION_NO_MIPS16E2,
1463 OPTION_CRC,
1464 OPTION_NO_CRC,
1465 OPTION_M4650,
1466 OPTION_NO_M4650,
1467 OPTION_M4010,
1468 OPTION_NO_M4010,
1469 OPTION_M4100,
1470 OPTION_NO_M4100,
1471 OPTION_M3900,
1472 OPTION_NO_M3900,
1473 OPTION_M7000_HILO_FIX,
1474 OPTION_MNO_7000_HILO_FIX,
1475 OPTION_FIX_24K,
1476 OPTION_NO_FIX_24K,
1477 OPTION_FIX_RM7000,
1478 OPTION_NO_FIX_RM7000,
1479 OPTION_FIX_LOONGSON2F_JUMP,
1480 OPTION_NO_FIX_LOONGSON2F_JUMP,
1481 OPTION_FIX_LOONGSON2F_NOP,
1482 OPTION_NO_FIX_LOONGSON2F_NOP,
1483 OPTION_FIX_VR4120,
1484 OPTION_NO_FIX_VR4120,
1485 OPTION_FIX_VR4130,
1486 OPTION_NO_FIX_VR4130,
1487 OPTION_FIX_CN63XXP1,
1488 OPTION_NO_FIX_CN63XXP1,
1489 OPTION_TRAP,
1490 OPTION_BREAK,
1491 OPTION_EB,
1492 OPTION_EL,
1493 OPTION_FP32,
1494 OPTION_GP32,
1495 OPTION_CONSTRUCT_FLOATS,
1496 OPTION_NO_CONSTRUCT_FLOATS,
1497 OPTION_FP64,
1498 OPTION_FPXX,
1499 OPTION_GP64,
1500 OPTION_RELAX_BRANCH,
1501 OPTION_NO_RELAX_BRANCH,
1502 OPTION_IGNORE_BRANCH_ISA,
1503 OPTION_NO_IGNORE_BRANCH_ISA,
1504 OPTION_INSN32,
1505 OPTION_NO_INSN32,
1506 OPTION_MSHARED,
1507 OPTION_MNO_SHARED,
1508 OPTION_MSYM32,
1509 OPTION_MNO_SYM32,
1510 OPTION_SOFT_FLOAT,
1511 OPTION_HARD_FLOAT,
1512 OPTION_SINGLE_FLOAT,
1513 OPTION_DOUBLE_FLOAT,
1514 OPTION_32,
1515 OPTION_CALL_SHARED,
1516 OPTION_CALL_NONPIC,
1517 OPTION_NON_SHARED,
1518 OPTION_XGOT,
1519 OPTION_MABI,
1520 OPTION_N32,
1521 OPTION_64,
1522 OPTION_MDEBUG,
1523 OPTION_NO_MDEBUG,
1524 OPTION_PDR,
1525 OPTION_NO_PDR,
1526 OPTION_MVXWORKS_PIC,
1527 OPTION_NAN,
1528 OPTION_ODD_SPREG,
1529 OPTION_NO_ODD_SPREG,
1530 OPTION_GINV,
1531 OPTION_NO_GINV,
1532 OPTION_LOONGSON_MMI,
1533 OPTION_NO_LOONGSON_MMI,
1534 OPTION_END_OF_ENUM
1535 };
1536
1537 struct option md_longopts[] =
1538 {
1539 /* Options which specify architecture. */
1540 {"march", required_argument, NULL, OPTION_MARCH},
1541 {"mtune", required_argument, NULL, OPTION_MTUNE},
1542 {"mips0", no_argument, NULL, OPTION_MIPS1},
1543 {"mips1", no_argument, NULL, OPTION_MIPS1},
1544 {"mips2", no_argument, NULL, OPTION_MIPS2},
1545 {"mips3", no_argument, NULL, OPTION_MIPS3},
1546 {"mips4", no_argument, NULL, OPTION_MIPS4},
1547 {"mips5", no_argument, NULL, OPTION_MIPS5},
1548 {"mips32", no_argument, NULL, OPTION_MIPS32},
1549 {"mips64", no_argument, NULL, OPTION_MIPS64},
1550 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1551 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1552 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
1553 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
1554 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1555 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1556 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
1557 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
1558
1559 /* Options which specify Application Specific Extensions (ASEs). */
1560 {"mips16", no_argument, NULL, OPTION_MIPS16},
1561 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1562 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1563 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1564 {"mdmx", no_argument, NULL, OPTION_MDMX},
1565 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1566 {"mdsp", no_argument, NULL, OPTION_DSP},
1567 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1568 {"mmt", no_argument, NULL, OPTION_MT},
1569 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1570 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1571 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1572 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1573 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1574 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1575 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
1576 {"meva", no_argument, NULL, OPTION_EVA},
1577 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1578 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1579 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1580 {"mmcu", no_argument, NULL, OPTION_MCU},
1581 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1582 {"mvirt", no_argument, NULL, OPTION_VIRT},
1583 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1584 {"mmsa", no_argument, NULL, OPTION_MSA},
1585 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
1586 {"mxpa", no_argument, NULL, OPTION_XPA},
1587 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
1588 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1589 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
1590 {"mcrc", no_argument, NULL, OPTION_CRC},
1591 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
1592 {"mginv", no_argument, NULL, OPTION_GINV},
1593 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
1594 {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1595 {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
1596
1597 /* Old-style architecture options. Don't add more of these. */
1598 {"m4650", no_argument, NULL, OPTION_M4650},
1599 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1600 {"m4010", no_argument, NULL, OPTION_M4010},
1601 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1602 {"m4100", no_argument, NULL, OPTION_M4100},
1603 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1604 {"m3900", no_argument, NULL, OPTION_M3900},
1605 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1606
1607 /* Options which enable bug fixes. */
1608 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1609 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1610 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1611 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1612 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1613 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1614 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1615 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1616 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1617 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1618 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1619 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1620 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1621 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1622 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
1623 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1624 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1625
1626 /* Miscellaneous options. */
1627 {"trap", no_argument, NULL, OPTION_TRAP},
1628 {"no-break", no_argument, NULL, OPTION_TRAP},
1629 {"break", no_argument, NULL, OPTION_BREAK},
1630 {"no-trap", no_argument, NULL, OPTION_BREAK},
1631 {"EB", no_argument, NULL, OPTION_EB},
1632 {"EL", no_argument, NULL, OPTION_EL},
1633 {"mfp32", no_argument, NULL, OPTION_FP32},
1634 {"mgp32", no_argument, NULL, OPTION_GP32},
1635 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1636 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1637 {"mfp64", no_argument, NULL, OPTION_FP64},
1638 {"mfpxx", no_argument, NULL, OPTION_FPXX},
1639 {"mgp64", no_argument, NULL, OPTION_GP64},
1640 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1641 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1642 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1643 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
1644 {"minsn32", no_argument, NULL, OPTION_INSN32},
1645 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1646 {"mshared", no_argument, NULL, OPTION_MSHARED},
1647 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1648 {"msym32", no_argument, NULL, OPTION_MSYM32},
1649 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1650 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1651 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1652 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1653 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1654 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1655 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
1656
1657 /* Strictly speaking this next option is ELF specific,
1658 but we allow it for other ports as well in order to
1659 make testing easier. */
1660 {"32", no_argument, NULL, OPTION_32},
1661
1662 /* ELF-specific options. */
1663 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1664 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1665 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1666 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1667 {"xgot", no_argument, NULL, OPTION_XGOT},
1668 {"mabi", required_argument, NULL, OPTION_MABI},
1669 {"n32", no_argument, NULL, OPTION_N32},
1670 {"64", no_argument, NULL, OPTION_64},
1671 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1672 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1673 {"mpdr", no_argument, NULL, OPTION_PDR},
1674 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1675 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1676 {"mnan", required_argument, NULL, OPTION_NAN},
1677
1678 {NULL, no_argument, NULL, 0}
1679 };
1680 size_t md_longopts_size = sizeof (md_longopts);
1681 \f
1682 /* Information about either an Application Specific Extension or an
1683 optional architecture feature that, for simplicity, we treat in the
1684 same way as an ASE. */
1685 struct mips_ase
1686 {
1687 /* The name of the ASE, used in both the command-line and .set options. */
1688 const char *name;
1689
1690 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1691 and 64-bit architectures, the flags here refer to the subset that
1692 is available on both. */
1693 unsigned int flags;
1694
1695 /* The ASE_* flag used for instructions that are available on 64-bit
1696 architectures but that are not included in FLAGS. */
1697 unsigned int flags64;
1698
1699 /* The command-line options that turn the ASE on and off. */
1700 int option_on;
1701 int option_off;
1702
1703 /* The minimum required architecture revisions for MIPS32, MIPS64,
1704 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1705 int mips32_rev;
1706 int mips64_rev;
1707 int micromips32_rev;
1708 int micromips64_rev;
1709
1710 /* The architecture where the ASE was removed or -1 if the extension has not
1711 been removed. */
1712 int rem_rev;
1713 };
1714
1715 /* A table of all supported ASEs. */
1716 static const struct mips_ase mips_ases[] = {
1717 { "dsp", ASE_DSP, ASE_DSP64,
1718 OPTION_DSP, OPTION_NO_DSP,
1719 2, 2, 2, 2,
1720 -1 },
1721
1722 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1723 OPTION_DSPR2, OPTION_NO_DSPR2,
1724 2, 2, 2, 2,
1725 -1 },
1726
1727 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1728 OPTION_DSPR3, OPTION_NO_DSPR3,
1729 6, 6, -1, -1,
1730 -1 },
1731
1732 { "eva", ASE_EVA, 0,
1733 OPTION_EVA, OPTION_NO_EVA,
1734 2, 2, 2, 2,
1735 -1 },
1736
1737 { "mcu", ASE_MCU, 0,
1738 OPTION_MCU, OPTION_NO_MCU,
1739 2, 2, 2, 2,
1740 -1 },
1741
1742 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1743 { "mdmx", ASE_MDMX, 0,
1744 OPTION_MDMX, OPTION_NO_MDMX,
1745 -1, 1, -1, -1,
1746 6 },
1747
1748 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1749 { "mips3d", ASE_MIPS3D, 0,
1750 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1751 2, 1, -1, -1,
1752 6 },
1753
1754 { "mt", ASE_MT, 0,
1755 OPTION_MT, OPTION_NO_MT,
1756 2, 2, -1, -1,
1757 -1 },
1758
1759 { "smartmips", ASE_SMARTMIPS, 0,
1760 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1761 1, -1, -1, -1,
1762 6 },
1763
1764 { "virt", ASE_VIRT, ASE_VIRT64,
1765 OPTION_VIRT, OPTION_NO_VIRT,
1766 2, 2, 2, 2,
1767 -1 },
1768
1769 { "msa", ASE_MSA, ASE_MSA64,
1770 OPTION_MSA, OPTION_NO_MSA,
1771 2, 2, 2, 2,
1772 -1 },
1773
1774 { "xpa", ASE_XPA, 0,
1775 OPTION_XPA, OPTION_NO_XPA,
1776 2, 2, 2, 2,
1777 -1 },
1778
1779 { "mips16e2", ASE_MIPS16E2, 0,
1780 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1781 2, 2, -1, -1,
1782 6 },
1783
1784 { "crc", ASE_CRC, ASE_CRC64,
1785 OPTION_CRC, OPTION_NO_CRC,
1786 6, 6, -1, -1,
1787 -1 },
1788
1789 { "ginv", ASE_GINV, 0,
1790 OPTION_GINV, OPTION_NO_GINV,
1791 6, 6, 6, 6,
1792 -1 },
1793
1794 { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1795 OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1796 0, 0, -1, -1,
1797 -1 },
1798 };
1799
1800 /* The set of ASEs that require -mfp64. */
1801 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
1802
1803 /* Groups of ASE_* flags that represent different revisions of an ASE. */
1804 static const unsigned int mips_ase_groups[] = {
1805 ASE_DSP | ASE_DSPR2 | ASE_DSPR3
1806 };
1807 \f
1808 /* Pseudo-op table.
1809
1810 The following pseudo-ops from the Kane and Heinrich MIPS book
1811 should be defined here, but are currently unsupported: .alias,
1812 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1813
1814 The following pseudo-ops from the Kane and Heinrich MIPS book are
1815 specific to the type of debugging information being generated, and
1816 should be defined by the object format: .aent, .begin, .bend,
1817 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1818 .vreg.
1819
1820 The following pseudo-ops from the Kane and Heinrich MIPS book are
1821 not MIPS CPU specific, but are also not specific to the object file
1822 format. This file is probably the best place to define them, but
1823 they are not currently supported: .asm0, .endr, .lab, .struct. */
1824
1825 static const pseudo_typeS mips_pseudo_table[] =
1826 {
1827 /* MIPS specific pseudo-ops. */
1828 {"option", s_option, 0},
1829 {"set", s_mipsset, 0},
1830 {"rdata", s_change_sec, 'r'},
1831 {"sdata", s_change_sec, 's'},
1832 {"livereg", s_ignore, 0},
1833 {"abicalls", s_abicalls, 0},
1834 {"cpload", s_cpload, 0},
1835 {"cpsetup", s_cpsetup, 0},
1836 {"cplocal", s_cplocal, 0},
1837 {"cprestore", s_cprestore, 0},
1838 {"cpreturn", s_cpreturn, 0},
1839 {"dtprelword", s_dtprelword, 0},
1840 {"dtpreldword", s_dtpreldword, 0},
1841 {"tprelword", s_tprelword, 0},
1842 {"tpreldword", s_tpreldword, 0},
1843 {"gpvalue", s_gpvalue, 0},
1844 {"gpword", s_gpword, 0},
1845 {"gpdword", s_gpdword, 0},
1846 {"ehword", s_ehword, 0},
1847 {"cpadd", s_cpadd, 0},
1848 {"insn", s_insn, 0},
1849 {"nan", s_nan, 0},
1850 {"module", s_module, 0},
1851
1852 /* Relatively generic pseudo-ops that happen to be used on MIPS
1853 chips. */
1854 {"asciiz", stringer, 8 + 1},
1855 {"bss", s_change_sec, 'b'},
1856 {"err", s_err, 0},
1857 {"half", s_cons, 1},
1858 {"dword", s_cons, 3},
1859 {"weakext", s_mips_weakext, 0},
1860 {"origin", s_org, 0},
1861 {"repeat", s_rept, 0},
1862
1863 /* For MIPS this is non-standard, but we define it for consistency. */
1864 {"sbss", s_change_sec, 'B'},
1865
1866 /* These pseudo-ops are defined in read.c, but must be overridden
1867 here for one reason or another. */
1868 {"align", s_align, 0},
1869 {"byte", s_cons, 0},
1870 {"data", s_change_sec, 'd'},
1871 {"double", s_float_cons, 'd'},
1872 {"float", s_float_cons, 'f'},
1873 {"globl", s_mips_globl, 0},
1874 {"global", s_mips_globl, 0},
1875 {"hword", s_cons, 1},
1876 {"int", s_cons, 2},
1877 {"long", s_cons, 2},
1878 {"octa", s_cons, 4},
1879 {"quad", s_cons, 3},
1880 {"section", s_change_section, 0},
1881 {"short", s_cons, 1},
1882 {"single", s_float_cons, 'f'},
1883 {"stabd", s_mips_stab, 'd'},
1884 {"stabn", s_mips_stab, 'n'},
1885 {"stabs", s_mips_stab, 's'},
1886 {"text", s_change_sec, 't'},
1887 {"word", s_cons, 2},
1888
1889 { "extern", ecoff_directive_extern, 0},
1890
1891 { NULL, NULL, 0 },
1892 };
1893
1894 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1895 {
1896 /* These pseudo-ops should be defined by the object file format.
1897 However, a.out doesn't support them, so we have versions here. */
1898 {"aent", s_mips_ent, 1},
1899 {"bgnb", s_ignore, 0},
1900 {"end", s_mips_end, 0},
1901 {"endb", s_ignore, 0},
1902 {"ent", s_mips_ent, 0},
1903 {"file", s_mips_file, 0},
1904 {"fmask", s_mips_mask, 'F'},
1905 {"frame", s_mips_frame, 0},
1906 {"loc", s_mips_loc, 0},
1907 {"mask", s_mips_mask, 'R'},
1908 {"verstamp", s_ignore, 0},
1909 { NULL, NULL, 0 },
1910 };
1911
1912 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1913 purpose of the `.dc.a' internal pseudo-op. */
1914
1915 int
1916 mips_address_bytes (void)
1917 {
1918 file_mips_check_options ();
1919 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1920 }
1921
1922 extern void pop_insert (const pseudo_typeS *);
1923
1924 void
1925 mips_pop_insert (void)
1926 {
1927 pop_insert (mips_pseudo_table);
1928 if (! ECOFF_DEBUGGING)
1929 pop_insert (mips_nonecoff_pseudo_table);
1930 }
1931 \f
1932 /* Symbols labelling the current insn. */
1933
1934 struct insn_label_list
1935 {
1936 struct insn_label_list *next;
1937 symbolS *label;
1938 };
1939
1940 static struct insn_label_list *free_insn_labels;
1941 #define label_list tc_segment_info_data.labels
1942
1943 static void mips_clear_insn_labels (void);
1944 static void mips_mark_labels (void);
1945 static void mips_compressed_mark_labels (void);
1946
1947 static inline void
1948 mips_clear_insn_labels (void)
1949 {
1950 struct insn_label_list **pl;
1951 segment_info_type *si;
1952
1953 if (now_seg)
1954 {
1955 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1956 ;
1957
1958 si = seg_info (now_seg);
1959 *pl = si->label_list;
1960 si->label_list = NULL;
1961 }
1962 }
1963
1964 /* Mark instruction labels in MIPS16/microMIPS mode. */
1965
1966 static inline void
1967 mips_mark_labels (void)
1968 {
1969 if (HAVE_CODE_COMPRESSION)
1970 mips_compressed_mark_labels ();
1971 }
1972 \f
1973 static char *expr_end;
1974
1975 /* An expression in a macro instruction. This is set by mips_ip and
1976 mips16_ip and when populated is always an O_constant. */
1977
1978 static expressionS imm_expr;
1979
1980 /* The relocatable field in an instruction and the relocs associated
1981 with it. These variables are used for instructions like LUI and
1982 JAL as well as true offsets. They are also used for address
1983 operands in macros. */
1984
1985 static expressionS offset_expr;
1986 static bfd_reloc_code_real_type offset_reloc[3]
1987 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1988
1989 /* This is set to the resulting size of the instruction to be produced
1990 by mips16_ip if an explicit extension is used or by mips_ip if an
1991 explicit size is supplied. */
1992
1993 static unsigned int forced_insn_length;
1994
1995 /* True if we are assembling an instruction. All dot symbols defined during
1996 this time should be treated as code labels. */
1997
1998 static bfd_boolean mips_assembling_insn;
1999
2000 /* The pdr segment for per procedure frame/regmask info. Not used for
2001 ECOFF debugging. */
2002
2003 static segT pdr_seg;
2004
2005 /* The default target format to use. */
2006
2007 #if defined (TE_FreeBSD)
2008 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2009 #elif defined (TE_TMIPS)
2010 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2011 #else
2012 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2013 #endif
2014
2015 const char *
2016 mips_target_format (void)
2017 {
2018 switch (OUTPUT_FLAVOR)
2019 {
2020 case bfd_target_elf_flavour:
2021 #ifdef TE_VXWORKS
2022 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2023 return (target_big_endian
2024 ? "elf32-bigmips-vxworks"
2025 : "elf32-littlemips-vxworks");
2026 #endif
2027 return (target_big_endian
2028 ? (HAVE_64BIT_OBJECTS
2029 ? ELF_TARGET ("elf64-", "big")
2030 : (HAVE_NEWABI
2031 ? ELF_TARGET ("elf32-n", "big")
2032 : ELF_TARGET ("elf32-", "big")))
2033 : (HAVE_64BIT_OBJECTS
2034 ? ELF_TARGET ("elf64-", "little")
2035 : (HAVE_NEWABI
2036 ? ELF_TARGET ("elf32-n", "little")
2037 : ELF_TARGET ("elf32-", "little"))));
2038 default:
2039 abort ();
2040 return NULL;
2041 }
2042 }
2043
2044 /* Return the ISA revision that is currently in use, or 0 if we are
2045 generating code for MIPS V or below. */
2046
2047 static int
2048 mips_isa_rev (void)
2049 {
2050 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2051 return 2;
2052
2053 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2054 return 3;
2055
2056 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2057 return 5;
2058
2059 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2060 return 6;
2061
2062 /* microMIPS implies revision 2 or above. */
2063 if (mips_opts.micromips)
2064 return 2;
2065
2066 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2067 return 1;
2068
2069 return 0;
2070 }
2071
2072 /* Return the mask of all ASEs that are revisions of those in FLAGS. */
2073
2074 static unsigned int
2075 mips_ase_mask (unsigned int flags)
2076 {
2077 unsigned int i;
2078
2079 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2080 if (flags & mips_ase_groups[i])
2081 flags |= mips_ase_groups[i];
2082 return flags;
2083 }
2084
2085 /* Check whether the current ISA supports ASE. Issue a warning if
2086 appropriate. */
2087
2088 static void
2089 mips_check_isa_supports_ase (const struct mips_ase *ase)
2090 {
2091 const char *base;
2092 int min_rev, size;
2093 static unsigned int warned_isa;
2094 static unsigned int warned_fp32;
2095
2096 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2097 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2098 else
2099 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2100 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2101 && (warned_isa & ase->flags) != ase->flags)
2102 {
2103 warned_isa |= ase->flags;
2104 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2105 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2106 if (min_rev < 0)
2107 as_warn (_("the %d-bit %s architecture does not support the"
2108 " `%s' extension"), size, base, ase->name);
2109 else
2110 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
2111 ase->name, base, size, min_rev);
2112 }
2113 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2114 && (warned_isa & ase->flags) != ase->flags)
2115 {
2116 warned_isa |= ase->flags;
2117 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2118 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2119 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2120 ase->name, base, size, ase->rem_rev);
2121 }
2122
2123 if ((ase->flags & FP64_ASES)
2124 && mips_opts.fp != 64
2125 && (warned_fp32 & ase->flags) != ase->flags)
2126 {
2127 warned_fp32 |= ase->flags;
2128 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
2129 }
2130 }
2131
2132 /* Check all enabled ASEs to see whether they are supported by the
2133 chosen architecture. */
2134
2135 static void
2136 mips_check_isa_supports_ases (void)
2137 {
2138 unsigned int i, mask;
2139
2140 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2141 {
2142 mask = mips_ase_mask (mips_ases[i].flags);
2143 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2144 mips_check_isa_supports_ase (&mips_ases[i]);
2145 }
2146 }
2147
2148 /* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2149 that were affected. */
2150
2151 static unsigned int
2152 mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2153 bfd_boolean enabled_p)
2154 {
2155 unsigned int mask;
2156
2157 mask = mips_ase_mask (ase->flags);
2158 opts->ase &= ~mask;
2159
2160 /* Clear combination ASE flags, which need to be recalculated based on
2161 updated regular ASE settings. */
2162 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT);
2163
2164 if (enabled_p)
2165 opts->ase |= ase->flags;
2166
2167 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2168 instructions which are only valid when both ASEs are enabled.
2169 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2170 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2171 {
2172 opts->ase |= ASE_XPA_VIRT;
2173 mask |= ASE_XPA_VIRT;
2174 }
2175 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2176 {
2177 opts->ase |= ASE_MIPS16E2_MT;
2178 mask |= ASE_MIPS16E2_MT;
2179 }
2180
2181 return mask;
2182 }
2183
2184 /* Return the ASE called NAME, or null if none. */
2185
2186 static const struct mips_ase *
2187 mips_lookup_ase (const char *name)
2188 {
2189 unsigned int i;
2190
2191 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2192 if (strcmp (name, mips_ases[i].name) == 0)
2193 return &mips_ases[i];
2194 return NULL;
2195 }
2196
2197 /* Return the length of a microMIPS instruction in bytes. If bits of
2198 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2199 otherwise it is a 32-bit instruction. */
2200
2201 static inline unsigned int
2202 micromips_insn_length (const struct mips_opcode *mo)
2203 {
2204 return mips_opcode_32bit_p (mo) ? 4 : 2;
2205 }
2206
2207 /* Return the length of MIPS16 instruction OPCODE. */
2208
2209 static inline unsigned int
2210 mips16_opcode_length (unsigned long opcode)
2211 {
2212 return (opcode >> 16) == 0 ? 2 : 4;
2213 }
2214
2215 /* Return the length of instruction INSN. */
2216
2217 static inline unsigned int
2218 insn_length (const struct mips_cl_insn *insn)
2219 {
2220 if (mips_opts.micromips)
2221 return micromips_insn_length (insn->insn_mo);
2222 else if (mips_opts.mips16)
2223 return mips16_opcode_length (insn->insn_opcode);
2224 else
2225 return 4;
2226 }
2227
2228 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2229
2230 static void
2231 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2232 {
2233 size_t i;
2234
2235 insn->insn_mo = mo;
2236 insn->insn_opcode = mo->match;
2237 insn->frag = NULL;
2238 insn->where = 0;
2239 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2240 insn->fixp[i] = NULL;
2241 insn->fixed_p = (mips_opts.noreorder > 0);
2242 insn->noreorder_p = (mips_opts.noreorder > 0);
2243 insn->mips16_absolute_jump_p = 0;
2244 insn->complete_p = 0;
2245 insn->cleared_p = 0;
2246 }
2247
2248 /* Get a list of all the operands in INSN. */
2249
2250 static const struct mips_operand_array *
2251 insn_operands (const struct mips_cl_insn *insn)
2252 {
2253 if (insn->insn_mo >= &mips_opcodes[0]
2254 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2255 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2256
2257 if (insn->insn_mo >= &mips16_opcodes[0]
2258 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2259 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2260
2261 if (insn->insn_mo >= &micromips_opcodes[0]
2262 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2263 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2264
2265 abort ();
2266 }
2267
2268 /* Get a description of operand OPNO of INSN. */
2269
2270 static const struct mips_operand *
2271 insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2272 {
2273 const struct mips_operand_array *operands;
2274
2275 operands = insn_operands (insn);
2276 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2277 abort ();
2278 return operands->operand[opno];
2279 }
2280
2281 /* Install UVAL as the value of OPERAND in INSN. */
2282
2283 static inline void
2284 insn_insert_operand (struct mips_cl_insn *insn,
2285 const struct mips_operand *operand, unsigned int uval)
2286 {
2287 if (mips_opts.mips16
2288 && operand->type == OP_INT && operand->lsb == 0
2289 && mips_opcode_32bit_p (insn->insn_mo))
2290 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2291 else
2292 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2293 }
2294
2295 /* Extract the value of OPERAND from INSN. */
2296
2297 static inline unsigned
2298 insn_extract_operand (const struct mips_cl_insn *insn,
2299 const struct mips_operand *operand)
2300 {
2301 return mips_extract_operand (operand, insn->insn_opcode);
2302 }
2303
2304 /* Record the current MIPS16/microMIPS mode in now_seg. */
2305
2306 static void
2307 mips_record_compressed_mode (void)
2308 {
2309 segment_info_type *si;
2310
2311 si = seg_info (now_seg);
2312 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2313 si->tc_segment_info_data.mips16 = mips_opts.mips16;
2314 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2315 si->tc_segment_info_data.micromips = mips_opts.micromips;
2316 }
2317
2318 /* Read a standard MIPS instruction from BUF. */
2319
2320 static unsigned long
2321 read_insn (char *buf)
2322 {
2323 if (target_big_endian)
2324 return bfd_getb32 ((bfd_byte *) buf);
2325 else
2326 return bfd_getl32 ((bfd_byte *) buf);
2327 }
2328
2329 /* Write standard MIPS instruction INSN to BUF. Return a pointer to
2330 the next byte. */
2331
2332 static char *
2333 write_insn (char *buf, unsigned int insn)
2334 {
2335 md_number_to_chars (buf, insn, 4);
2336 return buf + 4;
2337 }
2338
2339 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2340 has length LENGTH. */
2341
2342 static unsigned long
2343 read_compressed_insn (char *buf, unsigned int length)
2344 {
2345 unsigned long insn;
2346 unsigned int i;
2347
2348 insn = 0;
2349 for (i = 0; i < length; i += 2)
2350 {
2351 insn <<= 16;
2352 if (target_big_endian)
2353 insn |= bfd_getb16 ((char *) buf);
2354 else
2355 insn |= bfd_getl16 ((char *) buf);
2356 buf += 2;
2357 }
2358 return insn;
2359 }
2360
2361 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2362 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2363
2364 static char *
2365 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2366 {
2367 unsigned int i;
2368
2369 for (i = 0; i < length; i += 2)
2370 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2371 return buf + length;
2372 }
2373
2374 /* Install INSN at the location specified by its "frag" and "where" fields. */
2375
2376 static void
2377 install_insn (const struct mips_cl_insn *insn)
2378 {
2379 char *f = insn->frag->fr_literal + insn->where;
2380 if (HAVE_CODE_COMPRESSION)
2381 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2382 else
2383 write_insn (f, insn->insn_opcode);
2384 mips_record_compressed_mode ();
2385 }
2386
2387 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2388 and install the opcode in the new location. */
2389
2390 static void
2391 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2392 {
2393 size_t i;
2394
2395 insn->frag = frag;
2396 insn->where = where;
2397 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2398 if (insn->fixp[i] != NULL)
2399 {
2400 insn->fixp[i]->fx_frag = frag;
2401 insn->fixp[i]->fx_where = where;
2402 }
2403 install_insn (insn);
2404 }
2405
2406 /* Add INSN to the end of the output. */
2407
2408 static void
2409 add_fixed_insn (struct mips_cl_insn *insn)
2410 {
2411 char *f = frag_more (insn_length (insn));
2412 move_insn (insn, frag_now, f - frag_now->fr_literal);
2413 }
2414
2415 /* Start a variant frag and move INSN to the start of the variant part,
2416 marking it as fixed. The other arguments are as for frag_var. */
2417
2418 static void
2419 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2420 relax_substateT subtype, symbolS *symbol, offsetT offset)
2421 {
2422 frag_grow (max_chars);
2423 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2424 insn->fixed_p = 1;
2425 frag_var (rs_machine_dependent, max_chars, var,
2426 subtype, symbol, offset, NULL);
2427 }
2428
2429 /* Insert N copies of INSN into the history buffer, starting at
2430 position FIRST. Neither FIRST nor N need to be clipped. */
2431
2432 static void
2433 insert_into_history (unsigned int first, unsigned int n,
2434 const struct mips_cl_insn *insn)
2435 {
2436 if (mips_relax.sequence != 2)
2437 {
2438 unsigned int i;
2439
2440 for (i = ARRAY_SIZE (history); i-- > first;)
2441 if (i >= first + n)
2442 history[i] = history[i - n];
2443 else
2444 history[i] = *insn;
2445 }
2446 }
2447
2448 /* Clear the error in insn_error. */
2449
2450 static void
2451 clear_insn_error (void)
2452 {
2453 memset (&insn_error, 0, sizeof (insn_error));
2454 }
2455
2456 /* Possibly record error message MSG for the current instruction.
2457 If the error is about a particular argument, ARGNUM is the 1-based
2458 number of that argument, otherwise it is 0. FORMAT is the format
2459 of MSG. Return true if MSG was used, false if the current message
2460 was kept. */
2461
2462 static bfd_boolean
2463 set_insn_error_format (int argnum, enum mips_insn_error_format format,
2464 const char *msg)
2465 {
2466 if (argnum == 0)
2467 {
2468 /* Give priority to errors against specific arguments, and to
2469 the first whole-instruction message. */
2470 if (insn_error.msg)
2471 return FALSE;
2472 }
2473 else
2474 {
2475 /* Keep insn_error if it is against a later argument. */
2476 if (argnum < insn_error.min_argnum)
2477 return FALSE;
2478
2479 /* If both errors are against the same argument but are different,
2480 give up on reporting a specific error for this argument.
2481 See the comment about mips_insn_error for details. */
2482 if (argnum == insn_error.min_argnum
2483 && insn_error.msg
2484 && strcmp (insn_error.msg, msg) != 0)
2485 {
2486 insn_error.msg = 0;
2487 insn_error.min_argnum += 1;
2488 return FALSE;
2489 }
2490 }
2491 insn_error.min_argnum = argnum;
2492 insn_error.format = format;
2493 insn_error.msg = msg;
2494 return TRUE;
2495 }
2496
2497 /* Record an instruction error with no % format fields. ARGNUM and MSG are
2498 as for set_insn_error_format. */
2499
2500 static void
2501 set_insn_error (int argnum, const char *msg)
2502 {
2503 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2504 }
2505
2506 /* Record an instruction error with one %d field I. ARGNUM and MSG are
2507 as for set_insn_error_format. */
2508
2509 static void
2510 set_insn_error_i (int argnum, const char *msg, int i)
2511 {
2512 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2513 insn_error.u.i = i;
2514 }
2515
2516 /* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2517 are as for set_insn_error_format. */
2518
2519 static void
2520 set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2521 {
2522 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2523 {
2524 insn_error.u.ss[0] = s1;
2525 insn_error.u.ss[1] = s2;
2526 }
2527 }
2528
2529 /* Report the error in insn_error, which is against assembly code STR. */
2530
2531 static void
2532 report_insn_error (const char *str)
2533 {
2534 const char *msg = concat (insn_error.msg, " `%s'", NULL);
2535
2536 switch (insn_error.format)
2537 {
2538 case ERR_FMT_PLAIN:
2539 as_bad (msg, str);
2540 break;
2541
2542 case ERR_FMT_I:
2543 as_bad (msg, insn_error.u.i, str);
2544 break;
2545
2546 case ERR_FMT_SS:
2547 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2548 break;
2549 }
2550
2551 free ((char *) msg);
2552 }
2553
2554 /* Initialize vr4120_conflicts. There is a bit of duplication here:
2555 the idea is to make it obvious at a glance that each errata is
2556 included. */
2557
2558 static void
2559 init_vr4120_conflicts (void)
2560 {
2561 #define CONFLICT(FIRST, SECOND) \
2562 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2563
2564 /* Errata 21 - [D]DIV[U] after [D]MACC */
2565 CONFLICT (MACC, DIV);
2566 CONFLICT (DMACC, DIV);
2567
2568 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2569 CONFLICT (DMULT, DMULT);
2570 CONFLICT (DMULT, DMACC);
2571 CONFLICT (DMACC, DMULT);
2572 CONFLICT (DMACC, DMACC);
2573
2574 /* Errata 24 - MT{LO,HI} after [D]MACC */
2575 CONFLICT (MACC, MTHILO);
2576 CONFLICT (DMACC, MTHILO);
2577
2578 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2579 instruction is executed immediately after a MACC or DMACC
2580 instruction, the result of [either instruction] is incorrect." */
2581 CONFLICT (MACC, MULT);
2582 CONFLICT (MACC, DMULT);
2583 CONFLICT (DMACC, MULT);
2584 CONFLICT (DMACC, DMULT);
2585
2586 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2587 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2588 DDIV or DDIVU instruction, the result of the MACC or
2589 DMACC instruction is incorrect.". */
2590 CONFLICT (DMULT, MACC);
2591 CONFLICT (DMULT, DMACC);
2592 CONFLICT (DIV, MACC);
2593 CONFLICT (DIV, DMACC);
2594
2595 #undef CONFLICT
2596 }
2597
2598 struct regname {
2599 const char *name;
2600 unsigned int num;
2601 };
2602
2603 #define RNUM_MASK 0x00000ff
2604 #define RTYPE_MASK 0x0ffff00
2605 #define RTYPE_NUM 0x0000100
2606 #define RTYPE_FPU 0x0000200
2607 #define RTYPE_FCC 0x0000400
2608 #define RTYPE_VEC 0x0000800
2609 #define RTYPE_GP 0x0001000
2610 #define RTYPE_CP0 0x0002000
2611 #define RTYPE_PC 0x0004000
2612 #define RTYPE_ACC 0x0008000
2613 #define RTYPE_CCC 0x0010000
2614 #define RTYPE_VI 0x0020000
2615 #define RTYPE_VF 0x0040000
2616 #define RTYPE_R5900_I 0x0080000
2617 #define RTYPE_R5900_Q 0x0100000
2618 #define RTYPE_R5900_R 0x0200000
2619 #define RTYPE_R5900_ACC 0x0400000
2620 #define RTYPE_MSA 0x0800000
2621 #define RWARN 0x8000000
2622
2623 #define GENERIC_REGISTER_NUMBERS \
2624 {"$0", RTYPE_NUM | 0}, \
2625 {"$1", RTYPE_NUM | 1}, \
2626 {"$2", RTYPE_NUM | 2}, \
2627 {"$3", RTYPE_NUM | 3}, \
2628 {"$4", RTYPE_NUM | 4}, \
2629 {"$5", RTYPE_NUM | 5}, \
2630 {"$6", RTYPE_NUM | 6}, \
2631 {"$7", RTYPE_NUM | 7}, \
2632 {"$8", RTYPE_NUM | 8}, \
2633 {"$9", RTYPE_NUM | 9}, \
2634 {"$10", RTYPE_NUM | 10}, \
2635 {"$11", RTYPE_NUM | 11}, \
2636 {"$12", RTYPE_NUM | 12}, \
2637 {"$13", RTYPE_NUM | 13}, \
2638 {"$14", RTYPE_NUM | 14}, \
2639 {"$15", RTYPE_NUM | 15}, \
2640 {"$16", RTYPE_NUM | 16}, \
2641 {"$17", RTYPE_NUM | 17}, \
2642 {"$18", RTYPE_NUM | 18}, \
2643 {"$19", RTYPE_NUM | 19}, \
2644 {"$20", RTYPE_NUM | 20}, \
2645 {"$21", RTYPE_NUM | 21}, \
2646 {"$22", RTYPE_NUM | 22}, \
2647 {"$23", RTYPE_NUM | 23}, \
2648 {"$24", RTYPE_NUM | 24}, \
2649 {"$25", RTYPE_NUM | 25}, \
2650 {"$26", RTYPE_NUM | 26}, \
2651 {"$27", RTYPE_NUM | 27}, \
2652 {"$28", RTYPE_NUM | 28}, \
2653 {"$29", RTYPE_NUM | 29}, \
2654 {"$30", RTYPE_NUM | 30}, \
2655 {"$31", RTYPE_NUM | 31}
2656
2657 #define FPU_REGISTER_NAMES \
2658 {"$f0", RTYPE_FPU | 0}, \
2659 {"$f1", RTYPE_FPU | 1}, \
2660 {"$f2", RTYPE_FPU | 2}, \
2661 {"$f3", RTYPE_FPU | 3}, \
2662 {"$f4", RTYPE_FPU | 4}, \
2663 {"$f5", RTYPE_FPU | 5}, \
2664 {"$f6", RTYPE_FPU | 6}, \
2665 {"$f7", RTYPE_FPU | 7}, \
2666 {"$f8", RTYPE_FPU | 8}, \
2667 {"$f9", RTYPE_FPU | 9}, \
2668 {"$f10", RTYPE_FPU | 10}, \
2669 {"$f11", RTYPE_FPU | 11}, \
2670 {"$f12", RTYPE_FPU | 12}, \
2671 {"$f13", RTYPE_FPU | 13}, \
2672 {"$f14", RTYPE_FPU | 14}, \
2673 {"$f15", RTYPE_FPU | 15}, \
2674 {"$f16", RTYPE_FPU | 16}, \
2675 {"$f17", RTYPE_FPU | 17}, \
2676 {"$f18", RTYPE_FPU | 18}, \
2677 {"$f19", RTYPE_FPU | 19}, \
2678 {"$f20", RTYPE_FPU | 20}, \
2679 {"$f21", RTYPE_FPU | 21}, \
2680 {"$f22", RTYPE_FPU | 22}, \
2681 {"$f23", RTYPE_FPU | 23}, \
2682 {"$f24", RTYPE_FPU | 24}, \
2683 {"$f25", RTYPE_FPU | 25}, \
2684 {"$f26", RTYPE_FPU | 26}, \
2685 {"$f27", RTYPE_FPU | 27}, \
2686 {"$f28", RTYPE_FPU | 28}, \
2687 {"$f29", RTYPE_FPU | 29}, \
2688 {"$f30", RTYPE_FPU | 30}, \
2689 {"$f31", RTYPE_FPU | 31}
2690
2691 #define FPU_CONDITION_CODE_NAMES \
2692 {"$fcc0", RTYPE_FCC | 0}, \
2693 {"$fcc1", RTYPE_FCC | 1}, \
2694 {"$fcc2", RTYPE_FCC | 2}, \
2695 {"$fcc3", RTYPE_FCC | 3}, \
2696 {"$fcc4", RTYPE_FCC | 4}, \
2697 {"$fcc5", RTYPE_FCC | 5}, \
2698 {"$fcc6", RTYPE_FCC | 6}, \
2699 {"$fcc7", RTYPE_FCC | 7}
2700
2701 #define COPROC_CONDITION_CODE_NAMES \
2702 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2703 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2704 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2705 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2706 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2707 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2708 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2709 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2710
2711 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2712 {"$a4", RTYPE_GP | 8}, \
2713 {"$a5", RTYPE_GP | 9}, \
2714 {"$a6", RTYPE_GP | 10}, \
2715 {"$a7", RTYPE_GP | 11}, \
2716 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2717 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2718 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2719 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2720 {"$t0", RTYPE_GP | 12}, \
2721 {"$t1", RTYPE_GP | 13}, \
2722 {"$t2", RTYPE_GP | 14}, \
2723 {"$t3", RTYPE_GP | 15}
2724
2725 #define O32_SYMBOLIC_REGISTER_NAMES \
2726 {"$t0", RTYPE_GP | 8}, \
2727 {"$t1", RTYPE_GP | 9}, \
2728 {"$t2", RTYPE_GP | 10}, \
2729 {"$t3", RTYPE_GP | 11}, \
2730 {"$t4", RTYPE_GP | 12}, \
2731 {"$t5", RTYPE_GP | 13}, \
2732 {"$t6", RTYPE_GP | 14}, \
2733 {"$t7", RTYPE_GP | 15}, \
2734 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2735 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2736 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2737 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2738
2739 /* Remaining symbolic register names */
2740 #define SYMBOLIC_REGISTER_NAMES \
2741 {"$zero", RTYPE_GP | 0}, \
2742 {"$at", RTYPE_GP | 1}, \
2743 {"$AT", RTYPE_GP | 1}, \
2744 {"$v0", RTYPE_GP | 2}, \
2745 {"$v1", RTYPE_GP | 3}, \
2746 {"$a0", RTYPE_GP | 4}, \
2747 {"$a1", RTYPE_GP | 5}, \
2748 {"$a2", RTYPE_GP | 6}, \
2749 {"$a3", RTYPE_GP | 7}, \
2750 {"$s0", RTYPE_GP | 16}, \
2751 {"$s1", RTYPE_GP | 17}, \
2752 {"$s2", RTYPE_GP | 18}, \
2753 {"$s3", RTYPE_GP | 19}, \
2754 {"$s4", RTYPE_GP | 20}, \
2755 {"$s5", RTYPE_GP | 21}, \
2756 {"$s6", RTYPE_GP | 22}, \
2757 {"$s7", RTYPE_GP | 23}, \
2758 {"$t8", RTYPE_GP | 24}, \
2759 {"$t9", RTYPE_GP | 25}, \
2760 {"$k0", RTYPE_GP | 26}, \
2761 {"$kt0", RTYPE_GP | 26}, \
2762 {"$k1", RTYPE_GP | 27}, \
2763 {"$kt1", RTYPE_GP | 27}, \
2764 {"$gp", RTYPE_GP | 28}, \
2765 {"$sp", RTYPE_GP | 29}, \
2766 {"$s8", RTYPE_GP | 30}, \
2767 {"$fp", RTYPE_GP | 30}, \
2768 {"$ra", RTYPE_GP | 31}
2769
2770 #define MIPS16_SPECIAL_REGISTER_NAMES \
2771 {"$pc", RTYPE_PC | 0}
2772
2773 #define MDMX_VECTOR_REGISTER_NAMES \
2774 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2775 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2776 {"$v2", RTYPE_VEC | 2}, \
2777 {"$v3", RTYPE_VEC | 3}, \
2778 {"$v4", RTYPE_VEC | 4}, \
2779 {"$v5", RTYPE_VEC | 5}, \
2780 {"$v6", RTYPE_VEC | 6}, \
2781 {"$v7", RTYPE_VEC | 7}, \
2782 {"$v8", RTYPE_VEC | 8}, \
2783 {"$v9", RTYPE_VEC | 9}, \
2784 {"$v10", RTYPE_VEC | 10}, \
2785 {"$v11", RTYPE_VEC | 11}, \
2786 {"$v12", RTYPE_VEC | 12}, \
2787 {"$v13", RTYPE_VEC | 13}, \
2788 {"$v14", RTYPE_VEC | 14}, \
2789 {"$v15", RTYPE_VEC | 15}, \
2790 {"$v16", RTYPE_VEC | 16}, \
2791 {"$v17", RTYPE_VEC | 17}, \
2792 {"$v18", RTYPE_VEC | 18}, \
2793 {"$v19", RTYPE_VEC | 19}, \
2794 {"$v20", RTYPE_VEC | 20}, \
2795 {"$v21", RTYPE_VEC | 21}, \
2796 {"$v22", RTYPE_VEC | 22}, \
2797 {"$v23", RTYPE_VEC | 23}, \
2798 {"$v24", RTYPE_VEC | 24}, \
2799 {"$v25", RTYPE_VEC | 25}, \
2800 {"$v26", RTYPE_VEC | 26}, \
2801 {"$v27", RTYPE_VEC | 27}, \
2802 {"$v28", RTYPE_VEC | 28}, \
2803 {"$v29", RTYPE_VEC | 29}, \
2804 {"$v30", RTYPE_VEC | 30}, \
2805 {"$v31", RTYPE_VEC | 31}
2806
2807 #define R5900_I_NAMES \
2808 {"$I", RTYPE_R5900_I | 0}
2809
2810 #define R5900_Q_NAMES \
2811 {"$Q", RTYPE_R5900_Q | 0}
2812
2813 #define R5900_R_NAMES \
2814 {"$R", RTYPE_R5900_R | 0}
2815
2816 #define R5900_ACC_NAMES \
2817 {"$ACC", RTYPE_R5900_ACC | 0 }
2818
2819 #define MIPS_DSP_ACCUMULATOR_NAMES \
2820 {"$ac0", RTYPE_ACC | 0}, \
2821 {"$ac1", RTYPE_ACC | 1}, \
2822 {"$ac2", RTYPE_ACC | 2}, \
2823 {"$ac3", RTYPE_ACC | 3}
2824
2825 static const struct regname reg_names[] = {
2826 GENERIC_REGISTER_NUMBERS,
2827 FPU_REGISTER_NAMES,
2828 FPU_CONDITION_CODE_NAMES,
2829 COPROC_CONDITION_CODE_NAMES,
2830
2831 /* The $txx registers depends on the abi,
2832 these will be added later into the symbol table from
2833 one of the tables below once mips_abi is set after
2834 parsing of arguments from the command line. */
2835 SYMBOLIC_REGISTER_NAMES,
2836
2837 MIPS16_SPECIAL_REGISTER_NAMES,
2838 MDMX_VECTOR_REGISTER_NAMES,
2839 R5900_I_NAMES,
2840 R5900_Q_NAMES,
2841 R5900_R_NAMES,
2842 R5900_ACC_NAMES,
2843 MIPS_DSP_ACCUMULATOR_NAMES,
2844 {0, 0}
2845 };
2846
2847 static const struct regname reg_names_o32[] = {
2848 O32_SYMBOLIC_REGISTER_NAMES,
2849 {0, 0}
2850 };
2851
2852 static const struct regname reg_names_n32n64[] = {
2853 N32N64_SYMBOLIC_REGISTER_NAMES,
2854 {0, 0}
2855 };
2856
2857 /* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2858 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2859 of these register symbols, return the associated vector register,
2860 otherwise return SYMVAL itself. */
2861
2862 static unsigned int
2863 mips_prefer_vec_regno (unsigned int symval)
2864 {
2865 if ((symval & -2) == (RTYPE_GP | 2))
2866 return RTYPE_VEC | (symval & 1);
2867 return symval;
2868 }
2869
2870 /* Return true if string [S, E) is a valid register name, storing its
2871 symbol value in *SYMVAL_PTR if so. */
2872
2873 static bfd_boolean
2874 mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
2875 {
2876 char save_c;
2877 symbolS *symbol;
2878
2879 /* Terminate name. */
2880 save_c = *e;
2881 *e = '\0';
2882
2883 /* Look up the name. */
2884 symbol = symbol_find (s);
2885 *e = save_c;
2886
2887 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2888 return FALSE;
2889
2890 *symval_ptr = S_GET_VALUE (symbol);
2891 return TRUE;
2892 }
2893
2894 /* Return true if the string at *SPTR is a valid register name. Allow it
2895 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2896 is nonnull.
2897
2898 When returning true, move *SPTR past the register, store the
2899 register's symbol value in *SYMVAL_PTR and the channel mask in
2900 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2901 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2902 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2903
2904 static bfd_boolean
2905 mips_parse_register (char **sptr, unsigned int *symval_ptr,
2906 unsigned int *channels_ptr)
2907 {
2908 char *s, *e, *m;
2909 const char *q;
2910 unsigned int channels, symval, bit;
2911
2912 /* Find end of name. */
2913 s = e = *sptr;
2914 if (is_name_beginner (*e))
2915 ++e;
2916 while (is_part_of_name (*e))
2917 ++e;
2918
2919 channels = 0;
2920 if (!mips_parse_register_1 (s, e, &symval))
2921 {
2922 if (!channels_ptr)
2923 return FALSE;
2924
2925 /* Eat characters from the end of the string that are valid
2926 channel suffixes. The preceding register must be $ACC or
2927 end with a digit, so there is no ambiguity. */
2928 bit = 1;
2929 m = e;
2930 for (q = "wzyx"; *q; q++, bit <<= 1)
2931 if (m > s && m[-1] == *q)
2932 {
2933 --m;
2934 channels |= bit;
2935 }
2936
2937 if (channels == 0
2938 || !mips_parse_register_1 (s, m, &symval)
2939 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2940 return FALSE;
2941 }
2942
2943 *sptr = e;
2944 *symval_ptr = symval;
2945 if (channels_ptr)
2946 *channels_ptr = channels;
2947 return TRUE;
2948 }
2949
2950 /* Check if SPTR points at a valid register specifier according to TYPES.
2951 If so, then return 1, advance S to consume the specifier and store
2952 the register's number in REGNOP, otherwise return 0. */
2953
2954 static int
2955 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2956 {
2957 unsigned int regno;
2958
2959 if (mips_parse_register (s, &regno, NULL))
2960 {
2961 if (types & RTYPE_VEC)
2962 regno = mips_prefer_vec_regno (regno);
2963 if (regno & types)
2964 regno &= RNUM_MASK;
2965 else
2966 regno = ~0;
2967 }
2968 else
2969 {
2970 if (types & RWARN)
2971 as_warn (_("unrecognized register name `%s'"), *s);
2972 regno = ~0;
2973 }
2974 if (regnop)
2975 *regnop = regno;
2976 return regno <= RNUM_MASK;
2977 }
2978
2979 /* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2980 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2981
2982 static char *
2983 mips_parse_vu0_channels (char *s, unsigned int *channels)
2984 {
2985 unsigned int i;
2986
2987 *channels = 0;
2988 for (i = 0; i < 4; i++)
2989 if (*s == "xyzw"[i])
2990 {
2991 *channels |= 1 << (3 - i);
2992 ++s;
2993 }
2994 return s;
2995 }
2996
2997 /* Token types for parsed operand lists. */
2998 enum mips_operand_token_type {
2999 /* A plain register, e.g. $f2. */
3000 OT_REG,
3001
3002 /* A 4-bit XYZW channel mask. */
3003 OT_CHANNELS,
3004
3005 /* A constant vector index, e.g. [1]. */
3006 OT_INTEGER_INDEX,
3007
3008 /* A register vector index, e.g. [$2]. */
3009 OT_REG_INDEX,
3010
3011 /* A continuous range of registers, e.g. $s0-$s4. */
3012 OT_REG_RANGE,
3013
3014 /* A (possibly relocated) expression. */
3015 OT_INTEGER,
3016
3017 /* A floating-point value. */
3018 OT_FLOAT,
3019
3020 /* A single character. This can be '(', ')' or ',', but '(' only appears
3021 before OT_REGs. */
3022 OT_CHAR,
3023
3024 /* A doubled character, either "--" or "++". */
3025 OT_DOUBLE_CHAR,
3026
3027 /* The end of the operand list. */
3028 OT_END
3029 };
3030
3031 /* A parsed operand token. */
3032 struct mips_operand_token
3033 {
3034 /* The type of token. */
3035 enum mips_operand_token_type type;
3036 union
3037 {
3038 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
3039 unsigned int regno;
3040
3041 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3042 unsigned int channels;
3043
3044 /* The integer value of an OT_INTEGER_INDEX. */
3045 addressT index;
3046
3047 /* The two register symbol values involved in an OT_REG_RANGE. */
3048 struct {
3049 unsigned int regno1;
3050 unsigned int regno2;
3051 } reg_range;
3052
3053 /* The value of an OT_INTEGER. The value is represented as an
3054 expression and the relocation operators that were applied to
3055 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3056 relocation operators were used. */
3057 struct {
3058 expressionS value;
3059 bfd_reloc_code_real_type relocs[3];
3060 } integer;
3061
3062 /* The binary data for an OT_FLOAT constant, and the number of bytes
3063 in the constant. */
3064 struct {
3065 unsigned char data[8];
3066 int length;
3067 } flt;
3068
3069 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
3070 char ch;
3071 } u;
3072 };
3073
3074 /* An obstack used to construct lists of mips_operand_tokens. */
3075 static struct obstack mips_operand_tokens;
3076
3077 /* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3078
3079 static void
3080 mips_add_token (struct mips_operand_token *token,
3081 enum mips_operand_token_type type)
3082 {
3083 token->type = type;
3084 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3085 }
3086
3087 /* Check whether S is '(' followed by a register name. Add OT_CHAR
3088 and OT_REG tokens for them if so, and return a pointer to the first
3089 unconsumed character. Return null otherwise. */
3090
3091 static char *
3092 mips_parse_base_start (char *s)
3093 {
3094 struct mips_operand_token token;
3095 unsigned int regno, channels;
3096 bfd_boolean decrement_p;
3097
3098 if (*s != '(')
3099 return 0;
3100
3101 ++s;
3102 SKIP_SPACE_TABS (s);
3103
3104 /* Only match "--" as part of a base expression. In other contexts "--X"
3105 is a double negative. */
3106 decrement_p = (s[0] == '-' && s[1] == '-');
3107 if (decrement_p)
3108 {
3109 s += 2;
3110 SKIP_SPACE_TABS (s);
3111 }
3112
3113 /* Allow a channel specifier because that leads to better error messages
3114 than treating something like "$vf0x++" as an expression. */
3115 if (!mips_parse_register (&s, &regno, &channels))
3116 return 0;
3117
3118 token.u.ch = '(';
3119 mips_add_token (&token, OT_CHAR);
3120
3121 if (decrement_p)
3122 {
3123 token.u.ch = '-';
3124 mips_add_token (&token, OT_DOUBLE_CHAR);
3125 }
3126
3127 token.u.regno = regno;
3128 mips_add_token (&token, OT_REG);
3129
3130 if (channels)
3131 {
3132 token.u.channels = channels;
3133 mips_add_token (&token, OT_CHANNELS);
3134 }
3135
3136 /* For consistency, only match "++" as part of base expressions too. */
3137 SKIP_SPACE_TABS (s);
3138 if (s[0] == '+' && s[1] == '+')
3139 {
3140 s += 2;
3141 token.u.ch = '+';
3142 mips_add_token (&token, OT_DOUBLE_CHAR);
3143 }
3144
3145 return s;
3146 }
3147
3148 /* Parse one or more tokens from S. Return a pointer to the first
3149 unconsumed character on success. Return null if an error was found
3150 and store the error text in insn_error. FLOAT_FORMAT is as for
3151 mips_parse_arguments. */
3152
3153 static char *
3154 mips_parse_argument_token (char *s, char float_format)
3155 {
3156 char *end, *save_in;
3157 const char *err;
3158 unsigned int regno1, regno2, channels;
3159 struct mips_operand_token token;
3160
3161 /* First look for "($reg", since we want to treat that as an
3162 OT_CHAR and OT_REG rather than an expression. */
3163 end = mips_parse_base_start (s);
3164 if (end)
3165 return end;
3166
3167 /* Handle other characters that end up as OT_CHARs. */
3168 if (*s == ')' || *s == ',')
3169 {
3170 token.u.ch = *s;
3171 mips_add_token (&token, OT_CHAR);
3172 ++s;
3173 return s;
3174 }
3175
3176 /* Handle tokens that start with a register. */
3177 if (mips_parse_register (&s, &regno1, &channels))
3178 {
3179 if (channels)
3180 {
3181 /* A register and a VU0 channel suffix. */
3182 token.u.regno = regno1;
3183 mips_add_token (&token, OT_REG);
3184
3185 token.u.channels = channels;
3186 mips_add_token (&token, OT_CHANNELS);
3187 return s;
3188 }
3189
3190 SKIP_SPACE_TABS (s);
3191 if (*s == '-')
3192 {
3193 /* A register range. */
3194 ++s;
3195 SKIP_SPACE_TABS (s);
3196 if (!mips_parse_register (&s, &regno2, NULL))
3197 {
3198 set_insn_error (0, _("invalid register range"));
3199 return 0;
3200 }
3201
3202 token.u.reg_range.regno1 = regno1;
3203 token.u.reg_range.regno2 = regno2;
3204 mips_add_token (&token, OT_REG_RANGE);
3205 return s;
3206 }
3207
3208 /* Add the register itself. */
3209 token.u.regno = regno1;
3210 mips_add_token (&token, OT_REG);
3211
3212 /* Check for a vector index. */
3213 if (*s == '[')
3214 {
3215 ++s;
3216 SKIP_SPACE_TABS (s);
3217 if (mips_parse_register (&s, &token.u.regno, NULL))
3218 mips_add_token (&token, OT_REG_INDEX);
3219 else
3220 {
3221 expressionS element;
3222
3223 my_getExpression (&element, s);
3224 if (element.X_op != O_constant)
3225 {
3226 set_insn_error (0, _("vector element must be constant"));
3227 return 0;
3228 }
3229 s = expr_end;
3230 token.u.index = element.X_add_number;
3231 mips_add_token (&token, OT_INTEGER_INDEX);
3232 }
3233 SKIP_SPACE_TABS (s);
3234 if (*s != ']')
3235 {
3236 set_insn_error (0, _("missing `]'"));
3237 return 0;
3238 }
3239 ++s;
3240 }
3241 return s;
3242 }
3243
3244 if (float_format)
3245 {
3246 /* First try to treat expressions as floats. */
3247 save_in = input_line_pointer;
3248 input_line_pointer = s;
3249 err = md_atof (float_format, (char *) token.u.flt.data,
3250 &token.u.flt.length);
3251 end = input_line_pointer;
3252 input_line_pointer = save_in;
3253 if (err && *err)
3254 {
3255 set_insn_error (0, err);
3256 return 0;
3257 }
3258 if (s != end)
3259 {
3260 mips_add_token (&token, OT_FLOAT);
3261 return end;
3262 }
3263 }
3264
3265 /* Treat everything else as an integer expression. */
3266 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3267 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3268 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3269 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3270 s = expr_end;
3271 mips_add_token (&token, OT_INTEGER);
3272 return s;
3273 }
3274
3275 /* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3276 if expressions should be treated as 32-bit floating-point constants,
3277 'd' if they should be treated as 64-bit floating-point constants,
3278 or 0 if they should be treated as integer expressions (the usual case).
3279
3280 Return a list of tokens on success, otherwise return 0. The caller
3281 must obstack_free the list after use. */
3282
3283 static struct mips_operand_token *
3284 mips_parse_arguments (char *s, char float_format)
3285 {
3286 struct mips_operand_token token;
3287
3288 SKIP_SPACE_TABS (s);
3289 while (*s)
3290 {
3291 s = mips_parse_argument_token (s, float_format);
3292 if (!s)
3293 {
3294 obstack_free (&mips_operand_tokens,
3295 obstack_finish (&mips_operand_tokens));
3296 return 0;
3297 }
3298 SKIP_SPACE_TABS (s);
3299 }
3300 mips_add_token (&token, OT_END);
3301 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
3302 }
3303
3304 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3305 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
3306
3307 static bfd_boolean
3308 is_opcode_valid (const struct mips_opcode *mo)
3309 {
3310 int isa = mips_opts.isa;
3311 int ase = mips_opts.ase;
3312 int fp_s, fp_d;
3313 unsigned int i;
3314
3315 if (ISA_HAS_64BIT_REGS (isa))
3316 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3317 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3318 ase |= mips_ases[i].flags64;
3319
3320 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
3321 return FALSE;
3322
3323 /* Check whether the instruction or macro requires single-precision or
3324 double-precision floating-point support. Note that this information is
3325 stored differently in the opcode table for insns and macros. */
3326 if (mo->pinfo == INSN_MACRO)
3327 {
3328 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3329 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3330 }
3331 else
3332 {
3333 fp_s = mo->pinfo & FP_S;
3334 fp_d = mo->pinfo & FP_D;
3335 }
3336
3337 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3338 return FALSE;
3339
3340 if (fp_s && mips_opts.soft_float)
3341 return FALSE;
3342
3343 return TRUE;
3344 }
3345
3346 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
3347 selected ISA and architecture. */
3348
3349 static bfd_boolean
3350 is_opcode_valid_16 (const struct mips_opcode *mo)
3351 {
3352 int isa = mips_opts.isa;
3353 int ase = mips_opts.ase;
3354 unsigned int i;
3355
3356 if (ISA_HAS_64BIT_REGS (isa))
3357 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3358 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3359 ase |= mips_ases[i].flags64;
3360
3361 return opcode_is_member (mo, isa, ase, mips_opts.arch);
3362 }
3363
3364 /* Return TRUE if the size of the microMIPS opcode MO matches one
3365 explicitly requested. Always TRUE in the standard MIPS mode.
3366 Use is_size_valid_16 for MIPS16 opcodes. */
3367
3368 static bfd_boolean
3369 is_size_valid (const struct mips_opcode *mo)
3370 {
3371 if (!mips_opts.micromips)
3372 return TRUE;
3373
3374 if (mips_opts.insn32)
3375 {
3376 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3377 return FALSE;
3378 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3379 return FALSE;
3380 }
3381 if (!forced_insn_length)
3382 return TRUE;
3383 if (mo->pinfo == INSN_MACRO)
3384 return FALSE;
3385 return forced_insn_length == micromips_insn_length (mo);
3386 }
3387
3388 /* Return TRUE if the size of the MIPS16 opcode MO matches one
3389 explicitly requested. */
3390
3391 static bfd_boolean
3392 is_size_valid_16 (const struct mips_opcode *mo)
3393 {
3394 if (!forced_insn_length)
3395 return TRUE;
3396 if (mo->pinfo == INSN_MACRO)
3397 return FALSE;
3398 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3399 return FALSE;
3400 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3401 return FALSE;
3402 return TRUE;
3403 }
3404
3405 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
3406 of the preceding instruction. Always TRUE in the standard MIPS mode.
3407
3408 We don't accept macros in 16-bit delay slots to avoid a case where
3409 a macro expansion fails because it relies on a preceding 32-bit real
3410 instruction to have matched and does not handle the operands correctly.
3411 The only macros that may expand to 16-bit instructions are JAL that
3412 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3413 and BGT (that likewise cannot be placed in a delay slot) that decay to
3414 a NOP. In all these cases the macros precede any corresponding real
3415 instruction definitions in the opcode table, so they will match in the
3416 second pass where the size of the delay slot is ignored and therefore
3417 produce correct code. */
3418
3419 static bfd_boolean
3420 is_delay_slot_valid (const struct mips_opcode *mo)
3421 {
3422 if (!mips_opts.micromips)
3423 return TRUE;
3424
3425 if (mo->pinfo == INSN_MACRO)
3426 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
3427 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3428 && micromips_insn_length (mo) != 4)
3429 return FALSE;
3430 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3431 && micromips_insn_length (mo) != 2)
3432 return FALSE;
3433
3434 return TRUE;
3435 }
3436
3437 /* For consistency checking, verify that all bits of OPCODE are specified
3438 either by the match/mask part of the instruction definition, or by the
3439 operand list. Also build up a list of operands in OPERANDS.
3440
3441 INSN_BITS says which bits of the instruction are significant.
3442 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3443 provides the mips_operand description of each operand. DECODE_OPERAND
3444 is null for MIPS16 instructions. */
3445
3446 static int
3447 validate_mips_insn (const struct mips_opcode *opcode,
3448 unsigned long insn_bits,
3449 const struct mips_operand *(*decode_operand) (const char *),
3450 struct mips_operand_array *operands)
3451 {
3452 const char *s;
3453 unsigned long used_bits, doubled, undefined, opno, mask;
3454 const struct mips_operand *operand;
3455
3456 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3457 if ((mask & opcode->match) != opcode->match)
3458 {
3459 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3460 opcode->name, opcode->args);
3461 return 0;
3462 }
3463 used_bits = 0;
3464 opno = 0;
3465 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3466 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
3467 for (s = opcode->args; *s; ++s)
3468 switch (*s)
3469 {
3470 case ',':
3471 case '(':
3472 case ')':
3473 break;
3474
3475 case '#':
3476 s++;
3477 break;
3478
3479 default:
3480 if (!decode_operand)
3481 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
3482 else
3483 operand = decode_operand (s);
3484 if (!operand && opcode->pinfo != INSN_MACRO)
3485 {
3486 as_bad (_("internal: unknown operand type: %s %s"),
3487 opcode->name, opcode->args);
3488 return 0;
3489 }
3490 gas_assert (opno < MAX_OPERANDS);
3491 operands->operand[opno] = operand;
3492 if (!decode_operand && operand
3493 && operand->type == OP_INT && operand->lsb == 0
3494 && mips_opcode_32bit_p (opcode))
3495 used_bits |= mips16_immed_extend (-1, operand->size);
3496 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
3497 {
3498 used_bits = mips_insert_operand (operand, used_bits, -1);
3499 if (operand->type == OP_MDMX_IMM_REG)
3500 /* Bit 5 is the format selector (OB vs QH). The opcode table
3501 has separate entries for each format. */
3502 used_bits &= ~(1 << (operand->lsb + 5));
3503 if (operand->type == OP_ENTRY_EXIT_LIST)
3504 used_bits &= ~(mask & 0x700);
3505 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3506 operand field that cannot be fully described with LSB/SIZE. */
3507 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3508 used_bits &= ~0x6000;
3509 }
3510 /* Skip prefix characters. */
3511 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
3512 ++s;
3513 opno += 1;
3514 break;
3515 }
3516 doubled = used_bits & mask & insn_bits;
3517 if (doubled)
3518 {
3519 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3520 " %s %s"), doubled, opcode->name, opcode->args);
3521 return 0;
3522 }
3523 used_bits |= mask;
3524 undefined = ~used_bits & insn_bits;
3525 if (opcode->pinfo != INSN_MACRO && undefined)
3526 {
3527 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3528 undefined, opcode->name, opcode->args);
3529 return 0;
3530 }
3531 used_bits &= ~insn_bits;
3532 if (used_bits)
3533 {
3534 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3535 used_bits, opcode->name, opcode->args);
3536 return 0;
3537 }
3538 return 1;
3539 }
3540
3541 /* The MIPS16 version of validate_mips_insn. */
3542
3543 static int
3544 validate_mips16_insn (const struct mips_opcode *opcode,
3545 struct mips_operand_array *operands)
3546 {
3547 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
3548
3549 return validate_mips_insn (opcode, insn_bits, 0, operands);
3550 }
3551
3552 /* The microMIPS version of validate_mips_insn. */
3553
3554 static int
3555 validate_micromips_insn (const struct mips_opcode *opc,
3556 struct mips_operand_array *operands)
3557 {
3558 unsigned long insn_bits;
3559 unsigned long major;
3560 unsigned int length;
3561
3562 if (opc->pinfo == INSN_MACRO)
3563 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3564 operands);
3565
3566 length = micromips_insn_length (opc);
3567 if (length != 2 && length != 4)
3568 {
3569 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
3570 "%s %s"), length, opc->name, opc->args);
3571 return 0;
3572 }
3573 major = opc->match >> (10 + 8 * (length - 2));
3574 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3575 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3576 {
3577 as_bad (_("internal error: bad microMIPS opcode "
3578 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3579 return 0;
3580 }
3581
3582 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3583 insn_bits = 1 << 4 * length;
3584 insn_bits <<= 4 * length;
3585 insn_bits -= 1;
3586 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3587 operands);
3588 }
3589
3590 /* This function is called once, at assembler startup time. It should set up
3591 all the tables, etc. that the MD part of the assembler will need. */
3592
3593 void
3594 md_begin (void)
3595 {
3596 const char *retval = NULL;
3597 int i = 0;
3598 int broken = 0;
3599
3600 if (mips_pic != NO_PIC)
3601 {
3602 if (g_switch_seen && g_switch_value != 0)
3603 as_bad (_("-G may not be used in position-independent code"));
3604 g_switch_value = 0;
3605 }
3606 else if (mips_abicalls)
3607 {
3608 if (g_switch_seen && g_switch_value != 0)
3609 as_bad (_("-G may not be used with abicalls"));
3610 g_switch_value = 0;
3611 }
3612
3613 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3614 as_warn (_("could not set architecture and machine"));
3615
3616 op_hash = hash_new ();
3617
3618 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
3619 for (i = 0; i < NUMOPCODES;)
3620 {
3621 const char *name = mips_opcodes[i].name;
3622
3623 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
3624 if (retval != NULL)
3625 {
3626 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3627 mips_opcodes[i].name, retval);
3628 /* Probably a memory allocation problem? Give up now. */
3629 as_fatal (_("broken assembler, no assembly attempted"));
3630 }
3631 do
3632 {
3633 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3634 decode_mips_operand, &mips_operands[i]))
3635 broken = 1;
3636 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3637 {
3638 create_insn (&nop_insn, mips_opcodes + i);
3639 if (mips_fix_loongson2f_nop)
3640 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3641 nop_insn.fixed_p = 1;
3642 }
3643 ++i;
3644 }
3645 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3646 }
3647
3648 mips16_op_hash = hash_new ();
3649 mips16_operands = XCNEWVEC (struct mips_operand_array,
3650 bfd_mips16_num_opcodes);
3651
3652 i = 0;
3653 while (i < bfd_mips16_num_opcodes)
3654 {
3655 const char *name = mips16_opcodes[i].name;
3656
3657 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
3658 if (retval != NULL)
3659 as_fatal (_("internal: can't hash `%s': %s"),
3660 mips16_opcodes[i].name, retval);
3661 do
3662 {
3663 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3664 broken = 1;
3665 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3666 {
3667 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3668 mips16_nop_insn.fixed_p = 1;
3669 }
3670 ++i;
3671 }
3672 while (i < bfd_mips16_num_opcodes
3673 && strcmp (mips16_opcodes[i].name, name) == 0);
3674 }
3675
3676 micromips_op_hash = hash_new ();
3677 micromips_operands = XCNEWVEC (struct mips_operand_array,
3678 bfd_micromips_num_opcodes);
3679
3680 i = 0;
3681 while (i < bfd_micromips_num_opcodes)
3682 {
3683 const char *name = micromips_opcodes[i].name;
3684
3685 retval = hash_insert (micromips_op_hash, name,
3686 (void *) &micromips_opcodes[i]);
3687 if (retval != NULL)
3688 as_fatal (_("internal: can't hash `%s': %s"),
3689 micromips_opcodes[i].name, retval);
3690 do
3691 {
3692 struct mips_cl_insn *micromips_nop_insn;
3693
3694 if (!validate_micromips_insn (&micromips_opcodes[i],
3695 &micromips_operands[i]))
3696 broken = 1;
3697
3698 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3699 {
3700 if (micromips_insn_length (micromips_opcodes + i) == 2)
3701 micromips_nop_insn = &micromips_nop16_insn;
3702 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3703 micromips_nop_insn = &micromips_nop32_insn;
3704 else
3705 continue;
3706
3707 if (micromips_nop_insn->insn_mo == NULL
3708 && strcmp (name, "nop") == 0)
3709 {
3710 create_insn (micromips_nop_insn, micromips_opcodes + i);
3711 micromips_nop_insn->fixed_p = 1;
3712 }
3713 }
3714 }
3715 while (++i < bfd_micromips_num_opcodes
3716 && strcmp (micromips_opcodes[i].name, name) == 0);
3717 }
3718
3719 if (broken)
3720 as_fatal (_("broken assembler, no assembly attempted"));
3721
3722 /* We add all the general register names to the symbol table. This
3723 helps us detect invalid uses of them. */
3724 for (i = 0; reg_names[i].name; i++)
3725 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
3726 reg_names[i].num, /* & RNUM_MASK, */
3727 &zero_address_frag));
3728 if (HAVE_NEWABI)
3729 for (i = 0; reg_names_n32n64[i].name; i++)
3730 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
3731 reg_names_n32n64[i].num, /* & RNUM_MASK, */
3732 &zero_address_frag));
3733 else
3734 for (i = 0; reg_names_o32[i].name; i++)
3735 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
3736 reg_names_o32[i].num, /* & RNUM_MASK, */
3737 &zero_address_frag));
3738
3739 for (i = 0; i < 32; i++)
3740 {
3741 char regname[6];
3742
3743 /* R5900 VU0 floating-point register. */
3744 sprintf (regname, "$vf%d", i);
3745 symbol_table_insert (symbol_new (regname, reg_section,
3746 RTYPE_VF | i, &zero_address_frag));
3747
3748 /* R5900 VU0 integer register. */
3749 sprintf (regname, "$vi%d", i);
3750 symbol_table_insert (symbol_new (regname, reg_section,
3751 RTYPE_VI | i, &zero_address_frag));
3752
3753 /* MSA register. */
3754 sprintf (regname, "$w%d", i);
3755 symbol_table_insert (symbol_new (regname, reg_section,
3756 RTYPE_MSA | i, &zero_address_frag));
3757 }
3758
3759 obstack_init (&mips_operand_tokens);
3760
3761 mips_no_prev_insn ();
3762
3763 mips_gprmask = 0;
3764 mips_cprmask[0] = 0;
3765 mips_cprmask[1] = 0;
3766 mips_cprmask[2] = 0;
3767 mips_cprmask[3] = 0;
3768
3769 /* set the default alignment for the text section (2**2) */
3770 record_alignment (text_section, 2);
3771
3772 bfd_set_gp_size (stdoutput, g_switch_value);
3773
3774 /* On a native system other than VxWorks, sections must be aligned
3775 to 16 byte boundaries. When configured for an embedded ELF
3776 target, we don't bother. */
3777 if (strncmp (TARGET_OS, "elf", 3) != 0
3778 && strncmp (TARGET_OS, "vxworks", 7) != 0)
3779 {
3780 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3781 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3782 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3783 }
3784
3785 /* Create a .reginfo section for register masks and a .mdebug
3786 section for debugging information. */
3787 {
3788 segT seg;
3789 subsegT subseg;
3790 flagword flags;
3791 segT sec;
3792
3793 seg = now_seg;
3794 subseg = now_subseg;
3795
3796 /* The ABI says this section should be loaded so that the
3797 running program can access it. However, we don't load it
3798 if we are configured for an embedded target */
3799 flags = SEC_READONLY | SEC_DATA;
3800 if (strncmp (TARGET_OS, "elf", 3) != 0)
3801 flags |= SEC_ALLOC | SEC_LOAD;
3802
3803 if (mips_abi != N64_ABI)
3804 {
3805 sec = subseg_new (".reginfo", (subsegT) 0);
3806
3807 bfd_set_section_flags (stdoutput, sec, flags);
3808 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
3809
3810 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3811 }
3812 else
3813 {
3814 /* The 64-bit ABI uses a .MIPS.options section rather than
3815 .reginfo section. */
3816 sec = subseg_new (".MIPS.options", (subsegT) 0);
3817 bfd_set_section_flags (stdoutput, sec, flags);
3818 bfd_set_section_alignment (stdoutput, sec, 3);
3819
3820 /* Set up the option header. */
3821 {
3822 Elf_Internal_Options opthdr;
3823 char *f;
3824
3825 opthdr.kind = ODK_REGINFO;
3826 opthdr.size = (sizeof (Elf_External_Options)
3827 + sizeof (Elf64_External_RegInfo));
3828 opthdr.section = 0;
3829 opthdr.info = 0;
3830 f = frag_more (sizeof (Elf_External_Options));
3831 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3832 (Elf_External_Options *) f);
3833
3834 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3835 }
3836 }
3837
3838 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3839 bfd_set_section_flags (stdoutput, sec,
3840 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3841 bfd_set_section_alignment (stdoutput, sec, 3);
3842 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3843
3844 if (ECOFF_DEBUGGING)
3845 {
3846 sec = subseg_new (".mdebug", (subsegT) 0);
3847 (void) bfd_set_section_flags (stdoutput, sec,
3848 SEC_HAS_CONTENTS | SEC_READONLY);
3849 (void) bfd_set_section_alignment (stdoutput, sec, 2);
3850 }
3851 else if (mips_flag_pdr)
3852 {
3853 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3854 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3855 SEC_READONLY | SEC_RELOC
3856 | SEC_DEBUGGING);
3857 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3858 }
3859
3860 subseg_set (seg, subseg);
3861 }
3862
3863 if (mips_fix_vr4120)
3864 init_vr4120_conflicts ();
3865 }
3866
3867 static inline void
3868 fpabi_incompatible_with (int fpabi, const char *what)
3869 {
3870 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3871 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3872 }
3873
3874 static inline void
3875 fpabi_requires (int fpabi, const char *what)
3876 {
3877 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3878 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3879 }
3880
3881 /* Check -mabi and register sizes against the specified FP ABI. */
3882 static void
3883 check_fpabi (int fpabi)
3884 {
3885 switch (fpabi)
3886 {
3887 case Val_GNU_MIPS_ABI_FP_DOUBLE:
3888 if (file_mips_opts.soft_float)
3889 fpabi_incompatible_with (fpabi, "softfloat");
3890 else if (file_mips_opts.single_float)
3891 fpabi_incompatible_with (fpabi, "singlefloat");
3892 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3893 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3894 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3895 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
3896 break;
3897
3898 case Val_GNU_MIPS_ABI_FP_XX:
3899 if (mips_abi != O32_ABI)
3900 fpabi_requires (fpabi, "-mabi=32");
3901 else if (file_mips_opts.soft_float)
3902 fpabi_incompatible_with (fpabi, "softfloat");
3903 else if (file_mips_opts.single_float)
3904 fpabi_incompatible_with (fpabi, "singlefloat");
3905 else if (file_mips_opts.fp != 0)
3906 fpabi_requires (fpabi, "fp=xx");
3907 break;
3908
3909 case Val_GNU_MIPS_ABI_FP_64A:
3910 case Val_GNU_MIPS_ABI_FP_64:
3911 if (mips_abi != O32_ABI)
3912 fpabi_requires (fpabi, "-mabi=32");
3913 else if (file_mips_opts.soft_float)
3914 fpabi_incompatible_with (fpabi, "softfloat");
3915 else if (file_mips_opts.single_float)
3916 fpabi_incompatible_with (fpabi, "singlefloat");
3917 else if (file_mips_opts.fp != 64)
3918 fpabi_requires (fpabi, "fp=64");
3919 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3920 fpabi_incompatible_with (fpabi, "nooddspreg");
3921 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3922 fpabi_requires (fpabi, "nooddspreg");
3923 break;
3924
3925 case Val_GNU_MIPS_ABI_FP_SINGLE:
3926 if (file_mips_opts.soft_float)
3927 fpabi_incompatible_with (fpabi, "softfloat");
3928 else if (!file_mips_opts.single_float)
3929 fpabi_requires (fpabi, "singlefloat");
3930 break;
3931
3932 case Val_GNU_MIPS_ABI_FP_SOFT:
3933 if (!file_mips_opts.soft_float)
3934 fpabi_requires (fpabi, "softfloat");
3935 break;
3936
3937 case Val_GNU_MIPS_ABI_FP_OLD_64:
3938 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3939 Tag_GNU_MIPS_ABI_FP, fpabi);
3940 break;
3941
3942 case Val_GNU_MIPS_ABI_FP_NAN2008:
3943 /* Silently ignore compatibility value. */
3944 break;
3945
3946 default:
3947 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3948 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3949 break;
3950 }
3951 }
3952
3953 /* Perform consistency checks on the current options. */
3954
3955 static void
3956 mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3957 {
3958 /* Check the size of integer registers agrees with the ABI and ISA. */
3959 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3960 as_bad (_("`gp=64' used with a 32-bit processor"));
3961 else if (abi_checks
3962 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3963 as_bad (_("`gp=32' used with a 64-bit ABI"));
3964 else if (abi_checks
3965 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3966 as_bad (_("`gp=64' used with a 32-bit ABI"));
3967
3968 /* Check the size of the float registers agrees with the ABI and ISA. */
3969 switch (opts->fp)
3970 {
3971 case 0:
3972 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
3973 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
3974 else if (opts->single_float == 1)
3975 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
3976 break;
3977 case 64:
3978 if (!ISA_HAS_64BIT_FPRS (opts->isa))
3979 as_bad (_("`fp=64' used with a 32-bit fpu"));
3980 else if (abi_checks
3981 && ABI_NEEDS_32BIT_REGS (mips_abi)
3982 && !ISA_HAS_MXHC1 (opts->isa))
3983 as_warn (_("`fp=64' used with a 32-bit ABI"));
3984 break;
3985 case 32:
3986 if (abi_checks
3987 && ABI_NEEDS_64BIT_REGS (mips_abi))
3988 as_warn (_("`fp=32' used with a 64-bit ABI"));
3989 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
3990 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
3991 break;
3992 default:
3993 as_bad (_("Unknown size of floating point registers"));
3994 break;
3995 }
3996
3997 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
3998 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
3999
4000 if (opts->micromips == 1 && opts->mips16 == 1)
4001 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
4002 else if (ISA_IS_R6 (opts->isa)
4003 && (opts->micromips == 1
4004 || opts->mips16 == 1))
4005 as_fatal (_("`%s' cannot be used with `%s'"),
4006 opts->micromips ? "micromips" : "mips16",
4007 mips_cpu_info_from_isa (opts->isa)->name);
4008
4009 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4010 as_fatal (_("branch relaxation is not supported in `%s'"),
4011 mips_cpu_info_from_isa (opts->isa)->name);
4012 }
4013
4014 /* Perform consistency checks on the module level options exactly once.
4015 This is a deferred check that happens:
4016 at the first .set directive
4017 or, at the first pseudo op that generates code (inc .dc.a)
4018 or, at the first instruction
4019 or, at the end. */
4020
4021 static void
4022 file_mips_check_options (void)
4023 {
4024 const struct mips_cpu_info *arch_info = 0;
4025
4026 if (file_mips_opts_checked)
4027 return;
4028
4029 /* The following code determines the register size.
4030 Similar code was added to GCC 3.3 (see override_options() in
4031 config/mips/mips.c). The GAS and GCC code should be kept in sync
4032 as much as possible. */
4033
4034 if (file_mips_opts.gp < 0)
4035 {
4036 /* Infer the integer register size from the ABI and processor.
4037 Restrict ourselves to 32-bit registers if that's all the
4038 processor has, or if the ABI cannot handle 64-bit registers. */
4039 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4040 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4041 ? 32 : 64;
4042 }
4043
4044 if (file_mips_opts.fp < 0)
4045 {
4046 /* No user specified float register size.
4047 ??? GAS treats single-float processors as though they had 64-bit
4048 float registers (although it complains when double-precision
4049 instructions are used). As things stand, saying they have 32-bit
4050 registers would lead to spurious "register must be even" messages.
4051 So here we assume float registers are never smaller than the
4052 integer ones. */
4053 if (file_mips_opts.gp == 64)
4054 /* 64-bit integer registers implies 64-bit float registers. */
4055 file_mips_opts.fp = 64;
4056 else if ((file_mips_opts.ase & FP64_ASES)
4057 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4058 /* Handle ASEs that require 64-bit float registers, if possible. */
4059 file_mips_opts.fp = 64;
4060 else if (ISA_IS_R6 (mips_opts.isa))
4061 /* R6 implies 64-bit float registers. */
4062 file_mips_opts.fp = 64;
4063 else
4064 /* 32-bit float registers. */
4065 file_mips_opts.fp = 32;
4066 }
4067
4068 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
4069
4070 /* Disable operations on odd-numbered floating-point registers by default
4071 when using the FPXX ABI. */
4072 if (file_mips_opts.oddspreg < 0)
4073 {
4074 if (file_mips_opts.fp == 0)
4075 file_mips_opts.oddspreg = 0;
4076 else
4077 file_mips_opts.oddspreg = 1;
4078 }
4079
4080 /* End of GCC-shared inference code. */
4081
4082 /* This flag is set when we have a 64-bit capable CPU but use only
4083 32-bit wide registers. Note that EABI does not use it. */
4084 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4085 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4086 || mips_abi == O32_ABI))
4087 mips_32bitmode = 1;
4088
4089 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4090 as_bad (_("trap exception not supported at ISA 1"));
4091
4092 /* If the selected architecture includes support for ASEs, enable
4093 generation of code for them. */
4094 if (file_mips_opts.mips16 == -1)
4095 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4096 if (file_mips_opts.micromips == -1)
4097 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4098 ? 1 : 0;
4099
4100 if (mips_nan2008 == -1)
4101 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4102 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4103 as_fatal (_("`%s' does not support legacy NaN"),
4104 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4105
4106 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4107 being selected implicitly. */
4108 if (file_mips_opts.fp != 64)
4109 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4110
4111 /* If the user didn't explicitly select or deselect a particular ASE,
4112 use the default setting for the CPU. */
4113 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4114
4115 /* Set up the current options. These may change throughout assembly. */
4116 mips_opts = file_mips_opts;
4117
4118 mips_check_isa_supports_ases ();
4119 mips_check_options (&file_mips_opts, TRUE);
4120 file_mips_opts_checked = TRUE;
4121
4122 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4123 as_warn (_("could not set architecture and machine"));
4124 }
4125
4126 void
4127 md_assemble (char *str)
4128 {
4129 struct mips_cl_insn insn;
4130 bfd_reloc_code_real_type unused_reloc[3]
4131 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
4132
4133 file_mips_check_options ();
4134
4135 imm_expr.X_op = O_absent;
4136 offset_expr.X_op = O_absent;
4137 offset_reloc[0] = BFD_RELOC_UNUSED;
4138 offset_reloc[1] = BFD_RELOC_UNUSED;
4139 offset_reloc[2] = BFD_RELOC_UNUSED;
4140
4141 mips_mark_labels ();
4142 mips_assembling_insn = TRUE;
4143 clear_insn_error ();
4144
4145 if (mips_opts.mips16)
4146 mips16_ip (str, &insn);
4147 else
4148 {
4149 mips_ip (str, &insn);
4150 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4151 str, insn.insn_opcode));
4152 }
4153
4154 if (insn_error.msg)
4155 report_insn_error (str);
4156 else if (insn.insn_mo->pinfo == INSN_MACRO)
4157 {
4158 macro_start ();
4159 if (mips_opts.mips16)
4160 mips16_macro (&insn);
4161 else
4162 macro (&insn, str);
4163 macro_end ();
4164 }
4165 else
4166 {
4167 if (offset_expr.X_op != O_absent)
4168 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
4169 else
4170 append_insn (&insn, NULL, unused_reloc, FALSE);
4171 }
4172
4173 mips_assembling_insn = FALSE;
4174 }
4175
4176 /* Convenience functions for abstracting away the differences between
4177 MIPS16 and non-MIPS16 relocations. */
4178
4179 static inline bfd_boolean
4180 mips16_reloc_p (bfd_reloc_code_real_type reloc)
4181 {
4182 switch (reloc)
4183 {
4184 case BFD_RELOC_MIPS16_JMP:
4185 case BFD_RELOC_MIPS16_GPREL:
4186 case BFD_RELOC_MIPS16_GOT16:
4187 case BFD_RELOC_MIPS16_CALL16:
4188 case BFD_RELOC_MIPS16_HI16_S:
4189 case BFD_RELOC_MIPS16_HI16:
4190 case BFD_RELOC_MIPS16_LO16:
4191 case BFD_RELOC_MIPS16_16_PCREL_S1:
4192 return TRUE;
4193
4194 default:
4195 return FALSE;
4196 }
4197 }
4198
4199 static inline bfd_boolean
4200 micromips_reloc_p (bfd_reloc_code_real_type reloc)
4201 {
4202 switch (reloc)
4203 {
4204 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4205 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4206 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4207 case BFD_RELOC_MICROMIPS_GPREL16:
4208 case BFD_RELOC_MICROMIPS_JMP:
4209 case BFD_RELOC_MICROMIPS_HI16:
4210 case BFD_RELOC_MICROMIPS_HI16_S:
4211 case BFD_RELOC_MICROMIPS_LO16:
4212 case BFD_RELOC_MICROMIPS_LITERAL:
4213 case BFD_RELOC_MICROMIPS_GOT16:
4214 case BFD_RELOC_MICROMIPS_CALL16:
4215 case BFD_RELOC_MICROMIPS_GOT_HI16:
4216 case BFD_RELOC_MICROMIPS_GOT_LO16:
4217 case BFD_RELOC_MICROMIPS_CALL_HI16:
4218 case BFD_RELOC_MICROMIPS_CALL_LO16:
4219 case BFD_RELOC_MICROMIPS_SUB:
4220 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4221 case BFD_RELOC_MICROMIPS_GOT_OFST:
4222 case BFD_RELOC_MICROMIPS_GOT_DISP:
4223 case BFD_RELOC_MICROMIPS_HIGHEST:
4224 case BFD_RELOC_MICROMIPS_HIGHER:
4225 case BFD_RELOC_MICROMIPS_SCN_DISP:
4226 case BFD_RELOC_MICROMIPS_JALR:
4227 return TRUE;
4228
4229 default:
4230 return FALSE;
4231 }
4232 }
4233
4234 static inline bfd_boolean
4235 jmp_reloc_p (bfd_reloc_code_real_type reloc)
4236 {
4237 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4238 }
4239
4240 static inline bfd_boolean
4241 b_reloc_p (bfd_reloc_code_real_type reloc)
4242 {
4243 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4244 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4245 || reloc == BFD_RELOC_16_PCREL_S2
4246 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
4247 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4248 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4249 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4250 }
4251
4252 static inline bfd_boolean
4253 got16_reloc_p (bfd_reloc_code_real_type reloc)
4254 {
4255 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
4256 || reloc == BFD_RELOC_MICROMIPS_GOT16);
4257 }
4258
4259 static inline bfd_boolean
4260 hi16_reloc_p (bfd_reloc_code_real_type reloc)
4261 {
4262 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
4263 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
4264 }
4265
4266 static inline bfd_boolean
4267 lo16_reloc_p (bfd_reloc_code_real_type reloc)
4268 {
4269 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
4270 || reloc == BFD_RELOC_MICROMIPS_LO16);
4271 }
4272
4273 static inline bfd_boolean
4274 jalr_reloc_p (bfd_reloc_code_real_type reloc)
4275 {
4276 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
4277 }
4278
4279 static inline bfd_boolean
4280 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4281 {
4282 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4283 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4284 }
4285
4286 /* Return true if RELOC is a PC-relative relocation that does not have
4287 full address range. */
4288
4289 static inline bfd_boolean
4290 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4291 {
4292 switch (reloc)
4293 {
4294 case BFD_RELOC_16_PCREL_S2:
4295 case BFD_RELOC_MIPS16_16_PCREL_S1:
4296 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4297 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4298 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4299 case BFD_RELOC_MIPS_21_PCREL_S2:
4300 case BFD_RELOC_MIPS_26_PCREL_S2:
4301 case BFD_RELOC_MIPS_18_PCREL_S3:
4302 case BFD_RELOC_MIPS_19_PCREL_S2:
4303 return TRUE;
4304
4305 case BFD_RELOC_32_PCREL:
4306 case BFD_RELOC_HI16_S_PCREL:
4307 case BFD_RELOC_LO16_PCREL:
4308 return HAVE_64BIT_ADDRESSES;
4309
4310 default:
4311 return FALSE;
4312 }
4313 }
4314
4315 /* Return true if the given relocation might need a matching %lo().
4316 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4317 need a matching %lo() when applied to local symbols. */
4318
4319 static inline bfd_boolean
4320 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
4321 {
4322 return (HAVE_IN_PLACE_ADDENDS
4323 && (hi16_reloc_p (reloc)
4324 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4325 all GOT16 relocations evaluate to "G". */
4326 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4327 }
4328
4329 /* Return the type of %lo() reloc needed by RELOC, given that
4330 reloc_needs_lo_p. */
4331
4332 static inline bfd_reloc_code_real_type
4333 matching_lo_reloc (bfd_reloc_code_real_type reloc)
4334 {
4335 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4336 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4337 : BFD_RELOC_LO16));
4338 }
4339
4340 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
4341 relocation. */
4342
4343 static inline bfd_boolean
4344 fixup_has_matching_lo_p (fixS *fixp)
4345 {
4346 return (fixp->fx_next != NULL
4347 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
4348 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4349 && fixp->fx_offset == fixp->fx_next->fx_offset);
4350 }
4351
4352 /* Move all labels in LABELS to the current insertion point. TEXT_P
4353 says whether the labels refer to text or data. */
4354
4355 static void
4356 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
4357 {
4358 struct insn_label_list *l;
4359 valueT val;
4360
4361 for (l = labels; l != NULL; l = l->next)
4362 {
4363 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
4364 symbol_set_frag (l->label, frag_now);
4365 val = (valueT) frag_now_fix ();
4366 /* MIPS16/microMIPS text labels are stored as odd. */
4367 if (text_p && HAVE_CODE_COMPRESSION)
4368 ++val;
4369 S_SET_VALUE (l->label, val);
4370 }
4371 }
4372
4373 /* Move all labels in insn_labels to the current insertion point
4374 and treat them as text labels. */
4375
4376 static void
4377 mips_move_text_labels (void)
4378 {
4379 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4380 }
4381
4382 /* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4383
4384 static bfd_boolean
4385 s_is_linkonce (symbolS *sym, segT from_seg)
4386 {
4387 bfd_boolean linkonce = FALSE;
4388 segT symseg = S_GET_SEGMENT (sym);
4389
4390 if (symseg != from_seg && !S_IS_LOCAL (sym))
4391 {
4392 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4393 linkonce = TRUE;
4394 /* The GNU toolchain uses an extension for ELF: a section
4395 beginning with the magic string .gnu.linkonce is a
4396 linkonce section. */
4397 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4398 sizeof ".gnu.linkonce" - 1) == 0)
4399 linkonce = TRUE;
4400 }
4401 return linkonce;
4402 }
4403
4404 /* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
4405 linker to handle them specially, such as generating jalx instructions
4406 when needed. We also make them odd for the duration of the assembly,
4407 in order to generate the right sort of code. We will make them even
4408 in the adjust_symtab routine, while leaving them marked. This is
4409 convenient for the debugger and the disassembler. The linker knows
4410 to make them odd again. */
4411
4412 static void
4413 mips_compressed_mark_label (symbolS *label)
4414 {
4415 gas_assert (HAVE_CODE_COMPRESSION);
4416
4417 if (mips_opts.mips16)
4418 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4419 else
4420 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
4421 if ((S_GET_VALUE (label) & 1) == 0
4422 /* Don't adjust the address if the label is global or weak, or
4423 in a link-once section, since we'll be emitting symbol reloc
4424 references to it which will be patched up by the linker, and
4425 the final value of the symbol may or may not be MIPS16/microMIPS. */
4426 && !S_IS_WEAK (label)
4427 && !S_IS_EXTERNAL (label)
4428 && !s_is_linkonce (label, now_seg))
4429 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4430 }
4431
4432 /* Mark preceding MIPS16 or microMIPS instruction labels. */
4433
4434 static void
4435 mips_compressed_mark_labels (void)
4436 {
4437 struct insn_label_list *l;
4438
4439 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4440 mips_compressed_mark_label (l->label);
4441 }
4442
4443 /* End the current frag. Make it a variant frag and record the
4444 relaxation info. */
4445
4446 static void
4447 relax_close_frag (void)
4448 {
4449 mips_macro_warning.first_frag = frag_now;
4450 frag_var (rs_machine_dependent, 0, 0,
4451 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4452 mips_pic != NO_PIC),
4453 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4454
4455 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4456 mips_relax.first_fixup = 0;
4457 }
4458
4459 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
4460 See the comment above RELAX_ENCODE for more details. */
4461
4462 static void
4463 relax_start (symbolS *symbol)
4464 {
4465 gas_assert (mips_relax.sequence == 0);
4466 mips_relax.sequence = 1;
4467 mips_relax.symbol = symbol;
4468 }
4469
4470 /* Start generating the second version of a relaxable sequence.
4471 See the comment above RELAX_ENCODE for more details. */
4472
4473 static void
4474 relax_switch (void)
4475 {
4476 gas_assert (mips_relax.sequence == 1);
4477 mips_relax.sequence = 2;
4478 }
4479
4480 /* End the current relaxable sequence. */
4481
4482 static void
4483 relax_end (void)
4484 {
4485 gas_assert (mips_relax.sequence == 2);
4486 relax_close_frag ();
4487 mips_relax.sequence = 0;
4488 }
4489
4490 /* Return true if IP is a delayed branch or jump. */
4491
4492 static inline bfd_boolean
4493 delayed_branch_p (const struct mips_cl_insn *ip)
4494 {
4495 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4496 | INSN_COND_BRANCH_DELAY
4497 | INSN_COND_BRANCH_LIKELY)) != 0;
4498 }
4499
4500 /* Return true if IP is a compact branch or jump. */
4501
4502 static inline bfd_boolean
4503 compact_branch_p (const struct mips_cl_insn *ip)
4504 {
4505 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4506 | INSN2_COND_BRANCH)) != 0;
4507 }
4508
4509 /* Return true if IP is an unconditional branch or jump. */
4510
4511 static inline bfd_boolean
4512 uncond_branch_p (const struct mips_cl_insn *ip)
4513 {
4514 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
4515 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
4516 }
4517
4518 /* Return true if IP is a branch-likely instruction. */
4519
4520 static inline bfd_boolean
4521 branch_likely_p (const struct mips_cl_insn *ip)
4522 {
4523 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4524 }
4525
4526 /* Return the type of nop that should be used to fill the delay slot
4527 of delayed branch IP. */
4528
4529 static struct mips_cl_insn *
4530 get_delay_slot_nop (const struct mips_cl_insn *ip)
4531 {
4532 if (mips_opts.micromips
4533 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4534 return &micromips_nop32_insn;
4535 return NOP_INSN;
4536 }
4537
4538 /* Return a mask that has bit N set if OPCODE reads the register(s)
4539 in operand N. */
4540
4541 static unsigned int
4542 insn_read_mask (const struct mips_opcode *opcode)
4543 {
4544 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4545 }
4546
4547 /* Return a mask that has bit N set if OPCODE writes to the register(s)
4548 in operand N. */
4549
4550 static unsigned int
4551 insn_write_mask (const struct mips_opcode *opcode)
4552 {
4553 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4554 }
4555
4556 /* Return a mask of the registers specified by operand OPERAND of INSN.
4557 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4558 is set. */
4559
4560 static unsigned int
4561 operand_reg_mask (const struct mips_cl_insn *insn,
4562 const struct mips_operand *operand,
4563 unsigned int type_mask)
4564 {
4565 unsigned int uval, vsel;
4566
4567 switch (operand->type)
4568 {
4569 case OP_INT:
4570 case OP_MAPPED_INT:
4571 case OP_MSB:
4572 case OP_PCREL:
4573 case OP_PERF_REG:
4574 case OP_ADDIUSP_INT:
4575 case OP_ENTRY_EXIT_LIST:
4576 case OP_REPEAT_DEST_REG:
4577 case OP_REPEAT_PREV_REG:
4578 case OP_PC:
4579 case OP_VU0_SUFFIX:
4580 case OP_VU0_MATCH_SUFFIX:
4581 case OP_IMM_INDEX:
4582 abort ();
4583
4584 case OP_REG28:
4585 return 1 << 28;
4586
4587 case OP_REG:
4588 case OP_OPTIONAL_REG:
4589 {
4590 const struct mips_reg_operand *reg_op;
4591
4592 reg_op = (const struct mips_reg_operand *) operand;
4593 if (!(type_mask & (1 << reg_op->reg_type)))
4594 return 0;
4595 uval = insn_extract_operand (insn, operand);
4596 return 1 << mips_decode_reg_operand (reg_op, uval);
4597 }
4598
4599 case OP_REG_PAIR:
4600 {
4601 const struct mips_reg_pair_operand *pair_op;
4602
4603 pair_op = (const struct mips_reg_pair_operand *) operand;
4604 if (!(type_mask & (1 << pair_op->reg_type)))
4605 return 0;
4606 uval = insn_extract_operand (insn, operand);
4607 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4608 }
4609
4610 case OP_CLO_CLZ_DEST:
4611 if (!(type_mask & (1 << OP_REG_GP)))
4612 return 0;
4613 uval = insn_extract_operand (insn, operand);
4614 return (1 << (uval & 31)) | (1 << (uval >> 5));
4615
4616 case OP_SAME_RS_RT:
4617 if (!(type_mask & (1 << OP_REG_GP)))
4618 return 0;
4619 uval = insn_extract_operand (insn, operand);
4620 gas_assert ((uval & 31) == (uval >> 5));
4621 return 1 << (uval & 31);
4622
4623 case OP_CHECK_PREV:
4624 case OP_NON_ZERO_REG:
4625 if (!(type_mask & (1 << OP_REG_GP)))
4626 return 0;
4627 uval = insn_extract_operand (insn, operand);
4628 return 1 << (uval & 31);
4629
4630 case OP_LWM_SWM_LIST:
4631 abort ();
4632
4633 case OP_SAVE_RESTORE_LIST:
4634 abort ();
4635
4636 case OP_MDMX_IMM_REG:
4637 if (!(type_mask & (1 << OP_REG_VEC)))
4638 return 0;
4639 uval = insn_extract_operand (insn, operand);
4640 vsel = uval >> 5;
4641 if ((vsel & 0x18) == 0x18)
4642 return 0;
4643 return 1 << (uval & 31);
4644
4645 case OP_REG_INDEX:
4646 if (!(type_mask & (1 << OP_REG_GP)))
4647 return 0;
4648 return 1 << insn_extract_operand (insn, operand);
4649 }
4650 abort ();
4651 }
4652
4653 /* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4654 where bit N of OPNO_MASK is set if operand N should be included.
4655 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4656 is set. */
4657
4658 static unsigned int
4659 insn_reg_mask (const struct mips_cl_insn *insn,
4660 unsigned int type_mask, unsigned int opno_mask)
4661 {
4662 unsigned int opno, reg_mask;
4663
4664 opno = 0;
4665 reg_mask = 0;
4666 while (opno_mask != 0)
4667 {
4668 if (opno_mask & 1)
4669 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4670 opno_mask >>= 1;
4671 opno += 1;
4672 }
4673 return reg_mask;
4674 }
4675
4676 /* Return the mask of core registers that IP reads. */
4677
4678 static unsigned int
4679 gpr_read_mask (const struct mips_cl_insn *ip)
4680 {
4681 unsigned long pinfo, pinfo2;
4682 unsigned int mask;
4683
4684 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4685 pinfo = ip->insn_mo->pinfo;
4686 pinfo2 = ip->insn_mo->pinfo2;
4687 if (pinfo & INSN_UDI)
4688 {
4689 /* UDI instructions have traditionally been assumed to read RS
4690 and RT. */
4691 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4692 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4693 }
4694 if (pinfo & INSN_READ_GPR_24)
4695 mask |= 1 << 24;
4696 if (pinfo2 & INSN2_READ_GPR_16)
4697 mask |= 1 << 16;
4698 if (pinfo2 & INSN2_READ_SP)
4699 mask |= 1 << SP;
4700 if (pinfo2 & INSN2_READ_GPR_31)
4701 mask |= 1 << 31;
4702 /* Don't include register 0. */
4703 return mask & ~1;
4704 }
4705
4706 /* Return the mask of core registers that IP writes. */
4707
4708 static unsigned int
4709 gpr_write_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_write_mask (ip->insn_mo));
4715 pinfo = ip->insn_mo->pinfo;
4716 pinfo2 = ip->insn_mo->pinfo2;
4717 if (pinfo & INSN_WRITE_GPR_24)
4718 mask |= 1 << 24;
4719 if (pinfo & INSN_WRITE_GPR_31)
4720 mask |= 1 << 31;
4721 if (pinfo & INSN_UDI)
4722 /* UDI instructions have traditionally been assumed to write to RD. */
4723 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4724 if (pinfo2 & INSN2_WRITE_SP)
4725 mask |= 1 << SP;
4726 /* Don't include register 0. */
4727 return mask & ~1;
4728 }
4729
4730 /* Return the mask of floating-point registers that IP reads. */
4731
4732 static unsigned int
4733 fpr_read_mask (const struct mips_cl_insn *ip)
4734 {
4735 unsigned long pinfo;
4736 unsigned int mask;
4737
4738 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4739 | (1 << OP_REG_MSA)),
4740 insn_read_mask (ip->insn_mo));
4741 pinfo = ip->insn_mo->pinfo;
4742 /* Conservatively treat all operands to an FP_D instruction are doubles.
4743 (This is overly pessimistic for things like cvt.d.s.) */
4744 if (FPR_SIZE != 64 && (pinfo & FP_D))
4745 mask |= mask << 1;
4746 return mask;
4747 }
4748
4749 /* Return the mask of floating-point registers that IP writes. */
4750
4751 static unsigned int
4752 fpr_write_mask (const struct mips_cl_insn *ip)
4753 {
4754 unsigned long pinfo;
4755 unsigned int mask;
4756
4757 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4758 | (1 << OP_REG_MSA)),
4759 insn_write_mask (ip->insn_mo));
4760 pinfo = ip->insn_mo->pinfo;
4761 /* Conservatively treat all operands to an FP_D instruction are doubles.
4762 (This is overly pessimistic for things like cvt.s.d.) */
4763 if (FPR_SIZE != 64 && (pinfo & FP_D))
4764 mask |= mask << 1;
4765 return mask;
4766 }
4767
4768 /* Operand OPNUM of INSN is an odd-numbered floating-point register.
4769 Check whether that is allowed. */
4770
4771 static bfd_boolean
4772 mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4773 {
4774 const char *s = insn->name;
4775 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4776 || FPR_SIZE == 64)
4777 && mips_opts.oddspreg;
4778
4779 if (insn->pinfo == INSN_MACRO)
4780 /* Let a macro pass, we'll catch it later when it is expanded. */
4781 return TRUE;
4782
4783 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4784 otherwise it depends on oddspreg. */
4785 if ((insn->pinfo & FP_S)
4786 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
4787 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
4788 return FPR_SIZE == 32 || oddspreg;
4789
4790 /* Allow odd registers for single-precision ops and double-precision if the
4791 floating-point registers are 64-bit wide. */
4792 switch (insn->pinfo & (FP_S | FP_D))
4793 {
4794 case FP_S:
4795 case 0:
4796 return oddspreg;
4797 case FP_D:
4798 return FPR_SIZE == 64;
4799 default:
4800 break;
4801 }
4802
4803 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4804 s = strchr (insn->name, '.');
4805 if (s != NULL && opnum == 2)
4806 s = strchr (s + 1, '.');
4807 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4808 return oddspreg;
4809
4810 return FPR_SIZE == 64;
4811 }
4812
4813 /* Information about an instruction argument that we're trying to match. */
4814 struct mips_arg_info
4815 {
4816 /* The instruction so far. */
4817 struct mips_cl_insn *insn;
4818
4819 /* The first unconsumed operand token. */
4820 struct mips_operand_token *token;
4821
4822 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4823 int opnum;
4824
4825 /* The 1-based argument number, for error reporting. This does not
4826 count elided optional registers, etc.. */
4827 int argnum;
4828
4829 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4830 unsigned int last_regno;
4831
4832 /* If the first operand was an OP_REG, this is the register that it
4833 specified, otherwise it is ILLEGAL_REG. */
4834 unsigned int dest_regno;
4835
4836 /* The value of the last OP_INT operand. Only used for OP_MSB,
4837 where it gives the lsb position. */
4838 unsigned int last_op_int;
4839
4840 /* If true, match routines should assume that no later instruction
4841 alternative matches and should therefore be as accommodating as
4842 possible. Match routines should not report errors if something
4843 is only invalid for !LAX_MATCH. */
4844 bfd_boolean lax_match;
4845
4846 /* True if a reference to the current AT register was seen. */
4847 bfd_boolean seen_at;
4848 };
4849
4850 /* Record that the argument is out of range. */
4851
4852 static void
4853 match_out_of_range (struct mips_arg_info *arg)
4854 {
4855 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4856 }
4857
4858 /* Record that the argument isn't constant but needs to be. */
4859
4860 static void
4861 match_not_constant (struct mips_arg_info *arg)
4862 {
4863 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4864 arg->argnum);
4865 }
4866
4867 /* Try to match an OT_CHAR token for character CH. Consume the token
4868 and return true on success, otherwise return false. */
4869
4870 static bfd_boolean
4871 match_char (struct mips_arg_info *arg, char ch)
4872 {
4873 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4874 {
4875 ++arg->token;
4876 if (ch == ',')
4877 arg->argnum += 1;
4878 return TRUE;
4879 }
4880 return FALSE;
4881 }
4882
4883 /* Try to get an expression from the next tokens in ARG. Consume the
4884 tokens and return true on success, storing the expression value in
4885 VALUE and relocation types in R. */
4886
4887 static bfd_boolean
4888 match_expression (struct mips_arg_info *arg, expressionS *value,
4889 bfd_reloc_code_real_type *r)
4890 {
4891 /* If the next token is a '(' that was parsed as being part of a base
4892 expression, assume we have an elided offset. The later match will fail
4893 if this turns out to be wrong. */
4894 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4895 {
4896 value->X_op = O_constant;
4897 value->X_add_number = 0;
4898 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4899 return TRUE;
4900 }
4901
4902 /* Reject register-based expressions such as "0+$2" and "(($2))".
4903 For plain registers the default error seems more appropriate. */
4904 if (arg->token->type == OT_INTEGER
4905 && arg->token->u.integer.value.X_op == O_register)
4906 {
4907 set_insn_error (arg->argnum, _("register value used as expression"));
4908 return FALSE;
4909 }
4910
4911 if (arg->token->type == OT_INTEGER)
4912 {
4913 *value = arg->token->u.integer.value;
4914 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4915 ++arg->token;
4916 return TRUE;
4917 }
4918
4919 set_insn_error_i
4920 (arg->argnum, _("operand %d must be an immediate expression"),
4921 arg->argnum);
4922 return FALSE;
4923 }
4924
4925 /* Try to get a constant expression from the next tokens in ARG. Consume
4926 the tokens and return true on success, storing the constant value
4927 in *VALUE. */
4928
4929 static bfd_boolean
4930 match_const_int (struct mips_arg_info *arg, offsetT *value)
4931 {
4932 expressionS ex;
4933 bfd_reloc_code_real_type r[3];
4934
4935 if (!match_expression (arg, &ex, r))
4936 return FALSE;
4937
4938 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
4939 *value = ex.X_add_number;
4940 else
4941 {
4942 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
4943 match_out_of_range (arg);
4944 else
4945 match_not_constant (arg);
4946 return FALSE;
4947 }
4948 return TRUE;
4949 }
4950
4951 /* Return the RTYPE_* flags for a register operand of type TYPE that
4952 appears in instruction OPCODE. */
4953
4954 static unsigned int
4955 convert_reg_type (const struct mips_opcode *opcode,
4956 enum mips_reg_operand_type type)
4957 {
4958 switch (type)
4959 {
4960 case OP_REG_GP:
4961 return RTYPE_NUM | RTYPE_GP;
4962
4963 case OP_REG_FP:
4964 /* Allow vector register names for MDMX if the instruction is a 64-bit
4965 FPR load, store or move (including moves to and from GPRs). */
4966 if ((mips_opts.ase & ASE_MDMX)
4967 && (opcode->pinfo & FP_D)
4968 && (opcode->pinfo & (INSN_COPROC_MOVE
4969 | INSN_COPROC_MEMORY_DELAY
4970 | INSN_LOAD_COPROC
4971 | INSN_LOAD_MEMORY
4972 | INSN_STORE_MEMORY)))
4973 return RTYPE_FPU | RTYPE_VEC;
4974 return RTYPE_FPU;
4975
4976 case OP_REG_CCC:
4977 if (opcode->pinfo & (FP_D | FP_S))
4978 return RTYPE_CCC | RTYPE_FCC;
4979 return RTYPE_CCC;
4980
4981 case OP_REG_VEC:
4982 if (opcode->membership & INSN_5400)
4983 return RTYPE_FPU;
4984 return RTYPE_FPU | RTYPE_VEC;
4985
4986 case OP_REG_ACC:
4987 return RTYPE_ACC;
4988
4989 case OP_REG_COPRO:
4990 if (opcode->name[strlen (opcode->name) - 1] == '0')
4991 return RTYPE_NUM | RTYPE_CP0;
4992 return RTYPE_NUM;
4993
4994 case OP_REG_HW:
4995 return RTYPE_NUM;
4996
4997 case OP_REG_VI:
4998 return RTYPE_NUM | RTYPE_VI;
4999
5000 case OP_REG_VF:
5001 return RTYPE_NUM | RTYPE_VF;
5002
5003 case OP_REG_R5900_I:
5004 return RTYPE_R5900_I;
5005
5006 case OP_REG_R5900_Q:
5007 return RTYPE_R5900_Q;
5008
5009 case OP_REG_R5900_R:
5010 return RTYPE_R5900_R;
5011
5012 case OP_REG_R5900_ACC:
5013 return RTYPE_R5900_ACC;
5014
5015 case OP_REG_MSA:
5016 return RTYPE_MSA;
5017
5018 case OP_REG_MSA_CTRL:
5019 return RTYPE_NUM;
5020 }
5021 abort ();
5022 }
5023
5024 /* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5025
5026 static void
5027 check_regno (struct mips_arg_info *arg,
5028 enum mips_reg_operand_type type, unsigned int regno)
5029 {
5030 if (AT && type == OP_REG_GP && regno == AT)
5031 arg->seen_at = TRUE;
5032
5033 if (type == OP_REG_FP
5034 && (regno & 1) != 0
5035 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
5036 {
5037 /* This was a warning prior to introducing O32 FPXX and FP64 support
5038 so maintain a warning for FP32 but raise an error for the new
5039 cases. */
5040 if (FPR_SIZE == 32)
5041 as_warn (_("float register should be even, was %d"), regno);
5042 else
5043 as_bad (_("float register should be even, was %d"), regno);
5044 }
5045
5046 if (type == OP_REG_CCC)
5047 {
5048 const char *name;
5049 size_t length;
5050
5051 name = arg->insn->insn_mo->name;
5052 length = strlen (name);
5053 if ((regno & 1) != 0
5054 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5055 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
5056 as_warn (_("condition code register should be even for %s, was %d"),
5057 name, regno);
5058
5059 if ((regno & 3) != 0
5060 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
5061 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
5062 name, regno);
5063 }
5064 }
5065
5066 /* ARG is a register with symbol value SYMVAL. Try to interpret it as
5067 a register of type TYPE. Return true on success, storing the register
5068 number in *REGNO and warning about any dubious uses. */
5069
5070 static bfd_boolean
5071 match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5072 unsigned int symval, unsigned int *regno)
5073 {
5074 if (type == OP_REG_VEC)
5075 symval = mips_prefer_vec_regno (symval);
5076 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5077 return FALSE;
5078
5079 *regno = symval & RNUM_MASK;
5080 check_regno (arg, type, *regno);
5081 return TRUE;
5082 }
5083
5084 /* Try to interpret the next token in ARG as a register of type TYPE.
5085 Consume the token and return true on success, storing the register
5086 number in *REGNO. Return false on failure. */
5087
5088 static bfd_boolean
5089 match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5090 unsigned int *regno)
5091 {
5092 if (arg->token->type == OT_REG
5093 && match_regno (arg, type, arg->token->u.regno, regno))
5094 {
5095 ++arg->token;
5096 return TRUE;
5097 }
5098 return FALSE;
5099 }
5100
5101 /* Try to interpret the next token in ARG as a range of registers of type TYPE.
5102 Consume the token and return true on success, storing the register numbers
5103 in *REGNO1 and *REGNO2. Return false on failure. */
5104
5105 static bfd_boolean
5106 match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5107 unsigned int *regno1, unsigned int *regno2)
5108 {
5109 if (match_reg (arg, type, regno1))
5110 {
5111 *regno2 = *regno1;
5112 return TRUE;
5113 }
5114 if (arg->token->type == OT_REG_RANGE
5115 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5116 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5117 && *regno1 <= *regno2)
5118 {
5119 ++arg->token;
5120 return TRUE;
5121 }
5122 return FALSE;
5123 }
5124
5125 /* OP_INT matcher. */
5126
5127 static bfd_boolean
5128 match_int_operand (struct mips_arg_info *arg,
5129 const struct mips_operand *operand_base)
5130 {
5131 const struct mips_int_operand *operand;
5132 unsigned int uval;
5133 int min_val, max_val, factor;
5134 offsetT sval;
5135
5136 operand = (const struct mips_int_operand *) operand_base;
5137 factor = 1 << operand->shift;
5138 min_val = mips_int_operand_min (operand);
5139 max_val = mips_int_operand_max (operand);
5140
5141 if (operand_base->lsb == 0
5142 && operand_base->size == 16
5143 && operand->shift == 0
5144 && operand->bias == 0
5145 && (operand->max_val == 32767 || operand->max_val == 65535))
5146 {
5147 /* The operand can be relocated. */
5148 if (!match_expression (arg, &offset_expr, offset_reloc))
5149 return FALSE;
5150
5151 if (offset_expr.X_op == O_big)
5152 {
5153 match_out_of_range (arg);
5154 return FALSE;
5155 }
5156
5157 if (offset_reloc[0] != BFD_RELOC_UNUSED)
5158 /* Relocation operators were used. Accept the argument and
5159 leave the relocation value in offset_expr and offset_relocs
5160 for the caller to process. */
5161 return TRUE;
5162
5163 if (offset_expr.X_op != O_constant)
5164 {
5165 /* Accept non-constant operands if no later alternative matches,
5166 leaving it for the caller to process. */
5167 if (!arg->lax_match)
5168 {
5169 match_not_constant (arg);
5170 return FALSE;
5171 }
5172 offset_reloc[0] = BFD_RELOC_LO16;
5173 return TRUE;
5174 }
5175
5176 /* Clear the global state; we're going to install the operand
5177 ourselves. */
5178 sval = offset_expr.X_add_number;
5179 offset_expr.X_op = O_absent;
5180
5181 /* For compatibility with older assemblers, we accept
5182 0x8000-0xffff as signed 16-bit numbers when only
5183 signed numbers are allowed. */
5184 if (sval > max_val)
5185 {
5186 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5187 if (!arg->lax_match && sval <= max_val)
5188 {
5189 match_out_of_range (arg);
5190 return FALSE;
5191 }
5192 }
5193 }
5194 else
5195 {
5196 if (!match_const_int (arg, &sval))
5197 return FALSE;
5198 }
5199
5200 arg->last_op_int = sval;
5201
5202 if (sval < min_val || sval > max_val || sval % factor)
5203 {
5204 match_out_of_range (arg);
5205 return FALSE;
5206 }
5207
5208 uval = (unsigned int) sval >> operand->shift;
5209 uval -= operand->bias;
5210
5211 /* Handle -mfix-cn63xxp1. */
5212 if (arg->opnum == 1
5213 && mips_fix_cn63xxp1
5214 && !mips_opts.micromips
5215 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5216 switch (uval)
5217 {
5218 case 5:
5219 case 25:
5220 case 26:
5221 case 27:
5222 case 28:
5223 case 29:
5224 case 30:
5225 case 31:
5226 /* These are ok. */
5227 break;
5228
5229 default:
5230 /* The rest must be changed to 28. */
5231 uval = 28;
5232 break;
5233 }
5234
5235 insn_insert_operand (arg->insn, operand_base, uval);
5236 return TRUE;
5237 }
5238
5239 /* OP_MAPPED_INT matcher. */
5240
5241 static bfd_boolean
5242 match_mapped_int_operand (struct mips_arg_info *arg,
5243 const struct mips_operand *operand_base)
5244 {
5245 const struct mips_mapped_int_operand *operand;
5246 unsigned int uval, num_vals;
5247 offsetT sval;
5248
5249 operand = (const struct mips_mapped_int_operand *) operand_base;
5250 if (!match_const_int (arg, &sval))
5251 return FALSE;
5252
5253 num_vals = 1 << operand_base->size;
5254 for (uval = 0; uval < num_vals; uval++)
5255 if (operand->int_map[uval] == sval)
5256 break;
5257 if (uval == num_vals)
5258 {
5259 match_out_of_range (arg);
5260 return FALSE;
5261 }
5262
5263 insn_insert_operand (arg->insn, operand_base, uval);
5264 return TRUE;
5265 }
5266
5267 /* OP_MSB matcher. */
5268
5269 static bfd_boolean
5270 match_msb_operand (struct mips_arg_info *arg,
5271 const struct mips_operand *operand_base)
5272 {
5273 const struct mips_msb_operand *operand;
5274 int min_val, max_val, max_high;
5275 offsetT size, sval, high;
5276
5277 operand = (const struct mips_msb_operand *) operand_base;
5278 min_val = operand->bias;
5279 max_val = min_val + (1 << operand_base->size) - 1;
5280 max_high = operand->opsize;
5281
5282 if (!match_const_int (arg, &size))
5283 return FALSE;
5284
5285 high = size + arg->last_op_int;
5286 sval = operand->add_lsb ? high : size;
5287
5288 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5289 {
5290 match_out_of_range (arg);
5291 return FALSE;
5292 }
5293 insn_insert_operand (arg->insn, operand_base, sval - min_val);
5294 return TRUE;
5295 }
5296
5297 /* OP_REG matcher. */
5298
5299 static bfd_boolean
5300 match_reg_operand (struct mips_arg_info *arg,
5301 const struct mips_operand *operand_base)
5302 {
5303 const struct mips_reg_operand *operand;
5304 unsigned int regno, uval, num_vals;
5305
5306 operand = (const struct mips_reg_operand *) operand_base;
5307 if (!match_reg (arg, operand->reg_type, &regno))
5308 return FALSE;
5309
5310 if (operand->reg_map)
5311 {
5312 num_vals = 1 << operand->root.size;
5313 for (uval = 0; uval < num_vals; uval++)
5314 if (operand->reg_map[uval] == regno)
5315 break;
5316 if (num_vals == uval)
5317 return FALSE;
5318 }
5319 else
5320 uval = regno;
5321
5322 arg->last_regno = regno;
5323 if (arg->opnum == 1)
5324 arg->dest_regno = regno;
5325 insn_insert_operand (arg->insn, operand_base, uval);
5326 return TRUE;
5327 }
5328
5329 /* OP_REG_PAIR matcher. */
5330
5331 static bfd_boolean
5332 match_reg_pair_operand (struct mips_arg_info *arg,
5333 const struct mips_operand *operand_base)
5334 {
5335 const struct mips_reg_pair_operand *operand;
5336 unsigned int regno1, regno2, uval, num_vals;
5337
5338 operand = (const struct mips_reg_pair_operand *) operand_base;
5339 if (!match_reg (arg, operand->reg_type, &regno1)
5340 || !match_char (arg, ',')
5341 || !match_reg (arg, operand->reg_type, &regno2))
5342 return FALSE;
5343
5344 num_vals = 1 << operand_base->size;
5345 for (uval = 0; uval < num_vals; uval++)
5346 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5347 break;
5348 if (uval == num_vals)
5349 return FALSE;
5350
5351 insn_insert_operand (arg->insn, operand_base, uval);
5352 return TRUE;
5353 }
5354
5355 /* OP_PCREL matcher. The caller chooses the relocation type. */
5356
5357 static bfd_boolean
5358 match_pcrel_operand (struct mips_arg_info *arg)
5359 {
5360 bfd_reloc_code_real_type r[3];
5361
5362 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
5363 }
5364
5365 /* OP_PERF_REG matcher. */
5366
5367 static bfd_boolean
5368 match_perf_reg_operand (struct mips_arg_info *arg,
5369 const struct mips_operand *operand)
5370 {
5371 offsetT sval;
5372
5373 if (!match_const_int (arg, &sval))
5374 return FALSE;
5375
5376 if (sval != 0
5377 && (sval != 1
5378 || (mips_opts.arch == CPU_R5900
5379 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5380 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5381 {
5382 set_insn_error (arg->argnum, _("invalid performance register"));
5383 return FALSE;
5384 }
5385
5386 insn_insert_operand (arg->insn, operand, sval);
5387 return TRUE;
5388 }
5389
5390 /* OP_ADDIUSP matcher. */
5391
5392 static bfd_boolean
5393 match_addiusp_operand (struct mips_arg_info *arg,
5394 const struct mips_operand *operand)
5395 {
5396 offsetT sval;
5397 unsigned int uval;
5398
5399 if (!match_const_int (arg, &sval))
5400 return FALSE;
5401
5402 if (sval % 4)
5403 {
5404 match_out_of_range (arg);
5405 return FALSE;
5406 }
5407
5408 sval /= 4;
5409 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
5410 {
5411 match_out_of_range (arg);
5412 return FALSE;
5413 }
5414
5415 uval = (unsigned int) sval;
5416 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5417 insn_insert_operand (arg->insn, operand, uval);
5418 return TRUE;
5419 }
5420
5421 /* OP_CLO_CLZ_DEST matcher. */
5422
5423 static bfd_boolean
5424 match_clo_clz_dest_operand (struct mips_arg_info *arg,
5425 const struct mips_operand *operand)
5426 {
5427 unsigned int regno;
5428
5429 if (!match_reg (arg, OP_REG_GP, &regno))
5430 return FALSE;
5431
5432 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5433 return TRUE;
5434 }
5435
5436 /* OP_CHECK_PREV matcher. */
5437
5438 static bfd_boolean
5439 match_check_prev_operand (struct mips_arg_info *arg,
5440 const struct mips_operand *operand_base)
5441 {
5442 const struct mips_check_prev_operand *operand;
5443 unsigned int regno;
5444
5445 operand = (const struct mips_check_prev_operand *) operand_base;
5446
5447 if (!match_reg (arg, OP_REG_GP, &regno))
5448 return FALSE;
5449
5450 if (!operand->zero_ok && regno == 0)
5451 return FALSE;
5452
5453 if ((operand->less_than_ok && regno < arg->last_regno)
5454 || (operand->greater_than_ok && regno > arg->last_regno)
5455 || (operand->equal_ok && regno == arg->last_regno))
5456 {
5457 arg->last_regno = regno;
5458 insn_insert_operand (arg->insn, operand_base, regno);
5459 return TRUE;
5460 }
5461
5462 return FALSE;
5463 }
5464
5465 /* OP_SAME_RS_RT matcher. */
5466
5467 static bfd_boolean
5468 match_same_rs_rt_operand (struct mips_arg_info *arg,
5469 const struct mips_operand *operand)
5470 {
5471 unsigned int regno;
5472
5473 if (!match_reg (arg, OP_REG_GP, &regno))
5474 return FALSE;
5475
5476 if (regno == 0)
5477 {
5478 set_insn_error (arg->argnum, _("the source register must not be $0"));
5479 return FALSE;
5480 }
5481
5482 arg->last_regno = regno;
5483
5484 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5485 return TRUE;
5486 }
5487
5488 /* OP_LWM_SWM_LIST matcher. */
5489
5490 static bfd_boolean
5491 match_lwm_swm_list_operand (struct mips_arg_info *arg,
5492 const struct mips_operand *operand)
5493 {
5494 unsigned int reglist, sregs, ra, regno1, regno2;
5495 struct mips_arg_info reset;
5496
5497 reglist = 0;
5498 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5499 return FALSE;
5500 do
5501 {
5502 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5503 {
5504 reglist |= 1 << FP;
5505 regno2 = S7;
5506 }
5507 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5508 reset = *arg;
5509 }
5510 while (match_char (arg, ',')
5511 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5512 *arg = reset;
5513
5514 if (operand->size == 2)
5515 {
5516 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5517
5518 s0, ra
5519 s0, s1, ra, s2, s3
5520 s0-s2, ra
5521
5522 and any permutations of these. */
5523 if ((reglist & 0xfff1ffff) != 0x80010000)
5524 return FALSE;
5525
5526 sregs = (reglist >> 17) & 7;
5527 ra = 0;
5528 }
5529 else
5530 {
5531 /* The list must include at least one of ra and s0-sN,
5532 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5533 which are $23 and $30 respectively.) E.g.:
5534
5535 ra
5536 s0
5537 ra, s0, s1, s2
5538 s0-s8
5539 s0-s5, ra
5540
5541 and any permutations of these. */
5542 if ((reglist & 0x3f00ffff) != 0)
5543 return FALSE;
5544
5545 ra = (reglist >> 27) & 0x10;
5546 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5547 }
5548 sregs += 1;
5549 if ((sregs & -sregs) != sregs)
5550 return FALSE;
5551
5552 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5553 return TRUE;
5554 }
5555
5556 /* OP_ENTRY_EXIT_LIST matcher. */
5557
5558 static unsigned int
5559 match_entry_exit_operand (struct mips_arg_info *arg,
5560 const struct mips_operand *operand)
5561 {
5562 unsigned int mask;
5563 bfd_boolean is_exit;
5564
5565 /* The format is the same for both ENTRY and EXIT, but the constraints
5566 are different. */
5567 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5568 mask = (is_exit ? 7 << 3 : 0);
5569 do
5570 {
5571 unsigned int regno1, regno2;
5572 bfd_boolean is_freg;
5573
5574 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5575 is_freg = FALSE;
5576 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5577 is_freg = TRUE;
5578 else
5579 return FALSE;
5580
5581 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5582 {
5583 mask &= ~(7 << 3);
5584 mask |= (5 + regno2) << 3;
5585 }
5586 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5587 mask |= (regno2 - 3) << 3;
5588 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5589 mask |= (regno2 - 15) << 1;
5590 else if (regno1 == RA && regno2 == RA)
5591 mask |= 1;
5592 else
5593 return FALSE;
5594 }
5595 while (match_char (arg, ','));
5596
5597 insn_insert_operand (arg->insn, operand, mask);
5598 return TRUE;
5599 }
5600
5601 /* Encode regular MIPS SAVE/RESTORE instruction operands according to
5602 the argument register mask AMASK, the number of static registers
5603 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5604 respectively, and the frame size FRAME_SIZE. */
5605
5606 static unsigned int
5607 mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5608 unsigned int ra, unsigned int s0, unsigned int s1,
5609 unsigned int frame_size)
5610 {
5611 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5612 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5613 }
5614
5615 /* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5616 argument register mask AMASK, the number of static registers saved
5617 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5618 respectively, and the frame size FRAME_SIZE. */
5619
5620 static unsigned int
5621 mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5622 unsigned int ra, unsigned int s0, unsigned int s1,
5623 unsigned int frame_size)
5624 {
5625 unsigned int args;
5626
5627 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5628 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5629 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5630 | ((frame_size & 0xf0) << 16));
5631 return args;
5632 }
5633
5634 /* OP_SAVE_RESTORE_LIST matcher. */
5635
5636 static bfd_boolean
5637 match_save_restore_list_operand (struct mips_arg_info *arg)
5638 {
5639 unsigned int opcode, args, statics, sregs;
5640 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
5641 unsigned int arg_mask, ra, s0, s1;
5642 offsetT frame_size;
5643
5644 opcode = arg->insn->insn_opcode;
5645 frame_size = 0;
5646 num_frame_sizes = 0;
5647 args = 0;
5648 statics = 0;
5649 sregs = 0;
5650 ra = 0;
5651 s0 = 0;
5652 s1 = 0;
5653 do
5654 {
5655 unsigned int regno1, regno2;
5656
5657 if (arg->token->type == OT_INTEGER)
5658 {
5659 /* Handle the frame size. */
5660 if (!match_const_int (arg, &frame_size))
5661 return FALSE;
5662 num_frame_sizes += 1;
5663 }
5664 else
5665 {
5666 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5667 return FALSE;
5668
5669 while (regno1 <= regno2)
5670 {
5671 if (regno1 >= 4 && regno1 <= 7)
5672 {
5673 if (num_frame_sizes == 0)
5674 /* args $a0-$a3 */
5675 args |= 1 << (regno1 - 4);
5676 else
5677 /* statics $a0-$a3 */
5678 statics |= 1 << (regno1 - 4);
5679 }
5680 else if (regno1 >= 16 && regno1 <= 23)
5681 /* $s0-$s7 */
5682 sregs |= 1 << (regno1 - 16);
5683 else if (regno1 == 30)
5684 /* $s8 */
5685 sregs |= 1 << 8;
5686 else if (regno1 == 31)
5687 /* Add $ra to insn. */
5688 ra = 1;
5689 else
5690 return FALSE;
5691 regno1 += 1;
5692 if (regno1 == 24)
5693 regno1 = 30;
5694 }
5695 }
5696 }
5697 while (match_char (arg, ','));
5698
5699 /* Encode args/statics combination. */
5700 if (args & statics)
5701 return FALSE;
5702 else if (args == 0xf)
5703 /* All $a0-$a3 are args. */
5704 arg_mask = MIPS_SVRS_ALL_ARGS;
5705 else if (statics == 0xf)
5706 /* All $a0-$a3 are statics. */
5707 arg_mask = MIPS_SVRS_ALL_STATICS;
5708 else
5709 {
5710 /* Count arg registers. */
5711 num_args = 0;
5712 while (args & 0x1)
5713 {
5714 args >>= 1;
5715 num_args += 1;
5716 }
5717 if (args != 0)
5718 return FALSE;
5719
5720 /* Count static registers. */
5721 num_statics = 0;
5722 while (statics & 0x8)
5723 {
5724 statics = (statics << 1) & 0xf;
5725 num_statics += 1;
5726 }
5727 if (statics != 0)
5728 return FALSE;
5729
5730 /* Encode args/statics. */
5731 arg_mask = (num_args << 2) | num_statics;
5732 }
5733
5734 /* Encode $s0/$s1. */
5735 if (sregs & (1 << 0)) /* $s0 */
5736 s0 = 1;
5737 if (sregs & (1 << 1)) /* $s1 */
5738 s1 = 1;
5739 sregs >>= 2;
5740
5741 /* Encode $s2-$s8. */
5742 num_sregs = 0;
5743 while (sregs & 1)
5744 {
5745 sregs >>= 1;
5746 num_sregs += 1;
5747 }
5748 if (sregs != 0)
5749 return FALSE;
5750
5751 /* Encode frame size. */
5752 if (num_frame_sizes == 0)
5753 {
5754 set_insn_error (arg->argnum, _("missing frame size"));
5755 return FALSE;
5756 }
5757 if (num_frame_sizes > 1)
5758 {
5759 set_insn_error (arg->argnum, _("frame size specified twice"));
5760 return FALSE;
5761 }
5762 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5763 {
5764 set_insn_error (arg->argnum, _("invalid frame size"));
5765 return FALSE;
5766 }
5767 frame_size /= 8;
5768
5769 /* Finally build the instruction. */
5770 if (mips_opts.mips16)
5771 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5772 frame_size);
5773 else if (!mips_opts.micromips)
5774 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5775 frame_size);
5776 else
5777 abort ();
5778
5779 arg->insn->insn_opcode = opcode;
5780 return TRUE;
5781 }
5782
5783 /* OP_MDMX_IMM_REG matcher. */
5784
5785 static bfd_boolean
5786 match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
5787 const struct mips_operand *operand)
5788 {
5789 unsigned int regno, uval;
5790 bfd_boolean is_qh;
5791 const struct mips_opcode *opcode;
5792
5793 /* The mips_opcode records whether this is an octobyte or quadhalf
5794 instruction. Start out with that bit in place. */
5795 opcode = arg->insn->insn_mo;
5796 uval = mips_extract_operand (operand, opcode->match);
5797 is_qh = (uval != 0);
5798
5799 if (arg->token->type == OT_REG)
5800 {
5801 if ((opcode->membership & INSN_5400)
5802 && strcmp (opcode->name, "rzu.ob") == 0)
5803 {
5804 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5805 arg->argnum);
5806 return FALSE;
5807 }
5808
5809 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5810 return FALSE;
5811 ++arg->token;
5812
5813 /* Check whether this is a vector register or a broadcast of
5814 a single element. */
5815 if (arg->token->type == OT_INTEGER_INDEX)
5816 {
5817 if (arg->token->u.index > (is_qh ? 3 : 7))
5818 {
5819 set_insn_error (arg->argnum, _("invalid element selector"));
5820 return FALSE;
5821 }
5822 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5823 ++arg->token;
5824 }
5825 else
5826 {
5827 /* A full vector. */
5828 if ((opcode->membership & INSN_5400)
5829 && (strcmp (opcode->name, "sll.ob") == 0
5830 || strcmp (opcode->name, "srl.ob") == 0))
5831 {
5832 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5833 arg->argnum);
5834 return FALSE;
5835 }
5836
5837 if (is_qh)
5838 uval |= MDMX_FMTSEL_VEC_QH << 5;
5839 else
5840 uval |= MDMX_FMTSEL_VEC_OB << 5;
5841 }
5842 uval |= regno;
5843 }
5844 else
5845 {
5846 offsetT sval;
5847
5848 if (!match_const_int (arg, &sval))
5849 return FALSE;
5850 if (sval < 0 || sval > 31)
5851 {
5852 match_out_of_range (arg);
5853 return FALSE;
5854 }
5855 uval |= (sval & 31);
5856 if (is_qh)
5857 uval |= MDMX_FMTSEL_IMM_QH << 5;
5858 else
5859 uval |= MDMX_FMTSEL_IMM_OB << 5;
5860 }
5861 insn_insert_operand (arg->insn, operand, uval);
5862 return TRUE;
5863 }
5864
5865 /* OP_IMM_INDEX matcher. */
5866
5867 static bfd_boolean
5868 match_imm_index_operand (struct mips_arg_info *arg,
5869 const struct mips_operand *operand)
5870 {
5871 unsigned int max_val;
5872
5873 if (arg->token->type != OT_INTEGER_INDEX)
5874 return FALSE;
5875
5876 max_val = (1 << operand->size) - 1;
5877 if (arg->token->u.index > max_val)
5878 {
5879 match_out_of_range (arg);
5880 return FALSE;
5881 }
5882 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5883 ++arg->token;
5884 return TRUE;
5885 }
5886
5887 /* OP_REG_INDEX matcher. */
5888
5889 static bfd_boolean
5890 match_reg_index_operand (struct mips_arg_info *arg,
5891 const struct mips_operand *operand)
5892 {
5893 unsigned int regno;
5894
5895 if (arg->token->type != OT_REG_INDEX)
5896 return FALSE;
5897
5898 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5899 return FALSE;
5900
5901 insn_insert_operand (arg->insn, operand, regno);
5902 ++arg->token;
5903 return TRUE;
5904 }
5905
5906 /* OP_PC matcher. */
5907
5908 static bfd_boolean
5909 match_pc_operand (struct mips_arg_info *arg)
5910 {
5911 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5912 {
5913 ++arg->token;
5914 return TRUE;
5915 }
5916 return FALSE;
5917 }
5918
5919 /* OP_REG28 matcher. */
5920
5921 static bfd_boolean
5922 match_reg28_operand (struct mips_arg_info *arg)
5923 {
5924 unsigned int regno;
5925
5926 if (arg->token->type == OT_REG
5927 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5928 && regno == GP)
5929 {
5930 ++arg->token;
5931 return TRUE;
5932 }
5933 return FALSE;
5934 }
5935
5936 /* OP_NON_ZERO_REG matcher. */
5937
5938 static bfd_boolean
5939 match_non_zero_reg_operand (struct mips_arg_info *arg,
5940 const struct mips_operand *operand)
5941 {
5942 unsigned int regno;
5943
5944 if (!match_reg (arg, OP_REG_GP, &regno))
5945 return FALSE;
5946
5947 if (regno == 0)
5948 return FALSE;
5949
5950 arg->last_regno = regno;
5951 insn_insert_operand (arg->insn, operand, regno);
5952 return TRUE;
5953 }
5954
5955 /* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5956 register that we need to match. */
5957
5958 static bfd_boolean
5959 match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
5960 {
5961 unsigned int regno;
5962
5963 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
5964 }
5965
5966 /* Try to match a floating-point constant from ARG for LI.S or LI.D.
5967 LENGTH is the length of the value in bytes (4 for float, 8 for double)
5968 and USING_GPRS says whether the destination is a GPR rather than an FPR.
5969
5970 Return the constant in IMM and OFFSET as follows:
5971
5972 - If the constant should be loaded via memory, set IMM to O_absent and
5973 OFFSET to the memory address.
5974
5975 - Otherwise, if the constant should be loaded into two 32-bit registers,
5976 set IMM to the O_constant to load into the high register and OFFSET
5977 to the corresponding value for the low register.
5978
5979 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5980
5981 These constants only appear as the last operand in an instruction,
5982 and every instruction that accepts them in any variant accepts them
5983 in all variants. This means we don't have to worry about backing out
5984 any changes if the instruction does not match. We just match
5985 unconditionally and report an error if the constant is invalid. */
5986
5987 static bfd_boolean
5988 match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5989 expressionS *offset, int length, bfd_boolean using_gprs)
5990 {
5991 char *p;
5992 segT seg, new_seg;
5993 subsegT subseg;
5994 const char *newname;
5995 unsigned char *data;
5996
5997 /* Where the constant is placed is based on how the MIPS assembler
5998 does things:
5999
6000 length == 4 && using_gprs -- immediate value only
6001 length == 8 && using_gprs -- .rdata or immediate value
6002 length == 4 && !using_gprs -- .lit4 or immediate value
6003 length == 8 && !using_gprs -- .lit8 or immediate value
6004
6005 The .lit4 and .lit8 sections are only used if permitted by the
6006 -G argument. */
6007 if (arg->token->type != OT_FLOAT)
6008 {
6009 set_insn_error (arg->argnum, _("floating-point expression required"));
6010 return FALSE;
6011 }
6012
6013 gas_assert (arg->token->u.flt.length == length);
6014 data = arg->token->u.flt.data;
6015 ++arg->token;
6016
6017 /* Handle 32-bit constants for which an immediate value is best. */
6018 if (length == 4
6019 && (using_gprs
6020 || g_switch_value < 4
6021 || (data[0] == 0 && data[1] == 0)
6022 || (data[2] == 0 && data[3] == 0)))
6023 {
6024 imm->X_op = O_constant;
6025 if (!target_big_endian)
6026 imm->X_add_number = bfd_getl32 (data);
6027 else
6028 imm->X_add_number = bfd_getb32 (data);
6029 offset->X_op = O_absent;
6030 return TRUE;
6031 }
6032
6033 /* Handle 64-bit constants for which an immediate value is best. */
6034 if (length == 8
6035 && !mips_disable_float_construction
6036 /* Constants can only be constructed in GPRs and copied to FPRs if the
6037 GPRs are at least as wide as the FPRs or MTHC1 is available.
6038 Unlike most tests for 32-bit floating-point registers this check
6039 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6040 permit 64-bit moves without MXHC1.
6041 Force the constant into memory otherwise. */
6042 && (using_gprs
6043 || GPR_SIZE == 64
6044 || ISA_HAS_MXHC1 (mips_opts.isa)
6045 || FPR_SIZE == 32)
6046 && ((data[0] == 0 && data[1] == 0)
6047 || (data[2] == 0 && data[3] == 0))
6048 && ((data[4] == 0 && data[5] == 0)
6049 || (data[6] == 0 && data[7] == 0)))
6050 {
6051 /* The value is simple enough to load with a couple of instructions.
6052 If using 32-bit registers, set IMM to the high order 32 bits and
6053 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6054 64 bit constant. */
6055 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
6056 {
6057 imm->X_op = O_constant;
6058 offset->X_op = O_constant;
6059 if (!target_big_endian)
6060 {
6061 imm->X_add_number = bfd_getl32 (data + 4);
6062 offset->X_add_number = bfd_getl32 (data);
6063 }
6064 else
6065 {
6066 imm->X_add_number = bfd_getb32 (data);
6067 offset->X_add_number = bfd_getb32 (data + 4);
6068 }
6069 if (offset->X_add_number == 0)
6070 offset->X_op = O_absent;
6071 }
6072 else
6073 {
6074 imm->X_op = O_constant;
6075 if (!target_big_endian)
6076 imm->X_add_number = bfd_getl64 (data);
6077 else
6078 imm->X_add_number = bfd_getb64 (data);
6079 offset->X_op = O_absent;
6080 }
6081 return TRUE;
6082 }
6083
6084 /* Switch to the right section. */
6085 seg = now_seg;
6086 subseg = now_subseg;
6087 if (length == 4)
6088 {
6089 gas_assert (!using_gprs && g_switch_value >= 4);
6090 newname = ".lit4";
6091 }
6092 else
6093 {
6094 if (using_gprs || g_switch_value < 8)
6095 newname = RDATA_SECTION_NAME;
6096 else
6097 newname = ".lit8";
6098 }
6099
6100 new_seg = subseg_new (newname, (subsegT) 0);
6101 bfd_set_section_flags (stdoutput, new_seg,
6102 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6103 frag_align (length == 4 ? 2 : 3, 0, 0);
6104 if (strncmp (TARGET_OS, "elf", 3) != 0)
6105 record_alignment (new_seg, 4);
6106 else
6107 record_alignment (new_seg, length == 4 ? 2 : 3);
6108 if (seg == now_seg)
6109 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
6110
6111 /* Set the argument to the current address in the section. */
6112 imm->X_op = O_absent;
6113 offset->X_op = O_symbol;
6114 offset->X_add_symbol = symbol_temp_new_now ();
6115 offset->X_add_number = 0;
6116
6117 /* Put the floating point number into the section. */
6118 p = frag_more (length);
6119 memcpy (p, data, length);
6120
6121 /* Switch back to the original section. */
6122 subseg_set (seg, subseg);
6123 return TRUE;
6124 }
6125
6126 /* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6127 them. */
6128
6129 static bfd_boolean
6130 match_vu0_suffix_operand (struct mips_arg_info *arg,
6131 const struct mips_operand *operand,
6132 bfd_boolean match_p)
6133 {
6134 unsigned int uval;
6135
6136 /* The operand can be an XYZW mask or a single 2-bit channel index
6137 (with X being 0). */
6138 gas_assert (operand->size == 2 || operand->size == 4);
6139
6140 /* The suffix can be omitted when it is already part of the opcode. */
6141 if (arg->token->type != OT_CHANNELS)
6142 return match_p;
6143
6144 uval = arg->token->u.channels;
6145 if (operand->size == 2)
6146 {
6147 /* Check that a single bit is set and convert it into a 2-bit index. */
6148 if ((uval & -uval) != uval)
6149 return FALSE;
6150 uval = 4 - ffs (uval);
6151 }
6152
6153 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6154 return FALSE;
6155
6156 ++arg->token;
6157 if (!match_p)
6158 insn_insert_operand (arg->insn, operand, uval);
6159 return TRUE;
6160 }
6161
6162 /* Try to match a token from ARG against OPERAND. Consume the token
6163 and return true on success, otherwise return false. */
6164
6165 static bfd_boolean
6166 match_operand (struct mips_arg_info *arg,
6167 const struct mips_operand *operand)
6168 {
6169 switch (operand->type)
6170 {
6171 case OP_INT:
6172 return match_int_operand (arg, operand);
6173
6174 case OP_MAPPED_INT:
6175 return match_mapped_int_operand (arg, operand);
6176
6177 case OP_MSB:
6178 return match_msb_operand (arg, operand);
6179
6180 case OP_REG:
6181 case OP_OPTIONAL_REG:
6182 return match_reg_operand (arg, operand);
6183
6184 case OP_REG_PAIR:
6185 return match_reg_pair_operand (arg, operand);
6186
6187 case OP_PCREL:
6188 return match_pcrel_operand (arg);
6189
6190 case OP_PERF_REG:
6191 return match_perf_reg_operand (arg, operand);
6192
6193 case OP_ADDIUSP_INT:
6194 return match_addiusp_operand (arg, operand);
6195
6196 case OP_CLO_CLZ_DEST:
6197 return match_clo_clz_dest_operand (arg, operand);
6198
6199 case OP_LWM_SWM_LIST:
6200 return match_lwm_swm_list_operand (arg, operand);
6201
6202 case OP_ENTRY_EXIT_LIST:
6203 return match_entry_exit_operand (arg, operand);
6204
6205 case OP_SAVE_RESTORE_LIST:
6206 return match_save_restore_list_operand (arg);
6207
6208 case OP_MDMX_IMM_REG:
6209 return match_mdmx_imm_reg_operand (arg, operand);
6210
6211 case OP_REPEAT_DEST_REG:
6212 return match_tied_reg_operand (arg, arg->dest_regno);
6213
6214 case OP_REPEAT_PREV_REG:
6215 return match_tied_reg_operand (arg, arg->last_regno);
6216
6217 case OP_PC:
6218 return match_pc_operand (arg);
6219
6220 case OP_REG28:
6221 return match_reg28_operand (arg);
6222
6223 case OP_VU0_SUFFIX:
6224 return match_vu0_suffix_operand (arg, operand, FALSE);
6225
6226 case OP_VU0_MATCH_SUFFIX:
6227 return match_vu0_suffix_operand (arg, operand, TRUE);
6228
6229 case OP_IMM_INDEX:
6230 return match_imm_index_operand (arg, operand);
6231
6232 case OP_REG_INDEX:
6233 return match_reg_index_operand (arg, operand);
6234
6235 case OP_SAME_RS_RT:
6236 return match_same_rs_rt_operand (arg, operand);
6237
6238 case OP_CHECK_PREV:
6239 return match_check_prev_operand (arg, operand);
6240
6241 case OP_NON_ZERO_REG:
6242 return match_non_zero_reg_operand (arg, operand);
6243 }
6244 abort ();
6245 }
6246
6247 /* ARG is the state after successfully matching an instruction.
6248 Issue any queued-up warnings. */
6249
6250 static void
6251 check_completed_insn (struct mips_arg_info *arg)
6252 {
6253 if (arg->seen_at)
6254 {
6255 if (AT == ATREG)
6256 as_warn (_("used $at without \".set noat\""));
6257 else
6258 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
6259 }
6260 }
6261
6262 /* Return true if modifying general-purpose register REG needs a delay. */
6263
6264 static bfd_boolean
6265 reg_needs_delay (unsigned int reg)
6266 {
6267 unsigned long prev_pinfo;
6268
6269 prev_pinfo = history[0].insn_mo->pinfo;
6270 if (!mips_opts.noreorder
6271 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
6272 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
6273 && (gpr_write_mask (&history[0]) & (1 << reg)))
6274 return TRUE;
6275
6276 return FALSE;
6277 }
6278
6279 /* Classify an instruction according to the FIX_VR4120_* enumeration.
6280 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6281 by VR4120 errata. */
6282
6283 static unsigned int
6284 classify_vr4120_insn (const char *name)
6285 {
6286 if (strncmp (name, "macc", 4) == 0)
6287 return FIX_VR4120_MACC;
6288 if (strncmp (name, "dmacc", 5) == 0)
6289 return FIX_VR4120_DMACC;
6290 if (strncmp (name, "mult", 4) == 0)
6291 return FIX_VR4120_MULT;
6292 if (strncmp (name, "dmult", 5) == 0)
6293 return FIX_VR4120_DMULT;
6294 if (strstr (name, "div"))
6295 return FIX_VR4120_DIV;
6296 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6297 return FIX_VR4120_MTHILO;
6298 return NUM_FIX_VR4120_CLASSES;
6299 }
6300
6301 #define INSN_ERET 0x42000018
6302 #define INSN_DERET 0x4200001f
6303 #define INSN_DMULT 0x1c
6304 #define INSN_DMULTU 0x1d
6305
6306 /* Return the number of instructions that must separate INSN1 and INSN2,
6307 where INSN1 is the earlier instruction. Return the worst-case value
6308 for any INSN2 if INSN2 is null. */
6309
6310 static unsigned int
6311 insns_between (const struct mips_cl_insn *insn1,
6312 const struct mips_cl_insn *insn2)
6313 {
6314 unsigned long pinfo1, pinfo2;
6315 unsigned int mask;
6316
6317 /* If INFO2 is null, pessimistically assume that all flags are set for
6318 the second instruction. */
6319 pinfo1 = insn1->insn_mo->pinfo;
6320 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
6321
6322 /* For most targets, write-after-read dependencies on the HI and LO
6323 registers must be separated by at least two instructions. */
6324 if (!hilo_interlocks)
6325 {
6326 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6327 return 2;
6328 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6329 return 2;
6330 }
6331
6332 /* If we're working around r7000 errata, there must be two instructions
6333 between an mfhi or mflo and any instruction that uses the result. */
6334 if (mips_7000_hilo_fix
6335 && !mips_opts.micromips
6336 && MF_HILO_INSN (pinfo1)
6337 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
6338 return 2;
6339
6340 /* If we're working around 24K errata, one instruction is required
6341 if an ERET or DERET is followed by a branch instruction. */
6342 if (mips_fix_24k && !mips_opts.micromips)
6343 {
6344 if (insn1->insn_opcode == INSN_ERET
6345 || insn1->insn_opcode == INSN_DERET)
6346 {
6347 if (insn2 == NULL
6348 || insn2->insn_opcode == INSN_ERET
6349 || insn2->insn_opcode == INSN_DERET
6350 || delayed_branch_p (insn2))
6351 return 1;
6352 }
6353 }
6354
6355 /* If we're working around PMC RM7000 errata, there must be three
6356 nops between a dmult and a load instruction. */
6357 if (mips_fix_rm7000 && !mips_opts.micromips)
6358 {
6359 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6360 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6361 {
6362 if (pinfo2 & INSN_LOAD_MEMORY)
6363 return 3;
6364 }
6365 }
6366
6367 /* If working around VR4120 errata, check for combinations that need
6368 a single intervening instruction. */
6369 if (mips_fix_vr4120 && !mips_opts.micromips)
6370 {
6371 unsigned int class1, class2;
6372
6373 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6374 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
6375 {
6376 if (insn2 == NULL)
6377 return 1;
6378 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6379 if (vr4120_conflicts[class1] & (1 << class2))
6380 return 1;
6381 }
6382 }
6383
6384 if (!HAVE_CODE_COMPRESSION)
6385 {
6386 /* Check for GPR or coprocessor load delays. All such delays
6387 are on the RT register. */
6388 /* Itbl support may require additional care here. */
6389 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
6390 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
6391 {
6392 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
6393 return 1;
6394 }
6395
6396 /* Check for generic coprocessor hazards.
6397
6398 This case is not handled very well. There is no special
6399 knowledge of CP0 handling, and the coprocessors other than
6400 the floating point unit are not distinguished at all. */
6401 /* Itbl support may require additional care here. FIXME!
6402 Need to modify this to include knowledge about
6403 user specified delays! */
6404 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
6405 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6406 {
6407 /* Handle cases where INSN1 writes to a known general coprocessor
6408 register. There must be a one instruction delay before INSN2
6409 if INSN2 reads that register, otherwise no delay is needed. */
6410 mask = fpr_write_mask (insn1);
6411 if (mask != 0)
6412 {
6413 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
6414 return 1;
6415 }
6416 else
6417 {
6418 /* Read-after-write dependencies on the control registers
6419 require a two-instruction gap. */
6420 if ((pinfo1 & INSN_WRITE_COND_CODE)
6421 && (pinfo2 & INSN_READ_COND_CODE))
6422 return 2;
6423
6424 /* We don't know exactly what INSN1 does. If INSN2 is
6425 also a coprocessor instruction, assume there must be
6426 a one instruction gap. */
6427 if (pinfo2 & INSN_COP)
6428 return 1;
6429 }
6430 }
6431
6432 /* Check for read-after-write dependencies on the coprocessor
6433 control registers in cases where INSN1 does not need a general
6434 coprocessor delay. This means that INSN1 is a floating point
6435 comparison instruction. */
6436 /* Itbl support may require additional care here. */
6437 else if (!cop_interlocks
6438 && (pinfo1 & INSN_WRITE_COND_CODE)
6439 && (pinfo2 & INSN_READ_COND_CODE))
6440 return 1;
6441 }
6442
6443 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6444 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6445 and pause. */
6446 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6447 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6448 || (insn2 && delayed_branch_p (insn2))))
6449 return 1;
6450
6451 return 0;
6452 }
6453
6454 /* Return the number of nops that would be needed to work around the
6455 VR4130 mflo/mfhi errata if instruction INSN immediately followed
6456 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6457 that are contained within the first IGNORE instructions of HIST. */
6458
6459 static int
6460 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
6461 const struct mips_cl_insn *insn)
6462 {
6463 int i, j;
6464 unsigned int mask;
6465
6466 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6467 are not affected by the errata. */
6468 if (insn != 0
6469 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6470 || strcmp (insn->insn_mo->name, "mtlo") == 0
6471 || strcmp (insn->insn_mo->name, "mthi") == 0))
6472 return 0;
6473
6474 /* Search for the first MFLO or MFHI. */
6475 for (i = 0; i < MAX_VR4130_NOPS; i++)
6476 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
6477 {
6478 /* Extract the destination register. */
6479 mask = gpr_write_mask (&hist[i]);
6480
6481 /* No nops are needed if INSN reads that register. */
6482 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
6483 return 0;
6484
6485 /* ...or if any of the intervening instructions do. */
6486 for (j = 0; j < i; j++)
6487 if (gpr_read_mask (&hist[j]) & mask)
6488 return 0;
6489
6490 if (i >= ignore)
6491 return MAX_VR4130_NOPS - i;
6492 }
6493 return 0;
6494 }
6495
6496 #define BASE_REG_EQ(INSN1, INSN2) \
6497 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
6498 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6499
6500 /* Return the minimum alignment for this store instruction. */
6501
6502 static int
6503 fix_24k_align_to (const struct mips_opcode *mo)
6504 {
6505 if (strcmp (mo->name, "sh") == 0)
6506 return 2;
6507
6508 if (strcmp (mo->name, "swc1") == 0
6509 || strcmp (mo->name, "swc2") == 0
6510 || strcmp (mo->name, "sw") == 0
6511 || strcmp (mo->name, "sc") == 0
6512 || strcmp (mo->name, "s.s") == 0)
6513 return 4;
6514
6515 if (strcmp (mo->name, "sdc1") == 0
6516 || strcmp (mo->name, "sdc2") == 0
6517 || strcmp (mo->name, "s.d") == 0)
6518 return 8;
6519
6520 /* sb, swl, swr */
6521 return 1;
6522 }
6523
6524 struct fix_24k_store_info
6525 {
6526 /* Immediate offset, if any, for this store instruction. */
6527 short off;
6528 /* Alignment required by this store instruction. */
6529 int align_to;
6530 /* True for register offsets. */
6531 int register_offset;
6532 };
6533
6534 /* Comparison function used by qsort. */
6535
6536 static int
6537 fix_24k_sort (const void *a, const void *b)
6538 {
6539 const struct fix_24k_store_info *pos1 = a;
6540 const struct fix_24k_store_info *pos2 = b;
6541
6542 return (pos1->off - pos2->off);
6543 }
6544
6545 /* INSN is a store instruction. Try to record the store information
6546 in STINFO. Return false if the information isn't known. */
6547
6548 static bfd_boolean
6549 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
6550 const struct mips_cl_insn *insn)
6551 {
6552 /* The instruction must have a known offset. */
6553 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6554 return FALSE;
6555
6556 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6557 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6558 return TRUE;
6559 }
6560
6561 /* Return the number of nops that would be needed to work around the 24k
6562 "lost data on stores during refill" errata if instruction INSN
6563 immediately followed the 2 instructions described by HIST.
6564 Ignore hazards that are contained within the first IGNORE
6565 instructions of HIST.
6566
6567 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6568 for the data cache refills and store data. The following describes
6569 the scenario where the store data could be lost.
6570
6571 * A data cache miss, due to either a load or a store, causing fill
6572 data to be supplied by the memory subsystem
6573 * The first three doublewords of fill data are returned and written
6574 into the cache
6575 * A sequence of four stores occurs in consecutive cycles around the
6576 final doubleword of the fill:
6577 * Store A
6578 * Store B
6579 * Store C
6580 * Zero, One or more instructions
6581 * Store D
6582
6583 The four stores A-D must be to different doublewords of the line that
6584 is being filled. The fourth instruction in the sequence above permits
6585 the fill of the final doubleword to be transferred from the FSB into
6586 the cache. In the sequence above, the stores may be either integer
6587 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6588 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6589 different doublewords on the line. If the floating point unit is
6590 running in 1:2 mode, it is not possible to create the sequence above
6591 using only floating point store instructions.
6592
6593 In this case, the cache line being filled is incorrectly marked
6594 invalid, thereby losing the data from any store to the line that
6595 occurs between the original miss and the completion of the five
6596 cycle sequence shown above.
6597
6598 The workarounds are:
6599
6600 * Run the data cache in write-through mode.
6601 * Insert a non-store instruction between
6602 Store A and Store B or Store B and Store C. */
6603
6604 static int
6605 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
6606 const struct mips_cl_insn *insn)
6607 {
6608 struct fix_24k_store_info pos[3];
6609 int align, i, base_offset;
6610
6611 if (ignore >= 2)
6612 return 0;
6613
6614 /* If the previous instruction wasn't a store, there's nothing to
6615 worry about. */
6616 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6617 return 0;
6618
6619 /* If the instructions after the previous one are unknown, we have
6620 to assume the worst. */
6621 if (!insn)
6622 return 1;
6623
6624 /* Check whether we are dealing with three consecutive stores. */
6625 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6626 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6627 return 0;
6628
6629 /* If we don't know the relationship between the store addresses,
6630 assume the worst. */
6631 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
6632 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6633 return 1;
6634
6635 if (!fix_24k_record_store_info (&pos[0], insn)
6636 || !fix_24k_record_store_info (&pos[1], &hist[0])
6637 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6638 return 1;
6639
6640 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6641
6642 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6643 X bytes and such that the base register + X is known to be aligned
6644 to align bytes. */
6645
6646 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6647 align = 8;
6648 else
6649 {
6650 align = pos[0].align_to;
6651 base_offset = pos[0].off;
6652 for (i = 1; i < 3; i++)
6653 if (align < pos[i].align_to)
6654 {
6655 align = pos[i].align_to;
6656 base_offset = pos[i].off;
6657 }
6658 for (i = 0; i < 3; i++)
6659 pos[i].off -= base_offset;
6660 }
6661
6662 pos[0].off &= ~align + 1;
6663 pos[1].off &= ~align + 1;
6664 pos[2].off &= ~align + 1;
6665
6666 /* If any two stores write to the same chunk, they also write to the
6667 same doubleword. The offsets are still sorted at this point. */
6668 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6669 return 0;
6670
6671 /* A range of at least 9 bytes is needed for the stores to be in
6672 non-overlapping doublewords. */
6673 if (pos[2].off - pos[0].off <= 8)
6674 return 0;
6675
6676 if (pos[2].off - pos[1].off >= 24
6677 || pos[1].off - pos[0].off >= 24
6678 || pos[2].off - pos[0].off >= 32)
6679 return 0;
6680
6681 return 1;
6682 }
6683
6684 /* Return the number of nops that would be needed if instruction INSN
6685 immediately followed the MAX_NOPS instructions given by HIST,
6686 where HIST[0] is the most recent instruction. Ignore hazards
6687 between INSN and the first IGNORE instructions in HIST.
6688
6689 If INSN is null, return the worse-case number of nops for any
6690 instruction. */
6691
6692 static int
6693 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
6694 const struct mips_cl_insn *insn)
6695 {
6696 int i, nops, tmp_nops;
6697
6698 nops = 0;
6699 for (i = ignore; i < MAX_DELAY_NOPS; i++)
6700 {
6701 tmp_nops = insns_between (hist + i, insn) - i;
6702 if (tmp_nops > nops)
6703 nops = tmp_nops;
6704 }
6705
6706 if (mips_fix_vr4130 && !mips_opts.micromips)
6707 {
6708 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
6709 if (tmp_nops > nops)
6710 nops = tmp_nops;
6711 }
6712
6713 if (mips_fix_24k && !mips_opts.micromips)
6714 {
6715 tmp_nops = nops_for_24k (ignore, hist, insn);
6716 if (tmp_nops > nops)
6717 nops = tmp_nops;
6718 }
6719
6720 return nops;
6721 }
6722
6723 /* The variable arguments provide NUM_INSNS extra instructions that
6724 might be added to HIST. Return the largest number of nops that
6725 would be needed after the extended sequence, ignoring hazards
6726 in the first IGNORE instructions. */
6727
6728 static int
6729 nops_for_sequence (int num_insns, int ignore,
6730 const struct mips_cl_insn *hist, ...)
6731 {
6732 va_list args;
6733 struct mips_cl_insn buffer[MAX_NOPS];
6734 struct mips_cl_insn *cursor;
6735 int nops;
6736
6737 va_start (args, hist);
6738 cursor = buffer + num_insns;
6739 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
6740 while (cursor > buffer)
6741 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6742
6743 nops = nops_for_insn (ignore, buffer, NULL);
6744 va_end (args);
6745 return nops;
6746 }
6747
6748 /* Like nops_for_insn, but if INSN is a branch, take into account the
6749 worst-case delay for the branch target. */
6750
6751 static int
6752 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
6753 const struct mips_cl_insn *insn)
6754 {
6755 int nops, tmp_nops;
6756
6757 nops = nops_for_insn (ignore, hist, insn);
6758 if (delayed_branch_p (insn))
6759 {
6760 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
6761 hist, insn, get_delay_slot_nop (insn));
6762 if (tmp_nops > nops)
6763 nops = tmp_nops;
6764 }
6765 else if (compact_branch_p (insn))
6766 {
6767 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
6768 if (tmp_nops > nops)
6769 nops = tmp_nops;
6770 }
6771 return nops;
6772 }
6773
6774 /* Fix NOP issue: Replace nops by "or at,at,zero". */
6775
6776 static void
6777 fix_loongson2f_nop (struct mips_cl_insn * ip)
6778 {
6779 gas_assert (!HAVE_CODE_COMPRESSION);
6780 if (strcmp (ip->insn_mo->name, "nop") == 0)
6781 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6782 }
6783
6784 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6785 jr target pc &= 'hffff_ffff_cfff_ffff. */
6786
6787 static void
6788 fix_loongson2f_jump (struct mips_cl_insn * ip)
6789 {
6790 gas_assert (!HAVE_CODE_COMPRESSION);
6791 if (strcmp (ip->insn_mo->name, "j") == 0
6792 || strcmp (ip->insn_mo->name, "jr") == 0
6793 || strcmp (ip->insn_mo->name, "jalr") == 0)
6794 {
6795 int sreg;
6796 expressionS ep;
6797
6798 if (! mips_opts.at)
6799 return;
6800
6801 sreg = EXTRACT_OPERAND (0, RS, *ip);
6802 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6803 return;
6804
6805 ep.X_op = O_constant;
6806 ep.X_add_number = 0xcfff0000;
6807 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6808 ep.X_add_number = 0xffff;
6809 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6810 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6811 }
6812 }
6813
6814 static void
6815 fix_loongson2f (struct mips_cl_insn * ip)
6816 {
6817 if (mips_fix_loongson2f_nop)
6818 fix_loongson2f_nop (ip);
6819
6820 if (mips_fix_loongson2f_jump)
6821 fix_loongson2f_jump (ip);
6822 }
6823
6824 /* IP is a branch that has a delay slot, and we need to fill it
6825 automatically. Return true if we can do that by swapping IP
6826 with the previous instruction.
6827 ADDRESS_EXPR is an operand of the instruction to be used with
6828 RELOC_TYPE. */
6829
6830 static bfd_boolean
6831 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
6832 bfd_reloc_code_real_type *reloc_type)
6833 {
6834 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
6835 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
6836 unsigned int fpr_read, prev_fpr_write;
6837
6838 /* -O2 and above is required for this optimization. */
6839 if (mips_optimize < 2)
6840 return FALSE;
6841
6842 /* If we have seen .set volatile or .set nomove, don't optimize. */
6843 if (mips_opts.nomove)
6844 return FALSE;
6845
6846 /* We can't swap if the previous instruction's position is fixed. */
6847 if (history[0].fixed_p)
6848 return FALSE;
6849
6850 /* If the previous previous insn was in a .set noreorder, we can't
6851 swap. Actually, the MIPS assembler will swap in this situation.
6852 However, gcc configured -with-gnu-as will generate code like
6853
6854 .set noreorder
6855 lw $4,XXX
6856 .set reorder
6857 INSN
6858 bne $4,$0,foo
6859
6860 in which we can not swap the bne and INSN. If gcc is not configured
6861 -with-gnu-as, it does not output the .set pseudo-ops. */
6862 if (history[1].noreorder_p)
6863 return FALSE;
6864
6865 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6866 This means that the previous instruction was a 4-byte one anyhow. */
6867 if (mips_opts.mips16 && history[0].fixp[0])
6868 return FALSE;
6869
6870 /* If the branch is itself the target of a branch, we can not swap.
6871 We cheat on this; all we check for is whether there is a label on
6872 this instruction. If there are any branches to anything other than
6873 a label, users must use .set noreorder. */
6874 if (seg_info (now_seg)->label_list)
6875 return FALSE;
6876
6877 /* If the previous instruction is in a variant frag other than this
6878 branch's one, we cannot do the swap. This does not apply to
6879 MIPS16 code, which uses variant frags for different purposes. */
6880 if (!mips_opts.mips16
6881 && history[0].frag
6882 && history[0].frag->fr_type == rs_machine_dependent)
6883 return FALSE;
6884
6885 /* We do not swap with instructions that cannot architecturally
6886 be placed in a branch delay slot, such as SYNC or ERET. We
6887 also refrain from swapping with a trap instruction, since it
6888 complicates trap handlers to have the trap instruction be in
6889 a delay slot. */
6890 prev_pinfo = history[0].insn_mo->pinfo;
6891 if (prev_pinfo & INSN_NO_DELAY_SLOT)
6892 return FALSE;
6893
6894 /* Check for conflicts between the branch and the instructions
6895 before the candidate delay slot. */
6896 if (nops_for_insn (0, history + 1, ip) > 0)
6897 return FALSE;
6898
6899 /* Check for conflicts between the swapped sequence and the
6900 target of the branch. */
6901 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6902 return FALSE;
6903
6904 /* If the branch reads a register that the previous
6905 instruction sets, we can not swap. */
6906 gpr_read = gpr_read_mask (ip);
6907 prev_gpr_write = gpr_write_mask (&history[0]);
6908 if (gpr_read & prev_gpr_write)
6909 return FALSE;
6910
6911 fpr_read = fpr_read_mask (ip);
6912 prev_fpr_write = fpr_write_mask (&history[0]);
6913 if (fpr_read & prev_fpr_write)
6914 return FALSE;
6915
6916 /* If the branch writes a register that the previous
6917 instruction sets, we can not swap. */
6918 gpr_write = gpr_write_mask (ip);
6919 if (gpr_write & prev_gpr_write)
6920 return FALSE;
6921
6922 /* If the branch writes a register that the previous
6923 instruction reads, we can not swap. */
6924 prev_gpr_read = gpr_read_mask (&history[0]);
6925 if (gpr_write & prev_gpr_read)
6926 return FALSE;
6927
6928 /* If one instruction sets a condition code and the
6929 other one uses a condition code, we can not swap. */
6930 pinfo = ip->insn_mo->pinfo;
6931 if ((pinfo & INSN_READ_COND_CODE)
6932 && (prev_pinfo & INSN_WRITE_COND_CODE))
6933 return FALSE;
6934 if ((pinfo & INSN_WRITE_COND_CODE)
6935 && (prev_pinfo & INSN_READ_COND_CODE))
6936 return FALSE;
6937
6938 /* If the previous instruction uses the PC, we can not swap. */
6939 prev_pinfo2 = history[0].insn_mo->pinfo2;
6940 if (prev_pinfo2 & INSN2_READ_PC)
6941 return FALSE;
6942
6943 /* If the previous instruction has an incorrect size for a fixed
6944 branch delay slot in microMIPS mode, we cannot swap. */
6945 pinfo2 = ip->insn_mo->pinfo2;
6946 if (mips_opts.micromips
6947 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6948 && insn_length (history) != 2)
6949 return FALSE;
6950 if (mips_opts.micromips
6951 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6952 && insn_length (history) != 4)
6953 return FALSE;
6954
6955 /* On R5900 short loops need to be fixed by inserting a nop in
6956 the branch delay slots.
6957 A short loop can be terminated too early. */
6958 if (mips_opts.arch == CPU_R5900
6959 /* Check if instruction has a parameter, ignore "j $31". */
6960 && (address_expr != NULL)
6961 /* Parameter must be 16 bit. */
6962 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6963 /* Branch to same segment. */
6964 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
6965 /* Branch to same code fragment. */
6966 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
6967 /* Can only calculate branch offset if value is known. */
6968 && symbol_constant_p (address_expr->X_add_symbol)
6969 /* Check if branch is really conditional. */
6970 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6971 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6972 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6973 {
6974 int distance;
6975 /* Check if loop is shorter than 6 instructions including
6976 branch and delay slot. */
6977 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
6978 if (distance <= 20)
6979 {
6980 int i;
6981 int rv;
6982
6983 rv = FALSE;
6984 /* When the loop includes branches or jumps,
6985 it is not a short loop. */
6986 for (i = 0; i < (distance / 4); i++)
6987 {
6988 if ((history[i].cleared_p)
6989 || delayed_branch_p (&history[i]))
6990 {
6991 rv = TRUE;
6992 break;
6993 }
6994 }
6995 if (!rv)
6996 {
6997 /* Insert nop after branch to fix short loop. */
6998 return FALSE;
6999 }
7000 }
7001 }
7002
7003 return TRUE;
7004 }
7005
7006 /* Decide how we should add IP to the instruction stream.
7007 ADDRESS_EXPR is an operand of the instruction to be used with
7008 RELOC_TYPE. */
7009
7010 static enum append_method
7011 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
7012 bfd_reloc_code_real_type *reloc_type)
7013 {
7014 /* The relaxed version of a macro sequence must be inherently
7015 hazard-free. */
7016 if (mips_relax.sequence == 2)
7017 return APPEND_ADD;
7018
7019 /* We must not dabble with instructions in a ".set noreorder" block. */
7020 if (mips_opts.noreorder)
7021 return APPEND_ADD;
7022
7023 /* Otherwise, it's our responsibility to fill branch delay slots. */
7024 if (delayed_branch_p (ip))
7025 {
7026 if (!branch_likely_p (ip)
7027 && can_swap_branch_p (ip, address_expr, reloc_type))
7028 return APPEND_SWAP;
7029
7030 if (mips_opts.mips16
7031 && ISA_SUPPORTS_MIPS16E
7032 && gpr_read_mask (ip) != 0)
7033 return APPEND_ADD_COMPACT;
7034
7035 if (mips_opts.micromips
7036 && ((ip->insn_opcode & 0xffe0) == 0x4580
7037 || (!forced_insn_length
7038 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7039 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7040 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7041 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7042 return APPEND_ADD_COMPACT;
7043
7044 return APPEND_ADD_WITH_NOP;
7045 }
7046
7047 return APPEND_ADD;
7048 }
7049
7050 /* IP is an instruction whose opcode we have just changed, END points
7051 to the end of the opcode table processed. Point IP->insn_mo to the
7052 new opcode's definition. */
7053
7054 static void
7055 find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
7056 {
7057 const struct mips_opcode *mo;
7058
7059 for (mo = ip->insn_mo; mo < end; mo++)
7060 if (mo->pinfo != INSN_MACRO
7061 && (ip->insn_opcode & mo->mask) == mo->match)
7062 {
7063 ip->insn_mo = mo;
7064 return;
7065 }
7066 abort ();
7067 }
7068
7069 /* IP is a MIPS16 instruction whose opcode we have just changed.
7070 Point IP->insn_mo to the new opcode's definition. */
7071
7072 static void
7073 find_altered_mips16_opcode (struct mips_cl_insn *ip)
7074 {
7075 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7076 }
7077
7078 /* IP is a microMIPS instruction whose opcode we have just changed.
7079 Point IP->insn_mo to the new opcode's definition. */
7080
7081 static void
7082 find_altered_micromips_opcode (struct mips_cl_insn *ip)
7083 {
7084 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7085 }
7086
7087 /* For microMIPS macros, we need to generate a local number label
7088 as the target of branches. */
7089 #define MICROMIPS_LABEL_CHAR '\037'
7090 static unsigned long micromips_target_label;
7091 static char micromips_target_name[32];
7092
7093 static char *
7094 micromips_label_name (void)
7095 {
7096 char *p = micromips_target_name;
7097 char symbol_name_temporary[24];
7098 unsigned long l;
7099 int i;
7100
7101 if (*p)
7102 return p;
7103
7104 i = 0;
7105 l = micromips_target_label;
7106 #ifdef LOCAL_LABEL_PREFIX
7107 *p++ = LOCAL_LABEL_PREFIX;
7108 #endif
7109 *p++ = 'L';
7110 *p++ = MICROMIPS_LABEL_CHAR;
7111 do
7112 {
7113 symbol_name_temporary[i++] = l % 10 + '0';
7114 l /= 10;
7115 }
7116 while (l != 0);
7117 while (i > 0)
7118 *p++ = symbol_name_temporary[--i];
7119 *p = '\0';
7120
7121 return micromips_target_name;
7122 }
7123
7124 static void
7125 micromips_label_expr (expressionS *label_expr)
7126 {
7127 label_expr->X_op = O_symbol;
7128 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7129 label_expr->X_add_number = 0;
7130 }
7131
7132 static void
7133 micromips_label_inc (void)
7134 {
7135 micromips_target_label++;
7136 *micromips_target_name = '\0';
7137 }
7138
7139 static void
7140 micromips_add_label (void)
7141 {
7142 symbolS *s;
7143
7144 s = colon (micromips_label_name ());
7145 micromips_label_inc ();
7146 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
7147 }
7148
7149 /* If assembling microMIPS code, then return the microMIPS reloc
7150 corresponding to the requested one if any. Otherwise return
7151 the reloc unchanged. */
7152
7153 static bfd_reloc_code_real_type
7154 micromips_map_reloc (bfd_reloc_code_real_type reloc)
7155 {
7156 static const bfd_reloc_code_real_type relocs[][2] =
7157 {
7158 /* Keep sorted incrementally by the left-hand key. */
7159 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7160 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7161 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7162 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7163 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7164 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7165 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7166 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7167 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7168 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7169 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7170 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7171 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7172 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7173 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7174 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7175 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7176 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7177 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7178 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7179 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7180 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7181 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7182 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7183 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7184 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7185 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7186 };
7187 bfd_reloc_code_real_type r;
7188 size_t i;
7189
7190 if (!mips_opts.micromips)
7191 return reloc;
7192 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7193 {
7194 r = relocs[i][0];
7195 if (r > reloc)
7196 return reloc;
7197 if (r == reloc)
7198 return relocs[i][1];
7199 }
7200 return reloc;
7201 }
7202
7203 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7204 Return true on success, storing the resolved value in RESULT. */
7205
7206 static bfd_boolean
7207 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7208 offsetT *result)
7209 {
7210 switch (reloc)
7211 {
7212 case BFD_RELOC_MIPS_HIGHEST:
7213 case BFD_RELOC_MICROMIPS_HIGHEST:
7214 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7215 return TRUE;
7216
7217 case BFD_RELOC_MIPS_HIGHER:
7218 case BFD_RELOC_MICROMIPS_HIGHER:
7219 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7220 return TRUE;
7221
7222 case BFD_RELOC_HI16_S:
7223 case BFD_RELOC_HI16_S_PCREL:
7224 case BFD_RELOC_MICROMIPS_HI16_S:
7225 case BFD_RELOC_MIPS16_HI16_S:
7226 *result = ((operand + 0x8000) >> 16) & 0xffff;
7227 return TRUE;
7228
7229 case BFD_RELOC_HI16:
7230 case BFD_RELOC_MICROMIPS_HI16:
7231 case BFD_RELOC_MIPS16_HI16:
7232 *result = (operand >> 16) & 0xffff;
7233 return TRUE;
7234
7235 case BFD_RELOC_LO16:
7236 case BFD_RELOC_LO16_PCREL:
7237 case BFD_RELOC_MICROMIPS_LO16:
7238 case BFD_RELOC_MIPS16_LO16:
7239 *result = operand & 0xffff;
7240 return TRUE;
7241
7242 case BFD_RELOC_UNUSED:
7243 *result = operand;
7244 return TRUE;
7245
7246 default:
7247 return FALSE;
7248 }
7249 }
7250
7251 /* Output an instruction. IP is the instruction information.
7252 ADDRESS_EXPR is an operand of the instruction to be used with
7253 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7254 a macro expansion. */
7255
7256 static void
7257 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
7258 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
7259 {
7260 unsigned long prev_pinfo2, pinfo;
7261 bfd_boolean relaxed_branch = FALSE;
7262 enum append_method method;
7263 bfd_boolean relax32;
7264 int branch_disp;
7265
7266 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
7267 fix_loongson2f (ip);
7268
7269 file_ase_mips16 |= mips_opts.mips16;
7270 file_ase_micromips |= mips_opts.micromips;
7271
7272 prev_pinfo2 = history[0].insn_mo->pinfo2;
7273 pinfo = ip->insn_mo->pinfo;
7274
7275 /* Don't raise alarm about `nods' frags as they'll fill in the right
7276 kind of nop in relaxation if required. */
7277 if (mips_opts.micromips
7278 && !expansionp
7279 && !(history[0].frag
7280 && history[0].frag->fr_type == rs_machine_dependent
7281 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7282 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
7283 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7284 && micromips_insn_length (ip->insn_mo) != 2)
7285 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7286 && micromips_insn_length (ip->insn_mo) != 4)))
7287 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
7288 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
7289
7290 if (address_expr == NULL)
7291 ip->complete_p = 1;
7292 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7293 && reloc_type[1] == BFD_RELOC_UNUSED
7294 && reloc_type[2] == BFD_RELOC_UNUSED
7295 && address_expr->X_op == O_constant)
7296 {
7297 switch (*reloc_type)
7298 {
7299 case BFD_RELOC_MIPS_JMP:
7300 {
7301 int shift;
7302
7303 /* Shift is 2, unusually, for microMIPS JALX. */
7304 shift = (mips_opts.micromips
7305 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
7306 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7307 as_bad (_("jump to misaligned address (0x%lx)"),
7308 (unsigned long) address_expr->X_add_number);
7309 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7310 & 0x3ffffff);
7311 ip->complete_p = 1;
7312 }
7313 break;
7314
7315 case BFD_RELOC_MIPS16_JMP:
7316 if ((address_expr->X_add_number & 3) != 0)
7317 as_bad (_("jump to misaligned address (0x%lx)"),
7318 (unsigned long) address_expr->X_add_number);
7319 ip->insn_opcode |=
7320 (((address_expr->X_add_number & 0x7c0000) << 3)
7321 | ((address_expr->X_add_number & 0xf800000) >> 7)
7322 | ((address_expr->X_add_number & 0x3fffc) >> 2));
7323 ip->complete_p = 1;
7324 break;
7325
7326 case BFD_RELOC_16_PCREL_S2:
7327 {
7328 int shift;
7329
7330 shift = mips_opts.micromips ? 1 : 2;
7331 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7332 as_bad (_("branch to misaligned address (0x%lx)"),
7333 (unsigned long) address_expr->X_add_number);
7334 if (!mips_relax_branch)
7335 {
7336 if ((address_expr->X_add_number + (1 << (shift + 15)))
7337 & ~((1 << (shift + 16)) - 1))
7338 as_bad (_("branch address range overflow (0x%lx)"),
7339 (unsigned long) address_expr->X_add_number);
7340 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7341 & 0xffff);
7342 }
7343 }
7344 break;
7345
7346 case BFD_RELOC_MIPS_21_PCREL_S2:
7347 {
7348 int shift;
7349
7350 shift = 2;
7351 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7352 as_bad (_("branch to misaligned address (0x%lx)"),
7353 (unsigned long) address_expr->X_add_number);
7354 if ((address_expr->X_add_number + (1 << (shift + 20)))
7355 & ~((1 << (shift + 21)) - 1))
7356 as_bad (_("branch address range overflow (0x%lx)"),
7357 (unsigned long) address_expr->X_add_number);
7358 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7359 & 0x1fffff);
7360 }
7361 break;
7362
7363 case BFD_RELOC_MIPS_26_PCREL_S2:
7364 {
7365 int shift;
7366
7367 shift = 2;
7368 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7369 as_bad (_("branch to misaligned address (0x%lx)"),
7370 (unsigned long) address_expr->X_add_number);
7371 if ((address_expr->X_add_number + (1 << (shift + 25)))
7372 & ~((1 << (shift + 26)) - 1))
7373 as_bad (_("branch address range overflow (0x%lx)"),
7374 (unsigned long) address_expr->X_add_number);
7375 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7376 & 0x3ffffff);
7377 }
7378 break;
7379
7380 default:
7381 {
7382 offsetT value;
7383
7384 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7385 &value))
7386 {
7387 ip->insn_opcode |= value & 0xffff;
7388 ip->complete_p = 1;
7389 }
7390 }
7391 break;
7392 }
7393 }
7394
7395 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7396 {
7397 /* There are a lot of optimizations we could do that we don't.
7398 In particular, we do not, in general, reorder instructions.
7399 If you use gcc with optimization, it will reorder
7400 instructions and generally do much more optimization then we
7401 do here; repeating all that work in the assembler would only
7402 benefit hand written assembly code, and does not seem worth
7403 it. */
7404 int nops = (mips_optimize == 0
7405 ? nops_for_insn (0, history, NULL)
7406 : nops_for_insn_or_target (0, history, ip));
7407 if (nops > 0)
7408 {
7409 fragS *old_frag;
7410 unsigned long old_frag_offset;
7411 int i;
7412
7413 old_frag = frag_now;
7414 old_frag_offset = frag_now_fix ();
7415
7416 for (i = 0; i < nops; i++)
7417 add_fixed_insn (NOP_INSN);
7418 insert_into_history (0, nops, NOP_INSN);
7419
7420 if (listing)
7421 {
7422 listing_prev_line ();
7423 /* We may be at the start of a variant frag. In case we
7424 are, make sure there is enough space for the frag
7425 after the frags created by listing_prev_line. The
7426 argument to frag_grow here must be at least as large
7427 as the argument to all other calls to frag_grow in
7428 this file. We don't have to worry about being in the
7429 middle of a variant frag, because the variants insert
7430 all needed nop instructions themselves. */
7431 frag_grow (40);
7432 }
7433
7434 mips_move_text_labels ();
7435
7436 #ifndef NO_ECOFF_DEBUGGING
7437 if (ECOFF_DEBUGGING)
7438 ecoff_fix_loc (old_frag, old_frag_offset);
7439 #endif
7440 }
7441 }
7442 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7443 {
7444 int nops;
7445
7446 /* Work out how many nops in prev_nop_frag are needed by IP,
7447 ignoring hazards generated by the first prev_nop_frag_since
7448 instructions. */
7449 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
7450 gas_assert (nops <= prev_nop_frag_holds);
7451
7452 /* Enforce NOPS as a minimum. */
7453 if (nops > prev_nop_frag_required)
7454 prev_nop_frag_required = nops;
7455
7456 if (prev_nop_frag_holds == prev_nop_frag_required)
7457 {
7458 /* Settle for the current number of nops. Update the history
7459 accordingly (for the benefit of any future .set reorder code). */
7460 prev_nop_frag = NULL;
7461 insert_into_history (prev_nop_frag_since,
7462 prev_nop_frag_holds, NOP_INSN);
7463 }
7464 else
7465 {
7466 /* Allow this instruction to replace one of the nops that was
7467 tentatively added to prev_nop_frag. */
7468 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
7469 prev_nop_frag_holds--;
7470 prev_nop_frag_since++;
7471 }
7472 }
7473
7474 method = get_append_method (ip, address_expr, reloc_type);
7475 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
7476
7477 dwarf2_emit_insn (0);
7478 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7479 so "move" the instruction address accordingly.
7480
7481 Also, it doesn't seem appropriate for the assembler to reorder .loc
7482 entries. If this instruction is a branch that we are going to swap
7483 with the previous instruction, the two instructions should be
7484 treated as a unit, and the debug information for both instructions
7485 should refer to the start of the branch sequence. Using the
7486 current position is certainly wrong when swapping a 32-bit branch
7487 and a 16-bit delay slot, since the current position would then be
7488 in the middle of a branch. */
7489 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
7490
7491 relax32 = (mips_relax_branch
7492 /* Don't try branch relaxation within .set nomacro, or within
7493 .set noat if we use $at for PIC computations. If it turns
7494 out that the branch was out-of-range, we'll get an error. */
7495 && !mips_opts.warn_about_macros
7496 && (mips_opts.at || mips_pic == NO_PIC)
7497 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7498 as they have no complementing branches. */
7499 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
7500
7501 if (!HAVE_CODE_COMPRESSION
7502 && address_expr
7503 && relax32
7504 && *reloc_type == BFD_RELOC_16_PCREL_S2
7505 && delayed_branch_p (ip))
7506 {
7507 relaxed_branch = TRUE;
7508 add_relaxed_insn (ip, (relaxed_branch_length
7509 (NULL, NULL,
7510 uncond_branch_p (ip) ? -1
7511 : branch_likely_p (ip) ? 1
7512 : 0)), 4,
7513 RELAX_BRANCH_ENCODE
7514 (AT, mips_pic != NO_PIC,
7515 uncond_branch_p (ip),
7516 branch_likely_p (ip),
7517 pinfo & INSN_WRITE_GPR_31,
7518 0),
7519 address_expr->X_add_symbol,
7520 address_expr->X_add_number);
7521 *reloc_type = BFD_RELOC_UNUSED;
7522 }
7523 else if (mips_opts.micromips
7524 && address_expr
7525 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7526 || *reloc_type > BFD_RELOC_UNUSED)
7527 && (delayed_branch_p (ip) || compact_branch_p (ip))
7528 /* Don't try branch relaxation when users specify
7529 16-bit/32-bit instructions. */
7530 && !forced_insn_length)
7531 {
7532 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7533 && *reloc_type > BFD_RELOC_UNUSED);
7534 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
7535 int uncond = uncond_branch_p (ip) ? -1 : 0;
7536 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7537 int nods = method == APPEND_ADD_WITH_NOP;
7538 int al = pinfo & INSN_WRITE_GPR_31;
7539 int length32 = nods ? 8 : 4;
7540
7541 gas_assert (address_expr != NULL);
7542 gas_assert (!mips_relax.sequence);
7543
7544 relaxed_branch = TRUE;
7545 if (nods)
7546 method = APPEND_ADD;
7547 if (relax32)
7548 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7549 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
7550 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
7551 mips_pic != NO_PIC,
7552 uncond, compact, al, nods,
7553 relax32, 0, 0),
7554 address_expr->X_add_symbol,
7555 address_expr->X_add_number);
7556 *reloc_type = BFD_RELOC_UNUSED;
7557 }
7558 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
7559 {
7560 bfd_boolean require_unextended;
7561 bfd_boolean require_extended;
7562 symbolS *symbol;
7563 offsetT offset;
7564
7565 if (forced_insn_length != 0)
7566 {
7567 require_unextended = forced_insn_length == 2;
7568 require_extended = forced_insn_length == 4;
7569 }
7570 else
7571 {
7572 require_unextended = (mips_opts.noautoextend
7573 && !mips_opcode_32bit_p (ip->insn_mo));
7574 require_extended = 0;
7575 }
7576
7577 /* We need to set up a variant frag. */
7578 gas_assert (address_expr != NULL);
7579 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7580 symbol created by `make_expr_symbol' may not get a necessary
7581 external relocation produced. */
7582 if (address_expr->X_op == O_symbol)
7583 {
7584 symbol = address_expr->X_add_symbol;
7585 offset = address_expr->X_add_number;
7586 }
7587 else
7588 {
7589 symbol = make_expr_symbol (address_expr);
7590 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
7591 offset = 0;
7592 }
7593 add_relaxed_insn (ip, 12, 0,
7594 RELAX_MIPS16_ENCODE
7595 (*reloc_type - BFD_RELOC_UNUSED,
7596 mips_opts.ase & ASE_MIPS16E2,
7597 mips_pic != NO_PIC,
7598 HAVE_32BIT_SYMBOLS,
7599 mips_opts.warn_about_macros,
7600 require_unextended, require_extended,
7601 delayed_branch_p (&history[0]),
7602 history[0].mips16_absolute_jump_p),
7603 symbol, offset);
7604 }
7605 else if (mips_opts.mips16 && insn_length (ip) == 2)
7606 {
7607 if (!delayed_branch_p (ip))
7608 /* Make sure there is enough room to swap this instruction with
7609 a following jump instruction. */
7610 frag_grow (6);
7611 add_fixed_insn (ip);
7612 }
7613 else
7614 {
7615 if (mips_opts.mips16
7616 && mips_opts.noreorder
7617 && delayed_branch_p (&history[0]))
7618 as_warn (_("extended instruction in delay slot"));
7619
7620 if (mips_relax.sequence)
7621 {
7622 /* If we've reached the end of this frag, turn it into a variant
7623 frag and record the information for the instructions we've
7624 written so far. */
7625 if (frag_room () < 4)
7626 relax_close_frag ();
7627 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
7628 }
7629
7630 if (mips_relax.sequence != 2)
7631 {
7632 if (mips_macro_warning.first_insn_sizes[0] == 0)
7633 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7634 mips_macro_warning.sizes[0] += insn_length (ip);
7635 mips_macro_warning.insns[0]++;
7636 }
7637 if (mips_relax.sequence != 1)
7638 {
7639 if (mips_macro_warning.first_insn_sizes[1] == 0)
7640 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7641 mips_macro_warning.sizes[1] += insn_length (ip);
7642 mips_macro_warning.insns[1]++;
7643 }
7644
7645 if (mips_opts.mips16)
7646 {
7647 ip->fixed_p = 1;
7648 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7649 }
7650 add_fixed_insn (ip);
7651 }
7652
7653 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
7654 {
7655 bfd_reloc_code_real_type final_type[3];
7656 reloc_howto_type *howto0;
7657 reloc_howto_type *howto;
7658 int i;
7659
7660 /* Perform any necessary conversion to microMIPS relocations
7661 and find out how many relocations there actually are. */
7662 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7663 final_type[i] = micromips_map_reloc (reloc_type[i]);
7664
7665 /* In a compound relocation, it is the final (outermost)
7666 operator that determines the relocated field. */
7667 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
7668 if (!howto)
7669 abort ();
7670
7671 if (i > 1)
7672 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
7673 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7674 bfd_get_reloc_size (howto),
7675 address_expr,
7676 howto0 && howto0->pc_relative,
7677 final_type[0]);
7678 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7679 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
7680
7681 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
7682 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
7683 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7684
7685 /* These relocations can have an addend that won't fit in
7686 4 octets for 64bit assembly. */
7687 if (GPR_SIZE == 64
7688 && ! howto->partial_inplace
7689 && (reloc_type[0] == BFD_RELOC_16
7690 || reloc_type[0] == BFD_RELOC_32
7691 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7692 || reloc_type[0] == BFD_RELOC_GPREL16
7693 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7694 || reloc_type[0] == BFD_RELOC_GPREL32
7695 || reloc_type[0] == BFD_RELOC_64
7696 || reloc_type[0] == BFD_RELOC_CTOR
7697 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7698 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7699 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7700 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7701 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7702 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7703 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7704 || hi16_reloc_p (reloc_type[0])
7705 || lo16_reloc_p (reloc_type[0])))
7706 ip->fixp[0]->fx_no_overflow = 1;
7707
7708 /* These relocations can have an addend that won't fit in 2 octets. */
7709 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7710 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7711 ip->fixp[0]->fx_no_overflow = 1;
7712
7713 if (mips_relax.sequence)
7714 {
7715 if (mips_relax.first_fixup == 0)
7716 mips_relax.first_fixup = ip->fixp[0];
7717 }
7718 else if (reloc_needs_lo_p (*reloc_type))
7719 {
7720 struct mips_hi_fixup *hi_fixup;
7721
7722 /* Reuse the last entry if it already has a matching %lo. */
7723 hi_fixup = mips_hi_fixup_list;
7724 if (hi_fixup == 0
7725 || !fixup_has_matching_lo_p (hi_fixup->fixp))
7726 {
7727 hi_fixup = XNEW (struct mips_hi_fixup);
7728 hi_fixup->next = mips_hi_fixup_list;
7729 mips_hi_fixup_list = hi_fixup;
7730 }
7731 hi_fixup->fixp = ip->fixp[0];
7732 hi_fixup->seg = now_seg;
7733 }
7734
7735 /* Add fixups for the second and third relocations, if given.
7736 Note that the ABI allows the second relocation to be
7737 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7738 moment we only use RSS_UNDEF, but we could add support
7739 for the others if it ever becomes necessary. */
7740 for (i = 1; i < 3; i++)
7741 if (reloc_type[i] != BFD_RELOC_UNUSED)
7742 {
7743 ip->fixp[i] = fix_new (ip->frag, ip->where,
7744 ip->fixp[0]->fx_size, NULL, 0,
7745 FALSE, final_type[i]);
7746
7747 /* Use fx_tcbit to mark compound relocs. */
7748 ip->fixp[0]->fx_tcbit = 1;
7749 ip->fixp[i]->fx_tcbit = 1;
7750 }
7751 }
7752
7753 /* Update the register mask information. */
7754 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7755 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
7756
7757 switch (method)
7758 {
7759 case APPEND_ADD:
7760 insert_into_history (0, 1, ip);
7761 break;
7762
7763 case APPEND_ADD_WITH_NOP:
7764 {
7765 struct mips_cl_insn *nop;
7766
7767 insert_into_history (0, 1, ip);
7768 nop = get_delay_slot_nop (ip);
7769 add_fixed_insn (nop);
7770 insert_into_history (0, 1, nop);
7771 if (mips_relax.sequence)
7772 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7773 }
7774 break;
7775
7776 case APPEND_ADD_COMPACT:
7777 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7778 if (mips_opts.mips16)
7779 {
7780 ip->insn_opcode |= 0x0080;
7781 find_altered_mips16_opcode (ip);
7782 }
7783 /* Convert microMIPS instructions. */
7784 else if (mips_opts.micromips)
7785 {
7786 /* jr16->jrc */
7787 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7788 ip->insn_opcode |= 0x0020;
7789 /* b16->bc */
7790 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7791 ip->insn_opcode = 0x40e00000;
7792 /* beqz16->beqzc, bnez16->bnezc */
7793 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7794 {
7795 unsigned long regno;
7796
7797 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7798 regno &= MICROMIPSOP_MASK_MD;
7799 regno = micromips_to_32_reg_d_map[regno];
7800 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7801 | (regno << MICROMIPSOP_SH_RS)
7802 | 0x40a00000) ^ 0x00400000;
7803 }
7804 /* beqz->beqzc, bnez->bnezc */
7805 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7806 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7807 | ((ip->insn_opcode >> 7) & 0x00400000)
7808 | 0x40a00000) ^ 0x00400000;
7809 /* beq $0->beqzc, bne $0->bnezc */
7810 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7811 ip->insn_opcode = (((ip->insn_opcode >>
7812 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
7813 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
7814 | ((ip->insn_opcode >> 7) & 0x00400000)
7815 | 0x40a00000) ^ 0x00400000;
7816 else
7817 abort ();
7818 find_altered_micromips_opcode (ip);
7819 }
7820 else
7821 abort ();
7822 install_insn (ip);
7823 insert_into_history (0, 1, ip);
7824 break;
7825
7826 case APPEND_SWAP:
7827 {
7828 struct mips_cl_insn delay = history[0];
7829
7830 if (relaxed_branch || delay.frag != ip->frag)
7831 {
7832 /* Add the delay slot instruction to the end of the
7833 current frag and shrink the fixed part of the
7834 original frag. If the branch occupies the tail of
7835 the latter, move it backwards to cover the gap. */
7836 delay.frag->fr_fix -= branch_disp;
7837 if (delay.frag == ip->frag)
7838 move_insn (ip, ip->frag, ip->where - branch_disp);
7839 add_fixed_insn (&delay);
7840 }
7841 else
7842 {
7843 /* If this is not a relaxed branch and we are in the
7844 same frag, then just swap the instructions. */
7845 move_insn (ip, delay.frag, delay.where);
7846 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
7847 }
7848 history[0] = *ip;
7849 delay.fixed_p = 1;
7850 insert_into_history (0, 1, &delay);
7851 }
7852 break;
7853 }
7854
7855 /* If we have just completed an unconditional branch, clear the history. */
7856 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7857 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
7858 {
7859 unsigned int i;
7860
7861 mips_no_prev_insn ();
7862
7863 for (i = 0; i < ARRAY_SIZE (history); i++)
7864 history[i].cleared_p = 1;
7865 }
7866
7867 /* We need to emit a label at the end of branch-likely macros. */
7868 if (emit_branch_likely_macro)
7869 {
7870 emit_branch_likely_macro = FALSE;
7871 micromips_add_label ();
7872 }
7873
7874 /* We just output an insn, so the next one doesn't have a label. */
7875 mips_clear_insn_labels ();
7876 }
7877
7878 /* Forget that there was any previous instruction or label.
7879 When BRANCH is true, the branch history is also flushed. */
7880
7881 static void
7882 mips_no_prev_insn (void)
7883 {
7884 prev_nop_frag = NULL;
7885 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
7886 mips_clear_insn_labels ();
7887 }
7888
7889 /* This function must be called before we emit something other than
7890 instructions. It is like mips_no_prev_insn except that it inserts
7891 any NOPS that might be needed by previous instructions. */
7892
7893 void
7894 mips_emit_delays (void)
7895 {
7896 if (! mips_opts.noreorder)
7897 {
7898 int nops = nops_for_insn (0, history, NULL);
7899 if (nops > 0)
7900 {
7901 while (nops-- > 0)
7902 add_fixed_insn (NOP_INSN);
7903 mips_move_text_labels ();
7904 }
7905 }
7906 mips_no_prev_insn ();
7907 }
7908
7909 /* Start a (possibly nested) noreorder block. */
7910
7911 static void
7912 start_noreorder (void)
7913 {
7914 if (mips_opts.noreorder == 0)
7915 {
7916 unsigned int i;
7917 int nops;
7918
7919 /* None of the instructions before the .set noreorder can be moved. */
7920 for (i = 0; i < ARRAY_SIZE (history); i++)
7921 history[i].fixed_p = 1;
7922
7923 /* Insert any nops that might be needed between the .set noreorder
7924 block and the previous instructions. We will later remove any
7925 nops that turn out not to be needed. */
7926 nops = nops_for_insn (0, history, NULL);
7927 if (nops > 0)
7928 {
7929 if (mips_optimize != 0)
7930 {
7931 /* Record the frag which holds the nop instructions, so
7932 that we can remove them if we don't need them. */
7933 frag_grow (nops * NOP_INSN_SIZE);
7934 prev_nop_frag = frag_now;
7935 prev_nop_frag_holds = nops;
7936 prev_nop_frag_required = 0;
7937 prev_nop_frag_since = 0;
7938 }
7939
7940 for (; nops > 0; --nops)
7941 add_fixed_insn (NOP_INSN);
7942
7943 /* Move on to a new frag, so that it is safe to simply
7944 decrease the size of prev_nop_frag. */
7945 frag_wane (frag_now);
7946 frag_new (0);
7947 mips_move_text_labels ();
7948 }
7949 mips_mark_labels ();
7950 mips_clear_insn_labels ();
7951 }
7952 mips_opts.noreorder++;
7953 mips_any_noreorder = 1;
7954 }
7955
7956 /* End a nested noreorder block. */
7957
7958 static void
7959 end_noreorder (void)
7960 {
7961 mips_opts.noreorder--;
7962 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7963 {
7964 /* Commit to inserting prev_nop_frag_required nops and go back to
7965 handling nop insertion the .set reorder way. */
7966 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
7967 * NOP_INSN_SIZE);
7968 insert_into_history (prev_nop_frag_since,
7969 prev_nop_frag_required, NOP_INSN);
7970 prev_nop_frag = NULL;
7971 }
7972 }
7973
7974 /* Sign-extend 32-bit mode constants that have bit 31 set and all
7975 higher bits unset. */
7976
7977 static void
7978 normalize_constant_expr (expressionS *ex)
7979 {
7980 if (ex->X_op == O_constant
7981 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7982 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7983 - 0x80000000);
7984 }
7985
7986 /* Sign-extend 32-bit mode address offsets that have bit 31 set and
7987 all higher bits unset. */
7988
7989 static void
7990 normalize_address_expr (expressionS *ex)
7991 {
7992 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7993 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7994 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7995 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7996 - 0x80000000);
7997 }
7998
7999 /* Try to match TOKENS against OPCODE, storing the result in INSN.
8000 Return true if the match was successful.
8001
8002 OPCODE_EXTRA is a value that should be ORed into the opcode
8003 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
8004 there are more alternatives after OPCODE and SOFT_MATCH is
8005 as for mips_arg_info. */
8006
8007 static bfd_boolean
8008 match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8009 struct mips_operand_token *tokens, unsigned int opcode_extra,
8010 bfd_boolean lax_match, bfd_boolean complete_p)
8011 {
8012 const char *args;
8013 struct mips_arg_info arg;
8014 const struct mips_operand *operand;
8015 char c;
8016
8017 imm_expr.X_op = O_absent;
8018 offset_expr.X_op = O_absent;
8019 offset_reloc[0] = BFD_RELOC_UNUSED;
8020 offset_reloc[1] = BFD_RELOC_UNUSED;
8021 offset_reloc[2] = BFD_RELOC_UNUSED;
8022
8023 create_insn (insn, opcode);
8024 /* When no opcode suffix is specified, assume ".xyzw". */
8025 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8026 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8027 else
8028 insn->insn_opcode |= opcode_extra;
8029 memset (&arg, 0, sizeof (arg));
8030 arg.insn = insn;
8031 arg.token = tokens;
8032 arg.argnum = 1;
8033 arg.last_regno = ILLEGAL_REG;
8034 arg.dest_regno = ILLEGAL_REG;
8035 arg.lax_match = lax_match;
8036 for (args = opcode->args;; ++args)
8037 {
8038 if (arg.token->type == OT_END)
8039 {
8040 /* Handle unary instructions in which only one operand is given.
8041 The source is then the same as the destination. */
8042 if (arg.opnum == 1 && *args == ',')
8043 {
8044 operand = (mips_opts.micromips
8045 ? decode_micromips_operand (args + 1)
8046 : decode_mips_operand (args + 1));
8047 if (operand && mips_optional_operand_p (operand))
8048 {
8049 arg.token = tokens;
8050 arg.argnum = 1;
8051 continue;
8052 }
8053 }
8054
8055 /* Treat elided base registers as $0. */
8056 if (strcmp (args, "(b)") == 0)
8057 args += 3;
8058
8059 if (args[0] == '+')
8060 switch (args[1])
8061 {
8062 case 'K':
8063 case 'N':
8064 /* The register suffix is optional. */
8065 args += 2;
8066 break;
8067 }
8068
8069 /* Fail the match if there were too few operands. */
8070 if (*args)
8071 return FALSE;
8072
8073 /* Successful match. */
8074 if (!complete_p)
8075 return TRUE;
8076 clear_insn_error ();
8077 if (arg.dest_regno == arg.last_regno
8078 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8079 {
8080 if (arg.opnum == 2)
8081 set_insn_error
8082 (0, _("source and destination must be different"));
8083 else if (arg.last_regno == 31)
8084 set_insn_error
8085 (0, _("a destination register must be supplied"));
8086 }
8087 else if (arg.last_regno == 31
8088 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8089 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8090 set_insn_error (0, _("the source register must not be $31"));
8091 check_completed_insn (&arg);
8092 return TRUE;
8093 }
8094
8095 /* Fail the match if the line has too many operands. */
8096 if (*args == 0)
8097 return FALSE;
8098
8099 /* Handle characters that need to match exactly. */
8100 if (*args == '(' || *args == ')' || *args == ',')
8101 {
8102 if (match_char (&arg, *args))
8103 continue;
8104 return FALSE;
8105 }
8106 if (*args == '#')
8107 {
8108 ++args;
8109 if (arg.token->type == OT_DOUBLE_CHAR
8110 && arg.token->u.ch == *args)
8111 {
8112 ++arg.token;
8113 continue;
8114 }
8115 return FALSE;
8116 }
8117
8118 /* Handle special macro operands. Work out the properties of
8119 other operands. */
8120 arg.opnum += 1;
8121 switch (*args)
8122 {
8123 case '-':
8124 switch (args[1])
8125 {
8126 case 'A':
8127 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8128 break;
8129
8130 case 'B':
8131 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8132 break;
8133 }
8134 break;
8135
8136 case '+':
8137 switch (args[1])
8138 {
8139 case 'i':
8140 *offset_reloc = BFD_RELOC_MIPS_JMP;
8141 break;
8142
8143 case '\'':
8144 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8145 break;
8146
8147 case '\"':
8148 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8149 break;
8150 }
8151 break;
8152
8153 case 'I':
8154 if (!match_const_int (&arg, &imm_expr.X_add_number))
8155 return FALSE;
8156 imm_expr.X_op = O_constant;
8157 if (GPR_SIZE == 32)
8158 normalize_constant_expr (&imm_expr);
8159 continue;
8160
8161 case 'A':
8162 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8163 {
8164 /* Assume that the offset has been elided and that what
8165 we saw was a base register. The match will fail later
8166 if that assumption turns out to be wrong. */
8167 offset_expr.X_op = O_constant;
8168 offset_expr.X_add_number = 0;
8169 }
8170 else
8171 {
8172 if (!match_expression (&arg, &offset_expr, offset_reloc))
8173 return FALSE;
8174 normalize_address_expr (&offset_expr);
8175 }
8176 continue;
8177
8178 case 'F':
8179 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8180 8, TRUE))
8181 return FALSE;
8182 continue;
8183
8184 case 'L':
8185 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8186 8, FALSE))
8187 return FALSE;
8188 continue;
8189
8190 case 'f':
8191 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8192 4, TRUE))
8193 return FALSE;
8194 continue;
8195
8196 case 'l':
8197 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8198 4, FALSE))
8199 return FALSE;
8200 continue;
8201
8202 case 'p':
8203 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8204 break;
8205
8206 case 'a':
8207 *offset_reloc = BFD_RELOC_MIPS_JMP;
8208 break;
8209
8210 case 'm':
8211 gas_assert (mips_opts.micromips);
8212 c = args[1];
8213 switch (c)
8214 {
8215 case 'D':
8216 case 'E':
8217 if (!forced_insn_length)
8218 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8219 else if (c == 'D')
8220 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8221 else
8222 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8223 break;
8224 }
8225 break;
8226 }
8227
8228 operand = (mips_opts.micromips
8229 ? decode_micromips_operand (args)
8230 : decode_mips_operand (args));
8231 if (!operand)
8232 abort ();
8233
8234 /* Skip prefixes. */
8235 if (*args == '+' || *args == 'm' || *args == '-')
8236 args++;
8237
8238 if (mips_optional_operand_p (operand)
8239 && args[1] == ','
8240 && (arg.token[0].type != OT_REG
8241 || arg.token[1].type == OT_END))
8242 {
8243 /* Assume that the register has been elided and is the
8244 same as the first operand. */
8245 arg.token = tokens;
8246 arg.argnum = 1;
8247 }
8248
8249 if (!match_operand (&arg, operand))
8250 return FALSE;
8251 }
8252 }
8253
8254 /* Like match_insn, but for MIPS16. */
8255
8256 static bfd_boolean
8257 match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8258 struct mips_operand_token *tokens)
8259 {
8260 const char *args;
8261 const struct mips_operand *operand;
8262 const struct mips_operand *ext_operand;
8263 bfd_boolean pcrel = FALSE;
8264 int required_insn_length;
8265 struct mips_arg_info arg;
8266 int relax_char;
8267
8268 if (forced_insn_length)
8269 required_insn_length = forced_insn_length;
8270 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8271 required_insn_length = 2;
8272 else
8273 required_insn_length = 0;
8274
8275 create_insn (insn, opcode);
8276 imm_expr.X_op = O_absent;
8277 offset_expr.X_op = O_absent;
8278 offset_reloc[0] = BFD_RELOC_UNUSED;
8279 offset_reloc[1] = BFD_RELOC_UNUSED;
8280 offset_reloc[2] = BFD_RELOC_UNUSED;
8281 relax_char = 0;
8282
8283 memset (&arg, 0, sizeof (arg));
8284 arg.insn = insn;
8285 arg.token = tokens;
8286 arg.argnum = 1;
8287 arg.last_regno = ILLEGAL_REG;
8288 arg.dest_regno = ILLEGAL_REG;
8289 relax_char = 0;
8290 for (args = opcode->args;; ++args)
8291 {
8292 int c;
8293
8294 if (arg.token->type == OT_END)
8295 {
8296 offsetT value;
8297
8298 /* Handle unary instructions in which only one operand is given.
8299 The source is then the same as the destination. */
8300 if (arg.opnum == 1 && *args == ',')
8301 {
8302 operand = decode_mips16_operand (args[1], FALSE);
8303 if (operand && mips_optional_operand_p (operand))
8304 {
8305 arg.token = tokens;
8306 arg.argnum = 1;
8307 continue;
8308 }
8309 }
8310
8311 /* Fail the match if there were too few operands. */
8312 if (*args)
8313 return FALSE;
8314
8315 /* Successful match. Stuff the immediate value in now, if
8316 we can. */
8317 clear_insn_error ();
8318 if (opcode->pinfo == INSN_MACRO)
8319 {
8320 gas_assert (relax_char == 0 || relax_char == 'p');
8321 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8322 }
8323 else if (relax_char
8324 && offset_expr.X_op == O_constant
8325 && !pcrel
8326 && calculate_reloc (*offset_reloc,
8327 offset_expr.X_add_number,
8328 &value))
8329 {
8330 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
8331 required_insn_length, &insn->insn_opcode);
8332 offset_expr.X_op = O_absent;
8333 *offset_reloc = BFD_RELOC_UNUSED;
8334 }
8335 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8336 {
8337 if (required_insn_length == 2)
8338 set_insn_error (0, _("invalid unextended operand value"));
8339 else if (!mips_opcode_32bit_p (opcode))
8340 {
8341 forced_insn_length = 4;
8342 insn->insn_opcode |= MIPS16_EXTEND;
8343 }
8344 }
8345 else if (relax_char)
8346 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8347
8348 check_completed_insn (&arg);
8349 return TRUE;
8350 }
8351
8352 /* Fail the match if the line has too many operands. */
8353 if (*args == 0)
8354 return FALSE;
8355
8356 /* Handle characters that need to match exactly. */
8357 if (*args == '(' || *args == ')' || *args == ',')
8358 {
8359 if (match_char (&arg, *args))
8360 continue;
8361 return FALSE;
8362 }
8363
8364 arg.opnum += 1;
8365 c = *args;
8366 switch (c)
8367 {
8368 case 'p':
8369 case 'q':
8370 case 'A':
8371 case 'B':
8372 case 'E':
8373 case 'V':
8374 case 'u':
8375 relax_char = c;
8376 break;
8377
8378 case 'I':
8379 if (!match_const_int (&arg, &imm_expr.X_add_number))
8380 return FALSE;
8381 imm_expr.X_op = O_constant;
8382 if (GPR_SIZE == 32)
8383 normalize_constant_expr (&imm_expr);
8384 continue;
8385
8386 case 'a':
8387 case 'i':
8388 *offset_reloc = BFD_RELOC_MIPS16_JMP;
8389 break;
8390 }
8391
8392 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
8393 if (!operand)
8394 abort ();
8395
8396 if (operand->type == OP_PCREL)
8397 pcrel = TRUE;
8398 else
8399 {
8400 ext_operand = decode_mips16_operand (c, TRUE);
8401 if (operand != ext_operand)
8402 {
8403 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8404 {
8405 offset_expr.X_op = O_constant;
8406 offset_expr.X_add_number = 0;
8407 relax_char = c;
8408 continue;
8409 }
8410
8411 if (!match_expression (&arg, &offset_expr, offset_reloc))
8412 return FALSE;
8413
8414 /* '8' is used for SLTI(U) and has traditionally not
8415 been allowed to take relocation operators. */
8416 if (offset_reloc[0] != BFD_RELOC_UNUSED
8417 && (ext_operand->size != 16 || c == '8'))
8418 {
8419 match_not_constant (&arg);
8420 return FALSE;
8421 }
8422
8423 if (offset_expr.X_op == O_big)
8424 {
8425 match_out_of_range (&arg);
8426 return FALSE;
8427 }
8428
8429 relax_char = c;
8430 continue;
8431 }
8432 }
8433
8434 if (mips_optional_operand_p (operand)
8435 && args[1] == ','
8436 && (arg.token[0].type != OT_REG
8437 || arg.token[1].type == OT_END))
8438 {
8439 /* Assume that the register has been elided and is the
8440 same as the first operand. */
8441 arg.token = tokens;
8442 arg.argnum = 1;
8443 }
8444
8445 if (!match_operand (&arg, operand))
8446 return FALSE;
8447 }
8448 }
8449
8450 /* Record that the current instruction is invalid for the current ISA. */
8451
8452 static void
8453 match_invalid_for_isa (void)
8454 {
8455 set_insn_error_ss
8456 (0, _("opcode not supported on this processor: %s (%s)"),
8457 mips_cpu_info_from_arch (mips_opts.arch)->name,
8458 mips_cpu_info_from_isa (mips_opts.isa)->name);
8459 }
8460
8461 /* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8462 Return true if a definite match or failure was found, storing any match
8463 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8464 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8465 tried and failed to match under normal conditions and now want to try a
8466 more relaxed match. */
8467
8468 static bfd_boolean
8469 match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8470 const struct mips_opcode *past, struct mips_operand_token *tokens,
8471 int opcode_extra, bfd_boolean lax_match)
8472 {
8473 const struct mips_opcode *opcode;
8474 const struct mips_opcode *invalid_delay_slot;
8475 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8476
8477 /* Search for a match, ignoring alternatives that don't satisfy the
8478 current ISA or forced_length. */
8479 invalid_delay_slot = 0;
8480 seen_valid_for_isa = FALSE;
8481 seen_valid_for_size = FALSE;
8482 opcode = first;
8483 do
8484 {
8485 gas_assert (strcmp (opcode->name, first->name) == 0);
8486 if (is_opcode_valid (opcode))
8487 {
8488 seen_valid_for_isa = TRUE;
8489 if (is_size_valid (opcode))
8490 {
8491 bfd_boolean delay_slot_ok;
8492
8493 seen_valid_for_size = TRUE;
8494 delay_slot_ok = is_delay_slot_valid (opcode);
8495 if (match_insn (insn, opcode, tokens, opcode_extra,
8496 lax_match, delay_slot_ok))
8497 {
8498 if (!delay_slot_ok)
8499 {
8500 if (!invalid_delay_slot)
8501 invalid_delay_slot = opcode;
8502 }
8503 else
8504 return TRUE;
8505 }
8506 }
8507 }
8508 ++opcode;
8509 }
8510 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8511
8512 /* If the only matches we found had the wrong length for the delay slot,
8513 pick the first such match. We'll issue an appropriate warning later. */
8514 if (invalid_delay_slot)
8515 {
8516 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8517 lax_match, TRUE))
8518 return TRUE;
8519 abort ();
8520 }
8521
8522 /* Handle the case where we didn't try to match an instruction because
8523 all the alternatives were incompatible with the current ISA. */
8524 if (!seen_valid_for_isa)
8525 {
8526 match_invalid_for_isa ();
8527 return TRUE;
8528 }
8529
8530 /* Handle the case where we didn't try to match an instruction because
8531 all the alternatives were of the wrong size. */
8532 if (!seen_valid_for_size)
8533 {
8534 if (mips_opts.insn32)
8535 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
8536 else
8537 set_insn_error_i
8538 (0, _("unrecognized %d-bit version of microMIPS opcode"),
8539 8 * forced_insn_length);
8540 return TRUE;
8541 }
8542
8543 return FALSE;
8544 }
8545
8546 /* Like match_insns, but for MIPS16. */
8547
8548 static bfd_boolean
8549 match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8550 struct mips_operand_token *tokens)
8551 {
8552 const struct mips_opcode *opcode;
8553 bfd_boolean seen_valid_for_isa;
8554 bfd_boolean seen_valid_for_size;
8555
8556 /* Search for a match, ignoring alternatives that don't satisfy the
8557 current ISA. There are no separate entries for extended forms so
8558 we deal with forced_length later. */
8559 seen_valid_for_isa = FALSE;
8560 seen_valid_for_size = FALSE;
8561 opcode = first;
8562 do
8563 {
8564 gas_assert (strcmp (opcode->name, first->name) == 0);
8565 if (is_opcode_valid_16 (opcode))
8566 {
8567 seen_valid_for_isa = TRUE;
8568 if (is_size_valid_16 (opcode))
8569 {
8570 seen_valid_for_size = TRUE;
8571 if (match_mips16_insn (insn, opcode, tokens))
8572 return TRUE;
8573 }
8574 }
8575 ++opcode;
8576 }
8577 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8578 && strcmp (opcode->name, first->name) == 0);
8579
8580 /* Handle the case where we didn't try to match an instruction because
8581 all the alternatives were incompatible with the current ISA. */
8582 if (!seen_valid_for_isa)
8583 {
8584 match_invalid_for_isa ();
8585 return TRUE;
8586 }
8587
8588 /* Handle the case where we didn't try to match an instruction because
8589 all the alternatives were of the wrong size. */
8590 if (!seen_valid_for_size)
8591 {
8592 if (forced_insn_length == 2)
8593 set_insn_error
8594 (0, _("unrecognized unextended version of MIPS16 opcode"));
8595 else
8596 set_insn_error
8597 (0, _("unrecognized extended version of MIPS16 opcode"));
8598 return TRUE;
8599 }
8600
8601 return FALSE;
8602 }
8603
8604 /* Set up global variables for the start of a new macro. */
8605
8606 static void
8607 macro_start (void)
8608 {
8609 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
8610 memset (&mips_macro_warning.first_insn_sizes, 0,
8611 sizeof (mips_macro_warning.first_insn_sizes));
8612 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
8613 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
8614 && delayed_branch_p (&history[0]));
8615 if (history[0].frag
8616 && history[0].frag->fr_type == rs_machine_dependent
8617 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8618 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8619 mips_macro_warning.delay_slot_length = 0;
8620 else
8621 switch (history[0].insn_mo->pinfo2
8622 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8623 {
8624 case INSN2_BRANCH_DELAY_32BIT:
8625 mips_macro_warning.delay_slot_length = 4;
8626 break;
8627 case INSN2_BRANCH_DELAY_16BIT:
8628 mips_macro_warning.delay_slot_length = 2;
8629 break;
8630 default:
8631 mips_macro_warning.delay_slot_length = 0;
8632 break;
8633 }
8634 mips_macro_warning.first_frag = NULL;
8635 }
8636
8637 /* Given that a macro is longer than one instruction or of the wrong size,
8638 return the appropriate warning for it. Return null if no warning is
8639 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8640 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8641 and RELAX_NOMACRO. */
8642
8643 static const char *
8644 macro_warning (relax_substateT subtype)
8645 {
8646 if (subtype & RELAX_DELAY_SLOT)
8647 return _("macro instruction expanded into multiple instructions"
8648 " in a branch delay slot");
8649 else if (subtype & RELAX_NOMACRO)
8650 return _("macro instruction expanded into multiple instructions");
8651 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8652 | RELAX_DELAY_SLOT_SIZE_SECOND))
8653 return ((subtype & RELAX_DELAY_SLOT_16BIT)
8654 ? _("macro instruction expanded into a wrong size instruction"
8655 " in a 16-bit branch delay slot")
8656 : _("macro instruction expanded into a wrong size instruction"
8657 " in a 32-bit branch delay slot"));
8658 else
8659 return 0;
8660 }
8661
8662 /* Finish up a macro. Emit warnings as appropriate. */
8663
8664 static void
8665 macro_end (void)
8666 {
8667 /* Relaxation warning flags. */
8668 relax_substateT subtype = 0;
8669
8670 /* Check delay slot size requirements. */
8671 if (mips_macro_warning.delay_slot_length == 2)
8672 subtype |= RELAX_DELAY_SLOT_16BIT;
8673 if (mips_macro_warning.delay_slot_length != 0)
8674 {
8675 if (mips_macro_warning.delay_slot_length
8676 != mips_macro_warning.first_insn_sizes[0])
8677 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8678 if (mips_macro_warning.delay_slot_length
8679 != mips_macro_warning.first_insn_sizes[1])
8680 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8681 }
8682
8683 /* Check instruction count requirements. */
8684 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8685 {
8686 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
8687 subtype |= RELAX_SECOND_LONGER;
8688 if (mips_opts.warn_about_macros)
8689 subtype |= RELAX_NOMACRO;
8690 if (mips_macro_warning.delay_slot_p)
8691 subtype |= RELAX_DELAY_SLOT;
8692 }
8693
8694 /* If both alternatives fail to fill a delay slot correctly,
8695 emit the warning now. */
8696 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8697 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8698 {
8699 relax_substateT s;
8700 const char *msg;
8701
8702 s = subtype & (RELAX_DELAY_SLOT_16BIT
8703 | RELAX_DELAY_SLOT_SIZE_FIRST
8704 | RELAX_DELAY_SLOT_SIZE_SECOND);
8705 msg = macro_warning (s);
8706 if (msg != NULL)
8707 as_warn ("%s", msg);
8708 subtype &= ~s;
8709 }
8710
8711 /* If both implementations are longer than 1 instruction, then emit the
8712 warning now. */
8713 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8714 {
8715 relax_substateT s;
8716 const char *msg;
8717
8718 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8719 msg = macro_warning (s);
8720 if (msg != NULL)
8721 as_warn ("%s", msg);
8722 subtype &= ~s;
8723 }
8724
8725 /* If any flags still set, then one implementation might need a warning
8726 and the other either will need one of a different kind or none at all.
8727 Pass any remaining flags over to relaxation. */
8728 if (mips_macro_warning.first_frag != NULL)
8729 mips_macro_warning.first_frag->fr_subtype |= subtype;
8730 }
8731
8732 /* Instruction operand formats used in macros that vary between
8733 standard MIPS and microMIPS code. */
8734
8735 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
8736 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8737 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8738 static const char * const lui_fmt[2] = { "t,u", "s,u" };
8739 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
8740 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
8741 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8742 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8743
8744 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
8745 #define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8746 : cop12_fmt[mips_opts.micromips])
8747 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
8748 #define LUI_FMT (lui_fmt[mips_opts.micromips])
8749 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
8750 #define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8751 : mem12_fmt[mips_opts.micromips])
8752 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
8753 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
8754 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
8755
8756 /* Read a macro's relocation codes from *ARGS and store them in *R.
8757 The first argument in *ARGS will be either the code for a single
8758 relocation or -1 followed by the three codes that make up a
8759 composite relocation. */
8760
8761 static void
8762 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8763 {
8764 int i, next;
8765
8766 next = va_arg (*args, int);
8767 if (next >= 0)
8768 r[0] = (bfd_reloc_code_real_type) next;
8769 else
8770 {
8771 for (i = 0; i < 3; i++)
8772 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8773 /* This function is only used for 16-bit relocation fields.
8774 To make the macro code simpler, treat an unrelocated value
8775 in the same way as BFD_RELOC_LO16. */
8776 if (r[0] == BFD_RELOC_UNUSED)
8777 r[0] = BFD_RELOC_LO16;
8778 }
8779 }
8780
8781 /* Build an instruction created by a macro expansion. This is passed
8782 a pointer to the count of instructions created so far, an
8783 expression, the name of the instruction to build, an operand format
8784 string, and corresponding arguments. */
8785
8786 static void
8787 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
8788 {
8789 const struct mips_opcode *mo = NULL;
8790 bfd_reloc_code_real_type r[3];
8791 const struct mips_opcode *amo;
8792 const struct mips_operand *operand;
8793 struct hash_control *hash;
8794 struct mips_cl_insn insn;
8795 va_list args;
8796 unsigned int uval;
8797
8798 va_start (args, fmt);
8799
8800 if (mips_opts.mips16)
8801 {
8802 mips16_macro_build (ep, name, fmt, &args);
8803 va_end (args);
8804 return;
8805 }
8806
8807 r[0] = BFD_RELOC_UNUSED;
8808 r[1] = BFD_RELOC_UNUSED;
8809 r[2] = BFD_RELOC_UNUSED;
8810 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8811 amo = (struct mips_opcode *) hash_find (hash, name);
8812 gas_assert (amo);
8813 gas_assert (strcmp (name, amo->name) == 0);
8814
8815 do
8816 {
8817 /* Search until we get a match for NAME. It is assumed here that
8818 macros will never generate MDMX, MIPS-3D, or MT instructions.
8819 We try to match an instruction that fulfills the branch delay
8820 slot instruction length requirement (if any) of the previous
8821 instruction. While doing this we record the first instruction
8822 seen that matches all the other conditions and use it anyway
8823 if the requirement cannot be met; we will issue an appropriate
8824 warning later on. */
8825 if (strcmp (fmt, amo->args) == 0
8826 && amo->pinfo != INSN_MACRO
8827 && is_opcode_valid (amo)
8828 && is_size_valid (amo))
8829 {
8830 if (is_delay_slot_valid (amo))
8831 {
8832 mo = amo;
8833 break;
8834 }
8835 else if (!mo)
8836 mo = amo;
8837 }
8838
8839 ++amo;
8840 gas_assert (amo->name);
8841 }
8842 while (strcmp (name, amo->name) == 0);
8843
8844 gas_assert (mo);
8845 create_insn (&insn, mo);
8846 for (; *fmt; ++fmt)
8847 {
8848 switch (*fmt)
8849 {
8850 case ',':
8851 case '(':
8852 case ')':
8853 case 'z':
8854 break;
8855
8856 case 'i':
8857 case 'j':
8858 macro_read_relocs (&args, r);
8859 gas_assert (*r == BFD_RELOC_GPREL16
8860 || *r == BFD_RELOC_MIPS_HIGHER
8861 || *r == BFD_RELOC_HI16_S
8862 || *r == BFD_RELOC_LO16
8863 || *r == BFD_RELOC_MIPS_GOT_OFST
8864 || (mips_opts.micromips
8865 && (*r == BFD_RELOC_16
8866 || *r == BFD_RELOC_MIPS_GOT16
8867 || *r == BFD_RELOC_MIPS_CALL16
8868 || *r == BFD_RELOC_MIPS_GOT_HI16
8869 || *r == BFD_RELOC_MIPS_GOT_LO16
8870 || *r == BFD_RELOC_MIPS_CALL_HI16
8871 || *r == BFD_RELOC_MIPS_CALL_LO16
8872 || *r == BFD_RELOC_MIPS_SUB
8873 || *r == BFD_RELOC_MIPS_GOT_PAGE
8874 || *r == BFD_RELOC_MIPS_HIGHEST
8875 || *r == BFD_RELOC_MIPS_GOT_DISP
8876 || *r == BFD_RELOC_MIPS_TLS_GD
8877 || *r == BFD_RELOC_MIPS_TLS_LDM
8878 || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
8879 || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
8880 || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
8881 || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
8882 || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
8883 break;
8884
8885 case 'o':
8886 macro_read_relocs (&args, r);
8887 break;
8888
8889 case 'u':
8890 macro_read_relocs (&args, r);
8891 gas_assert (ep != NULL
8892 && (ep->X_op == O_constant
8893 || (ep->X_op == O_symbol
8894 && (*r == BFD_RELOC_MIPS_HIGHEST
8895 || *r == BFD_RELOC_HI16_S
8896 || *r == BFD_RELOC_HI16
8897 || *r == BFD_RELOC_GPREL16
8898 || *r == BFD_RELOC_MIPS_GOT_HI16
8899 || *r == BFD_RELOC_MIPS_CALL_HI16))));
8900 break;
8901
8902 case 'p':
8903 gas_assert (ep != NULL);
8904
8905 /*
8906 * This allows macro() to pass an immediate expression for
8907 * creating short branches without creating a symbol.
8908 *
8909 * We don't allow branch relaxation for these branches, as
8910 * they should only appear in ".set nomacro" anyway.
8911 */
8912 if (ep->X_op == O_constant)
8913 {
8914 /* For microMIPS we always use relocations for branches.
8915 So we should not resolve immediate values. */
8916 gas_assert (!mips_opts.micromips);
8917
8918 if ((ep->X_add_number & 3) != 0)
8919 as_bad (_("branch to misaligned address (0x%lx)"),
8920 (unsigned long) ep->X_add_number);
8921 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8922 as_bad (_("branch address range overflow (0x%lx)"),
8923 (unsigned long) ep->X_add_number);
8924 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8925 ep = NULL;
8926 }
8927 else
8928 *r = BFD_RELOC_16_PCREL_S2;
8929 break;
8930
8931 case 'a':
8932 gas_assert (ep != NULL);
8933 *r = BFD_RELOC_MIPS_JMP;
8934 break;
8935
8936 default:
8937 operand = (mips_opts.micromips
8938 ? decode_micromips_operand (fmt)
8939 : decode_mips_operand (fmt));
8940 if (!operand)
8941 abort ();
8942
8943 uval = va_arg (args, int);
8944 if (operand->type == OP_CLO_CLZ_DEST)
8945 uval |= (uval << 5);
8946 insn_insert_operand (&insn, operand, uval);
8947
8948 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
8949 ++fmt;
8950 break;
8951 }
8952 }
8953 va_end (args);
8954 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
8955
8956 append_insn (&insn, ep, r, TRUE);
8957 }
8958
8959 static void
8960 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
8961 va_list *args)
8962 {
8963 struct mips_opcode *mo;
8964 struct mips_cl_insn insn;
8965 const struct mips_operand *operand;
8966 bfd_reloc_code_real_type r[3]
8967 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
8968
8969 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
8970 gas_assert (mo);
8971 gas_assert (strcmp (name, mo->name) == 0);
8972
8973 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
8974 {
8975 ++mo;
8976 gas_assert (mo->name);
8977 gas_assert (strcmp (name, mo->name) == 0);
8978 }
8979
8980 create_insn (&insn, mo);
8981 for (; *fmt; ++fmt)
8982 {
8983 int c;
8984
8985 c = *fmt;
8986 switch (c)
8987 {
8988 case ',':
8989 case '(':
8990 case ')':
8991 break;
8992
8993 case '.':
8994 case 'S':
8995 case 'P':
8996 case 'R':
8997 break;
8998
8999 case '<':
9000 case '5':
9001 case 'F':
9002 case 'H':
9003 case 'W':
9004 case 'D':
9005 case 'j':
9006 case '8':
9007 case 'V':
9008 case 'C':
9009 case 'U':
9010 case 'k':
9011 case 'K':
9012 case 'p':
9013 case 'q':
9014 {
9015 offsetT value;
9016
9017 gas_assert (ep != NULL);
9018
9019 if (ep->X_op != O_constant)
9020 *r = (int) BFD_RELOC_UNUSED + c;
9021 else if (calculate_reloc (*r, ep->X_add_number, &value))
9022 {
9023 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
9024 ep = NULL;
9025 *r = BFD_RELOC_UNUSED;
9026 }
9027 }
9028 break;
9029
9030 default:
9031 operand = decode_mips16_operand (c, FALSE);
9032 if (!operand)
9033 abort ();
9034
9035 insn_insert_operand (&insn, operand, va_arg (*args, int));
9036 break;
9037 }
9038 }
9039
9040 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
9041
9042 append_insn (&insn, ep, r, TRUE);
9043 }
9044
9045 /*
9046 * Generate a "jalr" instruction with a relocation hint to the called
9047 * function. This occurs in NewABI PIC code.
9048 */
9049 static void
9050 macro_build_jalr (expressionS *ep, int cprestore)
9051 {
9052 static const bfd_reloc_code_real_type jalr_relocs[2]
9053 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9054 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9055 const char *jalr;
9056 char *f = NULL;
9057
9058 if (MIPS_JALR_HINT_P (ep))
9059 {
9060 frag_grow (8);
9061 f = frag_more (0);
9062 }
9063 if (mips_opts.micromips)
9064 {
9065 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9066 ? "jalr" : "jalrs");
9067 if (MIPS_JALR_HINT_P (ep)
9068 || mips_opts.insn32
9069 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9070 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9071 else
9072 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9073 }
9074 else
9075 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
9076 if (MIPS_JALR_HINT_P (ep))
9077 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
9078 }
9079
9080 /*
9081 * Generate a "lui" instruction.
9082 */
9083 static void
9084 macro_build_lui (expressionS *ep, int regnum)
9085 {
9086 gas_assert (! mips_opts.mips16);
9087
9088 if (ep->X_op != O_constant)
9089 {
9090 gas_assert (ep->X_op == O_symbol);
9091 /* _gp_disp is a special case, used from s_cpload.
9092 __gnu_local_gp is used if mips_no_shared. */
9093 gas_assert (mips_pic == NO_PIC
9094 || (! HAVE_NEWABI
9095 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9096 || (! mips_in_shared
9097 && strcmp (S_GET_NAME (ep->X_add_symbol),
9098 "__gnu_local_gp") == 0));
9099 }
9100
9101 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
9102 }
9103
9104 /* Generate a sequence of instructions to do a load or store from a constant
9105 offset off of a base register (breg) into/from a target register (treg),
9106 using AT if necessary. */
9107 static void
9108 macro_build_ldst_constoffset (expressionS *ep, const char *op,
9109 int treg, int breg, int dbl)
9110 {
9111 gas_assert (ep->X_op == O_constant);
9112
9113 /* Sign-extending 32-bit constants makes their handling easier. */
9114 if (!dbl)
9115 normalize_constant_expr (ep);
9116
9117 /* Right now, this routine can only handle signed 32-bit constants. */
9118 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
9119 as_warn (_("operand overflow"));
9120
9121 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9122 {
9123 /* Signed 16-bit offset will fit in the op. Easy! */
9124 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
9125 }
9126 else
9127 {
9128 /* 32-bit offset, need multiple instructions and AT, like:
9129 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9130 addu $tempreg,$tempreg,$breg
9131 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9132 to handle the complete offset. */
9133 macro_build_lui (ep, AT);
9134 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9135 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
9136
9137 if (!mips_opts.at)
9138 as_bad (_("macro used $at after \".set noat\""));
9139 }
9140 }
9141
9142 /* set_at()
9143 * Generates code to set the $at register to true (one)
9144 * if reg is less than the immediate expression.
9145 */
9146 static void
9147 set_at (int reg, int unsignedp)
9148 {
9149 if (imm_expr.X_add_number >= -0x8000
9150 && imm_expr.X_add_number < 0x8000)
9151 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9152 AT, reg, BFD_RELOC_LO16);
9153 else
9154 {
9155 load_register (AT, &imm_expr, GPR_SIZE == 64);
9156 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
9157 }
9158 }
9159
9160 /* Count the leading zeroes by performing a binary chop. This is a
9161 bulky bit of source, but performance is a LOT better for the
9162 majority of values than a simple loop to count the bits:
9163 for (lcnt = 0; (lcnt < 32); lcnt++)
9164 if ((v) & (1 << (31 - lcnt)))
9165 break;
9166 However it is not code size friendly, and the gain will drop a bit
9167 on certain cached systems.
9168 */
9169 #define COUNT_TOP_ZEROES(v) \
9170 (((v) & ~0xffff) == 0 \
9171 ? ((v) & ~0xff) == 0 \
9172 ? ((v) & ~0xf) == 0 \
9173 ? ((v) & ~0x3) == 0 \
9174 ? ((v) & ~0x1) == 0 \
9175 ? !(v) \
9176 ? 32 \
9177 : 31 \
9178 : 30 \
9179 : ((v) & ~0x7) == 0 \
9180 ? 29 \
9181 : 28 \
9182 : ((v) & ~0x3f) == 0 \
9183 ? ((v) & ~0x1f) == 0 \
9184 ? 27 \
9185 : 26 \
9186 : ((v) & ~0x7f) == 0 \
9187 ? 25 \
9188 : 24 \
9189 : ((v) & ~0xfff) == 0 \
9190 ? ((v) & ~0x3ff) == 0 \
9191 ? ((v) & ~0x1ff) == 0 \
9192 ? 23 \
9193 : 22 \
9194 : ((v) & ~0x7ff) == 0 \
9195 ? 21 \
9196 : 20 \
9197 : ((v) & ~0x3fff) == 0 \
9198 ? ((v) & ~0x1fff) == 0 \
9199 ? 19 \
9200 : 18 \
9201 : ((v) & ~0x7fff) == 0 \
9202 ? 17 \
9203 : 16 \
9204 : ((v) & ~0xffffff) == 0 \
9205 ? ((v) & ~0xfffff) == 0 \
9206 ? ((v) & ~0x3ffff) == 0 \
9207 ? ((v) & ~0x1ffff) == 0 \
9208 ? 15 \
9209 : 14 \
9210 : ((v) & ~0x7ffff) == 0 \
9211 ? 13 \
9212 : 12 \
9213 : ((v) & ~0x3fffff) == 0 \
9214 ? ((v) & ~0x1fffff) == 0 \
9215 ? 11 \
9216 : 10 \
9217 : ((v) & ~0x7fffff) == 0 \
9218 ? 9 \
9219 : 8 \
9220 : ((v) & ~0xfffffff) == 0 \
9221 ? ((v) & ~0x3ffffff) == 0 \
9222 ? ((v) & ~0x1ffffff) == 0 \
9223 ? 7 \
9224 : 6 \
9225 : ((v) & ~0x7ffffff) == 0 \
9226 ? 5 \
9227 : 4 \
9228 : ((v) & ~0x3fffffff) == 0 \
9229 ? ((v) & ~0x1fffffff) == 0 \
9230 ? 3 \
9231 : 2 \
9232 : ((v) & ~0x7fffffff) == 0 \
9233 ? 1 \
9234 : 0)
9235
9236 /* load_register()
9237 * This routine generates the least number of instructions necessary to load
9238 * an absolute expression value into a register.
9239 */
9240 static void
9241 load_register (int reg, expressionS *ep, int dbl)
9242 {
9243 int freg;
9244 expressionS hi32, lo32;
9245
9246 if (ep->X_op != O_big)
9247 {
9248 gas_assert (ep->X_op == O_constant);
9249
9250 /* Sign-extending 32-bit constants makes their handling easier. */
9251 if (!dbl)
9252 normalize_constant_expr (ep);
9253
9254 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
9255 {
9256 /* We can handle 16 bit signed values with an addiu to
9257 $zero. No need to ever use daddiu here, since $zero and
9258 the result are always correct in 32 bit mode. */
9259 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9260 return;
9261 }
9262 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9263 {
9264 /* We can handle 16 bit unsigned values with an ori to
9265 $zero. */
9266 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9267 return;
9268 }
9269 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
9270 {
9271 /* 32 bit values require an lui. */
9272 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9273 if ((ep->X_add_number & 0xffff) != 0)
9274 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9275 return;
9276 }
9277 }
9278
9279 /* The value is larger than 32 bits. */
9280
9281 if (!dbl || GPR_SIZE == 32)
9282 {
9283 char value[32];
9284
9285 sprintf_vma (value, ep->X_add_number);
9286 as_bad (_("number (0x%s) larger than 32 bits"), value);
9287 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9288 return;
9289 }
9290
9291 if (ep->X_op != O_big)
9292 {
9293 hi32 = *ep;
9294 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9295 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9296 hi32.X_add_number &= 0xffffffff;
9297 lo32 = *ep;
9298 lo32.X_add_number &= 0xffffffff;
9299 }
9300 else
9301 {
9302 gas_assert (ep->X_add_number > 2);
9303 if (ep->X_add_number == 3)
9304 generic_bignum[3] = 0;
9305 else if (ep->X_add_number > 4)
9306 as_bad (_("number larger than 64 bits"));
9307 lo32.X_op = O_constant;
9308 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9309 hi32.X_op = O_constant;
9310 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9311 }
9312
9313 if (hi32.X_add_number == 0)
9314 freg = 0;
9315 else
9316 {
9317 int shift, bit;
9318 unsigned long hi, lo;
9319
9320 if (hi32.X_add_number == (offsetT) 0xffffffff)
9321 {
9322 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9323 {
9324 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9325 return;
9326 }
9327 if (lo32.X_add_number & 0x80000000)
9328 {
9329 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9330 if (lo32.X_add_number & 0xffff)
9331 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9332 return;
9333 }
9334 }
9335
9336 /* Check for 16bit shifted constant. We know that hi32 is
9337 non-zero, so start the mask on the first bit of the hi32
9338 value. */
9339 shift = 17;
9340 do
9341 {
9342 unsigned long himask, lomask;
9343
9344 if (shift < 32)
9345 {
9346 himask = 0xffff >> (32 - shift);
9347 lomask = (0xffff << shift) & 0xffffffff;
9348 }
9349 else
9350 {
9351 himask = 0xffff << (shift - 32);
9352 lomask = 0;
9353 }
9354 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9355 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9356 {
9357 expressionS tmp;
9358
9359 tmp.X_op = O_constant;
9360 if (shift < 32)
9361 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9362 | (lo32.X_add_number >> shift));
9363 else
9364 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
9365 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9366 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9367 reg, reg, (shift >= 32) ? shift - 32 : shift);
9368 return;
9369 }
9370 ++shift;
9371 }
9372 while (shift <= (64 - 16));
9373
9374 /* Find the bit number of the lowest one bit, and store the
9375 shifted value in hi/lo. */
9376 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9377 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9378 if (lo != 0)
9379 {
9380 bit = 0;
9381 while ((lo & 1) == 0)
9382 {
9383 lo >>= 1;
9384 ++bit;
9385 }
9386 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9387 hi >>= bit;
9388 }
9389 else
9390 {
9391 bit = 32;
9392 while ((hi & 1) == 0)
9393 {
9394 hi >>= 1;
9395 ++bit;
9396 }
9397 lo = hi;
9398 hi = 0;
9399 }
9400
9401 /* Optimize if the shifted value is a (power of 2) - 1. */
9402 if ((hi == 0 && ((lo + 1) & lo) == 0)
9403 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
9404 {
9405 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
9406 if (shift != 0)
9407 {
9408 expressionS tmp;
9409
9410 /* This instruction will set the register to be all
9411 ones. */
9412 tmp.X_op = O_constant;
9413 tmp.X_add_number = (offsetT) -1;
9414 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9415 if (bit != 0)
9416 {
9417 bit += shift;
9418 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9419 reg, reg, (bit >= 32) ? bit - 32 : bit);
9420 }
9421 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
9422 reg, reg, (shift >= 32) ? shift - 32 : shift);
9423 return;
9424 }
9425 }
9426
9427 /* Sign extend hi32 before calling load_register, because we can
9428 generally get better code when we load a sign extended value. */
9429 if ((hi32.X_add_number & 0x80000000) != 0)
9430 hi32.X_add_number |= ~(offsetT) 0xffffffff;
9431 load_register (reg, &hi32, 0);
9432 freg = reg;
9433 }
9434 if ((lo32.X_add_number & 0xffff0000) == 0)
9435 {
9436 if (freg != 0)
9437 {
9438 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
9439 freg = reg;
9440 }
9441 }
9442 else
9443 {
9444 expressionS mid16;
9445
9446 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
9447 {
9448 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9449 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
9450 return;
9451 }
9452
9453 if (freg != 0)
9454 {
9455 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
9456 freg = reg;
9457 }
9458 mid16 = lo32;
9459 mid16.X_add_number >>= 16;
9460 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9461 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9462 freg = reg;
9463 }
9464 if ((lo32.X_add_number & 0xffff) != 0)
9465 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9466 }
9467
9468 static inline void
9469 load_delay_nop (void)
9470 {
9471 if (!gpr_interlocks)
9472 macro_build (NULL, "nop", "");
9473 }
9474
9475 /* Load an address into a register. */
9476
9477 static void
9478 load_address (int reg, expressionS *ep, int *used_at)
9479 {
9480 if (ep->X_op != O_constant
9481 && ep->X_op != O_symbol)
9482 {
9483 as_bad (_("expression too complex"));
9484 ep->X_op = O_constant;
9485 }
9486
9487 if (ep->X_op == O_constant)
9488 {
9489 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
9490 return;
9491 }
9492
9493 if (mips_pic == NO_PIC)
9494 {
9495 /* If this is a reference to a GP relative symbol, we want
9496 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
9497 Otherwise we want
9498 lui $reg,<sym> (BFD_RELOC_HI16_S)
9499 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9500 If we have an addend, we always use the latter form.
9501
9502 With 64bit address space and a usable $at we want
9503 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9504 lui $at,<sym> (BFD_RELOC_HI16_S)
9505 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9506 daddiu $at,<sym> (BFD_RELOC_LO16)
9507 dsll32 $reg,0
9508 daddu $reg,$reg,$at
9509
9510 If $at is already in use, we use a path which is suboptimal
9511 on superscalar processors.
9512 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9513 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9514 dsll $reg,16
9515 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9516 dsll $reg,16
9517 daddiu $reg,<sym> (BFD_RELOC_LO16)
9518
9519 For GP relative symbols in 64bit address space we can use
9520 the same sequence as in 32bit address space. */
9521 if (HAVE_64BIT_SYMBOLS)
9522 {
9523 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9524 && !nopic_need_relax (ep->X_add_symbol, 1))
9525 {
9526 relax_start (ep->X_add_symbol);
9527 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9528 mips_gp_register, BFD_RELOC_GPREL16);
9529 relax_switch ();
9530 }
9531
9532 if (*used_at == 0 && mips_opts.at)
9533 {
9534 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9535 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
9536 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9537 BFD_RELOC_MIPS_HIGHER);
9538 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
9539 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
9540 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
9541 *used_at = 1;
9542 }
9543 else
9544 {
9545 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9546 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9547 BFD_RELOC_MIPS_HIGHER);
9548 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9549 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
9550 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9551 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
9552 }
9553
9554 if (mips_relax.sequence)
9555 relax_end ();
9556 }
9557 else
9558 {
9559 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9560 && !nopic_need_relax (ep->X_add_symbol, 1))
9561 {
9562 relax_start (ep->X_add_symbol);
9563 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9564 mips_gp_register, BFD_RELOC_GPREL16);
9565 relax_switch ();
9566 }
9567 macro_build_lui (ep, reg);
9568 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9569 reg, reg, BFD_RELOC_LO16);
9570 if (mips_relax.sequence)
9571 relax_end ();
9572 }
9573 }
9574 else if (!mips_big_got)
9575 {
9576 expressionS ex;
9577
9578 /* If this is a reference to an external symbol, we want
9579 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9580 Otherwise we want
9581 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9582 nop
9583 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9584 If there is a constant, it must be added in after.
9585
9586 If we have NewABI, we want
9587 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9588 unless we're referencing a global symbol with a non-zero
9589 offset, in which case cst must be added separately. */
9590 if (HAVE_NEWABI)
9591 {
9592 if (ep->X_add_number)
9593 {
9594 ex.X_add_number = ep->X_add_number;
9595 ep->X_add_number = 0;
9596 relax_start (ep->X_add_symbol);
9597 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9598 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9599 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9600 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9601 ex.X_op = O_constant;
9602 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9603 reg, reg, BFD_RELOC_LO16);
9604 ep->X_add_number = ex.X_add_number;
9605 relax_switch ();
9606 }
9607 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9608 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9609 if (mips_relax.sequence)
9610 relax_end ();
9611 }
9612 else
9613 {
9614 ex.X_add_number = ep->X_add_number;
9615 ep->X_add_number = 0;
9616 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9617 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9618 load_delay_nop ();
9619 relax_start (ep->X_add_symbol);
9620 relax_switch ();
9621 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9622 BFD_RELOC_LO16);
9623 relax_end ();
9624
9625 if (ex.X_add_number != 0)
9626 {
9627 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9628 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9629 ex.X_op = O_constant;
9630 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9631 reg, reg, BFD_RELOC_LO16);
9632 }
9633 }
9634 }
9635 else if (mips_big_got)
9636 {
9637 expressionS ex;
9638
9639 /* This is the large GOT case. If this is a reference to an
9640 external symbol, we want
9641 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9642 addu $reg,$reg,$gp
9643 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
9644
9645 Otherwise, for a reference to a local symbol in old ABI, we want
9646 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9647 nop
9648 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9649 If there is a constant, it must be added in after.
9650
9651 In the NewABI, for local symbols, with or without offsets, we want:
9652 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9653 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
9654 */
9655 if (HAVE_NEWABI)
9656 {
9657 ex.X_add_number = ep->X_add_number;
9658 ep->X_add_number = 0;
9659 relax_start (ep->X_add_symbol);
9660 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9661 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9662 reg, reg, mips_gp_register);
9663 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9664 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9665 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9666 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9667 else if (ex.X_add_number)
9668 {
9669 ex.X_op = O_constant;
9670 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9671 BFD_RELOC_LO16);
9672 }
9673
9674 ep->X_add_number = ex.X_add_number;
9675 relax_switch ();
9676 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9677 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9678 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9679 BFD_RELOC_MIPS_GOT_OFST);
9680 relax_end ();
9681 }
9682 else
9683 {
9684 ex.X_add_number = ep->X_add_number;
9685 ep->X_add_number = 0;
9686 relax_start (ep->X_add_symbol);
9687 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9688 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9689 reg, reg, mips_gp_register);
9690 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9691 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9692 relax_switch ();
9693 if (reg_needs_delay (mips_gp_register))
9694 {
9695 /* We need a nop before loading from $gp. This special
9696 check is required because the lui which starts the main
9697 instruction stream does not refer to $gp, and so will not
9698 insert the nop which may be required. */
9699 macro_build (NULL, "nop", "");
9700 }
9701 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9702 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9703 load_delay_nop ();
9704 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9705 BFD_RELOC_LO16);
9706 relax_end ();
9707
9708 if (ex.X_add_number != 0)
9709 {
9710 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9711 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9712 ex.X_op = O_constant;
9713 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9714 BFD_RELOC_LO16);
9715 }
9716 }
9717 }
9718 else
9719 abort ();
9720
9721 if (!mips_opts.at && *used_at == 1)
9722 as_bad (_("macro used $at after \".set noat\""));
9723 }
9724
9725 /* Move the contents of register SOURCE into register DEST. */
9726
9727 static void
9728 move_register (int dest, int source)
9729 {
9730 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9731 instruction specifically requires a 32-bit one. */
9732 if (mips_opts.micromips
9733 && !mips_opts.insn32
9734 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9735 macro_build (NULL, "move", "mp,mj", dest, source);
9736 else
9737 macro_build (NULL, "or", "d,v,t", dest, source, 0);
9738 }
9739
9740 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
9741 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9742 The two alternatives are:
9743
9744 Global symbol Local symbol
9745 ------------- ------------
9746 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9747 ... ...
9748 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9749
9750 load_got_offset emits the first instruction and add_got_offset
9751 emits the second for a 16-bit offset or add_got_offset_hilo emits
9752 a sequence to add a 32-bit offset using a scratch register. */
9753
9754 static void
9755 load_got_offset (int dest, expressionS *local)
9756 {
9757 expressionS global;
9758
9759 global = *local;
9760 global.X_add_number = 0;
9761
9762 relax_start (local->X_add_symbol);
9763 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9764 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9765 relax_switch ();
9766 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9767 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9768 relax_end ();
9769 }
9770
9771 static void
9772 add_got_offset (int dest, expressionS *local)
9773 {
9774 expressionS global;
9775
9776 global.X_op = O_constant;
9777 global.X_op_symbol = NULL;
9778 global.X_add_symbol = NULL;
9779 global.X_add_number = local->X_add_number;
9780
9781 relax_start (local->X_add_symbol);
9782 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
9783 dest, dest, BFD_RELOC_LO16);
9784 relax_switch ();
9785 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
9786 relax_end ();
9787 }
9788
9789 static void
9790 add_got_offset_hilo (int dest, expressionS *local, int tmp)
9791 {
9792 expressionS global;
9793 int hold_mips_optimize;
9794
9795 global.X_op = O_constant;
9796 global.X_op_symbol = NULL;
9797 global.X_add_symbol = NULL;
9798 global.X_add_number = local->X_add_number;
9799
9800 relax_start (local->X_add_symbol);
9801 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9802 relax_switch ();
9803 /* Set mips_optimize around the lui instruction to avoid
9804 inserting an unnecessary nop after the lw. */
9805 hold_mips_optimize = mips_optimize;
9806 mips_optimize = 2;
9807 macro_build_lui (&global, tmp);
9808 mips_optimize = hold_mips_optimize;
9809 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9810 relax_end ();
9811
9812 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9813 }
9814
9815 /* Emit a sequence of instructions to emulate a branch likely operation.
9816 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9817 is its complementing branch with the original condition negated.
9818 CALL is set if the original branch specified the link operation.
9819 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9820
9821 Code like this is produced in the noreorder mode:
9822
9823 BRNEG <args>, 1f
9824 nop
9825 b <sym>
9826 delay slot (executed only if branch taken)
9827 1:
9828
9829 or, if CALL is set:
9830
9831 BRNEG <args>, 1f
9832 nop
9833 bal <sym>
9834 delay slot (executed only if branch taken)
9835 1:
9836
9837 In the reorder mode the delay slot would be filled with a nop anyway,
9838 so code produced is simply:
9839
9840 BR <args>, <sym>
9841 nop
9842
9843 This function is used when producing code for the microMIPS ASE that
9844 does not implement branch likely instructions in hardware. */
9845
9846 static void
9847 macro_build_branch_likely (const char *br, const char *brneg,
9848 int call, expressionS *ep, const char *fmt,
9849 unsigned int sreg, unsigned int treg)
9850 {
9851 int noreorder = mips_opts.noreorder;
9852 expressionS expr1;
9853
9854 gas_assert (mips_opts.micromips);
9855 start_noreorder ();
9856 if (noreorder)
9857 {
9858 micromips_label_expr (&expr1);
9859 macro_build (&expr1, brneg, fmt, sreg, treg);
9860 macro_build (NULL, "nop", "");
9861 macro_build (ep, call ? "bal" : "b", "p");
9862
9863 /* Set to true so that append_insn adds a label. */
9864 emit_branch_likely_macro = TRUE;
9865 }
9866 else
9867 {
9868 macro_build (ep, br, fmt, sreg, treg);
9869 macro_build (NULL, "nop", "");
9870 }
9871 end_noreorder ();
9872 }
9873
9874 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9875 the condition code tested. EP specifies the branch target. */
9876
9877 static void
9878 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9879 {
9880 const int call = 0;
9881 const char *brneg;
9882 const char *br;
9883
9884 switch (type)
9885 {
9886 case M_BC1FL:
9887 br = "bc1f";
9888 brneg = "bc1t";
9889 break;
9890 case M_BC1TL:
9891 br = "bc1t";
9892 brneg = "bc1f";
9893 break;
9894 case M_BC2FL:
9895 br = "bc2f";
9896 brneg = "bc2t";
9897 break;
9898 case M_BC2TL:
9899 br = "bc2t";
9900 brneg = "bc2f";
9901 break;
9902 default:
9903 abort ();
9904 }
9905 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9906 }
9907
9908 /* Emit a two-argument branch macro specified by TYPE, using SREG as
9909 the register tested. EP specifies the branch target. */
9910
9911 static void
9912 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9913 {
9914 const char *brneg = NULL;
9915 const char *br;
9916 int call = 0;
9917
9918 switch (type)
9919 {
9920 case M_BGEZ:
9921 br = "bgez";
9922 break;
9923 case M_BGEZL:
9924 br = mips_opts.micromips ? "bgez" : "bgezl";
9925 brneg = "bltz";
9926 break;
9927 case M_BGEZALL:
9928 gas_assert (mips_opts.micromips);
9929 br = mips_opts.insn32 ? "bgezal" : "bgezals";
9930 brneg = "bltz";
9931 call = 1;
9932 break;
9933 case M_BGTZ:
9934 br = "bgtz";
9935 break;
9936 case M_BGTZL:
9937 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9938 brneg = "blez";
9939 break;
9940 case M_BLEZ:
9941 br = "blez";
9942 break;
9943 case M_BLEZL:
9944 br = mips_opts.micromips ? "blez" : "blezl";
9945 brneg = "bgtz";
9946 break;
9947 case M_BLTZ:
9948 br = "bltz";
9949 break;
9950 case M_BLTZL:
9951 br = mips_opts.micromips ? "bltz" : "bltzl";
9952 brneg = "bgez";
9953 break;
9954 case M_BLTZALL:
9955 gas_assert (mips_opts.micromips);
9956 br = mips_opts.insn32 ? "bltzal" : "bltzals";
9957 brneg = "bgez";
9958 call = 1;
9959 break;
9960 default:
9961 abort ();
9962 }
9963 if (mips_opts.micromips && brneg)
9964 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9965 else
9966 macro_build (ep, br, "s,p", sreg);
9967 }
9968
9969 /* Emit a three-argument branch macro specified by TYPE, using SREG and
9970 TREG as the registers tested. EP specifies the branch target. */
9971
9972 static void
9973 macro_build_branch_rsrt (int type, expressionS *ep,
9974 unsigned int sreg, unsigned int treg)
9975 {
9976 const char *brneg = NULL;
9977 const int call = 0;
9978 const char *br;
9979
9980 switch (type)
9981 {
9982 case M_BEQ:
9983 case M_BEQ_I:
9984 br = "beq";
9985 break;
9986 case M_BEQL:
9987 case M_BEQL_I:
9988 br = mips_opts.micromips ? "beq" : "beql";
9989 brneg = "bne";
9990 break;
9991 case M_BNE:
9992 case M_BNE_I:
9993 br = "bne";
9994 break;
9995 case M_BNEL:
9996 case M_BNEL_I:
9997 br = mips_opts.micromips ? "bne" : "bnel";
9998 brneg = "beq";
9999 break;
10000 default:
10001 abort ();
10002 }
10003 if (mips_opts.micromips && brneg)
10004 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10005 else
10006 macro_build (ep, br, "s,t,p", sreg, treg);
10007 }
10008
10009 /* Return the high part that should be loaded in order to make the low
10010 part of VALUE accessible using an offset of OFFBITS bits. */
10011
10012 static offsetT
10013 offset_high_part (offsetT value, unsigned int offbits)
10014 {
10015 offsetT bias;
10016 addressT low_mask;
10017
10018 if (offbits == 0)
10019 return value;
10020 bias = 1 << (offbits - 1);
10021 low_mask = bias * 2 - 1;
10022 return (value + bias) & ~low_mask;
10023 }
10024
10025 /* Return true if the value stored in offset_expr and offset_reloc
10026 fits into a signed offset of OFFBITS bits. RANGE is the maximum
10027 amount that the caller wants to add without inducing overflow
10028 and ALIGN is the known alignment of the value in bytes. */
10029
10030 static bfd_boolean
10031 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10032 {
10033 if (offbits == 16)
10034 {
10035 /* Accept any relocation operator if overflow isn't a concern. */
10036 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10037 return TRUE;
10038
10039 /* These relocations are guaranteed not to overflow in correct links. */
10040 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10041 || gprel16_reloc_p (*offset_reloc))
10042 return TRUE;
10043 }
10044 if (offset_expr.X_op == O_constant
10045 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10046 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10047 return TRUE;
10048 return FALSE;
10049 }
10050
10051 /*
10052 * Build macros
10053 * This routine implements the seemingly endless macro or synthesized
10054 * instructions and addressing modes in the mips assembly language. Many
10055 * of these macros are simple and are similar to each other. These could
10056 * probably be handled by some kind of table or grammar approach instead of
10057 * this verbose method. Others are not simple macros but are more like
10058 * optimizing code generation.
10059 * One interesting optimization is when several store macros appear
10060 * consecutively that would load AT with the upper half of the same address.
10061 * The ensuing load upper instructions are omitted. This implies some kind
10062 * of global optimization. We currently only optimize within a single macro.
10063 * For many of the load and store macros if the address is specified as a
10064 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10065 * first load register 'at' with zero and use it as the base register. The
10066 * mips assembler simply uses register $zero. Just one tiny optimization
10067 * we're missing.
10068 */
10069 static void
10070 macro (struct mips_cl_insn *ip, char *str)
10071 {
10072 const struct mips_operand_array *operands;
10073 unsigned int breg, i;
10074 unsigned int tempreg;
10075 int mask;
10076 int used_at = 0;
10077 expressionS label_expr;
10078 expressionS expr1;
10079 expressionS *ep;
10080 const char *s;
10081 const char *s2;
10082 const char *fmt;
10083 int likely = 0;
10084 int coproc = 0;
10085 int offbits = 16;
10086 int call = 0;
10087 int jals = 0;
10088 int dbl = 0;
10089 int imm = 0;
10090 int ust = 0;
10091 int lp = 0;
10092 bfd_boolean large_offset;
10093 int off;
10094 int hold_mips_optimize;
10095 unsigned int align;
10096 unsigned int op[MAX_OPERANDS];
10097
10098 gas_assert (! mips_opts.mips16);
10099
10100 operands = insn_operands (ip);
10101 for (i = 0; i < MAX_OPERANDS; i++)
10102 if (operands->operand[i])
10103 op[i] = insn_extract_operand (ip, operands->operand[i]);
10104 else
10105 op[i] = -1;
10106
10107 mask = ip->insn_mo->mask;
10108
10109 label_expr.X_op = O_constant;
10110 label_expr.X_op_symbol = NULL;
10111 label_expr.X_add_symbol = NULL;
10112 label_expr.X_add_number = 0;
10113
10114 expr1.X_op = O_constant;
10115 expr1.X_op_symbol = NULL;
10116 expr1.X_add_symbol = NULL;
10117 expr1.X_add_number = 1;
10118 align = 1;
10119
10120 switch (mask)
10121 {
10122 case M_DABS:
10123 dbl = 1;
10124 /* Fall through. */
10125 case M_ABS:
10126 /* bgez $a0,1f
10127 move v0,$a0
10128 sub v0,$zero,$a0
10129 1:
10130 */
10131
10132 start_noreorder ();
10133
10134 if (mips_opts.micromips)
10135 micromips_label_expr (&label_expr);
10136 else
10137 label_expr.X_add_number = 8;
10138 macro_build (&label_expr, "bgez", "s,p", op[1]);
10139 if (op[0] == op[1])
10140 macro_build (NULL, "nop", "");
10141 else
10142 move_register (op[0], op[1]);
10143 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
10144 if (mips_opts.micromips)
10145 micromips_add_label ();
10146
10147 end_noreorder ();
10148 break;
10149
10150 case M_ADD_I:
10151 s = "addi";
10152 s2 = "add";
10153 goto do_addi;
10154 case M_ADDU_I:
10155 s = "addiu";
10156 s2 = "addu";
10157 goto do_addi;
10158 case M_DADD_I:
10159 dbl = 1;
10160 s = "daddi";
10161 s2 = "dadd";
10162 if (!mips_opts.micromips)
10163 goto do_addi;
10164 if (imm_expr.X_add_number >= -0x200
10165 && imm_expr.X_add_number < 0x200)
10166 {
10167 macro_build (NULL, s, "t,r,.", op[0], op[1],
10168 (int) imm_expr.X_add_number);
10169 break;
10170 }
10171 goto do_addi_i;
10172 case M_DADDU_I:
10173 dbl = 1;
10174 s = "daddiu";
10175 s2 = "daddu";
10176 do_addi:
10177 if (imm_expr.X_add_number >= -0x8000
10178 && imm_expr.X_add_number < 0x8000)
10179 {
10180 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
10181 break;
10182 }
10183 do_addi_i:
10184 used_at = 1;
10185 load_register (AT, &imm_expr, dbl);
10186 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10187 break;
10188
10189 case M_AND_I:
10190 s = "andi";
10191 s2 = "and";
10192 goto do_bit;
10193 case M_OR_I:
10194 s = "ori";
10195 s2 = "or";
10196 goto do_bit;
10197 case M_NOR_I:
10198 s = "";
10199 s2 = "nor";
10200 goto do_bit;
10201 case M_XOR_I:
10202 s = "xori";
10203 s2 = "xor";
10204 do_bit:
10205 if (imm_expr.X_add_number >= 0
10206 && imm_expr.X_add_number < 0x10000)
10207 {
10208 if (mask != M_NOR_I)
10209 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
10210 else
10211 {
10212 macro_build (&imm_expr, "ori", "t,r,i",
10213 op[0], op[1], BFD_RELOC_LO16);
10214 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
10215 }
10216 break;
10217 }
10218
10219 used_at = 1;
10220 load_register (AT, &imm_expr, GPR_SIZE == 64);
10221 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10222 break;
10223
10224 case M_BALIGN:
10225 switch (imm_expr.X_add_number)
10226 {
10227 case 0:
10228 macro_build (NULL, "nop", "");
10229 break;
10230 case 2:
10231 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
10232 break;
10233 case 1:
10234 case 3:
10235 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
10236 (int) imm_expr.X_add_number);
10237 break;
10238 default:
10239 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10240 (unsigned long) imm_expr.X_add_number);
10241 break;
10242 }
10243 break;
10244
10245 case M_BC1FL:
10246 case M_BC1TL:
10247 case M_BC2FL:
10248 case M_BC2TL:
10249 gas_assert (mips_opts.micromips);
10250 macro_build_branch_ccl (mask, &offset_expr,
10251 EXTRACT_OPERAND (1, BCC, *ip));
10252 break;
10253
10254 case M_BEQ_I:
10255 case M_BEQL_I:
10256 case M_BNE_I:
10257 case M_BNEL_I:
10258 if (imm_expr.X_add_number == 0)
10259 op[1] = 0;
10260 else
10261 {
10262 op[1] = AT;
10263 used_at = 1;
10264 load_register (op[1], &imm_expr, GPR_SIZE == 64);
10265 }
10266 /* Fall through. */
10267 case M_BEQL:
10268 case M_BNEL:
10269 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
10270 break;
10271
10272 case M_BGEL:
10273 likely = 1;
10274 /* Fall through. */
10275 case M_BGE:
10276 if (op[1] == 0)
10277 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10278 else if (op[0] == 0)
10279 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
10280 else
10281 {
10282 used_at = 1;
10283 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10284 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10285 &offset_expr, AT, ZERO);
10286 }
10287 break;
10288
10289 case M_BGEZL:
10290 case M_BGEZALL:
10291 case M_BGTZL:
10292 case M_BLEZL:
10293 case M_BLTZL:
10294 case M_BLTZALL:
10295 macro_build_branch_rs (mask, &offset_expr, op[0]);
10296 break;
10297
10298 case M_BGTL_I:
10299 likely = 1;
10300 /* Fall through. */
10301 case M_BGT_I:
10302 /* Check for > max integer. */
10303 if (imm_expr.X_add_number >= GPR_SMAX)
10304 {
10305 do_false:
10306 /* Result is always false. */
10307 if (! likely)
10308 macro_build (NULL, "nop", "");
10309 else
10310 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
10311 break;
10312 }
10313 ++imm_expr.X_add_number;
10314 /* FALLTHROUGH */
10315 case M_BGE_I:
10316 case M_BGEL_I:
10317 if (mask == M_BGEL_I)
10318 likely = 1;
10319 if (imm_expr.X_add_number == 0)
10320 {
10321 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
10322 &offset_expr, op[0]);
10323 break;
10324 }
10325 if (imm_expr.X_add_number == 1)
10326 {
10327 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
10328 &offset_expr, op[0]);
10329 break;
10330 }
10331 if (imm_expr.X_add_number <= GPR_SMIN)
10332 {
10333 do_true:
10334 /* result is always true */
10335 as_warn (_("branch %s is always true"), ip->insn_mo->name);
10336 macro_build (&offset_expr, "b", "p");
10337 break;
10338 }
10339 used_at = 1;
10340 set_at (op[0], 0);
10341 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10342 &offset_expr, AT, ZERO);
10343 break;
10344
10345 case M_BGEUL:
10346 likely = 1;
10347 /* Fall through. */
10348 case M_BGEU:
10349 if (op[1] == 0)
10350 goto do_true;
10351 else if (op[0] == 0)
10352 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10353 &offset_expr, ZERO, op[1]);
10354 else
10355 {
10356 used_at = 1;
10357 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10358 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10359 &offset_expr, AT, ZERO);
10360 }
10361 break;
10362
10363 case M_BGTUL_I:
10364 likely = 1;
10365 /* Fall through. */
10366 case M_BGTU_I:
10367 if (op[0] == 0
10368 || (GPR_SIZE == 32
10369 && imm_expr.X_add_number == -1))
10370 goto do_false;
10371 ++imm_expr.X_add_number;
10372 /* FALLTHROUGH */
10373 case M_BGEU_I:
10374 case M_BGEUL_I:
10375 if (mask == M_BGEUL_I)
10376 likely = 1;
10377 if (imm_expr.X_add_number == 0)
10378 goto do_true;
10379 else if (imm_expr.X_add_number == 1)
10380 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10381 &offset_expr, op[0], ZERO);
10382 else
10383 {
10384 used_at = 1;
10385 set_at (op[0], 1);
10386 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10387 &offset_expr, AT, ZERO);
10388 }
10389 break;
10390
10391 case M_BGTL:
10392 likely = 1;
10393 /* Fall through. */
10394 case M_BGT:
10395 if (op[1] == 0)
10396 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10397 else if (op[0] == 0)
10398 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
10399 else
10400 {
10401 used_at = 1;
10402 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10403 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10404 &offset_expr, AT, ZERO);
10405 }
10406 break;
10407
10408 case M_BGTUL:
10409 likely = 1;
10410 /* Fall through. */
10411 case M_BGTU:
10412 if (op[1] == 0)
10413 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10414 &offset_expr, op[0], ZERO);
10415 else if (op[0] == 0)
10416 goto do_false;
10417 else
10418 {
10419 used_at = 1;
10420 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10421 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10422 &offset_expr, AT, ZERO);
10423 }
10424 break;
10425
10426 case M_BLEL:
10427 likely = 1;
10428 /* Fall through. */
10429 case M_BLE:
10430 if (op[1] == 0)
10431 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10432 else if (op[0] == 0)
10433 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
10434 else
10435 {
10436 used_at = 1;
10437 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10438 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10439 &offset_expr, AT, ZERO);
10440 }
10441 break;
10442
10443 case M_BLEL_I:
10444 likely = 1;
10445 /* Fall through. */
10446 case M_BLE_I:
10447 if (imm_expr.X_add_number >= GPR_SMAX)
10448 goto do_true;
10449 ++imm_expr.X_add_number;
10450 /* FALLTHROUGH */
10451 case M_BLT_I:
10452 case M_BLTL_I:
10453 if (mask == M_BLTL_I)
10454 likely = 1;
10455 if (imm_expr.X_add_number == 0)
10456 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10457 else if (imm_expr.X_add_number == 1)
10458 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10459 else
10460 {
10461 used_at = 1;
10462 set_at (op[0], 0);
10463 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10464 &offset_expr, AT, ZERO);
10465 }
10466 break;
10467
10468 case M_BLEUL:
10469 likely = 1;
10470 /* Fall through. */
10471 case M_BLEU:
10472 if (op[1] == 0)
10473 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10474 &offset_expr, op[0], ZERO);
10475 else if (op[0] == 0)
10476 goto do_true;
10477 else
10478 {
10479 used_at = 1;
10480 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10481 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10482 &offset_expr, AT, ZERO);
10483 }
10484 break;
10485
10486 case M_BLEUL_I:
10487 likely = 1;
10488 /* Fall through. */
10489 case M_BLEU_I:
10490 if (op[0] == 0
10491 || (GPR_SIZE == 32
10492 && imm_expr.X_add_number == -1))
10493 goto do_true;
10494 ++imm_expr.X_add_number;
10495 /* FALLTHROUGH */
10496 case M_BLTU_I:
10497 case M_BLTUL_I:
10498 if (mask == M_BLTUL_I)
10499 likely = 1;
10500 if (imm_expr.X_add_number == 0)
10501 goto do_false;
10502 else if (imm_expr.X_add_number == 1)
10503 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10504 &offset_expr, op[0], ZERO);
10505 else
10506 {
10507 used_at = 1;
10508 set_at (op[0], 1);
10509 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10510 &offset_expr, AT, ZERO);
10511 }
10512 break;
10513
10514 case M_BLTL:
10515 likely = 1;
10516 /* Fall through. */
10517 case M_BLT:
10518 if (op[1] == 0)
10519 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10520 else if (op[0] == 0)
10521 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
10522 else
10523 {
10524 used_at = 1;
10525 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10526 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10527 &offset_expr, AT, ZERO);
10528 }
10529 break;
10530
10531 case M_BLTUL:
10532 likely = 1;
10533 /* Fall through. */
10534 case M_BLTU:
10535 if (op[1] == 0)
10536 goto do_false;
10537 else if (op[0] == 0)
10538 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10539 &offset_expr, ZERO, op[1]);
10540 else
10541 {
10542 used_at = 1;
10543 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10544 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10545 &offset_expr, AT, ZERO);
10546 }
10547 break;
10548
10549 case M_DDIV_3:
10550 dbl = 1;
10551 /* Fall through. */
10552 case M_DIV_3:
10553 s = "mflo";
10554 goto do_div3;
10555 case M_DREM_3:
10556 dbl = 1;
10557 /* Fall through. */
10558 case M_REM_3:
10559 s = "mfhi";
10560 do_div3:
10561 if (op[2] == 0)
10562 {
10563 as_warn (_("divide by zero"));
10564 if (mips_trap)
10565 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10566 else
10567 macro_build (NULL, "break", BRK_FMT, 7);
10568 break;
10569 }
10570
10571 start_noreorder ();
10572 if (mips_trap)
10573 {
10574 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10575 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10576 }
10577 else
10578 {
10579 if (mips_opts.micromips)
10580 micromips_label_expr (&label_expr);
10581 else
10582 label_expr.X_add_number = 8;
10583 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10584 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10585 macro_build (NULL, "break", BRK_FMT, 7);
10586 if (mips_opts.micromips)
10587 micromips_add_label ();
10588 }
10589 expr1.X_add_number = -1;
10590 used_at = 1;
10591 load_register (AT, &expr1, dbl);
10592 if (mips_opts.micromips)
10593 micromips_label_expr (&label_expr);
10594 else
10595 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
10596 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
10597 if (dbl)
10598 {
10599 expr1.X_add_number = 1;
10600 load_register (AT, &expr1, dbl);
10601 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
10602 }
10603 else
10604 {
10605 expr1.X_add_number = 0x80000000;
10606 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
10607 }
10608 if (mips_trap)
10609 {
10610 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
10611 /* We want to close the noreorder block as soon as possible, so
10612 that later insns are available for delay slot filling. */
10613 end_noreorder ();
10614 }
10615 else
10616 {
10617 if (mips_opts.micromips)
10618 micromips_label_expr (&label_expr);
10619 else
10620 label_expr.X_add_number = 8;
10621 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
10622 macro_build (NULL, "nop", "");
10623
10624 /* We want to close the noreorder block as soon as possible, so
10625 that later insns are available for delay slot filling. */
10626 end_noreorder ();
10627
10628 macro_build (NULL, "break", BRK_FMT, 6);
10629 }
10630 if (mips_opts.micromips)
10631 micromips_add_label ();
10632 macro_build (NULL, s, MFHL_FMT, op[0]);
10633 break;
10634
10635 case M_DIV_3I:
10636 s = "div";
10637 s2 = "mflo";
10638 goto do_divi;
10639 case M_DIVU_3I:
10640 s = "divu";
10641 s2 = "mflo";
10642 goto do_divi;
10643 case M_REM_3I:
10644 s = "div";
10645 s2 = "mfhi";
10646 goto do_divi;
10647 case M_REMU_3I:
10648 s = "divu";
10649 s2 = "mfhi";
10650 goto do_divi;
10651 case M_DDIV_3I:
10652 dbl = 1;
10653 s = "ddiv";
10654 s2 = "mflo";
10655 goto do_divi;
10656 case M_DDIVU_3I:
10657 dbl = 1;
10658 s = "ddivu";
10659 s2 = "mflo";
10660 goto do_divi;
10661 case M_DREM_3I:
10662 dbl = 1;
10663 s = "ddiv";
10664 s2 = "mfhi";
10665 goto do_divi;
10666 case M_DREMU_3I:
10667 dbl = 1;
10668 s = "ddivu";
10669 s2 = "mfhi";
10670 do_divi:
10671 if (imm_expr.X_add_number == 0)
10672 {
10673 as_warn (_("divide by zero"));
10674 if (mips_trap)
10675 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10676 else
10677 macro_build (NULL, "break", BRK_FMT, 7);
10678 break;
10679 }
10680 if (imm_expr.X_add_number == 1)
10681 {
10682 if (strcmp (s2, "mflo") == 0)
10683 move_register (op[0], op[1]);
10684 else
10685 move_register (op[0], ZERO);
10686 break;
10687 }
10688 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
10689 {
10690 if (strcmp (s2, "mflo") == 0)
10691 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
10692 else
10693 move_register (op[0], ZERO);
10694 break;
10695 }
10696
10697 used_at = 1;
10698 load_register (AT, &imm_expr, dbl);
10699 macro_build (NULL, s, "z,s,t", op[1], AT);
10700 macro_build (NULL, s2, MFHL_FMT, op[0]);
10701 break;
10702
10703 case M_DIVU_3:
10704 s = "divu";
10705 s2 = "mflo";
10706 goto do_divu3;
10707 case M_REMU_3:
10708 s = "divu";
10709 s2 = "mfhi";
10710 goto do_divu3;
10711 case M_DDIVU_3:
10712 s = "ddivu";
10713 s2 = "mflo";
10714 goto do_divu3;
10715 case M_DREMU_3:
10716 s = "ddivu";
10717 s2 = "mfhi";
10718 do_divu3:
10719 start_noreorder ();
10720 if (mips_trap)
10721 {
10722 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10723 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10724 /* We want to close the noreorder block as soon as possible, so
10725 that later insns are available for delay slot filling. */
10726 end_noreorder ();
10727 }
10728 else
10729 {
10730 if (mips_opts.micromips)
10731 micromips_label_expr (&label_expr);
10732 else
10733 label_expr.X_add_number = 8;
10734 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10735 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10736
10737 /* We want to close the noreorder block as soon as possible, so
10738 that later insns are available for delay slot filling. */
10739 end_noreorder ();
10740 macro_build (NULL, "break", BRK_FMT, 7);
10741 if (mips_opts.micromips)
10742 micromips_add_label ();
10743 }
10744 macro_build (NULL, s2, MFHL_FMT, op[0]);
10745 break;
10746
10747 case M_DLCA_AB:
10748 dbl = 1;
10749 /* Fall through. */
10750 case M_LCA_AB:
10751 call = 1;
10752 goto do_la;
10753 case M_DLA_AB:
10754 dbl = 1;
10755 /* Fall through. */
10756 case M_LA_AB:
10757 do_la:
10758 /* Load the address of a symbol into a register. If breg is not
10759 zero, we then add a base register to it. */
10760
10761 breg = op[2];
10762 if (dbl && GPR_SIZE == 32)
10763 as_warn (_("dla used to load 32-bit register; recommend using la "
10764 "instead"));
10765
10766 if (!dbl && HAVE_64BIT_OBJECTS)
10767 as_warn (_("la used to load 64-bit address; recommend using dla "
10768 "instead"));
10769
10770 if (small_offset_p (0, align, 16))
10771 {
10772 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
10773 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10774 break;
10775 }
10776
10777 if (mips_opts.at && (op[0] == breg))
10778 {
10779 tempreg = AT;
10780 used_at = 1;
10781 }
10782 else
10783 tempreg = op[0];
10784
10785 if (offset_expr.X_op != O_symbol
10786 && offset_expr.X_op != O_constant)
10787 {
10788 as_bad (_("expression too complex"));
10789 offset_expr.X_op = O_constant;
10790 }
10791
10792 if (offset_expr.X_op == O_constant)
10793 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
10794 else if (mips_pic == NO_PIC)
10795 {
10796 /* If this is a reference to a GP relative symbol, we want
10797 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
10798 Otherwise we want
10799 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10800 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10801 If we have a constant, we need two instructions anyhow,
10802 so we may as well always use the latter form.
10803
10804 With 64bit address space and a usable $at we want
10805 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10806 lui $at,<sym> (BFD_RELOC_HI16_S)
10807 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10808 daddiu $at,<sym> (BFD_RELOC_LO16)
10809 dsll32 $tempreg,0
10810 daddu $tempreg,$tempreg,$at
10811
10812 If $at is already in use, we use a path which is suboptimal
10813 on superscalar processors.
10814 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10815 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10816 dsll $tempreg,16
10817 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10818 dsll $tempreg,16
10819 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10820
10821 For GP relative symbols in 64bit address space we can use
10822 the same sequence as in 32bit address space. */
10823 if (HAVE_64BIT_SYMBOLS)
10824 {
10825 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10826 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10827 {
10828 relax_start (offset_expr.X_add_symbol);
10829 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10830 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10831 relax_switch ();
10832 }
10833
10834 if (used_at == 0 && mips_opts.at)
10835 {
10836 macro_build (&offset_expr, "lui", LUI_FMT,
10837 tempreg, BFD_RELOC_MIPS_HIGHEST);
10838 macro_build (&offset_expr, "lui", LUI_FMT,
10839 AT, BFD_RELOC_HI16_S);
10840 macro_build (&offset_expr, "daddiu", "t,r,j",
10841 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10842 macro_build (&offset_expr, "daddiu", "t,r,j",
10843 AT, AT, BFD_RELOC_LO16);
10844 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
10845 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
10846 used_at = 1;
10847 }
10848 else
10849 {
10850 macro_build (&offset_expr, "lui", LUI_FMT,
10851 tempreg, BFD_RELOC_MIPS_HIGHEST);
10852 macro_build (&offset_expr, "daddiu", "t,r,j",
10853 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10854 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10855 macro_build (&offset_expr, "daddiu", "t,r,j",
10856 tempreg, tempreg, BFD_RELOC_HI16_S);
10857 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10858 macro_build (&offset_expr, "daddiu", "t,r,j",
10859 tempreg, tempreg, BFD_RELOC_LO16);
10860 }
10861
10862 if (mips_relax.sequence)
10863 relax_end ();
10864 }
10865 else
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 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
10876 as_bad (_("offset too large"));
10877 macro_build_lui (&offset_expr, tempreg);
10878 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10879 tempreg, tempreg, BFD_RELOC_LO16);
10880 if (mips_relax.sequence)
10881 relax_end ();
10882 }
10883 }
10884 else if (!mips_big_got && !HAVE_NEWABI)
10885 {
10886 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10887
10888 /* If this is a reference to an external symbol, and there
10889 is no constant, we want
10890 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10891 or for lca or if tempreg is PIC_CALL_REG
10892 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10893 For a local symbol, we want
10894 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10895 nop
10896 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10897
10898 If we have a small constant, and this is a reference to
10899 an external symbol, we want
10900 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10901 nop
10902 addiu $tempreg,$tempreg,<constant>
10903 For a local symbol, we want the same instruction
10904 sequence, but we output a BFD_RELOC_LO16 reloc on the
10905 addiu instruction.
10906
10907 If we have a large constant, and this is a reference to
10908 an external symbol, we want
10909 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10910 lui $at,<hiconstant>
10911 addiu $at,$at,<loconstant>
10912 addu $tempreg,$tempreg,$at
10913 For a local symbol, we want the same instruction
10914 sequence, but we output a BFD_RELOC_LO16 reloc on the
10915 addiu instruction.
10916 */
10917
10918 if (offset_expr.X_add_number == 0)
10919 {
10920 if (mips_pic == SVR4_PIC
10921 && breg == 0
10922 && (call || tempreg == PIC_CALL_REG))
10923 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10924
10925 relax_start (offset_expr.X_add_symbol);
10926 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10927 lw_reloc_type, mips_gp_register);
10928 if (breg != 0)
10929 {
10930 /* We're going to put in an addu instruction using
10931 tempreg, so we may as well insert the nop right
10932 now. */
10933 load_delay_nop ();
10934 }
10935 relax_switch ();
10936 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10937 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
10938 load_delay_nop ();
10939 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10940 tempreg, tempreg, BFD_RELOC_LO16);
10941 relax_end ();
10942 /* FIXME: If breg == 0, and the next instruction uses
10943 $tempreg, then if this variant case is used an extra
10944 nop will be generated. */
10945 }
10946 else if (offset_expr.X_add_number >= -0x8000
10947 && offset_expr.X_add_number < 0x8000)
10948 {
10949 load_got_offset (tempreg, &offset_expr);
10950 load_delay_nop ();
10951 add_got_offset (tempreg, &offset_expr);
10952 }
10953 else
10954 {
10955 expr1.X_add_number = offset_expr.X_add_number;
10956 offset_expr.X_add_number =
10957 SEXT_16BIT (offset_expr.X_add_number);
10958 load_got_offset (tempreg, &offset_expr);
10959 offset_expr.X_add_number = expr1.X_add_number;
10960 /* If we are going to add in a base register, and the
10961 target register and the base register are the same,
10962 then we are using AT as a temporary register. Since
10963 we want to load the constant into AT, we add our
10964 current AT (from the global offset table) and the
10965 register into the register now, and pretend we were
10966 not using a base register. */
10967 if (breg == op[0])
10968 {
10969 load_delay_nop ();
10970 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10971 op[0], AT, breg);
10972 breg = 0;
10973 tempreg = op[0];
10974 }
10975 add_got_offset_hilo (tempreg, &offset_expr, AT);
10976 used_at = 1;
10977 }
10978 }
10979 else if (!mips_big_got && HAVE_NEWABI)
10980 {
10981 int add_breg_early = 0;
10982
10983 /* If this is a reference to an external, and there is no
10984 constant, or local symbol (*), with or without a
10985 constant, we want
10986 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10987 or for lca or if tempreg is PIC_CALL_REG
10988 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10989
10990 If we have a small constant, and this is a reference to
10991 an external symbol, we want
10992 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10993 addiu $tempreg,$tempreg,<constant>
10994
10995 If we have a large constant, and this is a reference to
10996 an external symbol, we want
10997 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10998 lui $at,<hiconstant>
10999 addiu $at,$at,<loconstant>
11000 addu $tempreg,$tempreg,$at
11001
11002 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11003 local symbols, even though it introduces an additional
11004 instruction. */
11005
11006 if (offset_expr.X_add_number)
11007 {
11008 expr1.X_add_number = offset_expr.X_add_number;
11009 offset_expr.X_add_number = 0;
11010
11011 relax_start (offset_expr.X_add_symbol);
11012 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11013 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11014
11015 if (expr1.X_add_number >= -0x8000
11016 && expr1.X_add_number < 0x8000)
11017 {
11018 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11019 tempreg, tempreg, BFD_RELOC_LO16);
11020 }
11021 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11022 {
11023 unsigned int dreg;
11024
11025 /* If we are going to add in a base register, and the
11026 target register and the base register are the same,
11027 then we are using AT as a temporary register. Since
11028 we want to load the constant into AT, we add our
11029 current AT (from the global offset table) and the
11030 register into the register now, and pretend we were
11031 not using a base register. */
11032 if (breg != op[0])
11033 dreg = tempreg;
11034 else
11035 {
11036 gas_assert (tempreg == AT);
11037 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11038 op[0], AT, breg);
11039 dreg = op[0];
11040 add_breg_early = 1;
11041 }
11042
11043 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11044 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11045 dreg, dreg, AT);
11046
11047 used_at = 1;
11048 }
11049 else
11050 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11051
11052 relax_switch ();
11053 offset_expr.X_add_number = expr1.X_add_number;
11054
11055 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11056 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11057 if (add_breg_early)
11058 {
11059 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11060 op[0], tempreg, breg);
11061 breg = 0;
11062 tempreg = op[0];
11063 }
11064 relax_end ();
11065 }
11066 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
11067 {
11068 relax_start (offset_expr.X_add_symbol);
11069 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11070 BFD_RELOC_MIPS_CALL16, mips_gp_register);
11071 relax_switch ();
11072 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11073 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11074 relax_end ();
11075 }
11076 else
11077 {
11078 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11079 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11080 }
11081 }
11082 else if (mips_big_got && !HAVE_NEWABI)
11083 {
11084 int gpdelay;
11085 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11086 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11087 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11088
11089 /* This is the large GOT case. If this is a reference to an
11090 external symbol, and there is no constant, we want
11091 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11092 addu $tempreg,$tempreg,$gp
11093 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11094 or for lca or if tempreg is PIC_CALL_REG
11095 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11096 addu $tempreg,$tempreg,$gp
11097 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11098 For a local symbol, we want
11099 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11100 nop
11101 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11102
11103 If we have a small constant, and this is a reference to
11104 an external symbol, we want
11105 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11106 addu $tempreg,$tempreg,$gp
11107 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11108 nop
11109 addiu $tempreg,$tempreg,<constant>
11110 For a local symbol, we want
11111 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11112 nop
11113 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11114
11115 If we have a large constant, and this is a reference to
11116 an external symbol, we want
11117 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11118 addu $tempreg,$tempreg,$gp
11119 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11120 lui $at,<hiconstant>
11121 addiu $at,$at,<loconstant>
11122 addu $tempreg,$tempreg,$at
11123 For a local symbol, we want
11124 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11125 lui $at,<hiconstant>
11126 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11127 addu $tempreg,$tempreg,$at
11128 */
11129
11130 expr1.X_add_number = offset_expr.X_add_number;
11131 offset_expr.X_add_number = 0;
11132 relax_start (offset_expr.X_add_symbol);
11133 gpdelay = reg_needs_delay (mips_gp_register);
11134 if (expr1.X_add_number == 0 && breg == 0
11135 && (call || tempreg == PIC_CALL_REG))
11136 {
11137 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11138 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11139 }
11140 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11141 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11142 tempreg, tempreg, mips_gp_register);
11143 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11144 tempreg, lw_reloc_type, tempreg);
11145 if (expr1.X_add_number == 0)
11146 {
11147 if (breg != 0)
11148 {
11149 /* We're going to put in an addu instruction using
11150 tempreg, so we may as well insert the nop right
11151 now. */
11152 load_delay_nop ();
11153 }
11154 }
11155 else if (expr1.X_add_number >= -0x8000
11156 && expr1.X_add_number < 0x8000)
11157 {
11158 load_delay_nop ();
11159 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11160 tempreg, tempreg, BFD_RELOC_LO16);
11161 }
11162 else
11163 {
11164 unsigned int dreg;
11165
11166 /* If we are going to add in a base register, and the
11167 target register and the base register are the same,
11168 then we are using AT as a temporary register. Since
11169 we want to load the constant into AT, we add our
11170 current AT (from the global offset table) and the
11171 register into the register now, and pretend we were
11172 not using a base register. */
11173 if (breg != op[0])
11174 dreg = tempreg;
11175 else
11176 {
11177 gas_assert (tempreg == AT);
11178 load_delay_nop ();
11179 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11180 op[0], AT, breg);
11181 dreg = op[0];
11182 }
11183
11184 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11185 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11186
11187 used_at = 1;
11188 }
11189 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
11190 relax_switch ();
11191
11192 if (gpdelay)
11193 {
11194 /* This is needed because this instruction uses $gp, but
11195 the first instruction on the main stream does not. */
11196 macro_build (NULL, "nop", "");
11197 }
11198
11199 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11200 local_reloc_type, mips_gp_register);
11201 if (expr1.X_add_number >= -0x8000
11202 && expr1.X_add_number < 0x8000)
11203 {
11204 load_delay_nop ();
11205 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11206 tempreg, tempreg, BFD_RELOC_LO16);
11207 /* FIXME: If add_number is 0, and there was no base
11208 register, the external symbol case ended with a load,
11209 so if the symbol turns out to not be external, and
11210 the next instruction uses tempreg, an unnecessary nop
11211 will be inserted. */
11212 }
11213 else
11214 {
11215 if (breg == op[0])
11216 {
11217 /* We must add in the base register now, as in the
11218 external symbol case. */
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 tempreg = op[0];
11224 /* We set breg to 0 because we have arranged to add
11225 it in in both cases. */
11226 breg = 0;
11227 }
11228
11229 macro_build_lui (&expr1, AT);
11230 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11231 AT, AT, BFD_RELOC_LO16);
11232 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11233 tempreg, tempreg, AT);
11234 used_at = 1;
11235 }
11236 relax_end ();
11237 }
11238 else if (mips_big_got && HAVE_NEWABI)
11239 {
11240 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11241 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11242 int add_breg_early = 0;
11243
11244 /* This is the large GOT case. If this is a reference to an
11245 external symbol, and there is no constant, we want
11246 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11247 add $tempreg,$tempreg,$gp
11248 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11249 or for lca or if tempreg is PIC_CALL_REG
11250 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11251 add $tempreg,$tempreg,$gp
11252 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11253
11254 If we have a small constant, and this is a reference to
11255 an external symbol, we want
11256 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11257 add $tempreg,$tempreg,$gp
11258 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11259 addi $tempreg,$tempreg,<constant>
11260
11261 If we have a large constant, and this is a reference to
11262 an external symbol, we want
11263 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11264 addu $tempreg,$tempreg,$gp
11265 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11266 lui $at,<hiconstant>
11267 addi $at,$at,<loconstant>
11268 add $tempreg,$tempreg,$at
11269
11270 If we have NewABI, and we know it's a local symbol, we want
11271 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11272 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11273 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11274
11275 relax_start (offset_expr.X_add_symbol);
11276
11277 expr1.X_add_number = offset_expr.X_add_number;
11278 offset_expr.X_add_number = 0;
11279
11280 if (expr1.X_add_number == 0 && breg == 0
11281 && (call || tempreg == PIC_CALL_REG))
11282 {
11283 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11284 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11285 }
11286 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11287 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11288 tempreg, tempreg, mips_gp_register);
11289 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11290 tempreg, lw_reloc_type, tempreg);
11291
11292 if (expr1.X_add_number == 0)
11293 ;
11294 else if (expr1.X_add_number >= -0x8000
11295 && expr1.X_add_number < 0x8000)
11296 {
11297 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11298 tempreg, tempreg, BFD_RELOC_LO16);
11299 }
11300 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11301 {
11302 unsigned int dreg;
11303
11304 /* If we are going to add in a base register, and the
11305 target register and the base register are the same,
11306 then we are using AT as a temporary register. Since
11307 we want to load the constant into AT, we add our
11308 current AT (from the global offset table) and the
11309 register into the register now, and pretend we were
11310 not using a base register. */
11311 if (breg != op[0])
11312 dreg = tempreg;
11313 else
11314 {
11315 gas_assert (tempreg == AT);
11316 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11317 op[0], AT, breg);
11318 dreg = op[0];
11319 add_breg_early = 1;
11320 }
11321
11322 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11323 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11324
11325 used_at = 1;
11326 }
11327 else
11328 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11329
11330 relax_switch ();
11331 offset_expr.X_add_number = expr1.X_add_number;
11332 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11333 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11334 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11335 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11336 if (add_breg_early)
11337 {
11338 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11339 op[0], tempreg, breg);
11340 breg = 0;
11341 tempreg = op[0];
11342 }
11343 relax_end ();
11344 }
11345 else
11346 abort ();
11347
11348 if (breg != 0)
11349 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
11350 break;
11351
11352 case M_MSGSND:
11353 gas_assert (!mips_opts.micromips);
11354 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
11355 break;
11356
11357 case M_MSGLD:
11358 gas_assert (!mips_opts.micromips);
11359 macro_build (NULL, "c2", "C", 0x02);
11360 break;
11361
11362 case M_MSGLD_T:
11363 gas_assert (!mips_opts.micromips);
11364 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
11365 break;
11366
11367 case M_MSGWAIT:
11368 gas_assert (!mips_opts.micromips);
11369 macro_build (NULL, "c2", "C", 3);
11370 break;
11371
11372 case M_MSGWAIT_T:
11373 gas_assert (!mips_opts.micromips);
11374 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
11375 break;
11376
11377 case M_J_A:
11378 /* The j instruction may not be used in PIC code, since it
11379 requires an absolute address. We convert it to a b
11380 instruction. */
11381 if (mips_pic == NO_PIC)
11382 macro_build (&offset_expr, "j", "a");
11383 else
11384 macro_build (&offset_expr, "b", "p");
11385 break;
11386
11387 /* The jal instructions must be handled as macros because when
11388 generating PIC code they expand to multi-instruction
11389 sequences. Normally they are simple instructions. */
11390 case M_JALS_1:
11391 op[1] = op[0];
11392 op[0] = RA;
11393 /* Fall through. */
11394 case M_JALS_2:
11395 gas_assert (mips_opts.micromips);
11396 if (mips_opts.insn32)
11397 {
11398 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11399 break;
11400 }
11401 jals = 1;
11402 goto jal;
11403 case M_JAL_1:
11404 op[1] = op[0];
11405 op[0] = RA;
11406 /* Fall through. */
11407 case M_JAL_2:
11408 jal:
11409 if (mips_pic == NO_PIC)
11410 {
11411 s = jals ? "jalrs" : "jalr";
11412 if (mips_opts.micromips
11413 && !mips_opts.insn32
11414 && op[0] == RA
11415 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11416 macro_build (NULL, s, "mj", op[1]);
11417 else
11418 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11419 }
11420 else
11421 {
11422 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11423 && mips_cprestore_offset >= 0);
11424
11425 if (op[1] != PIC_CALL_REG)
11426 as_warn (_("MIPS PIC call to register other than $25"));
11427
11428 s = ((mips_opts.micromips
11429 && !mips_opts.insn32
11430 && (!mips_opts.noreorder || cprestore))
11431 ? "jalrs" : "jalr");
11432 if (mips_opts.micromips
11433 && !mips_opts.insn32
11434 && op[0] == RA
11435 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11436 macro_build (NULL, s, "mj", op[1]);
11437 else
11438 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11439 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
11440 {
11441 if (mips_cprestore_offset < 0)
11442 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11443 else
11444 {
11445 if (!mips_frame_reg_valid)
11446 {
11447 as_warn (_("no .frame pseudo-op used in PIC code"));
11448 /* Quiet this warning. */
11449 mips_frame_reg_valid = 1;
11450 }
11451 if (!mips_cprestore_valid)
11452 {
11453 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11454 /* Quiet this warning. */
11455 mips_cprestore_valid = 1;
11456 }
11457 if (mips_opts.noreorder)
11458 macro_build (NULL, "nop", "");
11459 expr1.X_add_number = mips_cprestore_offset;
11460 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11461 mips_gp_register,
11462 mips_frame_reg,
11463 HAVE_64BIT_ADDRESSES);
11464 }
11465 }
11466 }
11467
11468 break;
11469
11470 case M_JALS_A:
11471 gas_assert (mips_opts.micromips);
11472 if (mips_opts.insn32)
11473 {
11474 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11475 break;
11476 }
11477 jals = 1;
11478 /* Fall through. */
11479 case M_JAL_A:
11480 if (mips_pic == NO_PIC)
11481 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
11482 else if (mips_pic == SVR4_PIC)
11483 {
11484 /* If this is a reference to an external symbol, and we are
11485 using a small GOT, we want
11486 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11487 nop
11488 jalr $ra,$25
11489 nop
11490 lw $gp,cprestore($sp)
11491 The cprestore value is set using the .cprestore
11492 pseudo-op. If we are using a big GOT, we want
11493 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11494 addu $25,$25,$gp
11495 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11496 nop
11497 jalr $ra,$25
11498 nop
11499 lw $gp,cprestore($sp)
11500 If the symbol is not external, we want
11501 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11502 nop
11503 addiu $25,$25,<sym> (BFD_RELOC_LO16)
11504 jalr $ra,$25
11505 nop
11506 lw $gp,cprestore($sp)
11507
11508 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11509 sequences above, minus nops, unless the symbol is local,
11510 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11511 GOT_DISP. */
11512 if (HAVE_NEWABI)
11513 {
11514 if (!mips_big_got)
11515 {
11516 relax_start (offset_expr.X_add_symbol);
11517 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11518 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11519 mips_gp_register);
11520 relax_switch ();
11521 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11522 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
11523 mips_gp_register);
11524 relax_end ();
11525 }
11526 else
11527 {
11528 relax_start (offset_expr.X_add_symbol);
11529 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11530 BFD_RELOC_MIPS_CALL_HI16);
11531 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11532 PIC_CALL_REG, mips_gp_register);
11533 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11534 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11535 PIC_CALL_REG);
11536 relax_switch ();
11537 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11538 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11539 mips_gp_register);
11540 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11541 PIC_CALL_REG, PIC_CALL_REG,
11542 BFD_RELOC_MIPS_GOT_OFST);
11543 relax_end ();
11544 }
11545
11546 macro_build_jalr (&offset_expr, 0);
11547 }
11548 else
11549 {
11550 relax_start (offset_expr.X_add_symbol);
11551 if (!mips_big_got)
11552 {
11553 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11554 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11555 mips_gp_register);
11556 load_delay_nop ();
11557 relax_switch ();
11558 }
11559 else
11560 {
11561 int gpdelay;
11562
11563 gpdelay = reg_needs_delay (mips_gp_register);
11564 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11565 BFD_RELOC_MIPS_CALL_HI16);
11566 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11567 PIC_CALL_REG, mips_gp_register);
11568 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11569 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11570 PIC_CALL_REG);
11571 load_delay_nop ();
11572 relax_switch ();
11573 if (gpdelay)
11574 macro_build (NULL, "nop", "");
11575 }
11576 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11577 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
11578 mips_gp_register);
11579 load_delay_nop ();
11580 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11581 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
11582 relax_end ();
11583 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
11584
11585 if (mips_cprestore_offset < 0)
11586 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11587 else
11588 {
11589 if (!mips_frame_reg_valid)
11590 {
11591 as_warn (_("no .frame pseudo-op used in PIC code"));
11592 /* Quiet this warning. */
11593 mips_frame_reg_valid = 1;
11594 }
11595 if (!mips_cprestore_valid)
11596 {
11597 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11598 /* Quiet this warning. */
11599 mips_cprestore_valid = 1;
11600 }
11601 if (mips_opts.noreorder)
11602 macro_build (NULL, "nop", "");
11603 expr1.X_add_number = mips_cprestore_offset;
11604 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11605 mips_gp_register,
11606 mips_frame_reg,
11607 HAVE_64BIT_ADDRESSES);
11608 }
11609 }
11610 }
11611 else if (mips_pic == VXWORKS_PIC)
11612 as_bad (_("non-PIC jump used in PIC library"));
11613 else
11614 abort ();
11615
11616 break;
11617
11618 case M_LBUE_AB:
11619 s = "lbue";
11620 fmt = "t,+j(b)";
11621 offbits = 9;
11622 goto ld_st;
11623 case M_LHUE_AB:
11624 s = "lhue";
11625 fmt = "t,+j(b)";
11626 offbits = 9;
11627 goto ld_st;
11628 case M_LBE_AB:
11629 s = "lbe";
11630 fmt = "t,+j(b)";
11631 offbits = 9;
11632 goto ld_st;
11633 case M_LHE_AB:
11634 s = "lhe";
11635 fmt = "t,+j(b)";
11636 offbits = 9;
11637 goto ld_st;
11638 case M_LLE_AB:
11639 s = "lle";
11640 fmt = "t,+j(b)";
11641 offbits = 9;
11642 goto ld_st;
11643 case M_LWE_AB:
11644 s = "lwe";
11645 fmt = "t,+j(b)";
11646 offbits = 9;
11647 goto ld_st;
11648 case M_LWLE_AB:
11649 s = "lwle";
11650 fmt = "t,+j(b)";
11651 offbits = 9;
11652 goto ld_st;
11653 case M_LWRE_AB:
11654 s = "lwre";
11655 fmt = "t,+j(b)";
11656 offbits = 9;
11657 goto ld_st;
11658 case M_SBE_AB:
11659 s = "sbe";
11660 fmt = "t,+j(b)";
11661 offbits = 9;
11662 goto ld_st;
11663 case M_SCE_AB:
11664 s = "sce";
11665 fmt = "t,+j(b)";
11666 offbits = 9;
11667 goto ld_st;
11668 case M_SHE_AB:
11669 s = "she";
11670 fmt = "t,+j(b)";
11671 offbits = 9;
11672 goto ld_st;
11673 case M_SWE_AB:
11674 s = "swe";
11675 fmt = "t,+j(b)";
11676 offbits = 9;
11677 goto ld_st;
11678 case M_SWLE_AB:
11679 s = "swle";
11680 fmt = "t,+j(b)";
11681 offbits = 9;
11682 goto ld_st;
11683 case M_SWRE_AB:
11684 s = "swre";
11685 fmt = "t,+j(b)";
11686 offbits = 9;
11687 goto ld_st;
11688 case M_ACLR_AB:
11689 s = "aclr";
11690 fmt = "\\,~(b)";
11691 offbits = 12;
11692 goto ld_st;
11693 case M_ASET_AB:
11694 s = "aset";
11695 fmt = "\\,~(b)";
11696 offbits = 12;
11697 goto ld_st;
11698 case M_LB_AB:
11699 s = "lb";
11700 fmt = "t,o(b)";
11701 goto ld;
11702 case M_LBU_AB:
11703 s = "lbu";
11704 fmt = "t,o(b)";
11705 goto ld;
11706 case M_LH_AB:
11707 s = "lh";
11708 fmt = "t,o(b)";
11709 goto ld;
11710 case M_LHU_AB:
11711 s = "lhu";
11712 fmt = "t,o(b)";
11713 goto ld;
11714 case M_LW_AB:
11715 s = "lw";
11716 fmt = "t,o(b)";
11717 goto ld;
11718 case M_LWC0_AB:
11719 gas_assert (!mips_opts.micromips);
11720 s = "lwc0";
11721 fmt = "E,o(b)";
11722 /* Itbl support may require additional care here. */
11723 coproc = 1;
11724 goto ld_st;
11725 case M_LWC1_AB:
11726 s = "lwc1";
11727 fmt = "T,o(b)";
11728 /* Itbl support may require additional care here. */
11729 coproc = 1;
11730 goto ld_st;
11731 case M_LWC2_AB:
11732 s = "lwc2";
11733 fmt = COP12_FMT;
11734 offbits = (mips_opts.micromips ? 12
11735 : ISA_IS_R6 (mips_opts.isa) ? 11
11736 : 16);
11737 /* Itbl support may require additional care here. */
11738 coproc = 1;
11739 goto ld_st;
11740 case M_LWC3_AB:
11741 gas_assert (!mips_opts.micromips);
11742 s = "lwc3";
11743 fmt = "E,o(b)";
11744 /* Itbl support may require additional care here. */
11745 coproc = 1;
11746 goto ld_st;
11747 case M_LWL_AB:
11748 s = "lwl";
11749 fmt = MEM12_FMT;
11750 offbits = (mips_opts.micromips ? 12 : 16);
11751 goto ld_st;
11752 case M_LWR_AB:
11753 s = "lwr";
11754 fmt = MEM12_FMT;
11755 offbits = (mips_opts.micromips ? 12 : 16);
11756 goto ld_st;
11757 case M_LDC1_AB:
11758 s = "ldc1";
11759 fmt = "T,o(b)";
11760 /* Itbl support may require additional care here. */
11761 coproc = 1;
11762 goto ld_st;
11763 case M_LDC2_AB:
11764 s = "ldc2";
11765 fmt = COP12_FMT;
11766 offbits = (mips_opts.micromips ? 12
11767 : ISA_IS_R6 (mips_opts.isa) ? 11
11768 : 16);
11769 /* Itbl support may require additional care here. */
11770 coproc = 1;
11771 goto ld_st;
11772 case M_LQC2_AB:
11773 s = "lqc2";
11774 fmt = "+7,o(b)";
11775 /* Itbl support may require additional care here. */
11776 coproc = 1;
11777 goto ld_st;
11778 case M_LDC3_AB:
11779 s = "ldc3";
11780 fmt = "E,o(b)";
11781 /* Itbl support may require additional care here. */
11782 coproc = 1;
11783 goto ld_st;
11784 case M_LDL_AB:
11785 s = "ldl";
11786 fmt = MEM12_FMT;
11787 offbits = (mips_opts.micromips ? 12 : 16);
11788 goto ld_st;
11789 case M_LDR_AB:
11790 s = "ldr";
11791 fmt = MEM12_FMT;
11792 offbits = (mips_opts.micromips ? 12 : 16);
11793 goto ld_st;
11794 case M_LL_AB:
11795 s = "ll";
11796 fmt = LL_SC_FMT;
11797 offbits = (mips_opts.micromips ? 12
11798 : ISA_IS_R6 (mips_opts.isa) ? 9
11799 : 16);
11800 goto ld;
11801 case M_LLD_AB:
11802 s = "lld";
11803 fmt = LL_SC_FMT;
11804 offbits = (mips_opts.micromips ? 12
11805 : ISA_IS_R6 (mips_opts.isa) ? 9
11806 : 16);
11807 goto ld;
11808 case M_LWU_AB:
11809 s = "lwu";
11810 fmt = MEM12_FMT;
11811 offbits = (mips_opts.micromips ? 12 : 16);
11812 goto ld;
11813 case M_LWP_AB:
11814 gas_assert (mips_opts.micromips);
11815 s = "lwp";
11816 fmt = "t,~(b)";
11817 offbits = 12;
11818 lp = 1;
11819 goto ld;
11820 case M_LDP_AB:
11821 gas_assert (mips_opts.micromips);
11822 s = "ldp";
11823 fmt = "t,~(b)";
11824 offbits = 12;
11825 lp = 1;
11826 goto ld;
11827 case M_LWM_AB:
11828 gas_assert (mips_opts.micromips);
11829 s = "lwm";
11830 fmt = "n,~(b)";
11831 offbits = 12;
11832 goto ld_st;
11833 case M_LDM_AB:
11834 gas_assert (mips_opts.micromips);
11835 s = "ldm";
11836 fmt = "n,~(b)";
11837 offbits = 12;
11838 goto ld_st;
11839
11840 ld:
11841 /* We don't want to use $0 as tempreg. */
11842 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
11843 goto ld_st;
11844 else
11845 tempreg = op[0] + lp;
11846 goto ld_noat;
11847
11848 case M_SB_AB:
11849 s = "sb";
11850 fmt = "t,o(b)";
11851 goto ld_st;
11852 case M_SH_AB:
11853 s = "sh";
11854 fmt = "t,o(b)";
11855 goto ld_st;
11856 case M_SW_AB:
11857 s = "sw";
11858 fmt = "t,o(b)";
11859 goto ld_st;
11860 case M_SWC0_AB:
11861 gas_assert (!mips_opts.micromips);
11862 s = "swc0";
11863 fmt = "E,o(b)";
11864 /* Itbl support may require additional care here. */
11865 coproc = 1;
11866 goto ld_st;
11867 case M_SWC1_AB:
11868 s = "swc1";
11869 fmt = "T,o(b)";
11870 /* Itbl support may require additional care here. */
11871 coproc = 1;
11872 goto ld_st;
11873 case M_SWC2_AB:
11874 s = "swc2";
11875 fmt = COP12_FMT;
11876 offbits = (mips_opts.micromips ? 12
11877 : ISA_IS_R6 (mips_opts.isa) ? 11
11878 : 16);
11879 /* Itbl support may require additional care here. */
11880 coproc = 1;
11881 goto ld_st;
11882 case M_SWC3_AB:
11883 gas_assert (!mips_opts.micromips);
11884 s = "swc3";
11885 fmt = "E,o(b)";
11886 /* Itbl support may require additional care here. */
11887 coproc = 1;
11888 goto ld_st;
11889 case M_SWL_AB:
11890 s = "swl";
11891 fmt = MEM12_FMT;
11892 offbits = (mips_opts.micromips ? 12 : 16);
11893 goto ld_st;
11894 case M_SWR_AB:
11895 s = "swr";
11896 fmt = MEM12_FMT;
11897 offbits = (mips_opts.micromips ? 12 : 16);
11898 goto ld_st;
11899 case M_SC_AB:
11900 s = "sc";
11901 fmt = LL_SC_FMT;
11902 offbits = (mips_opts.micromips ? 12
11903 : ISA_IS_R6 (mips_opts.isa) ? 9
11904 : 16);
11905 goto ld_st;
11906 case M_SCD_AB:
11907 s = "scd";
11908 fmt = LL_SC_FMT;
11909 offbits = (mips_opts.micromips ? 12
11910 : ISA_IS_R6 (mips_opts.isa) ? 9
11911 : 16);
11912 goto ld_st;
11913 case M_CACHE_AB:
11914 s = "cache";
11915 fmt = (mips_opts.micromips ? "k,~(b)"
11916 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11917 : "k,o(b)");
11918 offbits = (mips_opts.micromips ? 12
11919 : ISA_IS_R6 (mips_opts.isa) ? 9
11920 : 16);
11921 goto ld_st;
11922 case M_CACHEE_AB:
11923 s = "cachee";
11924 fmt = "k,+j(b)";
11925 offbits = 9;
11926 goto ld_st;
11927 case M_PREF_AB:
11928 s = "pref";
11929 fmt = (mips_opts.micromips ? "k,~(b)"
11930 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11931 : "k,o(b)");
11932 offbits = (mips_opts.micromips ? 12
11933 : ISA_IS_R6 (mips_opts.isa) ? 9
11934 : 16);
11935 goto ld_st;
11936 case M_PREFE_AB:
11937 s = "prefe";
11938 fmt = "k,+j(b)";
11939 offbits = 9;
11940 goto ld_st;
11941 case M_SDC1_AB:
11942 s = "sdc1";
11943 fmt = "T,o(b)";
11944 coproc = 1;
11945 /* Itbl support may require additional care here. */
11946 goto ld_st;
11947 case M_SDC2_AB:
11948 s = "sdc2";
11949 fmt = COP12_FMT;
11950 offbits = (mips_opts.micromips ? 12
11951 : ISA_IS_R6 (mips_opts.isa) ? 11
11952 : 16);
11953 /* Itbl support may require additional care here. */
11954 coproc = 1;
11955 goto ld_st;
11956 case M_SQC2_AB:
11957 s = "sqc2";
11958 fmt = "+7,o(b)";
11959 /* Itbl support may require additional care here. */
11960 coproc = 1;
11961 goto ld_st;
11962 case M_SDC3_AB:
11963 gas_assert (!mips_opts.micromips);
11964 s = "sdc3";
11965 fmt = "E,o(b)";
11966 /* Itbl support may require additional care here. */
11967 coproc = 1;
11968 goto ld_st;
11969 case M_SDL_AB:
11970 s = "sdl";
11971 fmt = MEM12_FMT;
11972 offbits = (mips_opts.micromips ? 12 : 16);
11973 goto ld_st;
11974 case M_SDR_AB:
11975 s = "sdr";
11976 fmt = MEM12_FMT;
11977 offbits = (mips_opts.micromips ? 12 : 16);
11978 goto ld_st;
11979 case M_SWP_AB:
11980 gas_assert (mips_opts.micromips);
11981 s = "swp";
11982 fmt = "t,~(b)";
11983 offbits = 12;
11984 goto ld_st;
11985 case M_SDP_AB:
11986 gas_assert (mips_opts.micromips);
11987 s = "sdp";
11988 fmt = "t,~(b)";
11989 offbits = 12;
11990 goto ld_st;
11991 case M_SWM_AB:
11992 gas_assert (mips_opts.micromips);
11993 s = "swm";
11994 fmt = "n,~(b)";
11995 offbits = 12;
11996 goto ld_st;
11997 case M_SDM_AB:
11998 gas_assert (mips_opts.micromips);
11999 s = "sdm";
12000 fmt = "n,~(b)";
12001 offbits = 12;
12002
12003 ld_st:
12004 tempreg = AT;
12005 ld_noat:
12006 breg = op[2];
12007 if (small_offset_p (0, align, 16))
12008 {
12009 /* The first case exists for M_LD_AB and M_SD_AB, which are
12010 macros for o32 but which should act like normal instructions
12011 otherwise. */
12012 if (offbits == 16)
12013 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
12014 offset_reloc[1], offset_reloc[2], breg);
12015 else if (small_offset_p (0, align, offbits))
12016 {
12017 if (offbits == 0)
12018 macro_build (NULL, s, fmt, op[0], breg);
12019 else
12020 macro_build (NULL, s, fmt, op[0],
12021 (int) offset_expr.X_add_number, breg);
12022 }
12023 else
12024 {
12025 if (tempreg == AT)
12026 used_at = 1;
12027 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12028 tempreg, breg, -1, offset_reloc[0],
12029 offset_reloc[1], offset_reloc[2]);
12030 if (offbits == 0)
12031 macro_build (NULL, s, fmt, op[0], tempreg);
12032 else
12033 macro_build (NULL, s, fmt, op[0], 0, tempreg);
12034 }
12035 break;
12036 }
12037
12038 if (tempreg == AT)
12039 used_at = 1;
12040
12041 if (offset_expr.X_op != O_constant
12042 && offset_expr.X_op != O_symbol)
12043 {
12044 as_bad (_("expression too complex"));
12045 offset_expr.X_op = O_constant;
12046 }
12047
12048 if (HAVE_32BIT_ADDRESSES
12049 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12050 {
12051 char value [32];
12052
12053 sprintf_vma (value, offset_expr.X_add_number);
12054 as_bad (_("number (0x%s) larger than 32 bits"), value);
12055 }
12056
12057 /* A constant expression in PIC code can be handled just as it
12058 is in non PIC code. */
12059 if (offset_expr.X_op == O_constant)
12060 {
12061 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12062 offbits == 0 ? 16 : offbits);
12063 offset_expr.X_add_number -= expr1.X_add_number;
12064
12065 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12066 if (breg != 0)
12067 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12068 tempreg, tempreg, breg);
12069 if (offbits == 0)
12070 {
12071 if (offset_expr.X_add_number != 0)
12072 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
12073 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
12074 macro_build (NULL, s, fmt, op[0], tempreg);
12075 }
12076 else if (offbits == 16)
12077 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12078 else
12079 macro_build (NULL, s, fmt, op[0],
12080 (int) offset_expr.X_add_number, tempreg);
12081 }
12082 else if (offbits != 16)
12083 {
12084 /* The offset field is too narrow to be used for a low-part
12085 relocation, so load the whole address into the auxiliary
12086 register. */
12087 load_address (tempreg, &offset_expr, &used_at);
12088 if (breg != 0)
12089 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12090 tempreg, tempreg, breg);
12091 if (offbits == 0)
12092 macro_build (NULL, s, fmt, op[0], tempreg);
12093 else
12094 macro_build (NULL, s, fmt, op[0], 0, tempreg);
12095 }
12096 else if (mips_pic == NO_PIC)
12097 {
12098 /* If this is a reference to a GP relative symbol, and there
12099 is no base register, we want
12100 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12101 Otherwise, if there is no base register, we want
12102 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12103 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12104 If we have a constant, we need two instructions anyhow,
12105 so we always use the latter form.
12106
12107 If we have a base register, and this is a reference to a
12108 GP relative symbol, we want
12109 addu $tempreg,$breg,$gp
12110 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
12111 Otherwise we want
12112 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12113 addu $tempreg,$tempreg,$breg
12114 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12115 With a constant we always use the latter case.
12116
12117 With 64bit address space and no base register and $at usable,
12118 we want
12119 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12120 lui $at,<sym> (BFD_RELOC_HI16_S)
12121 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12122 dsll32 $tempreg,0
12123 daddu $tempreg,$at
12124 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12125 If we have a base register, we want
12126 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12127 lui $at,<sym> (BFD_RELOC_HI16_S)
12128 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12129 daddu $at,$breg
12130 dsll32 $tempreg,0
12131 daddu $tempreg,$at
12132 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12133
12134 Without $at we can't generate the optimal path for superscalar
12135 processors here since this would require two temporary registers.
12136 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12137 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12138 dsll $tempreg,16
12139 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12140 dsll $tempreg,16
12141 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12142 If we have a base register, we want
12143 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12144 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12145 dsll $tempreg,16
12146 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12147 dsll $tempreg,16
12148 daddu $tempreg,$tempreg,$breg
12149 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12150
12151 For GP relative symbols in 64bit address space we can use
12152 the same sequence as in 32bit address space. */
12153 if (HAVE_64BIT_SYMBOLS)
12154 {
12155 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12156 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12157 {
12158 relax_start (offset_expr.X_add_symbol);
12159 if (breg == 0)
12160 {
12161 macro_build (&offset_expr, s, fmt, op[0],
12162 BFD_RELOC_GPREL16, mips_gp_register);
12163 }
12164 else
12165 {
12166 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12167 tempreg, breg, mips_gp_register);
12168 macro_build (&offset_expr, s, fmt, op[0],
12169 BFD_RELOC_GPREL16, tempreg);
12170 }
12171 relax_switch ();
12172 }
12173
12174 if (used_at == 0 && mips_opts.at)
12175 {
12176 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12177 BFD_RELOC_MIPS_HIGHEST);
12178 macro_build (&offset_expr, "lui", LUI_FMT, AT,
12179 BFD_RELOC_HI16_S);
12180 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12181 tempreg, BFD_RELOC_MIPS_HIGHER);
12182 if (breg != 0)
12183 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
12184 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
12185 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
12186 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
12187 tempreg);
12188 used_at = 1;
12189 }
12190 else
12191 {
12192 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12193 BFD_RELOC_MIPS_HIGHEST);
12194 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12195 tempreg, BFD_RELOC_MIPS_HIGHER);
12196 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12197 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12198 tempreg, BFD_RELOC_HI16_S);
12199 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12200 if (breg != 0)
12201 macro_build (NULL, "daddu", "d,v,t",
12202 tempreg, tempreg, breg);
12203 macro_build (&offset_expr, s, fmt, op[0],
12204 BFD_RELOC_LO16, tempreg);
12205 }
12206
12207 if (mips_relax.sequence)
12208 relax_end ();
12209 break;
12210 }
12211
12212 if (breg == 0)
12213 {
12214 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12215 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12216 {
12217 relax_start (offset_expr.X_add_symbol);
12218 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
12219 mips_gp_register);
12220 relax_switch ();
12221 }
12222 macro_build_lui (&offset_expr, tempreg);
12223 macro_build (&offset_expr, s, fmt, op[0],
12224 BFD_RELOC_LO16, tempreg);
12225 if (mips_relax.sequence)
12226 relax_end ();
12227 }
12228 else
12229 {
12230 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12231 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12232 {
12233 relax_start (offset_expr.X_add_symbol);
12234 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12235 tempreg, breg, mips_gp_register);
12236 macro_build (&offset_expr, s, fmt, op[0],
12237 BFD_RELOC_GPREL16, tempreg);
12238 relax_switch ();
12239 }
12240 macro_build_lui (&offset_expr, tempreg);
12241 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12242 tempreg, tempreg, breg);
12243 macro_build (&offset_expr, s, fmt, op[0],
12244 BFD_RELOC_LO16, tempreg);
12245 if (mips_relax.sequence)
12246 relax_end ();
12247 }
12248 }
12249 else if (!mips_big_got)
12250 {
12251 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
12252
12253 /* If this is a reference to an external symbol, we want
12254 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12255 nop
12256 <op> op[0],0($tempreg)
12257 Otherwise we want
12258 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12259 nop
12260 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12261 <op> op[0],0($tempreg)
12262
12263 For NewABI, we want
12264 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12265 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
12266
12267 If there is a base register, we add it to $tempreg before
12268 the <op>. If there is a constant, we stick it in the
12269 <op> instruction. We don't handle constants larger than
12270 16 bits, because we have no way to load the upper 16 bits
12271 (actually, we could handle them for the subset of cases
12272 in which we are not using $at). */
12273 gas_assert (offset_expr.X_op == O_symbol);
12274 if (HAVE_NEWABI)
12275 {
12276 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12277 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12278 if (breg != 0)
12279 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12280 tempreg, tempreg, breg);
12281 macro_build (&offset_expr, s, fmt, op[0],
12282 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12283 break;
12284 }
12285 expr1.X_add_number = offset_expr.X_add_number;
12286 offset_expr.X_add_number = 0;
12287 if (expr1.X_add_number < -0x8000
12288 || expr1.X_add_number >= 0x8000)
12289 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12290 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12291 lw_reloc_type, mips_gp_register);
12292 load_delay_nop ();
12293 relax_start (offset_expr.X_add_symbol);
12294 relax_switch ();
12295 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12296 tempreg, BFD_RELOC_LO16);
12297 relax_end ();
12298 if (breg != 0)
12299 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12300 tempreg, tempreg, breg);
12301 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12302 }
12303 else if (mips_big_got && !HAVE_NEWABI)
12304 {
12305 int gpdelay;
12306
12307 /* If this is a reference to an external symbol, we want
12308 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12309 addu $tempreg,$tempreg,$gp
12310 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12311 <op> op[0],0($tempreg)
12312 Otherwise we want
12313 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12314 nop
12315 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12316 <op> op[0],0($tempreg)
12317 If there is a base register, we add it to $tempreg before
12318 the <op>. If there is a constant, we stick it in the
12319 <op> instruction. We don't handle constants larger than
12320 16 bits, because we have no way to load the upper 16 bits
12321 (actually, we could handle them for the subset of cases
12322 in which we are not using $at). */
12323 gas_assert (offset_expr.X_op == O_symbol);
12324 expr1.X_add_number = offset_expr.X_add_number;
12325 offset_expr.X_add_number = 0;
12326 if (expr1.X_add_number < -0x8000
12327 || expr1.X_add_number >= 0x8000)
12328 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12329 gpdelay = reg_needs_delay (mips_gp_register);
12330 relax_start (offset_expr.X_add_symbol);
12331 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12332 BFD_RELOC_MIPS_GOT_HI16);
12333 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12334 mips_gp_register);
12335 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12336 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12337 relax_switch ();
12338 if (gpdelay)
12339 macro_build (NULL, "nop", "");
12340 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12341 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12342 load_delay_nop ();
12343 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12344 tempreg, BFD_RELOC_LO16);
12345 relax_end ();
12346
12347 if (breg != 0)
12348 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12349 tempreg, tempreg, breg);
12350 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12351 }
12352 else if (mips_big_got && HAVE_NEWABI)
12353 {
12354 /* If this is a reference to an external symbol, we want
12355 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12356 add $tempreg,$tempreg,$gp
12357 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12358 <op> op[0],<ofst>($tempreg)
12359 Otherwise, for local symbols, we want:
12360 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12361 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
12362 gas_assert (offset_expr.X_op == O_symbol);
12363 expr1.X_add_number = offset_expr.X_add_number;
12364 offset_expr.X_add_number = 0;
12365 if (expr1.X_add_number < -0x8000
12366 || expr1.X_add_number >= 0x8000)
12367 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12368 relax_start (offset_expr.X_add_symbol);
12369 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12370 BFD_RELOC_MIPS_GOT_HI16);
12371 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12372 mips_gp_register);
12373 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12374 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12375 if (breg != 0)
12376 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12377 tempreg, tempreg, breg);
12378 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12379
12380 relax_switch ();
12381 offset_expr.X_add_number = expr1.X_add_number;
12382 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12383 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12384 if (breg != 0)
12385 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12386 tempreg, tempreg, breg);
12387 macro_build (&offset_expr, s, fmt, op[0],
12388 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12389 relax_end ();
12390 }
12391 else
12392 abort ();
12393
12394 break;
12395
12396 case M_JRADDIUSP:
12397 gas_assert (mips_opts.micromips);
12398 gas_assert (mips_opts.insn32);
12399 start_noreorder ();
12400 macro_build (NULL, "jr", "s", RA);
12401 expr1.X_add_number = op[0] << 2;
12402 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12403 end_noreorder ();
12404 break;
12405
12406 case M_JRC:
12407 gas_assert (mips_opts.micromips);
12408 gas_assert (mips_opts.insn32);
12409 macro_build (NULL, "jr", "s", op[0]);
12410 if (mips_opts.noreorder)
12411 macro_build (NULL, "nop", "");
12412 break;
12413
12414 case M_LI:
12415 case M_LI_S:
12416 load_register (op[0], &imm_expr, 0);
12417 break;
12418
12419 case M_DLI:
12420 load_register (op[0], &imm_expr, 1);
12421 break;
12422
12423 case M_LI_SS:
12424 if (imm_expr.X_op == O_constant)
12425 {
12426 used_at = 1;
12427 load_register (AT, &imm_expr, 0);
12428 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12429 break;
12430 }
12431 else
12432 {
12433 gas_assert (imm_expr.X_op == O_absent
12434 && offset_expr.X_op == O_symbol
12435 && strcmp (segment_name (S_GET_SEGMENT
12436 (offset_expr.X_add_symbol)),
12437 ".lit4") == 0
12438 && offset_expr.X_add_number == 0);
12439 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
12440 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
12441 break;
12442 }
12443
12444 case M_LI_D:
12445 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12446 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12447 order 32 bits of the value and the low order 32 bits are either
12448 zero or in OFFSET_EXPR. */
12449 if (imm_expr.X_op == O_constant)
12450 {
12451 if (GPR_SIZE == 64)
12452 load_register (op[0], &imm_expr, 1);
12453 else
12454 {
12455 int hreg, lreg;
12456
12457 if (target_big_endian)
12458 {
12459 hreg = op[0];
12460 lreg = op[0] + 1;
12461 }
12462 else
12463 {
12464 hreg = op[0] + 1;
12465 lreg = op[0];
12466 }
12467
12468 if (hreg <= 31)
12469 load_register (hreg, &imm_expr, 0);
12470 if (lreg <= 31)
12471 {
12472 if (offset_expr.X_op == O_absent)
12473 move_register (lreg, 0);
12474 else
12475 {
12476 gas_assert (offset_expr.X_op == O_constant);
12477 load_register (lreg, &offset_expr, 0);
12478 }
12479 }
12480 }
12481 break;
12482 }
12483 gas_assert (imm_expr.X_op == O_absent);
12484
12485 /* We know that sym is in the .rdata section. First we get the
12486 upper 16 bits of the address. */
12487 if (mips_pic == NO_PIC)
12488 {
12489 macro_build_lui (&offset_expr, AT);
12490 used_at = 1;
12491 }
12492 else
12493 {
12494 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12495 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12496 used_at = 1;
12497 }
12498
12499 /* Now we load the register(s). */
12500 if (GPR_SIZE == 64)
12501 {
12502 used_at = 1;
12503 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12504 BFD_RELOC_LO16, AT);
12505 }
12506 else
12507 {
12508 used_at = 1;
12509 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12510 BFD_RELOC_LO16, AT);
12511 if (op[0] != RA)
12512 {
12513 /* FIXME: How in the world do we deal with the possible
12514 overflow here? */
12515 offset_expr.X_add_number += 4;
12516 macro_build (&offset_expr, "lw", "t,o(b)",
12517 op[0] + 1, BFD_RELOC_LO16, AT);
12518 }
12519 }
12520 break;
12521
12522 case M_LI_DD:
12523 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12524 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12525 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12526 the value and the low order 32 bits are either zero or in
12527 OFFSET_EXPR. */
12528 if (imm_expr.X_op == O_constant)
12529 {
12530 used_at = 1;
12531 load_register (AT, &imm_expr, FPR_SIZE == 64);
12532 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12533 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
12534 else
12535 {
12536 if (ISA_HAS_MXHC1 (mips_opts.isa))
12537 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12538 else if (FPR_SIZE != 32)
12539 as_bad (_("Unable to generate `%s' compliant code "
12540 "without mthc1"),
12541 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12542 else
12543 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
12544 if (offset_expr.X_op == O_absent)
12545 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
12546 else
12547 {
12548 gas_assert (offset_expr.X_op == O_constant);
12549 load_register (AT, &offset_expr, 0);
12550 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12551 }
12552 }
12553 break;
12554 }
12555
12556 gas_assert (imm_expr.X_op == O_absent
12557 && offset_expr.X_op == O_symbol
12558 && offset_expr.X_add_number == 0);
12559 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12560 if (strcmp (s, ".lit8") == 0)
12561 {
12562 op[2] = mips_gp_register;
12563 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12564 offset_reloc[1] = BFD_RELOC_UNUSED;
12565 offset_reloc[2] = BFD_RELOC_UNUSED;
12566 }
12567 else
12568 {
12569 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
12570 used_at = 1;
12571 if (mips_pic != NO_PIC)
12572 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12573 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12574 else
12575 {
12576 /* FIXME: This won't work for a 64 bit address. */
12577 macro_build_lui (&offset_expr, AT);
12578 }
12579
12580 op[2] = AT;
12581 offset_reloc[0] = BFD_RELOC_LO16;
12582 offset_reloc[1] = BFD_RELOC_UNUSED;
12583 offset_reloc[2] = BFD_RELOC_UNUSED;
12584 }
12585 align = 8;
12586 /* Fall through */
12587
12588 case M_L_DAB:
12589 /*
12590 * The MIPS assembler seems to check for X_add_number not
12591 * being double aligned and generating:
12592 * lui at,%hi(foo+1)
12593 * addu at,at,v1
12594 * addiu at,at,%lo(foo+1)
12595 * lwc1 f2,0(at)
12596 * lwc1 f3,4(at)
12597 * But, the resulting address is the same after relocation so why
12598 * generate the extra instruction?
12599 */
12600 /* Itbl support may require additional care here. */
12601 coproc = 1;
12602 fmt = "T,o(b)";
12603 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12604 {
12605 s = "ldc1";
12606 goto ld_st;
12607 }
12608 s = "lwc1";
12609 goto ldd_std;
12610
12611 case M_S_DAB:
12612 gas_assert (!mips_opts.micromips);
12613 /* Itbl support may require additional care here. */
12614 coproc = 1;
12615 fmt = "T,o(b)";
12616 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12617 {
12618 s = "sdc1";
12619 goto ld_st;
12620 }
12621 s = "swc1";
12622 goto ldd_std;
12623
12624 case M_LQ_AB:
12625 fmt = "t,o(b)";
12626 s = "lq";
12627 goto ld;
12628
12629 case M_SQ_AB:
12630 fmt = "t,o(b)";
12631 s = "sq";
12632 goto ld_st;
12633
12634 case M_LD_AB:
12635 fmt = "t,o(b)";
12636 if (GPR_SIZE == 64)
12637 {
12638 s = "ld";
12639 goto ld;
12640 }
12641 s = "lw";
12642 goto ldd_std;
12643
12644 case M_SD_AB:
12645 fmt = "t,o(b)";
12646 if (GPR_SIZE == 64)
12647 {
12648 s = "sd";
12649 goto ld_st;
12650 }
12651 s = "sw";
12652
12653 ldd_std:
12654 /* Even on a big endian machine $fn comes before $fn+1. We have
12655 to adjust when loading from memory. We set coproc if we must
12656 load $fn+1 first. */
12657 /* Itbl support may require additional care here. */
12658 if (!target_big_endian)
12659 coproc = 0;
12660
12661 breg = op[2];
12662 if (small_offset_p (0, align, 16))
12663 {
12664 ep = &offset_expr;
12665 if (!small_offset_p (4, align, 16))
12666 {
12667 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12668 -1, offset_reloc[0], offset_reloc[1],
12669 offset_reloc[2]);
12670 expr1.X_add_number = 0;
12671 ep = &expr1;
12672 breg = AT;
12673 used_at = 1;
12674 offset_reloc[0] = BFD_RELOC_LO16;
12675 offset_reloc[1] = BFD_RELOC_UNUSED;
12676 offset_reloc[2] = BFD_RELOC_UNUSED;
12677 }
12678 if (strcmp (s, "lw") == 0 && op[0] == breg)
12679 {
12680 ep->X_add_number += 4;
12681 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
12682 offset_reloc[1], offset_reloc[2], breg);
12683 ep->X_add_number -= 4;
12684 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
12685 offset_reloc[1], offset_reloc[2], breg);
12686 }
12687 else
12688 {
12689 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
12690 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12691 breg);
12692 ep->X_add_number += 4;
12693 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
12694 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12695 breg);
12696 }
12697 break;
12698 }
12699
12700 if (offset_expr.X_op != O_symbol
12701 && offset_expr.X_op != O_constant)
12702 {
12703 as_bad (_("expression too complex"));
12704 offset_expr.X_op = O_constant;
12705 }
12706
12707 if (HAVE_32BIT_ADDRESSES
12708 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12709 {
12710 char value [32];
12711
12712 sprintf_vma (value, offset_expr.X_add_number);
12713 as_bad (_("number (0x%s) larger than 32 bits"), value);
12714 }
12715
12716 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
12717 {
12718 /* If this is a reference to a GP relative symbol, we want
12719 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12720 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
12721 If we have a base register, we use this
12722 addu $at,$breg,$gp
12723 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12724 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
12725 If this is not a GP relative symbol, we want
12726 lui $at,<sym> (BFD_RELOC_HI16_S)
12727 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12728 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12729 If there is a base register, we add it to $at after the
12730 lui instruction. If there is a constant, we always use
12731 the last case. */
12732 if (offset_expr.X_op == O_symbol
12733 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12734 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12735 {
12736 relax_start (offset_expr.X_add_symbol);
12737 if (breg == 0)
12738 {
12739 tempreg = mips_gp_register;
12740 }
12741 else
12742 {
12743 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12744 AT, breg, mips_gp_register);
12745 tempreg = AT;
12746 used_at = 1;
12747 }
12748
12749 /* Itbl support may require additional care here. */
12750 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12751 BFD_RELOC_GPREL16, tempreg);
12752 offset_expr.X_add_number += 4;
12753
12754 /* Set mips_optimize to 2 to avoid inserting an
12755 undesired nop. */
12756 hold_mips_optimize = mips_optimize;
12757 mips_optimize = 2;
12758 /* Itbl support may require additional care here. */
12759 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12760 BFD_RELOC_GPREL16, tempreg);
12761 mips_optimize = hold_mips_optimize;
12762
12763 relax_switch ();
12764
12765 offset_expr.X_add_number -= 4;
12766 }
12767 used_at = 1;
12768 if (offset_high_part (offset_expr.X_add_number, 16)
12769 != offset_high_part (offset_expr.X_add_number + 4, 16))
12770 {
12771 load_address (AT, &offset_expr, &used_at);
12772 offset_expr.X_op = O_constant;
12773 offset_expr.X_add_number = 0;
12774 }
12775 else
12776 macro_build_lui (&offset_expr, AT);
12777 if (breg != 0)
12778 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12779 /* Itbl support may require additional care here. */
12780 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12781 BFD_RELOC_LO16, AT);
12782 /* FIXME: How do we handle overflow here? */
12783 offset_expr.X_add_number += 4;
12784 /* Itbl support may require additional care here. */
12785 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12786 BFD_RELOC_LO16, AT);
12787 if (mips_relax.sequence)
12788 relax_end ();
12789 }
12790 else if (!mips_big_got)
12791 {
12792 /* If this is a reference to an external symbol, we want
12793 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12794 nop
12795 <op> op[0],0($at)
12796 <op> op[0]+1,4($at)
12797 Otherwise we want
12798 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12799 nop
12800 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12801 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12802 If there is a base register we add it to $at before the
12803 lwc1 instructions. If there is a constant we include it
12804 in the lwc1 instructions. */
12805 used_at = 1;
12806 expr1.X_add_number = offset_expr.X_add_number;
12807 if (expr1.X_add_number < -0x8000
12808 || expr1.X_add_number >= 0x8000 - 4)
12809 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12810 load_got_offset (AT, &offset_expr);
12811 load_delay_nop ();
12812 if (breg != 0)
12813 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12814
12815 /* Set mips_optimize to 2 to avoid inserting an undesired
12816 nop. */
12817 hold_mips_optimize = mips_optimize;
12818 mips_optimize = 2;
12819
12820 /* Itbl support may require additional care here. */
12821 relax_start (offset_expr.X_add_symbol);
12822 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12823 BFD_RELOC_LO16, AT);
12824 expr1.X_add_number += 4;
12825 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12826 BFD_RELOC_LO16, AT);
12827 relax_switch ();
12828 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12829 BFD_RELOC_LO16, AT);
12830 offset_expr.X_add_number += 4;
12831 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12832 BFD_RELOC_LO16, AT);
12833 relax_end ();
12834
12835 mips_optimize = hold_mips_optimize;
12836 }
12837 else if (mips_big_got)
12838 {
12839 int gpdelay;
12840
12841 /* If this is a reference to an external symbol, we want
12842 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12843 addu $at,$at,$gp
12844 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12845 nop
12846 <op> op[0],0($at)
12847 <op> op[0]+1,4($at)
12848 Otherwise we want
12849 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12850 nop
12851 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12852 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12853 If there is a base register we add it to $at before the
12854 lwc1 instructions. If there is a constant we include it
12855 in the lwc1 instructions. */
12856 used_at = 1;
12857 expr1.X_add_number = offset_expr.X_add_number;
12858 offset_expr.X_add_number = 0;
12859 if (expr1.X_add_number < -0x8000
12860 || expr1.X_add_number >= 0x8000 - 4)
12861 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12862 gpdelay = reg_needs_delay (mips_gp_register);
12863 relax_start (offset_expr.X_add_symbol);
12864 macro_build (&offset_expr, "lui", LUI_FMT,
12865 AT, BFD_RELOC_MIPS_GOT_HI16);
12866 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12867 AT, AT, mips_gp_register);
12868 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
12869 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
12870 load_delay_nop ();
12871 if (breg != 0)
12872 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12873 /* Itbl support may require additional care here. */
12874 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12875 BFD_RELOC_LO16, AT);
12876 expr1.X_add_number += 4;
12877
12878 /* Set mips_optimize to 2 to avoid inserting an undesired
12879 nop. */
12880 hold_mips_optimize = mips_optimize;
12881 mips_optimize = 2;
12882 /* Itbl support may require additional care here. */
12883 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12884 BFD_RELOC_LO16, AT);
12885 mips_optimize = hold_mips_optimize;
12886 expr1.X_add_number -= 4;
12887
12888 relax_switch ();
12889 offset_expr.X_add_number = expr1.X_add_number;
12890 if (gpdelay)
12891 macro_build (NULL, "nop", "");
12892 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12893 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12894 load_delay_nop ();
12895 if (breg != 0)
12896 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12897 /* Itbl support may require additional care here. */
12898 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12899 BFD_RELOC_LO16, AT);
12900 offset_expr.X_add_number += 4;
12901
12902 /* Set mips_optimize to 2 to avoid inserting an undesired
12903 nop. */
12904 hold_mips_optimize = mips_optimize;
12905 mips_optimize = 2;
12906 /* Itbl support may require additional care here. */
12907 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12908 BFD_RELOC_LO16, AT);
12909 mips_optimize = hold_mips_optimize;
12910 relax_end ();
12911 }
12912 else
12913 abort ();
12914
12915 break;
12916
12917 case M_SAA_AB:
12918 s = "saa";
12919 goto saa_saad;
12920 case M_SAAD_AB:
12921 s = "saad";
12922 saa_saad:
12923 gas_assert (!mips_opts.micromips);
12924 offbits = 0;
12925 fmt = "t,(b)";
12926 goto ld_st;
12927
12928 /* New code added to support COPZ instructions.
12929 This code builds table entries out of the macros in mip_opcodes.
12930 R4000 uses interlocks to handle coproc delays.
12931 Other chips (like the R3000) require nops to be inserted for delays.
12932
12933 FIXME: Currently, we require that the user handle delays.
12934 In order to fill delay slots for non-interlocked chips,
12935 we must have a way to specify delays based on the coprocessor.
12936 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12937 What are the side-effects of the cop instruction?
12938 What cache support might we have and what are its effects?
12939 Both coprocessor & memory require delays. how long???
12940 What registers are read/set/modified?
12941
12942 If an itbl is provided to interpret cop instructions,
12943 this knowledge can be encoded in the itbl spec. */
12944
12945 case M_COP0:
12946 s = "c0";
12947 goto copz;
12948 case M_COP1:
12949 s = "c1";
12950 goto copz;
12951 case M_COP2:
12952 s = "c2";
12953 goto copz;
12954 case M_COP3:
12955 s = "c3";
12956 copz:
12957 gas_assert (!mips_opts.micromips);
12958 /* For now we just do C (same as Cz). The parameter will be
12959 stored in insn_opcode by mips_ip. */
12960 macro_build (NULL, s, "C", (int) ip->insn_opcode);
12961 break;
12962
12963 case M_MOVE:
12964 move_register (op[0], op[1]);
12965 break;
12966
12967 case M_MOVEP:
12968 gas_assert (mips_opts.micromips);
12969 gas_assert (mips_opts.insn32);
12970 move_register (micromips_to_32_reg_h_map1[op[0]],
12971 micromips_to_32_reg_m_map[op[1]]);
12972 move_register (micromips_to_32_reg_h_map2[op[0]],
12973 micromips_to_32_reg_n_map[op[2]]);
12974 break;
12975
12976 case M_DMUL:
12977 dbl = 1;
12978 /* Fall through. */
12979 case M_MUL:
12980 if (mips_opts.arch == CPU_R5900)
12981 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12982 op[2]);
12983 else
12984 {
12985 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12986 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12987 }
12988 break;
12989
12990 case M_DMUL_I:
12991 dbl = 1;
12992 /* Fall through. */
12993 case M_MUL_I:
12994 /* The MIPS assembler some times generates shifts and adds. I'm
12995 not trying to be that fancy. GCC should do this for us
12996 anyway. */
12997 used_at = 1;
12998 load_register (AT, &imm_expr, dbl);
12999 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13000 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13001 break;
13002
13003 case M_DMULO_I:
13004 dbl = 1;
13005 /* Fall through. */
13006 case M_MULO_I:
13007 imm = 1;
13008 goto do_mulo;
13009
13010 case M_DMULO:
13011 dbl = 1;
13012 /* Fall through. */
13013 case M_MULO:
13014 do_mulo:
13015 start_noreorder ();
13016 used_at = 1;
13017 if (imm)
13018 load_register (AT, &imm_expr, dbl);
13019 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13020 op[1], imm ? AT : op[2]);
13021 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13022 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
13023 macro_build (NULL, "mfhi", MFHL_FMT, AT);
13024 if (mips_trap)
13025 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
13026 else
13027 {
13028 if (mips_opts.micromips)
13029 micromips_label_expr (&label_expr);
13030 else
13031 label_expr.X_add_number = 8;
13032 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
13033 macro_build (NULL, "nop", "");
13034 macro_build (NULL, "break", BRK_FMT, 6);
13035 if (mips_opts.micromips)
13036 micromips_add_label ();
13037 }
13038 end_noreorder ();
13039 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13040 break;
13041
13042 case M_DMULOU_I:
13043 dbl = 1;
13044 /* Fall through. */
13045 case M_MULOU_I:
13046 imm = 1;
13047 goto do_mulou;
13048
13049 case M_DMULOU:
13050 dbl = 1;
13051 /* Fall through. */
13052 case M_MULOU:
13053 do_mulou:
13054 start_noreorder ();
13055 used_at = 1;
13056 if (imm)
13057 load_register (AT, &imm_expr, dbl);
13058 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
13059 op[1], imm ? AT : op[2]);
13060 macro_build (NULL, "mfhi", MFHL_FMT, AT);
13061 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13062 if (mips_trap)
13063 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
13064 else
13065 {
13066 if (mips_opts.micromips)
13067 micromips_label_expr (&label_expr);
13068 else
13069 label_expr.X_add_number = 8;
13070 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
13071 macro_build (NULL, "nop", "");
13072 macro_build (NULL, "break", BRK_FMT, 6);
13073 if (mips_opts.micromips)
13074 micromips_add_label ();
13075 }
13076 end_noreorder ();
13077 break;
13078
13079 case M_DROL:
13080 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13081 {
13082 if (op[0] == op[1])
13083 {
13084 tempreg = AT;
13085 used_at = 1;
13086 }
13087 else
13088 tempreg = op[0];
13089 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13090 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
13091 break;
13092 }
13093 used_at = 1;
13094 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13095 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13096 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13097 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13098 break;
13099
13100 case M_ROL:
13101 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13102 {
13103 if (op[0] == op[1])
13104 {
13105 tempreg = AT;
13106 used_at = 1;
13107 }
13108 else
13109 tempreg = op[0];
13110 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13111 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
13112 break;
13113 }
13114 used_at = 1;
13115 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13116 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13117 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13118 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13119 break;
13120
13121 case M_DROL_I:
13122 {
13123 unsigned int rot;
13124 const char *l;
13125 const char *rr;
13126
13127 rot = imm_expr.X_add_number & 0x3f;
13128 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13129 {
13130 rot = (64 - rot) & 0x3f;
13131 if (rot >= 32)
13132 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13133 else
13134 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13135 break;
13136 }
13137 if (rot == 0)
13138 {
13139 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13140 break;
13141 }
13142 l = (rot < 0x20) ? "dsll" : "dsll32";
13143 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
13144 rot &= 0x1f;
13145 used_at = 1;
13146 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13147 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13148 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13149 }
13150 break;
13151
13152 case M_ROL_I:
13153 {
13154 unsigned int rot;
13155
13156 rot = imm_expr.X_add_number & 0x1f;
13157 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13158 {
13159 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13160 (32 - rot) & 0x1f);
13161 break;
13162 }
13163 if (rot == 0)
13164 {
13165 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13166 break;
13167 }
13168 used_at = 1;
13169 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13170 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13171 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13172 }
13173 break;
13174
13175 case M_DROR:
13176 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13177 {
13178 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
13179 break;
13180 }
13181 used_at = 1;
13182 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13183 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13184 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13185 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13186 break;
13187
13188 case M_ROR:
13189 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13190 {
13191 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
13192 break;
13193 }
13194 used_at = 1;
13195 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13196 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13197 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13198 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13199 break;
13200
13201 case M_DROR_I:
13202 {
13203 unsigned int rot;
13204 const char *l;
13205 const char *rr;
13206
13207 rot = imm_expr.X_add_number & 0x3f;
13208 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13209 {
13210 if (rot >= 32)
13211 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13212 else
13213 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13214 break;
13215 }
13216 if (rot == 0)
13217 {
13218 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13219 break;
13220 }
13221 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
13222 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13223 rot &= 0x1f;
13224 used_at = 1;
13225 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13226 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13227 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13228 }
13229 break;
13230
13231 case M_ROR_I:
13232 {
13233 unsigned int rot;
13234
13235 rot = imm_expr.X_add_number & 0x1f;
13236 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13237 {
13238 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
13239 break;
13240 }
13241 if (rot == 0)
13242 {
13243 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13244 break;
13245 }
13246 used_at = 1;
13247 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13248 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13249 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13250 }
13251 break;
13252
13253 case M_SEQ:
13254 if (op[1] == 0)
13255 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13256 else if (op[2] == 0)
13257 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13258 else
13259 {
13260 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13261 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13262 }
13263 break;
13264
13265 case M_SEQ_I:
13266 if (imm_expr.X_add_number == 0)
13267 {
13268 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13269 break;
13270 }
13271 if (op[1] == 0)
13272 {
13273 as_warn (_("instruction %s: result is always false"),
13274 ip->insn_mo->name);
13275 move_register (op[0], 0);
13276 break;
13277 }
13278 if (CPU_HAS_SEQ (mips_opts.arch)
13279 && -512 <= imm_expr.X_add_number
13280 && imm_expr.X_add_number < 512)
13281 {
13282 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
13283 (int) imm_expr.X_add_number);
13284 break;
13285 }
13286 if (imm_expr.X_add_number >= 0
13287 && imm_expr.X_add_number < 0x10000)
13288 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
13289 else if (imm_expr.X_add_number > -0x8000
13290 && imm_expr.X_add_number < 0)
13291 {
13292 imm_expr.X_add_number = -imm_expr.X_add_number;
13293 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13294 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13295 }
13296 else if (CPU_HAS_SEQ (mips_opts.arch))
13297 {
13298 used_at = 1;
13299 load_register (AT, &imm_expr, GPR_SIZE == 64);
13300 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
13301 break;
13302 }
13303 else
13304 {
13305 load_register (AT, &imm_expr, GPR_SIZE == 64);
13306 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13307 used_at = 1;
13308 }
13309 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13310 break;
13311
13312 case M_SGE: /* X >= Y <==> not (X < Y) */
13313 s = "slt";
13314 goto sge;
13315 case M_SGEU:
13316 s = "sltu";
13317 sge:
13318 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13319 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13320 break;
13321
13322 case M_SGE_I: /* X >= I <==> not (X < I) */
13323 case M_SGEU_I:
13324 if (imm_expr.X_add_number >= -0x8000
13325 && imm_expr.X_add_number < 0x8000)
13326 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13327 op[0], op[1], BFD_RELOC_LO16);
13328 else
13329 {
13330 load_register (AT, &imm_expr, GPR_SIZE == 64);
13331 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
13332 op[0], op[1], AT);
13333 used_at = 1;
13334 }
13335 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13336 break;
13337
13338 case M_SGT: /* X > Y <==> Y < X */
13339 s = "slt";
13340 goto sgt;
13341 case M_SGTU:
13342 s = "sltu";
13343 sgt:
13344 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13345 break;
13346
13347 case M_SGT_I: /* X > I <==> I < X */
13348 s = "slt";
13349 goto sgti;
13350 case M_SGTU_I:
13351 s = "sltu";
13352 sgti:
13353 used_at = 1;
13354 load_register (AT, &imm_expr, GPR_SIZE == 64);
13355 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13356 break;
13357
13358 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
13359 s = "slt";
13360 goto sle;
13361 case M_SLEU:
13362 s = "sltu";
13363 sle:
13364 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13365 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13366 break;
13367
13368 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
13369 s = "slt";
13370 goto slei;
13371 case M_SLEU_I:
13372 s = "sltu";
13373 slei:
13374 used_at = 1;
13375 load_register (AT, &imm_expr, GPR_SIZE == 64);
13376 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13377 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13378 break;
13379
13380 case M_SLT_I:
13381 if (imm_expr.X_add_number >= -0x8000
13382 && imm_expr.X_add_number < 0x8000)
13383 {
13384 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13385 BFD_RELOC_LO16);
13386 break;
13387 }
13388 used_at = 1;
13389 load_register (AT, &imm_expr, GPR_SIZE == 64);
13390 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
13391 break;
13392
13393 case M_SLTU_I:
13394 if (imm_expr.X_add_number >= -0x8000
13395 && imm_expr.X_add_number < 0x8000)
13396 {
13397 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
13398 BFD_RELOC_LO16);
13399 break;
13400 }
13401 used_at = 1;
13402 load_register (AT, &imm_expr, GPR_SIZE == 64);
13403 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
13404 break;
13405
13406 case M_SNE:
13407 if (op[1] == 0)
13408 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13409 else if (op[2] == 0)
13410 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13411 else
13412 {
13413 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13414 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13415 }
13416 break;
13417
13418 case M_SNE_I:
13419 if (imm_expr.X_add_number == 0)
13420 {
13421 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13422 break;
13423 }
13424 if (op[1] == 0)
13425 {
13426 as_warn (_("instruction %s: result is always true"),
13427 ip->insn_mo->name);
13428 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
13429 op[0], 0, BFD_RELOC_LO16);
13430 break;
13431 }
13432 if (CPU_HAS_SEQ (mips_opts.arch)
13433 && -512 <= imm_expr.X_add_number
13434 && imm_expr.X_add_number < 512)
13435 {
13436 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
13437 (int) imm_expr.X_add_number);
13438 break;
13439 }
13440 if (imm_expr.X_add_number >= 0
13441 && imm_expr.X_add_number < 0x10000)
13442 {
13443 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13444 BFD_RELOC_LO16);
13445 }
13446 else if (imm_expr.X_add_number > -0x8000
13447 && imm_expr.X_add_number < 0)
13448 {
13449 imm_expr.X_add_number = -imm_expr.X_add_number;
13450 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13451 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13452 }
13453 else if (CPU_HAS_SEQ (mips_opts.arch))
13454 {
13455 used_at = 1;
13456 load_register (AT, &imm_expr, GPR_SIZE == 64);
13457 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
13458 break;
13459 }
13460 else
13461 {
13462 load_register (AT, &imm_expr, GPR_SIZE == 64);
13463 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13464 used_at = 1;
13465 }
13466 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13467 break;
13468
13469 case M_SUB_I:
13470 s = "addi";
13471 s2 = "sub";
13472 goto do_subi;
13473 case M_SUBU_I:
13474 s = "addiu";
13475 s2 = "subu";
13476 goto do_subi;
13477 case M_DSUB_I:
13478 dbl = 1;
13479 s = "daddi";
13480 s2 = "dsub";
13481 if (!mips_opts.micromips)
13482 goto do_subi;
13483 if (imm_expr.X_add_number > -0x200
13484 && imm_expr.X_add_number <= 0x200)
13485 {
13486 macro_build (NULL, s, "t,r,.", op[0], op[1],
13487 (int) -imm_expr.X_add_number);
13488 break;
13489 }
13490 goto do_subi_i;
13491 case M_DSUBU_I:
13492 dbl = 1;
13493 s = "daddiu";
13494 s2 = "dsubu";
13495 do_subi:
13496 if (imm_expr.X_add_number > -0x8000
13497 && imm_expr.X_add_number <= 0x8000)
13498 {
13499 imm_expr.X_add_number = -imm_expr.X_add_number;
13500 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13501 break;
13502 }
13503 do_subi_i:
13504 used_at = 1;
13505 load_register (AT, &imm_expr, dbl);
13506 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
13507 break;
13508
13509 case M_TEQ_I:
13510 s = "teq";
13511 goto trap;
13512 case M_TGE_I:
13513 s = "tge";
13514 goto trap;
13515 case M_TGEU_I:
13516 s = "tgeu";
13517 goto trap;
13518 case M_TLT_I:
13519 s = "tlt";
13520 goto trap;
13521 case M_TLTU_I:
13522 s = "tltu";
13523 goto trap;
13524 case M_TNE_I:
13525 s = "tne";
13526 trap:
13527 used_at = 1;
13528 load_register (AT, &imm_expr, GPR_SIZE == 64);
13529 macro_build (NULL, s, "s,t", op[0], AT);
13530 break;
13531
13532 case M_TRUNCWS:
13533 case M_TRUNCWD:
13534 gas_assert (!mips_opts.micromips);
13535 gas_assert (mips_opts.isa == ISA_MIPS1);
13536 used_at = 1;
13537
13538 /*
13539 * Is the double cfc1 instruction a bug in the mips assembler;
13540 * or is there a reason for it?
13541 */
13542 start_noreorder ();
13543 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13544 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13545 macro_build (NULL, "nop", "");
13546 expr1.X_add_number = 3;
13547 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
13548 expr1.X_add_number = 2;
13549 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13550 macro_build (NULL, "ctc1", "t,G", AT, RA);
13551 macro_build (NULL, "nop", "");
13552 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
13553 op[0], op[1]);
13554 macro_build (NULL, "ctc1", "t,G", op[2], RA);
13555 macro_build (NULL, "nop", "");
13556 end_noreorder ();
13557 break;
13558
13559 case M_ULH_AB:
13560 s = "lb";
13561 s2 = "lbu";
13562 off = 1;
13563 goto uld_st;
13564 case M_ULHU_AB:
13565 s = "lbu";
13566 s2 = "lbu";
13567 off = 1;
13568 goto uld_st;
13569 case M_ULW_AB:
13570 s = "lwl";
13571 s2 = "lwr";
13572 offbits = (mips_opts.micromips ? 12 : 16);
13573 off = 3;
13574 goto uld_st;
13575 case M_ULD_AB:
13576 s = "ldl";
13577 s2 = "ldr";
13578 offbits = (mips_opts.micromips ? 12 : 16);
13579 off = 7;
13580 goto uld_st;
13581 case M_USH_AB:
13582 s = "sb";
13583 s2 = "sb";
13584 off = 1;
13585 ust = 1;
13586 goto uld_st;
13587 case M_USW_AB:
13588 s = "swl";
13589 s2 = "swr";
13590 offbits = (mips_opts.micromips ? 12 : 16);
13591 off = 3;
13592 ust = 1;
13593 goto uld_st;
13594 case M_USD_AB:
13595 s = "sdl";
13596 s2 = "sdr";
13597 offbits = (mips_opts.micromips ? 12 : 16);
13598 off = 7;
13599 ust = 1;
13600
13601 uld_st:
13602 breg = op[2];
13603 large_offset = !small_offset_p (off, align, offbits);
13604 ep = &offset_expr;
13605 expr1.X_add_number = 0;
13606 if (large_offset)
13607 {
13608 used_at = 1;
13609 tempreg = AT;
13610 if (small_offset_p (0, align, 16))
13611 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13612 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13613 else
13614 {
13615 load_address (tempreg, ep, &used_at);
13616 if (breg != 0)
13617 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13618 tempreg, tempreg, breg);
13619 }
13620 offset_reloc[0] = BFD_RELOC_LO16;
13621 offset_reloc[1] = BFD_RELOC_UNUSED;
13622 offset_reloc[2] = BFD_RELOC_UNUSED;
13623 breg = tempreg;
13624 tempreg = op[0];
13625 ep = &expr1;
13626 }
13627 else if (!ust && op[0] == breg)
13628 {
13629 used_at = 1;
13630 tempreg = AT;
13631 }
13632 else
13633 tempreg = op[0];
13634
13635 if (off == 1)
13636 goto ulh_sh;
13637
13638 if (!target_big_endian)
13639 ep->X_add_number += off;
13640 if (offbits == 12)
13641 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
13642 else
13643 macro_build (ep, s, "t,o(b)", tempreg, -1,
13644 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13645
13646 if (!target_big_endian)
13647 ep->X_add_number -= off;
13648 else
13649 ep->X_add_number += off;
13650 if (offbits == 12)
13651 macro_build (NULL, s2, "t,~(b)",
13652 tempreg, (int) ep->X_add_number, breg);
13653 else
13654 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13655 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13656
13657 /* If necessary, move the result in tempreg to the final destination. */
13658 if (!ust && op[0] != tempreg)
13659 {
13660 /* Protect second load's delay slot. */
13661 load_delay_nop ();
13662 move_register (op[0], tempreg);
13663 }
13664 break;
13665
13666 ulh_sh:
13667 used_at = 1;
13668 if (target_big_endian == ust)
13669 ep->X_add_number += off;
13670 tempreg = ust || large_offset ? op[0] : AT;
13671 macro_build (ep, s, "t,o(b)", tempreg, -1,
13672 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13673
13674 /* For halfword transfers we need a temporary register to shuffle
13675 bytes. Unfortunately for M_USH_A we have none available before
13676 the next store as AT holds the base address. We deal with this
13677 case by clobbering TREG and then restoring it as with ULH. */
13678 tempreg = ust == large_offset ? op[0] : AT;
13679 if (ust)
13680 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
13681
13682 if (target_big_endian == ust)
13683 ep->X_add_number -= off;
13684 else
13685 ep->X_add_number += off;
13686 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13687 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13688
13689 /* For M_USH_A re-retrieve the LSB. */
13690 if (ust && large_offset)
13691 {
13692 if (target_big_endian)
13693 ep->X_add_number += off;
13694 else
13695 ep->X_add_number -= off;
13696 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13697 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
13698 }
13699 /* For ULH and M_USH_A OR the LSB in. */
13700 if (!ust || large_offset)
13701 {
13702 tempreg = !large_offset ? AT : op[0];
13703 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
13704 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13705 }
13706 break;
13707
13708 default:
13709 /* FIXME: Check if this is one of the itbl macros, since they
13710 are added dynamically. */
13711 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
13712 break;
13713 }
13714 if (!mips_opts.at && used_at)
13715 as_bad (_("macro used $at after \".set noat\""));
13716 }
13717
13718 /* Implement macros in mips16 mode. */
13719
13720 static void
13721 mips16_macro (struct mips_cl_insn *ip)
13722 {
13723 const struct mips_operand_array *operands;
13724 int mask;
13725 int tmp;
13726 expressionS expr1;
13727 int dbl;
13728 const char *s, *s2, *s3;
13729 unsigned int op[MAX_OPERANDS];
13730 unsigned int i;
13731
13732 mask = ip->insn_mo->mask;
13733
13734 operands = insn_operands (ip);
13735 for (i = 0; i < MAX_OPERANDS; i++)
13736 if (operands->operand[i])
13737 op[i] = insn_extract_operand (ip, operands->operand[i]);
13738 else
13739 op[i] = -1;
13740
13741 expr1.X_op = O_constant;
13742 expr1.X_op_symbol = NULL;
13743 expr1.X_add_symbol = NULL;
13744 expr1.X_add_number = 1;
13745
13746 dbl = 0;
13747
13748 switch (mask)
13749 {
13750 default:
13751 abort ();
13752
13753 case M_DDIV_3:
13754 dbl = 1;
13755 /* Fall through. */
13756 case M_DIV_3:
13757 s = "mflo";
13758 goto do_div3;
13759 case M_DREM_3:
13760 dbl = 1;
13761 /* Fall through. */
13762 case M_REM_3:
13763 s = "mfhi";
13764 do_div3:
13765 start_noreorder ();
13766 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
13767 expr1.X_add_number = 2;
13768 macro_build (&expr1, "bnez", "x,p", op[2]);
13769 macro_build (NULL, "break", "6", 7);
13770
13771 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13772 since that causes an overflow. We should do that as well,
13773 but I don't see how to do the comparisons without a temporary
13774 register. */
13775 end_noreorder ();
13776 macro_build (NULL, s, "x", op[0]);
13777 break;
13778
13779 case M_DIVU_3:
13780 s = "divu";
13781 s2 = "mflo";
13782 goto do_divu3;
13783 case M_REMU_3:
13784 s = "divu";
13785 s2 = "mfhi";
13786 goto do_divu3;
13787 case M_DDIVU_3:
13788 s = "ddivu";
13789 s2 = "mflo";
13790 goto do_divu3;
13791 case M_DREMU_3:
13792 s = "ddivu";
13793 s2 = "mfhi";
13794 do_divu3:
13795 start_noreorder ();
13796 macro_build (NULL, s, ".,x,y", op[1], op[2]);
13797 expr1.X_add_number = 2;
13798 macro_build (&expr1, "bnez", "x,p", op[2]);
13799 macro_build (NULL, "break", "6", 7);
13800 end_noreorder ();
13801 macro_build (NULL, s2, "x", op[0]);
13802 break;
13803
13804 case M_DMUL:
13805 dbl = 1;
13806 /* Fall through. */
13807 case M_MUL:
13808 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13809 macro_build (NULL, "mflo", "x", op[0]);
13810 break;
13811
13812 case M_DSUBU_I:
13813 dbl = 1;
13814 goto do_subu;
13815 case M_SUBU_I:
13816 do_subu:
13817 imm_expr.X_add_number = -imm_expr.X_add_number;
13818 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
13819 break;
13820
13821 case M_SUBU_I_2:
13822 imm_expr.X_add_number = -imm_expr.X_add_number;
13823 macro_build (&imm_expr, "addiu", "x,k", op[0]);
13824 break;
13825
13826 case M_DSUBU_I_2:
13827 imm_expr.X_add_number = -imm_expr.X_add_number;
13828 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
13829 break;
13830
13831 case M_BEQ:
13832 s = "cmp";
13833 s2 = "bteqz";
13834 goto do_branch;
13835 case M_BNE:
13836 s = "cmp";
13837 s2 = "btnez";
13838 goto do_branch;
13839 case M_BLT:
13840 s = "slt";
13841 s2 = "btnez";
13842 goto do_branch;
13843 case M_BLTU:
13844 s = "sltu";
13845 s2 = "btnez";
13846 goto do_branch;
13847 case M_BLE:
13848 s = "slt";
13849 s2 = "bteqz";
13850 goto do_reverse_branch;
13851 case M_BLEU:
13852 s = "sltu";
13853 s2 = "bteqz";
13854 goto do_reverse_branch;
13855 case M_BGE:
13856 s = "slt";
13857 s2 = "bteqz";
13858 goto do_branch;
13859 case M_BGEU:
13860 s = "sltu";
13861 s2 = "bteqz";
13862 goto do_branch;
13863 case M_BGT:
13864 s = "slt";
13865 s2 = "btnez";
13866 goto do_reverse_branch;
13867 case M_BGTU:
13868 s = "sltu";
13869 s2 = "btnez";
13870
13871 do_reverse_branch:
13872 tmp = op[1];
13873 op[1] = op[0];
13874 op[0] = tmp;
13875
13876 do_branch:
13877 macro_build (NULL, s, "x,y", op[0], op[1]);
13878 macro_build (&offset_expr, s2, "p");
13879 break;
13880
13881 case M_BEQ_I:
13882 s = "cmpi";
13883 s2 = "bteqz";
13884 s3 = "x,U";
13885 goto do_branch_i;
13886 case M_BNE_I:
13887 s = "cmpi";
13888 s2 = "btnez";
13889 s3 = "x,U";
13890 goto do_branch_i;
13891 case M_BLT_I:
13892 s = "slti";
13893 s2 = "btnez";
13894 s3 = "x,8";
13895 goto do_branch_i;
13896 case M_BLTU_I:
13897 s = "sltiu";
13898 s2 = "btnez";
13899 s3 = "x,8";
13900 goto do_branch_i;
13901 case M_BLE_I:
13902 s = "slti";
13903 s2 = "btnez";
13904 s3 = "x,8";
13905 goto do_addone_branch_i;
13906 case M_BLEU_I:
13907 s = "sltiu";
13908 s2 = "btnez";
13909 s3 = "x,8";
13910 goto do_addone_branch_i;
13911 case M_BGE_I:
13912 s = "slti";
13913 s2 = "bteqz";
13914 s3 = "x,8";
13915 goto do_branch_i;
13916 case M_BGEU_I:
13917 s = "sltiu";
13918 s2 = "bteqz";
13919 s3 = "x,8";
13920 goto do_branch_i;
13921 case M_BGT_I:
13922 s = "slti";
13923 s2 = "bteqz";
13924 s3 = "x,8";
13925 goto do_addone_branch_i;
13926 case M_BGTU_I:
13927 s = "sltiu";
13928 s2 = "bteqz";
13929 s3 = "x,8";
13930
13931 do_addone_branch_i:
13932 ++imm_expr.X_add_number;
13933
13934 do_branch_i:
13935 macro_build (&imm_expr, s, s3, op[0]);
13936 macro_build (&offset_expr, s2, "p");
13937 break;
13938
13939 case M_ABS:
13940 expr1.X_add_number = 0;
13941 macro_build (&expr1, "slti", "x,8", op[1]);
13942 if (op[0] != op[1])
13943 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
13944 expr1.X_add_number = 2;
13945 macro_build (&expr1, "bteqz", "p");
13946 macro_build (NULL, "neg", "x,w", op[0], op[0]);
13947 break;
13948 }
13949 }
13950
13951 /* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13952 opcode bits in *OPCODE_EXTRA. */
13953
13954 static struct mips_opcode *
13955 mips_lookup_insn (struct hash_control *hash, const char *start,
13956 ssize_t length, unsigned int *opcode_extra)
13957 {
13958 char *name, *dot, *p;
13959 unsigned int mask, suffix;
13960 ssize_t opend;
13961 struct mips_opcode *insn;
13962
13963 /* Make a copy of the instruction so that we can fiddle with it. */
13964 name = xstrndup (start, length);
13965
13966 /* Look up the instruction as-is. */
13967 insn = (struct mips_opcode *) hash_find (hash, name);
13968 if (insn)
13969 goto end;
13970
13971 dot = strchr (name, '.');
13972 if (dot && dot[1])
13973 {
13974 /* Try to interpret the text after the dot as a VU0 channel suffix. */
13975 p = mips_parse_vu0_channels (dot + 1, &mask);
13976 if (*p == 0 && mask != 0)
13977 {
13978 *dot = 0;
13979 insn = (struct mips_opcode *) hash_find (hash, name);
13980 *dot = '.';
13981 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13982 {
13983 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
13984 goto end;
13985 }
13986 }
13987 }
13988
13989 if (mips_opts.micromips)
13990 {
13991 /* See if there's an instruction size override suffix,
13992 either `16' or `32', at the end of the mnemonic proper,
13993 that defines the operation, i.e. before the first `.'
13994 character if any. Strip it and retry. */
13995 opend = dot != NULL ? dot - name : length;
13996 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13997 suffix = 2;
13998 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13999 suffix = 4;
14000 else
14001 suffix = 0;
14002 if (suffix)
14003 {
14004 memmove (name + opend - 2, name + opend, length - opend + 1);
14005 insn = (struct mips_opcode *) hash_find (hash, name);
14006 if (insn)
14007 {
14008 forced_insn_length = suffix;
14009 goto end;
14010 }
14011 }
14012 }
14013
14014 insn = NULL;
14015 end:
14016 free (name);
14017 return insn;
14018 }
14019
14020 /* Assemble an instruction into its binary format. If the instruction
14021 is a macro, set imm_expr and offset_expr to the values associated
14022 with "I" and "A" operands respectively. Otherwise store the value
14023 of the relocatable field (if any) in offset_expr. In both cases
14024 set offset_reloc to the relocation operators applied to offset_expr. */
14025
14026 static void
14027 mips_ip (char *str, struct mips_cl_insn *insn)
14028 {
14029 const struct mips_opcode *first, *past;
14030 struct hash_control *hash;
14031 char format;
14032 size_t end;
14033 struct mips_operand_token *tokens;
14034 unsigned int opcode_extra;
14035
14036 if (mips_opts.micromips)
14037 {
14038 hash = micromips_op_hash;
14039 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14040 }
14041 else
14042 {
14043 hash = op_hash;
14044 past = &mips_opcodes[NUMOPCODES];
14045 }
14046 forced_insn_length = 0;
14047 opcode_extra = 0;
14048
14049 /* We first try to match an instruction up to a space or to the end. */
14050 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14051 continue;
14052
14053 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14054 if (first == NULL)
14055 {
14056 set_insn_error (0, _("unrecognized opcode"));
14057 return;
14058 }
14059
14060 if (strcmp (first->name, "li.s") == 0)
14061 format = 'f';
14062 else if (strcmp (first->name, "li.d") == 0)
14063 format = 'd';
14064 else
14065 format = 0;
14066 tokens = mips_parse_arguments (str + end, format);
14067 if (!tokens)
14068 return;
14069
14070 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14071 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
14072 set_insn_error (0, _("invalid operands"));
14073
14074 obstack_free (&mips_operand_tokens, tokens);
14075 }
14076
14077 /* As for mips_ip, but used when assembling MIPS16 code.
14078 Also set forced_insn_length to the resulting instruction size in
14079 bytes if the user explicitly requested a small or extended instruction. */
14080
14081 static void
14082 mips16_ip (char *str, struct mips_cl_insn *insn)
14083 {
14084 char *end, *s, c;
14085 struct mips_opcode *first;
14086 struct mips_operand_token *tokens;
14087 unsigned int l;
14088
14089 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
14090 ;
14091 end = s;
14092 c = *end;
14093
14094 l = 0;
14095 switch (c)
14096 {
14097 case '\0':
14098 break;
14099
14100 case ' ':
14101 s++;
14102 break;
14103
14104 case '.':
14105 s++;
14106 if (*s == 't')
14107 {
14108 l = 2;
14109 s++;
14110 }
14111 else if (*s == 'e')
14112 {
14113 l = 4;
14114 s++;
14115 }
14116 if (*s == '\0')
14117 break;
14118 else if (*s++ == ' ')
14119 break;
14120 set_insn_error (0, _("unrecognized opcode"));
14121 return;
14122 }
14123 forced_insn_length = l;
14124
14125 *end = 0;
14126 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
14127 *end = c;
14128
14129 if (!first)
14130 {
14131 set_insn_error (0, _("unrecognized opcode"));
14132 return;
14133 }
14134
14135 tokens = mips_parse_arguments (s, 0);
14136 if (!tokens)
14137 return;
14138
14139 if (!match_mips16_insns (insn, first, tokens))
14140 set_insn_error (0, _("invalid operands"));
14141
14142 obstack_free (&mips_operand_tokens, tokens);
14143 }
14144
14145 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14146 NBITS is the number of significant bits in VAL. */
14147
14148 static unsigned long
14149 mips16_immed_extend (offsetT val, unsigned int nbits)
14150 {
14151 int extval;
14152
14153 extval = 0;
14154 val &= (1U << nbits) - 1;
14155 if (nbits == 16 || nbits == 9)
14156 {
14157 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14158 val &= 0x1f;
14159 }
14160 else if (nbits == 15)
14161 {
14162 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14163 val &= 0xf;
14164 }
14165 else if (nbits == 6)
14166 {
14167 extval = ((val & 0x1f) << 6) | (val & 0x20);
14168 val = 0;
14169 }
14170 return (extval << 16) | val;
14171 }
14172
14173 /* Like decode_mips16_operand, but require the operand to be defined and
14174 require it to be an integer. */
14175
14176 static const struct mips_int_operand *
14177 mips16_immed_operand (int type, bfd_boolean extended_p)
14178 {
14179 const struct mips_operand *operand;
14180
14181 operand = decode_mips16_operand (type, extended_p);
14182 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14183 abort ();
14184 return (const struct mips_int_operand *) operand;
14185 }
14186
14187 /* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14188
14189 static bfd_boolean
14190 mips16_immed_in_range_p (const struct mips_int_operand *operand,
14191 bfd_reloc_code_real_type reloc, offsetT sval)
14192 {
14193 int min_val, max_val;
14194
14195 min_val = mips_int_operand_min (operand);
14196 max_val = mips_int_operand_max (operand);
14197 if (reloc != BFD_RELOC_UNUSED)
14198 {
14199 if (min_val < 0)
14200 sval = SEXT_16BIT (sval);
14201 else
14202 sval &= 0xffff;
14203 }
14204
14205 return (sval >= min_val
14206 && sval <= max_val
14207 && (sval & ((1 << operand->shift) - 1)) == 0);
14208 }
14209
14210 /* Install immediate value VAL into MIPS16 instruction *INSN,
14211 extending it if necessary. The instruction in *INSN may
14212 already be extended.
14213
14214 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14215 if none. In the former case, VAL is a 16-bit number with no
14216 defined signedness.
14217
14218 TYPE is the type of the immediate field. USER_INSN_LENGTH
14219 is the length that the user requested, or 0 if none. */
14220
14221 static void
14222 mips16_immed (const char *file, unsigned int line, int type,
14223 bfd_reloc_code_real_type reloc, offsetT val,
14224 unsigned int user_insn_length, unsigned long *insn)
14225 {
14226 const struct mips_int_operand *operand;
14227 unsigned int uval, length;
14228
14229 operand = mips16_immed_operand (type, FALSE);
14230 if (!mips16_immed_in_range_p (operand, reloc, val))
14231 {
14232 /* We need an extended instruction. */
14233 if (user_insn_length == 2)
14234 as_bad_where (file, line, _("invalid unextended operand value"));
14235 else
14236 *insn |= MIPS16_EXTEND;
14237 }
14238 else if (user_insn_length == 4)
14239 {
14240 /* The operand doesn't force an unextended instruction to be extended.
14241 Warn if the user wanted an extended instruction anyway. */
14242 *insn |= MIPS16_EXTEND;
14243 as_warn_where (file, line,
14244 _("extended operand requested but not required"));
14245 }
14246
14247 length = mips16_opcode_length (*insn);
14248 if (length == 4)
14249 {
14250 operand = mips16_immed_operand (type, TRUE);
14251 if (!mips16_immed_in_range_p (operand, reloc, val))
14252 as_bad_where (file, line,
14253 _("operand value out of range for instruction"));
14254 }
14255 uval = ((unsigned int) val >> operand->shift) - operand->bias;
14256 if (length == 2 || operand->root.lsb != 0)
14257 *insn = mips_insert_operand (&operand->root, *insn, uval);
14258 else
14259 *insn |= mips16_immed_extend (uval, operand->root.size);
14260 }
14261 \f
14262 struct percent_op_match
14263 {
14264 const char *str;
14265 bfd_reloc_code_real_type reloc;
14266 };
14267
14268 static const struct percent_op_match mips_percent_op[] =
14269 {
14270 {"%lo", BFD_RELOC_LO16},
14271 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14272 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14273 {"%call16", BFD_RELOC_MIPS_CALL16},
14274 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14275 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14276 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14277 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14278 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14279 {"%got", BFD_RELOC_MIPS_GOT16},
14280 {"%gp_rel", BFD_RELOC_GPREL16},
14281 {"%gprel", BFD_RELOC_GPREL16},
14282 {"%half", BFD_RELOC_16},
14283 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14284 {"%higher", BFD_RELOC_MIPS_HIGHER},
14285 {"%neg", BFD_RELOC_MIPS_SUB},
14286 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14287 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14288 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14289 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14290 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14291 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14292 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14293 {"%hi", BFD_RELOC_HI16_S},
14294 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14295 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
14296 };
14297
14298 static const struct percent_op_match mips16_percent_op[] =
14299 {
14300 {"%lo", BFD_RELOC_MIPS16_LO16},
14301 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
14302 {"%gprel", BFD_RELOC_MIPS16_GPREL},
14303 {"%got", BFD_RELOC_MIPS16_GOT16},
14304 {"%call16", BFD_RELOC_MIPS16_CALL16},
14305 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14306 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14307 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14308 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14309 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14310 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14311 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14312 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14313 };
14314
14315
14316 /* Return true if *STR points to a relocation operator. When returning true,
14317 move *STR over the operator and store its relocation code in *RELOC.
14318 Leave both *STR and *RELOC alone when returning false. */
14319
14320 static bfd_boolean
14321 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14322 {
14323 const struct percent_op_match *percent_op;
14324 size_t limit, i;
14325
14326 if (mips_opts.mips16)
14327 {
14328 percent_op = mips16_percent_op;
14329 limit = ARRAY_SIZE (mips16_percent_op);
14330 }
14331 else
14332 {
14333 percent_op = mips_percent_op;
14334 limit = ARRAY_SIZE (mips_percent_op);
14335 }
14336
14337 for (i = 0; i < limit; i++)
14338 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14339 {
14340 int len = strlen (percent_op[i].str);
14341
14342 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14343 continue;
14344
14345 *str += strlen (percent_op[i].str);
14346 *reloc = percent_op[i].reloc;
14347
14348 /* Check whether the output BFD supports this relocation.
14349 If not, issue an error and fall back on something safe. */
14350 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14351 {
14352 as_bad (_("relocation %s isn't supported by the current ABI"),
14353 percent_op[i].str);
14354 *reloc = BFD_RELOC_UNUSED;
14355 }
14356 return TRUE;
14357 }
14358 return FALSE;
14359 }
14360
14361
14362 /* Parse string STR as a 16-bit relocatable operand. Store the
14363 expression in *EP and the relocations in the array starting
14364 at RELOC. Return the number of relocation operators used.
14365
14366 On exit, EXPR_END points to the first character after the expression. */
14367
14368 static size_t
14369 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14370 char *str)
14371 {
14372 bfd_reloc_code_real_type reversed_reloc[3];
14373 size_t reloc_index, i;
14374 int crux_depth, str_depth;
14375 char *crux;
14376
14377 /* Search for the start of the main expression, recoding relocations
14378 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14379 of the main expression and with CRUX_DEPTH containing the number
14380 of open brackets at that point. */
14381 reloc_index = -1;
14382 str_depth = 0;
14383 do
14384 {
14385 reloc_index++;
14386 crux = str;
14387 crux_depth = str_depth;
14388
14389 /* Skip over whitespace and brackets, keeping count of the number
14390 of brackets. */
14391 while (*str == ' ' || *str == '\t' || *str == '(')
14392 if (*str++ == '(')
14393 str_depth++;
14394 }
14395 while (*str == '%'
14396 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14397 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14398
14399 my_getExpression (ep, crux);
14400 str = expr_end;
14401
14402 /* Match every open bracket. */
14403 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14404 if (*str++ == ')')
14405 crux_depth--;
14406
14407 if (crux_depth > 0)
14408 as_bad (_("unclosed '('"));
14409
14410 expr_end = str;
14411
14412 if (reloc_index != 0)
14413 {
14414 prev_reloc_op_frag = frag_now;
14415 for (i = 0; i < reloc_index; i++)
14416 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14417 }
14418
14419 return reloc_index;
14420 }
14421
14422 static void
14423 my_getExpression (expressionS *ep, char *str)
14424 {
14425 char *save_in;
14426
14427 save_in = input_line_pointer;
14428 input_line_pointer = str;
14429 expression (ep);
14430 expr_end = input_line_pointer;
14431 input_line_pointer = save_in;
14432 }
14433
14434 const char *
14435 md_atof (int type, char *litP, int *sizeP)
14436 {
14437 return ieee_md_atof (type, litP, sizeP, target_big_endian);
14438 }
14439
14440 void
14441 md_number_to_chars (char *buf, valueT val, int n)
14442 {
14443 if (target_big_endian)
14444 number_to_chars_bigendian (buf, val, n);
14445 else
14446 number_to_chars_littleendian (buf, val, n);
14447 }
14448 \f
14449 static int support_64bit_objects(void)
14450 {
14451 const char **list, **l;
14452 int yes;
14453
14454 list = bfd_target_list ();
14455 for (l = list; *l != NULL; l++)
14456 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14457 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14458 break;
14459 yes = (*l != NULL);
14460 free (list);
14461 return yes;
14462 }
14463
14464 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14465 NEW_VALUE. Warn if another value was already specified. Note:
14466 we have to defer parsing the -march and -mtune arguments in order
14467 to handle 'from-abi' correctly, since the ABI might be specified
14468 in a later argument. */
14469
14470 static void
14471 mips_set_option_string (const char **string_ptr, const char *new_value)
14472 {
14473 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14474 as_warn (_("a different %s was already specified, is now %s"),
14475 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14476 new_value);
14477
14478 *string_ptr = new_value;
14479 }
14480
14481 int
14482 md_parse_option (int c, const char *arg)
14483 {
14484 unsigned int i;
14485
14486 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14487 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14488 {
14489 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
14490 c == mips_ases[i].option_on);
14491 return 1;
14492 }
14493
14494 switch (c)
14495 {
14496 case OPTION_CONSTRUCT_FLOATS:
14497 mips_disable_float_construction = 0;
14498 break;
14499
14500 case OPTION_NO_CONSTRUCT_FLOATS:
14501 mips_disable_float_construction = 1;
14502 break;
14503
14504 case OPTION_TRAP:
14505 mips_trap = 1;
14506 break;
14507
14508 case OPTION_BREAK:
14509 mips_trap = 0;
14510 break;
14511
14512 case OPTION_EB:
14513 target_big_endian = 1;
14514 break;
14515
14516 case OPTION_EL:
14517 target_big_endian = 0;
14518 break;
14519
14520 case 'O':
14521 if (arg == NULL)
14522 mips_optimize = 1;
14523 else if (arg[0] == '0')
14524 mips_optimize = 0;
14525 else if (arg[0] == '1')
14526 mips_optimize = 1;
14527 else
14528 mips_optimize = 2;
14529 break;
14530
14531 case 'g':
14532 if (arg == NULL)
14533 mips_debug = 2;
14534 else
14535 mips_debug = atoi (arg);
14536 break;
14537
14538 case OPTION_MIPS1:
14539 file_mips_opts.isa = ISA_MIPS1;
14540 break;
14541
14542 case OPTION_MIPS2:
14543 file_mips_opts.isa = ISA_MIPS2;
14544 break;
14545
14546 case OPTION_MIPS3:
14547 file_mips_opts.isa = ISA_MIPS3;
14548 break;
14549
14550 case OPTION_MIPS4:
14551 file_mips_opts.isa = ISA_MIPS4;
14552 break;
14553
14554 case OPTION_MIPS5:
14555 file_mips_opts.isa = ISA_MIPS5;
14556 break;
14557
14558 case OPTION_MIPS32:
14559 file_mips_opts.isa = ISA_MIPS32;
14560 break;
14561
14562 case OPTION_MIPS32R2:
14563 file_mips_opts.isa = ISA_MIPS32R2;
14564 break;
14565
14566 case OPTION_MIPS32R3:
14567 file_mips_opts.isa = ISA_MIPS32R3;
14568 break;
14569
14570 case OPTION_MIPS32R5:
14571 file_mips_opts.isa = ISA_MIPS32R5;
14572 break;
14573
14574 case OPTION_MIPS32R6:
14575 file_mips_opts.isa = ISA_MIPS32R6;
14576 break;
14577
14578 case OPTION_MIPS64R2:
14579 file_mips_opts.isa = ISA_MIPS64R2;
14580 break;
14581
14582 case OPTION_MIPS64R3:
14583 file_mips_opts.isa = ISA_MIPS64R3;
14584 break;
14585
14586 case OPTION_MIPS64R5:
14587 file_mips_opts.isa = ISA_MIPS64R5;
14588 break;
14589
14590 case OPTION_MIPS64R6:
14591 file_mips_opts.isa = ISA_MIPS64R6;
14592 break;
14593
14594 case OPTION_MIPS64:
14595 file_mips_opts.isa = ISA_MIPS64;
14596 break;
14597
14598 case OPTION_MTUNE:
14599 mips_set_option_string (&mips_tune_string, arg);
14600 break;
14601
14602 case OPTION_MARCH:
14603 mips_set_option_string (&mips_arch_string, arg);
14604 break;
14605
14606 case OPTION_M4650:
14607 mips_set_option_string (&mips_arch_string, "4650");
14608 mips_set_option_string (&mips_tune_string, "4650");
14609 break;
14610
14611 case OPTION_NO_M4650:
14612 break;
14613
14614 case OPTION_M4010:
14615 mips_set_option_string (&mips_arch_string, "4010");
14616 mips_set_option_string (&mips_tune_string, "4010");
14617 break;
14618
14619 case OPTION_NO_M4010:
14620 break;
14621
14622 case OPTION_M4100:
14623 mips_set_option_string (&mips_arch_string, "4100");
14624 mips_set_option_string (&mips_tune_string, "4100");
14625 break;
14626
14627 case OPTION_NO_M4100:
14628 break;
14629
14630 case OPTION_M3900:
14631 mips_set_option_string (&mips_arch_string, "3900");
14632 mips_set_option_string (&mips_tune_string, "3900");
14633 break;
14634
14635 case OPTION_NO_M3900:
14636 break;
14637
14638 case OPTION_MICROMIPS:
14639 if (file_mips_opts.mips16 == 1)
14640 {
14641 as_bad (_("-mmicromips cannot be used with -mips16"));
14642 return 0;
14643 }
14644 file_mips_opts.micromips = 1;
14645 mips_no_prev_insn ();
14646 break;
14647
14648 case OPTION_NO_MICROMIPS:
14649 file_mips_opts.micromips = 0;
14650 mips_no_prev_insn ();
14651 break;
14652
14653 case OPTION_MIPS16:
14654 if (file_mips_opts.micromips == 1)
14655 {
14656 as_bad (_("-mips16 cannot be used with -micromips"));
14657 return 0;
14658 }
14659 file_mips_opts.mips16 = 1;
14660 mips_no_prev_insn ();
14661 break;
14662
14663 case OPTION_NO_MIPS16:
14664 file_mips_opts.mips16 = 0;
14665 mips_no_prev_insn ();
14666 break;
14667
14668 case OPTION_FIX_24K:
14669 mips_fix_24k = 1;
14670 break;
14671
14672 case OPTION_NO_FIX_24K:
14673 mips_fix_24k = 0;
14674 break;
14675
14676 case OPTION_FIX_RM7000:
14677 mips_fix_rm7000 = 1;
14678 break;
14679
14680 case OPTION_NO_FIX_RM7000:
14681 mips_fix_rm7000 = 0;
14682 break;
14683
14684 case OPTION_FIX_LOONGSON2F_JUMP:
14685 mips_fix_loongson2f_jump = TRUE;
14686 break;
14687
14688 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14689 mips_fix_loongson2f_jump = FALSE;
14690 break;
14691
14692 case OPTION_FIX_LOONGSON2F_NOP:
14693 mips_fix_loongson2f_nop = TRUE;
14694 break;
14695
14696 case OPTION_NO_FIX_LOONGSON2F_NOP:
14697 mips_fix_loongson2f_nop = FALSE;
14698 break;
14699
14700 case OPTION_FIX_VR4120:
14701 mips_fix_vr4120 = 1;
14702 break;
14703
14704 case OPTION_NO_FIX_VR4120:
14705 mips_fix_vr4120 = 0;
14706 break;
14707
14708 case OPTION_FIX_VR4130:
14709 mips_fix_vr4130 = 1;
14710 break;
14711
14712 case OPTION_NO_FIX_VR4130:
14713 mips_fix_vr4130 = 0;
14714 break;
14715
14716 case OPTION_FIX_CN63XXP1:
14717 mips_fix_cn63xxp1 = TRUE;
14718 break;
14719
14720 case OPTION_NO_FIX_CN63XXP1:
14721 mips_fix_cn63xxp1 = FALSE;
14722 break;
14723
14724 case OPTION_RELAX_BRANCH:
14725 mips_relax_branch = 1;
14726 break;
14727
14728 case OPTION_NO_RELAX_BRANCH:
14729 mips_relax_branch = 0;
14730 break;
14731
14732 case OPTION_IGNORE_BRANCH_ISA:
14733 mips_ignore_branch_isa = TRUE;
14734 break;
14735
14736 case OPTION_NO_IGNORE_BRANCH_ISA:
14737 mips_ignore_branch_isa = FALSE;
14738 break;
14739
14740 case OPTION_INSN32:
14741 file_mips_opts.insn32 = TRUE;
14742 break;
14743
14744 case OPTION_NO_INSN32:
14745 file_mips_opts.insn32 = FALSE;
14746 break;
14747
14748 case OPTION_MSHARED:
14749 mips_in_shared = TRUE;
14750 break;
14751
14752 case OPTION_MNO_SHARED:
14753 mips_in_shared = FALSE;
14754 break;
14755
14756 case OPTION_MSYM32:
14757 file_mips_opts.sym32 = TRUE;
14758 break;
14759
14760 case OPTION_MNO_SYM32:
14761 file_mips_opts.sym32 = FALSE;
14762 break;
14763
14764 /* When generating ELF code, we permit -KPIC and -call_shared to
14765 select SVR4_PIC, and -non_shared to select no PIC. This is
14766 intended to be compatible with Irix 5. */
14767 case OPTION_CALL_SHARED:
14768 mips_pic = SVR4_PIC;
14769 mips_abicalls = TRUE;
14770 break;
14771
14772 case OPTION_CALL_NONPIC:
14773 mips_pic = NO_PIC;
14774 mips_abicalls = TRUE;
14775 break;
14776
14777 case OPTION_NON_SHARED:
14778 mips_pic = NO_PIC;
14779 mips_abicalls = FALSE;
14780 break;
14781
14782 /* The -xgot option tells the assembler to use 32 bit offsets
14783 when accessing the got in SVR4_PIC mode. It is for Irix
14784 compatibility. */
14785 case OPTION_XGOT:
14786 mips_big_got = 1;
14787 break;
14788
14789 case 'G':
14790 g_switch_value = atoi (arg);
14791 g_switch_seen = 1;
14792 break;
14793
14794 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14795 and -mabi=64. */
14796 case OPTION_32:
14797 mips_abi = O32_ABI;
14798 break;
14799
14800 case OPTION_N32:
14801 mips_abi = N32_ABI;
14802 break;
14803
14804 case OPTION_64:
14805 mips_abi = N64_ABI;
14806 if (!support_64bit_objects())
14807 as_fatal (_("no compiled in support for 64 bit object file format"));
14808 break;
14809
14810 case OPTION_GP32:
14811 file_mips_opts.gp = 32;
14812 break;
14813
14814 case OPTION_GP64:
14815 file_mips_opts.gp = 64;
14816 break;
14817
14818 case OPTION_FP32:
14819 file_mips_opts.fp = 32;
14820 break;
14821
14822 case OPTION_FPXX:
14823 file_mips_opts.fp = 0;
14824 break;
14825
14826 case OPTION_FP64:
14827 file_mips_opts.fp = 64;
14828 break;
14829
14830 case OPTION_ODD_SPREG:
14831 file_mips_opts.oddspreg = 1;
14832 break;
14833
14834 case OPTION_NO_ODD_SPREG:
14835 file_mips_opts.oddspreg = 0;
14836 break;
14837
14838 case OPTION_SINGLE_FLOAT:
14839 file_mips_opts.single_float = 1;
14840 break;
14841
14842 case OPTION_DOUBLE_FLOAT:
14843 file_mips_opts.single_float = 0;
14844 break;
14845
14846 case OPTION_SOFT_FLOAT:
14847 file_mips_opts.soft_float = 1;
14848 break;
14849
14850 case OPTION_HARD_FLOAT:
14851 file_mips_opts.soft_float = 0;
14852 break;
14853
14854 case OPTION_MABI:
14855 if (strcmp (arg, "32") == 0)
14856 mips_abi = O32_ABI;
14857 else if (strcmp (arg, "o64") == 0)
14858 mips_abi = O64_ABI;
14859 else if (strcmp (arg, "n32") == 0)
14860 mips_abi = N32_ABI;
14861 else if (strcmp (arg, "64") == 0)
14862 {
14863 mips_abi = N64_ABI;
14864 if (! support_64bit_objects())
14865 as_fatal (_("no compiled in support for 64 bit object file "
14866 "format"));
14867 }
14868 else if (strcmp (arg, "eabi") == 0)
14869 mips_abi = EABI_ABI;
14870 else
14871 {
14872 as_fatal (_("invalid abi -mabi=%s"), arg);
14873 return 0;
14874 }
14875 break;
14876
14877 case OPTION_M7000_HILO_FIX:
14878 mips_7000_hilo_fix = TRUE;
14879 break;
14880
14881 case OPTION_MNO_7000_HILO_FIX:
14882 mips_7000_hilo_fix = FALSE;
14883 break;
14884
14885 case OPTION_MDEBUG:
14886 mips_flag_mdebug = TRUE;
14887 break;
14888
14889 case OPTION_NO_MDEBUG:
14890 mips_flag_mdebug = FALSE;
14891 break;
14892
14893 case OPTION_PDR:
14894 mips_flag_pdr = TRUE;
14895 break;
14896
14897 case OPTION_NO_PDR:
14898 mips_flag_pdr = FALSE;
14899 break;
14900
14901 case OPTION_MVXWORKS_PIC:
14902 mips_pic = VXWORKS_PIC;
14903 break;
14904
14905 case OPTION_NAN:
14906 if (strcmp (arg, "2008") == 0)
14907 mips_nan2008 = 1;
14908 else if (strcmp (arg, "legacy") == 0)
14909 mips_nan2008 = 0;
14910 else
14911 {
14912 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
14913 return 0;
14914 }
14915 break;
14916
14917 default:
14918 return 0;
14919 }
14920
14921 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14922
14923 return 1;
14924 }
14925 \f
14926 /* Set up globals to tune for the ISA or processor described by INFO. */
14927
14928 static void
14929 mips_set_tune (const struct mips_cpu_info *info)
14930 {
14931 if (info != 0)
14932 mips_tune = info->cpu;
14933 }
14934
14935
14936 void
14937 mips_after_parse_args (void)
14938 {
14939 const struct mips_cpu_info *arch_info = 0;
14940 const struct mips_cpu_info *tune_info = 0;
14941
14942 /* GP relative stuff not working for PE */
14943 if (strncmp (TARGET_OS, "pe", 2) == 0)
14944 {
14945 if (g_switch_seen && g_switch_value != 0)
14946 as_bad (_("-G not supported in this configuration"));
14947 g_switch_value = 0;
14948 }
14949
14950 if (mips_abi == NO_ABI)
14951 mips_abi = MIPS_DEFAULT_ABI;
14952
14953 /* The following code determines the architecture.
14954 Similar code was added to GCC 3.3 (see override_options() in
14955 config/mips/mips.c). The GAS and GCC code should be kept in sync
14956 as much as possible. */
14957
14958 if (mips_arch_string != 0)
14959 arch_info = mips_parse_cpu ("-march", mips_arch_string);
14960
14961 if (file_mips_opts.isa != ISA_UNKNOWN)
14962 {
14963 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
14964 ISA level specified by -mipsN, while arch_info->isa contains
14965 the -march selection (if any). */
14966 if (arch_info != 0)
14967 {
14968 /* -march takes precedence over -mipsN, since it is more descriptive.
14969 There's no harm in specifying both as long as the ISA levels
14970 are the same. */
14971 if (file_mips_opts.isa != arch_info->isa)
14972 as_bad (_("-%s conflicts with the other architecture options,"
14973 " which imply -%s"),
14974 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
14975 mips_cpu_info_from_isa (arch_info->isa)->name);
14976 }
14977 else
14978 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
14979 }
14980
14981 if (arch_info == 0)
14982 {
14983 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
14984 gas_assert (arch_info);
14985 }
14986
14987 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
14988 as_bad (_("-march=%s is not compatible with the selected ABI"),
14989 arch_info->name);
14990
14991 file_mips_opts.arch = arch_info->cpu;
14992 file_mips_opts.isa = arch_info->isa;
14993
14994 /* Set up initial mips_opts state. */
14995 mips_opts = file_mips_opts;
14996
14997 /* The register size inference code is now placed in
14998 file_mips_check_options. */
14999
15000 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15001 processor. */
15002 if (mips_tune_string != 0)
15003 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15004
15005 if (tune_info == 0)
15006 mips_set_tune (arch_info);
15007 else
15008 mips_set_tune (tune_info);
15009
15010 if (mips_flag_mdebug < 0)
15011 mips_flag_mdebug = 0;
15012 }
15013 \f
15014 void
15015 mips_init_after_args (void)
15016 {
15017 /* initialize opcodes */
15018 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15019 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15020 }
15021
15022 long
15023 md_pcrel_from (fixS *fixP)
15024 {
15025 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15026 switch (fixP->fx_r_type)
15027 {
15028 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15029 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15030 /* Return the address of the delay slot. */
15031 return addr + 2;
15032
15033 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15034 case BFD_RELOC_MICROMIPS_JMP:
15035 case BFD_RELOC_MIPS16_16_PCREL_S1:
15036 case BFD_RELOC_16_PCREL_S2:
15037 case BFD_RELOC_MIPS_21_PCREL_S2:
15038 case BFD_RELOC_MIPS_26_PCREL_S2:
15039 case BFD_RELOC_MIPS_JMP:
15040 /* Return the address of the delay slot. */
15041 return addr + 4;
15042
15043 case BFD_RELOC_MIPS_18_PCREL_S3:
15044 /* Return the aligned address of the doubleword containing
15045 the instruction. */
15046 return addr & ~7;
15047
15048 default:
15049 return addr;
15050 }
15051 }
15052
15053 /* This is called before the symbol table is processed. In order to
15054 work with gcc when using mips-tfile, we must keep all local labels.
15055 However, in other cases, we want to discard them. If we were
15056 called with -g, but we didn't see any debugging information, it may
15057 mean that gcc is smuggling debugging information through to
15058 mips-tfile, in which case we must generate all local labels. */
15059
15060 void
15061 mips_frob_file_before_adjust (void)
15062 {
15063 #ifndef NO_ECOFF_DEBUGGING
15064 if (ECOFF_DEBUGGING
15065 && mips_debug != 0
15066 && ! ecoff_debugging_seen)
15067 flag_keep_locals = 1;
15068 #endif
15069 }
15070
15071 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15072 the corresponding LO16 reloc. This is called before md_apply_fix and
15073 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15074 relocation operators.
15075
15076 For our purposes, a %lo() expression matches a %got() or %hi()
15077 expression if:
15078
15079 (a) it refers to the same symbol; and
15080 (b) the offset applied in the %lo() expression is no lower than
15081 the offset applied in the %got() or %hi().
15082
15083 (b) allows us to cope with code like:
15084
15085 lui $4,%hi(foo)
15086 lh $4,%lo(foo+2)($4)
15087
15088 ...which is legal on RELA targets, and has a well-defined behaviour
15089 if the user knows that adding 2 to "foo" will not induce a carry to
15090 the high 16 bits.
15091
15092 When several %lo()s match a particular %got() or %hi(), we use the
15093 following rules to distinguish them:
15094
15095 (1) %lo()s with smaller offsets are a better match than %lo()s with
15096 higher offsets.
15097
15098 (2) %lo()s with no matching %got() or %hi() are better than those
15099 that already have a matching %got() or %hi().
15100
15101 (3) later %lo()s are better than earlier %lo()s.
15102
15103 These rules are applied in order.
15104
15105 (1) means, among other things, that %lo()s with identical offsets are
15106 chosen if they exist.
15107
15108 (2) means that we won't associate several high-part relocations with
15109 the same low-part relocation unless there's no alternative. Having
15110 several high parts for the same low part is a GNU extension; this rule
15111 allows careful users to avoid it.
15112
15113 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15114 with the last high-part relocation being at the front of the list.
15115 It therefore makes sense to choose the last matching low-part
15116 relocation, all other things being equal. It's also easier
15117 to code that way. */
15118
15119 void
15120 mips_frob_file (void)
15121 {
15122 struct mips_hi_fixup *l;
15123 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15124
15125 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15126 {
15127 segment_info_type *seginfo;
15128 bfd_boolean matched_lo_p;
15129 fixS **hi_pos, **lo_pos, **pos;
15130
15131 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15132
15133 /* If a GOT16 relocation turns out to be against a global symbol,
15134 there isn't supposed to be a matching LO. Ignore %gots against
15135 constants; we'll report an error for those later. */
15136 if (got16_reloc_p (l->fixp->fx_r_type)
15137 && !(l->fixp->fx_addsy
15138 && pic_need_relax (l->fixp->fx_addsy)))
15139 continue;
15140
15141 /* Check quickly whether the next fixup happens to be a matching %lo. */
15142 if (fixup_has_matching_lo_p (l->fixp))
15143 continue;
15144
15145 seginfo = seg_info (l->seg);
15146
15147 /* Set HI_POS to the position of this relocation in the chain.
15148 Set LO_POS to the position of the chosen low-part relocation.
15149 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15150 relocation that matches an immediately-preceding high-part
15151 relocation. */
15152 hi_pos = NULL;
15153 lo_pos = NULL;
15154 matched_lo_p = FALSE;
15155 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15156
15157 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15158 {
15159 if (*pos == l->fixp)
15160 hi_pos = pos;
15161
15162 if ((*pos)->fx_r_type == looking_for_rtype
15163 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15164 && (*pos)->fx_offset >= l->fixp->fx_offset
15165 && (lo_pos == NULL
15166 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15167 || (!matched_lo_p
15168 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15169 lo_pos = pos;
15170
15171 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15172 && fixup_has_matching_lo_p (*pos));
15173 }
15174
15175 /* If we found a match, remove the high-part relocation from its
15176 current position and insert it before the low-part relocation.
15177 Make the offsets match so that fixup_has_matching_lo_p()
15178 will return true.
15179
15180 We don't warn about unmatched high-part relocations since some
15181 versions of gcc have been known to emit dead "lui ...%hi(...)"
15182 instructions. */
15183 if (lo_pos != NULL)
15184 {
15185 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15186 if (l->fixp->fx_next != *lo_pos)
15187 {
15188 *hi_pos = l->fixp->fx_next;
15189 l->fixp->fx_next = *lo_pos;
15190 *lo_pos = l->fixp;
15191 }
15192 }
15193 }
15194 }
15195
15196 int
15197 mips_force_relocation (fixS *fixp)
15198 {
15199 if (generic_force_reloc (fixp))
15200 return 1;
15201
15202 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15203 so that the linker relaxation can update targets. */
15204 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15205 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15206 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15207 return 1;
15208
15209 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15210 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15211 microMIPS symbols so that we can do cross-mode branch diagnostics
15212 and BAL to JALX conversion by the linker. */
15213 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15214 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15215 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15216 && fixp->fx_addsy
15217 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15218 return 1;
15219
15220 /* We want all PC-relative relocations to be kept for R6 relaxation. */
15221 if (ISA_IS_R6 (file_mips_opts.isa)
15222 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15223 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15224 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15225 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15226 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15227 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15228 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15229 return 1;
15230
15231 return 0;
15232 }
15233
15234 /* Implement TC_FORCE_RELOCATION_ABS. */
15235
15236 bfd_boolean
15237 mips_force_relocation_abs (fixS *fixp)
15238 {
15239 if (generic_force_reloc (fixp))
15240 return TRUE;
15241
15242 /* These relocations do not have enough bits in the in-place addend
15243 to hold an arbitrary absolute section's offset. */
15244 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15245 return TRUE;
15246
15247 return FALSE;
15248 }
15249
15250 /* Read the instruction associated with RELOC from BUF. */
15251
15252 static unsigned int
15253 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15254 {
15255 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15256 return read_compressed_insn (buf, 4);
15257 else
15258 return read_insn (buf);
15259 }
15260
15261 /* Write instruction INSN to BUF, given that it has been relocated
15262 by RELOC. */
15263
15264 static void
15265 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15266 unsigned long insn)
15267 {
15268 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15269 write_compressed_insn (buf, insn, 4);
15270 else
15271 write_insn (buf, insn);
15272 }
15273
15274 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15275 to a symbol in another ISA mode, which cannot be converted to JALX. */
15276
15277 static bfd_boolean
15278 fix_bad_cross_mode_jump_p (fixS *fixP)
15279 {
15280 unsigned long opcode;
15281 int other;
15282 char *buf;
15283
15284 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15285 return FALSE;
15286
15287 other = S_GET_OTHER (fixP->fx_addsy);
15288 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15289 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15290 switch (fixP->fx_r_type)
15291 {
15292 case BFD_RELOC_MIPS_JMP:
15293 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15294 case BFD_RELOC_MICROMIPS_JMP:
15295 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15296 default:
15297 return FALSE;
15298 }
15299 }
15300
15301 /* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15302 jump to a symbol in the same ISA mode. */
15303
15304 static bfd_boolean
15305 fix_bad_same_mode_jalx_p (fixS *fixP)
15306 {
15307 unsigned long opcode;
15308 int other;
15309 char *buf;
15310
15311 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15312 return FALSE;
15313
15314 other = S_GET_OTHER (fixP->fx_addsy);
15315 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15316 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15317 switch (fixP->fx_r_type)
15318 {
15319 case BFD_RELOC_MIPS_JMP:
15320 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15321 case BFD_RELOC_MIPS16_JMP:
15322 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15323 case BFD_RELOC_MICROMIPS_JMP:
15324 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15325 default:
15326 return FALSE;
15327 }
15328 }
15329
15330 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15331 to a symbol whose value plus addend is not aligned according to the
15332 ultimate (after linker relaxation) jump instruction's immediate field
15333 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15334 regular MIPS code, to (1 << 2). */
15335
15336 static bfd_boolean
15337 fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15338 {
15339 bfd_boolean micro_to_mips_p;
15340 valueT val;
15341 int other;
15342
15343 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15344 return FALSE;
15345
15346 other = S_GET_OTHER (fixP->fx_addsy);
15347 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15348 val += fixP->fx_offset;
15349 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15350 && !ELF_ST_IS_MICROMIPS (other));
15351 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15352 != ELF_ST_IS_COMPRESSED (other));
15353 }
15354
15355 /* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15356 to a symbol whose annotation indicates another ISA mode. For absolute
15357 symbols check the ISA bit instead.
15358
15359 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15360 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15361 MIPS symbols and associated with BAL instructions as these instructions
15362 may be converted to JALX by the linker. */
15363
15364 static bfd_boolean
15365 fix_bad_cross_mode_branch_p (fixS *fixP)
15366 {
15367 bfd_boolean absolute_p;
15368 unsigned long opcode;
15369 asection *symsec;
15370 valueT val;
15371 int other;
15372 char *buf;
15373
15374 if (mips_ignore_branch_isa)
15375 return FALSE;
15376
15377 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15378 return FALSE;
15379
15380 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15381 absolute_p = bfd_is_abs_section (symsec);
15382
15383 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15384 other = S_GET_OTHER (fixP->fx_addsy);
15385
15386 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15387 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15388 switch (fixP->fx_r_type)
15389 {
15390 case BFD_RELOC_16_PCREL_S2:
15391 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15392 && opcode != 0x0411);
15393 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15394 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15395 && opcode != 0x4060);
15396 case BFD_RELOC_MIPS_21_PCREL_S2:
15397 case BFD_RELOC_MIPS_26_PCREL_S2:
15398 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15399 case BFD_RELOC_MIPS16_16_PCREL_S1:
15400 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15401 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15402 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15403 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15404 default:
15405 abort ();
15406 }
15407 }
15408
15409 /* Return TRUE if the symbol plus addend associated with a regular MIPS
15410 branch instruction pointed to by FIXP is not aligned according to the
15411 branch instruction's immediate field requirement. We need the addend
15412 to preserve the ISA bit and also the sum must not have bit 2 set. We
15413 must explicitly OR in the ISA bit from symbol annotation as the bit
15414 won't be set in the symbol's value then. */
15415
15416 static bfd_boolean
15417 fix_bad_misaligned_branch_p (fixS *fixP)
15418 {
15419 bfd_boolean absolute_p;
15420 asection *symsec;
15421 valueT isa_bit;
15422 valueT val;
15423 valueT off;
15424 int other;
15425
15426 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15427 return FALSE;
15428
15429 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15430 absolute_p = bfd_is_abs_section (symsec);
15431
15432 val = S_GET_VALUE (fixP->fx_addsy);
15433 other = S_GET_OTHER (fixP->fx_addsy);
15434 off = fixP->fx_offset;
15435
15436 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15437 val |= ELF_ST_IS_COMPRESSED (other);
15438 val += off;
15439 return (val & 0x3) != isa_bit;
15440 }
15441
15442 /* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15443 and its calculated value VAL. */
15444
15445 static void
15446 fix_validate_branch (fixS *fixP, valueT val)
15447 {
15448 if (fixP->fx_done && (val & 0x3) != 0)
15449 as_bad_where (fixP->fx_file, fixP->fx_line,
15450 _("branch to misaligned address (0x%lx)"),
15451 (long) (val + md_pcrel_from (fixP)));
15452 else if (fix_bad_cross_mode_branch_p (fixP))
15453 as_bad_where (fixP->fx_file, fixP->fx_line,
15454 _("branch to a symbol in another ISA mode"));
15455 else if (fix_bad_misaligned_branch_p (fixP))
15456 as_bad_where (fixP->fx_file, fixP->fx_line,
15457 _("branch to misaligned address (0x%lx)"),
15458 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15459 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15460 as_bad_where (fixP->fx_file, fixP->fx_line,
15461 _("cannot encode misaligned addend "
15462 "in the relocatable field (0x%lx)"),
15463 (long) fixP->fx_offset);
15464 }
15465
15466 /* Apply a fixup to the object file. */
15467
15468 void
15469 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15470 {
15471 char *buf;
15472 unsigned long insn;
15473 reloc_howto_type *howto;
15474
15475 if (fixP->fx_pcrel)
15476 switch (fixP->fx_r_type)
15477 {
15478 case BFD_RELOC_16_PCREL_S2:
15479 case BFD_RELOC_MIPS16_16_PCREL_S1:
15480 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15481 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15482 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15483 case BFD_RELOC_32_PCREL:
15484 case BFD_RELOC_MIPS_21_PCREL_S2:
15485 case BFD_RELOC_MIPS_26_PCREL_S2:
15486 case BFD_RELOC_MIPS_18_PCREL_S3:
15487 case BFD_RELOC_MIPS_19_PCREL_S2:
15488 case BFD_RELOC_HI16_S_PCREL:
15489 case BFD_RELOC_LO16_PCREL:
15490 break;
15491
15492 case BFD_RELOC_32:
15493 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15494 break;
15495
15496 default:
15497 as_bad_where (fixP->fx_file, fixP->fx_line,
15498 _("PC-relative reference to a different section"));
15499 break;
15500 }
15501
15502 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15503 that have no MIPS ELF equivalent. */
15504 if (fixP->fx_r_type != BFD_RELOC_8)
15505 {
15506 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15507 if (!howto)
15508 return;
15509 }
15510
15511 gas_assert (fixP->fx_size == 2
15512 || fixP->fx_size == 4
15513 || fixP->fx_r_type == BFD_RELOC_8
15514 || fixP->fx_r_type == BFD_RELOC_16
15515 || fixP->fx_r_type == BFD_RELOC_64
15516 || fixP->fx_r_type == BFD_RELOC_CTOR
15517 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15518 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15519 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15520 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15521 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15522 || fixP->fx_r_type == BFD_RELOC_NONE);
15523
15524 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15525
15526 /* Don't treat parts of a composite relocation as done. There are two
15527 reasons for this:
15528
15529 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15530 should nevertheless be emitted if the first part is.
15531
15532 (2) In normal usage, composite relocations are never assembly-time
15533 constants. The easiest way of dealing with the pathological
15534 exceptions is to generate a relocation against STN_UNDEF and
15535 leave everything up to the linker. */
15536 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15537 fixP->fx_done = 1;
15538
15539 switch (fixP->fx_r_type)
15540 {
15541 case BFD_RELOC_MIPS_TLS_GD:
15542 case BFD_RELOC_MIPS_TLS_LDM:
15543 case BFD_RELOC_MIPS_TLS_DTPREL32:
15544 case BFD_RELOC_MIPS_TLS_DTPREL64:
15545 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15546 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15547 case BFD_RELOC_MIPS_TLS_GOTTPREL:
15548 case BFD_RELOC_MIPS_TLS_TPREL32:
15549 case BFD_RELOC_MIPS_TLS_TPREL64:
15550 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15551 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15552 case BFD_RELOC_MICROMIPS_TLS_GD:
15553 case BFD_RELOC_MICROMIPS_TLS_LDM:
15554 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15555 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15556 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15557 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15558 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15559 case BFD_RELOC_MIPS16_TLS_GD:
15560 case BFD_RELOC_MIPS16_TLS_LDM:
15561 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15562 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15563 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15564 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15565 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15566 if (fixP->fx_addsy)
15567 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15568 else
15569 as_bad_where (fixP->fx_file, fixP->fx_line,
15570 _("TLS relocation against a constant"));
15571 break;
15572
15573 case BFD_RELOC_MIPS_JMP:
15574 case BFD_RELOC_MIPS16_JMP:
15575 case BFD_RELOC_MICROMIPS_JMP:
15576 {
15577 int shift;
15578
15579 gas_assert (!fixP->fx_done);
15580
15581 /* Shift is 2, unusually, for microMIPS JALX. */
15582 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15583 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15584 shift = 1;
15585 else
15586 shift = 2;
15587
15588 if (fix_bad_cross_mode_jump_p (fixP))
15589 as_bad_where (fixP->fx_file, fixP->fx_line,
15590 _("jump to a symbol in another ISA mode"));
15591 else if (fix_bad_same_mode_jalx_p (fixP))
15592 as_bad_where (fixP->fx_file, fixP->fx_line,
15593 _("JALX to a symbol in the same ISA mode"));
15594 else if (fix_bad_misaligned_jump_p (fixP, shift))
15595 as_bad_where (fixP->fx_file, fixP->fx_line,
15596 _("jump to misaligned address (0x%lx)"),
15597 (long) (S_GET_VALUE (fixP->fx_addsy)
15598 + fixP->fx_offset));
15599 else if (HAVE_IN_PLACE_ADDENDS
15600 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15601 as_bad_where (fixP->fx_file, fixP->fx_line,
15602 _("cannot encode misaligned addend "
15603 "in the relocatable field (0x%lx)"),
15604 (long) fixP->fx_offset);
15605 }
15606 /* Fall through. */
15607
15608 case BFD_RELOC_MIPS_SHIFT5:
15609 case BFD_RELOC_MIPS_SHIFT6:
15610 case BFD_RELOC_MIPS_GOT_DISP:
15611 case BFD_RELOC_MIPS_GOT_PAGE:
15612 case BFD_RELOC_MIPS_GOT_OFST:
15613 case BFD_RELOC_MIPS_SUB:
15614 case BFD_RELOC_MIPS_INSERT_A:
15615 case BFD_RELOC_MIPS_INSERT_B:
15616 case BFD_RELOC_MIPS_DELETE:
15617 case BFD_RELOC_MIPS_HIGHEST:
15618 case BFD_RELOC_MIPS_HIGHER:
15619 case BFD_RELOC_MIPS_SCN_DISP:
15620 case BFD_RELOC_MIPS_REL16:
15621 case BFD_RELOC_MIPS_RELGOT:
15622 case BFD_RELOC_MIPS_JALR:
15623 case BFD_RELOC_HI16:
15624 case BFD_RELOC_HI16_S:
15625 case BFD_RELOC_LO16:
15626 case BFD_RELOC_GPREL16:
15627 case BFD_RELOC_MIPS_LITERAL:
15628 case BFD_RELOC_MIPS_CALL16:
15629 case BFD_RELOC_MIPS_GOT16:
15630 case BFD_RELOC_GPREL32:
15631 case BFD_RELOC_MIPS_GOT_HI16:
15632 case BFD_RELOC_MIPS_GOT_LO16:
15633 case BFD_RELOC_MIPS_CALL_HI16:
15634 case BFD_RELOC_MIPS_CALL_LO16:
15635 case BFD_RELOC_HI16_S_PCREL:
15636 case BFD_RELOC_LO16_PCREL:
15637 case BFD_RELOC_MIPS16_GPREL:
15638 case BFD_RELOC_MIPS16_GOT16:
15639 case BFD_RELOC_MIPS16_CALL16:
15640 case BFD_RELOC_MIPS16_HI16:
15641 case BFD_RELOC_MIPS16_HI16_S:
15642 case BFD_RELOC_MIPS16_LO16:
15643 case BFD_RELOC_MICROMIPS_GOT_DISP:
15644 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15645 case BFD_RELOC_MICROMIPS_GOT_OFST:
15646 case BFD_RELOC_MICROMIPS_SUB:
15647 case BFD_RELOC_MICROMIPS_HIGHEST:
15648 case BFD_RELOC_MICROMIPS_HIGHER:
15649 case BFD_RELOC_MICROMIPS_SCN_DISP:
15650 case BFD_RELOC_MICROMIPS_JALR:
15651 case BFD_RELOC_MICROMIPS_HI16:
15652 case BFD_RELOC_MICROMIPS_HI16_S:
15653 case BFD_RELOC_MICROMIPS_LO16:
15654 case BFD_RELOC_MICROMIPS_GPREL16:
15655 case BFD_RELOC_MICROMIPS_LITERAL:
15656 case BFD_RELOC_MICROMIPS_CALL16:
15657 case BFD_RELOC_MICROMIPS_GOT16:
15658 case BFD_RELOC_MICROMIPS_GOT_HI16:
15659 case BFD_RELOC_MICROMIPS_GOT_LO16:
15660 case BFD_RELOC_MICROMIPS_CALL_HI16:
15661 case BFD_RELOC_MICROMIPS_CALL_LO16:
15662 case BFD_RELOC_MIPS_EH:
15663 if (fixP->fx_done)
15664 {
15665 offsetT value;
15666
15667 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15668 {
15669 insn = read_reloc_insn (buf, fixP->fx_r_type);
15670 if (mips16_reloc_p (fixP->fx_r_type))
15671 insn |= mips16_immed_extend (value, 16);
15672 else
15673 insn |= (value & 0xffff);
15674 write_reloc_insn (buf, fixP->fx_r_type, insn);
15675 }
15676 else
15677 as_bad_where (fixP->fx_file, fixP->fx_line,
15678 _("unsupported constant in relocation"));
15679 }
15680 break;
15681
15682 case BFD_RELOC_64:
15683 /* This is handled like BFD_RELOC_32, but we output a sign
15684 extended value if we are only 32 bits. */
15685 if (fixP->fx_done)
15686 {
15687 if (8 <= sizeof (valueT))
15688 md_number_to_chars (buf, *valP, 8);
15689 else
15690 {
15691 valueT hiv;
15692
15693 if ((*valP & 0x80000000) != 0)
15694 hiv = 0xffffffff;
15695 else
15696 hiv = 0;
15697 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15698 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
15699 }
15700 }
15701 break;
15702
15703 case BFD_RELOC_RVA:
15704 case BFD_RELOC_32:
15705 case BFD_RELOC_32_PCREL:
15706 case BFD_RELOC_16:
15707 case BFD_RELOC_8:
15708 /* If we are deleting this reloc entry, we must fill in the
15709 value now. This can happen if we have a .word which is not
15710 resolved when it appears but is later defined. */
15711 if (fixP->fx_done)
15712 md_number_to_chars (buf, *valP, fixP->fx_size);
15713 break;
15714
15715 case BFD_RELOC_MIPS_21_PCREL_S2:
15716 fix_validate_branch (fixP, *valP);
15717 if (!fixP->fx_done)
15718 break;
15719
15720 if (*valP + 0x400000 <= 0x7fffff)
15721 {
15722 insn = read_insn (buf);
15723 insn |= (*valP >> 2) & 0x1fffff;
15724 write_insn (buf, insn);
15725 }
15726 else
15727 as_bad_where (fixP->fx_file, fixP->fx_line,
15728 _("branch out of range"));
15729 break;
15730
15731 case BFD_RELOC_MIPS_26_PCREL_S2:
15732 fix_validate_branch (fixP, *valP);
15733 if (!fixP->fx_done)
15734 break;
15735
15736 if (*valP + 0x8000000 <= 0xfffffff)
15737 {
15738 insn = read_insn (buf);
15739 insn |= (*valP >> 2) & 0x3ffffff;
15740 write_insn (buf, insn);
15741 }
15742 else
15743 as_bad_where (fixP->fx_file, fixP->fx_line,
15744 _("branch out of range"));
15745 break;
15746
15747 case BFD_RELOC_MIPS_18_PCREL_S3:
15748 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
15749 as_bad_where (fixP->fx_file, fixP->fx_line,
15750 _("PC-relative access using misaligned symbol (%lx)"),
15751 (long) S_GET_VALUE (fixP->fx_addsy));
15752 if ((fixP->fx_offset & 0x7) != 0)
15753 as_bad_where (fixP->fx_file, fixP->fx_line,
15754 _("PC-relative access using misaligned offset (%lx)"),
15755 (long) fixP->fx_offset);
15756 if (!fixP->fx_done)
15757 break;
15758
15759 if (*valP + 0x100000 <= 0x1fffff)
15760 {
15761 insn = read_insn (buf);
15762 insn |= (*valP >> 3) & 0x3ffff;
15763 write_insn (buf, insn);
15764 }
15765 else
15766 as_bad_where (fixP->fx_file, fixP->fx_line,
15767 _("PC-relative access out of range"));
15768 break;
15769
15770 case BFD_RELOC_MIPS_19_PCREL_S2:
15771 if ((*valP & 0x3) != 0)
15772 as_bad_where (fixP->fx_file, fixP->fx_line,
15773 _("PC-relative access to misaligned address (%lx)"),
15774 (long) *valP);
15775 if (!fixP->fx_done)
15776 break;
15777
15778 if (*valP + 0x100000 <= 0x1fffff)
15779 {
15780 insn = read_insn (buf);
15781 insn |= (*valP >> 2) & 0x7ffff;
15782 write_insn (buf, insn);
15783 }
15784 else
15785 as_bad_where (fixP->fx_file, fixP->fx_line,
15786 _("PC-relative access out of range"));
15787 break;
15788
15789 case BFD_RELOC_16_PCREL_S2:
15790 fix_validate_branch (fixP, *valP);
15791
15792 /* We need to save the bits in the instruction since fixup_segment()
15793 might be deleting the relocation entry (i.e., a branch within
15794 the current segment). */
15795 if (! fixP->fx_done)
15796 break;
15797
15798 /* Update old instruction data. */
15799 insn = read_insn (buf);
15800
15801 if (*valP + 0x20000 <= 0x3ffff)
15802 {
15803 insn |= (*valP >> 2) & 0xffff;
15804 write_insn (buf, insn);
15805 }
15806 else if (fixP->fx_tcbit2
15807 && fixP->fx_done
15808 && fixP->fx_frag->fr_address >= text_section->vma
15809 && (fixP->fx_frag->fr_address
15810 < text_section->vma + bfd_get_section_size (text_section))
15811 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15812 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15813 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15814 {
15815 /* The branch offset is too large. If this is an
15816 unconditional branch, and we are not generating PIC code,
15817 we can convert it to an absolute jump instruction. */
15818 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15819 insn = 0x0c000000; /* jal */
15820 else
15821 insn = 0x08000000; /* j */
15822 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15823 fixP->fx_done = 0;
15824 fixP->fx_addsy = section_symbol (text_section);
15825 *valP += md_pcrel_from (fixP);
15826 write_insn (buf, insn);
15827 }
15828 else
15829 {
15830 /* If we got here, we have branch-relaxation disabled,
15831 and there's nothing we can do to fix this instruction
15832 without turning it into a longer sequence. */
15833 as_bad_where (fixP->fx_file, fixP->fx_line,
15834 _("branch out of range"));
15835 }
15836 break;
15837
15838 case BFD_RELOC_MIPS16_16_PCREL_S1:
15839 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15840 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15841 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15842 gas_assert (!fixP->fx_done);
15843 if (fix_bad_cross_mode_branch_p (fixP))
15844 as_bad_where (fixP->fx_file, fixP->fx_line,
15845 _("branch to a symbol in another ISA mode"));
15846 else if (fixP->fx_addsy
15847 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15848 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15849 && (fixP->fx_offset & 0x1) != 0)
15850 as_bad_where (fixP->fx_file, fixP->fx_line,
15851 _("branch to misaligned address (0x%lx)"),
15852 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15853 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15854 as_bad_where (fixP->fx_file, fixP->fx_line,
15855 _("cannot encode misaligned addend "
15856 "in the relocatable field (0x%lx)"),
15857 (long) fixP->fx_offset);
15858 break;
15859
15860 case BFD_RELOC_VTABLE_INHERIT:
15861 fixP->fx_done = 0;
15862 if (fixP->fx_addsy
15863 && !S_IS_DEFINED (fixP->fx_addsy)
15864 && !S_IS_WEAK (fixP->fx_addsy))
15865 S_SET_WEAK (fixP->fx_addsy);
15866 break;
15867
15868 case BFD_RELOC_NONE:
15869 case BFD_RELOC_VTABLE_ENTRY:
15870 fixP->fx_done = 0;
15871 break;
15872
15873 default:
15874 abort ();
15875 }
15876
15877 /* Remember value for tc_gen_reloc. */
15878 fixP->fx_addnumber = *valP;
15879 }
15880
15881 static symbolS *
15882 get_symbol (void)
15883 {
15884 int c;
15885 char *name;
15886 symbolS *p;
15887
15888 c = get_symbol_name (&name);
15889 p = (symbolS *) symbol_find_or_make (name);
15890 (void) restore_line_pointer (c);
15891 return p;
15892 }
15893
15894 /* Align the current frag to a given power of two. If a particular
15895 fill byte should be used, FILL points to an integer that contains
15896 that byte, otherwise FILL is null.
15897
15898 This function used to have the comment:
15899
15900 The MIPS assembler also automatically adjusts any preceding label.
15901
15902 The implementation therefore applied the adjustment to a maximum of
15903 one label. However, other label adjustments are applied to batches
15904 of labels, and adjusting just one caused problems when new labels
15905 were added for the sake of debugging or unwind information.
15906 We therefore adjust all preceding labels (given as LABELS) instead. */
15907
15908 static void
15909 mips_align (int to, int *fill, struct insn_label_list *labels)
15910 {
15911 mips_emit_delays ();
15912 mips_record_compressed_mode ();
15913 if (fill == NULL && subseg_text_p (now_seg))
15914 frag_align_code (to, 0);
15915 else
15916 frag_align (to, fill ? *fill : 0, 0);
15917 record_alignment (now_seg, to);
15918 mips_move_labels (labels, FALSE);
15919 }
15920
15921 /* Align to a given power of two. .align 0 turns off the automatic
15922 alignment used by the data creating pseudo-ops. */
15923
15924 static void
15925 s_align (int x ATTRIBUTE_UNUSED)
15926 {
15927 int temp, fill_value, *fill_ptr;
15928 long max_alignment = 28;
15929
15930 /* o Note that the assembler pulls down any immediately preceding label
15931 to the aligned address.
15932 o It's not documented but auto alignment is reinstated by
15933 a .align pseudo instruction.
15934 o Note also that after auto alignment is turned off the mips assembler
15935 issues an error on attempt to assemble an improperly aligned data item.
15936 We don't. */
15937
15938 temp = get_absolute_expression ();
15939 if (temp > max_alignment)
15940 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
15941 else if (temp < 0)
15942 {
15943 as_warn (_("alignment negative, 0 assumed"));
15944 temp = 0;
15945 }
15946 if (*input_line_pointer == ',')
15947 {
15948 ++input_line_pointer;
15949 fill_value = get_absolute_expression ();
15950 fill_ptr = &fill_value;
15951 }
15952 else
15953 fill_ptr = 0;
15954 if (temp)
15955 {
15956 segment_info_type *si = seg_info (now_seg);
15957 struct insn_label_list *l = si->label_list;
15958 /* Auto alignment should be switched on by next section change. */
15959 auto_align = 1;
15960 mips_align (temp, fill_ptr, l);
15961 }
15962 else
15963 {
15964 auto_align = 0;
15965 }
15966
15967 demand_empty_rest_of_line ();
15968 }
15969
15970 static void
15971 s_change_sec (int sec)
15972 {
15973 segT seg;
15974
15975 /* The ELF backend needs to know that we are changing sections, so
15976 that .previous works correctly. We could do something like check
15977 for an obj_section_change_hook macro, but that might be confusing
15978 as it would not be appropriate to use it in the section changing
15979 functions in read.c, since obj-elf.c intercepts those. FIXME:
15980 This should be cleaner, somehow. */
15981 obj_elf_section_change_hook ();
15982
15983 mips_emit_delays ();
15984
15985 switch (sec)
15986 {
15987 case 't':
15988 s_text (0);
15989 break;
15990 case 'd':
15991 s_data (0);
15992 break;
15993 case 'b':
15994 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15995 demand_empty_rest_of_line ();
15996 break;
15997
15998 case 'r':
15999 seg = subseg_new (RDATA_SECTION_NAME,
16000 (subsegT) get_absolute_expression ());
16001 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16002 | SEC_READONLY | SEC_RELOC
16003 | SEC_DATA));
16004 if (strncmp (TARGET_OS, "elf", 3) != 0)
16005 record_alignment (seg, 4);
16006 demand_empty_rest_of_line ();
16007 break;
16008
16009 case 's':
16010 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
16011 bfd_set_section_flags (stdoutput, seg,
16012 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16013 if (strncmp (TARGET_OS, "elf", 3) != 0)
16014 record_alignment (seg, 4);
16015 demand_empty_rest_of_line ();
16016 break;
16017
16018 case 'B':
16019 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16020 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16021 if (strncmp (TARGET_OS, "elf", 3) != 0)
16022 record_alignment (seg, 4);
16023 demand_empty_rest_of_line ();
16024 break;
16025 }
16026
16027 auto_align = 1;
16028 }
16029
16030 void
16031 s_change_section (int ignore ATTRIBUTE_UNUSED)
16032 {
16033 char *saved_ilp;
16034 char *section_name;
16035 char c, endc;
16036 char next_c = 0;
16037 int section_type;
16038 int section_flag;
16039 int section_entry_size;
16040 int section_alignment;
16041
16042 saved_ilp = input_line_pointer;
16043 endc = get_symbol_name (&section_name);
16044 c = (endc == '"' ? input_line_pointer[1] : endc);
16045 if (c)
16046 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
16047
16048 /* Do we have .section Name<,"flags">? */
16049 if (c != ',' || (c == ',' && next_c == '"'))
16050 {
16051 /* Just after name is now '\0'. */
16052 (void) restore_line_pointer (endc);
16053 input_line_pointer = saved_ilp;
16054 obj_elf_section (ignore);
16055 return;
16056 }
16057
16058 section_name = xstrdup (section_name);
16059 c = restore_line_pointer (endc);
16060
16061 input_line_pointer++;
16062
16063 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16064 if (c == ',')
16065 section_type = get_absolute_expression ();
16066 else
16067 section_type = 0;
16068
16069 if (*input_line_pointer++ == ',')
16070 section_flag = get_absolute_expression ();
16071 else
16072 section_flag = 0;
16073
16074 if (*input_line_pointer++ == ',')
16075 section_entry_size = get_absolute_expression ();
16076 else
16077 section_entry_size = 0;
16078
16079 if (*input_line_pointer++ == ',')
16080 section_alignment = get_absolute_expression ();
16081 else
16082 section_alignment = 0;
16083
16084 /* FIXME: really ignore? */
16085 (void) section_alignment;
16086
16087 /* When using the generic form of .section (as implemented by obj-elf.c),
16088 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16089 traditionally had to fall back on the more common @progbits instead.
16090
16091 There's nothing really harmful in this, since bfd will correct
16092 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
16093 means that, for backwards compatibility, the special_section entries
16094 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16095
16096 Even so, we shouldn't force users of the MIPS .section syntax to
16097 incorrectly label the sections as SHT_PROGBITS. The best compromise
16098 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16099 generic type-checking code. */
16100 if (section_type == SHT_MIPS_DWARF)
16101 section_type = SHT_PROGBITS;
16102
16103 obj_elf_change_section (section_name, section_type, 0, section_flag,
16104 section_entry_size, 0, 0, 0);
16105
16106 if (now_seg->name != section_name)
16107 free (section_name);
16108 }
16109
16110 void
16111 mips_enable_auto_align (void)
16112 {
16113 auto_align = 1;
16114 }
16115
16116 static void
16117 s_cons (int log_size)
16118 {
16119 segment_info_type *si = seg_info (now_seg);
16120 struct insn_label_list *l = si->label_list;
16121
16122 mips_emit_delays ();
16123 if (log_size > 0 && auto_align)
16124 mips_align (log_size, 0, l);
16125 cons (1 << log_size);
16126 mips_clear_insn_labels ();
16127 }
16128
16129 static void
16130 s_float_cons (int type)
16131 {
16132 segment_info_type *si = seg_info (now_seg);
16133 struct insn_label_list *l = si->label_list;
16134
16135 mips_emit_delays ();
16136
16137 if (auto_align)
16138 {
16139 if (type == 'd')
16140 mips_align (3, 0, l);
16141 else
16142 mips_align (2, 0, l);
16143 }
16144
16145 float_cons (type);
16146 mips_clear_insn_labels ();
16147 }
16148
16149 /* Handle .globl. We need to override it because on Irix 5 you are
16150 permitted to say
16151 .globl foo .text
16152 where foo is an undefined symbol, to mean that foo should be
16153 considered to be the address of a function. */
16154
16155 static void
16156 s_mips_globl (int x ATTRIBUTE_UNUSED)
16157 {
16158 char *name;
16159 int c;
16160 symbolS *symbolP;
16161 flagword flag;
16162
16163 do
16164 {
16165 c = get_symbol_name (&name);
16166 symbolP = symbol_find_or_make (name);
16167 S_SET_EXTERNAL (symbolP);
16168
16169 *input_line_pointer = c;
16170 SKIP_WHITESPACE_AFTER_NAME ();
16171
16172 /* On Irix 5, every global symbol that is not explicitly labelled as
16173 being a function is apparently labelled as being an object. */
16174 flag = BSF_OBJECT;
16175
16176 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16177 && (*input_line_pointer != ','))
16178 {
16179 char *secname;
16180 asection *sec;
16181
16182 c = get_symbol_name (&secname);
16183 sec = bfd_get_section_by_name (stdoutput, secname);
16184 if (sec == NULL)
16185 as_bad (_("%s: no such section"), secname);
16186 (void) restore_line_pointer (c);
16187
16188 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16189 flag = BSF_FUNCTION;
16190 }
16191
16192 symbol_get_bfdsym (symbolP)->flags |= flag;
16193
16194 c = *input_line_pointer;
16195 if (c == ',')
16196 {
16197 input_line_pointer++;
16198 SKIP_WHITESPACE ();
16199 if (is_end_of_line[(unsigned char) *input_line_pointer])
16200 c = '\n';
16201 }
16202 }
16203 while (c == ',');
16204
16205 demand_empty_rest_of_line ();
16206 }
16207
16208 static void
16209 s_option (int x ATTRIBUTE_UNUSED)
16210 {
16211 char *opt;
16212 char c;
16213
16214 c = get_symbol_name (&opt);
16215
16216 if (*opt == 'O')
16217 {
16218 /* FIXME: What does this mean? */
16219 }
16220 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
16221 {
16222 int i;
16223
16224 i = atoi (opt + 3);
16225 if (i != 0 && i != 2)
16226 as_bad (_(".option pic%d not supported"), i);
16227 else if (mips_pic == VXWORKS_PIC)
16228 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16229 else if (i == 0)
16230 mips_pic = NO_PIC;
16231 else if (i == 2)
16232 {
16233 mips_pic = SVR4_PIC;
16234 mips_abicalls = TRUE;
16235 }
16236
16237 if (mips_pic == SVR4_PIC)
16238 {
16239 if (g_switch_seen && g_switch_value != 0)
16240 as_warn (_("-G may not be used with SVR4 PIC code"));
16241 g_switch_value = 0;
16242 bfd_set_gp_size (stdoutput, 0);
16243 }
16244 }
16245 else
16246 as_warn (_("unrecognized option \"%s\""), opt);
16247
16248 (void) restore_line_pointer (c);
16249 demand_empty_rest_of_line ();
16250 }
16251
16252 /* This structure is used to hold a stack of .set values. */
16253
16254 struct mips_option_stack
16255 {
16256 struct mips_option_stack *next;
16257 struct mips_set_options options;
16258 };
16259
16260 static struct mips_option_stack *mips_opts_stack;
16261
16262 /* Return status for .set/.module option handling. */
16263
16264 enum code_option_type
16265 {
16266 /* Unrecognized option. */
16267 OPTION_TYPE_BAD = -1,
16268
16269 /* Ordinary option. */
16270 OPTION_TYPE_NORMAL,
16271
16272 /* ISA changing option. */
16273 OPTION_TYPE_ISA
16274 };
16275
16276 /* Handle common .set/.module options. Return status indicating option
16277 type. */
16278
16279 static enum code_option_type
16280 parse_code_option (char * name)
16281 {
16282 bfd_boolean isa_set = FALSE;
16283 const struct mips_ase *ase;
16284
16285 if (strncmp (name, "at=", 3) == 0)
16286 {
16287 char *s = name + 3;
16288
16289 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16290 as_bad (_("unrecognized register name `%s'"), s);
16291 }
16292 else if (strcmp (name, "at") == 0)
16293 mips_opts.at = ATREG;
16294 else if (strcmp (name, "noat") == 0)
16295 mips_opts.at = ZERO;
16296 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16297 mips_opts.nomove = 0;
16298 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16299 mips_opts.nomove = 1;
16300 else if (strcmp (name, "bopt") == 0)
16301 mips_opts.nobopt = 0;
16302 else if (strcmp (name, "nobopt") == 0)
16303 mips_opts.nobopt = 1;
16304 else if (strcmp (name, "gp=32") == 0)
16305 mips_opts.gp = 32;
16306 else if (strcmp (name, "gp=64") == 0)
16307 mips_opts.gp = 64;
16308 else if (strcmp (name, "fp=32") == 0)
16309 mips_opts.fp = 32;
16310 else if (strcmp (name, "fp=xx") == 0)
16311 mips_opts.fp = 0;
16312 else if (strcmp (name, "fp=64") == 0)
16313 mips_opts.fp = 64;
16314 else if (strcmp (name, "softfloat") == 0)
16315 mips_opts.soft_float = 1;
16316 else if (strcmp (name, "hardfloat") == 0)
16317 mips_opts.soft_float = 0;
16318 else if (strcmp (name, "singlefloat") == 0)
16319 mips_opts.single_float = 1;
16320 else if (strcmp (name, "doublefloat") == 0)
16321 mips_opts.single_float = 0;
16322 else if (strcmp (name, "nooddspreg") == 0)
16323 mips_opts.oddspreg = 0;
16324 else if (strcmp (name, "oddspreg") == 0)
16325 mips_opts.oddspreg = 1;
16326 else if (strcmp (name, "mips16") == 0
16327 || strcmp (name, "MIPS-16") == 0)
16328 mips_opts.mips16 = 1;
16329 else if (strcmp (name, "nomips16") == 0
16330 || strcmp (name, "noMIPS-16") == 0)
16331 mips_opts.mips16 = 0;
16332 else if (strcmp (name, "micromips") == 0)
16333 mips_opts.micromips = 1;
16334 else if (strcmp (name, "nomicromips") == 0)
16335 mips_opts.micromips = 0;
16336 else if (name[0] == 'n'
16337 && name[1] == 'o'
16338 && (ase = mips_lookup_ase (name + 2)))
16339 mips_set_ase (ase, &mips_opts, FALSE);
16340 else if ((ase = mips_lookup_ase (name)))
16341 mips_set_ase (ase, &mips_opts, TRUE);
16342 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16343 {
16344 /* Permit the user to change the ISA and architecture on the fly.
16345 Needless to say, misuse can cause serious problems. */
16346 if (strncmp (name, "arch=", 5) == 0)
16347 {
16348 const struct mips_cpu_info *p;
16349
16350 p = mips_parse_cpu ("internal use", name + 5);
16351 if (!p)
16352 as_bad (_("unknown architecture %s"), name + 5);
16353 else
16354 {
16355 mips_opts.arch = p->cpu;
16356 mips_opts.isa = p->isa;
16357 isa_set = TRUE;
16358 }
16359 }
16360 else if (strncmp (name, "mips", 4) == 0)
16361 {
16362 const struct mips_cpu_info *p;
16363
16364 p = mips_parse_cpu ("internal use", name);
16365 if (!p)
16366 as_bad (_("unknown ISA level %s"), name + 4);
16367 else
16368 {
16369 mips_opts.arch = p->cpu;
16370 mips_opts.isa = p->isa;
16371 isa_set = TRUE;
16372 }
16373 }
16374 else
16375 as_bad (_("unknown ISA or architecture %s"), name);
16376 }
16377 else if (strcmp (name, "autoextend") == 0)
16378 mips_opts.noautoextend = 0;
16379 else if (strcmp (name, "noautoextend") == 0)
16380 mips_opts.noautoextend = 1;
16381 else if (strcmp (name, "insn32") == 0)
16382 mips_opts.insn32 = TRUE;
16383 else if (strcmp (name, "noinsn32") == 0)
16384 mips_opts.insn32 = FALSE;
16385 else if (strcmp (name, "sym32") == 0)
16386 mips_opts.sym32 = TRUE;
16387 else if (strcmp (name, "nosym32") == 0)
16388 mips_opts.sym32 = FALSE;
16389 else
16390 return OPTION_TYPE_BAD;
16391
16392 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
16393 }
16394
16395 /* Handle the .set pseudo-op. */
16396
16397 static void
16398 s_mipsset (int x ATTRIBUTE_UNUSED)
16399 {
16400 enum code_option_type type = OPTION_TYPE_NORMAL;
16401 char *name = input_line_pointer, ch;
16402
16403 file_mips_check_options ();
16404
16405 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16406 ++input_line_pointer;
16407 ch = *input_line_pointer;
16408 *input_line_pointer = '\0';
16409
16410 if (strchr (name, ','))
16411 {
16412 /* Generic ".set" directive; use the generic handler. */
16413 *input_line_pointer = ch;
16414 input_line_pointer = name;
16415 s_set (0);
16416 return;
16417 }
16418
16419 if (strcmp (name, "reorder") == 0)
16420 {
16421 if (mips_opts.noreorder)
16422 end_noreorder ();
16423 }
16424 else if (strcmp (name, "noreorder") == 0)
16425 {
16426 if (!mips_opts.noreorder)
16427 start_noreorder ();
16428 }
16429 else if (strcmp (name, "macro") == 0)
16430 mips_opts.warn_about_macros = 0;
16431 else if (strcmp (name, "nomacro") == 0)
16432 {
16433 if (mips_opts.noreorder == 0)
16434 as_bad (_("`noreorder' must be set before `nomacro'"));
16435 mips_opts.warn_about_macros = 1;
16436 }
16437 else if (strcmp (name, "gp=default") == 0)
16438 mips_opts.gp = file_mips_opts.gp;
16439 else if (strcmp (name, "fp=default") == 0)
16440 mips_opts.fp = file_mips_opts.fp;
16441 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16442 {
16443 mips_opts.isa = file_mips_opts.isa;
16444 mips_opts.arch = file_mips_opts.arch;
16445 mips_opts.gp = file_mips_opts.gp;
16446 mips_opts.fp = file_mips_opts.fp;
16447 }
16448 else if (strcmp (name, "push") == 0)
16449 {
16450 struct mips_option_stack *s;
16451
16452 s = XNEW (struct mips_option_stack);
16453 s->next = mips_opts_stack;
16454 s->options = mips_opts;
16455 mips_opts_stack = s;
16456 }
16457 else if (strcmp (name, "pop") == 0)
16458 {
16459 struct mips_option_stack *s;
16460
16461 s = mips_opts_stack;
16462 if (s == NULL)
16463 as_bad (_(".set pop with no .set push"));
16464 else
16465 {
16466 /* If we're changing the reorder mode we need to handle
16467 delay slots correctly. */
16468 if (s->options.noreorder && ! mips_opts.noreorder)
16469 start_noreorder ();
16470 else if (! s->options.noreorder && mips_opts.noreorder)
16471 end_noreorder ();
16472
16473 mips_opts = s->options;
16474 mips_opts_stack = s->next;
16475 free (s);
16476 }
16477 }
16478 else
16479 {
16480 type = parse_code_option (name);
16481 if (type == OPTION_TYPE_BAD)
16482 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16483 }
16484
16485 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16486 registers based on what is supported by the arch/cpu. */
16487 if (type == OPTION_TYPE_ISA)
16488 {
16489 switch (mips_opts.isa)
16490 {
16491 case 0:
16492 break;
16493 case ISA_MIPS1:
16494 /* MIPS I cannot support FPXX. */
16495 mips_opts.fp = 32;
16496 /* fall-through. */
16497 case ISA_MIPS2:
16498 case ISA_MIPS32:
16499 case ISA_MIPS32R2:
16500 case ISA_MIPS32R3:
16501 case ISA_MIPS32R5:
16502 mips_opts.gp = 32;
16503 if (mips_opts.fp != 0)
16504 mips_opts.fp = 32;
16505 break;
16506 case ISA_MIPS32R6:
16507 mips_opts.gp = 32;
16508 mips_opts.fp = 64;
16509 break;
16510 case ISA_MIPS3:
16511 case ISA_MIPS4:
16512 case ISA_MIPS5:
16513 case ISA_MIPS64:
16514 case ISA_MIPS64R2:
16515 case ISA_MIPS64R3:
16516 case ISA_MIPS64R5:
16517 case ISA_MIPS64R6:
16518 mips_opts.gp = 64;
16519 if (mips_opts.fp != 0)
16520 {
16521 if (mips_opts.arch == CPU_R5900)
16522 mips_opts.fp = 32;
16523 else
16524 mips_opts.fp = 64;
16525 }
16526 break;
16527 default:
16528 as_bad (_("unknown ISA level %s"), name + 4);
16529 break;
16530 }
16531 }
16532
16533 mips_check_options (&mips_opts, FALSE);
16534
16535 mips_check_isa_supports_ases ();
16536 *input_line_pointer = ch;
16537 demand_empty_rest_of_line ();
16538 }
16539
16540 /* Handle the .module pseudo-op. */
16541
16542 static void
16543 s_module (int ignore ATTRIBUTE_UNUSED)
16544 {
16545 char *name = input_line_pointer, ch;
16546
16547 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16548 ++input_line_pointer;
16549 ch = *input_line_pointer;
16550 *input_line_pointer = '\0';
16551
16552 if (!file_mips_opts_checked)
16553 {
16554 if (parse_code_option (name) == OPTION_TYPE_BAD)
16555 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16556
16557 /* Update module level settings from mips_opts. */
16558 file_mips_opts = mips_opts;
16559 }
16560 else
16561 as_bad (_(".module is not permitted after generating code"));
16562
16563 *input_line_pointer = ch;
16564 demand_empty_rest_of_line ();
16565 }
16566
16567 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
16568 .option pic2. It means to generate SVR4 PIC calls. */
16569
16570 static void
16571 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16572 {
16573 mips_pic = SVR4_PIC;
16574 mips_abicalls = TRUE;
16575
16576 if (g_switch_seen && g_switch_value != 0)
16577 as_warn (_("-G may not be used with SVR4 PIC code"));
16578 g_switch_value = 0;
16579
16580 bfd_set_gp_size (stdoutput, 0);
16581 demand_empty_rest_of_line ();
16582 }
16583
16584 /* Handle the .cpload pseudo-op. This is used when generating SVR4
16585 PIC code. It sets the $gp register for the function based on the
16586 function address, which is in the register named in the argument.
16587 This uses a relocation against _gp_disp, which is handled specially
16588 by the linker. The result is:
16589 lui $gp,%hi(_gp_disp)
16590 addiu $gp,$gp,%lo(_gp_disp)
16591 addu $gp,$gp,.cpload argument
16592 The .cpload argument is normally $25 == $t9.
16593
16594 The -mno-shared option changes this to:
16595 lui $gp,%hi(__gnu_local_gp)
16596 addiu $gp,$gp,%lo(__gnu_local_gp)
16597 and the argument is ignored. This saves an instruction, but the
16598 resulting code is not position independent; it uses an absolute
16599 address for __gnu_local_gp. Thus code assembled with -mno-shared
16600 can go into an ordinary executable, but not into a shared library. */
16601
16602 static void
16603 s_cpload (int ignore ATTRIBUTE_UNUSED)
16604 {
16605 expressionS ex;
16606 int reg;
16607 int in_shared;
16608
16609 file_mips_check_options ();
16610
16611 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16612 .cpload is ignored. */
16613 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16614 {
16615 s_ignore (0);
16616 return;
16617 }
16618
16619 if (mips_opts.mips16)
16620 {
16621 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16622 ignore_rest_of_line ();
16623 return;
16624 }
16625
16626 /* .cpload should be in a .set noreorder section. */
16627 if (mips_opts.noreorder == 0)
16628 as_warn (_(".cpload not in noreorder section"));
16629
16630 reg = tc_get_register (0);
16631
16632 /* If we need to produce a 64-bit address, we are better off using
16633 the default instruction sequence. */
16634 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16635
16636 ex.X_op = O_symbol;
16637 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16638 "__gnu_local_gp");
16639 ex.X_op_symbol = NULL;
16640 ex.X_add_number = 0;
16641
16642 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16643 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16644
16645 mips_mark_labels ();
16646 mips_assembling_insn = TRUE;
16647
16648 macro_start ();
16649 macro_build_lui (&ex, mips_gp_register);
16650 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16651 mips_gp_register, BFD_RELOC_LO16);
16652 if (in_shared)
16653 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16654 mips_gp_register, reg);
16655 macro_end ();
16656
16657 mips_assembling_insn = FALSE;
16658 demand_empty_rest_of_line ();
16659 }
16660
16661 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16662 .cpsetup $reg1, offset|$reg2, label
16663
16664 If offset is given, this results in:
16665 sd $gp, offset($sp)
16666 lui $gp, %hi(%neg(%gp_rel(label)))
16667 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16668 daddu $gp, $gp, $reg1
16669
16670 If $reg2 is given, this results in:
16671 or $reg2, $gp, $0
16672 lui $gp, %hi(%neg(%gp_rel(label)))
16673 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16674 daddu $gp, $gp, $reg1
16675 $reg1 is normally $25 == $t9.
16676
16677 The -mno-shared option replaces the last three instructions with
16678 lui $gp,%hi(_gp)
16679 addiu $gp,$gp,%lo(_gp) */
16680
16681 static void
16682 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16683 {
16684 expressionS ex_off;
16685 expressionS ex_sym;
16686 int reg1;
16687
16688 file_mips_check_options ();
16689
16690 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16691 We also need NewABI support. */
16692 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16693 {
16694 s_ignore (0);
16695 return;
16696 }
16697
16698 if (mips_opts.mips16)
16699 {
16700 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16701 ignore_rest_of_line ();
16702 return;
16703 }
16704
16705 reg1 = tc_get_register (0);
16706 SKIP_WHITESPACE ();
16707 if (*input_line_pointer != ',')
16708 {
16709 as_bad (_("missing argument separator ',' for .cpsetup"));
16710 return;
16711 }
16712 else
16713 ++input_line_pointer;
16714 SKIP_WHITESPACE ();
16715 if (*input_line_pointer == '$')
16716 {
16717 mips_cpreturn_register = tc_get_register (0);
16718 mips_cpreturn_offset = -1;
16719 }
16720 else
16721 {
16722 mips_cpreturn_offset = get_absolute_expression ();
16723 mips_cpreturn_register = -1;
16724 }
16725 SKIP_WHITESPACE ();
16726 if (*input_line_pointer != ',')
16727 {
16728 as_bad (_("missing argument separator ',' for .cpsetup"));
16729 return;
16730 }
16731 else
16732 ++input_line_pointer;
16733 SKIP_WHITESPACE ();
16734 expression (&ex_sym);
16735
16736 mips_mark_labels ();
16737 mips_assembling_insn = TRUE;
16738
16739 macro_start ();
16740 if (mips_cpreturn_register == -1)
16741 {
16742 ex_off.X_op = O_constant;
16743 ex_off.X_add_symbol = NULL;
16744 ex_off.X_op_symbol = NULL;
16745 ex_off.X_add_number = mips_cpreturn_offset;
16746
16747 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16748 BFD_RELOC_LO16, SP);
16749 }
16750 else
16751 move_register (mips_cpreturn_register, mips_gp_register);
16752
16753 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16754 {
16755 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16756 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16757 BFD_RELOC_HI16_S);
16758
16759 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16760 mips_gp_register, -1, BFD_RELOC_GPREL16,
16761 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16762
16763 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16764 mips_gp_register, reg1);
16765 }
16766 else
16767 {
16768 expressionS ex;
16769
16770 ex.X_op = O_symbol;
16771 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16772 ex.X_op_symbol = NULL;
16773 ex.X_add_number = 0;
16774
16775 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16776 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16777
16778 macro_build_lui (&ex, mips_gp_register);
16779 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16780 mips_gp_register, BFD_RELOC_LO16);
16781 }
16782
16783 macro_end ();
16784
16785 mips_assembling_insn = FALSE;
16786 demand_empty_rest_of_line ();
16787 }
16788
16789 static void
16790 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16791 {
16792 file_mips_check_options ();
16793
16794 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16795 .cplocal is ignored. */
16796 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16797 {
16798 s_ignore (0);
16799 return;
16800 }
16801
16802 if (mips_opts.mips16)
16803 {
16804 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16805 ignore_rest_of_line ();
16806 return;
16807 }
16808
16809 mips_gp_register = tc_get_register (0);
16810 demand_empty_rest_of_line ();
16811 }
16812
16813 /* Handle the .cprestore pseudo-op. This stores $gp into a given
16814 offset from $sp. The offset is remembered, and after making a PIC
16815 call $gp is restored from that location. */
16816
16817 static void
16818 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16819 {
16820 expressionS ex;
16821
16822 file_mips_check_options ();
16823
16824 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16825 .cprestore is ignored. */
16826 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16827 {
16828 s_ignore (0);
16829 return;
16830 }
16831
16832 if (mips_opts.mips16)
16833 {
16834 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16835 ignore_rest_of_line ();
16836 return;
16837 }
16838
16839 mips_cprestore_offset = get_absolute_expression ();
16840 mips_cprestore_valid = 1;
16841
16842 ex.X_op = O_constant;
16843 ex.X_add_symbol = NULL;
16844 ex.X_op_symbol = NULL;
16845 ex.X_add_number = mips_cprestore_offset;
16846
16847 mips_mark_labels ();
16848 mips_assembling_insn = TRUE;
16849
16850 macro_start ();
16851 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16852 SP, HAVE_64BIT_ADDRESSES);
16853 macro_end ();
16854
16855 mips_assembling_insn = FALSE;
16856 demand_empty_rest_of_line ();
16857 }
16858
16859 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16860 was given in the preceding .cpsetup, it results in:
16861 ld $gp, offset($sp)
16862
16863 If a register $reg2 was given there, it results in:
16864 or $gp, $reg2, $0 */
16865
16866 static void
16867 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16868 {
16869 expressionS ex;
16870
16871 file_mips_check_options ();
16872
16873 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16874 We also need NewABI support. */
16875 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16876 {
16877 s_ignore (0);
16878 return;
16879 }
16880
16881 if (mips_opts.mips16)
16882 {
16883 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16884 ignore_rest_of_line ();
16885 return;
16886 }
16887
16888 mips_mark_labels ();
16889 mips_assembling_insn = TRUE;
16890
16891 macro_start ();
16892 if (mips_cpreturn_register == -1)
16893 {
16894 ex.X_op = O_constant;
16895 ex.X_add_symbol = NULL;
16896 ex.X_op_symbol = NULL;
16897 ex.X_add_number = mips_cpreturn_offset;
16898
16899 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16900 }
16901 else
16902 move_register (mips_gp_register, mips_cpreturn_register);
16903
16904 macro_end ();
16905
16906 mips_assembling_insn = FALSE;
16907 demand_empty_rest_of_line ();
16908 }
16909
16910 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16911 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16912 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16913 debug information or MIPS16 TLS. */
16914
16915 static void
16916 s_tls_rel_directive (const size_t bytes, const char *dirstr,
16917 bfd_reloc_code_real_type rtype)
16918 {
16919 expressionS ex;
16920 char *p;
16921
16922 expression (&ex);
16923
16924 if (ex.X_op != O_symbol)
16925 {
16926 as_bad (_("unsupported use of %s"), dirstr);
16927 ignore_rest_of_line ();
16928 }
16929
16930 p = frag_more (bytes);
16931 md_number_to_chars (p, 0, bytes);
16932 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
16933 demand_empty_rest_of_line ();
16934 mips_clear_insn_labels ();
16935 }
16936
16937 /* Handle .dtprelword. */
16938
16939 static void
16940 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16941 {
16942 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
16943 }
16944
16945 /* Handle .dtpreldword. */
16946
16947 static void
16948 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16949 {
16950 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16951 }
16952
16953 /* Handle .tprelword. */
16954
16955 static void
16956 s_tprelword (int ignore ATTRIBUTE_UNUSED)
16957 {
16958 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16959 }
16960
16961 /* Handle .tpreldword. */
16962
16963 static void
16964 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16965 {
16966 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
16967 }
16968
16969 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16970 code. It sets the offset to use in gp_rel relocations. */
16971
16972 static void
16973 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
16974 {
16975 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16976 We also need NewABI support. */
16977 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16978 {
16979 s_ignore (0);
16980 return;
16981 }
16982
16983 mips_gprel_offset = get_absolute_expression ();
16984
16985 demand_empty_rest_of_line ();
16986 }
16987
16988 /* Handle the .gpword pseudo-op. This is used when generating PIC
16989 code. It generates a 32 bit GP relative reloc. */
16990
16991 static void
16992 s_gpword (int ignore ATTRIBUTE_UNUSED)
16993 {
16994 segment_info_type *si;
16995 struct insn_label_list *l;
16996 expressionS ex;
16997 char *p;
16998
16999 /* When not generating PIC code, this is treated as .word. */
17000 if (mips_pic != SVR4_PIC)
17001 {
17002 s_cons (2);
17003 return;
17004 }
17005
17006 si = seg_info (now_seg);
17007 l = si->label_list;
17008 mips_emit_delays ();
17009 if (auto_align)
17010 mips_align (2, 0, l);
17011
17012 expression (&ex);
17013 mips_clear_insn_labels ();
17014
17015 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17016 {
17017 as_bad (_("unsupported use of .gpword"));
17018 ignore_rest_of_line ();
17019 }
17020
17021 p = frag_more (4);
17022 md_number_to_chars (p, 0, 4);
17023 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17024 BFD_RELOC_GPREL32);
17025
17026 demand_empty_rest_of_line ();
17027 }
17028
17029 static void
17030 s_gpdword (int ignore ATTRIBUTE_UNUSED)
17031 {
17032 segment_info_type *si;
17033 struct insn_label_list *l;
17034 expressionS ex;
17035 char *p;
17036
17037 /* When not generating PIC code, this is treated as .dword. */
17038 if (mips_pic != SVR4_PIC)
17039 {
17040 s_cons (3);
17041 return;
17042 }
17043
17044 si = seg_info (now_seg);
17045 l = si->label_list;
17046 mips_emit_delays ();
17047 if (auto_align)
17048 mips_align (3, 0, l);
17049
17050 expression (&ex);
17051 mips_clear_insn_labels ();
17052
17053 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17054 {
17055 as_bad (_("unsupported use of .gpdword"));
17056 ignore_rest_of_line ();
17057 }
17058
17059 p = frag_more (8);
17060 md_number_to_chars (p, 0, 8);
17061 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17062 BFD_RELOC_GPREL32)->fx_tcbit = 1;
17063
17064 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
17065 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17066 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
17067
17068 demand_empty_rest_of_line ();
17069 }
17070
17071 /* Handle the .ehword pseudo-op. This is used when generating unwinding
17072 tables. It generates a R_MIPS_EH reloc. */
17073
17074 static void
17075 s_ehword (int ignore ATTRIBUTE_UNUSED)
17076 {
17077 expressionS ex;
17078 char *p;
17079
17080 mips_emit_delays ();
17081
17082 expression (&ex);
17083 mips_clear_insn_labels ();
17084
17085 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17086 {
17087 as_bad (_("unsupported use of .ehword"));
17088 ignore_rest_of_line ();
17089 }
17090
17091 p = frag_more (4);
17092 md_number_to_chars (p, 0, 4);
17093 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17094 BFD_RELOC_32_PCREL);
17095
17096 demand_empty_rest_of_line ();
17097 }
17098
17099 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
17100 tables in SVR4 PIC code. */
17101
17102 static void
17103 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17104 {
17105 int reg;
17106
17107 file_mips_check_options ();
17108
17109 /* This is ignored when not generating SVR4 PIC code. */
17110 if (mips_pic != SVR4_PIC)
17111 {
17112 s_ignore (0);
17113 return;
17114 }
17115
17116 mips_mark_labels ();
17117 mips_assembling_insn = TRUE;
17118
17119 /* Add $gp to the register named as an argument. */
17120 macro_start ();
17121 reg = tc_get_register (0);
17122 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17123 macro_end ();
17124
17125 mips_assembling_insn = FALSE;
17126 demand_empty_rest_of_line ();
17127 }
17128
17129 /* Handle the .insn pseudo-op. This marks instruction labels in
17130 mips16/micromips mode. This permits the linker to handle them specially,
17131 such as generating jalx instructions when needed. We also make
17132 them odd for the duration of the assembly, in order to generate the
17133 right sort of code. We will make them even in the adjust_symtab
17134 routine, while leaving them marked. This is convenient for the
17135 debugger and the disassembler. The linker knows to make them odd
17136 again. */
17137
17138 static void
17139 s_insn (int ignore ATTRIBUTE_UNUSED)
17140 {
17141 file_mips_check_options ();
17142 file_ase_mips16 |= mips_opts.mips16;
17143 file_ase_micromips |= mips_opts.micromips;
17144
17145 mips_mark_labels ();
17146
17147 demand_empty_rest_of_line ();
17148 }
17149
17150 /* Handle the .nan pseudo-op. */
17151
17152 static void
17153 s_nan (int ignore ATTRIBUTE_UNUSED)
17154 {
17155 static const char str_legacy[] = "legacy";
17156 static const char str_2008[] = "2008";
17157 size_t i;
17158
17159 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17160
17161 if (i == sizeof (str_2008) - 1
17162 && memcmp (input_line_pointer, str_2008, i) == 0)
17163 mips_nan2008 = 1;
17164 else if (i == sizeof (str_legacy) - 1
17165 && memcmp (input_line_pointer, str_legacy, i) == 0)
17166 {
17167 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17168 mips_nan2008 = 0;
17169 else
17170 as_bad (_("`%s' does not support legacy NaN"),
17171 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17172 }
17173 else
17174 as_bad (_("bad .nan directive"));
17175
17176 input_line_pointer += i;
17177 demand_empty_rest_of_line ();
17178 }
17179
17180 /* Handle a .stab[snd] directive. Ideally these directives would be
17181 implemented in a transparent way, so that removing them would not
17182 have any effect on the generated instructions. However, s_stab
17183 internally changes the section, so in practice we need to decide
17184 now whether the preceding label marks compressed code. We do not
17185 support changing the compression mode of a label after a .stab*
17186 directive, such as in:
17187
17188 foo:
17189 .stabs ...
17190 .set mips16
17191
17192 so the current mode wins. */
17193
17194 static void
17195 s_mips_stab (int type)
17196 {
17197 file_mips_check_options ();
17198 mips_mark_labels ();
17199 s_stab (type);
17200 }
17201
17202 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
17203
17204 static void
17205 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17206 {
17207 char *name;
17208 int c;
17209 symbolS *symbolP;
17210 expressionS exp;
17211
17212 c = get_symbol_name (&name);
17213 symbolP = symbol_find_or_make (name);
17214 S_SET_WEAK (symbolP);
17215 *input_line_pointer = c;
17216
17217 SKIP_WHITESPACE_AFTER_NAME ();
17218
17219 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17220 {
17221 if (S_IS_DEFINED (symbolP))
17222 {
17223 as_bad (_("ignoring attempt to redefine symbol %s"),
17224 S_GET_NAME (symbolP));
17225 ignore_rest_of_line ();
17226 return;
17227 }
17228
17229 if (*input_line_pointer == ',')
17230 {
17231 ++input_line_pointer;
17232 SKIP_WHITESPACE ();
17233 }
17234
17235 expression (&exp);
17236 if (exp.X_op != O_symbol)
17237 {
17238 as_bad (_("bad .weakext directive"));
17239 ignore_rest_of_line ();
17240 return;
17241 }
17242 symbol_set_value_expression (symbolP, &exp);
17243 }
17244
17245 demand_empty_rest_of_line ();
17246 }
17247
17248 /* Parse a register string into a number. Called from the ECOFF code
17249 to parse .frame. The argument is non-zero if this is the frame
17250 register, so that we can record it in mips_frame_reg. */
17251
17252 int
17253 tc_get_register (int frame)
17254 {
17255 unsigned int reg;
17256
17257 SKIP_WHITESPACE ();
17258 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17259 reg = 0;
17260 if (frame)
17261 {
17262 mips_frame_reg = reg != 0 ? reg : SP;
17263 mips_frame_reg_valid = 1;
17264 mips_cprestore_valid = 0;
17265 }
17266 return reg;
17267 }
17268
17269 valueT
17270 md_section_align (asection *seg, valueT addr)
17271 {
17272 int align = bfd_get_section_alignment (stdoutput, seg);
17273
17274 /* We don't need to align ELF sections to the full alignment.
17275 However, Irix 5 may prefer that we align them at least to a 16
17276 byte boundary. We don't bother to align the sections if we
17277 are targeted for an embedded system. */
17278 if (strncmp (TARGET_OS, "elf", 3) == 0)
17279 return addr;
17280 if (align > 4)
17281 align = 4;
17282
17283 return ((addr + (1 << align) - 1) & -(1 << align));
17284 }
17285
17286 /* Utility routine, called from above as well. If called while the
17287 input file is still being read, it's only an approximation. (For
17288 example, a symbol may later become defined which appeared to be
17289 undefined earlier.) */
17290
17291 static int
17292 nopic_need_relax (symbolS *sym, int before_relaxing)
17293 {
17294 if (sym == 0)
17295 return 0;
17296
17297 if (g_switch_value > 0)
17298 {
17299 const char *symname;
17300 int change;
17301
17302 /* Find out whether this symbol can be referenced off the $gp
17303 register. It can be if it is smaller than the -G size or if
17304 it is in the .sdata or .sbss section. Certain symbols can
17305 not be referenced off the $gp, although it appears as though
17306 they can. */
17307 symname = S_GET_NAME (sym);
17308 if (symname != (const char *) NULL
17309 && (strcmp (symname, "eprol") == 0
17310 || strcmp (symname, "etext") == 0
17311 || strcmp (symname, "_gp") == 0
17312 || strcmp (symname, "edata") == 0
17313 || strcmp (symname, "_fbss") == 0
17314 || strcmp (symname, "_fdata") == 0
17315 || strcmp (symname, "_ftext") == 0
17316 || strcmp (symname, "end") == 0
17317 || strcmp (symname, "_gp_disp") == 0))
17318 change = 1;
17319 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17320 && (0
17321 #ifndef NO_ECOFF_DEBUGGING
17322 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17323 && (symbol_get_obj (sym)->ecoff_extern_size
17324 <= g_switch_value))
17325 #endif
17326 /* We must defer this decision until after the whole
17327 file has been read, since there might be a .extern
17328 after the first use of this symbol. */
17329 || (before_relaxing
17330 #ifndef NO_ECOFF_DEBUGGING
17331 && symbol_get_obj (sym)->ecoff_extern_size == 0
17332 #endif
17333 && S_GET_VALUE (sym) == 0)
17334 || (S_GET_VALUE (sym) != 0
17335 && S_GET_VALUE (sym) <= g_switch_value)))
17336 change = 0;
17337 else
17338 {
17339 const char *segname;
17340
17341 segname = segment_name (S_GET_SEGMENT (sym));
17342 gas_assert (strcmp (segname, ".lit8") != 0
17343 && strcmp (segname, ".lit4") != 0);
17344 change = (strcmp (segname, ".sdata") != 0
17345 && strcmp (segname, ".sbss") != 0
17346 && strncmp (segname, ".sdata.", 7) != 0
17347 && strncmp (segname, ".sbss.", 6) != 0
17348 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17349 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17350 }
17351 return change;
17352 }
17353 else
17354 /* We are not optimizing for the $gp register. */
17355 return 1;
17356 }
17357
17358
17359 /* Return true if the given symbol should be considered local for SVR4 PIC. */
17360
17361 static bfd_boolean
17362 pic_need_relax (symbolS *sym)
17363 {
17364 asection *symsec;
17365
17366 /* Handle the case of a symbol equated to another symbol. */
17367 while (symbol_equated_reloc_p (sym))
17368 {
17369 symbolS *n;
17370
17371 /* It's possible to get a loop here in a badly written program. */
17372 n = symbol_get_value_expression (sym)->X_add_symbol;
17373 if (n == sym)
17374 break;
17375 sym = n;
17376 }
17377
17378 if (symbol_section_p (sym))
17379 return TRUE;
17380
17381 symsec = S_GET_SEGMENT (sym);
17382
17383 /* This must duplicate the test in adjust_reloc_syms. */
17384 return (!bfd_is_und_section (symsec)
17385 && !bfd_is_abs_section (symsec)
17386 && !bfd_is_com_section (symsec)
17387 /* A global or weak symbol is treated as external. */
17388 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
17389 }
17390 \f
17391 /* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17392 convert a section-relative value VAL to the equivalent PC-relative
17393 value. */
17394
17395 static offsetT
17396 mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17397 offsetT val, long stretch)
17398 {
17399 fragS *sym_frag;
17400 addressT addr;
17401
17402 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17403
17404 sym_frag = symbol_get_frag (fragp->fr_symbol);
17405
17406 /* If the relax_marker of the symbol fragment differs from the
17407 relax_marker of this fragment, we have not yet adjusted the
17408 symbol fragment fr_address. We want to add in STRETCH in
17409 order to get a better estimate of the address. This
17410 particularly matters because of the shift bits. */
17411 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17412 {
17413 fragS *f;
17414
17415 /* Adjust stretch for any alignment frag. Note that if have
17416 been expanding the earlier code, the symbol may be
17417 defined in what appears to be an earlier frag. FIXME:
17418 This doesn't handle the fr_subtype field, which specifies
17419 a maximum number of bytes to skip when doing an
17420 alignment. */
17421 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17422 {
17423 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17424 {
17425 if (stretch < 0)
17426 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17427 else
17428 stretch &= ~((1 << (int) f->fr_offset) - 1);
17429 if (stretch == 0)
17430 break;
17431 }
17432 }
17433 if (f != NULL)
17434 val += stretch;
17435 }
17436
17437 addr = fragp->fr_address + fragp->fr_fix;
17438
17439 /* The base address rules are complicated. The base address of
17440 a branch is the following instruction. The base address of a
17441 PC relative load or add is the instruction itself, but if it
17442 is in a delay slot (in which case it can not be extended) use
17443 the address of the instruction whose delay slot it is in. */
17444 if (pcrel_op->include_isa_bit)
17445 {
17446 addr += 2;
17447
17448 /* If we are currently assuming that this frag should be
17449 extended, then the current address is two bytes higher. */
17450 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17451 addr += 2;
17452
17453 /* Ignore the low bit in the target, since it will be set
17454 for a text label. */
17455 val &= -2;
17456 }
17457 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17458 addr -= 4;
17459 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17460 addr -= 2;
17461
17462 val -= addr & -(1 << pcrel_op->align_log2);
17463
17464 return val;
17465 }
17466
17467 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17468 extended opcode. SEC is the section the frag is in. */
17469
17470 static int
17471 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17472 {
17473 const struct mips_int_operand *operand;
17474 offsetT val;
17475 segT symsec;
17476 int type;
17477
17478 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17479 return 0;
17480 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17481 return 1;
17482
17483 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17484 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17485 operand = mips16_immed_operand (type, FALSE);
17486 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17487 || (operand->root.type == OP_PCREL
17488 ? sec != symsec
17489 : !bfd_is_abs_section (symsec)))
17490 return 1;
17491
17492 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17493
17494 if (operand->root.type == OP_PCREL)
17495 {
17496 const struct mips_pcrel_operand *pcrel_op;
17497 offsetT maxtiny;
17498
17499 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
17500 return 1;
17501
17502 pcrel_op = (const struct mips_pcrel_operand *) operand;
17503 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17504
17505 /* If any of the shifted bits are set, we must use an extended
17506 opcode. If the address depends on the size of this
17507 instruction, this can lead to a loop, so we arrange to always
17508 use an extended opcode. */
17509 if ((val & ((1 << operand->shift) - 1)) != 0)
17510 {
17511 fragp->fr_subtype =
17512 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17513 return 1;
17514 }
17515
17516 /* If we are about to mark a frag as extended because the value
17517 is precisely the next value above maxtiny, then there is a
17518 chance of an infinite loop as in the following code:
17519 la $4,foo
17520 .skip 1020
17521 .align 2
17522 foo:
17523 In this case when the la is extended, foo is 0x3fc bytes
17524 away, so the la can be shrunk, but then foo is 0x400 away, so
17525 the la must be extended. To avoid this loop, we mark the
17526 frag as extended if it was small, and is about to become
17527 extended with the next value above maxtiny. */
17528 maxtiny = mips_int_operand_max (operand);
17529 if (val == maxtiny + (1 << operand->shift)
17530 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17531 {
17532 fragp->fr_subtype =
17533 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17534 return 1;
17535 }
17536 }
17537
17538 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17539 }
17540
17541 /* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17542 macro expansion. SEC is the section the frag is in. We only
17543 support PC-relative instructions (LA, DLA, LW, LD) here, in
17544 non-PIC code using 32-bit addressing. */
17545
17546 static int
17547 mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17548 {
17549 const struct mips_pcrel_operand *pcrel_op;
17550 const struct mips_int_operand *operand;
17551 offsetT val;
17552 segT symsec;
17553 int type;
17554
17555 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17556
17557 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17558 return 0;
17559 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17560 return 0;
17561
17562 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17563 switch (type)
17564 {
17565 case 'A':
17566 case 'B':
17567 case 'E':
17568 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17569 if (bfd_is_abs_section (symsec))
17570 return 1;
17571 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17572 return 0;
17573 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17574 return 1;
17575
17576 operand = mips16_immed_operand (type, TRUE);
17577 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17578 pcrel_op = (const struct mips_pcrel_operand *) operand;
17579 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17580
17581 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17582
17583 default:
17584 return 0;
17585 }
17586 }
17587
17588 /* Compute the length of a branch sequence, and adjust the
17589 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17590 worst-case length is computed, with UPDATE being used to indicate
17591 whether an unconditional (-1), branch-likely (+1) or regular (0)
17592 branch is to be computed. */
17593 static int
17594 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17595 {
17596 bfd_boolean toofar;
17597 int length;
17598
17599 if (fragp
17600 && S_IS_DEFINED (fragp->fr_symbol)
17601 && !S_IS_WEAK (fragp->fr_symbol)
17602 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17603 {
17604 addressT addr;
17605 offsetT val;
17606
17607 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17608
17609 addr = fragp->fr_address + fragp->fr_fix + 4;
17610
17611 val -= addr;
17612
17613 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17614 }
17615 else
17616 /* If the symbol is not defined or it's in a different segment,
17617 we emit the long sequence. */
17618 toofar = TRUE;
17619
17620 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17621 fragp->fr_subtype
17622 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17623 RELAX_BRANCH_PIC (fragp->fr_subtype),
17624 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17625 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17626 RELAX_BRANCH_LINK (fragp->fr_subtype),
17627 toofar);
17628
17629 length = 4;
17630 if (toofar)
17631 {
17632 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17633 length += 8;
17634
17635 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
17636 {
17637 /* Additional space for PIC loading of target address. */
17638 length += 8;
17639 if (mips_opts.isa == ISA_MIPS1)
17640 /* Additional space for $at-stabilizing nop. */
17641 length += 4;
17642 }
17643
17644 /* If branch is conditional. */
17645 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17646 length += 8;
17647 }
17648
17649 return length;
17650 }
17651
17652 /* Get a FRAG's branch instruction delay slot size, either from the
17653 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17654 or SHORT_INSN_SIZE otherwise. */
17655
17656 static int
17657 frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17658 {
17659 char *buf = fragp->fr_literal + fragp->fr_fix;
17660
17661 if (al)
17662 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17663 else
17664 return short_insn_size;
17665 }
17666
17667 /* Compute the length of a branch sequence, and adjust the
17668 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17669 worst-case length is computed, with UPDATE being used to indicate
17670 whether an unconditional (-1), or regular (0) branch is to be
17671 computed. */
17672
17673 static int
17674 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17675 {
17676 bfd_boolean insn32 = TRUE;
17677 bfd_boolean nods = TRUE;
17678 bfd_boolean pic = TRUE;
17679 bfd_boolean al = TRUE;
17680 int short_insn_size;
17681 bfd_boolean toofar;
17682 int length;
17683
17684 if (fragp)
17685 {
17686 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17687 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
17688 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
17689 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17690 }
17691 short_insn_size = insn32 ? 4 : 2;
17692
17693 if (fragp
17694 && S_IS_DEFINED (fragp->fr_symbol)
17695 && !S_IS_WEAK (fragp->fr_symbol)
17696 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17697 {
17698 addressT addr;
17699 offsetT val;
17700
17701 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17702 /* Ignore the low bit in the target, since it will be set
17703 for a text label. */
17704 if ((val & 1) != 0)
17705 --val;
17706
17707 addr = fragp->fr_address + fragp->fr_fix + 4;
17708
17709 val -= addr;
17710
17711 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17712 }
17713 else
17714 /* If the symbol is not defined or it's in a different segment,
17715 we emit the long sequence. */
17716 toofar = TRUE;
17717
17718 if (fragp && update
17719 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17720 fragp->fr_subtype = (toofar
17721 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17722 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17723
17724 length = 4;
17725 if (toofar)
17726 {
17727 bfd_boolean compact_known = fragp != NULL;
17728 bfd_boolean compact = FALSE;
17729 bfd_boolean uncond;
17730
17731 if (fragp)
17732 {
17733 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17734 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17735 }
17736 else
17737 uncond = update < 0;
17738
17739 /* If label is out of range, we turn branch <br>:
17740
17741 <br> label # 4 bytes
17742 0:
17743
17744 into:
17745
17746 j label # 4 bytes
17747 nop # 2/4 bytes if
17748 # compact && (!PIC || insn32)
17749 0:
17750 */
17751 if ((!pic || insn32) && (!compact_known || compact))
17752 length += short_insn_size;
17753
17754 /* If assembling PIC code, we further turn:
17755
17756 j label # 4 bytes
17757
17758 into:
17759
17760 lw/ld at, %got(label)(gp) # 4 bytes
17761 d/addiu at, %lo(label) # 4 bytes
17762 jr/c at # 2/4 bytes
17763 */
17764 if (pic)
17765 length += 4 + short_insn_size;
17766
17767 /* Add an extra nop if the jump has no compact form and we need
17768 to fill the delay slot. */
17769 if ((!pic || al) && nods)
17770 length += (fragp
17771 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
17772 : short_insn_size);
17773
17774 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17775
17776 <brneg> 0f # 4 bytes
17777 nop # 2/4 bytes if !compact
17778 */
17779 if (!uncond)
17780 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
17781 }
17782 else if (nods)
17783 {
17784 /* Add an extra nop to fill the delay slot. */
17785 gas_assert (fragp);
17786 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
17787 }
17788
17789 return length;
17790 }
17791
17792 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17793 bit accordingly. */
17794
17795 static int
17796 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17797 {
17798 bfd_boolean toofar;
17799
17800 if (fragp
17801 && S_IS_DEFINED (fragp->fr_symbol)
17802 && !S_IS_WEAK (fragp->fr_symbol)
17803 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17804 {
17805 addressT addr;
17806 offsetT val;
17807 int type;
17808
17809 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17810 /* Ignore the low bit in the target, since it will be set
17811 for a text label. */
17812 if ((val & 1) != 0)
17813 --val;
17814
17815 /* Assume this is a 2-byte branch. */
17816 addr = fragp->fr_address + fragp->fr_fix + 2;
17817
17818 /* We try to avoid the infinite loop by not adding 2 more bytes for
17819 long branches. */
17820
17821 val -= addr;
17822
17823 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17824 if (type == 'D')
17825 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17826 else if (type == 'E')
17827 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17828 else
17829 abort ();
17830 }
17831 else
17832 /* If the symbol is not defined or it's in a different segment,
17833 we emit a normal 32-bit branch. */
17834 toofar = TRUE;
17835
17836 if (fragp && update
17837 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17838 fragp->fr_subtype
17839 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17840 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17841
17842 if (toofar)
17843 return 4;
17844
17845 return 2;
17846 }
17847
17848 /* Estimate the size of a frag before relaxing. Unless this is the
17849 mips16, we are not really relaxing here, and the final size is
17850 encoded in the subtype information. For the mips16, we have to
17851 decide whether we are using an extended opcode or not. */
17852
17853 int
17854 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17855 {
17856 int change;
17857
17858 if (RELAX_BRANCH_P (fragp->fr_subtype))
17859 {
17860
17861 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17862
17863 return fragp->fr_var;
17864 }
17865
17866 if (RELAX_MIPS16_P (fragp->fr_subtype))
17867 {
17868 /* We don't want to modify the EXTENDED bit here; it might get us
17869 into infinite loops. We change it only in mips_relax_frag(). */
17870 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
17871 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
17872 else
17873 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
17874 }
17875
17876 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17877 {
17878 int length = 4;
17879
17880 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17881 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17882 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17883 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17884 fragp->fr_var = length;
17885
17886 return length;
17887 }
17888
17889 if (mips_pic == VXWORKS_PIC)
17890 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17891 change = 0;
17892 else if (RELAX_PIC (fragp->fr_subtype))
17893 change = pic_need_relax (fragp->fr_symbol);
17894 else
17895 change = nopic_need_relax (fragp->fr_symbol, 0);
17896
17897 if (change)
17898 {
17899 fragp->fr_subtype |= RELAX_USE_SECOND;
17900 return -RELAX_FIRST (fragp->fr_subtype);
17901 }
17902 else
17903 return -RELAX_SECOND (fragp->fr_subtype);
17904 }
17905
17906 /* This is called to see whether a reloc against a defined symbol
17907 should be converted into a reloc against a section. */
17908
17909 int
17910 mips_fix_adjustable (fixS *fixp)
17911 {
17912 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17913 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17914 return 0;
17915
17916 if (fixp->fx_addsy == NULL)
17917 return 1;
17918
17919 /* Allow relocs used for EH tables. */
17920 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17921 return 1;
17922
17923 /* If symbol SYM is in a mergeable section, relocations of the form
17924 SYM + 0 can usually be made section-relative. The mergeable data
17925 is then identified by the section offset rather than by the symbol.
17926
17927 However, if we're generating REL LO16 relocations, the offset is split
17928 between the LO16 and partnering high part relocation. The linker will
17929 need to recalculate the complete offset in order to correctly identify
17930 the merge data.
17931
17932 The linker has traditionally not looked for the partnering high part
17933 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17934 placed anywhere. Rather than break backwards compatibility by changing
17935 this, it seems better not to force the issue, and instead keep the
17936 original symbol. This will work with either linker behavior. */
17937 if ((lo16_reloc_p (fixp->fx_r_type)
17938 || reloc_needs_lo_p (fixp->fx_r_type))
17939 && HAVE_IN_PLACE_ADDENDS
17940 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17941 return 0;
17942
17943 /* There is no place to store an in-place offset for JALR relocations. */
17944 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
17945 return 0;
17946
17947 /* Likewise an in-range offset of limited PC-relative relocations may
17948 overflow the in-place relocatable field if recalculated against the
17949 start address of the symbol's containing section.
17950
17951 Also, PC relative relocations for MIPS R6 need to be symbol rather than
17952 section relative to allow linker relaxations to be performed later on. */
17953 if (limited_pcrel_reloc_p (fixp->fx_r_type)
17954 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
17955 return 0;
17956
17957 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17958 to a floating-point stub. The same is true for non-R_MIPS16_26
17959 relocations against MIPS16 functions; in this case, the stub becomes
17960 the function's canonical address.
17961
17962 Floating-point stubs are stored in unique .mips16.call.* or
17963 .mips16.fn.* sections. If a stub T for function F is in section S,
17964 the first relocation in section S must be against F; this is how the
17965 linker determines the target function. All relocations that might
17966 resolve to T must also be against F. We therefore have the following
17967 restrictions, which are given in an intentionally-redundant way:
17968
17969 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17970 symbols.
17971
17972 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17973 if that stub might be used.
17974
17975 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17976 symbols.
17977
17978 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17979 that stub might be used.
17980
17981 There is a further restriction:
17982
17983 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17984 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
17985 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
17986 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
17987 against MIPS16 or microMIPS symbols because we need to keep the
17988 MIPS16 or microMIPS symbol for the purpose of mode mismatch
17989 detection and JAL or BAL to JALX instruction conversion in the
17990 linker.
17991
17992 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17993 against a MIPS16 symbol. We deal with (5) by additionally leaving
17994 alone any jump and branch relocations against a microMIPS symbol.
17995
17996 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17997 relocation against some symbol R, no relocation against R may be
17998 reduced. (Note that this deals with (2) as well as (1) because
17999 relocations against global symbols will never be reduced on ELF
18000 targets.) This approach is a little simpler than trying to detect
18001 stub sections, and gives the "all or nothing" per-symbol consistency
18002 that we have for MIPS16 symbols. */
18003 if (fixp->fx_subsy == NULL
18004 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
18005 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
18006 && (jmp_reloc_p (fixp->fx_r_type)
18007 || b_reloc_p (fixp->fx_r_type)))
18008 || *symbol_get_tc (fixp->fx_addsy)))
18009 return 0;
18010
18011 return 1;
18012 }
18013
18014 /* Translate internal representation of relocation info to BFD target
18015 format. */
18016
18017 arelent **
18018 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
18019 {
18020 static arelent *retval[4];
18021 arelent *reloc;
18022 bfd_reloc_code_real_type code;
18023
18024 memset (retval, 0, sizeof(retval));
18025 reloc = retval[0] = XCNEW (arelent);
18026 reloc->sym_ptr_ptr = XNEW (asymbol *);
18027 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18028 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18029
18030 if (fixp->fx_pcrel)
18031 {
18032 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18033 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
18034 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18035 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
18036 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18037 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18038 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18039 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18040 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18041 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18042 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18043 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
18044
18045 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18046 Relocations want only the symbol offset. */
18047 switch (fixp->fx_r_type)
18048 {
18049 case BFD_RELOC_MIPS_18_PCREL_S3:
18050 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18051 break;
18052 default:
18053 reloc->addend = fixp->fx_addnumber + reloc->address;
18054 break;
18055 }
18056 }
18057 else if (HAVE_IN_PLACE_ADDENDS
18058 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18059 && (read_compressed_insn (fixp->fx_frag->fr_literal
18060 + fixp->fx_where, 4) >> 26) == 0x3c)
18061 {
18062 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18063 addend accordingly. */
18064 reloc->addend = fixp->fx_addnumber >> 1;
18065 }
18066 else
18067 reloc->addend = fixp->fx_addnumber;
18068
18069 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18070 entry to be used in the relocation's section offset. */
18071 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18072 {
18073 reloc->address = reloc->addend;
18074 reloc->addend = 0;
18075 }
18076
18077 code = fixp->fx_r_type;
18078
18079 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
18080 if (reloc->howto == NULL)
18081 {
18082 as_bad_where (fixp->fx_file, fixp->fx_line,
18083 _("cannot represent %s relocation in this object file"
18084 " format"),
18085 bfd_get_reloc_code_name (code));
18086 retval[0] = NULL;
18087 }
18088
18089 return retval;
18090 }
18091
18092 /* Relax a machine dependent frag. This returns the amount by which
18093 the current size of the frag should change. */
18094
18095 int
18096 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
18097 {
18098 if (RELAX_BRANCH_P (fragp->fr_subtype))
18099 {
18100 offsetT old_var = fragp->fr_var;
18101
18102 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
18103
18104 return fragp->fr_var - old_var;
18105 }
18106
18107 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18108 {
18109 offsetT old_var = fragp->fr_var;
18110 offsetT new_var = 4;
18111
18112 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18113 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18114 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18115 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18116 fragp->fr_var = new_var;
18117
18118 return new_var - old_var;
18119 }
18120
18121 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18122 return 0;
18123
18124 if (!mips16_extended_frag (fragp, sec, stretch))
18125 {
18126 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18127 {
18128 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18129 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
18130 }
18131 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18132 {
18133 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18134 return -2;
18135 }
18136 else
18137 return 0;
18138 }
18139 else if (!mips16_macro_frag (fragp, sec, stretch))
18140 {
18141 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18142 {
18143 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18144 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18145 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
18146 }
18147 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18148 {
18149 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18150 return 2;
18151 }
18152 else
18153 return 0;
18154 }
18155 else
18156 {
18157 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18158 return 0;
18159 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18160 {
18161 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18162 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18163 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
18164 }
18165 else
18166 {
18167 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18168 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
18169 }
18170 }
18171
18172 return 0;
18173 }
18174
18175 /* Convert a machine dependent frag. */
18176
18177 void
18178 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
18179 {
18180 if (RELAX_BRANCH_P (fragp->fr_subtype))
18181 {
18182 char *buf;
18183 unsigned long insn;
18184 fixS *fixp;
18185
18186 buf = fragp->fr_literal + fragp->fr_fix;
18187 insn = read_insn (buf);
18188
18189 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18190 {
18191 /* We generate a fixup instead of applying it right now
18192 because, if there are linker relaxations, we're going to
18193 need the relocations. */
18194 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18195 fragp->fr_symbol, fragp->fr_offset,
18196 TRUE, BFD_RELOC_16_PCREL_S2);
18197 fixp->fx_file = fragp->fr_file;
18198 fixp->fx_line = fragp->fr_line;
18199
18200 buf = write_insn (buf, insn);
18201 }
18202 else
18203 {
18204 int i;
18205
18206 as_warn_where (fragp->fr_file, fragp->fr_line,
18207 _("relaxed out-of-range branch into a jump"));
18208
18209 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18210 goto uncond;
18211
18212 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18213 {
18214 /* Reverse the branch. */
18215 switch ((insn >> 28) & 0xf)
18216 {
18217 case 4:
18218 if ((insn & 0xff000000) == 0x47000000
18219 || (insn & 0xff600000) == 0x45600000)
18220 {
18221 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18222 reversed by tweaking bit 23. */
18223 insn ^= 0x00800000;
18224 }
18225 else
18226 {
18227 /* bc[0-3][tf]l? instructions can have the condition
18228 reversed by tweaking a single TF bit, and their
18229 opcodes all have 0x4???????. */
18230 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18231 insn ^= 0x00010000;
18232 }
18233 break;
18234
18235 case 0:
18236 /* bltz 0x04000000 bgez 0x04010000
18237 bltzal 0x04100000 bgezal 0x04110000 */
18238 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18239 insn ^= 0x00010000;
18240 break;
18241
18242 case 1:
18243 /* beq 0x10000000 bne 0x14000000
18244 blez 0x18000000 bgtz 0x1c000000 */
18245 insn ^= 0x04000000;
18246 break;
18247
18248 default:
18249 abort ();
18250 }
18251 }
18252
18253 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18254 {
18255 /* Clear the and-link bit. */
18256 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18257
18258 /* bltzal 0x04100000 bgezal 0x04110000
18259 bltzall 0x04120000 bgezall 0x04130000 */
18260 insn &= ~0x00100000;
18261 }
18262
18263 /* Branch over the branch (if the branch was likely) or the
18264 full jump (not likely case). Compute the offset from the
18265 current instruction to branch to. */
18266 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18267 i = 16;
18268 else
18269 {
18270 /* How many bytes in instructions we've already emitted? */
18271 i = buf - fragp->fr_literal - fragp->fr_fix;
18272 /* How many bytes in instructions from here to the end? */
18273 i = fragp->fr_var - i;
18274 }
18275 /* Convert to instruction count. */
18276 i >>= 2;
18277 /* Branch counts from the next instruction. */
18278 i--;
18279 insn |= i;
18280 /* Branch over the jump. */
18281 buf = write_insn (buf, insn);
18282
18283 /* nop */
18284 buf = write_insn (buf, 0);
18285
18286 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18287 {
18288 /* beql $0, $0, 2f */
18289 insn = 0x50000000;
18290 /* Compute the PC offset from the current instruction to
18291 the end of the variable frag. */
18292 /* How many bytes in instructions we've already emitted? */
18293 i = buf - fragp->fr_literal - fragp->fr_fix;
18294 /* How many bytes in instructions from here to the end? */
18295 i = fragp->fr_var - i;
18296 /* Convert to instruction count. */
18297 i >>= 2;
18298 /* Don't decrement i, because we want to branch over the
18299 delay slot. */
18300 insn |= i;
18301
18302 buf = write_insn (buf, insn);
18303 buf = write_insn (buf, 0);
18304 }
18305
18306 uncond:
18307 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
18308 {
18309 /* j or jal. */
18310 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18311 ? 0x0c000000 : 0x08000000);
18312
18313 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18314 fragp->fr_symbol, fragp->fr_offset,
18315 FALSE, BFD_RELOC_MIPS_JMP);
18316 fixp->fx_file = fragp->fr_file;
18317 fixp->fx_line = fragp->fr_line;
18318
18319 buf = write_insn (buf, insn);
18320 }
18321 else
18322 {
18323 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18324
18325 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
18326 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18327 insn |= at << OP_SH_RT;
18328
18329 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18330 fragp->fr_symbol, fragp->fr_offset,
18331 FALSE, BFD_RELOC_MIPS_GOT16);
18332 fixp->fx_file = fragp->fr_file;
18333 fixp->fx_line = fragp->fr_line;
18334
18335 buf = write_insn (buf, insn);
18336
18337 if (mips_opts.isa == ISA_MIPS1)
18338 /* nop */
18339 buf = write_insn (buf, 0);
18340
18341 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
18342 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18343 insn |= at << OP_SH_RS | at << OP_SH_RT;
18344
18345 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18346 fragp->fr_symbol, fragp->fr_offset,
18347 FALSE, BFD_RELOC_LO16);
18348 fixp->fx_file = fragp->fr_file;
18349 fixp->fx_line = fragp->fr_line;
18350
18351 buf = write_insn (buf, insn);
18352
18353 /* j(al)r $at. */
18354 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18355 insn = 0x0000f809;
18356 else
18357 insn = 0x00000008;
18358 insn |= at << OP_SH_RS;
18359
18360 buf = write_insn (buf, insn);
18361 }
18362 }
18363
18364 fragp->fr_fix += fragp->fr_var;
18365 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18366 return;
18367 }
18368
18369 /* Relax microMIPS branches. */
18370 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18371 {
18372 char *buf = fragp->fr_literal + fragp->fr_fix;
18373 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18374 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18375 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18376 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18377 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18378 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18379 bfd_boolean short_ds;
18380 unsigned long insn;
18381 fixS *fixp;
18382
18383 fragp->fr_fix += fragp->fr_var;
18384
18385 /* Handle 16-bit branches that fit or are forced to fit. */
18386 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18387 {
18388 /* We generate a fixup instead of applying it right now,
18389 because if there is linker relaxation, we're going to
18390 need the relocations. */
18391 switch (type)
18392 {
18393 case 'D':
18394 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18395 fragp->fr_symbol, fragp->fr_offset,
18396 TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18397 break;
18398 case 'E':
18399 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18400 fragp->fr_symbol, fragp->fr_offset,
18401 TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18402 break;
18403 default:
18404 abort ();
18405 }
18406
18407 fixp->fx_file = fragp->fr_file;
18408 fixp->fx_line = fragp->fr_line;
18409
18410 /* These relocations can have an addend that won't fit in
18411 2 octets. */
18412 fixp->fx_no_overflow = 1;
18413
18414 return;
18415 }
18416
18417 /* Handle 32-bit branches that fit or are forced to fit. */
18418 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18419 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18420 {
18421 /* We generate a fixup instead of applying it right now,
18422 because if there is linker relaxation, we're going to
18423 need the relocations. */
18424 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18425 fragp->fr_symbol, fragp->fr_offset,
18426 TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18427 fixp->fx_file = fragp->fr_file;
18428 fixp->fx_line = fragp->fr_line;
18429
18430 if (type == 0)
18431 {
18432 insn = read_compressed_insn (buf, 4);
18433 buf += 4;
18434
18435 if (nods)
18436 {
18437 /* Check the short-delay-slot bit. */
18438 if (!al || (insn & 0x02000000) != 0)
18439 buf = write_compressed_insn (buf, 0x0c00, 2);
18440 else
18441 buf = write_compressed_insn (buf, 0x00000000, 4);
18442 }
18443
18444 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18445 return;
18446 }
18447 }
18448
18449 /* Relax 16-bit branches to 32-bit branches. */
18450 if (type != 0)
18451 {
18452 insn = read_compressed_insn (buf, 2);
18453
18454 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18455 insn = 0x94000000; /* beq */
18456 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18457 {
18458 unsigned long regno;
18459
18460 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18461 regno = micromips_to_32_reg_d_map [regno];
18462 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18463 insn |= regno << MICROMIPSOP_SH_RS;
18464 }
18465 else
18466 abort ();
18467
18468 /* Nothing else to do, just write it out. */
18469 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18470 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18471 {
18472 buf = write_compressed_insn (buf, insn, 4);
18473 if (nods)
18474 buf = write_compressed_insn (buf, 0x0c00, 2);
18475 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18476 return;
18477 }
18478 }
18479 else
18480 insn = read_compressed_insn (buf, 4);
18481
18482 /* Relax 32-bit branches to a sequence of instructions. */
18483 as_warn_where (fragp->fr_file, fragp->fr_line,
18484 _("relaxed out-of-range branch into a jump"));
18485
18486 /* Set the short-delay-slot bit. */
18487 short_ds = !al || (insn & 0x02000000) != 0;
18488
18489 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18490 {
18491 symbolS *l;
18492
18493 /* Reverse the branch. */
18494 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18495 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18496 insn ^= 0x20000000;
18497 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18498 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18499 || (insn & 0xffe00000) == 0x40800000 /* blez */
18500 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18501 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18502 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18503 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18504 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18505 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18506 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18507 insn ^= 0x00400000;
18508 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18509 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18510 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18511 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18512 insn ^= 0x00200000;
18513 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18514 BNZ.df */
18515 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18516 BNZ.V */
18517 insn ^= 0x00800000;
18518 else
18519 abort ();
18520
18521 if (al)
18522 {
18523 /* Clear the and-link and short-delay-slot bits. */
18524 gas_assert ((insn & 0xfda00000) == 0x40200000);
18525
18526 /* bltzal 0x40200000 bgezal 0x40600000 */
18527 /* bltzals 0x42200000 bgezals 0x42600000 */
18528 insn &= ~0x02200000;
18529 }
18530
18531 /* Make a label at the end for use with the branch. */
18532 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18533 micromips_label_inc ();
18534 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18535
18536 /* Refer to it. */
18537 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18538 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18539 fixp->fx_file = fragp->fr_file;
18540 fixp->fx_line = fragp->fr_line;
18541
18542 /* Branch over the jump. */
18543 buf = write_compressed_insn (buf, insn, 4);
18544
18545 if (!compact)
18546 {
18547 /* nop */
18548 if (insn32)
18549 buf = write_compressed_insn (buf, 0x00000000, 4);
18550 else
18551 buf = write_compressed_insn (buf, 0x0c00, 2);
18552 }
18553 }
18554
18555 if (!pic)
18556 {
18557 unsigned long jal = (short_ds || nods
18558 ? 0x74000000 : 0xf4000000); /* jal/s */
18559
18560 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18561 insn = al ? jal : 0xd4000000;
18562
18563 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18564 fragp->fr_symbol, fragp->fr_offset,
18565 FALSE, BFD_RELOC_MICROMIPS_JMP);
18566 fixp->fx_file = fragp->fr_file;
18567 fixp->fx_line = fragp->fr_line;
18568
18569 buf = write_compressed_insn (buf, insn, 4);
18570
18571 if (compact || nods)
18572 {
18573 /* nop */
18574 if (insn32)
18575 buf = write_compressed_insn (buf, 0x00000000, 4);
18576 else
18577 buf = write_compressed_insn (buf, 0x0c00, 2);
18578 }
18579 }
18580 else
18581 {
18582 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18583
18584 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18585 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18586 insn |= at << MICROMIPSOP_SH_RT;
18587
18588 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18589 fragp->fr_symbol, fragp->fr_offset,
18590 FALSE, BFD_RELOC_MICROMIPS_GOT16);
18591 fixp->fx_file = fragp->fr_file;
18592 fixp->fx_line = fragp->fr_line;
18593
18594 buf = write_compressed_insn (buf, insn, 4);
18595
18596 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18597 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18598 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18599
18600 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18601 fragp->fr_symbol, fragp->fr_offset,
18602 FALSE, BFD_RELOC_MICROMIPS_LO16);
18603 fixp->fx_file = fragp->fr_file;
18604 fixp->fx_line = fragp->fr_line;
18605
18606 buf = write_compressed_insn (buf, insn, 4);
18607
18608 if (insn32)
18609 {
18610 /* jr/jalr $at */
18611 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18612 insn |= at << MICROMIPSOP_SH_RS;
18613
18614 buf = write_compressed_insn (buf, insn, 4);
18615
18616 if (compact || nods)
18617 /* nop */
18618 buf = write_compressed_insn (buf, 0x00000000, 4);
18619 }
18620 else
18621 {
18622 /* jr/jrc/jalr/jalrs $at */
18623 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18624 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
18625
18626 insn = al ? jalr : jr;
18627 insn |= at << MICROMIPSOP_SH_MJ;
18628
18629 buf = write_compressed_insn (buf, insn, 2);
18630 if (al && nods)
18631 {
18632 /* nop */
18633 if (short_ds)
18634 buf = write_compressed_insn (buf, 0x0c00, 2);
18635 else
18636 buf = write_compressed_insn (buf, 0x00000000, 4);
18637 }
18638 }
18639 }
18640
18641 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18642 return;
18643 }
18644
18645 if (RELAX_MIPS16_P (fragp->fr_subtype))
18646 {
18647 int type;
18648 const struct mips_int_operand *operand;
18649 offsetT val;
18650 char *buf;
18651 unsigned int user_length;
18652 bfd_boolean need_reloc;
18653 unsigned long insn;
18654 bfd_boolean mac;
18655 bfd_boolean ext;
18656 segT symsec;
18657
18658 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18659 operand = mips16_immed_operand (type, FALSE);
18660
18661 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
18662 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18663 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
18664
18665 symsec = S_GET_SEGMENT (fragp->fr_symbol);
18666 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
18667 || (operand->root.type == OP_PCREL && !mac
18668 ? asec != symsec
18669 : !bfd_is_abs_section (symsec)));
18670
18671 if (operand->root.type == OP_PCREL && !mac)
18672 {
18673 const struct mips_pcrel_operand *pcrel_op;
18674
18675 pcrel_op = (const struct mips_pcrel_operand *) operand;
18676
18677 if (pcrel_op->include_isa_bit && !need_reloc)
18678 {
18679 if (!mips_ignore_branch_isa
18680 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
18681 as_bad_where (fragp->fr_file, fragp->fr_line,
18682 _("branch to a symbol in another ISA mode"));
18683 else if ((fragp->fr_offset & 0x1) != 0)
18684 as_bad_where (fragp->fr_file, fragp->fr_line,
18685 _("branch to misaligned address (0x%lx)"),
18686 (long) val);
18687 }
18688
18689 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
18690
18691 /* Make sure the section winds up with the alignment we have
18692 assumed. */
18693 if (operand->shift > 0)
18694 record_alignment (asec, operand->shift);
18695 }
18696
18697 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18698 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18699 {
18700 if (mac)
18701 as_warn_where (fragp->fr_file, fragp->fr_line,
18702 _("macro instruction expanded into multiple "
18703 "instructions in a branch delay slot"));
18704 else if (ext)
18705 as_warn_where (fragp->fr_file, fragp->fr_line,
18706 _("extended instruction in a branch delay slot"));
18707 }
18708 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
18709 as_warn_where (fragp->fr_file, fragp->fr_line,
18710 _("macro instruction expanded into multiple "
18711 "instructions"));
18712
18713 buf = fragp->fr_literal + fragp->fr_fix;
18714
18715 insn = read_compressed_insn (buf, 2);
18716 if (ext)
18717 insn |= MIPS16_EXTEND;
18718
18719 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18720 user_length = 4;
18721 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18722 user_length = 2;
18723 else
18724 user_length = 0;
18725
18726 if (mac)
18727 {
18728 unsigned long reg;
18729 unsigned long new;
18730 unsigned long op;
18731 bfd_boolean e2;
18732
18733 gas_assert (type == 'A' || type == 'B' || type == 'E');
18734 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
18735
18736 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
18737
18738 if (need_reloc)
18739 {
18740 fixS *fixp;
18741
18742 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
18743
18744 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18745 fragp->fr_symbol, fragp->fr_offset,
18746 FALSE, BFD_RELOC_MIPS16_HI16_S);
18747 fixp->fx_file = fragp->fr_file;
18748 fixp->fx_line = fragp->fr_line;
18749
18750 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
18751 fragp->fr_symbol, fragp->fr_offset,
18752 FALSE, BFD_RELOC_MIPS16_LO16);
18753 fixp->fx_file = fragp->fr_file;
18754 fixp->fx_line = fragp->fr_line;
18755
18756 val = 0;
18757 }
18758
18759 switch (insn & 0xf800)
18760 {
18761 case 0x0800: /* ADDIU */
18762 reg = (insn >> 8) & 0x7;
18763 op = 0xf0004800 | (reg << 8);
18764 break;
18765 case 0xb000: /* LW */
18766 reg = (insn >> 8) & 0x7;
18767 op = 0xf0009800 | (reg << 8) | (reg << 5);
18768 break;
18769 case 0xf800: /* I64 */
18770 reg = (insn >> 5) & 0x7;
18771 switch (insn & 0x0700)
18772 {
18773 case 0x0400: /* LD */
18774 op = 0xf0003800 | (reg << 8) | (reg << 5);
18775 break;
18776 case 0x0600: /* DADDIU */
18777 op = 0xf000fd00 | (reg << 5);
18778 break;
18779 default:
18780 abort ();
18781 }
18782 break;
18783 default:
18784 abort ();
18785 }
18786
18787 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
18788 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
18789 buf = write_compressed_insn (buf, new, 4);
18790 if (!e2)
18791 {
18792 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
18793 buf = write_compressed_insn (buf, new, 4);
18794 }
18795 op |= mips16_immed_extend (val, 16);
18796 buf = write_compressed_insn (buf, op, 4);
18797
18798 fragp->fr_fix += e2 ? 8 : 12;
18799 }
18800 else
18801 {
18802 unsigned int length = ext ? 4 : 2;
18803
18804 if (need_reloc)
18805 {
18806 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
18807 fixS *fixp;
18808
18809 switch (type)
18810 {
18811 case 'p':
18812 case 'q':
18813 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
18814 break;
18815 default:
18816 break;
18817 }
18818 if (mac || reloc == BFD_RELOC_NONE)
18819 as_bad_where (fragp->fr_file, fragp->fr_line,
18820 _("unsupported relocation"));
18821 else if (ext)
18822 {
18823 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18824 fragp->fr_symbol, fragp->fr_offset,
18825 TRUE, reloc);
18826 fixp->fx_file = fragp->fr_file;
18827 fixp->fx_line = fragp->fr_line;
18828 }
18829 else
18830 as_bad_where (fragp->fr_file, fragp->fr_line,
18831 _("invalid unextended operand value"));
18832 }
18833 else
18834 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18835 BFD_RELOC_UNUSED, val, user_length, &insn);
18836
18837 gas_assert (mips16_opcode_length (insn) == length);
18838 write_compressed_insn (buf, insn, length);
18839 fragp->fr_fix += length;
18840 }
18841 }
18842 else
18843 {
18844 relax_substateT subtype = fragp->fr_subtype;
18845 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18846 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18847 int first, second;
18848 fixS *fixp;
18849
18850 first = RELAX_FIRST (subtype);
18851 second = RELAX_SECOND (subtype);
18852 fixp = (fixS *) fragp->fr_opcode;
18853
18854 /* If the delay slot chosen does not match the size of the instruction,
18855 then emit a warning. */
18856 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18857 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18858 {
18859 relax_substateT s;
18860 const char *msg;
18861
18862 s = subtype & (RELAX_DELAY_SLOT_16BIT
18863 | RELAX_DELAY_SLOT_SIZE_FIRST
18864 | RELAX_DELAY_SLOT_SIZE_SECOND);
18865 msg = macro_warning (s);
18866 if (msg != NULL)
18867 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18868 subtype &= ~s;
18869 }
18870
18871 /* Possibly emit a warning if we've chosen the longer option. */
18872 if (use_second == second_longer)
18873 {
18874 relax_substateT s;
18875 const char *msg;
18876
18877 s = (subtype
18878 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18879 msg = macro_warning (s);
18880 if (msg != NULL)
18881 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18882 subtype &= ~s;
18883 }
18884
18885 /* Go through all the fixups for the first sequence. Disable them
18886 (by marking them as done) if we're going to use the second
18887 sequence instead. */
18888 while (fixp
18889 && fixp->fx_frag == fragp
18890 && fixp->fx_where < fragp->fr_fix - second)
18891 {
18892 if (subtype & RELAX_USE_SECOND)
18893 fixp->fx_done = 1;
18894 fixp = fixp->fx_next;
18895 }
18896
18897 /* Go through the fixups for the second sequence. Disable them if
18898 we're going to use the first sequence, otherwise adjust their
18899 addresses to account for the relaxation. */
18900 while (fixp && fixp->fx_frag == fragp)
18901 {
18902 if (subtype & RELAX_USE_SECOND)
18903 fixp->fx_where -= first;
18904 else
18905 fixp->fx_done = 1;
18906 fixp = fixp->fx_next;
18907 }
18908
18909 /* Now modify the frag contents. */
18910 if (subtype & RELAX_USE_SECOND)
18911 {
18912 char *start;
18913
18914 start = fragp->fr_literal + fragp->fr_fix - first - second;
18915 memmove (start, start + first, second);
18916 fragp->fr_fix -= first;
18917 }
18918 else
18919 fragp->fr_fix -= second;
18920 }
18921 }
18922
18923 /* This function is called after the relocs have been generated.
18924 We've been storing mips16 text labels as odd. Here we convert them
18925 back to even for the convenience of the debugger. */
18926
18927 void
18928 mips_frob_file_after_relocs (void)
18929 {
18930 asymbol **syms;
18931 unsigned int count, i;
18932
18933 syms = bfd_get_outsymbols (stdoutput);
18934 count = bfd_get_symcount (stdoutput);
18935 for (i = 0; i < count; i++, syms++)
18936 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18937 && ((*syms)->value & 1) != 0)
18938 {
18939 (*syms)->value &= ~1;
18940 /* If the symbol has an odd size, it was probably computed
18941 incorrectly, so adjust that as well. */
18942 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18943 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18944 }
18945 }
18946
18947 /* This function is called whenever a label is defined, including fake
18948 labels instantiated off the dot special symbol. It is used when
18949 handling branch delays; if a branch has a label, we assume we cannot
18950 move it. This also bumps the value of the symbol by 1 in compressed
18951 code. */
18952
18953 static void
18954 mips_record_label (symbolS *sym)
18955 {
18956 segment_info_type *si = seg_info (now_seg);
18957 struct insn_label_list *l;
18958
18959 if (free_insn_labels == NULL)
18960 l = XNEW (struct insn_label_list);
18961 else
18962 {
18963 l = free_insn_labels;
18964 free_insn_labels = l->next;
18965 }
18966
18967 l->label = sym;
18968 l->next = si->label_list;
18969 si->label_list = l;
18970 }
18971
18972 /* This function is called as tc_frob_label() whenever a label is defined
18973 and adds a DWARF-2 record we only want for true labels. */
18974
18975 void
18976 mips_define_label (symbolS *sym)
18977 {
18978 mips_record_label (sym);
18979 dwarf2_emit_label (sym);
18980 }
18981
18982 /* This function is called by tc_new_dot_label whenever a new dot symbol
18983 is defined. */
18984
18985 void
18986 mips_add_dot_label (symbolS *sym)
18987 {
18988 mips_record_label (sym);
18989 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18990 mips_compressed_mark_label (sym);
18991 }
18992 \f
18993 /* Converting ASE flags from internal to .MIPS.abiflags values. */
18994 static unsigned int
18995 mips_convert_ase_flags (int ase)
18996 {
18997 unsigned int ext_ases = 0;
18998
18999 if (ase & ASE_DSP)
19000 ext_ases |= AFL_ASE_DSP;
19001 if (ase & ASE_DSPR2)
19002 ext_ases |= AFL_ASE_DSPR2;
19003 if (ase & ASE_DSPR3)
19004 ext_ases |= AFL_ASE_DSPR3;
19005 if (ase & ASE_EVA)
19006 ext_ases |= AFL_ASE_EVA;
19007 if (ase & ASE_MCU)
19008 ext_ases |= AFL_ASE_MCU;
19009 if (ase & ASE_MDMX)
19010 ext_ases |= AFL_ASE_MDMX;
19011 if (ase & ASE_MIPS3D)
19012 ext_ases |= AFL_ASE_MIPS3D;
19013 if (ase & ASE_MT)
19014 ext_ases |= AFL_ASE_MT;
19015 if (ase & ASE_SMARTMIPS)
19016 ext_ases |= AFL_ASE_SMARTMIPS;
19017 if (ase & ASE_VIRT)
19018 ext_ases |= AFL_ASE_VIRT;
19019 if (ase & ASE_MSA)
19020 ext_ases |= AFL_ASE_MSA;
19021 if (ase & ASE_XPA)
19022 ext_ases |= AFL_ASE_XPA;
19023 if (ase & ASE_MIPS16E2)
19024 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
19025 if (ase & ASE_CRC)
19026 ext_ases |= AFL_ASE_CRC;
19027 if (ase & ASE_GINV)
19028 ext_ases |= AFL_ASE_GINV;
19029 if (ase & ASE_LOONGSON_MMI)
19030 ext_ases |= AFL_ASE_LOONGSON_MMI;
19031
19032 return ext_ases;
19033 }
19034 /* Some special processing for a MIPS ELF file. */
19035
19036 void
19037 mips_elf_final_processing (void)
19038 {
19039 int fpabi;
19040 Elf_Internal_ABIFlags_v0 flags;
19041
19042 flags.version = 0;
19043 flags.isa_rev = 0;
19044 switch (file_mips_opts.isa)
19045 {
19046 case INSN_ISA1:
19047 flags.isa_level = 1;
19048 break;
19049 case INSN_ISA2:
19050 flags.isa_level = 2;
19051 break;
19052 case INSN_ISA3:
19053 flags.isa_level = 3;
19054 break;
19055 case INSN_ISA4:
19056 flags.isa_level = 4;
19057 break;
19058 case INSN_ISA5:
19059 flags.isa_level = 5;
19060 break;
19061 case INSN_ISA32:
19062 flags.isa_level = 32;
19063 flags.isa_rev = 1;
19064 break;
19065 case INSN_ISA32R2:
19066 flags.isa_level = 32;
19067 flags.isa_rev = 2;
19068 break;
19069 case INSN_ISA32R3:
19070 flags.isa_level = 32;
19071 flags.isa_rev = 3;
19072 break;
19073 case INSN_ISA32R5:
19074 flags.isa_level = 32;
19075 flags.isa_rev = 5;
19076 break;
19077 case INSN_ISA32R6:
19078 flags.isa_level = 32;
19079 flags.isa_rev = 6;
19080 break;
19081 case INSN_ISA64:
19082 flags.isa_level = 64;
19083 flags.isa_rev = 1;
19084 break;
19085 case INSN_ISA64R2:
19086 flags.isa_level = 64;
19087 flags.isa_rev = 2;
19088 break;
19089 case INSN_ISA64R3:
19090 flags.isa_level = 64;
19091 flags.isa_rev = 3;
19092 break;
19093 case INSN_ISA64R5:
19094 flags.isa_level = 64;
19095 flags.isa_rev = 5;
19096 break;
19097 case INSN_ISA64R6:
19098 flags.isa_level = 64;
19099 flags.isa_rev = 6;
19100 break;
19101 }
19102
19103 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19104 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19105 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19106 : (file_mips_opts.fp == 64) ? AFL_REG_64
19107 : AFL_REG_32;
19108 flags.cpr2_size = AFL_REG_NONE;
19109 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19110 Tag_GNU_MIPS_ABI_FP);
19111 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19112 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19113 if (file_ase_mips16)
19114 flags.ases |= AFL_ASE_MIPS16;
19115 if (file_ase_micromips)
19116 flags.ases |= AFL_ASE_MICROMIPS;
19117 flags.flags1 = 0;
19118 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19119 || file_mips_opts.fp == 64)
19120 && file_mips_opts.oddspreg)
19121 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19122 flags.flags2 = 0;
19123
19124 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19125 ((Elf_External_ABIFlags_v0 *)
19126 mips_flags_frag));
19127
19128 /* Write out the register information. */
19129 if (mips_abi != N64_ABI)
19130 {
19131 Elf32_RegInfo s;
19132
19133 s.ri_gprmask = mips_gprmask;
19134 s.ri_cprmask[0] = mips_cprmask[0];
19135 s.ri_cprmask[1] = mips_cprmask[1];
19136 s.ri_cprmask[2] = mips_cprmask[2];
19137 s.ri_cprmask[3] = mips_cprmask[3];
19138 /* The gp_value field is set by the MIPS ELF backend. */
19139
19140 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19141 ((Elf32_External_RegInfo *)
19142 mips_regmask_frag));
19143 }
19144 else
19145 {
19146 Elf64_Internal_RegInfo s;
19147
19148 s.ri_gprmask = mips_gprmask;
19149 s.ri_pad = 0;
19150 s.ri_cprmask[0] = mips_cprmask[0];
19151 s.ri_cprmask[1] = mips_cprmask[1];
19152 s.ri_cprmask[2] = mips_cprmask[2];
19153 s.ri_cprmask[3] = mips_cprmask[3];
19154 /* The gp_value field is set by the MIPS ELF backend. */
19155
19156 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19157 ((Elf64_External_RegInfo *)
19158 mips_regmask_frag));
19159 }
19160
19161 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19162 sort of BFD interface for this. */
19163 if (mips_any_noreorder)
19164 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19165 if (mips_pic != NO_PIC)
19166 {
19167 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
19168 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19169 }
19170 if (mips_abicalls)
19171 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19172
19173 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19174 defined at present; this might need to change in future. */
19175 if (file_ase_mips16)
19176 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
19177 if (file_ase_micromips)
19178 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
19179 if (file_mips_opts.ase & ASE_MDMX)
19180 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
19181
19182 /* Set the MIPS ELF ABI flags. */
19183 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
19184 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
19185 else if (mips_abi == O64_ABI)
19186 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
19187 else if (mips_abi == EABI_ABI)
19188 {
19189 if (file_mips_opts.gp == 64)
19190 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19191 else
19192 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19193 }
19194
19195 /* Nothing to do for N32_ABI or N64_ABI. */
19196
19197 if (mips_32bitmode)
19198 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
19199
19200 if (mips_nan2008 == 1)
19201 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19202
19203 /* 32 bit code with 64 bit FP registers. */
19204 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19205 Tag_GNU_MIPS_ABI_FP);
19206 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
19207 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
19208 }
19209 \f
19210 typedef struct proc {
19211 symbolS *func_sym;
19212 symbolS *func_end_sym;
19213 unsigned long reg_mask;
19214 unsigned long reg_offset;
19215 unsigned long fpreg_mask;
19216 unsigned long fpreg_offset;
19217 unsigned long frame_offset;
19218 unsigned long frame_reg;
19219 unsigned long pc_reg;
19220 } procS;
19221
19222 static procS cur_proc;
19223 static procS *cur_proc_ptr;
19224 static int numprocs;
19225
19226 /* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19227 as "2", and a normal nop as "0". */
19228
19229 #define NOP_OPCODE_MIPS 0
19230 #define NOP_OPCODE_MIPS16 1
19231 #define NOP_OPCODE_MICROMIPS 2
19232
19233 char
19234 mips_nop_opcode (void)
19235 {
19236 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19237 return NOP_OPCODE_MICROMIPS;
19238 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19239 return NOP_OPCODE_MIPS16;
19240 else
19241 return NOP_OPCODE_MIPS;
19242 }
19243
19244 /* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19245 32-bit microMIPS NOPs here (if applicable). */
19246
19247 void
19248 mips_handle_align (fragS *fragp)
19249 {
19250 char nop_opcode;
19251 char *p;
19252 int bytes, size, excess;
19253 valueT opcode;
19254
19255 if (fragp->fr_type != rs_align_code)
19256 return;
19257
19258 p = fragp->fr_literal + fragp->fr_fix;
19259 nop_opcode = *p;
19260 switch (nop_opcode)
19261 {
19262 case NOP_OPCODE_MICROMIPS:
19263 opcode = micromips_nop32_insn.insn_opcode;
19264 size = 4;
19265 break;
19266 case NOP_OPCODE_MIPS16:
19267 opcode = mips16_nop_insn.insn_opcode;
19268 size = 2;
19269 break;
19270 case NOP_OPCODE_MIPS:
19271 default:
19272 opcode = nop_insn.insn_opcode;
19273 size = 4;
19274 break;
19275 }
19276
19277 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19278 excess = bytes % size;
19279
19280 /* Handle the leading part if we're not inserting a whole number of
19281 instructions, and make it the end of the fixed part of the frag.
19282 Try to fit in a short microMIPS NOP if applicable and possible,
19283 and use zeroes otherwise. */
19284 gas_assert (excess < 4);
19285 fragp->fr_fix += excess;
19286 switch (excess)
19287 {
19288 case 3:
19289 *p++ = '\0';
19290 /* Fall through. */
19291 case 2:
19292 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
19293 {
19294 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
19295 break;
19296 }
19297 *p++ = '\0';
19298 /* Fall through. */
19299 case 1:
19300 *p++ = '\0';
19301 /* Fall through. */
19302 case 0:
19303 break;
19304 }
19305
19306 md_number_to_chars (p, opcode, size);
19307 fragp->fr_var = size;
19308 }
19309
19310 static long
19311 get_number (void)
19312 {
19313 int negative = 0;
19314 long val = 0;
19315
19316 if (*input_line_pointer == '-')
19317 {
19318 ++input_line_pointer;
19319 negative = 1;
19320 }
19321 if (!ISDIGIT (*input_line_pointer))
19322 as_bad (_("expected simple number"));
19323 if (input_line_pointer[0] == '0')
19324 {
19325 if (input_line_pointer[1] == 'x')
19326 {
19327 input_line_pointer += 2;
19328 while (ISXDIGIT (*input_line_pointer))
19329 {
19330 val <<= 4;
19331 val |= hex_value (*input_line_pointer++);
19332 }
19333 return negative ? -val : val;
19334 }
19335 else
19336 {
19337 ++input_line_pointer;
19338 while (ISDIGIT (*input_line_pointer))
19339 {
19340 val <<= 3;
19341 val |= *input_line_pointer++ - '0';
19342 }
19343 return negative ? -val : val;
19344 }
19345 }
19346 if (!ISDIGIT (*input_line_pointer))
19347 {
19348 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19349 *input_line_pointer, *input_line_pointer);
19350 as_warn (_("invalid number"));
19351 return -1;
19352 }
19353 while (ISDIGIT (*input_line_pointer))
19354 {
19355 val *= 10;
19356 val += *input_line_pointer++ - '0';
19357 }
19358 return negative ? -val : val;
19359 }
19360
19361 /* The .file directive; just like the usual .file directive, but there
19362 is an initial number which is the ECOFF file index. In the non-ECOFF
19363 case .file implies DWARF-2. */
19364
19365 static void
19366 s_mips_file (int x ATTRIBUTE_UNUSED)
19367 {
19368 static int first_file_directive = 0;
19369
19370 if (ECOFF_DEBUGGING)
19371 {
19372 get_number ();
19373 s_app_file (0);
19374 }
19375 else
19376 {
19377 char *filename;
19378
19379 filename = dwarf2_directive_filename ();
19380
19381 /* Versions of GCC up to 3.1 start files with a ".file"
19382 directive even for stabs output. Make sure that this
19383 ".file" is handled. Note that you need a version of GCC
19384 after 3.1 in order to support DWARF-2 on MIPS. */
19385 if (filename != NULL && ! first_file_directive)
19386 {
19387 (void) new_logical_line (filename, -1);
19388 s_app_file_string (filename, 0);
19389 }
19390 first_file_directive = 1;
19391 }
19392 }
19393
19394 /* The .loc directive, implying DWARF-2. */
19395
19396 static void
19397 s_mips_loc (int x ATTRIBUTE_UNUSED)
19398 {
19399 if (!ECOFF_DEBUGGING)
19400 dwarf2_directive_loc (0);
19401 }
19402
19403 /* The .end directive. */
19404
19405 static void
19406 s_mips_end (int x ATTRIBUTE_UNUSED)
19407 {
19408 symbolS *p;
19409
19410 /* Following functions need their own .frame and .cprestore directives. */
19411 mips_frame_reg_valid = 0;
19412 mips_cprestore_valid = 0;
19413
19414 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19415 {
19416 p = get_symbol ();
19417 demand_empty_rest_of_line ();
19418 }
19419 else
19420 p = NULL;
19421
19422 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19423 as_warn (_(".end not in text section"));
19424
19425 if (!cur_proc_ptr)
19426 {
19427 as_warn (_(".end directive without a preceding .ent directive"));
19428 demand_empty_rest_of_line ();
19429 return;
19430 }
19431
19432 if (p != NULL)
19433 {
19434 gas_assert (S_GET_NAME (p));
19435 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
19436 as_warn (_(".end symbol does not match .ent symbol"));
19437
19438 if (debug_type == DEBUG_STABS)
19439 stabs_generate_asm_endfunc (S_GET_NAME (p),
19440 S_GET_NAME (p));
19441 }
19442 else
19443 as_warn (_(".end directive missing or unknown symbol"));
19444
19445 /* Create an expression to calculate the size of the function. */
19446 if (p && cur_proc_ptr)
19447 {
19448 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19449 expressionS *exp = XNEW (expressionS);
19450
19451 obj->size = exp;
19452 exp->X_op = O_subtract;
19453 exp->X_add_symbol = symbol_temp_new_now ();
19454 exp->X_op_symbol = p;
19455 exp->X_add_number = 0;
19456
19457 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19458 }
19459
19460 #ifdef md_flush_pending_output
19461 md_flush_pending_output ();
19462 #endif
19463
19464 /* Generate a .pdr section. */
19465 if (!ECOFF_DEBUGGING && mips_flag_pdr)
19466 {
19467 segT saved_seg = now_seg;
19468 subsegT saved_subseg = now_subseg;
19469 expressionS exp;
19470 char *fragp;
19471
19472 gas_assert (pdr_seg);
19473 subseg_set (pdr_seg, 0);
19474
19475 /* Write the symbol. */
19476 exp.X_op = O_symbol;
19477 exp.X_add_symbol = p;
19478 exp.X_add_number = 0;
19479 emit_expr (&exp, 4);
19480
19481 fragp = frag_more (7 * 4);
19482
19483 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19484 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19485 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19486 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19487 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19488 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19489 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19490
19491 subseg_set (saved_seg, saved_subseg);
19492 }
19493
19494 cur_proc_ptr = NULL;
19495 }
19496
19497 /* The .aent and .ent directives. */
19498
19499 static void
19500 s_mips_ent (int aent)
19501 {
19502 symbolS *symbolP;
19503
19504 symbolP = get_symbol ();
19505 if (*input_line_pointer == ',')
19506 ++input_line_pointer;
19507 SKIP_WHITESPACE ();
19508 if (ISDIGIT (*input_line_pointer)
19509 || *input_line_pointer == '-')
19510 get_number ();
19511
19512 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19513 as_warn (_(".ent or .aent not in text section"));
19514
19515 if (!aent && cur_proc_ptr)
19516 as_warn (_("missing .end"));
19517
19518 if (!aent)
19519 {
19520 /* This function needs its own .frame and .cprestore directives. */
19521 mips_frame_reg_valid = 0;
19522 mips_cprestore_valid = 0;
19523
19524 cur_proc_ptr = &cur_proc;
19525 memset (cur_proc_ptr, '\0', sizeof (procS));
19526
19527 cur_proc_ptr->func_sym = symbolP;
19528
19529 ++numprocs;
19530
19531 if (debug_type == DEBUG_STABS)
19532 stabs_generate_asm_func (S_GET_NAME (symbolP),
19533 S_GET_NAME (symbolP));
19534 }
19535
19536 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19537
19538 demand_empty_rest_of_line ();
19539 }
19540
19541 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19542 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19543 s_mips_frame is used so that we can set the PDR information correctly.
19544 We can't use the ecoff routines because they make reference to the ecoff
19545 symbol table (in the mdebug section). */
19546
19547 static void
19548 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19549 {
19550 if (ECOFF_DEBUGGING)
19551 s_ignore (ignore);
19552 else
19553 {
19554 long val;
19555
19556 if (cur_proc_ptr == (procS *) NULL)
19557 {
19558 as_warn (_(".frame outside of .ent"));
19559 demand_empty_rest_of_line ();
19560 return;
19561 }
19562
19563 cur_proc_ptr->frame_reg = tc_get_register (1);
19564
19565 SKIP_WHITESPACE ();
19566 if (*input_line_pointer++ != ','
19567 || get_absolute_expression_and_terminator (&val) != ',')
19568 {
19569 as_warn (_("bad .frame directive"));
19570 --input_line_pointer;
19571 demand_empty_rest_of_line ();
19572 return;
19573 }
19574
19575 cur_proc_ptr->frame_offset = val;
19576 cur_proc_ptr->pc_reg = tc_get_register (0);
19577
19578 demand_empty_rest_of_line ();
19579 }
19580 }
19581
19582 /* The .fmask and .mask directives. If the mdebug section is present
19583 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19584 embedded targets, s_mips_mask is used so that we can set the PDR
19585 information correctly. We can't use the ecoff routines because they
19586 make reference to the ecoff symbol table (in the mdebug section). */
19587
19588 static void
19589 s_mips_mask (int reg_type)
19590 {
19591 if (ECOFF_DEBUGGING)
19592 s_ignore (reg_type);
19593 else
19594 {
19595 long mask, off;
19596
19597 if (cur_proc_ptr == (procS *) NULL)
19598 {
19599 as_warn (_(".mask/.fmask outside of .ent"));
19600 demand_empty_rest_of_line ();
19601 return;
19602 }
19603
19604 if (get_absolute_expression_and_terminator (&mask) != ',')
19605 {
19606 as_warn (_("bad .mask/.fmask directive"));
19607 --input_line_pointer;
19608 demand_empty_rest_of_line ();
19609 return;
19610 }
19611
19612 off = get_absolute_expression ();
19613
19614 if (reg_type == 'F')
19615 {
19616 cur_proc_ptr->fpreg_mask = mask;
19617 cur_proc_ptr->fpreg_offset = off;
19618 }
19619 else
19620 {
19621 cur_proc_ptr->reg_mask = mask;
19622 cur_proc_ptr->reg_offset = off;
19623 }
19624
19625 demand_empty_rest_of_line ();
19626 }
19627 }
19628
19629 /* A table describing all the processors gas knows about. Names are
19630 matched in the order listed.
19631
19632 To ease comparison, please keep this table in the same order as
19633 gcc's mips_cpu_info_table[]. */
19634 static const struct mips_cpu_info mips_cpu_info_table[] =
19635 {
19636 /* Entries for generic ISAs */
19637 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19638 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19639 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19640 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19641 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19642 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19643 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19644 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19645 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
19646 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
19647 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19648 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
19649 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19650 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
19651 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
19652
19653 /* MIPS I */
19654 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19655 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19656 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
19657
19658 /* MIPS II */
19659 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
19660
19661 /* MIPS III */
19662 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19663 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19664 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19665 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19666 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19667 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19668 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19669 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19670 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19671 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19672 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19673 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19674 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
19675 /* ST Microelectronics Loongson 2E and 2F cores */
19676 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19677 { "loongson2f", 0, ASE_LOONGSON_MMI, ISA_MIPS3, CPU_LOONGSON_2F },
19678
19679 /* MIPS IV */
19680 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19681 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19682 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19683 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19684 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19685 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19686 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19687 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19688 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19689 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19690 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19691 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19692 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19693 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19694 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
19695
19696 /* MIPS 32 */
19697 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19698 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19699 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19700 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19701
19702 /* MIPS 32 Release 2 */
19703 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19704 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19705 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19706 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19707 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19708 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19709 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19710 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19711 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19712 ISA_MIPS32R2, CPU_MIPS32R2 },
19713 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19714 ISA_MIPS32R2, CPU_MIPS32R2 },
19715 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19716 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19717 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19718 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19719 /* Deprecated forms of the above. */
19720 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19721 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19722 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
19723 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19724 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19725 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19726 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19727 /* Deprecated forms of the above. */
19728 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19729 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19730 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
19731 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19732 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19733 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19734 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19735 /* Deprecated forms of the above. */
19736 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19737 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19738 /* 34Kn is a 34kc without DSP. */
19739 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19740 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19741 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19742 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19743 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19744 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19745 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19746 /* Deprecated forms of the above. */
19747 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19748 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19749 /* 1004K cores are multiprocessor versions of the 34K. */
19750 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19751 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19752 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19753 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19754 /* interaptiv is the new name for 1004kf */
19755 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19756 { "interaptiv-mr2", 0,
19757 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
19758 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
19759 /* M5100 family */
19760 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19761 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19762 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
19763 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
19764
19765 /* MIPS 64 */
19766 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19767 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19768 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19769 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19770
19771 /* Broadcom SB-1 CPU core */
19772 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19773 /* Broadcom SB-1A CPU core */
19774 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19775
19776 { "loongson3a", 0, ASE_LOONGSON_MMI, ISA_MIPS64R2, CPU_LOONGSON_3A },
19777
19778 /* MIPS 64 Release 2 */
19779
19780 /* Cavium Networks Octeon CPU core */
19781 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19782 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19783 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
19784 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
19785
19786 /* RMI Xlr */
19787 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
19788
19789 /* Broadcom XLP.
19790 XLP is mostly like XLR, with the prominent exception that it is
19791 MIPS64R2 rather than MIPS64. */
19792 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
19793
19794 /* MIPS 64 Release 6 */
19795 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
19796 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
19797
19798 /* End marker */
19799 { NULL, 0, 0, 0, 0 }
19800 };
19801
19802
19803 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19804 with a final "000" replaced by "k". Ignore case.
19805
19806 Note: this function is shared between GCC and GAS. */
19807
19808 static bfd_boolean
19809 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19810 {
19811 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19812 given++, canonical++;
19813
19814 return ((*given == 0 && *canonical == 0)
19815 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19816 }
19817
19818
19819 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19820 CPU name. We've traditionally allowed a lot of variation here.
19821
19822 Note: this function is shared between GCC and GAS. */
19823
19824 static bfd_boolean
19825 mips_matching_cpu_name_p (const char *canonical, const char *given)
19826 {
19827 /* First see if the name matches exactly, or with a final "000"
19828 turned into "k". */
19829 if (mips_strict_matching_cpu_name_p (canonical, given))
19830 return TRUE;
19831
19832 /* If not, try comparing based on numerical designation alone.
19833 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19834 if (TOLOWER (*given) == 'r')
19835 given++;
19836 if (!ISDIGIT (*given))
19837 return FALSE;
19838
19839 /* Skip over some well-known prefixes in the canonical name,
19840 hoping to find a number there too. */
19841 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19842 canonical += 2;
19843 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19844 canonical += 2;
19845 else if (TOLOWER (canonical[0]) == 'r')
19846 canonical += 1;
19847
19848 return mips_strict_matching_cpu_name_p (canonical, given);
19849 }
19850
19851
19852 /* Parse an option that takes the name of a processor as its argument.
19853 OPTION is the name of the option and CPU_STRING is the argument.
19854 Return the corresponding processor enumeration if the CPU_STRING is
19855 recognized, otherwise report an error and return null.
19856
19857 A similar function exists in GCC. */
19858
19859 static const struct mips_cpu_info *
19860 mips_parse_cpu (const char *option, const char *cpu_string)
19861 {
19862 const struct mips_cpu_info *p;
19863
19864 /* 'from-abi' selects the most compatible architecture for the given
19865 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19866 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19867 version. Look first at the -mgp options, if given, otherwise base
19868 the choice on MIPS_DEFAULT_64BIT.
19869
19870 Treat NO_ABI like the EABIs. One reason to do this is that the
19871 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19872 architecture. This code picks MIPS I for 'mips' and MIPS III for
19873 'mips64', just as we did in the days before 'from-abi'. */
19874 if (strcasecmp (cpu_string, "from-abi") == 0)
19875 {
19876 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19877 return mips_cpu_info_from_isa (ISA_MIPS1);
19878
19879 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19880 return mips_cpu_info_from_isa (ISA_MIPS3);
19881
19882 if (file_mips_opts.gp >= 0)
19883 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
19884 ? ISA_MIPS1 : ISA_MIPS3);
19885
19886 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19887 ? ISA_MIPS3
19888 : ISA_MIPS1);
19889 }
19890
19891 /* 'default' has traditionally been a no-op. Probably not very useful. */
19892 if (strcasecmp (cpu_string, "default") == 0)
19893 return 0;
19894
19895 for (p = mips_cpu_info_table; p->name != 0; p++)
19896 if (mips_matching_cpu_name_p (p->name, cpu_string))
19897 return p;
19898
19899 as_bad (_("bad value (%s) for %s"), cpu_string, option);
19900 return 0;
19901 }
19902
19903 /* Return the canonical processor information for ISA (a member of the
19904 ISA_MIPS* enumeration). */
19905
19906 static const struct mips_cpu_info *
19907 mips_cpu_info_from_isa (int isa)
19908 {
19909 int i;
19910
19911 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19912 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19913 && isa == mips_cpu_info_table[i].isa)
19914 return (&mips_cpu_info_table[i]);
19915
19916 return NULL;
19917 }
19918
19919 static const struct mips_cpu_info *
19920 mips_cpu_info_from_arch (int arch)
19921 {
19922 int i;
19923
19924 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19925 if (arch == mips_cpu_info_table[i].cpu)
19926 return (&mips_cpu_info_table[i]);
19927
19928 return NULL;
19929 }
19930 \f
19931 static void
19932 show (FILE *stream, const char *string, int *col_p, int *first_p)
19933 {
19934 if (*first_p)
19935 {
19936 fprintf (stream, "%24s", "");
19937 *col_p = 24;
19938 }
19939 else
19940 {
19941 fprintf (stream, ", ");
19942 *col_p += 2;
19943 }
19944
19945 if (*col_p + strlen (string) > 72)
19946 {
19947 fprintf (stream, "\n%24s", "");
19948 *col_p = 24;
19949 }
19950
19951 fprintf (stream, "%s", string);
19952 *col_p += strlen (string);
19953
19954 *first_p = 0;
19955 }
19956
19957 void
19958 md_show_usage (FILE *stream)
19959 {
19960 int column, first;
19961 size_t i;
19962
19963 fprintf (stream, _("\
19964 MIPS options:\n\
19965 -EB generate big endian output\n\
19966 -EL generate little endian output\n\
19967 -g, -g2 do not remove unneeded NOPs or swap branches\n\
19968 -G NUM allow referencing objects up to NUM bytes\n\
19969 implicitly with the gp register [default 8]\n"));
19970 fprintf (stream, _("\
19971 -mips1 generate MIPS ISA I instructions\n\
19972 -mips2 generate MIPS ISA II instructions\n\
19973 -mips3 generate MIPS ISA III instructions\n\
19974 -mips4 generate MIPS ISA IV instructions\n\
19975 -mips5 generate MIPS ISA V instructions\n\
19976 -mips32 generate MIPS32 ISA instructions\n\
19977 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
19978 -mips32r3 generate MIPS32 release 3 ISA instructions\n\
19979 -mips32r5 generate MIPS32 release 5 ISA instructions\n\
19980 -mips32r6 generate MIPS32 release 6 ISA instructions\n\
19981 -mips64 generate MIPS64 ISA instructions\n\
19982 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
19983 -mips64r3 generate MIPS64 release 3 ISA instructions\n\
19984 -mips64r5 generate MIPS64 release 5 ISA instructions\n\
19985 -mips64r6 generate MIPS64 release 6 ISA instructions\n\
19986 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19987
19988 first = 1;
19989
19990 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19991 show (stream, mips_cpu_info_table[i].name, &column, &first);
19992 show (stream, "from-abi", &column, &first);
19993 fputc ('\n', stream);
19994
19995 fprintf (stream, _("\
19996 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19997 -no-mCPU don't generate code specific to CPU.\n\
19998 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19999
20000 first = 1;
20001
20002 show (stream, "3900", &column, &first);
20003 show (stream, "4010", &column, &first);
20004 show (stream, "4100", &column, &first);
20005 show (stream, "4650", &column, &first);
20006 fputc ('\n', stream);
20007
20008 fprintf (stream, _("\
20009 -mips16 generate mips16 instructions\n\
20010 -no-mips16 do not generate mips16 instructions\n"));
20011 fprintf (stream, _("\
20012 -mmips16e2 generate MIPS16e2 instructions\n\
20013 -mno-mips16e2 do not generate MIPS16e2 instructions\n"));
20014 fprintf (stream, _("\
20015 -mmicromips generate microMIPS instructions\n\
20016 -mno-micromips do not generate microMIPS instructions\n"));
20017 fprintf (stream, _("\
20018 -msmartmips generate smartmips instructions\n\
20019 -mno-smartmips do not generate smartmips instructions\n"));
20020 fprintf (stream, _("\
20021 -mdsp generate DSP instructions\n\
20022 -mno-dsp do not generate DSP instructions\n"));
20023 fprintf (stream, _("\
20024 -mdspr2 generate DSP R2 instructions\n\
20025 -mno-dspr2 do not generate DSP R2 instructions\n"));
20026 fprintf (stream, _("\
20027 -mdspr3 generate DSP R3 instructions\n\
20028 -mno-dspr3 do not generate DSP R3 instructions\n"));
20029 fprintf (stream, _("\
20030 -mmt generate MT instructions\n\
20031 -mno-mt do not generate MT instructions\n"));
20032 fprintf (stream, _("\
20033 -mmcu generate MCU instructions\n\
20034 -mno-mcu do not generate MCU instructions\n"));
20035 fprintf (stream, _("\
20036 -mmsa generate MSA instructions\n\
20037 -mno-msa do not generate MSA instructions\n"));
20038 fprintf (stream, _("\
20039 -mxpa generate eXtended Physical Address (XPA) instructions\n\
20040 -mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20041 fprintf (stream, _("\
20042 -mvirt generate Virtualization instructions\n\
20043 -mno-virt do not generate Virtualization instructions\n"));
20044 fprintf (stream, _("\
20045 -mcrc generate CRC instructions\n\
20046 -mno-crc do not generate CRC instructions\n"));
20047 fprintf (stream, _("\
20048 -mginv generate Global INValidate (GINV) instructions\n\
20049 -mno-ginv do not generate Global INValidate instructions\n"));
20050 fprintf (stream, _("\
20051 -mloongson-mmi generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20052 -mno-loongson-mmi do not generate Loongson MultiMedia extensions Instructions\n"));
20053 fprintf (stream, _("\
20054 -minsn32 only generate 32-bit microMIPS instructions\n\
20055 -mno-insn32 generate all microMIPS instructions\n"));
20056 fprintf (stream, _("\
20057 -mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20058 -mfix-loongson2f-nop work around Loongson2F NOP errata\n\
20059 -mfix-vr4120 work around certain VR4120 errata\n\
20060 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
20061 -mfix-24k insert a nop after ERET and DERET instructions\n\
20062 -mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
20063 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20064 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
20065 -msym32 assume all symbols have 32-bit values\n\
20066 -O0 do not remove unneeded NOPs, do not swap branches\n\
20067 -O, -O1 remove unneeded NOPs, do not swap branches\n\
20068 -O2 remove unneeded NOPs and swap branches\n\
20069 --trap, --no-break trap exception on div by 0 and mult overflow\n\
20070 --break, --no-trap break exception on div by 0 and mult overflow\n"));
20071 fprintf (stream, _("\
20072 -mhard-float allow floating-point instructions\n\
20073 -msoft-float do not allow floating-point instructions\n\
20074 -msingle-float only allow 32-bit floating-point operations\n\
20075 -mdouble-float allow 32-bit and 64-bit floating-point operations\n\
20076 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
20077 --[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
20078 -mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20079 -mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
20080 -mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20081
20082 first = 1;
20083
20084 show (stream, "legacy", &column, &first);
20085 show (stream, "2008", &column, &first);
20086
20087 fputc ('\n', stream);
20088
20089 fprintf (stream, _("\
20090 -KPIC, -call_shared generate SVR4 position independent code\n\
20091 -call_nonpic generate non-PIC code that can operate with DSOs\n\
20092 -mvxworks-pic generate VxWorks position independent code\n\
20093 -non_shared do not generate code that can operate with DSOs\n\
20094 -xgot assume a 32 bit GOT\n\
20095 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
20096 -mshared, -mno-shared disable/enable .cpload optimization for\n\
20097 position dependent (non shared) code\n\
20098 -mabi=ABI create ABI conformant object file for:\n"));
20099
20100 first = 1;
20101
20102 show (stream, "32", &column, &first);
20103 show (stream, "o64", &column, &first);
20104 show (stream, "n32", &column, &first);
20105 show (stream, "64", &column, &first);
20106 show (stream, "eabi", &column, &first);
20107
20108 fputc ('\n', stream);
20109
20110 fprintf (stream, _("\
20111 -32 create o32 ABI object file%s\n"),
20112 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20113 fprintf (stream, _("\
20114 -n32 create n32 ABI object file%s\n"),
20115 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20116 fprintf (stream, _("\
20117 -64 create 64 ABI object file%s\n"),
20118 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
20119 }
20120
20121 #ifdef TE_IRIX
20122 enum dwarf2_format
20123 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
20124 {
20125 if (HAVE_64BIT_SYMBOLS)
20126 return dwarf2_format_64bit_irix;
20127 else
20128 return dwarf2_format_32bit;
20129 }
20130 #endif
20131
20132 int
20133 mips_dwarf2_addr_size (void)
20134 {
20135 if (HAVE_64BIT_OBJECTS)
20136 return 8;
20137 else
20138 return 4;
20139 }
20140
20141 /* Standard calling conventions leave the CFA at SP on entry. */
20142 void
20143 mips_cfi_frame_initial_instructions (void)
20144 {
20145 cfi_add_CFA_def_cfa_register (SP);
20146 }
20147
20148 int
20149 tc_mips_regname_to_dw2regnum (char *regname)
20150 {
20151 unsigned int regnum = -1;
20152 unsigned int reg;
20153
20154 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20155 regnum = reg;
20156
20157 return regnum;
20158 }
20159
20160 /* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20161 Given a symbolic attribute NAME, return the proper integer value.
20162 Returns -1 if the attribute is not known. */
20163
20164 int
20165 mips_convert_symbolic_attribute (const char *name)
20166 {
20167 static const struct
20168 {
20169 const char * name;
20170 const int tag;
20171 }
20172 attribute_table[] =
20173 {
20174 #define T(tag) {#tag, tag}
20175 T (Tag_GNU_MIPS_ABI_FP),
20176 T (Tag_GNU_MIPS_ABI_MSA),
20177 #undef T
20178 };
20179 unsigned int i;
20180
20181 if (name == NULL)
20182 return -1;
20183
20184 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20185 if (streq (name, attribute_table[i].name))
20186 return attribute_table[i].tag;
20187
20188 return -1;
20189 }
20190
20191 void
20192 md_mips_end (void)
20193 {
20194 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20195
20196 mips_emit_delays ();
20197 if (cur_proc_ptr)
20198 as_warn (_("missing .end at end of assembly"));
20199
20200 /* Just in case no code was emitted, do the consistency check. */
20201 file_mips_check_options ();
20202
20203 /* Set a floating-point ABI if the user did not. */
20204 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20205 {
20206 /* Perform consistency checks on the floating-point ABI. */
20207 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20208 Tag_GNU_MIPS_ABI_FP);
20209 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20210 check_fpabi (fpabi);
20211 }
20212 else
20213 {
20214 /* Soft-float gets precedence over single-float, the two options should
20215 not be used together so this should not matter. */
20216 if (file_mips_opts.soft_float == 1)
20217 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20218 /* Single-float gets precedence over all double_float cases. */
20219 else if (file_mips_opts.single_float == 1)
20220 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20221 else
20222 {
20223 switch (file_mips_opts.fp)
20224 {
20225 case 32:
20226 if (file_mips_opts.gp == 32)
20227 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20228 break;
20229 case 0:
20230 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20231 break;
20232 case 64:
20233 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20234 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20235 else if (file_mips_opts.gp == 32)
20236 fpabi = Val_GNU_MIPS_ABI_FP_64;
20237 else
20238 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20239 break;
20240 }
20241 }
20242
20243 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20244 Tag_GNU_MIPS_ABI_FP, fpabi);
20245 }
20246 }
20247
20248 /* Returns the relocation type required for a particular CFI encoding. */
20249
20250 bfd_reloc_code_real_type
20251 mips_cfi_reloc_for_encoding (int encoding)
20252 {
20253 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20254 return BFD_RELOC_32_PCREL;
20255 else return BFD_RELOC_NONE;
20256 }
This page took 0.478487 seconds and 4 git commands to generate.