MIPS/GAS: Respect the `insn32' mode in branch relaxation
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright (C) 1993-2016 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 \f
947 /* The expansion of many macros depends on the type of symbol that
948 they refer to. For example, when generating position-dependent code,
949 a macro that refers to a symbol may have two different expansions,
950 one which uses GP-relative addresses and one which uses absolute
951 addresses. When generating SVR4-style PIC, a macro may have
952 different expansions for local and global symbols.
953
954 We handle these situations by generating both sequences and putting
955 them in variant frags. In position-dependent code, the first sequence
956 will be the GP-relative one and the second sequence will be the
957 absolute one. In SVR4 PIC, the first sequence will be for global
958 symbols and the second will be for local symbols.
959
960 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
961 SECOND are the lengths of the two sequences in bytes. These fields
962 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
963 the subtype has the following flags:
964
965 RELAX_USE_SECOND
966 Set if it has been decided that we should use the second
967 sequence instead of the first.
968
969 RELAX_SECOND_LONGER
970 Set in the first variant frag if the macro's second implementation
971 is longer than its first. This refers to the macro as a whole,
972 not an individual relaxation.
973
974 RELAX_NOMACRO
975 Set in the first variant frag if the macro appeared in a .set nomacro
976 block and if one alternative requires a warning but the other does not.
977
978 RELAX_DELAY_SLOT
979 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
980 delay slot.
981
982 RELAX_DELAY_SLOT_16BIT
983 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
984 16-bit instruction.
985
986 RELAX_DELAY_SLOT_SIZE_FIRST
987 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
988 the macro is of the wrong size for the branch delay slot.
989
990 RELAX_DELAY_SLOT_SIZE_SECOND
991 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
992 the macro is of the wrong size for the branch delay slot.
993
994 The frag's "opcode" points to the first fixup for relaxable code.
995
996 Relaxable macros are generated using a sequence such as:
997
998 relax_start (SYMBOL);
999 ... generate first expansion ...
1000 relax_switch ();
1001 ... generate second expansion ...
1002 relax_end ();
1003
1004 The code and fixups for the unwanted alternative are discarded
1005 by md_convert_frag. */
1006 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
1007
1008 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1009 #define RELAX_SECOND(X) ((X) & 0xff)
1010 #define RELAX_USE_SECOND 0x10000
1011 #define RELAX_SECOND_LONGER 0x20000
1012 #define RELAX_NOMACRO 0x40000
1013 #define RELAX_DELAY_SLOT 0x80000
1014 #define RELAX_DELAY_SLOT_16BIT 0x100000
1015 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
1016 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
1017
1018 /* Branch without likely bit. If label is out of range, we turn:
1019
1020 beq reg1, reg2, label
1021 delay slot
1022
1023 into
1024
1025 bne reg1, reg2, 0f
1026 nop
1027 j label
1028 0: delay slot
1029
1030 with the following opcode replacements:
1031
1032 beq <-> bne
1033 blez <-> bgtz
1034 bltz <-> bgez
1035 bc1f <-> bc1t
1036
1037 bltzal <-> bgezal (with jal label instead of j label)
1038
1039 Even though keeping the delay slot instruction in the delay slot of
1040 the branch would be more efficient, it would be very tricky to do
1041 correctly, because we'd have to introduce a variable frag *after*
1042 the delay slot instruction, and expand that instead. Let's do it
1043 the easy way for now, even if the branch-not-taken case now costs
1044 one additional instruction. Out-of-range branches are not supposed
1045 to be common, anyway.
1046
1047 Branch likely. If label is out of range, we turn:
1048
1049 beql reg1, reg2, label
1050 delay slot (annulled if branch not taken)
1051
1052 into
1053
1054 beql reg1, reg2, 1f
1055 nop
1056 beql $0, $0, 2f
1057 nop
1058 1: j[al] label
1059 delay slot (executed only if branch taken)
1060 2:
1061
1062 It would be possible to generate a shorter sequence by losing the
1063 likely bit, generating something like:
1064
1065 bne reg1, reg2, 0f
1066 nop
1067 j[al] label
1068 delay slot (executed only if branch taken)
1069 0:
1070
1071 beql -> bne
1072 bnel -> beq
1073 blezl -> bgtz
1074 bgtzl -> blez
1075 bltzl -> bgez
1076 bgezl -> bltz
1077 bc1fl -> bc1t
1078 bc1tl -> bc1f
1079
1080 bltzall -> bgezal (with jal label instead of j label)
1081 bgezall -> bltzal (ditto)
1082
1083
1084 but it's not clear that it would actually improve performance. */
1085 #define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1086 ((relax_substateT) \
1087 (0xc0000000 \
1088 | ((at) & 0x1f) \
1089 | ((toofar) ? 0x20 : 0) \
1090 | ((link) ? 0x40 : 0) \
1091 | ((likely) ? 0x80 : 0) \
1092 | ((uncond) ? 0x100 : 0)))
1093 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1094 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1095 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1096 #define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1097 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1098 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1099
1100 /* For mips16 code, we use an entirely different form of relaxation.
1101 mips16 supports two versions of most instructions which take
1102 immediate values: a small one which takes some small value, and a
1103 larger one which takes a 16 bit value. Since branches also follow
1104 this pattern, relaxing these values is required.
1105
1106 We can assemble both mips16 and normal MIPS code in a single
1107 object. Therefore, we need to support this type of relaxation at
1108 the same time that we support the relaxation described above. We
1109 use the high bit of the subtype field to distinguish these cases.
1110
1111 The information we store for this type of relaxation is the
1112 argument code found in the opcode file for this relocation, whether
1113 the user explicitly requested a small or extended form, and whether
1114 the relocation is in a jump or jal delay slot. That tells us the
1115 size of the value, and how it should be stored. We also store
1116 whether the fragment is considered to be extended or not. We also
1117 store whether this is known to be a branch to a different section,
1118 whether we have tried to relax this frag yet, and whether we have
1119 ever extended a PC relative fragment because of a shift count. */
1120 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1121 (0x80000000 \
1122 | ((type) & 0xff) \
1123 | ((small) ? 0x100 : 0) \
1124 | ((ext) ? 0x200 : 0) \
1125 | ((dslot) ? 0x400 : 0) \
1126 | ((jal_dslot) ? 0x800 : 0))
1127 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1128 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1129 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1130 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1131 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1132 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1133 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1134 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1135 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1136 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1137 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1138 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
1139
1140 /* For microMIPS code, we use relaxation similar to one we use for
1141 MIPS16 code. Some instructions that take immediate values support
1142 two encodings: a small one which takes some small value, and a
1143 larger one which takes a 16 bit value. As some branches also follow
1144 this pattern, relaxing these values is required.
1145
1146 We can assemble both microMIPS and normal MIPS code in a single
1147 object. Therefore, we need to support this type of relaxation at
1148 the same time that we support the relaxation described above. We
1149 use one of the high bits of the subtype field to distinguish these
1150 cases.
1151
1152 The information we store for this type of relaxation is the argument
1153 code found in the opcode file for this relocation, the register
1154 selected as the assembler temporary, whether in the 32-bit
1155 instruction mode, whether the branch is unconditional, whether it is
1156 compact, whether it stores the link address implicitly in $ra,
1157 whether relaxation of out-of-range 32-bit branches to a sequence of
1158 instructions is enabled, and whether the displacement of a branch is
1159 too large to fit as an immediate argument of a 16-bit and a 32-bit
1160 branch, respectively. */
1161 #define RELAX_MICROMIPS_ENCODE(type, at, insn32, \
1162 uncond, compact, link, \
1163 relax32, toofar16, toofar32) \
1164 (0x40000000 \
1165 | ((type) & 0xff) \
1166 | (((at) & 0x1f) << 8) \
1167 | ((insn32) ? 0x2000 : 0) \
1168 | ((uncond) ? 0x4000 : 0) \
1169 | ((compact) ? 0x8000 : 0) \
1170 | ((link) ? 0x10000 : 0) \
1171 | ((relax32) ? 0x20000 : 0) \
1172 | ((toofar16) ? 0x40000 : 0) \
1173 | ((toofar32) ? 0x80000 : 0))
1174 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1175 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1176 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1177 #define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
1178 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x4000) != 0)
1179 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x8000) != 0)
1180 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x10000) != 0)
1181 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x20000) != 0)
1182
1183 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x40000) != 0)
1184 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x40000)
1185 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x40000)
1186 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x80000) != 0)
1187 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x80000)
1188 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x80000)
1189
1190 /* Sign-extend 16-bit value X. */
1191 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1192
1193 /* Is the given value a sign-extended 32-bit value? */
1194 #define IS_SEXT_32BIT_NUM(x) \
1195 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1196 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1197
1198 /* Is the given value a sign-extended 16-bit value? */
1199 #define IS_SEXT_16BIT_NUM(x) \
1200 (((x) &~ (offsetT) 0x7fff) == 0 \
1201 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1202
1203 /* Is the given value a sign-extended 12-bit value? */
1204 #define IS_SEXT_12BIT_NUM(x) \
1205 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1206
1207 /* Is the given value a sign-extended 9-bit value? */
1208 #define IS_SEXT_9BIT_NUM(x) \
1209 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1210
1211 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
1212 #define IS_ZEXT_32BIT_NUM(x) \
1213 (((x) &~ (offsetT) 0xffffffff) == 0 \
1214 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1215
1216 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1217 SHIFT places. */
1218 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1219 (((STRUCT) >> (SHIFT)) & (MASK))
1220
1221 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
1222 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1223 (!(MICROMIPS) \
1224 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1225 : EXTRACT_BITS ((INSN).insn_opcode, \
1226 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1227 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1228 EXTRACT_BITS ((INSN).insn_opcode, \
1229 MIPS16OP_MASK_##FIELD, \
1230 MIPS16OP_SH_##FIELD)
1231
1232 /* The MIPS16 EXTEND opcode, shifted left 16 places. */
1233 #define MIPS16_EXTEND (0xf000U << 16)
1234 \f
1235 /* Whether or not we are emitting a branch-likely macro. */
1236 static bfd_boolean emit_branch_likely_macro = FALSE;
1237
1238 /* Global variables used when generating relaxable macros. See the
1239 comment above RELAX_ENCODE for more details about how relaxation
1240 is used. */
1241 static struct {
1242 /* 0 if we're not emitting a relaxable macro.
1243 1 if we're emitting the first of the two relaxation alternatives.
1244 2 if we're emitting the second alternative. */
1245 int sequence;
1246
1247 /* The first relaxable fixup in the current frag. (In other words,
1248 the first fixup that refers to relaxable code.) */
1249 fixS *first_fixup;
1250
1251 /* sizes[0] says how many bytes of the first alternative are stored in
1252 the current frag. Likewise sizes[1] for the second alternative. */
1253 unsigned int sizes[2];
1254
1255 /* The symbol on which the choice of sequence depends. */
1256 symbolS *symbol;
1257 } mips_relax;
1258 \f
1259 /* Global variables used to decide whether a macro needs a warning. */
1260 static struct {
1261 /* True if the macro is in a branch delay slot. */
1262 bfd_boolean delay_slot_p;
1263
1264 /* Set to the length in bytes required if the macro is in a delay slot
1265 that requires a specific length of instruction, otherwise zero. */
1266 unsigned int delay_slot_length;
1267
1268 /* For relaxable macros, sizes[0] is the length of the first alternative
1269 in bytes and sizes[1] is the length of the second alternative.
1270 For non-relaxable macros, both elements give the length of the
1271 macro in bytes. */
1272 unsigned int sizes[2];
1273
1274 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1275 instruction of the first alternative in bytes and first_insn_sizes[1]
1276 is the length of the first instruction of the second alternative.
1277 For non-relaxable macros, both elements give the length of the first
1278 instruction in bytes.
1279
1280 Set to zero if we haven't yet seen the first instruction. */
1281 unsigned int first_insn_sizes[2];
1282
1283 /* For relaxable macros, insns[0] is the number of instructions for the
1284 first alternative and insns[1] is the number of instructions for the
1285 second alternative.
1286
1287 For non-relaxable macros, both elements give the number of
1288 instructions for the macro. */
1289 unsigned int insns[2];
1290
1291 /* The first variant frag for this macro. */
1292 fragS *first_frag;
1293 } mips_macro_warning;
1294 \f
1295 /* Prototypes for static functions. */
1296
1297 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1298
1299 static void append_insn
1300 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1301 bfd_boolean expansionp);
1302 static void mips_no_prev_insn (void);
1303 static void macro_build (expressionS *, const char *, const char *, ...);
1304 static void mips16_macro_build
1305 (expressionS *, const char *, const char *, va_list *);
1306 static void load_register (int, expressionS *, int);
1307 static void macro_start (void);
1308 static void macro_end (void);
1309 static void macro (struct mips_cl_insn *ip, char *str);
1310 static void mips16_macro (struct mips_cl_insn * ip);
1311 static void mips_ip (char *str, struct mips_cl_insn * ip);
1312 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1313 static void mips16_immed
1314 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1315 unsigned int, unsigned long *);
1316 static size_t my_getSmallExpression
1317 (expressionS *, bfd_reloc_code_real_type *, char *);
1318 static void my_getExpression (expressionS *, char *);
1319 static void s_align (int);
1320 static void s_change_sec (int);
1321 static void s_change_section (int);
1322 static void s_cons (int);
1323 static void s_float_cons (int);
1324 static void s_mips_globl (int);
1325 static void s_option (int);
1326 static void s_mipsset (int);
1327 static void s_abicalls (int);
1328 static void s_cpload (int);
1329 static void s_cpsetup (int);
1330 static void s_cplocal (int);
1331 static void s_cprestore (int);
1332 static void s_cpreturn (int);
1333 static void s_dtprelword (int);
1334 static void s_dtpreldword (int);
1335 static void s_tprelword (int);
1336 static void s_tpreldword (int);
1337 static void s_gpvalue (int);
1338 static void s_gpword (int);
1339 static void s_gpdword (int);
1340 static void s_ehword (int);
1341 static void s_cpadd (int);
1342 static void s_insn (int);
1343 static void s_nan (int);
1344 static void s_module (int);
1345 static void s_mips_ent (int);
1346 static void s_mips_end (int);
1347 static void s_mips_frame (int);
1348 static void s_mips_mask (int reg_type);
1349 static void s_mips_stab (int);
1350 static void s_mips_weakext (int);
1351 static void s_mips_file (int);
1352 static void s_mips_loc (int);
1353 static bfd_boolean pic_need_relax (symbolS *, asection *);
1354 static int relaxed_branch_length (fragS *, asection *, int);
1355 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1356 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1357 static void file_mips_check_options (void);
1358
1359 /* Table and functions used to map between CPU/ISA names, and
1360 ISA levels, and CPU numbers. */
1361
1362 struct mips_cpu_info
1363 {
1364 const char *name; /* CPU or ISA name. */
1365 int flags; /* MIPS_CPU_* flags. */
1366 int ase; /* Set of ASEs implemented by the CPU. */
1367 int isa; /* ISA level. */
1368 int cpu; /* CPU number (default CPU if ISA). */
1369 };
1370
1371 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1372
1373 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1374 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1375 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1376 \f
1377 /* Command-line options. */
1378 const char *md_shortopts = "O::g::G:";
1379
1380 enum options
1381 {
1382 OPTION_MARCH = OPTION_MD_BASE,
1383 OPTION_MTUNE,
1384 OPTION_MIPS1,
1385 OPTION_MIPS2,
1386 OPTION_MIPS3,
1387 OPTION_MIPS4,
1388 OPTION_MIPS5,
1389 OPTION_MIPS32,
1390 OPTION_MIPS64,
1391 OPTION_MIPS32R2,
1392 OPTION_MIPS32R3,
1393 OPTION_MIPS32R5,
1394 OPTION_MIPS32R6,
1395 OPTION_MIPS64R2,
1396 OPTION_MIPS64R3,
1397 OPTION_MIPS64R5,
1398 OPTION_MIPS64R6,
1399 OPTION_MIPS16,
1400 OPTION_NO_MIPS16,
1401 OPTION_MIPS3D,
1402 OPTION_NO_MIPS3D,
1403 OPTION_MDMX,
1404 OPTION_NO_MDMX,
1405 OPTION_DSP,
1406 OPTION_NO_DSP,
1407 OPTION_MT,
1408 OPTION_NO_MT,
1409 OPTION_VIRT,
1410 OPTION_NO_VIRT,
1411 OPTION_MSA,
1412 OPTION_NO_MSA,
1413 OPTION_SMARTMIPS,
1414 OPTION_NO_SMARTMIPS,
1415 OPTION_DSPR2,
1416 OPTION_NO_DSPR2,
1417 OPTION_DSPR3,
1418 OPTION_NO_DSPR3,
1419 OPTION_EVA,
1420 OPTION_NO_EVA,
1421 OPTION_XPA,
1422 OPTION_NO_XPA,
1423 OPTION_MICROMIPS,
1424 OPTION_NO_MICROMIPS,
1425 OPTION_MCU,
1426 OPTION_NO_MCU,
1427 OPTION_COMPAT_ARCH_BASE,
1428 OPTION_M4650,
1429 OPTION_NO_M4650,
1430 OPTION_M4010,
1431 OPTION_NO_M4010,
1432 OPTION_M4100,
1433 OPTION_NO_M4100,
1434 OPTION_M3900,
1435 OPTION_NO_M3900,
1436 OPTION_M7000_HILO_FIX,
1437 OPTION_MNO_7000_HILO_FIX,
1438 OPTION_FIX_24K,
1439 OPTION_NO_FIX_24K,
1440 OPTION_FIX_RM7000,
1441 OPTION_NO_FIX_RM7000,
1442 OPTION_FIX_LOONGSON2F_JUMP,
1443 OPTION_NO_FIX_LOONGSON2F_JUMP,
1444 OPTION_FIX_LOONGSON2F_NOP,
1445 OPTION_NO_FIX_LOONGSON2F_NOP,
1446 OPTION_FIX_VR4120,
1447 OPTION_NO_FIX_VR4120,
1448 OPTION_FIX_VR4130,
1449 OPTION_NO_FIX_VR4130,
1450 OPTION_FIX_CN63XXP1,
1451 OPTION_NO_FIX_CN63XXP1,
1452 OPTION_TRAP,
1453 OPTION_BREAK,
1454 OPTION_EB,
1455 OPTION_EL,
1456 OPTION_FP32,
1457 OPTION_GP32,
1458 OPTION_CONSTRUCT_FLOATS,
1459 OPTION_NO_CONSTRUCT_FLOATS,
1460 OPTION_FP64,
1461 OPTION_FPXX,
1462 OPTION_GP64,
1463 OPTION_RELAX_BRANCH,
1464 OPTION_NO_RELAX_BRANCH,
1465 OPTION_INSN32,
1466 OPTION_NO_INSN32,
1467 OPTION_MSHARED,
1468 OPTION_MNO_SHARED,
1469 OPTION_MSYM32,
1470 OPTION_MNO_SYM32,
1471 OPTION_SOFT_FLOAT,
1472 OPTION_HARD_FLOAT,
1473 OPTION_SINGLE_FLOAT,
1474 OPTION_DOUBLE_FLOAT,
1475 OPTION_32,
1476 OPTION_CALL_SHARED,
1477 OPTION_CALL_NONPIC,
1478 OPTION_NON_SHARED,
1479 OPTION_XGOT,
1480 OPTION_MABI,
1481 OPTION_N32,
1482 OPTION_64,
1483 OPTION_MDEBUG,
1484 OPTION_NO_MDEBUG,
1485 OPTION_PDR,
1486 OPTION_NO_PDR,
1487 OPTION_MVXWORKS_PIC,
1488 OPTION_NAN,
1489 OPTION_ODD_SPREG,
1490 OPTION_NO_ODD_SPREG,
1491 OPTION_END_OF_ENUM
1492 };
1493
1494 struct option md_longopts[] =
1495 {
1496 /* Options which specify architecture. */
1497 {"march", required_argument, NULL, OPTION_MARCH},
1498 {"mtune", required_argument, NULL, OPTION_MTUNE},
1499 {"mips0", no_argument, NULL, OPTION_MIPS1},
1500 {"mips1", no_argument, NULL, OPTION_MIPS1},
1501 {"mips2", no_argument, NULL, OPTION_MIPS2},
1502 {"mips3", no_argument, NULL, OPTION_MIPS3},
1503 {"mips4", no_argument, NULL, OPTION_MIPS4},
1504 {"mips5", no_argument, NULL, OPTION_MIPS5},
1505 {"mips32", no_argument, NULL, OPTION_MIPS32},
1506 {"mips64", no_argument, NULL, OPTION_MIPS64},
1507 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1508 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1509 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
1510 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
1511 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1512 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1513 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
1514 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
1515
1516 /* Options which specify Application Specific Extensions (ASEs). */
1517 {"mips16", no_argument, NULL, OPTION_MIPS16},
1518 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1519 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1520 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1521 {"mdmx", no_argument, NULL, OPTION_MDMX},
1522 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1523 {"mdsp", no_argument, NULL, OPTION_DSP},
1524 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1525 {"mmt", no_argument, NULL, OPTION_MT},
1526 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1527 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1528 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1529 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1530 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1531 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1532 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
1533 {"meva", no_argument, NULL, OPTION_EVA},
1534 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1535 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1536 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1537 {"mmcu", no_argument, NULL, OPTION_MCU},
1538 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1539 {"mvirt", no_argument, NULL, OPTION_VIRT},
1540 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1541 {"mmsa", no_argument, NULL, OPTION_MSA},
1542 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
1543 {"mxpa", no_argument, NULL, OPTION_XPA},
1544 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
1545
1546 /* Old-style architecture options. Don't add more of these. */
1547 {"m4650", no_argument, NULL, OPTION_M4650},
1548 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1549 {"m4010", no_argument, NULL, OPTION_M4010},
1550 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1551 {"m4100", no_argument, NULL, OPTION_M4100},
1552 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1553 {"m3900", no_argument, NULL, OPTION_M3900},
1554 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1555
1556 /* Options which enable bug fixes. */
1557 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1558 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1559 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1560 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1561 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1562 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1563 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1564 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1565 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1566 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1567 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1568 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1569 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1570 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1571 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
1572 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1573 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1574
1575 /* Miscellaneous options. */
1576 {"trap", no_argument, NULL, OPTION_TRAP},
1577 {"no-break", no_argument, NULL, OPTION_TRAP},
1578 {"break", no_argument, NULL, OPTION_BREAK},
1579 {"no-trap", no_argument, NULL, OPTION_BREAK},
1580 {"EB", no_argument, NULL, OPTION_EB},
1581 {"EL", no_argument, NULL, OPTION_EL},
1582 {"mfp32", no_argument, NULL, OPTION_FP32},
1583 {"mgp32", no_argument, NULL, OPTION_GP32},
1584 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1585 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1586 {"mfp64", no_argument, NULL, OPTION_FP64},
1587 {"mfpxx", no_argument, NULL, OPTION_FPXX},
1588 {"mgp64", no_argument, NULL, OPTION_GP64},
1589 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1590 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1591 {"minsn32", no_argument, NULL, OPTION_INSN32},
1592 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1593 {"mshared", no_argument, NULL, OPTION_MSHARED},
1594 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1595 {"msym32", no_argument, NULL, OPTION_MSYM32},
1596 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1597 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1598 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1599 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1600 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1601 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1602 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
1603
1604 /* Strictly speaking this next option is ELF specific,
1605 but we allow it for other ports as well in order to
1606 make testing easier. */
1607 {"32", no_argument, NULL, OPTION_32},
1608
1609 /* ELF-specific options. */
1610 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1611 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1612 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1613 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1614 {"xgot", no_argument, NULL, OPTION_XGOT},
1615 {"mabi", required_argument, NULL, OPTION_MABI},
1616 {"n32", no_argument, NULL, OPTION_N32},
1617 {"64", no_argument, NULL, OPTION_64},
1618 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1619 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1620 {"mpdr", no_argument, NULL, OPTION_PDR},
1621 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1622 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1623 {"mnan", required_argument, NULL, OPTION_NAN},
1624
1625 {NULL, no_argument, NULL, 0}
1626 };
1627 size_t md_longopts_size = sizeof (md_longopts);
1628 \f
1629 /* Information about either an Application Specific Extension or an
1630 optional architecture feature that, for simplicity, we treat in the
1631 same way as an ASE. */
1632 struct mips_ase
1633 {
1634 /* The name of the ASE, used in both the command-line and .set options. */
1635 const char *name;
1636
1637 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1638 and 64-bit architectures, the flags here refer to the subset that
1639 is available on both. */
1640 unsigned int flags;
1641
1642 /* The ASE_* flag used for instructions that are available on 64-bit
1643 architectures but that are not included in FLAGS. */
1644 unsigned int flags64;
1645
1646 /* The command-line options that turn the ASE on and off. */
1647 int option_on;
1648 int option_off;
1649
1650 /* The minimum required architecture revisions for MIPS32, MIPS64,
1651 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1652 int mips32_rev;
1653 int mips64_rev;
1654 int micromips32_rev;
1655 int micromips64_rev;
1656
1657 /* The architecture where the ASE was removed or -1 if the extension has not
1658 been removed. */
1659 int rem_rev;
1660 };
1661
1662 /* A table of all supported ASEs. */
1663 static const struct mips_ase mips_ases[] = {
1664 { "dsp", ASE_DSP, ASE_DSP64,
1665 OPTION_DSP, OPTION_NO_DSP,
1666 2, 2, 2, 2,
1667 -1 },
1668
1669 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1670 OPTION_DSPR2, OPTION_NO_DSPR2,
1671 2, 2, 2, 2,
1672 -1 },
1673
1674 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1675 OPTION_DSPR3, OPTION_NO_DSPR3,
1676 6, 6, -1, -1,
1677 -1 },
1678
1679 { "eva", ASE_EVA, 0,
1680 OPTION_EVA, OPTION_NO_EVA,
1681 2, 2, 2, 2,
1682 -1 },
1683
1684 { "mcu", ASE_MCU, 0,
1685 OPTION_MCU, OPTION_NO_MCU,
1686 2, 2, 2, 2,
1687 -1 },
1688
1689 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1690 { "mdmx", ASE_MDMX, 0,
1691 OPTION_MDMX, OPTION_NO_MDMX,
1692 -1, 1, -1, -1,
1693 6 },
1694
1695 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1696 { "mips3d", ASE_MIPS3D, 0,
1697 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1698 2, 1, -1, -1,
1699 6 },
1700
1701 { "mt", ASE_MT, 0,
1702 OPTION_MT, OPTION_NO_MT,
1703 2, 2, -1, -1,
1704 -1 },
1705
1706 { "smartmips", ASE_SMARTMIPS, 0,
1707 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1708 1, -1, -1, -1,
1709 6 },
1710
1711 { "virt", ASE_VIRT, ASE_VIRT64,
1712 OPTION_VIRT, OPTION_NO_VIRT,
1713 2, 2, 2, 2,
1714 -1 },
1715
1716 { "msa", ASE_MSA, ASE_MSA64,
1717 OPTION_MSA, OPTION_NO_MSA,
1718 2, 2, 2, 2,
1719 -1 },
1720
1721 { "xpa", ASE_XPA, 0,
1722 OPTION_XPA, OPTION_NO_XPA,
1723 2, 2, -1, -1,
1724 -1 },
1725 };
1726
1727 /* The set of ASEs that require -mfp64. */
1728 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
1729
1730 /* Groups of ASE_* flags that represent different revisions of an ASE. */
1731 static const unsigned int mips_ase_groups[] = {
1732 ASE_DSP | ASE_DSPR2 | ASE_DSPR3
1733 };
1734 \f
1735 /* Pseudo-op table.
1736
1737 The following pseudo-ops from the Kane and Heinrich MIPS book
1738 should be defined here, but are currently unsupported: .alias,
1739 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1740
1741 The following pseudo-ops from the Kane and Heinrich MIPS book are
1742 specific to the type of debugging information being generated, and
1743 should be defined by the object format: .aent, .begin, .bend,
1744 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1745 .vreg.
1746
1747 The following pseudo-ops from the Kane and Heinrich MIPS book are
1748 not MIPS CPU specific, but are also not specific to the object file
1749 format. This file is probably the best place to define them, but
1750 they are not currently supported: .asm0, .endr, .lab, .struct. */
1751
1752 static const pseudo_typeS mips_pseudo_table[] =
1753 {
1754 /* MIPS specific pseudo-ops. */
1755 {"option", s_option, 0},
1756 {"set", s_mipsset, 0},
1757 {"rdata", s_change_sec, 'r'},
1758 {"sdata", s_change_sec, 's'},
1759 {"livereg", s_ignore, 0},
1760 {"abicalls", s_abicalls, 0},
1761 {"cpload", s_cpload, 0},
1762 {"cpsetup", s_cpsetup, 0},
1763 {"cplocal", s_cplocal, 0},
1764 {"cprestore", s_cprestore, 0},
1765 {"cpreturn", s_cpreturn, 0},
1766 {"dtprelword", s_dtprelword, 0},
1767 {"dtpreldword", s_dtpreldword, 0},
1768 {"tprelword", s_tprelword, 0},
1769 {"tpreldword", s_tpreldword, 0},
1770 {"gpvalue", s_gpvalue, 0},
1771 {"gpword", s_gpword, 0},
1772 {"gpdword", s_gpdword, 0},
1773 {"ehword", s_ehword, 0},
1774 {"cpadd", s_cpadd, 0},
1775 {"insn", s_insn, 0},
1776 {"nan", s_nan, 0},
1777 {"module", s_module, 0},
1778
1779 /* Relatively generic pseudo-ops that happen to be used on MIPS
1780 chips. */
1781 {"asciiz", stringer, 8 + 1},
1782 {"bss", s_change_sec, 'b'},
1783 {"err", s_err, 0},
1784 {"half", s_cons, 1},
1785 {"dword", s_cons, 3},
1786 {"weakext", s_mips_weakext, 0},
1787 {"origin", s_org, 0},
1788 {"repeat", s_rept, 0},
1789
1790 /* For MIPS this is non-standard, but we define it for consistency. */
1791 {"sbss", s_change_sec, 'B'},
1792
1793 /* These pseudo-ops are defined in read.c, but must be overridden
1794 here for one reason or another. */
1795 {"align", s_align, 0},
1796 {"byte", s_cons, 0},
1797 {"data", s_change_sec, 'd'},
1798 {"double", s_float_cons, 'd'},
1799 {"float", s_float_cons, 'f'},
1800 {"globl", s_mips_globl, 0},
1801 {"global", s_mips_globl, 0},
1802 {"hword", s_cons, 1},
1803 {"int", s_cons, 2},
1804 {"long", s_cons, 2},
1805 {"octa", s_cons, 4},
1806 {"quad", s_cons, 3},
1807 {"section", s_change_section, 0},
1808 {"short", s_cons, 1},
1809 {"single", s_float_cons, 'f'},
1810 {"stabd", s_mips_stab, 'd'},
1811 {"stabn", s_mips_stab, 'n'},
1812 {"stabs", s_mips_stab, 's'},
1813 {"text", s_change_sec, 't'},
1814 {"word", s_cons, 2},
1815
1816 { "extern", ecoff_directive_extern, 0},
1817
1818 { NULL, NULL, 0 },
1819 };
1820
1821 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1822 {
1823 /* These pseudo-ops should be defined by the object file format.
1824 However, a.out doesn't support them, so we have versions here. */
1825 {"aent", s_mips_ent, 1},
1826 {"bgnb", s_ignore, 0},
1827 {"end", s_mips_end, 0},
1828 {"endb", s_ignore, 0},
1829 {"ent", s_mips_ent, 0},
1830 {"file", s_mips_file, 0},
1831 {"fmask", s_mips_mask, 'F'},
1832 {"frame", s_mips_frame, 0},
1833 {"loc", s_mips_loc, 0},
1834 {"mask", s_mips_mask, 'R'},
1835 {"verstamp", s_ignore, 0},
1836 { NULL, NULL, 0 },
1837 };
1838
1839 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1840 purpose of the `.dc.a' internal pseudo-op. */
1841
1842 int
1843 mips_address_bytes (void)
1844 {
1845 file_mips_check_options ();
1846 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1847 }
1848
1849 extern void pop_insert (const pseudo_typeS *);
1850
1851 void
1852 mips_pop_insert (void)
1853 {
1854 pop_insert (mips_pseudo_table);
1855 if (! ECOFF_DEBUGGING)
1856 pop_insert (mips_nonecoff_pseudo_table);
1857 }
1858 \f
1859 /* Symbols labelling the current insn. */
1860
1861 struct insn_label_list
1862 {
1863 struct insn_label_list *next;
1864 symbolS *label;
1865 };
1866
1867 static struct insn_label_list *free_insn_labels;
1868 #define label_list tc_segment_info_data.labels
1869
1870 static void mips_clear_insn_labels (void);
1871 static void mips_mark_labels (void);
1872 static void mips_compressed_mark_labels (void);
1873
1874 static inline void
1875 mips_clear_insn_labels (void)
1876 {
1877 struct insn_label_list **pl;
1878 segment_info_type *si;
1879
1880 if (now_seg)
1881 {
1882 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1883 ;
1884
1885 si = seg_info (now_seg);
1886 *pl = si->label_list;
1887 si->label_list = NULL;
1888 }
1889 }
1890
1891 /* Mark instruction labels in MIPS16/microMIPS mode. */
1892
1893 static inline void
1894 mips_mark_labels (void)
1895 {
1896 if (HAVE_CODE_COMPRESSION)
1897 mips_compressed_mark_labels ();
1898 }
1899 \f
1900 static char *expr_end;
1901
1902 /* An expression in a macro instruction. This is set by mips_ip and
1903 mips16_ip and when populated is always an O_constant. */
1904
1905 static expressionS imm_expr;
1906
1907 /* The relocatable field in an instruction and the relocs associated
1908 with it. These variables are used for instructions like LUI and
1909 JAL as well as true offsets. They are also used for address
1910 operands in macros. */
1911
1912 static expressionS offset_expr;
1913 static bfd_reloc_code_real_type offset_reloc[3]
1914 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1915
1916 /* This is set to the resulting size of the instruction to be produced
1917 by mips16_ip if an explicit extension is used or by mips_ip if an
1918 explicit size is supplied. */
1919
1920 static unsigned int forced_insn_length;
1921
1922 /* True if we are assembling an instruction. All dot symbols defined during
1923 this time should be treated as code labels. */
1924
1925 static bfd_boolean mips_assembling_insn;
1926
1927 /* The pdr segment for per procedure frame/regmask info. Not used for
1928 ECOFF debugging. */
1929
1930 static segT pdr_seg;
1931
1932 /* The default target format to use. */
1933
1934 #if defined (TE_FreeBSD)
1935 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1936 #elif defined (TE_TMIPS)
1937 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1938 #else
1939 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1940 #endif
1941
1942 const char *
1943 mips_target_format (void)
1944 {
1945 switch (OUTPUT_FLAVOR)
1946 {
1947 case bfd_target_elf_flavour:
1948 #ifdef TE_VXWORKS
1949 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1950 return (target_big_endian
1951 ? "elf32-bigmips-vxworks"
1952 : "elf32-littlemips-vxworks");
1953 #endif
1954 return (target_big_endian
1955 ? (HAVE_64BIT_OBJECTS
1956 ? ELF_TARGET ("elf64-", "big")
1957 : (HAVE_NEWABI
1958 ? ELF_TARGET ("elf32-n", "big")
1959 : ELF_TARGET ("elf32-", "big")))
1960 : (HAVE_64BIT_OBJECTS
1961 ? ELF_TARGET ("elf64-", "little")
1962 : (HAVE_NEWABI
1963 ? ELF_TARGET ("elf32-n", "little")
1964 : ELF_TARGET ("elf32-", "little"))));
1965 default:
1966 abort ();
1967 return NULL;
1968 }
1969 }
1970
1971 /* Return the ISA revision that is currently in use, or 0 if we are
1972 generating code for MIPS V or below. */
1973
1974 static int
1975 mips_isa_rev (void)
1976 {
1977 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
1978 return 2;
1979
1980 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
1981 return 3;
1982
1983 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
1984 return 5;
1985
1986 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
1987 return 6;
1988
1989 /* microMIPS implies revision 2 or above. */
1990 if (mips_opts.micromips)
1991 return 2;
1992
1993 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
1994 return 1;
1995
1996 return 0;
1997 }
1998
1999 /* Return the mask of all ASEs that are revisions of those in FLAGS. */
2000
2001 static unsigned int
2002 mips_ase_mask (unsigned int flags)
2003 {
2004 unsigned int i;
2005
2006 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2007 if (flags & mips_ase_groups[i])
2008 flags |= mips_ase_groups[i];
2009 return flags;
2010 }
2011
2012 /* Check whether the current ISA supports ASE. Issue a warning if
2013 appropriate. */
2014
2015 static void
2016 mips_check_isa_supports_ase (const struct mips_ase *ase)
2017 {
2018 const char *base;
2019 int min_rev, size;
2020 static unsigned int warned_isa;
2021 static unsigned int warned_fp32;
2022
2023 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2024 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2025 else
2026 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2027 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2028 && (warned_isa & ase->flags) != ase->flags)
2029 {
2030 warned_isa |= ase->flags;
2031 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2032 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2033 if (min_rev < 0)
2034 as_warn (_("the %d-bit %s architecture does not support the"
2035 " `%s' extension"), size, base, ase->name);
2036 else
2037 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
2038 ase->name, base, size, min_rev);
2039 }
2040 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2041 && (warned_isa & ase->flags) != ase->flags)
2042 {
2043 warned_isa |= ase->flags;
2044 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2045 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2046 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2047 ase->name, base, size, ase->rem_rev);
2048 }
2049
2050 if ((ase->flags & FP64_ASES)
2051 && mips_opts.fp != 64
2052 && (warned_fp32 & ase->flags) != ase->flags)
2053 {
2054 warned_fp32 |= ase->flags;
2055 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
2056 }
2057 }
2058
2059 /* Check all enabled ASEs to see whether they are supported by the
2060 chosen architecture. */
2061
2062 static void
2063 mips_check_isa_supports_ases (void)
2064 {
2065 unsigned int i, mask;
2066
2067 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2068 {
2069 mask = mips_ase_mask (mips_ases[i].flags);
2070 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2071 mips_check_isa_supports_ase (&mips_ases[i]);
2072 }
2073 }
2074
2075 /* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2076 that were affected. */
2077
2078 static unsigned int
2079 mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2080 bfd_boolean enabled_p)
2081 {
2082 unsigned int mask;
2083
2084 mask = mips_ase_mask (ase->flags);
2085 opts->ase &= ~mask;
2086 if (enabled_p)
2087 opts->ase |= ase->flags;
2088 return mask;
2089 }
2090
2091 /* Return the ASE called NAME, or null if none. */
2092
2093 static const struct mips_ase *
2094 mips_lookup_ase (const char *name)
2095 {
2096 unsigned int i;
2097
2098 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2099 if (strcmp (name, mips_ases[i].name) == 0)
2100 return &mips_ases[i];
2101 return NULL;
2102 }
2103
2104 /* Return the length of a microMIPS instruction in bytes. If bits of
2105 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2106 otherwise it is a 32-bit instruction. */
2107
2108 static inline unsigned int
2109 micromips_insn_length (const struct mips_opcode *mo)
2110 {
2111 return (mo->mask >> 16) == 0 ? 2 : 4;
2112 }
2113
2114 /* Return the length of MIPS16 instruction OPCODE. */
2115
2116 static inline unsigned int
2117 mips16_opcode_length (unsigned long opcode)
2118 {
2119 return (opcode >> 16) == 0 ? 2 : 4;
2120 }
2121
2122 /* Return the length of instruction INSN. */
2123
2124 static inline unsigned int
2125 insn_length (const struct mips_cl_insn *insn)
2126 {
2127 if (mips_opts.micromips)
2128 return micromips_insn_length (insn->insn_mo);
2129 else if (mips_opts.mips16)
2130 return mips16_opcode_length (insn->insn_opcode);
2131 else
2132 return 4;
2133 }
2134
2135 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2136
2137 static void
2138 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2139 {
2140 size_t i;
2141
2142 insn->insn_mo = mo;
2143 insn->insn_opcode = mo->match;
2144 insn->frag = NULL;
2145 insn->where = 0;
2146 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2147 insn->fixp[i] = NULL;
2148 insn->fixed_p = (mips_opts.noreorder > 0);
2149 insn->noreorder_p = (mips_opts.noreorder > 0);
2150 insn->mips16_absolute_jump_p = 0;
2151 insn->complete_p = 0;
2152 insn->cleared_p = 0;
2153 }
2154
2155 /* Get a list of all the operands in INSN. */
2156
2157 static const struct mips_operand_array *
2158 insn_operands (const struct mips_cl_insn *insn)
2159 {
2160 if (insn->insn_mo >= &mips_opcodes[0]
2161 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2162 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2163
2164 if (insn->insn_mo >= &mips16_opcodes[0]
2165 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2166 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2167
2168 if (insn->insn_mo >= &micromips_opcodes[0]
2169 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2170 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2171
2172 abort ();
2173 }
2174
2175 /* Get a description of operand OPNO of INSN. */
2176
2177 static const struct mips_operand *
2178 insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2179 {
2180 const struct mips_operand_array *operands;
2181
2182 operands = insn_operands (insn);
2183 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2184 abort ();
2185 return operands->operand[opno];
2186 }
2187
2188 /* Install UVAL as the value of OPERAND in INSN. */
2189
2190 static inline void
2191 insn_insert_operand (struct mips_cl_insn *insn,
2192 const struct mips_operand *operand, unsigned int uval)
2193 {
2194 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2195 }
2196
2197 /* Extract the value of OPERAND from INSN. */
2198
2199 static inline unsigned
2200 insn_extract_operand (const struct mips_cl_insn *insn,
2201 const struct mips_operand *operand)
2202 {
2203 return mips_extract_operand (operand, insn->insn_opcode);
2204 }
2205
2206 /* Record the current MIPS16/microMIPS mode in now_seg. */
2207
2208 static void
2209 mips_record_compressed_mode (void)
2210 {
2211 segment_info_type *si;
2212
2213 si = seg_info (now_seg);
2214 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2215 si->tc_segment_info_data.mips16 = mips_opts.mips16;
2216 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2217 si->tc_segment_info_data.micromips = mips_opts.micromips;
2218 }
2219
2220 /* Read a standard MIPS instruction from BUF. */
2221
2222 static unsigned long
2223 read_insn (char *buf)
2224 {
2225 if (target_big_endian)
2226 return bfd_getb32 ((bfd_byte *) buf);
2227 else
2228 return bfd_getl32 ((bfd_byte *) buf);
2229 }
2230
2231 /* Write standard MIPS instruction INSN to BUF. Return a pointer to
2232 the next byte. */
2233
2234 static char *
2235 write_insn (char *buf, unsigned int insn)
2236 {
2237 md_number_to_chars (buf, insn, 4);
2238 return buf + 4;
2239 }
2240
2241 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2242 has length LENGTH. */
2243
2244 static unsigned long
2245 read_compressed_insn (char *buf, unsigned int length)
2246 {
2247 unsigned long insn;
2248 unsigned int i;
2249
2250 insn = 0;
2251 for (i = 0; i < length; i += 2)
2252 {
2253 insn <<= 16;
2254 if (target_big_endian)
2255 insn |= bfd_getb16 ((char *) buf);
2256 else
2257 insn |= bfd_getl16 ((char *) buf);
2258 buf += 2;
2259 }
2260 return insn;
2261 }
2262
2263 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2264 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2265
2266 static char *
2267 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2268 {
2269 unsigned int i;
2270
2271 for (i = 0; i < length; i += 2)
2272 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2273 return buf + length;
2274 }
2275
2276 /* Install INSN at the location specified by its "frag" and "where" fields. */
2277
2278 static void
2279 install_insn (const struct mips_cl_insn *insn)
2280 {
2281 char *f = insn->frag->fr_literal + insn->where;
2282 if (HAVE_CODE_COMPRESSION)
2283 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2284 else
2285 write_insn (f, insn->insn_opcode);
2286 mips_record_compressed_mode ();
2287 }
2288
2289 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2290 and install the opcode in the new location. */
2291
2292 static void
2293 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2294 {
2295 size_t i;
2296
2297 insn->frag = frag;
2298 insn->where = where;
2299 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2300 if (insn->fixp[i] != NULL)
2301 {
2302 insn->fixp[i]->fx_frag = frag;
2303 insn->fixp[i]->fx_where = where;
2304 }
2305 install_insn (insn);
2306 }
2307
2308 /* Add INSN to the end of the output. */
2309
2310 static void
2311 add_fixed_insn (struct mips_cl_insn *insn)
2312 {
2313 char *f = frag_more (insn_length (insn));
2314 move_insn (insn, frag_now, f - frag_now->fr_literal);
2315 }
2316
2317 /* Start a variant frag and move INSN to the start of the variant part,
2318 marking it as fixed. The other arguments are as for frag_var. */
2319
2320 static void
2321 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2322 relax_substateT subtype, symbolS *symbol, offsetT offset)
2323 {
2324 frag_grow (max_chars);
2325 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2326 insn->fixed_p = 1;
2327 frag_var (rs_machine_dependent, max_chars, var,
2328 subtype, symbol, offset, NULL);
2329 }
2330
2331 /* Insert N copies of INSN into the history buffer, starting at
2332 position FIRST. Neither FIRST nor N need to be clipped. */
2333
2334 static void
2335 insert_into_history (unsigned int first, unsigned int n,
2336 const struct mips_cl_insn *insn)
2337 {
2338 if (mips_relax.sequence != 2)
2339 {
2340 unsigned int i;
2341
2342 for (i = ARRAY_SIZE (history); i-- > first;)
2343 if (i >= first + n)
2344 history[i] = history[i - n];
2345 else
2346 history[i] = *insn;
2347 }
2348 }
2349
2350 /* Clear the error in insn_error. */
2351
2352 static void
2353 clear_insn_error (void)
2354 {
2355 memset (&insn_error, 0, sizeof (insn_error));
2356 }
2357
2358 /* Possibly record error message MSG for the current instruction.
2359 If the error is about a particular argument, ARGNUM is the 1-based
2360 number of that argument, otherwise it is 0. FORMAT is the format
2361 of MSG. Return true if MSG was used, false if the current message
2362 was kept. */
2363
2364 static bfd_boolean
2365 set_insn_error_format (int argnum, enum mips_insn_error_format format,
2366 const char *msg)
2367 {
2368 if (argnum == 0)
2369 {
2370 /* Give priority to errors against specific arguments, and to
2371 the first whole-instruction message. */
2372 if (insn_error.msg)
2373 return FALSE;
2374 }
2375 else
2376 {
2377 /* Keep insn_error if it is against a later argument. */
2378 if (argnum < insn_error.min_argnum)
2379 return FALSE;
2380
2381 /* If both errors are against the same argument but are different,
2382 give up on reporting a specific error for this argument.
2383 See the comment about mips_insn_error for details. */
2384 if (argnum == insn_error.min_argnum
2385 && insn_error.msg
2386 && strcmp (insn_error.msg, msg) != 0)
2387 {
2388 insn_error.msg = 0;
2389 insn_error.min_argnum += 1;
2390 return FALSE;
2391 }
2392 }
2393 insn_error.min_argnum = argnum;
2394 insn_error.format = format;
2395 insn_error.msg = msg;
2396 return TRUE;
2397 }
2398
2399 /* Record an instruction error with no % format fields. ARGNUM and MSG are
2400 as for set_insn_error_format. */
2401
2402 static void
2403 set_insn_error (int argnum, const char *msg)
2404 {
2405 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2406 }
2407
2408 /* Record an instruction error with one %d field I. ARGNUM and MSG are
2409 as for set_insn_error_format. */
2410
2411 static void
2412 set_insn_error_i (int argnum, const char *msg, int i)
2413 {
2414 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2415 insn_error.u.i = i;
2416 }
2417
2418 /* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2419 are as for set_insn_error_format. */
2420
2421 static void
2422 set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2423 {
2424 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2425 {
2426 insn_error.u.ss[0] = s1;
2427 insn_error.u.ss[1] = s2;
2428 }
2429 }
2430
2431 /* Report the error in insn_error, which is against assembly code STR. */
2432
2433 static void
2434 report_insn_error (const char *str)
2435 {
2436 const char *msg = concat (insn_error.msg, " `%s'", NULL);
2437
2438 switch (insn_error.format)
2439 {
2440 case ERR_FMT_PLAIN:
2441 as_bad (msg, str);
2442 break;
2443
2444 case ERR_FMT_I:
2445 as_bad (msg, insn_error.u.i, str);
2446 break;
2447
2448 case ERR_FMT_SS:
2449 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2450 break;
2451 }
2452
2453 free ((char *) msg);
2454 }
2455
2456 /* Initialize vr4120_conflicts. There is a bit of duplication here:
2457 the idea is to make it obvious at a glance that each errata is
2458 included. */
2459
2460 static void
2461 init_vr4120_conflicts (void)
2462 {
2463 #define CONFLICT(FIRST, SECOND) \
2464 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2465
2466 /* Errata 21 - [D]DIV[U] after [D]MACC */
2467 CONFLICT (MACC, DIV);
2468 CONFLICT (DMACC, DIV);
2469
2470 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2471 CONFLICT (DMULT, DMULT);
2472 CONFLICT (DMULT, DMACC);
2473 CONFLICT (DMACC, DMULT);
2474 CONFLICT (DMACC, DMACC);
2475
2476 /* Errata 24 - MT{LO,HI} after [D]MACC */
2477 CONFLICT (MACC, MTHILO);
2478 CONFLICT (DMACC, MTHILO);
2479
2480 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2481 instruction is executed immediately after a MACC or DMACC
2482 instruction, the result of [either instruction] is incorrect." */
2483 CONFLICT (MACC, MULT);
2484 CONFLICT (MACC, DMULT);
2485 CONFLICT (DMACC, MULT);
2486 CONFLICT (DMACC, DMULT);
2487
2488 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2489 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2490 DDIV or DDIVU instruction, the result of the MACC or
2491 DMACC instruction is incorrect.". */
2492 CONFLICT (DMULT, MACC);
2493 CONFLICT (DMULT, DMACC);
2494 CONFLICT (DIV, MACC);
2495 CONFLICT (DIV, DMACC);
2496
2497 #undef CONFLICT
2498 }
2499
2500 struct regname {
2501 const char *name;
2502 unsigned int num;
2503 };
2504
2505 #define RNUM_MASK 0x00000ff
2506 #define RTYPE_MASK 0x0ffff00
2507 #define RTYPE_NUM 0x0000100
2508 #define RTYPE_FPU 0x0000200
2509 #define RTYPE_FCC 0x0000400
2510 #define RTYPE_VEC 0x0000800
2511 #define RTYPE_GP 0x0001000
2512 #define RTYPE_CP0 0x0002000
2513 #define RTYPE_PC 0x0004000
2514 #define RTYPE_ACC 0x0008000
2515 #define RTYPE_CCC 0x0010000
2516 #define RTYPE_VI 0x0020000
2517 #define RTYPE_VF 0x0040000
2518 #define RTYPE_R5900_I 0x0080000
2519 #define RTYPE_R5900_Q 0x0100000
2520 #define RTYPE_R5900_R 0x0200000
2521 #define RTYPE_R5900_ACC 0x0400000
2522 #define RTYPE_MSA 0x0800000
2523 #define RWARN 0x8000000
2524
2525 #define GENERIC_REGISTER_NUMBERS \
2526 {"$0", RTYPE_NUM | 0}, \
2527 {"$1", RTYPE_NUM | 1}, \
2528 {"$2", RTYPE_NUM | 2}, \
2529 {"$3", RTYPE_NUM | 3}, \
2530 {"$4", RTYPE_NUM | 4}, \
2531 {"$5", RTYPE_NUM | 5}, \
2532 {"$6", RTYPE_NUM | 6}, \
2533 {"$7", RTYPE_NUM | 7}, \
2534 {"$8", RTYPE_NUM | 8}, \
2535 {"$9", RTYPE_NUM | 9}, \
2536 {"$10", RTYPE_NUM | 10}, \
2537 {"$11", RTYPE_NUM | 11}, \
2538 {"$12", RTYPE_NUM | 12}, \
2539 {"$13", RTYPE_NUM | 13}, \
2540 {"$14", RTYPE_NUM | 14}, \
2541 {"$15", RTYPE_NUM | 15}, \
2542 {"$16", RTYPE_NUM | 16}, \
2543 {"$17", RTYPE_NUM | 17}, \
2544 {"$18", RTYPE_NUM | 18}, \
2545 {"$19", RTYPE_NUM | 19}, \
2546 {"$20", RTYPE_NUM | 20}, \
2547 {"$21", RTYPE_NUM | 21}, \
2548 {"$22", RTYPE_NUM | 22}, \
2549 {"$23", RTYPE_NUM | 23}, \
2550 {"$24", RTYPE_NUM | 24}, \
2551 {"$25", RTYPE_NUM | 25}, \
2552 {"$26", RTYPE_NUM | 26}, \
2553 {"$27", RTYPE_NUM | 27}, \
2554 {"$28", RTYPE_NUM | 28}, \
2555 {"$29", RTYPE_NUM | 29}, \
2556 {"$30", RTYPE_NUM | 30}, \
2557 {"$31", RTYPE_NUM | 31}
2558
2559 #define FPU_REGISTER_NAMES \
2560 {"$f0", RTYPE_FPU | 0}, \
2561 {"$f1", RTYPE_FPU | 1}, \
2562 {"$f2", RTYPE_FPU | 2}, \
2563 {"$f3", RTYPE_FPU | 3}, \
2564 {"$f4", RTYPE_FPU | 4}, \
2565 {"$f5", RTYPE_FPU | 5}, \
2566 {"$f6", RTYPE_FPU | 6}, \
2567 {"$f7", RTYPE_FPU | 7}, \
2568 {"$f8", RTYPE_FPU | 8}, \
2569 {"$f9", RTYPE_FPU | 9}, \
2570 {"$f10", RTYPE_FPU | 10}, \
2571 {"$f11", RTYPE_FPU | 11}, \
2572 {"$f12", RTYPE_FPU | 12}, \
2573 {"$f13", RTYPE_FPU | 13}, \
2574 {"$f14", RTYPE_FPU | 14}, \
2575 {"$f15", RTYPE_FPU | 15}, \
2576 {"$f16", RTYPE_FPU | 16}, \
2577 {"$f17", RTYPE_FPU | 17}, \
2578 {"$f18", RTYPE_FPU | 18}, \
2579 {"$f19", RTYPE_FPU | 19}, \
2580 {"$f20", RTYPE_FPU | 20}, \
2581 {"$f21", RTYPE_FPU | 21}, \
2582 {"$f22", RTYPE_FPU | 22}, \
2583 {"$f23", RTYPE_FPU | 23}, \
2584 {"$f24", RTYPE_FPU | 24}, \
2585 {"$f25", RTYPE_FPU | 25}, \
2586 {"$f26", RTYPE_FPU | 26}, \
2587 {"$f27", RTYPE_FPU | 27}, \
2588 {"$f28", RTYPE_FPU | 28}, \
2589 {"$f29", RTYPE_FPU | 29}, \
2590 {"$f30", RTYPE_FPU | 30}, \
2591 {"$f31", RTYPE_FPU | 31}
2592
2593 #define FPU_CONDITION_CODE_NAMES \
2594 {"$fcc0", RTYPE_FCC | 0}, \
2595 {"$fcc1", RTYPE_FCC | 1}, \
2596 {"$fcc2", RTYPE_FCC | 2}, \
2597 {"$fcc3", RTYPE_FCC | 3}, \
2598 {"$fcc4", RTYPE_FCC | 4}, \
2599 {"$fcc5", RTYPE_FCC | 5}, \
2600 {"$fcc6", RTYPE_FCC | 6}, \
2601 {"$fcc7", RTYPE_FCC | 7}
2602
2603 #define COPROC_CONDITION_CODE_NAMES \
2604 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2605 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2606 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2607 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2608 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2609 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2610 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2611 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2612
2613 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2614 {"$a4", RTYPE_GP | 8}, \
2615 {"$a5", RTYPE_GP | 9}, \
2616 {"$a6", RTYPE_GP | 10}, \
2617 {"$a7", RTYPE_GP | 11}, \
2618 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2619 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2620 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2621 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2622 {"$t0", RTYPE_GP | 12}, \
2623 {"$t1", RTYPE_GP | 13}, \
2624 {"$t2", RTYPE_GP | 14}, \
2625 {"$t3", RTYPE_GP | 15}
2626
2627 #define O32_SYMBOLIC_REGISTER_NAMES \
2628 {"$t0", RTYPE_GP | 8}, \
2629 {"$t1", RTYPE_GP | 9}, \
2630 {"$t2", RTYPE_GP | 10}, \
2631 {"$t3", RTYPE_GP | 11}, \
2632 {"$t4", RTYPE_GP | 12}, \
2633 {"$t5", RTYPE_GP | 13}, \
2634 {"$t6", RTYPE_GP | 14}, \
2635 {"$t7", RTYPE_GP | 15}, \
2636 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2637 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2638 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2639 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2640
2641 /* Remaining symbolic register names */
2642 #define SYMBOLIC_REGISTER_NAMES \
2643 {"$zero", RTYPE_GP | 0}, \
2644 {"$at", RTYPE_GP | 1}, \
2645 {"$AT", RTYPE_GP | 1}, \
2646 {"$v0", RTYPE_GP | 2}, \
2647 {"$v1", RTYPE_GP | 3}, \
2648 {"$a0", RTYPE_GP | 4}, \
2649 {"$a1", RTYPE_GP | 5}, \
2650 {"$a2", RTYPE_GP | 6}, \
2651 {"$a3", RTYPE_GP | 7}, \
2652 {"$s0", RTYPE_GP | 16}, \
2653 {"$s1", RTYPE_GP | 17}, \
2654 {"$s2", RTYPE_GP | 18}, \
2655 {"$s3", RTYPE_GP | 19}, \
2656 {"$s4", RTYPE_GP | 20}, \
2657 {"$s5", RTYPE_GP | 21}, \
2658 {"$s6", RTYPE_GP | 22}, \
2659 {"$s7", RTYPE_GP | 23}, \
2660 {"$t8", RTYPE_GP | 24}, \
2661 {"$t9", RTYPE_GP | 25}, \
2662 {"$k0", RTYPE_GP | 26}, \
2663 {"$kt0", RTYPE_GP | 26}, \
2664 {"$k1", RTYPE_GP | 27}, \
2665 {"$kt1", RTYPE_GP | 27}, \
2666 {"$gp", RTYPE_GP | 28}, \
2667 {"$sp", RTYPE_GP | 29}, \
2668 {"$s8", RTYPE_GP | 30}, \
2669 {"$fp", RTYPE_GP | 30}, \
2670 {"$ra", RTYPE_GP | 31}
2671
2672 #define MIPS16_SPECIAL_REGISTER_NAMES \
2673 {"$pc", RTYPE_PC | 0}
2674
2675 #define MDMX_VECTOR_REGISTER_NAMES \
2676 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2677 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2678 {"$v2", RTYPE_VEC | 2}, \
2679 {"$v3", RTYPE_VEC | 3}, \
2680 {"$v4", RTYPE_VEC | 4}, \
2681 {"$v5", RTYPE_VEC | 5}, \
2682 {"$v6", RTYPE_VEC | 6}, \
2683 {"$v7", RTYPE_VEC | 7}, \
2684 {"$v8", RTYPE_VEC | 8}, \
2685 {"$v9", RTYPE_VEC | 9}, \
2686 {"$v10", RTYPE_VEC | 10}, \
2687 {"$v11", RTYPE_VEC | 11}, \
2688 {"$v12", RTYPE_VEC | 12}, \
2689 {"$v13", RTYPE_VEC | 13}, \
2690 {"$v14", RTYPE_VEC | 14}, \
2691 {"$v15", RTYPE_VEC | 15}, \
2692 {"$v16", RTYPE_VEC | 16}, \
2693 {"$v17", RTYPE_VEC | 17}, \
2694 {"$v18", RTYPE_VEC | 18}, \
2695 {"$v19", RTYPE_VEC | 19}, \
2696 {"$v20", RTYPE_VEC | 20}, \
2697 {"$v21", RTYPE_VEC | 21}, \
2698 {"$v22", RTYPE_VEC | 22}, \
2699 {"$v23", RTYPE_VEC | 23}, \
2700 {"$v24", RTYPE_VEC | 24}, \
2701 {"$v25", RTYPE_VEC | 25}, \
2702 {"$v26", RTYPE_VEC | 26}, \
2703 {"$v27", RTYPE_VEC | 27}, \
2704 {"$v28", RTYPE_VEC | 28}, \
2705 {"$v29", RTYPE_VEC | 29}, \
2706 {"$v30", RTYPE_VEC | 30}, \
2707 {"$v31", RTYPE_VEC | 31}
2708
2709 #define R5900_I_NAMES \
2710 {"$I", RTYPE_R5900_I | 0}
2711
2712 #define R5900_Q_NAMES \
2713 {"$Q", RTYPE_R5900_Q | 0}
2714
2715 #define R5900_R_NAMES \
2716 {"$R", RTYPE_R5900_R | 0}
2717
2718 #define R5900_ACC_NAMES \
2719 {"$ACC", RTYPE_R5900_ACC | 0 }
2720
2721 #define MIPS_DSP_ACCUMULATOR_NAMES \
2722 {"$ac0", RTYPE_ACC | 0}, \
2723 {"$ac1", RTYPE_ACC | 1}, \
2724 {"$ac2", RTYPE_ACC | 2}, \
2725 {"$ac3", RTYPE_ACC | 3}
2726
2727 static const struct regname reg_names[] = {
2728 GENERIC_REGISTER_NUMBERS,
2729 FPU_REGISTER_NAMES,
2730 FPU_CONDITION_CODE_NAMES,
2731 COPROC_CONDITION_CODE_NAMES,
2732
2733 /* The $txx registers depends on the abi,
2734 these will be added later into the symbol table from
2735 one of the tables below once mips_abi is set after
2736 parsing of arguments from the command line. */
2737 SYMBOLIC_REGISTER_NAMES,
2738
2739 MIPS16_SPECIAL_REGISTER_NAMES,
2740 MDMX_VECTOR_REGISTER_NAMES,
2741 R5900_I_NAMES,
2742 R5900_Q_NAMES,
2743 R5900_R_NAMES,
2744 R5900_ACC_NAMES,
2745 MIPS_DSP_ACCUMULATOR_NAMES,
2746 {0, 0}
2747 };
2748
2749 static const struct regname reg_names_o32[] = {
2750 O32_SYMBOLIC_REGISTER_NAMES,
2751 {0, 0}
2752 };
2753
2754 static const struct regname reg_names_n32n64[] = {
2755 N32N64_SYMBOLIC_REGISTER_NAMES,
2756 {0, 0}
2757 };
2758
2759 /* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2760 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2761 of these register symbols, return the associated vector register,
2762 otherwise return SYMVAL itself. */
2763
2764 static unsigned int
2765 mips_prefer_vec_regno (unsigned int symval)
2766 {
2767 if ((symval & -2) == (RTYPE_GP | 2))
2768 return RTYPE_VEC | (symval & 1);
2769 return symval;
2770 }
2771
2772 /* Return true if string [S, E) is a valid register name, storing its
2773 symbol value in *SYMVAL_PTR if so. */
2774
2775 static bfd_boolean
2776 mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
2777 {
2778 char save_c;
2779 symbolS *symbol;
2780
2781 /* Terminate name. */
2782 save_c = *e;
2783 *e = '\0';
2784
2785 /* Look up the name. */
2786 symbol = symbol_find (s);
2787 *e = save_c;
2788
2789 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2790 return FALSE;
2791
2792 *symval_ptr = S_GET_VALUE (symbol);
2793 return TRUE;
2794 }
2795
2796 /* Return true if the string at *SPTR is a valid register name. Allow it
2797 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2798 is nonnull.
2799
2800 When returning true, move *SPTR past the register, store the
2801 register's symbol value in *SYMVAL_PTR and the channel mask in
2802 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2803 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2804 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2805
2806 static bfd_boolean
2807 mips_parse_register (char **sptr, unsigned int *symval_ptr,
2808 unsigned int *channels_ptr)
2809 {
2810 char *s, *e, *m;
2811 const char *q;
2812 unsigned int channels, symval, bit;
2813
2814 /* Find end of name. */
2815 s = e = *sptr;
2816 if (is_name_beginner (*e))
2817 ++e;
2818 while (is_part_of_name (*e))
2819 ++e;
2820
2821 channels = 0;
2822 if (!mips_parse_register_1 (s, e, &symval))
2823 {
2824 if (!channels_ptr)
2825 return FALSE;
2826
2827 /* Eat characters from the end of the string that are valid
2828 channel suffixes. The preceding register must be $ACC or
2829 end with a digit, so there is no ambiguity. */
2830 bit = 1;
2831 m = e;
2832 for (q = "wzyx"; *q; q++, bit <<= 1)
2833 if (m > s && m[-1] == *q)
2834 {
2835 --m;
2836 channels |= bit;
2837 }
2838
2839 if (channels == 0
2840 || !mips_parse_register_1 (s, m, &symval)
2841 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2842 return FALSE;
2843 }
2844
2845 *sptr = e;
2846 *symval_ptr = symval;
2847 if (channels_ptr)
2848 *channels_ptr = channels;
2849 return TRUE;
2850 }
2851
2852 /* Check if SPTR points at a valid register specifier according to TYPES.
2853 If so, then return 1, advance S to consume the specifier and store
2854 the register's number in REGNOP, otherwise return 0. */
2855
2856 static int
2857 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2858 {
2859 unsigned int regno;
2860
2861 if (mips_parse_register (s, &regno, NULL))
2862 {
2863 if (types & RTYPE_VEC)
2864 regno = mips_prefer_vec_regno (regno);
2865 if (regno & types)
2866 regno &= RNUM_MASK;
2867 else
2868 regno = ~0;
2869 }
2870 else
2871 {
2872 if (types & RWARN)
2873 as_warn (_("unrecognized register name `%s'"), *s);
2874 regno = ~0;
2875 }
2876 if (regnop)
2877 *regnop = regno;
2878 return regno <= RNUM_MASK;
2879 }
2880
2881 /* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2882 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2883
2884 static char *
2885 mips_parse_vu0_channels (char *s, unsigned int *channels)
2886 {
2887 unsigned int i;
2888
2889 *channels = 0;
2890 for (i = 0; i < 4; i++)
2891 if (*s == "xyzw"[i])
2892 {
2893 *channels |= 1 << (3 - i);
2894 ++s;
2895 }
2896 return s;
2897 }
2898
2899 /* Token types for parsed operand lists. */
2900 enum mips_operand_token_type {
2901 /* A plain register, e.g. $f2. */
2902 OT_REG,
2903
2904 /* A 4-bit XYZW channel mask. */
2905 OT_CHANNELS,
2906
2907 /* A constant vector index, e.g. [1]. */
2908 OT_INTEGER_INDEX,
2909
2910 /* A register vector index, e.g. [$2]. */
2911 OT_REG_INDEX,
2912
2913 /* A continuous range of registers, e.g. $s0-$s4. */
2914 OT_REG_RANGE,
2915
2916 /* A (possibly relocated) expression. */
2917 OT_INTEGER,
2918
2919 /* A floating-point value. */
2920 OT_FLOAT,
2921
2922 /* A single character. This can be '(', ')' or ',', but '(' only appears
2923 before OT_REGs. */
2924 OT_CHAR,
2925
2926 /* A doubled character, either "--" or "++". */
2927 OT_DOUBLE_CHAR,
2928
2929 /* The end of the operand list. */
2930 OT_END
2931 };
2932
2933 /* A parsed operand token. */
2934 struct mips_operand_token
2935 {
2936 /* The type of token. */
2937 enum mips_operand_token_type type;
2938 union
2939 {
2940 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
2941 unsigned int regno;
2942
2943 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
2944 unsigned int channels;
2945
2946 /* The integer value of an OT_INTEGER_INDEX. */
2947 addressT index;
2948
2949 /* The two register symbol values involved in an OT_REG_RANGE. */
2950 struct {
2951 unsigned int regno1;
2952 unsigned int regno2;
2953 } reg_range;
2954
2955 /* The value of an OT_INTEGER. The value is represented as an
2956 expression and the relocation operators that were applied to
2957 that expression. The reloc entries are BFD_RELOC_UNUSED if no
2958 relocation operators were used. */
2959 struct {
2960 expressionS value;
2961 bfd_reloc_code_real_type relocs[3];
2962 } integer;
2963
2964 /* The binary data for an OT_FLOAT constant, and the number of bytes
2965 in the constant. */
2966 struct {
2967 unsigned char data[8];
2968 int length;
2969 } flt;
2970
2971 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
2972 char ch;
2973 } u;
2974 };
2975
2976 /* An obstack used to construct lists of mips_operand_tokens. */
2977 static struct obstack mips_operand_tokens;
2978
2979 /* Give TOKEN type TYPE and add it to mips_operand_tokens. */
2980
2981 static void
2982 mips_add_token (struct mips_operand_token *token,
2983 enum mips_operand_token_type type)
2984 {
2985 token->type = type;
2986 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
2987 }
2988
2989 /* Check whether S is '(' followed by a register name. Add OT_CHAR
2990 and OT_REG tokens for them if so, and return a pointer to the first
2991 unconsumed character. Return null otherwise. */
2992
2993 static char *
2994 mips_parse_base_start (char *s)
2995 {
2996 struct mips_operand_token token;
2997 unsigned int regno, channels;
2998 bfd_boolean decrement_p;
2999
3000 if (*s != '(')
3001 return 0;
3002
3003 ++s;
3004 SKIP_SPACE_TABS (s);
3005
3006 /* Only match "--" as part of a base expression. In other contexts "--X"
3007 is a double negative. */
3008 decrement_p = (s[0] == '-' && s[1] == '-');
3009 if (decrement_p)
3010 {
3011 s += 2;
3012 SKIP_SPACE_TABS (s);
3013 }
3014
3015 /* Allow a channel specifier because that leads to better error messages
3016 than treating something like "$vf0x++" as an expression. */
3017 if (!mips_parse_register (&s, &regno, &channels))
3018 return 0;
3019
3020 token.u.ch = '(';
3021 mips_add_token (&token, OT_CHAR);
3022
3023 if (decrement_p)
3024 {
3025 token.u.ch = '-';
3026 mips_add_token (&token, OT_DOUBLE_CHAR);
3027 }
3028
3029 token.u.regno = regno;
3030 mips_add_token (&token, OT_REG);
3031
3032 if (channels)
3033 {
3034 token.u.channels = channels;
3035 mips_add_token (&token, OT_CHANNELS);
3036 }
3037
3038 /* For consistency, only match "++" as part of base expressions too. */
3039 SKIP_SPACE_TABS (s);
3040 if (s[0] == '+' && s[1] == '+')
3041 {
3042 s += 2;
3043 token.u.ch = '+';
3044 mips_add_token (&token, OT_DOUBLE_CHAR);
3045 }
3046
3047 return s;
3048 }
3049
3050 /* Parse one or more tokens from S. Return a pointer to the first
3051 unconsumed character on success. Return null if an error was found
3052 and store the error text in insn_error. FLOAT_FORMAT is as for
3053 mips_parse_arguments. */
3054
3055 static char *
3056 mips_parse_argument_token (char *s, char float_format)
3057 {
3058 char *end, *save_in;
3059 const char *err;
3060 unsigned int regno1, regno2, channels;
3061 struct mips_operand_token token;
3062
3063 /* First look for "($reg", since we want to treat that as an
3064 OT_CHAR and OT_REG rather than an expression. */
3065 end = mips_parse_base_start (s);
3066 if (end)
3067 return end;
3068
3069 /* Handle other characters that end up as OT_CHARs. */
3070 if (*s == ')' || *s == ',')
3071 {
3072 token.u.ch = *s;
3073 mips_add_token (&token, OT_CHAR);
3074 ++s;
3075 return s;
3076 }
3077
3078 /* Handle tokens that start with a register. */
3079 if (mips_parse_register (&s, &regno1, &channels))
3080 {
3081 if (channels)
3082 {
3083 /* A register and a VU0 channel suffix. */
3084 token.u.regno = regno1;
3085 mips_add_token (&token, OT_REG);
3086
3087 token.u.channels = channels;
3088 mips_add_token (&token, OT_CHANNELS);
3089 return s;
3090 }
3091
3092 SKIP_SPACE_TABS (s);
3093 if (*s == '-')
3094 {
3095 /* A register range. */
3096 ++s;
3097 SKIP_SPACE_TABS (s);
3098 if (!mips_parse_register (&s, &regno2, NULL))
3099 {
3100 set_insn_error (0, _("invalid register range"));
3101 return 0;
3102 }
3103
3104 token.u.reg_range.regno1 = regno1;
3105 token.u.reg_range.regno2 = regno2;
3106 mips_add_token (&token, OT_REG_RANGE);
3107 return s;
3108 }
3109
3110 /* Add the register itself. */
3111 token.u.regno = regno1;
3112 mips_add_token (&token, OT_REG);
3113
3114 /* Check for a vector index. */
3115 if (*s == '[')
3116 {
3117 ++s;
3118 SKIP_SPACE_TABS (s);
3119 if (mips_parse_register (&s, &token.u.regno, NULL))
3120 mips_add_token (&token, OT_REG_INDEX);
3121 else
3122 {
3123 expressionS element;
3124
3125 my_getExpression (&element, s);
3126 if (element.X_op != O_constant)
3127 {
3128 set_insn_error (0, _("vector element must be constant"));
3129 return 0;
3130 }
3131 s = expr_end;
3132 token.u.index = element.X_add_number;
3133 mips_add_token (&token, OT_INTEGER_INDEX);
3134 }
3135 SKIP_SPACE_TABS (s);
3136 if (*s != ']')
3137 {
3138 set_insn_error (0, _("missing `]'"));
3139 return 0;
3140 }
3141 ++s;
3142 }
3143 return s;
3144 }
3145
3146 if (float_format)
3147 {
3148 /* First try to treat expressions as floats. */
3149 save_in = input_line_pointer;
3150 input_line_pointer = s;
3151 err = md_atof (float_format, (char *) token.u.flt.data,
3152 &token.u.flt.length);
3153 end = input_line_pointer;
3154 input_line_pointer = save_in;
3155 if (err && *err)
3156 {
3157 set_insn_error (0, err);
3158 return 0;
3159 }
3160 if (s != end)
3161 {
3162 mips_add_token (&token, OT_FLOAT);
3163 return end;
3164 }
3165 }
3166
3167 /* Treat everything else as an integer expression. */
3168 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3169 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3170 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3171 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3172 s = expr_end;
3173 mips_add_token (&token, OT_INTEGER);
3174 return s;
3175 }
3176
3177 /* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3178 if expressions should be treated as 32-bit floating-point constants,
3179 'd' if they should be treated as 64-bit floating-point constants,
3180 or 0 if they should be treated as integer expressions (the usual case).
3181
3182 Return a list of tokens on success, otherwise return 0. The caller
3183 must obstack_free the list after use. */
3184
3185 static struct mips_operand_token *
3186 mips_parse_arguments (char *s, char float_format)
3187 {
3188 struct mips_operand_token token;
3189
3190 SKIP_SPACE_TABS (s);
3191 while (*s)
3192 {
3193 s = mips_parse_argument_token (s, float_format);
3194 if (!s)
3195 {
3196 obstack_free (&mips_operand_tokens,
3197 obstack_finish (&mips_operand_tokens));
3198 return 0;
3199 }
3200 SKIP_SPACE_TABS (s);
3201 }
3202 mips_add_token (&token, OT_END);
3203 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
3204 }
3205
3206 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3207 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
3208
3209 static bfd_boolean
3210 is_opcode_valid (const struct mips_opcode *mo)
3211 {
3212 int isa = mips_opts.isa;
3213 int ase = mips_opts.ase;
3214 int fp_s, fp_d;
3215 unsigned int i;
3216
3217 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
3218 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3219 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3220 ase |= mips_ases[i].flags64;
3221
3222 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
3223 return FALSE;
3224
3225 /* Check whether the instruction or macro requires single-precision or
3226 double-precision floating-point support. Note that this information is
3227 stored differently in the opcode table for insns and macros. */
3228 if (mo->pinfo == INSN_MACRO)
3229 {
3230 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3231 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3232 }
3233 else
3234 {
3235 fp_s = mo->pinfo & FP_S;
3236 fp_d = mo->pinfo & FP_D;
3237 }
3238
3239 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3240 return FALSE;
3241
3242 if (fp_s && mips_opts.soft_float)
3243 return FALSE;
3244
3245 return TRUE;
3246 }
3247
3248 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
3249 selected ISA and architecture. */
3250
3251 static bfd_boolean
3252 is_opcode_valid_16 (const struct mips_opcode *mo)
3253 {
3254 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
3255 }
3256
3257 /* Return TRUE if the size of the microMIPS opcode MO matches one
3258 explicitly requested. Always TRUE in the standard MIPS mode. */
3259
3260 static bfd_boolean
3261 is_size_valid (const struct mips_opcode *mo)
3262 {
3263 if (!mips_opts.micromips)
3264 return TRUE;
3265
3266 if (mips_opts.insn32)
3267 {
3268 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3269 return FALSE;
3270 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3271 return FALSE;
3272 }
3273 if (!forced_insn_length)
3274 return TRUE;
3275 if (mo->pinfo == INSN_MACRO)
3276 return FALSE;
3277 return forced_insn_length == micromips_insn_length (mo);
3278 }
3279
3280 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
3281 of the preceding instruction. Always TRUE in the standard MIPS mode.
3282
3283 We don't accept macros in 16-bit delay slots to avoid a case where
3284 a macro expansion fails because it relies on a preceding 32-bit real
3285 instruction to have matched and does not handle the operands correctly.
3286 The only macros that may expand to 16-bit instructions are JAL that
3287 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3288 and BGT (that likewise cannot be placed in a delay slot) that decay to
3289 a NOP. In all these cases the macros precede any corresponding real
3290 instruction definitions in the opcode table, so they will match in the
3291 second pass where the size of the delay slot is ignored and therefore
3292 produce correct code. */
3293
3294 static bfd_boolean
3295 is_delay_slot_valid (const struct mips_opcode *mo)
3296 {
3297 if (!mips_opts.micromips)
3298 return TRUE;
3299
3300 if (mo->pinfo == INSN_MACRO)
3301 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
3302 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3303 && micromips_insn_length (mo) != 4)
3304 return FALSE;
3305 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3306 && micromips_insn_length (mo) != 2)
3307 return FALSE;
3308
3309 return TRUE;
3310 }
3311
3312 /* For consistency checking, verify that all bits of OPCODE are specified
3313 either by the match/mask part of the instruction definition, or by the
3314 operand list. Also build up a list of operands in OPERANDS.
3315
3316 INSN_BITS says which bits of the instruction are significant.
3317 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3318 provides the mips_operand description of each operand. DECODE_OPERAND
3319 is null for MIPS16 instructions. */
3320
3321 static int
3322 validate_mips_insn (const struct mips_opcode *opcode,
3323 unsigned long insn_bits,
3324 const struct mips_operand *(*decode_operand) (const char *),
3325 struct mips_operand_array *operands)
3326 {
3327 const char *s;
3328 unsigned long used_bits, doubled, undefined, opno, mask;
3329 const struct mips_operand *operand;
3330
3331 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3332 if ((mask & opcode->match) != opcode->match)
3333 {
3334 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3335 opcode->name, opcode->args);
3336 return 0;
3337 }
3338 used_bits = 0;
3339 opno = 0;
3340 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3341 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
3342 for (s = opcode->args; *s; ++s)
3343 switch (*s)
3344 {
3345 case ',':
3346 case '(':
3347 case ')':
3348 break;
3349
3350 case '#':
3351 s++;
3352 break;
3353
3354 default:
3355 if (!decode_operand)
3356 operand = decode_mips16_operand (*s, FALSE);
3357 else
3358 operand = decode_operand (s);
3359 if (!operand && opcode->pinfo != INSN_MACRO)
3360 {
3361 as_bad (_("internal: unknown operand type: %s %s"),
3362 opcode->name, opcode->args);
3363 return 0;
3364 }
3365 gas_assert (opno < MAX_OPERANDS);
3366 operands->operand[opno] = operand;
3367 if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
3368 {
3369 used_bits = mips_insert_operand (operand, used_bits, -1);
3370 if (operand->type == OP_MDMX_IMM_REG)
3371 /* Bit 5 is the format selector (OB vs QH). The opcode table
3372 has separate entries for each format. */
3373 used_bits &= ~(1 << (operand->lsb + 5));
3374 if (operand->type == OP_ENTRY_EXIT_LIST)
3375 used_bits &= ~(mask & 0x700);
3376 }
3377 /* Skip prefix characters. */
3378 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
3379 ++s;
3380 opno += 1;
3381 break;
3382 }
3383 doubled = used_bits & mask & insn_bits;
3384 if (doubled)
3385 {
3386 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3387 " %s %s"), doubled, opcode->name, opcode->args);
3388 return 0;
3389 }
3390 used_bits |= mask;
3391 undefined = ~used_bits & insn_bits;
3392 if (opcode->pinfo != INSN_MACRO && undefined)
3393 {
3394 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3395 undefined, opcode->name, opcode->args);
3396 return 0;
3397 }
3398 used_bits &= ~insn_bits;
3399 if (used_bits)
3400 {
3401 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3402 used_bits, opcode->name, opcode->args);
3403 return 0;
3404 }
3405 return 1;
3406 }
3407
3408 /* The MIPS16 version of validate_mips_insn. */
3409
3410 static int
3411 validate_mips16_insn (const struct mips_opcode *opcode,
3412 struct mips_operand_array *operands)
3413 {
3414 if (opcode->args[0] == 'a' || opcode->args[0] == 'i')
3415 {
3416 /* In this case OPCODE defines the first 16 bits in a 32-bit jump
3417 instruction. Use TMP to describe the full instruction. */
3418 struct mips_opcode tmp;
3419
3420 tmp = *opcode;
3421 tmp.match <<= 16;
3422 tmp.mask <<= 16;
3423 return validate_mips_insn (&tmp, 0xffffffff, 0, operands);
3424 }
3425 return validate_mips_insn (opcode, 0xffff, 0, operands);
3426 }
3427
3428 /* The microMIPS version of validate_mips_insn. */
3429
3430 static int
3431 validate_micromips_insn (const struct mips_opcode *opc,
3432 struct mips_operand_array *operands)
3433 {
3434 unsigned long insn_bits;
3435 unsigned long major;
3436 unsigned int length;
3437
3438 if (opc->pinfo == INSN_MACRO)
3439 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3440 operands);
3441
3442 length = micromips_insn_length (opc);
3443 if (length != 2 && length != 4)
3444 {
3445 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
3446 "%s %s"), length, opc->name, opc->args);
3447 return 0;
3448 }
3449 major = opc->match >> (10 + 8 * (length - 2));
3450 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3451 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3452 {
3453 as_bad (_("internal error: bad microMIPS opcode "
3454 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3455 return 0;
3456 }
3457
3458 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3459 insn_bits = 1 << 4 * length;
3460 insn_bits <<= 4 * length;
3461 insn_bits -= 1;
3462 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3463 operands);
3464 }
3465
3466 /* This function is called once, at assembler startup time. It should set up
3467 all the tables, etc. that the MD part of the assembler will need. */
3468
3469 void
3470 md_begin (void)
3471 {
3472 const char *retval = NULL;
3473 int i = 0;
3474 int broken = 0;
3475
3476 if (mips_pic != NO_PIC)
3477 {
3478 if (g_switch_seen && g_switch_value != 0)
3479 as_bad (_("-G may not be used in position-independent code"));
3480 g_switch_value = 0;
3481 }
3482 else if (mips_abicalls)
3483 {
3484 if (g_switch_seen && g_switch_value != 0)
3485 as_bad (_("-G may not be used with abicalls"));
3486 g_switch_value = 0;
3487 }
3488
3489 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3490 as_warn (_("could not set architecture and machine"));
3491
3492 op_hash = hash_new ();
3493
3494 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
3495 for (i = 0; i < NUMOPCODES;)
3496 {
3497 const char *name = mips_opcodes[i].name;
3498
3499 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
3500 if (retval != NULL)
3501 {
3502 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3503 mips_opcodes[i].name, retval);
3504 /* Probably a memory allocation problem? Give up now. */
3505 as_fatal (_("broken assembler, no assembly attempted"));
3506 }
3507 do
3508 {
3509 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3510 decode_mips_operand, &mips_operands[i]))
3511 broken = 1;
3512 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3513 {
3514 create_insn (&nop_insn, mips_opcodes + i);
3515 if (mips_fix_loongson2f_nop)
3516 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3517 nop_insn.fixed_p = 1;
3518 }
3519 ++i;
3520 }
3521 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3522 }
3523
3524 mips16_op_hash = hash_new ();
3525 mips16_operands = XCNEWVEC (struct mips_operand_array,
3526 bfd_mips16_num_opcodes);
3527
3528 i = 0;
3529 while (i < bfd_mips16_num_opcodes)
3530 {
3531 const char *name = mips16_opcodes[i].name;
3532
3533 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
3534 if (retval != NULL)
3535 as_fatal (_("internal: can't hash `%s': %s"),
3536 mips16_opcodes[i].name, retval);
3537 do
3538 {
3539 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3540 broken = 1;
3541 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3542 {
3543 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3544 mips16_nop_insn.fixed_p = 1;
3545 }
3546 ++i;
3547 }
3548 while (i < bfd_mips16_num_opcodes
3549 && strcmp (mips16_opcodes[i].name, name) == 0);
3550 }
3551
3552 micromips_op_hash = hash_new ();
3553 micromips_operands = XCNEWVEC (struct mips_operand_array,
3554 bfd_micromips_num_opcodes);
3555
3556 i = 0;
3557 while (i < bfd_micromips_num_opcodes)
3558 {
3559 const char *name = micromips_opcodes[i].name;
3560
3561 retval = hash_insert (micromips_op_hash, name,
3562 (void *) &micromips_opcodes[i]);
3563 if (retval != NULL)
3564 as_fatal (_("internal: can't hash `%s': %s"),
3565 micromips_opcodes[i].name, retval);
3566 do
3567 {
3568 struct mips_cl_insn *micromips_nop_insn;
3569
3570 if (!validate_micromips_insn (&micromips_opcodes[i],
3571 &micromips_operands[i]))
3572 broken = 1;
3573
3574 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3575 {
3576 if (micromips_insn_length (micromips_opcodes + i) == 2)
3577 micromips_nop_insn = &micromips_nop16_insn;
3578 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3579 micromips_nop_insn = &micromips_nop32_insn;
3580 else
3581 continue;
3582
3583 if (micromips_nop_insn->insn_mo == NULL
3584 && strcmp (name, "nop") == 0)
3585 {
3586 create_insn (micromips_nop_insn, micromips_opcodes + i);
3587 micromips_nop_insn->fixed_p = 1;
3588 }
3589 }
3590 }
3591 while (++i < bfd_micromips_num_opcodes
3592 && strcmp (micromips_opcodes[i].name, name) == 0);
3593 }
3594
3595 if (broken)
3596 as_fatal (_("broken assembler, no assembly attempted"));
3597
3598 /* We add all the general register names to the symbol table. This
3599 helps us detect invalid uses of them. */
3600 for (i = 0; reg_names[i].name; i++)
3601 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
3602 reg_names[i].num, /* & RNUM_MASK, */
3603 &zero_address_frag));
3604 if (HAVE_NEWABI)
3605 for (i = 0; reg_names_n32n64[i].name; i++)
3606 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
3607 reg_names_n32n64[i].num, /* & RNUM_MASK, */
3608 &zero_address_frag));
3609 else
3610 for (i = 0; reg_names_o32[i].name; i++)
3611 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
3612 reg_names_o32[i].num, /* & RNUM_MASK, */
3613 &zero_address_frag));
3614
3615 for (i = 0; i < 32; i++)
3616 {
3617 char regname[6];
3618
3619 /* R5900 VU0 floating-point register. */
3620 sprintf (regname, "$vf%d", i);
3621 symbol_table_insert (symbol_new (regname, reg_section,
3622 RTYPE_VF | i, &zero_address_frag));
3623
3624 /* R5900 VU0 integer register. */
3625 sprintf (regname, "$vi%d", i);
3626 symbol_table_insert (symbol_new (regname, reg_section,
3627 RTYPE_VI | i, &zero_address_frag));
3628
3629 /* MSA register. */
3630 sprintf (regname, "$w%d", i);
3631 symbol_table_insert (symbol_new (regname, reg_section,
3632 RTYPE_MSA | i, &zero_address_frag));
3633 }
3634
3635 obstack_init (&mips_operand_tokens);
3636
3637 mips_no_prev_insn ();
3638
3639 mips_gprmask = 0;
3640 mips_cprmask[0] = 0;
3641 mips_cprmask[1] = 0;
3642 mips_cprmask[2] = 0;
3643 mips_cprmask[3] = 0;
3644
3645 /* set the default alignment for the text section (2**2) */
3646 record_alignment (text_section, 2);
3647
3648 bfd_set_gp_size (stdoutput, g_switch_value);
3649
3650 /* On a native system other than VxWorks, sections must be aligned
3651 to 16 byte boundaries. When configured for an embedded ELF
3652 target, we don't bother. */
3653 if (strncmp (TARGET_OS, "elf", 3) != 0
3654 && strncmp (TARGET_OS, "vxworks", 7) != 0)
3655 {
3656 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3657 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3658 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3659 }
3660
3661 /* Create a .reginfo section for register masks and a .mdebug
3662 section for debugging information. */
3663 {
3664 segT seg;
3665 subsegT subseg;
3666 flagword flags;
3667 segT sec;
3668
3669 seg = now_seg;
3670 subseg = now_subseg;
3671
3672 /* The ABI says this section should be loaded so that the
3673 running program can access it. However, we don't load it
3674 if we are configured for an embedded target */
3675 flags = SEC_READONLY | SEC_DATA;
3676 if (strncmp (TARGET_OS, "elf", 3) != 0)
3677 flags |= SEC_ALLOC | SEC_LOAD;
3678
3679 if (mips_abi != N64_ABI)
3680 {
3681 sec = subseg_new (".reginfo", (subsegT) 0);
3682
3683 bfd_set_section_flags (stdoutput, sec, flags);
3684 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
3685
3686 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3687 }
3688 else
3689 {
3690 /* The 64-bit ABI uses a .MIPS.options section rather than
3691 .reginfo section. */
3692 sec = subseg_new (".MIPS.options", (subsegT) 0);
3693 bfd_set_section_flags (stdoutput, sec, flags);
3694 bfd_set_section_alignment (stdoutput, sec, 3);
3695
3696 /* Set up the option header. */
3697 {
3698 Elf_Internal_Options opthdr;
3699 char *f;
3700
3701 opthdr.kind = ODK_REGINFO;
3702 opthdr.size = (sizeof (Elf_External_Options)
3703 + sizeof (Elf64_External_RegInfo));
3704 opthdr.section = 0;
3705 opthdr.info = 0;
3706 f = frag_more (sizeof (Elf_External_Options));
3707 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3708 (Elf_External_Options *) f);
3709
3710 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3711 }
3712 }
3713
3714 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3715 bfd_set_section_flags (stdoutput, sec,
3716 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3717 bfd_set_section_alignment (stdoutput, sec, 3);
3718 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3719
3720 if (ECOFF_DEBUGGING)
3721 {
3722 sec = subseg_new (".mdebug", (subsegT) 0);
3723 (void) bfd_set_section_flags (stdoutput, sec,
3724 SEC_HAS_CONTENTS | SEC_READONLY);
3725 (void) bfd_set_section_alignment (stdoutput, sec, 2);
3726 }
3727 else if (mips_flag_pdr)
3728 {
3729 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3730 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3731 SEC_READONLY | SEC_RELOC
3732 | SEC_DEBUGGING);
3733 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3734 }
3735
3736 subseg_set (seg, subseg);
3737 }
3738
3739 if (mips_fix_vr4120)
3740 init_vr4120_conflicts ();
3741 }
3742
3743 static inline void
3744 fpabi_incompatible_with (int fpabi, const char *what)
3745 {
3746 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3747 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3748 }
3749
3750 static inline void
3751 fpabi_requires (int fpabi, const char *what)
3752 {
3753 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3754 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3755 }
3756
3757 /* Check -mabi and register sizes against the specified FP ABI. */
3758 static void
3759 check_fpabi (int fpabi)
3760 {
3761 switch (fpabi)
3762 {
3763 case Val_GNU_MIPS_ABI_FP_DOUBLE:
3764 if (file_mips_opts.soft_float)
3765 fpabi_incompatible_with (fpabi, "softfloat");
3766 else if (file_mips_opts.single_float)
3767 fpabi_incompatible_with (fpabi, "singlefloat");
3768 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3769 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3770 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3771 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
3772 break;
3773
3774 case Val_GNU_MIPS_ABI_FP_XX:
3775 if (mips_abi != O32_ABI)
3776 fpabi_requires (fpabi, "-mabi=32");
3777 else if (file_mips_opts.soft_float)
3778 fpabi_incompatible_with (fpabi, "softfloat");
3779 else if (file_mips_opts.single_float)
3780 fpabi_incompatible_with (fpabi, "singlefloat");
3781 else if (file_mips_opts.fp != 0)
3782 fpabi_requires (fpabi, "fp=xx");
3783 break;
3784
3785 case Val_GNU_MIPS_ABI_FP_64A:
3786 case Val_GNU_MIPS_ABI_FP_64:
3787 if (mips_abi != O32_ABI)
3788 fpabi_requires (fpabi, "-mabi=32");
3789 else if (file_mips_opts.soft_float)
3790 fpabi_incompatible_with (fpabi, "softfloat");
3791 else if (file_mips_opts.single_float)
3792 fpabi_incompatible_with (fpabi, "singlefloat");
3793 else if (file_mips_opts.fp != 64)
3794 fpabi_requires (fpabi, "fp=64");
3795 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3796 fpabi_incompatible_with (fpabi, "nooddspreg");
3797 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3798 fpabi_requires (fpabi, "nooddspreg");
3799 break;
3800
3801 case Val_GNU_MIPS_ABI_FP_SINGLE:
3802 if (file_mips_opts.soft_float)
3803 fpabi_incompatible_with (fpabi, "softfloat");
3804 else if (!file_mips_opts.single_float)
3805 fpabi_requires (fpabi, "singlefloat");
3806 break;
3807
3808 case Val_GNU_MIPS_ABI_FP_SOFT:
3809 if (!file_mips_opts.soft_float)
3810 fpabi_requires (fpabi, "softfloat");
3811 break;
3812
3813 case Val_GNU_MIPS_ABI_FP_OLD_64:
3814 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3815 Tag_GNU_MIPS_ABI_FP, fpabi);
3816 break;
3817
3818 case Val_GNU_MIPS_ABI_FP_NAN2008:
3819 /* Silently ignore compatibility value. */
3820 break;
3821
3822 default:
3823 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3824 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3825 break;
3826 }
3827 }
3828
3829 /* Perform consistency checks on the current options. */
3830
3831 static void
3832 mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3833 {
3834 /* Check the size of integer registers agrees with the ABI and ISA. */
3835 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3836 as_bad (_("`gp=64' used with a 32-bit processor"));
3837 else if (abi_checks
3838 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3839 as_bad (_("`gp=32' used with a 64-bit ABI"));
3840 else if (abi_checks
3841 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3842 as_bad (_("`gp=64' used with a 32-bit ABI"));
3843
3844 /* Check the size of the float registers agrees with the ABI and ISA. */
3845 switch (opts->fp)
3846 {
3847 case 0:
3848 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
3849 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
3850 else if (opts->single_float == 1)
3851 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
3852 break;
3853 case 64:
3854 if (!ISA_HAS_64BIT_FPRS (opts->isa))
3855 as_bad (_("`fp=64' used with a 32-bit fpu"));
3856 else if (abi_checks
3857 && ABI_NEEDS_32BIT_REGS (mips_abi)
3858 && !ISA_HAS_MXHC1 (opts->isa))
3859 as_warn (_("`fp=64' used with a 32-bit ABI"));
3860 break;
3861 case 32:
3862 if (abi_checks
3863 && ABI_NEEDS_64BIT_REGS (mips_abi))
3864 as_warn (_("`fp=32' used with a 64-bit ABI"));
3865 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
3866 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
3867 break;
3868 default:
3869 as_bad (_("Unknown size of floating point registers"));
3870 break;
3871 }
3872
3873 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
3874 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
3875
3876 if (opts->micromips == 1 && opts->mips16 == 1)
3877 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
3878 else if (ISA_IS_R6 (opts->isa)
3879 && (opts->micromips == 1
3880 || opts->mips16 == 1))
3881 as_fatal (_("`%s' cannot be used with `%s'"),
3882 opts->micromips ? "micromips" : "mips16",
3883 mips_cpu_info_from_isa (opts->isa)->name);
3884
3885 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
3886 as_fatal (_("branch relaxation is not supported in `%s'"),
3887 mips_cpu_info_from_isa (opts->isa)->name);
3888 }
3889
3890 /* Perform consistency checks on the module level options exactly once.
3891 This is a deferred check that happens:
3892 at the first .set directive
3893 or, at the first pseudo op that generates code (inc .dc.a)
3894 or, at the first instruction
3895 or, at the end. */
3896
3897 static void
3898 file_mips_check_options (void)
3899 {
3900 const struct mips_cpu_info *arch_info = 0;
3901
3902 if (file_mips_opts_checked)
3903 return;
3904
3905 /* The following code determines the register size.
3906 Similar code was added to GCC 3.3 (see override_options() in
3907 config/mips/mips.c). The GAS and GCC code should be kept in sync
3908 as much as possible. */
3909
3910 if (file_mips_opts.gp < 0)
3911 {
3912 /* Infer the integer register size from the ABI and processor.
3913 Restrict ourselves to 32-bit registers if that's all the
3914 processor has, or if the ABI cannot handle 64-bit registers. */
3915 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
3916 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
3917 ? 32 : 64;
3918 }
3919
3920 if (file_mips_opts.fp < 0)
3921 {
3922 /* No user specified float register size.
3923 ??? GAS treats single-float processors as though they had 64-bit
3924 float registers (although it complains when double-precision
3925 instructions are used). As things stand, saying they have 32-bit
3926 registers would lead to spurious "register must be even" messages.
3927 So here we assume float registers are never smaller than the
3928 integer ones. */
3929 if (file_mips_opts.gp == 64)
3930 /* 64-bit integer registers implies 64-bit float registers. */
3931 file_mips_opts.fp = 64;
3932 else if ((file_mips_opts.ase & FP64_ASES)
3933 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
3934 /* Handle ASEs that require 64-bit float registers, if possible. */
3935 file_mips_opts.fp = 64;
3936 else if (ISA_IS_R6 (mips_opts.isa))
3937 /* R6 implies 64-bit float registers. */
3938 file_mips_opts.fp = 64;
3939 else
3940 /* 32-bit float registers. */
3941 file_mips_opts.fp = 32;
3942 }
3943
3944 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
3945
3946 /* Disable operations on odd-numbered floating-point registers by default
3947 when using the FPXX ABI. */
3948 if (file_mips_opts.oddspreg < 0)
3949 {
3950 if (file_mips_opts.fp == 0)
3951 file_mips_opts.oddspreg = 0;
3952 else
3953 file_mips_opts.oddspreg = 1;
3954 }
3955
3956 /* End of GCC-shared inference code. */
3957
3958 /* This flag is set when we have a 64-bit capable CPU but use only
3959 32-bit wide registers. Note that EABI does not use it. */
3960 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
3961 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
3962 || mips_abi == O32_ABI))
3963 mips_32bitmode = 1;
3964
3965 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
3966 as_bad (_("trap exception not supported at ISA 1"));
3967
3968 /* If the selected architecture includes support for ASEs, enable
3969 generation of code for them. */
3970 if (file_mips_opts.mips16 == -1)
3971 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
3972 if (file_mips_opts.micromips == -1)
3973 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
3974 ? 1 : 0;
3975
3976 if (mips_nan2008 == -1)
3977 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
3978 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
3979 as_fatal (_("`%s' does not support legacy NaN"),
3980 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
3981
3982 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
3983 being selected implicitly. */
3984 if (file_mips_opts.fp != 64)
3985 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
3986
3987 /* If the user didn't explicitly select or deselect a particular ASE,
3988 use the default setting for the CPU. */
3989 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
3990
3991 /* Set up the current options. These may change throughout assembly. */
3992 mips_opts = file_mips_opts;
3993
3994 mips_check_isa_supports_ases ();
3995 mips_check_options (&file_mips_opts, TRUE);
3996 file_mips_opts_checked = TRUE;
3997
3998 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3999 as_warn (_("could not set architecture and machine"));
4000 }
4001
4002 void
4003 md_assemble (char *str)
4004 {
4005 struct mips_cl_insn insn;
4006 bfd_reloc_code_real_type unused_reloc[3]
4007 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
4008
4009 file_mips_check_options ();
4010
4011 imm_expr.X_op = O_absent;
4012 offset_expr.X_op = O_absent;
4013 offset_reloc[0] = BFD_RELOC_UNUSED;
4014 offset_reloc[1] = BFD_RELOC_UNUSED;
4015 offset_reloc[2] = BFD_RELOC_UNUSED;
4016
4017 mips_mark_labels ();
4018 mips_assembling_insn = TRUE;
4019 clear_insn_error ();
4020
4021 if (mips_opts.mips16)
4022 mips16_ip (str, &insn);
4023 else
4024 {
4025 mips_ip (str, &insn);
4026 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4027 str, insn.insn_opcode));
4028 }
4029
4030 if (insn_error.msg)
4031 report_insn_error (str);
4032 else if (insn.insn_mo->pinfo == INSN_MACRO)
4033 {
4034 macro_start ();
4035 if (mips_opts.mips16)
4036 mips16_macro (&insn);
4037 else
4038 macro (&insn, str);
4039 macro_end ();
4040 }
4041 else
4042 {
4043 if (offset_expr.X_op != O_absent)
4044 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
4045 else
4046 append_insn (&insn, NULL, unused_reloc, FALSE);
4047 }
4048
4049 mips_assembling_insn = FALSE;
4050 }
4051
4052 /* Convenience functions for abstracting away the differences between
4053 MIPS16 and non-MIPS16 relocations. */
4054
4055 static inline bfd_boolean
4056 mips16_reloc_p (bfd_reloc_code_real_type reloc)
4057 {
4058 switch (reloc)
4059 {
4060 case BFD_RELOC_MIPS16_JMP:
4061 case BFD_RELOC_MIPS16_GPREL:
4062 case BFD_RELOC_MIPS16_GOT16:
4063 case BFD_RELOC_MIPS16_CALL16:
4064 case BFD_RELOC_MIPS16_HI16_S:
4065 case BFD_RELOC_MIPS16_HI16:
4066 case BFD_RELOC_MIPS16_LO16:
4067 case BFD_RELOC_MIPS16_16_PCREL_S1:
4068 return TRUE;
4069
4070 default:
4071 return FALSE;
4072 }
4073 }
4074
4075 static inline bfd_boolean
4076 micromips_reloc_p (bfd_reloc_code_real_type reloc)
4077 {
4078 switch (reloc)
4079 {
4080 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4081 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4082 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4083 case BFD_RELOC_MICROMIPS_GPREL16:
4084 case BFD_RELOC_MICROMIPS_JMP:
4085 case BFD_RELOC_MICROMIPS_HI16:
4086 case BFD_RELOC_MICROMIPS_HI16_S:
4087 case BFD_RELOC_MICROMIPS_LO16:
4088 case BFD_RELOC_MICROMIPS_LITERAL:
4089 case BFD_RELOC_MICROMIPS_GOT16:
4090 case BFD_RELOC_MICROMIPS_CALL16:
4091 case BFD_RELOC_MICROMIPS_GOT_HI16:
4092 case BFD_RELOC_MICROMIPS_GOT_LO16:
4093 case BFD_RELOC_MICROMIPS_CALL_HI16:
4094 case BFD_RELOC_MICROMIPS_CALL_LO16:
4095 case BFD_RELOC_MICROMIPS_SUB:
4096 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4097 case BFD_RELOC_MICROMIPS_GOT_OFST:
4098 case BFD_RELOC_MICROMIPS_GOT_DISP:
4099 case BFD_RELOC_MICROMIPS_HIGHEST:
4100 case BFD_RELOC_MICROMIPS_HIGHER:
4101 case BFD_RELOC_MICROMIPS_SCN_DISP:
4102 case BFD_RELOC_MICROMIPS_JALR:
4103 return TRUE;
4104
4105 default:
4106 return FALSE;
4107 }
4108 }
4109
4110 static inline bfd_boolean
4111 jmp_reloc_p (bfd_reloc_code_real_type reloc)
4112 {
4113 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4114 }
4115
4116 static inline bfd_boolean
4117 b_reloc_p (bfd_reloc_code_real_type reloc)
4118 {
4119 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4120 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4121 || reloc == BFD_RELOC_16_PCREL_S2
4122 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
4123 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4124 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4125 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4126 }
4127
4128 static inline bfd_boolean
4129 got16_reloc_p (bfd_reloc_code_real_type reloc)
4130 {
4131 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
4132 || reloc == BFD_RELOC_MICROMIPS_GOT16);
4133 }
4134
4135 static inline bfd_boolean
4136 hi16_reloc_p (bfd_reloc_code_real_type reloc)
4137 {
4138 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
4139 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
4140 }
4141
4142 static inline bfd_boolean
4143 lo16_reloc_p (bfd_reloc_code_real_type reloc)
4144 {
4145 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
4146 || reloc == BFD_RELOC_MICROMIPS_LO16);
4147 }
4148
4149 static inline bfd_boolean
4150 jalr_reloc_p (bfd_reloc_code_real_type reloc)
4151 {
4152 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
4153 }
4154
4155 static inline bfd_boolean
4156 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4157 {
4158 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4159 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4160 }
4161
4162 /* Return true if RELOC is a PC-relative relocation that does not have
4163 full address range. */
4164
4165 static inline bfd_boolean
4166 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4167 {
4168 switch (reloc)
4169 {
4170 case BFD_RELOC_16_PCREL_S2:
4171 case BFD_RELOC_MIPS16_16_PCREL_S1:
4172 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4173 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4174 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4175 case BFD_RELOC_MIPS_21_PCREL_S2:
4176 case BFD_RELOC_MIPS_26_PCREL_S2:
4177 case BFD_RELOC_MIPS_18_PCREL_S3:
4178 case BFD_RELOC_MIPS_19_PCREL_S2:
4179 return TRUE;
4180
4181 case BFD_RELOC_32_PCREL:
4182 case BFD_RELOC_HI16_S_PCREL:
4183 case BFD_RELOC_LO16_PCREL:
4184 return HAVE_64BIT_ADDRESSES;
4185
4186 default:
4187 return FALSE;
4188 }
4189 }
4190
4191 /* Return true if the given relocation might need a matching %lo().
4192 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4193 need a matching %lo() when applied to local symbols. */
4194
4195 static inline bfd_boolean
4196 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
4197 {
4198 return (HAVE_IN_PLACE_ADDENDS
4199 && (hi16_reloc_p (reloc)
4200 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4201 all GOT16 relocations evaluate to "G". */
4202 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4203 }
4204
4205 /* Return the type of %lo() reloc needed by RELOC, given that
4206 reloc_needs_lo_p. */
4207
4208 static inline bfd_reloc_code_real_type
4209 matching_lo_reloc (bfd_reloc_code_real_type reloc)
4210 {
4211 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4212 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4213 : BFD_RELOC_LO16));
4214 }
4215
4216 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
4217 relocation. */
4218
4219 static inline bfd_boolean
4220 fixup_has_matching_lo_p (fixS *fixp)
4221 {
4222 return (fixp->fx_next != NULL
4223 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
4224 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4225 && fixp->fx_offset == fixp->fx_next->fx_offset);
4226 }
4227
4228 /* Move all labels in LABELS to the current insertion point. TEXT_P
4229 says whether the labels refer to text or data. */
4230
4231 static void
4232 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
4233 {
4234 struct insn_label_list *l;
4235 valueT val;
4236
4237 for (l = labels; l != NULL; l = l->next)
4238 {
4239 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
4240 symbol_set_frag (l->label, frag_now);
4241 val = (valueT) frag_now_fix ();
4242 /* MIPS16/microMIPS text labels are stored as odd. */
4243 if (text_p && HAVE_CODE_COMPRESSION)
4244 ++val;
4245 S_SET_VALUE (l->label, val);
4246 }
4247 }
4248
4249 /* Move all labels in insn_labels to the current insertion point
4250 and treat them as text labels. */
4251
4252 static void
4253 mips_move_text_labels (void)
4254 {
4255 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4256 }
4257
4258 static bfd_boolean
4259 s_is_linkonce (symbolS *sym, segT from_seg)
4260 {
4261 bfd_boolean linkonce = FALSE;
4262 segT symseg = S_GET_SEGMENT (sym);
4263
4264 if (symseg != from_seg && !S_IS_LOCAL (sym))
4265 {
4266 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4267 linkonce = TRUE;
4268 /* The GNU toolchain uses an extension for ELF: a section
4269 beginning with the magic string .gnu.linkonce is a
4270 linkonce section. */
4271 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4272 sizeof ".gnu.linkonce" - 1) == 0)
4273 linkonce = TRUE;
4274 }
4275 return linkonce;
4276 }
4277
4278 /* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
4279 linker to handle them specially, such as generating jalx instructions
4280 when needed. We also make them odd for the duration of the assembly,
4281 in order to generate the right sort of code. We will make them even
4282 in the adjust_symtab routine, while leaving them marked. This is
4283 convenient for the debugger and the disassembler. The linker knows
4284 to make them odd again. */
4285
4286 static void
4287 mips_compressed_mark_label (symbolS *label)
4288 {
4289 gas_assert (HAVE_CODE_COMPRESSION);
4290
4291 if (mips_opts.mips16)
4292 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4293 else
4294 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
4295 if ((S_GET_VALUE (label) & 1) == 0
4296 /* Don't adjust the address if the label is global or weak, or
4297 in a link-once section, since we'll be emitting symbol reloc
4298 references to it which will be patched up by the linker, and
4299 the final value of the symbol may or may not be MIPS16/microMIPS. */
4300 && !S_IS_WEAK (label)
4301 && !S_IS_EXTERNAL (label)
4302 && !s_is_linkonce (label, now_seg))
4303 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4304 }
4305
4306 /* Mark preceding MIPS16 or microMIPS instruction labels. */
4307
4308 static void
4309 mips_compressed_mark_labels (void)
4310 {
4311 struct insn_label_list *l;
4312
4313 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4314 mips_compressed_mark_label (l->label);
4315 }
4316
4317 /* End the current frag. Make it a variant frag and record the
4318 relaxation info. */
4319
4320 static void
4321 relax_close_frag (void)
4322 {
4323 mips_macro_warning.first_frag = frag_now;
4324 frag_var (rs_machine_dependent, 0, 0,
4325 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4326 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4327
4328 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4329 mips_relax.first_fixup = 0;
4330 }
4331
4332 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
4333 See the comment above RELAX_ENCODE for more details. */
4334
4335 static void
4336 relax_start (symbolS *symbol)
4337 {
4338 gas_assert (mips_relax.sequence == 0);
4339 mips_relax.sequence = 1;
4340 mips_relax.symbol = symbol;
4341 }
4342
4343 /* Start generating the second version of a relaxable sequence.
4344 See the comment above RELAX_ENCODE for more details. */
4345
4346 static void
4347 relax_switch (void)
4348 {
4349 gas_assert (mips_relax.sequence == 1);
4350 mips_relax.sequence = 2;
4351 }
4352
4353 /* End the current relaxable sequence. */
4354
4355 static void
4356 relax_end (void)
4357 {
4358 gas_assert (mips_relax.sequence == 2);
4359 relax_close_frag ();
4360 mips_relax.sequence = 0;
4361 }
4362
4363 /* Return true if IP is a delayed branch or jump. */
4364
4365 static inline bfd_boolean
4366 delayed_branch_p (const struct mips_cl_insn *ip)
4367 {
4368 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4369 | INSN_COND_BRANCH_DELAY
4370 | INSN_COND_BRANCH_LIKELY)) != 0;
4371 }
4372
4373 /* Return true if IP is a compact branch or jump. */
4374
4375 static inline bfd_boolean
4376 compact_branch_p (const struct mips_cl_insn *ip)
4377 {
4378 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4379 | INSN2_COND_BRANCH)) != 0;
4380 }
4381
4382 /* Return true if IP is an unconditional branch or jump. */
4383
4384 static inline bfd_boolean
4385 uncond_branch_p (const struct mips_cl_insn *ip)
4386 {
4387 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
4388 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
4389 }
4390
4391 /* Return true if IP is a branch-likely instruction. */
4392
4393 static inline bfd_boolean
4394 branch_likely_p (const struct mips_cl_insn *ip)
4395 {
4396 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4397 }
4398
4399 /* Return the type of nop that should be used to fill the delay slot
4400 of delayed branch IP. */
4401
4402 static struct mips_cl_insn *
4403 get_delay_slot_nop (const struct mips_cl_insn *ip)
4404 {
4405 if (mips_opts.micromips
4406 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4407 return &micromips_nop32_insn;
4408 return NOP_INSN;
4409 }
4410
4411 /* Return a mask that has bit N set if OPCODE reads the register(s)
4412 in operand N. */
4413
4414 static unsigned int
4415 insn_read_mask (const struct mips_opcode *opcode)
4416 {
4417 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4418 }
4419
4420 /* Return a mask that has bit N set if OPCODE writes to the register(s)
4421 in operand N. */
4422
4423 static unsigned int
4424 insn_write_mask (const struct mips_opcode *opcode)
4425 {
4426 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4427 }
4428
4429 /* Return a mask of the registers specified by operand OPERAND of INSN.
4430 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4431 is set. */
4432
4433 static unsigned int
4434 operand_reg_mask (const struct mips_cl_insn *insn,
4435 const struct mips_operand *operand,
4436 unsigned int type_mask)
4437 {
4438 unsigned int uval, vsel;
4439
4440 switch (operand->type)
4441 {
4442 case OP_INT:
4443 case OP_MAPPED_INT:
4444 case OP_MSB:
4445 case OP_PCREL:
4446 case OP_PERF_REG:
4447 case OP_ADDIUSP_INT:
4448 case OP_ENTRY_EXIT_LIST:
4449 case OP_REPEAT_DEST_REG:
4450 case OP_REPEAT_PREV_REG:
4451 case OP_PC:
4452 case OP_VU0_SUFFIX:
4453 case OP_VU0_MATCH_SUFFIX:
4454 case OP_IMM_INDEX:
4455 abort ();
4456
4457 case OP_REG:
4458 case OP_OPTIONAL_REG:
4459 {
4460 const struct mips_reg_operand *reg_op;
4461
4462 reg_op = (const struct mips_reg_operand *) operand;
4463 if (!(type_mask & (1 << reg_op->reg_type)))
4464 return 0;
4465 uval = insn_extract_operand (insn, operand);
4466 return 1 << mips_decode_reg_operand (reg_op, uval);
4467 }
4468
4469 case OP_REG_PAIR:
4470 {
4471 const struct mips_reg_pair_operand *pair_op;
4472
4473 pair_op = (const struct mips_reg_pair_operand *) operand;
4474 if (!(type_mask & (1 << pair_op->reg_type)))
4475 return 0;
4476 uval = insn_extract_operand (insn, operand);
4477 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4478 }
4479
4480 case OP_CLO_CLZ_DEST:
4481 if (!(type_mask & (1 << OP_REG_GP)))
4482 return 0;
4483 uval = insn_extract_operand (insn, operand);
4484 return (1 << (uval & 31)) | (1 << (uval >> 5));
4485
4486 case OP_SAME_RS_RT:
4487 if (!(type_mask & (1 << OP_REG_GP)))
4488 return 0;
4489 uval = insn_extract_operand (insn, operand);
4490 gas_assert ((uval & 31) == (uval >> 5));
4491 return 1 << (uval & 31);
4492
4493 case OP_CHECK_PREV:
4494 case OP_NON_ZERO_REG:
4495 if (!(type_mask & (1 << OP_REG_GP)))
4496 return 0;
4497 uval = insn_extract_operand (insn, operand);
4498 return 1 << (uval & 31);
4499
4500 case OP_LWM_SWM_LIST:
4501 abort ();
4502
4503 case OP_SAVE_RESTORE_LIST:
4504 abort ();
4505
4506 case OP_MDMX_IMM_REG:
4507 if (!(type_mask & (1 << OP_REG_VEC)))
4508 return 0;
4509 uval = insn_extract_operand (insn, operand);
4510 vsel = uval >> 5;
4511 if ((vsel & 0x18) == 0x18)
4512 return 0;
4513 return 1 << (uval & 31);
4514
4515 case OP_REG_INDEX:
4516 if (!(type_mask & (1 << OP_REG_GP)))
4517 return 0;
4518 return 1 << insn_extract_operand (insn, operand);
4519 }
4520 abort ();
4521 }
4522
4523 /* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4524 where bit N of OPNO_MASK is set if operand N should be included.
4525 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4526 is set. */
4527
4528 static unsigned int
4529 insn_reg_mask (const struct mips_cl_insn *insn,
4530 unsigned int type_mask, unsigned int opno_mask)
4531 {
4532 unsigned int opno, reg_mask;
4533
4534 opno = 0;
4535 reg_mask = 0;
4536 while (opno_mask != 0)
4537 {
4538 if (opno_mask & 1)
4539 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4540 opno_mask >>= 1;
4541 opno += 1;
4542 }
4543 return reg_mask;
4544 }
4545
4546 /* Return the mask of core registers that IP reads. */
4547
4548 static unsigned int
4549 gpr_read_mask (const struct mips_cl_insn *ip)
4550 {
4551 unsigned long pinfo, pinfo2;
4552 unsigned int mask;
4553
4554 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4555 pinfo = ip->insn_mo->pinfo;
4556 pinfo2 = ip->insn_mo->pinfo2;
4557 if (pinfo & INSN_UDI)
4558 {
4559 /* UDI instructions have traditionally been assumed to read RS
4560 and RT. */
4561 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4562 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4563 }
4564 if (pinfo & INSN_READ_GPR_24)
4565 mask |= 1 << 24;
4566 if (pinfo2 & INSN2_READ_GPR_16)
4567 mask |= 1 << 16;
4568 if (pinfo2 & INSN2_READ_SP)
4569 mask |= 1 << SP;
4570 if (pinfo2 & INSN2_READ_GPR_31)
4571 mask |= 1 << 31;
4572 /* Don't include register 0. */
4573 return mask & ~1;
4574 }
4575
4576 /* Return the mask of core registers that IP writes. */
4577
4578 static unsigned int
4579 gpr_write_mask (const struct mips_cl_insn *ip)
4580 {
4581 unsigned long pinfo, pinfo2;
4582 unsigned int mask;
4583
4584 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4585 pinfo = ip->insn_mo->pinfo;
4586 pinfo2 = ip->insn_mo->pinfo2;
4587 if (pinfo & INSN_WRITE_GPR_24)
4588 mask |= 1 << 24;
4589 if (pinfo & INSN_WRITE_GPR_31)
4590 mask |= 1 << 31;
4591 if (pinfo & INSN_UDI)
4592 /* UDI instructions have traditionally been assumed to write to RD. */
4593 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4594 if (pinfo2 & INSN2_WRITE_SP)
4595 mask |= 1 << SP;
4596 /* Don't include register 0. */
4597 return mask & ~1;
4598 }
4599
4600 /* Return the mask of floating-point registers that IP reads. */
4601
4602 static unsigned int
4603 fpr_read_mask (const struct mips_cl_insn *ip)
4604 {
4605 unsigned long pinfo;
4606 unsigned int mask;
4607
4608 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4609 | (1 << OP_REG_MSA)),
4610 insn_read_mask (ip->insn_mo));
4611 pinfo = ip->insn_mo->pinfo;
4612 /* Conservatively treat all operands to an FP_D instruction are doubles.
4613 (This is overly pessimistic for things like cvt.d.s.) */
4614 if (FPR_SIZE != 64 && (pinfo & FP_D))
4615 mask |= mask << 1;
4616 return mask;
4617 }
4618
4619 /* Return the mask of floating-point registers that IP writes. */
4620
4621 static unsigned int
4622 fpr_write_mask (const struct mips_cl_insn *ip)
4623 {
4624 unsigned long pinfo;
4625 unsigned int mask;
4626
4627 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4628 | (1 << OP_REG_MSA)),
4629 insn_write_mask (ip->insn_mo));
4630 pinfo = ip->insn_mo->pinfo;
4631 /* Conservatively treat all operands to an FP_D instruction are doubles.
4632 (This is overly pessimistic for things like cvt.s.d.) */
4633 if (FPR_SIZE != 64 && (pinfo & FP_D))
4634 mask |= mask << 1;
4635 return mask;
4636 }
4637
4638 /* Operand OPNUM of INSN is an odd-numbered floating-point register.
4639 Check whether that is allowed. */
4640
4641 static bfd_boolean
4642 mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4643 {
4644 const char *s = insn->name;
4645 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4646 || FPR_SIZE == 64)
4647 && mips_opts.oddspreg;
4648
4649 if (insn->pinfo == INSN_MACRO)
4650 /* Let a macro pass, we'll catch it later when it is expanded. */
4651 return TRUE;
4652
4653 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4654 otherwise it depends on oddspreg. */
4655 if ((insn->pinfo & FP_S)
4656 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
4657 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
4658 return FPR_SIZE == 32 || oddspreg;
4659
4660 /* Allow odd registers for single-precision ops and double-precision if the
4661 floating-point registers are 64-bit wide. */
4662 switch (insn->pinfo & (FP_S | FP_D))
4663 {
4664 case FP_S:
4665 case 0:
4666 return oddspreg;
4667 case FP_D:
4668 return FPR_SIZE == 64;
4669 default:
4670 break;
4671 }
4672
4673 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4674 s = strchr (insn->name, '.');
4675 if (s != NULL && opnum == 2)
4676 s = strchr (s + 1, '.');
4677 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4678 return oddspreg;
4679
4680 return FPR_SIZE == 64;
4681 }
4682
4683 /* Information about an instruction argument that we're trying to match. */
4684 struct mips_arg_info
4685 {
4686 /* The instruction so far. */
4687 struct mips_cl_insn *insn;
4688
4689 /* The first unconsumed operand token. */
4690 struct mips_operand_token *token;
4691
4692 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4693 int opnum;
4694
4695 /* The 1-based argument number, for error reporting. This does not
4696 count elided optional registers, etc.. */
4697 int argnum;
4698
4699 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4700 unsigned int last_regno;
4701
4702 /* If the first operand was an OP_REG, this is the register that it
4703 specified, otherwise it is ILLEGAL_REG. */
4704 unsigned int dest_regno;
4705
4706 /* The value of the last OP_INT operand. Only used for OP_MSB,
4707 where it gives the lsb position. */
4708 unsigned int last_op_int;
4709
4710 /* If true, match routines should assume that no later instruction
4711 alternative matches and should therefore be as accomodating as
4712 possible. Match routines should not report errors if something
4713 is only invalid for !LAX_MATCH. */
4714 bfd_boolean lax_match;
4715
4716 /* True if a reference to the current AT register was seen. */
4717 bfd_boolean seen_at;
4718 };
4719
4720 /* Record that the argument is out of range. */
4721
4722 static void
4723 match_out_of_range (struct mips_arg_info *arg)
4724 {
4725 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4726 }
4727
4728 /* Record that the argument isn't constant but needs to be. */
4729
4730 static void
4731 match_not_constant (struct mips_arg_info *arg)
4732 {
4733 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4734 arg->argnum);
4735 }
4736
4737 /* Try to match an OT_CHAR token for character CH. Consume the token
4738 and return true on success, otherwise return false. */
4739
4740 static bfd_boolean
4741 match_char (struct mips_arg_info *arg, char ch)
4742 {
4743 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4744 {
4745 ++arg->token;
4746 if (ch == ',')
4747 arg->argnum += 1;
4748 return TRUE;
4749 }
4750 return FALSE;
4751 }
4752
4753 /* Try to get an expression from the next tokens in ARG. Consume the
4754 tokens and return true on success, storing the expression value in
4755 VALUE and relocation types in R. */
4756
4757 static bfd_boolean
4758 match_expression (struct mips_arg_info *arg, expressionS *value,
4759 bfd_reloc_code_real_type *r)
4760 {
4761 /* If the next token is a '(' that was parsed as being part of a base
4762 expression, assume we have an elided offset. The later match will fail
4763 if this turns out to be wrong. */
4764 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4765 {
4766 value->X_op = O_constant;
4767 value->X_add_number = 0;
4768 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4769 return TRUE;
4770 }
4771
4772 /* Reject register-based expressions such as "0+$2" and "(($2))".
4773 For plain registers the default error seems more appropriate. */
4774 if (arg->token->type == OT_INTEGER
4775 && arg->token->u.integer.value.X_op == O_register)
4776 {
4777 set_insn_error (arg->argnum, _("register value used as expression"));
4778 return FALSE;
4779 }
4780
4781 if (arg->token->type == OT_INTEGER)
4782 {
4783 *value = arg->token->u.integer.value;
4784 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4785 ++arg->token;
4786 return TRUE;
4787 }
4788
4789 set_insn_error_i
4790 (arg->argnum, _("operand %d must be an immediate expression"),
4791 arg->argnum);
4792 return FALSE;
4793 }
4794
4795 /* Try to get a constant expression from the next tokens in ARG. Consume
4796 the tokens and return return true on success, storing the constant value
4797 in *VALUE. Use FALLBACK as the value if the match succeeded with an
4798 error. */
4799
4800 static bfd_boolean
4801 match_const_int (struct mips_arg_info *arg, offsetT *value)
4802 {
4803 expressionS ex;
4804 bfd_reloc_code_real_type r[3];
4805
4806 if (!match_expression (arg, &ex, r))
4807 return FALSE;
4808
4809 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
4810 *value = ex.X_add_number;
4811 else
4812 {
4813 match_not_constant (arg);
4814 return FALSE;
4815 }
4816 return TRUE;
4817 }
4818
4819 /* Return the RTYPE_* flags for a register operand of type TYPE that
4820 appears in instruction OPCODE. */
4821
4822 static unsigned int
4823 convert_reg_type (const struct mips_opcode *opcode,
4824 enum mips_reg_operand_type type)
4825 {
4826 switch (type)
4827 {
4828 case OP_REG_GP:
4829 return RTYPE_NUM | RTYPE_GP;
4830
4831 case OP_REG_FP:
4832 /* Allow vector register names for MDMX if the instruction is a 64-bit
4833 FPR load, store or move (including moves to and from GPRs). */
4834 if ((mips_opts.ase & ASE_MDMX)
4835 && (opcode->pinfo & FP_D)
4836 && (opcode->pinfo & (INSN_COPROC_MOVE
4837 | INSN_COPROC_MEMORY_DELAY
4838 | INSN_LOAD_COPROC
4839 | INSN_LOAD_MEMORY
4840 | INSN_STORE_MEMORY)))
4841 return RTYPE_FPU | RTYPE_VEC;
4842 return RTYPE_FPU;
4843
4844 case OP_REG_CCC:
4845 if (opcode->pinfo & (FP_D | FP_S))
4846 return RTYPE_CCC | RTYPE_FCC;
4847 return RTYPE_CCC;
4848
4849 case OP_REG_VEC:
4850 if (opcode->membership & INSN_5400)
4851 return RTYPE_FPU;
4852 return RTYPE_FPU | RTYPE_VEC;
4853
4854 case OP_REG_ACC:
4855 return RTYPE_ACC;
4856
4857 case OP_REG_COPRO:
4858 if (opcode->name[strlen (opcode->name) - 1] == '0')
4859 return RTYPE_NUM | RTYPE_CP0;
4860 return RTYPE_NUM;
4861
4862 case OP_REG_HW:
4863 return RTYPE_NUM;
4864
4865 case OP_REG_VI:
4866 return RTYPE_NUM | RTYPE_VI;
4867
4868 case OP_REG_VF:
4869 return RTYPE_NUM | RTYPE_VF;
4870
4871 case OP_REG_R5900_I:
4872 return RTYPE_R5900_I;
4873
4874 case OP_REG_R5900_Q:
4875 return RTYPE_R5900_Q;
4876
4877 case OP_REG_R5900_R:
4878 return RTYPE_R5900_R;
4879
4880 case OP_REG_R5900_ACC:
4881 return RTYPE_R5900_ACC;
4882
4883 case OP_REG_MSA:
4884 return RTYPE_MSA;
4885
4886 case OP_REG_MSA_CTRL:
4887 return RTYPE_NUM;
4888 }
4889 abort ();
4890 }
4891
4892 /* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4893
4894 static void
4895 check_regno (struct mips_arg_info *arg,
4896 enum mips_reg_operand_type type, unsigned int regno)
4897 {
4898 if (AT && type == OP_REG_GP && regno == AT)
4899 arg->seen_at = TRUE;
4900
4901 if (type == OP_REG_FP
4902 && (regno & 1) != 0
4903 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
4904 {
4905 /* This was a warning prior to introducing O32 FPXX and FP64 support
4906 so maintain a warning for FP32 but raise an error for the new
4907 cases. */
4908 if (FPR_SIZE == 32)
4909 as_warn (_("float register should be even, was %d"), regno);
4910 else
4911 as_bad (_("float register should be even, was %d"), regno);
4912 }
4913
4914 if (type == OP_REG_CCC)
4915 {
4916 const char *name;
4917 size_t length;
4918
4919 name = arg->insn->insn_mo->name;
4920 length = strlen (name);
4921 if ((regno & 1) != 0
4922 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4923 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
4924 as_warn (_("condition code register should be even for %s, was %d"),
4925 name, regno);
4926
4927 if ((regno & 3) != 0
4928 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
4929 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
4930 name, regno);
4931 }
4932 }
4933
4934 /* ARG is a register with symbol value SYMVAL. Try to interpret it as
4935 a register of type TYPE. Return true on success, storing the register
4936 number in *REGNO and warning about any dubious uses. */
4937
4938 static bfd_boolean
4939 match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4940 unsigned int symval, unsigned int *regno)
4941 {
4942 if (type == OP_REG_VEC)
4943 symval = mips_prefer_vec_regno (symval);
4944 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4945 return FALSE;
4946
4947 *regno = symval & RNUM_MASK;
4948 check_regno (arg, type, *regno);
4949 return TRUE;
4950 }
4951
4952 /* Try to interpret the next token in ARG as a register of type TYPE.
4953 Consume the token and return true on success, storing the register
4954 number in *REGNO. Return false on failure. */
4955
4956 static bfd_boolean
4957 match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4958 unsigned int *regno)
4959 {
4960 if (arg->token->type == OT_REG
4961 && match_regno (arg, type, arg->token->u.regno, regno))
4962 {
4963 ++arg->token;
4964 return TRUE;
4965 }
4966 return FALSE;
4967 }
4968
4969 /* Try to interpret the next token in ARG as a range of registers of type TYPE.
4970 Consume the token and return true on success, storing the register numbers
4971 in *REGNO1 and *REGNO2. Return false on failure. */
4972
4973 static bfd_boolean
4974 match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4975 unsigned int *regno1, unsigned int *regno2)
4976 {
4977 if (match_reg (arg, type, regno1))
4978 {
4979 *regno2 = *regno1;
4980 return TRUE;
4981 }
4982 if (arg->token->type == OT_REG_RANGE
4983 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
4984 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
4985 && *regno1 <= *regno2)
4986 {
4987 ++arg->token;
4988 return TRUE;
4989 }
4990 return FALSE;
4991 }
4992
4993 /* OP_INT matcher. */
4994
4995 static bfd_boolean
4996 match_int_operand (struct mips_arg_info *arg,
4997 const struct mips_operand *operand_base)
4998 {
4999 const struct mips_int_operand *operand;
5000 unsigned int uval;
5001 int min_val, max_val, factor;
5002 offsetT sval;
5003
5004 operand = (const struct mips_int_operand *) operand_base;
5005 factor = 1 << operand->shift;
5006 min_val = mips_int_operand_min (operand);
5007 max_val = mips_int_operand_max (operand);
5008
5009 if (operand_base->lsb == 0
5010 && operand_base->size == 16
5011 && operand->shift == 0
5012 && operand->bias == 0
5013 && (operand->max_val == 32767 || operand->max_val == 65535))
5014 {
5015 /* The operand can be relocated. */
5016 if (!match_expression (arg, &offset_expr, offset_reloc))
5017 return FALSE;
5018
5019 if (offset_reloc[0] != BFD_RELOC_UNUSED)
5020 /* Relocation operators were used. Accept the arguent and
5021 leave the relocation value in offset_expr and offset_relocs
5022 for the caller to process. */
5023 return TRUE;
5024
5025 if (offset_expr.X_op != O_constant)
5026 {
5027 /* Accept non-constant operands if no later alternative matches,
5028 leaving it for the caller to process. */
5029 if (!arg->lax_match)
5030 return FALSE;
5031 offset_reloc[0] = BFD_RELOC_LO16;
5032 return TRUE;
5033 }
5034
5035 /* Clear the global state; we're going to install the operand
5036 ourselves. */
5037 sval = offset_expr.X_add_number;
5038 offset_expr.X_op = O_absent;
5039
5040 /* For compatibility with older assemblers, we accept
5041 0x8000-0xffff as signed 16-bit numbers when only
5042 signed numbers are allowed. */
5043 if (sval > max_val)
5044 {
5045 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5046 if (!arg->lax_match && sval <= max_val)
5047 return FALSE;
5048 }
5049 }
5050 else
5051 {
5052 if (!match_const_int (arg, &sval))
5053 return FALSE;
5054 }
5055
5056 arg->last_op_int = sval;
5057
5058 if (sval < min_val || sval > max_val || sval % factor)
5059 {
5060 match_out_of_range (arg);
5061 return FALSE;
5062 }
5063
5064 uval = (unsigned int) sval >> operand->shift;
5065 uval -= operand->bias;
5066
5067 /* Handle -mfix-cn63xxp1. */
5068 if (arg->opnum == 1
5069 && mips_fix_cn63xxp1
5070 && !mips_opts.micromips
5071 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5072 switch (uval)
5073 {
5074 case 5:
5075 case 25:
5076 case 26:
5077 case 27:
5078 case 28:
5079 case 29:
5080 case 30:
5081 case 31:
5082 /* These are ok. */
5083 break;
5084
5085 default:
5086 /* The rest must be changed to 28. */
5087 uval = 28;
5088 break;
5089 }
5090
5091 insn_insert_operand (arg->insn, operand_base, uval);
5092 return TRUE;
5093 }
5094
5095 /* OP_MAPPED_INT matcher. */
5096
5097 static bfd_boolean
5098 match_mapped_int_operand (struct mips_arg_info *arg,
5099 const struct mips_operand *operand_base)
5100 {
5101 const struct mips_mapped_int_operand *operand;
5102 unsigned int uval, num_vals;
5103 offsetT sval;
5104
5105 operand = (const struct mips_mapped_int_operand *) operand_base;
5106 if (!match_const_int (arg, &sval))
5107 return FALSE;
5108
5109 num_vals = 1 << operand_base->size;
5110 for (uval = 0; uval < num_vals; uval++)
5111 if (operand->int_map[uval] == sval)
5112 break;
5113 if (uval == num_vals)
5114 {
5115 match_out_of_range (arg);
5116 return FALSE;
5117 }
5118
5119 insn_insert_operand (arg->insn, operand_base, uval);
5120 return TRUE;
5121 }
5122
5123 /* OP_MSB matcher. */
5124
5125 static bfd_boolean
5126 match_msb_operand (struct mips_arg_info *arg,
5127 const struct mips_operand *operand_base)
5128 {
5129 const struct mips_msb_operand *operand;
5130 int min_val, max_val, max_high;
5131 offsetT size, sval, high;
5132
5133 operand = (const struct mips_msb_operand *) operand_base;
5134 min_val = operand->bias;
5135 max_val = min_val + (1 << operand_base->size) - 1;
5136 max_high = operand->opsize;
5137
5138 if (!match_const_int (arg, &size))
5139 return FALSE;
5140
5141 high = size + arg->last_op_int;
5142 sval = operand->add_lsb ? high : size;
5143
5144 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5145 {
5146 match_out_of_range (arg);
5147 return FALSE;
5148 }
5149 insn_insert_operand (arg->insn, operand_base, sval - min_val);
5150 return TRUE;
5151 }
5152
5153 /* OP_REG matcher. */
5154
5155 static bfd_boolean
5156 match_reg_operand (struct mips_arg_info *arg,
5157 const struct mips_operand *operand_base)
5158 {
5159 const struct mips_reg_operand *operand;
5160 unsigned int regno, uval, num_vals;
5161
5162 operand = (const struct mips_reg_operand *) operand_base;
5163 if (!match_reg (arg, operand->reg_type, &regno))
5164 return FALSE;
5165
5166 if (operand->reg_map)
5167 {
5168 num_vals = 1 << operand->root.size;
5169 for (uval = 0; uval < num_vals; uval++)
5170 if (operand->reg_map[uval] == regno)
5171 break;
5172 if (num_vals == uval)
5173 return FALSE;
5174 }
5175 else
5176 uval = regno;
5177
5178 arg->last_regno = regno;
5179 if (arg->opnum == 1)
5180 arg->dest_regno = regno;
5181 insn_insert_operand (arg->insn, operand_base, uval);
5182 return TRUE;
5183 }
5184
5185 /* OP_REG_PAIR matcher. */
5186
5187 static bfd_boolean
5188 match_reg_pair_operand (struct mips_arg_info *arg,
5189 const struct mips_operand *operand_base)
5190 {
5191 const struct mips_reg_pair_operand *operand;
5192 unsigned int regno1, regno2, uval, num_vals;
5193
5194 operand = (const struct mips_reg_pair_operand *) operand_base;
5195 if (!match_reg (arg, operand->reg_type, &regno1)
5196 || !match_char (arg, ',')
5197 || !match_reg (arg, operand->reg_type, &regno2))
5198 return FALSE;
5199
5200 num_vals = 1 << operand_base->size;
5201 for (uval = 0; uval < num_vals; uval++)
5202 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5203 break;
5204 if (uval == num_vals)
5205 return FALSE;
5206
5207 insn_insert_operand (arg->insn, operand_base, uval);
5208 return TRUE;
5209 }
5210
5211 /* OP_PCREL matcher. The caller chooses the relocation type. */
5212
5213 static bfd_boolean
5214 match_pcrel_operand (struct mips_arg_info *arg)
5215 {
5216 bfd_reloc_code_real_type r[3];
5217
5218 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
5219 }
5220
5221 /* OP_PERF_REG matcher. */
5222
5223 static bfd_boolean
5224 match_perf_reg_operand (struct mips_arg_info *arg,
5225 const struct mips_operand *operand)
5226 {
5227 offsetT sval;
5228
5229 if (!match_const_int (arg, &sval))
5230 return FALSE;
5231
5232 if (sval != 0
5233 && (sval != 1
5234 || (mips_opts.arch == CPU_R5900
5235 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5236 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5237 {
5238 set_insn_error (arg->argnum, _("invalid performance register"));
5239 return FALSE;
5240 }
5241
5242 insn_insert_operand (arg->insn, operand, sval);
5243 return TRUE;
5244 }
5245
5246 /* OP_ADDIUSP matcher. */
5247
5248 static bfd_boolean
5249 match_addiusp_operand (struct mips_arg_info *arg,
5250 const struct mips_operand *operand)
5251 {
5252 offsetT sval;
5253 unsigned int uval;
5254
5255 if (!match_const_int (arg, &sval))
5256 return FALSE;
5257
5258 if (sval % 4)
5259 {
5260 match_out_of_range (arg);
5261 return FALSE;
5262 }
5263
5264 sval /= 4;
5265 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
5266 {
5267 match_out_of_range (arg);
5268 return FALSE;
5269 }
5270
5271 uval = (unsigned int) sval;
5272 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5273 insn_insert_operand (arg->insn, operand, uval);
5274 return TRUE;
5275 }
5276
5277 /* OP_CLO_CLZ_DEST matcher. */
5278
5279 static bfd_boolean
5280 match_clo_clz_dest_operand (struct mips_arg_info *arg,
5281 const struct mips_operand *operand)
5282 {
5283 unsigned int regno;
5284
5285 if (!match_reg (arg, OP_REG_GP, &regno))
5286 return FALSE;
5287
5288 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5289 return TRUE;
5290 }
5291
5292 /* OP_CHECK_PREV matcher. */
5293
5294 static bfd_boolean
5295 match_check_prev_operand (struct mips_arg_info *arg,
5296 const struct mips_operand *operand_base)
5297 {
5298 const struct mips_check_prev_operand *operand;
5299 unsigned int regno;
5300
5301 operand = (const struct mips_check_prev_operand *) operand_base;
5302
5303 if (!match_reg (arg, OP_REG_GP, &regno))
5304 return FALSE;
5305
5306 if (!operand->zero_ok && regno == 0)
5307 return FALSE;
5308
5309 if ((operand->less_than_ok && regno < arg->last_regno)
5310 || (operand->greater_than_ok && regno > arg->last_regno)
5311 || (operand->equal_ok && regno == arg->last_regno))
5312 {
5313 arg->last_regno = regno;
5314 insn_insert_operand (arg->insn, operand_base, regno);
5315 return TRUE;
5316 }
5317
5318 return FALSE;
5319 }
5320
5321 /* OP_SAME_RS_RT matcher. */
5322
5323 static bfd_boolean
5324 match_same_rs_rt_operand (struct mips_arg_info *arg,
5325 const struct mips_operand *operand)
5326 {
5327 unsigned int regno;
5328
5329 if (!match_reg (arg, OP_REG_GP, &regno))
5330 return FALSE;
5331
5332 if (regno == 0)
5333 {
5334 set_insn_error (arg->argnum, _("the source register must not be $0"));
5335 return FALSE;
5336 }
5337
5338 arg->last_regno = regno;
5339
5340 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5341 return TRUE;
5342 }
5343
5344 /* OP_LWM_SWM_LIST matcher. */
5345
5346 static bfd_boolean
5347 match_lwm_swm_list_operand (struct mips_arg_info *arg,
5348 const struct mips_operand *operand)
5349 {
5350 unsigned int reglist, sregs, ra, regno1, regno2;
5351 struct mips_arg_info reset;
5352
5353 reglist = 0;
5354 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5355 return FALSE;
5356 do
5357 {
5358 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5359 {
5360 reglist |= 1 << FP;
5361 regno2 = S7;
5362 }
5363 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5364 reset = *arg;
5365 }
5366 while (match_char (arg, ',')
5367 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5368 *arg = reset;
5369
5370 if (operand->size == 2)
5371 {
5372 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5373
5374 s0, ra
5375 s0, s1, ra, s2, s3
5376 s0-s2, ra
5377
5378 and any permutations of these. */
5379 if ((reglist & 0xfff1ffff) != 0x80010000)
5380 return FALSE;
5381
5382 sregs = (reglist >> 17) & 7;
5383 ra = 0;
5384 }
5385 else
5386 {
5387 /* The list must include at least one of ra and s0-sN,
5388 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5389 which are $23 and $30 respectively.) E.g.:
5390
5391 ra
5392 s0
5393 ra, s0, s1, s2
5394 s0-s8
5395 s0-s5, ra
5396
5397 and any permutations of these. */
5398 if ((reglist & 0x3f00ffff) != 0)
5399 return FALSE;
5400
5401 ra = (reglist >> 27) & 0x10;
5402 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5403 }
5404 sregs += 1;
5405 if ((sregs & -sregs) != sregs)
5406 return FALSE;
5407
5408 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5409 return TRUE;
5410 }
5411
5412 /* OP_ENTRY_EXIT_LIST matcher. */
5413
5414 static unsigned int
5415 match_entry_exit_operand (struct mips_arg_info *arg,
5416 const struct mips_operand *operand)
5417 {
5418 unsigned int mask;
5419 bfd_boolean is_exit;
5420
5421 /* The format is the same for both ENTRY and EXIT, but the constraints
5422 are different. */
5423 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5424 mask = (is_exit ? 7 << 3 : 0);
5425 do
5426 {
5427 unsigned int regno1, regno2;
5428 bfd_boolean is_freg;
5429
5430 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5431 is_freg = FALSE;
5432 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5433 is_freg = TRUE;
5434 else
5435 return FALSE;
5436
5437 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5438 {
5439 mask &= ~(7 << 3);
5440 mask |= (5 + regno2) << 3;
5441 }
5442 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5443 mask |= (regno2 - 3) << 3;
5444 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5445 mask |= (regno2 - 15) << 1;
5446 else if (regno1 == RA && regno2 == RA)
5447 mask |= 1;
5448 else
5449 return FALSE;
5450 }
5451 while (match_char (arg, ','));
5452
5453 insn_insert_operand (arg->insn, operand, mask);
5454 return TRUE;
5455 }
5456
5457 /* OP_SAVE_RESTORE_LIST matcher. */
5458
5459 static bfd_boolean
5460 match_save_restore_list_operand (struct mips_arg_info *arg)
5461 {
5462 unsigned int opcode, args, statics, sregs;
5463 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
5464 offsetT frame_size;
5465
5466 opcode = arg->insn->insn_opcode;
5467 frame_size = 0;
5468 num_frame_sizes = 0;
5469 args = 0;
5470 statics = 0;
5471 sregs = 0;
5472 do
5473 {
5474 unsigned int regno1, regno2;
5475
5476 if (arg->token->type == OT_INTEGER)
5477 {
5478 /* Handle the frame size. */
5479 if (!match_const_int (arg, &frame_size))
5480 return FALSE;
5481 num_frame_sizes += 1;
5482 }
5483 else
5484 {
5485 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5486 return FALSE;
5487
5488 while (regno1 <= regno2)
5489 {
5490 if (regno1 >= 4 && regno1 <= 7)
5491 {
5492 if (num_frame_sizes == 0)
5493 /* args $a0-$a3 */
5494 args |= 1 << (regno1 - 4);
5495 else
5496 /* statics $a0-$a3 */
5497 statics |= 1 << (regno1 - 4);
5498 }
5499 else if (regno1 >= 16 && regno1 <= 23)
5500 /* $s0-$s7 */
5501 sregs |= 1 << (regno1 - 16);
5502 else if (regno1 == 30)
5503 /* $s8 */
5504 sregs |= 1 << 8;
5505 else if (regno1 == 31)
5506 /* Add $ra to insn. */
5507 opcode |= 0x40;
5508 else
5509 return FALSE;
5510 regno1 += 1;
5511 if (regno1 == 24)
5512 regno1 = 30;
5513 }
5514 }
5515 }
5516 while (match_char (arg, ','));
5517
5518 /* Encode args/statics combination. */
5519 if (args & statics)
5520 return FALSE;
5521 else if (args == 0xf)
5522 /* All $a0-$a3 are args. */
5523 opcode |= MIPS16_ALL_ARGS << 16;
5524 else if (statics == 0xf)
5525 /* All $a0-$a3 are statics. */
5526 opcode |= MIPS16_ALL_STATICS << 16;
5527 else
5528 {
5529 /* Count arg registers. */
5530 num_args = 0;
5531 while (args & 0x1)
5532 {
5533 args >>= 1;
5534 num_args += 1;
5535 }
5536 if (args != 0)
5537 return FALSE;
5538
5539 /* Count static registers. */
5540 num_statics = 0;
5541 while (statics & 0x8)
5542 {
5543 statics = (statics << 1) & 0xf;
5544 num_statics += 1;
5545 }
5546 if (statics != 0)
5547 return FALSE;
5548
5549 /* Encode args/statics. */
5550 opcode |= ((num_args << 2) | num_statics) << 16;
5551 }
5552
5553 /* Encode $s0/$s1. */
5554 if (sregs & (1 << 0)) /* $s0 */
5555 opcode |= 0x20;
5556 if (sregs & (1 << 1)) /* $s1 */
5557 opcode |= 0x10;
5558 sregs >>= 2;
5559
5560 /* Encode $s2-$s8. */
5561 num_sregs = 0;
5562 while (sregs & 1)
5563 {
5564 sregs >>= 1;
5565 num_sregs += 1;
5566 }
5567 if (sregs != 0)
5568 return FALSE;
5569 opcode |= num_sregs << 24;
5570
5571 /* Encode frame size. */
5572 if (num_frame_sizes == 0)
5573 {
5574 set_insn_error (arg->argnum, _("missing frame size"));
5575 return FALSE;
5576 }
5577 if (num_frame_sizes > 1)
5578 {
5579 set_insn_error (arg->argnum, _("frame size specified twice"));
5580 return FALSE;
5581 }
5582 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5583 {
5584 set_insn_error (arg->argnum, _("invalid frame size"));
5585 return FALSE;
5586 }
5587 if (frame_size != 128 || (opcode >> 16) != 0)
5588 {
5589 frame_size /= 8;
5590 opcode |= (((frame_size & 0xf0) << 16)
5591 | (frame_size & 0x0f));
5592 }
5593
5594 /* Finally build the instruction. */
5595 if ((opcode >> 16) != 0 || frame_size == 0)
5596 opcode |= MIPS16_EXTEND;
5597 arg->insn->insn_opcode = opcode;
5598 return TRUE;
5599 }
5600
5601 /* OP_MDMX_IMM_REG matcher. */
5602
5603 static bfd_boolean
5604 match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
5605 const struct mips_operand *operand)
5606 {
5607 unsigned int regno, uval;
5608 bfd_boolean is_qh;
5609 const struct mips_opcode *opcode;
5610
5611 /* The mips_opcode records whether this is an octobyte or quadhalf
5612 instruction. Start out with that bit in place. */
5613 opcode = arg->insn->insn_mo;
5614 uval = mips_extract_operand (operand, opcode->match);
5615 is_qh = (uval != 0);
5616
5617 if (arg->token->type == OT_REG)
5618 {
5619 if ((opcode->membership & INSN_5400)
5620 && strcmp (opcode->name, "rzu.ob") == 0)
5621 {
5622 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5623 arg->argnum);
5624 return FALSE;
5625 }
5626
5627 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5628 return FALSE;
5629 ++arg->token;
5630
5631 /* Check whether this is a vector register or a broadcast of
5632 a single element. */
5633 if (arg->token->type == OT_INTEGER_INDEX)
5634 {
5635 if (arg->token->u.index > (is_qh ? 3 : 7))
5636 {
5637 set_insn_error (arg->argnum, _("invalid element selector"));
5638 return FALSE;
5639 }
5640 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5641 ++arg->token;
5642 }
5643 else
5644 {
5645 /* A full vector. */
5646 if ((opcode->membership & INSN_5400)
5647 && (strcmp (opcode->name, "sll.ob") == 0
5648 || strcmp (opcode->name, "srl.ob") == 0))
5649 {
5650 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5651 arg->argnum);
5652 return FALSE;
5653 }
5654
5655 if (is_qh)
5656 uval |= MDMX_FMTSEL_VEC_QH << 5;
5657 else
5658 uval |= MDMX_FMTSEL_VEC_OB << 5;
5659 }
5660 uval |= regno;
5661 }
5662 else
5663 {
5664 offsetT sval;
5665
5666 if (!match_const_int (arg, &sval))
5667 return FALSE;
5668 if (sval < 0 || sval > 31)
5669 {
5670 match_out_of_range (arg);
5671 return FALSE;
5672 }
5673 uval |= (sval & 31);
5674 if (is_qh)
5675 uval |= MDMX_FMTSEL_IMM_QH << 5;
5676 else
5677 uval |= MDMX_FMTSEL_IMM_OB << 5;
5678 }
5679 insn_insert_operand (arg->insn, operand, uval);
5680 return TRUE;
5681 }
5682
5683 /* OP_IMM_INDEX matcher. */
5684
5685 static bfd_boolean
5686 match_imm_index_operand (struct mips_arg_info *arg,
5687 const struct mips_operand *operand)
5688 {
5689 unsigned int max_val;
5690
5691 if (arg->token->type != OT_INTEGER_INDEX)
5692 return FALSE;
5693
5694 max_val = (1 << operand->size) - 1;
5695 if (arg->token->u.index > max_val)
5696 {
5697 match_out_of_range (arg);
5698 return FALSE;
5699 }
5700 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5701 ++arg->token;
5702 return TRUE;
5703 }
5704
5705 /* OP_REG_INDEX matcher. */
5706
5707 static bfd_boolean
5708 match_reg_index_operand (struct mips_arg_info *arg,
5709 const struct mips_operand *operand)
5710 {
5711 unsigned int regno;
5712
5713 if (arg->token->type != OT_REG_INDEX)
5714 return FALSE;
5715
5716 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5717 return FALSE;
5718
5719 insn_insert_operand (arg->insn, operand, regno);
5720 ++arg->token;
5721 return TRUE;
5722 }
5723
5724 /* OP_PC matcher. */
5725
5726 static bfd_boolean
5727 match_pc_operand (struct mips_arg_info *arg)
5728 {
5729 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5730 {
5731 ++arg->token;
5732 return TRUE;
5733 }
5734 return FALSE;
5735 }
5736
5737 /* OP_NON_ZERO_REG matcher. */
5738
5739 static bfd_boolean
5740 match_non_zero_reg_operand (struct mips_arg_info *arg,
5741 const struct mips_operand *operand)
5742 {
5743 unsigned int regno;
5744
5745 if (!match_reg (arg, OP_REG_GP, &regno))
5746 return FALSE;
5747
5748 if (regno == 0)
5749 return FALSE;
5750
5751 arg->last_regno = regno;
5752 insn_insert_operand (arg->insn, operand, regno);
5753 return TRUE;
5754 }
5755
5756 /* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5757 register that we need to match. */
5758
5759 static bfd_boolean
5760 match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
5761 {
5762 unsigned int regno;
5763
5764 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
5765 }
5766
5767 /* Read a floating-point constant from S for LI.S or LI.D. LENGTH is
5768 the length of the value in bytes (4 for float, 8 for double) and
5769 USING_GPRS says whether the destination is a GPR rather than an FPR.
5770
5771 Return the constant in IMM and OFFSET as follows:
5772
5773 - If the constant should be loaded via memory, set IMM to O_absent and
5774 OFFSET to the memory address.
5775
5776 - Otherwise, if the constant should be loaded into two 32-bit registers,
5777 set IMM to the O_constant to load into the high register and OFFSET
5778 to the corresponding value for the low register.
5779
5780 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5781
5782 These constants only appear as the last operand in an instruction,
5783 and every instruction that accepts them in any variant accepts them
5784 in all variants. This means we don't have to worry about backing out
5785 any changes if the instruction does not match. We just match
5786 unconditionally and report an error if the constant is invalid. */
5787
5788 static bfd_boolean
5789 match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5790 expressionS *offset, int length, bfd_boolean using_gprs)
5791 {
5792 char *p;
5793 segT seg, new_seg;
5794 subsegT subseg;
5795 const char *newname;
5796 unsigned char *data;
5797
5798 /* Where the constant is placed is based on how the MIPS assembler
5799 does things:
5800
5801 length == 4 && using_gprs -- immediate value only
5802 length == 8 && using_gprs -- .rdata or immediate value
5803 length == 4 && !using_gprs -- .lit4 or immediate value
5804 length == 8 && !using_gprs -- .lit8 or immediate value
5805
5806 The .lit4 and .lit8 sections are only used if permitted by the
5807 -G argument. */
5808 if (arg->token->type != OT_FLOAT)
5809 {
5810 set_insn_error (arg->argnum, _("floating-point expression required"));
5811 return FALSE;
5812 }
5813
5814 gas_assert (arg->token->u.flt.length == length);
5815 data = arg->token->u.flt.data;
5816 ++arg->token;
5817
5818 /* Handle 32-bit constants for which an immediate value is best. */
5819 if (length == 4
5820 && (using_gprs
5821 || g_switch_value < 4
5822 || (data[0] == 0 && data[1] == 0)
5823 || (data[2] == 0 && data[3] == 0)))
5824 {
5825 imm->X_op = O_constant;
5826 if (!target_big_endian)
5827 imm->X_add_number = bfd_getl32 (data);
5828 else
5829 imm->X_add_number = bfd_getb32 (data);
5830 offset->X_op = O_absent;
5831 return TRUE;
5832 }
5833
5834 /* Handle 64-bit constants for which an immediate value is best. */
5835 if (length == 8
5836 && !mips_disable_float_construction
5837 /* Constants can only be constructed in GPRs and copied to FPRs if the
5838 GPRs are at least as wide as the FPRs or MTHC1 is available.
5839 Unlike most tests for 32-bit floating-point registers this check
5840 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
5841 permit 64-bit moves without MXHC1.
5842 Force the constant into memory otherwise. */
5843 && (using_gprs
5844 || GPR_SIZE == 64
5845 || ISA_HAS_MXHC1 (mips_opts.isa)
5846 || FPR_SIZE == 32)
5847 && ((data[0] == 0 && data[1] == 0)
5848 || (data[2] == 0 && data[3] == 0))
5849 && ((data[4] == 0 && data[5] == 0)
5850 || (data[6] == 0 && data[7] == 0)))
5851 {
5852 /* The value is simple enough to load with a couple of instructions.
5853 If using 32-bit registers, set IMM to the high order 32 bits and
5854 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
5855 64 bit constant. */
5856 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
5857 {
5858 imm->X_op = O_constant;
5859 offset->X_op = O_constant;
5860 if (!target_big_endian)
5861 {
5862 imm->X_add_number = bfd_getl32 (data + 4);
5863 offset->X_add_number = bfd_getl32 (data);
5864 }
5865 else
5866 {
5867 imm->X_add_number = bfd_getb32 (data);
5868 offset->X_add_number = bfd_getb32 (data + 4);
5869 }
5870 if (offset->X_add_number == 0)
5871 offset->X_op = O_absent;
5872 }
5873 else
5874 {
5875 imm->X_op = O_constant;
5876 if (!target_big_endian)
5877 imm->X_add_number = bfd_getl64 (data);
5878 else
5879 imm->X_add_number = bfd_getb64 (data);
5880 offset->X_op = O_absent;
5881 }
5882 return TRUE;
5883 }
5884
5885 /* Switch to the right section. */
5886 seg = now_seg;
5887 subseg = now_subseg;
5888 if (length == 4)
5889 {
5890 gas_assert (!using_gprs && g_switch_value >= 4);
5891 newname = ".lit4";
5892 }
5893 else
5894 {
5895 if (using_gprs || g_switch_value < 8)
5896 newname = RDATA_SECTION_NAME;
5897 else
5898 newname = ".lit8";
5899 }
5900
5901 new_seg = subseg_new (newname, (subsegT) 0);
5902 bfd_set_section_flags (stdoutput, new_seg,
5903 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
5904 frag_align (length == 4 ? 2 : 3, 0, 0);
5905 if (strncmp (TARGET_OS, "elf", 3) != 0)
5906 record_alignment (new_seg, 4);
5907 else
5908 record_alignment (new_seg, length == 4 ? 2 : 3);
5909 if (seg == now_seg)
5910 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
5911
5912 /* Set the argument to the current address in the section. */
5913 imm->X_op = O_absent;
5914 offset->X_op = O_symbol;
5915 offset->X_add_symbol = symbol_temp_new_now ();
5916 offset->X_add_number = 0;
5917
5918 /* Put the floating point number into the section. */
5919 p = frag_more (length);
5920 memcpy (p, data, length);
5921
5922 /* Switch back to the original section. */
5923 subseg_set (seg, subseg);
5924 return TRUE;
5925 }
5926
5927 /* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
5928 them. */
5929
5930 static bfd_boolean
5931 match_vu0_suffix_operand (struct mips_arg_info *arg,
5932 const struct mips_operand *operand,
5933 bfd_boolean match_p)
5934 {
5935 unsigned int uval;
5936
5937 /* The operand can be an XYZW mask or a single 2-bit channel index
5938 (with X being 0). */
5939 gas_assert (operand->size == 2 || operand->size == 4);
5940
5941 /* The suffix can be omitted when it is already part of the opcode. */
5942 if (arg->token->type != OT_CHANNELS)
5943 return match_p;
5944
5945 uval = arg->token->u.channels;
5946 if (operand->size == 2)
5947 {
5948 /* Check that a single bit is set and convert it into a 2-bit index. */
5949 if ((uval & -uval) != uval)
5950 return FALSE;
5951 uval = 4 - ffs (uval);
5952 }
5953
5954 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
5955 return FALSE;
5956
5957 ++arg->token;
5958 if (!match_p)
5959 insn_insert_operand (arg->insn, operand, uval);
5960 return TRUE;
5961 }
5962
5963 /* S is the text seen for ARG. Match it against OPERAND. Return the end
5964 of the argument text if the match is successful, otherwise return null. */
5965
5966 static bfd_boolean
5967 match_operand (struct mips_arg_info *arg,
5968 const struct mips_operand *operand)
5969 {
5970 switch (operand->type)
5971 {
5972 case OP_INT:
5973 return match_int_operand (arg, operand);
5974
5975 case OP_MAPPED_INT:
5976 return match_mapped_int_operand (arg, operand);
5977
5978 case OP_MSB:
5979 return match_msb_operand (arg, operand);
5980
5981 case OP_REG:
5982 case OP_OPTIONAL_REG:
5983 return match_reg_operand (arg, operand);
5984
5985 case OP_REG_PAIR:
5986 return match_reg_pair_operand (arg, operand);
5987
5988 case OP_PCREL:
5989 return match_pcrel_operand (arg);
5990
5991 case OP_PERF_REG:
5992 return match_perf_reg_operand (arg, operand);
5993
5994 case OP_ADDIUSP_INT:
5995 return match_addiusp_operand (arg, operand);
5996
5997 case OP_CLO_CLZ_DEST:
5998 return match_clo_clz_dest_operand (arg, operand);
5999
6000 case OP_LWM_SWM_LIST:
6001 return match_lwm_swm_list_operand (arg, operand);
6002
6003 case OP_ENTRY_EXIT_LIST:
6004 return match_entry_exit_operand (arg, operand);
6005
6006 case OP_SAVE_RESTORE_LIST:
6007 return match_save_restore_list_operand (arg);
6008
6009 case OP_MDMX_IMM_REG:
6010 return match_mdmx_imm_reg_operand (arg, operand);
6011
6012 case OP_REPEAT_DEST_REG:
6013 return match_tied_reg_operand (arg, arg->dest_regno);
6014
6015 case OP_REPEAT_PREV_REG:
6016 return match_tied_reg_operand (arg, arg->last_regno);
6017
6018 case OP_PC:
6019 return match_pc_operand (arg);
6020
6021 case OP_VU0_SUFFIX:
6022 return match_vu0_suffix_operand (arg, operand, FALSE);
6023
6024 case OP_VU0_MATCH_SUFFIX:
6025 return match_vu0_suffix_operand (arg, operand, TRUE);
6026
6027 case OP_IMM_INDEX:
6028 return match_imm_index_operand (arg, operand);
6029
6030 case OP_REG_INDEX:
6031 return match_reg_index_operand (arg, operand);
6032
6033 case OP_SAME_RS_RT:
6034 return match_same_rs_rt_operand (arg, operand);
6035
6036 case OP_CHECK_PREV:
6037 return match_check_prev_operand (arg, operand);
6038
6039 case OP_NON_ZERO_REG:
6040 return match_non_zero_reg_operand (arg, operand);
6041 }
6042 abort ();
6043 }
6044
6045 /* ARG is the state after successfully matching an instruction.
6046 Issue any queued-up warnings. */
6047
6048 static void
6049 check_completed_insn (struct mips_arg_info *arg)
6050 {
6051 if (arg->seen_at)
6052 {
6053 if (AT == ATREG)
6054 as_warn (_("used $at without \".set noat\""));
6055 else
6056 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
6057 }
6058 }
6059
6060 /* Return true if modifying general-purpose register REG needs a delay. */
6061
6062 static bfd_boolean
6063 reg_needs_delay (unsigned int reg)
6064 {
6065 unsigned long prev_pinfo;
6066
6067 prev_pinfo = history[0].insn_mo->pinfo;
6068 if (!mips_opts.noreorder
6069 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
6070 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
6071 && (gpr_write_mask (&history[0]) & (1 << reg)))
6072 return TRUE;
6073
6074 return FALSE;
6075 }
6076
6077 /* Classify an instruction according to the FIX_VR4120_* enumeration.
6078 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6079 by VR4120 errata. */
6080
6081 static unsigned int
6082 classify_vr4120_insn (const char *name)
6083 {
6084 if (strncmp (name, "macc", 4) == 0)
6085 return FIX_VR4120_MACC;
6086 if (strncmp (name, "dmacc", 5) == 0)
6087 return FIX_VR4120_DMACC;
6088 if (strncmp (name, "mult", 4) == 0)
6089 return FIX_VR4120_MULT;
6090 if (strncmp (name, "dmult", 5) == 0)
6091 return FIX_VR4120_DMULT;
6092 if (strstr (name, "div"))
6093 return FIX_VR4120_DIV;
6094 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6095 return FIX_VR4120_MTHILO;
6096 return NUM_FIX_VR4120_CLASSES;
6097 }
6098
6099 #define INSN_ERET 0x42000018
6100 #define INSN_DERET 0x4200001f
6101 #define INSN_DMULT 0x1c
6102 #define INSN_DMULTU 0x1d
6103
6104 /* Return the number of instructions that must separate INSN1 and INSN2,
6105 where INSN1 is the earlier instruction. Return the worst-case value
6106 for any INSN2 if INSN2 is null. */
6107
6108 static unsigned int
6109 insns_between (const struct mips_cl_insn *insn1,
6110 const struct mips_cl_insn *insn2)
6111 {
6112 unsigned long pinfo1, pinfo2;
6113 unsigned int mask;
6114
6115 /* If INFO2 is null, pessimistically assume that all flags are set for
6116 the second instruction. */
6117 pinfo1 = insn1->insn_mo->pinfo;
6118 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
6119
6120 /* For most targets, write-after-read dependencies on the HI and LO
6121 registers must be separated by at least two instructions. */
6122 if (!hilo_interlocks)
6123 {
6124 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6125 return 2;
6126 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6127 return 2;
6128 }
6129
6130 /* If we're working around r7000 errata, there must be two instructions
6131 between an mfhi or mflo and any instruction that uses the result. */
6132 if (mips_7000_hilo_fix
6133 && !mips_opts.micromips
6134 && MF_HILO_INSN (pinfo1)
6135 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
6136 return 2;
6137
6138 /* If we're working around 24K errata, one instruction is required
6139 if an ERET or DERET is followed by a branch instruction. */
6140 if (mips_fix_24k && !mips_opts.micromips)
6141 {
6142 if (insn1->insn_opcode == INSN_ERET
6143 || insn1->insn_opcode == INSN_DERET)
6144 {
6145 if (insn2 == NULL
6146 || insn2->insn_opcode == INSN_ERET
6147 || insn2->insn_opcode == INSN_DERET
6148 || delayed_branch_p (insn2))
6149 return 1;
6150 }
6151 }
6152
6153 /* If we're working around PMC RM7000 errata, there must be three
6154 nops between a dmult and a load instruction. */
6155 if (mips_fix_rm7000 && !mips_opts.micromips)
6156 {
6157 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6158 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6159 {
6160 if (pinfo2 & INSN_LOAD_MEMORY)
6161 return 3;
6162 }
6163 }
6164
6165 /* If working around VR4120 errata, check for combinations that need
6166 a single intervening instruction. */
6167 if (mips_fix_vr4120 && !mips_opts.micromips)
6168 {
6169 unsigned int class1, class2;
6170
6171 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6172 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
6173 {
6174 if (insn2 == NULL)
6175 return 1;
6176 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6177 if (vr4120_conflicts[class1] & (1 << class2))
6178 return 1;
6179 }
6180 }
6181
6182 if (!HAVE_CODE_COMPRESSION)
6183 {
6184 /* Check for GPR or coprocessor load delays. All such delays
6185 are on the RT register. */
6186 /* Itbl support may require additional care here. */
6187 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
6188 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
6189 {
6190 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
6191 return 1;
6192 }
6193
6194 /* Check for generic coprocessor hazards.
6195
6196 This case is not handled very well. There is no special
6197 knowledge of CP0 handling, and the coprocessors other than
6198 the floating point unit are not distinguished at all. */
6199 /* Itbl support may require additional care here. FIXME!
6200 Need to modify this to include knowledge about
6201 user specified delays! */
6202 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
6203 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6204 {
6205 /* Handle cases where INSN1 writes to a known general coprocessor
6206 register. There must be a one instruction delay before INSN2
6207 if INSN2 reads that register, otherwise no delay is needed. */
6208 mask = fpr_write_mask (insn1);
6209 if (mask != 0)
6210 {
6211 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
6212 return 1;
6213 }
6214 else
6215 {
6216 /* Read-after-write dependencies on the control registers
6217 require a two-instruction gap. */
6218 if ((pinfo1 & INSN_WRITE_COND_CODE)
6219 && (pinfo2 & INSN_READ_COND_CODE))
6220 return 2;
6221
6222 /* We don't know exactly what INSN1 does. If INSN2 is
6223 also a coprocessor instruction, assume there must be
6224 a one instruction gap. */
6225 if (pinfo2 & INSN_COP)
6226 return 1;
6227 }
6228 }
6229
6230 /* Check for read-after-write dependencies on the coprocessor
6231 control registers in cases where INSN1 does not need a general
6232 coprocessor delay. This means that INSN1 is a floating point
6233 comparison instruction. */
6234 /* Itbl support may require additional care here. */
6235 else if (!cop_interlocks
6236 && (pinfo1 & INSN_WRITE_COND_CODE)
6237 && (pinfo2 & INSN_READ_COND_CODE))
6238 return 1;
6239 }
6240
6241 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6242 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6243 and pause. */
6244 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6245 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6246 || (insn2 && delayed_branch_p (insn2))))
6247 return 1;
6248
6249 return 0;
6250 }
6251
6252 /* Return the number of nops that would be needed to work around the
6253 VR4130 mflo/mfhi errata if instruction INSN immediately followed
6254 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6255 that are contained within the first IGNORE instructions of HIST. */
6256
6257 static int
6258 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
6259 const struct mips_cl_insn *insn)
6260 {
6261 int i, j;
6262 unsigned int mask;
6263
6264 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6265 are not affected by the errata. */
6266 if (insn != 0
6267 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6268 || strcmp (insn->insn_mo->name, "mtlo") == 0
6269 || strcmp (insn->insn_mo->name, "mthi") == 0))
6270 return 0;
6271
6272 /* Search for the first MFLO or MFHI. */
6273 for (i = 0; i < MAX_VR4130_NOPS; i++)
6274 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
6275 {
6276 /* Extract the destination register. */
6277 mask = gpr_write_mask (&hist[i]);
6278
6279 /* No nops are needed if INSN reads that register. */
6280 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
6281 return 0;
6282
6283 /* ...or if any of the intervening instructions do. */
6284 for (j = 0; j < i; j++)
6285 if (gpr_read_mask (&hist[j]) & mask)
6286 return 0;
6287
6288 if (i >= ignore)
6289 return MAX_VR4130_NOPS - i;
6290 }
6291 return 0;
6292 }
6293
6294 #define BASE_REG_EQ(INSN1, INSN2) \
6295 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
6296 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6297
6298 /* Return the minimum alignment for this store instruction. */
6299
6300 static int
6301 fix_24k_align_to (const struct mips_opcode *mo)
6302 {
6303 if (strcmp (mo->name, "sh") == 0)
6304 return 2;
6305
6306 if (strcmp (mo->name, "swc1") == 0
6307 || strcmp (mo->name, "swc2") == 0
6308 || strcmp (mo->name, "sw") == 0
6309 || strcmp (mo->name, "sc") == 0
6310 || strcmp (mo->name, "s.s") == 0)
6311 return 4;
6312
6313 if (strcmp (mo->name, "sdc1") == 0
6314 || strcmp (mo->name, "sdc2") == 0
6315 || strcmp (mo->name, "s.d") == 0)
6316 return 8;
6317
6318 /* sb, swl, swr */
6319 return 1;
6320 }
6321
6322 struct fix_24k_store_info
6323 {
6324 /* Immediate offset, if any, for this store instruction. */
6325 short off;
6326 /* Alignment required by this store instruction. */
6327 int align_to;
6328 /* True for register offsets. */
6329 int register_offset;
6330 };
6331
6332 /* Comparison function used by qsort. */
6333
6334 static int
6335 fix_24k_sort (const void *a, const void *b)
6336 {
6337 const struct fix_24k_store_info *pos1 = a;
6338 const struct fix_24k_store_info *pos2 = b;
6339
6340 return (pos1->off - pos2->off);
6341 }
6342
6343 /* INSN is a store instruction. Try to record the store information
6344 in STINFO. Return false if the information isn't known. */
6345
6346 static bfd_boolean
6347 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
6348 const struct mips_cl_insn *insn)
6349 {
6350 /* The instruction must have a known offset. */
6351 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6352 return FALSE;
6353
6354 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6355 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6356 return TRUE;
6357 }
6358
6359 /* Return the number of nops that would be needed to work around the 24k
6360 "lost data on stores during refill" errata if instruction INSN
6361 immediately followed the 2 instructions described by HIST.
6362 Ignore hazards that are contained within the first IGNORE
6363 instructions of HIST.
6364
6365 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6366 for the data cache refills and store data. The following describes
6367 the scenario where the store data could be lost.
6368
6369 * A data cache miss, due to either a load or a store, causing fill
6370 data to be supplied by the memory subsystem
6371 * The first three doublewords of fill data are returned and written
6372 into the cache
6373 * A sequence of four stores occurs in consecutive cycles around the
6374 final doubleword of the fill:
6375 * Store A
6376 * Store B
6377 * Store C
6378 * Zero, One or more instructions
6379 * Store D
6380
6381 The four stores A-D must be to different doublewords of the line that
6382 is being filled. The fourth instruction in the sequence above permits
6383 the fill of the final doubleword to be transferred from the FSB into
6384 the cache. In the sequence above, the stores may be either integer
6385 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6386 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6387 different doublewords on the line. If the floating point unit is
6388 running in 1:2 mode, it is not possible to create the sequence above
6389 using only floating point store instructions.
6390
6391 In this case, the cache line being filled is incorrectly marked
6392 invalid, thereby losing the data from any store to the line that
6393 occurs between the original miss and the completion of the five
6394 cycle sequence shown above.
6395
6396 The workarounds are:
6397
6398 * Run the data cache in write-through mode.
6399 * Insert a non-store instruction between
6400 Store A and Store B or Store B and Store C. */
6401
6402 static int
6403 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
6404 const struct mips_cl_insn *insn)
6405 {
6406 struct fix_24k_store_info pos[3];
6407 int align, i, base_offset;
6408
6409 if (ignore >= 2)
6410 return 0;
6411
6412 /* If the previous instruction wasn't a store, there's nothing to
6413 worry about. */
6414 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6415 return 0;
6416
6417 /* If the instructions after the previous one are unknown, we have
6418 to assume the worst. */
6419 if (!insn)
6420 return 1;
6421
6422 /* Check whether we are dealing with three consecutive stores. */
6423 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6424 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6425 return 0;
6426
6427 /* If we don't know the relationship between the store addresses,
6428 assume the worst. */
6429 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
6430 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6431 return 1;
6432
6433 if (!fix_24k_record_store_info (&pos[0], insn)
6434 || !fix_24k_record_store_info (&pos[1], &hist[0])
6435 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6436 return 1;
6437
6438 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6439
6440 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6441 X bytes and such that the base register + X is known to be aligned
6442 to align bytes. */
6443
6444 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6445 align = 8;
6446 else
6447 {
6448 align = pos[0].align_to;
6449 base_offset = pos[0].off;
6450 for (i = 1; i < 3; i++)
6451 if (align < pos[i].align_to)
6452 {
6453 align = pos[i].align_to;
6454 base_offset = pos[i].off;
6455 }
6456 for (i = 0; i < 3; i++)
6457 pos[i].off -= base_offset;
6458 }
6459
6460 pos[0].off &= ~align + 1;
6461 pos[1].off &= ~align + 1;
6462 pos[2].off &= ~align + 1;
6463
6464 /* If any two stores write to the same chunk, they also write to the
6465 same doubleword. The offsets are still sorted at this point. */
6466 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6467 return 0;
6468
6469 /* A range of at least 9 bytes is needed for the stores to be in
6470 non-overlapping doublewords. */
6471 if (pos[2].off - pos[0].off <= 8)
6472 return 0;
6473
6474 if (pos[2].off - pos[1].off >= 24
6475 || pos[1].off - pos[0].off >= 24
6476 || pos[2].off - pos[0].off >= 32)
6477 return 0;
6478
6479 return 1;
6480 }
6481
6482 /* Return the number of nops that would be needed if instruction INSN
6483 immediately followed the MAX_NOPS instructions given by HIST,
6484 where HIST[0] is the most recent instruction. Ignore hazards
6485 between INSN and the first IGNORE instructions in HIST.
6486
6487 If INSN is null, return the worse-case number of nops for any
6488 instruction. */
6489
6490 static int
6491 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
6492 const struct mips_cl_insn *insn)
6493 {
6494 int i, nops, tmp_nops;
6495
6496 nops = 0;
6497 for (i = ignore; i < MAX_DELAY_NOPS; i++)
6498 {
6499 tmp_nops = insns_between (hist + i, insn) - i;
6500 if (tmp_nops > nops)
6501 nops = tmp_nops;
6502 }
6503
6504 if (mips_fix_vr4130 && !mips_opts.micromips)
6505 {
6506 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
6507 if (tmp_nops > nops)
6508 nops = tmp_nops;
6509 }
6510
6511 if (mips_fix_24k && !mips_opts.micromips)
6512 {
6513 tmp_nops = nops_for_24k (ignore, hist, insn);
6514 if (tmp_nops > nops)
6515 nops = tmp_nops;
6516 }
6517
6518 return nops;
6519 }
6520
6521 /* The variable arguments provide NUM_INSNS extra instructions that
6522 might be added to HIST. Return the largest number of nops that
6523 would be needed after the extended sequence, ignoring hazards
6524 in the first IGNORE instructions. */
6525
6526 static int
6527 nops_for_sequence (int num_insns, int ignore,
6528 const struct mips_cl_insn *hist, ...)
6529 {
6530 va_list args;
6531 struct mips_cl_insn buffer[MAX_NOPS];
6532 struct mips_cl_insn *cursor;
6533 int nops;
6534
6535 va_start (args, hist);
6536 cursor = buffer + num_insns;
6537 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
6538 while (cursor > buffer)
6539 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6540
6541 nops = nops_for_insn (ignore, buffer, NULL);
6542 va_end (args);
6543 return nops;
6544 }
6545
6546 /* Like nops_for_insn, but if INSN is a branch, take into account the
6547 worst-case delay for the branch target. */
6548
6549 static int
6550 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
6551 const struct mips_cl_insn *insn)
6552 {
6553 int nops, tmp_nops;
6554
6555 nops = nops_for_insn (ignore, hist, insn);
6556 if (delayed_branch_p (insn))
6557 {
6558 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
6559 hist, insn, get_delay_slot_nop (insn));
6560 if (tmp_nops > nops)
6561 nops = tmp_nops;
6562 }
6563 else if (compact_branch_p (insn))
6564 {
6565 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
6566 if (tmp_nops > nops)
6567 nops = tmp_nops;
6568 }
6569 return nops;
6570 }
6571
6572 /* Fix NOP issue: Replace nops by "or at,at,zero". */
6573
6574 static void
6575 fix_loongson2f_nop (struct mips_cl_insn * ip)
6576 {
6577 gas_assert (!HAVE_CODE_COMPRESSION);
6578 if (strcmp (ip->insn_mo->name, "nop") == 0)
6579 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6580 }
6581
6582 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6583 jr target pc &= 'hffff_ffff_cfff_ffff. */
6584
6585 static void
6586 fix_loongson2f_jump (struct mips_cl_insn * ip)
6587 {
6588 gas_assert (!HAVE_CODE_COMPRESSION);
6589 if (strcmp (ip->insn_mo->name, "j") == 0
6590 || strcmp (ip->insn_mo->name, "jr") == 0
6591 || strcmp (ip->insn_mo->name, "jalr") == 0)
6592 {
6593 int sreg;
6594 expressionS ep;
6595
6596 if (! mips_opts.at)
6597 return;
6598
6599 sreg = EXTRACT_OPERAND (0, RS, *ip);
6600 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6601 return;
6602
6603 ep.X_op = O_constant;
6604 ep.X_add_number = 0xcfff0000;
6605 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6606 ep.X_add_number = 0xffff;
6607 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6608 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6609 }
6610 }
6611
6612 static void
6613 fix_loongson2f (struct mips_cl_insn * ip)
6614 {
6615 if (mips_fix_loongson2f_nop)
6616 fix_loongson2f_nop (ip);
6617
6618 if (mips_fix_loongson2f_jump)
6619 fix_loongson2f_jump (ip);
6620 }
6621
6622 /* IP is a branch that has a delay slot, and we need to fill it
6623 automatically. Return true if we can do that by swapping IP
6624 with the previous instruction.
6625 ADDRESS_EXPR is an operand of the instruction to be used with
6626 RELOC_TYPE. */
6627
6628 static bfd_boolean
6629 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
6630 bfd_reloc_code_real_type *reloc_type)
6631 {
6632 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
6633 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
6634 unsigned int fpr_read, prev_fpr_write;
6635
6636 /* -O2 and above is required for this optimization. */
6637 if (mips_optimize < 2)
6638 return FALSE;
6639
6640 /* If we have seen .set volatile or .set nomove, don't optimize. */
6641 if (mips_opts.nomove)
6642 return FALSE;
6643
6644 /* We can't swap if the previous instruction's position is fixed. */
6645 if (history[0].fixed_p)
6646 return FALSE;
6647
6648 /* If the previous previous insn was in a .set noreorder, we can't
6649 swap. Actually, the MIPS assembler will swap in this situation.
6650 However, gcc configured -with-gnu-as will generate code like
6651
6652 .set noreorder
6653 lw $4,XXX
6654 .set reorder
6655 INSN
6656 bne $4,$0,foo
6657
6658 in which we can not swap the bne and INSN. If gcc is not configured
6659 -with-gnu-as, it does not output the .set pseudo-ops. */
6660 if (history[1].noreorder_p)
6661 return FALSE;
6662
6663 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6664 This means that the previous instruction was a 4-byte one anyhow. */
6665 if (mips_opts.mips16 && history[0].fixp[0])
6666 return FALSE;
6667
6668 /* If the branch is itself the target of a branch, we can not swap.
6669 We cheat on this; all we check for is whether there is a label on
6670 this instruction. If there are any branches to anything other than
6671 a label, users must use .set noreorder. */
6672 if (seg_info (now_seg)->label_list)
6673 return FALSE;
6674
6675 /* If the previous instruction is in a variant frag other than this
6676 branch's one, we cannot do the swap. This does not apply to
6677 MIPS16 code, which uses variant frags for different purposes. */
6678 if (!mips_opts.mips16
6679 && history[0].frag
6680 && history[0].frag->fr_type == rs_machine_dependent)
6681 return FALSE;
6682
6683 /* We do not swap with instructions that cannot architecturally
6684 be placed in a branch delay slot, such as SYNC or ERET. We
6685 also refrain from swapping with a trap instruction, since it
6686 complicates trap handlers to have the trap instruction be in
6687 a delay slot. */
6688 prev_pinfo = history[0].insn_mo->pinfo;
6689 if (prev_pinfo & INSN_NO_DELAY_SLOT)
6690 return FALSE;
6691
6692 /* Check for conflicts between the branch and the instructions
6693 before the candidate delay slot. */
6694 if (nops_for_insn (0, history + 1, ip) > 0)
6695 return FALSE;
6696
6697 /* Check for conflicts between the swapped sequence and the
6698 target of the branch. */
6699 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6700 return FALSE;
6701
6702 /* If the branch reads a register that the previous
6703 instruction sets, we can not swap. */
6704 gpr_read = gpr_read_mask (ip);
6705 prev_gpr_write = gpr_write_mask (&history[0]);
6706 if (gpr_read & prev_gpr_write)
6707 return FALSE;
6708
6709 fpr_read = fpr_read_mask (ip);
6710 prev_fpr_write = fpr_write_mask (&history[0]);
6711 if (fpr_read & prev_fpr_write)
6712 return FALSE;
6713
6714 /* If the branch writes a register that the previous
6715 instruction sets, we can not swap. */
6716 gpr_write = gpr_write_mask (ip);
6717 if (gpr_write & prev_gpr_write)
6718 return FALSE;
6719
6720 /* If the branch writes a register that the previous
6721 instruction reads, we can not swap. */
6722 prev_gpr_read = gpr_read_mask (&history[0]);
6723 if (gpr_write & prev_gpr_read)
6724 return FALSE;
6725
6726 /* If one instruction sets a condition code and the
6727 other one uses a condition code, we can not swap. */
6728 pinfo = ip->insn_mo->pinfo;
6729 if ((pinfo & INSN_READ_COND_CODE)
6730 && (prev_pinfo & INSN_WRITE_COND_CODE))
6731 return FALSE;
6732 if ((pinfo & INSN_WRITE_COND_CODE)
6733 && (prev_pinfo & INSN_READ_COND_CODE))
6734 return FALSE;
6735
6736 /* If the previous instruction uses the PC, we can not swap. */
6737 prev_pinfo2 = history[0].insn_mo->pinfo2;
6738 if (prev_pinfo2 & INSN2_READ_PC)
6739 return FALSE;
6740
6741 /* If the previous instruction has an incorrect size for a fixed
6742 branch delay slot in microMIPS mode, we cannot swap. */
6743 pinfo2 = ip->insn_mo->pinfo2;
6744 if (mips_opts.micromips
6745 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6746 && insn_length (history) != 2)
6747 return FALSE;
6748 if (mips_opts.micromips
6749 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6750 && insn_length (history) != 4)
6751 return FALSE;
6752
6753 /* On R5900 short loops need to be fixed by inserting a nop in
6754 the branch delay slots.
6755 A short loop can be terminated too early. */
6756 if (mips_opts.arch == CPU_R5900
6757 /* Check if instruction has a parameter, ignore "j $31". */
6758 && (address_expr != NULL)
6759 /* Parameter must be 16 bit. */
6760 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6761 /* Branch to same segment. */
6762 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
6763 /* Branch to same code fragment. */
6764 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
6765 /* Can only calculate branch offset if value is known. */
6766 && symbol_constant_p (address_expr->X_add_symbol)
6767 /* Check if branch is really conditional. */
6768 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6769 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6770 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6771 {
6772 int distance;
6773 /* Check if loop is shorter than 6 instructions including
6774 branch and delay slot. */
6775 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
6776 if (distance <= 20)
6777 {
6778 int i;
6779 int rv;
6780
6781 rv = FALSE;
6782 /* When the loop includes branches or jumps,
6783 it is not a short loop. */
6784 for (i = 0; i < (distance / 4); i++)
6785 {
6786 if ((history[i].cleared_p)
6787 || delayed_branch_p (&history[i]))
6788 {
6789 rv = TRUE;
6790 break;
6791 }
6792 }
6793 if (rv == FALSE)
6794 {
6795 /* Insert nop after branch to fix short loop. */
6796 return FALSE;
6797 }
6798 }
6799 }
6800
6801 return TRUE;
6802 }
6803
6804 /* Decide how we should add IP to the instruction stream.
6805 ADDRESS_EXPR is an operand of the instruction to be used with
6806 RELOC_TYPE. */
6807
6808 static enum append_method
6809 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
6810 bfd_reloc_code_real_type *reloc_type)
6811 {
6812 /* The relaxed version of a macro sequence must be inherently
6813 hazard-free. */
6814 if (mips_relax.sequence == 2)
6815 return APPEND_ADD;
6816
6817 /* We must not dabble with instructions in a ".set noreorder" block. */
6818 if (mips_opts.noreorder)
6819 return APPEND_ADD;
6820
6821 /* Otherwise, it's our responsibility to fill branch delay slots. */
6822 if (delayed_branch_p (ip))
6823 {
6824 if (!branch_likely_p (ip)
6825 && can_swap_branch_p (ip, address_expr, reloc_type))
6826 return APPEND_SWAP;
6827
6828 if (mips_opts.mips16
6829 && ISA_SUPPORTS_MIPS16E
6830 && gpr_read_mask (ip) != 0)
6831 return APPEND_ADD_COMPACT;
6832
6833 return APPEND_ADD_WITH_NOP;
6834 }
6835
6836 return APPEND_ADD;
6837 }
6838
6839 /* IP is a MIPS16 instruction whose opcode we have just changed.
6840 Point IP->insn_mo to the new opcode's definition. */
6841
6842 static void
6843 find_altered_mips16_opcode (struct mips_cl_insn *ip)
6844 {
6845 const struct mips_opcode *mo, *end;
6846
6847 end = &mips16_opcodes[bfd_mips16_num_opcodes];
6848 for (mo = ip->insn_mo; mo < end; mo++)
6849 if ((ip->insn_opcode & mo->mask) == mo->match)
6850 {
6851 ip->insn_mo = mo;
6852 return;
6853 }
6854 abort ();
6855 }
6856
6857 /* For microMIPS macros, we need to generate a local number label
6858 as the target of branches. */
6859 #define MICROMIPS_LABEL_CHAR '\037'
6860 static unsigned long micromips_target_label;
6861 static char micromips_target_name[32];
6862
6863 static char *
6864 micromips_label_name (void)
6865 {
6866 char *p = micromips_target_name;
6867 char symbol_name_temporary[24];
6868 unsigned long l;
6869 int i;
6870
6871 if (*p)
6872 return p;
6873
6874 i = 0;
6875 l = micromips_target_label;
6876 #ifdef LOCAL_LABEL_PREFIX
6877 *p++ = LOCAL_LABEL_PREFIX;
6878 #endif
6879 *p++ = 'L';
6880 *p++ = MICROMIPS_LABEL_CHAR;
6881 do
6882 {
6883 symbol_name_temporary[i++] = l % 10 + '0';
6884 l /= 10;
6885 }
6886 while (l != 0);
6887 while (i > 0)
6888 *p++ = symbol_name_temporary[--i];
6889 *p = '\0';
6890
6891 return micromips_target_name;
6892 }
6893
6894 static void
6895 micromips_label_expr (expressionS *label_expr)
6896 {
6897 label_expr->X_op = O_symbol;
6898 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
6899 label_expr->X_add_number = 0;
6900 }
6901
6902 static void
6903 micromips_label_inc (void)
6904 {
6905 micromips_target_label++;
6906 *micromips_target_name = '\0';
6907 }
6908
6909 static void
6910 micromips_add_label (void)
6911 {
6912 symbolS *s;
6913
6914 s = colon (micromips_label_name ());
6915 micromips_label_inc ();
6916 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
6917 }
6918
6919 /* If assembling microMIPS code, then return the microMIPS reloc
6920 corresponding to the requested one if any. Otherwise return
6921 the reloc unchanged. */
6922
6923 static bfd_reloc_code_real_type
6924 micromips_map_reloc (bfd_reloc_code_real_type reloc)
6925 {
6926 static const bfd_reloc_code_real_type relocs[][2] =
6927 {
6928 /* Keep sorted incrementally by the left-hand key. */
6929 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
6930 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
6931 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
6932 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
6933 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
6934 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
6935 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
6936 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
6937 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
6938 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
6939 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
6940 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
6941 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
6942 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
6943 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
6944 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
6945 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
6946 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
6947 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
6948 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
6949 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
6950 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
6951 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
6952 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
6953 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
6954 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
6955 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
6956 };
6957 bfd_reloc_code_real_type r;
6958 size_t i;
6959
6960 if (!mips_opts.micromips)
6961 return reloc;
6962 for (i = 0; i < ARRAY_SIZE (relocs); i++)
6963 {
6964 r = relocs[i][0];
6965 if (r > reloc)
6966 return reloc;
6967 if (r == reloc)
6968 return relocs[i][1];
6969 }
6970 return reloc;
6971 }
6972
6973 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
6974 Return true on success, storing the resolved value in RESULT. */
6975
6976 static bfd_boolean
6977 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
6978 offsetT *result)
6979 {
6980 switch (reloc)
6981 {
6982 case BFD_RELOC_MIPS_HIGHEST:
6983 case BFD_RELOC_MICROMIPS_HIGHEST:
6984 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
6985 return TRUE;
6986
6987 case BFD_RELOC_MIPS_HIGHER:
6988 case BFD_RELOC_MICROMIPS_HIGHER:
6989 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
6990 return TRUE;
6991
6992 case BFD_RELOC_HI16_S:
6993 case BFD_RELOC_HI16_S_PCREL:
6994 case BFD_RELOC_MICROMIPS_HI16_S:
6995 case BFD_RELOC_MIPS16_HI16_S:
6996 *result = ((operand + 0x8000) >> 16) & 0xffff;
6997 return TRUE;
6998
6999 case BFD_RELOC_HI16:
7000 case BFD_RELOC_MICROMIPS_HI16:
7001 case BFD_RELOC_MIPS16_HI16:
7002 *result = (operand >> 16) & 0xffff;
7003 return TRUE;
7004
7005 case BFD_RELOC_LO16:
7006 case BFD_RELOC_LO16_PCREL:
7007 case BFD_RELOC_MICROMIPS_LO16:
7008 case BFD_RELOC_MIPS16_LO16:
7009 *result = operand & 0xffff;
7010 return TRUE;
7011
7012 case BFD_RELOC_UNUSED:
7013 *result = operand;
7014 return TRUE;
7015
7016 default:
7017 return FALSE;
7018 }
7019 }
7020
7021 /* Output an instruction. IP is the instruction information.
7022 ADDRESS_EXPR is an operand of the instruction to be used with
7023 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7024 a macro expansion. */
7025
7026 static void
7027 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
7028 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
7029 {
7030 unsigned long prev_pinfo2, pinfo;
7031 bfd_boolean relaxed_branch = FALSE;
7032 enum append_method method;
7033 bfd_boolean relax32;
7034 int branch_disp;
7035
7036 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
7037 fix_loongson2f (ip);
7038
7039 file_ase_mips16 |= mips_opts.mips16;
7040 file_ase_micromips |= mips_opts.micromips;
7041
7042 prev_pinfo2 = history[0].insn_mo->pinfo2;
7043 pinfo = ip->insn_mo->pinfo;
7044
7045 if (mips_opts.micromips
7046 && !expansionp
7047 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7048 && micromips_insn_length (ip->insn_mo) != 2)
7049 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7050 && micromips_insn_length (ip->insn_mo) != 4)))
7051 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
7052 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
7053
7054 if (address_expr == NULL)
7055 ip->complete_p = 1;
7056 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7057 && reloc_type[1] == BFD_RELOC_UNUSED
7058 && reloc_type[2] == BFD_RELOC_UNUSED
7059 && address_expr->X_op == O_constant)
7060 {
7061 switch (*reloc_type)
7062 {
7063 case BFD_RELOC_MIPS_JMP:
7064 {
7065 int shift;
7066
7067 /* Shift is 2, unusually, for microMIPS JALX. */
7068 shift = (mips_opts.micromips
7069 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
7070 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7071 as_bad (_("jump to misaligned address (0x%lx)"),
7072 (unsigned long) address_expr->X_add_number);
7073 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7074 & 0x3ffffff);
7075 ip->complete_p = 1;
7076 }
7077 break;
7078
7079 case BFD_RELOC_MIPS16_JMP:
7080 if ((address_expr->X_add_number & 3) != 0)
7081 as_bad (_("jump to misaligned address (0x%lx)"),
7082 (unsigned long) address_expr->X_add_number);
7083 ip->insn_opcode |=
7084 (((address_expr->X_add_number & 0x7c0000) << 3)
7085 | ((address_expr->X_add_number & 0xf800000) >> 7)
7086 | ((address_expr->X_add_number & 0x3fffc) >> 2));
7087 ip->complete_p = 1;
7088 break;
7089
7090 case BFD_RELOC_16_PCREL_S2:
7091 {
7092 int shift;
7093
7094 shift = mips_opts.micromips ? 1 : 2;
7095 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7096 as_bad (_("branch to misaligned address (0x%lx)"),
7097 (unsigned long) address_expr->X_add_number);
7098 if (!mips_relax_branch)
7099 {
7100 if ((address_expr->X_add_number + (1 << (shift + 15)))
7101 & ~((1 << (shift + 16)) - 1))
7102 as_bad (_("branch address range overflow (0x%lx)"),
7103 (unsigned long) address_expr->X_add_number);
7104 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7105 & 0xffff);
7106 }
7107 }
7108 break;
7109
7110 case BFD_RELOC_MIPS_21_PCREL_S2:
7111 {
7112 int shift;
7113
7114 shift = 2;
7115 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7116 as_bad (_("branch to misaligned address (0x%lx)"),
7117 (unsigned long) address_expr->X_add_number);
7118 if ((address_expr->X_add_number + (1 << (shift + 20)))
7119 & ~((1 << (shift + 21)) - 1))
7120 as_bad (_("branch address range overflow (0x%lx)"),
7121 (unsigned long) address_expr->X_add_number);
7122 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7123 & 0x1fffff);
7124 }
7125 break;
7126
7127 case BFD_RELOC_MIPS_26_PCREL_S2:
7128 {
7129 int shift;
7130
7131 shift = 2;
7132 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7133 as_bad (_("branch to misaligned address (0x%lx)"),
7134 (unsigned long) address_expr->X_add_number);
7135 if ((address_expr->X_add_number + (1 << (shift + 25)))
7136 & ~((1 << (shift + 26)) - 1))
7137 as_bad (_("branch address range overflow (0x%lx)"),
7138 (unsigned long) address_expr->X_add_number);
7139 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7140 & 0x3ffffff);
7141 }
7142 break;
7143
7144 default:
7145 {
7146 offsetT value;
7147
7148 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7149 &value))
7150 {
7151 ip->insn_opcode |= value & 0xffff;
7152 ip->complete_p = 1;
7153 }
7154 }
7155 break;
7156 }
7157 }
7158
7159 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7160 {
7161 /* There are a lot of optimizations we could do that we don't.
7162 In particular, we do not, in general, reorder instructions.
7163 If you use gcc with optimization, it will reorder
7164 instructions and generally do much more optimization then we
7165 do here; repeating all that work in the assembler would only
7166 benefit hand written assembly code, and does not seem worth
7167 it. */
7168 int nops = (mips_optimize == 0
7169 ? nops_for_insn (0, history, NULL)
7170 : nops_for_insn_or_target (0, history, ip));
7171 if (nops > 0)
7172 {
7173 fragS *old_frag;
7174 unsigned long old_frag_offset;
7175 int i;
7176
7177 old_frag = frag_now;
7178 old_frag_offset = frag_now_fix ();
7179
7180 for (i = 0; i < nops; i++)
7181 add_fixed_insn (NOP_INSN);
7182 insert_into_history (0, nops, NOP_INSN);
7183
7184 if (listing)
7185 {
7186 listing_prev_line ();
7187 /* We may be at the start of a variant frag. In case we
7188 are, make sure there is enough space for the frag
7189 after the frags created by listing_prev_line. The
7190 argument to frag_grow here must be at least as large
7191 as the argument to all other calls to frag_grow in
7192 this file. We don't have to worry about being in the
7193 middle of a variant frag, because the variants insert
7194 all needed nop instructions themselves. */
7195 frag_grow (40);
7196 }
7197
7198 mips_move_text_labels ();
7199
7200 #ifndef NO_ECOFF_DEBUGGING
7201 if (ECOFF_DEBUGGING)
7202 ecoff_fix_loc (old_frag, old_frag_offset);
7203 #endif
7204 }
7205 }
7206 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7207 {
7208 int nops;
7209
7210 /* Work out how many nops in prev_nop_frag are needed by IP,
7211 ignoring hazards generated by the first prev_nop_frag_since
7212 instructions. */
7213 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
7214 gas_assert (nops <= prev_nop_frag_holds);
7215
7216 /* Enforce NOPS as a minimum. */
7217 if (nops > prev_nop_frag_required)
7218 prev_nop_frag_required = nops;
7219
7220 if (prev_nop_frag_holds == prev_nop_frag_required)
7221 {
7222 /* Settle for the current number of nops. Update the history
7223 accordingly (for the benefit of any future .set reorder code). */
7224 prev_nop_frag = NULL;
7225 insert_into_history (prev_nop_frag_since,
7226 prev_nop_frag_holds, NOP_INSN);
7227 }
7228 else
7229 {
7230 /* Allow this instruction to replace one of the nops that was
7231 tentatively added to prev_nop_frag. */
7232 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
7233 prev_nop_frag_holds--;
7234 prev_nop_frag_since++;
7235 }
7236 }
7237
7238 method = get_append_method (ip, address_expr, reloc_type);
7239 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
7240
7241 dwarf2_emit_insn (0);
7242 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7243 so "move" the instruction address accordingly.
7244
7245 Also, it doesn't seem appropriate for the assembler to reorder .loc
7246 entries. If this instruction is a branch that we are going to swap
7247 with the previous instruction, the two instructions should be
7248 treated as a unit, and the debug information for both instructions
7249 should refer to the start of the branch sequence. Using the
7250 current position is certainly wrong when swapping a 32-bit branch
7251 and a 16-bit delay slot, since the current position would then be
7252 in the middle of a branch. */
7253 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
7254
7255 relax32 = (mips_relax_branch
7256 /* Don't try branch relaxation within .set nomacro, or within
7257 .set noat if we use $at for PIC computations. If it turns
7258 out that the branch was out-of-range, we'll get an error. */
7259 && !mips_opts.warn_about_macros
7260 && (mips_opts.at || mips_pic == NO_PIC)
7261 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7262 as they have no complementing branches. */
7263 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
7264
7265 if (!HAVE_CODE_COMPRESSION
7266 && address_expr
7267 && relax32
7268 && *reloc_type == BFD_RELOC_16_PCREL_S2
7269 && delayed_branch_p (ip))
7270 {
7271 relaxed_branch = TRUE;
7272 add_relaxed_insn (ip, (relaxed_branch_length
7273 (NULL, NULL,
7274 uncond_branch_p (ip) ? -1
7275 : branch_likely_p (ip) ? 1
7276 : 0)), 4,
7277 RELAX_BRANCH_ENCODE
7278 (AT,
7279 uncond_branch_p (ip),
7280 branch_likely_p (ip),
7281 pinfo & INSN_WRITE_GPR_31,
7282 0),
7283 address_expr->X_add_symbol,
7284 address_expr->X_add_number);
7285 *reloc_type = BFD_RELOC_UNUSED;
7286 }
7287 else if (mips_opts.micromips
7288 && address_expr
7289 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7290 || *reloc_type > BFD_RELOC_UNUSED)
7291 && (delayed_branch_p (ip) || compact_branch_p (ip))
7292 /* Don't try branch relaxation when users specify
7293 16-bit/32-bit instructions. */
7294 && !forced_insn_length)
7295 {
7296 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
7297 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
7298 int uncond = uncond_branch_p (ip) ? -1 : 0;
7299 int compact = compact_branch_p (ip);
7300 int al = pinfo & INSN_WRITE_GPR_31;
7301 int length32;
7302
7303 gas_assert (address_expr != NULL);
7304 gas_assert (!mips_relax.sequence);
7305
7306 relaxed_branch = TRUE;
7307 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7308 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
7309 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
7310 uncond, compact, al,
7311 relax32, 0, 0),
7312 address_expr->X_add_symbol,
7313 address_expr->X_add_number);
7314 *reloc_type = BFD_RELOC_UNUSED;
7315 }
7316 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
7317 {
7318 symbolS *symbol;
7319 offsetT offset;
7320
7321 /* We need to set up a variant frag. */
7322 gas_assert (address_expr != NULL);
7323 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7324 symbol created by `make_expr_symbol' may not get a necessary
7325 external relocation produced. */
7326 if (address_expr->X_op == O_symbol)
7327 {
7328 symbol = address_expr->X_add_symbol;
7329 offset = address_expr->X_add_number;
7330 }
7331 else
7332 {
7333 symbol = make_expr_symbol (address_expr);
7334 offset = 0;
7335 }
7336 add_relaxed_insn (ip, 4, 0,
7337 RELAX_MIPS16_ENCODE
7338 (*reloc_type - BFD_RELOC_UNUSED,
7339 forced_insn_length == 2, forced_insn_length == 4,
7340 delayed_branch_p (&history[0]),
7341 history[0].mips16_absolute_jump_p),
7342 symbol, offset);
7343 }
7344 else if (mips_opts.mips16 && insn_length (ip) == 2)
7345 {
7346 if (!delayed_branch_p (ip))
7347 /* Make sure there is enough room to swap this instruction with
7348 a following jump instruction. */
7349 frag_grow (6);
7350 add_fixed_insn (ip);
7351 }
7352 else
7353 {
7354 if (mips_opts.mips16
7355 && mips_opts.noreorder
7356 && delayed_branch_p (&history[0]))
7357 as_warn (_("extended instruction in delay slot"));
7358
7359 if (mips_relax.sequence)
7360 {
7361 /* If we've reached the end of this frag, turn it into a variant
7362 frag and record the information for the instructions we've
7363 written so far. */
7364 if (frag_room () < 4)
7365 relax_close_frag ();
7366 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
7367 }
7368
7369 if (mips_relax.sequence != 2)
7370 {
7371 if (mips_macro_warning.first_insn_sizes[0] == 0)
7372 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7373 mips_macro_warning.sizes[0] += insn_length (ip);
7374 mips_macro_warning.insns[0]++;
7375 }
7376 if (mips_relax.sequence != 1)
7377 {
7378 if (mips_macro_warning.first_insn_sizes[1] == 0)
7379 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7380 mips_macro_warning.sizes[1] += insn_length (ip);
7381 mips_macro_warning.insns[1]++;
7382 }
7383
7384 if (mips_opts.mips16)
7385 {
7386 ip->fixed_p = 1;
7387 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7388 }
7389 add_fixed_insn (ip);
7390 }
7391
7392 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
7393 {
7394 bfd_reloc_code_real_type final_type[3];
7395 reloc_howto_type *howto0;
7396 reloc_howto_type *howto;
7397 int i;
7398
7399 /* Perform any necessary conversion to microMIPS relocations
7400 and find out how many relocations there actually are. */
7401 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7402 final_type[i] = micromips_map_reloc (reloc_type[i]);
7403
7404 /* In a compound relocation, it is the final (outermost)
7405 operator that determines the relocated field. */
7406 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
7407 if (!howto)
7408 abort ();
7409
7410 if (i > 1)
7411 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
7412 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7413 bfd_get_reloc_size (howto),
7414 address_expr,
7415 howto0 && howto0->pc_relative,
7416 final_type[0]);
7417
7418 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
7419 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
7420 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7421
7422 /* These relocations can have an addend that won't fit in
7423 4 octets for 64bit assembly. */
7424 if (GPR_SIZE == 64
7425 && ! howto->partial_inplace
7426 && (reloc_type[0] == BFD_RELOC_16
7427 || reloc_type[0] == BFD_RELOC_32
7428 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7429 || reloc_type[0] == BFD_RELOC_GPREL16
7430 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7431 || reloc_type[0] == BFD_RELOC_GPREL32
7432 || reloc_type[0] == BFD_RELOC_64
7433 || reloc_type[0] == BFD_RELOC_CTOR
7434 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7435 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7436 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7437 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7438 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7439 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7440 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7441 || hi16_reloc_p (reloc_type[0])
7442 || lo16_reloc_p (reloc_type[0])))
7443 ip->fixp[0]->fx_no_overflow = 1;
7444
7445 /* These relocations can have an addend that won't fit in 2 octets. */
7446 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7447 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7448 ip->fixp[0]->fx_no_overflow = 1;
7449
7450 if (mips_relax.sequence)
7451 {
7452 if (mips_relax.first_fixup == 0)
7453 mips_relax.first_fixup = ip->fixp[0];
7454 }
7455 else if (reloc_needs_lo_p (*reloc_type))
7456 {
7457 struct mips_hi_fixup *hi_fixup;
7458
7459 /* Reuse the last entry if it already has a matching %lo. */
7460 hi_fixup = mips_hi_fixup_list;
7461 if (hi_fixup == 0
7462 || !fixup_has_matching_lo_p (hi_fixup->fixp))
7463 {
7464 hi_fixup = XNEW (struct mips_hi_fixup);
7465 hi_fixup->next = mips_hi_fixup_list;
7466 mips_hi_fixup_list = hi_fixup;
7467 }
7468 hi_fixup->fixp = ip->fixp[0];
7469 hi_fixup->seg = now_seg;
7470 }
7471
7472 /* Add fixups for the second and third relocations, if given.
7473 Note that the ABI allows the second relocation to be
7474 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7475 moment we only use RSS_UNDEF, but we could add support
7476 for the others if it ever becomes necessary. */
7477 for (i = 1; i < 3; i++)
7478 if (reloc_type[i] != BFD_RELOC_UNUSED)
7479 {
7480 ip->fixp[i] = fix_new (ip->frag, ip->where,
7481 ip->fixp[0]->fx_size, NULL, 0,
7482 FALSE, final_type[i]);
7483
7484 /* Use fx_tcbit to mark compound relocs. */
7485 ip->fixp[0]->fx_tcbit = 1;
7486 ip->fixp[i]->fx_tcbit = 1;
7487 }
7488 }
7489
7490 /* Update the register mask information. */
7491 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7492 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
7493
7494 switch (method)
7495 {
7496 case APPEND_ADD:
7497 insert_into_history (0, 1, ip);
7498 break;
7499
7500 case APPEND_ADD_WITH_NOP:
7501 {
7502 struct mips_cl_insn *nop;
7503
7504 insert_into_history (0, 1, ip);
7505 nop = get_delay_slot_nop (ip);
7506 add_fixed_insn (nop);
7507 insert_into_history (0, 1, nop);
7508 if (mips_relax.sequence)
7509 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7510 }
7511 break;
7512
7513 case APPEND_ADD_COMPACT:
7514 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7515 gas_assert (mips_opts.mips16);
7516 ip->insn_opcode |= 0x0080;
7517 find_altered_mips16_opcode (ip);
7518 install_insn (ip);
7519 insert_into_history (0, 1, ip);
7520 break;
7521
7522 case APPEND_SWAP:
7523 {
7524 struct mips_cl_insn delay = history[0];
7525
7526 if (relaxed_branch || delay.frag != ip->frag)
7527 {
7528 /* Add the delay slot instruction to the end of the
7529 current frag and shrink the fixed part of the
7530 original frag. If the branch occupies the tail of
7531 the latter, move it backwards to cover the gap. */
7532 delay.frag->fr_fix -= branch_disp;
7533 if (delay.frag == ip->frag)
7534 move_insn (ip, ip->frag, ip->where - branch_disp);
7535 add_fixed_insn (&delay);
7536 }
7537 else
7538 {
7539 /* If this is not a relaxed branch and we are in the
7540 same frag, then just swap the instructions. */
7541 move_insn (ip, delay.frag, delay.where);
7542 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
7543 }
7544 history[0] = *ip;
7545 delay.fixed_p = 1;
7546 insert_into_history (0, 1, &delay);
7547 }
7548 break;
7549 }
7550
7551 /* If we have just completed an unconditional branch, clear the history. */
7552 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7553 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
7554 {
7555 unsigned int i;
7556
7557 mips_no_prev_insn ();
7558
7559 for (i = 0; i < ARRAY_SIZE (history); i++)
7560 history[i].cleared_p = 1;
7561 }
7562
7563 /* We need to emit a label at the end of branch-likely macros. */
7564 if (emit_branch_likely_macro)
7565 {
7566 emit_branch_likely_macro = FALSE;
7567 micromips_add_label ();
7568 }
7569
7570 /* We just output an insn, so the next one doesn't have a label. */
7571 mips_clear_insn_labels ();
7572 }
7573
7574 /* Forget that there was any previous instruction or label.
7575 When BRANCH is true, the branch history is also flushed. */
7576
7577 static void
7578 mips_no_prev_insn (void)
7579 {
7580 prev_nop_frag = NULL;
7581 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
7582 mips_clear_insn_labels ();
7583 }
7584
7585 /* This function must be called before we emit something other than
7586 instructions. It is like mips_no_prev_insn except that it inserts
7587 any NOPS that might be needed by previous instructions. */
7588
7589 void
7590 mips_emit_delays (void)
7591 {
7592 if (! mips_opts.noreorder)
7593 {
7594 int nops = nops_for_insn (0, history, NULL);
7595 if (nops > 0)
7596 {
7597 while (nops-- > 0)
7598 add_fixed_insn (NOP_INSN);
7599 mips_move_text_labels ();
7600 }
7601 }
7602 mips_no_prev_insn ();
7603 }
7604
7605 /* Start a (possibly nested) noreorder block. */
7606
7607 static void
7608 start_noreorder (void)
7609 {
7610 if (mips_opts.noreorder == 0)
7611 {
7612 unsigned int i;
7613 int nops;
7614
7615 /* None of the instructions before the .set noreorder can be moved. */
7616 for (i = 0; i < ARRAY_SIZE (history); i++)
7617 history[i].fixed_p = 1;
7618
7619 /* Insert any nops that might be needed between the .set noreorder
7620 block and the previous instructions. We will later remove any
7621 nops that turn out not to be needed. */
7622 nops = nops_for_insn (0, history, NULL);
7623 if (nops > 0)
7624 {
7625 if (mips_optimize != 0)
7626 {
7627 /* Record the frag which holds the nop instructions, so
7628 that we can remove them if we don't need them. */
7629 frag_grow (nops * NOP_INSN_SIZE);
7630 prev_nop_frag = frag_now;
7631 prev_nop_frag_holds = nops;
7632 prev_nop_frag_required = 0;
7633 prev_nop_frag_since = 0;
7634 }
7635
7636 for (; nops > 0; --nops)
7637 add_fixed_insn (NOP_INSN);
7638
7639 /* Move on to a new frag, so that it is safe to simply
7640 decrease the size of prev_nop_frag. */
7641 frag_wane (frag_now);
7642 frag_new (0);
7643 mips_move_text_labels ();
7644 }
7645 mips_mark_labels ();
7646 mips_clear_insn_labels ();
7647 }
7648 mips_opts.noreorder++;
7649 mips_any_noreorder = 1;
7650 }
7651
7652 /* End a nested noreorder block. */
7653
7654 static void
7655 end_noreorder (void)
7656 {
7657 mips_opts.noreorder--;
7658 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7659 {
7660 /* Commit to inserting prev_nop_frag_required nops and go back to
7661 handling nop insertion the .set reorder way. */
7662 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
7663 * NOP_INSN_SIZE);
7664 insert_into_history (prev_nop_frag_since,
7665 prev_nop_frag_required, NOP_INSN);
7666 prev_nop_frag = NULL;
7667 }
7668 }
7669
7670 /* Sign-extend 32-bit mode constants that have bit 31 set and all
7671 higher bits unset. */
7672
7673 static void
7674 normalize_constant_expr (expressionS *ex)
7675 {
7676 if (ex->X_op == O_constant
7677 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7678 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7679 - 0x80000000);
7680 }
7681
7682 /* Sign-extend 32-bit mode address offsets that have bit 31 set and
7683 all higher bits unset. */
7684
7685 static void
7686 normalize_address_expr (expressionS *ex)
7687 {
7688 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7689 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7690 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7691 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7692 - 0x80000000);
7693 }
7694
7695 /* Try to match TOKENS against OPCODE, storing the result in INSN.
7696 Return true if the match was successful.
7697
7698 OPCODE_EXTRA is a value that should be ORed into the opcode
7699 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
7700 there are more alternatives after OPCODE and SOFT_MATCH is
7701 as for mips_arg_info. */
7702
7703 static bfd_boolean
7704 match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7705 struct mips_operand_token *tokens, unsigned int opcode_extra,
7706 bfd_boolean lax_match, bfd_boolean complete_p)
7707 {
7708 const char *args;
7709 struct mips_arg_info arg;
7710 const struct mips_operand *operand;
7711 char c;
7712
7713 imm_expr.X_op = O_absent;
7714 offset_expr.X_op = O_absent;
7715 offset_reloc[0] = BFD_RELOC_UNUSED;
7716 offset_reloc[1] = BFD_RELOC_UNUSED;
7717 offset_reloc[2] = BFD_RELOC_UNUSED;
7718
7719 create_insn (insn, opcode);
7720 /* When no opcode suffix is specified, assume ".xyzw". */
7721 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
7722 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
7723 else
7724 insn->insn_opcode |= opcode_extra;
7725 memset (&arg, 0, sizeof (arg));
7726 arg.insn = insn;
7727 arg.token = tokens;
7728 arg.argnum = 1;
7729 arg.last_regno = ILLEGAL_REG;
7730 arg.dest_regno = ILLEGAL_REG;
7731 arg.lax_match = lax_match;
7732 for (args = opcode->args;; ++args)
7733 {
7734 if (arg.token->type == OT_END)
7735 {
7736 /* Handle unary instructions in which only one operand is given.
7737 The source is then the same as the destination. */
7738 if (arg.opnum == 1 && *args == ',')
7739 {
7740 operand = (mips_opts.micromips
7741 ? decode_micromips_operand (args + 1)
7742 : decode_mips_operand (args + 1));
7743 if (operand && mips_optional_operand_p (operand))
7744 {
7745 arg.token = tokens;
7746 arg.argnum = 1;
7747 continue;
7748 }
7749 }
7750
7751 /* Treat elided base registers as $0. */
7752 if (strcmp (args, "(b)") == 0)
7753 args += 3;
7754
7755 if (args[0] == '+')
7756 switch (args[1])
7757 {
7758 case 'K':
7759 case 'N':
7760 /* The register suffix is optional. */
7761 args += 2;
7762 break;
7763 }
7764
7765 /* Fail the match if there were too few operands. */
7766 if (*args)
7767 return FALSE;
7768
7769 /* Successful match. */
7770 if (!complete_p)
7771 return TRUE;
7772 clear_insn_error ();
7773 if (arg.dest_regno == arg.last_regno
7774 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
7775 {
7776 if (arg.opnum == 2)
7777 set_insn_error
7778 (0, _("source and destination must be different"));
7779 else if (arg.last_regno == 31)
7780 set_insn_error
7781 (0, _("a destination register must be supplied"));
7782 }
7783 else if (arg.last_regno == 31
7784 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
7785 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
7786 set_insn_error (0, _("the source register must not be $31"));
7787 check_completed_insn (&arg);
7788 return TRUE;
7789 }
7790
7791 /* Fail the match if the line has too many operands. */
7792 if (*args == 0)
7793 return FALSE;
7794
7795 /* Handle characters that need to match exactly. */
7796 if (*args == '(' || *args == ')' || *args == ',')
7797 {
7798 if (match_char (&arg, *args))
7799 continue;
7800 return FALSE;
7801 }
7802 if (*args == '#')
7803 {
7804 ++args;
7805 if (arg.token->type == OT_DOUBLE_CHAR
7806 && arg.token->u.ch == *args)
7807 {
7808 ++arg.token;
7809 continue;
7810 }
7811 return FALSE;
7812 }
7813
7814 /* Handle special macro operands. Work out the properties of
7815 other operands. */
7816 arg.opnum += 1;
7817 switch (*args)
7818 {
7819 case '-':
7820 switch (args[1])
7821 {
7822 case 'A':
7823 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
7824 break;
7825
7826 case 'B':
7827 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
7828 break;
7829 }
7830 break;
7831
7832 case '+':
7833 switch (args[1])
7834 {
7835 case 'i':
7836 *offset_reloc = BFD_RELOC_MIPS_JMP;
7837 break;
7838
7839 case '\'':
7840 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
7841 break;
7842
7843 case '\"':
7844 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
7845 break;
7846 }
7847 break;
7848
7849 case 'I':
7850 if (!match_const_int (&arg, &imm_expr.X_add_number))
7851 return FALSE;
7852 imm_expr.X_op = O_constant;
7853 if (GPR_SIZE == 32)
7854 normalize_constant_expr (&imm_expr);
7855 continue;
7856
7857 case 'A':
7858 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
7859 {
7860 /* Assume that the offset has been elided and that what
7861 we saw was a base register. The match will fail later
7862 if that assumption turns out to be wrong. */
7863 offset_expr.X_op = O_constant;
7864 offset_expr.X_add_number = 0;
7865 }
7866 else
7867 {
7868 if (!match_expression (&arg, &offset_expr, offset_reloc))
7869 return FALSE;
7870 normalize_address_expr (&offset_expr);
7871 }
7872 continue;
7873
7874 case 'F':
7875 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7876 8, TRUE))
7877 return FALSE;
7878 continue;
7879
7880 case 'L':
7881 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7882 8, FALSE))
7883 return FALSE;
7884 continue;
7885
7886 case 'f':
7887 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7888 4, TRUE))
7889 return FALSE;
7890 continue;
7891
7892 case 'l':
7893 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7894 4, FALSE))
7895 return FALSE;
7896 continue;
7897
7898 case 'p':
7899 *offset_reloc = BFD_RELOC_16_PCREL_S2;
7900 break;
7901
7902 case 'a':
7903 *offset_reloc = BFD_RELOC_MIPS_JMP;
7904 break;
7905
7906 case 'm':
7907 gas_assert (mips_opts.micromips);
7908 c = args[1];
7909 switch (c)
7910 {
7911 case 'D':
7912 case 'E':
7913 if (!forced_insn_length)
7914 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
7915 else if (c == 'D')
7916 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
7917 else
7918 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
7919 break;
7920 }
7921 break;
7922 }
7923
7924 operand = (mips_opts.micromips
7925 ? decode_micromips_operand (args)
7926 : decode_mips_operand (args));
7927 if (!operand)
7928 abort ();
7929
7930 /* Skip prefixes. */
7931 if (*args == '+' || *args == 'm' || *args == '-')
7932 args++;
7933
7934 if (mips_optional_operand_p (operand)
7935 && args[1] == ','
7936 && (arg.token[0].type != OT_REG
7937 || arg.token[1].type == OT_END))
7938 {
7939 /* Assume that the register has been elided and is the
7940 same as the first operand. */
7941 arg.token = tokens;
7942 arg.argnum = 1;
7943 }
7944
7945 if (!match_operand (&arg, operand))
7946 return FALSE;
7947 }
7948 }
7949
7950 /* Like match_insn, but for MIPS16. */
7951
7952 static bfd_boolean
7953 match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7954 struct mips_operand_token *tokens)
7955 {
7956 const char *args;
7957 const struct mips_operand *operand;
7958 const struct mips_operand *ext_operand;
7959 struct mips_arg_info arg;
7960 int relax_char;
7961
7962 create_insn (insn, opcode);
7963 imm_expr.X_op = O_absent;
7964 offset_expr.X_op = O_absent;
7965 offset_reloc[0] = BFD_RELOC_UNUSED;
7966 offset_reloc[1] = BFD_RELOC_UNUSED;
7967 offset_reloc[2] = BFD_RELOC_UNUSED;
7968 relax_char = 0;
7969
7970 memset (&arg, 0, sizeof (arg));
7971 arg.insn = insn;
7972 arg.token = tokens;
7973 arg.argnum = 1;
7974 arg.last_regno = ILLEGAL_REG;
7975 arg.dest_regno = ILLEGAL_REG;
7976 relax_char = 0;
7977 for (args = opcode->args;; ++args)
7978 {
7979 int c;
7980
7981 if (arg.token->type == OT_END)
7982 {
7983 offsetT value;
7984
7985 /* Handle unary instructions in which only one operand is given.
7986 The source is then the same as the destination. */
7987 if (arg.opnum == 1 && *args == ',')
7988 {
7989 operand = decode_mips16_operand (args[1], FALSE);
7990 if (operand && mips_optional_operand_p (operand))
7991 {
7992 arg.token = tokens;
7993 arg.argnum = 1;
7994 continue;
7995 }
7996 }
7997
7998 /* Fail the match if there were too few operands. */
7999 if (*args)
8000 return FALSE;
8001
8002 /* Successful match. Stuff the immediate value in now, if
8003 we can. */
8004 clear_insn_error ();
8005 if (opcode->pinfo == INSN_MACRO)
8006 {
8007 gas_assert (relax_char == 0 || relax_char == 'p');
8008 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8009 }
8010 else if (relax_char
8011 && offset_expr.X_op == O_constant
8012 && calculate_reloc (*offset_reloc,
8013 offset_expr.X_add_number,
8014 &value))
8015 {
8016 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
8017 forced_insn_length, &insn->insn_opcode);
8018 offset_expr.X_op = O_absent;
8019 *offset_reloc = BFD_RELOC_UNUSED;
8020 }
8021 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8022 {
8023 if (forced_insn_length == 2)
8024 set_insn_error (0, _("invalid unextended operand value"));
8025 forced_insn_length = 4;
8026 insn->insn_opcode |= MIPS16_EXTEND;
8027 }
8028 else if (relax_char)
8029 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8030
8031 check_completed_insn (&arg);
8032 return TRUE;
8033 }
8034
8035 /* Fail the match if the line has too many operands. */
8036 if (*args == 0)
8037 return FALSE;
8038
8039 /* Handle characters that need to match exactly. */
8040 if (*args == '(' || *args == ')' || *args == ',')
8041 {
8042 if (match_char (&arg, *args))
8043 continue;
8044 return FALSE;
8045 }
8046
8047 arg.opnum += 1;
8048 c = *args;
8049 switch (c)
8050 {
8051 case 'p':
8052 case 'q':
8053 case 'A':
8054 case 'B':
8055 case 'E':
8056 relax_char = c;
8057 break;
8058
8059 case 'I':
8060 if (!match_const_int (&arg, &imm_expr.X_add_number))
8061 return FALSE;
8062 imm_expr.X_op = O_constant;
8063 if (GPR_SIZE == 32)
8064 normalize_constant_expr (&imm_expr);
8065 continue;
8066
8067 case 'a':
8068 case 'i':
8069 *offset_reloc = BFD_RELOC_MIPS16_JMP;
8070 insn->insn_opcode <<= 16;
8071 break;
8072 }
8073
8074 operand = decode_mips16_operand (c, FALSE);
8075 if (!operand)
8076 abort ();
8077
8078 /* '6' is a special case. It is used for BREAK and SDBBP,
8079 whose operands are only meaningful to the software that decodes
8080 them. This means that there is no architectural reason why
8081 they cannot be prefixed by EXTEND, but in practice,
8082 exception handlers will only look at the instruction
8083 itself. We therefore allow '6' to be extended when
8084 disassembling but not when assembling. */
8085 if (operand->type != OP_PCREL && c != '6')
8086 {
8087 ext_operand = decode_mips16_operand (c, TRUE);
8088 if (operand != ext_operand)
8089 {
8090 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8091 {
8092 offset_expr.X_op = O_constant;
8093 offset_expr.X_add_number = 0;
8094 relax_char = c;
8095 continue;
8096 }
8097
8098 /* We need the OT_INTEGER check because some MIPS16
8099 immediate variants are listed before the register ones. */
8100 if (arg.token->type != OT_INTEGER
8101 || !match_expression (&arg, &offset_expr, offset_reloc))
8102 return FALSE;
8103
8104 /* '8' is used for SLTI(U) and has traditionally not
8105 been allowed to take relocation operators. */
8106 if (offset_reloc[0] != BFD_RELOC_UNUSED
8107 && (ext_operand->size != 16 || c == '8'))
8108 return FALSE;
8109
8110 relax_char = c;
8111 continue;
8112 }
8113 }
8114
8115 if (mips_optional_operand_p (operand)
8116 && args[1] == ','
8117 && (arg.token[0].type != OT_REG
8118 || arg.token[1].type == OT_END))
8119 {
8120 /* Assume that the register has been elided and is the
8121 same as the first operand. */
8122 arg.token = tokens;
8123 arg.argnum = 1;
8124 }
8125
8126 if (!match_operand (&arg, operand))
8127 return FALSE;
8128 }
8129 }
8130
8131 /* Record that the current instruction is invalid for the current ISA. */
8132
8133 static void
8134 match_invalid_for_isa (void)
8135 {
8136 set_insn_error_ss
8137 (0, _("opcode not supported on this processor: %s (%s)"),
8138 mips_cpu_info_from_arch (mips_opts.arch)->name,
8139 mips_cpu_info_from_isa (mips_opts.isa)->name);
8140 }
8141
8142 /* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8143 Return true if a definite match or failure was found, storing any match
8144 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8145 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8146 tried and failed to match under normal conditions and now want to try a
8147 more relaxed match. */
8148
8149 static bfd_boolean
8150 match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8151 const struct mips_opcode *past, struct mips_operand_token *tokens,
8152 int opcode_extra, bfd_boolean lax_match)
8153 {
8154 const struct mips_opcode *opcode;
8155 const struct mips_opcode *invalid_delay_slot;
8156 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8157
8158 /* Search for a match, ignoring alternatives that don't satisfy the
8159 current ISA or forced_length. */
8160 invalid_delay_slot = 0;
8161 seen_valid_for_isa = FALSE;
8162 seen_valid_for_size = FALSE;
8163 opcode = first;
8164 do
8165 {
8166 gas_assert (strcmp (opcode->name, first->name) == 0);
8167 if (is_opcode_valid (opcode))
8168 {
8169 seen_valid_for_isa = TRUE;
8170 if (is_size_valid (opcode))
8171 {
8172 bfd_boolean delay_slot_ok;
8173
8174 seen_valid_for_size = TRUE;
8175 delay_slot_ok = is_delay_slot_valid (opcode);
8176 if (match_insn (insn, opcode, tokens, opcode_extra,
8177 lax_match, delay_slot_ok))
8178 {
8179 if (!delay_slot_ok)
8180 {
8181 if (!invalid_delay_slot)
8182 invalid_delay_slot = opcode;
8183 }
8184 else
8185 return TRUE;
8186 }
8187 }
8188 }
8189 ++opcode;
8190 }
8191 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8192
8193 /* If the only matches we found had the wrong length for the delay slot,
8194 pick the first such match. We'll issue an appropriate warning later. */
8195 if (invalid_delay_slot)
8196 {
8197 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8198 lax_match, TRUE))
8199 return TRUE;
8200 abort ();
8201 }
8202
8203 /* Handle the case where we didn't try to match an instruction because
8204 all the alternatives were incompatible with the current ISA. */
8205 if (!seen_valid_for_isa)
8206 {
8207 match_invalid_for_isa ();
8208 return TRUE;
8209 }
8210
8211 /* Handle the case where we didn't try to match an instruction because
8212 all the alternatives were of the wrong size. */
8213 if (!seen_valid_for_size)
8214 {
8215 if (mips_opts.insn32)
8216 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
8217 else
8218 set_insn_error_i
8219 (0, _("unrecognized %d-bit version of microMIPS opcode"),
8220 8 * forced_insn_length);
8221 return TRUE;
8222 }
8223
8224 return FALSE;
8225 }
8226
8227 /* Like match_insns, but for MIPS16. */
8228
8229 static bfd_boolean
8230 match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8231 struct mips_operand_token *tokens)
8232 {
8233 const struct mips_opcode *opcode;
8234 bfd_boolean seen_valid_for_isa;
8235
8236 /* Search for a match, ignoring alternatives that don't satisfy the
8237 current ISA. There are no separate entries for extended forms so
8238 we deal with forced_length later. */
8239 seen_valid_for_isa = FALSE;
8240 opcode = first;
8241 do
8242 {
8243 gas_assert (strcmp (opcode->name, first->name) == 0);
8244 if (is_opcode_valid_16 (opcode))
8245 {
8246 seen_valid_for_isa = TRUE;
8247 if (match_mips16_insn (insn, opcode, tokens))
8248 return TRUE;
8249 }
8250 ++opcode;
8251 }
8252 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8253 && strcmp (opcode->name, first->name) == 0);
8254
8255 /* Handle the case where we didn't try to match an instruction because
8256 all the alternatives were incompatible with the current ISA. */
8257 if (!seen_valid_for_isa)
8258 {
8259 match_invalid_for_isa ();
8260 return TRUE;
8261 }
8262
8263 return FALSE;
8264 }
8265
8266 /* Set up global variables for the start of a new macro. */
8267
8268 static void
8269 macro_start (void)
8270 {
8271 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
8272 memset (&mips_macro_warning.first_insn_sizes, 0,
8273 sizeof (mips_macro_warning.first_insn_sizes));
8274 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
8275 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
8276 && delayed_branch_p (&history[0]));
8277 switch (history[0].insn_mo->pinfo2
8278 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8279 {
8280 case INSN2_BRANCH_DELAY_32BIT:
8281 mips_macro_warning.delay_slot_length = 4;
8282 break;
8283 case INSN2_BRANCH_DELAY_16BIT:
8284 mips_macro_warning.delay_slot_length = 2;
8285 break;
8286 default:
8287 mips_macro_warning.delay_slot_length = 0;
8288 break;
8289 }
8290 mips_macro_warning.first_frag = NULL;
8291 }
8292
8293 /* Given that a macro is longer than one instruction or of the wrong size,
8294 return the appropriate warning for it. Return null if no warning is
8295 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8296 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8297 and RELAX_NOMACRO. */
8298
8299 static const char *
8300 macro_warning (relax_substateT subtype)
8301 {
8302 if (subtype & RELAX_DELAY_SLOT)
8303 return _("macro instruction expanded into multiple instructions"
8304 " in a branch delay slot");
8305 else if (subtype & RELAX_NOMACRO)
8306 return _("macro instruction expanded into multiple instructions");
8307 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8308 | RELAX_DELAY_SLOT_SIZE_SECOND))
8309 return ((subtype & RELAX_DELAY_SLOT_16BIT)
8310 ? _("macro instruction expanded into a wrong size instruction"
8311 " in a 16-bit branch delay slot")
8312 : _("macro instruction expanded into a wrong size instruction"
8313 " in a 32-bit branch delay slot"));
8314 else
8315 return 0;
8316 }
8317
8318 /* Finish up a macro. Emit warnings as appropriate. */
8319
8320 static void
8321 macro_end (void)
8322 {
8323 /* Relaxation warning flags. */
8324 relax_substateT subtype = 0;
8325
8326 /* Check delay slot size requirements. */
8327 if (mips_macro_warning.delay_slot_length == 2)
8328 subtype |= RELAX_DELAY_SLOT_16BIT;
8329 if (mips_macro_warning.delay_slot_length != 0)
8330 {
8331 if (mips_macro_warning.delay_slot_length
8332 != mips_macro_warning.first_insn_sizes[0])
8333 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8334 if (mips_macro_warning.delay_slot_length
8335 != mips_macro_warning.first_insn_sizes[1])
8336 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8337 }
8338
8339 /* Check instruction count requirements. */
8340 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8341 {
8342 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
8343 subtype |= RELAX_SECOND_LONGER;
8344 if (mips_opts.warn_about_macros)
8345 subtype |= RELAX_NOMACRO;
8346 if (mips_macro_warning.delay_slot_p)
8347 subtype |= RELAX_DELAY_SLOT;
8348 }
8349
8350 /* If both alternatives fail to fill a delay slot correctly,
8351 emit the warning now. */
8352 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8353 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8354 {
8355 relax_substateT s;
8356 const char *msg;
8357
8358 s = subtype & (RELAX_DELAY_SLOT_16BIT
8359 | RELAX_DELAY_SLOT_SIZE_FIRST
8360 | RELAX_DELAY_SLOT_SIZE_SECOND);
8361 msg = macro_warning (s);
8362 if (msg != NULL)
8363 as_warn ("%s", msg);
8364 subtype &= ~s;
8365 }
8366
8367 /* If both implementations are longer than 1 instruction, then emit the
8368 warning now. */
8369 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8370 {
8371 relax_substateT s;
8372 const char *msg;
8373
8374 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8375 msg = macro_warning (s);
8376 if (msg != NULL)
8377 as_warn ("%s", msg);
8378 subtype &= ~s;
8379 }
8380
8381 /* If any flags still set, then one implementation might need a warning
8382 and the other either will need one of a different kind or none at all.
8383 Pass any remaining flags over to relaxation. */
8384 if (mips_macro_warning.first_frag != NULL)
8385 mips_macro_warning.first_frag->fr_subtype |= subtype;
8386 }
8387
8388 /* Instruction operand formats used in macros that vary between
8389 standard MIPS and microMIPS code. */
8390
8391 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
8392 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8393 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8394 static const char * const lui_fmt[2] = { "t,u", "s,u" };
8395 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
8396 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
8397 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8398 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8399
8400 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
8401 #define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8402 : cop12_fmt[mips_opts.micromips])
8403 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
8404 #define LUI_FMT (lui_fmt[mips_opts.micromips])
8405 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
8406 #define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8407 : mem12_fmt[mips_opts.micromips])
8408 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
8409 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
8410 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
8411
8412 /* Read a macro's relocation codes from *ARGS and store them in *R.
8413 The first argument in *ARGS will be either the code for a single
8414 relocation or -1 followed by the three codes that make up a
8415 composite relocation. */
8416
8417 static void
8418 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8419 {
8420 int i, next;
8421
8422 next = va_arg (*args, int);
8423 if (next >= 0)
8424 r[0] = (bfd_reloc_code_real_type) next;
8425 else
8426 {
8427 for (i = 0; i < 3; i++)
8428 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8429 /* This function is only used for 16-bit relocation fields.
8430 To make the macro code simpler, treat an unrelocated value
8431 in the same way as BFD_RELOC_LO16. */
8432 if (r[0] == BFD_RELOC_UNUSED)
8433 r[0] = BFD_RELOC_LO16;
8434 }
8435 }
8436
8437 /* Build an instruction created by a macro expansion. This is passed
8438 a pointer to the count of instructions created so far, an
8439 expression, the name of the instruction to build, an operand format
8440 string, and corresponding arguments. */
8441
8442 static void
8443 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
8444 {
8445 const struct mips_opcode *mo = NULL;
8446 bfd_reloc_code_real_type r[3];
8447 const struct mips_opcode *amo;
8448 const struct mips_operand *operand;
8449 struct hash_control *hash;
8450 struct mips_cl_insn insn;
8451 va_list args;
8452 unsigned int uval;
8453
8454 va_start (args, fmt);
8455
8456 if (mips_opts.mips16)
8457 {
8458 mips16_macro_build (ep, name, fmt, &args);
8459 va_end (args);
8460 return;
8461 }
8462
8463 r[0] = BFD_RELOC_UNUSED;
8464 r[1] = BFD_RELOC_UNUSED;
8465 r[2] = BFD_RELOC_UNUSED;
8466 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8467 amo = (struct mips_opcode *) hash_find (hash, name);
8468 gas_assert (amo);
8469 gas_assert (strcmp (name, amo->name) == 0);
8470
8471 do
8472 {
8473 /* Search until we get a match for NAME. It is assumed here that
8474 macros will never generate MDMX, MIPS-3D, or MT instructions.
8475 We try to match an instruction that fulfils the branch delay
8476 slot instruction length requirement (if any) of the previous
8477 instruction. While doing this we record the first instruction
8478 seen that matches all the other conditions and use it anyway
8479 if the requirement cannot be met; we will issue an appropriate
8480 warning later on. */
8481 if (strcmp (fmt, amo->args) == 0
8482 && amo->pinfo != INSN_MACRO
8483 && is_opcode_valid (amo)
8484 && is_size_valid (amo))
8485 {
8486 if (is_delay_slot_valid (amo))
8487 {
8488 mo = amo;
8489 break;
8490 }
8491 else if (!mo)
8492 mo = amo;
8493 }
8494
8495 ++amo;
8496 gas_assert (amo->name);
8497 }
8498 while (strcmp (name, amo->name) == 0);
8499
8500 gas_assert (mo);
8501 create_insn (&insn, mo);
8502 for (; *fmt; ++fmt)
8503 {
8504 switch (*fmt)
8505 {
8506 case ',':
8507 case '(':
8508 case ')':
8509 case 'z':
8510 break;
8511
8512 case 'i':
8513 case 'j':
8514 macro_read_relocs (&args, r);
8515 gas_assert (*r == BFD_RELOC_GPREL16
8516 || *r == BFD_RELOC_MIPS_HIGHER
8517 || *r == BFD_RELOC_HI16_S
8518 || *r == BFD_RELOC_LO16
8519 || *r == BFD_RELOC_MIPS_GOT_OFST);
8520 break;
8521
8522 case 'o':
8523 macro_read_relocs (&args, r);
8524 break;
8525
8526 case 'u':
8527 macro_read_relocs (&args, r);
8528 gas_assert (ep != NULL
8529 && (ep->X_op == O_constant
8530 || (ep->X_op == O_symbol
8531 && (*r == BFD_RELOC_MIPS_HIGHEST
8532 || *r == BFD_RELOC_HI16_S
8533 || *r == BFD_RELOC_HI16
8534 || *r == BFD_RELOC_GPREL16
8535 || *r == BFD_RELOC_MIPS_GOT_HI16
8536 || *r == BFD_RELOC_MIPS_CALL_HI16))));
8537 break;
8538
8539 case 'p':
8540 gas_assert (ep != NULL);
8541
8542 /*
8543 * This allows macro() to pass an immediate expression for
8544 * creating short branches without creating a symbol.
8545 *
8546 * We don't allow branch relaxation for these branches, as
8547 * they should only appear in ".set nomacro" anyway.
8548 */
8549 if (ep->X_op == O_constant)
8550 {
8551 /* For microMIPS we always use relocations for branches.
8552 So we should not resolve immediate values. */
8553 gas_assert (!mips_opts.micromips);
8554
8555 if ((ep->X_add_number & 3) != 0)
8556 as_bad (_("branch to misaligned address (0x%lx)"),
8557 (unsigned long) ep->X_add_number);
8558 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8559 as_bad (_("branch address range overflow (0x%lx)"),
8560 (unsigned long) ep->X_add_number);
8561 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8562 ep = NULL;
8563 }
8564 else
8565 *r = BFD_RELOC_16_PCREL_S2;
8566 break;
8567
8568 case 'a':
8569 gas_assert (ep != NULL);
8570 *r = BFD_RELOC_MIPS_JMP;
8571 break;
8572
8573 default:
8574 operand = (mips_opts.micromips
8575 ? decode_micromips_operand (fmt)
8576 : decode_mips_operand (fmt));
8577 if (!operand)
8578 abort ();
8579
8580 uval = va_arg (args, int);
8581 if (operand->type == OP_CLO_CLZ_DEST)
8582 uval |= (uval << 5);
8583 insn_insert_operand (&insn, operand, uval);
8584
8585 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
8586 ++fmt;
8587 break;
8588 }
8589 }
8590 va_end (args);
8591 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
8592
8593 append_insn (&insn, ep, r, TRUE);
8594 }
8595
8596 static void
8597 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
8598 va_list *args)
8599 {
8600 struct mips_opcode *mo;
8601 struct mips_cl_insn insn;
8602 const struct mips_operand *operand;
8603 bfd_reloc_code_real_type r[3]
8604 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
8605
8606 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
8607 gas_assert (mo);
8608 gas_assert (strcmp (name, mo->name) == 0);
8609
8610 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
8611 {
8612 ++mo;
8613 gas_assert (mo->name);
8614 gas_assert (strcmp (name, mo->name) == 0);
8615 }
8616
8617 create_insn (&insn, mo);
8618 for (; *fmt; ++fmt)
8619 {
8620 int c;
8621
8622 c = *fmt;
8623 switch (c)
8624 {
8625 case ',':
8626 case '(':
8627 case ')':
8628 break;
8629
8630 case '0':
8631 case 'S':
8632 case 'P':
8633 case 'R':
8634 break;
8635
8636 case '<':
8637 case '>':
8638 case '4':
8639 case '5':
8640 case 'H':
8641 case 'W':
8642 case 'D':
8643 case 'j':
8644 case '8':
8645 case 'V':
8646 case 'C':
8647 case 'U':
8648 case 'k':
8649 case 'K':
8650 case 'p':
8651 case 'q':
8652 {
8653 offsetT value;
8654
8655 gas_assert (ep != NULL);
8656
8657 if (ep->X_op != O_constant)
8658 *r = (int) BFD_RELOC_UNUSED + c;
8659 else if (calculate_reloc (*r, ep->X_add_number, &value))
8660 {
8661 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
8662 ep = NULL;
8663 *r = BFD_RELOC_UNUSED;
8664 }
8665 }
8666 break;
8667
8668 default:
8669 operand = decode_mips16_operand (c, FALSE);
8670 if (!operand)
8671 abort ();
8672
8673 insn_insert_operand (&insn, operand, va_arg (*args, int));
8674 break;
8675 }
8676 }
8677
8678 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
8679
8680 append_insn (&insn, ep, r, TRUE);
8681 }
8682
8683 /*
8684 * Generate a "jalr" instruction with a relocation hint to the called
8685 * function. This occurs in NewABI PIC code.
8686 */
8687 static void
8688 macro_build_jalr (expressionS *ep, int cprestore)
8689 {
8690 static const bfd_reloc_code_real_type jalr_relocs[2]
8691 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
8692 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
8693 const char *jalr;
8694 char *f = NULL;
8695
8696 if (MIPS_JALR_HINT_P (ep))
8697 {
8698 frag_grow (8);
8699 f = frag_more (0);
8700 }
8701 if (mips_opts.micromips)
8702 {
8703 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
8704 ? "jalr" : "jalrs");
8705 if (MIPS_JALR_HINT_P (ep)
8706 || mips_opts.insn32
8707 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
8708 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
8709 else
8710 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
8711 }
8712 else
8713 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
8714 if (MIPS_JALR_HINT_P (ep))
8715 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
8716 }
8717
8718 /*
8719 * Generate a "lui" instruction.
8720 */
8721 static void
8722 macro_build_lui (expressionS *ep, int regnum)
8723 {
8724 gas_assert (! mips_opts.mips16);
8725
8726 if (ep->X_op != O_constant)
8727 {
8728 gas_assert (ep->X_op == O_symbol);
8729 /* _gp_disp is a special case, used from s_cpload.
8730 __gnu_local_gp is used if mips_no_shared. */
8731 gas_assert (mips_pic == NO_PIC
8732 || (! HAVE_NEWABI
8733 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
8734 || (! mips_in_shared
8735 && strcmp (S_GET_NAME (ep->X_add_symbol),
8736 "__gnu_local_gp") == 0));
8737 }
8738
8739 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
8740 }
8741
8742 /* Generate a sequence of instructions to do a load or store from a constant
8743 offset off of a base register (breg) into/from a target register (treg),
8744 using AT if necessary. */
8745 static void
8746 macro_build_ldst_constoffset (expressionS *ep, const char *op,
8747 int treg, int breg, int dbl)
8748 {
8749 gas_assert (ep->X_op == O_constant);
8750
8751 /* Sign-extending 32-bit constants makes their handling easier. */
8752 if (!dbl)
8753 normalize_constant_expr (ep);
8754
8755 /* Right now, this routine can only handle signed 32-bit constants. */
8756 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
8757 as_warn (_("operand overflow"));
8758
8759 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
8760 {
8761 /* Signed 16-bit offset will fit in the op. Easy! */
8762 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
8763 }
8764 else
8765 {
8766 /* 32-bit offset, need multiple instructions and AT, like:
8767 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
8768 addu $tempreg,$tempreg,$breg
8769 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
8770 to handle the complete offset. */
8771 macro_build_lui (ep, AT);
8772 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
8773 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
8774
8775 if (!mips_opts.at)
8776 as_bad (_("macro used $at after \".set noat\""));
8777 }
8778 }
8779
8780 /* set_at()
8781 * Generates code to set the $at register to true (one)
8782 * if reg is less than the immediate expression.
8783 */
8784 static void
8785 set_at (int reg, int unsignedp)
8786 {
8787 if (imm_expr.X_add_number >= -0x8000
8788 && imm_expr.X_add_number < 0x8000)
8789 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
8790 AT, reg, BFD_RELOC_LO16);
8791 else
8792 {
8793 load_register (AT, &imm_expr, GPR_SIZE == 64);
8794 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
8795 }
8796 }
8797
8798 /* Count the leading zeroes by performing a binary chop. This is a
8799 bulky bit of source, but performance is a LOT better for the
8800 majority of values than a simple loop to count the bits:
8801 for (lcnt = 0; (lcnt < 32); lcnt++)
8802 if ((v) & (1 << (31 - lcnt)))
8803 break;
8804 However it is not code size friendly, and the gain will drop a bit
8805 on certain cached systems.
8806 */
8807 #define COUNT_TOP_ZEROES(v) \
8808 (((v) & ~0xffff) == 0 \
8809 ? ((v) & ~0xff) == 0 \
8810 ? ((v) & ~0xf) == 0 \
8811 ? ((v) & ~0x3) == 0 \
8812 ? ((v) & ~0x1) == 0 \
8813 ? !(v) \
8814 ? 32 \
8815 : 31 \
8816 : 30 \
8817 : ((v) & ~0x7) == 0 \
8818 ? 29 \
8819 : 28 \
8820 : ((v) & ~0x3f) == 0 \
8821 ? ((v) & ~0x1f) == 0 \
8822 ? 27 \
8823 : 26 \
8824 : ((v) & ~0x7f) == 0 \
8825 ? 25 \
8826 : 24 \
8827 : ((v) & ~0xfff) == 0 \
8828 ? ((v) & ~0x3ff) == 0 \
8829 ? ((v) & ~0x1ff) == 0 \
8830 ? 23 \
8831 : 22 \
8832 : ((v) & ~0x7ff) == 0 \
8833 ? 21 \
8834 : 20 \
8835 : ((v) & ~0x3fff) == 0 \
8836 ? ((v) & ~0x1fff) == 0 \
8837 ? 19 \
8838 : 18 \
8839 : ((v) & ~0x7fff) == 0 \
8840 ? 17 \
8841 : 16 \
8842 : ((v) & ~0xffffff) == 0 \
8843 ? ((v) & ~0xfffff) == 0 \
8844 ? ((v) & ~0x3ffff) == 0 \
8845 ? ((v) & ~0x1ffff) == 0 \
8846 ? 15 \
8847 : 14 \
8848 : ((v) & ~0x7ffff) == 0 \
8849 ? 13 \
8850 : 12 \
8851 : ((v) & ~0x3fffff) == 0 \
8852 ? ((v) & ~0x1fffff) == 0 \
8853 ? 11 \
8854 : 10 \
8855 : ((v) & ~0x7fffff) == 0 \
8856 ? 9 \
8857 : 8 \
8858 : ((v) & ~0xfffffff) == 0 \
8859 ? ((v) & ~0x3ffffff) == 0 \
8860 ? ((v) & ~0x1ffffff) == 0 \
8861 ? 7 \
8862 : 6 \
8863 : ((v) & ~0x7ffffff) == 0 \
8864 ? 5 \
8865 : 4 \
8866 : ((v) & ~0x3fffffff) == 0 \
8867 ? ((v) & ~0x1fffffff) == 0 \
8868 ? 3 \
8869 : 2 \
8870 : ((v) & ~0x7fffffff) == 0 \
8871 ? 1 \
8872 : 0)
8873
8874 /* load_register()
8875 * This routine generates the least number of instructions necessary to load
8876 * an absolute expression value into a register.
8877 */
8878 static void
8879 load_register (int reg, expressionS *ep, int dbl)
8880 {
8881 int freg;
8882 expressionS hi32, lo32;
8883
8884 if (ep->X_op != O_big)
8885 {
8886 gas_assert (ep->X_op == O_constant);
8887
8888 /* Sign-extending 32-bit constants makes their handling easier. */
8889 if (!dbl)
8890 normalize_constant_expr (ep);
8891
8892 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
8893 {
8894 /* We can handle 16 bit signed values with an addiu to
8895 $zero. No need to ever use daddiu here, since $zero and
8896 the result are always correct in 32 bit mode. */
8897 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
8898 return;
8899 }
8900 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
8901 {
8902 /* We can handle 16 bit unsigned values with an ori to
8903 $zero. */
8904 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
8905 return;
8906 }
8907 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
8908 {
8909 /* 32 bit values require an lui. */
8910 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
8911 if ((ep->X_add_number & 0xffff) != 0)
8912 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
8913 return;
8914 }
8915 }
8916
8917 /* The value is larger than 32 bits. */
8918
8919 if (!dbl || GPR_SIZE == 32)
8920 {
8921 char value[32];
8922
8923 sprintf_vma (value, ep->X_add_number);
8924 as_bad (_("number (0x%s) larger than 32 bits"), value);
8925 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
8926 return;
8927 }
8928
8929 if (ep->X_op != O_big)
8930 {
8931 hi32 = *ep;
8932 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
8933 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
8934 hi32.X_add_number &= 0xffffffff;
8935 lo32 = *ep;
8936 lo32.X_add_number &= 0xffffffff;
8937 }
8938 else
8939 {
8940 gas_assert (ep->X_add_number > 2);
8941 if (ep->X_add_number == 3)
8942 generic_bignum[3] = 0;
8943 else if (ep->X_add_number > 4)
8944 as_bad (_("number larger than 64 bits"));
8945 lo32.X_op = O_constant;
8946 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
8947 hi32.X_op = O_constant;
8948 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
8949 }
8950
8951 if (hi32.X_add_number == 0)
8952 freg = 0;
8953 else
8954 {
8955 int shift, bit;
8956 unsigned long hi, lo;
8957
8958 if (hi32.X_add_number == (offsetT) 0xffffffff)
8959 {
8960 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
8961 {
8962 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
8963 return;
8964 }
8965 if (lo32.X_add_number & 0x80000000)
8966 {
8967 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
8968 if (lo32.X_add_number & 0xffff)
8969 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
8970 return;
8971 }
8972 }
8973
8974 /* Check for 16bit shifted constant. We know that hi32 is
8975 non-zero, so start the mask on the first bit of the hi32
8976 value. */
8977 shift = 17;
8978 do
8979 {
8980 unsigned long himask, lomask;
8981
8982 if (shift < 32)
8983 {
8984 himask = 0xffff >> (32 - shift);
8985 lomask = (0xffff << shift) & 0xffffffff;
8986 }
8987 else
8988 {
8989 himask = 0xffff << (shift - 32);
8990 lomask = 0;
8991 }
8992 if ((hi32.X_add_number & ~(offsetT) himask) == 0
8993 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
8994 {
8995 expressionS tmp;
8996
8997 tmp.X_op = O_constant;
8998 if (shift < 32)
8999 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9000 | (lo32.X_add_number >> shift));
9001 else
9002 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
9003 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9004 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9005 reg, reg, (shift >= 32) ? shift - 32 : shift);
9006 return;
9007 }
9008 ++shift;
9009 }
9010 while (shift <= (64 - 16));
9011
9012 /* Find the bit number of the lowest one bit, and store the
9013 shifted value in hi/lo. */
9014 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9015 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9016 if (lo != 0)
9017 {
9018 bit = 0;
9019 while ((lo & 1) == 0)
9020 {
9021 lo >>= 1;
9022 ++bit;
9023 }
9024 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9025 hi >>= bit;
9026 }
9027 else
9028 {
9029 bit = 32;
9030 while ((hi & 1) == 0)
9031 {
9032 hi >>= 1;
9033 ++bit;
9034 }
9035 lo = hi;
9036 hi = 0;
9037 }
9038
9039 /* Optimize if the shifted value is a (power of 2) - 1. */
9040 if ((hi == 0 && ((lo + 1) & lo) == 0)
9041 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
9042 {
9043 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
9044 if (shift != 0)
9045 {
9046 expressionS tmp;
9047
9048 /* This instruction will set the register to be all
9049 ones. */
9050 tmp.X_op = O_constant;
9051 tmp.X_add_number = (offsetT) -1;
9052 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9053 if (bit != 0)
9054 {
9055 bit += shift;
9056 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9057 reg, reg, (bit >= 32) ? bit - 32 : bit);
9058 }
9059 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
9060 reg, reg, (shift >= 32) ? shift - 32 : shift);
9061 return;
9062 }
9063 }
9064
9065 /* Sign extend hi32 before calling load_register, because we can
9066 generally get better code when we load a sign extended value. */
9067 if ((hi32.X_add_number & 0x80000000) != 0)
9068 hi32.X_add_number |= ~(offsetT) 0xffffffff;
9069 load_register (reg, &hi32, 0);
9070 freg = reg;
9071 }
9072 if ((lo32.X_add_number & 0xffff0000) == 0)
9073 {
9074 if (freg != 0)
9075 {
9076 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
9077 freg = reg;
9078 }
9079 }
9080 else
9081 {
9082 expressionS mid16;
9083
9084 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
9085 {
9086 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9087 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
9088 return;
9089 }
9090
9091 if (freg != 0)
9092 {
9093 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
9094 freg = reg;
9095 }
9096 mid16 = lo32;
9097 mid16.X_add_number >>= 16;
9098 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9099 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9100 freg = reg;
9101 }
9102 if ((lo32.X_add_number & 0xffff) != 0)
9103 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9104 }
9105
9106 static inline void
9107 load_delay_nop (void)
9108 {
9109 if (!gpr_interlocks)
9110 macro_build (NULL, "nop", "");
9111 }
9112
9113 /* Load an address into a register. */
9114
9115 static void
9116 load_address (int reg, expressionS *ep, int *used_at)
9117 {
9118 if (ep->X_op != O_constant
9119 && ep->X_op != O_symbol)
9120 {
9121 as_bad (_("expression too complex"));
9122 ep->X_op = O_constant;
9123 }
9124
9125 if (ep->X_op == O_constant)
9126 {
9127 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
9128 return;
9129 }
9130
9131 if (mips_pic == NO_PIC)
9132 {
9133 /* If this is a reference to a GP relative symbol, we want
9134 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
9135 Otherwise we want
9136 lui $reg,<sym> (BFD_RELOC_HI16_S)
9137 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9138 If we have an addend, we always use the latter form.
9139
9140 With 64bit address space and a usable $at we want
9141 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9142 lui $at,<sym> (BFD_RELOC_HI16_S)
9143 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9144 daddiu $at,<sym> (BFD_RELOC_LO16)
9145 dsll32 $reg,0
9146 daddu $reg,$reg,$at
9147
9148 If $at is already in use, we use a path which is suboptimal
9149 on superscalar processors.
9150 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9151 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9152 dsll $reg,16
9153 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9154 dsll $reg,16
9155 daddiu $reg,<sym> (BFD_RELOC_LO16)
9156
9157 For GP relative symbols in 64bit address space we can use
9158 the same sequence as in 32bit address space. */
9159 if (HAVE_64BIT_SYMBOLS)
9160 {
9161 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9162 && !nopic_need_relax (ep->X_add_symbol, 1))
9163 {
9164 relax_start (ep->X_add_symbol);
9165 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9166 mips_gp_register, BFD_RELOC_GPREL16);
9167 relax_switch ();
9168 }
9169
9170 if (*used_at == 0 && mips_opts.at)
9171 {
9172 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9173 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
9174 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9175 BFD_RELOC_MIPS_HIGHER);
9176 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
9177 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
9178 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
9179 *used_at = 1;
9180 }
9181 else
9182 {
9183 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9184 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9185 BFD_RELOC_MIPS_HIGHER);
9186 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9187 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
9188 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9189 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
9190 }
9191
9192 if (mips_relax.sequence)
9193 relax_end ();
9194 }
9195 else
9196 {
9197 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9198 && !nopic_need_relax (ep->X_add_symbol, 1))
9199 {
9200 relax_start (ep->X_add_symbol);
9201 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9202 mips_gp_register, BFD_RELOC_GPREL16);
9203 relax_switch ();
9204 }
9205 macro_build_lui (ep, reg);
9206 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9207 reg, reg, BFD_RELOC_LO16);
9208 if (mips_relax.sequence)
9209 relax_end ();
9210 }
9211 }
9212 else if (!mips_big_got)
9213 {
9214 expressionS ex;
9215
9216 /* If this is a reference to an external symbol, we want
9217 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9218 Otherwise we want
9219 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9220 nop
9221 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9222 If there is a constant, it must be added in after.
9223
9224 If we have NewABI, we want
9225 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9226 unless we're referencing a global symbol with a non-zero
9227 offset, in which case cst must be added separately. */
9228 if (HAVE_NEWABI)
9229 {
9230 if (ep->X_add_number)
9231 {
9232 ex.X_add_number = ep->X_add_number;
9233 ep->X_add_number = 0;
9234 relax_start (ep->X_add_symbol);
9235 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9236 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9237 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9238 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9239 ex.X_op = O_constant;
9240 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9241 reg, reg, BFD_RELOC_LO16);
9242 ep->X_add_number = ex.X_add_number;
9243 relax_switch ();
9244 }
9245 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9246 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9247 if (mips_relax.sequence)
9248 relax_end ();
9249 }
9250 else
9251 {
9252 ex.X_add_number = ep->X_add_number;
9253 ep->X_add_number = 0;
9254 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9255 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9256 load_delay_nop ();
9257 relax_start (ep->X_add_symbol);
9258 relax_switch ();
9259 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9260 BFD_RELOC_LO16);
9261 relax_end ();
9262
9263 if (ex.X_add_number != 0)
9264 {
9265 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9266 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9267 ex.X_op = O_constant;
9268 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9269 reg, reg, BFD_RELOC_LO16);
9270 }
9271 }
9272 }
9273 else if (mips_big_got)
9274 {
9275 expressionS ex;
9276
9277 /* This is the large GOT case. If this is a reference to an
9278 external symbol, we want
9279 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9280 addu $reg,$reg,$gp
9281 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
9282
9283 Otherwise, for a reference to a local symbol in old ABI, we want
9284 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9285 nop
9286 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9287 If there is a constant, it must be added in after.
9288
9289 In the NewABI, for local symbols, with or without offsets, we want:
9290 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9291 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
9292 */
9293 if (HAVE_NEWABI)
9294 {
9295 ex.X_add_number = ep->X_add_number;
9296 ep->X_add_number = 0;
9297 relax_start (ep->X_add_symbol);
9298 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9299 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9300 reg, reg, mips_gp_register);
9301 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9302 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9303 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9304 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9305 else if (ex.X_add_number)
9306 {
9307 ex.X_op = O_constant;
9308 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9309 BFD_RELOC_LO16);
9310 }
9311
9312 ep->X_add_number = ex.X_add_number;
9313 relax_switch ();
9314 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9315 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9316 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9317 BFD_RELOC_MIPS_GOT_OFST);
9318 relax_end ();
9319 }
9320 else
9321 {
9322 ex.X_add_number = ep->X_add_number;
9323 ep->X_add_number = 0;
9324 relax_start (ep->X_add_symbol);
9325 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9326 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9327 reg, reg, mips_gp_register);
9328 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9329 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9330 relax_switch ();
9331 if (reg_needs_delay (mips_gp_register))
9332 {
9333 /* We need a nop before loading from $gp. This special
9334 check is required because the lui which starts the main
9335 instruction stream does not refer to $gp, and so will not
9336 insert the nop which may be required. */
9337 macro_build (NULL, "nop", "");
9338 }
9339 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9340 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9341 load_delay_nop ();
9342 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9343 BFD_RELOC_LO16);
9344 relax_end ();
9345
9346 if (ex.X_add_number != 0)
9347 {
9348 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9349 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9350 ex.X_op = O_constant;
9351 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9352 BFD_RELOC_LO16);
9353 }
9354 }
9355 }
9356 else
9357 abort ();
9358
9359 if (!mips_opts.at && *used_at == 1)
9360 as_bad (_("macro used $at after \".set noat\""));
9361 }
9362
9363 /* Move the contents of register SOURCE into register DEST. */
9364
9365 static void
9366 move_register (int dest, int source)
9367 {
9368 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9369 instruction specifically requires a 32-bit one. */
9370 if (mips_opts.micromips
9371 && !mips_opts.insn32
9372 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9373 macro_build (NULL, "move", "mp,mj", dest, source);
9374 else
9375 macro_build (NULL, "or", "d,v,t", dest, source, 0);
9376 }
9377
9378 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
9379 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9380 The two alternatives are:
9381
9382 Global symbol Local sybmol
9383 ------------- ------------
9384 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9385 ... ...
9386 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9387
9388 load_got_offset emits the first instruction and add_got_offset
9389 emits the second for a 16-bit offset or add_got_offset_hilo emits
9390 a sequence to add a 32-bit offset using a scratch register. */
9391
9392 static void
9393 load_got_offset (int dest, expressionS *local)
9394 {
9395 expressionS global;
9396
9397 global = *local;
9398 global.X_add_number = 0;
9399
9400 relax_start (local->X_add_symbol);
9401 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9402 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9403 relax_switch ();
9404 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9405 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9406 relax_end ();
9407 }
9408
9409 static void
9410 add_got_offset (int dest, expressionS *local)
9411 {
9412 expressionS global;
9413
9414 global.X_op = O_constant;
9415 global.X_op_symbol = NULL;
9416 global.X_add_symbol = NULL;
9417 global.X_add_number = local->X_add_number;
9418
9419 relax_start (local->X_add_symbol);
9420 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
9421 dest, dest, BFD_RELOC_LO16);
9422 relax_switch ();
9423 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
9424 relax_end ();
9425 }
9426
9427 static void
9428 add_got_offset_hilo (int dest, expressionS *local, int tmp)
9429 {
9430 expressionS global;
9431 int hold_mips_optimize;
9432
9433 global.X_op = O_constant;
9434 global.X_op_symbol = NULL;
9435 global.X_add_symbol = NULL;
9436 global.X_add_number = local->X_add_number;
9437
9438 relax_start (local->X_add_symbol);
9439 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9440 relax_switch ();
9441 /* Set mips_optimize around the lui instruction to avoid
9442 inserting an unnecessary nop after the lw. */
9443 hold_mips_optimize = mips_optimize;
9444 mips_optimize = 2;
9445 macro_build_lui (&global, tmp);
9446 mips_optimize = hold_mips_optimize;
9447 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9448 relax_end ();
9449
9450 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9451 }
9452
9453 /* Emit a sequence of instructions to emulate a branch likely operation.
9454 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9455 is its complementing branch with the original condition negated.
9456 CALL is set if the original branch specified the link operation.
9457 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9458
9459 Code like this is produced in the noreorder mode:
9460
9461 BRNEG <args>, 1f
9462 nop
9463 b <sym>
9464 delay slot (executed only if branch taken)
9465 1:
9466
9467 or, if CALL is set:
9468
9469 BRNEG <args>, 1f
9470 nop
9471 bal <sym>
9472 delay slot (executed only if branch taken)
9473 1:
9474
9475 In the reorder mode the delay slot would be filled with a nop anyway,
9476 so code produced is simply:
9477
9478 BR <args>, <sym>
9479 nop
9480
9481 This function is used when producing code for the microMIPS ASE that
9482 does not implement branch likely instructions in hardware. */
9483
9484 static void
9485 macro_build_branch_likely (const char *br, const char *brneg,
9486 int call, expressionS *ep, const char *fmt,
9487 unsigned int sreg, unsigned int treg)
9488 {
9489 int noreorder = mips_opts.noreorder;
9490 expressionS expr1;
9491
9492 gas_assert (mips_opts.micromips);
9493 start_noreorder ();
9494 if (noreorder)
9495 {
9496 micromips_label_expr (&expr1);
9497 macro_build (&expr1, brneg, fmt, sreg, treg);
9498 macro_build (NULL, "nop", "");
9499 macro_build (ep, call ? "bal" : "b", "p");
9500
9501 /* Set to true so that append_insn adds a label. */
9502 emit_branch_likely_macro = TRUE;
9503 }
9504 else
9505 {
9506 macro_build (ep, br, fmt, sreg, treg);
9507 macro_build (NULL, "nop", "");
9508 }
9509 end_noreorder ();
9510 }
9511
9512 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9513 the condition code tested. EP specifies the branch target. */
9514
9515 static void
9516 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9517 {
9518 const int call = 0;
9519 const char *brneg;
9520 const char *br;
9521
9522 switch (type)
9523 {
9524 case M_BC1FL:
9525 br = "bc1f";
9526 brneg = "bc1t";
9527 break;
9528 case M_BC1TL:
9529 br = "bc1t";
9530 brneg = "bc1f";
9531 break;
9532 case M_BC2FL:
9533 br = "bc2f";
9534 brneg = "bc2t";
9535 break;
9536 case M_BC2TL:
9537 br = "bc2t";
9538 brneg = "bc2f";
9539 break;
9540 default:
9541 abort ();
9542 }
9543 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9544 }
9545
9546 /* Emit a two-argument branch macro specified by TYPE, using SREG as
9547 the register tested. EP specifies the branch target. */
9548
9549 static void
9550 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9551 {
9552 const char *brneg = NULL;
9553 const char *br;
9554 int call = 0;
9555
9556 switch (type)
9557 {
9558 case M_BGEZ:
9559 br = "bgez";
9560 break;
9561 case M_BGEZL:
9562 br = mips_opts.micromips ? "bgez" : "bgezl";
9563 brneg = "bltz";
9564 break;
9565 case M_BGEZALL:
9566 gas_assert (mips_opts.micromips);
9567 br = mips_opts.insn32 ? "bgezal" : "bgezals";
9568 brneg = "bltz";
9569 call = 1;
9570 break;
9571 case M_BGTZ:
9572 br = "bgtz";
9573 break;
9574 case M_BGTZL:
9575 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9576 brneg = "blez";
9577 break;
9578 case M_BLEZ:
9579 br = "blez";
9580 break;
9581 case M_BLEZL:
9582 br = mips_opts.micromips ? "blez" : "blezl";
9583 brneg = "bgtz";
9584 break;
9585 case M_BLTZ:
9586 br = "bltz";
9587 break;
9588 case M_BLTZL:
9589 br = mips_opts.micromips ? "bltz" : "bltzl";
9590 brneg = "bgez";
9591 break;
9592 case M_BLTZALL:
9593 gas_assert (mips_opts.micromips);
9594 br = mips_opts.insn32 ? "bltzal" : "bltzals";
9595 brneg = "bgez";
9596 call = 1;
9597 break;
9598 default:
9599 abort ();
9600 }
9601 if (mips_opts.micromips && brneg)
9602 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9603 else
9604 macro_build (ep, br, "s,p", sreg);
9605 }
9606
9607 /* Emit a three-argument branch macro specified by TYPE, using SREG and
9608 TREG as the registers tested. EP specifies the branch target. */
9609
9610 static void
9611 macro_build_branch_rsrt (int type, expressionS *ep,
9612 unsigned int sreg, unsigned int treg)
9613 {
9614 const char *brneg = NULL;
9615 const int call = 0;
9616 const char *br;
9617
9618 switch (type)
9619 {
9620 case M_BEQ:
9621 case M_BEQ_I:
9622 br = "beq";
9623 break;
9624 case M_BEQL:
9625 case M_BEQL_I:
9626 br = mips_opts.micromips ? "beq" : "beql";
9627 brneg = "bne";
9628 break;
9629 case M_BNE:
9630 case M_BNE_I:
9631 br = "bne";
9632 break;
9633 case M_BNEL:
9634 case M_BNEL_I:
9635 br = mips_opts.micromips ? "bne" : "bnel";
9636 brneg = "beq";
9637 break;
9638 default:
9639 abort ();
9640 }
9641 if (mips_opts.micromips && brneg)
9642 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
9643 else
9644 macro_build (ep, br, "s,t,p", sreg, treg);
9645 }
9646
9647 /* Return the high part that should be loaded in order to make the low
9648 part of VALUE accessible using an offset of OFFBITS bits. */
9649
9650 static offsetT
9651 offset_high_part (offsetT value, unsigned int offbits)
9652 {
9653 offsetT bias;
9654 addressT low_mask;
9655
9656 if (offbits == 0)
9657 return value;
9658 bias = 1 << (offbits - 1);
9659 low_mask = bias * 2 - 1;
9660 return (value + bias) & ~low_mask;
9661 }
9662
9663 /* Return true if the value stored in offset_expr and offset_reloc
9664 fits into a signed offset of OFFBITS bits. RANGE is the maximum
9665 amount that the caller wants to add without inducing overflow
9666 and ALIGN is the known alignment of the value in bytes. */
9667
9668 static bfd_boolean
9669 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
9670 {
9671 if (offbits == 16)
9672 {
9673 /* Accept any relocation operator if overflow isn't a concern. */
9674 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
9675 return TRUE;
9676
9677 /* These relocations are guaranteed not to overflow in correct links. */
9678 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
9679 || gprel16_reloc_p (*offset_reloc))
9680 return TRUE;
9681 }
9682 if (offset_expr.X_op == O_constant
9683 && offset_high_part (offset_expr.X_add_number, offbits) == 0
9684 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
9685 return TRUE;
9686 return FALSE;
9687 }
9688
9689 /*
9690 * Build macros
9691 * This routine implements the seemingly endless macro or synthesized
9692 * instructions and addressing modes in the mips assembly language. Many
9693 * of these macros are simple and are similar to each other. These could
9694 * probably be handled by some kind of table or grammar approach instead of
9695 * this verbose method. Others are not simple macros but are more like
9696 * optimizing code generation.
9697 * One interesting optimization is when several store macros appear
9698 * consecutively that would load AT with the upper half of the same address.
9699 * The ensuing load upper instructions are ommited. This implies some kind
9700 * of global optimization. We currently only optimize within a single macro.
9701 * For many of the load and store macros if the address is specified as a
9702 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
9703 * first load register 'at' with zero and use it as the base register. The
9704 * mips assembler simply uses register $zero. Just one tiny optimization
9705 * we're missing.
9706 */
9707 static void
9708 macro (struct mips_cl_insn *ip, char *str)
9709 {
9710 const struct mips_operand_array *operands;
9711 unsigned int breg, i;
9712 unsigned int tempreg;
9713 int mask;
9714 int used_at = 0;
9715 expressionS label_expr;
9716 expressionS expr1;
9717 expressionS *ep;
9718 const char *s;
9719 const char *s2;
9720 const char *fmt;
9721 int likely = 0;
9722 int coproc = 0;
9723 int offbits = 16;
9724 int call = 0;
9725 int jals = 0;
9726 int dbl = 0;
9727 int imm = 0;
9728 int ust = 0;
9729 int lp = 0;
9730 bfd_boolean large_offset;
9731 int off;
9732 int hold_mips_optimize;
9733 unsigned int align;
9734 unsigned int op[MAX_OPERANDS];
9735
9736 gas_assert (! mips_opts.mips16);
9737
9738 operands = insn_operands (ip);
9739 for (i = 0; i < MAX_OPERANDS; i++)
9740 if (operands->operand[i])
9741 op[i] = insn_extract_operand (ip, operands->operand[i]);
9742 else
9743 op[i] = -1;
9744
9745 mask = ip->insn_mo->mask;
9746
9747 label_expr.X_op = O_constant;
9748 label_expr.X_op_symbol = NULL;
9749 label_expr.X_add_symbol = NULL;
9750 label_expr.X_add_number = 0;
9751
9752 expr1.X_op = O_constant;
9753 expr1.X_op_symbol = NULL;
9754 expr1.X_add_symbol = NULL;
9755 expr1.X_add_number = 1;
9756 align = 1;
9757
9758 switch (mask)
9759 {
9760 case M_DABS:
9761 dbl = 1;
9762 case M_ABS:
9763 /* bgez $a0,1f
9764 move v0,$a0
9765 sub v0,$zero,$a0
9766 1:
9767 */
9768
9769 start_noreorder ();
9770
9771 if (mips_opts.micromips)
9772 micromips_label_expr (&label_expr);
9773 else
9774 label_expr.X_add_number = 8;
9775 macro_build (&label_expr, "bgez", "s,p", op[1]);
9776 if (op[0] == op[1])
9777 macro_build (NULL, "nop", "");
9778 else
9779 move_register (op[0], op[1]);
9780 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
9781 if (mips_opts.micromips)
9782 micromips_add_label ();
9783
9784 end_noreorder ();
9785 break;
9786
9787 case M_ADD_I:
9788 s = "addi";
9789 s2 = "add";
9790 goto do_addi;
9791 case M_ADDU_I:
9792 s = "addiu";
9793 s2 = "addu";
9794 goto do_addi;
9795 case M_DADD_I:
9796 dbl = 1;
9797 s = "daddi";
9798 s2 = "dadd";
9799 if (!mips_opts.micromips)
9800 goto do_addi;
9801 if (imm_expr.X_add_number >= -0x200
9802 && imm_expr.X_add_number < 0x200)
9803 {
9804 macro_build (NULL, s, "t,r,.", op[0], op[1],
9805 (int) imm_expr.X_add_number);
9806 break;
9807 }
9808 goto do_addi_i;
9809 case M_DADDU_I:
9810 dbl = 1;
9811 s = "daddiu";
9812 s2 = "daddu";
9813 do_addi:
9814 if (imm_expr.X_add_number >= -0x8000
9815 && imm_expr.X_add_number < 0x8000)
9816 {
9817 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
9818 break;
9819 }
9820 do_addi_i:
9821 used_at = 1;
9822 load_register (AT, &imm_expr, dbl);
9823 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
9824 break;
9825
9826 case M_AND_I:
9827 s = "andi";
9828 s2 = "and";
9829 goto do_bit;
9830 case M_OR_I:
9831 s = "ori";
9832 s2 = "or";
9833 goto do_bit;
9834 case M_NOR_I:
9835 s = "";
9836 s2 = "nor";
9837 goto do_bit;
9838 case M_XOR_I:
9839 s = "xori";
9840 s2 = "xor";
9841 do_bit:
9842 if (imm_expr.X_add_number >= 0
9843 && imm_expr.X_add_number < 0x10000)
9844 {
9845 if (mask != M_NOR_I)
9846 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
9847 else
9848 {
9849 macro_build (&imm_expr, "ori", "t,r,i",
9850 op[0], op[1], BFD_RELOC_LO16);
9851 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
9852 }
9853 break;
9854 }
9855
9856 used_at = 1;
9857 load_register (AT, &imm_expr, GPR_SIZE == 64);
9858 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
9859 break;
9860
9861 case M_BALIGN:
9862 switch (imm_expr.X_add_number)
9863 {
9864 case 0:
9865 macro_build (NULL, "nop", "");
9866 break;
9867 case 2:
9868 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
9869 break;
9870 case 1:
9871 case 3:
9872 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
9873 (int) imm_expr.X_add_number);
9874 break;
9875 default:
9876 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
9877 (unsigned long) imm_expr.X_add_number);
9878 break;
9879 }
9880 break;
9881
9882 case M_BC1FL:
9883 case M_BC1TL:
9884 case M_BC2FL:
9885 case M_BC2TL:
9886 gas_assert (mips_opts.micromips);
9887 macro_build_branch_ccl (mask, &offset_expr,
9888 EXTRACT_OPERAND (1, BCC, *ip));
9889 break;
9890
9891 case M_BEQ_I:
9892 case M_BEQL_I:
9893 case M_BNE_I:
9894 case M_BNEL_I:
9895 if (imm_expr.X_add_number == 0)
9896 op[1] = 0;
9897 else
9898 {
9899 op[1] = AT;
9900 used_at = 1;
9901 load_register (op[1], &imm_expr, GPR_SIZE == 64);
9902 }
9903 /* Fall through. */
9904 case M_BEQL:
9905 case M_BNEL:
9906 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
9907 break;
9908
9909 case M_BGEL:
9910 likely = 1;
9911 case M_BGE:
9912 if (op[1] == 0)
9913 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
9914 else if (op[0] == 0)
9915 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
9916 else
9917 {
9918 used_at = 1;
9919 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
9920 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9921 &offset_expr, AT, ZERO);
9922 }
9923 break;
9924
9925 case M_BGEZL:
9926 case M_BGEZALL:
9927 case M_BGTZL:
9928 case M_BLEZL:
9929 case M_BLTZL:
9930 case M_BLTZALL:
9931 macro_build_branch_rs (mask, &offset_expr, op[0]);
9932 break;
9933
9934 case M_BGTL_I:
9935 likely = 1;
9936 case M_BGT_I:
9937 /* Check for > max integer. */
9938 if (imm_expr.X_add_number >= GPR_SMAX)
9939 {
9940 do_false:
9941 /* Result is always false. */
9942 if (! likely)
9943 macro_build (NULL, "nop", "");
9944 else
9945 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
9946 break;
9947 }
9948 ++imm_expr.X_add_number;
9949 /* FALLTHROUGH */
9950 case M_BGE_I:
9951 case M_BGEL_I:
9952 if (mask == M_BGEL_I)
9953 likely = 1;
9954 if (imm_expr.X_add_number == 0)
9955 {
9956 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
9957 &offset_expr, op[0]);
9958 break;
9959 }
9960 if (imm_expr.X_add_number == 1)
9961 {
9962 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
9963 &offset_expr, op[0]);
9964 break;
9965 }
9966 if (imm_expr.X_add_number <= GPR_SMIN)
9967 {
9968 do_true:
9969 /* result is always true */
9970 as_warn (_("branch %s is always true"), ip->insn_mo->name);
9971 macro_build (&offset_expr, "b", "p");
9972 break;
9973 }
9974 used_at = 1;
9975 set_at (op[0], 0);
9976 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9977 &offset_expr, AT, ZERO);
9978 break;
9979
9980 case M_BGEUL:
9981 likely = 1;
9982 case M_BGEU:
9983 if (op[1] == 0)
9984 goto do_true;
9985 else if (op[0] == 0)
9986 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9987 &offset_expr, ZERO, op[1]);
9988 else
9989 {
9990 used_at = 1;
9991 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
9992 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9993 &offset_expr, AT, ZERO);
9994 }
9995 break;
9996
9997 case M_BGTUL_I:
9998 likely = 1;
9999 case M_BGTU_I:
10000 if (op[0] == 0
10001 || (GPR_SIZE == 32
10002 && imm_expr.X_add_number == -1))
10003 goto do_false;
10004 ++imm_expr.X_add_number;
10005 /* FALLTHROUGH */
10006 case M_BGEU_I:
10007 case M_BGEUL_I:
10008 if (mask == M_BGEUL_I)
10009 likely = 1;
10010 if (imm_expr.X_add_number == 0)
10011 goto do_true;
10012 else if (imm_expr.X_add_number == 1)
10013 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10014 &offset_expr, op[0], ZERO);
10015 else
10016 {
10017 used_at = 1;
10018 set_at (op[0], 1);
10019 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10020 &offset_expr, AT, ZERO);
10021 }
10022 break;
10023
10024 case M_BGTL:
10025 likely = 1;
10026 case M_BGT:
10027 if (op[1] == 0)
10028 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10029 else if (op[0] == 0)
10030 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
10031 else
10032 {
10033 used_at = 1;
10034 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10035 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10036 &offset_expr, AT, ZERO);
10037 }
10038 break;
10039
10040 case M_BGTUL:
10041 likely = 1;
10042 case M_BGTU:
10043 if (op[1] == 0)
10044 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10045 &offset_expr, op[0], ZERO);
10046 else if (op[0] == 0)
10047 goto do_false;
10048 else
10049 {
10050 used_at = 1;
10051 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10052 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10053 &offset_expr, AT, ZERO);
10054 }
10055 break;
10056
10057 case M_BLEL:
10058 likely = 1;
10059 case M_BLE:
10060 if (op[1] == 0)
10061 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10062 else if (op[0] == 0)
10063 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
10064 else
10065 {
10066 used_at = 1;
10067 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10068 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10069 &offset_expr, AT, ZERO);
10070 }
10071 break;
10072
10073 case M_BLEL_I:
10074 likely = 1;
10075 case M_BLE_I:
10076 if (imm_expr.X_add_number >= GPR_SMAX)
10077 goto do_true;
10078 ++imm_expr.X_add_number;
10079 /* FALLTHROUGH */
10080 case M_BLT_I:
10081 case M_BLTL_I:
10082 if (mask == M_BLTL_I)
10083 likely = 1;
10084 if (imm_expr.X_add_number == 0)
10085 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10086 else if (imm_expr.X_add_number == 1)
10087 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10088 else
10089 {
10090 used_at = 1;
10091 set_at (op[0], 0);
10092 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10093 &offset_expr, AT, ZERO);
10094 }
10095 break;
10096
10097 case M_BLEUL:
10098 likely = 1;
10099 case M_BLEU:
10100 if (op[1] == 0)
10101 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10102 &offset_expr, op[0], ZERO);
10103 else if (op[0] == 0)
10104 goto do_true;
10105 else
10106 {
10107 used_at = 1;
10108 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10109 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10110 &offset_expr, AT, ZERO);
10111 }
10112 break;
10113
10114 case M_BLEUL_I:
10115 likely = 1;
10116 case M_BLEU_I:
10117 if (op[0] == 0
10118 || (GPR_SIZE == 32
10119 && imm_expr.X_add_number == -1))
10120 goto do_true;
10121 ++imm_expr.X_add_number;
10122 /* FALLTHROUGH */
10123 case M_BLTU_I:
10124 case M_BLTUL_I:
10125 if (mask == M_BLTUL_I)
10126 likely = 1;
10127 if (imm_expr.X_add_number == 0)
10128 goto do_false;
10129 else if (imm_expr.X_add_number == 1)
10130 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10131 &offset_expr, op[0], ZERO);
10132 else
10133 {
10134 used_at = 1;
10135 set_at (op[0], 1);
10136 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10137 &offset_expr, AT, ZERO);
10138 }
10139 break;
10140
10141 case M_BLTL:
10142 likely = 1;
10143 case M_BLT:
10144 if (op[1] == 0)
10145 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10146 else if (op[0] == 0)
10147 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
10148 else
10149 {
10150 used_at = 1;
10151 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10152 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10153 &offset_expr, AT, ZERO);
10154 }
10155 break;
10156
10157 case M_BLTUL:
10158 likely = 1;
10159 case M_BLTU:
10160 if (op[1] == 0)
10161 goto do_false;
10162 else if (op[0] == 0)
10163 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10164 &offset_expr, ZERO, op[1]);
10165 else
10166 {
10167 used_at = 1;
10168 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10169 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10170 &offset_expr, AT, ZERO);
10171 }
10172 break;
10173
10174 case M_DDIV_3:
10175 dbl = 1;
10176 case M_DIV_3:
10177 s = "mflo";
10178 goto do_div3;
10179 case M_DREM_3:
10180 dbl = 1;
10181 case M_REM_3:
10182 s = "mfhi";
10183 do_div3:
10184 if (op[2] == 0)
10185 {
10186 as_warn (_("divide by zero"));
10187 if (mips_trap)
10188 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10189 else
10190 macro_build (NULL, "break", BRK_FMT, 7);
10191 break;
10192 }
10193
10194 start_noreorder ();
10195 if (mips_trap)
10196 {
10197 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10198 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10199 }
10200 else
10201 {
10202 if (mips_opts.micromips)
10203 micromips_label_expr (&label_expr);
10204 else
10205 label_expr.X_add_number = 8;
10206 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10207 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10208 macro_build (NULL, "break", BRK_FMT, 7);
10209 if (mips_opts.micromips)
10210 micromips_add_label ();
10211 }
10212 expr1.X_add_number = -1;
10213 used_at = 1;
10214 load_register (AT, &expr1, dbl);
10215 if (mips_opts.micromips)
10216 micromips_label_expr (&label_expr);
10217 else
10218 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
10219 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
10220 if (dbl)
10221 {
10222 expr1.X_add_number = 1;
10223 load_register (AT, &expr1, dbl);
10224 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
10225 }
10226 else
10227 {
10228 expr1.X_add_number = 0x80000000;
10229 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
10230 }
10231 if (mips_trap)
10232 {
10233 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
10234 /* We want to close the noreorder block as soon as possible, so
10235 that later insns are available for delay slot filling. */
10236 end_noreorder ();
10237 }
10238 else
10239 {
10240 if (mips_opts.micromips)
10241 micromips_label_expr (&label_expr);
10242 else
10243 label_expr.X_add_number = 8;
10244 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
10245 macro_build (NULL, "nop", "");
10246
10247 /* We want to close the noreorder block as soon as possible, so
10248 that later insns are available for delay slot filling. */
10249 end_noreorder ();
10250
10251 macro_build (NULL, "break", BRK_FMT, 6);
10252 }
10253 if (mips_opts.micromips)
10254 micromips_add_label ();
10255 macro_build (NULL, s, MFHL_FMT, op[0]);
10256 break;
10257
10258 case M_DIV_3I:
10259 s = "div";
10260 s2 = "mflo";
10261 goto do_divi;
10262 case M_DIVU_3I:
10263 s = "divu";
10264 s2 = "mflo";
10265 goto do_divi;
10266 case M_REM_3I:
10267 s = "div";
10268 s2 = "mfhi";
10269 goto do_divi;
10270 case M_REMU_3I:
10271 s = "divu";
10272 s2 = "mfhi";
10273 goto do_divi;
10274 case M_DDIV_3I:
10275 dbl = 1;
10276 s = "ddiv";
10277 s2 = "mflo";
10278 goto do_divi;
10279 case M_DDIVU_3I:
10280 dbl = 1;
10281 s = "ddivu";
10282 s2 = "mflo";
10283 goto do_divi;
10284 case M_DREM_3I:
10285 dbl = 1;
10286 s = "ddiv";
10287 s2 = "mfhi";
10288 goto do_divi;
10289 case M_DREMU_3I:
10290 dbl = 1;
10291 s = "ddivu";
10292 s2 = "mfhi";
10293 do_divi:
10294 if (imm_expr.X_add_number == 0)
10295 {
10296 as_warn (_("divide by zero"));
10297 if (mips_trap)
10298 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10299 else
10300 macro_build (NULL, "break", BRK_FMT, 7);
10301 break;
10302 }
10303 if (imm_expr.X_add_number == 1)
10304 {
10305 if (strcmp (s2, "mflo") == 0)
10306 move_register (op[0], op[1]);
10307 else
10308 move_register (op[0], ZERO);
10309 break;
10310 }
10311 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
10312 {
10313 if (strcmp (s2, "mflo") == 0)
10314 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
10315 else
10316 move_register (op[0], ZERO);
10317 break;
10318 }
10319
10320 used_at = 1;
10321 load_register (AT, &imm_expr, dbl);
10322 macro_build (NULL, s, "z,s,t", op[1], AT);
10323 macro_build (NULL, s2, MFHL_FMT, op[0]);
10324 break;
10325
10326 case M_DIVU_3:
10327 s = "divu";
10328 s2 = "mflo";
10329 goto do_divu3;
10330 case M_REMU_3:
10331 s = "divu";
10332 s2 = "mfhi";
10333 goto do_divu3;
10334 case M_DDIVU_3:
10335 s = "ddivu";
10336 s2 = "mflo";
10337 goto do_divu3;
10338 case M_DREMU_3:
10339 s = "ddivu";
10340 s2 = "mfhi";
10341 do_divu3:
10342 start_noreorder ();
10343 if (mips_trap)
10344 {
10345 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10346 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10347 /* We want to close the noreorder block as soon as possible, so
10348 that later insns are available for delay slot filling. */
10349 end_noreorder ();
10350 }
10351 else
10352 {
10353 if (mips_opts.micromips)
10354 micromips_label_expr (&label_expr);
10355 else
10356 label_expr.X_add_number = 8;
10357 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10358 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10359
10360 /* We want to close the noreorder block as soon as possible, so
10361 that later insns are available for delay slot filling. */
10362 end_noreorder ();
10363 macro_build (NULL, "break", BRK_FMT, 7);
10364 if (mips_opts.micromips)
10365 micromips_add_label ();
10366 }
10367 macro_build (NULL, s2, MFHL_FMT, op[0]);
10368 break;
10369
10370 case M_DLCA_AB:
10371 dbl = 1;
10372 case M_LCA_AB:
10373 call = 1;
10374 goto do_la;
10375 case M_DLA_AB:
10376 dbl = 1;
10377 case M_LA_AB:
10378 do_la:
10379 /* Load the address of a symbol into a register. If breg is not
10380 zero, we then add a base register to it. */
10381
10382 breg = op[2];
10383 if (dbl && GPR_SIZE == 32)
10384 as_warn (_("dla used to load 32-bit register; recommend using la "
10385 "instead"));
10386
10387 if (!dbl && HAVE_64BIT_OBJECTS)
10388 as_warn (_("la used to load 64-bit address; recommend using dla "
10389 "instead"));
10390
10391 if (small_offset_p (0, align, 16))
10392 {
10393 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
10394 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10395 break;
10396 }
10397
10398 if (mips_opts.at && (op[0] == breg))
10399 {
10400 tempreg = AT;
10401 used_at = 1;
10402 }
10403 else
10404 tempreg = op[0];
10405
10406 if (offset_expr.X_op != O_symbol
10407 && offset_expr.X_op != O_constant)
10408 {
10409 as_bad (_("expression too complex"));
10410 offset_expr.X_op = O_constant;
10411 }
10412
10413 if (offset_expr.X_op == O_constant)
10414 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
10415 else if (mips_pic == NO_PIC)
10416 {
10417 /* If this is a reference to a GP relative symbol, we want
10418 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
10419 Otherwise we want
10420 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10421 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10422 If we have a constant, we need two instructions anyhow,
10423 so we may as well always use the latter form.
10424
10425 With 64bit address space and a usable $at we want
10426 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10427 lui $at,<sym> (BFD_RELOC_HI16_S)
10428 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10429 daddiu $at,<sym> (BFD_RELOC_LO16)
10430 dsll32 $tempreg,0
10431 daddu $tempreg,$tempreg,$at
10432
10433 If $at is already in use, we use a path which is suboptimal
10434 on superscalar processors.
10435 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10436 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10437 dsll $tempreg,16
10438 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10439 dsll $tempreg,16
10440 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10441
10442 For GP relative symbols in 64bit address space we can use
10443 the same sequence as in 32bit address space. */
10444 if (HAVE_64BIT_SYMBOLS)
10445 {
10446 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10447 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10448 {
10449 relax_start (offset_expr.X_add_symbol);
10450 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10451 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10452 relax_switch ();
10453 }
10454
10455 if (used_at == 0 && mips_opts.at)
10456 {
10457 macro_build (&offset_expr, "lui", LUI_FMT,
10458 tempreg, BFD_RELOC_MIPS_HIGHEST);
10459 macro_build (&offset_expr, "lui", LUI_FMT,
10460 AT, BFD_RELOC_HI16_S);
10461 macro_build (&offset_expr, "daddiu", "t,r,j",
10462 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10463 macro_build (&offset_expr, "daddiu", "t,r,j",
10464 AT, AT, BFD_RELOC_LO16);
10465 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
10466 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
10467 used_at = 1;
10468 }
10469 else
10470 {
10471 macro_build (&offset_expr, "lui", LUI_FMT,
10472 tempreg, BFD_RELOC_MIPS_HIGHEST);
10473 macro_build (&offset_expr, "daddiu", "t,r,j",
10474 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10475 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10476 macro_build (&offset_expr, "daddiu", "t,r,j",
10477 tempreg, tempreg, BFD_RELOC_HI16_S);
10478 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10479 macro_build (&offset_expr, "daddiu", "t,r,j",
10480 tempreg, tempreg, BFD_RELOC_LO16);
10481 }
10482
10483 if (mips_relax.sequence)
10484 relax_end ();
10485 }
10486 else
10487 {
10488 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10489 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10490 {
10491 relax_start (offset_expr.X_add_symbol);
10492 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10493 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10494 relax_switch ();
10495 }
10496 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
10497 as_bad (_("offset too large"));
10498 macro_build_lui (&offset_expr, tempreg);
10499 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10500 tempreg, tempreg, BFD_RELOC_LO16);
10501 if (mips_relax.sequence)
10502 relax_end ();
10503 }
10504 }
10505 else if (!mips_big_got && !HAVE_NEWABI)
10506 {
10507 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10508
10509 /* If this is a reference to an external symbol, and there
10510 is no constant, we want
10511 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10512 or for lca or if tempreg is PIC_CALL_REG
10513 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10514 For a local symbol, we want
10515 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10516 nop
10517 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10518
10519 If we have a small constant, and this is a reference to
10520 an external symbol, we want
10521 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10522 nop
10523 addiu $tempreg,$tempreg,<constant>
10524 For a local symbol, we want the same instruction
10525 sequence, but we output a BFD_RELOC_LO16 reloc on the
10526 addiu instruction.
10527
10528 If we have a large constant, and this is a reference to
10529 an external symbol, we want
10530 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10531 lui $at,<hiconstant>
10532 addiu $at,$at,<loconstant>
10533 addu $tempreg,$tempreg,$at
10534 For a local symbol, we want the same instruction
10535 sequence, but we output a BFD_RELOC_LO16 reloc on the
10536 addiu instruction.
10537 */
10538
10539 if (offset_expr.X_add_number == 0)
10540 {
10541 if (mips_pic == SVR4_PIC
10542 && breg == 0
10543 && (call || tempreg == PIC_CALL_REG))
10544 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10545
10546 relax_start (offset_expr.X_add_symbol);
10547 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10548 lw_reloc_type, mips_gp_register);
10549 if (breg != 0)
10550 {
10551 /* We're going to put in an addu instruction using
10552 tempreg, so we may as well insert the nop right
10553 now. */
10554 load_delay_nop ();
10555 }
10556 relax_switch ();
10557 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10558 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
10559 load_delay_nop ();
10560 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10561 tempreg, tempreg, BFD_RELOC_LO16);
10562 relax_end ();
10563 /* FIXME: If breg == 0, and the next instruction uses
10564 $tempreg, then if this variant case is used an extra
10565 nop will be generated. */
10566 }
10567 else if (offset_expr.X_add_number >= -0x8000
10568 && offset_expr.X_add_number < 0x8000)
10569 {
10570 load_got_offset (tempreg, &offset_expr);
10571 load_delay_nop ();
10572 add_got_offset (tempreg, &offset_expr);
10573 }
10574 else
10575 {
10576 expr1.X_add_number = offset_expr.X_add_number;
10577 offset_expr.X_add_number =
10578 SEXT_16BIT (offset_expr.X_add_number);
10579 load_got_offset (tempreg, &offset_expr);
10580 offset_expr.X_add_number = expr1.X_add_number;
10581 /* If we are going to add in a base register, and the
10582 target register and the base register are the same,
10583 then we are using AT as a temporary register. Since
10584 we want to load the constant into AT, we add our
10585 current AT (from the global offset table) and the
10586 register into the register now, and pretend we were
10587 not using a base register. */
10588 if (breg == op[0])
10589 {
10590 load_delay_nop ();
10591 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10592 op[0], AT, breg);
10593 breg = 0;
10594 tempreg = op[0];
10595 }
10596 add_got_offset_hilo (tempreg, &offset_expr, AT);
10597 used_at = 1;
10598 }
10599 }
10600 else if (!mips_big_got && HAVE_NEWABI)
10601 {
10602 int add_breg_early = 0;
10603
10604 /* If this is a reference to an external, and there is no
10605 constant, or local symbol (*), with or without a
10606 constant, we want
10607 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10608 or for lca or if tempreg is PIC_CALL_REG
10609 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10610
10611 If we have a small constant, and this is a reference to
10612 an external symbol, we want
10613 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10614 addiu $tempreg,$tempreg,<constant>
10615
10616 If we have a large constant, and this is a reference to
10617 an external symbol, we want
10618 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10619 lui $at,<hiconstant>
10620 addiu $at,$at,<loconstant>
10621 addu $tempreg,$tempreg,$at
10622
10623 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
10624 local symbols, even though it introduces an additional
10625 instruction. */
10626
10627 if (offset_expr.X_add_number)
10628 {
10629 expr1.X_add_number = offset_expr.X_add_number;
10630 offset_expr.X_add_number = 0;
10631
10632 relax_start (offset_expr.X_add_symbol);
10633 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10634 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10635
10636 if (expr1.X_add_number >= -0x8000
10637 && expr1.X_add_number < 0x8000)
10638 {
10639 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10640 tempreg, tempreg, BFD_RELOC_LO16);
10641 }
10642 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
10643 {
10644 unsigned int dreg;
10645
10646 /* If we are going to add in a base register, and the
10647 target register and the base register are the same,
10648 then we are using AT as a temporary register. Since
10649 we want to load the constant into AT, we add our
10650 current AT (from the global offset table) and the
10651 register into the register now, and pretend we were
10652 not using a base register. */
10653 if (breg != op[0])
10654 dreg = tempreg;
10655 else
10656 {
10657 gas_assert (tempreg == AT);
10658 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10659 op[0], AT, breg);
10660 dreg = op[0];
10661 add_breg_early = 1;
10662 }
10663
10664 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
10665 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10666 dreg, dreg, AT);
10667
10668 used_at = 1;
10669 }
10670 else
10671 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10672
10673 relax_switch ();
10674 offset_expr.X_add_number = expr1.X_add_number;
10675
10676 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10677 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10678 if (add_breg_early)
10679 {
10680 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10681 op[0], tempreg, breg);
10682 breg = 0;
10683 tempreg = op[0];
10684 }
10685 relax_end ();
10686 }
10687 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
10688 {
10689 relax_start (offset_expr.X_add_symbol);
10690 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10691 BFD_RELOC_MIPS_CALL16, mips_gp_register);
10692 relax_switch ();
10693 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10694 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10695 relax_end ();
10696 }
10697 else
10698 {
10699 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10700 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10701 }
10702 }
10703 else if (mips_big_got && !HAVE_NEWABI)
10704 {
10705 int gpdelay;
10706 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10707 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
10708 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10709
10710 /* This is the large GOT case. If this is a reference to an
10711 external symbol, and there is no constant, we want
10712 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10713 addu $tempreg,$tempreg,$gp
10714 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10715 or for lca or if tempreg is PIC_CALL_REG
10716 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10717 addu $tempreg,$tempreg,$gp
10718 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
10719 For a local symbol, we want
10720 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10721 nop
10722 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10723
10724 If we have a small constant, and this is a reference to
10725 an external symbol, we want
10726 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10727 addu $tempreg,$tempreg,$gp
10728 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10729 nop
10730 addiu $tempreg,$tempreg,<constant>
10731 For a local symbol, we want
10732 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10733 nop
10734 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
10735
10736 If we have a large constant, and this is a reference to
10737 an external symbol, we want
10738 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10739 addu $tempreg,$tempreg,$gp
10740 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10741 lui $at,<hiconstant>
10742 addiu $at,$at,<loconstant>
10743 addu $tempreg,$tempreg,$at
10744 For a local symbol, we want
10745 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10746 lui $at,<hiconstant>
10747 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
10748 addu $tempreg,$tempreg,$at
10749 */
10750
10751 expr1.X_add_number = offset_expr.X_add_number;
10752 offset_expr.X_add_number = 0;
10753 relax_start (offset_expr.X_add_symbol);
10754 gpdelay = reg_needs_delay (mips_gp_register);
10755 if (expr1.X_add_number == 0 && breg == 0
10756 && (call || tempreg == PIC_CALL_REG))
10757 {
10758 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10759 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10760 }
10761 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
10762 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10763 tempreg, tempreg, mips_gp_register);
10764 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10765 tempreg, lw_reloc_type, tempreg);
10766 if (expr1.X_add_number == 0)
10767 {
10768 if (breg != 0)
10769 {
10770 /* We're going to put in an addu instruction using
10771 tempreg, so we may as well insert the nop right
10772 now. */
10773 load_delay_nop ();
10774 }
10775 }
10776 else if (expr1.X_add_number >= -0x8000
10777 && expr1.X_add_number < 0x8000)
10778 {
10779 load_delay_nop ();
10780 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10781 tempreg, tempreg, BFD_RELOC_LO16);
10782 }
10783 else
10784 {
10785 unsigned int dreg;
10786
10787 /* If we are going to add in a base register, and the
10788 target register and the base register are the same,
10789 then we are using AT as a temporary register. Since
10790 we want to load the constant into AT, we add our
10791 current AT (from the global offset table) and the
10792 register into the register now, and pretend we were
10793 not using a base register. */
10794 if (breg != op[0])
10795 dreg = tempreg;
10796 else
10797 {
10798 gas_assert (tempreg == AT);
10799 load_delay_nop ();
10800 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10801 op[0], AT, breg);
10802 dreg = op[0];
10803 }
10804
10805 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
10806 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
10807
10808 used_at = 1;
10809 }
10810 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
10811 relax_switch ();
10812
10813 if (gpdelay)
10814 {
10815 /* This is needed because this instruction uses $gp, but
10816 the first instruction on the main stream does not. */
10817 macro_build (NULL, "nop", "");
10818 }
10819
10820 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10821 local_reloc_type, mips_gp_register);
10822 if (expr1.X_add_number >= -0x8000
10823 && expr1.X_add_number < 0x8000)
10824 {
10825 load_delay_nop ();
10826 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10827 tempreg, tempreg, BFD_RELOC_LO16);
10828 /* FIXME: If add_number is 0, and there was no base
10829 register, the external symbol case ended with a load,
10830 so if the symbol turns out to not be external, and
10831 the next instruction uses tempreg, an unnecessary nop
10832 will be inserted. */
10833 }
10834 else
10835 {
10836 if (breg == op[0])
10837 {
10838 /* We must add in the base register now, as in the
10839 external symbol case. */
10840 gas_assert (tempreg == AT);
10841 load_delay_nop ();
10842 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10843 op[0], AT, breg);
10844 tempreg = op[0];
10845 /* We set breg to 0 because we have arranged to add
10846 it in in both cases. */
10847 breg = 0;
10848 }
10849
10850 macro_build_lui (&expr1, AT);
10851 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10852 AT, AT, BFD_RELOC_LO16);
10853 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10854 tempreg, tempreg, AT);
10855 used_at = 1;
10856 }
10857 relax_end ();
10858 }
10859 else if (mips_big_got && HAVE_NEWABI)
10860 {
10861 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10862 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
10863 int add_breg_early = 0;
10864
10865 /* This is the large GOT case. If this is a reference to an
10866 external symbol, and there is no constant, we want
10867 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10868 add $tempreg,$tempreg,$gp
10869 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10870 or for lca or if tempreg is PIC_CALL_REG
10871 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10872 add $tempreg,$tempreg,$gp
10873 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
10874
10875 If we have a small constant, and this is a reference to
10876 an external symbol, we want
10877 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10878 add $tempreg,$tempreg,$gp
10879 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10880 addi $tempreg,$tempreg,<constant>
10881
10882 If we have a large constant, and this is a reference to
10883 an external symbol, we want
10884 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10885 addu $tempreg,$tempreg,$gp
10886 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10887 lui $at,<hiconstant>
10888 addi $at,$at,<loconstant>
10889 add $tempreg,$tempreg,$at
10890
10891 If we have NewABI, and we know it's a local symbol, we want
10892 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
10893 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
10894 otherwise we have to resort to GOT_HI16/GOT_LO16. */
10895
10896 relax_start (offset_expr.X_add_symbol);
10897
10898 expr1.X_add_number = offset_expr.X_add_number;
10899 offset_expr.X_add_number = 0;
10900
10901 if (expr1.X_add_number == 0 && breg == 0
10902 && (call || tempreg == PIC_CALL_REG))
10903 {
10904 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10905 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10906 }
10907 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
10908 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10909 tempreg, tempreg, mips_gp_register);
10910 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10911 tempreg, lw_reloc_type, tempreg);
10912
10913 if (expr1.X_add_number == 0)
10914 ;
10915 else if (expr1.X_add_number >= -0x8000
10916 && expr1.X_add_number < 0x8000)
10917 {
10918 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10919 tempreg, tempreg, BFD_RELOC_LO16);
10920 }
10921 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
10922 {
10923 unsigned int dreg;
10924
10925 /* If we are going to add in a base register, and the
10926 target register and the base register are the same,
10927 then we are using AT as a temporary register. Since
10928 we want to load the constant into AT, we add our
10929 current AT (from the global offset table) and the
10930 register into the register now, and pretend we were
10931 not using a base register. */
10932 if (breg != op[0])
10933 dreg = tempreg;
10934 else
10935 {
10936 gas_assert (tempreg == AT);
10937 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10938 op[0], AT, breg);
10939 dreg = op[0];
10940 add_breg_early = 1;
10941 }
10942
10943 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
10944 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
10945
10946 used_at = 1;
10947 }
10948 else
10949 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10950
10951 relax_switch ();
10952 offset_expr.X_add_number = expr1.X_add_number;
10953 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10954 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
10955 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
10956 tempreg, BFD_RELOC_MIPS_GOT_OFST);
10957 if (add_breg_early)
10958 {
10959 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10960 op[0], tempreg, breg);
10961 breg = 0;
10962 tempreg = op[0];
10963 }
10964 relax_end ();
10965 }
10966 else
10967 abort ();
10968
10969 if (breg != 0)
10970 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
10971 break;
10972
10973 case M_MSGSND:
10974 gas_assert (!mips_opts.micromips);
10975 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
10976 break;
10977
10978 case M_MSGLD:
10979 gas_assert (!mips_opts.micromips);
10980 macro_build (NULL, "c2", "C", 0x02);
10981 break;
10982
10983 case M_MSGLD_T:
10984 gas_assert (!mips_opts.micromips);
10985 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
10986 break;
10987
10988 case M_MSGWAIT:
10989 gas_assert (!mips_opts.micromips);
10990 macro_build (NULL, "c2", "C", 3);
10991 break;
10992
10993 case M_MSGWAIT_T:
10994 gas_assert (!mips_opts.micromips);
10995 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
10996 break;
10997
10998 case M_J_A:
10999 /* The j instruction may not be used in PIC code, since it
11000 requires an absolute address. We convert it to a b
11001 instruction. */
11002 if (mips_pic == NO_PIC)
11003 macro_build (&offset_expr, "j", "a");
11004 else
11005 macro_build (&offset_expr, "b", "p");
11006 break;
11007
11008 /* The jal instructions must be handled as macros because when
11009 generating PIC code they expand to multi-instruction
11010 sequences. Normally they are simple instructions. */
11011 case M_JALS_1:
11012 op[1] = op[0];
11013 op[0] = RA;
11014 /* Fall through. */
11015 case M_JALS_2:
11016 gas_assert (mips_opts.micromips);
11017 if (mips_opts.insn32)
11018 {
11019 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11020 break;
11021 }
11022 jals = 1;
11023 goto jal;
11024 case M_JAL_1:
11025 op[1] = op[0];
11026 op[0] = RA;
11027 /* Fall through. */
11028 case M_JAL_2:
11029 jal:
11030 if (mips_pic == NO_PIC)
11031 {
11032 s = jals ? "jalrs" : "jalr";
11033 if (mips_opts.micromips
11034 && !mips_opts.insn32
11035 && op[0] == RA
11036 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11037 macro_build (NULL, s, "mj", op[1]);
11038 else
11039 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11040 }
11041 else
11042 {
11043 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11044 && mips_cprestore_offset >= 0);
11045
11046 if (op[1] != PIC_CALL_REG)
11047 as_warn (_("MIPS PIC call to register other than $25"));
11048
11049 s = ((mips_opts.micromips
11050 && !mips_opts.insn32
11051 && (!mips_opts.noreorder || cprestore))
11052 ? "jalrs" : "jalr");
11053 if (mips_opts.micromips
11054 && !mips_opts.insn32
11055 && op[0] == RA
11056 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11057 macro_build (NULL, s, "mj", op[1]);
11058 else
11059 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11060 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
11061 {
11062 if (mips_cprestore_offset < 0)
11063 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11064 else
11065 {
11066 if (!mips_frame_reg_valid)
11067 {
11068 as_warn (_("no .frame pseudo-op used in PIC code"));
11069 /* Quiet this warning. */
11070 mips_frame_reg_valid = 1;
11071 }
11072 if (!mips_cprestore_valid)
11073 {
11074 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11075 /* Quiet this warning. */
11076 mips_cprestore_valid = 1;
11077 }
11078 if (mips_opts.noreorder)
11079 macro_build (NULL, "nop", "");
11080 expr1.X_add_number = mips_cprestore_offset;
11081 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11082 mips_gp_register,
11083 mips_frame_reg,
11084 HAVE_64BIT_ADDRESSES);
11085 }
11086 }
11087 }
11088
11089 break;
11090
11091 case M_JALS_A:
11092 gas_assert (mips_opts.micromips);
11093 if (mips_opts.insn32)
11094 {
11095 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11096 break;
11097 }
11098 jals = 1;
11099 /* Fall through. */
11100 case M_JAL_A:
11101 if (mips_pic == NO_PIC)
11102 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
11103 else if (mips_pic == SVR4_PIC)
11104 {
11105 /* If this is a reference to an external symbol, and we are
11106 using a small GOT, we want
11107 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11108 nop
11109 jalr $ra,$25
11110 nop
11111 lw $gp,cprestore($sp)
11112 The cprestore value is set using the .cprestore
11113 pseudo-op. If we are using a big GOT, we want
11114 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11115 addu $25,$25,$gp
11116 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11117 nop
11118 jalr $ra,$25
11119 nop
11120 lw $gp,cprestore($sp)
11121 If the symbol is not external, we want
11122 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11123 nop
11124 addiu $25,$25,<sym> (BFD_RELOC_LO16)
11125 jalr $ra,$25
11126 nop
11127 lw $gp,cprestore($sp)
11128
11129 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11130 sequences above, minus nops, unless the symbol is local,
11131 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11132 GOT_DISP. */
11133 if (HAVE_NEWABI)
11134 {
11135 if (!mips_big_got)
11136 {
11137 relax_start (offset_expr.X_add_symbol);
11138 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11139 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11140 mips_gp_register);
11141 relax_switch ();
11142 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11143 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
11144 mips_gp_register);
11145 relax_end ();
11146 }
11147 else
11148 {
11149 relax_start (offset_expr.X_add_symbol);
11150 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11151 BFD_RELOC_MIPS_CALL_HI16);
11152 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11153 PIC_CALL_REG, mips_gp_register);
11154 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11155 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11156 PIC_CALL_REG);
11157 relax_switch ();
11158 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11159 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11160 mips_gp_register);
11161 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11162 PIC_CALL_REG, PIC_CALL_REG,
11163 BFD_RELOC_MIPS_GOT_OFST);
11164 relax_end ();
11165 }
11166
11167 macro_build_jalr (&offset_expr, 0);
11168 }
11169 else
11170 {
11171 relax_start (offset_expr.X_add_symbol);
11172 if (!mips_big_got)
11173 {
11174 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11175 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11176 mips_gp_register);
11177 load_delay_nop ();
11178 relax_switch ();
11179 }
11180 else
11181 {
11182 int gpdelay;
11183
11184 gpdelay = reg_needs_delay (mips_gp_register);
11185 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11186 BFD_RELOC_MIPS_CALL_HI16);
11187 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11188 PIC_CALL_REG, mips_gp_register);
11189 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11190 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11191 PIC_CALL_REG);
11192 load_delay_nop ();
11193 relax_switch ();
11194 if (gpdelay)
11195 macro_build (NULL, "nop", "");
11196 }
11197 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11198 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
11199 mips_gp_register);
11200 load_delay_nop ();
11201 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11202 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
11203 relax_end ();
11204 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
11205
11206 if (mips_cprestore_offset < 0)
11207 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11208 else
11209 {
11210 if (!mips_frame_reg_valid)
11211 {
11212 as_warn (_("no .frame pseudo-op used in PIC code"));
11213 /* Quiet this warning. */
11214 mips_frame_reg_valid = 1;
11215 }
11216 if (!mips_cprestore_valid)
11217 {
11218 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11219 /* Quiet this warning. */
11220 mips_cprestore_valid = 1;
11221 }
11222 if (mips_opts.noreorder)
11223 macro_build (NULL, "nop", "");
11224 expr1.X_add_number = mips_cprestore_offset;
11225 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11226 mips_gp_register,
11227 mips_frame_reg,
11228 HAVE_64BIT_ADDRESSES);
11229 }
11230 }
11231 }
11232 else if (mips_pic == VXWORKS_PIC)
11233 as_bad (_("non-PIC jump used in PIC library"));
11234 else
11235 abort ();
11236
11237 break;
11238
11239 case M_LBUE_AB:
11240 s = "lbue";
11241 fmt = "t,+j(b)";
11242 offbits = 9;
11243 goto ld_st;
11244 case M_LHUE_AB:
11245 s = "lhue";
11246 fmt = "t,+j(b)";
11247 offbits = 9;
11248 goto ld_st;
11249 case M_LBE_AB:
11250 s = "lbe";
11251 fmt = "t,+j(b)";
11252 offbits = 9;
11253 goto ld_st;
11254 case M_LHE_AB:
11255 s = "lhe";
11256 fmt = "t,+j(b)";
11257 offbits = 9;
11258 goto ld_st;
11259 case M_LLE_AB:
11260 s = "lle";
11261 fmt = "t,+j(b)";
11262 offbits = 9;
11263 goto ld_st;
11264 case M_LWE_AB:
11265 s = "lwe";
11266 fmt = "t,+j(b)";
11267 offbits = 9;
11268 goto ld_st;
11269 case M_LWLE_AB:
11270 s = "lwle";
11271 fmt = "t,+j(b)";
11272 offbits = 9;
11273 goto ld_st;
11274 case M_LWRE_AB:
11275 s = "lwre";
11276 fmt = "t,+j(b)";
11277 offbits = 9;
11278 goto ld_st;
11279 case M_SBE_AB:
11280 s = "sbe";
11281 fmt = "t,+j(b)";
11282 offbits = 9;
11283 goto ld_st;
11284 case M_SCE_AB:
11285 s = "sce";
11286 fmt = "t,+j(b)";
11287 offbits = 9;
11288 goto ld_st;
11289 case M_SHE_AB:
11290 s = "she";
11291 fmt = "t,+j(b)";
11292 offbits = 9;
11293 goto ld_st;
11294 case M_SWE_AB:
11295 s = "swe";
11296 fmt = "t,+j(b)";
11297 offbits = 9;
11298 goto ld_st;
11299 case M_SWLE_AB:
11300 s = "swle";
11301 fmt = "t,+j(b)";
11302 offbits = 9;
11303 goto ld_st;
11304 case M_SWRE_AB:
11305 s = "swre";
11306 fmt = "t,+j(b)";
11307 offbits = 9;
11308 goto ld_st;
11309 case M_ACLR_AB:
11310 s = "aclr";
11311 fmt = "\\,~(b)";
11312 offbits = 12;
11313 goto ld_st;
11314 case M_ASET_AB:
11315 s = "aset";
11316 fmt = "\\,~(b)";
11317 offbits = 12;
11318 goto ld_st;
11319 case M_LB_AB:
11320 s = "lb";
11321 fmt = "t,o(b)";
11322 goto ld;
11323 case M_LBU_AB:
11324 s = "lbu";
11325 fmt = "t,o(b)";
11326 goto ld;
11327 case M_LH_AB:
11328 s = "lh";
11329 fmt = "t,o(b)";
11330 goto ld;
11331 case M_LHU_AB:
11332 s = "lhu";
11333 fmt = "t,o(b)";
11334 goto ld;
11335 case M_LW_AB:
11336 s = "lw";
11337 fmt = "t,o(b)";
11338 goto ld;
11339 case M_LWC0_AB:
11340 gas_assert (!mips_opts.micromips);
11341 s = "lwc0";
11342 fmt = "E,o(b)";
11343 /* Itbl support may require additional care here. */
11344 coproc = 1;
11345 goto ld_st;
11346 case M_LWC1_AB:
11347 s = "lwc1";
11348 fmt = "T,o(b)";
11349 /* Itbl support may require additional care here. */
11350 coproc = 1;
11351 goto ld_st;
11352 case M_LWC2_AB:
11353 s = "lwc2";
11354 fmt = COP12_FMT;
11355 offbits = (mips_opts.micromips ? 12
11356 : ISA_IS_R6 (mips_opts.isa) ? 11
11357 : 16);
11358 /* Itbl support may require additional care here. */
11359 coproc = 1;
11360 goto ld_st;
11361 case M_LWC3_AB:
11362 gas_assert (!mips_opts.micromips);
11363 s = "lwc3";
11364 fmt = "E,o(b)";
11365 /* Itbl support may require additional care here. */
11366 coproc = 1;
11367 goto ld_st;
11368 case M_LWL_AB:
11369 s = "lwl";
11370 fmt = MEM12_FMT;
11371 offbits = (mips_opts.micromips ? 12 : 16);
11372 goto ld_st;
11373 case M_LWR_AB:
11374 s = "lwr";
11375 fmt = MEM12_FMT;
11376 offbits = (mips_opts.micromips ? 12 : 16);
11377 goto ld_st;
11378 case M_LDC1_AB:
11379 s = "ldc1";
11380 fmt = "T,o(b)";
11381 /* Itbl support may require additional care here. */
11382 coproc = 1;
11383 goto ld_st;
11384 case M_LDC2_AB:
11385 s = "ldc2";
11386 fmt = COP12_FMT;
11387 offbits = (mips_opts.micromips ? 12
11388 : ISA_IS_R6 (mips_opts.isa) ? 11
11389 : 16);
11390 /* Itbl support may require additional care here. */
11391 coproc = 1;
11392 goto ld_st;
11393 case M_LQC2_AB:
11394 s = "lqc2";
11395 fmt = "+7,o(b)";
11396 /* Itbl support may require additional care here. */
11397 coproc = 1;
11398 goto ld_st;
11399 case M_LDC3_AB:
11400 s = "ldc3";
11401 fmt = "E,o(b)";
11402 /* Itbl support may require additional care here. */
11403 coproc = 1;
11404 goto ld_st;
11405 case M_LDL_AB:
11406 s = "ldl";
11407 fmt = MEM12_FMT;
11408 offbits = (mips_opts.micromips ? 12 : 16);
11409 goto ld_st;
11410 case M_LDR_AB:
11411 s = "ldr";
11412 fmt = MEM12_FMT;
11413 offbits = (mips_opts.micromips ? 12 : 16);
11414 goto ld_st;
11415 case M_LL_AB:
11416 s = "ll";
11417 fmt = LL_SC_FMT;
11418 offbits = (mips_opts.micromips ? 12
11419 : ISA_IS_R6 (mips_opts.isa) ? 9
11420 : 16);
11421 goto ld;
11422 case M_LLD_AB:
11423 s = "lld";
11424 fmt = LL_SC_FMT;
11425 offbits = (mips_opts.micromips ? 12
11426 : ISA_IS_R6 (mips_opts.isa) ? 9
11427 : 16);
11428 goto ld;
11429 case M_LWU_AB:
11430 s = "lwu";
11431 fmt = MEM12_FMT;
11432 offbits = (mips_opts.micromips ? 12 : 16);
11433 goto ld;
11434 case M_LWP_AB:
11435 gas_assert (mips_opts.micromips);
11436 s = "lwp";
11437 fmt = "t,~(b)";
11438 offbits = 12;
11439 lp = 1;
11440 goto ld;
11441 case M_LDP_AB:
11442 gas_assert (mips_opts.micromips);
11443 s = "ldp";
11444 fmt = "t,~(b)";
11445 offbits = 12;
11446 lp = 1;
11447 goto ld;
11448 case M_LWM_AB:
11449 gas_assert (mips_opts.micromips);
11450 s = "lwm";
11451 fmt = "n,~(b)";
11452 offbits = 12;
11453 goto ld_st;
11454 case M_LDM_AB:
11455 gas_assert (mips_opts.micromips);
11456 s = "ldm";
11457 fmt = "n,~(b)";
11458 offbits = 12;
11459 goto ld_st;
11460
11461 ld:
11462 /* We don't want to use $0 as tempreg. */
11463 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
11464 goto ld_st;
11465 else
11466 tempreg = op[0] + lp;
11467 goto ld_noat;
11468
11469 case M_SB_AB:
11470 s = "sb";
11471 fmt = "t,o(b)";
11472 goto ld_st;
11473 case M_SH_AB:
11474 s = "sh";
11475 fmt = "t,o(b)";
11476 goto ld_st;
11477 case M_SW_AB:
11478 s = "sw";
11479 fmt = "t,o(b)";
11480 goto ld_st;
11481 case M_SWC0_AB:
11482 gas_assert (!mips_opts.micromips);
11483 s = "swc0";
11484 fmt = "E,o(b)";
11485 /* Itbl support may require additional care here. */
11486 coproc = 1;
11487 goto ld_st;
11488 case M_SWC1_AB:
11489 s = "swc1";
11490 fmt = "T,o(b)";
11491 /* Itbl support may require additional care here. */
11492 coproc = 1;
11493 goto ld_st;
11494 case M_SWC2_AB:
11495 s = "swc2";
11496 fmt = COP12_FMT;
11497 offbits = (mips_opts.micromips ? 12
11498 : ISA_IS_R6 (mips_opts.isa) ? 11
11499 : 16);
11500 /* Itbl support may require additional care here. */
11501 coproc = 1;
11502 goto ld_st;
11503 case M_SWC3_AB:
11504 gas_assert (!mips_opts.micromips);
11505 s = "swc3";
11506 fmt = "E,o(b)";
11507 /* Itbl support may require additional care here. */
11508 coproc = 1;
11509 goto ld_st;
11510 case M_SWL_AB:
11511 s = "swl";
11512 fmt = MEM12_FMT;
11513 offbits = (mips_opts.micromips ? 12 : 16);
11514 goto ld_st;
11515 case M_SWR_AB:
11516 s = "swr";
11517 fmt = MEM12_FMT;
11518 offbits = (mips_opts.micromips ? 12 : 16);
11519 goto ld_st;
11520 case M_SC_AB:
11521 s = "sc";
11522 fmt = LL_SC_FMT;
11523 offbits = (mips_opts.micromips ? 12
11524 : ISA_IS_R6 (mips_opts.isa) ? 9
11525 : 16);
11526 goto ld_st;
11527 case M_SCD_AB:
11528 s = "scd";
11529 fmt = LL_SC_FMT;
11530 offbits = (mips_opts.micromips ? 12
11531 : ISA_IS_R6 (mips_opts.isa) ? 9
11532 : 16);
11533 goto ld_st;
11534 case M_CACHE_AB:
11535 s = "cache";
11536 fmt = (mips_opts.micromips ? "k,~(b)"
11537 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11538 : "k,o(b)");
11539 offbits = (mips_opts.micromips ? 12
11540 : ISA_IS_R6 (mips_opts.isa) ? 9
11541 : 16);
11542 goto ld_st;
11543 case M_CACHEE_AB:
11544 s = "cachee";
11545 fmt = "k,+j(b)";
11546 offbits = 9;
11547 goto ld_st;
11548 case M_PREF_AB:
11549 s = "pref";
11550 fmt = (mips_opts.micromips ? "k,~(b)"
11551 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11552 : "k,o(b)");
11553 offbits = (mips_opts.micromips ? 12
11554 : ISA_IS_R6 (mips_opts.isa) ? 9
11555 : 16);
11556 goto ld_st;
11557 case M_PREFE_AB:
11558 s = "prefe";
11559 fmt = "k,+j(b)";
11560 offbits = 9;
11561 goto ld_st;
11562 case M_SDC1_AB:
11563 s = "sdc1";
11564 fmt = "T,o(b)";
11565 coproc = 1;
11566 /* Itbl support may require additional care here. */
11567 goto ld_st;
11568 case M_SDC2_AB:
11569 s = "sdc2";
11570 fmt = COP12_FMT;
11571 offbits = (mips_opts.micromips ? 12
11572 : ISA_IS_R6 (mips_opts.isa) ? 11
11573 : 16);
11574 /* Itbl support may require additional care here. */
11575 coproc = 1;
11576 goto ld_st;
11577 case M_SQC2_AB:
11578 s = "sqc2";
11579 fmt = "+7,o(b)";
11580 /* Itbl support may require additional care here. */
11581 coproc = 1;
11582 goto ld_st;
11583 case M_SDC3_AB:
11584 gas_assert (!mips_opts.micromips);
11585 s = "sdc3";
11586 fmt = "E,o(b)";
11587 /* Itbl support may require additional care here. */
11588 coproc = 1;
11589 goto ld_st;
11590 case M_SDL_AB:
11591 s = "sdl";
11592 fmt = MEM12_FMT;
11593 offbits = (mips_opts.micromips ? 12 : 16);
11594 goto ld_st;
11595 case M_SDR_AB:
11596 s = "sdr";
11597 fmt = MEM12_FMT;
11598 offbits = (mips_opts.micromips ? 12 : 16);
11599 goto ld_st;
11600 case M_SWP_AB:
11601 gas_assert (mips_opts.micromips);
11602 s = "swp";
11603 fmt = "t,~(b)";
11604 offbits = 12;
11605 goto ld_st;
11606 case M_SDP_AB:
11607 gas_assert (mips_opts.micromips);
11608 s = "sdp";
11609 fmt = "t,~(b)";
11610 offbits = 12;
11611 goto ld_st;
11612 case M_SWM_AB:
11613 gas_assert (mips_opts.micromips);
11614 s = "swm";
11615 fmt = "n,~(b)";
11616 offbits = 12;
11617 goto ld_st;
11618 case M_SDM_AB:
11619 gas_assert (mips_opts.micromips);
11620 s = "sdm";
11621 fmt = "n,~(b)";
11622 offbits = 12;
11623
11624 ld_st:
11625 tempreg = AT;
11626 ld_noat:
11627 breg = op[2];
11628 if (small_offset_p (0, align, 16))
11629 {
11630 /* The first case exists for M_LD_AB and M_SD_AB, which are
11631 macros for o32 but which should act like normal instructions
11632 otherwise. */
11633 if (offbits == 16)
11634 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
11635 offset_reloc[1], offset_reloc[2], breg);
11636 else if (small_offset_p (0, align, offbits))
11637 {
11638 if (offbits == 0)
11639 macro_build (NULL, s, fmt, op[0], breg);
11640 else
11641 macro_build (NULL, s, fmt, op[0],
11642 (int) offset_expr.X_add_number, breg);
11643 }
11644 else
11645 {
11646 if (tempreg == AT)
11647 used_at = 1;
11648 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11649 tempreg, breg, -1, offset_reloc[0],
11650 offset_reloc[1], offset_reloc[2]);
11651 if (offbits == 0)
11652 macro_build (NULL, s, fmt, op[0], tempreg);
11653 else
11654 macro_build (NULL, s, fmt, op[0], 0, tempreg);
11655 }
11656 break;
11657 }
11658
11659 if (tempreg == AT)
11660 used_at = 1;
11661
11662 if (offset_expr.X_op != O_constant
11663 && offset_expr.X_op != O_symbol)
11664 {
11665 as_bad (_("expression too complex"));
11666 offset_expr.X_op = O_constant;
11667 }
11668
11669 if (HAVE_32BIT_ADDRESSES
11670 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
11671 {
11672 char value [32];
11673
11674 sprintf_vma (value, offset_expr.X_add_number);
11675 as_bad (_("number (0x%s) larger than 32 bits"), value);
11676 }
11677
11678 /* A constant expression in PIC code can be handled just as it
11679 is in non PIC code. */
11680 if (offset_expr.X_op == O_constant)
11681 {
11682 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
11683 offbits == 0 ? 16 : offbits);
11684 offset_expr.X_add_number -= expr1.X_add_number;
11685
11686 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
11687 if (breg != 0)
11688 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11689 tempreg, tempreg, breg);
11690 if (offbits == 0)
11691 {
11692 if (offset_expr.X_add_number != 0)
11693 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
11694 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
11695 macro_build (NULL, s, fmt, op[0], tempreg);
11696 }
11697 else if (offbits == 16)
11698 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
11699 else
11700 macro_build (NULL, s, fmt, op[0],
11701 (int) offset_expr.X_add_number, tempreg);
11702 }
11703 else if (offbits != 16)
11704 {
11705 /* The offset field is too narrow to be used for a low-part
11706 relocation, so load the whole address into the auxillary
11707 register. */
11708 load_address (tempreg, &offset_expr, &used_at);
11709 if (breg != 0)
11710 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11711 tempreg, tempreg, breg);
11712 if (offbits == 0)
11713 macro_build (NULL, s, fmt, op[0], tempreg);
11714 else
11715 macro_build (NULL, s, fmt, op[0], 0, tempreg);
11716 }
11717 else if (mips_pic == NO_PIC)
11718 {
11719 /* If this is a reference to a GP relative symbol, and there
11720 is no base register, we want
11721 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
11722 Otherwise, if there is no base register, we want
11723 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11724 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11725 If we have a constant, we need two instructions anyhow,
11726 so we always use the latter form.
11727
11728 If we have a base register, and this is a reference to a
11729 GP relative symbol, we want
11730 addu $tempreg,$breg,$gp
11731 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
11732 Otherwise we want
11733 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11734 addu $tempreg,$tempreg,$breg
11735 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11736 With a constant we always use the latter case.
11737
11738 With 64bit address space and no base register and $at usable,
11739 we want
11740 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11741 lui $at,<sym> (BFD_RELOC_HI16_S)
11742 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11743 dsll32 $tempreg,0
11744 daddu $tempreg,$at
11745 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11746 If we have a base register, we want
11747 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11748 lui $at,<sym> (BFD_RELOC_HI16_S)
11749 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11750 daddu $at,$breg
11751 dsll32 $tempreg,0
11752 daddu $tempreg,$at
11753 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11754
11755 Without $at we can't generate the optimal path for superscalar
11756 processors here since this would require two temporary registers.
11757 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11758 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11759 dsll $tempreg,16
11760 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11761 dsll $tempreg,16
11762 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11763 If we have a base register, we want
11764 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11765 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11766 dsll $tempreg,16
11767 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11768 dsll $tempreg,16
11769 daddu $tempreg,$tempreg,$breg
11770 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11771
11772 For GP relative symbols in 64bit address space we can use
11773 the same sequence as in 32bit address space. */
11774 if (HAVE_64BIT_SYMBOLS)
11775 {
11776 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11777 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11778 {
11779 relax_start (offset_expr.X_add_symbol);
11780 if (breg == 0)
11781 {
11782 macro_build (&offset_expr, s, fmt, op[0],
11783 BFD_RELOC_GPREL16, mips_gp_register);
11784 }
11785 else
11786 {
11787 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11788 tempreg, breg, mips_gp_register);
11789 macro_build (&offset_expr, s, fmt, op[0],
11790 BFD_RELOC_GPREL16, tempreg);
11791 }
11792 relax_switch ();
11793 }
11794
11795 if (used_at == 0 && mips_opts.at)
11796 {
11797 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
11798 BFD_RELOC_MIPS_HIGHEST);
11799 macro_build (&offset_expr, "lui", LUI_FMT, AT,
11800 BFD_RELOC_HI16_S);
11801 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11802 tempreg, BFD_RELOC_MIPS_HIGHER);
11803 if (breg != 0)
11804 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
11805 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
11806 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
11807 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
11808 tempreg);
11809 used_at = 1;
11810 }
11811 else
11812 {
11813 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
11814 BFD_RELOC_MIPS_HIGHEST);
11815 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11816 tempreg, BFD_RELOC_MIPS_HIGHER);
11817 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11818 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11819 tempreg, BFD_RELOC_HI16_S);
11820 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11821 if (breg != 0)
11822 macro_build (NULL, "daddu", "d,v,t",
11823 tempreg, tempreg, breg);
11824 macro_build (&offset_expr, s, fmt, op[0],
11825 BFD_RELOC_LO16, tempreg);
11826 }
11827
11828 if (mips_relax.sequence)
11829 relax_end ();
11830 break;
11831 }
11832
11833 if (breg == 0)
11834 {
11835 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11836 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11837 {
11838 relax_start (offset_expr.X_add_symbol);
11839 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
11840 mips_gp_register);
11841 relax_switch ();
11842 }
11843 macro_build_lui (&offset_expr, tempreg);
11844 macro_build (&offset_expr, s, fmt, op[0],
11845 BFD_RELOC_LO16, tempreg);
11846 if (mips_relax.sequence)
11847 relax_end ();
11848 }
11849 else
11850 {
11851 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11852 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11853 {
11854 relax_start (offset_expr.X_add_symbol);
11855 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11856 tempreg, breg, mips_gp_register);
11857 macro_build (&offset_expr, s, fmt, op[0],
11858 BFD_RELOC_GPREL16, tempreg);
11859 relax_switch ();
11860 }
11861 macro_build_lui (&offset_expr, tempreg);
11862 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11863 tempreg, tempreg, breg);
11864 macro_build (&offset_expr, s, fmt, op[0],
11865 BFD_RELOC_LO16, tempreg);
11866 if (mips_relax.sequence)
11867 relax_end ();
11868 }
11869 }
11870 else if (!mips_big_got)
11871 {
11872 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11873
11874 /* If this is a reference to an external symbol, we want
11875 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11876 nop
11877 <op> op[0],0($tempreg)
11878 Otherwise we want
11879 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11880 nop
11881 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11882 <op> op[0],0($tempreg)
11883
11884 For NewABI, we want
11885 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11886 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
11887
11888 If there is a base register, we add it to $tempreg before
11889 the <op>. If there is a constant, we stick it in the
11890 <op> instruction. We don't handle constants larger than
11891 16 bits, because we have no way to load the upper 16 bits
11892 (actually, we could handle them for the subset of cases
11893 in which we are not using $at). */
11894 gas_assert (offset_expr.X_op == O_symbol);
11895 if (HAVE_NEWABI)
11896 {
11897 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11898 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11899 if (breg != 0)
11900 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11901 tempreg, tempreg, breg);
11902 macro_build (&offset_expr, s, fmt, op[0],
11903 BFD_RELOC_MIPS_GOT_OFST, tempreg);
11904 break;
11905 }
11906 expr1.X_add_number = offset_expr.X_add_number;
11907 offset_expr.X_add_number = 0;
11908 if (expr1.X_add_number < -0x8000
11909 || expr1.X_add_number >= 0x8000)
11910 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
11911 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11912 lw_reloc_type, mips_gp_register);
11913 load_delay_nop ();
11914 relax_start (offset_expr.X_add_symbol);
11915 relax_switch ();
11916 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11917 tempreg, BFD_RELOC_LO16);
11918 relax_end ();
11919 if (breg != 0)
11920 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11921 tempreg, tempreg, breg);
11922 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
11923 }
11924 else if (mips_big_got && !HAVE_NEWABI)
11925 {
11926 int gpdelay;
11927
11928 /* If this is a reference to an external symbol, we want
11929 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11930 addu $tempreg,$tempreg,$gp
11931 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11932 <op> op[0],0($tempreg)
11933 Otherwise we want
11934 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11935 nop
11936 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11937 <op> op[0],0($tempreg)
11938 If there is a base register, we add it to $tempreg before
11939 the <op>. If there is a constant, we stick it in the
11940 <op> instruction. We don't handle constants larger than
11941 16 bits, because we have no way to load the upper 16 bits
11942 (actually, we could handle them for the subset of cases
11943 in which we are not using $at). */
11944 gas_assert (offset_expr.X_op == O_symbol);
11945 expr1.X_add_number = offset_expr.X_add_number;
11946 offset_expr.X_add_number = 0;
11947 if (expr1.X_add_number < -0x8000
11948 || expr1.X_add_number >= 0x8000)
11949 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
11950 gpdelay = reg_needs_delay (mips_gp_register);
11951 relax_start (offset_expr.X_add_symbol);
11952 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
11953 BFD_RELOC_MIPS_GOT_HI16);
11954 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
11955 mips_gp_register);
11956 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11957 BFD_RELOC_MIPS_GOT_LO16, tempreg);
11958 relax_switch ();
11959 if (gpdelay)
11960 macro_build (NULL, "nop", "");
11961 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11962 BFD_RELOC_MIPS_GOT16, mips_gp_register);
11963 load_delay_nop ();
11964 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11965 tempreg, BFD_RELOC_LO16);
11966 relax_end ();
11967
11968 if (breg != 0)
11969 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11970 tempreg, tempreg, breg);
11971 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
11972 }
11973 else if (mips_big_got && HAVE_NEWABI)
11974 {
11975 /* If this is a reference to an external symbol, we want
11976 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11977 add $tempreg,$tempreg,$gp
11978 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11979 <op> op[0],<ofst>($tempreg)
11980 Otherwise, for local symbols, we want:
11981 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11982 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
11983 gas_assert (offset_expr.X_op == O_symbol);
11984 expr1.X_add_number = offset_expr.X_add_number;
11985 offset_expr.X_add_number = 0;
11986 if (expr1.X_add_number < -0x8000
11987 || expr1.X_add_number >= 0x8000)
11988 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
11989 relax_start (offset_expr.X_add_symbol);
11990 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
11991 BFD_RELOC_MIPS_GOT_HI16);
11992 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
11993 mips_gp_register);
11994 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11995 BFD_RELOC_MIPS_GOT_LO16, tempreg);
11996 if (breg != 0)
11997 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11998 tempreg, tempreg, breg);
11999 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12000
12001 relax_switch ();
12002 offset_expr.X_add_number = expr1.X_add_number;
12003 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12004 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12005 if (breg != 0)
12006 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12007 tempreg, tempreg, breg);
12008 macro_build (&offset_expr, s, fmt, op[0],
12009 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12010 relax_end ();
12011 }
12012 else
12013 abort ();
12014
12015 break;
12016
12017 case M_JRADDIUSP:
12018 gas_assert (mips_opts.micromips);
12019 gas_assert (mips_opts.insn32);
12020 start_noreorder ();
12021 macro_build (NULL, "jr", "s", RA);
12022 expr1.X_add_number = op[0] << 2;
12023 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12024 end_noreorder ();
12025 break;
12026
12027 case M_JRC:
12028 gas_assert (mips_opts.micromips);
12029 gas_assert (mips_opts.insn32);
12030 macro_build (NULL, "jr", "s", op[0]);
12031 if (mips_opts.noreorder)
12032 macro_build (NULL, "nop", "");
12033 break;
12034
12035 case M_LI:
12036 case M_LI_S:
12037 load_register (op[0], &imm_expr, 0);
12038 break;
12039
12040 case M_DLI:
12041 load_register (op[0], &imm_expr, 1);
12042 break;
12043
12044 case M_LI_SS:
12045 if (imm_expr.X_op == O_constant)
12046 {
12047 used_at = 1;
12048 load_register (AT, &imm_expr, 0);
12049 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12050 break;
12051 }
12052 else
12053 {
12054 gas_assert (imm_expr.X_op == O_absent
12055 && offset_expr.X_op == O_symbol
12056 && strcmp (segment_name (S_GET_SEGMENT
12057 (offset_expr.X_add_symbol)),
12058 ".lit4") == 0
12059 && offset_expr.X_add_number == 0);
12060 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
12061 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
12062 break;
12063 }
12064
12065 case M_LI_D:
12066 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12067 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12068 order 32 bits of the value and the low order 32 bits are either
12069 zero or in OFFSET_EXPR. */
12070 if (imm_expr.X_op == O_constant)
12071 {
12072 if (GPR_SIZE == 64)
12073 load_register (op[0], &imm_expr, 1);
12074 else
12075 {
12076 int hreg, lreg;
12077
12078 if (target_big_endian)
12079 {
12080 hreg = op[0];
12081 lreg = op[0] + 1;
12082 }
12083 else
12084 {
12085 hreg = op[0] + 1;
12086 lreg = op[0];
12087 }
12088
12089 if (hreg <= 31)
12090 load_register (hreg, &imm_expr, 0);
12091 if (lreg <= 31)
12092 {
12093 if (offset_expr.X_op == O_absent)
12094 move_register (lreg, 0);
12095 else
12096 {
12097 gas_assert (offset_expr.X_op == O_constant);
12098 load_register (lreg, &offset_expr, 0);
12099 }
12100 }
12101 }
12102 break;
12103 }
12104 gas_assert (imm_expr.X_op == O_absent);
12105
12106 /* We know that sym is in the .rdata section. First we get the
12107 upper 16 bits of the address. */
12108 if (mips_pic == NO_PIC)
12109 {
12110 macro_build_lui (&offset_expr, AT);
12111 used_at = 1;
12112 }
12113 else
12114 {
12115 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12116 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12117 used_at = 1;
12118 }
12119
12120 /* Now we load the register(s). */
12121 if (GPR_SIZE == 64)
12122 {
12123 used_at = 1;
12124 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12125 BFD_RELOC_LO16, AT);
12126 }
12127 else
12128 {
12129 used_at = 1;
12130 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12131 BFD_RELOC_LO16, AT);
12132 if (op[0] != RA)
12133 {
12134 /* FIXME: How in the world do we deal with the possible
12135 overflow here? */
12136 offset_expr.X_add_number += 4;
12137 macro_build (&offset_expr, "lw", "t,o(b)",
12138 op[0] + 1, BFD_RELOC_LO16, AT);
12139 }
12140 }
12141 break;
12142
12143 case M_LI_DD:
12144 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12145 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12146 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12147 the value and the low order 32 bits are either zero or in
12148 OFFSET_EXPR. */
12149 if (imm_expr.X_op == O_constant)
12150 {
12151 used_at = 1;
12152 load_register (AT, &imm_expr, FPR_SIZE == 64);
12153 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12154 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
12155 else
12156 {
12157 if (ISA_HAS_MXHC1 (mips_opts.isa))
12158 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12159 else if (FPR_SIZE != 32)
12160 as_bad (_("Unable to generate `%s' compliant code "
12161 "without mthc1"),
12162 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12163 else
12164 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
12165 if (offset_expr.X_op == O_absent)
12166 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
12167 else
12168 {
12169 gas_assert (offset_expr.X_op == O_constant);
12170 load_register (AT, &offset_expr, 0);
12171 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12172 }
12173 }
12174 break;
12175 }
12176
12177 gas_assert (imm_expr.X_op == O_absent
12178 && offset_expr.X_op == O_symbol
12179 && offset_expr.X_add_number == 0);
12180 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12181 if (strcmp (s, ".lit8") == 0)
12182 {
12183 op[2] = mips_gp_register;
12184 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12185 offset_reloc[1] = BFD_RELOC_UNUSED;
12186 offset_reloc[2] = BFD_RELOC_UNUSED;
12187 }
12188 else
12189 {
12190 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
12191 used_at = 1;
12192 if (mips_pic != NO_PIC)
12193 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12194 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12195 else
12196 {
12197 /* FIXME: This won't work for a 64 bit address. */
12198 macro_build_lui (&offset_expr, AT);
12199 }
12200
12201 op[2] = AT;
12202 offset_reloc[0] = BFD_RELOC_LO16;
12203 offset_reloc[1] = BFD_RELOC_UNUSED;
12204 offset_reloc[2] = BFD_RELOC_UNUSED;
12205 }
12206 align = 8;
12207 /* Fall through */
12208
12209 case M_L_DAB:
12210 /*
12211 * The MIPS assembler seems to check for X_add_number not
12212 * being double aligned and generating:
12213 * lui at,%hi(foo+1)
12214 * addu at,at,v1
12215 * addiu at,at,%lo(foo+1)
12216 * lwc1 f2,0(at)
12217 * lwc1 f3,4(at)
12218 * But, the resulting address is the same after relocation so why
12219 * generate the extra instruction?
12220 */
12221 /* Itbl support may require additional care here. */
12222 coproc = 1;
12223 fmt = "T,o(b)";
12224 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12225 {
12226 s = "ldc1";
12227 goto ld_st;
12228 }
12229 s = "lwc1";
12230 goto ldd_std;
12231
12232 case M_S_DAB:
12233 gas_assert (!mips_opts.micromips);
12234 /* Itbl support may require additional care here. */
12235 coproc = 1;
12236 fmt = "T,o(b)";
12237 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12238 {
12239 s = "sdc1";
12240 goto ld_st;
12241 }
12242 s = "swc1";
12243 goto ldd_std;
12244
12245 case M_LQ_AB:
12246 fmt = "t,o(b)";
12247 s = "lq";
12248 goto ld;
12249
12250 case M_SQ_AB:
12251 fmt = "t,o(b)";
12252 s = "sq";
12253 goto ld_st;
12254
12255 case M_LD_AB:
12256 fmt = "t,o(b)";
12257 if (GPR_SIZE == 64)
12258 {
12259 s = "ld";
12260 goto ld;
12261 }
12262 s = "lw";
12263 goto ldd_std;
12264
12265 case M_SD_AB:
12266 fmt = "t,o(b)";
12267 if (GPR_SIZE == 64)
12268 {
12269 s = "sd";
12270 goto ld_st;
12271 }
12272 s = "sw";
12273
12274 ldd_std:
12275 /* Even on a big endian machine $fn comes before $fn+1. We have
12276 to adjust when loading from memory. We set coproc if we must
12277 load $fn+1 first. */
12278 /* Itbl support may require additional care here. */
12279 if (!target_big_endian)
12280 coproc = 0;
12281
12282 breg = op[2];
12283 if (small_offset_p (0, align, 16))
12284 {
12285 ep = &offset_expr;
12286 if (!small_offset_p (4, align, 16))
12287 {
12288 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12289 -1, offset_reloc[0], offset_reloc[1],
12290 offset_reloc[2]);
12291 expr1.X_add_number = 0;
12292 ep = &expr1;
12293 breg = AT;
12294 used_at = 1;
12295 offset_reloc[0] = BFD_RELOC_LO16;
12296 offset_reloc[1] = BFD_RELOC_UNUSED;
12297 offset_reloc[2] = BFD_RELOC_UNUSED;
12298 }
12299 if (strcmp (s, "lw") == 0 && op[0] == breg)
12300 {
12301 ep->X_add_number += 4;
12302 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
12303 offset_reloc[1], offset_reloc[2], breg);
12304 ep->X_add_number -= 4;
12305 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
12306 offset_reloc[1], offset_reloc[2], breg);
12307 }
12308 else
12309 {
12310 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
12311 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12312 breg);
12313 ep->X_add_number += 4;
12314 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
12315 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12316 breg);
12317 }
12318 break;
12319 }
12320
12321 if (offset_expr.X_op != O_symbol
12322 && offset_expr.X_op != O_constant)
12323 {
12324 as_bad (_("expression too complex"));
12325 offset_expr.X_op = O_constant;
12326 }
12327
12328 if (HAVE_32BIT_ADDRESSES
12329 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12330 {
12331 char value [32];
12332
12333 sprintf_vma (value, offset_expr.X_add_number);
12334 as_bad (_("number (0x%s) larger than 32 bits"), value);
12335 }
12336
12337 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
12338 {
12339 /* If this is a reference to a GP relative symbol, we want
12340 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12341 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
12342 If we have a base register, we use this
12343 addu $at,$breg,$gp
12344 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12345 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
12346 If this is not a GP relative symbol, we want
12347 lui $at,<sym> (BFD_RELOC_HI16_S)
12348 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12349 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12350 If there is a base register, we add it to $at after the
12351 lui instruction. If there is a constant, we always use
12352 the last case. */
12353 if (offset_expr.X_op == O_symbol
12354 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12355 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12356 {
12357 relax_start (offset_expr.X_add_symbol);
12358 if (breg == 0)
12359 {
12360 tempreg = mips_gp_register;
12361 }
12362 else
12363 {
12364 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12365 AT, breg, mips_gp_register);
12366 tempreg = AT;
12367 used_at = 1;
12368 }
12369
12370 /* Itbl support may require additional care here. */
12371 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12372 BFD_RELOC_GPREL16, tempreg);
12373 offset_expr.X_add_number += 4;
12374
12375 /* Set mips_optimize to 2 to avoid inserting an
12376 undesired nop. */
12377 hold_mips_optimize = mips_optimize;
12378 mips_optimize = 2;
12379 /* Itbl support may require additional care here. */
12380 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12381 BFD_RELOC_GPREL16, tempreg);
12382 mips_optimize = hold_mips_optimize;
12383
12384 relax_switch ();
12385
12386 offset_expr.X_add_number -= 4;
12387 }
12388 used_at = 1;
12389 if (offset_high_part (offset_expr.X_add_number, 16)
12390 != offset_high_part (offset_expr.X_add_number + 4, 16))
12391 {
12392 load_address (AT, &offset_expr, &used_at);
12393 offset_expr.X_op = O_constant;
12394 offset_expr.X_add_number = 0;
12395 }
12396 else
12397 macro_build_lui (&offset_expr, AT);
12398 if (breg != 0)
12399 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12400 /* Itbl support may require additional care here. */
12401 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12402 BFD_RELOC_LO16, AT);
12403 /* FIXME: How do we handle overflow here? */
12404 offset_expr.X_add_number += 4;
12405 /* Itbl support may require additional care here. */
12406 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12407 BFD_RELOC_LO16, AT);
12408 if (mips_relax.sequence)
12409 relax_end ();
12410 }
12411 else if (!mips_big_got)
12412 {
12413 /* If this is a reference to an external symbol, we want
12414 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12415 nop
12416 <op> op[0],0($at)
12417 <op> op[0]+1,4($at)
12418 Otherwise we want
12419 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12420 nop
12421 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12422 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12423 If there is a base register we add it to $at before the
12424 lwc1 instructions. If there is a constant we include it
12425 in the lwc1 instructions. */
12426 used_at = 1;
12427 expr1.X_add_number = offset_expr.X_add_number;
12428 if (expr1.X_add_number < -0x8000
12429 || expr1.X_add_number >= 0x8000 - 4)
12430 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12431 load_got_offset (AT, &offset_expr);
12432 load_delay_nop ();
12433 if (breg != 0)
12434 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12435
12436 /* Set mips_optimize to 2 to avoid inserting an undesired
12437 nop. */
12438 hold_mips_optimize = mips_optimize;
12439 mips_optimize = 2;
12440
12441 /* Itbl support may require additional care here. */
12442 relax_start (offset_expr.X_add_symbol);
12443 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12444 BFD_RELOC_LO16, AT);
12445 expr1.X_add_number += 4;
12446 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12447 BFD_RELOC_LO16, AT);
12448 relax_switch ();
12449 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12450 BFD_RELOC_LO16, AT);
12451 offset_expr.X_add_number += 4;
12452 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12453 BFD_RELOC_LO16, AT);
12454 relax_end ();
12455
12456 mips_optimize = hold_mips_optimize;
12457 }
12458 else if (mips_big_got)
12459 {
12460 int gpdelay;
12461
12462 /* If this is a reference to an external symbol, we want
12463 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12464 addu $at,$at,$gp
12465 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12466 nop
12467 <op> op[0],0($at)
12468 <op> op[0]+1,4($at)
12469 Otherwise we want
12470 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12471 nop
12472 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12473 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12474 If there is a base register we add it to $at before the
12475 lwc1 instructions. If there is a constant we include it
12476 in the lwc1 instructions. */
12477 used_at = 1;
12478 expr1.X_add_number = offset_expr.X_add_number;
12479 offset_expr.X_add_number = 0;
12480 if (expr1.X_add_number < -0x8000
12481 || expr1.X_add_number >= 0x8000 - 4)
12482 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12483 gpdelay = reg_needs_delay (mips_gp_register);
12484 relax_start (offset_expr.X_add_symbol);
12485 macro_build (&offset_expr, "lui", LUI_FMT,
12486 AT, BFD_RELOC_MIPS_GOT_HI16);
12487 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12488 AT, AT, mips_gp_register);
12489 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
12490 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
12491 load_delay_nop ();
12492 if (breg != 0)
12493 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12494 /* Itbl support may require additional care here. */
12495 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12496 BFD_RELOC_LO16, AT);
12497 expr1.X_add_number += 4;
12498
12499 /* Set mips_optimize to 2 to avoid inserting an undesired
12500 nop. */
12501 hold_mips_optimize = mips_optimize;
12502 mips_optimize = 2;
12503 /* Itbl support may require additional care here. */
12504 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12505 BFD_RELOC_LO16, AT);
12506 mips_optimize = hold_mips_optimize;
12507 expr1.X_add_number -= 4;
12508
12509 relax_switch ();
12510 offset_expr.X_add_number = expr1.X_add_number;
12511 if (gpdelay)
12512 macro_build (NULL, "nop", "");
12513 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12514 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12515 load_delay_nop ();
12516 if (breg != 0)
12517 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12518 /* Itbl support may require additional care here. */
12519 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12520 BFD_RELOC_LO16, AT);
12521 offset_expr.X_add_number += 4;
12522
12523 /* Set mips_optimize to 2 to avoid inserting an undesired
12524 nop. */
12525 hold_mips_optimize = mips_optimize;
12526 mips_optimize = 2;
12527 /* Itbl support may require additional care here. */
12528 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12529 BFD_RELOC_LO16, AT);
12530 mips_optimize = hold_mips_optimize;
12531 relax_end ();
12532 }
12533 else
12534 abort ();
12535
12536 break;
12537
12538 case M_SAA_AB:
12539 s = "saa";
12540 goto saa_saad;
12541 case M_SAAD_AB:
12542 s = "saad";
12543 saa_saad:
12544 gas_assert (!mips_opts.micromips);
12545 offbits = 0;
12546 fmt = "t,(b)";
12547 goto ld_st;
12548
12549 /* New code added to support COPZ instructions.
12550 This code builds table entries out of the macros in mip_opcodes.
12551 R4000 uses interlocks to handle coproc delays.
12552 Other chips (like the R3000) require nops to be inserted for delays.
12553
12554 FIXME: Currently, we require that the user handle delays.
12555 In order to fill delay slots for non-interlocked chips,
12556 we must have a way to specify delays based on the coprocessor.
12557 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12558 What are the side-effects of the cop instruction?
12559 What cache support might we have and what are its effects?
12560 Both coprocessor & memory require delays. how long???
12561 What registers are read/set/modified?
12562
12563 If an itbl is provided to interpret cop instructions,
12564 this knowledge can be encoded in the itbl spec. */
12565
12566 case M_COP0:
12567 s = "c0";
12568 goto copz;
12569 case M_COP1:
12570 s = "c1";
12571 goto copz;
12572 case M_COP2:
12573 s = "c2";
12574 goto copz;
12575 case M_COP3:
12576 s = "c3";
12577 copz:
12578 gas_assert (!mips_opts.micromips);
12579 /* For now we just do C (same as Cz). The parameter will be
12580 stored in insn_opcode by mips_ip. */
12581 macro_build (NULL, s, "C", (int) ip->insn_opcode);
12582 break;
12583
12584 case M_MOVE:
12585 move_register (op[0], op[1]);
12586 break;
12587
12588 case M_MOVEP:
12589 gas_assert (mips_opts.micromips);
12590 gas_assert (mips_opts.insn32);
12591 move_register (micromips_to_32_reg_h_map1[op[0]],
12592 micromips_to_32_reg_m_map[op[1]]);
12593 move_register (micromips_to_32_reg_h_map2[op[0]],
12594 micromips_to_32_reg_n_map[op[2]]);
12595 break;
12596
12597 case M_DMUL:
12598 dbl = 1;
12599 case M_MUL:
12600 if (mips_opts.arch == CPU_R5900)
12601 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12602 op[2]);
12603 else
12604 {
12605 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12606 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12607 }
12608 break;
12609
12610 case M_DMUL_I:
12611 dbl = 1;
12612 case M_MUL_I:
12613 /* The MIPS assembler some times generates shifts and adds. I'm
12614 not trying to be that fancy. GCC should do this for us
12615 anyway. */
12616 used_at = 1;
12617 load_register (AT, &imm_expr, dbl);
12618 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
12619 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12620 break;
12621
12622 case M_DMULO_I:
12623 dbl = 1;
12624 case M_MULO_I:
12625 imm = 1;
12626 goto do_mulo;
12627
12628 case M_DMULO:
12629 dbl = 1;
12630 case M_MULO:
12631 do_mulo:
12632 start_noreorder ();
12633 used_at = 1;
12634 if (imm)
12635 load_register (AT, &imm_expr, dbl);
12636 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
12637 op[1], imm ? AT : op[2]);
12638 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12639 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
12640 macro_build (NULL, "mfhi", MFHL_FMT, AT);
12641 if (mips_trap)
12642 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
12643 else
12644 {
12645 if (mips_opts.micromips)
12646 micromips_label_expr (&label_expr);
12647 else
12648 label_expr.X_add_number = 8;
12649 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
12650 macro_build (NULL, "nop", "");
12651 macro_build (NULL, "break", BRK_FMT, 6);
12652 if (mips_opts.micromips)
12653 micromips_add_label ();
12654 }
12655 end_noreorder ();
12656 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12657 break;
12658
12659 case M_DMULOU_I:
12660 dbl = 1;
12661 case M_MULOU_I:
12662 imm = 1;
12663 goto do_mulou;
12664
12665 case M_DMULOU:
12666 dbl = 1;
12667 case M_MULOU:
12668 do_mulou:
12669 start_noreorder ();
12670 used_at = 1;
12671 if (imm)
12672 load_register (AT, &imm_expr, dbl);
12673 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
12674 op[1], imm ? AT : op[2]);
12675 macro_build (NULL, "mfhi", MFHL_FMT, AT);
12676 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12677 if (mips_trap)
12678 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
12679 else
12680 {
12681 if (mips_opts.micromips)
12682 micromips_label_expr (&label_expr);
12683 else
12684 label_expr.X_add_number = 8;
12685 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
12686 macro_build (NULL, "nop", "");
12687 macro_build (NULL, "break", BRK_FMT, 6);
12688 if (mips_opts.micromips)
12689 micromips_add_label ();
12690 }
12691 end_noreorder ();
12692 break;
12693
12694 case M_DROL:
12695 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
12696 {
12697 if (op[0] == op[1])
12698 {
12699 tempreg = AT;
12700 used_at = 1;
12701 }
12702 else
12703 tempreg = op[0];
12704 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
12705 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
12706 break;
12707 }
12708 used_at = 1;
12709 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12710 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
12711 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
12712 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12713 break;
12714
12715 case M_ROL:
12716 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
12717 {
12718 if (op[0] == op[1])
12719 {
12720 tempreg = AT;
12721 used_at = 1;
12722 }
12723 else
12724 tempreg = op[0];
12725 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
12726 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
12727 break;
12728 }
12729 used_at = 1;
12730 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12731 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
12732 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
12733 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12734 break;
12735
12736 case M_DROL_I:
12737 {
12738 unsigned int rot;
12739 const char *l;
12740 const char *rr;
12741
12742 rot = imm_expr.X_add_number & 0x3f;
12743 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
12744 {
12745 rot = (64 - rot) & 0x3f;
12746 if (rot >= 32)
12747 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
12748 else
12749 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
12750 break;
12751 }
12752 if (rot == 0)
12753 {
12754 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
12755 break;
12756 }
12757 l = (rot < 0x20) ? "dsll" : "dsll32";
12758 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
12759 rot &= 0x1f;
12760 used_at = 1;
12761 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
12762 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12763 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12764 }
12765 break;
12766
12767 case M_ROL_I:
12768 {
12769 unsigned int rot;
12770
12771 rot = imm_expr.X_add_number & 0x1f;
12772 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
12773 {
12774 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
12775 (32 - rot) & 0x1f);
12776 break;
12777 }
12778 if (rot == 0)
12779 {
12780 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
12781 break;
12782 }
12783 used_at = 1;
12784 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
12785 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12786 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12787 }
12788 break;
12789
12790 case M_DROR:
12791 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
12792 {
12793 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
12794 break;
12795 }
12796 used_at = 1;
12797 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12798 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
12799 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
12800 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12801 break;
12802
12803 case M_ROR:
12804 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
12805 {
12806 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
12807 break;
12808 }
12809 used_at = 1;
12810 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12811 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
12812 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
12813 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12814 break;
12815
12816 case M_DROR_I:
12817 {
12818 unsigned int rot;
12819 const char *l;
12820 const char *rr;
12821
12822 rot = imm_expr.X_add_number & 0x3f;
12823 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
12824 {
12825 if (rot >= 32)
12826 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
12827 else
12828 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
12829 break;
12830 }
12831 if (rot == 0)
12832 {
12833 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
12834 break;
12835 }
12836 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
12837 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
12838 rot &= 0x1f;
12839 used_at = 1;
12840 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
12841 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12842 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12843 }
12844 break;
12845
12846 case M_ROR_I:
12847 {
12848 unsigned int rot;
12849
12850 rot = imm_expr.X_add_number & 0x1f;
12851 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
12852 {
12853 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
12854 break;
12855 }
12856 if (rot == 0)
12857 {
12858 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
12859 break;
12860 }
12861 used_at = 1;
12862 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
12863 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12864 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12865 }
12866 break;
12867
12868 case M_SEQ:
12869 if (op[1] == 0)
12870 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
12871 else if (op[2] == 0)
12872 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
12873 else
12874 {
12875 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
12876 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
12877 }
12878 break;
12879
12880 case M_SEQ_I:
12881 if (imm_expr.X_add_number == 0)
12882 {
12883 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
12884 break;
12885 }
12886 if (op[1] == 0)
12887 {
12888 as_warn (_("instruction %s: result is always false"),
12889 ip->insn_mo->name);
12890 move_register (op[0], 0);
12891 break;
12892 }
12893 if (CPU_HAS_SEQ (mips_opts.arch)
12894 && -512 <= imm_expr.X_add_number
12895 && imm_expr.X_add_number < 512)
12896 {
12897 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
12898 (int) imm_expr.X_add_number);
12899 break;
12900 }
12901 if (imm_expr.X_add_number >= 0
12902 && imm_expr.X_add_number < 0x10000)
12903 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
12904 else if (imm_expr.X_add_number > -0x8000
12905 && imm_expr.X_add_number < 0)
12906 {
12907 imm_expr.X_add_number = -imm_expr.X_add_number;
12908 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
12909 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
12910 }
12911 else if (CPU_HAS_SEQ (mips_opts.arch))
12912 {
12913 used_at = 1;
12914 load_register (AT, &imm_expr, GPR_SIZE == 64);
12915 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
12916 break;
12917 }
12918 else
12919 {
12920 load_register (AT, &imm_expr, GPR_SIZE == 64);
12921 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
12922 used_at = 1;
12923 }
12924 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
12925 break;
12926
12927 case M_SGE: /* X >= Y <==> not (X < Y) */
12928 s = "slt";
12929 goto sge;
12930 case M_SGEU:
12931 s = "sltu";
12932 sge:
12933 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
12934 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
12935 break;
12936
12937 case M_SGE_I: /* X >= I <==> not (X < I) */
12938 case M_SGEU_I:
12939 if (imm_expr.X_add_number >= -0x8000
12940 && imm_expr.X_add_number < 0x8000)
12941 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
12942 op[0], op[1], BFD_RELOC_LO16);
12943 else
12944 {
12945 load_register (AT, &imm_expr, GPR_SIZE == 64);
12946 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
12947 op[0], op[1], AT);
12948 used_at = 1;
12949 }
12950 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
12951 break;
12952
12953 case M_SGT: /* X > Y <==> Y < X */
12954 s = "slt";
12955 goto sgt;
12956 case M_SGTU:
12957 s = "sltu";
12958 sgt:
12959 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
12960 break;
12961
12962 case M_SGT_I: /* X > I <==> I < X */
12963 s = "slt";
12964 goto sgti;
12965 case M_SGTU_I:
12966 s = "sltu";
12967 sgti:
12968 used_at = 1;
12969 load_register (AT, &imm_expr, GPR_SIZE == 64);
12970 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
12971 break;
12972
12973 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
12974 s = "slt";
12975 goto sle;
12976 case M_SLEU:
12977 s = "sltu";
12978 sle:
12979 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
12980 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
12981 break;
12982
12983 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
12984 s = "slt";
12985 goto slei;
12986 case M_SLEU_I:
12987 s = "sltu";
12988 slei:
12989 used_at = 1;
12990 load_register (AT, &imm_expr, GPR_SIZE == 64);
12991 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
12992 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
12993 break;
12994
12995 case M_SLT_I:
12996 if (imm_expr.X_add_number >= -0x8000
12997 && imm_expr.X_add_number < 0x8000)
12998 {
12999 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13000 BFD_RELOC_LO16);
13001 break;
13002 }
13003 used_at = 1;
13004 load_register (AT, &imm_expr, GPR_SIZE == 64);
13005 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
13006 break;
13007
13008 case M_SLTU_I:
13009 if (imm_expr.X_add_number >= -0x8000
13010 && imm_expr.X_add_number < 0x8000)
13011 {
13012 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
13013 BFD_RELOC_LO16);
13014 break;
13015 }
13016 used_at = 1;
13017 load_register (AT, &imm_expr, GPR_SIZE == 64);
13018 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
13019 break;
13020
13021 case M_SNE:
13022 if (op[1] == 0)
13023 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13024 else if (op[2] == 0)
13025 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13026 else
13027 {
13028 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13029 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13030 }
13031 break;
13032
13033 case M_SNE_I:
13034 if (imm_expr.X_add_number == 0)
13035 {
13036 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13037 break;
13038 }
13039 if (op[1] == 0)
13040 {
13041 as_warn (_("instruction %s: result is always true"),
13042 ip->insn_mo->name);
13043 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
13044 op[0], 0, BFD_RELOC_LO16);
13045 break;
13046 }
13047 if (CPU_HAS_SEQ (mips_opts.arch)
13048 && -512 <= imm_expr.X_add_number
13049 && imm_expr.X_add_number < 512)
13050 {
13051 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
13052 (int) imm_expr.X_add_number);
13053 break;
13054 }
13055 if (imm_expr.X_add_number >= 0
13056 && imm_expr.X_add_number < 0x10000)
13057 {
13058 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13059 BFD_RELOC_LO16);
13060 }
13061 else if (imm_expr.X_add_number > -0x8000
13062 && imm_expr.X_add_number < 0)
13063 {
13064 imm_expr.X_add_number = -imm_expr.X_add_number;
13065 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13066 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13067 }
13068 else if (CPU_HAS_SEQ (mips_opts.arch))
13069 {
13070 used_at = 1;
13071 load_register (AT, &imm_expr, GPR_SIZE == 64);
13072 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
13073 break;
13074 }
13075 else
13076 {
13077 load_register (AT, &imm_expr, GPR_SIZE == 64);
13078 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13079 used_at = 1;
13080 }
13081 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13082 break;
13083
13084 case M_SUB_I:
13085 s = "addi";
13086 s2 = "sub";
13087 goto do_subi;
13088 case M_SUBU_I:
13089 s = "addiu";
13090 s2 = "subu";
13091 goto do_subi;
13092 case M_DSUB_I:
13093 dbl = 1;
13094 s = "daddi";
13095 s2 = "dsub";
13096 if (!mips_opts.micromips)
13097 goto do_subi;
13098 if (imm_expr.X_add_number > -0x200
13099 && imm_expr.X_add_number <= 0x200)
13100 {
13101 macro_build (NULL, s, "t,r,.", op[0], op[1],
13102 (int) -imm_expr.X_add_number);
13103 break;
13104 }
13105 goto do_subi_i;
13106 case M_DSUBU_I:
13107 dbl = 1;
13108 s = "daddiu";
13109 s2 = "dsubu";
13110 do_subi:
13111 if (imm_expr.X_add_number > -0x8000
13112 && imm_expr.X_add_number <= 0x8000)
13113 {
13114 imm_expr.X_add_number = -imm_expr.X_add_number;
13115 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13116 break;
13117 }
13118 do_subi_i:
13119 used_at = 1;
13120 load_register (AT, &imm_expr, dbl);
13121 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
13122 break;
13123
13124 case M_TEQ_I:
13125 s = "teq";
13126 goto trap;
13127 case M_TGE_I:
13128 s = "tge";
13129 goto trap;
13130 case M_TGEU_I:
13131 s = "tgeu";
13132 goto trap;
13133 case M_TLT_I:
13134 s = "tlt";
13135 goto trap;
13136 case M_TLTU_I:
13137 s = "tltu";
13138 goto trap;
13139 case M_TNE_I:
13140 s = "tne";
13141 trap:
13142 used_at = 1;
13143 load_register (AT, &imm_expr, GPR_SIZE == 64);
13144 macro_build (NULL, s, "s,t", op[0], AT);
13145 break;
13146
13147 case M_TRUNCWS:
13148 case M_TRUNCWD:
13149 gas_assert (!mips_opts.micromips);
13150 gas_assert (mips_opts.isa == ISA_MIPS1);
13151 used_at = 1;
13152
13153 /*
13154 * Is the double cfc1 instruction a bug in the mips assembler;
13155 * or is there a reason for it?
13156 */
13157 start_noreorder ();
13158 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13159 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13160 macro_build (NULL, "nop", "");
13161 expr1.X_add_number = 3;
13162 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
13163 expr1.X_add_number = 2;
13164 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13165 macro_build (NULL, "ctc1", "t,G", AT, RA);
13166 macro_build (NULL, "nop", "");
13167 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
13168 op[0], op[1]);
13169 macro_build (NULL, "ctc1", "t,G", op[2], RA);
13170 macro_build (NULL, "nop", "");
13171 end_noreorder ();
13172 break;
13173
13174 case M_ULH_AB:
13175 s = "lb";
13176 s2 = "lbu";
13177 off = 1;
13178 goto uld_st;
13179 case M_ULHU_AB:
13180 s = "lbu";
13181 s2 = "lbu";
13182 off = 1;
13183 goto uld_st;
13184 case M_ULW_AB:
13185 s = "lwl";
13186 s2 = "lwr";
13187 offbits = (mips_opts.micromips ? 12 : 16);
13188 off = 3;
13189 goto uld_st;
13190 case M_ULD_AB:
13191 s = "ldl";
13192 s2 = "ldr";
13193 offbits = (mips_opts.micromips ? 12 : 16);
13194 off = 7;
13195 goto uld_st;
13196 case M_USH_AB:
13197 s = "sb";
13198 s2 = "sb";
13199 off = 1;
13200 ust = 1;
13201 goto uld_st;
13202 case M_USW_AB:
13203 s = "swl";
13204 s2 = "swr";
13205 offbits = (mips_opts.micromips ? 12 : 16);
13206 off = 3;
13207 ust = 1;
13208 goto uld_st;
13209 case M_USD_AB:
13210 s = "sdl";
13211 s2 = "sdr";
13212 offbits = (mips_opts.micromips ? 12 : 16);
13213 off = 7;
13214 ust = 1;
13215
13216 uld_st:
13217 breg = op[2];
13218 large_offset = !small_offset_p (off, align, offbits);
13219 ep = &offset_expr;
13220 expr1.X_add_number = 0;
13221 if (large_offset)
13222 {
13223 used_at = 1;
13224 tempreg = AT;
13225 if (small_offset_p (0, align, 16))
13226 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13227 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13228 else
13229 {
13230 load_address (tempreg, ep, &used_at);
13231 if (breg != 0)
13232 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13233 tempreg, tempreg, breg);
13234 }
13235 offset_reloc[0] = BFD_RELOC_LO16;
13236 offset_reloc[1] = BFD_RELOC_UNUSED;
13237 offset_reloc[2] = BFD_RELOC_UNUSED;
13238 breg = tempreg;
13239 tempreg = op[0];
13240 ep = &expr1;
13241 }
13242 else if (!ust && op[0] == breg)
13243 {
13244 used_at = 1;
13245 tempreg = AT;
13246 }
13247 else
13248 tempreg = op[0];
13249
13250 if (off == 1)
13251 goto ulh_sh;
13252
13253 if (!target_big_endian)
13254 ep->X_add_number += off;
13255 if (offbits == 12)
13256 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
13257 else
13258 macro_build (ep, s, "t,o(b)", tempreg, -1,
13259 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13260
13261 if (!target_big_endian)
13262 ep->X_add_number -= off;
13263 else
13264 ep->X_add_number += off;
13265 if (offbits == 12)
13266 macro_build (NULL, s2, "t,~(b)",
13267 tempreg, (int) ep->X_add_number, breg);
13268 else
13269 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13270 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13271
13272 /* If necessary, move the result in tempreg to the final destination. */
13273 if (!ust && op[0] != tempreg)
13274 {
13275 /* Protect second load's delay slot. */
13276 load_delay_nop ();
13277 move_register (op[0], tempreg);
13278 }
13279 break;
13280
13281 ulh_sh:
13282 used_at = 1;
13283 if (target_big_endian == ust)
13284 ep->X_add_number += off;
13285 tempreg = ust || large_offset ? op[0] : AT;
13286 macro_build (ep, s, "t,o(b)", tempreg, -1,
13287 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13288
13289 /* For halfword transfers we need a temporary register to shuffle
13290 bytes. Unfortunately for M_USH_A we have none available before
13291 the next store as AT holds the base address. We deal with this
13292 case by clobbering TREG and then restoring it as with ULH. */
13293 tempreg = ust == large_offset ? op[0] : AT;
13294 if (ust)
13295 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
13296
13297 if (target_big_endian == ust)
13298 ep->X_add_number -= off;
13299 else
13300 ep->X_add_number += off;
13301 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13302 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13303
13304 /* For M_USH_A re-retrieve the LSB. */
13305 if (ust && large_offset)
13306 {
13307 if (target_big_endian)
13308 ep->X_add_number += off;
13309 else
13310 ep->X_add_number -= off;
13311 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13312 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
13313 }
13314 /* For ULH and M_USH_A OR the LSB in. */
13315 if (!ust || large_offset)
13316 {
13317 tempreg = !large_offset ? AT : op[0];
13318 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
13319 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13320 }
13321 break;
13322
13323 default:
13324 /* FIXME: Check if this is one of the itbl macros, since they
13325 are added dynamically. */
13326 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
13327 break;
13328 }
13329 if (!mips_opts.at && used_at)
13330 as_bad (_("macro used $at after \".set noat\""));
13331 }
13332
13333 /* Implement macros in mips16 mode. */
13334
13335 static void
13336 mips16_macro (struct mips_cl_insn *ip)
13337 {
13338 const struct mips_operand_array *operands;
13339 int mask;
13340 int tmp;
13341 expressionS expr1;
13342 int dbl;
13343 const char *s, *s2, *s3;
13344 unsigned int op[MAX_OPERANDS];
13345 unsigned int i;
13346
13347 mask = ip->insn_mo->mask;
13348
13349 operands = insn_operands (ip);
13350 for (i = 0; i < MAX_OPERANDS; i++)
13351 if (operands->operand[i])
13352 op[i] = insn_extract_operand (ip, operands->operand[i]);
13353 else
13354 op[i] = -1;
13355
13356 expr1.X_op = O_constant;
13357 expr1.X_op_symbol = NULL;
13358 expr1.X_add_symbol = NULL;
13359 expr1.X_add_number = 1;
13360
13361 dbl = 0;
13362
13363 switch (mask)
13364 {
13365 default:
13366 abort ();
13367
13368 case M_DDIV_3:
13369 dbl = 1;
13370 case M_DIV_3:
13371 s = "mflo";
13372 goto do_div3;
13373 case M_DREM_3:
13374 dbl = 1;
13375 case M_REM_3:
13376 s = "mfhi";
13377 do_div3:
13378 start_noreorder ();
13379 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", op[1], op[2]);
13380 expr1.X_add_number = 2;
13381 macro_build (&expr1, "bnez", "x,p", op[2]);
13382 macro_build (NULL, "break", "6", 7);
13383
13384 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13385 since that causes an overflow. We should do that as well,
13386 but I don't see how to do the comparisons without a temporary
13387 register. */
13388 end_noreorder ();
13389 macro_build (NULL, s, "x", op[0]);
13390 break;
13391
13392 case M_DIVU_3:
13393 s = "divu";
13394 s2 = "mflo";
13395 goto do_divu3;
13396 case M_REMU_3:
13397 s = "divu";
13398 s2 = "mfhi";
13399 goto do_divu3;
13400 case M_DDIVU_3:
13401 s = "ddivu";
13402 s2 = "mflo";
13403 goto do_divu3;
13404 case M_DREMU_3:
13405 s = "ddivu";
13406 s2 = "mfhi";
13407 do_divu3:
13408 start_noreorder ();
13409 macro_build (NULL, s, "0,x,y", op[1], op[2]);
13410 expr1.X_add_number = 2;
13411 macro_build (&expr1, "bnez", "x,p", op[2]);
13412 macro_build (NULL, "break", "6", 7);
13413 end_noreorder ();
13414 macro_build (NULL, s2, "x", op[0]);
13415 break;
13416
13417 case M_DMUL:
13418 dbl = 1;
13419 case M_MUL:
13420 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13421 macro_build (NULL, "mflo", "x", op[0]);
13422 break;
13423
13424 case M_DSUBU_I:
13425 dbl = 1;
13426 goto do_subu;
13427 case M_SUBU_I:
13428 do_subu:
13429 imm_expr.X_add_number = -imm_expr.X_add_number;
13430 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", op[0], op[1]);
13431 break;
13432
13433 case M_SUBU_I_2:
13434 imm_expr.X_add_number = -imm_expr.X_add_number;
13435 macro_build (&imm_expr, "addiu", "x,k", op[0]);
13436 break;
13437
13438 case M_DSUBU_I_2:
13439 imm_expr.X_add_number = -imm_expr.X_add_number;
13440 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
13441 break;
13442
13443 case M_BEQ:
13444 s = "cmp";
13445 s2 = "bteqz";
13446 goto do_branch;
13447 case M_BNE:
13448 s = "cmp";
13449 s2 = "btnez";
13450 goto do_branch;
13451 case M_BLT:
13452 s = "slt";
13453 s2 = "btnez";
13454 goto do_branch;
13455 case M_BLTU:
13456 s = "sltu";
13457 s2 = "btnez";
13458 goto do_branch;
13459 case M_BLE:
13460 s = "slt";
13461 s2 = "bteqz";
13462 goto do_reverse_branch;
13463 case M_BLEU:
13464 s = "sltu";
13465 s2 = "bteqz";
13466 goto do_reverse_branch;
13467 case M_BGE:
13468 s = "slt";
13469 s2 = "bteqz";
13470 goto do_branch;
13471 case M_BGEU:
13472 s = "sltu";
13473 s2 = "bteqz";
13474 goto do_branch;
13475 case M_BGT:
13476 s = "slt";
13477 s2 = "btnez";
13478 goto do_reverse_branch;
13479 case M_BGTU:
13480 s = "sltu";
13481 s2 = "btnez";
13482
13483 do_reverse_branch:
13484 tmp = op[1];
13485 op[1] = op[0];
13486 op[0] = tmp;
13487
13488 do_branch:
13489 macro_build (NULL, s, "x,y", op[0], op[1]);
13490 macro_build (&offset_expr, s2, "p");
13491 break;
13492
13493 case M_BEQ_I:
13494 s = "cmpi";
13495 s2 = "bteqz";
13496 s3 = "x,U";
13497 goto do_branch_i;
13498 case M_BNE_I:
13499 s = "cmpi";
13500 s2 = "btnez";
13501 s3 = "x,U";
13502 goto do_branch_i;
13503 case M_BLT_I:
13504 s = "slti";
13505 s2 = "btnez";
13506 s3 = "x,8";
13507 goto do_branch_i;
13508 case M_BLTU_I:
13509 s = "sltiu";
13510 s2 = "btnez";
13511 s3 = "x,8";
13512 goto do_branch_i;
13513 case M_BLE_I:
13514 s = "slti";
13515 s2 = "btnez";
13516 s3 = "x,8";
13517 goto do_addone_branch_i;
13518 case M_BLEU_I:
13519 s = "sltiu";
13520 s2 = "btnez";
13521 s3 = "x,8";
13522 goto do_addone_branch_i;
13523 case M_BGE_I:
13524 s = "slti";
13525 s2 = "bteqz";
13526 s3 = "x,8";
13527 goto do_branch_i;
13528 case M_BGEU_I:
13529 s = "sltiu";
13530 s2 = "bteqz";
13531 s3 = "x,8";
13532 goto do_branch_i;
13533 case M_BGT_I:
13534 s = "slti";
13535 s2 = "bteqz";
13536 s3 = "x,8";
13537 goto do_addone_branch_i;
13538 case M_BGTU_I:
13539 s = "sltiu";
13540 s2 = "bteqz";
13541 s3 = "x,8";
13542
13543 do_addone_branch_i:
13544 ++imm_expr.X_add_number;
13545
13546 do_branch_i:
13547 macro_build (&imm_expr, s, s3, op[0]);
13548 macro_build (&offset_expr, s2, "p");
13549 break;
13550
13551 case M_ABS:
13552 expr1.X_add_number = 0;
13553 macro_build (&expr1, "slti", "x,8", op[1]);
13554 if (op[0] != op[1])
13555 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
13556 expr1.X_add_number = 2;
13557 macro_build (&expr1, "bteqz", "p");
13558 macro_build (NULL, "neg", "x,w", op[0], op[0]);
13559 break;
13560 }
13561 }
13562
13563 /* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13564 opcode bits in *OPCODE_EXTRA. */
13565
13566 static struct mips_opcode *
13567 mips_lookup_insn (struct hash_control *hash, const char *start,
13568 ssize_t length, unsigned int *opcode_extra)
13569 {
13570 char *name, *dot, *p;
13571 unsigned int mask, suffix;
13572 ssize_t opend;
13573 struct mips_opcode *insn;
13574
13575 /* Make a copy of the instruction so that we can fiddle with it. */
13576 name = xstrndup (start, length);
13577
13578 /* Look up the instruction as-is. */
13579 insn = (struct mips_opcode *) hash_find (hash, name);
13580 if (insn)
13581 goto end;
13582
13583 dot = strchr (name, '.');
13584 if (dot && dot[1])
13585 {
13586 /* Try to interpret the text after the dot as a VU0 channel suffix. */
13587 p = mips_parse_vu0_channels (dot + 1, &mask);
13588 if (*p == 0 && mask != 0)
13589 {
13590 *dot = 0;
13591 insn = (struct mips_opcode *) hash_find (hash, name);
13592 *dot = '.';
13593 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13594 {
13595 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
13596 goto end;
13597 }
13598 }
13599 }
13600
13601 if (mips_opts.micromips)
13602 {
13603 /* See if there's an instruction size override suffix,
13604 either `16' or `32', at the end of the mnemonic proper,
13605 that defines the operation, i.e. before the first `.'
13606 character if any. Strip it and retry. */
13607 opend = dot != NULL ? dot - name : length;
13608 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13609 suffix = 2;
13610 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13611 suffix = 4;
13612 else
13613 suffix = 0;
13614 if (suffix)
13615 {
13616 memcpy (name + opend - 2, name + opend, length - opend + 1);
13617 insn = (struct mips_opcode *) hash_find (hash, name);
13618 if (insn)
13619 {
13620 forced_insn_length = suffix;
13621 goto end;
13622 }
13623 }
13624 }
13625
13626 insn = NULL;
13627 end:
13628 free (name);
13629 return insn;
13630 }
13631
13632 /* Assemble an instruction into its binary format. If the instruction
13633 is a macro, set imm_expr and offset_expr to the values associated
13634 with "I" and "A" operands respectively. Otherwise store the value
13635 of the relocatable field (if any) in offset_expr. In both cases
13636 set offset_reloc to the relocation operators applied to offset_expr. */
13637
13638 static void
13639 mips_ip (char *str, struct mips_cl_insn *insn)
13640 {
13641 const struct mips_opcode *first, *past;
13642 struct hash_control *hash;
13643 char format;
13644 size_t end;
13645 struct mips_operand_token *tokens;
13646 unsigned int opcode_extra;
13647
13648 if (mips_opts.micromips)
13649 {
13650 hash = micromips_op_hash;
13651 past = &micromips_opcodes[bfd_micromips_num_opcodes];
13652 }
13653 else
13654 {
13655 hash = op_hash;
13656 past = &mips_opcodes[NUMOPCODES];
13657 }
13658 forced_insn_length = 0;
13659 opcode_extra = 0;
13660
13661 /* We first try to match an instruction up to a space or to the end. */
13662 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
13663 continue;
13664
13665 first = mips_lookup_insn (hash, str, end, &opcode_extra);
13666 if (first == NULL)
13667 {
13668 set_insn_error (0, _("unrecognized opcode"));
13669 return;
13670 }
13671
13672 if (strcmp (first->name, "li.s") == 0)
13673 format = 'f';
13674 else if (strcmp (first->name, "li.d") == 0)
13675 format = 'd';
13676 else
13677 format = 0;
13678 tokens = mips_parse_arguments (str + end, format);
13679 if (!tokens)
13680 return;
13681
13682 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
13683 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
13684 set_insn_error (0, _("invalid operands"));
13685
13686 obstack_free (&mips_operand_tokens, tokens);
13687 }
13688
13689 /* As for mips_ip, but used when assembling MIPS16 code.
13690 Also set forced_insn_length to the resulting instruction size in
13691 bytes if the user explicitly requested a small or extended instruction. */
13692
13693 static void
13694 mips16_ip (char *str, struct mips_cl_insn *insn)
13695 {
13696 char *end, *s, c;
13697 struct mips_opcode *first;
13698 struct mips_operand_token *tokens;
13699
13700 forced_insn_length = 0;
13701
13702 for (s = str; ISLOWER (*s); ++s)
13703 ;
13704 end = s;
13705 c = *end;
13706 switch (c)
13707 {
13708 case '\0':
13709 break;
13710
13711 case ' ':
13712 s++;
13713 break;
13714
13715 case '.':
13716 if (s[1] == 't' && s[2] == ' ')
13717 {
13718 forced_insn_length = 2;
13719 s += 3;
13720 break;
13721 }
13722 else if (s[1] == 'e' && s[2] == ' ')
13723 {
13724 forced_insn_length = 4;
13725 s += 3;
13726 break;
13727 }
13728 /* Fall through. */
13729 default:
13730 set_insn_error (0, _("unrecognized opcode"));
13731 return;
13732 }
13733
13734 if (mips_opts.noautoextend && !forced_insn_length)
13735 forced_insn_length = 2;
13736
13737 *end = 0;
13738 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
13739 *end = c;
13740
13741 if (!first)
13742 {
13743 set_insn_error (0, _("unrecognized opcode"));
13744 return;
13745 }
13746
13747 tokens = mips_parse_arguments (s, 0);
13748 if (!tokens)
13749 return;
13750
13751 if (!match_mips16_insns (insn, first, tokens))
13752 set_insn_error (0, _("invalid operands"));
13753
13754 obstack_free (&mips_operand_tokens, tokens);
13755 }
13756
13757 /* Marshal immediate value VAL for an extended MIPS16 instruction.
13758 NBITS is the number of significant bits in VAL. */
13759
13760 static unsigned long
13761 mips16_immed_extend (offsetT val, unsigned int nbits)
13762 {
13763 int extval;
13764 if (nbits == 16)
13765 {
13766 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
13767 val &= 0x1f;
13768 }
13769 else if (nbits == 15)
13770 {
13771 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
13772 val &= 0xf;
13773 }
13774 else
13775 {
13776 extval = ((val & 0x1f) << 6) | (val & 0x20);
13777 val = 0;
13778 }
13779 return (extval << 16) | val;
13780 }
13781
13782 /* Like decode_mips16_operand, but require the operand to be defined and
13783 require it to be an integer. */
13784
13785 static const struct mips_int_operand *
13786 mips16_immed_operand (int type, bfd_boolean extended_p)
13787 {
13788 const struct mips_operand *operand;
13789
13790 operand = decode_mips16_operand (type, extended_p);
13791 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
13792 abort ();
13793 return (const struct mips_int_operand *) operand;
13794 }
13795
13796 /* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
13797
13798 static bfd_boolean
13799 mips16_immed_in_range_p (const struct mips_int_operand *operand,
13800 bfd_reloc_code_real_type reloc, offsetT sval)
13801 {
13802 int min_val, max_val;
13803
13804 min_val = mips_int_operand_min (operand);
13805 max_val = mips_int_operand_max (operand);
13806 if (reloc != BFD_RELOC_UNUSED)
13807 {
13808 if (min_val < 0)
13809 sval = SEXT_16BIT (sval);
13810 else
13811 sval &= 0xffff;
13812 }
13813
13814 return (sval >= min_val
13815 && sval <= max_val
13816 && (sval & ((1 << operand->shift) - 1)) == 0);
13817 }
13818
13819 /* Install immediate value VAL into MIPS16 instruction *INSN,
13820 extending it if necessary. The instruction in *INSN may
13821 already be extended.
13822
13823 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
13824 if none. In the former case, VAL is a 16-bit number with no
13825 defined signedness.
13826
13827 TYPE is the type of the immediate field. USER_INSN_LENGTH
13828 is the length that the user requested, or 0 if none. */
13829
13830 static void
13831 mips16_immed (const char *file, unsigned int line, int type,
13832 bfd_reloc_code_real_type reloc, offsetT val,
13833 unsigned int user_insn_length, unsigned long *insn)
13834 {
13835 const struct mips_int_operand *operand;
13836 unsigned int uval, length;
13837
13838 operand = mips16_immed_operand (type, FALSE);
13839 if (!mips16_immed_in_range_p (operand, reloc, val))
13840 {
13841 /* We need an extended instruction. */
13842 if (user_insn_length == 2)
13843 as_bad_where (file, line, _("invalid unextended operand value"));
13844 else
13845 *insn |= MIPS16_EXTEND;
13846 }
13847 else if (user_insn_length == 4)
13848 {
13849 /* The operand doesn't force an unextended instruction to be extended.
13850 Warn if the user wanted an extended instruction anyway. */
13851 *insn |= MIPS16_EXTEND;
13852 as_warn_where (file, line,
13853 _("extended operand requested but not required"));
13854 }
13855
13856 length = mips16_opcode_length (*insn);
13857 if (length == 4)
13858 {
13859 operand = mips16_immed_operand (type, TRUE);
13860 if (!mips16_immed_in_range_p (operand, reloc, val))
13861 as_bad_where (file, line,
13862 _("operand value out of range for instruction"));
13863 }
13864 uval = ((unsigned int) val >> operand->shift) - operand->bias;
13865 if (length == 2)
13866 *insn = mips_insert_operand (&operand->root, *insn, uval);
13867 else
13868 *insn |= mips16_immed_extend (uval, operand->root.size);
13869 }
13870 \f
13871 struct percent_op_match
13872 {
13873 const char *str;
13874 bfd_reloc_code_real_type reloc;
13875 };
13876
13877 static const struct percent_op_match mips_percent_op[] =
13878 {
13879 {"%lo", BFD_RELOC_LO16},
13880 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
13881 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
13882 {"%call16", BFD_RELOC_MIPS_CALL16},
13883 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
13884 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
13885 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
13886 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
13887 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
13888 {"%got", BFD_RELOC_MIPS_GOT16},
13889 {"%gp_rel", BFD_RELOC_GPREL16},
13890 {"%half", BFD_RELOC_16},
13891 {"%highest", BFD_RELOC_MIPS_HIGHEST},
13892 {"%higher", BFD_RELOC_MIPS_HIGHER},
13893 {"%neg", BFD_RELOC_MIPS_SUB},
13894 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
13895 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
13896 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
13897 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
13898 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
13899 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
13900 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
13901 {"%hi", BFD_RELOC_HI16_S},
13902 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
13903 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
13904 };
13905
13906 static const struct percent_op_match mips16_percent_op[] =
13907 {
13908 {"%lo", BFD_RELOC_MIPS16_LO16},
13909 {"%gprel", BFD_RELOC_MIPS16_GPREL},
13910 {"%got", BFD_RELOC_MIPS16_GOT16},
13911 {"%call16", BFD_RELOC_MIPS16_CALL16},
13912 {"%hi", BFD_RELOC_MIPS16_HI16_S},
13913 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
13914 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
13915 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
13916 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
13917 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
13918 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
13919 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
13920 };
13921
13922
13923 /* Return true if *STR points to a relocation operator. When returning true,
13924 move *STR over the operator and store its relocation code in *RELOC.
13925 Leave both *STR and *RELOC alone when returning false. */
13926
13927 static bfd_boolean
13928 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
13929 {
13930 const struct percent_op_match *percent_op;
13931 size_t limit, i;
13932
13933 if (mips_opts.mips16)
13934 {
13935 percent_op = mips16_percent_op;
13936 limit = ARRAY_SIZE (mips16_percent_op);
13937 }
13938 else
13939 {
13940 percent_op = mips_percent_op;
13941 limit = ARRAY_SIZE (mips_percent_op);
13942 }
13943
13944 for (i = 0; i < limit; i++)
13945 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
13946 {
13947 int len = strlen (percent_op[i].str);
13948
13949 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
13950 continue;
13951
13952 *str += strlen (percent_op[i].str);
13953 *reloc = percent_op[i].reloc;
13954
13955 /* Check whether the output BFD supports this relocation.
13956 If not, issue an error and fall back on something safe. */
13957 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
13958 {
13959 as_bad (_("relocation %s isn't supported by the current ABI"),
13960 percent_op[i].str);
13961 *reloc = BFD_RELOC_UNUSED;
13962 }
13963 return TRUE;
13964 }
13965 return FALSE;
13966 }
13967
13968
13969 /* Parse string STR as a 16-bit relocatable operand. Store the
13970 expression in *EP and the relocations in the array starting
13971 at RELOC. Return the number of relocation operators used.
13972
13973 On exit, EXPR_END points to the first character after the expression. */
13974
13975 static size_t
13976 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
13977 char *str)
13978 {
13979 bfd_reloc_code_real_type reversed_reloc[3];
13980 size_t reloc_index, i;
13981 int crux_depth, str_depth;
13982 char *crux;
13983
13984 /* Search for the start of the main expression, recoding relocations
13985 in REVERSED_RELOC. End the loop with CRUX pointing to the start
13986 of the main expression and with CRUX_DEPTH containing the number
13987 of open brackets at that point. */
13988 reloc_index = -1;
13989 str_depth = 0;
13990 do
13991 {
13992 reloc_index++;
13993 crux = str;
13994 crux_depth = str_depth;
13995
13996 /* Skip over whitespace and brackets, keeping count of the number
13997 of brackets. */
13998 while (*str == ' ' || *str == '\t' || *str == '(')
13999 if (*str++ == '(')
14000 str_depth++;
14001 }
14002 while (*str == '%'
14003 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14004 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14005
14006 my_getExpression (ep, crux);
14007 str = expr_end;
14008
14009 /* Match every open bracket. */
14010 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14011 if (*str++ == ')')
14012 crux_depth--;
14013
14014 if (crux_depth > 0)
14015 as_bad (_("unclosed '('"));
14016
14017 expr_end = str;
14018
14019 if (reloc_index != 0)
14020 {
14021 prev_reloc_op_frag = frag_now;
14022 for (i = 0; i < reloc_index; i++)
14023 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14024 }
14025
14026 return reloc_index;
14027 }
14028
14029 static void
14030 my_getExpression (expressionS *ep, char *str)
14031 {
14032 char *save_in;
14033
14034 save_in = input_line_pointer;
14035 input_line_pointer = str;
14036 expression (ep);
14037 expr_end = input_line_pointer;
14038 input_line_pointer = save_in;
14039 }
14040
14041 const char *
14042 md_atof (int type, char *litP, int *sizeP)
14043 {
14044 return ieee_md_atof (type, litP, sizeP, target_big_endian);
14045 }
14046
14047 void
14048 md_number_to_chars (char *buf, valueT val, int n)
14049 {
14050 if (target_big_endian)
14051 number_to_chars_bigendian (buf, val, n);
14052 else
14053 number_to_chars_littleendian (buf, val, n);
14054 }
14055 \f
14056 static int support_64bit_objects(void)
14057 {
14058 const char **list, **l;
14059 int yes;
14060
14061 list = bfd_target_list ();
14062 for (l = list; *l != NULL; l++)
14063 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14064 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14065 break;
14066 yes = (*l != NULL);
14067 free (list);
14068 return yes;
14069 }
14070
14071 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14072 NEW_VALUE. Warn if another value was already specified. Note:
14073 we have to defer parsing the -march and -mtune arguments in order
14074 to handle 'from-abi' correctly, since the ABI might be specified
14075 in a later argument. */
14076
14077 static void
14078 mips_set_option_string (const char **string_ptr, const char *new_value)
14079 {
14080 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14081 as_warn (_("a different %s was already specified, is now %s"),
14082 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14083 new_value);
14084
14085 *string_ptr = new_value;
14086 }
14087
14088 int
14089 md_parse_option (int c, const char *arg)
14090 {
14091 unsigned int i;
14092
14093 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14094 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14095 {
14096 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
14097 c == mips_ases[i].option_on);
14098 return 1;
14099 }
14100
14101 switch (c)
14102 {
14103 case OPTION_CONSTRUCT_FLOATS:
14104 mips_disable_float_construction = 0;
14105 break;
14106
14107 case OPTION_NO_CONSTRUCT_FLOATS:
14108 mips_disable_float_construction = 1;
14109 break;
14110
14111 case OPTION_TRAP:
14112 mips_trap = 1;
14113 break;
14114
14115 case OPTION_BREAK:
14116 mips_trap = 0;
14117 break;
14118
14119 case OPTION_EB:
14120 target_big_endian = 1;
14121 break;
14122
14123 case OPTION_EL:
14124 target_big_endian = 0;
14125 break;
14126
14127 case 'O':
14128 if (arg == NULL)
14129 mips_optimize = 1;
14130 else if (arg[0] == '0')
14131 mips_optimize = 0;
14132 else if (arg[0] == '1')
14133 mips_optimize = 1;
14134 else
14135 mips_optimize = 2;
14136 break;
14137
14138 case 'g':
14139 if (arg == NULL)
14140 mips_debug = 2;
14141 else
14142 mips_debug = atoi (arg);
14143 break;
14144
14145 case OPTION_MIPS1:
14146 file_mips_opts.isa = ISA_MIPS1;
14147 break;
14148
14149 case OPTION_MIPS2:
14150 file_mips_opts.isa = ISA_MIPS2;
14151 break;
14152
14153 case OPTION_MIPS3:
14154 file_mips_opts.isa = ISA_MIPS3;
14155 break;
14156
14157 case OPTION_MIPS4:
14158 file_mips_opts.isa = ISA_MIPS4;
14159 break;
14160
14161 case OPTION_MIPS5:
14162 file_mips_opts.isa = ISA_MIPS5;
14163 break;
14164
14165 case OPTION_MIPS32:
14166 file_mips_opts.isa = ISA_MIPS32;
14167 break;
14168
14169 case OPTION_MIPS32R2:
14170 file_mips_opts.isa = ISA_MIPS32R2;
14171 break;
14172
14173 case OPTION_MIPS32R3:
14174 file_mips_opts.isa = ISA_MIPS32R3;
14175 break;
14176
14177 case OPTION_MIPS32R5:
14178 file_mips_opts.isa = ISA_MIPS32R5;
14179 break;
14180
14181 case OPTION_MIPS32R6:
14182 file_mips_opts.isa = ISA_MIPS32R6;
14183 break;
14184
14185 case OPTION_MIPS64R2:
14186 file_mips_opts.isa = ISA_MIPS64R2;
14187 break;
14188
14189 case OPTION_MIPS64R3:
14190 file_mips_opts.isa = ISA_MIPS64R3;
14191 break;
14192
14193 case OPTION_MIPS64R5:
14194 file_mips_opts.isa = ISA_MIPS64R5;
14195 break;
14196
14197 case OPTION_MIPS64R6:
14198 file_mips_opts.isa = ISA_MIPS64R6;
14199 break;
14200
14201 case OPTION_MIPS64:
14202 file_mips_opts.isa = ISA_MIPS64;
14203 break;
14204
14205 case OPTION_MTUNE:
14206 mips_set_option_string (&mips_tune_string, arg);
14207 break;
14208
14209 case OPTION_MARCH:
14210 mips_set_option_string (&mips_arch_string, arg);
14211 break;
14212
14213 case OPTION_M4650:
14214 mips_set_option_string (&mips_arch_string, "4650");
14215 mips_set_option_string (&mips_tune_string, "4650");
14216 break;
14217
14218 case OPTION_NO_M4650:
14219 break;
14220
14221 case OPTION_M4010:
14222 mips_set_option_string (&mips_arch_string, "4010");
14223 mips_set_option_string (&mips_tune_string, "4010");
14224 break;
14225
14226 case OPTION_NO_M4010:
14227 break;
14228
14229 case OPTION_M4100:
14230 mips_set_option_string (&mips_arch_string, "4100");
14231 mips_set_option_string (&mips_tune_string, "4100");
14232 break;
14233
14234 case OPTION_NO_M4100:
14235 break;
14236
14237 case OPTION_M3900:
14238 mips_set_option_string (&mips_arch_string, "3900");
14239 mips_set_option_string (&mips_tune_string, "3900");
14240 break;
14241
14242 case OPTION_NO_M3900:
14243 break;
14244
14245 case OPTION_MICROMIPS:
14246 if (file_mips_opts.mips16 == 1)
14247 {
14248 as_bad (_("-mmicromips cannot be used with -mips16"));
14249 return 0;
14250 }
14251 file_mips_opts.micromips = 1;
14252 mips_no_prev_insn ();
14253 break;
14254
14255 case OPTION_NO_MICROMIPS:
14256 file_mips_opts.micromips = 0;
14257 mips_no_prev_insn ();
14258 break;
14259
14260 case OPTION_MIPS16:
14261 if (file_mips_opts.micromips == 1)
14262 {
14263 as_bad (_("-mips16 cannot be used with -micromips"));
14264 return 0;
14265 }
14266 file_mips_opts.mips16 = 1;
14267 mips_no_prev_insn ();
14268 break;
14269
14270 case OPTION_NO_MIPS16:
14271 file_mips_opts.mips16 = 0;
14272 mips_no_prev_insn ();
14273 break;
14274
14275 case OPTION_FIX_24K:
14276 mips_fix_24k = 1;
14277 break;
14278
14279 case OPTION_NO_FIX_24K:
14280 mips_fix_24k = 0;
14281 break;
14282
14283 case OPTION_FIX_RM7000:
14284 mips_fix_rm7000 = 1;
14285 break;
14286
14287 case OPTION_NO_FIX_RM7000:
14288 mips_fix_rm7000 = 0;
14289 break;
14290
14291 case OPTION_FIX_LOONGSON2F_JUMP:
14292 mips_fix_loongson2f_jump = TRUE;
14293 break;
14294
14295 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14296 mips_fix_loongson2f_jump = FALSE;
14297 break;
14298
14299 case OPTION_FIX_LOONGSON2F_NOP:
14300 mips_fix_loongson2f_nop = TRUE;
14301 break;
14302
14303 case OPTION_NO_FIX_LOONGSON2F_NOP:
14304 mips_fix_loongson2f_nop = FALSE;
14305 break;
14306
14307 case OPTION_FIX_VR4120:
14308 mips_fix_vr4120 = 1;
14309 break;
14310
14311 case OPTION_NO_FIX_VR4120:
14312 mips_fix_vr4120 = 0;
14313 break;
14314
14315 case OPTION_FIX_VR4130:
14316 mips_fix_vr4130 = 1;
14317 break;
14318
14319 case OPTION_NO_FIX_VR4130:
14320 mips_fix_vr4130 = 0;
14321 break;
14322
14323 case OPTION_FIX_CN63XXP1:
14324 mips_fix_cn63xxp1 = TRUE;
14325 break;
14326
14327 case OPTION_NO_FIX_CN63XXP1:
14328 mips_fix_cn63xxp1 = FALSE;
14329 break;
14330
14331 case OPTION_RELAX_BRANCH:
14332 mips_relax_branch = 1;
14333 break;
14334
14335 case OPTION_NO_RELAX_BRANCH:
14336 mips_relax_branch = 0;
14337 break;
14338
14339 case OPTION_INSN32:
14340 file_mips_opts.insn32 = TRUE;
14341 break;
14342
14343 case OPTION_NO_INSN32:
14344 file_mips_opts.insn32 = FALSE;
14345 break;
14346
14347 case OPTION_MSHARED:
14348 mips_in_shared = TRUE;
14349 break;
14350
14351 case OPTION_MNO_SHARED:
14352 mips_in_shared = FALSE;
14353 break;
14354
14355 case OPTION_MSYM32:
14356 file_mips_opts.sym32 = TRUE;
14357 break;
14358
14359 case OPTION_MNO_SYM32:
14360 file_mips_opts.sym32 = FALSE;
14361 break;
14362
14363 /* When generating ELF code, we permit -KPIC and -call_shared to
14364 select SVR4_PIC, and -non_shared to select no PIC. This is
14365 intended to be compatible with Irix 5. */
14366 case OPTION_CALL_SHARED:
14367 mips_pic = SVR4_PIC;
14368 mips_abicalls = TRUE;
14369 break;
14370
14371 case OPTION_CALL_NONPIC:
14372 mips_pic = NO_PIC;
14373 mips_abicalls = TRUE;
14374 break;
14375
14376 case OPTION_NON_SHARED:
14377 mips_pic = NO_PIC;
14378 mips_abicalls = FALSE;
14379 break;
14380
14381 /* The -xgot option tells the assembler to use 32 bit offsets
14382 when accessing the got in SVR4_PIC mode. It is for Irix
14383 compatibility. */
14384 case OPTION_XGOT:
14385 mips_big_got = 1;
14386 break;
14387
14388 case 'G':
14389 g_switch_value = atoi (arg);
14390 g_switch_seen = 1;
14391 break;
14392
14393 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14394 and -mabi=64. */
14395 case OPTION_32:
14396 mips_abi = O32_ABI;
14397 break;
14398
14399 case OPTION_N32:
14400 mips_abi = N32_ABI;
14401 break;
14402
14403 case OPTION_64:
14404 mips_abi = N64_ABI;
14405 if (!support_64bit_objects())
14406 as_fatal (_("no compiled in support for 64 bit object file format"));
14407 break;
14408
14409 case OPTION_GP32:
14410 file_mips_opts.gp = 32;
14411 break;
14412
14413 case OPTION_GP64:
14414 file_mips_opts.gp = 64;
14415 break;
14416
14417 case OPTION_FP32:
14418 file_mips_opts.fp = 32;
14419 break;
14420
14421 case OPTION_FPXX:
14422 file_mips_opts.fp = 0;
14423 break;
14424
14425 case OPTION_FP64:
14426 file_mips_opts.fp = 64;
14427 break;
14428
14429 case OPTION_ODD_SPREG:
14430 file_mips_opts.oddspreg = 1;
14431 break;
14432
14433 case OPTION_NO_ODD_SPREG:
14434 file_mips_opts.oddspreg = 0;
14435 break;
14436
14437 case OPTION_SINGLE_FLOAT:
14438 file_mips_opts.single_float = 1;
14439 break;
14440
14441 case OPTION_DOUBLE_FLOAT:
14442 file_mips_opts.single_float = 0;
14443 break;
14444
14445 case OPTION_SOFT_FLOAT:
14446 file_mips_opts.soft_float = 1;
14447 break;
14448
14449 case OPTION_HARD_FLOAT:
14450 file_mips_opts.soft_float = 0;
14451 break;
14452
14453 case OPTION_MABI:
14454 if (strcmp (arg, "32") == 0)
14455 mips_abi = O32_ABI;
14456 else if (strcmp (arg, "o64") == 0)
14457 mips_abi = O64_ABI;
14458 else if (strcmp (arg, "n32") == 0)
14459 mips_abi = N32_ABI;
14460 else if (strcmp (arg, "64") == 0)
14461 {
14462 mips_abi = N64_ABI;
14463 if (! support_64bit_objects())
14464 as_fatal (_("no compiled in support for 64 bit object file "
14465 "format"));
14466 }
14467 else if (strcmp (arg, "eabi") == 0)
14468 mips_abi = EABI_ABI;
14469 else
14470 {
14471 as_fatal (_("invalid abi -mabi=%s"), arg);
14472 return 0;
14473 }
14474 break;
14475
14476 case OPTION_M7000_HILO_FIX:
14477 mips_7000_hilo_fix = TRUE;
14478 break;
14479
14480 case OPTION_MNO_7000_HILO_FIX:
14481 mips_7000_hilo_fix = FALSE;
14482 break;
14483
14484 case OPTION_MDEBUG:
14485 mips_flag_mdebug = TRUE;
14486 break;
14487
14488 case OPTION_NO_MDEBUG:
14489 mips_flag_mdebug = FALSE;
14490 break;
14491
14492 case OPTION_PDR:
14493 mips_flag_pdr = TRUE;
14494 break;
14495
14496 case OPTION_NO_PDR:
14497 mips_flag_pdr = FALSE;
14498 break;
14499
14500 case OPTION_MVXWORKS_PIC:
14501 mips_pic = VXWORKS_PIC;
14502 break;
14503
14504 case OPTION_NAN:
14505 if (strcmp (arg, "2008") == 0)
14506 mips_nan2008 = 1;
14507 else if (strcmp (arg, "legacy") == 0)
14508 mips_nan2008 = 0;
14509 else
14510 {
14511 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
14512 return 0;
14513 }
14514 break;
14515
14516 default:
14517 return 0;
14518 }
14519
14520 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14521
14522 return 1;
14523 }
14524 \f
14525 /* Set up globals to tune for the ISA or processor described by INFO. */
14526
14527 static void
14528 mips_set_tune (const struct mips_cpu_info *info)
14529 {
14530 if (info != 0)
14531 mips_tune = info->cpu;
14532 }
14533
14534
14535 void
14536 mips_after_parse_args (void)
14537 {
14538 const struct mips_cpu_info *arch_info = 0;
14539 const struct mips_cpu_info *tune_info = 0;
14540
14541 /* GP relative stuff not working for PE */
14542 if (strncmp (TARGET_OS, "pe", 2) == 0)
14543 {
14544 if (g_switch_seen && g_switch_value != 0)
14545 as_bad (_("-G not supported in this configuration"));
14546 g_switch_value = 0;
14547 }
14548
14549 if (mips_abi == NO_ABI)
14550 mips_abi = MIPS_DEFAULT_ABI;
14551
14552 /* The following code determines the architecture.
14553 Similar code was added to GCC 3.3 (see override_options() in
14554 config/mips/mips.c). The GAS and GCC code should be kept in sync
14555 as much as possible. */
14556
14557 if (mips_arch_string != 0)
14558 arch_info = mips_parse_cpu ("-march", mips_arch_string);
14559
14560 if (file_mips_opts.isa != ISA_UNKNOWN)
14561 {
14562 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
14563 ISA level specified by -mipsN, while arch_info->isa contains
14564 the -march selection (if any). */
14565 if (arch_info != 0)
14566 {
14567 /* -march takes precedence over -mipsN, since it is more descriptive.
14568 There's no harm in specifying both as long as the ISA levels
14569 are the same. */
14570 if (file_mips_opts.isa != arch_info->isa)
14571 as_bad (_("-%s conflicts with the other architecture options,"
14572 " which imply -%s"),
14573 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
14574 mips_cpu_info_from_isa (arch_info->isa)->name);
14575 }
14576 else
14577 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
14578 }
14579
14580 if (arch_info == 0)
14581 {
14582 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
14583 gas_assert (arch_info);
14584 }
14585
14586 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
14587 as_bad (_("-march=%s is not compatible with the selected ABI"),
14588 arch_info->name);
14589
14590 file_mips_opts.arch = arch_info->cpu;
14591 file_mips_opts.isa = arch_info->isa;
14592
14593 /* Set up initial mips_opts state. */
14594 mips_opts = file_mips_opts;
14595
14596 /* The register size inference code is now placed in
14597 file_mips_check_options. */
14598
14599 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
14600 processor. */
14601 if (mips_tune_string != 0)
14602 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
14603
14604 if (tune_info == 0)
14605 mips_set_tune (arch_info);
14606 else
14607 mips_set_tune (tune_info);
14608
14609 if (mips_flag_mdebug < 0)
14610 mips_flag_mdebug = 0;
14611 }
14612 \f
14613 void
14614 mips_init_after_args (void)
14615 {
14616 /* initialize opcodes */
14617 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
14618 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
14619 }
14620
14621 long
14622 md_pcrel_from (fixS *fixP)
14623 {
14624 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14625 switch (fixP->fx_r_type)
14626 {
14627 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14628 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14629 /* Return the address of the delay slot. */
14630 return addr + 2;
14631
14632 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14633 case BFD_RELOC_MICROMIPS_JMP:
14634 case BFD_RELOC_MIPS16_16_PCREL_S1:
14635 case BFD_RELOC_16_PCREL_S2:
14636 case BFD_RELOC_MIPS_21_PCREL_S2:
14637 case BFD_RELOC_MIPS_26_PCREL_S2:
14638 case BFD_RELOC_MIPS_JMP:
14639 /* Return the address of the delay slot. */
14640 return addr + 4;
14641
14642 case BFD_RELOC_MIPS_18_PCREL_S3:
14643 /* Return the aligned address of the doubleword containing
14644 the instruction. */
14645 return addr & ~7;
14646
14647 default:
14648 return addr;
14649 }
14650 }
14651
14652 /* This is called before the symbol table is processed. In order to
14653 work with gcc when using mips-tfile, we must keep all local labels.
14654 However, in other cases, we want to discard them. If we were
14655 called with -g, but we didn't see any debugging information, it may
14656 mean that gcc is smuggling debugging information through to
14657 mips-tfile, in which case we must generate all local labels. */
14658
14659 void
14660 mips_frob_file_before_adjust (void)
14661 {
14662 #ifndef NO_ECOFF_DEBUGGING
14663 if (ECOFF_DEBUGGING
14664 && mips_debug != 0
14665 && ! ecoff_debugging_seen)
14666 flag_keep_locals = 1;
14667 #endif
14668 }
14669
14670 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
14671 the corresponding LO16 reloc. This is called before md_apply_fix and
14672 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
14673 relocation operators.
14674
14675 For our purposes, a %lo() expression matches a %got() or %hi()
14676 expression if:
14677
14678 (a) it refers to the same symbol; and
14679 (b) the offset applied in the %lo() expression is no lower than
14680 the offset applied in the %got() or %hi().
14681
14682 (b) allows us to cope with code like:
14683
14684 lui $4,%hi(foo)
14685 lh $4,%lo(foo+2)($4)
14686
14687 ...which is legal on RELA targets, and has a well-defined behaviour
14688 if the user knows that adding 2 to "foo" will not induce a carry to
14689 the high 16 bits.
14690
14691 When several %lo()s match a particular %got() or %hi(), we use the
14692 following rules to distinguish them:
14693
14694 (1) %lo()s with smaller offsets are a better match than %lo()s with
14695 higher offsets.
14696
14697 (2) %lo()s with no matching %got() or %hi() are better than those
14698 that already have a matching %got() or %hi().
14699
14700 (3) later %lo()s are better than earlier %lo()s.
14701
14702 These rules are applied in order.
14703
14704 (1) means, among other things, that %lo()s with identical offsets are
14705 chosen if they exist.
14706
14707 (2) means that we won't associate several high-part relocations with
14708 the same low-part relocation unless there's no alternative. Having
14709 several high parts for the same low part is a GNU extension; this rule
14710 allows careful users to avoid it.
14711
14712 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
14713 with the last high-part relocation being at the front of the list.
14714 It therefore makes sense to choose the last matching low-part
14715 relocation, all other things being equal. It's also easier
14716 to code that way. */
14717
14718 void
14719 mips_frob_file (void)
14720 {
14721 struct mips_hi_fixup *l;
14722 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
14723
14724 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
14725 {
14726 segment_info_type *seginfo;
14727 bfd_boolean matched_lo_p;
14728 fixS **hi_pos, **lo_pos, **pos;
14729
14730 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
14731
14732 /* If a GOT16 relocation turns out to be against a global symbol,
14733 there isn't supposed to be a matching LO. Ignore %gots against
14734 constants; we'll report an error for those later. */
14735 if (got16_reloc_p (l->fixp->fx_r_type)
14736 && !(l->fixp->fx_addsy
14737 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
14738 continue;
14739
14740 /* Check quickly whether the next fixup happens to be a matching %lo. */
14741 if (fixup_has_matching_lo_p (l->fixp))
14742 continue;
14743
14744 seginfo = seg_info (l->seg);
14745
14746 /* Set HI_POS to the position of this relocation in the chain.
14747 Set LO_POS to the position of the chosen low-part relocation.
14748 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
14749 relocation that matches an immediately-preceding high-part
14750 relocation. */
14751 hi_pos = NULL;
14752 lo_pos = NULL;
14753 matched_lo_p = FALSE;
14754 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
14755
14756 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
14757 {
14758 if (*pos == l->fixp)
14759 hi_pos = pos;
14760
14761 if ((*pos)->fx_r_type == looking_for_rtype
14762 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
14763 && (*pos)->fx_offset >= l->fixp->fx_offset
14764 && (lo_pos == NULL
14765 || (*pos)->fx_offset < (*lo_pos)->fx_offset
14766 || (!matched_lo_p
14767 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
14768 lo_pos = pos;
14769
14770 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
14771 && fixup_has_matching_lo_p (*pos));
14772 }
14773
14774 /* If we found a match, remove the high-part relocation from its
14775 current position and insert it before the low-part relocation.
14776 Make the offsets match so that fixup_has_matching_lo_p()
14777 will return true.
14778
14779 We don't warn about unmatched high-part relocations since some
14780 versions of gcc have been known to emit dead "lui ...%hi(...)"
14781 instructions. */
14782 if (lo_pos != NULL)
14783 {
14784 l->fixp->fx_offset = (*lo_pos)->fx_offset;
14785 if (l->fixp->fx_next != *lo_pos)
14786 {
14787 *hi_pos = l->fixp->fx_next;
14788 l->fixp->fx_next = *lo_pos;
14789 *lo_pos = l->fixp;
14790 }
14791 }
14792 }
14793 }
14794
14795 int
14796 mips_force_relocation (fixS *fixp)
14797 {
14798 if (generic_force_reloc (fixp))
14799 return 1;
14800
14801 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
14802 so that the linker relaxation can update targets. */
14803 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
14804 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
14805 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
14806 return 1;
14807
14808 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
14809 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
14810 microMIPS symbols so that we can do cross-mode branch diagnostics
14811 and BAL to JALX conversion by the linker. */
14812 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
14813 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
14814 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
14815 && fixp->fx_addsy
14816 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
14817 return 1;
14818
14819 /* We want all PC-relative relocations to be kept for R6 relaxation. */
14820 if (ISA_IS_R6 (file_mips_opts.isa)
14821 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
14822 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
14823 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
14824 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
14825 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
14826 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
14827 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
14828 return 1;
14829
14830 return 0;
14831 }
14832
14833 /* Implement TC_FORCE_RELOCATION_ABS. */
14834
14835 bfd_boolean
14836 mips_force_relocation_abs (fixS *fixp)
14837 {
14838 if (generic_force_reloc (fixp))
14839 return TRUE;
14840
14841 /* These relocations do not have enough bits in the in-place addend
14842 to hold an arbitrary absolute section's offset. */
14843 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
14844 return TRUE;
14845
14846 return FALSE;
14847 }
14848
14849 /* Read the instruction associated with RELOC from BUF. */
14850
14851 static unsigned int
14852 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
14853 {
14854 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14855 return read_compressed_insn (buf, 4);
14856 else
14857 return read_insn (buf);
14858 }
14859
14860 /* Write instruction INSN to BUF, given that it has been relocated
14861 by RELOC. */
14862
14863 static void
14864 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
14865 unsigned long insn)
14866 {
14867 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14868 write_compressed_insn (buf, insn, 4);
14869 else
14870 write_insn (buf, insn);
14871 }
14872
14873 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
14874 to a symbol in another ISA mode, which cannot be converted to JALX. */
14875
14876 static bfd_boolean
14877 fix_bad_cross_mode_jump_p (fixS *fixP)
14878 {
14879 unsigned long opcode;
14880 int other;
14881 char *buf;
14882
14883 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
14884 return FALSE;
14885
14886 other = S_GET_OTHER (fixP->fx_addsy);
14887 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
14888 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
14889 switch (fixP->fx_r_type)
14890 {
14891 case BFD_RELOC_MIPS_JMP:
14892 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
14893 case BFD_RELOC_MICROMIPS_JMP:
14894 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
14895 default:
14896 return FALSE;
14897 }
14898 }
14899
14900 /* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
14901 jump to a symbol in the same ISA mode. */
14902
14903 static bfd_boolean
14904 fix_bad_same_mode_jalx_p (fixS *fixP)
14905 {
14906 unsigned long opcode;
14907 int other;
14908 char *buf;
14909
14910 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
14911 return FALSE;
14912
14913 other = S_GET_OTHER (fixP->fx_addsy);
14914 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
14915 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
14916 switch (fixP->fx_r_type)
14917 {
14918 case BFD_RELOC_MIPS_JMP:
14919 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
14920 case BFD_RELOC_MIPS16_JMP:
14921 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
14922 case BFD_RELOC_MICROMIPS_JMP:
14923 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
14924 default:
14925 return FALSE;
14926 }
14927 }
14928
14929 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
14930 to a symbol whose value plus addend is not aligned according to the
14931 ultimate (after linker relaxation) jump instruction's immediate field
14932 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
14933 regular MIPS code, to (1 << 2). */
14934
14935 static bfd_boolean
14936 fix_bad_misaligned_jump_p (fixS *fixP, int shift)
14937 {
14938 bfd_boolean micro_to_mips_p;
14939 valueT val;
14940 int other;
14941
14942 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
14943 return FALSE;
14944
14945 other = S_GET_OTHER (fixP->fx_addsy);
14946 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
14947 val += fixP->fx_offset;
14948 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
14949 && !ELF_ST_IS_MICROMIPS (other));
14950 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
14951 != ELF_ST_IS_COMPRESSED (other));
14952 }
14953
14954 /* Return TRUE if the instruction pointed to by FIXP is an invalid branch
14955 to a symbol whose annotation indicates another ISA mode. For absolute
14956 symbols check the ISA bit instead.
14957
14958 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
14959 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
14960 MIPS symbols and associated with BAL instructions as these instructions
14961 may be be converted to JALX by the linker. */
14962
14963 static bfd_boolean
14964 fix_bad_cross_mode_branch_p (fixS *fixP)
14965 {
14966 bfd_boolean absolute_p;
14967 unsigned long opcode;
14968 asection *symsec;
14969 valueT val;
14970 int other;
14971 char *buf;
14972
14973 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
14974 return FALSE;
14975
14976 symsec = S_GET_SEGMENT (fixP->fx_addsy);
14977 absolute_p = bfd_is_abs_section (symsec);
14978
14979 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
14980 other = S_GET_OTHER (fixP->fx_addsy);
14981
14982 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
14983 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
14984 switch (fixP->fx_r_type)
14985 {
14986 case BFD_RELOC_16_PCREL_S2:
14987 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
14988 && opcode != 0x0411);
14989 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14990 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
14991 && opcode != 0x4060);
14992 case BFD_RELOC_MIPS_21_PCREL_S2:
14993 case BFD_RELOC_MIPS_26_PCREL_S2:
14994 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
14995 case BFD_RELOC_MIPS16_16_PCREL_S1:
14996 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
14997 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14998 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14999 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15000 default:
15001 abort ();
15002 }
15003 }
15004
15005 /* Return TRUE if the symbol plus addend associated with a regular MIPS
15006 branch instruction pointed to by FIXP is not aligned according to the
15007 branch instruction's immediate field requirement. We need the addend
15008 to preserve the ISA bit and also the sum must not have bit 2 set. We
15009 must explicitly OR in the ISA bit from symbol annotation as the bit
15010 won't be set in the symbol's value then. */
15011
15012 static bfd_boolean
15013 fix_bad_misaligned_branch_p (fixS *fixP)
15014 {
15015 bfd_boolean absolute_p;
15016 asection *symsec;
15017 valueT isa_bit;
15018 valueT val;
15019 valueT off;
15020 int other;
15021
15022 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15023 return FALSE;
15024
15025 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15026 absolute_p = bfd_is_abs_section (symsec);
15027
15028 val = S_GET_VALUE (fixP->fx_addsy);
15029 other = S_GET_OTHER (fixP->fx_addsy);
15030 off = fixP->fx_offset;
15031
15032 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15033 val |= ELF_ST_IS_COMPRESSED (other);
15034 val += off;
15035 return (val & 0x3) != isa_bit;
15036 }
15037
15038 /* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15039 and its calculated value VAL. */
15040
15041 static void
15042 fix_validate_branch (fixS *fixP, valueT val)
15043 {
15044 if (fixP->fx_done && (val & 0x3) != 0)
15045 as_bad_where (fixP->fx_file, fixP->fx_line,
15046 _("branch to misaligned address (0x%lx)"),
15047 (long) (val + md_pcrel_from (fixP)));
15048 else if (fix_bad_cross_mode_branch_p (fixP))
15049 as_bad_where (fixP->fx_file, fixP->fx_line,
15050 _("branch to a symbol in another ISA mode"));
15051 else if (fix_bad_misaligned_branch_p (fixP))
15052 as_bad_where (fixP->fx_file, fixP->fx_line,
15053 _("branch to misaligned address (0x%lx)"),
15054 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15055 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15056 as_bad_where (fixP->fx_file, fixP->fx_line,
15057 _("cannot encode misaligned addend "
15058 "in the relocatable field (0x%lx)"),
15059 (long) fixP->fx_offset);
15060 }
15061
15062 /* Apply a fixup to the object file. */
15063
15064 void
15065 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15066 {
15067 char *buf;
15068 unsigned long insn;
15069 reloc_howto_type *howto;
15070
15071 if (fixP->fx_pcrel)
15072 switch (fixP->fx_r_type)
15073 {
15074 case BFD_RELOC_16_PCREL_S2:
15075 case BFD_RELOC_MIPS16_16_PCREL_S1:
15076 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15077 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15078 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15079 case BFD_RELOC_32_PCREL:
15080 case BFD_RELOC_MIPS_21_PCREL_S2:
15081 case BFD_RELOC_MIPS_26_PCREL_S2:
15082 case BFD_RELOC_MIPS_18_PCREL_S3:
15083 case BFD_RELOC_MIPS_19_PCREL_S2:
15084 case BFD_RELOC_HI16_S_PCREL:
15085 case BFD_RELOC_LO16_PCREL:
15086 break;
15087
15088 case BFD_RELOC_32:
15089 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15090 break;
15091
15092 default:
15093 as_bad_where (fixP->fx_file, fixP->fx_line,
15094 _("PC-relative reference to a different section"));
15095 break;
15096 }
15097
15098 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15099 that have no MIPS ELF equivalent. */
15100 if (fixP->fx_r_type != BFD_RELOC_8)
15101 {
15102 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15103 if (!howto)
15104 return;
15105 }
15106
15107 gas_assert (fixP->fx_size == 2
15108 || fixP->fx_size == 4
15109 || fixP->fx_r_type == BFD_RELOC_8
15110 || fixP->fx_r_type == BFD_RELOC_16
15111 || fixP->fx_r_type == BFD_RELOC_64
15112 || fixP->fx_r_type == BFD_RELOC_CTOR
15113 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15114 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15115 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15116 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15117 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15118 || fixP->fx_r_type == BFD_RELOC_NONE);
15119
15120 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15121
15122 /* Don't treat parts of a composite relocation as done. There are two
15123 reasons for this:
15124
15125 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15126 should nevertheless be emitted if the first part is.
15127
15128 (2) In normal usage, composite relocations are never assembly-time
15129 constants. The easiest way of dealing with the pathological
15130 exceptions is to generate a relocation against STN_UNDEF and
15131 leave everything up to the linker. */
15132 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15133 fixP->fx_done = 1;
15134
15135 switch (fixP->fx_r_type)
15136 {
15137 case BFD_RELOC_MIPS_TLS_GD:
15138 case BFD_RELOC_MIPS_TLS_LDM:
15139 case BFD_RELOC_MIPS_TLS_DTPREL32:
15140 case BFD_RELOC_MIPS_TLS_DTPREL64:
15141 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15142 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15143 case BFD_RELOC_MIPS_TLS_GOTTPREL:
15144 case BFD_RELOC_MIPS_TLS_TPREL32:
15145 case BFD_RELOC_MIPS_TLS_TPREL64:
15146 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15147 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15148 case BFD_RELOC_MICROMIPS_TLS_GD:
15149 case BFD_RELOC_MICROMIPS_TLS_LDM:
15150 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15151 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15152 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15153 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15154 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15155 case BFD_RELOC_MIPS16_TLS_GD:
15156 case BFD_RELOC_MIPS16_TLS_LDM:
15157 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15158 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15159 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15160 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15161 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15162 if (fixP->fx_addsy)
15163 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15164 else
15165 as_bad_where (fixP->fx_file, fixP->fx_line,
15166 _("TLS relocation against a constant"));
15167 break;
15168
15169 case BFD_RELOC_MIPS_JMP:
15170 case BFD_RELOC_MIPS16_JMP:
15171 case BFD_RELOC_MICROMIPS_JMP:
15172 {
15173 int shift;
15174
15175 gas_assert (!fixP->fx_done);
15176
15177 /* Shift is 2, unusually, for microMIPS JALX. */
15178 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15179 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15180 shift = 1;
15181 else
15182 shift = 2;
15183
15184 if (fix_bad_cross_mode_jump_p (fixP))
15185 as_bad_where (fixP->fx_file, fixP->fx_line,
15186 _("jump to a symbol in another ISA mode"));
15187 else if (fix_bad_same_mode_jalx_p (fixP))
15188 as_bad_where (fixP->fx_file, fixP->fx_line,
15189 _("JALX to a symbol in the same ISA mode"));
15190 else if (fix_bad_misaligned_jump_p (fixP, shift))
15191 as_bad_where (fixP->fx_file, fixP->fx_line,
15192 _("jump to misaligned address (0x%lx)"),
15193 (long) (S_GET_VALUE (fixP->fx_addsy)
15194 + fixP->fx_offset));
15195 else if (HAVE_IN_PLACE_ADDENDS
15196 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15197 as_bad_where (fixP->fx_file, fixP->fx_line,
15198 _("cannot encode misaligned addend "
15199 "in the relocatable field (0x%lx)"),
15200 (long) fixP->fx_offset);
15201 }
15202 /* Fall through. */
15203
15204 case BFD_RELOC_MIPS_SHIFT5:
15205 case BFD_RELOC_MIPS_SHIFT6:
15206 case BFD_RELOC_MIPS_GOT_DISP:
15207 case BFD_RELOC_MIPS_GOT_PAGE:
15208 case BFD_RELOC_MIPS_GOT_OFST:
15209 case BFD_RELOC_MIPS_SUB:
15210 case BFD_RELOC_MIPS_INSERT_A:
15211 case BFD_RELOC_MIPS_INSERT_B:
15212 case BFD_RELOC_MIPS_DELETE:
15213 case BFD_RELOC_MIPS_HIGHEST:
15214 case BFD_RELOC_MIPS_HIGHER:
15215 case BFD_RELOC_MIPS_SCN_DISP:
15216 case BFD_RELOC_MIPS_REL16:
15217 case BFD_RELOC_MIPS_RELGOT:
15218 case BFD_RELOC_MIPS_JALR:
15219 case BFD_RELOC_HI16:
15220 case BFD_RELOC_HI16_S:
15221 case BFD_RELOC_LO16:
15222 case BFD_RELOC_GPREL16:
15223 case BFD_RELOC_MIPS_LITERAL:
15224 case BFD_RELOC_MIPS_CALL16:
15225 case BFD_RELOC_MIPS_GOT16:
15226 case BFD_RELOC_GPREL32:
15227 case BFD_RELOC_MIPS_GOT_HI16:
15228 case BFD_RELOC_MIPS_GOT_LO16:
15229 case BFD_RELOC_MIPS_CALL_HI16:
15230 case BFD_RELOC_MIPS_CALL_LO16:
15231 case BFD_RELOC_HI16_S_PCREL:
15232 case BFD_RELOC_LO16_PCREL:
15233 case BFD_RELOC_MIPS16_GPREL:
15234 case BFD_RELOC_MIPS16_GOT16:
15235 case BFD_RELOC_MIPS16_CALL16:
15236 case BFD_RELOC_MIPS16_HI16:
15237 case BFD_RELOC_MIPS16_HI16_S:
15238 case BFD_RELOC_MIPS16_LO16:
15239 case BFD_RELOC_MICROMIPS_GOT_DISP:
15240 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15241 case BFD_RELOC_MICROMIPS_GOT_OFST:
15242 case BFD_RELOC_MICROMIPS_SUB:
15243 case BFD_RELOC_MICROMIPS_HIGHEST:
15244 case BFD_RELOC_MICROMIPS_HIGHER:
15245 case BFD_RELOC_MICROMIPS_SCN_DISP:
15246 case BFD_RELOC_MICROMIPS_JALR:
15247 case BFD_RELOC_MICROMIPS_HI16:
15248 case BFD_RELOC_MICROMIPS_HI16_S:
15249 case BFD_RELOC_MICROMIPS_LO16:
15250 case BFD_RELOC_MICROMIPS_GPREL16:
15251 case BFD_RELOC_MICROMIPS_LITERAL:
15252 case BFD_RELOC_MICROMIPS_CALL16:
15253 case BFD_RELOC_MICROMIPS_GOT16:
15254 case BFD_RELOC_MICROMIPS_GOT_HI16:
15255 case BFD_RELOC_MICROMIPS_GOT_LO16:
15256 case BFD_RELOC_MICROMIPS_CALL_HI16:
15257 case BFD_RELOC_MICROMIPS_CALL_LO16:
15258 case BFD_RELOC_MIPS_EH:
15259 if (fixP->fx_done)
15260 {
15261 offsetT value;
15262
15263 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15264 {
15265 insn = read_reloc_insn (buf, fixP->fx_r_type);
15266 if (mips16_reloc_p (fixP->fx_r_type))
15267 insn |= mips16_immed_extend (value, 16);
15268 else
15269 insn |= (value & 0xffff);
15270 write_reloc_insn (buf, fixP->fx_r_type, insn);
15271 }
15272 else
15273 as_bad_where (fixP->fx_file, fixP->fx_line,
15274 _("unsupported constant in relocation"));
15275 }
15276 break;
15277
15278 case BFD_RELOC_64:
15279 /* This is handled like BFD_RELOC_32, but we output a sign
15280 extended value if we are only 32 bits. */
15281 if (fixP->fx_done)
15282 {
15283 if (8 <= sizeof (valueT))
15284 md_number_to_chars (buf, *valP, 8);
15285 else
15286 {
15287 valueT hiv;
15288
15289 if ((*valP & 0x80000000) != 0)
15290 hiv = 0xffffffff;
15291 else
15292 hiv = 0;
15293 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15294 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
15295 }
15296 }
15297 break;
15298
15299 case BFD_RELOC_RVA:
15300 case BFD_RELOC_32:
15301 case BFD_RELOC_32_PCREL:
15302 case BFD_RELOC_16:
15303 case BFD_RELOC_8:
15304 /* If we are deleting this reloc entry, we must fill in the
15305 value now. This can happen if we have a .word which is not
15306 resolved when it appears but is later defined. */
15307 if (fixP->fx_done)
15308 md_number_to_chars (buf, *valP, fixP->fx_size);
15309 break;
15310
15311 case BFD_RELOC_MIPS_21_PCREL_S2:
15312 fix_validate_branch (fixP, *valP);
15313 if (!fixP->fx_done)
15314 break;
15315
15316 if (*valP + 0x400000 <= 0x7fffff)
15317 {
15318 insn = read_insn (buf);
15319 insn |= (*valP >> 2) & 0x1fffff;
15320 write_insn (buf, insn);
15321 }
15322 else
15323 as_bad_where (fixP->fx_file, fixP->fx_line,
15324 _("branch out of range"));
15325 break;
15326
15327 case BFD_RELOC_MIPS_26_PCREL_S2:
15328 fix_validate_branch (fixP, *valP);
15329 if (!fixP->fx_done)
15330 break;
15331
15332 if (*valP + 0x8000000 <= 0xfffffff)
15333 {
15334 insn = read_insn (buf);
15335 insn |= (*valP >> 2) & 0x3ffffff;
15336 write_insn (buf, insn);
15337 }
15338 else
15339 as_bad_where (fixP->fx_file, fixP->fx_line,
15340 _("branch out of range"));
15341 break;
15342
15343 case BFD_RELOC_MIPS_18_PCREL_S3:
15344 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
15345 as_bad_where (fixP->fx_file, fixP->fx_line,
15346 _("PC-relative access using misaligned symbol (%lx)"),
15347 (long) S_GET_VALUE (fixP->fx_addsy));
15348 if ((fixP->fx_offset & 0x7) != 0)
15349 as_bad_where (fixP->fx_file, fixP->fx_line,
15350 _("PC-relative access using misaligned offset (%lx)"),
15351 (long) fixP->fx_offset);
15352 if (!fixP->fx_done)
15353 break;
15354
15355 if (*valP + 0x100000 <= 0x1fffff)
15356 {
15357 insn = read_insn (buf);
15358 insn |= (*valP >> 3) & 0x3ffff;
15359 write_insn (buf, insn);
15360 }
15361 else
15362 as_bad_where (fixP->fx_file, fixP->fx_line,
15363 _("PC-relative access out of range"));
15364 break;
15365
15366 case BFD_RELOC_MIPS_19_PCREL_S2:
15367 if ((*valP & 0x3) != 0)
15368 as_bad_where (fixP->fx_file, fixP->fx_line,
15369 _("PC-relative access to misaligned address (%lx)"),
15370 (long) *valP);
15371 if (!fixP->fx_done)
15372 break;
15373
15374 if (*valP + 0x100000 <= 0x1fffff)
15375 {
15376 insn = read_insn (buf);
15377 insn |= (*valP >> 2) & 0x7ffff;
15378 write_insn (buf, insn);
15379 }
15380 else
15381 as_bad_where (fixP->fx_file, fixP->fx_line,
15382 _("PC-relative access out of range"));
15383 break;
15384
15385 case BFD_RELOC_16_PCREL_S2:
15386 fix_validate_branch (fixP, *valP);
15387
15388 /* We need to save the bits in the instruction since fixup_segment()
15389 might be deleting the relocation entry (i.e., a branch within
15390 the current segment). */
15391 if (! fixP->fx_done)
15392 break;
15393
15394 /* Update old instruction data. */
15395 insn = read_insn (buf);
15396
15397 if (*valP + 0x20000 <= 0x3ffff)
15398 {
15399 insn |= (*valP >> 2) & 0xffff;
15400 write_insn (buf, insn);
15401 }
15402 else if (mips_pic == NO_PIC
15403 && fixP->fx_done
15404 && fixP->fx_frag->fr_address >= text_section->vma
15405 && (fixP->fx_frag->fr_address
15406 < text_section->vma + bfd_get_section_size (text_section))
15407 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15408 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15409 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15410 {
15411 /* The branch offset is too large. If this is an
15412 unconditional branch, and we are not generating PIC code,
15413 we can convert it to an absolute jump instruction. */
15414 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15415 insn = 0x0c000000; /* jal */
15416 else
15417 insn = 0x08000000; /* j */
15418 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15419 fixP->fx_done = 0;
15420 fixP->fx_addsy = section_symbol (text_section);
15421 *valP += md_pcrel_from (fixP);
15422 write_insn (buf, insn);
15423 }
15424 else
15425 {
15426 /* If we got here, we have branch-relaxation disabled,
15427 and there's nothing we can do to fix this instruction
15428 without turning it into a longer sequence. */
15429 as_bad_where (fixP->fx_file, fixP->fx_line,
15430 _("branch out of range"));
15431 }
15432 break;
15433
15434 case BFD_RELOC_MIPS16_16_PCREL_S1:
15435 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15436 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15437 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15438 gas_assert (!fixP->fx_done);
15439 if (fix_bad_cross_mode_branch_p (fixP))
15440 as_bad_where (fixP->fx_file, fixP->fx_line,
15441 _("branch to a symbol in another ISA mode"));
15442 else if (fixP->fx_addsy
15443 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15444 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15445 && (fixP->fx_offset & 0x1) != 0)
15446 as_bad_where (fixP->fx_file, fixP->fx_line,
15447 _("branch to misaligned address (0x%lx)"),
15448 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15449 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15450 as_bad_where (fixP->fx_file, fixP->fx_line,
15451 _("cannot encode misaligned addend "
15452 "in the relocatable field (0x%lx)"),
15453 (long) fixP->fx_offset);
15454 break;
15455
15456 case BFD_RELOC_VTABLE_INHERIT:
15457 fixP->fx_done = 0;
15458 if (fixP->fx_addsy
15459 && !S_IS_DEFINED (fixP->fx_addsy)
15460 && !S_IS_WEAK (fixP->fx_addsy))
15461 S_SET_WEAK (fixP->fx_addsy);
15462 break;
15463
15464 case BFD_RELOC_NONE:
15465 case BFD_RELOC_VTABLE_ENTRY:
15466 fixP->fx_done = 0;
15467 break;
15468
15469 default:
15470 abort ();
15471 }
15472
15473 /* Remember value for tc_gen_reloc. */
15474 fixP->fx_addnumber = *valP;
15475 }
15476
15477 static symbolS *
15478 get_symbol (void)
15479 {
15480 int c;
15481 char *name;
15482 symbolS *p;
15483
15484 c = get_symbol_name (&name);
15485 p = (symbolS *) symbol_find_or_make (name);
15486 (void) restore_line_pointer (c);
15487 return p;
15488 }
15489
15490 /* Align the current frag to a given power of two. If a particular
15491 fill byte should be used, FILL points to an integer that contains
15492 that byte, otherwise FILL is null.
15493
15494 This function used to have the comment:
15495
15496 The MIPS assembler also automatically adjusts any preceding label.
15497
15498 The implementation therefore applied the adjustment to a maximum of
15499 one label. However, other label adjustments are applied to batches
15500 of labels, and adjusting just one caused problems when new labels
15501 were added for the sake of debugging or unwind information.
15502 We therefore adjust all preceding labels (given as LABELS) instead. */
15503
15504 static void
15505 mips_align (int to, int *fill, struct insn_label_list *labels)
15506 {
15507 mips_emit_delays ();
15508 mips_record_compressed_mode ();
15509 if (fill == NULL && subseg_text_p (now_seg))
15510 frag_align_code (to, 0);
15511 else
15512 frag_align (to, fill ? *fill : 0, 0);
15513 record_alignment (now_seg, to);
15514 mips_move_labels (labels, FALSE);
15515 }
15516
15517 /* Align to a given power of two. .align 0 turns off the automatic
15518 alignment used by the data creating pseudo-ops. */
15519
15520 static void
15521 s_align (int x ATTRIBUTE_UNUSED)
15522 {
15523 int temp, fill_value, *fill_ptr;
15524 long max_alignment = 28;
15525
15526 /* o Note that the assembler pulls down any immediately preceding label
15527 to the aligned address.
15528 o It's not documented but auto alignment is reinstated by
15529 a .align pseudo instruction.
15530 o Note also that after auto alignment is turned off the mips assembler
15531 issues an error on attempt to assemble an improperly aligned data item.
15532 We don't. */
15533
15534 temp = get_absolute_expression ();
15535 if (temp > max_alignment)
15536 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
15537 else if (temp < 0)
15538 {
15539 as_warn (_("alignment negative, 0 assumed"));
15540 temp = 0;
15541 }
15542 if (*input_line_pointer == ',')
15543 {
15544 ++input_line_pointer;
15545 fill_value = get_absolute_expression ();
15546 fill_ptr = &fill_value;
15547 }
15548 else
15549 fill_ptr = 0;
15550 if (temp)
15551 {
15552 segment_info_type *si = seg_info (now_seg);
15553 struct insn_label_list *l = si->label_list;
15554 /* Auto alignment should be switched on by next section change. */
15555 auto_align = 1;
15556 mips_align (temp, fill_ptr, l);
15557 }
15558 else
15559 {
15560 auto_align = 0;
15561 }
15562
15563 demand_empty_rest_of_line ();
15564 }
15565
15566 static void
15567 s_change_sec (int sec)
15568 {
15569 segT seg;
15570
15571 /* The ELF backend needs to know that we are changing sections, so
15572 that .previous works correctly. We could do something like check
15573 for an obj_section_change_hook macro, but that might be confusing
15574 as it would not be appropriate to use it in the section changing
15575 functions in read.c, since obj-elf.c intercepts those. FIXME:
15576 This should be cleaner, somehow. */
15577 obj_elf_section_change_hook ();
15578
15579 mips_emit_delays ();
15580
15581 switch (sec)
15582 {
15583 case 't':
15584 s_text (0);
15585 break;
15586 case 'd':
15587 s_data (0);
15588 break;
15589 case 'b':
15590 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15591 demand_empty_rest_of_line ();
15592 break;
15593
15594 case 'r':
15595 seg = subseg_new (RDATA_SECTION_NAME,
15596 (subsegT) get_absolute_expression ());
15597 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15598 | SEC_READONLY | SEC_RELOC
15599 | SEC_DATA));
15600 if (strncmp (TARGET_OS, "elf", 3) != 0)
15601 record_alignment (seg, 4);
15602 demand_empty_rest_of_line ();
15603 break;
15604
15605 case 's':
15606 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
15607 bfd_set_section_flags (stdoutput, seg,
15608 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
15609 if (strncmp (TARGET_OS, "elf", 3) != 0)
15610 record_alignment (seg, 4);
15611 demand_empty_rest_of_line ();
15612 break;
15613
15614 case 'B':
15615 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
15616 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15617 if (strncmp (TARGET_OS, "elf", 3) != 0)
15618 record_alignment (seg, 4);
15619 demand_empty_rest_of_line ();
15620 break;
15621 }
15622
15623 auto_align = 1;
15624 }
15625
15626 void
15627 s_change_section (int ignore ATTRIBUTE_UNUSED)
15628 {
15629 char *saved_ilp;
15630 char *section_name;
15631 char c, endc;
15632 char next_c = 0;
15633 int section_type;
15634 int section_flag;
15635 int section_entry_size;
15636 int section_alignment;
15637
15638 saved_ilp = input_line_pointer;
15639 endc = get_symbol_name (&section_name);
15640 c = (endc == '"' ? input_line_pointer[1] : endc);
15641 if (c)
15642 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
15643
15644 /* Do we have .section Name<,"flags">? */
15645 if (c != ',' || (c == ',' && next_c == '"'))
15646 {
15647 /* Just after name is now '\0'. */
15648 (void) restore_line_pointer (endc);
15649 input_line_pointer = saved_ilp;
15650 obj_elf_section (ignore);
15651 return;
15652 }
15653
15654 section_name = xstrdup (section_name);
15655 c = restore_line_pointer (endc);
15656
15657 input_line_pointer++;
15658
15659 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
15660 if (c == ',')
15661 section_type = get_absolute_expression ();
15662 else
15663 section_type = 0;
15664
15665 if (*input_line_pointer++ == ',')
15666 section_flag = get_absolute_expression ();
15667 else
15668 section_flag = 0;
15669
15670 if (*input_line_pointer++ == ',')
15671 section_entry_size = get_absolute_expression ();
15672 else
15673 section_entry_size = 0;
15674
15675 if (*input_line_pointer++ == ',')
15676 section_alignment = get_absolute_expression ();
15677 else
15678 section_alignment = 0;
15679
15680 /* FIXME: really ignore? */
15681 (void) section_alignment;
15682
15683 /* When using the generic form of .section (as implemented by obj-elf.c),
15684 there's no way to set the section type to SHT_MIPS_DWARF. Users have
15685 traditionally had to fall back on the more common @progbits instead.
15686
15687 There's nothing really harmful in this, since bfd will correct
15688 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
15689 means that, for backwards compatibility, the special_section entries
15690 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
15691
15692 Even so, we shouldn't force users of the MIPS .section syntax to
15693 incorrectly label the sections as SHT_PROGBITS. The best compromise
15694 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
15695 generic type-checking code. */
15696 if (section_type == SHT_MIPS_DWARF)
15697 section_type = SHT_PROGBITS;
15698
15699 obj_elf_change_section (section_name, section_type, section_flag,
15700 section_entry_size, 0, 0, 0);
15701
15702 if (now_seg->name != section_name)
15703 free (section_name);
15704 }
15705
15706 void
15707 mips_enable_auto_align (void)
15708 {
15709 auto_align = 1;
15710 }
15711
15712 static void
15713 s_cons (int log_size)
15714 {
15715 segment_info_type *si = seg_info (now_seg);
15716 struct insn_label_list *l = si->label_list;
15717
15718 mips_emit_delays ();
15719 if (log_size > 0 && auto_align)
15720 mips_align (log_size, 0, l);
15721 cons (1 << log_size);
15722 mips_clear_insn_labels ();
15723 }
15724
15725 static void
15726 s_float_cons (int type)
15727 {
15728 segment_info_type *si = seg_info (now_seg);
15729 struct insn_label_list *l = si->label_list;
15730
15731 mips_emit_delays ();
15732
15733 if (auto_align)
15734 {
15735 if (type == 'd')
15736 mips_align (3, 0, l);
15737 else
15738 mips_align (2, 0, l);
15739 }
15740
15741 float_cons (type);
15742 mips_clear_insn_labels ();
15743 }
15744
15745 /* Handle .globl. We need to override it because on Irix 5 you are
15746 permitted to say
15747 .globl foo .text
15748 where foo is an undefined symbol, to mean that foo should be
15749 considered to be the address of a function. */
15750
15751 static void
15752 s_mips_globl (int x ATTRIBUTE_UNUSED)
15753 {
15754 char *name;
15755 int c;
15756 symbolS *symbolP;
15757 flagword flag;
15758
15759 do
15760 {
15761 c = get_symbol_name (&name);
15762 symbolP = symbol_find_or_make (name);
15763 S_SET_EXTERNAL (symbolP);
15764
15765 *input_line_pointer = c;
15766 SKIP_WHITESPACE_AFTER_NAME ();
15767
15768 /* On Irix 5, every global symbol that is not explicitly labelled as
15769 being a function is apparently labelled as being an object. */
15770 flag = BSF_OBJECT;
15771
15772 if (!is_end_of_line[(unsigned char) *input_line_pointer]
15773 && (*input_line_pointer != ','))
15774 {
15775 char *secname;
15776 asection *sec;
15777
15778 c = get_symbol_name (&secname);
15779 sec = bfd_get_section_by_name (stdoutput, secname);
15780 if (sec == NULL)
15781 as_bad (_("%s: no such section"), secname);
15782 (void) restore_line_pointer (c);
15783
15784 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
15785 flag = BSF_FUNCTION;
15786 }
15787
15788 symbol_get_bfdsym (symbolP)->flags |= flag;
15789
15790 c = *input_line_pointer;
15791 if (c == ',')
15792 {
15793 input_line_pointer++;
15794 SKIP_WHITESPACE ();
15795 if (is_end_of_line[(unsigned char) *input_line_pointer])
15796 c = '\n';
15797 }
15798 }
15799 while (c == ',');
15800
15801 demand_empty_rest_of_line ();
15802 }
15803
15804 static void
15805 s_option (int x ATTRIBUTE_UNUSED)
15806 {
15807 char *opt;
15808 char c;
15809
15810 c = get_symbol_name (&opt);
15811
15812 if (*opt == 'O')
15813 {
15814 /* FIXME: What does this mean? */
15815 }
15816 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
15817 {
15818 int i;
15819
15820 i = atoi (opt + 3);
15821 if (i != 0 && i != 2)
15822 as_bad (_(".option pic%d not supported"), i);
15823 else if (mips_pic == VXWORKS_PIC)
15824 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
15825 else if (i == 0)
15826 mips_pic = NO_PIC;
15827 else if (i == 2)
15828 {
15829 mips_pic = SVR4_PIC;
15830 mips_abicalls = TRUE;
15831 }
15832
15833 if (mips_pic == SVR4_PIC)
15834 {
15835 if (g_switch_seen && g_switch_value != 0)
15836 as_warn (_("-G may not be used with SVR4 PIC code"));
15837 g_switch_value = 0;
15838 bfd_set_gp_size (stdoutput, 0);
15839 }
15840 }
15841 else
15842 as_warn (_("unrecognized option \"%s\""), opt);
15843
15844 (void) restore_line_pointer (c);
15845 demand_empty_rest_of_line ();
15846 }
15847
15848 /* This structure is used to hold a stack of .set values. */
15849
15850 struct mips_option_stack
15851 {
15852 struct mips_option_stack *next;
15853 struct mips_set_options options;
15854 };
15855
15856 static struct mips_option_stack *mips_opts_stack;
15857
15858 /* Return status for .set/.module option handling. */
15859
15860 enum code_option_type
15861 {
15862 /* Unrecognized option. */
15863 OPTION_TYPE_BAD = -1,
15864
15865 /* Ordinary option. */
15866 OPTION_TYPE_NORMAL,
15867
15868 /* ISA changing option. */
15869 OPTION_TYPE_ISA
15870 };
15871
15872 /* Handle common .set/.module options. Return status indicating option
15873 type. */
15874
15875 static enum code_option_type
15876 parse_code_option (char * name)
15877 {
15878 bfd_boolean isa_set = FALSE;
15879 const struct mips_ase *ase;
15880
15881 if (strncmp (name, "at=", 3) == 0)
15882 {
15883 char *s = name + 3;
15884
15885 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
15886 as_bad (_("unrecognized register name `%s'"), s);
15887 }
15888 else if (strcmp (name, "at") == 0)
15889 mips_opts.at = ATREG;
15890 else if (strcmp (name, "noat") == 0)
15891 mips_opts.at = ZERO;
15892 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
15893 mips_opts.nomove = 0;
15894 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
15895 mips_opts.nomove = 1;
15896 else if (strcmp (name, "bopt") == 0)
15897 mips_opts.nobopt = 0;
15898 else if (strcmp (name, "nobopt") == 0)
15899 mips_opts.nobopt = 1;
15900 else if (strcmp (name, "gp=32") == 0)
15901 mips_opts.gp = 32;
15902 else if (strcmp (name, "gp=64") == 0)
15903 mips_opts.gp = 64;
15904 else if (strcmp (name, "fp=32") == 0)
15905 mips_opts.fp = 32;
15906 else if (strcmp (name, "fp=xx") == 0)
15907 mips_opts.fp = 0;
15908 else if (strcmp (name, "fp=64") == 0)
15909 mips_opts.fp = 64;
15910 else if (strcmp (name, "softfloat") == 0)
15911 mips_opts.soft_float = 1;
15912 else if (strcmp (name, "hardfloat") == 0)
15913 mips_opts.soft_float = 0;
15914 else if (strcmp (name, "singlefloat") == 0)
15915 mips_opts.single_float = 1;
15916 else if (strcmp (name, "doublefloat") == 0)
15917 mips_opts.single_float = 0;
15918 else if (strcmp (name, "nooddspreg") == 0)
15919 mips_opts.oddspreg = 0;
15920 else if (strcmp (name, "oddspreg") == 0)
15921 mips_opts.oddspreg = 1;
15922 else if (strcmp (name, "mips16") == 0
15923 || strcmp (name, "MIPS-16") == 0)
15924 mips_opts.mips16 = 1;
15925 else if (strcmp (name, "nomips16") == 0
15926 || strcmp (name, "noMIPS-16") == 0)
15927 mips_opts.mips16 = 0;
15928 else if (strcmp (name, "micromips") == 0)
15929 mips_opts.micromips = 1;
15930 else if (strcmp (name, "nomicromips") == 0)
15931 mips_opts.micromips = 0;
15932 else if (name[0] == 'n'
15933 && name[1] == 'o'
15934 && (ase = mips_lookup_ase (name + 2)))
15935 mips_set_ase (ase, &mips_opts, FALSE);
15936 else if ((ase = mips_lookup_ase (name)))
15937 mips_set_ase (ase, &mips_opts, TRUE);
15938 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
15939 {
15940 /* Permit the user to change the ISA and architecture on the fly.
15941 Needless to say, misuse can cause serious problems. */
15942 if (strncmp (name, "arch=", 5) == 0)
15943 {
15944 const struct mips_cpu_info *p;
15945
15946 p = mips_parse_cpu ("internal use", name + 5);
15947 if (!p)
15948 as_bad (_("unknown architecture %s"), name + 5);
15949 else
15950 {
15951 mips_opts.arch = p->cpu;
15952 mips_opts.isa = p->isa;
15953 isa_set = TRUE;
15954 }
15955 }
15956 else if (strncmp (name, "mips", 4) == 0)
15957 {
15958 const struct mips_cpu_info *p;
15959
15960 p = mips_parse_cpu ("internal use", name);
15961 if (!p)
15962 as_bad (_("unknown ISA level %s"), name + 4);
15963 else
15964 {
15965 mips_opts.arch = p->cpu;
15966 mips_opts.isa = p->isa;
15967 isa_set = TRUE;
15968 }
15969 }
15970 else
15971 as_bad (_("unknown ISA or architecture %s"), name);
15972 }
15973 else if (strcmp (name, "autoextend") == 0)
15974 mips_opts.noautoextend = 0;
15975 else if (strcmp (name, "noautoextend") == 0)
15976 mips_opts.noautoextend = 1;
15977 else if (strcmp (name, "insn32") == 0)
15978 mips_opts.insn32 = TRUE;
15979 else if (strcmp (name, "noinsn32") == 0)
15980 mips_opts.insn32 = FALSE;
15981 else if (strcmp (name, "sym32") == 0)
15982 mips_opts.sym32 = TRUE;
15983 else if (strcmp (name, "nosym32") == 0)
15984 mips_opts.sym32 = FALSE;
15985 else
15986 return OPTION_TYPE_BAD;
15987
15988 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
15989 }
15990
15991 /* Handle the .set pseudo-op. */
15992
15993 static void
15994 s_mipsset (int x ATTRIBUTE_UNUSED)
15995 {
15996 enum code_option_type type = OPTION_TYPE_NORMAL;
15997 char *name = input_line_pointer, ch;
15998
15999 file_mips_check_options ();
16000
16001 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16002 ++input_line_pointer;
16003 ch = *input_line_pointer;
16004 *input_line_pointer = '\0';
16005
16006 if (strchr (name, ','))
16007 {
16008 /* Generic ".set" directive; use the generic handler. */
16009 *input_line_pointer = ch;
16010 input_line_pointer = name;
16011 s_set (0);
16012 return;
16013 }
16014
16015 if (strcmp (name, "reorder") == 0)
16016 {
16017 if (mips_opts.noreorder)
16018 end_noreorder ();
16019 }
16020 else if (strcmp (name, "noreorder") == 0)
16021 {
16022 if (!mips_opts.noreorder)
16023 start_noreorder ();
16024 }
16025 else if (strcmp (name, "macro") == 0)
16026 mips_opts.warn_about_macros = 0;
16027 else if (strcmp (name, "nomacro") == 0)
16028 {
16029 if (mips_opts.noreorder == 0)
16030 as_bad (_("`noreorder' must be set before `nomacro'"));
16031 mips_opts.warn_about_macros = 1;
16032 }
16033 else if (strcmp (name, "gp=default") == 0)
16034 mips_opts.gp = file_mips_opts.gp;
16035 else if (strcmp (name, "fp=default") == 0)
16036 mips_opts.fp = file_mips_opts.fp;
16037 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16038 {
16039 mips_opts.isa = file_mips_opts.isa;
16040 mips_opts.arch = file_mips_opts.arch;
16041 mips_opts.gp = file_mips_opts.gp;
16042 mips_opts.fp = file_mips_opts.fp;
16043 }
16044 else if (strcmp (name, "push") == 0)
16045 {
16046 struct mips_option_stack *s;
16047
16048 s = XNEW (struct mips_option_stack);
16049 s->next = mips_opts_stack;
16050 s->options = mips_opts;
16051 mips_opts_stack = s;
16052 }
16053 else if (strcmp (name, "pop") == 0)
16054 {
16055 struct mips_option_stack *s;
16056
16057 s = mips_opts_stack;
16058 if (s == NULL)
16059 as_bad (_(".set pop with no .set push"));
16060 else
16061 {
16062 /* If we're changing the reorder mode we need to handle
16063 delay slots correctly. */
16064 if (s->options.noreorder && ! mips_opts.noreorder)
16065 start_noreorder ();
16066 else if (! s->options.noreorder && mips_opts.noreorder)
16067 end_noreorder ();
16068
16069 mips_opts = s->options;
16070 mips_opts_stack = s->next;
16071 free (s);
16072 }
16073 }
16074 else
16075 {
16076 type = parse_code_option (name);
16077 if (type == OPTION_TYPE_BAD)
16078 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16079 }
16080
16081 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16082 registers based on what is supported by the arch/cpu. */
16083 if (type == OPTION_TYPE_ISA)
16084 {
16085 switch (mips_opts.isa)
16086 {
16087 case 0:
16088 break;
16089 case ISA_MIPS1:
16090 /* MIPS I cannot support FPXX. */
16091 mips_opts.fp = 32;
16092 /* fall-through. */
16093 case ISA_MIPS2:
16094 case ISA_MIPS32:
16095 case ISA_MIPS32R2:
16096 case ISA_MIPS32R3:
16097 case ISA_MIPS32R5:
16098 mips_opts.gp = 32;
16099 if (mips_opts.fp != 0)
16100 mips_opts.fp = 32;
16101 break;
16102 case ISA_MIPS32R6:
16103 mips_opts.gp = 32;
16104 mips_opts.fp = 64;
16105 break;
16106 case ISA_MIPS3:
16107 case ISA_MIPS4:
16108 case ISA_MIPS5:
16109 case ISA_MIPS64:
16110 case ISA_MIPS64R2:
16111 case ISA_MIPS64R3:
16112 case ISA_MIPS64R5:
16113 case ISA_MIPS64R6:
16114 mips_opts.gp = 64;
16115 if (mips_opts.fp != 0)
16116 {
16117 if (mips_opts.arch == CPU_R5900)
16118 mips_opts.fp = 32;
16119 else
16120 mips_opts.fp = 64;
16121 }
16122 break;
16123 default:
16124 as_bad (_("unknown ISA level %s"), name + 4);
16125 break;
16126 }
16127 }
16128
16129 mips_check_options (&mips_opts, FALSE);
16130
16131 mips_check_isa_supports_ases ();
16132 *input_line_pointer = ch;
16133 demand_empty_rest_of_line ();
16134 }
16135
16136 /* Handle the .module pseudo-op. */
16137
16138 static void
16139 s_module (int ignore ATTRIBUTE_UNUSED)
16140 {
16141 char *name = input_line_pointer, ch;
16142
16143 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16144 ++input_line_pointer;
16145 ch = *input_line_pointer;
16146 *input_line_pointer = '\0';
16147
16148 if (!file_mips_opts_checked)
16149 {
16150 if (parse_code_option (name) == OPTION_TYPE_BAD)
16151 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16152
16153 /* Update module level settings from mips_opts. */
16154 file_mips_opts = mips_opts;
16155 }
16156 else
16157 as_bad (_(".module is not permitted after generating code"));
16158
16159 *input_line_pointer = ch;
16160 demand_empty_rest_of_line ();
16161 }
16162
16163 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
16164 .option pic2. It means to generate SVR4 PIC calls. */
16165
16166 static void
16167 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16168 {
16169 mips_pic = SVR4_PIC;
16170 mips_abicalls = TRUE;
16171
16172 if (g_switch_seen && g_switch_value != 0)
16173 as_warn (_("-G may not be used with SVR4 PIC code"));
16174 g_switch_value = 0;
16175
16176 bfd_set_gp_size (stdoutput, 0);
16177 demand_empty_rest_of_line ();
16178 }
16179
16180 /* Handle the .cpload pseudo-op. This is used when generating SVR4
16181 PIC code. It sets the $gp register for the function based on the
16182 function address, which is in the register named in the argument.
16183 This uses a relocation against _gp_disp, which is handled specially
16184 by the linker. The result is:
16185 lui $gp,%hi(_gp_disp)
16186 addiu $gp,$gp,%lo(_gp_disp)
16187 addu $gp,$gp,.cpload argument
16188 The .cpload argument is normally $25 == $t9.
16189
16190 The -mno-shared option changes this to:
16191 lui $gp,%hi(__gnu_local_gp)
16192 addiu $gp,$gp,%lo(__gnu_local_gp)
16193 and the argument is ignored. This saves an instruction, but the
16194 resulting code is not position independent; it uses an absolute
16195 address for __gnu_local_gp. Thus code assembled with -mno-shared
16196 can go into an ordinary executable, but not into a shared library. */
16197
16198 static void
16199 s_cpload (int ignore ATTRIBUTE_UNUSED)
16200 {
16201 expressionS ex;
16202 int reg;
16203 int in_shared;
16204
16205 file_mips_check_options ();
16206
16207 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16208 .cpload is ignored. */
16209 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16210 {
16211 s_ignore (0);
16212 return;
16213 }
16214
16215 if (mips_opts.mips16)
16216 {
16217 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16218 ignore_rest_of_line ();
16219 return;
16220 }
16221
16222 /* .cpload should be in a .set noreorder section. */
16223 if (mips_opts.noreorder == 0)
16224 as_warn (_(".cpload not in noreorder section"));
16225
16226 reg = tc_get_register (0);
16227
16228 /* If we need to produce a 64-bit address, we are better off using
16229 the default instruction sequence. */
16230 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16231
16232 ex.X_op = O_symbol;
16233 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16234 "__gnu_local_gp");
16235 ex.X_op_symbol = NULL;
16236 ex.X_add_number = 0;
16237
16238 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16239 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16240
16241 mips_mark_labels ();
16242 mips_assembling_insn = TRUE;
16243
16244 macro_start ();
16245 macro_build_lui (&ex, mips_gp_register);
16246 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16247 mips_gp_register, BFD_RELOC_LO16);
16248 if (in_shared)
16249 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16250 mips_gp_register, reg);
16251 macro_end ();
16252
16253 mips_assembling_insn = FALSE;
16254 demand_empty_rest_of_line ();
16255 }
16256
16257 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16258 .cpsetup $reg1, offset|$reg2, label
16259
16260 If offset is given, this results in:
16261 sd $gp, offset($sp)
16262 lui $gp, %hi(%neg(%gp_rel(label)))
16263 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16264 daddu $gp, $gp, $reg1
16265
16266 If $reg2 is given, this results in:
16267 or $reg2, $gp, $0
16268 lui $gp, %hi(%neg(%gp_rel(label)))
16269 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16270 daddu $gp, $gp, $reg1
16271 $reg1 is normally $25 == $t9.
16272
16273 The -mno-shared option replaces the last three instructions with
16274 lui $gp,%hi(_gp)
16275 addiu $gp,$gp,%lo(_gp) */
16276
16277 static void
16278 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16279 {
16280 expressionS ex_off;
16281 expressionS ex_sym;
16282 int reg1;
16283
16284 file_mips_check_options ();
16285
16286 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16287 We also need NewABI support. */
16288 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16289 {
16290 s_ignore (0);
16291 return;
16292 }
16293
16294 if (mips_opts.mips16)
16295 {
16296 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16297 ignore_rest_of_line ();
16298 return;
16299 }
16300
16301 reg1 = tc_get_register (0);
16302 SKIP_WHITESPACE ();
16303 if (*input_line_pointer != ',')
16304 {
16305 as_bad (_("missing argument separator ',' for .cpsetup"));
16306 return;
16307 }
16308 else
16309 ++input_line_pointer;
16310 SKIP_WHITESPACE ();
16311 if (*input_line_pointer == '$')
16312 {
16313 mips_cpreturn_register = tc_get_register (0);
16314 mips_cpreturn_offset = -1;
16315 }
16316 else
16317 {
16318 mips_cpreturn_offset = get_absolute_expression ();
16319 mips_cpreturn_register = -1;
16320 }
16321 SKIP_WHITESPACE ();
16322 if (*input_line_pointer != ',')
16323 {
16324 as_bad (_("missing argument separator ',' for .cpsetup"));
16325 return;
16326 }
16327 else
16328 ++input_line_pointer;
16329 SKIP_WHITESPACE ();
16330 expression (&ex_sym);
16331
16332 mips_mark_labels ();
16333 mips_assembling_insn = TRUE;
16334
16335 macro_start ();
16336 if (mips_cpreturn_register == -1)
16337 {
16338 ex_off.X_op = O_constant;
16339 ex_off.X_add_symbol = NULL;
16340 ex_off.X_op_symbol = NULL;
16341 ex_off.X_add_number = mips_cpreturn_offset;
16342
16343 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16344 BFD_RELOC_LO16, SP);
16345 }
16346 else
16347 move_register (mips_cpreturn_register, mips_gp_register);
16348
16349 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16350 {
16351 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16352 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16353 BFD_RELOC_HI16_S);
16354
16355 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16356 mips_gp_register, -1, BFD_RELOC_GPREL16,
16357 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16358
16359 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16360 mips_gp_register, reg1);
16361 }
16362 else
16363 {
16364 expressionS ex;
16365
16366 ex.X_op = O_symbol;
16367 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16368 ex.X_op_symbol = NULL;
16369 ex.X_add_number = 0;
16370
16371 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16372 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16373
16374 macro_build_lui (&ex, mips_gp_register);
16375 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16376 mips_gp_register, BFD_RELOC_LO16);
16377 }
16378
16379 macro_end ();
16380
16381 mips_assembling_insn = FALSE;
16382 demand_empty_rest_of_line ();
16383 }
16384
16385 static void
16386 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16387 {
16388 file_mips_check_options ();
16389
16390 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16391 .cplocal is ignored. */
16392 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16393 {
16394 s_ignore (0);
16395 return;
16396 }
16397
16398 if (mips_opts.mips16)
16399 {
16400 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16401 ignore_rest_of_line ();
16402 return;
16403 }
16404
16405 mips_gp_register = tc_get_register (0);
16406 demand_empty_rest_of_line ();
16407 }
16408
16409 /* Handle the .cprestore pseudo-op. This stores $gp into a given
16410 offset from $sp. The offset is remembered, and after making a PIC
16411 call $gp is restored from that location. */
16412
16413 static void
16414 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16415 {
16416 expressionS ex;
16417
16418 file_mips_check_options ();
16419
16420 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16421 .cprestore is ignored. */
16422 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16423 {
16424 s_ignore (0);
16425 return;
16426 }
16427
16428 if (mips_opts.mips16)
16429 {
16430 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16431 ignore_rest_of_line ();
16432 return;
16433 }
16434
16435 mips_cprestore_offset = get_absolute_expression ();
16436 mips_cprestore_valid = 1;
16437
16438 ex.X_op = O_constant;
16439 ex.X_add_symbol = NULL;
16440 ex.X_op_symbol = NULL;
16441 ex.X_add_number = mips_cprestore_offset;
16442
16443 mips_mark_labels ();
16444 mips_assembling_insn = TRUE;
16445
16446 macro_start ();
16447 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16448 SP, HAVE_64BIT_ADDRESSES);
16449 macro_end ();
16450
16451 mips_assembling_insn = FALSE;
16452 demand_empty_rest_of_line ();
16453 }
16454
16455 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16456 was given in the preceding .cpsetup, it results in:
16457 ld $gp, offset($sp)
16458
16459 If a register $reg2 was given there, it results in:
16460 or $gp, $reg2, $0 */
16461
16462 static void
16463 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16464 {
16465 expressionS ex;
16466
16467 file_mips_check_options ();
16468
16469 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16470 We also need NewABI support. */
16471 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16472 {
16473 s_ignore (0);
16474 return;
16475 }
16476
16477 if (mips_opts.mips16)
16478 {
16479 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16480 ignore_rest_of_line ();
16481 return;
16482 }
16483
16484 mips_mark_labels ();
16485 mips_assembling_insn = TRUE;
16486
16487 macro_start ();
16488 if (mips_cpreturn_register == -1)
16489 {
16490 ex.X_op = O_constant;
16491 ex.X_add_symbol = NULL;
16492 ex.X_op_symbol = NULL;
16493 ex.X_add_number = mips_cpreturn_offset;
16494
16495 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16496 }
16497 else
16498 move_register (mips_gp_register, mips_cpreturn_register);
16499
16500 macro_end ();
16501
16502 mips_assembling_insn = FALSE;
16503 demand_empty_rest_of_line ();
16504 }
16505
16506 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16507 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16508 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16509 debug information or MIPS16 TLS. */
16510
16511 static void
16512 s_tls_rel_directive (const size_t bytes, const char *dirstr,
16513 bfd_reloc_code_real_type rtype)
16514 {
16515 expressionS ex;
16516 char *p;
16517
16518 expression (&ex);
16519
16520 if (ex.X_op != O_symbol)
16521 {
16522 as_bad (_("unsupported use of %s"), dirstr);
16523 ignore_rest_of_line ();
16524 }
16525
16526 p = frag_more (bytes);
16527 md_number_to_chars (p, 0, bytes);
16528 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
16529 demand_empty_rest_of_line ();
16530 mips_clear_insn_labels ();
16531 }
16532
16533 /* Handle .dtprelword. */
16534
16535 static void
16536 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16537 {
16538 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
16539 }
16540
16541 /* Handle .dtpreldword. */
16542
16543 static void
16544 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16545 {
16546 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16547 }
16548
16549 /* Handle .tprelword. */
16550
16551 static void
16552 s_tprelword (int ignore ATTRIBUTE_UNUSED)
16553 {
16554 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16555 }
16556
16557 /* Handle .tpreldword. */
16558
16559 static void
16560 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16561 {
16562 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
16563 }
16564
16565 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16566 code. It sets the offset to use in gp_rel relocations. */
16567
16568 static void
16569 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
16570 {
16571 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16572 We also need NewABI support. */
16573 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16574 {
16575 s_ignore (0);
16576 return;
16577 }
16578
16579 mips_gprel_offset = get_absolute_expression ();
16580
16581 demand_empty_rest_of_line ();
16582 }
16583
16584 /* Handle the .gpword pseudo-op. This is used when generating PIC
16585 code. It generates a 32 bit GP relative reloc. */
16586
16587 static void
16588 s_gpword (int ignore ATTRIBUTE_UNUSED)
16589 {
16590 segment_info_type *si;
16591 struct insn_label_list *l;
16592 expressionS ex;
16593 char *p;
16594
16595 /* When not generating PIC code, this is treated as .word. */
16596 if (mips_pic != SVR4_PIC)
16597 {
16598 s_cons (2);
16599 return;
16600 }
16601
16602 si = seg_info (now_seg);
16603 l = si->label_list;
16604 mips_emit_delays ();
16605 if (auto_align)
16606 mips_align (2, 0, l);
16607
16608 expression (&ex);
16609 mips_clear_insn_labels ();
16610
16611 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16612 {
16613 as_bad (_("unsupported use of .gpword"));
16614 ignore_rest_of_line ();
16615 }
16616
16617 p = frag_more (4);
16618 md_number_to_chars (p, 0, 4);
16619 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16620 BFD_RELOC_GPREL32);
16621
16622 demand_empty_rest_of_line ();
16623 }
16624
16625 static void
16626 s_gpdword (int ignore ATTRIBUTE_UNUSED)
16627 {
16628 segment_info_type *si;
16629 struct insn_label_list *l;
16630 expressionS ex;
16631 char *p;
16632
16633 /* When not generating PIC code, this is treated as .dword. */
16634 if (mips_pic != SVR4_PIC)
16635 {
16636 s_cons (3);
16637 return;
16638 }
16639
16640 si = seg_info (now_seg);
16641 l = si->label_list;
16642 mips_emit_delays ();
16643 if (auto_align)
16644 mips_align (3, 0, l);
16645
16646 expression (&ex);
16647 mips_clear_insn_labels ();
16648
16649 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16650 {
16651 as_bad (_("unsupported use of .gpdword"));
16652 ignore_rest_of_line ();
16653 }
16654
16655 p = frag_more (8);
16656 md_number_to_chars (p, 0, 8);
16657 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16658 BFD_RELOC_GPREL32)->fx_tcbit = 1;
16659
16660 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
16661 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
16662 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
16663
16664 demand_empty_rest_of_line ();
16665 }
16666
16667 /* Handle the .ehword pseudo-op. This is used when generating unwinding
16668 tables. It generates a R_MIPS_EH reloc. */
16669
16670 static void
16671 s_ehword (int ignore ATTRIBUTE_UNUSED)
16672 {
16673 expressionS ex;
16674 char *p;
16675
16676 mips_emit_delays ();
16677
16678 expression (&ex);
16679 mips_clear_insn_labels ();
16680
16681 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16682 {
16683 as_bad (_("unsupported use of .ehword"));
16684 ignore_rest_of_line ();
16685 }
16686
16687 p = frag_more (4);
16688 md_number_to_chars (p, 0, 4);
16689 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16690 BFD_RELOC_32_PCREL);
16691
16692 demand_empty_rest_of_line ();
16693 }
16694
16695 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
16696 tables in SVR4 PIC code. */
16697
16698 static void
16699 s_cpadd (int ignore ATTRIBUTE_UNUSED)
16700 {
16701 int reg;
16702
16703 file_mips_check_options ();
16704
16705 /* This is ignored when not generating SVR4 PIC code. */
16706 if (mips_pic != SVR4_PIC)
16707 {
16708 s_ignore (0);
16709 return;
16710 }
16711
16712 mips_mark_labels ();
16713 mips_assembling_insn = TRUE;
16714
16715 /* Add $gp to the register named as an argument. */
16716 macro_start ();
16717 reg = tc_get_register (0);
16718 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
16719 macro_end ();
16720
16721 mips_assembling_insn = FALSE;
16722 demand_empty_rest_of_line ();
16723 }
16724
16725 /* Handle the .insn pseudo-op. This marks instruction labels in
16726 mips16/micromips mode. This permits the linker to handle them specially,
16727 such as generating jalx instructions when needed. We also make
16728 them odd for the duration of the assembly, in order to generate the
16729 right sort of code. We will make them even in the adjust_symtab
16730 routine, while leaving them marked. This is convenient for the
16731 debugger and the disassembler. The linker knows to make them odd
16732 again. */
16733
16734 static void
16735 s_insn (int ignore ATTRIBUTE_UNUSED)
16736 {
16737 file_mips_check_options ();
16738 file_ase_mips16 |= mips_opts.mips16;
16739 file_ase_micromips |= mips_opts.micromips;
16740
16741 mips_mark_labels ();
16742
16743 demand_empty_rest_of_line ();
16744 }
16745
16746 /* Handle the .nan pseudo-op. */
16747
16748 static void
16749 s_nan (int ignore ATTRIBUTE_UNUSED)
16750 {
16751 static const char str_legacy[] = "legacy";
16752 static const char str_2008[] = "2008";
16753 size_t i;
16754
16755 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
16756
16757 if (i == sizeof (str_2008) - 1
16758 && memcmp (input_line_pointer, str_2008, i) == 0)
16759 mips_nan2008 = 1;
16760 else if (i == sizeof (str_legacy) - 1
16761 && memcmp (input_line_pointer, str_legacy, i) == 0)
16762 {
16763 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
16764 mips_nan2008 = 0;
16765 else
16766 as_bad (_("`%s' does not support legacy NaN"),
16767 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
16768 }
16769 else
16770 as_bad (_("bad .nan directive"));
16771
16772 input_line_pointer += i;
16773 demand_empty_rest_of_line ();
16774 }
16775
16776 /* Handle a .stab[snd] directive. Ideally these directives would be
16777 implemented in a transparent way, so that removing them would not
16778 have any effect on the generated instructions. However, s_stab
16779 internally changes the section, so in practice we need to decide
16780 now whether the preceding label marks compressed code. We do not
16781 support changing the compression mode of a label after a .stab*
16782 directive, such as in:
16783
16784 foo:
16785 .stabs ...
16786 .set mips16
16787
16788 so the current mode wins. */
16789
16790 static void
16791 s_mips_stab (int type)
16792 {
16793 mips_mark_labels ();
16794 s_stab (type);
16795 }
16796
16797 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
16798
16799 static void
16800 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
16801 {
16802 char *name;
16803 int c;
16804 symbolS *symbolP;
16805 expressionS exp;
16806
16807 c = get_symbol_name (&name);
16808 symbolP = symbol_find_or_make (name);
16809 S_SET_WEAK (symbolP);
16810 *input_line_pointer = c;
16811
16812 SKIP_WHITESPACE_AFTER_NAME ();
16813
16814 if (! is_end_of_line[(unsigned char) *input_line_pointer])
16815 {
16816 if (S_IS_DEFINED (symbolP))
16817 {
16818 as_bad (_("ignoring attempt to redefine symbol %s"),
16819 S_GET_NAME (symbolP));
16820 ignore_rest_of_line ();
16821 return;
16822 }
16823
16824 if (*input_line_pointer == ',')
16825 {
16826 ++input_line_pointer;
16827 SKIP_WHITESPACE ();
16828 }
16829
16830 expression (&exp);
16831 if (exp.X_op != O_symbol)
16832 {
16833 as_bad (_("bad .weakext directive"));
16834 ignore_rest_of_line ();
16835 return;
16836 }
16837 symbol_set_value_expression (symbolP, &exp);
16838 }
16839
16840 demand_empty_rest_of_line ();
16841 }
16842
16843 /* Parse a register string into a number. Called from the ECOFF code
16844 to parse .frame. The argument is non-zero if this is the frame
16845 register, so that we can record it in mips_frame_reg. */
16846
16847 int
16848 tc_get_register (int frame)
16849 {
16850 unsigned int reg;
16851
16852 SKIP_WHITESPACE ();
16853 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
16854 reg = 0;
16855 if (frame)
16856 {
16857 mips_frame_reg = reg != 0 ? reg : SP;
16858 mips_frame_reg_valid = 1;
16859 mips_cprestore_valid = 0;
16860 }
16861 return reg;
16862 }
16863
16864 valueT
16865 md_section_align (asection *seg, valueT addr)
16866 {
16867 int align = bfd_get_section_alignment (stdoutput, seg);
16868
16869 /* We don't need to align ELF sections to the full alignment.
16870 However, Irix 5 may prefer that we align them at least to a 16
16871 byte boundary. We don't bother to align the sections if we
16872 are targeted for an embedded system. */
16873 if (strncmp (TARGET_OS, "elf", 3) == 0)
16874 return addr;
16875 if (align > 4)
16876 align = 4;
16877
16878 return ((addr + (1 << align) - 1) & -(1 << align));
16879 }
16880
16881 /* Utility routine, called from above as well. If called while the
16882 input file is still being read, it's only an approximation. (For
16883 example, a symbol may later become defined which appeared to be
16884 undefined earlier.) */
16885
16886 static int
16887 nopic_need_relax (symbolS *sym, int before_relaxing)
16888 {
16889 if (sym == 0)
16890 return 0;
16891
16892 if (g_switch_value > 0)
16893 {
16894 const char *symname;
16895 int change;
16896
16897 /* Find out whether this symbol can be referenced off the $gp
16898 register. It can be if it is smaller than the -G size or if
16899 it is in the .sdata or .sbss section. Certain symbols can
16900 not be referenced off the $gp, although it appears as though
16901 they can. */
16902 symname = S_GET_NAME (sym);
16903 if (symname != (const char *) NULL
16904 && (strcmp (symname, "eprol") == 0
16905 || strcmp (symname, "etext") == 0
16906 || strcmp (symname, "_gp") == 0
16907 || strcmp (symname, "edata") == 0
16908 || strcmp (symname, "_fbss") == 0
16909 || strcmp (symname, "_fdata") == 0
16910 || strcmp (symname, "_ftext") == 0
16911 || strcmp (symname, "end") == 0
16912 || strcmp (symname, "_gp_disp") == 0))
16913 change = 1;
16914 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
16915 && (0
16916 #ifndef NO_ECOFF_DEBUGGING
16917 || (symbol_get_obj (sym)->ecoff_extern_size != 0
16918 && (symbol_get_obj (sym)->ecoff_extern_size
16919 <= g_switch_value))
16920 #endif
16921 /* We must defer this decision until after the whole
16922 file has been read, since there might be a .extern
16923 after the first use of this symbol. */
16924 || (before_relaxing
16925 #ifndef NO_ECOFF_DEBUGGING
16926 && symbol_get_obj (sym)->ecoff_extern_size == 0
16927 #endif
16928 && S_GET_VALUE (sym) == 0)
16929 || (S_GET_VALUE (sym) != 0
16930 && S_GET_VALUE (sym) <= g_switch_value)))
16931 change = 0;
16932 else
16933 {
16934 const char *segname;
16935
16936 segname = segment_name (S_GET_SEGMENT (sym));
16937 gas_assert (strcmp (segname, ".lit8") != 0
16938 && strcmp (segname, ".lit4") != 0);
16939 change = (strcmp (segname, ".sdata") != 0
16940 && strcmp (segname, ".sbss") != 0
16941 && strncmp (segname, ".sdata.", 7) != 0
16942 && strncmp (segname, ".sbss.", 6) != 0
16943 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
16944 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
16945 }
16946 return change;
16947 }
16948 else
16949 /* We are not optimizing for the $gp register. */
16950 return 1;
16951 }
16952
16953
16954 /* Return true if the given symbol should be considered local for SVR4 PIC. */
16955
16956 static bfd_boolean
16957 pic_need_relax (symbolS *sym, asection *segtype)
16958 {
16959 asection *symsec;
16960
16961 /* Handle the case of a symbol equated to another symbol. */
16962 while (symbol_equated_reloc_p (sym))
16963 {
16964 symbolS *n;
16965
16966 /* It's possible to get a loop here in a badly written program. */
16967 n = symbol_get_value_expression (sym)->X_add_symbol;
16968 if (n == sym)
16969 break;
16970 sym = n;
16971 }
16972
16973 if (symbol_section_p (sym))
16974 return TRUE;
16975
16976 symsec = S_GET_SEGMENT (sym);
16977
16978 /* This must duplicate the test in adjust_reloc_syms. */
16979 return (!bfd_is_und_section (symsec)
16980 && !bfd_is_abs_section (symsec)
16981 && !bfd_is_com_section (symsec)
16982 && !s_is_linkonce (sym, segtype)
16983 /* A global or weak symbol is treated as external. */
16984 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
16985 }
16986
16987
16988 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
16989 extended opcode. SEC is the section the frag is in. */
16990
16991 static int
16992 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
16993 {
16994 int type;
16995 const struct mips_int_operand *operand;
16996 offsetT val;
16997 segT symsec;
16998 fragS *sym_frag;
16999
17000 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17001 return 0;
17002 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17003 return 1;
17004
17005 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17006 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17007 operand = mips16_immed_operand (type, FALSE);
17008 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17009 || (operand->root.type == OP_PCREL
17010 ? sec != symsec
17011 : !bfd_is_abs_section (symsec)))
17012 return 1;
17013
17014 sym_frag = symbol_get_frag (fragp->fr_symbol);
17015 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17016
17017 if (operand->root.type == OP_PCREL)
17018 {
17019 const struct mips_pcrel_operand *pcrel_op;
17020 addressT addr;
17021 offsetT maxtiny;
17022
17023 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17024 return 1;
17025
17026 pcrel_op = (const struct mips_pcrel_operand *) operand;
17027
17028 /* If the relax_marker of the symbol fragment differs from the
17029 relax_marker of this fragment, we have not yet adjusted the
17030 symbol fragment fr_address. We want to add in STRETCH in
17031 order to get a better estimate of the address. This
17032 particularly matters because of the shift bits. */
17033 if (stretch != 0
17034 && sym_frag->relax_marker != fragp->relax_marker)
17035 {
17036 fragS *f;
17037
17038 /* Adjust stretch for any alignment frag. Note that if have
17039 been expanding the earlier code, the symbol may be
17040 defined in what appears to be an earlier frag. FIXME:
17041 This doesn't handle the fr_subtype field, which specifies
17042 a maximum number of bytes to skip when doing an
17043 alignment. */
17044 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17045 {
17046 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17047 {
17048 if (stretch < 0)
17049 stretch = - ((- stretch)
17050 & ~ ((1 << (int) f->fr_offset) - 1));
17051 else
17052 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17053 if (stretch == 0)
17054 break;
17055 }
17056 }
17057 if (f != NULL)
17058 val += stretch;
17059 }
17060
17061 addr = fragp->fr_address + fragp->fr_fix;
17062
17063 /* The base address rules are complicated. The base address of
17064 a branch is the following instruction. The base address of a
17065 PC relative load or add is the instruction itself, but if it
17066 is in a delay slot (in which case it can not be extended) use
17067 the address of the instruction whose delay slot it is in. */
17068 if (pcrel_op->include_isa_bit)
17069 {
17070 addr += 2;
17071
17072 /* If we are currently assuming that this frag should be
17073 extended, then, the current address is two bytes
17074 higher. */
17075 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17076 addr += 2;
17077
17078 /* Ignore the low bit in the target, since it will be set
17079 for a text label. */
17080 val &= -2;
17081 }
17082 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17083 addr -= 4;
17084 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17085 addr -= 2;
17086
17087 val -= addr & -(1 << pcrel_op->align_log2);
17088
17089 /* If any of the shifted bits are set, we must use an extended
17090 opcode. If the address depends on the size of this
17091 instruction, this can lead to a loop, so we arrange to always
17092 use an extended opcode. */
17093 if ((val & ((1 << operand->shift) - 1)) != 0)
17094 {
17095 fragp->fr_subtype =
17096 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17097 return 1;
17098 }
17099
17100 /* If we are about to mark a frag as extended because the value
17101 is precisely the next value above maxtiny, then there is a
17102 chance of an infinite loop as in the following code:
17103 la $4,foo
17104 .skip 1020
17105 .align 2
17106 foo:
17107 In this case when the la is extended, foo is 0x3fc bytes
17108 away, so the la can be shrunk, but then foo is 0x400 away, so
17109 the la must be extended. To avoid this loop, we mark the
17110 frag as extended if it was small, and is about to become
17111 extended with the next value above maxtiny. */
17112 maxtiny = mips_int_operand_max (operand);
17113 if (val == maxtiny + (1 << operand->shift)
17114 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17115 {
17116 fragp->fr_subtype =
17117 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17118 return 1;
17119 }
17120 }
17121
17122 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17123 }
17124
17125 /* Compute the length of a branch sequence, and adjust the
17126 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17127 worst-case length is computed, with UPDATE being used to indicate
17128 whether an unconditional (-1), branch-likely (+1) or regular (0)
17129 branch is to be computed. */
17130 static int
17131 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17132 {
17133 bfd_boolean toofar;
17134 int length;
17135
17136 if (fragp
17137 && S_IS_DEFINED (fragp->fr_symbol)
17138 && !S_IS_WEAK (fragp->fr_symbol)
17139 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17140 {
17141 addressT addr;
17142 offsetT val;
17143
17144 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17145
17146 addr = fragp->fr_address + fragp->fr_fix + 4;
17147
17148 val -= addr;
17149
17150 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17151 }
17152 else
17153 /* If the symbol is not defined or it's in a different segment,
17154 we emit the long sequence. */
17155 toofar = TRUE;
17156
17157 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17158 fragp->fr_subtype
17159 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17160 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17161 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17162 RELAX_BRANCH_LINK (fragp->fr_subtype),
17163 toofar);
17164
17165 length = 4;
17166 if (toofar)
17167 {
17168 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17169 length += 8;
17170
17171 if (mips_pic != NO_PIC)
17172 {
17173 /* Additional space for PIC loading of target address. */
17174 length += 8;
17175 if (mips_opts.isa == ISA_MIPS1)
17176 /* Additional space for $at-stabilizing nop. */
17177 length += 4;
17178 }
17179
17180 /* If branch is conditional. */
17181 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17182 length += 8;
17183 }
17184
17185 return length;
17186 }
17187
17188 /* Compute the length of a branch sequence, and adjust the
17189 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17190 worst-case length is computed, with UPDATE being used to indicate
17191 whether an unconditional (-1), or regular (0) branch is to be
17192 computed. */
17193
17194 static int
17195 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17196 {
17197 bfd_boolean toofar;
17198 int length;
17199
17200 if (fragp
17201 && S_IS_DEFINED (fragp->fr_symbol)
17202 && !S_IS_WEAK (fragp->fr_symbol)
17203 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17204 {
17205 addressT addr;
17206 offsetT val;
17207
17208 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17209 /* Ignore the low bit in the target, since it will be set
17210 for a text label. */
17211 if ((val & 1) != 0)
17212 --val;
17213
17214 addr = fragp->fr_address + fragp->fr_fix + 4;
17215
17216 val -= addr;
17217
17218 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17219 }
17220 else
17221 /* If the symbol is not defined or it's in a different segment,
17222 we emit the long sequence. */
17223 toofar = TRUE;
17224
17225 if (fragp && update
17226 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17227 fragp->fr_subtype = (toofar
17228 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17229 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17230
17231 length = 4;
17232 if (toofar)
17233 {
17234 bfd_boolean compact_known = fragp != NULL;
17235 bfd_boolean compact = FALSE;
17236 bfd_boolean insn32 = TRUE;
17237 bfd_boolean uncond;
17238 int short_insn_size;
17239
17240 if (fragp)
17241 {
17242 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17243 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17244 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17245 }
17246 else
17247 uncond = update < 0;
17248 short_insn_size = insn32 ? 4 : 2;
17249
17250 /* If label is out of range, we turn branch <br>:
17251
17252 <br> label # 4 bytes
17253 0:
17254
17255 into:
17256
17257 j label # 4 bytes
17258 nop # 2/4 bytes if
17259 # compact && (!PIC || insn32)
17260 0:
17261 */
17262 if ((mips_pic == NO_PIC || insn32) && (!compact_known || compact))
17263 length += short_insn_size;
17264
17265 /* If assembling PIC code, we further turn:
17266
17267 j label # 4 bytes
17268
17269 into:
17270
17271 lw/ld at, %got(label)(gp) # 4 bytes
17272 d/addiu at, %lo(label) # 4 bytes
17273 jr/c at # 2/4 bytes
17274 */
17275 if (mips_pic != NO_PIC)
17276 length += 4 + short_insn_size;
17277
17278 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17279
17280 <brneg> 0f # 4 bytes
17281 nop # 2/4 bytes if !compact
17282 */
17283 if (!uncond)
17284 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
17285 }
17286
17287 return length;
17288 }
17289
17290 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17291 bit accordingly. */
17292
17293 static int
17294 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17295 {
17296 bfd_boolean toofar;
17297
17298 if (fragp
17299 && S_IS_DEFINED (fragp->fr_symbol)
17300 && !S_IS_WEAK (fragp->fr_symbol)
17301 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17302 {
17303 addressT addr;
17304 offsetT val;
17305 int type;
17306
17307 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17308 /* Ignore the low bit in the target, since it will be set
17309 for a text label. */
17310 if ((val & 1) != 0)
17311 --val;
17312
17313 /* Assume this is a 2-byte branch. */
17314 addr = fragp->fr_address + fragp->fr_fix + 2;
17315
17316 /* We try to avoid the infinite loop by not adding 2 more bytes for
17317 long branches. */
17318
17319 val -= addr;
17320
17321 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17322 if (type == 'D')
17323 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17324 else if (type == 'E')
17325 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17326 else
17327 abort ();
17328 }
17329 else
17330 /* If the symbol is not defined or it's in a different segment,
17331 we emit a normal 32-bit branch. */
17332 toofar = TRUE;
17333
17334 if (fragp && update
17335 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17336 fragp->fr_subtype
17337 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17338 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17339
17340 if (toofar)
17341 return 4;
17342
17343 return 2;
17344 }
17345
17346 /* Estimate the size of a frag before relaxing. Unless this is the
17347 mips16, we are not really relaxing here, and the final size is
17348 encoded in the subtype information. For the mips16, we have to
17349 decide whether we are using an extended opcode or not. */
17350
17351 int
17352 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17353 {
17354 int change;
17355
17356 if (RELAX_BRANCH_P (fragp->fr_subtype))
17357 {
17358
17359 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17360
17361 return fragp->fr_var;
17362 }
17363
17364 if (RELAX_MIPS16_P (fragp->fr_subtype))
17365 /* We don't want to modify the EXTENDED bit here; it might get us
17366 into infinite loops. We change it only in mips_relax_frag(). */
17367 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
17368
17369 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17370 {
17371 int length = 4;
17372
17373 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17374 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17375 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17376 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17377 fragp->fr_var = length;
17378
17379 return length;
17380 }
17381
17382 if (mips_pic == NO_PIC)
17383 change = nopic_need_relax (fragp->fr_symbol, 0);
17384 else if (mips_pic == SVR4_PIC)
17385 change = pic_need_relax (fragp->fr_symbol, segtype);
17386 else if (mips_pic == VXWORKS_PIC)
17387 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17388 change = 0;
17389 else
17390 abort ();
17391
17392 if (change)
17393 {
17394 fragp->fr_subtype |= RELAX_USE_SECOND;
17395 return -RELAX_FIRST (fragp->fr_subtype);
17396 }
17397 else
17398 return -RELAX_SECOND (fragp->fr_subtype);
17399 }
17400
17401 /* This is called to see whether a reloc against a defined symbol
17402 should be converted into a reloc against a section. */
17403
17404 int
17405 mips_fix_adjustable (fixS *fixp)
17406 {
17407 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17408 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17409 return 0;
17410
17411 if (fixp->fx_addsy == NULL)
17412 return 1;
17413
17414 /* Allow relocs used for EH tables. */
17415 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17416 return 1;
17417
17418 /* If symbol SYM is in a mergeable section, relocations of the form
17419 SYM + 0 can usually be made section-relative. The mergeable data
17420 is then identified by the section offset rather than by the symbol.
17421
17422 However, if we're generating REL LO16 relocations, the offset is split
17423 between the LO16 and parterning high part relocation. The linker will
17424 need to recalculate the complete offset in order to correctly identify
17425 the merge data.
17426
17427 The linker has traditionally not looked for the parterning high part
17428 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17429 placed anywhere. Rather than break backwards compatibility by changing
17430 this, it seems better not to force the issue, and instead keep the
17431 original symbol. This will work with either linker behavior. */
17432 if ((lo16_reloc_p (fixp->fx_r_type)
17433 || reloc_needs_lo_p (fixp->fx_r_type))
17434 && HAVE_IN_PLACE_ADDENDS
17435 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17436 return 0;
17437
17438 /* There is no place to store an in-place offset for JALR relocations. */
17439 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
17440 return 0;
17441
17442 /* Likewise an in-range offset of limited PC-relative relocations may
17443 overflow the in-place relocatable field if recalculated against the
17444 start address of the symbol's containing section.
17445
17446 Also, PC relative relocations for MIPS R6 need to be symbol rather than
17447 section relative to allow linker relaxations to be performed later on. */
17448 if (limited_pcrel_reloc_p (fixp->fx_r_type)
17449 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
17450 return 0;
17451
17452 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17453 to a floating-point stub. The same is true for non-R_MIPS16_26
17454 relocations against MIPS16 functions; in this case, the stub becomes
17455 the function's canonical address.
17456
17457 Floating-point stubs are stored in unique .mips16.call.* or
17458 .mips16.fn.* sections. If a stub T for function F is in section S,
17459 the first relocation in section S must be against F; this is how the
17460 linker determines the target function. All relocations that might
17461 resolve to T must also be against F. We therefore have the following
17462 restrictions, which are given in an intentionally-redundant way:
17463
17464 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17465 symbols.
17466
17467 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17468 if that stub might be used.
17469
17470 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17471 symbols.
17472
17473 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17474 that stub might be used.
17475
17476 There is a further restriction:
17477
17478 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17479 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
17480 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
17481 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
17482 against MIPS16 or microMIPS symbols because we need to keep the
17483 MIPS16 or microMIPS symbol for the purpose of mode mismatch
17484 detection and JAL or BAL to JALX instruction conversion in the
17485 linker.
17486
17487 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17488 against a MIPS16 symbol. We deal with (5) by additionally leaving
17489 alone any jump and branch relocations against a microMIPS symbol.
17490
17491 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17492 relocation against some symbol R, no relocation against R may be
17493 reduced. (Note that this deals with (2) as well as (1) because
17494 relocations against global symbols will never be reduced on ELF
17495 targets.) This approach is a little simpler than trying to detect
17496 stub sections, and gives the "all or nothing" per-symbol consistency
17497 that we have for MIPS16 symbols. */
17498 if (fixp->fx_subsy == NULL
17499 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
17500 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17501 && (jmp_reloc_p (fixp->fx_r_type)
17502 || b_reloc_p (fixp->fx_r_type)))
17503 || *symbol_get_tc (fixp->fx_addsy)))
17504 return 0;
17505
17506 return 1;
17507 }
17508
17509 /* Translate internal representation of relocation info to BFD target
17510 format. */
17511
17512 arelent **
17513 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
17514 {
17515 static arelent *retval[4];
17516 arelent *reloc;
17517 bfd_reloc_code_real_type code;
17518
17519 memset (retval, 0, sizeof(retval));
17520 reloc = retval[0] = XCNEW (arelent);
17521 reloc->sym_ptr_ptr = XNEW (asymbol *);
17522 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
17523 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17524
17525 if (fixp->fx_pcrel)
17526 {
17527 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17528 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
17529 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17530 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17531 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
17532 || fixp->fx_r_type == BFD_RELOC_32_PCREL
17533 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
17534 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
17535 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
17536 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
17537 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
17538 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
17539
17540 /* At this point, fx_addnumber is "symbol offset - pcrel address".
17541 Relocations want only the symbol offset. */
17542 switch (fixp->fx_r_type)
17543 {
17544 case BFD_RELOC_MIPS_18_PCREL_S3:
17545 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
17546 break;
17547 default:
17548 reloc->addend = fixp->fx_addnumber + reloc->address;
17549 break;
17550 }
17551 }
17552 else if (HAVE_IN_PLACE_ADDENDS
17553 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
17554 && (read_compressed_insn (fixp->fx_frag->fr_literal
17555 + fixp->fx_where, 4) >> 26) == 0x3c)
17556 {
17557 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
17558 addend accordingly. */
17559 reloc->addend = fixp->fx_addnumber >> 1;
17560 }
17561 else
17562 reloc->addend = fixp->fx_addnumber;
17563
17564 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17565 entry to be used in the relocation's section offset. */
17566 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17567 {
17568 reloc->address = reloc->addend;
17569 reloc->addend = 0;
17570 }
17571
17572 code = fixp->fx_r_type;
17573
17574 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
17575 if (reloc->howto == NULL)
17576 {
17577 as_bad_where (fixp->fx_file, fixp->fx_line,
17578 _("cannot represent %s relocation in this object file"
17579 " format"),
17580 bfd_get_reloc_code_name (code));
17581 retval[0] = NULL;
17582 }
17583
17584 return retval;
17585 }
17586
17587 /* Relax a machine dependent frag. This returns the amount by which
17588 the current size of the frag should change. */
17589
17590 int
17591 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
17592 {
17593 if (RELAX_BRANCH_P (fragp->fr_subtype))
17594 {
17595 offsetT old_var = fragp->fr_var;
17596
17597 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
17598
17599 return fragp->fr_var - old_var;
17600 }
17601
17602 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17603 {
17604 offsetT old_var = fragp->fr_var;
17605 offsetT new_var = 4;
17606
17607 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17608 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17609 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17610 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17611 fragp->fr_var = new_var;
17612
17613 return new_var - old_var;
17614 }
17615
17616 if (! RELAX_MIPS16_P (fragp->fr_subtype))
17617 return 0;
17618
17619 if (mips16_extended_frag (fragp, sec, stretch))
17620 {
17621 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17622 return 0;
17623 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17624 return 2;
17625 }
17626 else
17627 {
17628 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17629 return 0;
17630 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17631 return -2;
17632 }
17633
17634 return 0;
17635 }
17636
17637 /* Convert a machine dependent frag. */
17638
17639 void
17640 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
17641 {
17642 if (RELAX_BRANCH_P (fragp->fr_subtype))
17643 {
17644 char *buf;
17645 unsigned long insn;
17646 expressionS exp;
17647 fixS *fixp;
17648
17649 buf = fragp->fr_literal + fragp->fr_fix;
17650 insn = read_insn (buf);
17651
17652 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17653 {
17654 /* We generate a fixup instead of applying it right now
17655 because, if there are linker relaxations, we're going to
17656 need the relocations. */
17657 exp.X_op = O_symbol;
17658 exp.X_add_symbol = fragp->fr_symbol;
17659 exp.X_add_number = fragp->fr_offset;
17660
17661 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
17662 BFD_RELOC_16_PCREL_S2);
17663 fixp->fx_file = fragp->fr_file;
17664 fixp->fx_line = fragp->fr_line;
17665
17666 buf = write_insn (buf, insn);
17667 }
17668 else
17669 {
17670 int i;
17671
17672 as_warn_where (fragp->fr_file, fragp->fr_line,
17673 _("relaxed out-of-range branch into a jump"));
17674
17675 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
17676 goto uncond;
17677
17678 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17679 {
17680 /* Reverse the branch. */
17681 switch ((insn >> 28) & 0xf)
17682 {
17683 case 4:
17684 if ((insn & 0xff000000) == 0x47000000
17685 || (insn & 0xff600000) == 0x45600000)
17686 {
17687 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
17688 reversed by tweaking bit 23. */
17689 insn ^= 0x00800000;
17690 }
17691 else
17692 {
17693 /* bc[0-3][tf]l? instructions can have the condition
17694 reversed by tweaking a single TF bit, and their
17695 opcodes all have 0x4???????. */
17696 gas_assert ((insn & 0xf3e00000) == 0x41000000);
17697 insn ^= 0x00010000;
17698 }
17699 break;
17700
17701 case 0:
17702 /* bltz 0x04000000 bgez 0x04010000
17703 bltzal 0x04100000 bgezal 0x04110000 */
17704 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
17705 insn ^= 0x00010000;
17706 break;
17707
17708 case 1:
17709 /* beq 0x10000000 bne 0x14000000
17710 blez 0x18000000 bgtz 0x1c000000 */
17711 insn ^= 0x04000000;
17712 break;
17713
17714 default:
17715 abort ();
17716 }
17717 }
17718
17719 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
17720 {
17721 /* Clear the and-link bit. */
17722 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
17723
17724 /* bltzal 0x04100000 bgezal 0x04110000
17725 bltzall 0x04120000 bgezall 0x04130000 */
17726 insn &= ~0x00100000;
17727 }
17728
17729 /* Branch over the branch (if the branch was likely) or the
17730 full jump (not likely case). Compute the offset from the
17731 current instruction to branch to. */
17732 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17733 i = 16;
17734 else
17735 {
17736 /* How many bytes in instructions we've already emitted? */
17737 i = buf - fragp->fr_literal - fragp->fr_fix;
17738 /* How many bytes in instructions from here to the end? */
17739 i = fragp->fr_var - i;
17740 }
17741 /* Convert to instruction count. */
17742 i >>= 2;
17743 /* Branch counts from the next instruction. */
17744 i--;
17745 insn |= i;
17746 /* Branch over the jump. */
17747 buf = write_insn (buf, insn);
17748
17749 /* nop */
17750 buf = write_insn (buf, 0);
17751
17752 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17753 {
17754 /* beql $0, $0, 2f */
17755 insn = 0x50000000;
17756 /* Compute the PC offset from the current instruction to
17757 the end of the variable frag. */
17758 /* How many bytes in instructions we've already emitted? */
17759 i = buf - fragp->fr_literal - fragp->fr_fix;
17760 /* How many bytes in instructions from here to the end? */
17761 i = fragp->fr_var - i;
17762 /* Convert to instruction count. */
17763 i >>= 2;
17764 /* Don't decrement i, because we want to branch over the
17765 delay slot. */
17766 insn |= i;
17767
17768 buf = write_insn (buf, insn);
17769 buf = write_insn (buf, 0);
17770 }
17771
17772 uncond:
17773 if (mips_pic == NO_PIC)
17774 {
17775 /* j or jal. */
17776 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
17777 ? 0x0c000000 : 0x08000000);
17778 exp.X_op = O_symbol;
17779 exp.X_add_symbol = fragp->fr_symbol;
17780 exp.X_add_number = fragp->fr_offset;
17781
17782 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
17783 FALSE, BFD_RELOC_MIPS_JMP);
17784 fixp->fx_file = fragp->fr_file;
17785 fixp->fx_line = fragp->fr_line;
17786
17787 buf = write_insn (buf, insn);
17788 }
17789 else
17790 {
17791 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
17792
17793 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
17794 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
17795 insn |= at << OP_SH_RT;
17796 exp.X_op = O_symbol;
17797 exp.X_add_symbol = fragp->fr_symbol;
17798 exp.X_add_number = fragp->fr_offset;
17799
17800 if (fragp->fr_offset)
17801 {
17802 exp.X_add_symbol = make_expr_symbol (&exp);
17803 exp.X_add_number = 0;
17804 }
17805
17806 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
17807 FALSE, BFD_RELOC_MIPS_GOT16);
17808 fixp->fx_file = fragp->fr_file;
17809 fixp->fx_line = fragp->fr_line;
17810
17811 buf = write_insn (buf, insn);
17812
17813 if (mips_opts.isa == ISA_MIPS1)
17814 /* nop */
17815 buf = write_insn (buf, 0);
17816
17817 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
17818 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
17819 insn |= at << OP_SH_RS | at << OP_SH_RT;
17820
17821 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
17822 FALSE, BFD_RELOC_LO16);
17823 fixp->fx_file = fragp->fr_file;
17824 fixp->fx_line = fragp->fr_line;
17825
17826 buf = write_insn (buf, insn);
17827
17828 /* j(al)r $at. */
17829 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
17830 insn = 0x0000f809;
17831 else
17832 insn = 0x00000008;
17833 insn |= at << OP_SH_RS;
17834
17835 buf = write_insn (buf, insn);
17836 }
17837 }
17838
17839 fragp->fr_fix += fragp->fr_var;
17840 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
17841 return;
17842 }
17843
17844 /* Relax microMIPS branches. */
17845 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17846 {
17847 char *buf = fragp->fr_literal + fragp->fr_fix;
17848 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17849 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17850 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17851 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17852 bfd_boolean short_ds;
17853 unsigned long insn;
17854 expressionS exp;
17855 fixS *fixp;
17856
17857 exp.X_op = O_symbol;
17858 exp.X_add_symbol = fragp->fr_symbol;
17859 exp.X_add_number = fragp->fr_offset;
17860
17861 fragp->fr_fix += fragp->fr_var;
17862
17863 /* Handle 16-bit branches that fit or are forced to fit. */
17864 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17865 {
17866 /* We generate a fixup instead of applying it right now,
17867 because if there is linker relaxation, we're going to
17868 need the relocations. */
17869 if (type == 'D')
17870 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
17871 BFD_RELOC_MICROMIPS_10_PCREL_S1);
17872 else if (type == 'E')
17873 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
17874 BFD_RELOC_MICROMIPS_7_PCREL_S1);
17875 else
17876 abort ();
17877
17878 fixp->fx_file = fragp->fr_file;
17879 fixp->fx_line = fragp->fr_line;
17880
17881 /* These relocations can have an addend that won't fit in
17882 2 octets. */
17883 fixp->fx_no_overflow = 1;
17884
17885 return;
17886 }
17887
17888 /* Handle 32-bit branches that fit or are forced to fit. */
17889 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
17890 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17891 {
17892 /* We generate a fixup instead of applying it right now,
17893 because if there is linker relaxation, we're going to
17894 need the relocations. */
17895 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
17896 BFD_RELOC_MICROMIPS_16_PCREL_S1);
17897 fixp->fx_file = fragp->fr_file;
17898 fixp->fx_line = fragp->fr_line;
17899
17900 if (type == 0)
17901 return;
17902 }
17903
17904 /* Relax 16-bit branches to 32-bit branches. */
17905 if (type != 0)
17906 {
17907 insn = read_compressed_insn (buf, 2);
17908
17909 if ((insn & 0xfc00) == 0xcc00) /* b16 */
17910 insn = 0x94000000; /* beq */
17911 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
17912 {
17913 unsigned long regno;
17914
17915 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
17916 regno = micromips_to_32_reg_d_map [regno];
17917 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
17918 insn |= regno << MICROMIPSOP_SH_RS;
17919 }
17920 else
17921 abort ();
17922
17923 /* Nothing else to do, just write it out. */
17924 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
17925 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17926 {
17927 buf = write_compressed_insn (buf, insn, 4);
17928 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
17929 return;
17930 }
17931 }
17932 else
17933 insn = read_compressed_insn (buf, 4);
17934
17935 /* Relax 32-bit branches to a sequence of instructions. */
17936 as_warn_where (fragp->fr_file, fragp->fr_line,
17937 _("relaxed out-of-range branch into a jump"));
17938
17939 /* Set the short-delay-slot bit. */
17940 short_ds = al && (insn & 0x02000000) != 0;
17941
17942 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
17943 {
17944 symbolS *l;
17945
17946 /* Reverse the branch. */
17947 if ((insn & 0xfc000000) == 0x94000000 /* beq */
17948 || (insn & 0xfc000000) == 0xb4000000) /* bne */
17949 insn ^= 0x20000000;
17950 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
17951 || (insn & 0xffe00000) == 0x40400000 /* bgez */
17952 || (insn & 0xffe00000) == 0x40800000 /* blez */
17953 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
17954 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
17955 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
17956 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
17957 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
17958 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
17959 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
17960 insn ^= 0x00400000;
17961 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
17962 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
17963 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
17964 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
17965 insn ^= 0x00200000;
17966 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
17967 BNZ.df */
17968 || (insn & 0xff600000) == 0x81600000) /* BZ.V
17969 BNZ.V */
17970 insn ^= 0x00800000;
17971 else
17972 abort ();
17973
17974 if (al)
17975 {
17976 /* Clear the and-link and short-delay-slot bits. */
17977 gas_assert ((insn & 0xfda00000) == 0x40200000);
17978
17979 /* bltzal 0x40200000 bgezal 0x40600000 */
17980 /* bltzals 0x42200000 bgezals 0x42600000 */
17981 insn &= ~0x02200000;
17982 }
17983
17984 /* Make a label at the end for use with the branch. */
17985 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
17986 micromips_label_inc ();
17987 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
17988
17989 /* Refer to it. */
17990 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
17991 BFD_RELOC_MICROMIPS_16_PCREL_S1);
17992 fixp->fx_file = fragp->fr_file;
17993 fixp->fx_line = fragp->fr_line;
17994
17995 /* Branch over the jump. */
17996 buf = write_compressed_insn (buf, insn, 4);
17997
17998 if (!compact)
17999 {
18000 /* nop */
18001 if (insn32)
18002 buf = write_compressed_insn (buf, 0x00000000, 4);
18003 else
18004 buf = write_compressed_insn (buf, 0x0c00, 2);
18005 }
18006 }
18007
18008 if (mips_pic == NO_PIC)
18009 {
18010 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
18011
18012 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18013 insn = al ? jal : 0xd4000000;
18014
18015 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18016 BFD_RELOC_MICROMIPS_JMP);
18017 fixp->fx_file = fragp->fr_file;
18018 fixp->fx_line = fragp->fr_line;
18019
18020 buf = write_compressed_insn (buf, insn, 4);
18021
18022 if (compact)
18023 {
18024 /* nop */
18025 if (insn32)
18026 buf = write_compressed_insn (buf, 0x00000000, 4);
18027 else
18028 buf = write_compressed_insn (buf, 0x0c00, 2);
18029 }
18030 }
18031 else
18032 {
18033 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18034
18035 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18036 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18037 insn |= at << MICROMIPSOP_SH_RT;
18038
18039 if (exp.X_add_number)
18040 {
18041 exp.X_add_symbol = make_expr_symbol (&exp);
18042 exp.X_add_number = 0;
18043 }
18044
18045 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18046 BFD_RELOC_MICROMIPS_GOT16);
18047 fixp->fx_file = fragp->fr_file;
18048 fixp->fx_line = fragp->fr_line;
18049
18050 buf = write_compressed_insn (buf, insn, 4);
18051
18052 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18053 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18054 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18055
18056 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18057 BFD_RELOC_MICROMIPS_LO16);
18058 fixp->fx_file = fragp->fr_file;
18059 fixp->fx_line = fragp->fr_line;
18060
18061 buf = write_compressed_insn (buf, insn, 4);
18062
18063 if (insn32)
18064 {
18065 /* jr/jalr $at */
18066 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18067 insn |= at << MICROMIPSOP_SH_RS;
18068
18069 buf = write_compressed_insn (buf, insn, 4);
18070
18071 if (compact)
18072 /* nop */
18073 buf = write_compressed_insn (buf, 0x00000000, 4);
18074 }
18075 else
18076 {
18077 /* jr/jrc/jalr/jalrs $at */
18078 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18079 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
18080
18081 insn = al ? jalr : jr;
18082 insn |= at << MICROMIPSOP_SH_MJ;
18083
18084 buf = write_compressed_insn (buf, insn, 2);
18085 }
18086 }
18087
18088 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18089 return;
18090 }
18091
18092 if (RELAX_MIPS16_P (fragp->fr_subtype))
18093 {
18094 int type;
18095 const struct mips_int_operand *operand;
18096 offsetT val;
18097 char *buf;
18098 unsigned int user_length, length;
18099 bfd_boolean need_reloc;
18100 unsigned long insn;
18101 bfd_boolean ext;
18102 segT symsec;
18103
18104 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18105 operand = mips16_immed_operand (type, FALSE);
18106
18107 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18108 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
18109
18110 symsec = S_GET_SEGMENT (fragp->fr_symbol);
18111 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
18112 || (operand->root.type == OP_PCREL
18113 ? asec != symsec
18114 : !bfd_is_abs_section (symsec)));
18115
18116 if (operand->root.type == OP_PCREL)
18117 {
18118 const struct mips_pcrel_operand *pcrel_op;
18119 addressT addr;
18120
18121 pcrel_op = (const struct mips_pcrel_operand *) operand;
18122 addr = fragp->fr_address + fragp->fr_fix;
18123
18124 /* The rules for the base address of a PC relative reloc are
18125 complicated; see mips16_extended_frag. */
18126 if (pcrel_op->include_isa_bit)
18127 {
18128 if (!need_reloc)
18129 {
18130 if (!ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
18131 as_bad_where (fragp->fr_file, fragp->fr_line,
18132 _("branch to a symbol in another ISA mode"));
18133 else if ((fragp->fr_offset & 0x1) != 0)
18134 as_bad_where (fragp->fr_file, fragp->fr_line,
18135 _("branch to misaligned address (0x%lx)"),
18136 (long) val);
18137 }
18138 addr += 2;
18139 if (ext)
18140 addr += 2;
18141 /* Ignore the low bit in the target, since it will be
18142 set for a text label. */
18143 val &= -2;
18144 }
18145 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18146 addr -= 4;
18147 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18148 addr -= 2;
18149
18150 addr &= -(1 << pcrel_op->align_log2);
18151 val -= addr;
18152
18153 /* Make sure the section winds up with the alignment we have
18154 assumed. */
18155 if (operand->shift > 0)
18156 record_alignment (asec, operand->shift);
18157 }
18158
18159 if (ext
18160 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18161 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18162 as_warn_where (fragp->fr_file, fragp->fr_line,
18163 _("extended instruction in delay slot"));
18164
18165 buf = fragp->fr_literal + fragp->fr_fix;
18166
18167 insn = read_compressed_insn (buf, 2);
18168 if (ext)
18169 insn |= MIPS16_EXTEND;
18170
18171 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18172 user_length = 4;
18173 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18174 user_length = 2;
18175 else
18176 user_length = 0;
18177
18178 if (need_reloc)
18179 {
18180 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
18181 expressionS exp;
18182 fixS *fixp;
18183
18184 switch (type)
18185 {
18186 case 'p':
18187 case 'q':
18188 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
18189 break;
18190 default:
18191 as_bad_where (fragp->fr_file, fragp->fr_line,
18192 _("unsupported relocation"));
18193 break;
18194 }
18195 if (reloc != BFD_RELOC_NONE)
18196 {
18197 gas_assert (ext);
18198
18199 exp.X_op = O_symbol;
18200 exp.X_add_symbol = fragp->fr_symbol;
18201 exp.X_add_number = fragp->fr_offset;
18202
18203 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp,
18204 TRUE, reloc);
18205
18206 fixp->fx_file = fragp->fr_file;
18207 fixp->fx_line = fragp->fr_line;
18208
18209 /* These relocations can have an addend that won't fit
18210 in 2 octets. */
18211 fixp->fx_no_overflow = 1;
18212 }
18213 }
18214 else
18215 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18216 BFD_RELOC_UNUSED, val, user_length, &insn);
18217
18218 length = (ext ? 4 : 2);
18219 gas_assert (mips16_opcode_length (insn) == length);
18220 write_compressed_insn (buf, insn, length);
18221 fragp->fr_fix += length;
18222 }
18223 else
18224 {
18225 relax_substateT subtype = fragp->fr_subtype;
18226 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18227 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18228 int first, second;
18229 fixS *fixp;
18230
18231 first = RELAX_FIRST (subtype);
18232 second = RELAX_SECOND (subtype);
18233 fixp = (fixS *) fragp->fr_opcode;
18234
18235 /* If the delay slot chosen does not match the size of the instruction,
18236 then emit a warning. */
18237 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18238 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18239 {
18240 relax_substateT s;
18241 const char *msg;
18242
18243 s = subtype & (RELAX_DELAY_SLOT_16BIT
18244 | RELAX_DELAY_SLOT_SIZE_FIRST
18245 | RELAX_DELAY_SLOT_SIZE_SECOND);
18246 msg = macro_warning (s);
18247 if (msg != NULL)
18248 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18249 subtype &= ~s;
18250 }
18251
18252 /* Possibly emit a warning if we've chosen the longer option. */
18253 if (use_second == second_longer)
18254 {
18255 relax_substateT s;
18256 const char *msg;
18257
18258 s = (subtype
18259 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18260 msg = macro_warning (s);
18261 if (msg != NULL)
18262 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18263 subtype &= ~s;
18264 }
18265
18266 /* Go through all the fixups for the first sequence. Disable them
18267 (by marking them as done) if we're going to use the second
18268 sequence instead. */
18269 while (fixp
18270 && fixp->fx_frag == fragp
18271 && fixp->fx_where < fragp->fr_fix - second)
18272 {
18273 if (subtype & RELAX_USE_SECOND)
18274 fixp->fx_done = 1;
18275 fixp = fixp->fx_next;
18276 }
18277
18278 /* Go through the fixups for the second sequence. Disable them if
18279 we're going to use the first sequence, otherwise adjust their
18280 addresses to account for the relaxation. */
18281 while (fixp && fixp->fx_frag == fragp)
18282 {
18283 if (subtype & RELAX_USE_SECOND)
18284 fixp->fx_where -= first;
18285 else
18286 fixp->fx_done = 1;
18287 fixp = fixp->fx_next;
18288 }
18289
18290 /* Now modify the frag contents. */
18291 if (subtype & RELAX_USE_SECOND)
18292 {
18293 char *start;
18294
18295 start = fragp->fr_literal + fragp->fr_fix - first - second;
18296 memmove (start, start + first, second);
18297 fragp->fr_fix -= first;
18298 }
18299 else
18300 fragp->fr_fix -= second;
18301 }
18302 }
18303
18304 /* This function is called after the relocs have been generated.
18305 We've been storing mips16 text labels as odd. Here we convert them
18306 back to even for the convenience of the debugger. */
18307
18308 void
18309 mips_frob_file_after_relocs (void)
18310 {
18311 asymbol **syms;
18312 unsigned int count, i;
18313
18314 syms = bfd_get_outsymbols (stdoutput);
18315 count = bfd_get_symcount (stdoutput);
18316 for (i = 0; i < count; i++, syms++)
18317 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18318 && ((*syms)->value & 1) != 0)
18319 {
18320 (*syms)->value &= ~1;
18321 /* If the symbol has an odd size, it was probably computed
18322 incorrectly, so adjust that as well. */
18323 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18324 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18325 }
18326 }
18327
18328 /* This function is called whenever a label is defined, including fake
18329 labels instantiated off the dot special symbol. It is used when
18330 handling branch delays; if a branch has a label, we assume we cannot
18331 move it. This also bumps the value of the symbol by 1 in compressed
18332 code. */
18333
18334 static void
18335 mips_record_label (symbolS *sym)
18336 {
18337 segment_info_type *si = seg_info (now_seg);
18338 struct insn_label_list *l;
18339
18340 if (free_insn_labels == NULL)
18341 l = XNEW (struct insn_label_list);
18342 else
18343 {
18344 l = free_insn_labels;
18345 free_insn_labels = l->next;
18346 }
18347
18348 l->label = sym;
18349 l->next = si->label_list;
18350 si->label_list = l;
18351 }
18352
18353 /* This function is called as tc_frob_label() whenever a label is defined
18354 and adds a DWARF-2 record we only want for true labels. */
18355
18356 void
18357 mips_define_label (symbolS *sym)
18358 {
18359 mips_record_label (sym);
18360 dwarf2_emit_label (sym);
18361 }
18362
18363 /* This function is called by tc_new_dot_label whenever a new dot symbol
18364 is defined. */
18365
18366 void
18367 mips_add_dot_label (symbolS *sym)
18368 {
18369 mips_record_label (sym);
18370 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18371 mips_compressed_mark_label (sym);
18372 }
18373 \f
18374 /* Converting ASE flags from internal to .MIPS.abiflags values. */
18375 static unsigned int
18376 mips_convert_ase_flags (int ase)
18377 {
18378 unsigned int ext_ases = 0;
18379
18380 if (ase & ASE_DSP)
18381 ext_ases |= AFL_ASE_DSP;
18382 if (ase & ASE_DSPR2)
18383 ext_ases |= AFL_ASE_DSPR2;
18384 if (ase & ASE_DSPR3)
18385 ext_ases |= AFL_ASE_DSPR3;
18386 if (ase & ASE_EVA)
18387 ext_ases |= AFL_ASE_EVA;
18388 if (ase & ASE_MCU)
18389 ext_ases |= AFL_ASE_MCU;
18390 if (ase & ASE_MDMX)
18391 ext_ases |= AFL_ASE_MDMX;
18392 if (ase & ASE_MIPS3D)
18393 ext_ases |= AFL_ASE_MIPS3D;
18394 if (ase & ASE_MT)
18395 ext_ases |= AFL_ASE_MT;
18396 if (ase & ASE_SMARTMIPS)
18397 ext_ases |= AFL_ASE_SMARTMIPS;
18398 if (ase & ASE_VIRT)
18399 ext_ases |= AFL_ASE_VIRT;
18400 if (ase & ASE_MSA)
18401 ext_ases |= AFL_ASE_MSA;
18402 if (ase & ASE_XPA)
18403 ext_ases |= AFL_ASE_XPA;
18404
18405 return ext_ases;
18406 }
18407 /* Some special processing for a MIPS ELF file. */
18408
18409 void
18410 mips_elf_final_processing (void)
18411 {
18412 int fpabi;
18413 Elf_Internal_ABIFlags_v0 flags;
18414
18415 flags.version = 0;
18416 flags.isa_rev = 0;
18417 switch (file_mips_opts.isa)
18418 {
18419 case INSN_ISA1:
18420 flags.isa_level = 1;
18421 break;
18422 case INSN_ISA2:
18423 flags.isa_level = 2;
18424 break;
18425 case INSN_ISA3:
18426 flags.isa_level = 3;
18427 break;
18428 case INSN_ISA4:
18429 flags.isa_level = 4;
18430 break;
18431 case INSN_ISA5:
18432 flags.isa_level = 5;
18433 break;
18434 case INSN_ISA32:
18435 flags.isa_level = 32;
18436 flags.isa_rev = 1;
18437 break;
18438 case INSN_ISA32R2:
18439 flags.isa_level = 32;
18440 flags.isa_rev = 2;
18441 break;
18442 case INSN_ISA32R3:
18443 flags.isa_level = 32;
18444 flags.isa_rev = 3;
18445 break;
18446 case INSN_ISA32R5:
18447 flags.isa_level = 32;
18448 flags.isa_rev = 5;
18449 break;
18450 case INSN_ISA32R6:
18451 flags.isa_level = 32;
18452 flags.isa_rev = 6;
18453 break;
18454 case INSN_ISA64:
18455 flags.isa_level = 64;
18456 flags.isa_rev = 1;
18457 break;
18458 case INSN_ISA64R2:
18459 flags.isa_level = 64;
18460 flags.isa_rev = 2;
18461 break;
18462 case INSN_ISA64R3:
18463 flags.isa_level = 64;
18464 flags.isa_rev = 3;
18465 break;
18466 case INSN_ISA64R5:
18467 flags.isa_level = 64;
18468 flags.isa_rev = 5;
18469 break;
18470 case INSN_ISA64R6:
18471 flags.isa_level = 64;
18472 flags.isa_rev = 6;
18473 break;
18474 }
18475
18476 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
18477 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
18478 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
18479 : (file_mips_opts.fp == 64) ? AFL_REG_64
18480 : AFL_REG_32;
18481 flags.cpr2_size = AFL_REG_NONE;
18482 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
18483 Tag_GNU_MIPS_ABI_FP);
18484 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
18485 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
18486 if (file_ase_mips16)
18487 flags.ases |= AFL_ASE_MIPS16;
18488 if (file_ase_micromips)
18489 flags.ases |= AFL_ASE_MICROMIPS;
18490 flags.flags1 = 0;
18491 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
18492 || file_mips_opts.fp == 64)
18493 && file_mips_opts.oddspreg)
18494 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
18495 flags.flags2 = 0;
18496
18497 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
18498 ((Elf_External_ABIFlags_v0 *)
18499 mips_flags_frag));
18500
18501 /* Write out the register information. */
18502 if (mips_abi != N64_ABI)
18503 {
18504 Elf32_RegInfo s;
18505
18506 s.ri_gprmask = mips_gprmask;
18507 s.ri_cprmask[0] = mips_cprmask[0];
18508 s.ri_cprmask[1] = mips_cprmask[1];
18509 s.ri_cprmask[2] = mips_cprmask[2];
18510 s.ri_cprmask[3] = mips_cprmask[3];
18511 /* The gp_value field is set by the MIPS ELF backend. */
18512
18513 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18514 ((Elf32_External_RegInfo *)
18515 mips_regmask_frag));
18516 }
18517 else
18518 {
18519 Elf64_Internal_RegInfo s;
18520
18521 s.ri_gprmask = mips_gprmask;
18522 s.ri_pad = 0;
18523 s.ri_cprmask[0] = mips_cprmask[0];
18524 s.ri_cprmask[1] = mips_cprmask[1];
18525 s.ri_cprmask[2] = mips_cprmask[2];
18526 s.ri_cprmask[3] = mips_cprmask[3];
18527 /* The gp_value field is set by the MIPS ELF backend. */
18528
18529 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18530 ((Elf64_External_RegInfo *)
18531 mips_regmask_frag));
18532 }
18533
18534 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18535 sort of BFD interface for this. */
18536 if (mips_any_noreorder)
18537 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18538 if (mips_pic != NO_PIC)
18539 {
18540 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
18541 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18542 }
18543 if (mips_abicalls)
18544 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18545
18546 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
18547 defined at present; this might need to change in future. */
18548 if (file_ase_mips16)
18549 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
18550 if (file_ase_micromips)
18551 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
18552 if (file_mips_opts.ase & ASE_MDMX)
18553 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
18554
18555 /* Set the MIPS ELF ABI flags. */
18556 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
18557 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
18558 else if (mips_abi == O64_ABI)
18559 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
18560 else if (mips_abi == EABI_ABI)
18561 {
18562 if (file_mips_opts.gp == 64)
18563 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18564 else
18565 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18566 }
18567 else if (mips_abi == N32_ABI)
18568 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18569
18570 /* Nothing to do for N64_ABI. */
18571
18572 if (mips_32bitmode)
18573 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
18574
18575 if (mips_nan2008 == 1)
18576 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
18577
18578 /* 32 bit code with 64 bit FP registers. */
18579 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
18580 Tag_GNU_MIPS_ABI_FP);
18581 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
18582 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
18583 }
18584 \f
18585 typedef struct proc {
18586 symbolS *func_sym;
18587 symbolS *func_end_sym;
18588 unsigned long reg_mask;
18589 unsigned long reg_offset;
18590 unsigned long fpreg_mask;
18591 unsigned long fpreg_offset;
18592 unsigned long frame_offset;
18593 unsigned long frame_reg;
18594 unsigned long pc_reg;
18595 } procS;
18596
18597 static procS cur_proc;
18598 static procS *cur_proc_ptr;
18599 static int numprocs;
18600
18601 /* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
18602 as "2", and a normal nop as "0". */
18603
18604 #define NOP_OPCODE_MIPS 0
18605 #define NOP_OPCODE_MIPS16 1
18606 #define NOP_OPCODE_MICROMIPS 2
18607
18608 char
18609 mips_nop_opcode (void)
18610 {
18611 if (seg_info (now_seg)->tc_segment_info_data.micromips)
18612 return NOP_OPCODE_MICROMIPS;
18613 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18614 return NOP_OPCODE_MIPS16;
18615 else
18616 return NOP_OPCODE_MIPS;
18617 }
18618
18619 /* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
18620 32-bit microMIPS NOPs here (if applicable). */
18621
18622 void
18623 mips_handle_align (fragS *fragp)
18624 {
18625 char nop_opcode;
18626 char *p;
18627 int bytes, size, excess;
18628 valueT opcode;
18629
18630 if (fragp->fr_type != rs_align_code)
18631 return;
18632
18633 p = fragp->fr_literal + fragp->fr_fix;
18634 nop_opcode = *p;
18635 switch (nop_opcode)
18636 {
18637 case NOP_OPCODE_MICROMIPS:
18638 opcode = micromips_nop32_insn.insn_opcode;
18639 size = 4;
18640 break;
18641 case NOP_OPCODE_MIPS16:
18642 opcode = mips16_nop_insn.insn_opcode;
18643 size = 2;
18644 break;
18645 case NOP_OPCODE_MIPS:
18646 default:
18647 opcode = nop_insn.insn_opcode;
18648 size = 4;
18649 break;
18650 }
18651
18652 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18653 excess = bytes % size;
18654
18655 /* Handle the leading part if we're not inserting a whole number of
18656 instructions, and make it the end of the fixed part of the frag.
18657 Try to fit in a short microMIPS NOP if applicable and possible,
18658 and use zeroes otherwise. */
18659 gas_assert (excess < 4);
18660 fragp->fr_fix += excess;
18661 switch (excess)
18662 {
18663 case 3:
18664 *p++ = '\0';
18665 /* Fall through. */
18666 case 2:
18667 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
18668 {
18669 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
18670 break;
18671 }
18672 *p++ = '\0';
18673 /* Fall through. */
18674 case 1:
18675 *p++ = '\0';
18676 /* Fall through. */
18677 case 0:
18678 break;
18679 }
18680
18681 md_number_to_chars (p, opcode, size);
18682 fragp->fr_var = size;
18683 }
18684
18685 static long
18686 get_number (void)
18687 {
18688 int negative = 0;
18689 long val = 0;
18690
18691 if (*input_line_pointer == '-')
18692 {
18693 ++input_line_pointer;
18694 negative = 1;
18695 }
18696 if (!ISDIGIT (*input_line_pointer))
18697 as_bad (_("expected simple number"));
18698 if (input_line_pointer[0] == '0')
18699 {
18700 if (input_line_pointer[1] == 'x')
18701 {
18702 input_line_pointer += 2;
18703 while (ISXDIGIT (*input_line_pointer))
18704 {
18705 val <<= 4;
18706 val |= hex_value (*input_line_pointer++);
18707 }
18708 return negative ? -val : val;
18709 }
18710 else
18711 {
18712 ++input_line_pointer;
18713 while (ISDIGIT (*input_line_pointer))
18714 {
18715 val <<= 3;
18716 val |= *input_line_pointer++ - '0';
18717 }
18718 return negative ? -val : val;
18719 }
18720 }
18721 if (!ISDIGIT (*input_line_pointer))
18722 {
18723 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18724 *input_line_pointer, *input_line_pointer);
18725 as_warn (_("invalid number"));
18726 return -1;
18727 }
18728 while (ISDIGIT (*input_line_pointer))
18729 {
18730 val *= 10;
18731 val += *input_line_pointer++ - '0';
18732 }
18733 return negative ? -val : val;
18734 }
18735
18736 /* The .file directive; just like the usual .file directive, but there
18737 is an initial number which is the ECOFF file index. In the non-ECOFF
18738 case .file implies DWARF-2. */
18739
18740 static void
18741 s_mips_file (int x ATTRIBUTE_UNUSED)
18742 {
18743 static int first_file_directive = 0;
18744
18745 if (ECOFF_DEBUGGING)
18746 {
18747 get_number ();
18748 s_app_file (0);
18749 }
18750 else
18751 {
18752 char *filename;
18753
18754 filename = dwarf2_directive_file (0);
18755
18756 /* Versions of GCC up to 3.1 start files with a ".file"
18757 directive even for stabs output. Make sure that this
18758 ".file" is handled. Note that you need a version of GCC
18759 after 3.1 in order to support DWARF-2 on MIPS. */
18760 if (filename != NULL && ! first_file_directive)
18761 {
18762 (void) new_logical_line (filename, -1);
18763 s_app_file_string (filename, 0);
18764 }
18765 first_file_directive = 1;
18766 }
18767 }
18768
18769 /* The .loc directive, implying DWARF-2. */
18770
18771 static void
18772 s_mips_loc (int x ATTRIBUTE_UNUSED)
18773 {
18774 if (!ECOFF_DEBUGGING)
18775 dwarf2_directive_loc (0);
18776 }
18777
18778 /* The .end directive. */
18779
18780 static void
18781 s_mips_end (int x ATTRIBUTE_UNUSED)
18782 {
18783 symbolS *p;
18784
18785 /* Following functions need their own .frame and .cprestore directives. */
18786 mips_frame_reg_valid = 0;
18787 mips_cprestore_valid = 0;
18788
18789 if (!is_end_of_line[(unsigned char) *input_line_pointer])
18790 {
18791 p = get_symbol ();
18792 demand_empty_rest_of_line ();
18793 }
18794 else
18795 p = NULL;
18796
18797 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
18798 as_warn (_(".end not in text section"));
18799
18800 if (!cur_proc_ptr)
18801 {
18802 as_warn (_(".end directive without a preceding .ent directive"));
18803 demand_empty_rest_of_line ();
18804 return;
18805 }
18806
18807 if (p != NULL)
18808 {
18809 gas_assert (S_GET_NAME (p));
18810 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
18811 as_warn (_(".end symbol does not match .ent symbol"));
18812
18813 if (debug_type == DEBUG_STABS)
18814 stabs_generate_asm_endfunc (S_GET_NAME (p),
18815 S_GET_NAME (p));
18816 }
18817 else
18818 as_warn (_(".end directive missing or unknown symbol"));
18819
18820 /* Create an expression to calculate the size of the function. */
18821 if (p && cur_proc_ptr)
18822 {
18823 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
18824 expressionS *exp = XNEW (expressionS);
18825
18826 obj->size = exp;
18827 exp->X_op = O_subtract;
18828 exp->X_add_symbol = symbol_temp_new_now ();
18829 exp->X_op_symbol = p;
18830 exp->X_add_number = 0;
18831
18832 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
18833 }
18834
18835 /* Generate a .pdr section. */
18836 if (!ECOFF_DEBUGGING && mips_flag_pdr)
18837 {
18838 segT saved_seg = now_seg;
18839 subsegT saved_subseg = now_subseg;
18840 expressionS exp;
18841 char *fragp;
18842
18843 #ifdef md_flush_pending_output
18844 md_flush_pending_output ();
18845 #endif
18846
18847 gas_assert (pdr_seg);
18848 subseg_set (pdr_seg, 0);
18849
18850 /* Write the symbol. */
18851 exp.X_op = O_symbol;
18852 exp.X_add_symbol = p;
18853 exp.X_add_number = 0;
18854 emit_expr (&exp, 4);
18855
18856 fragp = frag_more (7 * 4);
18857
18858 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
18859 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
18860 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
18861 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
18862 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
18863 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
18864 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
18865
18866 subseg_set (saved_seg, saved_subseg);
18867 }
18868
18869 cur_proc_ptr = NULL;
18870 }
18871
18872 /* The .aent and .ent directives. */
18873
18874 static void
18875 s_mips_ent (int aent)
18876 {
18877 symbolS *symbolP;
18878
18879 symbolP = get_symbol ();
18880 if (*input_line_pointer == ',')
18881 ++input_line_pointer;
18882 SKIP_WHITESPACE ();
18883 if (ISDIGIT (*input_line_pointer)
18884 || *input_line_pointer == '-')
18885 get_number ();
18886
18887 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
18888 as_warn (_(".ent or .aent not in text section"));
18889
18890 if (!aent && cur_proc_ptr)
18891 as_warn (_("missing .end"));
18892
18893 if (!aent)
18894 {
18895 /* This function needs its own .frame and .cprestore directives. */
18896 mips_frame_reg_valid = 0;
18897 mips_cprestore_valid = 0;
18898
18899 cur_proc_ptr = &cur_proc;
18900 memset (cur_proc_ptr, '\0', sizeof (procS));
18901
18902 cur_proc_ptr->func_sym = symbolP;
18903
18904 ++numprocs;
18905
18906 if (debug_type == DEBUG_STABS)
18907 stabs_generate_asm_func (S_GET_NAME (symbolP),
18908 S_GET_NAME (symbolP));
18909 }
18910
18911 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
18912
18913 demand_empty_rest_of_line ();
18914 }
18915
18916 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
18917 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
18918 s_mips_frame is used so that we can set the PDR information correctly.
18919 We can't use the ecoff routines because they make reference to the ecoff
18920 symbol table (in the mdebug section). */
18921
18922 static void
18923 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
18924 {
18925 if (ECOFF_DEBUGGING)
18926 s_ignore (ignore);
18927 else
18928 {
18929 long val;
18930
18931 if (cur_proc_ptr == (procS *) NULL)
18932 {
18933 as_warn (_(".frame outside of .ent"));
18934 demand_empty_rest_of_line ();
18935 return;
18936 }
18937
18938 cur_proc_ptr->frame_reg = tc_get_register (1);
18939
18940 SKIP_WHITESPACE ();
18941 if (*input_line_pointer++ != ','
18942 || get_absolute_expression_and_terminator (&val) != ',')
18943 {
18944 as_warn (_("bad .frame directive"));
18945 --input_line_pointer;
18946 demand_empty_rest_of_line ();
18947 return;
18948 }
18949
18950 cur_proc_ptr->frame_offset = val;
18951 cur_proc_ptr->pc_reg = tc_get_register (0);
18952
18953 demand_empty_rest_of_line ();
18954 }
18955 }
18956
18957 /* The .fmask and .mask directives. If the mdebug section is present
18958 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
18959 embedded targets, s_mips_mask is used so that we can set the PDR
18960 information correctly. We can't use the ecoff routines because they
18961 make reference to the ecoff symbol table (in the mdebug section). */
18962
18963 static void
18964 s_mips_mask (int reg_type)
18965 {
18966 if (ECOFF_DEBUGGING)
18967 s_ignore (reg_type);
18968 else
18969 {
18970 long mask, off;
18971
18972 if (cur_proc_ptr == (procS *) NULL)
18973 {
18974 as_warn (_(".mask/.fmask outside of .ent"));
18975 demand_empty_rest_of_line ();
18976 return;
18977 }
18978
18979 if (get_absolute_expression_and_terminator (&mask) != ',')
18980 {
18981 as_warn (_("bad .mask/.fmask directive"));
18982 --input_line_pointer;
18983 demand_empty_rest_of_line ();
18984 return;
18985 }
18986
18987 off = get_absolute_expression ();
18988
18989 if (reg_type == 'F')
18990 {
18991 cur_proc_ptr->fpreg_mask = mask;
18992 cur_proc_ptr->fpreg_offset = off;
18993 }
18994 else
18995 {
18996 cur_proc_ptr->reg_mask = mask;
18997 cur_proc_ptr->reg_offset = off;
18998 }
18999
19000 demand_empty_rest_of_line ();
19001 }
19002 }
19003
19004 /* A table describing all the processors gas knows about. Names are
19005 matched in the order listed.
19006
19007 To ease comparison, please keep this table in the same order as
19008 gcc's mips_cpu_info_table[]. */
19009 static const struct mips_cpu_info mips_cpu_info_table[] =
19010 {
19011 /* Entries for generic ISAs */
19012 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19013 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19014 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19015 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19016 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19017 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19018 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19019 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19020 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
19021 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
19022 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19023 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
19024 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19025 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
19026 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
19027
19028 /* MIPS I */
19029 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19030 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19031 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
19032
19033 /* MIPS II */
19034 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
19035
19036 /* MIPS III */
19037 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19038 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19039 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19040 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19041 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19042 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19043 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19044 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19045 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19046 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19047 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19048 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19049 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
19050 /* ST Microelectronics Loongson 2E and 2F cores */
19051 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19052 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
19053
19054 /* MIPS IV */
19055 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19056 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19057 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19058 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19059 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19060 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19061 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19062 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19063 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19064 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19065 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19066 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19067 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19068 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19069 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
19070
19071 /* MIPS 32 */
19072 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19073 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19074 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19075 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19076
19077 /* MIPS 32 Release 2 */
19078 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19079 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19080 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19081 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19082 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19083 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19084 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19085 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19086 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19087 ISA_MIPS32R2, CPU_MIPS32R2 },
19088 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19089 ISA_MIPS32R2, CPU_MIPS32R2 },
19090 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19091 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19092 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19093 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19094 /* Deprecated forms of the above. */
19095 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19096 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19097 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
19098 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19099 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19100 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19101 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19102 /* Deprecated forms of the above. */
19103 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19104 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19105 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
19106 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19107 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19108 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19109 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19110 /* Deprecated forms of the above. */
19111 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19112 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19113 /* 34Kn is a 34kc without DSP. */
19114 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19115 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19116 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19117 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19118 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19119 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19120 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19121 /* Deprecated forms of the above. */
19122 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19123 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19124 /* 1004K cores are multiprocessor versions of the 34K. */
19125 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19126 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19127 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19128 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19129 /* interaptiv is the new name for 1004kf */
19130 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19131 /* M5100 family */
19132 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19133 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19134 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
19135 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
19136
19137 /* MIPS 64 */
19138 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19139 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19140 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19141 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19142
19143 /* Broadcom SB-1 CPU core */
19144 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19145 /* Broadcom SB-1A CPU core */
19146 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19147
19148 { "loongson3a", 0, 0, ISA_MIPS64R2, CPU_LOONGSON_3A },
19149
19150 /* MIPS 64 Release 2 */
19151
19152 /* Cavium Networks Octeon CPU core */
19153 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19154 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19155 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
19156 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
19157
19158 /* RMI Xlr */
19159 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
19160
19161 /* Broadcom XLP.
19162 XLP is mostly like XLR, with the prominent exception that it is
19163 MIPS64R2 rather than MIPS64. */
19164 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
19165
19166 /* MIPS 64 Release 6 */
19167 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
19168 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
19169
19170 /* End marker */
19171 { NULL, 0, 0, 0, 0 }
19172 };
19173
19174
19175 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19176 with a final "000" replaced by "k". Ignore case.
19177
19178 Note: this function is shared between GCC and GAS. */
19179
19180 static bfd_boolean
19181 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19182 {
19183 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19184 given++, canonical++;
19185
19186 return ((*given == 0 && *canonical == 0)
19187 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19188 }
19189
19190
19191 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19192 CPU name. We've traditionally allowed a lot of variation here.
19193
19194 Note: this function is shared between GCC and GAS. */
19195
19196 static bfd_boolean
19197 mips_matching_cpu_name_p (const char *canonical, const char *given)
19198 {
19199 /* First see if the name matches exactly, or with a final "000"
19200 turned into "k". */
19201 if (mips_strict_matching_cpu_name_p (canonical, given))
19202 return TRUE;
19203
19204 /* If not, try comparing based on numerical designation alone.
19205 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19206 if (TOLOWER (*given) == 'r')
19207 given++;
19208 if (!ISDIGIT (*given))
19209 return FALSE;
19210
19211 /* Skip over some well-known prefixes in the canonical name,
19212 hoping to find a number there too. */
19213 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19214 canonical += 2;
19215 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19216 canonical += 2;
19217 else if (TOLOWER (canonical[0]) == 'r')
19218 canonical += 1;
19219
19220 return mips_strict_matching_cpu_name_p (canonical, given);
19221 }
19222
19223
19224 /* Parse an option that takes the name of a processor as its argument.
19225 OPTION is the name of the option and CPU_STRING is the argument.
19226 Return the corresponding processor enumeration if the CPU_STRING is
19227 recognized, otherwise report an error and return null.
19228
19229 A similar function exists in GCC. */
19230
19231 static const struct mips_cpu_info *
19232 mips_parse_cpu (const char *option, const char *cpu_string)
19233 {
19234 const struct mips_cpu_info *p;
19235
19236 /* 'from-abi' selects the most compatible architecture for the given
19237 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19238 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19239 version. Look first at the -mgp options, if given, otherwise base
19240 the choice on MIPS_DEFAULT_64BIT.
19241
19242 Treat NO_ABI like the EABIs. One reason to do this is that the
19243 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19244 architecture. This code picks MIPS I for 'mips' and MIPS III for
19245 'mips64', just as we did in the days before 'from-abi'. */
19246 if (strcasecmp (cpu_string, "from-abi") == 0)
19247 {
19248 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19249 return mips_cpu_info_from_isa (ISA_MIPS1);
19250
19251 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19252 return mips_cpu_info_from_isa (ISA_MIPS3);
19253
19254 if (file_mips_opts.gp >= 0)
19255 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
19256 ? ISA_MIPS1 : ISA_MIPS3);
19257
19258 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19259 ? ISA_MIPS3
19260 : ISA_MIPS1);
19261 }
19262
19263 /* 'default' has traditionally been a no-op. Probably not very useful. */
19264 if (strcasecmp (cpu_string, "default") == 0)
19265 return 0;
19266
19267 for (p = mips_cpu_info_table; p->name != 0; p++)
19268 if (mips_matching_cpu_name_p (p->name, cpu_string))
19269 return p;
19270
19271 as_bad (_("bad value (%s) for %s"), cpu_string, option);
19272 return 0;
19273 }
19274
19275 /* Return the canonical processor information for ISA (a member of the
19276 ISA_MIPS* enumeration). */
19277
19278 static const struct mips_cpu_info *
19279 mips_cpu_info_from_isa (int isa)
19280 {
19281 int i;
19282
19283 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19284 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19285 && isa == mips_cpu_info_table[i].isa)
19286 return (&mips_cpu_info_table[i]);
19287
19288 return NULL;
19289 }
19290
19291 static const struct mips_cpu_info *
19292 mips_cpu_info_from_arch (int arch)
19293 {
19294 int i;
19295
19296 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19297 if (arch == mips_cpu_info_table[i].cpu)
19298 return (&mips_cpu_info_table[i]);
19299
19300 return NULL;
19301 }
19302 \f
19303 static void
19304 show (FILE *stream, const char *string, int *col_p, int *first_p)
19305 {
19306 if (*first_p)
19307 {
19308 fprintf (stream, "%24s", "");
19309 *col_p = 24;
19310 }
19311 else
19312 {
19313 fprintf (stream, ", ");
19314 *col_p += 2;
19315 }
19316
19317 if (*col_p + strlen (string) > 72)
19318 {
19319 fprintf (stream, "\n%24s", "");
19320 *col_p = 24;
19321 }
19322
19323 fprintf (stream, "%s", string);
19324 *col_p += strlen (string);
19325
19326 *first_p = 0;
19327 }
19328
19329 void
19330 md_show_usage (FILE *stream)
19331 {
19332 int column, first;
19333 size_t i;
19334
19335 fprintf (stream, _("\
19336 MIPS options:\n\
19337 -EB generate big endian output\n\
19338 -EL generate little endian output\n\
19339 -g, -g2 do not remove unneeded NOPs or swap branches\n\
19340 -G NUM allow referencing objects up to NUM bytes\n\
19341 implicitly with the gp register [default 8]\n"));
19342 fprintf (stream, _("\
19343 -mips1 generate MIPS ISA I instructions\n\
19344 -mips2 generate MIPS ISA II instructions\n\
19345 -mips3 generate MIPS ISA III instructions\n\
19346 -mips4 generate MIPS ISA IV instructions\n\
19347 -mips5 generate MIPS ISA V instructions\n\
19348 -mips32 generate MIPS32 ISA instructions\n\
19349 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
19350 -mips32r3 generate MIPS32 release 3 ISA instructions\n\
19351 -mips32r5 generate MIPS32 release 5 ISA instructions\n\
19352 -mips32r6 generate MIPS32 release 6 ISA instructions\n\
19353 -mips64 generate MIPS64 ISA instructions\n\
19354 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
19355 -mips64r3 generate MIPS64 release 3 ISA instructions\n\
19356 -mips64r5 generate MIPS64 release 5 ISA instructions\n\
19357 -mips64r6 generate MIPS64 release 6 ISA instructions\n\
19358 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19359
19360 first = 1;
19361
19362 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19363 show (stream, mips_cpu_info_table[i].name, &column, &first);
19364 show (stream, "from-abi", &column, &first);
19365 fputc ('\n', stream);
19366
19367 fprintf (stream, _("\
19368 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19369 -no-mCPU don't generate code specific to CPU.\n\
19370 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19371
19372 first = 1;
19373
19374 show (stream, "3900", &column, &first);
19375 show (stream, "4010", &column, &first);
19376 show (stream, "4100", &column, &first);
19377 show (stream, "4650", &column, &first);
19378 fputc ('\n', stream);
19379
19380 fprintf (stream, _("\
19381 -mips16 generate mips16 instructions\n\
19382 -no-mips16 do not generate mips16 instructions\n"));
19383 fprintf (stream, _("\
19384 -mmicromips generate microMIPS instructions\n\
19385 -mno-micromips do not generate microMIPS instructions\n"));
19386 fprintf (stream, _("\
19387 -msmartmips generate smartmips instructions\n\
19388 -mno-smartmips do not generate smartmips instructions\n"));
19389 fprintf (stream, _("\
19390 -mdsp generate DSP instructions\n\
19391 -mno-dsp do not generate DSP instructions\n"));
19392 fprintf (stream, _("\
19393 -mdspr2 generate DSP R2 instructions\n\
19394 -mno-dspr2 do not generate DSP R2 instructions\n"));
19395 fprintf (stream, _("\
19396 -mdspr3 generate DSP R3 instructions\n\
19397 -mno-dspr3 do not generate DSP R3 instructions\n"));
19398 fprintf (stream, _("\
19399 -mmt generate MT instructions\n\
19400 -mno-mt do not generate MT instructions\n"));
19401 fprintf (stream, _("\
19402 -mmcu generate MCU instructions\n\
19403 -mno-mcu do not generate MCU instructions\n"));
19404 fprintf (stream, _("\
19405 -mmsa generate MSA instructions\n\
19406 -mno-msa do not generate MSA instructions\n"));
19407 fprintf (stream, _("\
19408 -mxpa generate eXtended Physical Address (XPA) instructions\n\
19409 -mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
19410 fprintf (stream, _("\
19411 -mvirt generate Virtualization instructions\n\
19412 -mno-virt do not generate Virtualization instructions\n"));
19413 fprintf (stream, _("\
19414 -minsn32 only generate 32-bit microMIPS instructions\n\
19415 -mno-insn32 generate all microMIPS instructions\n"));
19416 fprintf (stream, _("\
19417 -mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19418 -mfix-loongson2f-nop work around Loongson2F NOP errata\n\
19419 -mfix-vr4120 work around certain VR4120 errata\n\
19420 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
19421 -mfix-24k insert a nop after ERET and DERET instructions\n\
19422 -mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
19423 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19424 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
19425 -msym32 assume all symbols have 32-bit values\n\
19426 -O0 remove unneeded NOPs, do not swap branches\n\
19427 -O remove unneeded NOPs and swap branches\n\
19428 --trap, --no-break trap exception on div by 0 and mult overflow\n\
19429 --break, --no-trap break exception on div by 0 and mult overflow\n"));
19430 fprintf (stream, _("\
19431 -mhard-float allow floating-point instructions\n\
19432 -msoft-float do not allow floating-point instructions\n\
19433 -msingle-float only allow 32-bit floating-point operations\n\
19434 -mdouble-float allow 32-bit and 64-bit floating-point operations\n\
19435 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
19436 --[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
19437 -mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
19438
19439 first = 1;
19440
19441 show (stream, "legacy", &column, &first);
19442 show (stream, "2008", &column, &first);
19443
19444 fputc ('\n', stream);
19445
19446 fprintf (stream, _("\
19447 -KPIC, -call_shared generate SVR4 position independent code\n\
19448 -call_nonpic generate non-PIC code that can operate with DSOs\n\
19449 -mvxworks-pic generate VxWorks position independent code\n\
19450 -non_shared do not generate code that can operate with DSOs\n\
19451 -xgot assume a 32 bit GOT\n\
19452 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
19453 -mshared, -mno-shared disable/enable .cpload optimization for\n\
19454 position dependent (non shared) code\n\
19455 -mabi=ABI create ABI conformant object file for:\n"));
19456
19457 first = 1;
19458
19459 show (stream, "32", &column, &first);
19460 show (stream, "o64", &column, &first);
19461 show (stream, "n32", &column, &first);
19462 show (stream, "64", &column, &first);
19463 show (stream, "eabi", &column, &first);
19464
19465 fputc ('\n', stream);
19466
19467 fprintf (stream, _("\
19468 -32 create o32 ABI object file (default)\n\
19469 -n32 create n32 ABI object file\n\
19470 -64 create 64 ABI object file\n"));
19471 }
19472
19473 #ifdef TE_IRIX
19474 enum dwarf2_format
19475 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
19476 {
19477 if (HAVE_64BIT_SYMBOLS)
19478 return dwarf2_format_64bit_irix;
19479 else
19480 return dwarf2_format_32bit;
19481 }
19482 #endif
19483
19484 int
19485 mips_dwarf2_addr_size (void)
19486 {
19487 if (HAVE_64BIT_OBJECTS)
19488 return 8;
19489 else
19490 return 4;
19491 }
19492
19493 /* Standard calling conventions leave the CFA at SP on entry. */
19494 void
19495 mips_cfi_frame_initial_instructions (void)
19496 {
19497 cfi_add_CFA_def_cfa_register (SP);
19498 }
19499
19500 int
19501 tc_mips_regname_to_dw2regnum (char *regname)
19502 {
19503 unsigned int regnum = -1;
19504 unsigned int reg;
19505
19506 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19507 regnum = reg;
19508
19509 return regnum;
19510 }
19511
19512 /* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
19513 Given a symbolic attribute NAME, return the proper integer value.
19514 Returns -1 if the attribute is not known. */
19515
19516 int
19517 mips_convert_symbolic_attribute (const char *name)
19518 {
19519 static const struct
19520 {
19521 const char * name;
19522 const int tag;
19523 }
19524 attribute_table[] =
19525 {
19526 #define T(tag) {#tag, tag}
19527 T (Tag_GNU_MIPS_ABI_FP),
19528 T (Tag_GNU_MIPS_ABI_MSA),
19529 #undef T
19530 };
19531 unsigned int i;
19532
19533 if (name == NULL)
19534 return -1;
19535
19536 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
19537 if (streq (name, attribute_table[i].name))
19538 return attribute_table[i].tag;
19539
19540 return -1;
19541 }
19542
19543 void
19544 md_mips_end (void)
19545 {
19546 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
19547
19548 mips_emit_delays ();
19549 if (cur_proc_ptr)
19550 as_warn (_("missing .end at end of assembly"));
19551
19552 /* Just in case no code was emitted, do the consistency check. */
19553 file_mips_check_options ();
19554
19555 /* Set a floating-point ABI if the user did not. */
19556 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
19557 {
19558 /* Perform consistency checks on the floating-point ABI. */
19559 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19560 Tag_GNU_MIPS_ABI_FP);
19561 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
19562 check_fpabi (fpabi);
19563 }
19564 else
19565 {
19566 /* Soft-float gets precedence over single-float, the two options should
19567 not be used together so this should not matter. */
19568 if (file_mips_opts.soft_float == 1)
19569 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
19570 /* Single-float gets precedence over all double_float cases. */
19571 else if (file_mips_opts.single_float == 1)
19572 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
19573 else
19574 {
19575 switch (file_mips_opts.fp)
19576 {
19577 case 32:
19578 if (file_mips_opts.gp == 32)
19579 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
19580 break;
19581 case 0:
19582 fpabi = Val_GNU_MIPS_ABI_FP_XX;
19583 break;
19584 case 64:
19585 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
19586 fpabi = Val_GNU_MIPS_ABI_FP_64A;
19587 else if (file_mips_opts.gp == 32)
19588 fpabi = Val_GNU_MIPS_ABI_FP_64;
19589 else
19590 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
19591 break;
19592 }
19593 }
19594
19595 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19596 Tag_GNU_MIPS_ABI_FP, fpabi);
19597 }
19598 }
19599
19600 /* Returns the relocation type required for a particular CFI encoding. */
19601
19602 bfd_reloc_code_real_type
19603 mips_cfi_reloc_for_encoding (int encoding)
19604 {
19605 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
19606 return BFD_RELOC_32_PCREL;
19607 else return BFD_RELOC_NONE;
19608 }
This page took 0.705452 seconds and 5 git commands to generate.