Add support for intel SERIALIZE instruction
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright (C) 1993-2020 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 name if this is an label. */
145 char label[16];
146
147 /* The target label name if this is an branch. */
148 char target[16];
149
150 /* The frag that contains the instruction. */
151 struct frag *frag;
152
153 /* The offset into FRAG of the first instruction byte. */
154 long where;
155
156 /* The relocs associated with the instruction, if any. */
157 fixS *fixp[3];
158
159 /* True if this entry cannot be moved from its current position. */
160 unsigned int fixed_p : 1;
161
162 /* True if this instruction occurred in a .set noreorder block. */
163 unsigned int noreorder_p : 1;
164
165 /* True for mips16 instructions that jump to an absolute address. */
166 unsigned int mips16_absolute_jump_p : 1;
167
168 /* True if this instruction is complete. */
169 unsigned int complete_p : 1;
170
171 /* True if this instruction is cleared from history by unconditional
172 branch. */
173 unsigned int cleared_p : 1;
174 };
175
176 /* The ABI to use. */
177 enum mips_abi_level
178 {
179 NO_ABI = 0,
180 O32_ABI,
181 O64_ABI,
182 N32_ABI,
183 N64_ABI,
184 EABI_ABI
185 };
186
187 /* MIPS ABI we are using for this output file. */
188 static enum mips_abi_level mips_abi = NO_ABI;
189
190 /* Whether or not we have code that can call pic code. */
191 int mips_abicalls = FALSE;
192
193 /* Whether or not we have code which can be put into a shared
194 library. */
195 static bfd_boolean mips_in_shared = TRUE;
196
197 /* This is the set of options which may be modified by the .set
198 pseudo-op. We use a struct so that .set push and .set pop are more
199 reliable. */
200
201 struct mips_set_options
202 {
203 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
204 if it has not been initialized. Changed by `.set mipsN', and the
205 -mipsN command line option, and the default CPU. */
206 int isa;
207 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
208 <asename>', by command line options, and based on the default
209 architecture. */
210 int ase;
211 /* Whether we are assembling for the mips16 processor. 0 if we are
212 not, 1 if we are, and -1 if the value has not been initialized.
213 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
214 -nomips16 command line options, and the default CPU. */
215 int mips16;
216 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
217 1 if we are, and -1 if the value has not been initialized. Changed
218 by `.set micromips' and `.set nomicromips', and the -mmicromips
219 and -mno-micromips command line options, and the default CPU. */
220 int micromips;
221 /* Non-zero if we should not reorder instructions. Changed by `.set
222 reorder' and `.set noreorder'. */
223 int noreorder;
224 /* Non-zero if we should not permit the register designated "assembler
225 temporary" to be used in instructions. The value is the register
226 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
227 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
228 unsigned int at;
229 /* Non-zero if we should warn when a macro instruction expands into
230 more than one machine instruction. Changed by `.set nomacro' and
231 `.set macro'. */
232 int warn_about_macros;
233 /* Non-zero if we should not move instructions. Changed by `.set
234 move', `.set volatile', `.set nomove', and `.set novolatile'. */
235 int nomove;
236 /* Non-zero if we should not optimize branches by moving the target
237 of the branch into the delay slot. Actually, we don't perform
238 this optimization anyhow. Changed by `.set bopt' and `.set
239 nobopt'. */
240 int nobopt;
241 /* Non-zero if we should not autoextend mips16 instructions.
242 Changed by `.set autoextend' and `.set noautoextend'. */
243 int noautoextend;
244 /* True if we should only emit 32-bit microMIPS instructions.
245 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
246 and -mno-insn32 command line options. */
247 bfd_boolean insn32;
248 /* Restrict general purpose registers and floating point registers
249 to 32 bit. This is initially determined when -mgp32 or -mfp32
250 is passed but can changed if the assembler code uses .set mipsN. */
251 int gp;
252 int fp;
253 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
254 command line option, and the default CPU. */
255 int arch;
256 /* True if ".set sym32" is in effect. */
257 bfd_boolean sym32;
258 /* True if floating-point operations are not allowed. Changed by .set
259 softfloat or .set hardfloat, by command line options -msoft-float or
260 -mhard-float. The default is false. */
261 bfd_boolean soft_float;
262
263 /* True if only single-precision floating-point operations are allowed.
264 Changed by .set singlefloat or .set doublefloat, command-line options
265 -msingle-float or -mdouble-float. The default is false. */
266 bfd_boolean single_float;
267
268 /* 1 if single-precision operations on odd-numbered registers are
269 allowed. */
270 int oddspreg;
271
272 /* The set of ASEs that should be enabled for the user specified
273 architecture. This cannot be inferred from 'arch' for all cores
274 as processors only have a unique 'arch' if they add architecture
275 specific instructions (UDI). */
276 int init_ase;
277 };
278
279 /* Specifies whether module level options have been checked yet. */
280 static bfd_boolean file_mips_opts_checked = FALSE;
281
282 /* Do we support nan2008? 0 if we don't, 1 if we do, and -1 if the
283 value has not been initialized. Changed by `.nan legacy' and
284 `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
285 options, and the default CPU. */
286 static int mips_nan2008 = -1;
287
288 /* This is the struct we use to hold the module level set of options.
289 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
290 fp fields to -1 to indicate that they have not been initialized. */
291
292 static struct mips_set_options file_mips_opts =
293 {
294 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
295 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
296 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
297 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
298 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1,
299 /* init_ase */ 0
300 };
301
302 /* This is similar to file_mips_opts, but for the current set of options. */
303
304 static struct mips_set_options mips_opts =
305 {
306 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
307 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
308 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
309 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
310 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1,
311 /* init_ase */ 0
312 };
313
314 /* Which bits of file_ase were explicitly set or cleared by ASE options. */
315 static unsigned int file_ase_explicit;
316
317 /* These variables are filled in with the masks of registers used.
318 The object format code reads them and puts them in the appropriate
319 place. */
320 unsigned long mips_gprmask;
321 unsigned long mips_cprmask[4];
322
323 /* True if any MIPS16 code was produced. */
324 static int file_ase_mips16;
325
326 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
327 || mips_opts.isa == ISA_MIPS32R2 \
328 || mips_opts.isa == ISA_MIPS32R3 \
329 || mips_opts.isa == ISA_MIPS32R5 \
330 || mips_opts.isa == ISA_MIPS64 \
331 || mips_opts.isa == ISA_MIPS64R2 \
332 || mips_opts.isa == ISA_MIPS64R3 \
333 || mips_opts.isa == ISA_MIPS64R5)
334
335 /* True if any microMIPS code was produced. */
336 static int file_ase_micromips;
337
338 /* True if we want to create R_MIPS_JALR for jalr $25. */
339 #ifdef TE_IRIX
340 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
341 #else
342 /* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
343 because there's no place for any addend, the only acceptable
344 expression is a bare symbol. */
345 #define MIPS_JALR_HINT_P(EXPR) \
346 (!HAVE_IN_PLACE_ADDENDS \
347 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
348 #endif
349
350 /* The argument of the -march= flag. The architecture we are assembling. */
351 static const char *mips_arch_string;
352
353 /* The argument of the -mtune= flag. The architecture for which we
354 are optimizing. */
355 static int mips_tune = CPU_UNKNOWN;
356 static const char *mips_tune_string;
357
358 /* True when generating 32-bit code for a 64-bit processor. */
359 static int mips_32bitmode = 0;
360
361 /* True if the given ABI requires 32-bit registers. */
362 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
363
364 /* Likewise 64-bit registers. */
365 #define ABI_NEEDS_64BIT_REGS(ABI) \
366 ((ABI) == N32_ABI \
367 || (ABI) == N64_ABI \
368 || (ABI) == O64_ABI)
369
370 #define ISA_IS_R6(ISA) \
371 ((ISA) == ISA_MIPS32R6 \
372 || (ISA) == ISA_MIPS64R6)
373
374 /* Return true if ISA supports 64 bit wide gp registers. */
375 #define ISA_HAS_64BIT_REGS(ISA) \
376 ((ISA) == ISA_MIPS3 \
377 || (ISA) == ISA_MIPS4 \
378 || (ISA) == ISA_MIPS5 \
379 || (ISA) == ISA_MIPS64 \
380 || (ISA) == ISA_MIPS64R2 \
381 || (ISA) == ISA_MIPS64R3 \
382 || (ISA) == ISA_MIPS64R5 \
383 || (ISA) == ISA_MIPS64R6)
384
385 /* Return true if ISA supports 64 bit wide float registers. */
386 #define ISA_HAS_64BIT_FPRS(ISA) \
387 ((ISA) == ISA_MIPS3 \
388 || (ISA) == ISA_MIPS4 \
389 || (ISA) == ISA_MIPS5 \
390 || (ISA) == ISA_MIPS32R2 \
391 || (ISA) == ISA_MIPS32R3 \
392 || (ISA) == ISA_MIPS32R5 \
393 || (ISA) == ISA_MIPS32R6 \
394 || (ISA) == ISA_MIPS64 \
395 || (ISA) == ISA_MIPS64R2 \
396 || (ISA) == ISA_MIPS64R3 \
397 || (ISA) == ISA_MIPS64R5 \
398 || (ISA) == ISA_MIPS64R6)
399
400 /* Return true if ISA supports 64-bit right rotate (dror et al.)
401 instructions. */
402 #define ISA_HAS_DROR(ISA) \
403 ((ISA) == ISA_MIPS64R2 \
404 || (ISA) == ISA_MIPS64R3 \
405 || (ISA) == ISA_MIPS64R5 \
406 || (ISA) == ISA_MIPS64R6 \
407 || (mips_opts.micromips \
408 && ISA_HAS_64BIT_REGS (ISA)) \
409 )
410
411 /* Return true if ISA supports 32-bit right rotate (ror et al.)
412 instructions. */
413 #define ISA_HAS_ROR(ISA) \
414 ((ISA) == ISA_MIPS32R2 \
415 || (ISA) == ISA_MIPS32R3 \
416 || (ISA) == ISA_MIPS32R5 \
417 || (ISA) == ISA_MIPS32R6 \
418 || (ISA) == ISA_MIPS64R2 \
419 || (ISA) == ISA_MIPS64R3 \
420 || (ISA) == ISA_MIPS64R5 \
421 || (ISA) == ISA_MIPS64R6 \
422 || (mips_opts.ase & ASE_SMARTMIPS) \
423 || mips_opts.micromips \
424 )
425
426 /* Return true if ISA supports single-precision floats in odd registers. */
427 #define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
428 (((ISA) == ISA_MIPS32 \
429 || (ISA) == ISA_MIPS32R2 \
430 || (ISA) == ISA_MIPS32R3 \
431 || (ISA) == ISA_MIPS32R5 \
432 || (ISA) == ISA_MIPS32R6 \
433 || (ISA) == ISA_MIPS64 \
434 || (ISA) == ISA_MIPS64R2 \
435 || (ISA) == ISA_MIPS64R3 \
436 || (ISA) == ISA_MIPS64R5 \
437 || (ISA) == ISA_MIPS64R6 \
438 || (CPU) == CPU_R5900) \
439 && ((CPU) != CPU_GS464 \
440 || (CPU) != CPU_GS464E \
441 || (CPU) != CPU_GS264E))
442
443 /* Return true if ISA supports move to/from high part of a 64-bit
444 floating-point register. */
445 #define ISA_HAS_MXHC1(ISA) \
446 ((ISA) == ISA_MIPS32R2 \
447 || (ISA) == ISA_MIPS32R3 \
448 || (ISA) == ISA_MIPS32R5 \
449 || (ISA) == ISA_MIPS32R6 \
450 || (ISA) == ISA_MIPS64R2 \
451 || (ISA) == ISA_MIPS64R3 \
452 || (ISA) == ISA_MIPS64R5 \
453 || (ISA) == ISA_MIPS64R6)
454
455 /* Return true if ISA supports legacy NAN. */
456 #define ISA_HAS_LEGACY_NAN(ISA) \
457 ((ISA) == ISA_MIPS1 \
458 || (ISA) == ISA_MIPS2 \
459 || (ISA) == ISA_MIPS3 \
460 || (ISA) == ISA_MIPS4 \
461 || (ISA) == ISA_MIPS5 \
462 || (ISA) == ISA_MIPS32 \
463 || (ISA) == ISA_MIPS32R2 \
464 || (ISA) == ISA_MIPS32R3 \
465 || (ISA) == ISA_MIPS32R5 \
466 || (ISA) == ISA_MIPS64 \
467 || (ISA) == ISA_MIPS64R2 \
468 || (ISA) == ISA_MIPS64R3 \
469 || (ISA) == ISA_MIPS64R5)
470
471 #define GPR_SIZE \
472 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
473 ? 32 \
474 : mips_opts.gp)
475
476 #define FPR_SIZE \
477 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
478 ? 32 \
479 : mips_opts.fp)
480
481 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
482
483 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
484
485 /* True if relocations are stored in-place. */
486 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
487
488 /* The ABI-derived address size. */
489 #define HAVE_64BIT_ADDRESSES \
490 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
491 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
492
493 /* The size of symbolic constants (i.e., expressions of the form
494 "SYMBOL" or "SYMBOL + OFFSET"). */
495 #define HAVE_32BIT_SYMBOLS \
496 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
497 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
498
499 /* Addresses are loaded in different ways, depending on the address size
500 in use. The n32 ABI Documentation also mandates the use of additions
501 with overflow checking, but existing implementations don't follow it. */
502 #define ADDRESS_ADD_INSN \
503 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
504
505 #define ADDRESS_ADDI_INSN \
506 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
507
508 #define ADDRESS_LOAD_INSN \
509 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
510
511 #define ADDRESS_STORE_INSN \
512 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
513
514 /* Return true if the given CPU supports the MIPS16 ASE. */
515 #define CPU_HAS_MIPS16(cpu) \
516 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
517 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
518
519 /* Return true if the given CPU supports the microMIPS ASE. */
520 #define CPU_HAS_MICROMIPS(cpu) 0
521
522 /* True if CPU has a dror instruction. */
523 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
524
525 /* True if CPU has a ror instruction. */
526 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
527
528 /* True if CPU is in the Octeon family. */
529 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
530 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
531
532 /* True if CPU has seq/sne and seqi/snei instructions. */
533 #define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
534
535 /* True, if CPU has support for ldc1 and sdc1. */
536 #define CPU_HAS_LDC1_SDC1(CPU) \
537 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
538
539 /* True if mflo and mfhi can be immediately followed by instructions
540 which write to the HI and LO registers.
541
542 According to MIPS specifications, MIPS ISAs I, II, and III need
543 (at least) two instructions between the reads of HI/LO and
544 instructions which write them, and later ISAs do not. Contradicting
545 the MIPS specifications, some MIPS IV processor user manuals (e.g.
546 the UM for the NEC Vr5000) document needing the instructions between
547 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
548 MIPS64 and later ISAs to have the interlocks, plus any specific
549 earlier-ISA CPUs for which CPU documentation declares that the
550 instructions are really interlocked. */
551 #define hilo_interlocks \
552 (mips_opts.isa == ISA_MIPS32 \
553 || mips_opts.isa == ISA_MIPS32R2 \
554 || mips_opts.isa == ISA_MIPS32R3 \
555 || mips_opts.isa == ISA_MIPS32R5 \
556 || mips_opts.isa == ISA_MIPS32R6 \
557 || mips_opts.isa == ISA_MIPS64 \
558 || mips_opts.isa == ISA_MIPS64R2 \
559 || mips_opts.isa == ISA_MIPS64R3 \
560 || mips_opts.isa == ISA_MIPS64R5 \
561 || mips_opts.isa == ISA_MIPS64R6 \
562 || mips_opts.arch == CPU_R4010 \
563 || mips_opts.arch == CPU_R5900 \
564 || mips_opts.arch == CPU_R10000 \
565 || mips_opts.arch == CPU_R12000 \
566 || mips_opts.arch == CPU_R14000 \
567 || mips_opts.arch == CPU_R16000 \
568 || mips_opts.arch == CPU_RM7000 \
569 || mips_opts.arch == CPU_VR5500 \
570 || mips_opts.micromips \
571 )
572
573 /* Whether the processor uses hardware interlocks to protect reads
574 from the GPRs after they are loaded from memory, and thus does not
575 require nops to be inserted. This applies to instructions marked
576 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
577 level I and microMIPS mode instructions are always interlocked. */
578 #define gpr_interlocks \
579 (mips_opts.isa != ISA_MIPS1 \
580 || mips_opts.arch == CPU_R3900 \
581 || mips_opts.arch == CPU_R5900 \
582 || mips_opts.micromips \
583 )
584
585 /* Whether the processor uses hardware interlocks to avoid delays
586 required by coprocessor instructions, and thus does not require
587 nops to be inserted. This applies to instructions marked
588 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
589 instructions marked INSN_WRITE_COND_CODE and ones marked
590 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
591 levels I, II, and III and microMIPS mode instructions are always
592 interlocked. */
593 /* Itbl support may require additional care here. */
594 #define cop_interlocks \
595 ((mips_opts.isa != ISA_MIPS1 \
596 && mips_opts.isa != ISA_MIPS2 \
597 && mips_opts.isa != ISA_MIPS3) \
598 || mips_opts.arch == CPU_R4300 \
599 || mips_opts.micromips \
600 )
601
602 /* Whether the processor uses hardware interlocks to protect reads
603 from coprocessor registers after they are loaded from memory, and
604 thus does not require nops to be inserted. This applies to
605 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
606 requires at MIPS ISA level I and microMIPS mode instructions are
607 always interlocked. */
608 #define cop_mem_interlocks \
609 (mips_opts.isa != ISA_MIPS1 \
610 || mips_opts.micromips \
611 )
612
613 /* Is this a mfhi or mflo instruction? */
614 #define MF_HILO_INSN(PINFO) \
615 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
616
617 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
618 has been selected. This implies, in particular, that addresses of text
619 labels have their LSB set. */
620 #define HAVE_CODE_COMPRESSION \
621 ((mips_opts.mips16 | mips_opts.micromips) != 0)
622
623 /* The minimum and maximum signed values that can be stored in a GPR. */
624 #define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
625 #define GPR_SMIN (-GPR_SMAX - 1)
626
627 /* MIPS PIC level. */
628
629 enum mips_pic_level mips_pic;
630
631 /* 1 if we should generate 32 bit offsets from the $gp register in
632 SVR4_PIC mode. Currently has no meaning in other modes. */
633 static int mips_big_got = 0;
634
635 /* 1 if trap instructions should used for overflow rather than break
636 instructions. */
637 static int mips_trap = 0;
638
639 /* 1 if double width floating point constants should not be constructed
640 by assembling two single width halves into two single width floating
641 point registers which just happen to alias the double width destination
642 register. On some architectures this aliasing can be disabled by a bit
643 in the status register, and the setting of this bit cannot be determined
644 automatically at assemble time. */
645 static int mips_disable_float_construction;
646
647 /* Non-zero if any .set noreorder directives were used. */
648
649 static int mips_any_noreorder;
650
651 /* Non-zero if nops should be inserted when the register referenced in
652 an mfhi/mflo instruction is read in the next two instructions. */
653 static int mips_7000_hilo_fix;
654
655 /* The size of objects in the small data section. */
656 static unsigned int g_switch_value = 8;
657 /* Whether the -G option was used. */
658 static int g_switch_seen = 0;
659
660 #define N_RMASK 0xc4
661 #define N_VFP 0xd4
662
663 /* If we can determine in advance that GP optimization won't be
664 possible, we can skip the relaxation stuff that tries to produce
665 GP-relative references. This makes delay slot optimization work
666 better.
667
668 This function can only provide a guess, but it seems to work for
669 gcc output. It needs to guess right for gcc, otherwise gcc
670 will put what it thinks is a GP-relative instruction in a branch
671 delay slot.
672
673 I don't know if a fix is needed for the SVR4_PIC mode. I've only
674 fixed it for the non-PIC mode. KR 95/04/07 */
675 static int nopic_need_relax (symbolS *, int);
676
677 /* Handle of the OPCODE hash table. */
678 static struct hash_control *op_hash = NULL;
679
680 /* The opcode hash table we use for the mips16. */
681 static struct hash_control *mips16_op_hash = NULL;
682
683 /* The opcode hash table we use for the microMIPS ASE. */
684 static struct hash_control *micromips_op_hash = NULL;
685
686 /* This array holds the chars that always start a comment. If the
687 pre-processor is disabled, these aren't very useful. */
688 const char comment_chars[] = "#";
689
690 /* This array holds the chars that only start a comment at the beginning of
691 a line. If the line seems to have the form '# 123 filename'
692 .line and .file directives will appear in the pre-processed output. */
693 /* Note that input_file.c hand checks for '#' at the beginning of the
694 first line of the input file. This is because the compiler outputs
695 #NO_APP at the beginning of its output. */
696 /* Also note that C style comments are always supported. */
697 const char line_comment_chars[] = "#";
698
699 /* This array holds machine specific line separator characters. */
700 const char line_separator_chars[] = ";";
701
702 /* Chars that can be used to separate mant from exp in floating point nums. */
703 const char EXP_CHARS[] = "eE";
704
705 /* Chars that mean this number is a floating point constant.
706 As in 0f12.456
707 or 0d1.2345e12. */
708 const char FLT_CHARS[] = "rRsSfFdDxXpP";
709
710 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
711 changed in read.c . Ideally it shouldn't have to know about it at all,
712 but nothing is ideal around here. */
713
714 /* Types of printf format used for instruction-related error messages.
715 "I" means int ("%d") and "S" means string ("%s"). */
716 enum mips_insn_error_format
717 {
718 ERR_FMT_PLAIN,
719 ERR_FMT_I,
720 ERR_FMT_SS,
721 };
722
723 /* Information about an error that was found while assembling the current
724 instruction. */
725 struct mips_insn_error
726 {
727 /* We sometimes need to match an instruction against more than one
728 opcode table entry. Errors found during this matching are reported
729 against a particular syntactic argument rather than against the
730 instruction as a whole. We grade these messages so that errors
731 against argument N have a greater priority than an error against
732 any argument < N, since the former implies that arguments up to N
733 were acceptable and that the opcode entry was therefore a closer match.
734 If several matches report an error against the same argument,
735 we only use that error if it is the same in all cases.
736
737 min_argnum is the minimum argument number for which an error message
738 should be accepted. It is 0 if MSG is against the instruction as
739 a whole. */
740 int min_argnum;
741
742 /* The printf()-style message, including its format and arguments. */
743 enum mips_insn_error_format format;
744 const char *msg;
745 union
746 {
747 int i;
748 const char *ss[2];
749 } u;
750 };
751
752 /* The error that should be reported for the current instruction. */
753 static struct mips_insn_error insn_error;
754
755 static int auto_align = 1;
756
757 /* When outputting SVR4 PIC code, the assembler needs to know the
758 offset in the stack frame from which to restore the $gp register.
759 This is set by the .cprestore pseudo-op, and saved in this
760 variable. */
761 static offsetT mips_cprestore_offset = -1;
762
763 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
764 more optimizations, it can use a register value instead of a memory-saved
765 offset and even an other register than $gp as global pointer. */
766 static offsetT mips_cpreturn_offset = -1;
767 static int mips_cpreturn_register = -1;
768 static int mips_gp_register = GP;
769 static int mips_gprel_offset = 0;
770
771 /* Whether mips_cprestore_offset has been set in the current function
772 (or whether it has already been warned about, if not). */
773 static int mips_cprestore_valid = 0;
774
775 /* This is the register which holds the stack frame, as set by the
776 .frame pseudo-op. This is needed to implement .cprestore. */
777 static int mips_frame_reg = SP;
778
779 /* Whether mips_frame_reg has been set in the current function
780 (or whether it has already been warned about, if not). */
781 static int mips_frame_reg_valid = 0;
782
783 /* To output NOP instructions correctly, we need to keep information
784 about the previous two instructions. */
785
786 /* Whether we are optimizing. The default value of 2 means to remove
787 unneeded NOPs and swap branch instructions when possible. A value
788 of 1 means to not swap branches. A value of 0 means to always
789 insert NOPs. */
790 static int mips_optimize = 2;
791
792 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
793 equivalent to seeing no -g option at all. */
794 static int mips_debug = 0;
795
796 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
797 #define MAX_VR4130_NOPS 4
798
799 /* The maximum number of NOPs needed to fill delay slots. */
800 #define MAX_DELAY_NOPS 2
801
802 /* The maximum number of NOPs needed for any purpose. */
803 #define MAX_NOPS 4
804
805 /* The maximum range of context length of ll/sc. */
806 #define MAX_LLSC_RANGE 20
807
808 /* A list of previous instructions, with index 0 being the most recent.
809 We need to look back MAX_NOPS instructions when filling delay slots
810 or working around processor errata. We need to look back one
811 instruction further if we're thinking about using history[0] to
812 fill a branch delay slot. */
813 static struct mips_cl_insn history[1 + MAX_NOPS + MAX_LLSC_RANGE];
814
815 /* The maximum number of LABELS detect for the same address. */
816 #define MAX_LABELS_SAME 10
817
818 /* Arrays of operands for each instruction. */
819 #define MAX_OPERANDS 6
820 struct mips_operand_array
821 {
822 const struct mips_operand *operand[MAX_OPERANDS];
823 };
824 static struct mips_operand_array *mips_operands;
825 static struct mips_operand_array *mips16_operands;
826 static struct mips_operand_array *micromips_operands;
827
828 /* Nop instructions used by emit_nop. */
829 static struct mips_cl_insn nop_insn;
830 static struct mips_cl_insn mips16_nop_insn;
831 static struct mips_cl_insn micromips_nop16_insn;
832 static struct mips_cl_insn micromips_nop32_insn;
833
834 /* Sync instructions used by insert sync. */
835 static struct mips_cl_insn sync_insn;
836
837 /* The appropriate nop for the current mode. */
838 #define NOP_INSN (mips_opts.mips16 \
839 ? &mips16_nop_insn \
840 : (mips_opts.micromips \
841 ? (mips_opts.insn32 \
842 ? &micromips_nop32_insn \
843 : &micromips_nop16_insn) \
844 : &nop_insn))
845
846 /* The size of NOP_INSN in bytes. */
847 #define NOP_INSN_SIZE ((mips_opts.mips16 \
848 || (mips_opts.micromips && !mips_opts.insn32)) \
849 ? 2 : 4)
850
851 /* If this is set, it points to a frag holding nop instructions which
852 were inserted before the start of a noreorder section. If those
853 nops turn out to be unnecessary, the size of the frag can be
854 decreased. */
855 static fragS *prev_nop_frag;
856
857 /* The number of nop instructions we created in prev_nop_frag. */
858 static int prev_nop_frag_holds;
859
860 /* The number of nop instructions that we know we need in
861 prev_nop_frag. */
862 static int prev_nop_frag_required;
863
864 /* The number of instructions we've seen since prev_nop_frag. */
865 static int prev_nop_frag_since;
866
867 /* Relocations against symbols are sometimes done in two parts, with a HI
868 relocation and a LO relocation. Each relocation has only 16 bits of
869 space to store an addend. This means that in order for the linker to
870 handle carries correctly, it must be able to locate both the HI and
871 the LO relocation. This means that the relocations must appear in
872 order in the relocation table.
873
874 In order to implement this, we keep track of each unmatched HI
875 relocation. We then sort them so that they immediately precede the
876 corresponding LO relocation. */
877
878 struct mips_hi_fixup
879 {
880 /* Next HI fixup. */
881 struct mips_hi_fixup *next;
882 /* This fixup. */
883 fixS *fixp;
884 /* The section this fixup is in. */
885 segT seg;
886 };
887
888 /* The list of unmatched HI relocs. */
889
890 static struct mips_hi_fixup *mips_hi_fixup_list;
891
892 /* The frag containing the last explicit relocation operator.
893 Null if explicit relocations have not been used. */
894
895 static fragS *prev_reloc_op_frag;
896
897 /* Map mips16 register numbers to normal MIPS register numbers. */
898
899 static const unsigned int mips16_to_32_reg_map[] =
900 {
901 16, 17, 2, 3, 4, 5, 6, 7
902 };
903
904 /* Map microMIPS register numbers to normal MIPS register numbers. */
905
906 #define micromips_to_32_reg_d_map mips16_to_32_reg_map
907
908 /* The microMIPS registers with type h. */
909 static const unsigned int micromips_to_32_reg_h_map1[] =
910 {
911 5, 5, 6, 4, 4, 4, 4, 4
912 };
913 static const unsigned int micromips_to_32_reg_h_map2[] =
914 {
915 6, 7, 7, 21, 22, 5, 6, 7
916 };
917
918 /* The microMIPS registers with type m. */
919 static const unsigned int micromips_to_32_reg_m_map[] =
920 {
921 0, 17, 2, 3, 16, 18, 19, 20
922 };
923
924 #define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
925
926 /* Classifies the kind of instructions we're interested in when
927 implementing -mfix-vr4120. */
928 enum fix_vr4120_class
929 {
930 FIX_VR4120_MACC,
931 FIX_VR4120_DMACC,
932 FIX_VR4120_MULT,
933 FIX_VR4120_DMULT,
934 FIX_VR4120_DIV,
935 FIX_VR4120_MTHILO,
936 NUM_FIX_VR4120_CLASSES
937 };
938
939 /* ...likewise -mfix-loongson2f-jump. */
940 static bfd_boolean mips_fix_loongson2f_jump;
941
942 /* ...likewise -mfix-loongson2f-nop. */
943 static bfd_boolean mips_fix_loongson2f_nop;
944
945 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
946 static bfd_boolean mips_fix_loongson2f;
947
948 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
949 there must be at least one other instruction between an instruction
950 of type X and an instruction of type Y. */
951 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
952
953 /* True if -mfix-vr4120 is in force. */
954 static int mips_fix_vr4120;
955
956 /* ...likewise -mfix-vr4130. */
957 static int mips_fix_vr4130;
958
959 /* ...likewise -mfix-24k. */
960 static int mips_fix_24k;
961
962 /* ...likewise -mfix-rm7000 */
963 static int mips_fix_rm7000;
964
965 /* ...likewise -mfix-cn63xxp1 */
966 static bfd_boolean mips_fix_cn63xxp1;
967
968 /* ...likewise -mfix-r5900 */
969 static bfd_boolean mips_fix_r5900;
970 static bfd_boolean mips_fix_r5900_explicit;
971
972 /* ...likewise -mfix-loongson3-llsc. */
973 static bfd_boolean mips_fix_loongson3_llsc = DEFAULT_MIPS_FIX_LOONGSON3_LLSC;
974
975 /* We don't relax branches by default, since this causes us to expand
976 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
977 fail to compute the offset before expanding the macro to the most
978 efficient expansion. */
979
980 static int mips_relax_branch;
981
982 /* TRUE if checks are suppressed for invalid branches between ISA modes.
983 Needed for broken assembly produced by some GCC versions and some
984 sloppy code out there, where branches to data labels are present. */
985 static bfd_boolean mips_ignore_branch_isa;
986 \f
987 /* The expansion of many macros depends on the type of symbol that
988 they refer to. For example, when generating position-dependent code,
989 a macro that refers to a symbol may have two different expansions,
990 one which uses GP-relative addresses and one which uses absolute
991 addresses. When generating SVR4-style PIC, a macro may have
992 different expansions for local and global symbols.
993
994 We handle these situations by generating both sequences and putting
995 them in variant frags. In position-dependent code, the first sequence
996 will be the GP-relative one and the second sequence will be the
997 absolute one. In SVR4 PIC, the first sequence will be for global
998 symbols and the second will be for local symbols.
999
1000 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
1001 SECOND are the lengths of the two sequences in bytes. These fields
1002 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
1003 the subtype has the following flags:
1004
1005 RELAX_PIC
1006 Set if generating PIC code.
1007
1008 RELAX_USE_SECOND
1009 Set if it has been decided that we should use the second
1010 sequence instead of the first.
1011
1012 RELAX_SECOND_LONGER
1013 Set in the first variant frag if the macro's second implementation
1014 is longer than its first. This refers to the macro as a whole,
1015 not an individual relaxation.
1016
1017 RELAX_NOMACRO
1018 Set in the first variant frag if the macro appeared in a .set nomacro
1019 block and if one alternative requires a warning but the other does not.
1020
1021 RELAX_DELAY_SLOT
1022 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
1023 delay slot.
1024
1025 RELAX_DELAY_SLOT_16BIT
1026 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
1027 16-bit instruction.
1028
1029 RELAX_DELAY_SLOT_SIZE_FIRST
1030 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1031 the macro is of the wrong size for the branch delay slot.
1032
1033 RELAX_DELAY_SLOT_SIZE_SECOND
1034 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1035 the macro is of the wrong size for the branch delay slot.
1036
1037 The frag's "opcode" points to the first fixup for relaxable code.
1038
1039 Relaxable macros are generated using a sequence such as:
1040
1041 relax_start (SYMBOL);
1042 ... generate first expansion ...
1043 relax_switch ();
1044 ... generate second expansion ...
1045 relax_end ();
1046
1047 The code and fixups for the unwanted alternative are discarded
1048 by md_convert_frag. */
1049 #define RELAX_ENCODE(FIRST, SECOND, PIC) \
1050 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
1051
1052 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1053 #define RELAX_SECOND(X) ((X) & 0xff)
1054 #define RELAX_PIC(X) (((X) & 0x10000) != 0)
1055 #define RELAX_USE_SECOND 0x20000
1056 #define RELAX_SECOND_LONGER 0x40000
1057 #define RELAX_NOMACRO 0x80000
1058 #define RELAX_DELAY_SLOT 0x100000
1059 #define RELAX_DELAY_SLOT_16BIT 0x200000
1060 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1061 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
1062
1063 /* Branch without likely bit. If label is out of range, we turn:
1064
1065 beq reg1, reg2, label
1066 delay slot
1067
1068 into
1069
1070 bne reg1, reg2, 0f
1071 nop
1072 j label
1073 0: delay slot
1074
1075 with the following opcode replacements:
1076
1077 beq <-> bne
1078 blez <-> bgtz
1079 bltz <-> bgez
1080 bc1f <-> bc1t
1081
1082 bltzal <-> bgezal (with jal label instead of j label)
1083
1084 Even though keeping the delay slot instruction in the delay slot of
1085 the branch would be more efficient, it would be very tricky to do
1086 correctly, because we'd have to introduce a variable frag *after*
1087 the delay slot instruction, and expand that instead. Let's do it
1088 the easy way for now, even if the branch-not-taken case now costs
1089 one additional instruction. Out-of-range branches are not supposed
1090 to be common, anyway.
1091
1092 Branch likely. If label is out of range, we turn:
1093
1094 beql reg1, reg2, label
1095 delay slot (annulled if branch not taken)
1096
1097 into
1098
1099 beql reg1, reg2, 1f
1100 nop
1101 beql $0, $0, 2f
1102 nop
1103 1: j[al] label
1104 delay slot (executed only if branch taken)
1105 2:
1106
1107 It would be possible to generate a shorter sequence by losing the
1108 likely bit, generating something like:
1109
1110 bne reg1, reg2, 0f
1111 nop
1112 j[al] label
1113 delay slot (executed only if branch taken)
1114 0:
1115
1116 beql -> bne
1117 bnel -> beq
1118 blezl -> bgtz
1119 bgtzl -> blez
1120 bltzl -> bgez
1121 bgezl -> bltz
1122 bc1fl -> bc1t
1123 bc1tl -> bc1f
1124
1125 bltzall -> bgezal (with jal label instead of j label)
1126 bgezall -> bltzal (ditto)
1127
1128
1129 but it's not clear that it would actually improve performance. */
1130 #define RELAX_BRANCH_ENCODE(at, pic, \
1131 uncond, likely, link, toofar) \
1132 ((relax_substateT) \
1133 (0xc0000000 \
1134 | ((at) & 0x1f) \
1135 | ((pic) ? 0x20 : 0) \
1136 | ((toofar) ? 0x40 : 0) \
1137 | ((link) ? 0x80 : 0) \
1138 | ((likely) ? 0x100 : 0) \
1139 | ((uncond) ? 0x200 : 0)))
1140 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1141 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1142 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1143 #define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1144 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1145 #define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
1146 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1147
1148 /* For mips16 code, we use an entirely different form of relaxation.
1149 mips16 supports two versions of most instructions which take
1150 immediate values: a small one which takes some small value, and a
1151 larger one which takes a 16 bit value. Since branches also follow
1152 this pattern, relaxing these values is required.
1153
1154 We can assemble both mips16 and normal MIPS code in a single
1155 object. Therefore, we need to support this type of relaxation at
1156 the same time that we support the relaxation described above. We
1157 use the high bit of the subtype field to distinguish these cases.
1158
1159 The information we store for this type of relaxation is the
1160 argument code found in the opcode file for this relocation, whether
1161 the user explicitly requested a small or extended form, and whether
1162 the relocation is in a jump or jal delay slot. That tells us the
1163 size of the value, and how it should be stored. We also store
1164 whether the fragment is considered to be extended or not. We also
1165 store whether this is known to be a branch to a different section,
1166 whether we have tried to relax this frag yet, and whether we have
1167 ever extended a PC relative fragment because of a shift count. */
1168 #define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
1169 small, ext, \
1170 dslot, jal_dslot) \
1171 (0x80000000 \
1172 | ((type) & 0xff) \
1173 | ((e2) ? 0x100 : 0) \
1174 | ((pic) ? 0x200 : 0) \
1175 | ((sym32) ? 0x400 : 0) \
1176 | ((nomacro) ? 0x800 : 0) \
1177 | ((small) ? 0x1000 : 0) \
1178 | ((ext) ? 0x2000 : 0) \
1179 | ((dslot) ? 0x4000 : 0) \
1180 | ((jal_dslot) ? 0x8000 : 0))
1181
1182 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1183 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1184 #define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1185 #define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1186 #define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1187 #define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1188 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1189 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1190 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1191 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1192
1193 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1194 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1195 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1196 #define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1197 #define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1198 #define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1199 #define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1200 #define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1201 #define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
1202
1203 /* For microMIPS code, we use relaxation similar to one we use for
1204 MIPS16 code. Some instructions that take immediate values support
1205 two encodings: a small one which takes some small value, and a
1206 larger one which takes a 16 bit value. As some branches also follow
1207 this pattern, relaxing these values is required.
1208
1209 We can assemble both microMIPS and normal MIPS code in a single
1210 object. Therefore, we need to support this type of relaxation at
1211 the same time that we support the relaxation described above. We
1212 use one of the high bits of the subtype field to distinguish these
1213 cases.
1214
1215 The information we store for this type of relaxation is the argument
1216 code found in the opcode file for this relocation, the register
1217 selected as the assembler temporary, whether in the 32-bit
1218 instruction mode, whether the branch is unconditional, whether it is
1219 compact, whether there is no delay-slot instruction available to fill
1220 in, whether it stores the link address implicitly in $ra, whether
1221 relaxation of out-of-range 32-bit branches to a sequence of
1222 instructions is enabled, and whether the displacement of a branch is
1223 too large to fit as an immediate argument of a 16-bit and a 32-bit
1224 branch, respectively. */
1225 #define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
1226 uncond, compact, link, nods, \
1227 relax32, toofar16, toofar32) \
1228 (0x40000000 \
1229 | ((type) & 0xff) \
1230 | (((at) & 0x1f) << 8) \
1231 | ((insn32) ? 0x2000 : 0) \
1232 | ((pic) ? 0x4000 : 0) \
1233 | ((uncond) ? 0x8000 : 0) \
1234 | ((compact) ? 0x10000 : 0) \
1235 | ((link) ? 0x20000 : 0) \
1236 | ((nods) ? 0x40000 : 0) \
1237 | ((relax32) ? 0x80000 : 0) \
1238 | ((toofar16) ? 0x100000 : 0) \
1239 | ((toofar32) ? 0x200000 : 0))
1240 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1241 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1242 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1243 #define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
1244 #define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1245 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1246 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1247 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1248 #define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1249 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1250
1251 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1252 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1253 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1254 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1255 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1256 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
1257
1258 /* Sign-extend 16-bit value X. */
1259 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1260
1261 /* Is the given value a sign-extended 32-bit value? */
1262 #define IS_SEXT_32BIT_NUM(x) \
1263 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1264 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1265
1266 /* Is the given value a sign-extended 16-bit value? */
1267 #define IS_SEXT_16BIT_NUM(x) \
1268 (((x) &~ (offsetT) 0x7fff) == 0 \
1269 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1270
1271 /* Is the given value a sign-extended 12-bit value? */
1272 #define IS_SEXT_12BIT_NUM(x) \
1273 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1274
1275 /* Is the given value a sign-extended 9-bit value? */
1276 #define IS_SEXT_9BIT_NUM(x) \
1277 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1278
1279 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
1280 #define IS_ZEXT_32BIT_NUM(x) \
1281 (((x) &~ (offsetT) 0xffffffff) == 0 \
1282 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1283
1284 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1285 SHIFT places. */
1286 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1287 (((STRUCT) >> (SHIFT)) & (MASK))
1288
1289 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
1290 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1291 (!(MICROMIPS) \
1292 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1293 : EXTRACT_BITS ((INSN).insn_opcode, \
1294 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1295 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1296 EXTRACT_BITS ((INSN).insn_opcode, \
1297 MIPS16OP_MASK_##FIELD, \
1298 MIPS16OP_SH_##FIELD)
1299
1300 /* The MIPS16 EXTEND opcode, shifted left 16 places. */
1301 #define MIPS16_EXTEND (0xf000U << 16)
1302 \f
1303 /* Whether or not we are emitting a branch-likely macro. */
1304 static bfd_boolean emit_branch_likely_macro = FALSE;
1305
1306 /* Global variables used when generating relaxable macros. See the
1307 comment above RELAX_ENCODE for more details about how relaxation
1308 is used. */
1309 static struct {
1310 /* 0 if we're not emitting a relaxable macro.
1311 1 if we're emitting the first of the two relaxation alternatives.
1312 2 if we're emitting the second alternative. */
1313 int sequence;
1314
1315 /* The first relaxable fixup in the current frag. (In other words,
1316 the first fixup that refers to relaxable code.) */
1317 fixS *first_fixup;
1318
1319 /* sizes[0] says how many bytes of the first alternative are stored in
1320 the current frag. Likewise sizes[1] for the second alternative. */
1321 unsigned int sizes[2];
1322
1323 /* The symbol on which the choice of sequence depends. */
1324 symbolS *symbol;
1325 } mips_relax;
1326 \f
1327 /* Global variables used to decide whether a macro needs a warning. */
1328 static struct {
1329 /* True if the macro is in a branch delay slot. */
1330 bfd_boolean delay_slot_p;
1331
1332 /* Set to the length in bytes required if the macro is in a delay slot
1333 that requires a specific length of instruction, otherwise zero. */
1334 unsigned int delay_slot_length;
1335
1336 /* For relaxable macros, sizes[0] is the length of the first alternative
1337 in bytes and sizes[1] is the length of the second alternative.
1338 For non-relaxable macros, both elements give the length of the
1339 macro in bytes. */
1340 unsigned int sizes[2];
1341
1342 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1343 instruction of the first alternative in bytes and first_insn_sizes[1]
1344 is the length of the first instruction of the second alternative.
1345 For non-relaxable macros, both elements give the length of the first
1346 instruction in bytes.
1347
1348 Set to zero if we haven't yet seen the first instruction. */
1349 unsigned int first_insn_sizes[2];
1350
1351 /* For relaxable macros, insns[0] is the number of instructions for the
1352 first alternative and insns[1] is the number of instructions for the
1353 second alternative.
1354
1355 For non-relaxable macros, both elements give the number of
1356 instructions for the macro. */
1357 unsigned int insns[2];
1358
1359 /* The first variant frag for this macro. */
1360 fragS *first_frag;
1361 } mips_macro_warning;
1362 \f
1363 /* Prototypes for static functions. */
1364
1365 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1366
1367 static void append_insn
1368 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1369 bfd_boolean expansionp);
1370 static void mips_no_prev_insn (void);
1371 static void macro_build (expressionS *, const char *, const char *, ...);
1372 static void mips16_macro_build
1373 (expressionS *, const char *, const char *, va_list *);
1374 static void load_register (int, expressionS *, int);
1375 static void macro_start (void);
1376 static void macro_end (void);
1377 static void macro (struct mips_cl_insn *ip, char *str);
1378 static void mips16_macro (struct mips_cl_insn * ip);
1379 static void mips_ip (char *str, struct mips_cl_insn * ip);
1380 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1381 static unsigned long mips16_immed_extend (offsetT, unsigned int);
1382 static void mips16_immed
1383 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1384 unsigned int, unsigned long *);
1385 static size_t my_getSmallExpression
1386 (expressionS *, bfd_reloc_code_real_type *, char *);
1387 static void my_getExpression (expressionS *, char *);
1388 static void s_align (int);
1389 static void s_change_sec (int);
1390 static void s_change_section (int);
1391 static void s_cons (int);
1392 static void s_float_cons (int);
1393 static void s_mips_globl (int);
1394 static void s_option (int);
1395 static void s_mipsset (int);
1396 static void s_abicalls (int);
1397 static void s_cpload (int);
1398 static void s_cpsetup (int);
1399 static void s_cplocal (int);
1400 static void s_cprestore (int);
1401 static void s_cpreturn (int);
1402 static void s_dtprelword (int);
1403 static void s_dtpreldword (int);
1404 static void s_tprelword (int);
1405 static void s_tpreldword (int);
1406 static void s_gpvalue (int);
1407 static void s_gpword (int);
1408 static void s_gpdword (int);
1409 static void s_ehword (int);
1410 static void s_cpadd (int);
1411 static void s_insn (int);
1412 static void s_nan (int);
1413 static void s_module (int);
1414 static void s_mips_ent (int);
1415 static void s_mips_end (int);
1416 static void s_mips_frame (int);
1417 static void s_mips_mask (int reg_type);
1418 static void s_mips_stab (int);
1419 static void s_mips_weakext (int);
1420 static void s_mips_file (int);
1421 static void s_mips_loc (int);
1422 static bfd_boolean pic_need_relax (symbolS *);
1423 static int relaxed_branch_length (fragS *, asection *, int);
1424 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1425 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1426 static void file_mips_check_options (void);
1427
1428 /* Table and functions used to map between CPU/ISA names, and
1429 ISA levels, and CPU numbers. */
1430
1431 struct mips_cpu_info
1432 {
1433 const char *name; /* CPU or ISA name. */
1434 int flags; /* MIPS_CPU_* flags. */
1435 int ase; /* Set of ASEs implemented by the CPU. */
1436 int isa; /* ISA level. */
1437 int cpu; /* CPU number (default CPU if ISA). */
1438 };
1439
1440 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1441
1442 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1443 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1444 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1445 \f
1446 /* Command-line options. */
1447 const char *md_shortopts = "O::g::G:";
1448
1449 enum options
1450 {
1451 OPTION_MARCH = OPTION_MD_BASE,
1452 OPTION_MTUNE,
1453 OPTION_MIPS1,
1454 OPTION_MIPS2,
1455 OPTION_MIPS3,
1456 OPTION_MIPS4,
1457 OPTION_MIPS5,
1458 OPTION_MIPS32,
1459 OPTION_MIPS64,
1460 OPTION_MIPS32R2,
1461 OPTION_MIPS32R3,
1462 OPTION_MIPS32R5,
1463 OPTION_MIPS32R6,
1464 OPTION_MIPS64R2,
1465 OPTION_MIPS64R3,
1466 OPTION_MIPS64R5,
1467 OPTION_MIPS64R6,
1468 OPTION_MIPS16,
1469 OPTION_NO_MIPS16,
1470 OPTION_MIPS3D,
1471 OPTION_NO_MIPS3D,
1472 OPTION_MDMX,
1473 OPTION_NO_MDMX,
1474 OPTION_DSP,
1475 OPTION_NO_DSP,
1476 OPTION_MT,
1477 OPTION_NO_MT,
1478 OPTION_VIRT,
1479 OPTION_NO_VIRT,
1480 OPTION_MSA,
1481 OPTION_NO_MSA,
1482 OPTION_SMARTMIPS,
1483 OPTION_NO_SMARTMIPS,
1484 OPTION_DSPR2,
1485 OPTION_NO_DSPR2,
1486 OPTION_DSPR3,
1487 OPTION_NO_DSPR3,
1488 OPTION_EVA,
1489 OPTION_NO_EVA,
1490 OPTION_XPA,
1491 OPTION_NO_XPA,
1492 OPTION_MICROMIPS,
1493 OPTION_NO_MICROMIPS,
1494 OPTION_MCU,
1495 OPTION_NO_MCU,
1496 OPTION_MIPS16E2,
1497 OPTION_NO_MIPS16E2,
1498 OPTION_CRC,
1499 OPTION_NO_CRC,
1500 OPTION_M4650,
1501 OPTION_NO_M4650,
1502 OPTION_M4010,
1503 OPTION_NO_M4010,
1504 OPTION_M4100,
1505 OPTION_NO_M4100,
1506 OPTION_M3900,
1507 OPTION_NO_M3900,
1508 OPTION_M7000_HILO_FIX,
1509 OPTION_MNO_7000_HILO_FIX,
1510 OPTION_FIX_24K,
1511 OPTION_NO_FIX_24K,
1512 OPTION_FIX_RM7000,
1513 OPTION_NO_FIX_RM7000,
1514 OPTION_FIX_LOONGSON3_LLSC,
1515 OPTION_NO_FIX_LOONGSON3_LLSC,
1516 OPTION_FIX_LOONGSON2F_JUMP,
1517 OPTION_NO_FIX_LOONGSON2F_JUMP,
1518 OPTION_FIX_LOONGSON2F_NOP,
1519 OPTION_NO_FIX_LOONGSON2F_NOP,
1520 OPTION_FIX_VR4120,
1521 OPTION_NO_FIX_VR4120,
1522 OPTION_FIX_VR4130,
1523 OPTION_NO_FIX_VR4130,
1524 OPTION_FIX_CN63XXP1,
1525 OPTION_NO_FIX_CN63XXP1,
1526 OPTION_FIX_R5900,
1527 OPTION_NO_FIX_R5900,
1528 OPTION_TRAP,
1529 OPTION_BREAK,
1530 OPTION_EB,
1531 OPTION_EL,
1532 OPTION_FP32,
1533 OPTION_GP32,
1534 OPTION_CONSTRUCT_FLOATS,
1535 OPTION_NO_CONSTRUCT_FLOATS,
1536 OPTION_FP64,
1537 OPTION_FPXX,
1538 OPTION_GP64,
1539 OPTION_RELAX_BRANCH,
1540 OPTION_NO_RELAX_BRANCH,
1541 OPTION_IGNORE_BRANCH_ISA,
1542 OPTION_NO_IGNORE_BRANCH_ISA,
1543 OPTION_INSN32,
1544 OPTION_NO_INSN32,
1545 OPTION_MSHARED,
1546 OPTION_MNO_SHARED,
1547 OPTION_MSYM32,
1548 OPTION_MNO_SYM32,
1549 OPTION_SOFT_FLOAT,
1550 OPTION_HARD_FLOAT,
1551 OPTION_SINGLE_FLOAT,
1552 OPTION_DOUBLE_FLOAT,
1553 OPTION_32,
1554 OPTION_CALL_SHARED,
1555 OPTION_CALL_NONPIC,
1556 OPTION_NON_SHARED,
1557 OPTION_XGOT,
1558 OPTION_MABI,
1559 OPTION_N32,
1560 OPTION_64,
1561 OPTION_MDEBUG,
1562 OPTION_NO_MDEBUG,
1563 OPTION_PDR,
1564 OPTION_NO_PDR,
1565 OPTION_MVXWORKS_PIC,
1566 OPTION_NAN,
1567 OPTION_ODD_SPREG,
1568 OPTION_NO_ODD_SPREG,
1569 OPTION_GINV,
1570 OPTION_NO_GINV,
1571 OPTION_LOONGSON_MMI,
1572 OPTION_NO_LOONGSON_MMI,
1573 OPTION_LOONGSON_CAM,
1574 OPTION_NO_LOONGSON_CAM,
1575 OPTION_LOONGSON_EXT,
1576 OPTION_NO_LOONGSON_EXT,
1577 OPTION_LOONGSON_EXT2,
1578 OPTION_NO_LOONGSON_EXT2,
1579 OPTION_END_OF_ENUM
1580 };
1581
1582 struct option md_longopts[] =
1583 {
1584 /* Options which specify architecture. */
1585 {"march", required_argument, NULL, OPTION_MARCH},
1586 {"mtune", required_argument, NULL, OPTION_MTUNE},
1587 {"mips0", no_argument, NULL, OPTION_MIPS1},
1588 {"mips1", no_argument, NULL, OPTION_MIPS1},
1589 {"mips2", no_argument, NULL, OPTION_MIPS2},
1590 {"mips3", no_argument, NULL, OPTION_MIPS3},
1591 {"mips4", no_argument, NULL, OPTION_MIPS4},
1592 {"mips5", no_argument, NULL, OPTION_MIPS5},
1593 {"mips32", no_argument, NULL, OPTION_MIPS32},
1594 {"mips64", no_argument, NULL, OPTION_MIPS64},
1595 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1596 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1597 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
1598 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
1599 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1600 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1601 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
1602 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
1603
1604 /* Options which specify Application Specific Extensions (ASEs). */
1605 {"mips16", no_argument, NULL, OPTION_MIPS16},
1606 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1607 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1608 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1609 {"mdmx", no_argument, NULL, OPTION_MDMX},
1610 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1611 {"mdsp", no_argument, NULL, OPTION_DSP},
1612 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1613 {"mmt", no_argument, NULL, OPTION_MT},
1614 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1615 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1616 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1617 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1618 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1619 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1620 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
1621 {"meva", no_argument, NULL, OPTION_EVA},
1622 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1623 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1624 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1625 {"mmcu", no_argument, NULL, OPTION_MCU},
1626 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1627 {"mvirt", no_argument, NULL, OPTION_VIRT},
1628 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1629 {"mmsa", no_argument, NULL, OPTION_MSA},
1630 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
1631 {"mxpa", no_argument, NULL, OPTION_XPA},
1632 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
1633 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1634 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
1635 {"mcrc", no_argument, NULL, OPTION_CRC},
1636 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
1637 {"mginv", no_argument, NULL, OPTION_GINV},
1638 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
1639 {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1640 {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
1641 {"mloongson-cam", no_argument, NULL, OPTION_LOONGSON_CAM},
1642 {"mno-loongson-cam", no_argument, NULL, OPTION_NO_LOONGSON_CAM},
1643 {"mloongson-ext", no_argument, NULL, OPTION_LOONGSON_EXT},
1644 {"mno-loongson-ext", no_argument, NULL, OPTION_NO_LOONGSON_EXT},
1645 {"mloongson-ext2", no_argument, NULL, OPTION_LOONGSON_EXT2},
1646 {"mno-loongson-ext2", no_argument, NULL, OPTION_NO_LOONGSON_EXT2},
1647
1648 /* Old-style architecture options. Don't add more of these. */
1649 {"m4650", no_argument, NULL, OPTION_M4650},
1650 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1651 {"m4010", no_argument, NULL, OPTION_M4010},
1652 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1653 {"m4100", no_argument, NULL, OPTION_M4100},
1654 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1655 {"m3900", no_argument, NULL, OPTION_M3900},
1656 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1657
1658 /* Options which enable bug fixes. */
1659 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1660 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1661 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1662 {"mfix-loongson3-llsc", no_argument, NULL, OPTION_FIX_LOONGSON3_LLSC},
1663 {"mno-fix-loongson3-llsc", no_argument, NULL, OPTION_NO_FIX_LOONGSON3_LLSC},
1664 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1665 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1666 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1667 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1668 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1669 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1670 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1671 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1672 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1673 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1674 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1675 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
1676 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1677 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1678 {"mfix-r5900", no_argument, NULL, OPTION_FIX_R5900},
1679 {"mno-fix-r5900", no_argument, NULL, OPTION_NO_FIX_R5900},
1680
1681 /* Miscellaneous options. */
1682 {"trap", no_argument, NULL, OPTION_TRAP},
1683 {"no-break", no_argument, NULL, OPTION_TRAP},
1684 {"break", no_argument, NULL, OPTION_BREAK},
1685 {"no-trap", no_argument, NULL, OPTION_BREAK},
1686 {"EB", no_argument, NULL, OPTION_EB},
1687 {"EL", no_argument, NULL, OPTION_EL},
1688 {"mfp32", no_argument, NULL, OPTION_FP32},
1689 {"mgp32", no_argument, NULL, OPTION_GP32},
1690 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1691 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1692 {"mfp64", no_argument, NULL, OPTION_FP64},
1693 {"mfpxx", no_argument, NULL, OPTION_FPXX},
1694 {"mgp64", no_argument, NULL, OPTION_GP64},
1695 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1696 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1697 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1698 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
1699 {"minsn32", no_argument, NULL, OPTION_INSN32},
1700 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1701 {"mshared", no_argument, NULL, OPTION_MSHARED},
1702 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1703 {"msym32", no_argument, NULL, OPTION_MSYM32},
1704 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1705 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1706 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1707 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1708 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1709 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1710 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
1711
1712 /* Strictly speaking this next option is ELF specific,
1713 but we allow it for other ports as well in order to
1714 make testing easier. */
1715 {"32", no_argument, NULL, OPTION_32},
1716
1717 /* ELF-specific options. */
1718 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1719 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1720 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1721 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1722 {"xgot", no_argument, NULL, OPTION_XGOT},
1723 {"mabi", required_argument, NULL, OPTION_MABI},
1724 {"n32", no_argument, NULL, OPTION_N32},
1725 {"64", no_argument, NULL, OPTION_64},
1726 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1727 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1728 {"mpdr", no_argument, NULL, OPTION_PDR},
1729 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1730 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1731 {"mnan", required_argument, NULL, OPTION_NAN},
1732
1733 {NULL, no_argument, NULL, 0}
1734 };
1735 size_t md_longopts_size = sizeof (md_longopts);
1736 \f
1737 /* Information about either an Application Specific Extension or an
1738 optional architecture feature that, for simplicity, we treat in the
1739 same way as an ASE. */
1740 struct mips_ase
1741 {
1742 /* The name of the ASE, used in both the command-line and .set options. */
1743 const char *name;
1744
1745 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1746 and 64-bit architectures, the flags here refer to the subset that
1747 is available on both. */
1748 unsigned int flags;
1749
1750 /* The ASE_* flag used for instructions that are available on 64-bit
1751 architectures but that are not included in FLAGS. */
1752 unsigned int flags64;
1753
1754 /* The command-line options that turn the ASE on and off. */
1755 int option_on;
1756 int option_off;
1757
1758 /* The minimum required architecture revisions for MIPS32, MIPS64,
1759 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1760 int mips32_rev;
1761 int mips64_rev;
1762 int micromips32_rev;
1763 int micromips64_rev;
1764
1765 /* The architecture where the ASE was removed or -1 if the extension has not
1766 been removed. */
1767 int rem_rev;
1768 };
1769
1770 /* A table of all supported ASEs. */
1771 static const struct mips_ase mips_ases[] = {
1772 { "dsp", ASE_DSP, ASE_DSP64,
1773 OPTION_DSP, OPTION_NO_DSP,
1774 2, 2, 2, 2,
1775 -1 },
1776
1777 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1778 OPTION_DSPR2, OPTION_NO_DSPR2,
1779 2, 2, 2, 2,
1780 -1 },
1781
1782 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1783 OPTION_DSPR3, OPTION_NO_DSPR3,
1784 6, 6, -1, -1,
1785 -1 },
1786
1787 { "eva", ASE_EVA, 0,
1788 OPTION_EVA, OPTION_NO_EVA,
1789 2, 2, 2, 2,
1790 -1 },
1791
1792 { "mcu", ASE_MCU, 0,
1793 OPTION_MCU, OPTION_NO_MCU,
1794 2, 2, 2, 2,
1795 -1 },
1796
1797 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1798 { "mdmx", ASE_MDMX, 0,
1799 OPTION_MDMX, OPTION_NO_MDMX,
1800 -1, 1, -1, -1,
1801 6 },
1802
1803 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1804 { "mips3d", ASE_MIPS3D, 0,
1805 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1806 2, 1, -1, -1,
1807 6 },
1808
1809 { "mt", ASE_MT, 0,
1810 OPTION_MT, OPTION_NO_MT,
1811 2, 2, -1, -1,
1812 -1 },
1813
1814 { "smartmips", ASE_SMARTMIPS, 0,
1815 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1816 1, -1, -1, -1,
1817 6 },
1818
1819 { "virt", ASE_VIRT, ASE_VIRT64,
1820 OPTION_VIRT, OPTION_NO_VIRT,
1821 2, 2, 2, 2,
1822 -1 },
1823
1824 { "msa", ASE_MSA, ASE_MSA64,
1825 OPTION_MSA, OPTION_NO_MSA,
1826 2, 2, 2, 2,
1827 -1 },
1828
1829 { "xpa", ASE_XPA, 0,
1830 OPTION_XPA, OPTION_NO_XPA,
1831 2, 2, 2, 2,
1832 -1 },
1833
1834 { "mips16e2", ASE_MIPS16E2, 0,
1835 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1836 2, 2, -1, -1,
1837 6 },
1838
1839 { "crc", ASE_CRC, ASE_CRC64,
1840 OPTION_CRC, OPTION_NO_CRC,
1841 6, 6, -1, -1,
1842 -1 },
1843
1844 { "ginv", ASE_GINV, 0,
1845 OPTION_GINV, OPTION_NO_GINV,
1846 6, 6, 6, 6,
1847 -1 },
1848
1849 { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1850 OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1851 0, 0, -1, -1,
1852 -1 },
1853
1854 { "loongson-cam", ASE_LOONGSON_CAM, 0,
1855 OPTION_LOONGSON_CAM, OPTION_NO_LOONGSON_CAM,
1856 0, 0, -1, -1,
1857 -1 },
1858
1859 { "loongson-ext", ASE_LOONGSON_EXT, 0,
1860 OPTION_LOONGSON_EXT, OPTION_NO_LOONGSON_EXT,
1861 0, 0, -1, -1,
1862 -1 },
1863
1864 { "loongson-ext2", ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2, 0,
1865 OPTION_LOONGSON_EXT2, OPTION_NO_LOONGSON_EXT2,
1866 0, 0, -1, -1,
1867 -1 },
1868 };
1869
1870 /* The set of ASEs that require -mfp64. */
1871 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
1872
1873 /* Groups of ASE_* flags that represent different revisions of an ASE. */
1874 static const unsigned int mips_ase_groups[] = {
1875 ASE_DSP | ASE_DSPR2 | ASE_DSPR3,
1876 ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2
1877 };
1878 \f
1879 /* Pseudo-op table.
1880
1881 The following pseudo-ops from the Kane and Heinrich MIPS book
1882 should be defined here, but are currently unsupported: .alias,
1883 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1884
1885 The following pseudo-ops from the Kane and Heinrich MIPS book are
1886 specific to the type of debugging information being generated, and
1887 should be defined by the object format: .aent, .begin, .bend,
1888 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1889 .vreg.
1890
1891 The following pseudo-ops from the Kane and Heinrich MIPS book are
1892 not MIPS CPU specific, but are also not specific to the object file
1893 format. This file is probably the best place to define them, but
1894 they are not currently supported: .asm0, .endr, .lab, .struct. */
1895
1896 static const pseudo_typeS mips_pseudo_table[] =
1897 {
1898 /* MIPS specific pseudo-ops. */
1899 {"option", s_option, 0},
1900 {"set", s_mipsset, 0},
1901 {"rdata", s_change_sec, 'r'},
1902 {"sdata", s_change_sec, 's'},
1903 {"livereg", s_ignore, 0},
1904 {"abicalls", s_abicalls, 0},
1905 {"cpload", s_cpload, 0},
1906 {"cpsetup", s_cpsetup, 0},
1907 {"cplocal", s_cplocal, 0},
1908 {"cprestore", s_cprestore, 0},
1909 {"cpreturn", s_cpreturn, 0},
1910 {"dtprelword", s_dtprelword, 0},
1911 {"dtpreldword", s_dtpreldword, 0},
1912 {"tprelword", s_tprelword, 0},
1913 {"tpreldword", s_tpreldword, 0},
1914 {"gpvalue", s_gpvalue, 0},
1915 {"gpword", s_gpword, 0},
1916 {"gpdword", s_gpdword, 0},
1917 {"ehword", s_ehword, 0},
1918 {"cpadd", s_cpadd, 0},
1919 {"insn", s_insn, 0},
1920 {"nan", s_nan, 0},
1921 {"module", s_module, 0},
1922
1923 /* Relatively generic pseudo-ops that happen to be used on MIPS
1924 chips. */
1925 {"asciiz", stringer, 8 + 1},
1926 {"bss", s_change_sec, 'b'},
1927 {"err", s_err, 0},
1928 {"half", s_cons, 1},
1929 {"dword", s_cons, 3},
1930 {"weakext", s_mips_weakext, 0},
1931 {"origin", s_org, 0},
1932 {"repeat", s_rept, 0},
1933
1934 /* For MIPS this is non-standard, but we define it for consistency. */
1935 {"sbss", s_change_sec, 'B'},
1936
1937 /* These pseudo-ops are defined in read.c, but must be overridden
1938 here for one reason or another. */
1939 {"align", s_align, 0},
1940 {"byte", s_cons, 0},
1941 {"data", s_change_sec, 'd'},
1942 {"double", s_float_cons, 'd'},
1943 {"float", s_float_cons, 'f'},
1944 {"globl", s_mips_globl, 0},
1945 {"global", s_mips_globl, 0},
1946 {"hword", s_cons, 1},
1947 {"int", s_cons, 2},
1948 {"long", s_cons, 2},
1949 {"octa", s_cons, 4},
1950 {"quad", s_cons, 3},
1951 {"section", s_change_section, 0},
1952 {"short", s_cons, 1},
1953 {"single", s_float_cons, 'f'},
1954 {"stabd", s_mips_stab, 'd'},
1955 {"stabn", s_mips_stab, 'n'},
1956 {"stabs", s_mips_stab, 's'},
1957 {"text", s_change_sec, 't'},
1958 {"word", s_cons, 2},
1959
1960 { "extern", ecoff_directive_extern, 0},
1961
1962 { NULL, NULL, 0 },
1963 };
1964
1965 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1966 {
1967 /* These pseudo-ops should be defined by the object file format.
1968 However, a.out doesn't support them, so we have versions here. */
1969 {"aent", s_mips_ent, 1},
1970 {"bgnb", s_ignore, 0},
1971 {"end", s_mips_end, 0},
1972 {"endb", s_ignore, 0},
1973 {"ent", s_mips_ent, 0},
1974 {"file", s_mips_file, 0},
1975 {"fmask", s_mips_mask, 'F'},
1976 {"frame", s_mips_frame, 0},
1977 {"loc", s_mips_loc, 0},
1978 {"mask", s_mips_mask, 'R'},
1979 {"verstamp", s_ignore, 0},
1980 { NULL, NULL, 0 },
1981 };
1982
1983 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1984 purpose of the `.dc.a' internal pseudo-op. */
1985
1986 int
1987 mips_address_bytes (void)
1988 {
1989 file_mips_check_options ();
1990 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1991 }
1992
1993 extern void pop_insert (const pseudo_typeS *);
1994
1995 void
1996 mips_pop_insert (void)
1997 {
1998 pop_insert (mips_pseudo_table);
1999 if (! ECOFF_DEBUGGING)
2000 pop_insert (mips_nonecoff_pseudo_table);
2001 }
2002 \f
2003 /* Symbols labelling the current insn. */
2004
2005 struct insn_label_list
2006 {
2007 struct insn_label_list *next;
2008 symbolS *label;
2009 };
2010
2011 static struct insn_label_list *free_insn_labels;
2012 #define label_list tc_segment_info_data.labels
2013
2014 static void mips_clear_insn_labels (void);
2015 static void mips_mark_labels (void);
2016 static void mips_compressed_mark_labels (void);
2017
2018 static inline void
2019 mips_clear_insn_labels (void)
2020 {
2021 struct insn_label_list **pl;
2022 segment_info_type *si;
2023
2024 if (now_seg)
2025 {
2026 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
2027 ;
2028
2029 si = seg_info (now_seg);
2030 *pl = si->label_list;
2031 si->label_list = NULL;
2032 }
2033 }
2034
2035 /* Mark instruction labels in MIPS16/microMIPS mode. */
2036
2037 static inline void
2038 mips_mark_labels (void)
2039 {
2040 if (HAVE_CODE_COMPRESSION)
2041 mips_compressed_mark_labels ();
2042 }
2043 \f
2044 static char *expr_end;
2045
2046 /* An expression in a macro instruction. This is set by mips_ip and
2047 mips16_ip and when populated is always an O_constant. */
2048
2049 static expressionS imm_expr;
2050
2051 /* The relocatable field in an instruction and the relocs associated
2052 with it. These variables are used for instructions like LUI and
2053 JAL as well as true offsets. They are also used for address
2054 operands in macros. */
2055
2056 static expressionS offset_expr;
2057 static bfd_reloc_code_real_type offset_reloc[3]
2058 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2059
2060 /* This is set to the resulting size of the instruction to be produced
2061 by mips16_ip if an explicit extension is used or by mips_ip if an
2062 explicit size is supplied. */
2063
2064 static unsigned int forced_insn_length;
2065
2066 /* True if we are assembling an instruction. All dot symbols defined during
2067 this time should be treated as code labels. */
2068
2069 static bfd_boolean mips_assembling_insn;
2070
2071 /* The pdr segment for per procedure frame/regmask info. Not used for
2072 ECOFF debugging. */
2073
2074 static segT pdr_seg;
2075
2076 /* The default target format to use. */
2077
2078 #if defined (TE_FreeBSD)
2079 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2080 #elif defined (TE_TMIPS)
2081 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2082 #else
2083 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2084 #endif
2085
2086 const char *
2087 mips_target_format (void)
2088 {
2089 switch (OUTPUT_FLAVOR)
2090 {
2091 case bfd_target_elf_flavour:
2092 #ifdef TE_VXWORKS
2093 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2094 return (target_big_endian
2095 ? "elf32-bigmips-vxworks"
2096 : "elf32-littlemips-vxworks");
2097 #endif
2098 return (target_big_endian
2099 ? (HAVE_64BIT_OBJECTS
2100 ? ELF_TARGET ("elf64-", "big")
2101 : (HAVE_NEWABI
2102 ? ELF_TARGET ("elf32-n", "big")
2103 : ELF_TARGET ("elf32-", "big")))
2104 : (HAVE_64BIT_OBJECTS
2105 ? ELF_TARGET ("elf64-", "little")
2106 : (HAVE_NEWABI
2107 ? ELF_TARGET ("elf32-n", "little")
2108 : ELF_TARGET ("elf32-", "little"))));
2109 default:
2110 abort ();
2111 return NULL;
2112 }
2113 }
2114
2115 /* Return the ISA revision that is currently in use, or 0 if we are
2116 generating code for MIPS V or below. */
2117
2118 static int
2119 mips_isa_rev (void)
2120 {
2121 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2122 return 2;
2123
2124 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2125 return 3;
2126
2127 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2128 return 5;
2129
2130 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2131 return 6;
2132
2133 /* microMIPS implies revision 2 or above. */
2134 if (mips_opts.micromips)
2135 return 2;
2136
2137 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2138 return 1;
2139
2140 return 0;
2141 }
2142
2143 /* Return the mask of all ASEs that are revisions of those in FLAGS. */
2144
2145 static unsigned int
2146 mips_ase_mask (unsigned int flags)
2147 {
2148 unsigned int i;
2149
2150 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2151 if (flags & mips_ase_groups[i])
2152 flags |= mips_ase_groups[i];
2153 return flags;
2154 }
2155
2156 /* Check whether the current ISA supports ASE. Issue a warning if
2157 appropriate. */
2158
2159 static void
2160 mips_check_isa_supports_ase (const struct mips_ase *ase)
2161 {
2162 const char *base;
2163 int min_rev, size;
2164 static unsigned int warned_isa;
2165 static unsigned int warned_fp32;
2166
2167 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2168 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2169 else
2170 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2171 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2172 && (warned_isa & ase->flags) != ase->flags)
2173 {
2174 warned_isa |= ase->flags;
2175 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2176 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2177 if (min_rev < 0)
2178 as_warn (_("the %d-bit %s architecture does not support the"
2179 " `%s' extension"), size, base, ase->name);
2180 else
2181 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
2182 ase->name, base, size, min_rev);
2183 }
2184 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2185 && (warned_isa & ase->flags) != ase->flags)
2186 {
2187 warned_isa |= ase->flags;
2188 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2189 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2190 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2191 ase->name, base, size, ase->rem_rev);
2192 }
2193
2194 if ((ase->flags & FP64_ASES)
2195 && mips_opts.fp != 64
2196 && (warned_fp32 & ase->flags) != ase->flags)
2197 {
2198 warned_fp32 |= ase->flags;
2199 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
2200 }
2201 }
2202
2203 /* Check all enabled ASEs to see whether they are supported by the
2204 chosen architecture. */
2205
2206 static void
2207 mips_check_isa_supports_ases (void)
2208 {
2209 unsigned int i, mask;
2210
2211 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2212 {
2213 mask = mips_ase_mask (mips_ases[i].flags);
2214 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2215 mips_check_isa_supports_ase (&mips_ases[i]);
2216 }
2217 }
2218
2219 /* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2220 that were affected. */
2221
2222 static unsigned int
2223 mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2224 bfd_boolean enabled_p)
2225 {
2226 unsigned int mask;
2227
2228 mask = mips_ase_mask (ase->flags);
2229 opts->ase &= ~mask;
2230
2231 /* Clear combination ASE flags, which need to be recalculated based on
2232 updated regular ASE settings. */
2233 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT | ASE_EVA_R6);
2234
2235 if (enabled_p)
2236 opts->ase |= ase->flags;
2237
2238 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2239 instructions which are only valid when both ASEs are enabled.
2240 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2241 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2242 {
2243 opts->ase |= ASE_XPA_VIRT;
2244 mask |= ASE_XPA_VIRT;
2245 }
2246 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2247 {
2248 opts->ase |= ASE_MIPS16E2_MT;
2249 mask |= ASE_MIPS16E2_MT;
2250 }
2251
2252 /* The EVA Extension has instructions which are only valid when the R6 ISA
2253 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
2254 present. */
2255 if (((opts->ase & ASE_EVA) != 0) && ISA_IS_R6 (opts->isa))
2256 {
2257 opts->ase |= ASE_EVA_R6;
2258 mask |= ASE_EVA_R6;
2259 }
2260
2261 return mask;
2262 }
2263
2264 /* Return the ASE called NAME, or null if none. */
2265
2266 static const struct mips_ase *
2267 mips_lookup_ase (const char *name)
2268 {
2269 unsigned int i;
2270
2271 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2272 if (strcmp (name, mips_ases[i].name) == 0)
2273 return &mips_ases[i];
2274 return NULL;
2275 }
2276
2277 /* Return the length of a microMIPS instruction in bytes. If bits of
2278 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2279 otherwise it is a 32-bit instruction. */
2280
2281 static inline unsigned int
2282 micromips_insn_length (const struct mips_opcode *mo)
2283 {
2284 return mips_opcode_32bit_p (mo) ? 4 : 2;
2285 }
2286
2287 /* Return the length of MIPS16 instruction OPCODE. */
2288
2289 static inline unsigned int
2290 mips16_opcode_length (unsigned long opcode)
2291 {
2292 return (opcode >> 16) == 0 ? 2 : 4;
2293 }
2294
2295 /* Return the length of instruction INSN. */
2296
2297 static inline unsigned int
2298 insn_length (const struct mips_cl_insn *insn)
2299 {
2300 if (mips_opts.micromips)
2301 return micromips_insn_length (insn->insn_mo);
2302 else if (mips_opts.mips16)
2303 return mips16_opcode_length (insn->insn_opcode);
2304 else
2305 return 4;
2306 }
2307
2308 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2309
2310 static void
2311 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2312 {
2313 size_t i;
2314
2315 insn->insn_mo = mo;
2316 insn->insn_opcode = mo->match;
2317 insn->frag = NULL;
2318 insn->where = 0;
2319 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2320 insn->fixp[i] = NULL;
2321 insn->fixed_p = (mips_opts.noreorder > 0);
2322 insn->noreorder_p = (mips_opts.noreorder > 0);
2323 insn->mips16_absolute_jump_p = 0;
2324 insn->complete_p = 0;
2325 insn->cleared_p = 0;
2326 }
2327
2328 /* Get a list of all the operands in INSN. */
2329
2330 static const struct mips_operand_array *
2331 insn_operands (const struct mips_cl_insn *insn)
2332 {
2333 if (insn->insn_mo >= &mips_opcodes[0]
2334 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2335 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2336
2337 if (insn->insn_mo >= &mips16_opcodes[0]
2338 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2339 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2340
2341 if (insn->insn_mo >= &micromips_opcodes[0]
2342 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2343 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2344
2345 abort ();
2346 }
2347
2348 /* Get a description of operand OPNO of INSN. */
2349
2350 static const struct mips_operand *
2351 insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2352 {
2353 const struct mips_operand_array *operands;
2354
2355 operands = insn_operands (insn);
2356 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2357 abort ();
2358 return operands->operand[opno];
2359 }
2360
2361 /* Install UVAL as the value of OPERAND in INSN. */
2362
2363 static inline void
2364 insn_insert_operand (struct mips_cl_insn *insn,
2365 const struct mips_operand *operand, unsigned int uval)
2366 {
2367 if (mips_opts.mips16
2368 && operand->type == OP_INT && operand->lsb == 0
2369 && mips_opcode_32bit_p (insn->insn_mo))
2370 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2371 else
2372 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2373 }
2374
2375 /* Extract the value of OPERAND from INSN. */
2376
2377 static inline unsigned
2378 insn_extract_operand (const struct mips_cl_insn *insn,
2379 const struct mips_operand *operand)
2380 {
2381 return mips_extract_operand (operand, insn->insn_opcode);
2382 }
2383
2384 /* Record the current MIPS16/microMIPS mode in now_seg. */
2385
2386 static void
2387 mips_record_compressed_mode (void)
2388 {
2389 segment_info_type *si;
2390
2391 si = seg_info (now_seg);
2392 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2393 si->tc_segment_info_data.mips16 = mips_opts.mips16;
2394 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2395 si->tc_segment_info_data.micromips = mips_opts.micromips;
2396 }
2397
2398 /* Read a standard MIPS instruction from BUF. */
2399
2400 static unsigned long
2401 read_insn (char *buf)
2402 {
2403 if (target_big_endian)
2404 return bfd_getb32 ((bfd_byte *) buf);
2405 else
2406 return bfd_getl32 ((bfd_byte *) buf);
2407 }
2408
2409 /* Write standard MIPS instruction INSN to BUF. Return a pointer to
2410 the next byte. */
2411
2412 static char *
2413 write_insn (char *buf, unsigned int insn)
2414 {
2415 md_number_to_chars (buf, insn, 4);
2416 return buf + 4;
2417 }
2418
2419 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2420 has length LENGTH. */
2421
2422 static unsigned long
2423 read_compressed_insn (char *buf, unsigned int length)
2424 {
2425 unsigned long insn;
2426 unsigned int i;
2427
2428 insn = 0;
2429 for (i = 0; i < length; i += 2)
2430 {
2431 insn <<= 16;
2432 if (target_big_endian)
2433 insn |= bfd_getb16 ((char *) buf);
2434 else
2435 insn |= bfd_getl16 ((char *) buf);
2436 buf += 2;
2437 }
2438 return insn;
2439 }
2440
2441 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2442 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2443
2444 static char *
2445 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2446 {
2447 unsigned int i;
2448
2449 for (i = 0; i < length; i += 2)
2450 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2451 return buf + length;
2452 }
2453
2454 /* Install INSN at the location specified by its "frag" and "where" fields. */
2455
2456 static void
2457 install_insn (const struct mips_cl_insn *insn)
2458 {
2459 char *f = insn->frag->fr_literal + insn->where;
2460 if (HAVE_CODE_COMPRESSION)
2461 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2462 else
2463 write_insn (f, insn->insn_opcode);
2464 mips_record_compressed_mode ();
2465 }
2466
2467 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2468 and install the opcode in the new location. */
2469
2470 static void
2471 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2472 {
2473 size_t i;
2474
2475 insn->frag = frag;
2476 insn->where = where;
2477 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2478 if (insn->fixp[i] != NULL)
2479 {
2480 insn->fixp[i]->fx_frag = frag;
2481 insn->fixp[i]->fx_where = where;
2482 }
2483 install_insn (insn);
2484 }
2485
2486 /* Add INSN to the end of the output. */
2487
2488 static void
2489 add_fixed_insn (struct mips_cl_insn *insn)
2490 {
2491 char *f = frag_more (insn_length (insn));
2492 move_insn (insn, frag_now, f - frag_now->fr_literal);
2493 }
2494
2495 /* Start a variant frag and move INSN to the start of the variant part,
2496 marking it as fixed. The other arguments are as for frag_var. */
2497
2498 static void
2499 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2500 relax_substateT subtype, symbolS *symbol, offsetT offset)
2501 {
2502 frag_grow (max_chars);
2503 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2504 insn->fixed_p = 1;
2505 frag_var (rs_machine_dependent, max_chars, var,
2506 subtype, symbol, offset, NULL);
2507 }
2508
2509 /* Insert N copies of INSN into the history buffer, starting at
2510 position FIRST. Neither FIRST nor N need to be clipped. */
2511
2512 static void
2513 insert_into_history (unsigned int first, unsigned int n,
2514 const struct mips_cl_insn *insn)
2515 {
2516 if (mips_relax.sequence != 2)
2517 {
2518 unsigned int i;
2519
2520 for (i = ARRAY_SIZE (history); i-- > first;)
2521 if (i >= first + n)
2522 history[i] = history[i - n];
2523 else
2524 history[i] = *insn;
2525 }
2526 }
2527
2528 /* Clear the error in insn_error. */
2529
2530 static void
2531 clear_insn_error (void)
2532 {
2533 memset (&insn_error, 0, sizeof (insn_error));
2534 }
2535
2536 /* Possibly record error message MSG for the current instruction.
2537 If the error is about a particular argument, ARGNUM is the 1-based
2538 number of that argument, otherwise it is 0. FORMAT is the format
2539 of MSG. Return true if MSG was used, false if the current message
2540 was kept. */
2541
2542 static bfd_boolean
2543 set_insn_error_format (int argnum, enum mips_insn_error_format format,
2544 const char *msg)
2545 {
2546 if (argnum == 0)
2547 {
2548 /* Give priority to errors against specific arguments, and to
2549 the first whole-instruction message. */
2550 if (insn_error.msg)
2551 return FALSE;
2552 }
2553 else
2554 {
2555 /* Keep insn_error if it is against a later argument. */
2556 if (argnum < insn_error.min_argnum)
2557 return FALSE;
2558
2559 /* If both errors are against the same argument but are different,
2560 give up on reporting a specific error for this argument.
2561 See the comment about mips_insn_error for details. */
2562 if (argnum == insn_error.min_argnum
2563 && insn_error.msg
2564 && strcmp (insn_error.msg, msg) != 0)
2565 {
2566 insn_error.msg = 0;
2567 insn_error.min_argnum += 1;
2568 return FALSE;
2569 }
2570 }
2571 insn_error.min_argnum = argnum;
2572 insn_error.format = format;
2573 insn_error.msg = msg;
2574 return TRUE;
2575 }
2576
2577 /* Record an instruction error with no % format fields. ARGNUM and MSG are
2578 as for set_insn_error_format. */
2579
2580 static void
2581 set_insn_error (int argnum, const char *msg)
2582 {
2583 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2584 }
2585
2586 /* Record an instruction error with one %d field I. ARGNUM and MSG are
2587 as for set_insn_error_format. */
2588
2589 static void
2590 set_insn_error_i (int argnum, const char *msg, int i)
2591 {
2592 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2593 insn_error.u.i = i;
2594 }
2595
2596 /* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2597 are as for set_insn_error_format. */
2598
2599 static void
2600 set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2601 {
2602 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2603 {
2604 insn_error.u.ss[0] = s1;
2605 insn_error.u.ss[1] = s2;
2606 }
2607 }
2608
2609 /* Report the error in insn_error, which is against assembly code STR. */
2610
2611 static void
2612 report_insn_error (const char *str)
2613 {
2614 const char *msg = concat (insn_error.msg, " `%s'", NULL);
2615
2616 switch (insn_error.format)
2617 {
2618 case ERR_FMT_PLAIN:
2619 as_bad (msg, str);
2620 break;
2621
2622 case ERR_FMT_I:
2623 as_bad (msg, insn_error.u.i, str);
2624 break;
2625
2626 case ERR_FMT_SS:
2627 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2628 break;
2629 }
2630
2631 free ((char *) msg);
2632 }
2633
2634 /* Initialize vr4120_conflicts. There is a bit of duplication here:
2635 the idea is to make it obvious at a glance that each errata is
2636 included. */
2637
2638 static void
2639 init_vr4120_conflicts (void)
2640 {
2641 #define CONFLICT(FIRST, SECOND) \
2642 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2643
2644 /* Errata 21 - [D]DIV[U] after [D]MACC */
2645 CONFLICT (MACC, DIV);
2646 CONFLICT (DMACC, DIV);
2647
2648 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2649 CONFLICT (DMULT, DMULT);
2650 CONFLICT (DMULT, DMACC);
2651 CONFLICT (DMACC, DMULT);
2652 CONFLICT (DMACC, DMACC);
2653
2654 /* Errata 24 - MT{LO,HI} after [D]MACC */
2655 CONFLICT (MACC, MTHILO);
2656 CONFLICT (DMACC, MTHILO);
2657
2658 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2659 instruction is executed immediately after a MACC or DMACC
2660 instruction, the result of [either instruction] is incorrect." */
2661 CONFLICT (MACC, MULT);
2662 CONFLICT (MACC, DMULT);
2663 CONFLICT (DMACC, MULT);
2664 CONFLICT (DMACC, DMULT);
2665
2666 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2667 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2668 DDIV or DDIVU instruction, the result of the MACC or
2669 DMACC instruction is incorrect.". */
2670 CONFLICT (DMULT, MACC);
2671 CONFLICT (DMULT, DMACC);
2672 CONFLICT (DIV, MACC);
2673 CONFLICT (DIV, DMACC);
2674
2675 #undef CONFLICT
2676 }
2677
2678 struct regname {
2679 const char *name;
2680 unsigned int num;
2681 };
2682
2683 #define RNUM_MASK 0x00000ff
2684 #define RTYPE_MASK 0x0ffff00
2685 #define RTYPE_NUM 0x0000100
2686 #define RTYPE_FPU 0x0000200
2687 #define RTYPE_FCC 0x0000400
2688 #define RTYPE_VEC 0x0000800
2689 #define RTYPE_GP 0x0001000
2690 #define RTYPE_CP0 0x0002000
2691 #define RTYPE_PC 0x0004000
2692 #define RTYPE_ACC 0x0008000
2693 #define RTYPE_CCC 0x0010000
2694 #define RTYPE_VI 0x0020000
2695 #define RTYPE_VF 0x0040000
2696 #define RTYPE_R5900_I 0x0080000
2697 #define RTYPE_R5900_Q 0x0100000
2698 #define RTYPE_R5900_R 0x0200000
2699 #define RTYPE_R5900_ACC 0x0400000
2700 #define RTYPE_MSA 0x0800000
2701 #define RWARN 0x8000000
2702
2703 #define GENERIC_REGISTER_NUMBERS \
2704 {"$0", RTYPE_NUM | 0}, \
2705 {"$1", RTYPE_NUM | 1}, \
2706 {"$2", RTYPE_NUM | 2}, \
2707 {"$3", RTYPE_NUM | 3}, \
2708 {"$4", RTYPE_NUM | 4}, \
2709 {"$5", RTYPE_NUM | 5}, \
2710 {"$6", RTYPE_NUM | 6}, \
2711 {"$7", RTYPE_NUM | 7}, \
2712 {"$8", RTYPE_NUM | 8}, \
2713 {"$9", RTYPE_NUM | 9}, \
2714 {"$10", RTYPE_NUM | 10}, \
2715 {"$11", RTYPE_NUM | 11}, \
2716 {"$12", RTYPE_NUM | 12}, \
2717 {"$13", RTYPE_NUM | 13}, \
2718 {"$14", RTYPE_NUM | 14}, \
2719 {"$15", RTYPE_NUM | 15}, \
2720 {"$16", RTYPE_NUM | 16}, \
2721 {"$17", RTYPE_NUM | 17}, \
2722 {"$18", RTYPE_NUM | 18}, \
2723 {"$19", RTYPE_NUM | 19}, \
2724 {"$20", RTYPE_NUM | 20}, \
2725 {"$21", RTYPE_NUM | 21}, \
2726 {"$22", RTYPE_NUM | 22}, \
2727 {"$23", RTYPE_NUM | 23}, \
2728 {"$24", RTYPE_NUM | 24}, \
2729 {"$25", RTYPE_NUM | 25}, \
2730 {"$26", RTYPE_NUM | 26}, \
2731 {"$27", RTYPE_NUM | 27}, \
2732 {"$28", RTYPE_NUM | 28}, \
2733 {"$29", RTYPE_NUM | 29}, \
2734 {"$30", RTYPE_NUM | 30}, \
2735 {"$31", RTYPE_NUM | 31}
2736
2737 #define FPU_REGISTER_NAMES \
2738 {"$f0", RTYPE_FPU | 0}, \
2739 {"$f1", RTYPE_FPU | 1}, \
2740 {"$f2", RTYPE_FPU | 2}, \
2741 {"$f3", RTYPE_FPU | 3}, \
2742 {"$f4", RTYPE_FPU | 4}, \
2743 {"$f5", RTYPE_FPU | 5}, \
2744 {"$f6", RTYPE_FPU | 6}, \
2745 {"$f7", RTYPE_FPU | 7}, \
2746 {"$f8", RTYPE_FPU | 8}, \
2747 {"$f9", RTYPE_FPU | 9}, \
2748 {"$f10", RTYPE_FPU | 10}, \
2749 {"$f11", RTYPE_FPU | 11}, \
2750 {"$f12", RTYPE_FPU | 12}, \
2751 {"$f13", RTYPE_FPU | 13}, \
2752 {"$f14", RTYPE_FPU | 14}, \
2753 {"$f15", RTYPE_FPU | 15}, \
2754 {"$f16", RTYPE_FPU | 16}, \
2755 {"$f17", RTYPE_FPU | 17}, \
2756 {"$f18", RTYPE_FPU | 18}, \
2757 {"$f19", RTYPE_FPU | 19}, \
2758 {"$f20", RTYPE_FPU | 20}, \
2759 {"$f21", RTYPE_FPU | 21}, \
2760 {"$f22", RTYPE_FPU | 22}, \
2761 {"$f23", RTYPE_FPU | 23}, \
2762 {"$f24", RTYPE_FPU | 24}, \
2763 {"$f25", RTYPE_FPU | 25}, \
2764 {"$f26", RTYPE_FPU | 26}, \
2765 {"$f27", RTYPE_FPU | 27}, \
2766 {"$f28", RTYPE_FPU | 28}, \
2767 {"$f29", RTYPE_FPU | 29}, \
2768 {"$f30", RTYPE_FPU | 30}, \
2769 {"$f31", RTYPE_FPU | 31}
2770
2771 #define FPU_CONDITION_CODE_NAMES \
2772 {"$fcc0", RTYPE_FCC | 0}, \
2773 {"$fcc1", RTYPE_FCC | 1}, \
2774 {"$fcc2", RTYPE_FCC | 2}, \
2775 {"$fcc3", RTYPE_FCC | 3}, \
2776 {"$fcc4", RTYPE_FCC | 4}, \
2777 {"$fcc5", RTYPE_FCC | 5}, \
2778 {"$fcc6", RTYPE_FCC | 6}, \
2779 {"$fcc7", RTYPE_FCC | 7}
2780
2781 #define COPROC_CONDITION_CODE_NAMES \
2782 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2783 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2784 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2785 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2786 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2787 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2788 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2789 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2790
2791 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2792 {"$a4", RTYPE_GP | 8}, \
2793 {"$a5", RTYPE_GP | 9}, \
2794 {"$a6", RTYPE_GP | 10}, \
2795 {"$a7", RTYPE_GP | 11}, \
2796 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2797 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2798 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2799 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2800 {"$t0", RTYPE_GP | 12}, \
2801 {"$t1", RTYPE_GP | 13}, \
2802 {"$t2", RTYPE_GP | 14}, \
2803 {"$t3", RTYPE_GP | 15}
2804
2805 #define O32_SYMBOLIC_REGISTER_NAMES \
2806 {"$t0", RTYPE_GP | 8}, \
2807 {"$t1", RTYPE_GP | 9}, \
2808 {"$t2", RTYPE_GP | 10}, \
2809 {"$t3", RTYPE_GP | 11}, \
2810 {"$t4", RTYPE_GP | 12}, \
2811 {"$t5", RTYPE_GP | 13}, \
2812 {"$t6", RTYPE_GP | 14}, \
2813 {"$t7", RTYPE_GP | 15}, \
2814 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2815 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2816 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2817 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2818
2819 /* Remaining symbolic register names. */
2820 #define SYMBOLIC_REGISTER_NAMES \
2821 {"$zero", RTYPE_GP | 0}, \
2822 {"$at", RTYPE_GP | 1}, \
2823 {"$AT", RTYPE_GP | 1}, \
2824 {"$v0", RTYPE_GP | 2}, \
2825 {"$v1", RTYPE_GP | 3}, \
2826 {"$a0", RTYPE_GP | 4}, \
2827 {"$a1", RTYPE_GP | 5}, \
2828 {"$a2", RTYPE_GP | 6}, \
2829 {"$a3", RTYPE_GP | 7}, \
2830 {"$s0", RTYPE_GP | 16}, \
2831 {"$s1", RTYPE_GP | 17}, \
2832 {"$s2", RTYPE_GP | 18}, \
2833 {"$s3", RTYPE_GP | 19}, \
2834 {"$s4", RTYPE_GP | 20}, \
2835 {"$s5", RTYPE_GP | 21}, \
2836 {"$s6", RTYPE_GP | 22}, \
2837 {"$s7", RTYPE_GP | 23}, \
2838 {"$t8", RTYPE_GP | 24}, \
2839 {"$t9", RTYPE_GP | 25}, \
2840 {"$k0", RTYPE_GP | 26}, \
2841 {"$kt0", RTYPE_GP | 26}, \
2842 {"$k1", RTYPE_GP | 27}, \
2843 {"$kt1", RTYPE_GP | 27}, \
2844 {"$gp", RTYPE_GP | 28}, \
2845 {"$sp", RTYPE_GP | 29}, \
2846 {"$s8", RTYPE_GP | 30}, \
2847 {"$fp", RTYPE_GP | 30}, \
2848 {"$ra", RTYPE_GP | 31}
2849
2850 #define MIPS16_SPECIAL_REGISTER_NAMES \
2851 {"$pc", RTYPE_PC | 0}
2852
2853 #define MDMX_VECTOR_REGISTER_NAMES \
2854 /* {"$v0", RTYPE_VEC | 0}, Clash with REG 2 above. */ \
2855 /* {"$v1", RTYPE_VEC | 1}, Clash with REG 3 above. */ \
2856 {"$v2", RTYPE_VEC | 2}, \
2857 {"$v3", RTYPE_VEC | 3}, \
2858 {"$v4", RTYPE_VEC | 4}, \
2859 {"$v5", RTYPE_VEC | 5}, \
2860 {"$v6", RTYPE_VEC | 6}, \
2861 {"$v7", RTYPE_VEC | 7}, \
2862 {"$v8", RTYPE_VEC | 8}, \
2863 {"$v9", RTYPE_VEC | 9}, \
2864 {"$v10", RTYPE_VEC | 10}, \
2865 {"$v11", RTYPE_VEC | 11}, \
2866 {"$v12", RTYPE_VEC | 12}, \
2867 {"$v13", RTYPE_VEC | 13}, \
2868 {"$v14", RTYPE_VEC | 14}, \
2869 {"$v15", RTYPE_VEC | 15}, \
2870 {"$v16", RTYPE_VEC | 16}, \
2871 {"$v17", RTYPE_VEC | 17}, \
2872 {"$v18", RTYPE_VEC | 18}, \
2873 {"$v19", RTYPE_VEC | 19}, \
2874 {"$v20", RTYPE_VEC | 20}, \
2875 {"$v21", RTYPE_VEC | 21}, \
2876 {"$v22", RTYPE_VEC | 22}, \
2877 {"$v23", RTYPE_VEC | 23}, \
2878 {"$v24", RTYPE_VEC | 24}, \
2879 {"$v25", RTYPE_VEC | 25}, \
2880 {"$v26", RTYPE_VEC | 26}, \
2881 {"$v27", RTYPE_VEC | 27}, \
2882 {"$v28", RTYPE_VEC | 28}, \
2883 {"$v29", RTYPE_VEC | 29}, \
2884 {"$v30", RTYPE_VEC | 30}, \
2885 {"$v31", RTYPE_VEC | 31}
2886
2887 #define R5900_I_NAMES \
2888 {"$I", RTYPE_R5900_I | 0}
2889
2890 #define R5900_Q_NAMES \
2891 {"$Q", RTYPE_R5900_Q | 0}
2892
2893 #define R5900_R_NAMES \
2894 {"$R", RTYPE_R5900_R | 0}
2895
2896 #define R5900_ACC_NAMES \
2897 {"$ACC", RTYPE_R5900_ACC | 0 }
2898
2899 #define MIPS_DSP_ACCUMULATOR_NAMES \
2900 {"$ac0", RTYPE_ACC | 0}, \
2901 {"$ac1", RTYPE_ACC | 1}, \
2902 {"$ac2", RTYPE_ACC | 2}, \
2903 {"$ac3", RTYPE_ACC | 3}
2904
2905 static const struct regname reg_names[] = {
2906 GENERIC_REGISTER_NUMBERS,
2907 FPU_REGISTER_NAMES,
2908 FPU_CONDITION_CODE_NAMES,
2909 COPROC_CONDITION_CODE_NAMES,
2910
2911 /* The $txx registers depends on the abi,
2912 these will be added later into the symbol table from
2913 one of the tables below once mips_abi is set after
2914 parsing of arguments from the command line. */
2915 SYMBOLIC_REGISTER_NAMES,
2916
2917 MIPS16_SPECIAL_REGISTER_NAMES,
2918 MDMX_VECTOR_REGISTER_NAMES,
2919 R5900_I_NAMES,
2920 R5900_Q_NAMES,
2921 R5900_R_NAMES,
2922 R5900_ACC_NAMES,
2923 MIPS_DSP_ACCUMULATOR_NAMES,
2924 {0, 0}
2925 };
2926
2927 static const struct regname reg_names_o32[] = {
2928 O32_SYMBOLIC_REGISTER_NAMES,
2929 {0, 0}
2930 };
2931
2932 static const struct regname reg_names_n32n64[] = {
2933 N32N64_SYMBOLIC_REGISTER_NAMES,
2934 {0, 0}
2935 };
2936
2937 /* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2938 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2939 of these register symbols, return the associated vector register,
2940 otherwise return SYMVAL itself. */
2941
2942 static unsigned int
2943 mips_prefer_vec_regno (unsigned int symval)
2944 {
2945 if ((symval & -2) == (RTYPE_GP | 2))
2946 return RTYPE_VEC | (symval & 1);
2947 return symval;
2948 }
2949
2950 /* Return true if string [S, E) is a valid register name, storing its
2951 symbol value in *SYMVAL_PTR if so. */
2952
2953 static bfd_boolean
2954 mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
2955 {
2956 char save_c;
2957 symbolS *symbol;
2958
2959 /* Terminate name. */
2960 save_c = *e;
2961 *e = '\0';
2962
2963 /* Look up the name. */
2964 symbol = symbol_find (s);
2965 *e = save_c;
2966
2967 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2968 return FALSE;
2969
2970 *symval_ptr = S_GET_VALUE (symbol);
2971 return TRUE;
2972 }
2973
2974 /* Return true if the string at *SPTR is a valid register name. Allow it
2975 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2976 is nonnull.
2977
2978 When returning true, move *SPTR past the register, store the
2979 register's symbol value in *SYMVAL_PTR and the channel mask in
2980 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2981 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2982 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2983
2984 static bfd_boolean
2985 mips_parse_register (char **sptr, unsigned int *symval_ptr,
2986 unsigned int *channels_ptr)
2987 {
2988 char *s, *e, *m;
2989 const char *q;
2990 unsigned int channels, symval, bit;
2991
2992 /* Find end of name. */
2993 s = e = *sptr;
2994 if (is_name_beginner (*e))
2995 ++e;
2996 while (is_part_of_name (*e))
2997 ++e;
2998
2999 channels = 0;
3000 if (!mips_parse_register_1 (s, e, &symval))
3001 {
3002 if (!channels_ptr)
3003 return FALSE;
3004
3005 /* Eat characters from the end of the string that are valid
3006 channel suffixes. The preceding register must be $ACC or
3007 end with a digit, so there is no ambiguity. */
3008 bit = 1;
3009 m = e;
3010 for (q = "wzyx"; *q; q++, bit <<= 1)
3011 if (m > s && m[-1] == *q)
3012 {
3013 --m;
3014 channels |= bit;
3015 }
3016
3017 if (channels == 0
3018 || !mips_parse_register_1 (s, m, &symval)
3019 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
3020 return FALSE;
3021 }
3022
3023 *sptr = e;
3024 *symval_ptr = symval;
3025 if (channels_ptr)
3026 *channels_ptr = channels;
3027 return TRUE;
3028 }
3029
3030 /* Check if SPTR points at a valid register specifier according to TYPES.
3031 If so, then return 1, advance S to consume the specifier and store
3032 the register's number in REGNOP, otherwise return 0. */
3033
3034 static int
3035 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
3036 {
3037 unsigned int regno;
3038
3039 if (mips_parse_register (s, &regno, NULL))
3040 {
3041 if (types & RTYPE_VEC)
3042 regno = mips_prefer_vec_regno (regno);
3043 if (regno & types)
3044 regno &= RNUM_MASK;
3045 else
3046 regno = ~0;
3047 }
3048 else
3049 {
3050 if (types & RWARN)
3051 as_warn (_("unrecognized register name `%s'"), *s);
3052 regno = ~0;
3053 }
3054 if (regnop)
3055 *regnop = regno;
3056 return regno <= RNUM_MASK;
3057 }
3058
3059 /* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
3060 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
3061
3062 static char *
3063 mips_parse_vu0_channels (char *s, unsigned int *channels)
3064 {
3065 unsigned int i;
3066
3067 *channels = 0;
3068 for (i = 0; i < 4; i++)
3069 if (*s == "xyzw"[i])
3070 {
3071 *channels |= 1 << (3 - i);
3072 ++s;
3073 }
3074 return s;
3075 }
3076
3077 /* Token types for parsed operand lists. */
3078 enum mips_operand_token_type {
3079 /* A plain register, e.g. $f2. */
3080 OT_REG,
3081
3082 /* A 4-bit XYZW channel mask. */
3083 OT_CHANNELS,
3084
3085 /* A constant vector index, e.g. [1]. */
3086 OT_INTEGER_INDEX,
3087
3088 /* A register vector index, e.g. [$2]. */
3089 OT_REG_INDEX,
3090
3091 /* A continuous range of registers, e.g. $s0-$s4. */
3092 OT_REG_RANGE,
3093
3094 /* A (possibly relocated) expression. */
3095 OT_INTEGER,
3096
3097 /* A floating-point value. */
3098 OT_FLOAT,
3099
3100 /* A single character. This can be '(', ')' or ',', but '(' only appears
3101 before OT_REGs. */
3102 OT_CHAR,
3103
3104 /* A doubled character, either "--" or "++". */
3105 OT_DOUBLE_CHAR,
3106
3107 /* The end of the operand list. */
3108 OT_END
3109 };
3110
3111 /* A parsed operand token. */
3112 struct mips_operand_token
3113 {
3114 /* The type of token. */
3115 enum mips_operand_token_type type;
3116 union
3117 {
3118 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
3119 unsigned int regno;
3120
3121 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3122 unsigned int channels;
3123
3124 /* The integer value of an OT_INTEGER_INDEX. */
3125 addressT index;
3126
3127 /* The two register symbol values involved in an OT_REG_RANGE. */
3128 struct {
3129 unsigned int regno1;
3130 unsigned int regno2;
3131 } reg_range;
3132
3133 /* The value of an OT_INTEGER. The value is represented as an
3134 expression and the relocation operators that were applied to
3135 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3136 relocation operators were used. */
3137 struct {
3138 expressionS value;
3139 bfd_reloc_code_real_type relocs[3];
3140 } integer;
3141
3142 /* The binary data for an OT_FLOAT constant, and the number of bytes
3143 in the constant. */
3144 struct {
3145 unsigned char data[8];
3146 int length;
3147 } flt;
3148
3149 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
3150 char ch;
3151 } u;
3152 };
3153
3154 /* An obstack used to construct lists of mips_operand_tokens. */
3155 static struct obstack mips_operand_tokens;
3156
3157 /* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3158
3159 static void
3160 mips_add_token (struct mips_operand_token *token,
3161 enum mips_operand_token_type type)
3162 {
3163 token->type = type;
3164 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3165 }
3166
3167 /* Check whether S is '(' followed by a register name. Add OT_CHAR
3168 and OT_REG tokens for them if so, and return a pointer to the first
3169 unconsumed character. Return null otherwise. */
3170
3171 static char *
3172 mips_parse_base_start (char *s)
3173 {
3174 struct mips_operand_token token;
3175 unsigned int regno, channels;
3176 bfd_boolean decrement_p;
3177
3178 if (*s != '(')
3179 return 0;
3180
3181 ++s;
3182 SKIP_SPACE_TABS (s);
3183
3184 /* Only match "--" as part of a base expression. In other contexts "--X"
3185 is a double negative. */
3186 decrement_p = (s[0] == '-' && s[1] == '-');
3187 if (decrement_p)
3188 {
3189 s += 2;
3190 SKIP_SPACE_TABS (s);
3191 }
3192
3193 /* Allow a channel specifier because that leads to better error messages
3194 than treating something like "$vf0x++" as an expression. */
3195 if (!mips_parse_register (&s, &regno, &channels))
3196 return 0;
3197
3198 token.u.ch = '(';
3199 mips_add_token (&token, OT_CHAR);
3200
3201 if (decrement_p)
3202 {
3203 token.u.ch = '-';
3204 mips_add_token (&token, OT_DOUBLE_CHAR);
3205 }
3206
3207 token.u.regno = regno;
3208 mips_add_token (&token, OT_REG);
3209
3210 if (channels)
3211 {
3212 token.u.channels = channels;
3213 mips_add_token (&token, OT_CHANNELS);
3214 }
3215
3216 /* For consistency, only match "++" as part of base expressions too. */
3217 SKIP_SPACE_TABS (s);
3218 if (s[0] == '+' && s[1] == '+')
3219 {
3220 s += 2;
3221 token.u.ch = '+';
3222 mips_add_token (&token, OT_DOUBLE_CHAR);
3223 }
3224
3225 return s;
3226 }
3227
3228 /* Parse one or more tokens from S. Return a pointer to the first
3229 unconsumed character on success. Return null if an error was found
3230 and store the error text in insn_error. FLOAT_FORMAT is as for
3231 mips_parse_arguments. */
3232
3233 static char *
3234 mips_parse_argument_token (char *s, char float_format)
3235 {
3236 char *end, *save_in;
3237 const char *err;
3238 unsigned int regno1, regno2, channels;
3239 struct mips_operand_token token;
3240
3241 /* First look for "($reg", since we want to treat that as an
3242 OT_CHAR and OT_REG rather than an expression. */
3243 end = mips_parse_base_start (s);
3244 if (end)
3245 return end;
3246
3247 /* Handle other characters that end up as OT_CHARs. */
3248 if (*s == ')' || *s == ',')
3249 {
3250 token.u.ch = *s;
3251 mips_add_token (&token, OT_CHAR);
3252 ++s;
3253 return s;
3254 }
3255
3256 /* Handle tokens that start with a register. */
3257 if (mips_parse_register (&s, &regno1, &channels))
3258 {
3259 if (channels)
3260 {
3261 /* A register and a VU0 channel suffix. */
3262 token.u.regno = regno1;
3263 mips_add_token (&token, OT_REG);
3264
3265 token.u.channels = channels;
3266 mips_add_token (&token, OT_CHANNELS);
3267 return s;
3268 }
3269
3270 SKIP_SPACE_TABS (s);
3271 if (*s == '-')
3272 {
3273 /* A register range. */
3274 ++s;
3275 SKIP_SPACE_TABS (s);
3276 if (!mips_parse_register (&s, &regno2, NULL))
3277 {
3278 set_insn_error (0, _("invalid register range"));
3279 return 0;
3280 }
3281
3282 token.u.reg_range.regno1 = regno1;
3283 token.u.reg_range.regno2 = regno2;
3284 mips_add_token (&token, OT_REG_RANGE);
3285 return s;
3286 }
3287
3288 /* Add the register itself. */
3289 token.u.regno = regno1;
3290 mips_add_token (&token, OT_REG);
3291
3292 /* Check for a vector index. */
3293 if (*s == '[')
3294 {
3295 ++s;
3296 SKIP_SPACE_TABS (s);
3297 if (mips_parse_register (&s, &token.u.regno, NULL))
3298 mips_add_token (&token, OT_REG_INDEX);
3299 else
3300 {
3301 expressionS element;
3302
3303 my_getExpression (&element, s);
3304 if (element.X_op != O_constant)
3305 {
3306 set_insn_error (0, _("vector element must be constant"));
3307 return 0;
3308 }
3309 s = expr_end;
3310 token.u.index = element.X_add_number;
3311 mips_add_token (&token, OT_INTEGER_INDEX);
3312 }
3313 SKIP_SPACE_TABS (s);
3314 if (*s != ']')
3315 {
3316 set_insn_error (0, _("missing `]'"));
3317 return 0;
3318 }
3319 ++s;
3320 }
3321 return s;
3322 }
3323
3324 if (float_format)
3325 {
3326 /* First try to treat expressions as floats. */
3327 save_in = input_line_pointer;
3328 input_line_pointer = s;
3329 err = md_atof (float_format, (char *) token.u.flt.data,
3330 &token.u.flt.length);
3331 end = input_line_pointer;
3332 input_line_pointer = save_in;
3333 if (err && *err)
3334 {
3335 set_insn_error (0, err);
3336 return 0;
3337 }
3338 if (s != end)
3339 {
3340 mips_add_token (&token, OT_FLOAT);
3341 return end;
3342 }
3343 }
3344
3345 /* Treat everything else as an integer expression. */
3346 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3347 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3348 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3349 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3350 s = expr_end;
3351 mips_add_token (&token, OT_INTEGER);
3352 return s;
3353 }
3354
3355 /* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3356 if expressions should be treated as 32-bit floating-point constants,
3357 'd' if they should be treated as 64-bit floating-point constants,
3358 or 0 if they should be treated as integer expressions (the usual case).
3359
3360 Return a list of tokens on success, otherwise return 0. The caller
3361 must obstack_free the list after use. */
3362
3363 static struct mips_operand_token *
3364 mips_parse_arguments (char *s, char float_format)
3365 {
3366 struct mips_operand_token token;
3367
3368 SKIP_SPACE_TABS (s);
3369 while (*s)
3370 {
3371 s = mips_parse_argument_token (s, float_format);
3372 if (!s)
3373 {
3374 obstack_free (&mips_operand_tokens,
3375 obstack_finish (&mips_operand_tokens));
3376 return 0;
3377 }
3378 SKIP_SPACE_TABS (s);
3379 }
3380 mips_add_token (&token, OT_END);
3381 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
3382 }
3383
3384 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3385 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
3386
3387 static bfd_boolean
3388 is_opcode_valid (const struct mips_opcode *mo)
3389 {
3390 int isa = mips_opts.isa;
3391 int ase = mips_opts.ase;
3392 int fp_s, fp_d;
3393 unsigned int i;
3394
3395 if (ISA_HAS_64BIT_REGS (isa))
3396 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3397 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3398 ase |= mips_ases[i].flags64;
3399
3400 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
3401 return FALSE;
3402
3403 /* Check whether the instruction or macro requires single-precision or
3404 double-precision floating-point support. Note that this information is
3405 stored differently in the opcode table for insns and macros. */
3406 if (mo->pinfo == INSN_MACRO)
3407 {
3408 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3409 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3410 }
3411 else
3412 {
3413 fp_s = mo->pinfo & FP_S;
3414 fp_d = mo->pinfo & FP_D;
3415 }
3416
3417 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3418 return FALSE;
3419
3420 if (fp_s && mips_opts.soft_float)
3421 return FALSE;
3422
3423 return TRUE;
3424 }
3425
3426 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
3427 selected ISA and architecture. */
3428
3429 static bfd_boolean
3430 is_opcode_valid_16 (const struct mips_opcode *mo)
3431 {
3432 int isa = mips_opts.isa;
3433 int ase = mips_opts.ase;
3434 unsigned int i;
3435
3436 if (ISA_HAS_64BIT_REGS (isa))
3437 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3438 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3439 ase |= mips_ases[i].flags64;
3440
3441 return opcode_is_member (mo, isa, ase, mips_opts.arch);
3442 }
3443
3444 /* Return TRUE if the size of the microMIPS opcode MO matches one
3445 explicitly requested. Always TRUE in the standard MIPS mode.
3446 Use is_size_valid_16 for MIPS16 opcodes. */
3447
3448 static bfd_boolean
3449 is_size_valid (const struct mips_opcode *mo)
3450 {
3451 if (!mips_opts.micromips)
3452 return TRUE;
3453
3454 if (mips_opts.insn32)
3455 {
3456 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3457 return FALSE;
3458 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3459 return FALSE;
3460 }
3461 if (!forced_insn_length)
3462 return TRUE;
3463 if (mo->pinfo == INSN_MACRO)
3464 return FALSE;
3465 return forced_insn_length == micromips_insn_length (mo);
3466 }
3467
3468 /* Return TRUE if the size of the MIPS16 opcode MO matches one
3469 explicitly requested. */
3470
3471 static bfd_boolean
3472 is_size_valid_16 (const struct mips_opcode *mo)
3473 {
3474 if (!forced_insn_length)
3475 return TRUE;
3476 if (mo->pinfo == INSN_MACRO)
3477 return FALSE;
3478 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3479 return FALSE;
3480 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3481 return FALSE;
3482 return TRUE;
3483 }
3484
3485 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
3486 of the preceding instruction. Always TRUE in the standard MIPS mode.
3487
3488 We don't accept macros in 16-bit delay slots to avoid a case where
3489 a macro expansion fails because it relies on a preceding 32-bit real
3490 instruction to have matched and does not handle the operands correctly.
3491 The only macros that may expand to 16-bit instructions are JAL that
3492 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3493 and BGT (that likewise cannot be placed in a delay slot) that decay to
3494 a NOP. In all these cases the macros precede any corresponding real
3495 instruction definitions in the opcode table, so they will match in the
3496 second pass where the size of the delay slot is ignored and therefore
3497 produce correct code. */
3498
3499 static bfd_boolean
3500 is_delay_slot_valid (const struct mips_opcode *mo)
3501 {
3502 if (!mips_opts.micromips)
3503 return TRUE;
3504
3505 if (mo->pinfo == INSN_MACRO)
3506 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
3507 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3508 && micromips_insn_length (mo) != 4)
3509 return FALSE;
3510 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3511 && micromips_insn_length (mo) != 2)
3512 return FALSE;
3513
3514 return TRUE;
3515 }
3516
3517 /* For consistency checking, verify that all bits of OPCODE are specified
3518 either by the match/mask part of the instruction definition, or by the
3519 operand list. Also build up a list of operands in OPERANDS.
3520
3521 INSN_BITS says which bits of the instruction are significant.
3522 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3523 provides the mips_operand description of each operand. DECODE_OPERAND
3524 is null for MIPS16 instructions. */
3525
3526 static int
3527 validate_mips_insn (const struct mips_opcode *opcode,
3528 unsigned long insn_bits,
3529 const struct mips_operand *(*decode_operand) (const char *),
3530 struct mips_operand_array *operands)
3531 {
3532 const char *s;
3533 unsigned long used_bits, doubled, undefined, opno, mask;
3534 const struct mips_operand *operand;
3535
3536 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3537 if ((mask & opcode->match) != opcode->match)
3538 {
3539 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3540 opcode->name, opcode->args);
3541 return 0;
3542 }
3543 used_bits = 0;
3544 opno = 0;
3545 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3546 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
3547 for (s = opcode->args; *s; ++s)
3548 switch (*s)
3549 {
3550 case ',':
3551 case '(':
3552 case ')':
3553 break;
3554
3555 case '#':
3556 s++;
3557 break;
3558
3559 default:
3560 if (!decode_operand)
3561 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
3562 else
3563 operand = decode_operand (s);
3564 if (!operand && opcode->pinfo != INSN_MACRO)
3565 {
3566 as_bad (_("internal: unknown operand type: %s %s"),
3567 opcode->name, opcode->args);
3568 return 0;
3569 }
3570 gas_assert (opno < MAX_OPERANDS);
3571 operands->operand[opno] = operand;
3572 if (!decode_operand && operand
3573 && operand->type == OP_INT && operand->lsb == 0
3574 && mips_opcode_32bit_p (opcode))
3575 used_bits |= mips16_immed_extend (-1, operand->size);
3576 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
3577 {
3578 used_bits = mips_insert_operand (operand, used_bits, -1);
3579 if (operand->type == OP_MDMX_IMM_REG)
3580 /* Bit 5 is the format selector (OB vs QH). The opcode table
3581 has separate entries for each format. */
3582 used_bits &= ~(1 << (operand->lsb + 5));
3583 if (operand->type == OP_ENTRY_EXIT_LIST)
3584 used_bits &= ~(mask & 0x700);
3585 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3586 operand field that cannot be fully described with LSB/SIZE. */
3587 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3588 used_bits &= ~0x6000;
3589 }
3590 /* Skip prefix characters. */
3591 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
3592 ++s;
3593 opno += 1;
3594 break;
3595 }
3596 doubled = used_bits & mask & insn_bits;
3597 if (doubled)
3598 {
3599 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3600 " %s %s"), doubled, opcode->name, opcode->args);
3601 return 0;
3602 }
3603 used_bits |= mask;
3604 undefined = ~used_bits & insn_bits;
3605 if (opcode->pinfo != INSN_MACRO && undefined)
3606 {
3607 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3608 undefined, opcode->name, opcode->args);
3609 return 0;
3610 }
3611 used_bits &= ~insn_bits;
3612 if (used_bits)
3613 {
3614 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3615 used_bits, opcode->name, opcode->args);
3616 return 0;
3617 }
3618 return 1;
3619 }
3620
3621 /* The MIPS16 version of validate_mips_insn. */
3622
3623 static int
3624 validate_mips16_insn (const struct mips_opcode *opcode,
3625 struct mips_operand_array *operands)
3626 {
3627 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
3628
3629 return validate_mips_insn (opcode, insn_bits, 0, operands);
3630 }
3631
3632 /* The microMIPS version of validate_mips_insn. */
3633
3634 static int
3635 validate_micromips_insn (const struct mips_opcode *opc,
3636 struct mips_operand_array *operands)
3637 {
3638 unsigned long insn_bits;
3639 unsigned long major;
3640 unsigned int length;
3641
3642 if (opc->pinfo == INSN_MACRO)
3643 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3644 operands);
3645
3646 length = micromips_insn_length (opc);
3647 if (length != 2 && length != 4)
3648 {
3649 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
3650 "%s %s"), length, opc->name, opc->args);
3651 return 0;
3652 }
3653 major = opc->match >> (10 + 8 * (length - 2));
3654 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3655 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3656 {
3657 as_bad (_("internal error: bad microMIPS opcode "
3658 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3659 return 0;
3660 }
3661
3662 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3663 insn_bits = 1 << 4 * length;
3664 insn_bits <<= 4 * length;
3665 insn_bits -= 1;
3666 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3667 operands);
3668 }
3669
3670 /* This function is called once, at assembler startup time. It should set up
3671 all the tables, etc. that the MD part of the assembler will need. */
3672
3673 void
3674 md_begin (void)
3675 {
3676 const char *retval = NULL;
3677 int i = 0;
3678 int broken = 0;
3679
3680 if (mips_pic != NO_PIC)
3681 {
3682 if (g_switch_seen && g_switch_value != 0)
3683 as_bad (_("-G may not be used in position-independent code"));
3684 g_switch_value = 0;
3685 }
3686 else if (mips_abicalls)
3687 {
3688 if (g_switch_seen && g_switch_value != 0)
3689 as_bad (_("-G may not be used with abicalls"));
3690 g_switch_value = 0;
3691 }
3692
3693 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3694 as_warn (_("could not set architecture and machine"));
3695
3696 op_hash = hash_new ();
3697
3698 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
3699 for (i = 0; i < NUMOPCODES;)
3700 {
3701 const char *name = mips_opcodes[i].name;
3702
3703 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
3704 if (retval != NULL)
3705 {
3706 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3707 mips_opcodes[i].name, retval);
3708 /* Probably a memory allocation problem? Give up now. */
3709 as_fatal (_("broken assembler, no assembly attempted"));
3710 }
3711 do
3712 {
3713 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3714 decode_mips_operand, &mips_operands[i]))
3715 broken = 1;
3716
3717 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3718 {
3719 create_insn (&nop_insn, mips_opcodes + i);
3720 if (mips_fix_loongson2f_nop)
3721 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3722 nop_insn.fixed_p = 1;
3723 }
3724
3725 if (sync_insn.insn_mo == NULL && strcmp (name, "sync") == 0)
3726 create_insn (&sync_insn, mips_opcodes + i);
3727
3728 ++i;
3729 }
3730 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3731 }
3732
3733 mips16_op_hash = hash_new ();
3734 mips16_operands = XCNEWVEC (struct mips_operand_array,
3735 bfd_mips16_num_opcodes);
3736
3737 i = 0;
3738 while (i < bfd_mips16_num_opcodes)
3739 {
3740 const char *name = mips16_opcodes[i].name;
3741
3742 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
3743 if (retval != NULL)
3744 as_fatal (_("internal: can't hash `%s': %s"),
3745 mips16_opcodes[i].name, retval);
3746 do
3747 {
3748 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3749 broken = 1;
3750 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3751 {
3752 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3753 mips16_nop_insn.fixed_p = 1;
3754 }
3755 ++i;
3756 }
3757 while (i < bfd_mips16_num_opcodes
3758 && strcmp (mips16_opcodes[i].name, name) == 0);
3759 }
3760
3761 micromips_op_hash = hash_new ();
3762 micromips_operands = XCNEWVEC (struct mips_operand_array,
3763 bfd_micromips_num_opcodes);
3764
3765 i = 0;
3766 while (i < bfd_micromips_num_opcodes)
3767 {
3768 const char *name = micromips_opcodes[i].name;
3769
3770 retval = hash_insert (micromips_op_hash, name,
3771 (void *) &micromips_opcodes[i]);
3772 if (retval != NULL)
3773 as_fatal (_("internal: can't hash `%s': %s"),
3774 micromips_opcodes[i].name, retval);
3775 do
3776 {
3777 struct mips_cl_insn *micromips_nop_insn;
3778
3779 if (!validate_micromips_insn (&micromips_opcodes[i],
3780 &micromips_operands[i]))
3781 broken = 1;
3782
3783 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3784 {
3785 if (micromips_insn_length (micromips_opcodes + i) == 2)
3786 micromips_nop_insn = &micromips_nop16_insn;
3787 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3788 micromips_nop_insn = &micromips_nop32_insn;
3789 else
3790 continue;
3791
3792 if (micromips_nop_insn->insn_mo == NULL
3793 && strcmp (name, "nop") == 0)
3794 {
3795 create_insn (micromips_nop_insn, micromips_opcodes + i);
3796 micromips_nop_insn->fixed_p = 1;
3797 }
3798 }
3799 }
3800 while (++i < bfd_micromips_num_opcodes
3801 && strcmp (micromips_opcodes[i].name, name) == 0);
3802 }
3803
3804 if (broken)
3805 as_fatal (_("broken assembler, no assembly attempted"));
3806
3807 /* We add all the general register names to the symbol table. This
3808 helps us detect invalid uses of them. */
3809 for (i = 0; reg_names[i].name; i++)
3810 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
3811 reg_names[i].num, /* & RNUM_MASK, */
3812 &zero_address_frag));
3813 if (HAVE_NEWABI)
3814 for (i = 0; reg_names_n32n64[i].name; i++)
3815 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
3816 reg_names_n32n64[i].num, /* & RNUM_MASK, */
3817 &zero_address_frag));
3818 else
3819 for (i = 0; reg_names_o32[i].name; i++)
3820 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
3821 reg_names_o32[i].num, /* & RNUM_MASK, */
3822 &zero_address_frag));
3823
3824 for (i = 0; i < 32; i++)
3825 {
3826 char regname[6];
3827
3828 /* R5900 VU0 floating-point register. */
3829 sprintf (regname, "$vf%d", i);
3830 symbol_table_insert (symbol_new (regname, reg_section,
3831 RTYPE_VF | i, &zero_address_frag));
3832
3833 /* R5900 VU0 integer register. */
3834 sprintf (regname, "$vi%d", i);
3835 symbol_table_insert (symbol_new (regname, reg_section,
3836 RTYPE_VI | i, &zero_address_frag));
3837
3838 /* MSA register. */
3839 sprintf (regname, "$w%d", i);
3840 symbol_table_insert (symbol_new (regname, reg_section,
3841 RTYPE_MSA | i, &zero_address_frag));
3842 }
3843
3844 obstack_init (&mips_operand_tokens);
3845
3846 mips_no_prev_insn ();
3847
3848 mips_gprmask = 0;
3849 mips_cprmask[0] = 0;
3850 mips_cprmask[1] = 0;
3851 mips_cprmask[2] = 0;
3852 mips_cprmask[3] = 0;
3853
3854 /* set the default alignment for the text section (2**2) */
3855 record_alignment (text_section, 2);
3856
3857 bfd_set_gp_size (stdoutput, g_switch_value);
3858
3859 /* On a native system other than VxWorks, sections must be aligned
3860 to 16 byte boundaries. When configured for an embedded ELF
3861 target, we don't bother. */
3862 if (strncmp (TARGET_OS, "elf", 3) != 0
3863 && strncmp (TARGET_OS, "vxworks", 7) != 0)
3864 {
3865 bfd_set_section_alignment (text_section, 4);
3866 bfd_set_section_alignment (data_section, 4);
3867 bfd_set_section_alignment (bss_section, 4);
3868 }
3869
3870 /* Create a .reginfo section for register masks and a .mdebug
3871 section for debugging information. */
3872 {
3873 segT seg;
3874 subsegT subseg;
3875 flagword flags;
3876 segT sec;
3877
3878 seg = now_seg;
3879 subseg = now_subseg;
3880
3881 /* The ABI says this section should be loaded so that the
3882 running program can access it. However, we don't load it
3883 if we are configured for an embedded target. */
3884 flags = SEC_READONLY | SEC_DATA;
3885 if (strncmp (TARGET_OS, "elf", 3) != 0)
3886 flags |= SEC_ALLOC | SEC_LOAD;
3887
3888 if (mips_abi != N64_ABI)
3889 {
3890 sec = subseg_new (".reginfo", (subsegT) 0);
3891
3892 bfd_set_section_flags (sec, flags);
3893 bfd_set_section_alignment (sec, HAVE_NEWABI ? 3 : 2);
3894
3895 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3896 }
3897 else
3898 {
3899 /* The 64-bit ABI uses a .MIPS.options section rather than
3900 .reginfo section. */
3901 sec = subseg_new (".MIPS.options", (subsegT) 0);
3902 bfd_set_section_flags (sec, flags);
3903 bfd_set_section_alignment (sec, 3);
3904
3905 /* Set up the option header. */
3906 {
3907 Elf_Internal_Options opthdr;
3908 char *f;
3909
3910 opthdr.kind = ODK_REGINFO;
3911 opthdr.size = (sizeof (Elf_External_Options)
3912 + sizeof (Elf64_External_RegInfo));
3913 opthdr.section = 0;
3914 opthdr.info = 0;
3915 f = frag_more (sizeof (Elf_External_Options));
3916 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3917 (Elf_External_Options *) f);
3918
3919 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3920 }
3921 }
3922
3923 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3924 bfd_set_section_flags (sec,
3925 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3926 bfd_set_section_alignment (sec, 3);
3927 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3928
3929 if (ECOFF_DEBUGGING)
3930 {
3931 sec = subseg_new (".mdebug", (subsegT) 0);
3932 bfd_set_section_flags (sec, SEC_HAS_CONTENTS | SEC_READONLY);
3933 bfd_set_section_alignment (sec, 2);
3934 }
3935 else if (mips_flag_pdr)
3936 {
3937 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3938 bfd_set_section_flags (pdr_seg,
3939 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
3940 bfd_set_section_alignment (pdr_seg, 2);
3941 }
3942
3943 subseg_set (seg, subseg);
3944 }
3945
3946 if (mips_fix_vr4120)
3947 init_vr4120_conflicts ();
3948 }
3949
3950 static inline void
3951 fpabi_incompatible_with (int fpabi, const char *what)
3952 {
3953 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3954 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3955 }
3956
3957 static inline void
3958 fpabi_requires (int fpabi, const char *what)
3959 {
3960 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3961 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3962 }
3963
3964 /* Check -mabi and register sizes against the specified FP ABI. */
3965 static void
3966 check_fpabi (int fpabi)
3967 {
3968 switch (fpabi)
3969 {
3970 case Val_GNU_MIPS_ABI_FP_DOUBLE:
3971 if (file_mips_opts.soft_float)
3972 fpabi_incompatible_with (fpabi, "softfloat");
3973 else if (file_mips_opts.single_float)
3974 fpabi_incompatible_with (fpabi, "singlefloat");
3975 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3976 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3977 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3978 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
3979 break;
3980
3981 case Val_GNU_MIPS_ABI_FP_XX:
3982 if (mips_abi != O32_ABI)
3983 fpabi_requires (fpabi, "-mabi=32");
3984 else if (file_mips_opts.soft_float)
3985 fpabi_incompatible_with (fpabi, "softfloat");
3986 else if (file_mips_opts.single_float)
3987 fpabi_incompatible_with (fpabi, "singlefloat");
3988 else if (file_mips_opts.fp != 0)
3989 fpabi_requires (fpabi, "fp=xx");
3990 break;
3991
3992 case Val_GNU_MIPS_ABI_FP_64A:
3993 case Val_GNU_MIPS_ABI_FP_64:
3994 if (mips_abi != O32_ABI)
3995 fpabi_requires (fpabi, "-mabi=32");
3996 else if (file_mips_opts.soft_float)
3997 fpabi_incompatible_with (fpabi, "softfloat");
3998 else if (file_mips_opts.single_float)
3999 fpabi_incompatible_with (fpabi, "singlefloat");
4000 else if (file_mips_opts.fp != 64)
4001 fpabi_requires (fpabi, "fp=64");
4002 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
4003 fpabi_incompatible_with (fpabi, "nooddspreg");
4004 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
4005 fpabi_requires (fpabi, "nooddspreg");
4006 break;
4007
4008 case Val_GNU_MIPS_ABI_FP_SINGLE:
4009 if (file_mips_opts.soft_float)
4010 fpabi_incompatible_with (fpabi, "softfloat");
4011 else if (!file_mips_opts.single_float)
4012 fpabi_requires (fpabi, "singlefloat");
4013 break;
4014
4015 case Val_GNU_MIPS_ABI_FP_SOFT:
4016 if (!file_mips_opts.soft_float)
4017 fpabi_requires (fpabi, "softfloat");
4018 break;
4019
4020 case Val_GNU_MIPS_ABI_FP_OLD_64:
4021 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
4022 Tag_GNU_MIPS_ABI_FP, fpabi);
4023 break;
4024
4025 case Val_GNU_MIPS_ABI_FP_NAN2008:
4026 /* Silently ignore compatibility value. */
4027 break;
4028
4029 default:
4030 as_warn (_(".gnu_attribute %d,%d is not a recognized"
4031 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
4032 break;
4033 }
4034 }
4035
4036 /* Perform consistency checks on the current options. */
4037
4038 static void
4039 mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
4040 {
4041 /* Check the size of integer registers agrees with the ABI and ISA. */
4042 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
4043 as_bad (_("`gp=64' used with a 32-bit processor"));
4044 else if (abi_checks
4045 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
4046 as_bad (_("`gp=32' used with a 64-bit ABI"));
4047 else if (abi_checks
4048 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
4049 as_bad (_("`gp=64' used with a 32-bit ABI"));
4050
4051 /* Check the size of the float registers agrees with the ABI and ISA. */
4052 switch (opts->fp)
4053 {
4054 case 0:
4055 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
4056 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
4057 else if (opts->single_float == 1)
4058 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
4059 break;
4060 case 64:
4061 if (!ISA_HAS_64BIT_FPRS (opts->isa))
4062 as_bad (_("`fp=64' used with a 32-bit fpu"));
4063 else if (abi_checks
4064 && ABI_NEEDS_32BIT_REGS (mips_abi)
4065 && !ISA_HAS_MXHC1 (opts->isa))
4066 as_warn (_("`fp=64' used with a 32-bit ABI"));
4067 break;
4068 case 32:
4069 if (abi_checks
4070 && ABI_NEEDS_64BIT_REGS (mips_abi))
4071 as_warn (_("`fp=32' used with a 64-bit ABI"));
4072 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
4073 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
4074 break;
4075 default:
4076 as_bad (_("Unknown size of floating point registers"));
4077 break;
4078 }
4079
4080 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
4081 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4082
4083 if (opts->micromips == 1 && opts->mips16 == 1)
4084 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
4085 else if (ISA_IS_R6 (opts->isa)
4086 && (opts->micromips == 1
4087 || opts->mips16 == 1))
4088 as_fatal (_("`%s' cannot be used with `%s'"),
4089 opts->micromips ? "micromips" : "mips16",
4090 mips_cpu_info_from_isa (opts->isa)->name);
4091
4092 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4093 as_fatal (_("branch relaxation is not supported in `%s'"),
4094 mips_cpu_info_from_isa (opts->isa)->name);
4095 }
4096
4097 /* Perform consistency checks on the module level options exactly once.
4098 This is a deferred check that happens:
4099 at the first .set directive
4100 or, at the first pseudo op that generates code (inc .dc.a)
4101 or, at the first instruction
4102 or, at the end. */
4103
4104 static void
4105 file_mips_check_options (void)
4106 {
4107 if (file_mips_opts_checked)
4108 return;
4109
4110 /* The following code determines the register size.
4111 Similar code was added to GCC 3.3 (see override_options() in
4112 config/mips/mips.c). The GAS and GCC code should be kept in sync
4113 as much as possible. */
4114
4115 if (file_mips_opts.gp < 0)
4116 {
4117 /* Infer the integer register size from the ABI and processor.
4118 Restrict ourselves to 32-bit registers if that's all the
4119 processor has, or if the ABI cannot handle 64-bit registers. */
4120 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4121 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4122 ? 32 : 64;
4123 }
4124
4125 if (file_mips_opts.fp < 0)
4126 {
4127 /* No user specified float register size.
4128 ??? GAS treats single-float processors as though they had 64-bit
4129 float registers (although it complains when double-precision
4130 instructions are used). As things stand, saying they have 32-bit
4131 registers would lead to spurious "register must be even" messages.
4132 So here we assume float registers are never smaller than the
4133 integer ones. */
4134 if (file_mips_opts.gp == 64)
4135 /* 64-bit integer registers implies 64-bit float registers. */
4136 file_mips_opts.fp = 64;
4137 else if ((file_mips_opts.ase & FP64_ASES)
4138 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4139 /* Handle ASEs that require 64-bit float registers, if possible. */
4140 file_mips_opts.fp = 64;
4141 else if (ISA_IS_R6 (mips_opts.isa))
4142 /* R6 implies 64-bit float registers. */
4143 file_mips_opts.fp = 64;
4144 else
4145 /* 32-bit float registers. */
4146 file_mips_opts.fp = 32;
4147 }
4148
4149 /* Disable operations on odd-numbered floating-point registers by default
4150 when using the FPXX ABI. */
4151 if (file_mips_opts.oddspreg < 0)
4152 {
4153 if (file_mips_opts.fp == 0)
4154 file_mips_opts.oddspreg = 0;
4155 else
4156 file_mips_opts.oddspreg = 1;
4157 }
4158
4159 /* End of GCC-shared inference code. */
4160
4161 /* This flag is set when we have a 64-bit capable CPU but use only
4162 32-bit wide registers. Note that EABI does not use it. */
4163 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4164 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4165 || mips_abi == O32_ABI))
4166 mips_32bitmode = 1;
4167
4168 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4169 as_bad (_("trap exception not supported at ISA 1"));
4170
4171 /* If the selected architecture includes support for ASEs, enable
4172 generation of code for them. */
4173 if (file_mips_opts.mips16 == -1)
4174 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4175 if (file_mips_opts.micromips == -1)
4176 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4177 ? 1 : 0;
4178
4179 if (mips_nan2008 == -1)
4180 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4181 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4182 as_fatal (_("`%s' does not support legacy NaN"),
4183 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4184
4185 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4186 being selected implicitly. */
4187 if (file_mips_opts.fp != 64)
4188 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4189
4190 /* If the user didn't explicitly select or deselect a particular ASE,
4191 use the default setting for the CPU. */
4192 file_mips_opts.ase |= (file_mips_opts.init_ase & ~file_ase_explicit);
4193
4194 /* Set up the current options. These may change throughout assembly. */
4195 mips_opts = file_mips_opts;
4196
4197 mips_check_isa_supports_ases ();
4198 mips_check_options (&file_mips_opts, TRUE);
4199 file_mips_opts_checked = TRUE;
4200
4201 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4202 as_warn (_("could not set architecture and machine"));
4203 }
4204
4205 void
4206 md_assemble (char *str)
4207 {
4208 struct mips_cl_insn insn;
4209 bfd_reloc_code_real_type unused_reloc[3]
4210 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
4211
4212 file_mips_check_options ();
4213
4214 imm_expr.X_op = O_absent;
4215 offset_expr.X_op = O_absent;
4216 offset_reloc[0] = BFD_RELOC_UNUSED;
4217 offset_reloc[1] = BFD_RELOC_UNUSED;
4218 offset_reloc[2] = BFD_RELOC_UNUSED;
4219
4220 mips_mark_labels ();
4221 mips_assembling_insn = TRUE;
4222 clear_insn_error ();
4223
4224 if (mips_opts.mips16)
4225 mips16_ip (str, &insn);
4226 else
4227 {
4228 mips_ip (str, &insn);
4229 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4230 str, insn.insn_opcode));
4231 }
4232
4233 if (insn_error.msg)
4234 report_insn_error (str);
4235 else if (insn.insn_mo->pinfo == INSN_MACRO)
4236 {
4237 macro_start ();
4238 if (mips_opts.mips16)
4239 mips16_macro (&insn);
4240 else
4241 macro (&insn, str);
4242 macro_end ();
4243 }
4244 else
4245 {
4246 if (offset_expr.X_op != O_absent)
4247 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
4248 else
4249 append_insn (&insn, NULL, unused_reloc, FALSE);
4250 }
4251
4252 mips_assembling_insn = FALSE;
4253 }
4254
4255 /* Convenience functions for abstracting away the differences between
4256 MIPS16 and non-MIPS16 relocations. */
4257
4258 static inline bfd_boolean
4259 mips16_reloc_p (bfd_reloc_code_real_type reloc)
4260 {
4261 switch (reloc)
4262 {
4263 case BFD_RELOC_MIPS16_JMP:
4264 case BFD_RELOC_MIPS16_GPREL:
4265 case BFD_RELOC_MIPS16_GOT16:
4266 case BFD_RELOC_MIPS16_CALL16:
4267 case BFD_RELOC_MIPS16_HI16_S:
4268 case BFD_RELOC_MIPS16_HI16:
4269 case BFD_RELOC_MIPS16_LO16:
4270 case BFD_RELOC_MIPS16_16_PCREL_S1:
4271 return TRUE;
4272
4273 default:
4274 return FALSE;
4275 }
4276 }
4277
4278 static inline bfd_boolean
4279 micromips_reloc_p (bfd_reloc_code_real_type reloc)
4280 {
4281 switch (reloc)
4282 {
4283 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4284 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4285 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4286 case BFD_RELOC_MICROMIPS_GPREL16:
4287 case BFD_RELOC_MICROMIPS_JMP:
4288 case BFD_RELOC_MICROMIPS_HI16:
4289 case BFD_RELOC_MICROMIPS_HI16_S:
4290 case BFD_RELOC_MICROMIPS_LO16:
4291 case BFD_RELOC_MICROMIPS_LITERAL:
4292 case BFD_RELOC_MICROMIPS_GOT16:
4293 case BFD_RELOC_MICROMIPS_CALL16:
4294 case BFD_RELOC_MICROMIPS_GOT_HI16:
4295 case BFD_RELOC_MICROMIPS_GOT_LO16:
4296 case BFD_RELOC_MICROMIPS_CALL_HI16:
4297 case BFD_RELOC_MICROMIPS_CALL_LO16:
4298 case BFD_RELOC_MICROMIPS_SUB:
4299 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4300 case BFD_RELOC_MICROMIPS_GOT_OFST:
4301 case BFD_RELOC_MICROMIPS_GOT_DISP:
4302 case BFD_RELOC_MICROMIPS_HIGHEST:
4303 case BFD_RELOC_MICROMIPS_HIGHER:
4304 case BFD_RELOC_MICROMIPS_SCN_DISP:
4305 case BFD_RELOC_MICROMIPS_JALR:
4306 return TRUE;
4307
4308 default:
4309 return FALSE;
4310 }
4311 }
4312
4313 static inline bfd_boolean
4314 jmp_reloc_p (bfd_reloc_code_real_type reloc)
4315 {
4316 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4317 }
4318
4319 static inline bfd_boolean
4320 b_reloc_p (bfd_reloc_code_real_type reloc)
4321 {
4322 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4323 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4324 || reloc == BFD_RELOC_16_PCREL_S2
4325 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
4326 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4327 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4328 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4329 }
4330
4331 static inline bfd_boolean
4332 got16_reloc_p (bfd_reloc_code_real_type reloc)
4333 {
4334 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
4335 || reloc == BFD_RELOC_MICROMIPS_GOT16);
4336 }
4337
4338 static inline bfd_boolean
4339 hi16_reloc_p (bfd_reloc_code_real_type reloc)
4340 {
4341 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
4342 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
4343 }
4344
4345 static inline bfd_boolean
4346 lo16_reloc_p (bfd_reloc_code_real_type reloc)
4347 {
4348 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
4349 || reloc == BFD_RELOC_MICROMIPS_LO16);
4350 }
4351
4352 static inline bfd_boolean
4353 jalr_reloc_p (bfd_reloc_code_real_type reloc)
4354 {
4355 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
4356 }
4357
4358 static inline bfd_boolean
4359 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4360 {
4361 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4362 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4363 }
4364
4365 /* Return true if RELOC is a PC-relative relocation that does not have
4366 full address range. */
4367
4368 static inline bfd_boolean
4369 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4370 {
4371 switch (reloc)
4372 {
4373 case BFD_RELOC_16_PCREL_S2:
4374 case BFD_RELOC_MIPS16_16_PCREL_S1:
4375 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4376 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4377 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4378 case BFD_RELOC_MIPS_21_PCREL_S2:
4379 case BFD_RELOC_MIPS_26_PCREL_S2:
4380 case BFD_RELOC_MIPS_18_PCREL_S3:
4381 case BFD_RELOC_MIPS_19_PCREL_S2:
4382 return TRUE;
4383
4384 case BFD_RELOC_32_PCREL:
4385 case BFD_RELOC_HI16_S_PCREL:
4386 case BFD_RELOC_LO16_PCREL:
4387 return HAVE_64BIT_ADDRESSES;
4388
4389 default:
4390 return FALSE;
4391 }
4392 }
4393
4394 /* Return true if the given relocation might need a matching %lo().
4395 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4396 need a matching %lo() when applied to local symbols. */
4397
4398 static inline bfd_boolean
4399 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
4400 {
4401 return (HAVE_IN_PLACE_ADDENDS
4402 && (hi16_reloc_p (reloc)
4403 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4404 all GOT16 relocations evaluate to "G". */
4405 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4406 }
4407
4408 /* Return the type of %lo() reloc needed by RELOC, given that
4409 reloc_needs_lo_p. */
4410
4411 static inline bfd_reloc_code_real_type
4412 matching_lo_reloc (bfd_reloc_code_real_type reloc)
4413 {
4414 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4415 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4416 : BFD_RELOC_LO16));
4417 }
4418
4419 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
4420 relocation. */
4421
4422 static inline bfd_boolean
4423 fixup_has_matching_lo_p (fixS *fixp)
4424 {
4425 return (fixp->fx_next != NULL
4426 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
4427 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4428 && fixp->fx_offset == fixp->fx_next->fx_offset);
4429 }
4430
4431 /* Move all labels in LABELS to the current insertion point. TEXT_P
4432 says whether the labels refer to text or data. */
4433
4434 static void
4435 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
4436 {
4437 struct insn_label_list *l;
4438 valueT val;
4439
4440 for (l = labels; l != NULL; l = l->next)
4441 {
4442 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
4443 symbol_set_frag (l->label, frag_now);
4444 val = (valueT) frag_now_fix ();
4445 /* MIPS16/microMIPS text labels are stored as odd.
4446 We just carry the ISA mode bit forward. */
4447 if (text_p && HAVE_CODE_COMPRESSION)
4448 val |= (S_GET_VALUE (l->label) & 0x1);
4449 S_SET_VALUE (l->label, val);
4450 }
4451 }
4452
4453 /* Move all labels in insn_labels to the current insertion point
4454 and treat them as text labels. */
4455
4456 static void
4457 mips_move_text_labels (void)
4458 {
4459 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4460 }
4461
4462 /* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4463
4464 static bfd_boolean
4465 s_is_linkonce (symbolS *sym, segT from_seg)
4466 {
4467 bfd_boolean linkonce = FALSE;
4468 segT symseg = S_GET_SEGMENT (sym);
4469
4470 if (symseg != from_seg && !S_IS_LOCAL (sym))
4471 {
4472 if ((bfd_section_flags (symseg) & SEC_LINK_ONCE))
4473 linkonce = TRUE;
4474 /* The GNU toolchain uses an extension for ELF: a section
4475 beginning with the magic string .gnu.linkonce is a
4476 linkonce section. */
4477 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4478 sizeof ".gnu.linkonce" - 1) == 0)
4479 linkonce = TRUE;
4480 }
4481 return linkonce;
4482 }
4483
4484 /* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
4485 linker to handle them specially, such as generating jalx instructions
4486 when needed. We also make them odd for the duration of the assembly,
4487 in order to generate the right sort of code. We will make them even
4488 in the adjust_symtab routine, while leaving them marked. This is
4489 convenient for the debugger and the disassembler. The linker knows
4490 to make them odd again. */
4491
4492 static void
4493 mips_compressed_mark_label (symbolS *label)
4494 {
4495 gas_assert (HAVE_CODE_COMPRESSION);
4496
4497 if (mips_opts.mips16)
4498 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4499 else
4500 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
4501 if ((S_GET_VALUE (label) & 1) == 0
4502 /* Don't adjust the address if the label is global or weak, or
4503 in a link-once section, since we'll be emitting symbol reloc
4504 references to it which will be patched up by the linker, and
4505 the final value of the symbol may or may not be MIPS16/microMIPS. */
4506 && !S_IS_WEAK (label)
4507 && !S_IS_EXTERNAL (label)
4508 && !s_is_linkonce (label, now_seg))
4509 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4510 }
4511
4512 /* Mark preceding MIPS16 or microMIPS instruction labels. */
4513
4514 static void
4515 mips_compressed_mark_labels (void)
4516 {
4517 struct insn_label_list *l;
4518
4519 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4520 mips_compressed_mark_label (l->label);
4521 }
4522
4523 /* End the current frag. Make it a variant frag and record the
4524 relaxation info. */
4525
4526 static void
4527 relax_close_frag (void)
4528 {
4529 mips_macro_warning.first_frag = frag_now;
4530 frag_var (rs_machine_dependent, 0, 0,
4531 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4532 mips_pic != NO_PIC),
4533 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4534
4535 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4536 mips_relax.first_fixup = 0;
4537 }
4538
4539 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
4540 See the comment above RELAX_ENCODE for more details. */
4541
4542 static void
4543 relax_start (symbolS *symbol)
4544 {
4545 gas_assert (mips_relax.sequence == 0);
4546 mips_relax.sequence = 1;
4547 mips_relax.symbol = symbol;
4548 }
4549
4550 /* Start generating the second version of a relaxable sequence.
4551 See the comment above RELAX_ENCODE for more details. */
4552
4553 static void
4554 relax_switch (void)
4555 {
4556 gas_assert (mips_relax.sequence == 1);
4557 mips_relax.sequence = 2;
4558 }
4559
4560 /* End the current relaxable sequence. */
4561
4562 static void
4563 relax_end (void)
4564 {
4565 gas_assert (mips_relax.sequence == 2);
4566 relax_close_frag ();
4567 mips_relax.sequence = 0;
4568 }
4569
4570 /* Return true if IP is a delayed branch or jump. */
4571
4572 static inline bfd_boolean
4573 delayed_branch_p (const struct mips_cl_insn *ip)
4574 {
4575 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4576 | INSN_COND_BRANCH_DELAY
4577 | INSN_COND_BRANCH_LIKELY)) != 0;
4578 }
4579
4580 /* Return true if IP is a compact branch or jump. */
4581
4582 static inline bfd_boolean
4583 compact_branch_p (const struct mips_cl_insn *ip)
4584 {
4585 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4586 | INSN2_COND_BRANCH)) != 0;
4587 }
4588
4589 /* Return true if IP is an unconditional branch or jump. */
4590
4591 static inline bfd_boolean
4592 uncond_branch_p (const struct mips_cl_insn *ip)
4593 {
4594 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
4595 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
4596 }
4597
4598 /* Return true if IP is a branch-likely instruction. */
4599
4600 static inline bfd_boolean
4601 branch_likely_p (const struct mips_cl_insn *ip)
4602 {
4603 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4604 }
4605
4606 /* Return the type of nop that should be used to fill the delay slot
4607 of delayed branch IP. */
4608
4609 static struct mips_cl_insn *
4610 get_delay_slot_nop (const struct mips_cl_insn *ip)
4611 {
4612 if (mips_opts.micromips
4613 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4614 return &micromips_nop32_insn;
4615 return NOP_INSN;
4616 }
4617
4618 /* Return a mask that has bit N set if OPCODE reads the register(s)
4619 in operand N. */
4620
4621 static unsigned int
4622 insn_read_mask (const struct mips_opcode *opcode)
4623 {
4624 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4625 }
4626
4627 /* Return a mask that has bit N set if OPCODE writes to the register(s)
4628 in operand N. */
4629
4630 static unsigned int
4631 insn_write_mask (const struct mips_opcode *opcode)
4632 {
4633 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4634 }
4635
4636 /* Return a mask of the registers specified by operand OPERAND of INSN.
4637 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4638 is set. */
4639
4640 static unsigned int
4641 operand_reg_mask (const struct mips_cl_insn *insn,
4642 const struct mips_operand *operand,
4643 unsigned int type_mask)
4644 {
4645 unsigned int uval, vsel;
4646
4647 switch (operand->type)
4648 {
4649 case OP_INT:
4650 case OP_MAPPED_INT:
4651 case OP_MSB:
4652 case OP_PCREL:
4653 case OP_PERF_REG:
4654 case OP_ADDIUSP_INT:
4655 case OP_ENTRY_EXIT_LIST:
4656 case OP_REPEAT_DEST_REG:
4657 case OP_REPEAT_PREV_REG:
4658 case OP_PC:
4659 case OP_VU0_SUFFIX:
4660 case OP_VU0_MATCH_SUFFIX:
4661 case OP_IMM_INDEX:
4662 abort ();
4663
4664 case OP_REG28:
4665 return 1 << 28;
4666
4667 case OP_REG:
4668 case OP_OPTIONAL_REG:
4669 {
4670 const struct mips_reg_operand *reg_op;
4671
4672 reg_op = (const struct mips_reg_operand *) operand;
4673 if (!(type_mask & (1 << reg_op->reg_type)))
4674 return 0;
4675 uval = insn_extract_operand (insn, operand);
4676 return 1 << mips_decode_reg_operand (reg_op, uval);
4677 }
4678
4679 case OP_REG_PAIR:
4680 {
4681 const struct mips_reg_pair_operand *pair_op;
4682
4683 pair_op = (const struct mips_reg_pair_operand *) operand;
4684 if (!(type_mask & (1 << pair_op->reg_type)))
4685 return 0;
4686 uval = insn_extract_operand (insn, operand);
4687 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4688 }
4689
4690 case OP_CLO_CLZ_DEST:
4691 if (!(type_mask & (1 << OP_REG_GP)))
4692 return 0;
4693 uval = insn_extract_operand (insn, operand);
4694 return (1 << (uval & 31)) | (1 << (uval >> 5));
4695
4696 case OP_SAME_RS_RT:
4697 if (!(type_mask & (1 << OP_REG_GP)))
4698 return 0;
4699 uval = insn_extract_operand (insn, operand);
4700 gas_assert ((uval & 31) == (uval >> 5));
4701 return 1 << (uval & 31);
4702
4703 case OP_CHECK_PREV:
4704 case OP_NON_ZERO_REG:
4705 if (!(type_mask & (1 << OP_REG_GP)))
4706 return 0;
4707 uval = insn_extract_operand (insn, operand);
4708 return 1 << (uval & 31);
4709
4710 case OP_LWM_SWM_LIST:
4711 abort ();
4712
4713 case OP_SAVE_RESTORE_LIST:
4714 abort ();
4715
4716 case OP_MDMX_IMM_REG:
4717 if (!(type_mask & (1 << OP_REG_VEC)))
4718 return 0;
4719 uval = insn_extract_operand (insn, operand);
4720 vsel = uval >> 5;
4721 if ((vsel & 0x18) == 0x18)
4722 return 0;
4723 return 1 << (uval & 31);
4724
4725 case OP_REG_INDEX:
4726 if (!(type_mask & (1 << OP_REG_GP)))
4727 return 0;
4728 return 1 << insn_extract_operand (insn, operand);
4729 }
4730 abort ();
4731 }
4732
4733 /* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4734 where bit N of OPNO_MASK is set if operand N should be included.
4735 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4736 is set. */
4737
4738 static unsigned int
4739 insn_reg_mask (const struct mips_cl_insn *insn,
4740 unsigned int type_mask, unsigned int opno_mask)
4741 {
4742 unsigned int opno, reg_mask;
4743
4744 opno = 0;
4745 reg_mask = 0;
4746 while (opno_mask != 0)
4747 {
4748 if (opno_mask & 1)
4749 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4750 opno_mask >>= 1;
4751 opno += 1;
4752 }
4753 return reg_mask;
4754 }
4755
4756 /* Return the mask of core registers that IP reads. */
4757
4758 static unsigned int
4759 gpr_read_mask (const struct mips_cl_insn *ip)
4760 {
4761 unsigned long pinfo, pinfo2;
4762 unsigned int mask;
4763
4764 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4765 pinfo = ip->insn_mo->pinfo;
4766 pinfo2 = ip->insn_mo->pinfo2;
4767 if (pinfo & INSN_UDI)
4768 {
4769 /* UDI instructions have traditionally been assumed to read RS
4770 and RT. */
4771 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4772 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4773 }
4774 if (pinfo & INSN_READ_GPR_24)
4775 mask |= 1 << 24;
4776 if (pinfo2 & INSN2_READ_GPR_16)
4777 mask |= 1 << 16;
4778 if (pinfo2 & INSN2_READ_SP)
4779 mask |= 1 << SP;
4780 if (pinfo2 & INSN2_READ_GPR_31)
4781 mask |= 1u << 31;
4782 /* Don't include register 0. */
4783 return mask & ~1;
4784 }
4785
4786 /* Return the mask of core registers that IP writes. */
4787
4788 static unsigned int
4789 gpr_write_mask (const struct mips_cl_insn *ip)
4790 {
4791 unsigned long pinfo, pinfo2;
4792 unsigned int mask;
4793
4794 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4795 pinfo = ip->insn_mo->pinfo;
4796 pinfo2 = ip->insn_mo->pinfo2;
4797 if (pinfo & INSN_WRITE_GPR_24)
4798 mask |= 1 << 24;
4799 if (pinfo & INSN_WRITE_GPR_31)
4800 mask |= 1u << 31;
4801 if (pinfo & INSN_UDI)
4802 /* UDI instructions have traditionally been assumed to write to RD. */
4803 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4804 if (pinfo2 & INSN2_WRITE_SP)
4805 mask |= 1 << SP;
4806 /* Don't include register 0. */
4807 return mask & ~1;
4808 }
4809
4810 /* Return the mask of floating-point registers that IP reads. */
4811
4812 static unsigned int
4813 fpr_read_mask (const struct mips_cl_insn *ip)
4814 {
4815 unsigned long pinfo;
4816 unsigned int mask;
4817
4818 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4819 | (1 << OP_REG_MSA)),
4820 insn_read_mask (ip->insn_mo));
4821 pinfo = ip->insn_mo->pinfo;
4822 /* Conservatively treat all operands to an FP_D instruction are doubles.
4823 (This is overly pessimistic for things like cvt.d.s.) */
4824 if (FPR_SIZE != 64 && (pinfo & FP_D))
4825 mask |= mask << 1;
4826 return mask;
4827 }
4828
4829 /* Return the mask of floating-point registers that IP writes. */
4830
4831 static unsigned int
4832 fpr_write_mask (const struct mips_cl_insn *ip)
4833 {
4834 unsigned long pinfo;
4835 unsigned int mask;
4836
4837 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4838 | (1 << OP_REG_MSA)),
4839 insn_write_mask (ip->insn_mo));
4840 pinfo = ip->insn_mo->pinfo;
4841 /* Conservatively treat all operands to an FP_D instruction are doubles.
4842 (This is overly pessimistic for things like cvt.s.d.) */
4843 if (FPR_SIZE != 64 && (pinfo & FP_D))
4844 mask |= mask << 1;
4845 return mask;
4846 }
4847
4848 /* Operand OPNUM of INSN is an odd-numbered floating-point register.
4849 Check whether that is allowed. */
4850
4851 static bfd_boolean
4852 mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4853 {
4854 const char *s = insn->name;
4855 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4856 || FPR_SIZE == 64)
4857 && mips_opts.oddspreg;
4858
4859 if (insn->pinfo == INSN_MACRO)
4860 /* Let a macro pass, we'll catch it later when it is expanded. */
4861 return TRUE;
4862
4863 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4864 otherwise it depends on oddspreg. */
4865 if ((insn->pinfo & FP_S)
4866 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
4867 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
4868 return FPR_SIZE == 32 || oddspreg;
4869
4870 /* Allow odd registers for single-precision ops and double-precision if the
4871 floating-point registers are 64-bit wide. */
4872 switch (insn->pinfo & (FP_S | FP_D))
4873 {
4874 case FP_S:
4875 case 0:
4876 return oddspreg;
4877 case FP_D:
4878 return FPR_SIZE == 64;
4879 default:
4880 break;
4881 }
4882
4883 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4884 s = strchr (insn->name, '.');
4885 if (s != NULL && opnum == 2)
4886 s = strchr (s + 1, '.');
4887 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4888 return oddspreg;
4889
4890 return FPR_SIZE == 64;
4891 }
4892
4893 /* Information about an instruction argument that we're trying to match. */
4894 struct mips_arg_info
4895 {
4896 /* The instruction so far. */
4897 struct mips_cl_insn *insn;
4898
4899 /* The first unconsumed operand token. */
4900 struct mips_operand_token *token;
4901
4902 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4903 int opnum;
4904
4905 /* The 1-based argument number, for error reporting. This does not
4906 count elided optional registers, etc.. */
4907 int argnum;
4908
4909 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4910 unsigned int last_regno;
4911
4912 /* If the first operand was an OP_REG, this is the register that it
4913 specified, otherwise it is ILLEGAL_REG. */
4914 unsigned int dest_regno;
4915
4916 /* The value of the last OP_INT operand. Only used for OP_MSB,
4917 where it gives the lsb position. */
4918 unsigned int last_op_int;
4919
4920 /* If true, match routines should assume that no later instruction
4921 alternative matches and should therefore be as accommodating as
4922 possible. Match routines should not report errors if something
4923 is only invalid for !LAX_MATCH. */
4924 bfd_boolean lax_match;
4925
4926 /* True if a reference to the current AT register was seen. */
4927 bfd_boolean seen_at;
4928 };
4929
4930 /* Record that the argument is out of range. */
4931
4932 static void
4933 match_out_of_range (struct mips_arg_info *arg)
4934 {
4935 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4936 }
4937
4938 /* Record that the argument isn't constant but needs to be. */
4939
4940 static void
4941 match_not_constant (struct mips_arg_info *arg)
4942 {
4943 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4944 arg->argnum);
4945 }
4946
4947 /* Try to match an OT_CHAR token for character CH. Consume the token
4948 and return true on success, otherwise return false. */
4949
4950 static bfd_boolean
4951 match_char (struct mips_arg_info *arg, char ch)
4952 {
4953 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4954 {
4955 ++arg->token;
4956 if (ch == ',')
4957 arg->argnum += 1;
4958 return TRUE;
4959 }
4960 return FALSE;
4961 }
4962
4963 /* Try to get an expression from the next tokens in ARG. Consume the
4964 tokens and return true on success, storing the expression value in
4965 VALUE and relocation types in R. */
4966
4967 static bfd_boolean
4968 match_expression (struct mips_arg_info *arg, expressionS *value,
4969 bfd_reloc_code_real_type *r)
4970 {
4971 /* If the next token is a '(' that was parsed as being part of a base
4972 expression, assume we have an elided offset. The later match will fail
4973 if this turns out to be wrong. */
4974 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4975 {
4976 value->X_op = O_constant;
4977 value->X_add_number = 0;
4978 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4979 return TRUE;
4980 }
4981
4982 /* Reject register-based expressions such as "0+$2" and "(($2))".
4983 For plain registers the default error seems more appropriate. */
4984 if (arg->token->type == OT_INTEGER
4985 && arg->token->u.integer.value.X_op == O_register)
4986 {
4987 set_insn_error (arg->argnum, _("register value used as expression"));
4988 return FALSE;
4989 }
4990
4991 if (arg->token->type == OT_INTEGER)
4992 {
4993 *value = arg->token->u.integer.value;
4994 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4995 ++arg->token;
4996 return TRUE;
4997 }
4998
4999 set_insn_error_i
5000 (arg->argnum, _("operand %d must be an immediate expression"),
5001 arg->argnum);
5002 return FALSE;
5003 }
5004
5005 /* Try to get a constant expression from the next tokens in ARG. Consume
5006 the tokens and return true on success, storing the constant value
5007 in *VALUE. */
5008
5009 static bfd_boolean
5010 match_const_int (struct mips_arg_info *arg, offsetT *value)
5011 {
5012 expressionS ex;
5013 bfd_reloc_code_real_type r[3];
5014
5015 if (!match_expression (arg, &ex, r))
5016 return FALSE;
5017
5018 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
5019 *value = ex.X_add_number;
5020 else
5021 {
5022 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
5023 match_out_of_range (arg);
5024 else
5025 match_not_constant (arg);
5026 return FALSE;
5027 }
5028 return TRUE;
5029 }
5030
5031 /* Return the RTYPE_* flags for a register operand of type TYPE that
5032 appears in instruction OPCODE. */
5033
5034 static unsigned int
5035 convert_reg_type (const struct mips_opcode *opcode,
5036 enum mips_reg_operand_type type)
5037 {
5038 switch (type)
5039 {
5040 case OP_REG_GP:
5041 return RTYPE_NUM | RTYPE_GP;
5042
5043 case OP_REG_FP:
5044 /* Allow vector register names for MDMX if the instruction is a 64-bit
5045 FPR load, store or move (including moves to and from GPRs). */
5046 if ((mips_opts.ase & ASE_MDMX)
5047 && (opcode->pinfo & FP_D)
5048 && (opcode->pinfo & (INSN_COPROC_MOVE
5049 | INSN_COPROC_MEMORY_DELAY
5050 | INSN_LOAD_COPROC
5051 | INSN_LOAD_MEMORY
5052 | INSN_STORE_MEMORY)))
5053 return RTYPE_FPU | RTYPE_VEC;
5054 return RTYPE_FPU;
5055
5056 case OP_REG_CCC:
5057 if (opcode->pinfo & (FP_D | FP_S))
5058 return RTYPE_CCC | RTYPE_FCC;
5059 return RTYPE_CCC;
5060
5061 case OP_REG_VEC:
5062 if (opcode->membership & INSN_5400)
5063 return RTYPE_FPU;
5064 return RTYPE_FPU | RTYPE_VEC;
5065
5066 case OP_REG_ACC:
5067 return RTYPE_ACC;
5068
5069 case OP_REG_COPRO:
5070 if (opcode->name[strlen (opcode->name) - 1] == '0')
5071 return RTYPE_NUM | RTYPE_CP0;
5072 return RTYPE_NUM;
5073
5074 case OP_REG_HW:
5075 return RTYPE_NUM;
5076
5077 case OP_REG_VI:
5078 return RTYPE_NUM | RTYPE_VI;
5079
5080 case OP_REG_VF:
5081 return RTYPE_NUM | RTYPE_VF;
5082
5083 case OP_REG_R5900_I:
5084 return RTYPE_R5900_I;
5085
5086 case OP_REG_R5900_Q:
5087 return RTYPE_R5900_Q;
5088
5089 case OP_REG_R5900_R:
5090 return RTYPE_R5900_R;
5091
5092 case OP_REG_R5900_ACC:
5093 return RTYPE_R5900_ACC;
5094
5095 case OP_REG_MSA:
5096 return RTYPE_MSA;
5097
5098 case OP_REG_MSA_CTRL:
5099 return RTYPE_NUM;
5100 }
5101 abort ();
5102 }
5103
5104 /* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5105
5106 static void
5107 check_regno (struct mips_arg_info *arg,
5108 enum mips_reg_operand_type type, unsigned int regno)
5109 {
5110 if (AT && type == OP_REG_GP && regno == AT)
5111 arg->seen_at = TRUE;
5112
5113 if (type == OP_REG_FP
5114 && (regno & 1) != 0
5115 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
5116 {
5117 /* This was a warning prior to introducing O32 FPXX and FP64 support
5118 so maintain a warning for FP32 but raise an error for the new
5119 cases. */
5120 if (FPR_SIZE == 32)
5121 as_warn (_("float register should be even, was %d"), regno);
5122 else
5123 as_bad (_("float register should be even, was %d"), regno);
5124 }
5125
5126 if (type == OP_REG_CCC)
5127 {
5128 const char *name;
5129 size_t length;
5130
5131 name = arg->insn->insn_mo->name;
5132 length = strlen (name);
5133 if ((regno & 1) != 0
5134 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5135 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
5136 as_warn (_("condition code register should be even for %s, was %d"),
5137 name, regno);
5138
5139 if ((regno & 3) != 0
5140 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
5141 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
5142 name, regno);
5143 }
5144 }
5145
5146 /* ARG is a register with symbol value SYMVAL. Try to interpret it as
5147 a register of type TYPE. Return true on success, storing the register
5148 number in *REGNO and warning about any dubious uses. */
5149
5150 static bfd_boolean
5151 match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5152 unsigned int symval, unsigned int *regno)
5153 {
5154 if (type == OP_REG_VEC)
5155 symval = mips_prefer_vec_regno (symval);
5156 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5157 return FALSE;
5158
5159 *regno = symval & RNUM_MASK;
5160 check_regno (arg, type, *regno);
5161 return TRUE;
5162 }
5163
5164 /* Try to interpret the next token in ARG as a register of type TYPE.
5165 Consume the token and return true on success, storing the register
5166 number in *REGNO. Return false on failure. */
5167
5168 static bfd_boolean
5169 match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5170 unsigned int *regno)
5171 {
5172 if (arg->token->type == OT_REG
5173 && match_regno (arg, type, arg->token->u.regno, regno))
5174 {
5175 ++arg->token;
5176 return TRUE;
5177 }
5178 return FALSE;
5179 }
5180
5181 /* Try to interpret the next token in ARG as a range of registers of type TYPE.
5182 Consume the token and return true on success, storing the register numbers
5183 in *REGNO1 and *REGNO2. Return false on failure. */
5184
5185 static bfd_boolean
5186 match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5187 unsigned int *regno1, unsigned int *regno2)
5188 {
5189 if (match_reg (arg, type, regno1))
5190 {
5191 *regno2 = *regno1;
5192 return TRUE;
5193 }
5194 if (arg->token->type == OT_REG_RANGE
5195 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5196 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5197 && *regno1 <= *regno2)
5198 {
5199 ++arg->token;
5200 return TRUE;
5201 }
5202 return FALSE;
5203 }
5204
5205 /* OP_INT matcher. */
5206
5207 static bfd_boolean
5208 match_int_operand (struct mips_arg_info *arg,
5209 const struct mips_operand *operand_base)
5210 {
5211 const struct mips_int_operand *operand;
5212 unsigned int uval;
5213 int min_val, max_val, factor;
5214 offsetT sval;
5215
5216 operand = (const struct mips_int_operand *) operand_base;
5217 factor = 1 << operand->shift;
5218 min_val = mips_int_operand_min (operand);
5219 max_val = mips_int_operand_max (operand);
5220
5221 if (operand_base->lsb == 0
5222 && operand_base->size == 16
5223 && operand->shift == 0
5224 && operand->bias == 0
5225 && (operand->max_val == 32767 || operand->max_val == 65535))
5226 {
5227 /* The operand can be relocated. */
5228 if (!match_expression (arg, &offset_expr, offset_reloc))
5229 return FALSE;
5230
5231 if (offset_expr.X_op == O_big)
5232 {
5233 match_out_of_range (arg);
5234 return FALSE;
5235 }
5236
5237 if (offset_reloc[0] != BFD_RELOC_UNUSED)
5238 /* Relocation operators were used. Accept the argument and
5239 leave the relocation value in offset_expr and offset_relocs
5240 for the caller to process. */
5241 return TRUE;
5242
5243 if (offset_expr.X_op != O_constant)
5244 {
5245 /* Accept non-constant operands if no later alternative matches,
5246 leaving it for the caller to process. */
5247 if (!arg->lax_match)
5248 {
5249 match_not_constant (arg);
5250 return FALSE;
5251 }
5252 offset_reloc[0] = BFD_RELOC_LO16;
5253 return TRUE;
5254 }
5255
5256 /* Clear the global state; we're going to install the operand
5257 ourselves. */
5258 sval = offset_expr.X_add_number;
5259 offset_expr.X_op = O_absent;
5260
5261 /* For compatibility with older assemblers, we accept
5262 0x8000-0xffff as signed 16-bit numbers when only
5263 signed numbers are allowed. */
5264 if (sval > max_val)
5265 {
5266 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5267 if (!arg->lax_match && sval <= max_val)
5268 {
5269 match_out_of_range (arg);
5270 return FALSE;
5271 }
5272 }
5273 }
5274 else
5275 {
5276 if (!match_const_int (arg, &sval))
5277 return FALSE;
5278 }
5279
5280 arg->last_op_int = sval;
5281
5282 if (sval < min_val || sval > max_val || sval % factor)
5283 {
5284 match_out_of_range (arg);
5285 return FALSE;
5286 }
5287
5288 uval = (unsigned int) sval >> operand->shift;
5289 uval -= operand->bias;
5290
5291 /* Handle -mfix-cn63xxp1. */
5292 if (arg->opnum == 1
5293 && mips_fix_cn63xxp1
5294 && !mips_opts.micromips
5295 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5296 switch (uval)
5297 {
5298 case 5:
5299 case 25:
5300 case 26:
5301 case 27:
5302 case 28:
5303 case 29:
5304 case 30:
5305 case 31:
5306 /* These are ok. */
5307 break;
5308
5309 default:
5310 /* The rest must be changed to 28. */
5311 uval = 28;
5312 break;
5313 }
5314
5315 insn_insert_operand (arg->insn, operand_base, uval);
5316 return TRUE;
5317 }
5318
5319 /* OP_MAPPED_INT matcher. */
5320
5321 static bfd_boolean
5322 match_mapped_int_operand (struct mips_arg_info *arg,
5323 const struct mips_operand *operand_base)
5324 {
5325 const struct mips_mapped_int_operand *operand;
5326 unsigned int uval, num_vals;
5327 offsetT sval;
5328
5329 operand = (const struct mips_mapped_int_operand *) operand_base;
5330 if (!match_const_int (arg, &sval))
5331 return FALSE;
5332
5333 num_vals = 1 << operand_base->size;
5334 for (uval = 0; uval < num_vals; uval++)
5335 if (operand->int_map[uval] == sval)
5336 break;
5337 if (uval == num_vals)
5338 {
5339 match_out_of_range (arg);
5340 return FALSE;
5341 }
5342
5343 insn_insert_operand (arg->insn, operand_base, uval);
5344 return TRUE;
5345 }
5346
5347 /* OP_MSB matcher. */
5348
5349 static bfd_boolean
5350 match_msb_operand (struct mips_arg_info *arg,
5351 const struct mips_operand *operand_base)
5352 {
5353 const struct mips_msb_operand *operand;
5354 int min_val, max_val, max_high;
5355 offsetT size, sval, high;
5356
5357 operand = (const struct mips_msb_operand *) operand_base;
5358 min_val = operand->bias;
5359 max_val = min_val + (1 << operand_base->size) - 1;
5360 max_high = operand->opsize;
5361
5362 if (!match_const_int (arg, &size))
5363 return FALSE;
5364
5365 high = size + arg->last_op_int;
5366 sval = operand->add_lsb ? high : size;
5367
5368 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5369 {
5370 match_out_of_range (arg);
5371 return FALSE;
5372 }
5373 insn_insert_operand (arg->insn, operand_base, sval - min_val);
5374 return TRUE;
5375 }
5376
5377 /* OP_REG matcher. */
5378
5379 static bfd_boolean
5380 match_reg_operand (struct mips_arg_info *arg,
5381 const struct mips_operand *operand_base)
5382 {
5383 const struct mips_reg_operand *operand;
5384 unsigned int regno, uval, num_vals;
5385
5386 operand = (const struct mips_reg_operand *) operand_base;
5387 if (!match_reg (arg, operand->reg_type, &regno))
5388 return FALSE;
5389
5390 if (operand->reg_map)
5391 {
5392 num_vals = 1 << operand->root.size;
5393 for (uval = 0; uval < num_vals; uval++)
5394 if (operand->reg_map[uval] == regno)
5395 break;
5396 if (num_vals == uval)
5397 return FALSE;
5398 }
5399 else
5400 uval = regno;
5401
5402 arg->last_regno = regno;
5403 if (arg->opnum == 1)
5404 arg->dest_regno = regno;
5405 insn_insert_operand (arg->insn, operand_base, uval);
5406 return TRUE;
5407 }
5408
5409 /* OP_REG_PAIR matcher. */
5410
5411 static bfd_boolean
5412 match_reg_pair_operand (struct mips_arg_info *arg,
5413 const struct mips_operand *operand_base)
5414 {
5415 const struct mips_reg_pair_operand *operand;
5416 unsigned int regno1, regno2, uval, num_vals;
5417
5418 operand = (const struct mips_reg_pair_operand *) operand_base;
5419 if (!match_reg (arg, operand->reg_type, &regno1)
5420 || !match_char (arg, ',')
5421 || !match_reg (arg, operand->reg_type, &regno2))
5422 return FALSE;
5423
5424 num_vals = 1 << operand_base->size;
5425 for (uval = 0; uval < num_vals; uval++)
5426 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5427 break;
5428 if (uval == num_vals)
5429 return FALSE;
5430
5431 insn_insert_operand (arg->insn, operand_base, uval);
5432 return TRUE;
5433 }
5434
5435 /* OP_PCREL matcher. The caller chooses the relocation type. */
5436
5437 static bfd_boolean
5438 match_pcrel_operand (struct mips_arg_info *arg)
5439 {
5440 bfd_reloc_code_real_type r[3];
5441
5442 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
5443 }
5444
5445 /* OP_PERF_REG matcher. */
5446
5447 static bfd_boolean
5448 match_perf_reg_operand (struct mips_arg_info *arg,
5449 const struct mips_operand *operand)
5450 {
5451 offsetT sval;
5452
5453 if (!match_const_int (arg, &sval))
5454 return FALSE;
5455
5456 if (sval != 0
5457 && (sval != 1
5458 || (mips_opts.arch == CPU_R5900
5459 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5460 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5461 {
5462 set_insn_error (arg->argnum, _("invalid performance register"));
5463 return FALSE;
5464 }
5465
5466 insn_insert_operand (arg->insn, operand, sval);
5467 return TRUE;
5468 }
5469
5470 /* OP_ADDIUSP matcher. */
5471
5472 static bfd_boolean
5473 match_addiusp_operand (struct mips_arg_info *arg,
5474 const struct mips_operand *operand)
5475 {
5476 offsetT sval;
5477 unsigned int uval;
5478
5479 if (!match_const_int (arg, &sval))
5480 return FALSE;
5481
5482 if (sval % 4)
5483 {
5484 match_out_of_range (arg);
5485 return FALSE;
5486 }
5487
5488 sval /= 4;
5489 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
5490 {
5491 match_out_of_range (arg);
5492 return FALSE;
5493 }
5494
5495 uval = (unsigned int) sval;
5496 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5497 insn_insert_operand (arg->insn, operand, uval);
5498 return TRUE;
5499 }
5500
5501 /* OP_CLO_CLZ_DEST matcher. */
5502
5503 static bfd_boolean
5504 match_clo_clz_dest_operand (struct mips_arg_info *arg,
5505 const struct mips_operand *operand)
5506 {
5507 unsigned int regno;
5508
5509 if (!match_reg (arg, OP_REG_GP, &regno))
5510 return FALSE;
5511
5512 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5513 return TRUE;
5514 }
5515
5516 /* OP_CHECK_PREV matcher. */
5517
5518 static bfd_boolean
5519 match_check_prev_operand (struct mips_arg_info *arg,
5520 const struct mips_operand *operand_base)
5521 {
5522 const struct mips_check_prev_operand *operand;
5523 unsigned int regno;
5524
5525 operand = (const struct mips_check_prev_operand *) operand_base;
5526
5527 if (!match_reg (arg, OP_REG_GP, &regno))
5528 return FALSE;
5529
5530 if (!operand->zero_ok && regno == 0)
5531 return FALSE;
5532
5533 if ((operand->less_than_ok && regno < arg->last_regno)
5534 || (operand->greater_than_ok && regno > arg->last_regno)
5535 || (operand->equal_ok && regno == arg->last_regno))
5536 {
5537 arg->last_regno = regno;
5538 insn_insert_operand (arg->insn, operand_base, regno);
5539 return TRUE;
5540 }
5541
5542 return FALSE;
5543 }
5544
5545 /* OP_SAME_RS_RT matcher. */
5546
5547 static bfd_boolean
5548 match_same_rs_rt_operand (struct mips_arg_info *arg,
5549 const struct mips_operand *operand)
5550 {
5551 unsigned int regno;
5552
5553 if (!match_reg (arg, OP_REG_GP, &regno))
5554 return FALSE;
5555
5556 if (regno == 0)
5557 {
5558 set_insn_error (arg->argnum, _("the source register must not be $0"));
5559 return FALSE;
5560 }
5561
5562 arg->last_regno = regno;
5563
5564 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5565 return TRUE;
5566 }
5567
5568 /* OP_LWM_SWM_LIST matcher. */
5569
5570 static bfd_boolean
5571 match_lwm_swm_list_operand (struct mips_arg_info *arg,
5572 const struct mips_operand *operand)
5573 {
5574 unsigned int reglist, sregs, ra, regno1, regno2;
5575 struct mips_arg_info reset;
5576
5577 reglist = 0;
5578 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5579 return FALSE;
5580 do
5581 {
5582 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5583 {
5584 reglist |= 1 << FP;
5585 regno2 = S7;
5586 }
5587 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5588 reset = *arg;
5589 }
5590 while (match_char (arg, ',')
5591 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5592 *arg = reset;
5593
5594 if (operand->size == 2)
5595 {
5596 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5597
5598 s0, ra
5599 s0, s1, ra, s2, s3
5600 s0-s2, ra
5601
5602 and any permutations of these. */
5603 if ((reglist & 0xfff1ffff) != 0x80010000)
5604 return FALSE;
5605
5606 sregs = (reglist >> 17) & 7;
5607 ra = 0;
5608 }
5609 else
5610 {
5611 /* The list must include at least one of ra and s0-sN,
5612 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5613 which are $23 and $30 respectively.) E.g.:
5614
5615 ra
5616 s0
5617 ra, s0, s1, s2
5618 s0-s8
5619 s0-s5, ra
5620
5621 and any permutations of these. */
5622 if ((reglist & 0x3f00ffff) != 0)
5623 return FALSE;
5624
5625 ra = (reglist >> 27) & 0x10;
5626 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5627 }
5628 sregs += 1;
5629 if ((sregs & -sregs) != sregs)
5630 return FALSE;
5631
5632 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5633 return TRUE;
5634 }
5635
5636 /* OP_ENTRY_EXIT_LIST matcher. */
5637
5638 static unsigned int
5639 match_entry_exit_operand (struct mips_arg_info *arg,
5640 const struct mips_operand *operand)
5641 {
5642 unsigned int mask;
5643 bfd_boolean is_exit;
5644
5645 /* The format is the same for both ENTRY and EXIT, but the constraints
5646 are different. */
5647 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5648 mask = (is_exit ? 7 << 3 : 0);
5649 do
5650 {
5651 unsigned int regno1, regno2;
5652 bfd_boolean is_freg;
5653
5654 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5655 is_freg = FALSE;
5656 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5657 is_freg = TRUE;
5658 else
5659 return FALSE;
5660
5661 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5662 {
5663 mask &= ~(7 << 3);
5664 mask |= (5 + regno2) << 3;
5665 }
5666 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5667 mask |= (regno2 - 3) << 3;
5668 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5669 mask |= (regno2 - 15) << 1;
5670 else if (regno1 == RA && regno2 == RA)
5671 mask |= 1;
5672 else
5673 return FALSE;
5674 }
5675 while (match_char (arg, ','));
5676
5677 insn_insert_operand (arg->insn, operand, mask);
5678 return TRUE;
5679 }
5680
5681 /* Encode regular MIPS SAVE/RESTORE instruction operands according to
5682 the argument register mask AMASK, the number of static registers
5683 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5684 respectively, and the frame size FRAME_SIZE. */
5685
5686 static unsigned int
5687 mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5688 unsigned int ra, unsigned int s0, unsigned int s1,
5689 unsigned int frame_size)
5690 {
5691 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5692 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5693 }
5694
5695 /* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5696 argument register mask AMASK, the number of static registers saved
5697 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5698 respectively, and the frame size FRAME_SIZE. */
5699
5700 static unsigned int
5701 mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5702 unsigned int ra, unsigned int s0, unsigned int s1,
5703 unsigned int frame_size)
5704 {
5705 unsigned int args;
5706
5707 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5708 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5709 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5710 | ((frame_size & 0xf0) << 16));
5711 return args;
5712 }
5713
5714 /* OP_SAVE_RESTORE_LIST matcher. */
5715
5716 static bfd_boolean
5717 match_save_restore_list_operand (struct mips_arg_info *arg)
5718 {
5719 unsigned int opcode, args, statics, sregs;
5720 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
5721 unsigned int arg_mask, ra, s0, s1;
5722 offsetT frame_size;
5723
5724 opcode = arg->insn->insn_opcode;
5725 frame_size = 0;
5726 num_frame_sizes = 0;
5727 args = 0;
5728 statics = 0;
5729 sregs = 0;
5730 ra = 0;
5731 s0 = 0;
5732 s1 = 0;
5733 do
5734 {
5735 unsigned int regno1, regno2;
5736
5737 if (arg->token->type == OT_INTEGER)
5738 {
5739 /* Handle the frame size. */
5740 if (!match_const_int (arg, &frame_size))
5741 return FALSE;
5742 num_frame_sizes += 1;
5743 }
5744 else
5745 {
5746 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5747 return FALSE;
5748
5749 while (regno1 <= regno2)
5750 {
5751 if (regno1 >= 4 && regno1 <= 7)
5752 {
5753 if (num_frame_sizes == 0)
5754 /* args $a0-$a3 */
5755 args |= 1 << (regno1 - 4);
5756 else
5757 /* statics $a0-$a3 */
5758 statics |= 1 << (regno1 - 4);
5759 }
5760 else if (regno1 >= 16 && regno1 <= 23)
5761 /* $s0-$s7 */
5762 sregs |= 1 << (regno1 - 16);
5763 else if (regno1 == 30)
5764 /* $s8 */
5765 sregs |= 1 << 8;
5766 else if (regno1 == 31)
5767 /* Add $ra to insn. */
5768 ra = 1;
5769 else
5770 return FALSE;
5771 regno1 += 1;
5772 if (regno1 == 24)
5773 regno1 = 30;
5774 }
5775 }
5776 }
5777 while (match_char (arg, ','));
5778
5779 /* Encode args/statics combination. */
5780 if (args & statics)
5781 return FALSE;
5782 else if (args == 0xf)
5783 /* All $a0-$a3 are args. */
5784 arg_mask = MIPS_SVRS_ALL_ARGS;
5785 else if (statics == 0xf)
5786 /* All $a0-$a3 are statics. */
5787 arg_mask = MIPS_SVRS_ALL_STATICS;
5788 else
5789 {
5790 /* Count arg registers. */
5791 num_args = 0;
5792 while (args & 0x1)
5793 {
5794 args >>= 1;
5795 num_args += 1;
5796 }
5797 if (args != 0)
5798 return FALSE;
5799
5800 /* Count static registers. */
5801 num_statics = 0;
5802 while (statics & 0x8)
5803 {
5804 statics = (statics << 1) & 0xf;
5805 num_statics += 1;
5806 }
5807 if (statics != 0)
5808 return FALSE;
5809
5810 /* Encode args/statics. */
5811 arg_mask = (num_args << 2) | num_statics;
5812 }
5813
5814 /* Encode $s0/$s1. */
5815 if (sregs & (1 << 0)) /* $s0 */
5816 s0 = 1;
5817 if (sregs & (1 << 1)) /* $s1 */
5818 s1 = 1;
5819 sregs >>= 2;
5820
5821 /* Encode $s2-$s8. */
5822 num_sregs = 0;
5823 while (sregs & 1)
5824 {
5825 sregs >>= 1;
5826 num_sregs += 1;
5827 }
5828 if (sregs != 0)
5829 return FALSE;
5830
5831 /* Encode frame size. */
5832 if (num_frame_sizes == 0)
5833 {
5834 set_insn_error (arg->argnum, _("missing frame size"));
5835 return FALSE;
5836 }
5837 if (num_frame_sizes > 1)
5838 {
5839 set_insn_error (arg->argnum, _("frame size specified twice"));
5840 return FALSE;
5841 }
5842 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5843 {
5844 set_insn_error (arg->argnum, _("invalid frame size"));
5845 return FALSE;
5846 }
5847 frame_size /= 8;
5848
5849 /* Finally build the instruction. */
5850 if (mips_opts.mips16)
5851 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5852 frame_size);
5853 else if (!mips_opts.micromips)
5854 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5855 frame_size);
5856 else
5857 abort ();
5858
5859 arg->insn->insn_opcode = opcode;
5860 return TRUE;
5861 }
5862
5863 /* OP_MDMX_IMM_REG matcher. */
5864
5865 static bfd_boolean
5866 match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
5867 const struct mips_operand *operand)
5868 {
5869 unsigned int regno, uval;
5870 bfd_boolean is_qh;
5871 const struct mips_opcode *opcode;
5872
5873 /* The mips_opcode records whether this is an octobyte or quadhalf
5874 instruction. Start out with that bit in place. */
5875 opcode = arg->insn->insn_mo;
5876 uval = mips_extract_operand (operand, opcode->match);
5877 is_qh = (uval != 0);
5878
5879 if (arg->token->type == OT_REG)
5880 {
5881 if ((opcode->membership & INSN_5400)
5882 && strcmp (opcode->name, "rzu.ob") == 0)
5883 {
5884 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5885 arg->argnum);
5886 return FALSE;
5887 }
5888
5889 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5890 return FALSE;
5891 ++arg->token;
5892
5893 /* Check whether this is a vector register or a broadcast of
5894 a single element. */
5895 if (arg->token->type == OT_INTEGER_INDEX)
5896 {
5897 if (arg->token->u.index > (is_qh ? 3 : 7))
5898 {
5899 set_insn_error (arg->argnum, _("invalid element selector"));
5900 return FALSE;
5901 }
5902 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5903 ++arg->token;
5904 }
5905 else
5906 {
5907 /* A full vector. */
5908 if ((opcode->membership & INSN_5400)
5909 && (strcmp (opcode->name, "sll.ob") == 0
5910 || strcmp (opcode->name, "srl.ob") == 0))
5911 {
5912 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5913 arg->argnum);
5914 return FALSE;
5915 }
5916
5917 if (is_qh)
5918 uval |= MDMX_FMTSEL_VEC_QH << 5;
5919 else
5920 uval |= MDMX_FMTSEL_VEC_OB << 5;
5921 }
5922 uval |= regno;
5923 }
5924 else
5925 {
5926 offsetT sval;
5927
5928 if (!match_const_int (arg, &sval))
5929 return FALSE;
5930 if (sval < 0 || sval > 31)
5931 {
5932 match_out_of_range (arg);
5933 return FALSE;
5934 }
5935 uval |= (sval & 31);
5936 if (is_qh)
5937 uval |= MDMX_FMTSEL_IMM_QH << 5;
5938 else
5939 uval |= MDMX_FMTSEL_IMM_OB << 5;
5940 }
5941 insn_insert_operand (arg->insn, operand, uval);
5942 return TRUE;
5943 }
5944
5945 /* OP_IMM_INDEX matcher. */
5946
5947 static bfd_boolean
5948 match_imm_index_operand (struct mips_arg_info *arg,
5949 const struct mips_operand *operand)
5950 {
5951 unsigned int max_val;
5952
5953 if (arg->token->type != OT_INTEGER_INDEX)
5954 return FALSE;
5955
5956 max_val = (1 << operand->size) - 1;
5957 if (arg->token->u.index > max_val)
5958 {
5959 match_out_of_range (arg);
5960 return FALSE;
5961 }
5962 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5963 ++arg->token;
5964 return TRUE;
5965 }
5966
5967 /* OP_REG_INDEX matcher. */
5968
5969 static bfd_boolean
5970 match_reg_index_operand (struct mips_arg_info *arg,
5971 const struct mips_operand *operand)
5972 {
5973 unsigned int regno;
5974
5975 if (arg->token->type != OT_REG_INDEX)
5976 return FALSE;
5977
5978 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5979 return FALSE;
5980
5981 insn_insert_operand (arg->insn, operand, regno);
5982 ++arg->token;
5983 return TRUE;
5984 }
5985
5986 /* OP_PC matcher. */
5987
5988 static bfd_boolean
5989 match_pc_operand (struct mips_arg_info *arg)
5990 {
5991 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5992 {
5993 ++arg->token;
5994 return TRUE;
5995 }
5996 return FALSE;
5997 }
5998
5999 /* OP_REG28 matcher. */
6000
6001 static bfd_boolean
6002 match_reg28_operand (struct mips_arg_info *arg)
6003 {
6004 unsigned int regno;
6005
6006 if (arg->token->type == OT_REG
6007 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
6008 && regno == GP)
6009 {
6010 ++arg->token;
6011 return TRUE;
6012 }
6013 return FALSE;
6014 }
6015
6016 /* OP_NON_ZERO_REG matcher. */
6017
6018 static bfd_boolean
6019 match_non_zero_reg_operand (struct mips_arg_info *arg,
6020 const struct mips_operand *operand)
6021 {
6022 unsigned int regno;
6023
6024 if (!match_reg (arg, OP_REG_GP, &regno))
6025 return FALSE;
6026
6027 if (regno == 0)
6028 {
6029 set_insn_error (arg->argnum, _("the source register must not be $0"));
6030 return FALSE;
6031 }
6032
6033 arg->last_regno = regno;
6034 insn_insert_operand (arg->insn, operand, regno);
6035 return TRUE;
6036 }
6037
6038 /* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
6039 register that we need to match. */
6040
6041 static bfd_boolean
6042 match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
6043 {
6044 unsigned int regno;
6045
6046 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
6047 }
6048
6049 /* Try to match a floating-point constant from ARG for LI.S or LI.D.
6050 LENGTH is the length of the value in bytes (4 for float, 8 for double)
6051 and USING_GPRS says whether the destination is a GPR rather than an FPR.
6052
6053 Return the constant in IMM and OFFSET as follows:
6054
6055 - If the constant should be loaded via memory, set IMM to O_absent and
6056 OFFSET to the memory address.
6057
6058 - Otherwise, if the constant should be loaded into two 32-bit registers,
6059 set IMM to the O_constant to load into the high register and OFFSET
6060 to the corresponding value for the low register.
6061
6062 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6063
6064 These constants only appear as the last operand in an instruction,
6065 and every instruction that accepts them in any variant accepts them
6066 in all variants. This means we don't have to worry about backing out
6067 any changes if the instruction does not match. We just match
6068 unconditionally and report an error if the constant is invalid. */
6069
6070 static bfd_boolean
6071 match_float_constant (struct mips_arg_info *arg, expressionS *imm,
6072 expressionS *offset, int length, bfd_boolean using_gprs)
6073 {
6074 char *p;
6075 segT seg, new_seg;
6076 subsegT subseg;
6077 const char *newname;
6078 unsigned char *data;
6079
6080 /* Where the constant is placed is based on how the MIPS assembler
6081 does things:
6082
6083 length == 4 && using_gprs -- immediate value only
6084 length == 8 && using_gprs -- .rdata or immediate value
6085 length == 4 && !using_gprs -- .lit4 or immediate value
6086 length == 8 && !using_gprs -- .lit8 or immediate value
6087
6088 The .lit4 and .lit8 sections are only used if permitted by the
6089 -G argument. */
6090 if (arg->token->type != OT_FLOAT)
6091 {
6092 set_insn_error (arg->argnum, _("floating-point expression required"));
6093 return FALSE;
6094 }
6095
6096 gas_assert (arg->token->u.flt.length == length);
6097 data = arg->token->u.flt.data;
6098 ++arg->token;
6099
6100 /* Handle 32-bit constants for which an immediate value is best. */
6101 if (length == 4
6102 && (using_gprs
6103 || g_switch_value < 4
6104 || (data[0] == 0 && data[1] == 0)
6105 || (data[2] == 0 && data[3] == 0)))
6106 {
6107 imm->X_op = O_constant;
6108 if (!target_big_endian)
6109 imm->X_add_number = bfd_getl32 (data);
6110 else
6111 imm->X_add_number = bfd_getb32 (data);
6112 offset->X_op = O_absent;
6113 return TRUE;
6114 }
6115
6116 /* Handle 64-bit constants for which an immediate value is best. */
6117 if (length == 8
6118 && !mips_disable_float_construction
6119 /* Constants can only be constructed in GPRs and copied to FPRs if the
6120 GPRs are at least as wide as the FPRs or MTHC1 is available.
6121 Unlike most tests for 32-bit floating-point registers this check
6122 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6123 permit 64-bit moves without MXHC1.
6124 Force the constant into memory otherwise. */
6125 && (using_gprs
6126 || GPR_SIZE == 64
6127 || ISA_HAS_MXHC1 (mips_opts.isa)
6128 || FPR_SIZE == 32)
6129 && ((data[0] == 0 && data[1] == 0)
6130 || (data[2] == 0 && data[3] == 0))
6131 && ((data[4] == 0 && data[5] == 0)
6132 || (data[6] == 0 && data[7] == 0)))
6133 {
6134 /* The value is simple enough to load with a couple of instructions.
6135 If using 32-bit registers, set IMM to the high order 32 bits and
6136 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6137 64 bit constant. */
6138 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
6139 {
6140 imm->X_op = O_constant;
6141 offset->X_op = O_constant;
6142 if (!target_big_endian)
6143 {
6144 imm->X_add_number = bfd_getl32 (data + 4);
6145 offset->X_add_number = bfd_getl32 (data);
6146 }
6147 else
6148 {
6149 imm->X_add_number = bfd_getb32 (data);
6150 offset->X_add_number = bfd_getb32 (data + 4);
6151 }
6152 if (offset->X_add_number == 0)
6153 offset->X_op = O_absent;
6154 }
6155 else
6156 {
6157 imm->X_op = O_constant;
6158 if (!target_big_endian)
6159 imm->X_add_number = bfd_getl64 (data);
6160 else
6161 imm->X_add_number = bfd_getb64 (data);
6162 offset->X_op = O_absent;
6163 }
6164 return TRUE;
6165 }
6166
6167 /* Switch to the right section. */
6168 seg = now_seg;
6169 subseg = now_subseg;
6170 if (length == 4)
6171 {
6172 gas_assert (!using_gprs && g_switch_value >= 4);
6173 newname = ".lit4";
6174 }
6175 else
6176 {
6177 if (using_gprs || g_switch_value < 8)
6178 newname = RDATA_SECTION_NAME;
6179 else
6180 newname = ".lit8";
6181 }
6182
6183 new_seg = subseg_new (newname, (subsegT) 0);
6184 bfd_set_section_flags (new_seg,
6185 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6186 frag_align (length == 4 ? 2 : 3, 0, 0);
6187 if (strncmp (TARGET_OS, "elf", 3) != 0)
6188 record_alignment (new_seg, 4);
6189 else
6190 record_alignment (new_seg, length == 4 ? 2 : 3);
6191 if (seg == now_seg)
6192 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
6193
6194 /* Set the argument to the current address in the section. */
6195 imm->X_op = O_absent;
6196 offset->X_op = O_symbol;
6197 offset->X_add_symbol = symbol_temp_new_now ();
6198 offset->X_add_number = 0;
6199
6200 /* Put the floating point number into the section. */
6201 p = frag_more (length);
6202 memcpy (p, data, length);
6203
6204 /* Switch back to the original section. */
6205 subseg_set (seg, subseg);
6206 return TRUE;
6207 }
6208
6209 /* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6210 them. */
6211
6212 static bfd_boolean
6213 match_vu0_suffix_operand (struct mips_arg_info *arg,
6214 const struct mips_operand *operand,
6215 bfd_boolean match_p)
6216 {
6217 unsigned int uval;
6218
6219 /* The operand can be an XYZW mask or a single 2-bit channel index
6220 (with X being 0). */
6221 gas_assert (operand->size == 2 || operand->size == 4);
6222
6223 /* The suffix can be omitted when it is already part of the opcode. */
6224 if (arg->token->type != OT_CHANNELS)
6225 return match_p;
6226
6227 uval = arg->token->u.channels;
6228 if (operand->size == 2)
6229 {
6230 /* Check that a single bit is set and convert it into a 2-bit index. */
6231 if ((uval & -uval) != uval)
6232 return FALSE;
6233 uval = 4 - ffs (uval);
6234 }
6235
6236 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6237 return FALSE;
6238
6239 ++arg->token;
6240 if (!match_p)
6241 insn_insert_operand (arg->insn, operand, uval);
6242 return TRUE;
6243 }
6244
6245 /* Try to match a token from ARG against OPERAND. Consume the token
6246 and return true on success, otherwise return false. */
6247
6248 static bfd_boolean
6249 match_operand (struct mips_arg_info *arg,
6250 const struct mips_operand *operand)
6251 {
6252 switch (operand->type)
6253 {
6254 case OP_INT:
6255 return match_int_operand (arg, operand);
6256
6257 case OP_MAPPED_INT:
6258 return match_mapped_int_operand (arg, operand);
6259
6260 case OP_MSB:
6261 return match_msb_operand (arg, operand);
6262
6263 case OP_REG:
6264 case OP_OPTIONAL_REG:
6265 return match_reg_operand (arg, operand);
6266
6267 case OP_REG_PAIR:
6268 return match_reg_pair_operand (arg, operand);
6269
6270 case OP_PCREL:
6271 return match_pcrel_operand (arg);
6272
6273 case OP_PERF_REG:
6274 return match_perf_reg_operand (arg, operand);
6275
6276 case OP_ADDIUSP_INT:
6277 return match_addiusp_operand (arg, operand);
6278
6279 case OP_CLO_CLZ_DEST:
6280 return match_clo_clz_dest_operand (arg, operand);
6281
6282 case OP_LWM_SWM_LIST:
6283 return match_lwm_swm_list_operand (arg, operand);
6284
6285 case OP_ENTRY_EXIT_LIST:
6286 return match_entry_exit_operand (arg, operand);
6287
6288 case OP_SAVE_RESTORE_LIST:
6289 return match_save_restore_list_operand (arg);
6290
6291 case OP_MDMX_IMM_REG:
6292 return match_mdmx_imm_reg_operand (arg, operand);
6293
6294 case OP_REPEAT_DEST_REG:
6295 return match_tied_reg_operand (arg, arg->dest_regno);
6296
6297 case OP_REPEAT_PREV_REG:
6298 return match_tied_reg_operand (arg, arg->last_regno);
6299
6300 case OP_PC:
6301 return match_pc_operand (arg);
6302
6303 case OP_REG28:
6304 return match_reg28_operand (arg);
6305
6306 case OP_VU0_SUFFIX:
6307 return match_vu0_suffix_operand (arg, operand, FALSE);
6308
6309 case OP_VU0_MATCH_SUFFIX:
6310 return match_vu0_suffix_operand (arg, operand, TRUE);
6311
6312 case OP_IMM_INDEX:
6313 return match_imm_index_operand (arg, operand);
6314
6315 case OP_REG_INDEX:
6316 return match_reg_index_operand (arg, operand);
6317
6318 case OP_SAME_RS_RT:
6319 return match_same_rs_rt_operand (arg, operand);
6320
6321 case OP_CHECK_PREV:
6322 return match_check_prev_operand (arg, operand);
6323
6324 case OP_NON_ZERO_REG:
6325 return match_non_zero_reg_operand (arg, operand);
6326 }
6327 abort ();
6328 }
6329
6330 /* ARG is the state after successfully matching an instruction.
6331 Issue any queued-up warnings. */
6332
6333 static void
6334 check_completed_insn (struct mips_arg_info *arg)
6335 {
6336 if (arg->seen_at)
6337 {
6338 if (AT == ATREG)
6339 as_warn (_("used $at without \".set noat\""));
6340 else
6341 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
6342 }
6343 }
6344
6345 /* Return true if modifying general-purpose register REG needs a delay. */
6346
6347 static bfd_boolean
6348 reg_needs_delay (unsigned int reg)
6349 {
6350 unsigned long prev_pinfo;
6351
6352 prev_pinfo = history[0].insn_mo->pinfo;
6353 if (!mips_opts.noreorder
6354 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
6355 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
6356 && (gpr_write_mask (&history[0]) & (1 << reg)))
6357 return TRUE;
6358
6359 return FALSE;
6360 }
6361
6362 /* Classify an instruction according to the FIX_VR4120_* enumeration.
6363 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6364 by VR4120 errata. */
6365
6366 static unsigned int
6367 classify_vr4120_insn (const char *name)
6368 {
6369 if (strncmp (name, "macc", 4) == 0)
6370 return FIX_VR4120_MACC;
6371 if (strncmp (name, "dmacc", 5) == 0)
6372 return FIX_VR4120_DMACC;
6373 if (strncmp (name, "mult", 4) == 0)
6374 return FIX_VR4120_MULT;
6375 if (strncmp (name, "dmult", 5) == 0)
6376 return FIX_VR4120_DMULT;
6377 if (strstr (name, "div"))
6378 return FIX_VR4120_DIV;
6379 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6380 return FIX_VR4120_MTHILO;
6381 return NUM_FIX_VR4120_CLASSES;
6382 }
6383
6384 #define INSN_ERET 0x42000018
6385 #define INSN_DERET 0x4200001f
6386 #define INSN_DMULT 0x1c
6387 #define INSN_DMULTU 0x1d
6388
6389 /* Return the number of instructions that must separate INSN1 and INSN2,
6390 where INSN1 is the earlier instruction. Return the worst-case value
6391 for any INSN2 if INSN2 is null. */
6392
6393 static unsigned int
6394 insns_between (const struct mips_cl_insn *insn1,
6395 const struct mips_cl_insn *insn2)
6396 {
6397 unsigned long pinfo1, pinfo2;
6398 unsigned int mask;
6399
6400 /* If INFO2 is null, pessimistically assume that all flags are set for
6401 the second instruction. */
6402 pinfo1 = insn1->insn_mo->pinfo;
6403 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
6404
6405 /* For most targets, write-after-read dependencies on the HI and LO
6406 registers must be separated by at least two instructions. */
6407 if (!hilo_interlocks)
6408 {
6409 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6410 return 2;
6411 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6412 return 2;
6413 }
6414
6415 /* If we're working around r7000 errata, there must be two instructions
6416 between an mfhi or mflo and any instruction that uses the result. */
6417 if (mips_7000_hilo_fix
6418 && !mips_opts.micromips
6419 && MF_HILO_INSN (pinfo1)
6420 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
6421 return 2;
6422
6423 /* If we're working around 24K errata, one instruction is required
6424 if an ERET or DERET is followed by a branch instruction. */
6425 if (mips_fix_24k && !mips_opts.micromips)
6426 {
6427 if (insn1->insn_opcode == INSN_ERET
6428 || insn1->insn_opcode == INSN_DERET)
6429 {
6430 if (insn2 == NULL
6431 || insn2->insn_opcode == INSN_ERET
6432 || insn2->insn_opcode == INSN_DERET
6433 || delayed_branch_p (insn2))
6434 return 1;
6435 }
6436 }
6437
6438 /* If we're working around PMC RM7000 errata, there must be three
6439 nops between a dmult and a load instruction. */
6440 if (mips_fix_rm7000 && !mips_opts.micromips)
6441 {
6442 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6443 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6444 {
6445 if (pinfo2 & INSN_LOAD_MEMORY)
6446 return 3;
6447 }
6448 }
6449
6450 /* If working around VR4120 errata, check for combinations that need
6451 a single intervening instruction. */
6452 if (mips_fix_vr4120 && !mips_opts.micromips)
6453 {
6454 unsigned int class1, class2;
6455
6456 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6457 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
6458 {
6459 if (insn2 == NULL)
6460 return 1;
6461 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6462 if (vr4120_conflicts[class1] & (1 << class2))
6463 return 1;
6464 }
6465 }
6466
6467 if (!HAVE_CODE_COMPRESSION)
6468 {
6469 /* Check for GPR or coprocessor load delays. All such delays
6470 are on the RT register. */
6471 /* Itbl support may require additional care here. */
6472 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
6473 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
6474 {
6475 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
6476 return 1;
6477 }
6478
6479 /* Check for generic coprocessor hazards.
6480
6481 This case is not handled very well. There is no special
6482 knowledge of CP0 handling, and the coprocessors other than
6483 the floating point unit are not distinguished at all. */
6484 /* Itbl support may require additional care here. FIXME!
6485 Need to modify this to include knowledge about
6486 user specified delays! */
6487 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
6488 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6489 {
6490 /* Handle cases where INSN1 writes to a known general coprocessor
6491 register. There must be a one instruction delay before INSN2
6492 if INSN2 reads that register, otherwise no delay is needed. */
6493 mask = fpr_write_mask (insn1);
6494 if (mask != 0)
6495 {
6496 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
6497 return 1;
6498 }
6499 else
6500 {
6501 /* Read-after-write dependencies on the control registers
6502 require a two-instruction gap. */
6503 if ((pinfo1 & INSN_WRITE_COND_CODE)
6504 && (pinfo2 & INSN_READ_COND_CODE))
6505 return 2;
6506
6507 /* We don't know exactly what INSN1 does. If INSN2 is
6508 also a coprocessor instruction, assume there must be
6509 a one instruction gap. */
6510 if (pinfo2 & INSN_COP)
6511 return 1;
6512 }
6513 }
6514
6515 /* Check for read-after-write dependencies on the coprocessor
6516 control registers in cases where INSN1 does not need a general
6517 coprocessor delay. This means that INSN1 is a floating point
6518 comparison instruction. */
6519 /* Itbl support may require additional care here. */
6520 else if (!cop_interlocks
6521 && (pinfo1 & INSN_WRITE_COND_CODE)
6522 && (pinfo2 & INSN_READ_COND_CODE))
6523 return 1;
6524 }
6525
6526 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6527 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6528 and pause. */
6529 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6530 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6531 || (insn2 && delayed_branch_p (insn2))))
6532 return 1;
6533
6534 return 0;
6535 }
6536
6537 /* Return the number of nops that would be needed to work around the
6538 VR4130 mflo/mfhi errata if instruction INSN immediately followed
6539 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6540 that are contained within the first IGNORE instructions of HIST. */
6541
6542 static int
6543 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
6544 const struct mips_cl_insn *insn)
6545 {
6546 int i, j;
6547 unsigned int mask;
6548
6549 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6550 are not affected by the errata. */
6551 if (insn != 0
6552 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6553 || strcmp (insn->insn_mo->name, "mtlo") == 0
6554 || strcmp (insn->insn_mo->name, "mthi") == 0))
6555 return 0;
6556
6557 /* Search for the first MFLO or MFHI. */
6558 for (i = 0; i < MAX_VR4130_NOPS; i++)
6559 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
6560 {
6561 /* Extract the destination register. */
6562 mask = gpr_write_mask (&hist[i]);
6563
6564 /* No nops are needed if INSN reads that register. */
6565 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
6566 return 0;
6567
6568 /* ...or if any of the intervening instructions do. */
6569 for (j = 0; j < i; j++)
6570 if (gpr_read_mask (&hist[j]) & mask)
6571 return 0;
6572
6573 if (i >= ignore)
6574 return MAX_VR4130_NOPS - i;
6575 }
6576 return 0;
6577 }
6578
6579 #define BASE_REG_EQ(INSN1, INSN2) \
6580 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
6581 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6582
6583 /* Return the minimum alignment for this store instruction. */
6584
6585 static int
6586 fix_24k_align_to (const struct mips_opcode *mo)
6587 {
6588 if (strcmp (mo->name, "sh") == 0)
6589 return 2;
6590
6591 if (strcmp (mo->name, "swc1") == 0
6592 || strcmp (mo->name, "swc2") == 0
6593 || strcmp (mo->name, "sw") == 0
6594 || strcmp (mo->name, "sc") == 0
6595 || strcmp (mo->name, "s.s") == 0)
6596 return 4;
6597
6598 if (strcmp (mo->name, "sdc1") == 0
6599 || strcmp (mo->name, "sdc2") == 0
6600 || strcmp (mo->name, "s.d") == 0)
6601 return 8;
6602
6603 /* sb, swl, swr */
6604 return 1;
6605 }
6606
6607 struct fix_24k_store_info
6608 {
6609 /* Immediate offset, if any, for this store instruction. */
6610 short off;
6611 /* Alignment required by this store instruction. */
6612 int align_to;
6613 /* True for register offsets. */
6614 int register_offset;
6615 };
6616
6617 /* Comparison function used by qsort. */
6618
6619 static int
6620 fix_24k_sort (const void *a, const void *b)
6621 {
6622 const struct fix_24k_store_info *pos1 = a;
6623 const struct fix_24k_store_info *pos2 = b;
6624
6625 return (pos1->off - pos2->off);
6626 }
6627
6628 /* INSN is a store instruction. Try to record the store information
6629 in STINFO. Return false if the information isn't known. */
6630
6631 static bfd_boolean
6632 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
6633 const struct mips_cl_insn *insn)
6634 {
6635 /* The instruction must have a known offset. */
6636 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6637 return FALSE;
6638
6639 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6640 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6641 return TRUE;
6642 }
6643
6644 /* Return the number of nops that would be needed to work around the 24k
6645 "lost data on stores during refill" errata if instruction INSN
6646 immediately followed the 2 instructions described by HIST.
6647 Ignore hazards that are contained within the first IGNORE
6648 instructions of HIST.
6649
6650 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6651 for the data cache refills and store data. The following describes
6652 the scenario where the store data could be lost.
6653
6654 * A data cache miss, due to either a load or a store, causing fill
6655 data to be supplied by the memory subsystem
6656 * The first three doublewords of fill data are returned and written
6657 into the cache
6658 * A sequence of four stores occurs in consecutive cycles around the
6659 final doubleword of the fill:
6660 * Store A
6661 * Store B
6662 * Store C
6663 * Zero, One or more instructions
6664 * Store D
6665
6666 The four stores A-D must be to different doublewords of the line that
6667 is being filled. The fourth instruction in the sequence above permits
6668 the fill of the final doubleword to be transferred from the FSB into
6669 the cache. In the sequence above, the stores may be either integer
6670 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6671 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6672 different doublewords on the line. If the floating point unit is
6673 running in 1:2 mode, it is not possible to create the sequence above
6674 using only floating point store instructions.
6675
6676 In this case, the cache line being filled is incorrectly marked
6677 invalid, thereby losing the data from any store to the line that
6678 occurs between the original miss and the completion of the five
6679 cycle sequence shown above.
6680
6681 The workarounds are:
6682
6683 * Run the data cache in write-through mode.
6684 * Insert a non-store instruction between
6685 Store A and Store B or Store B and Store C. */
6686
6687 static int
6688 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
6689 const struct mips_cl_insn *insn)
6690 {
6691 struct fix_24k_store_info pos[3];
6692 int align, i, base_offset;
6693
6694 if (ignore >= 2)
6695 return 0;
6696
6697 /* If the previous instruction wasn't a store, there's nothing to
6698 worry about. */
6699 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6700 return 0;
6701
6702 /* If the instructions after the previous one are unknown, we have
6703 to assume the worst. */
6704 if (!insn)
6705 return 1;
6706
6707 /* Check whether we are dealing with three consecutive stores. */
6708 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6709 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6710 return 0;
6711
6712 /* If we don't know the relationship between the store addresses,
6713 assume the worst. */
6714 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
6715 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6716 return 1;
6717
6718 if (!fix_24k_record_store_info (&pos[0], insn)
6719 || !fix_24k_record_store_info (&pos[1], &hist[0])
6720 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6721 return 1;
6722
6723 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6724
6725 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6726 X bytes and such that the base register + X is known to be aligned
6727 to align bytes. */
6728
6729 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6730 align = 8;
6731 else
6732 {
6733 align = pos[0].align_to;
6734 base_offset = pos[0].off;
6735 for (i = 1; i < 3; i++)
6736 if (align < pos[i].align_to)
6737 {
6738 align = pos[i].align_to;
6739 base_offset = pos[i].off;
6740 }
6741 for (i = 0; i < 3; i++)
6742 pos[i].off -= base_offset;
6743 }
6744
6745 pos[0].off &= ~align + 1;
6746 pos[1].off &= ~align + 1;
6747 pos[2].off &= ~align + 1;
6748
6749 /* If any two stores write to the same chunk, they also write to the
6750 same doubleword. The offsets are still sorted at this point. */
6751 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6752 return 0;
6753
6754 /* A range of at least 9 bytes is needed for the stores to be in
6755 non-overlapping doublewords. */
6756 if (pos[2].off - pos[0].off <= 8)
6757 return 0;
6758
6759 if (pos[2].off - pos[1].off >= 24
6760 || pos[1].off - pos[0].off >= 24
6761 || pos[2].off - pos[0].off >= 32)
6762 return 0;
6763
6764 return 1;
6765 }
6766
6767 /* Return the number of nops that would be needed if instruction INSN
6768 immediately followed the MAX_NOPS instructions given by HIST,
6769 where HIST[0] is the most recent instruction. Ignore hazards
6770 between INSN and the first IGNORE instructions in HIST.
6771
6772 If INSN is null, return the worse-case number of nops for any
6773 instruction. */
6774
6775 static int
6776 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
6777 const struct mips_cl_insn *insn)
6778 {
6779 int i, nops, tmp_nops;
6780
6781 nops = 0;
6782 for (i = ignore; i < MAX_DELAY_NOPS; i++)
6783 {
6784 tmp_nops = insns_between (hist + i, insn) - i;
6785 if (tmp_nops > nops)
6786 nops = tmp_nops;
6787 }
6788
6789 if (mips_fix_vr4130 && !mips_opts.micromips)
6790 {
6791 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
6792 if (tmp_nops > nops)
6793 nops = tmp_nops;
6794 }
6795
6796 if (mips_fix_24k && !mips_opts.micromips)
6797 {
6798 tmp_nops = nops_for_24k (ignore, hist, insn);
6799 if (tmp_nops > nops)
6800 nops = tmp_nops;
6801 }
6802
6803 return nops;
6804 }
6805
6806 /* The variable arguments provide NUM_INSNS extra instructions that
6807 might be added to HIST. Return the largest number of nops that
6808 would be needed after the extended sequence, ignoring hazards
6809 in the first IGNORE instructions. */
6810
6811 static int
6812 nops_for_sequence (int num_insns, int ignore,
6813 const struct mips_cl_insn *hist, ...)
6814 {
6815 va_list args;
6816 struct mips_cl_insn buffer[MAX_NOPS];
6817 struct mips_cl_insn *cursor;
6818 int nops;
6819
6820 va_start (args, hist);
6821 cursor = buffer + num_insns;
6822 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
6823 while (cursor > buffer)
6824 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6825
6826 nops = nops_for_insn (ignore, buffer, NULL);
6827 va_end (args);
6828 return nops;
6829 }
6830
6831 /* Like nops_for_insn, but if INSN is a branch, take into account the
6832 worst-case delay for the branch target. */
6833
6834 static int
6835 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
6836 const struct mips_cl_insn *insn)
6837 {
6838 int nops, tmp_nops;
6839
6840 nops = nops_for_insn (ignore, hist, insn);
6841 if (delayed_branch_p (insn))
6842 {
6843 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
6844 hist, insn, get_delay_slot_nop (insn));
6845 if (tmp_nops > nops)
6846 nops = tmp_nops;
6847 }
6848 else if (compact_branch_p (insn))
6849 {
6850 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
6851 if (tmp_nops > nops)
6852 nops = tmp_nops;
6853 }
6854 return nops;
6855 }
6856
6857 /* Fix NOP issue: Replace nops by "or at,at,zero". */
6858
6859 static void
6860 fix_loongson2f_nop (struct mips_cl_insn * ip)
6861 {
6862 gas_assert (!HAVE_CODE_COMPRESSION);
6863 if (strcmp (ip->insn_mo->name, "nop") == 0)
6864 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6865 }
6866
6867 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6868 jr target pc &= 'hffff_ffff_cfff_ffff. */
6869
6870 static void
6871 fix_loongson2f_jump (struct mips_cl_insn * ip)
6872 {
6873 gas_assert (!HAVE_CODE_COMPRESSION);
6874 if (strcmp (ip->insn_mo->name, "j") == 0
6875 || strcmp (ip->insn_mo->name, "jr") == 0
6876 || strcmp (ip->insn_mo->name, "jalr") == 0)
6877 {
6878 int sreg;
6879 expressionS ep;
6880
6881 if (! mips_opts.at)
6882 return;
6883
6884 sreg = EXTRACT_OPERAND (0, RS, *ip);
6885 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6886 return;
6887
6888 ep.X_op = O_constant;
6889 ep.X_add_number = 0xcfff0000;
6890 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6891 ep.X_add_number = 0xffff;
6892 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6893 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6894 }
6895 }
6896
6897 static void
6898 fix_loongson2f (struct mips_cl_insn * ip)
6899 {
6900 if (mips_fix_loongson2f_nop)
6901 fix_loongson2f_nop (ip);
6902
6903 if (mips_fix_loongson2f_jump)
6904 fix_loongson2f_jump (ip);
6905 }
6906
6907 static bfd_boolean
6908 has_label_name (const char *arr[], size_t len ,const char *s)
6909 {
6910 unsigned long i;
6911 for (i = 0; i < len; i++)
6912 {
6913 if (!arr[i])
6914 return FALSE;
6915 if (streq (arr[i], s))
6916 return TRUE;
6917 }
6918 return FALSE;
6919 }
6920
6921 /* Fix loongson3 llsc errata: Insert sync before ll/lld. */
6922
6923 static void
6924 fix_loongson3_llsc (struct mips_cl_insn * ip)
6925 {
6926 gas_assert (!HAVE_CODE_COMPRESSION);
6927
6928 /* If is an local label and the insn is not sync,
6929 look forward that whether an branch between ll/sc jump to here
6930 if so, insert a sync. */
6931 if (seg_info (now_seg)->label_list
6932 && S_IS_LOCAL (seg_info (now_seg)->label_list->label)
6933 && (strcmp (ip->insn_mo->name, "sync") != 0))
6934 {
6935 unsigned long i;
6936 valueT label_value;
6937 const char *label_names[MAX_LABELS_SAME];
6938 const char *label_name;
6939
6940 label_name = S_GET_NAME (seg_info (now_seg)->label_list->label);
6941 label_names[0] = label_name;
6942 struct insn_label_list *llist = seg_info (now_seg)->label_list;
6943 label_value = S_GET_VALUE (llist->label);
6944
6945 for (i = 1; i < MAX_LABELS_SAME; i++)
6946 {
6947 llist = llist->next;
6948 if (!llist)
6949 break;
6950 if (S_GET_VALUE (llist->label) == label_value)
6951 label_names[i] = S_GET_NAME (llist->label);
6952 else
6953 break;
6954 }
6955 for (; i < MAX_LABELS_SAME; i++)
6956 label_names[i] = NULL;
6957
6958 unsigned long lookback = ARRAY_SIZE (history);
6959 for (i = 0; i < lookback; i++)
6960 {
6961 if (streq (history[i].insn_mo->name, "ll")
6962 || streq (history[i].insn_mo->name, "lld"))
6963 break;
6964
6965 if (streq (history[i].insn_mo->name, "sc")
6966 || streq (history[i].insn_mo->name, "scd"))
6967 {
6968 unsigned long j;
6969
6970 for (j = i + 1; j < lookback; j++)
6971 {
6972 if (streq (history[i].insn_mo->name, "ll")
6973 || streq (history[i].insn_mo->name, "lld"))
6974 break;
6975
6976 if (delayed_branch_p (&history[j]))
6977 {
6978 if (has_label_name (label_names,
6979 MAX_LABELS_SAME,
6980 history[j].target))
6981 {
6982 add_fixed_insn (&sync_insn);
6983 insert_into_history (0, 1, &sync_insn);
6984 i = lookback;
6985 break;
6986 }
6987 }
6988 }
6989 }
6990 }
6991 }
6992 /* If we find a sc, we look forward to look for an branch insn,
6993 and see whether it jump back and out of ll/sc. */
6994 else if (streq (ip->insn_mo->name, "sc") || streq (ip->insn_mo->name, "scd"))
6995 {
6996 unsigned long lookback = ARRAY_SIZE (history) - 1;
6997 unsigned long i;
6998
6999 for (i = 0; i < lookback; i++)
7000 {
7001 if (streq (history[i].insn_mo->name, "ll")
7002 || streq (history[i].insn_mo->name, "lld"))
7003 break;
7004
7005 if (delayed_branch_p (&history[i]))
7006 {
7007 unsigned long j;
7008
7009 for (j = i + 1; j < lookback; j++)
7010 {
7011 if (streq (history[j].insn_mo->name, "ll")
7012 || streq (history[i].insn_mo->name, "lld"))
7013 break;
7014 }
7015
7016 for (; j < lookback; j++)
7017 {
7018 if (history[j].label[0] != '\0'
7019 && streq (history[j].label, history[i].target)
7020 && strcmp (history[j+1].insn_mo->name, "sync") != 0)
7021 {
7022 add_fixed_insn (&sync_insn);
7023 insert_into_history (++j, 1, &sync_insn);
7024 }
7025 }
7026 }
7027 }
7028 }
7029
7030 /* Skip if there is a sync before ll/lld. */
7031 if ((strcmp (ip->insn_mo->name, "ll") == 0
7032 || strcmp (ip->insn_mo->name, "lld") == 0)
7033 && (strcmp (history[0].insn_mo->name, "sync") != 0))
7034 {
7035 add_fixed_insn (&sync_insn);
7036 insert_into_history (0, 1, &sync_insn);
7037 }
7038 }
7039
7040 /* IP is a branch that has a delay slot, and we need to fill it
7041 automatically. Return true if we can do that by swapping IP
7042 with the previous instruction.
7043 ADDRESS_EXPR is an operand of the instruction to be used with
7044 RELOC_TYPE. */
7045
7046 static bfd_boolean
7047 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
7048 bfd_reloc_code_real_type *reloc_type)
7049 {
7050 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
7051 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
7052 unsigned int fpr_read, prev_fpr_write;
7053
7054 /* -O2 and above is required for this optimization. */
7055 if (mips_optimize < 2)
7056 return FALSE;
7057
7058 /* If we have seen .set volatile or .set nomove, don't optimize. */
7059 if (mips_opts.nomove)
7060 return FALSE;
7061
7062 /* We can't swap if the previous instruction's position is fixed. */
7063 if (history[0].fixed_p)
7064 return FALSE;
7065
7066 /* If the previous previous insn was in a .set noreorder, we can't
7067 swap. Actually, the MIPS assembler will swap in this situation.
7068 However, gcc configured -with-gnu-as will generate code like
7069
7070 .set noreorder
7071 lw $4,XXX
7072 .set reorder
7073 INSN
7074 bne $4,$0,foo
7075
7076 in which we can not swap the bne and INSN. If gcc is not configured
7077 -with-gnu-as, it does not output the .set pseudo-ops. */
7078 if (history[1].noreorder_p)
7079 return FALSE;
7080
7081 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
7082 This means that the previous instruction was a 4-byte one anyhow. */
7083 if (mips_opts.mips16 && history[0].fixp[0])
7084 return FALSE;
7085
7086 /* If the branch is itself the target of a branch, we can not swap.
7087 We cheat on this; all we check for is whether there is a label on
7088 this instruction. If there are any branches to anything other than
7089 a label, users must use .set noreorder. */
7090 if (seg_info (now_seg)->label_list)
7091 return FALSE;
7092
7093 /* If the previous instruction is in a variant frag other than this
7094 branch's one, we cannot do the swap. This does not apply to
7095 MIPS16 code, which uses variant frags for different purposes. */
7096 if (!mips_opts.mips16
7097 && history[0].frag
7098 && history[0].frag->fr_type == rs_machine_dependent)
7099 return FALSE;
7100
7101 /* We do not swap with instructions that cannot architecturally
7102 be placed in a branch delay slot, such as SYNC or ERET. We
7103 also refrain from swapping with a trap instruction, since it
7104 complicates trap handlers to have the trap instruction be in
7105 a delay slot. */
7106 prev_pinfo = history[0].insn_mo->pinfo;
7107 if (prev_pinfo & INSN_NO_DELAY_SLOT)
7108 return FALSE;
7109
7110 /* Check for conflicts between the branch and the instructions
7111 before the candidate delay slot. */
7112 if (nops_for_insn (0, history + 1, ip) > 0)
7113 return FALSE;
7114
7115 /* Check for conflicts between the swapped sequence and the
7116 target of the branch. */
7117 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
7118 return FALSE;
7119
7120 /* If the branch reads a register that the previous
7121 instruction sets, we can not swap. */
7122 gpr_read = gpr_read_mask (ip);
7123 prev_gpr_write = gpr_write_mask (&history[0]);
7124 if (gpr_read & prev_gpr_write)
7125 return FALSE;
7126
7127 fpr_read = fpr_read_mask (ip);
7128 prev_fpr_write = fpr_write_mask (&history[0]);
7129 if (fpr_read & prev_fpr_write)
7130 return FALSE;
7131
7132 /* If the branch writes a register that the previous
7133 instruction sets, we can not swap. */
7134 gpr_write = gpr_write_mask (ip);
7135 if (gpr_write & prev_gpr_write)
7136 return FALSE;
7137
7138 /* If the branch writes a register that the previous
7139 instruction reads, we can not swap. */
7140 prev_gpr_read = gpr_read_mask (&history[0]);
7141 if (gpr_write & prev_gpr_read)
7142 return FALSE;
7143
7144 /* If one instruction sets a condition code and the
7145 other one uses a condition code, we can not swap. */
7146 pinfo = ip->insn_mo->pinfo;
7147 if ((pinfo & INSN_READ_COND_CODE)
7148 && (prev_pinfo & INSN_WRITE_COND_CODE))
7149 return FALSE;
7150 if ((pinfo & INSN_WRITE_COND_CODE)
7151 && (prev_pinfo & INSN_READ_COND_CODE))
7152 return FALSE;
7153
7154 /* If the previous instruction uses the PC, we can not swap. */
7155 prev_pinfo2 = history[0].insn_mo->pinfo2;
7156 if (prev_pinfo2 & INSN2_READ_PC)
7157 return FALSE;
7158
7159 /* If the previous instruction has an incorrect size for a fixed
7160 branch delay slot in microMIPS mode, we cannot swap. */
7161 pinfo2 = ip->insn_mo->pinfo2;
7162 if (mips_opts.micromips
7163 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
7164 && insn_length (history) != 2)
7165 return FALSE;
7166 if (mips_opts.micromips
7167 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
7168 && insn_length (history) != 4)
7169 return FALSE;
7170
7171 /* On the R5900 short loops need to be fixed by inserting a NOP in the
7172 branch delay slot.
7173
7174 The short loop bug under certain conditions causes loops to execute
7175 only once or twice. We must ensure that the assembler never
7176 generates loops that satisfy all of the following conditions:
7177
7178 - a loop consists of less than or equal to six instructions
7179 (including the branch delay slot);
7180 - a loop contains only one conditional branch instruction at the end
7181 of the loop;
7182 - a loop does not contain any other branch or jump instructions;
7183 - a branch delay slot of the loop is not NOP (EE 2.9 or later).
7184
7185 We need to do this because of a hardware bug in the R5900 chip. */
7186 if (mips_fix_r5900
7187 /* Check if instruction has a parameter, ignore "j $31". */
7188 && (address_expr != NULL)
7189 /* Parameter must be 16 bit. */
7190 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
7191 /* Branch to same segment. */
7192 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
7193 /* Branch to same code fragment. */
7194 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
7195 /* Can only calculate branch offset if value is known. */
7196 && symbol_constant_p (address_expr->X_add_symbol)
7197 /* Check if branch is really conditional. */
7198 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
7199 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
7200 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7201 {
7202 int distance;
7203 /* Check if loop is shorter than or equal to 6 instructions
7204 including branch and delay slot. */
7205 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
7206 if (distance <= 20)
7207 {
7208 int i;
7209 int rv;
7210
7211 rv = FALSE;
7212 /* When the loop includes branches or jumps,
7213 it is not a short loop. */
7214 for (i = 0; i < (distance / 4); i++)
7215 {
7216 if ((history[i].cleared_p)
7217 || delayed_branch_p (&history[i]))
7218 {
7219 rv = TRUE;
7220 break;
7221 }
7222 }
7223 if (!rv)
7224 {
7225 /* Insert nop after branch to fix short loop. */
7226 return FALSE;
7227 }
7228 }
7229 }
7230
7231 return TRUE;
7232 }
7233
7234 /* Decide how we should add IP to the instruction stream.
7235 ADDRESS_EXPR is an operand of the instruction to be used with
7236 RELOC_TYPE. */
7237
7238 static enum append_method
7239 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
7240 bfd_reloc_code_real_type *reloc_type)
7241 {
7242 /* The relaxed version of a macro sequence must be inherently
7243 hazard-free. */
7244 if (mips_relax.sequence == 2)
7245 return APPEND_ADD;
7246
7247 /* We must not dabble with instructions in a ".set noreorder" block. */
7248 if (mips_opts.noreorder)
7249 return APPEND_ADD;
7250
7251 /* Otherwise, it's our responsibility to fill branch delay slots. */
7252 if (delayed_branch_p (ip))
7253 {
7254 if (!branch_likely_p (ip)
7255 && can_swap_branch_p (ip, address_expr, reloc_type))
7256 return APPEND_SWAP;
7257
7258 if (mips_opts.mips16
7259 && ISA_SUPPORTS_MIPS16E
7260 && gpr_read_mask (ip) != 0)
7261 return APPEND_ADD_COMPACT;
7262
7263 if (mips_opts.micromips
7264 && ((ip->insn_opcode & 0xffe0) == 0x4580
7265 || (!forced_insn_length
7266 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7267 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7268 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7269 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7270 return APPEND_ADD_COMPACT;
7271
7272 return APPEND_ADD_WITH_NOP;
7273 }
7274
7275 return APPEND_ADD;
7276 }
7277
7278 /* IP is an instruction whose opcode we have just changed, END points
7279 to the end of the opcode table processed. Point IP->insn_mo to the
7280 new opcode's definition. */
7281
7282 static void
7283 find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
7284 {
7285 const struct mips_opcode *mo;
7286
7287 for (mo = ip->insn_mo; mo < end; mo++)
7288 if (mo->pinfo != INSN_MACRO
7289 && (ip->insn_opcode & mo->mask) == mo->match)
7290 {
7291 ip->insn_mo = mo;
7292 return;
7293 }
7294 abort ();
7295 }
7296
7297 /* IP is a MIPS16 instruction whose opcode we have just changed.
7298 Point IP->insn_mo to the new opcode's definition. */
7299
7300 static void
7301 find_altered_mips16_opcode (struct mips_cl_insn *ip)
7302 {
7303 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7304 }
7305
7306 /* IP is a microMIPS instruction whose opcode we have just changed.
7307 Point IP->insn_mo to the new opcode's definition. */
7308
7309 static void
7310 find_altered_micromips_opcode (struct mips_cl_insn *ip)
7311 {
7312 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7313 }
7314
7315 /* For microMIPS macros, we need to generate a local number label
7316 as the target of branches. */
7317 #define MICROMIPS_LABEL_CHAR '\037'
7318 static unsigned long micromips_target_label;
7319 static char micromips_target_name[32];
7320
7321 static char *
7322 micromips_label_name (void)
7323 {
7324 char *p = micromips_target_name;
7325 char symbol_name_temporary[24];
7326 unsigned long l;
7327 int i;
7328
7329 if (*p)
7330 return p;
7331
7332 i = 0;
7333 l = micromips_target_label;
7334 #ifdef LOCAL_LABEL_PREFIX
7335 *p++ = LOCAL_LABEL_PREFIX;
7336 #endif
7337 *p++ = 'L';
7338 *p++ = MICROMIPS_LABEL_CHAR;
7339 do
7340 {
7341 symbol_name_temporary[i++] = l % 10 + '0';
7342 l /= 10;
7343 }
7344 while (l != 0);
7345 while (i > 0)
7346 *p++ = symbol_name_temporary[--i];
7347 *p = '\0';
7348
7349 return micromips_target_name;
7350 }
7351
7352 static void
7353 micromips_label_expr (expressionS *label_expr)
7354 {
7355 label_expr->X_op = O_symbol;
7356 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7357 label_expr->X_add_number = 0;
7358 }
7359
7360 static void
7361 micromips_label_inc (void)
7362 {
7363 micromips_target_label++;
7364 *micromips_target_name = '\0';
7365 }
7366
7367 static void
7368 micromips_add_label (void)
7369 {
7370 symbolS *s;
7371
7372 s = colon (micromips_label_name ());
7373 micromips_label_inc ();
7374 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
7375 }
7376
7377 /* If assembling microMIPS code, then return the microMIPS reloc
7378 corresponding to the requested one if any. Otherwise return
7379 the reloc unchanged. */
7380
7381 static bfd_reloc_code_real_type
7382 micromips_map_reloc (bfd_reloc_code_real_type reloc)
7383 {
7384 static const bfd_reloc_code_real_type relocs[][2] =
7385 {
7386 /* Keep sorted incrementally by the left-hand key. */
7387 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7388 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7389 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7390 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7391 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7392 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7393 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7394 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7395 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7396 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7397 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7398 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7399 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7400 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7401 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7402 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7403 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7404 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7405 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7406 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7407 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7408 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7409 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7410 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7411 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7412 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7413 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7414 };
7415 bfd_reloc_code_real_type r;
7416 size_t i;
7417
7418 if (!mips_opts.micromips)
7419 return reloc;
7420 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7421 {
7422 r = relocs[i][0];
7423 if (r > reloc)
7424 return reloc;
7425 if (r == reloc)
7426 return relocs[i][1];
7427 }
7428 return reloc;
7429 }
7430
7431 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7432 Return true on success, storing the resolved value in RESULT. */
7433
7434 static bfd_boolean
7435 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7436 offsetT *result)
7437 {
7438 switch (reloc)
7439 {
7440 case BFD_RELOC_MIPS_HIGHEST:
7441 case BFD_RELOC_MICROMIPS_HIGHEST:
7442 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7443 return TRUE;
7444
7445 case BFD_RELOC_MIPS_HIGHER:
7446 case BFD_RELOC_MICROMIPS_HIGHER:
7447 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7448 return TRUE;
7449
7450 case BFD_RELOC_HI16_S:
7451 case BFD_RELOC_HI16_S_PCREL:
7452 case BFD_RELOC_MICROMIPS_HI16_S:
7453 case BFD_RELOC_MIPS16_HI16_S:
7454 *result = ((operand + 0x8000) >> 16) & 0xffff;
7455 return TRUE;
7456
7457 case BFD_RELOC_HI16:
7458 case BFD_RELOC_MICROMIPS_HI16:
7459 case BFD_RELOC_MIPS16_HI16:
7460 *result = (operand >> 16) & 0xffff;
7461 return TRUE;
7462
7463 case BFD_RELOC_LO16:
7464 case BFD_RELOC_LO16_PCREL:
7465 case BFD_RELOC_MICROMIPS_LO16:
7466 case BFD_RELOC_MIPS16_LO16:
7467 *result = operand & 0xffff;
7468 return TRUE;
7469
7470 case BFD_RELOC_UNUSED:
7471 *result = operand;
7472 return TRUE;
7473
7474 default:
7475 return FALSE;
7476 }
7477 }
7478
7479 /* Output an instruction. IP is the instruction information.
7480 ADDRESS_EXPR is an operand of the instruction to be used with
7481 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7482 a macro expansion. */
7483
7484 static void
7485 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
7486 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
7487 {
7488 unsigned long prev_pinfo2, pinfo;
7489 bfd_boolean relaxed_branch = FALSE;
7490 enum append_method method;
7491 bfd_boolean relax32;
7492 int branch_disp;
7493
7494 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
7495 fix_loongson2f (ip);
7496
7497 ip->target[0] = '\0';
7498 if (offset_expr.X_op == O_symbol)
7499 strncpy (ip->target, S_GET_NAME (offset_expr.X_add_symbol), 15);
7500 ip->label[0] = '\0';
7501 if (seg_info (now_seg)->label_list)
7502 strncpy (ip->label, S_GET_NAME (seg_info (now_seg)->label_list->label), 15);
7503 if (mips_fix_loongson3_llsc && !HAVE_CODE_COMPRESSION)
7504 fix_loongson3_llsc (ip);
7505
7506 file_ase_mips16 |= mips_opts.mips16;
7507 file_ase_micromips |= mips_opts.micromips;
7508
7509 prev_pinfo2 = history[0].insn_mo->pinfo2;
7510 pinfo = ip->insn_mo->pinfo;
7511
7512 /* Don't raise alarm about `nods' frags as they'll fill in the right
7513 kind of nop in relaxation if required. */
7514 if (mips_opts.micromips
7515 && !expansionp
7516 && !(history[0].frag
7517 && history[0].frag->fr_type == rs_machine_dependent
7518 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7519 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
7520 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7521 && micromips_insn_length (ip->insn_mo) != 2)
7522 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7523 && micromips_insn_length (ip->insn_mo) != 4)))
7524 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
7525 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
7526
7527 if (address_expr == NULL)
7528 ip->complete_p = 1;
7529 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7530 && reloc_type[1] == BFD_RELOC_UNUSED
7531 && reloc_type[2] == BFD_RELOC_UNUSED
7532 && address_expr->X_op == O_constant)
7533 {
7534 switch (*reloc_type)
7535 {
7536 case BFD_RELOC_MIPS_JMP:
7537 {
7538 int shift;
7539
7540 /* Shift is 2, unusually, for microMIPS JALX. */
7541 shift = (mips_opts.micromips
7542 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
7543 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7544 as_bad (_("jump to misaligned address (0x%lx)"),
7545 (unsigned long) address_expr->X_add_number);
7546 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7547 & 0x3ffffff);
7548 ip->complete_p = 1;
7549 }
7550 break;
7551
7552 case BFD_RELOC_MIPS16_JMP:
7553 if ((address_expr->X_add_number & 3) != 0)
7554 as_bad (_("jump to misaligned address (0x%lx)"),
7555 (unsigned long) address_expr->X_add_number);
7556 ip->insn_opcode |=
7557 (((address_expr->X_add_number & 0x7c0000) << 3)
7558 | ((address_expr->X_add_number & 0xf800000) >> 7)
7559 | ((address_expr->X_add_number & 0x3fffc) >> 2));
7560 ip->complete_p = 1;
7561 break;
7562
7563 case BFD_RELOC_16_PCREL_S2:
7564 {
7565 int shift;
7566
7567 shift = mips_opts.micromips ? 1 : 2;
7568 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7569 as_bad (_("branch to misaligned address (0x%lx)"),
7570 (unsigned long) address_expr->X_add_number);
7571 if (!mips_relax_branch)
7572 {
7573 if ((address_expr->X_add_number + (1 << (shift + 15)))
7574 & ~((1 << (shift + 16)) - 1))
7575 as_bad (_("branch address range overflow (0x%lx)"),
7576 (unsigned long) address_expr->X_add_number);
7577 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7578 & 0xffff);
7579 }
7580 }
7581 break;
7582
7583 case BFD_RELOC_MIPS_21_PCREL_S2:
7584 {
7585 int shift;
7586
7587 shift = 2;
7588 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7589 as_bad (_("branch to misaligned address (0x%lx)"),
7590 (unsigned long) address_expr->X_add_number);
7591 if ((address_expr->X_add_number + (1 << (shift + 20)))
7592 & ~((1 << (shift + 21)) - 1))
7593 as_bad (_("branch address range overflow (0x%lx)"),
7594 (unsigned long) address_expr->X_add_number);
7595 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7596 & 0x1fffff);
7597 }
7598 break;
7599
7600 case BFD_RELOC_MIPS_26_PCREL_S2:
7601 {
7602 int shift;
7603
7604 shift = 2;
7605 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7606 as_bad (_("branch to misaligned address (0x%lx)"),
7607 (unsigned long) address_expr->X_add_number);
7608 if ((address_expr->X_add_number + (1 << (shift + 25)))
7609 & ~((1 << (shift + 26)) - 1))
7610 as_bad (_("branch address range overflow (0x%lx)"),
7611 (unsigned long) address_expr->X_add_number);
7612 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7613 & 0x3ffffff);
7614 }
7615 break;
7616
7617 default:
7618 {
7619 offsetT value;
7620
7621 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7622 &value))
7623 {
7624 ip->insn_opcode |= value & 0xffff;
7625 ip->complete_p = 1;
7626 }
7627 }
7628 break;
7629 }
7630 }
7631
7632 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7633 {
7634 /* There are a lot of optimizations we could do that we don't.
7635 In particular, we do not, in general, reorder instructions.
7636 If you use gcc with optimization, it will reorder
7637 instructions and generally do much more optimization then we
7638 do here; repeating all that work in the assembler would only
7639 benefit hand written assembly code, and does not seem worth
7640 it. */
7641 int nops = (mips_optimize == 0
7642 ? nops_for_insn (0, history, NULL)
7643 : nops_for_insn_or_target (0, history, ip));
7644 if (nops > 0)
7645 {
7646 fragS *old_frag;
7647 unsigned long old_frag_offset;
7648 int i;
7649
7650 old_frag = frag_now;
7651 old_frag_offset = frag_now_fix ();
7652
7653 for (i = 0; i < nops; i++)
7654 add_fixed_insn (NOP_INSN);
7655 insert_into_history (0, nops, NOP_INSN);
7656
7657 if (listing)
7658 {
7659 listing_prev_line ();
7660 /* We may be at the start of a variant frag. In case we
7661 are, make sure there is enough space for the frag
7662 after the frags created by listing_prev_line. The
7663 argument to frag_grow here must be at least as large
7664 as the argument to all other calls to frag_grow in
7665 this file. We don't have to worry about being in the
7666 middle of a variant frag, because the variants insert
7667 all needed nop instructions themselves. */
7668 frag_grow (40);
7669 }
7670
7671 mips_move_text_labels ();
7672
7673 #ifndef NO_ECOFF_DEBUGGING
7674 if (ECOFF_DEBUGGING)
7675 ecoff_fix_loc (old_frag, old_frag_offset);
7676 #endif
7677 }
7678 }
7679 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7680 {
7681 int nops;
7682
7683 /* Work out how many nops in prev_nop_frag are needed by IP,
7684 ignoring hazards generated by the first prev_nop_frag_since
7685 instructions. */
7686 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
7687 gas_assert (nops <= prev_nop_frag_holds);
7688
7689 /* Enforce NOPS as a minimum. */
7690 if (nops > prev_nop_frag_required)
7691 prev_nop_frag_required = nops;
7692
7693 if (prev_nop_frag_holds == prev_nop_frag_required)
7694 {
7695 /* Settle for the current number of nops. Update the history
7696 accordingly (for the benefit of any future .set reorder code). */
7697 prev_nop_frag = NULL;
7698 insert_into_history (prev_nop_frag_since,
7699 prev_nop_frag_holds, NOP_INSN);
7700 }
7701 else
7702 {
7703 /* Allow this instruction to replace one of the nops that was
7704 tentatively added to prev_nop_frag. */
7705 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
7706 prev_nop_frag_holds--;
7707 prev_nop_frag_since++;
7708 }
7709 }
7710
7711 method = get_append_method (ip, address_expr, reloc_type);
7712 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
7713
7714 dwarf2_emit_insn (0);
7715 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7716 so "move" the instruction address accordingly.
7717
7718 Also, it doesn't seem appropriate for the assembler to reorder .loc
7719 entries. If this instruction is a branch that we are going to swap
7720 with the previous instruction, the two instructions should be
7721 treated as a unit, and the debug information for both instructions
7722 should refer to the start of the branch sequence. Using the
7723 current position is certainly wrong when swapping a 32-bit branch
7724 and a 16-bit delay slot, since the current position would then be
7725 in the middle of a branch. */
7726 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
7727
7728 relax32 = (mips_relax_branch
7729 /* Don't try branch relaxation within .set nomacro, or within
7730 .set noat if we use $at for PIC computations. If it turns
7731 out that the branch was out-of-range, we'll get an error. */
7732 && !mips_opts.warn_about_macros
7733 && (mips_opts.at || mips_pic == NO_PIC)
7734 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7735 as they have no complementing branches. */
7736 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
7737
7738 if (!HAVE_CODE_COMPRESSION
7739 && address_expr
7740 && relax32
7741 && *reloc_type == BFD_RELOC_16_PCREL_S2
7742 && delayed_branch_p (ip))
7743 {
7744 relaxed_branch = TRUE;
7745 add_relaxed_insn (ip, (relaxed_branch_length
7746 (NULL, NULL,
7747 uncond_branch_p (ip) ? -1
7748 : branch_likely_p (ip) ? 1
7749 : 0)), 4,
7750 RELAX_BRANCH_ENCODE
7751 (AT, mips_pic != NO_PIC,
7752 uncond_branch_p (ip),
7753 branch_likely_p (ip),
7754 pinfo & INSN_WRITE_GPR_31,
7755 0),
7756 address_expr->X_add_symbol,
7757 address_expr->X_add_number);
7758 *reloc_type = BFD_RELOC_UNUSED;
7759 }
7760 else if (mips_opts.micromips
7761 && address_expr
7762 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7763 || *reloc_type > BFD_RELOC_UNUSED)
7764 && (delayed_branch_p (ip) || compact_branch_p (ip))
7765 /* Don't try branch relaxation when users specify
7766 16-bit/32-bit instructions. */
7767 && !forced_insn_length)
7768 {
7769 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7770 && *reloc_type > BFD_RELOC_UNUSED);
7771 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
7772 int uncond = uncond_branch_p (ip) ? -1 : 0;
7773 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7774 int nods = method == APPEND_ADD_WITH_NOP;
7775 int al = pinfo & INSN_WRITE_GPR_31;
7776 int length32 = nods ? 8 : 4;
7777
7778 gas_assert (address_expr != NULL);
7779 gas_assert (!mips_relax.sequence);
7780
7781 relaxed_branch = TRUE;
7782 if (nods)
7783 method = APPEND_ADD;
7784 if (relax32)
7785 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7786 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
7787 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
7788 mips_pic != NO_PIC,
7789 uncond, compact, al, nods,
7790 relax32, 0, 0),
7791 address_expr->X_add_symbol,
7792 address_expr->X_add_number);
7793 *reloc_type = BFD_RELOC_UNUSED;
7794 }
7795 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
7796 {
7797 bfd_boolean require_unextended;
7798 bfd_boolean require_extended;
7799 symbolS *symbol;
7800 offsetT offset;
7801
7802 if (forced_insn_length != 0)
7803 {
7804 require_unextended = forced_insn_length == 2;
7805 require_extended = forced_insn_length == 4;
7806 }
7807 else
7808 {
7809 require_unextended = (mips_opts.noautoextend
7810 && !mips_opcode_32bit_p (ip->insn_mo));
7811 require_extended = 0;
7812 }
7813
7814 /* We need to set up a variant frag. */
7815 gas_assert (address_expr != NULL);
7816 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7817 symbol created by `make_expr_symbol' may not get a necessary
7818 external relocation produced. */
7819 if (address_expr->X_op == O_symbol)
7820 {
7821 symbol = address_expr->X_add_symbol;
7822 offset = address_expr->X_add_number;
7823 }
7824 else
7825 {
7826 symbol = make_expr_symbol (address_expr);
7827 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
7828 offset = 0;
7829 }
7830 add_relaxed_insn (ip, 12, 0,
7831 RELAX_MIPS16_ENCODE
7832 (*reloc_type - BFD_RELOC_UNUSED,
7833 mips_opts.ase & ASE_MIPS16E2,
7834 mips_pic != NO_PIC,
7835 HAVE_32BIT_SYMBOLS,
7836 mips_opts.warn_about_macros,
7837 require_unextended, require_extended,
7838 delayed_branch_p (&history[0]),
7839 history[0].mips16_absolute_jump_p),
7840 symbol, offset);
7841 }
7842 else if (mips_opts.mips16 && insn_length (ip) == 2)
7843 {
7844 if (!delayed_branch_p (ip))
7845 /* Make sure there is enough room to swap this instruction with
7846 a following jump instruction. */
7847 frag_grow (6);
7848 add_fixed_insn (ip);
7849 }
7850 else
7851 {
7852 if (mips_opts.mips16
7853 && mips_opts.noreorder
7854 && delayed_branch_p (&history[0]))
7855 as_warn (_("extended instruction in delay slot"));
7856
7857 if (mips_relax.sequence)
7858 {
7859 /* If we've reached the end of this frag, turn it into a variant
7860 frag and record the information for the instructions we've
7861 written so far. */
7862 if (frag_room () < 4)
7863 relax_close_frag ();
7864 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
7865 }
7866
7867 if (mips_relax.sequence != 2)
7868 {
7869 if (mips_macro_warning.first_insn_sizes[0] == 0)
7870 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7871 mips_macro_warning.sizes[0] += insn_length (ip);
7872 mips_macro_warning.insns[0]++;
7873 }
7874 if (mips_relax.sequence != 1)
7875 {
7876 if (mips_macro_warning.first_insn_sizes[1] == 0)
7877 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7878 mips_macro_warning.sizes[1] += insn_length (ip);
7879 mips_macro_warning.insns[1]++;
7880 }
7881
7882 if (mips_opts.mips16)
7883 {
7884 ip->fixed_p = 1;
7885 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7886 }
7887 add_fixed_insn (ip);
7888 }
7889
7890 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
7891 {
7892 bfd_reloc_code_real_type final_type[3];
7893 reloc_howto_type *howto0;
7894 reloc_howto_type *howto;
7895 int i;
7896
7897 /* Perform any necessary conversion to microMIPS relocations
7898 and find out how many relocations there actually are. */
7899 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7900 final_type[i] = micromips_map_reloc (reloc_type[i]);
7901
7902 /* In a compound relocation, it is the final (outermost)
7903 operator that determines the relocated field. */
7904 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
7905 if (!howto)
7906 abort ();
7907
7908 if (i > 1)
7909 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
7910 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7911 bfd_get_reloc_size (howto),
7912 address_expr,
7913 howto0 && howto0->pc_relative,
7914 final_type[0]);
7915 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7916 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
7917
7918 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
7919 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
7920 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7921
7922 /* These relocations can have an addend that won't fit in
7923 4 octets for 64bit assembly. */
7924 if (GPR_SIZE == 64
7925 && ! howto->partial_inplace
7926 && (reloc_type[0] == BFD_RELOC_16
7927 || reloc_type[0] == BFD_RELOC_32
7928 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7929 || reloc_type[0] == BFD_RELOC_GPREL16
7930 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7931 || reloc_type[0] == BFD_RELOC_GPREL32
7932 || reloc_type[0] == BFD_RELOC_64
7933 || reloc_type[0] == BFD_RELOC_CTOR
7934 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7935 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7936 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7937 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7938 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7939 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7940 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7941 || hi16_reloc_p (reloc_type[0])
7942 || lo16_reloc_p (reloc_type[0])))
7943 ip->fixp[0]->fx_no_overflow = 1;
7944
7945 /* These relocations can have an addend that won't fit in 2 octets. */
7946 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7947 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7948 ip->fixp[0]->fx_no_overflow = 1;
7949
7950 if (mips_relax.sequence)
7951 {
7952 if (mips_relax.first_fixup == 0)
7953 mips_relax.first_fixup = ip->fixp[0];
7954 }
7955 else if (reloc_needs_lo_p (*reloc_type))
7956 {
7957 struct mips_hi_fixup *hi_fixup;
7958
7959 /* Reuse the last entry if it already has a matching %lo. */
7960 hi_fixup = mips_hi_fixup_list;
7961 if (hi_fixup == 0
7962 || !fixup_has_matching_lo_p (hi_fixup->fixp))
7963 {
7964 hi_fixup = XNEW (struct mips_hi_fixup);
7965 hi_fixup->next = mips_hi_fixup_list;
7966 mips_hi_fixup_list = hi_fixup;
7967 }
7968 hi_fixup->fixp = ip->fixp[0];
7969 hi_fixup->seg = now_seg;
7970 }
7971
7972 /* Add fixups for the second and third relocations, if given.
7973 Note that the ABI allows the second relocation to be
7974 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7975 moment we only use RSS_UNDEF, but we could add support
7976 for the others if it ever becomes necessary. */
7977 for (i = 1; i < 3; i++)
7978 if (reloc_type[i] != BFD_RELOC_UNUSED)
7979 {
7980 ip->fixp[i] = fix_new (ip->frag, ip->where,
7981 ip->fixp[0]->fx_size, NULL, 0,
7982 FALSE, final_type[i]);
7983
7984 /* Use fx_tcbit to mark compound relocs. */
7985 ip->fixp[0]->fx_tcbit = 1;
7986 ip->fixp[i]->fx_tcbit = 1;
7987 }
7988 }
7989
7990 /* Update the register mask information. */
7991 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7992 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
7993
7994 switch (method)
7995 {
7996 case APPEND_ADD:
7997 insert_into_history (0, 1, ip);
7998 break;
7999
8000 case APPEND_ADD_WITH_NOP:
8001 {
8002 struct mips_cl_insn *nop;
8003
8004 insert_into_history (0, 1, ip);
8005 nop = get_delay_slot_nop (ip);
8006 add_fixed_insn (nop);
8007 insert_into_history (0, 1, nop);
8008 if (mips_relax.sequence)
8009 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
8010 }
8011 break;
8012
8013 case APPEND_ADD_COMPACT:
8014 /* Convert MIPS16 jr/jalr into a "compact" jump. */
8015 if (mips_opts.mips16)
8016 {
8017 ip->insn_opcode |= 0x0080;
8018 find_altered_mips16_opcode (ip);
8019 }
8020 /* Convert microMIPS instructions. */
8021 else if (mips_opts.micromips)
8022 {
8023 /* jr16->jrc */
8024 if ((ip->insn_opcode & 0xffe0) == 0x4580)
8025 ip->insn_opcode |= 0x0020;
8026 /* b16->bc */
8027 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
8028 ip->insn_opcode = 0x40e00000;
8029 /* beqz16->beqzc, bnez16->bnezc */
8030 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
8031 {
8032 unsigned long regno;
8033
8034 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
8035 regno &= MICROMIPSOP_MASK_MD;
8036 regno = micromips_to_32_reg_d_map[regno];
8037 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
8038 | (regno << MICROMIPSOP_SH_RS)
8039 | 0x40a00000) ^ 0x00400000;
8040 }
8041 /* beqz->beqzc, bnez->bnezc */
8042 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
8043 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
8044 | ((ip->insn_opcode >> 7) & 0x00400000)
8045 | 0x40a00000) ^ 0x00400000;
8046 /* beq $0->beqzc, bne $0->bnezc */
8047 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
8048 ip->insn_opcode = (((ip->insn_opcode >>
8049 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
8050 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
8051 | ((ip->insn_opcode >> 7) & 0x00400000)
8052 | 0x40a00000) ^ 0x00400000;
8053 else
8054 abort ();
8055 find_altered_micromips_opcode (ip);
8056 }
8057 else
8058 abort ();
8059 install_insn (ip);
8060 insert_into_history (0, 1, ip);
8061 break;
8062
8063 case APPEND_SWAP:
8064 {
8065 struct mips_cl_insn delay = history[0];
8066
8067 if (relaxed_branch || delay.frag != ip->frag)
8068 {
8069 /* Add the delay slot instruction to the end of the
8070 current frag and shrink the fixed part of the
8071 original frag. If the branch occupies the tail of
8072 the latter, move it backwards to cover the gap. */
8073 delay.frag->fr_fix -= branch_disp;
8074 if (delay.frag == ip->frag)
8075 move_insn (ip, ip->frag, ip->where - branch_disp);
8076 add_fixed_insn (&delay);
8077 }
8078 else
8079 {
8080 /* If this is not a relaxed branch and we are in the
8081 same frag, then just swap the instructions. */
8082 move_insn (ip, delay.frag, delay.where);
8083 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
8084 }
8085 history[0] = *ip;
8086 delay.fixed_p = 1;
8087 insert_into_history (0, 1, &delay);
8088 }
8089 break;
8090 }
8091
8092 /* If we have just completed an unconditional branch, clear the history. */
8093 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
8094 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
8095 {
8096 unsigned int i;
8097
8098 mips_no_prev_insn ();
8099
8100 for (i = 0; i < ARRAY_SIZE (history); i++)
8101 history[i].cleared_p = 1;
8102 }
8103
8104 /* We need to emit a label at the end of branch-likely macros. */
8105 if (emit_branch_likely_macro)
8106 {
8107 emit_branch_likely_macro = FALSE;
8108 micromips_add_label ();
8109 }
8110
8111 /* We just output an insn, so the next one doesn't have a label. */
8112 mips_clear_insn_labels ();
8113 }
8114
8115 /* Forget that there was any previous instruction or label.
8116 When BRANCH is true, the branch history is also flushed. */
8117
8118 static void
8119 mips_no_prev_insn (void)
8120 {
8121 prev_nop_frag = NULL;
8122 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
8123 mips_clear_insn_labels ();
8124 }
8125
8126 /* This function must be called before we emit something other than
8127 instructions. It is like mips_no_prev_insn except that it inserts
8128 any NOPS that might be needed by previous instructions. */
8129
8130 void
8131 mips_emit_delays (void)
8132 {
8133 if (! mips_opts.noreorder)
8134 {
8135 int nops = nops_for_insn (0, history, NULL);
8136 if (nops > 0)
8137 {
8138 while (nops-- > 0)
8139 add_fixed_insn (NOP_INSN);
8140 mips_move_text_labels ();
8141 }
8142 }
8143 mips_no_prev_insn ();
8144 }
8145
8146 /* Start a (possibly nested) noreorder block. */
8147
8148 static void
8149 start_noreorder (void)
8150 {
8151 if (mips_opts.noreorder == 0)
8152 {
8153 unsigned int i;
8154 int nops;
8155
8156 /* None of the instructions before the .set noreorder can be moved. */
8157 for (i = 0; i < ARRAY_SIZE (history); i++)
8158 history[i].fixed_p = 1;
8159
8160 /* Insert any nops that might be needed between the .set noreorder
8161 block and the previous instructions. We will later remove any
8162 nops that turn out not to be needed. */
8163 nops = nops_for_insn (0, history, NULL);
8164 if (nops > 0)
8165 {
8166 if (mips_optimize != 0)
8167 {
8168 /* Record the frag which holds the nop instructions, so
8169 that we can remove them if we don't need them. */
8170 frag_grow (nops * NOP_INSN_SIZE);
8171 prev_nop_frag = frag_now;
8172 prev_nop_frag_holds = nops;
8173 prev_nop_frag_required = 0;
8174 prev_nop_frag_since = 0;
8175 }
8176
8177 for (; nops > 0; --nops)
8178 add_fixed_insn (NOP_INSN);
8179
8180 /* Move on to a new frag, so that it is safe to simply
8181 decrease the size of prev_nop_frag. */
8182 frag_wane (frag_now);
8183 frag_new (0);
8184 mips_move_text_labels ();
8185 }
8186 mips_mark_labels ();
8187 mips_clear_insn_labels ();
8188 }
8189 mips_opts.noreorder++;
8190 mips_any_noreorder = 1;
8191 }
8192
8193 /* End a nested noreorder block. */
8194
8195 static void
8196 end_noreorder (void)
8197 {
8198 mips_opts.noreorder--;
8199 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
8200 {
8201 /* Commit to inserting prev_nop_frag_required nops and go back to
8202 handling nop insertion the .set reorder way. */
8203 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
8204 * NOP_INSN_SIZE);
8205 insert_into_history (prev_nop_frag_since,
8206 prev_nop_frag_required, NOP_INSN);
8207 prev_nop_frag = NULL;
8208 }
8209 }
8210
8211 /* Sign-extend 32-bit mode constants that have bit 31 set and all
8212 higher bits unset. */
8213
8214 static void
8215 normalize_constant_expr (expressionS *ex)
8216 {
8217 if (ex->X_op == O_constant
8218 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8219 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8220 - 0x80000000);
8221 }
8222
8223 /* Sign-extend 32-bit mode address offsets that have bit 31 set and
8224 all higher bits unset. */
8225
8226 static void
8227 normalize_address_expr (expressionS *ex)
8228 {
8229 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8230 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8231 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8232 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8233 - 0x80000000);
8234 }
8235
8236 /* Try to match TOKENS against OPCODE, storing the result in INSN.
8237 Return true if the match was successful.
8238
8239 OPCODE_EXTRA is a value that should be ORed into the opcode
8240 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
8241 there are more alternatives after OPCODE and SOFT_MATCH is
8242 as for mips_arg_info. */
8243
8244 static bfd_boolean
8245 match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8246 struct mips_operand_token *tokens, unsigned int opcode_extra,
8247 bfd_boolean lax_match, bfd_boolean complete_p)
8248 {
8249 const char *args;
8250 struct mips_arg_info arg;
8251 const struct mips_operand *operand;
8252 char c;
8253
8254 imm_expr.X_op = O_absent;
8255 offset_expr.X_op = O_absent;
8256 offset_reloc[0] = BFD_RELOC_UNUSED;
8257 offset_reloc[1] = BFD_RELOC_UNUSED;
8258 offset_reloc[2] = BFD_RELOC_UNUSED;
8259
8260 create_insn (insn, opcode);
8261 /* When no opcode suffix is specified, assume ".xyzw". */
8262 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8263 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8264 else
8265 insn->insn_opcode |= opcode_extra;
8266 memset (&arg, 0, sizeof (arg));
8267 arg.insn = insn;
8268 arg.token = tokens;
8269 arg.argnum = 1;
8270 arg.last_regno = ILLEGAL_REG;
8271 arg.dest_regno = ILLEGAL_REG;
8272 arg.lax_match = lax_match;
8273 for (args = opcode->args;; ++args)
8274 {
8275 if (arg.token->type == OT_END)
8276 {
8277 /* Handle unary instructions in which only one operand is given.
8278 The source is then the same as the destination. */
8279 if (arg.opnum == 1 && *args == ',')
8280 {
8281 operand = (mips_opts.micromips
8282 ? decode_micromips_operand (args + 1)
8283 : decode_mips_operand (args + 1));
8284 if (operand && mips_optional_operand_p (operand))
8285 {
8286 arg.token = tokens;
8287 arg.argnum = 1;
8288 continue;
8289 }
8290 }
8291
8292 /* Treat elided base registers as $0. */
8293 if (strcmp (args, "(b)") == 0)
8294 args += 3;
8295
8296 if (args[0] == '+')
8297 switch (args[1])
8298 {
8299 case 'K':
8300 case 'N':
8301 /* The register suffix is optional. */
8302 args += 2;
8303 break;
8304 }
8305
8306 /* Fail the match if there were too few operands. */
8307 if (*args)
8308 return FALSE;
8309
8310 /* Successful match. */
8311 if (!complete_p)
8312 return TRUE;
8313 clear_insn_error ();
8314 if (arg.dest_regno == arg.last_regno
8315 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8316 {
8317 if (arg.opnum == 2)
8318 set_insn_error
8319 (0, _("source and destination must be different"));
8320 else if (arg.last_regno == 31)
8321 set_insn_error
8322 (0, _("a destination register must be supplied"));
8323 }
8324 else if (arg.last_regno == 31
8325 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8326 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8327 set_insn_error (0, _("the source register must not be $31"));
8328 check_completed_insn (&arg);
8329 return TRUE;
8330 }
8331
8332 /* Fail the match if the line has too many operands. */
8333 if (*args == 0)
8334 return FALSE;
8335
8336 /* Handle characters that need to match exactly. */
8337 if (*args == '(' || *args == ')' || *args == ',')
8338 {
8339 if (match_char (&arg, *args))
8340 continue;
8341 return FALSE;
8342 }
8343 if (*args == '#')
8344 {
8345 ++args;
8346 if (arg.token->type == OT_DOUBLE_CHAR
8347 && arg.token->u.ch == *args)
8348 {
8349 ++arg.token;
8350 continue;
8351 }
8352 return FALSE;
8353 }
8354
8355 /* Handle special macro operands. Work out the properties of
8356 other operands. */
8357 arg.opnum += 1;
8358 switch (*args)
8359 {
8360 case '-':
8361 switch (args[1])
8362 {
8363 case 'A':
8364 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8365 break;
8366
8367 case 'B':
8368 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8369 break;
8370 }
8371 break;
8372
8373 case '+':
8374 switch (args[1])
8375 {
8376 case 'i':
8377 *offset_reloc = BFD_RELOC_MIPS_JMP;
8378 break;
8379
8380 case '\'':
8381 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8382 break;
8383
8384 case '\"':
8385 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8386 break;
8387 }
8388 break;
8389
8390 case 'I':
8391 if (!match_const_int (&arg, &imm_expr.X_add_number))
8392 return FALSE;
8393 imm_expr.X_op = O_constant;
8394 if (GPR_SIZE == 32)
8395 normalize_constant_expr (&imm_expr);
8396 continue;
8397
8398 case 'A':
8399 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8400 {
8401 /* Assume that the offset has been elided and that what
8402 we saw was a base register. The match will fail later
8403 if that assumption turns out to be wrong. */
8404 offset_expr.X_op = O_constant;
8405 offset_expr.X_add_number = 0;
8406 }
8407 else
8408 {
8409 if (!match_expression (&arg, &offset_expr, offset_reloc))
8410 return FALSE;
8411 normalize_address_expr (&offset_expr);
8412 }
8413 continue;
8414
8415 case 'F':
8416 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8417 8, TRUE))
8418 return FALSE;
8419 continue;
8420
8421 case 'L':
8422 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8423 8, FALSE))
8424 return FALSE;
8425 continue;
8426
8427 case 'f':
8428 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8429 4, TRUE))
8430 return FALSE;
8431 continue;
8432
8433 case 'l':
8434 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8435 4, FALSE))
8436 return FALSE;
8437 continue;
8438
8439 case 'p':
8440 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8441 break;
8442
8443 case 'a':
8444 *offset_reloc = BFD_RELOC_MIPS_JMP;
8445 break;
8446
8447 case 'm':
8448 gas_assert (mips_opts.micromips);
8449 c = args[1];
8450 switch (c)
8451 {
8452 case 'D':
8453 case 'E':
8454 if (!forced_insn_length)
8455 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8456 else if (c == 'D')
8457 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8458 else
8459 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8460 break;
8461 }
8462 break;
8463 }
8464
8465 operand = (mips_opts.micromips
8466 ? decode_micromips_operand (args)
8467 : decode_mips_operand (args));
8468 if (!operand)
8469 abort ();
8470
8471 /* Skip prefixes. */
8472 if (*args == '+' || *args == 'm' || *args == '-')
8473 args++;
8474
8475 if (mips_optional_operand_p (operand)
8476 && args[1] == ','
8477 && (arg.token[0].type != OT_REG
8478 || arg.token[1].type == OT_END))
8479 {
8480 /* Assume that the register has been elided and is the
8481 same as the first operand. */
8482 arg.token = tokens;
8483 arg.argnum = 1;
8484 }
8485
8486 if (!match_operand (&arg, operand))
8487 return FALSE;
8488 }
8489 }
8490
8491 /* Like match_insn, but for MIPS16. */
8492
8493 static bfd_boolean
8494 match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8495 struct mips_operand_token *tokens)
8496 {
8497 const char *args;
8498 const struct mips_operand *operand;
8499 const struct mips_operand *ext_operand;
8500 bfd_boolean pcrel = FALSE;
8501 int required_insn_length;
8502 struct mips_arg_info arg;
8503 int relax_char;
8504
8505 if (forced_insn_length)
8506 required_insn_length = forced_insn_length;
8507 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8508 required_insn_length = 2;
8509 else
8510 required_insn_length = 0;
8511
8512 create_insn (insn, opcode);
8513 imm_expr.X_op = O_absent;
8514 offset_expr.X_op = O_absent;
8515 offset_reloc[0] = BFD_RELOC_UNUSED;
8516 offset_reloc[1] = BFD_RELOC_UNUSED;
8517 offset_reloc[2] = BFD_RELOC_UNUSED;
8518 relax_char = 0;
8519
8520 memset (&arg, 0, sizeof (arg));
8521 arg.insn = insn;
8522 arg.token = tokens;
8523 arg.argnum = 1;
8524 arg.last_regno = ILLEGAL_REG;
8525 arg.dest_regno = ILLEGAL_REG;
8526 relax_char = 0;
8527 for (args = opcode->args;; ++args)
8528 {
8529 int c;
8530
8531 if (arg.token->type == OT_END)
8532 {
8533 offsetT value;
8534
8535 /* Handle unary instructions in which only one operand is given.
8536 The source is then the same as the destination. */
8537 if (arg.opnum == 1 && *args == ',')
8538 {
8539 operand = decode_mips16_operand (args[1], FALSE);
8540 if (operand && mips_optional_operand_p (operand))
8541 {
8542 arg.token = tokens;
8543 arg.argnum = 1;
8544 continue;
8545 }
8546 }
8547
8548 /* Fail the match if there were too few operands. */
8549 if (*args)
8550 return FALSE;
8551
8552 /* Successful match. Stuff the immediate value in now, if
8553 we can. */
8554 clear_insn_error ();
8555 if (opcode->pinfo == INSN_MACRO)
8556 {
8557 gas_assert (relax_char == 0 || relax_char == 'p');
8558 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8559 }
8560 else if (relax_char
8561 && offset_expr.X_op == O_constant
8562 && !pcrel
8563 && calculate_reloc (*offset_reloc,
8564 offset_expr.X_add_number,
8565 &value))
8566 {
8567 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
8568 required_insn_length, &insn->insn_opcode);
8569 offset_expr.X_op = O_absent;
8570 *offset_reloc = BFD_RELOC_UNUSED;
8571 }
8572 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8573 {
8574 if (required_insn_length == 2)
8575 set_insn_error (0, _("invalid unextended operand value"));
8576 else if (!mips_opcode_32bit_p (opcode))
8577 {
8578 forced_insn_length = 4;
8579 insn->insn_opcode |= MIPS16_EXTEND;
8580 }
8581 }
8582 else if (relax_char)
8583 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8584
8585 check_completed_insn (&arg);
8586 return TRUE;
8587 }
8588
8589 /* Fail the match if the line has too many operands. */
8590 if (*args == 0)
8591 return FALSE;
8592
8593 /* Handle characters that need to match exactly. */
8594 if (*args == '(' || *args == ')' || *args == ',')
8595 {
8596 if (match_char (&arg, *args))
8597 continue;
8598 return FALSE;
8599 }
8600
8601 arg.opnum += 1;
8602 c = *args;
8603 switch (c)
8604 {
8605 case 'p':
8606 case 'q':
8607 case 'A':
8608 case 'B':
8609 case 'E':
8610 case 'V':
8611 case 'u':
8612 relax_char = c;
8613 break;
8614
8615 case 'I':
8616 if (!match_const_int (&arg, &imm_expr.X_add_number))
8617 return FALSE;
8618 imm_expr.X_op = O_constant;
8619 if (GPR_SIZE == 32)
8620 normalize_constant_expr (&imm_expr);
8621 continue;
8622
8623 case 'a':
8624 case 'i':
8625 *offset_reloc = BFD_RELOC_MIPS16_JMP;
8626 break;
8627 }
8628
8629 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
8630 if (!operand)
8631 abort ();
8632
8633 if (operand->type == OP_PCREL)
8634 pcrel = TRUE;
8635 else
8636 {
8637 ext_operand = decode_mips16_operand (c, TRUE);
8638 if (operand != ext_operand)
8639 {
8640 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8641 {
8642 offset_expr.X_op = O_constant;
8643 offset_expr.X_add_number = 0;
8644 relax_char = c;
8645 continue;
8646 }
8647
8648 if (!match_expression (&arg, &offset_expr, offset_reloc))
8649 return FALSE;
8650
8651 /* '8' is used for SLTI(U) and has traditionally not
8652 been allowed to take relocation operators. */
8653 if (offset_reloc[0] != BFD_RELOC_UNUSED
8654 && (ext_operand->size != 16 || c == '8'))
8655 {
8656 match_not_constant (&arg);
8657 return FALSE;
8658 }
8659
8660 if (offset_expr.X_op == O_big)
8661 {
8662 match_out_of_range (&arg);
8663 return FALSE;
8664 }
8665
8666 relax_char = c;
8667 continue;
8668 }
8669 }
8670
8671 if (mips_optional_operand_p (operand)
8672 && args[1] == ','
8673 && (arg.token[0].type != OT_REG
8674 || arg.token[1].type == OT_END))
8675 {
8676 /* Assume that the register has been elided and is the
8677 same as the first operand. */
8678 arg.token = tokens;
8679 arg.argnum = 1;
8680 }
8681
8682 if (!match_operand (&arg, operand))
8683 return FALSE;
8684 }
8685 }
8686
8687 /* Record that the current instruction is invalid for the current ISA. */
8688
8689 static void
8690 match_invalid_for_isa (void)
8691 {
8692 set_insn_error_ss
8693 (0, _("opcode not supported on this processor: %s (%s)"),
8694 mips_cpu_info_from_arch (mips_opts.arch)->name,
8695 mips_cpu_info_from_isa (mips_opts.isa)->name);
8696 }
8697
8698 /* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8699 Return true if a definite match or failure was found, storing any match
8700 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8701 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8702 tried and failed to match under normal conditions and now want to try a
8703 more relaxed match. */
8704
8705 static bfd_boolean
8706 match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8707 const struct mips_opcode *past, struct mips_operand_token *tokens,
8708 int opcode_extra, bfd_boolean lax_match)
8709 {
8710 const struct mips_opcode *opcode;
8711 const struct mips_opcode *invalid_delay_slot;
8712 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8713
8714 /* Search for a match, ignoring alternatives that don't satisfy the
8715 current ISA or forced_length. */
8716 invalid_delay_slot = 0;
8717 seen_valid_for_isa = FALSE;
8718 seen_valid_for_size = FALSE;
8719 opcode = first;
8720 do
8721 {
8722 gas_assert (strcmp (opcode->name, first->name) == 0);
8723 if (is_opcode_valid (opcode))
8724 {
8725 seen_valid_for_isa = TRUE;
8726 if (is_size_valid (opcode))
8727 {
8728 bfd_boolean delay_slot_ok;
8729
8730 seen_valid_for_size = TRUE;
8731 delay_slot_ok = is_delay_slot_valid (opcode);
8732 if (match_insn (insn, opcode, tokens, opcode_extra,
8733 lax_match, delay_slot_ok))
8734 {
8735 if (!delay_slot_ok)
8736 {
8737 if (!invalid_delay_slot)
8738 invalid_delay_slot = opcode;
8739 }
8740 else
8741 return TRUE;
8742 }
8743 }
8744 }
8745 ++opcode;
8746 }
8747 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8748
8749 /* If the only matches we found had the wrong length for the delay slot,
8750 pick the first such match. We'll issue an appropriate warning later. */
8751 if (invalid_delay_slot)
8752 {
8753 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8754 lax_match, TRUE))
8755 return TRUE;
8756 abort ();
8757 }
8758
8759 /* Handle the case where we didn't try to match an instruction because
8760 all the alternatives were incompatible with the current ISA. */
8761 if (!seen_valid_for_isa)
8762 {
8763 match_invalid_for_isa ();
8764 return TRUE;
8765 }
8766
8767 /* Handle the case where we didn't try to match an instruction because
8768 all the alternatives were of the wrong size. */
8769 if (!seen_valid_for_size)
8770 {
8771 if (mips_opts.insn32)
8772 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
8773 else
8774 set_insn_error_i
8775 (0, _("unrecognized %d-bit version of microMIPS opcode"),
8776 8 * forced_insn_length);
8777 return TRUE;
8778 }
8779
8780 return FALSE;
8781 }
8782
8783 /* Like match_insns, but for MIPS16. */
8784
8785 static bfd_boolean
8786 match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8787 struct mips_operand_token *tokens)
8788 {
8789 const struct mips_opcode *opcode;
8790 bfd_boolean seen_valid_for_isa;
8791 bfd_boolean seen_valid_for_size;
8792
8793 /* Search for a match, ignoring alternatives that don't satisfy the
8794 current ISA. There are no separate entries for extended forms so
8795 we deal with forced_length later. */
8796 seen_valid_for_isa = FALSE;
8797 seen_valid_for_size = FALSE;
8798 opcode = first;
8799 do
8800 {
8801 gas_assert (strcmp (opcode->name, first->name) == 0);
8802 if (is_opcode_valid_16 (opcode))
8803 {
8804 seen_valid_for_isa = TRUE;
8805 if (is_size_valid_16 (opcode))
8806 {
8807 seen_valid_for_size = TRUE;
8808 if (match_mips16_insn (insn, opcode, tokens))
8809 return TRUE;
8810 }
8811 }
8812 ++opcode;
8813 }
8814 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8815 && strcmp (opcode->name, first->name) == 0);
8816
8817 /* Handle the case where we didn't try to match an instruction because
8818 all the alternatives were incompatible with the current ISA. */
8819 if (!seen_valid_for_isa)
8820 {
8821 match_invalid_for_isa ();
8822 return TRUE;
8823 }
8824
8825 /* Handle the case where we didn't try to match an instruction because
8826 all the alternatives were of the wrong size. */
8827 if (!seen_valid_for_size)
8828 {
8829 if (forced_insn_length == 2)
8830 set_insn_error
8831 (0, _("unrecognized unextended version of MIPS16 opcode"));
8832 else
8833 set_insn_error
8834 (0, _("unrecognized extended version of MIPS16 opcode"));
8835 return TRUE;
8836 }
8837
8838 return FALSE;
8839 }
8840
8841 /* Set up global variables for the start of a new macro. */
8842
8843 static void
8844 macro_start (void)
8845 {
8846 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
8847 memset (&mips_macro_warning.first_insn_sizes, 0,
8848 sizeof (mips_macro_warning.first_insn_sizes));
8849 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
8850 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
8851 && delayed_branch_p (&history[0]));
8852 if (history[0].frag
8853 && history[0].frag->fr_type == rs_machine_dependent
8854 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8855 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8856 mips_macro_warning.delay_slot_length = 0;
8857 else
8858 switch (history[0].insn_mo->pinfo2
8859 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8860 {
8861 case INSN2_BRANCH_DELAY_32BIT:
8862 mips_macro_warning.delay_slot_length = 4;
8863 break;
8864 case INSN2_BRANCH_DELAY_16BIT:
8865 mips_macro_warning.delay_slot_length = 2;
8866 break;
8867 default:
8868 mips_macro_warning.delay_slot_length = 0;
8869 break;
8870 }
8871 mips_macro_warning.first_frag = NULL;
8872 }
8873
8874 /* Given that a macro is longer than one instruction or of the wrong size,
8875 return the appropriate warning for it. Return null if no warning is
8876 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8877 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8878 and RELAX_NOMACRO. */
8879
8880 static const char *
8881 macro_warning (relax_substateT subtype)
8882 {
8883 if (subtype & RELAX_DELAY_SLOT)
8884 return _("macro instruction expanded into multiple instructions"
8885 " in a branch delay slot");
8886 else if (subtype & RELAX_NOMACRO)
8887 return _("macro instruction expanded into multiple instructions");
8888 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8889 | RELAX_DELAY_SLOT_SIZE_SECOND))
8890 return ((subtype & RELAX_DELAY_SLOT_16BIT)
8891 ? _("macro instruction expanded into a wrong size instruction"
8892 " in a 16-bit branch delay slot")
8893 : _("macro instruction expanded into a wrong size instruction"
8894 " in a 32-bit branch delay slot"));
8895 else
8896 return 0;
8897 }
8898
8899 /* Finish up a macro. Emit warnings as appropriate. */
8900
8901 static void
8902 macro_end (void)
8903 {
8904 /* Relaxation warning flags. */
8905 relax_substateT subtype = 0;
8906
8907 /* Check delay slot size requirements. */
8908 if (mips_macro_warning.delay_slot_length == 2)
8909 subtype |= RELAX_DELAY_SLOT_16BIT;
8910 if (mips_macro_warning.delay_slot_length != 0)
8911 {
8912 if (mips_macro_warning.delay_slot_length
8913 != mips_macro_warning.first_insn_sizes[0])
8914 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8915 if (mips_macro_warning.delay_slot_length
8916 != mips_macro_warning.first_insn_sizes[1])
8917 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8918 }
8919
8920 /* Check instruction count requirements. */
8921 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8922 {
8923 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
8924 subtype |= RELAX_SECOND_LONGER;
8925 if (mips_opts.warn_about_macros)
8926 subtype |= RELAX_NOMACRO;
8927 if (mips_macro_warning.delay_slot_p)
8928 subtype |= RELAX_DELAY_SLOT;
8929 }
8930
8931 /* If both alternatives fail to fill a delay slot correctly,
8932 emit the warning now. */
8933 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8934 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8935 {
8936 relax_substateT s;
8937 const char *msg;
8938
8939 s = subtype & (RELAX_DELAY_SLOT_16BIT
8940 | RELAX_DELAY_SLOT_SIZE_FIRST
8941 | RELAX_DELAY_SLOT_SIZE_SECOND);
8942 msg = macro_warning (s);
8943 if (msg != NULL)
8944 as_warn ("%s", msg);
8945 subtype &= ~s;
8946 }
8947
8948 /* If both implementations are longer than 1 instruction, then emit the
8949 warning now. */
8950 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8951 {
8952 relax_substateT s;
8953 const char *msg;
8954
8955 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8956 msg = macro_warning (s);
8957 if (msg != NULL)
8958 as_warn ("%s", msg);
8959 subtype &= ~s;
8960 }
8961
8962 /* If any flags still set, then one implementation might need a warning
8963 and the other either will need one of a different kind or none at all.
8964 Pass any remaining flags over to relaxation. */
8965 if (mips_macro_warning.first_frag != NULL)
8966 mips_macro_warning.first_frag->fr_subtype |= subtype;
8967 }
8968
8969 /* Instruction operand formats used in macros that vary between
8970 standard MIPS and microMIPS code. */
8971
8972 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
8973 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8974 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8975 static const char * const lui_fmt[2] = { "t,u", "s,u" };
8976 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
8977 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
8978 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8979 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8980
8981 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
8982 #define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8983 : cop12_fmt[mips_opts.micromips])
8984 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
8985 #define LUI_FMT (lui_fmt[mips_opts.micromips])
8986 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
8987 #define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8988 : mem12_fmt[mips_opts.micromips])
8989 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
8990 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
8991 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
8992
8993 /* Read a macro's relocation codes from *ARGS and store them in *R.
8994 The first argument in *ARGS will be either the code for a single
8995 relocation or -1 followed by the three codes that make up a
8996 composite relocation. */
8997
8998 static void
8999 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
9000 {
9001 int i, next;
9002
9003 next = va_arg (*args, int);
9004 if (next >= 0)
9005 r[0] = (bfd_reloc_code_real_type) next;
9006 else
9007 {
9008 for (i = 0; i < 3; i++)
9009 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
9010 /* This function is only used for 16-bit relocation fields.
9011 To make the macro code simpler, treat an unrelocated value
9012 in the same way as BFD_RELOC_LO16. */
9013 if (r[0] == BFD_RELOC_UNUSED)
9014 r[0] = BFD_RELOC_LO16;
9015 }
9016 }
9017
9018 /* Build an instruction created by a macro expansion. This is passed
9019 a pointer to the count of instructions created so far, an
9020 expression, the name of the instruction to build, an operand format
9021 string, and corresponding arguments. */
9022
9023 static void
9024 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
9025 {
9026 const struct mips_opcode *mo = NULL;
9027 bfd_reloc_code_real_type r[3];
9028 const struct mips_opcode *amo;
9029 const struct mips_operand *operand;
9030 struct hash_control *hash;
9031 struct mips_cl_insn insn;
9032 va_list args;
9033 unsigned int uval;
9034
9035 va_start (args, fmt);
9036
9037 if (mips_opts.mips16)
9038 {
9039 mips16_macro_build (ep, name, fmt, &args);
9040 va_end (args);
9041 return;
9042 }
9043
9044 r[0] = BFD_RELOC_UNUSED;
9045 r[1] = BFD_RELOC_UNUSED;
9046 r[2] = BFD_RELOC_UNUSED;
9047 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
9048 amo = (struct mips_opcode *) hash_find (hash, name);
9049 gas_assert (amo);
9050 gas_assert (strcmp (name, amo->name) == 0);
9051
9052 do
9053 {
9054 /* Search until we get a match for NAME. It is assumed here that
9055 macros will never generate MDMX, MIPS-3D, or MT instructions.
9056 We try to match an instruction that fulfills the branch delay
9057 slot instruction length requirement (if any) of the previous
9058 instruction. While doing this we record the first instruction
9059 seen that matches all the other conditions and use it anyway
9060 if the requirement cannot be met; we will issue an appropriate
9061 warning later on. */
9062 if (strcmp (fmt, amo->args) == 0
9063 && amo->pinfo != INSN_MACRO
9064 && is_opcode_valid (amo)
9065 && is_size_valid (amo))
9066 {
9067 if (is_delay_slot_valid (amo))
9068 {
9069 mo = amo;
9070 break;
9071 }
9072 else if (!mo)
9073 mo = amo;
9074 }
9075
9076 ++amo;
9077 gas_assert (amo->name);
9078 }
9079 while (strcmp (name, amo->name) == 0);
9080
9081 gas_assert (mo);
9082 create_insn (&insn, mo);
9083 for (; *fmt; ++fmt)
9084 {
9085 switch (*fmt)
9086 {
9087 case ',':
9088 case '(':
9089 case ')':
9090 case 'z':
9091 break;
9092
9093 case 'i':
9094 case 'j':
9095 macro_read_relocs (&args, r);
9096 gas_assert (*r == BFD_RELOC_GPREL16
9097 || *r == BFD_RELOC_MIPS_HIGHER
9098 || *r == BFD_RELOC_HI16_S
9099 || *r == BFD_RELOC_LO16
9100 || *r == BFD_RELOC_MIPS_GOT_OFST
9101 || (mips_opts.micromips
9102 && (*r == BFD_RELOC_16
9103 || *r == BFD_RELOC_MIPS_GOT16
9104 || *r == BFD_RELOC_MIPS_CALL16
9105 || *r == BFD_RELOC_MIPS_GOT_HI16
9106 || *r == BFD_RELOC_MIPS_GOT_LO16
9107 || *r == BFD_RELOC_MIPS_CALL_HI16
9108 || *r == BFD_RELOC_MIPS_CALL_LO16
9109 || *r == BFD_RELOC_MIPS_SUB
9110 || *r == BFD_RELOC_MIPS_GOT_PAGE
9111 || *r == BFD_RELOC_MIPS_HIGHEST
9112 || *r == BFD_RELOC_MIPS_GOT_DISP
9113 || *r == BFD_RELOC_MIPS_TLS_GD
9114 || *r == BFD_RELOC_MIPS_TLS_LDM
9115 || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
9116 || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
9117 || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
9118 || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
9119 || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
9120 break;
9121
9122 case 'o':
9123 macro_read_relocs (&args, r);
9124 break;
9125
9126 case 'u':
9127 macro_read_relocs (&args, r);
9128 gas_assert (ep != NULL
9129 && (ep->X_op == O_constant
9130 || (ep->X_op == O_symbol
9131 && (*r == BFD_RELOC_MIPS_HIGHEST
9132 || *r == BFD_RELOC_HI16_S
9133 || *r == BFD_RELOC_HI16
9134 || *r == BFD_RELOC_GPREL16
9135 || *r == BFD_RELOC_MIPS_GOT_HI16
9136 || *r == BFD_RELOC_MIPS_CALL_HI16))));
9137 break;
9138
9139 case 'p':
9140 gas_assert (ep != NULL);
9141
9142 /*
9143 * This allows macro() to pass an immediate expression for
9144 * creating short branches without creating a symbol.
9145 *
9146 * We don't allow branch relaxation for these branches, as
9147 * they should only appear in ".set nomacro" anyway.
9148 */
9149 if (ep->X_op == O_constant)
9150 {
9151 /* For microMIPS we always use relocations for branches.
9152 So we should not resolve immediate values. */
9153 gas_assert (!mips_opts.micromips);
9154
9155 if ((ep->X_add_number & 3) != 0)
9156 as_bad (_("branch to misaligned address (0x%lx)"),
9157 (unsigned long) ep->X_add_number);
9158 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
9159 as_bad (_("branch address range overflow (0x%lx)"),
9160 (unsigned long) ep->X_add_number);
9161 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
9162 ep = NULL;
9163 }
9164 else
9165 *r = BFD_RELOC_16_PCREL_S2;
9166 break;
9167
9168 case 'a':
9169 gas_assert (ep != NULL);
9170 *r = BFD_RELOC_MIPS_JMP;
9171 break;
9172
9173 default:
9174 operand = (mips_opts.micromips
9175 ? decode_micromips_operand (fmt)
9176 : decode_mips_operand (fmt));
9177 if (!operand)
9178 abort ();
9179
9180 uval = va_arg (args, int);
9181 if (operand->type == OP_CLO_CLZ_DEST)
9182 uval |= (uval << 5);
9183 insn_insert_operand (&insn, operand, uval);
9184
9185 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
9186 ++fmt;
9187 break;
9188 }
9189 }
9190 va_end (args);
9191 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
9192
9193 append_insn (&insn, ep, r, TRUE);
9194 }
9195
9196 static void
9197 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
9198 va_list *args)
9199 {
9200 struct mips_opcode *mo;
9201 struct mips_cl_insn insn;
9202 const struct mips_operand *operand;
9203 bfd_reloc_code_real_type r[3]
9204 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
9205
9206 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9207 gas_assert (mo);
9208 gas_assert (strcmp (name, mo->name) == 0);
9209
9210 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
9211 {
9212 ++mo;
9213 gas_assert (mo->name);
9214 gas_assert (strcmp (name, mo->name) == 0);
9215 }
9216
9217 create_insn (&insn, mo);
9218 for (; *fmt; ++fmt)
9219 {
9220 int c;
9221
9222 c = *fmt;
9223 switch (c)
9224 {
9225 case ',':
9226 case '(':
9227 case ')':
9228 break;
9229
9230 case '.':
9231 case 'S':
9232 case 'P':
9233 case 'R':
9234 break;
9235
9236 case '<':
9237 case '5':
9238 case 'F':
9239 case 'H':
9240 case 'W':
9241 case 'D':
9242 case 'j':
9243 case '8':
9244 case 'V':
9245 case 'C':
9246 case 'U':
9247 case 'k':
9248 case 'K':
9249 case 'p':
9250 case 'q':
9251 {
9252 offsetT value;
9253
9254 gas_assert (ep != NULL);
9255
9256 if (ep->X_op != O_constant)
9257 *r = (int) BFD_RELOC_UNUSED + c;
9258 else if (calculate_reloc (*r, ep->X_add_number, &value))
9259 {
9260 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
9261 ep = NULL;
9262 *r = BFD_RELOC_UNUSED;
9263 }
9264 }
9265 break;
9266
9267 default:
9268 operand = decode_mips16_operand (c, FALSE);
9269 if (!operand)
9270 abort ();
9271
9272 insn_insert_operand (&insn, operand, va_arg (*args, int));
9273 break;
9274 }
9275 }
9276
9277 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
9278
9279 append_insn (&insn, ep, r, TRUE);
9280 }
9281
9282 /*
9283 * Generate a "jalr" instruction with a relocation hint to the called
9284 * function. This occurs in NewABI PIC code.
9285 */
9286 static void
9287 macro_build_jalr (expressionS *ep, int cprestore)
9288 {
9289 static const bfd_reloc_code_real_type jalr_relocs[2]
9290 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9291 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9292 const char *jalr;
9293 char *f = NULL;
9294
9295 if (MIPS_JALR_HINT_P (ep))
9296 {
9297 frag_grow (8);
9298 f = frag_more (0);
9299 }
9300 if (mips_opts.micromips)
9301 {
9302 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9303 ? "jalr" : "jalrs");
9304 if (MIPS_JALR_HINT_P (ep)
9305 || mips_opts.insn32
9306 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9307 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9308 else
9309 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9310 }
9311 else
9312 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
9313 if (MIPS_JALR_HINT_P (ep))
9314 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
9315 }
9316
9317 /*
9318 * Generate a "lui" instruction.
9319 */
9320 static void
9321 macro_build_lui (expressionS *ep, int regnum)
9322 {
9323 gas_assert (! mips_opts.mips16);
9324
9325 if (ep->X_op != O_constant)
9326 {
9327 gas_assert (ep->X_op == O_symbol);
9328 /* _gp_disp is a special case, used from s_cpload.
9329 __gnu_local_gp is used if mips_no_shared. */
9330 gas_assert (mips_pic == NO_PIC
9331 || (! HAVE_NEWABI
9332 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9333 || (! mips_in_shared
9334 && strcmp (S_GET_NAME (ep->X_add_symbol),
9335 "__gnu_local_gp") == 0));
9336 }
9337
9338 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
9339 }
9340
9341 /* Generate a sequence of instructions to do a load or store from a constant
9342 offset off of a base register (breg) into/from a target register (treg),
9343 using AT if necessary. */
9344 static void
9345 macro_build_ldst_constoffset (expressionS *ep, const char *op,
9346 int treg, int breg, int dbl)
9347 {
9348 gas_assert (ep->X_op == O_constant);
9349
9350 /* Sign-extending 32-bit constants makes their handling easier. */
9351 if (!dbl)
9352 normalize_constant_expr (ep);
9353
9354 /* Right now, this routine can only handle signed 32-bit constants. */
9355 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
9356 as_warn (_("operand overflow"));
9357
9358 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9359 {
9360 /* Signed 16-bit offset will fit in the op. Easy! */
9361 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
9362 }
9363 else
9364 {
9365 /* 32-bit offset, need multiple instructions and AT, like:
9366 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9367 addu $tempreg,$tempreg,$breg
9368 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9369 to handle the complete offset. */
9370 macro_build_lui (ep, AT);
9371 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9372 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
9373
9374 if (!mips_opts.at)
9375 as_bad (_("macro used $at after \".set noat\""));
9376 }
9377 }
9378
9379 /* set_at()
9380 * Generates code to set the $at register to true (one)
9381 * if reg is less than the immediate expression.
9382 */
9383 static void
9384 set_at (int reg, int unsignedp)
9385 {
9386 if (imm_expr.X_add_number >= -0x8000
9387 && imm_expr.X_add_number < 0x8000)
9388 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9389 AT, reg, BFD_RELOC_LO16);
9390 else
9391 {
9392 load_register (AT, &imm_expr, GPR_SIZE == 64);
9393 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
9394 }
9395 }
9396
9397 /* Count the leading zeroes by performing a binary chop. This is a
9398 bulky bit of source, but performance is a LOT better for the
9399 majority of values than a simple loop to count the bits:
9400 for (lcnt = 0; (lcnt < 32); lcnt++)
9401 if ((v) & (1 << (31 - lcnt)))
9402 break;
9403 However it is not code size friendly, and the gain will drop a bit
9404 on certain cached systems.
9405 */
9406 #define COUNT_TOP_ZEROES(v) \
9407 (((v) & ~0xffff) == 0 \
9408 ? ((v) & ~0xff) == 0 \
9409 ? ((v) & ~0xf) == 0 \
9410 ? ((v) & ~0x3) == 0 \
9411 ? ((v) & ~0x1) == 0 \
9412 ? !(v) \
9413 ? 32 \
9414 : 31 \
9415 : 30 \
9416 : ((v) & ~0x7) == 0 \
9417 ? 29 \
9418 : 28 \
9419 : ((v) & ~0x3f) == 0 \
9420 ? ((v) & ~0x1f) == 0 \
9421 ? 27 \
9422 : 26 \
9423 : ((v) & ~0x7f) == 0 \
9424 ? 25 \
9425 : 24 \
9426 : ((v) & ~0xfff) == 0 \
9427 ? ((v) & ~0x3ff) == 0 \
9428 ? ((v) & ~0x1ff) == 0 \
9429 ? 23 \
9430 : 22 \
9431 : ((v) & ~0x7ff) == 0 \
9432 ? 21 \
9433 : 20 \
9434 : ((v) & ~0x3fff) == 0 \
9435 ? ((v) & ~0x1fff) == 0 \
9436 ? 19 \
9437 : 18 \
9438 : ((v) & ~0x7fff) == 0 \
9439 ? 17 \
9440 : 16 \
9441 : ((v) & ~0xffffff) == 0 \
9442 ? ((v) & ~0xfffff) == 0 \
9443 ? ((v) & ~0x3ffff) == 0 \
9444 ? ((v) & ~0x1ffff) == 0 \
9445 ? 15 \
9446 : 14 \
9447 : ((v) & ~0x7ffff) == 0 \
9448 ? 13 \
9449 : 12 \
9450 : ((v) & ~0x3fffff) == 0 \
9451 ? ((v) & ~0x1fffff) == 0 \
9452 ? 11 \
9453 : 10 \
9454 : ((v) & ~0x7fffff) == 0 \
9455 ? 9 \
9456 : 8 \
9457 : ((v) & ~0xfffffff) == 0 \
9458 ? ((v) & ~0x3ffffff) == 0 \
9459 ? ((v) & ~0x1ffffff) == 0 \
9460 ? 7 \
9461 : 6 \
9462 : ((v) & ~0x7ffffff) == 0 \
9463 ? 5 \
9464 : 4 \
9465 : ((v) & ~0x3fffffff) == 0 \
9466 ? ((v) & ~0x1fffffff) == 0 \
9467 ? 3 \
9468 : 2 \
9469 : ((v) & ~0x7fffffff) == 0 \
9470 ? 1 \
9471 : 0)
9472
9473 /* load_register()
9474 * This routine generates the least number of instructions necessary to load
9475 * an absolute expression value into a register.
9476 */
9477 static void
9478 load_register (int reg, expressionS *ep, int dbl)
9479 {
9480 int freg;
9481 expressionS hi32, lo32;
9482
9483 if (ep->X_op != O_big)
9484 {
9485 gas_assert (ep->X_op == O_constant);
9486
9487 /* Sign-extending 32-bit constants makes their handling easier. */
9488 if (!dbl)
9489 normalize_constant_expr (ep);
9490
9491 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
9492 {
9493 /* We can handle 16 bit signed values with an addiu to
9494 $zero. No need to ever use daddiu here, since $zero and
9495 the result are always correct in 32 bit mode. */
9496 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9497 return;
9498 }
9499 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9500 {
9501 /* We can handle 16 bit unsigned values with an ori to
9502 $zero. */
9503 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9504 return;
9505 }
9506 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
9507 {
9508 /* 32 bit values require an lui. */
9509 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9510 if ((ep->X_add_number & 0xffff) != 0)
9511 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9512 return;
9513 }
9514 }
9515
9516 /* The value is larger than 32 bits. */
9517
9518 if (!dbl || GPR_SIZE == 32)
9519 {
9520 char value[32];
9521
9522 sprintf_vma (value, ep->X_add_number);
9523 as_bad (_("number (0x%s) larger than 32 bits"), value);
9524 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9525 return;
9526 }
9527
9528 if (ep->X_op != O_big)
9529 {
9530 hi32 = *ep;
9531 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9532 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9533 hi32.X_add_number &= 0xffffffff;
9534 lo32 = *ep;
9535 lo32.X_add_number &= 0xffffffff;
9536 }
9537 else
9538 {
9539 gas_assert (ep->X_add_number > 2);
9540 if (ep->X_add_number == 3)
9541 generic_bignum[3] = 0;
9542 else if (ep->X_add_number > 4)
9543 as_bad (_("number larger than 64 bits"));
9544 lo32.X_op = O_constant;
9545 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9546 hi32.X_op = O_constant;
9547 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9548 }
9549
9550 if (hi32.X_add_number == 0)
9551 freg = 0;
9552 else
9553 {
9554 int shift, bit;
9555 unsigned long hi, lo;
9556
9557 if (hi32.X_add_number == (offsetT) 0xffffffff)
9558 {
9559 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9560 {
9561 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9562 return;
9563 }
9564 if (lo32.X_add_number & 0x80000000)
9565 {
9566 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9567 if (lo32.X_add_number & 0xffff)
9568 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9569 return;
9570 }
9571 }
9572
9573 /* Check for 16bit shifted constant. We know that hi32 is
9574 non-zero, so start the mask on the first bit of the hi32
9575 value. */
9576 shift = 17;
9577 do
9578 {
9579 unsigned long himask, lomask;
9580
9581 if (shift < 32)
9582 {
9583 himask = 0xffff >> (32 - shift);
9584 lomask = (0xffff << shift) & 0xffffffff;
9585 }
9586 else
9587 {
9588 himask = 0xffff << (shift - 32);
9589 lomask = 0;
9590 }
9591 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9592 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9593 {
9594 expressionS tmp;
9595
9596 tmp.X_op = O_constant;
9597 if (shift < 32)
9598 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9599 | (lo32.X_add_number >> shift));
9600 else
9601 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
9602 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9603 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9604 reg, reg, (shift >= 32) ? shift - 32 : shift);
9605 return;
9606 }
9607 ++shift;
9608 }
9609 while (shift <= (64 - 16));
9610
9611 /* Find the bit number of the lowest one bit, and store the
9612 shifted value in hi/lo. */
9613 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9614 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9615 if (lo != 0)
9616 {
9617 bit = 0;
9618 while ((lo & 1) == 0)
9619 {
9620 lo >>= 1;
9621 ++bit;
9622 }
9623 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9624 hi >>= bit;
9625 }
9626 else
9627 {
9628 bit = 32;
9629 while ((hi & 1) == 0)
9630 {
9631 hi >>= 1;
9632 ++bit;
9633 }
9634 lo = hi;
9635 hi = 0;
9636 }
9637
9638 /* Optimize if the shifted value is a (power of 2) - 1. */
9639 if ((hi == 0 && ((lo + 1) & lo) == 0)
9640 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
9641 {
9642 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
9643 if (shift != 0)
9644 {
9645 expressionS tmp;
9646
9647 /* This instruction will set the register to be all
9648 ones. */
9649 tmp.X_op = O_constant;
9650 tmp.X_add_number = (offsetT) -1;
9651 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9652 if (bit != 0)
9653 {
9654 bit += shift;
9655 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9656 reg, reg, (bit >= 32) ? bit - 32 : bit);
9657 }
9658 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
9659 reg, reg, (shift >= 32) ? shift - 32 : shift);
9660 return;
9661 }
9662 }
9663
9664 /* Sign extend hi32 before calling load_register, because we can
9665 generally get better code when we load a sign extended value. */
9666 if ((hi32.X_add_number & 0x80000000) != 0)
9667 hi32.X_add_number |= ~(offsetT) 0xffffffff;
9668 load_register (reg, &hi32, 0);
9669 freg = reg;
9670 }
9671 if ((lo32.X_add_number & 0xffff0000) == 0)
9672 {
9673 if (freg != 0)
9674 {
9675 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
9676 freg = reg;
9677 }
9678 }
9679 else
9680 {
9681 expressionS mid16;
9682
9683 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
9684 {
9685 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9686 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
9687 return;
9688 }
9689
9690 if (freg != 0)
9691 {
9692 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
9693 freg = reg;
9694 }
9695 mid16 = lo32;
9696 mid16.X_add_number >>= 16;
9697 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9698 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9699 freg = reg;
9700 }
9701 if ((lo32.X_add_number & 0xffff) != 0)
9702 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9703 }
9704
9705 static inline void
9706 load_delay_nop (void)
9707 {
9708 if (!gpr_interlocks)
9709 macro_build (NULL, "nop", "");
9710 }
9711
9712 /* Load an address into a register. */
9713
9714 static void
9715 load_address (int reg, expressionS *ep, int *used_at)
9716 {
9717 if (ep->X_op != O_constant
9718 && ep->X_op != O_symbol)
9719 {
9720 as_bad (_("expression too complex"));
9721 ep->X_op = O_constant;
9722 }
9723
9724 if (ep->X_op == O_constant)
9725 {
9726 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
9727 return;
9728 }
9729
9730 if (mips_pic == NO_PIC)
9731 {
9732 /* If this is a reference to a GP relative symbol, we want
9733 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
9734 Otherwise we want
9735 lui $reg,<sym> (BFD_RELOC_HI16_S)
9736 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9737 If we have an addend, we always use the latter form.
9738
9739 With 64bit address space and a usable $at we want
9740 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9741 lui $at,<sym> (BFD_RELOC_HI16_S)
9742 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9743 daddiu $at,<sym> (BFD_RELOC_LO16)
9744 dsll32 $reg,0
9745 daddu $reg,$reg,$at
9746
9747 If $at is already in use, we use a path which is suboptimal
9748 on superscalar processors.
9749 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9750 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9751 dsll $reg,16
9752 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9753 dsll $reg,16
9754 daddiu $reg,<sym> (BFD_RELOC_LO16)
9755
9756 For GP relative symbols in 64bit address space we can use
9757 the same sequence as in 32bit address space. */
9758 if (HAVE_64BIT_SYMBOLS)
9759 {
9760 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9761 && !nopic_need_relax (ep->X_add_symbol, 1))
9762 {
9763 relax_start (ep->X_add_symbol);
9764 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9765 mips_gp_register, BFD_RELOC_GPREL16);
9766 relax_switch ();
9767 }
9768
9769 if (*used_at == 0 && mips_opts.at)
9770 {
9771 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9772 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
9773 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9774 BFD_RELOC_MIPS_HIGHER);
9775 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
9776 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
9777 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
9778 *used_at = 1;
9779 }
9780 else
9781 {
9782 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9783 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9784 BFD_RELOC_MIPS_HIGHER);
9785 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9786 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
9787 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9788 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
9789 }
9790
9791 if (mips_relax.sequence)
9792 relax_end ();
9793 }
9794 else
9795 {
9796 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9797 && !nopic_need_relax (ep->X_add_symbol, 1))
9798 {
9799 relax_start (ep->X_add_symbol);
9800 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9801 mips_gp_register, BFD_RELOC_GPREL16);
9802 relax_switch ();
9803 }
9804 macro_build_lui (ep, reg);
9805 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9806 reg, reg, BFD_RELOC_LO16);
9807 if (mips_relax.sequence)
9808 relax_end ();
9809 }
9810 }
9811 else if (!mips_big_got)
9812 {
9813 expressionS ex;
9814
9815 /* If this is a reference to an external symbol, we want
9816 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9817 Otherwise we want
9818 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9819 nop
9820 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9821 If there is a constant, it must be added in after.
9822
9823 If we have NewABI, we want
9824 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9825 unless we're referencing a global symbol with a non-zero
9826 offset, in which case cst must be added separately. */
9827 if (HAVE_NEWABI)
9828 {
9829 if (ep->X_add_number)
9830 {
9831 ex.X_add_number = ep->X_add_number;
9832 ep->X_add_number = 0;
9833 relax_start (ep->X_add_symbol);
9834 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9835 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9836 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9837 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9838 ex.X_op = O_constant;
9839 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9840 reg, reg, BFD_RELOC_LO16);
9841 ep->X_add_number = ex.X_add_number;
9842 relax_switch ();
9843 }
9844 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9845 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9846 if (mips_relax.sequence)
9847 relax_end ();
9848 }
9849 else
9850 {
9851 ex.X_add_number = ep->X_add_number;
9852 ep->X_add_number = 0;
9853 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9854 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9855 load_delay_nop ();
9856 relax_start (ep->X_add_symbol);
9857 relax_switch ();
9858 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9859 BFD_RELOC_LO16);
9860 relax_end ();
9861
9862 if (ex.X_add_number != 0)
9863 {
9864 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9865 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9866 ex.X_op = O_constant;
9867 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9868 reg, reg, BFD_RELOC_LO16);
9869 }
9870 }
9871 }
9872 else if (mips_big_got)
9873 {
9874 expressionS ex;
9875
9876 /* This is the large GOT case. If this is a reference to an
9877 external symbol, we want
9878 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9879 addu $reg,$reg,$gp
9880 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
9881
9882 Otherwise, for a reference to a local symbol in old ABI, we want
9883 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9884 nop
9885 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9886 If there is a constant, it must be added in after.
9887
9888 In the NewABI, for local symbols, with or without offsets, we want:
9889 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9890 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
9891 */
9892 if (HAVE_NEWABI)
9893 {
9894 ex.X_add_number = ep->X_add_number;
9895 ep->X_add_number = 0;
9896 relax_start (ep->X_add_symbol);
9897 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9898 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9899 reg, reg, mips_gp_register);
9900 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9901 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9902 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9903 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9904 else if (ex.X_add_number)
9905 {
9906 ex.X_op = O_constant;
9907 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9908 BFD_RELOC_LO16);
9909 }
9910
9911 ep->X_add_number = ex.X_add_number;
9912 relax_switch ();
9913 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9914 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9915 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9916 BFD_RELOC_MIPS_GOT_OFST);
9917 relax_end ();
9918 }
9919 else
9920 {
9921 ex.X_add_number = ep->X_add_number;
9922 ep->X_add_number = 0;
9923 relax_start (ep->X_add_symbol);
9924 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9925 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9926 reg, reg, mips_gp_register);
9927 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9928 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9929 relax_switch ();
9930 if (reg_needs_delay (mips_gp_register))
9931 {
9932 /* We need a nop before loading from $gp. This special
9933 check is required because the lui which starts the main
9934 instruction stream does not refer to $gp, and so will not
9935 insert the nop which may be required. */
9936 macro_build (NULL, "nop", "");
9937 }
9938 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9939 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9940 load_delay_nop ();
9941 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9942 BFD_RELOC_LO16);
9943 relax_end ();
9944
9945 if (ex.X_add_number != 0)
9946 {
9947 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9948 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9949 ex.X_op = O_constant;
9950 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9951 BFD_RELOC_LO16);
9952 }
9953 }
9954 }
9955 else
9956 abort ();
9957
9958 if (!mips_opts.at && *used_at == 1)
9959 as_bad (_("macro used $at after \".set noat\""));
9960 }
9961
9962 /* Move the contents of register SOURCE into register DEST. */
9963
9964 static void
9965 move_register (int dest, int source)
9966 {
9967 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9968 instruction specifically requires a 32-bit one. */
9969 if (mips_opts.micromips
9970 && !mips_opts.insn32
9971 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9972 macro_build (NULL, "move", "mp,mj", dest, source);
9973 else
9974 macro_build (NULL, "or", "d,v,t", dest, source, 0);
9975 }
9976
9977 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
9978 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9979 The two alternatives are:
9980
9981 Global symbol Local symbol
9982 ------------- ------------
9983 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9984 ... ...
9985 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9986
9987 load_got_offset emits the first instruction and add_got_offset
9988 emits the second for a 16-bit offset or add_got_offset_hilo emits
9989 a sequence to add a 32-bit offset using a scratch register. */
9990
9991 static void
9992 load_got_offset (int dest, expressionS *local)
9993 {
9994 expressionS global;
9995
9996 global = *local;
9997 global.X_add_number = 0;
9998
9999 relax_start (local->X_add_symbol);
10000 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
10001 BFD_RELOC_MIPS_GOT16, mips_gp_register);
10002 relax_switch ();
10003 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
10004 BFD_RELOC_MIPS_GOT16, mips_gp_register);
10005 relax_end ();
10006 }
10007
10008 static void
10009 add_got_offset (int dest, expressionS *local)
10010 {
10011 expressionS global;
10012
10013 global.X_op = O_constant;
10014 global.X_op_symbol = NULL;
10015 global.X_add_symbol = NULL;
10016 global.X_add_number = local->X_add_number;
10017
10018 relax_start (local->X_add_symbol);
10019 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
10020 dest, dest, BFD_RELOC_LO16);
10021 relax_switch ();
10022 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
10023 relax_end ();
10024 }
10025
10026 static void
10027 add_got_offset_hilo (int dest, expressionS *local, int tmp)
10028 {
10029 expressionS global;
10030 int hold_mips_optimize;
10031
10032 global.X_op = O_constant;
10033 global.X_op_symbol = NULL;
10034 global.X_add_symbol = NULL;
10035 global.X_add_number = local->X_add_number;
10036
10037 relax_start (local->X_add_symbol);
10038 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
10039 relax_switch ();
10040 /* Set mips_optimize around the lui instruction to avoid
10041 inserting an unnecessary nop after the lw. */
10042 hold_mips_optimize = mips_optimize;
10043 mips_optimize = 2;
10044 macro_build_lui (&global, tmp);
10045 mips_optimize = hold_mips_optimize;
10046 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
10047 relax_end ();
10048
10049 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
10050 }
10051
10052 /* Emit a sequence of instructions to emulate a branch likely operation.
10053 BR is an ordinary branch corresponding to one to be emulated. BRNEG
10054 is its complementing branch with the original condition negated.
10055 CALL is set if the original branch specified the link operation.
10056 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
10057
10058 Code like this is produced in the noreorder mode:
10059
10060 BRNEG <args>, 1f
10061 nop
10062 b <sym>
10063 delay slot (executed only if branch taken)
10064 1:
10065
10066 or, if CALL is set:
10067
10068 BRNEG <args>, 1f
10069 nop
10070 bal <sym>
10071 delay slot (executed only if branch taken)
10072 1:
10073
10074 In the reorder mode the delay slot would be filled with a nop anyway,
10075 so code produced is simply:
10076
10077 BR <args>, <sym>
10078 nop
10079
10080 This function is used when producing code for the microMIPS ASE that
10081 does not implement branch likely instructions in hardware. */
10082
10083 static void
10084 macro_build_branch_likely (const char *br, const char *brneg,
10085 int call, expressionS *ep, const char *fmt,
10086 unsigned int sreg, unsigned int treg)
10087 {
10088 int noreorder = mips_opts.noreorder;
10089 expressionS expr1;
10090
10091 gas_assert (mips_opts.micromips);
10092 start_noreorder ();
10093 if (noreorder)
10094 {
10095 micromips_label_expr (&expr1);
10096 macro_build (&expr1, brneg, fmt, sreg, treg);
10097 macro_build (NULL, "nop", "");
10098 macro_build (ep, call ? "bal" : "b", "p");
10099
10100 /* Set to true so that append_insn adds a label. */
10101 emit_branch_likely_macro = TRUE;
10102 }
10103 else
10104 {
10105 macro_build (ep, br, fmt, sreg, treg);
10106 macro_build (NULL, "nop", "");
10107 }
10108 end_noreorder ();
10109 }
10110
10111 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
10112 the condition code tested. EP specifies the branch target. */
10113
10114 static void
10115 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
10116 {
10117 const int call = 0;
10118 const char *brneg;
10119 const char *br;
10120
10121 switch (type)
10122 {
10123 case M_BC1FL:
10124 br = "bc1f";
10125 brneg = "bc1t";
10126 break;
10127 case M_BC1TL:
10128 br = "bc1t";
10129 brneg = "bc1f";
10130 break;
10131 case M_BC2FL:
10132 br = "bc2f";
10133 brneg = "bc2t";
10134 break;
10135 case M_BC2TL:
10136 br = "bc2t";
10137 brneg = "bc2f";
10138 break;
10139 default:
10140 abort ();
10141 }
10142 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
10143 }
10144
10145 /* Emit a two-argument branch macro specified by TYPE, using SREG as
10146 the register tested. EP specifies the branch target. */
10147
10148 static void
10149 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
10150 {
10151 const char *brneg = NULL;
10152 const char *br;
10153 int call = 0;
10154
10155 switch (type)
10156 {
10157 case M_BGEZ:
10158 br = "bgez";
10159 break;
10160 case M_BGEZL:
10161 br = mips_opts.micromips ? "bgez" : "bgezl";
10162 brneg = "bltz";
10163 break;
10164 case M_BGEZALL:
10165 gas_assert (mips_opts.micromips);
10166 br = mips_opts.insn32 ? "bgezal" : "bgezals";
10167 brneg = "bltz";
10168 call = 1;
10169 break;
10170 case M_BGTZ:
10171 br = "bgtz";
10172 break;
10173 case M_BGTZL:
10174 br = mips_opts.micromips ? "bgtz" : "bgtzl";
10175 brneg = "blez";
10176 break;
10177 case M_BLEZ:
10178 br = "blez";
10179 break;
10180 case M_BLEZL:
10181 br = mips_opts.micromips ? "blez" : "blezl";
10182 brneg = "bgtz";
10183 break;
10184 case M_BLTZ:
10185 br = "bltz";
10186 break;
10187 case M_BLTZL:
10188 br = mips_opts.micromips ? "bltz" : "bltzl";
10189 brneg = "bgez";
10190 break;
10191 case M_BLTZALL:
10192 gas_assert (mips_opts.micromips);
10193 br = mips_opts.insn32 ? "bltzal" : "bltzals";
10194 brneg = "bgez";
10195 call = 1;
10196 break;
10197 default:
10198 abort ();
10199 }
10200 if (mips_opts.micromips && brneg)
10201 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
10202 else
10203 macro_build (ep, br, "s,p", sreg);
10204 }
10205
10206 /* Emit a three-argument branch macro specified by TYPE, using SREG and
10207 TREG as the registers tested. EP specifies the branch target. */
10208
10209 static void
10210 macro_build_branch_rsrt (int type, expressionS *ep,
10211 unsigned int sreg, unsigned int treg)
10212 {
10213 const char *brneg = NULL;
10214 const int call = 0;
10215 const char *br;
10216
10217 switch (type)
10218 {
10219 case M_BEQ:
10220 case M_BEQ_I:
10221 br = "beq";
10222 break;
10223 case M_BEQL:
10224 case M_BEQL_I:
10225 br = mips_opts.micromips ? "beq" : "beql";
10226 brneg = "bne";
10227 break;
10228 case M_BNE:
10229 case M_BNE_I:
10230 br = "bne";
10231 break;
10232 case M_BNEL:
10233 case M_BNEL_I:
10234 br = mips_opts.micromips ? "bne" : "bnel";
10235 brneg = "beq";
10236 break;
10237 default:
10238 abort ();
10239 }
10240 if (mips_opts.micromips && brneg)
10241 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10242 else
10243 macro_build (ep, br, "s,t,p", sreg, treg);
10244 }
10245
10246 /* Return the high part that should be loaded in order to make the low
10247 part of VALUE accessible using an offset of OFFBITS bits. */
10248
10249 static offsetT
10250 offset_high_part (offsetT value, unsigned int offbits)
10251 {
10252 offsetT bias;
10253 addressT low_mask;
10254
10255 if (offbits == 0)
10256 return value;
10257 bias = 1 << (offbits - 1);
10258 low_mask = bias * 2 - 1;
10259 return (value + bias) & ~low_mask;
10260 }
10261
10262 /* Return true if the value stored in offset_expr and offset_reloc
10263 fits into a signed offset of OFFBITS bits. RANGE is the maximum
10264 amount that the caller wants to add without inducing overflow
10265 and ALIGN is the known alignment of the value in bytes. */
10266
10267 static bfd_boolean
10268 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10269 {
10270 if (offbits == 16)
10271 {
10272 /* Accept any relocation operator if overflow isn't a concern. */
10273 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10274 return TRUE;
10275
10276 /* These relocations are guaranteed not to overflow in correct links. */
10277 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10278 || gprel16_reloc_p (*offset_reloc))
10279 return TRUE;
10280 }
10281 if (offset_expr.X_op == O_constant
10282 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10283 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10284 return TRUE;
10285 return FALSE;
10286 }
10287
10288 /*
10289 * Build macros
10290 * This routine implements the seemingly endless macro or synthesized
10291 * instructions and addressing modes in the mips assembly language. Many
10292 * of these macros are simple and are similar to each other. These could
10293 * probably be handled by some kind of table or grammar approach instead of
10294 * this verbose method. Others are not simple macros but are more like
10295 * optimizing code generation.
10296 * One interesting optimization is when several store macros appear
10297 * consecutively that would load AT with the upper half of the same address.
10298 * The ensuing load upper instructions are omitted. This implies some kind
10299 * of global optimization. We currently only optimize within a single macro.
10300 * For many of the load and store macros if the address is specified as a
10301 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10302 * first load register 'at' with zero and use it as the base register. The
10303 * mips assembler simply uses register $zero. Just one tiny optimization
10304 * we're missing.
10305 */
10306 static void
10307 macro (struct mips_cl_insn *ip, char *str)
10308 {
10309 const struct mips_operand_array *operands;
10310 unsigned int breg, i;
10311 unsigned int tempreg;
10312 int mask;
10313 int used_at = 0;
10314 expressionS label_expr;
10315 expressionS expr1;
10316 expressionS *ep;
10317 const char *s;
10318 const char *s2;
10319 const char *fmt;
10320 int likely = 0;
10321 int coproc = 0;
10322 int offbits = 16;
10323 int call = 0;
10324 int jals = 0;
10325 int dbl = 0;
10326 int imm = 0;
10327 int ust = 0;
10328 int lp = 0;
10329 int ll_sc_paired = 0;
10330 bfd_boolean large_offset;
10331 int off;
10332 int hold_mips_optimize;
10333 unsigned int align;
10334 unsigned int op[MAX_OPERANDS];
10335
10336 gas_assert (! mips_opts.mips16);
10337
10338 operands = insn_operands (ip);
10339 for (i = 0; i < MAX_OPERANDS; i++)
10340 if (operands->operand[i])
10341 op[i] = insn_extract_operand (ip, operands->operand[i]);
10342 else
10343 op[i] = -1;
10344
10345 mask = ip->insn_mo->mask;
10346
10347 label_expr.X_op = O_constant;
10348 label_expr.X_op_symbol = NULL;
10349 label_expr.X_add_symbol = NULL;
10350 label_expr.X_add_number = 0;
10351
10352 expr1.X_op = O_constant;
10353 expr1.X_op_symbol = NULL;
10354 expr1.X_add_symbol = NULL;
10355 expr1.X_add_number = 1;
10356 align = 1;
10357
10358 switch (mask)
10359 {
10360 case M_DABS:
10361 dbl = 1;
10362 /* Fall through. */
10363 case M_ABS:
10364 /* bgez $a0,1f
10365 move v0,$a0
10366 sub v0,$zero,$a0
10367 1:
10368 */
10369
10370 start_noreorder ();
10371
10372 if (mips_opts.micromips)
10373 micromips_label_expr (&label_expr);
10374 else
10375 label_expr.X_add_number = 8;
10376 macro_build (&label_expr, "bgez", "s,p", op[1]);
10377 if (op[0] == op[1])
10378 macro_build (NULL, "nop", "");
10379 else
10380 move_register (op[0], op[1]);
10381 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
10382 if (mips_opts.micromips)
10383 micromips_add_label ();
10384
10385 end_noreorder ();
10386 break;
10387
10388 case M_ADD_I:
10389 s = "addi";
10390 s2 = "add";
10391 if (ISA_IS_R6 (mips_opts.isa))
10392 goto do_addi_i;
10393 else
10394 goto do_addi;
10395 case M_ADDU_I:
10396 s = "addiu";
10397 s2 = "addu";
10398 goto do_addi;
10399 case M_DADD_I:
10400 dbl = 1;
10401 s = "daddi";
10402 s2 = "dadd";
10403 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
10404 goto do_addi;
10405 if (imm_expr.X_add_number >= -0x200
10406 && imm_expr.X_add_number < 0x200
10407 && !ISA_IS_R6 (mips_opts.isa))
10408 {
10409 macro_build (NULL, s, "t,r,.", op[0], op[1],
10410 (int) imm_expr.X_add_number);
10411 break;
10412 }
10413 goto do_addi_i;
10414 case M_DADDU_I:
10415 dbl = 1;
10416 s = "daddiu";
10417 s2 = "daddu";
10418 do_addi:
10419 if (imm_expr.X_add_number >= -0x8000
10420 && imm_expr.X_add_number < 0x8000)
10421 {
10422 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
10423 break;
10424 }
10425 do_addi_i:
10426 used_at = 1;
10427 load_register (AT, &imm_expr, dbl);
10428 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10429 break;
10430
10431 case M_AND_I:
10432 s = "andi";
10433 s2 = "and";
10434 goto do_bit;
10435 case M_OR_I:
10436 s = "ori";
10437 s2 = "or";
10438 goto do_bit;
10439 case M_NOR_I:
10440 s = "";
10441 s2 = "nor";
10442 goto do_bit;
10443 case M_XOR_I:
10444 s = "xori";
10445 s2 = "xor";
10446 do_bit:
10447 if (imm_expr.X_add_number >= 0
10448 && imm_expr.X_add_number < 0x10000)
10449 {
10450 if (mask != M_NOR_I)
10451 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
10452 else
10453 {
10454 macro_build (&imm_expr, "ori", "t,r,i",
10455 op[0], op[1], BFD_RELOC_LO16);
10456 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
10457 }
10458 break;
10459 }
10460
10461 used_at = 1;
10462 load_register (AT, &imm_expr, GPR_SIZE == 64);
10463 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10464 break;
10465
10466 case M_BALIGN:
10467 switch (imm_expr.X_add_number)
10468 {
10469 case 0:
10470 macro_build (NULL, "nop", "");
10471 break;
10472 case 2:
10473 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
10474 break;
10475 case 1:
10476 case 3:
10477 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
10478 (int) imm_expr.X_add_number);
10479 break;
10480 default:
10481 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10482 (unsigned long) imm_expr.X_add_number);
10483 break;
10484 }
10485 break;
10486
10487 case M_BC1FL:
10488 case M_BC1TL:
10489 case M_BC2FL:
10490 case M_BC2TL:
10491 gas_assert (mips_opts.micromips);
10492 macro_build_branch_ccl (mask, &offset_expr,
10493 EXTRACT_OPERAND (1, BCC, *ip));
10494 break;
10495
10496 case M_BEQ_I:
10497 case M_BEQL_I:
10498 case M_BNE_I:
10499 case M_BNEL_I:
10500 if (imm_expr.X_add_number == 0)
10501 op[1] = 0;
10502 else
10503 {
10504 op[1] = AT;
10505 used_at = 1;
10506 load_register (op[1], &imm_expr, GPR_SIZE == 64);
10507 }
10508 /* Fall through. */
10509 case M_BEQL:
10510 case M_BNEL:
10511 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
10512 break;
10513
10514 case M_BGEL:
10515 likely = 1;
10516 /* Fall through. */
10517 case M_BGE:
10518 if (op[1] == 0)
10519 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10520 else if (op[0] == 0)
10521 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
10522 else
10523 {
10524 used_at = 1;
10525 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10526 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10527 &offset_expr, AT, ZERO);
10528 }
10529 break;
10530
10531 case M_BGEZL:
10532 case M_BGEZALL:
10533 case M_BGTZL:
10534 case M_BLEZL:
10535 case M_BLTZL:
10536 case M_BLTZALL:
10537 macro_build_branch_rs (mask, &offset_expr, op[0]);
10538 break;
10539
10540 case M_BGTL_I:
10541 likely = 1;
10542 /* Fall through. */
10543 case M_BGT_I:
10544 /* Check for > max integer. */
10545 if (imm_expr.X_add_number >= GPR_SMAX)
10546 {
10547 do_false:
10548 /* Result is always false. */
10549 if (! likely)
10550 macro_build (NULL, "nop", "");
10551 else
10552 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
10553 break;
10554 }
10555 ++imm_expr.X_add_number;
10556 /* Fall through. */
10557 case M_BGE_I:
10558 case M_BGEL_I:
10559 if (mask == M_BGEL_I)
10560 likely = 1;
10561 if (imm_expr.X_add_number == 0)
10562 {
10563 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
10564 &offset_expr, op[0]);
10565 break;
10566 }
10567 if (imm_expr.X_add_number == 1)
10568 {
10569 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
10570 &offset_expr, op[0]);
10571 break;
10572 }
10573 if (imm_expr.X_add_number <= GPR_SMIN)
10574 {
10575 do_true:
10576 /* Result is always true. */
10577 as_warn (_("branch %s is always true"), ip->insn_mo->name);
10578 macro_build (&offset_expr, "b", "p");
10579 break;
10580 }
10581 used_at = 1;
10582 set_at (op[0], 0);
10583 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10584 &offset_expr, AT, ZERO);
10585 break;
10586
10587 case M_BGEUL:
10588 likely = 1;
10589 /* Fall through. */
10590 case M_BGEU:
10591 if (op[1] == 0)
10592 goto do_true;
10593 else if (op[0] == 0)
10594 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10595 &offset_expr, ZERO, op[1]);
10596 else
10597 {
10598 used_at = 1;
10599 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10600 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10601 &offset_expr, AT, ZERO);
10602 }
10603 break;
10604
10605 case M_BGTUL_I:
10606 likely = 1;
10607 /* Fall through. */
10608 case M_BGTU_I:
10609 if (op[0] == 0
10610 || (GPR_SIZE == 32
10611 && imm_expr.X_add_number == -1))
10612 goto do_false;
10613 ++imm_expr.X_add_number;
10614 /* Fall through. */
10615 case M_BGEU_I:
10616 case M_BGEUL_I:
10617 if (mask == M_BGEUL_I)
10618 likely = 1;
10619 if (imm_expr.X_add_number == 0)
10620 goto do_true;
10621 else if (imm_expr.X_add_number == 1)
10622 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10623 &offset_expr, op[0], ZERO);
10624 else
10625 {
10626 used_at = 1;
10627 set_at (op[0], 1);
10628 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10629 &offset_expr, AT, ZERO);
10630 }
10631 break;
10632
10633 case M_BGTL:
10634 likely = 1;
10635 /* Fall through. */
10636 case M_BGT:
10637 if (op[1] == 0)
10638 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10639 else if (op[0] == 0)
10640 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
10641 else
10642 {
10643 used_at = 1;
10644 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10645 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10646 &offset_expr, AT, ZERO);
10647 }
10648 break;
10649
10650 case M_BGTUL:
10651 likely = 1;
10652 /* Fall through. */
10653 case M_BGTU:
10654 if (op[1] == 0)
10655 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10656 &offset_expr, op[0], ZERO);
10657 else if (op[0] == 0)
10658 goto do_false;
10659 else
10660 {
10661 used_at = 1;
10662 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10663 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10664 &offset_expr, AT, ZERO);
10665 }
10666 break;
10667
10668 case M_BLEL:
10669 likely = 1;
10670 /* Fall through. */
10671 case M_BLE:
10672 if (op[1] == 0)
10673 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10674 else if (op[0] == 0)
10675 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
10676 else
10677 {
10678 used_at = 1;
10679 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10680 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10681 &offset_expr, AT, ZERO);
10682 }
10683 break;
10684
10685 case M_BLEL_I:
10686 likely = 1;
10687 /* Fall through. */
10688 case M_BLE_I:
10689 if (imm_expr.X_add_number >= GPR_SMAX)
10690 goto do_true;
10691 ++imm_expr.X_add_number;
10692 /* Fall through. */
10693 case M_BLT_I:
10694 case M_BLTL_I:
10695 if (mask == M_BLTL_I)
10696 likely = 1;
10697 if (imm_expr.X_add_number == 0)
10698 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10699 else if (imm_expr.X_add_number == 1)
10700 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10701 else
10702 {
10703 used_at = 1;
10704 set_at (op[0], 0);
10705 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10706 &offset_expr, AT, ZERO);
10707 }
10708 break;
10709
10710 case M_BLEUL:
10711 likely = 1;
10712 /* Fall through. */
10713 case M_BLEU:
10714 if (op[1] == 0)
10715 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10716 &offset_expr, op[0], ZERO);
10717 else if (op[0] == 0)
10718 goto do_true;
10719 else
10720 {
10721 used_at = 1;
10722 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10723 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10724 &offset_expr, AT, ZERO);
10725 }
10726 break;
10727
10728 case M_BLEUL_I:
10729 likely = 1;
10730 /* Fall through. */
10731 case M_BLEU_I:
10732 if (op[0] == 0
10733 || (GPR_SIZE == 32
10734 && imm_expr.X_add_number == -1))
10735 goto do_true;
10736 ++imm_expr.X_add_number;
10737 /* Fall through. */
10738 case M_BLTU_I:
10739 case M_BLTUL_I:
10740 if (mask == M_BLTUL_I)
10741 likely = 1;
10742 if (imm_expr.X_add_number == 0)
10743 goto do_false;
10744 else if (imm_expr.X_add_number == 1)
10745 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10746 &offset_expr, op[0], ZERO);
10747 else
10748 {
10749 used_at = 1;
10750 set_at (op[0], 1);
10751 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10752 &offset_expr, AT, ZERO);
10753 }
10754 break;
10755
10756 case M_BLTL:
10757 likely = 1;
10758 /* Fall through. */
10759 case M_BLT:
10760 if (op[1] == 0)
10761 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10762 else if (op[0] == 0)
10763 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
10764 else
10765 {
10766 used_at = 1;
10767 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10768 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10769 &offset_expr, AT, ZERO);
10770 }
10771 break;
10772
10773 case M_BLTUL:
10774 likely = 1;
10775 /* Fall through. */
10776 case M_BLTU:
10777 if (op[1] == 0)
10778 goto do_false;
10779 else if (op[0] == 0)
10780 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10781 &offset_expr, ZERO, op[1]);
10782 else
10783 {
10784 used_at = 1;
10785 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10786 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10787 &offset_expr, AT, ZERO);
10788 }
10789 break;
10790
10791 case M_DDIV_3:
10792 dbl = 1;
10793 /* Fall through. */
10794 case M_DIV_3:
10795 s = "mflo";
10796 goto do_div3;
10797 case M_DREM_3:
10798 dbl = 1;
10799 /* Fall through. */
10800 case M_REM_3:
10801 s = "mfhi";
10802 do_div3:
10803 if (op[2] == 0)
10804 {
10805 as_warn (_("divide by zero"));
10806 if (mips_trap)
10807 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10808 else
10809 macro_build (NULL, "break", BRK_FMT, 7);
10810 break;
10811 }
10812
10813 start_noreorder ();
10814 if (mips_trap)
10815 {
10816 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10817 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10818 }
10819 else
10820 {
10821 if (mips_opts.micromips)
10822 micromips_label_expr (&label_expr);
10823 else
10824 label_expr.X_add_number = 8;
10825 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10826 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10827 macro_build (NULL, "break", BRK_FMT, 7);
10828 if (mips_opts.micromips)
10829 micromips_add_label ();
10830 }
10831 expr1.X_add_number = -1;
10832 used_at = 1;
10833 load_register (AT, &expr1, dbl);
10834 if (mips_opts.micromips)
10835 micromips_label_expr (&label_expr);
10836 else
10837 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
10838 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
10839 if (dbl)
10840 {
10841 expr1.X_add_number = 1;
10842 load_register (AT, &expr1, dbl);
10843 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
10844 }
10845 else
10846 {
10847 expr1.X_add_number = 0x80000000;
10848 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
10849 }
10850 if (mips_trap)
10851 {
10852 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
10853 /* We want to close the noreorder block as soon as possible, so
10854 that later insns are available for delay slot filling. */
10855 end_noreorder ();
10856 }
10857 else
10858 {
10859 if (mips_opts.micromips)
10860 micromips_label_expr (&label_expr);
10861 else
10862 label_expr.X_add_number = 8;
10863 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
10864 macro_build (NULL, "nop", "");
10865
10866 /* We want to close the noreorder block as soon as possible, so
10867 that later insns are available for delay slot filling. */
10868 end_noreorder ();
10869
10870 macro_build (NULL, "break", BRK_FMT, 6);
10871 }
10872 if (mips_opts.micromips)
10873 micromips_add_label ();
10874 macro_build (NULL, s, MFHL_FMT, op[0]);
10875 break;
10876
10877 case M_DIV_3I:
10878 s = "div";
10879 s2 = "mflo";
10880 goto do_divi;
10881 case M_DIVU_3I:
10882 s = "divu";
10883 s2 = "mflo";
10884 goto do_divi;
10885 case M_REM_3I:
10886 s = "div";
10887 s2 = "mfhi";
10888 goto do_divi;
10889 case M_REMU_3I:
10890 s = "divu";
10891 s2 = "mfhi";
10892 goto do_divi;
10893 case M_DDIV_3I:
10894 dbl = 1;
10895 s = "ddiv";
10896 s2 = "mflo";
10897 goto do_divi;
10898 case M_DDIVU_3I:
10899 dbl = 1;
10900 s = "ddivu";
10901 s2 = "mflo";
10902 goto do_divi;
10903 case M_DREM_3I:
10904 dbl = 1;
10905 s = "ddiv";
10906 s2 = "mfhi";
10907 goto do_divi;
10908 case M_DREMU_3I:
10909 dbl = 1;
10910 s = "ddivu";
10911 s2 = "mfhi";
10912 do_divi:
10913 if (imm_expr.X_add_number == 0)
10914 {
10915 as_warn (_("divide by zero"));
10916 if (mips_trap)
10917 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10918 else
10919 macro_build (NULL, "break", BRK_FMT, 7);
10920 break;
10921 }
10922 if (imm_expr.X_add_number == 1)
10923 {
10924 if (strcmp (s2, "mflo") == 0)
10925 move_register (op[0], op[1]);
10926 else
10927 move_register (op[0], ZERO);
10928 break;
10929 }
10930 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
10931 {
10932 if (strcmp (s2, "mflo") == 0)
10933 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
10934 else
10935 move_register (op[0], ZERO);
10936 break;
10937 }
10938
10939 used_at = 1;
10940 load_register (AT, &imm_expr, dbl);
10941 macro_build (NULL, s, "z,s,t", op[1], AT);
10942 macro_build (NULL, s2, MFHL_FMT, op[0]);
10943 break;
10944
10945 case M_DIVU_3:
10946 s = "divu";
10947 s2 = "mflo";
10948 goto do_divu3;
10949 case M_REMU_3:
10950 s = "divu";
10951 s2 = "mfhi";
10952 goto do_divu3;
10953 case M_DDIVU_3:
10954 s = "ddivu";
10955 s2 = "mflo";
10956 goto do_divu3;
10957 case M_DREMU_3:
10958 s = "ddivu";
10959 s2 = "mfhi";
10960 do_divu3:
10961 start_noreorder ();
10962 if (mips_trap)
10963 {
10964 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10965 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10966 /* We want to close the noreorder block as soon as possible, so
10967 that later insns are available for delay slot filling. */
10968 end_noreorder ();
10969 }
10970 else
10971 {
10972 if (mips_opts.micromips)
10973 micromips_label_expr (&label_expr);
10974 else
10975 label_expr.X_add_number = 8;
10976 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10977 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10978
10979 /* We want to close the noreorder block as soon as possible, so
10980 that later insns are available for delay slot filling. */
10981 end_noreorder ();
10982 macro_build (NULL, "break", BRK_FMT, 7);
10983 if (mips_opts.micromips)
10984 micromips_add_label ();
10985 }
10986 macro_build (NULL, s2, MFHL_FMT, op[0]);
10987 break;
10988
10989 case M_DLCA_AB:
10990 dbl = 1;
10991 /* Fall through. */
10992 case M_LCA_AB:
10993 call = 1;
10994 goto do_la;
10995 case M_DLA_AB:
10996 dbl = 1;
10997 /* Fall through. */
10998 case M_LA_AB:
10999 do_la:
11000 /* Load the address of a symbol into a register. If breg is not
11001 zero, we then add a base register to it. */
11002
11003 breg = op[2];
11004 if (dbl && GPR_SIZE == 32)
11005 as_warn (_("dla used to load 32-bit register; recommend using la "
11006 "instead"));
11007
11008 if (!dbl && HAVE_64BIT_OBJECTS)
11009 as_warn (_("la used to load 64-bit address; recommend using dla "
11010 "instead"));
11011
11012 if (small_offset_p (0, align, 16))
11013 {
11014 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
11015 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
11016 break;
11017 }
11018
11019 if (mips_opts.at && (op[0] == breg))
11020 {
11021 tempreg = AT;
11022 used_at = 1;
11023 }
11024 else
11025 tempreg = op[0];
11026
11027 if (offset_expr.X_op != O_symbol
11028 && offset_expr.X_op != O_constant)
11029 {
11030 as_bad (_("expression too complex"));
11031 offset_expr.X_op = O_constant;
11032 }
11033
11034 if (offset_expr.X_op == O_constant)
11035 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
11036 else if (mips_pic == NO_PIC)
11037 {
11038 /* If this is a reference to a GP relative symbol, we want
11039 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
11040 Otherwise we want
11041 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11042 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11043 If we have a constant, we need two instructions anyhow,
11044 so we may as well always use the latter form.
11045
11046 With 64bit address space and a usable $at we want
11047 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11048 lui $at,<sym> (BFD_RELOC_HI16_S)
11049 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11050 daddiu $at,<sym> (BFD_RELOC_LO16)
11051 dsll32 $tempreg,0
11052 daddu $tempreg,$tempreg,$at
11053
11054 If $at is already in use, we use a path which is suboptimal
11055 on superscalar processors.
11056 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11057 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11058 dsll $tempreg,16
11059 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11060 dsll $tempreg,16
11061 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
11062
11063 For GP relative symbols in 64bit address space we can use
11064 the same sequence as in 32bit address space. */
11065 if (HAVE_64BIT_SYMBOLS)
11066 {
11067 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11068 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11069 {
11070 relax_start (offset_expr.X_add_symbol);
11071 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11072 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11073 relax_switch ();
11074 }
11075
11076 if (used_at == 0 && mips_opts.at)
11077 {
11078 macro_build (&offset_expr, "lui", LUI_FMT,
11079 tempreg, BFD_RELOC_MIPS_HIGHEST);
11080 macro_build (&offset_expr, "lui", LUI_FMT,
11081 AT, BFD_RELOC_HI16_S);
11082 macro_build (&offset_expr, "daddiu", "t,r,j",
11083 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
11084 macro_build (&offset_expr, "daddiu", "t,r,j",
11085 AT, AT, BFD_RELOC_LO16);
11086 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
11087 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
11088 used_at = 1;
11089 }
11090 else
11091 {
11092 macro_build (&offset_expr, "lui", LUI_FMT,
11093 tempreg, BFD_RELOC_MIPS_HIGHEST);
11094 macro_build (&offset_expr, "daddiu", "t,r,j",
11095 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
11096 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11097 macro_build (&offset_expr, "daddiu", "t,r,j",
11098 tempreg, tempreg, BFD_RELOC_HI16_S);
11099 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11100 macro_build (&offset_expr, "daddiu", "t,r,j",
11101 tempreg, tempreg, BFD_RELOC_LO16);
11102 }
11103
11104 if (mips_relax.sequence)
11105 relax_end ();
11106 }
11107 else
11108 {
11109 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11110 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11111 {
11112 relax_start (offset_expr.X_add_symbol);
11113 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11114 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11115 relax_switch ();
11116 }
11117 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
11118 as_bad (_("offset too large"));
11119 macro_build_lui (&offset_expr, tempreg);
11120 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11121 tempreg, tempreg, BFD_RELOC_LO16);
11122 if (mips_relax.sequence)
11123 relax_end ();
11124 }
11125 }
11126 else if (!mips_big_got && !HAVE_NEWABI)
11127 {
11128 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11129
11130 /* If this is a reference to an external symbol, and there
11131 is no constant, we want
11132 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11133 or for lca or if tempreg is PIC_CALL_REG
11134 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11135 For a local symbol, we want
11136 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11137 nop
11138 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11139
11140 If we have a small constant, and this is a reference to
11141 an external symbol, we want
11142 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11143 nop
11144 addiu $tempreg,$tempreg,<constant>
11145 For a local symbol, we want the same instruction
11146 sequence, but we output a BFD_RELOC_LO16 reloc on the
11147 addiu instruction.
11148
11149 If we have a large constant, and this is a reference to
11150 an external symbol, we want
11151 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11152 lui $at,<hiconstant>
11153 addiu $at,$at,<loconstant>
11154 addu $tempreg,$tempreg,$at
11155 For a local symbol, we want the same instruction
11156 sequence, but we output a BFD_RELOC_LO16 reloc on the
11157 addiu instruction.
11158 */
11159
11160 if (offset_expr.X_add_number == 0)
11161 {
11162 if (mips_pic == SVR4_PIC
11163 && breg == 0
11164 && (call || tempreg == PIC_CALL_REG))
11165 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
11166
11167 relax_start (offset_expr.X_add_symbol);
11168 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11169 lw_reloc_type, mips_gp_register);
11170 if (breg != 0)
11171 {
11172 /* We're going to put in an addu instruction using
11173 tempreg, so we may as well insert the nop right
11174 now. */
11175 load_delay_nop ();
11176 }
11177 relax_switch ();
11178 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11179 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
11180 load_delay_nop ();
11181 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11182 tempreg, tempreg, BFD_RELOC_LO16);
11183 relax_end ();
11184 /* FIXME: If breg == 0, and the next instruction uses
11185 $tempreg, then if this variant case is used an extra
11186 nop will be generated. */
11187 }
11188 else if (offset_expr.X_add_number >= -0x8000
11189 && offset_expr.X_add_number < 0x8000)
11190 {
11191 load_got_offset (tempreg, &offset_expr);
11192 load_delay_nop ();
11193 add_got_offset (tempreg, &offset_expr);
11194 }
11195 else
11196 {
11197 expr1.X_add_number = offset_expr.X_add_number;
11198 offset_expr.X_add_number =
11199 SEXT_16BIT (offset_expr.X_add_number);
11200 load_got_offset (tempreg, &offset_expr);
11201 offset_expr.X_add_number = expr1.X_add_number;
11202 /* If we are going to add in a base register, and the
11203 target register and the base register are the same,
11204 then we are using AT as a temporary register. Since
11205 we want to load the constant into AT, we add our
11206 current AT (from the global offset table) and the
11207 register into the register now, and pretend we were
11208 not using a base register. */
11209 if (breg == op[0])
11210 {
11211 load_delay_nop ();
11212 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11213 op[0], AT, breg);
11214 breg = 0;
11215 tempreg = op[0];
11216 }
11217 add_got_offset_hilo (tempreg, &offset_expr, AT);
11218 used_at = 1;
11219 }
11220 }
11221 else if (!mips_big_got && HAVE_NEWABI)
11222 {
11223 int add_breg_early = 0;
11224
11225 /* If this is a reference to an external, and there is no
11226 constant, or local symbol (*), with or without a
11227 constant, we want
11228 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11229 or for lca or if tempreg is PIC_CALL_REG
11230 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11231
11232 If we have a small constant, and this is a reference to
11233 an external symbol, we want
11234 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11235 addiu $tempreg,$tempreg,<constant>
11236
11237 If we have a large constant, and this is a reference to
11238 an external symbol, we want
11239 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11240 lui $at,<hiconstant>
11241 addiu $at,$at,<loconstant>
11242 addu $tempreg,$tempreg,$at
11243
11244 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11245 local symbols, even though it introduces an additional
11246 instruction. */
11247
11248 if (offset_expr.X_add_number)
11249 {
11250 expr1.X_add_number = offset_expr.X_add_number;
11251 offset_expr.X_add_number = 0;
11252
11253 relax_start (offset_expr.X_add_symbol);
11254 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11255 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11256
11257 if (expr1.X_add_number >= -0x8000
11258 && expr1.X_add_number < 0x8000)
11259 {
11260 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11261 tempreg, tempreg, BFD_RELOC_LO16);
11262 }
11263 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11264 {
11265 unsigned int dreg;
11266
11267 /* If we are going to add in a base register, and the
11268 target register and the base register are the same,
11269 then we are using AT as a temporary register. Since
11270 we want to load the constant into AT, we add our
11271 current AT (from the global offset table) and the
11272 register into the register now, and pretend we were
11273 not using a base register. */
11274 if (breg != op[0])
11275 dreg = tempreg;
11276 else
11277 {
11278 gas_assert (tempreg == AT);
11279 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11280 op[0], AT, breg);
11281 dreg = op[0];
11282 add_breg_early = 1;
11283 }
11284
11285 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11286 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11287 dreg, dreg, AT);
11288
11289 used_at = 1;
11290 }
11291 else
11292 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11293
11294 relax_switch ();
11295 offset_expr.X_add_number = expr1.X_add_number;
11296
11297 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11298 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11299 if (add_breg_early)
11300 {
11301 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11302 op[0], tempreg, breg);
11303 breg = 0;
11304 tempreg = op[0];
11305 }
11306 relax_end ();
11307 }
11308 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
11309 {
11310 relax_start (offset_expr.X_add_symbol);
11311 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11312 BFD_RELOC_MIPS_CALL16, mips_gp_register);
11313 relax_switch ();
11314 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11315 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11316 relax_end ();
11317 }
11318 else
11319 {
11320 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11321 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11322 }
11323 }
11324 else if (mips_big_got && !HAVE_NEWABI)
11325 {
11326 int gpdelay;
11327 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11328 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11329 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11330
11331 /* This is the large GOT case. If this is a reference to an
11332 external symbol, and there is no constant, we want
11333 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11334 addu $tempreg,$tempreg,$gp
11335 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11336 or for lca or if tempreg is PIC_CALL_REG
11337 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11338 addu $tempreg,$tempreg,$gp
11339 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11340 For a local symbol, we want
11341 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11342 nop
11343 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11344
11345 If we have a small constant, and this is a reference to
11346 an external symbol, we want
11347 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11348 addu $tempreg,$tempreg,$gp
11349 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11350 nop
11351 addiu $tempreg,$tempreg,<constant>
11352 For a local symbol, we want
11353 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11354 nop
11355 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11356
11357 If we have a large constant, and this is a reference to
11358 an external symbol, we want
11359 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11360 addu $tempreg,$tempreg,$gp
11361 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11362 lui $at,<hiconstant>
11363 addiu $at,$at,<loconstant>
11364 addu $tempreg,$tempreg,$at
11365 For a local symbol, we want
11366 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11367 lui $at,<hiconstant>
11368 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11369 addu $tempreg,$tempreg,$at
11370 */
11371
11372 expr1.X_add_number = offset_expr.X_add_number;
11373 offset_expr.X_add_number = 0;
11374 relax_start (offset_expr.X_add_symbol);
11375 gpdelay = reg_needs_delay (mips_gp_register);
11376 if (expr1.X_add_number == 0 && breg == 0
11377 && (call || tempreg == PIC_CALL_REG))
11378 {
11379 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11380 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11381 }
11382 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11383 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11384 tempreg, tempreg, mips_gp_register);
11385 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11386 tempreg, lw_reloc_type, tempreg);
11387 if (expr1.X_add_number == 0)
11388 {
11389 if (breg != 0)
11390 {
11391 /* We're going to put in an addu instruction using
11392 tempreg, so we may as well insert the nop right
11393 now. */
11394 load_delay_nop ();
11395 }
11396 }
11397 else if (expr1.X_add_number >= -0x8000
11398 && expr1.X_add_number < 0x8000)
11399 {
11400 load_delay_nop ();
11401 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11402 tempreg, tempreg, BFD_RELOC_LO16);
11403 }
11404 else
11405 {
11406 unsigned int dreg;
11407
11408 /* If we are going to add in a base register, and the
11409 target register and the base register are the same,
11410 then we are using AT as a temporary register. Since
11411 we want to load the constant into AT, we add our
11412 current AT (from the global offset table) and the
11413 register into the register now, and pretend we were
11414 not using a base register. */
11415 if (breg != op[0])
11416 dreg = tempreg;
11417 else
11418 {
11419 gas_assert (tempreg == AT);
11420 load_delay_nop ();
11421 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11422 op[0], AT, breg);
11423 dreg = op[0];
11424 }
11425
11426 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11427 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11428
11429 used_at = 1;
11430 }
11431 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
11432 relax_switch ();
11433
11434 if (gpdelay)
11435 {
11436 /* This is needed because this instruction uses $gp, but
11437 the first instruction on the main stream does not. */
11438 macro_build (NULL, "nop", "");
11439 }
11440
11441 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11442 local_reloc_type, mips_gp_register);
11443 if (expr1.X_add_number >= -0x8000
11444 && expr1.X_add_number < 0x8000)
11445 {
11446 load_delay_nop ();
11447 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11448 tempreg, tempreg, BFD_RELOC_LO16);
11449 /* FIXME: If add_number is 0, and there was no base
11450 register, the external symbol case ended with a load,
11451 so if the symbol turns out to not be external, and
11452 the next instruction uses tempreg, an unnecessary nop
11453 will be inserted. */
11454 }
11455 else
11456 {
11457 if (breg == op[0])
11458 {
11459 /* We must add in the base register now, as in the
11460 external symbol case. */
11461 gas_assert (tempreg == AT);
11462 load_delay_nop ();
11463 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11464 op[0], AT, breg);
11465 tempreg = op[0];
11466 /* We set breg to 0 because we have arranged to add
11467 it in in both cases. */
11468 breg = 0;
11469 }
11470
11471 macro_build_lui (&expr1, AT);
11472 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11473 AT, AT, BFD_RELOC_LO16);
11474 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11475 tempreg, tempreg, AT);
11476 used_at = 1;
11477 }
11478 relax_end ();
11479 }
11480 else if (mips_big_got && HAVE_NEWABI)
11481 {
11482 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11483 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11484 int add_breg_early = 0;
11485
11486 /* This is the large GOT case. If this is a reference to an
11487 external symbol, and there is no constant, we want
11488 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11489 add $tempreg,$tempreg,$gp
11490 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11491 or for lca or if tempreg is PIC_CALL_REG
11492 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11493 add $tempreg,$tempreg,$gp
11494 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11495
11496 If we have a small constant, and this is a reference to
11497 an external symbol, we want
11498 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11499 add $tempreg,$tempreg,$gp
11500 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11501 addi $tempreg,$tempreg,<constant>
11502
11503 If we have a large constant, and this is a reference to
11504 an external symbol, we want
11505 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11506 addu $tempreg,$tempreg,$gp
11507 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11508 lui $at,<hiconstant>
11509 addi $at,$at,<loconstant>
11510 add $tempreg,$tempreg,$at
11511
11512 If we have NewABI, and we know it's a local symbol, we want
11513 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11514 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11515 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11516
11517 relax_start (offset_expr.X_add_symbol);
11518
11519 expr1.X_add_number = offset_expr.X_add_number;
11520 offset_expr.X_add_number = 0;
11521
11522 if (expr1.X_add_number == 0 && breg == 0
11523 && (call || tempreg == PIC_CALL_REG))
11524 {
11525 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11526 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11527 }
11528 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11529 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11530 tempreg, tempreg, mips_gp_register);
11531 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11532 tempreg, lw_reloc_type, tempreg);
11533
11534 if (expr1.X_add_number == 0)
11535 ;
11536 else if (expr1.X_add_number >= -0x8000
11537 && expr1.X_add_number < 0x8000)
11538 {
11539 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11540 tempreg, tempreg, BFD_RELOC_LO16);
11541 }
11542 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11543 {
11544 unsigned int dreg;
11545
11546 /* If we are going to add in a base register, and the
11547 target register and the base register are the same,
11548 then we are using AT as a temporary register. Since
11549 we want to load the constant into AT, we add our
11550 current AT (from the global offset table) and the
11551 register into the register now, and pretend we were
11552 not using a base register. */
11553 if (breg != op[0])
11554 dreg = tempreg;
11555 else
11556 {
11557 gas_assert (tempreg == AT);
11558 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11559 op[0], AT, breg);
11560 dreg = op[0];
11561 add_breg_early = 1;
11562 }
11563
11564 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11565 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11566
11567 used_at = 1;
11568 }
11569 else
11570 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11571
11572 relax_switch ();
11573 offset_expr.X_add_number = expr1.X_add_number;
11574 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11575 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11576 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11577 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11578 if (add_breg_early)
11579 {
11580 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11581 op[0], tempreg, breg);
11582 breg = 0;
11583 tempreg = op[0];
11584 }
11585 relax_end ();
11586 }
11587 else
11588 abort ();
11589
11590 if (breg != 0)
11591 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
11592 break;
11593
11594 case M_MSGSND:
11595 gas_assert (!mips_opts.micromips);
11596 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
11597 break;
11598
11599 case M_MSGLD:
11600 gas_assert (!mips_opts.micromips);
11601 macro_build (NULL, "c2", "C", 0x02);
11602 break;
11603
11604 case M_MSGLD_T:
11605 gas_assert (!mips_opts.micromips);
11606 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
11607 break;
11608
11609 case M_MSGWAIT:
11610 gas_assert (!mips_opts.micromips);
11611 macro_build (NULL, "c2", "C", 3);
11612 break;
11613
11614 case M_MSGWAIT_T:
11615 gas_assert (!mips_opts.micromips);
11616 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
11617 break;
11618
11619 case M_J_A:
11620 /* The j instruction may not be used in PIC code, since it
11621 requires an absolute address. We convert it to a b
11622 instruction. */
11623 if (mips_pic == NO_PIC)
11624 macro_build (&offset_expr, "j", "a");
11625 else
11626 macro_build (&offset_expr, "b", "p");
11627 break;
11628
11629 /* The jal instructions must be handled as macros because when
11630 generating PIC code they expand to multi-instruction
11631 sequences. Normally they are simple instructions. */
11632 case M_JALS_1:
11633 op[1] = op[0];
11634 op[0] = RA;
11635 /* Fall through. */
11636 case M_JALS_2:
11637 gas_assert (mips_opts.micromips);
11638 if (mips_opts.insn32)
11639 {
11640 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11641 break;
11642 }
11643 jals = 1;
11644 goto jal;
11645 case M_JAL_1:
11646 op[1] = op[0];
11647 op[0] = RA;
11648 /* Fall through. */
11649 case M_JAL_2:
11650 jal:
11651 if (mips_pic == NO_PIC)
11652 {
11653 s = jals ? "jalrs" : "jalr";
11654 if (mips_opts.micromips
11655 && !mips_opts.insn32
11656 && op[0] == RA
11657 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11658 macro_build (NULL, s, "mj", op[1]);
11659 else
11660 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11661 }
11662 else
11663 {
11664 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11665 && mips_cprestore_offset >= 0);
11666
11667 if (op[1] != PIC_CALL_REG)
11668 as_warn (_("MIPS PIC call to register other than $25"));
11669
11670 s = ((mips_opts.micromips
11671 && !mips_opts.insn32
11672 && (!mips_opts.noreorder || cprestore))
11673 ? "jalrs" : "jalr");
11674 if (mips_opts.micromips
11675 && !mips_opts.insn32
11676 && op[0] == RA
11677 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11678 macro_build (NULL, s, "mj", op[1]);
11679 else
11680 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11681 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
11682 {
11683 if (mips_cprestore_offset < 0)
11684 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11685 else
11686 {
11687 if (!mips_frame_reg_valid)
11688 {
11689 as_warn (_("no .frame pseudo-op used in PIC code"));
11690 /* Quiet this warning. */
11691 mips_frame_reg_valid = 1;
11692 }
11693 if (!mips_cprestore_valid)
11694 {
11695 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11696 /* Quiet this warning. */
11697 mips_cprestore_valid = 1;
11698 }
11699 if (mips_opts.noreorder)
11700 macro_build (NULL, "nop", "");
11701 expr1.X_add_number = mips_cprestore_offset;
11702 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11703 mips_gp_register,
11704 mips_frame_reg,
11705 HAVE_64BIT_ADDRESSES);
11706 }
11707 }
11708 }
11709
11710 break;
11711
11712 case M_JALS_A:
11713 gas_assert (mips_opts.micromips);
11714 if (mips_opts.insn32)
11715 {
11716 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11717 break;
11718 }
11719 jals = 1;
11720 /* Fall through. */
11721 case M_JAL_A:
11722 if (mips_pic == NO_PIC)
11723 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
11724 else if (mips_pic == SVR4_PIC)
11725 {
11726 /* If this is a reference to an external symbol, and we are
11727 using a small GOT, we want
11728 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11729 nop
11730 jalr $ra,$25
11731 nop
11732 lw $gp,cprestore($sp)
11733 The cprestore value is set using the .cprestore
11734 pseudo-op. If we are using a big GOT, we want
11735 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11736 addu $25,$25,$gp
11737 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11738 nop
11739 jalr $ra,$25
11740 nop
11741 lw $gp,cprestore($sp)
11742 If the symbol is not external, we want
11743 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11744 nop
11745 addiu $25,$25,<sym> (BFD_RELOC_LO16)
11746 jalr $ra,$25
11747 nop
11748 lw $gp,cprestore($sp)
11749
11750 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11751 sequences above, minus nops, unless the symbol is local,
11752 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11753 GOT_DISP. */
11754 if (HAVE_NEWABI)
11755 {
11756 if (!mips_big_got)
11757 {
11758 relax_start (offset_expr.X_add_symbol);
11759 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11760 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11761 mips_gp_register);
11762 relax_switch ();
11763 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11764 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
11765 mips_gp_register);
11766 relax_end ();
11767 }
11768 else
11769 {
11770 relax_start (offset_expr.X_add_symbol);
11771 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11772 BFD_RELOC_MIPS_CALL_HI16);
11773 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11774 PIC_CALL_REG, mips_gp_register);
11775 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11776 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11777 PIC_CALL_REG);
11778 relax_switch ();
11779 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11780 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11781 mips_gp_register);
11782 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11783 PIC_CALL_REG, PIC_CALL_REG,
11784 BFD_RELOC_MIPS_GOT_OFST);
11785 relax_end ();
11786 }
11787
11788 macro_build_jalr (&offset_expr, 0);
11789 }
11790 else
11791 {
11792 relax_start (offset_expr.X_add_symbol);
11793 if (!mips_big_got)
11794 {
11795 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11796 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11797 mips_gp_register);
11798 load_delay_nop ();
11799 relax_switch ();
11800 }
11801 else
11802 {
11803 int gpdelay;
11804
11805 gpdelay = reg_needs_delay (mips_gp_register);
11806 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11807 BFD_RELOC_MIPS_CALL_HI16);
11808 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11809 PIC_CALL_REG, mips_gp_register);
11810 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11811 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11812 PIC_CALL_REG);
11813 load_delay_nop ();
11814 relax_switch ();
11815 if (gpdelay)
11816 macro_build (NULL, "nop", "");
11817 }
11818 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11819 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
11820 mips_gp_register);
11821 load_delay_nop ();
11822 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11823 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
11824 relax_end ();
11825 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
11826
11827 if (mips_cprestore_offset < 0)
11828 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11829 else
11830 {
11831 if (!mips_frame_reg_valid)
11832 {
11833 as_warn (_("no .frame pseudo-op used in PIC code"));
11834 /* Quiet this warning. */
11835 mips_frame_reg_valid = 1;
11836 }
11837 if (!mips_cprestore_valid)
11838 {
11839 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11840 /* Quiet this warning. */
11841 mips_cprestore_valid = 1;
11842 }
11843 if (mips_opts.noreorder)
11844 macro_build (NULL, "nop", "");
11845 expr1.X_add_number = mips_cprestore_offset;
11846 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11847 mips_gp_register,
11848 mips_frame_reg,
11849 HAVE_64BIT_ADDRESSES);
11850 }
11851 }
11852 }
11853 else if (mips_pic == VXWORKS_PIC)
11854 as_bad (_("non-PIC jump used in PIC library"));
11855 else
11856 abort ();
11857
11858 break;
11859
11860 case M_LBUE_AB:
11861 s = "lbue";
11862 fmt = "t,+j(b)";
11863 offbits = 9;
11864 goto ld_st;
11865 case M_LHUE_AB:
11866 s = "lhue";
11867 fmt = "t,+j(b)";
11868 offbits = 9;
11869 goto ld_st;
11870 case M_LBE_AB:
11871 s = "lbe";
11872 fmt = "t,+j(b)";
11873 offbits = 9;
11874 goto ld_st;
11875 case M_LHE_AB:
11876 s = "lhe";
11877 fmt = "t,+j(b)";
11878 offbits = 9;
11879 goto ld_st;
11880 case M_LLE_AB:
11881 s = "lle";
11882 fmt = "t,+j(b)";
11883 offbits = 9;
11884 goto ld_st;
11885 case M_LWE_AB:
11886 s = "lwe";
11887 fmt = "t,+j(b)";
11888 offbits = 9;
11889 goto ld_st;
11890 case M_LWLE_AB:
11891 s = "lwle";
11892 fmt = "t,+j(b)";
11893 offbits = 9;
11894 goto ld_st;
11895 case M_LWRE_AB:
11896 s = "lwre";
11897 fmt = "t,+j(b)";
11898 offbits = 9;
11899 goto ld_st;
11900 case M_SBE_AB:
11901 s = "sbe";
11902 fmt = "t,+j(b)";
11903 offbits = 9;
11904 goto ld_st;
11905 case M_SCE_AB:
11906 s = "sce";
11907 fmt = "t,+j(b)";
11908 offbits = 9;
11909 goto ld_st;
11910 case M_SHE_AB:
11911 s = "she";
11912 fmt = "t,+j(b)";
11913 offbits = 9;
11914 goto ld_st;
11915 case M_SWE_AB:
11916 s = "swe";
11917 fmt = "t,+j(b)";
11918 offbits = 9;
11919 goto ld_st;
11920 case M_SWLE_AB:
11921 s = "swle";
11922 fmt = "t,+j(b)";
11923 offbits = 9;
11924 goto ld_st;
11925 case M_SWRE_AB:
11926 s = "swre";
11927 fmt = "t,+j(b)";
11928 offbits = 9;
11929 goto ld_st;
11930 case M_ACLR_AB:
11931 s = "aclr";
11932 fmt = "\\,~(b)";
11933 offbits = 12;
11934 goto ld_st;
11935 case M_ASET_AB:
11936 s = "aset";
11937 fmt = "\\,~(b)";
11938 offbits = 12;
11939 goto ld_st;
11940 case M_LB_AB:
11941 s = "lb";
11942 fmt = "t,o(b)";
11943 goto ld;
11944 case M_LBU_AB:
11945 s = "lbu";
11946 fmt = "t,o(b)";
11947 goto ld;
11948 case M_LH_AB:
11949 s = "lh";
11950 fmt = "t,o(b)";
11951 goto ld;
11952 case M_LHU_AB:
11953 s = "lhu";
11954 fmt = "t,o(b)";
11955 goto ld;
11956 case M_LW_AB:
11957 s = "lw";
11958 fmt = "t,o(b)";
11959 goto ld;
11960 case M_LWC0_AB:
11961 gas_assert (!mips_opts.micromips);
11962 s = "lwc0";
11963 fmt = "E,o(b)";
11964 /* Itbl support may require additional care here. */
11965 coproc = 1;
11966 goto ld_st;
11967 case M_LWC1_AB:
11968 s = "lwc1";
11969 fmt = "T,o(b)";
11970 /* Itbl support may require additional care here. */
11971 coproc = 1;
11972 goto ld_st;
11973 case M_LWC2_AB:
11974 s = "lwc2";
11975 fmt = COP12_FMT;
11976 offbits = (mips_opts.micromips ? 12
11977 : ISA_IS_R6 (mips_opts.isa) ? 11
11978 : 16);
11979 /* Itbl support may require additional care here. */
11980 coproc = 1;
11981 goto ld_st;
11982 case M_LWC3_AB:
11983 gas_assert (!mips_opts.micromips);
11984 s = "lwc3";
11985 fmt = "E,o(b)";
11986 /* Itbl support may require additional care here. */
11987 coproc = 1;
11988 goto ld_st;
11989 case M_LWL_AB:
11990 s = "lwl";
11991 fmt = MEM12_FMT;
11992 offbits = (mips_opts.micromips ? 12 : 16);
11993 goto ld_st;
11994 case M_LWR_AB:
11995 s = "lwr";
11996 fmt = MEM12_FMT;
11997 offbits = (mips_opts.micromips ? 12 : 16);
11998 goto ld_st;
11999 case M_LDC1_AB:
12000 s = "ldc1";
12001 fmt = "T,o(b)";
12002 /* Itbl support may require additional care here. */
12003 coproc = 1;
12004 goto ld_st;
12005 case M_LDC2_AB:
12006 s = "ldc2";
12007 fmt = COP12_FMT;
12008 offbits = (mips_opts.micromips ? 12
12009 : ISA_IS_R6 (mips_opts.isa) ? 11
12010 : 16);
12011 /* Itbl support may require additional care here. */
12012 coproc = 1;
12013 goto ld_st;
12014 case M_LQC2_AB:
12015 s = "lqc2";
12016 fmt = "+7,o(b)";
12017 /* Itbl support may require additional care here. */
12018 coproc = 1;
12019 goto ld_st;
12020 case M_LDC3_AB:
12021 s = "ldc3";
12022 fmt = "E,o(b)";
12023 /* Itbl support may require additional care here. */
12024 coproc = 1;
12025 goto ld_st;
12026 case M_LDL_AB:
12027 s = "ldl";
12028 fmt = MEM12_FMT;
12029 offbits = (mips_opts.micromips ? 12 : 16);
12030 goto ld_st;
12031 case M_LDR_AB:
12032 s = "ldr";
12033 fmt = MEM12_FMT;
12034 offbits = (mips_opts.micromips ? 12 : 16);
12035 goto ld_st;
12036 case M_LL_AB:
12037 s = "ll";
12038 fmt = LL_SC_FMT;
12039 offbits = (mips_opts.micromips ? 12
12040 : ISA_IS_R6 (mips_opts.isa) ? 9
12041 : 16);
12042 goto ld;
12043 case M_LLD_AB:
12044 s = "lld";
12045 fmt = LL_SC_FMT;
12046 offbits = (mips_opts.micromips ? 12
12047 : ISA_IS_R6 (mips_opts.isa) ? 9
12048 : 16);
12049 goto ld;
12050 case M_LWU_AB:
12051 s = "lwu";
12052 fmt = MEM12_FMT;
12053 offbits = (mips_opts.micromips ? 12 : 16);
12054 goto ld;
12055 case M_LWP_AB:
12056 gas_assert (mips_opts.micromips);
12057 s = "lwp";
12058 fmt = "t,~(b)";
12059 offbits = 12;
12060 lp = 1;
12061 goto ld;
12062 case M_LDP_AB:
12063 gas_assert (mips_opts.micromips);
12064 s = "ldp";
12065 fmt = "t,~(b)";
12066 offbits = 12;
12067 lp = 1;
12068 goto ld;
12069 case M_LLDP_AB:
12070 case M_LLWP_AB:
12071 case M_LLWPE_AB:
12072 s = ip->insn_mo->name;
12073 fmt = "t,d,s";
12074 ll_sc_paired = 1;
12075 offbits = 0;
12076 goto ld;
12077 case M_LWM_AB:
12078 gas_assert (mips_opts.micromips);
12079 s = "lwm";
12080 fmt = "n,~(b)";
12081 offbits = 12;
12082 goto ld_st;
12083 case M_LDM_AB:
12084 gas_assert (mips_opts.micromips);
12085 s = "ldm";
12086 fmt = "n,~(b)";
12087 offbits = 12;
12088 goto ld_st;
12089
12090 ld:
12091 /* Try to use one the the load registers to compute the base address.
12092 We don't want to use $0 as tempreg. */
12093 if (ll_sc_paired)
12094 {
12095 if ((op[0] == ZERO && op[3] == op[1])
12096 || (op[1] == ZERO && op[3] == op[0])
12097 || (op[0] == ZERO && op[1] == ZERO))
12098 goto ld_st;
12099 else if (op[0] != op[3] && op[0] != ZERO)
12100 tempreg = op[0];
12101 else
12102 tempreg = op[1];
12103 }
12104 else
12105 {
12106 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
12107 goto ld_st;
12108 else
12109 tempreg = op[0] + lp;
12110 }
12111 goto ld_noat;
12112
12113 case M_SB_AB:
12114 s = "sb";
12115 fmt = "t,o(b)";
12116 goto ld_st;
12117 case M_SH_AB:
12118 s = "sh";
12119 fmt = "t,o(b)";
12120 goto ld_st;
12121 case M_SW_AB:
12122 s = "sw";
12123 fmt = "t,o(b)";
12124 goto ld_st;
12125 case M_SWC0_AB:
12126 gas_assert (!mips_opts.micromips);
12127 s = "swc0";
12128 fmt = "E,o(b)";
12129 /* Itbl support may require additional care here. */
12130 coproc = 1;
12131 goto ld_st;
12132 case M_SWC1_AB:
12133 s = "swc1";
12134 fmt = "T,o(b)";
12135 /* Itbl support may require additional care here. */
12136 coproc = 1;
12137 goto ld_st;
12138 case M_SWC2_AB:
12139 s = "swc2";
12140 fmt = COP12_FMT;
12141 offbits = (mips_opts.micromips ? 12
12142 : ISA_IS_R6 (mips_opts.isa) ? 11
12143 : 16);
12144 /* Itbl support may require additional care here. */
12145 coproc = 1;
12146 goto ld_st;
12147 case M_SWC3_AB:
12148 gas_assert (!mips_opts.micromips);
12149 s = "swc3";
12150 fmt = "E,o(b)";
12151 /* Itbl support may require additional care here. */
12152 coproc = 1;
12153 goto ld_st;
12154 case M_SWL_AB:
12155 s = "swl";
12156 fmt = MEM12_FMT;
12157 offbits = (mips_opts.micromips ? 12 : 16);
12158 goto ld_st;
12159 case M_SWR_AB:
12160 s = "swr";
12161 fmt = MEM12_FMT;
12162 offbits = (mips_opts.micromips ? 12 : 16);
12163 goto ld_st;
12164 case M_SC_AB:
12165 s = "sc";
12166 fmt = LL_SC_FMT;
12167 offbits = (mips_opts.micromips ? 12
12168 : ISA_IS_R6 (mips_opts.isa) ? 9
12169 : 16);
12170 goto ld_st;
12171 case M_SCD_AB:
12172 s = "scd";
12173 fmt = LL_SC_FMT;
12174 offbits = (mips_opts.micromips ? 12
12175 : ISA_IS_R6 (mips_opts.isa) ? 9
12176 : 16);
12177 goto ld_st;
12178 case M_SCDP_AB:
12179 case M_SCWP_AB:
12180 case M_SCWPE_AB:
12181 s = ip->insn_mo->name;
12182 fmt = "t,d,s";
12183 ll_sc_paired = 1;
12184 offbits = 0;
12185 goto ld_st;
12186 case M_CACHE_AB:
12187 s = "cache";
12188 fmt = (mips_opts.micromips ? "k,~(b)"
12189 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12190 : "k,o(b)");
12191 offbits = (mips_opts.micromips ? 12
12192 : ISA_IS_R6 (mips_opts.isa) ? 9
12193 : 16);
12194 goto ld_st;
12195 case M_CACHEE_AB:
12196 s = "cachee";
12197 fmt = "k,+j(b)";
12198 offbits = 9;
12199 goto ld_st;
12200 case M_PREF_AB:
12201 s = "pref";
12202 fmt = (mips_opts.micromips ? "k,~(b)"
12203 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12204 : "k,o(b)");
12205 offbits = (mips_opts.micromips ? 12
12206 : ISA_IS_R6 (mips_opts.isa) ? 9
12207 : 16);
12208 goto ld_st;
12209 case M_PREFE_AB:
12210 s = "prefe";
12211 fmt = "k,+j(b)";
12212 offbits = 9;
12213 goto ld_st;
12214 case M_SDC1_AB:
12215 s = "sdc1";
12216 fmt = "T,o(b)";
12217 coproc = 1;
12218 /* Itbl support may require additional care here. */
12219 goto ld_st;
12220 case M_SDC2_AB:
12221 s = "sdc2";
12222 fmt = COP12_FMT;
12223 offbits = (mips_opts.micromips ? 12
12224 : ISA_IS_R6 (mips_opts.isa) ? 11
12225 : 16);
12226 /* Itbl support may require additional care here. */
12227 coproc = 1;
12228 goto ld_st;
12229 case M_SQC2_AB:
12230 s = "sqc2";
12231 fmt = "+7,o(b)";
12232 /* Itbl support may require additional care here. */
12233 coproc = 1;
12234 goto ld_st;
12235 case M_SDC3_AB:
12236 gas_assert (!mips_opts.micromips);
12237 s = "sdc3";
12238 fmt = "E,o(b)";
12239 /* Itbl support may require additional care here. */
12240 coproc = 1;
12241 goto ld_st;
12242 case M_SDL_AB:
12243 s = "sdl";
12244 fmt = MEM12_FMT;
12245 offbits = (mips_opts.micromips ? 12 : 16);
12246 goto ld_st;
12247 case M_SDR_AB:
12248 s = "sdr";
12249 fmt = MEM12_FMT;
12250 offbits = (mips_opts.micromips ? 12 : 16);
12251 goto ld_st;
12252 case M_SWP_AB:
12253 gas_assert (mips_opts.micromips);
12254 s = "swp";
12255 fmt = "t,~(b)";
12256 offbits = 12;
12257 goto ld_st;
12258 case M_SDP_AB:
12259 gas_assert (mips_opts.micromips);
12260 s = "sdp";
12261 fmt = "t,~(b)";
12262 offbits = 12;
12263 goto ld_st;
12264 case M_SWM_AB:
12265 gas_assert (mips_opts.micromips);
12266 s = "swm";
12267 fmt = "n,~(b)";
12268 offbits = 12;
12269 goto ld_st;
12270 case M_SDM_AB:
12271 gas_assert (mips_opts.micromips);
12272 s = "sdm";
12273 fmt = "n,~(b)";
12274 offbits = 12;
12275
12276 ld_st:
12277 tempreg = AT;
12278 ld_noat:
12279 breg = ll_sc_paired ? op[3] : op[2];
12280 if (small_offset_p (0, align, 16))
12281 {
12282 /* The first case exists for M_LD_AB and M_SD_AB, which are
12283 macros for o32 but which should act like normal instructions
12284 otherwise. */
12285 if (offbits == 16)
12286 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
12287 offset_reloc[1], offset_reloc[2], breg);
12288 else if (small_offset_p (0, align, offbits))
12289 {
12290 if (offbits == 0)
12291 {
12292 if (ll_sc_paired)
12293 macro_build (NULL, s, fmt, op[0], op[1], breg);
12294 else
12295 macro_build (NULL, s, fmt, op[0], breg);
12296 }
12297 else
12298 macro_build (NULL, s, fmt, op[0],
12299 (int) offset_expr.X_add_number, breg);
12300 }
12301 else
12302 {
12303 if (tempreg == AT)
12304 used_at = 1;
12305 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12306 tempreg, breg, -1, offset_reloc[0],
12307 offset_reloc[1], offset_reloc[2]);
12308 if (offbits == 0)
12309 {
12310 if (ll_sc_paired)
12311 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12312 else
12313 macro_build (NULL, s, fmt, op[0], tempreg);
12314 }
12315 else
12316 macro_build (NULL, s, fmt, op[0], 0, tempreg);
12317 }
12318 break;
12319 }
12320
12321 if (tempreg == AT)
12322 used_at = 1;
12323
12324 if (offset_expr.X_op != O_constant
12325 && offset_expr.X_op != O_symbol)
12326 {
12327 as_bad (_("expression too complex"));
12328 offset_expr.X_op = O_constant;
12329 }
12330
12331 if (HAVE_32BIT_ADDRESSES
12332 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12333 {
12334 char value [32];
12335
12336 sprintf_vma (value, offset_expr.X_add_number);
12337 as_bad (_("number (0x%s) larger than 32 bits"), value);
12338 }
12339
12340 /* A constant expression in PIC code can be handled just as it
12341 is in non PIC code. */
12342 if (offset_expr.X_op == O_constant)
12343 {
12344 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12345 offbits == 0 ? 16 : offbits);
12346 offset_expr.X_add_number -= expr1.X_add_number;
12347
12348 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12349 if (breg != 0)
12350 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12351 tempreg, tempreg, breg);
12352 if (offbits == 0)
12353 {
12354 if (offset_expr.X_add_number != 0)
12355 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
12356 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
12357 if (ll_sc_paired)
12358 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12359 else
12360 macro_build (NULL, s, fmt, op[0], tempreg);
12361 }
12362 else if (offbits == 16)
12363 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12364 else
12365 macro_build (NULL, s, fmt, op[0],
12366 (int) offset_expr.X_add_number, tempreg);
12367 }
12368 else if (offbits != 16)
12369 {
12370 /* The offset field is too narrow to be used for a low-part
12371 relocation, so load the whole address into the auxiliary
12372 register. */
12373 load_address (tempreg, &offset_expr, &used_at);
12374 if (breg != 0)
12375 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12376 tempreg, tempreg, breg);
12377 if (offbits == 0)
12378 {
12379 if (ll_sc_paired)
12380 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12381 else
12382 macro_build (NULL, s, fmt, op[0], tempreg);
12383 }
12384 else
12385 macro_build (NULL, s, fmt, op[0], 0, tempreg);
12386 }
12387 else if (mips_pic == NO_PIC)
12388 {
12389 /* If this is a reference to a GP relative symbol, and there
12390 is no base register, we want
12391 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12392 Otherwise, if there is no base register, we want
12393 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12394 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12395 If we have a constant, we need two instructions anyhow,
12396 so we always use the latter form.
12397
12398 If we have a base register, and this is a reference to a
12399 GP relative symbol, we want
12400 addu $tempreg,$breg,$gp
12401 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
12402 Otherwise we want
12403 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12404 addu $tempreg,$tempreg,$breg
12405 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12406 With a constant we always use the latter case.
12407
12408 With 64bit address space and no base register and $at usable,
12409 we want
12410 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12411 lui $at,<sym> (BFD_RELOC_HI16_S)
12412 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12413 dsll32 $tempreg,0
12414 daddu $tempreg,$at
12415 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12416 If we have a base register, we want
12417 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12418 lui $at,<sym> (BFD_RELOC_HI16_S)
12419 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12420 daddu $at,$breg
12421 dsll32 $tempreg,0
12422 daddu $tempreg,$at
12423 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12424
12425 Without $at we can't generate the optimal path for superscalar
12426 processors here since this would require two temporary registers.
12427 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12428 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12429 dsll $tempreg,16
12430 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12431 dsll $tempreg,16
12432 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12433 If we have a base register, we want
12434 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12435 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12436 dsll $tempreg,16
12437 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12438 dsll $tempreg,16
12439 daddu $tempreg,$tempreg,$breg
12440 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12441
12442 For GP relative symbols in 64bit address space we can use
12443 the same sequence as in 32bit address space. */
12444 if (HAVE_64BIT_SYMBOLS)
12445 {
12446 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12447 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12448 {
12449 relax_start (offset_expr.X_add_symbol);
12450 if (breg == 0)
12451 {
12452 macro_build (&offset_expr, s, fmt, op[0],
12453 BFD_RELOC_GPREL16, mips_gp_register);
12454 }
12455 else
12456 {
12457 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12458 tempreg, breg, mips_gp_register);
12459 macro_build (&offset_expr, s, fmt, op[0],
12460 BFD_RELOC_GPREL16, tempreg);
12461 }
12462 relax_switch ();
12463 }
12464
12465 if (used_at == 0 && mips_opts.at)
12466 {
12467 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12468 BFD_RELOC_MIPS_HIGHEST);
12469 macro_build (&offset_expr, "lui", LUI_FMT, AT,
12470 BFD_RELOC_HI16_S);
12471 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12472 tempreg, BFD_RELOC_MIPS_HIGHER);
12473 if (breg != 0)
12474 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
12475 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
12476 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
12477 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
12478 tempreg);
12479 used_at = 1;
12480 }
12481 else
12482 {
12483 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12484 BFD_RELOC_MIPS_HIGHEST);
12485 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12486 tempreg, BFD_RELOC_MIPS_HIGHER);
12487 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12488 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12489 tempreg, BFD_RELOC_HI16_S);
12490 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12491 if (breg != 0)
12492 macro_build (NULL, "daddu", "d,v,t",
12493 tempreg, tempreg, breg);
12494 macro_build (&offset_expr, s, fmt, op[0],
12495 BFD_RELOC_LO16, tempreg);
12496 }
12497
12498 if (mips_relax.sequence)
12499 relax_end ();
12500 break;
12501 }
12502
12503 if (breg == 0)
12504 {
12505 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12506 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12507 {
12508 relax_start (offset_expr.X_add_symbol);
12509 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
12510 mips_gp_register);
12511 relax_switch ();
12512 }
12513 macro_build_lui (&offset_expr, tempreg);
12514 macro_build (&offset_expr, s, fmt, op[0],
12515 BFD_RELOC_LO16, tempreg);
12516 if (mips_relax.sequence)
12517 relax_end ();
12518 }
12519 else
12520 {
12521 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12522 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12523 {
12524 relax_start (offset_expr.X_add_symbol);
12525 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12526 tempreg, breg, mips_gp_register);
12527 macro_build (&offset_expr, s, fmt, op[0],
12528 BFD_RELOC_GPREL16, tempreg);
12529 relax_switch ();
12530 }
12531 macro_build_lui (&offset_expr, tempreg);
12532 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12533 tempreg, tempreg, breg);
12534 macro_build (&offset_expr, s, fmt, op[0],
12535 BFD_RELOC_LO16, tempreg);
12536 if (mips_relax.sequence)
12537 relax_end ();
12538 }
12539 }
12540 else if (!mips_big_got)
12541 {
12542 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
12543
12544 /* If this is a reference to an external symbol, we want
12545 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12546 nop
12547 <op> op[0],0($tempreg)
12548 Otherwise we want
12549 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12550 nop
12551 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12552 <op> op[0],0($tempreg)
12553
12554 For NewABI, we want
12555 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12556 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
12557
12558 If there is a base register, we add it to $tempreg before
12559 the <op>. If there is a constant, we stick it in the
12560 <op> instruction. We don't handle constants larger than
12561 16 bits, because we have no way to load the upper 16 bits
12562 (actually, we could handle them for the subset of cases
12563 in which we are not using $at). */
12564 gas_assert (offset_expr.X_op == O_symbol);
12565 if (HAVE_NEWABI)
12566 {
12567 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12568 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12569 if (breg != 0)
12570 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12571 tempreg, tempreg, breg);
12572 macro_build (&offset_expr, s, fmt, op[0],
12573 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12574 break;
12575 }
12576 expr1.X_add_number = offset_expr.X_add_number;
12577 offset_expr.X_add_number = 0;
12578 if (expr1.X_add_number < -0x8000
12579 || expr1.X_add_number >= 0x8000)
12580 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12581 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12582 lw_reloc_type, mips_gp_register);
12583 load_delay_nop ();
12584 relax_start (offset_expr.X_add_symbol);
12585 relax_switch ();
12586 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12587 tempreg, BFD_RELOC_LO16);
12588 relax_end ();
12589 if (breg != 0)
12590 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12591 tempreg, tempreg, breg);
12592 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12593 }
12594 else if (mips_big_got && !HAVE_NEWABI)
12595 {
12596 int gpdelay;
12597
12598 /* If this is a reference to an external symbol, we want
12599 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12600 addu $tempreg,$tempreg,$gp
12601 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12602 <op> op[0],0($tempreg)
12603 Otherwise we want
12604 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12605 nop
12606 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12607 <op> op[0],0($tempreg)
12608 If there is a base register, we add it to $tempreg before
12609 the <op>. If there is a constant, we stick it in the
12610 <op> instruction. We don't handle constants larger than
12611 16 bits, because we have no way to load the upper 16 bits
12612 (actually, we could handle them for the subset of cases
12613 in which we are not using $at). */
12614 gas_assert (offset_expr.X_op == O_symbol);
12615 expr1.X_add_number = offset_expr.X_add_number;
12616 offset_expr.X_add_number = 0;
12617 if (expr1.X_add_number < -0x8000
12618 || expr1.X_add_number >= 0x8000)
12619 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12620 gpdelay = reg_needs_delay (mips_gp_register);
12621 relax_start (offset_expr.X_add_symbol);
12622 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12623 BFD_RELOC_MIPS_GOT_HI16);
12624 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12625 mips_gp_register);
12626 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12627 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12628 relax_switch ();
12629 if (gpdelay)
12630 macro_build (NULL, "nop", "");
12631 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12632 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12633 load_delay_nop ();
12634 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12635 tempreg, BFD_RELOC_LO16);
12636 relax_end ();
12637
12638 if (breg != 0)
12639 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12640 tempreg, tempreg, breg);
12641 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12642 }
12643 else if (mips_big_got && HAVE_NEWABI)
12644 {
12645 /* If this is a reference to an external symbol, we want
12646 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12647 add $tempreg,$tempreg,$gp
12648 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12649 <op> op[0],<ofst>($tempreg)
12650 Otherwise, for local symbols, we want:
12651 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12652 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
12653 gas_assert (offset_expr.X_op == O_symbol);
12654 expr1.X_add_number = offset_expr.X_add_number;
12655 offset_expr.X_add_number = 0;
12656 if (expr1.X_add_number < -0x8000
12657 || expr1.X_add_number >= 0x8000)
12658 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12659 relax_start (offset_expr.X_add_symbol);
12660 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12661 BFD_RELOC_MIPS_GOT_HI16);
12662 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12663 mips_gp_register);
12664 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12665 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12666 if (breg != 0)
12667 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12668 tempreg, tempreg, breg);
12669 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12670
12671 relax_switch ();
12672 offset_expr.X_add_number = expr1.X_add_number;
12673 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12674 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12675 if (breg != 0)
12676 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12677 tempreg, tempreg, breg);
12678 macro_build (&offset_expr, s, fmt, op[0],
12679 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12680 relax_end ();
12681 }
12682 else
12683 abort ();
12684
12685 break;
12686
12687 case M_JRADDIUSP:
12688 gas_assert (mips_opts.micromips);
12689 gas_assert (mips_opts.insn32);
12690 start_noreorder ();
12691 macro_build (NULL, "jr", "s", RA);
12692 expr1.X_add_number = op[0] << 2;
12693 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12694 end_noreorder ();
12695 break;
12696
12697 case M_JRC:
12698 gas_assert (mips_opts.micromips);
12699 gas_assert (mips_opts.insn32);
12700 macro_build (NULL, "jr", "s", op[0]);
12701 if (mips_opts.noreorder)
12702 macro_build (NULL, "nop", "");
12703 break;
12704
12705 case M_LI:
12706 case M_LI_S:
12707 load_register (op[0], &imm_expr, 0);
12708 break;
12709
12710 case M_DLI:
12711 load_register (op[0], &imm_expr, 1);
12712 break;
12713
12714 case M_LI_SS:
12715 if (imm_expr.X_op == O_constant)
12716 {
12717 used_at = 1;
12718 load_register (AT, &imm_expr, 0);
12719 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12720 break;
12721 }
12722 else
12723 {
12724 gas_assert (imm_expr.X_op == O_absent
12725 && offset_expr.X_op == O_symbol
12726 && strcmp (segment_name (S_GET_SEGMENT
12727 (offset_expr.X_add_symbol)),
12728 ".lit4") == 0
12729 && offset_expr.X_add_number == 0);
12730 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
12731 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
12732 break;
12733 }
12734
12735 case M_LI_D:
12736 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12737 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12738 order 32 bits of the value and the low order 32 bits are either
12739 zero or in OFFSET_EXPR. */
12740 if (imm_expr.X_op == O_constant)
12741 {
12742 if (GPR_SIZE == 64)
12743 load_register (op[0], &imm_expr, 1);
12744 else
12745 {
12746 int hreg, lreg;
12747
12748 if (target_big_endian)
12749 {
12750 hreg = op[0];
12751 lreg = op[0] + 1;
12752 }
12753 else
12754 {
12755 hreg = op[0] + 1;
12756 lreg = op[0];
12757 }
12758
12759 if (hreg <= 31)
12760 load_register (hreg, &imm_expr, 0);
12761 if (lreg <= 31)
12762 {
12763 if (offset_expr.X_op == O_absent)
12764 move_register (lreg, 0);
12765 else
12766 {
12767 gas_assert (offset_expr.X_op == O_constant);
12768 load_register (lreg, &offset_expr, 0);
12769 }
12770 }
12771 }
12772 break;
12773 }
12774 gas_assert (imm_expr.X_op == O_absent);
12775
12776 /* We know that sym is in the .rdata section. First we get the
12777 upper 16 bits of the address. */
12778 if (mips_pic == NO_PIC)
12779 {
12780 macro_build_lui (&offset_expr, AT);
12781 used_at = 1;
12782 }
12783 else
12784 {
12785 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12786 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12787 used_at = 1;
12788 }
12789
12790 /* Now we load the register(s). */
12791 if (GPR_SIZE == 64)
12792 {
12793 used_at = 1;
12794 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12795 BFD_RELOC_LO16, AT);
12796 }
12797 else
12798 {
12799 used_at = 1;
12800 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12801 BFD_RELOC_LO16, AT);
12802 if (op[0] != RA)
12803 {
12804 /* FIXME: How in the world do we deal with the possible
12805 overflow here? */
12806 offset_expr.X_add_number += 4;
12807 macro_build (&offset_expr, "lw", "t,o(b)",
12808 op[0] + 1, BFD_RELOC_LO16, AT);
12809 }
12810 }
12811 break;
12812
12813 case M_LI_DD:
12814 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12815 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12816 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12817 the value and the low order 32 bits are either zero or in
12818 OFFSET_EXPR. */
12819 if (imm_expr.X_op == O_constant)
12820 {
12821 tempreg = ZERO;
12822 if (((FPR_SIZE == 64 && GPR_SIZE == 64)
12823 || !ISA_HAS_MXHC1 (mips_opts.isa))
12824 && imm_expr.X_add_number != 0)
12825 {
12826 used_at = 1;
12827 tempreg = AT;
12828 load_register (AT, &imm_expr, FPR_SIZE == 64);
12829 }
12830 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12831 macro_build (NULL, "dmtc1", "t,S", tempreg, op[0]);
12832 else
12833 {
12834 if (!ISA_HAS_MXHC1 (mips_opts.isa))
12835 {
12836 if (FPR_SIZE != 32)
12837 as_bad (_("Unable to generate `%s' compliant code "
12838 "without mthc1"),
12839 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12840 else
12841 macro_build (NULL, "mtc1", "t,G", tempreg, op[0] + 1);
12842 }
12843 if (offset_expr.X_op == O_absent)
12844 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
12845 else
12846 {
12847 gas_assert (offset_expr.X_op == O_constant);
12848 load_register (AT, &offset_expr, 0);
12849 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12850 }
12851 if (ISA_HAS_MXHC1 (mips_opts.isa))
12852 {
12853 if (imm_expr.X_add_number != 0)
12854 {
12855 used_at = 1;
12856 tempreg = AT;
12857 load_register (AT, &imm_expr, 0);
12858 }
12859 macro_build (NULL, "mthc1", "t,G", tempreg, op[0]);
12860 }
12861 }
12862 break;
12863 }
12864
12865 gas_assert (imm_expr.X_op == O_absent
12866 && offset_expr.X_op == O_symbol
12867 && offset_expr.X_add_number == 0);
12868 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12869 if (strcmp (s, ".lit8") == 0)
12870 {
12871 op[2] = mips_gp_register;
12872 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12873 offset_reloc[1] = BFD_RELOC_UNUSED;
12874 offset_reloc[2] = BFD_RELOC_UNUSED;
12875 }
12876 else
12877 {
12878 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
12879 used_at = 1;
12880 if (mips_pic != NO_PIC)
12881 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12882 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12883 else
12884 {
12885 /* FIXME: This won't work for a 64 bit address. */
12886 macro_build_lui (&offset_expr, AT);
12887 }
12888
12889 op[2] = AT;
12890 offset_reloc[0] = BFD_RELOC_LO16;
12891 offset_reloc[1] = BFD_RELOC_UNUSED;
12892 offset_reloc[2] = BFD_RELOC_UNUSED;
12893 }
12894 align = 8;
12895 /* Fall through. */
12896
12897 case M_L_DAB:
12898 /* The MIPS assembler seems to check for X_add_number not
12899 being double aligned and generating:
12900 lui at,%hi(foo+1)
12901 addu at,at,v1
12902 addiu at,at,%lo(foo+1)
12903 lwc1 f2,0(at)
12904 lwc1 f3,4(at)
12905 But, the resulting address is the same after relocation so why
12906 generate the extra instruction? */
12907 /* Itbl support may require additional care here. */
12908 coproc = 1;
12909 fmt = "T,o(b)";
12910 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12911 {
12912 s = "ldc1";
12913 goto ld_st;
12914 }
12915 s = "lwc1";
12916 goto ldd_std;
12917
12918 case M_S_DAB:
12919 gas_assert (!mips_opts.micromips);
12920 /* Itbl support may require additional care here. */
12921 coproc = 1;
12922 fmt = "T,o(b)";
12923 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12924 {
12925 s = "sdc1";
12926 goto ld_st;
12927 }
12928 s = "swc1";
12929 goto ldd_std;
12930
12931 case M_LQ_AB:
12932 fmt = "t,o(b)";
12933 s = "lq";
12934 goto ld;
12935
12936 case M_SQ_AB:
12937 fmt = "t,o(b)";
12938 s = "sq";
12939 goto ld_st;
12940
12941 case M_LD_AB:
12942 fmt = "t,o(b)";
12943 if (GPR_SIZE == 64)
12944 {
12945 s = "ld";
12946 goto ld;
12947 }
12948 s = "lw";
12949 goto ldd_std;
12950
12951 case M_SD_AB:
12952 fmt = "t,o(b)";
12953 if (GPR_SIZE == 64)
12954 {
12955 s = "sd";
12956 goto ld_st;
12957 }
12958 s = "sw";
12959
12960 ldd_std:
12961 /* Even on a big endian machine $fn comes before $fn+1. We have
12962 to adjust when loading from memory. We set coproc if we must
12963 load $fn+1 first. */
12964 /* Itbl support may require additional care here. */
12965 if (!target_big_endian)
12966 coproc = 0;
12967
12968 breg = op[2];
12969 if (small_offset_p (0, align, 16))
12970 {
12971 ep = &offset_expr;
12972 if (!small_offset_p (4, align, 16))
12973 {
12974 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12975 -1, offset_reloc[0], offset_reloc[1],
12976 offset_reloc[2]);
12977 expr1.X_add_number = 0;
12978 ep = &expr1;
12979 breg = AT;
12980 used_at = 1;
12981 offset_reloc[0] = BFD_RELOC_LO16;
12982 offset_reloc[1] = BFD_RELOC_UNUSED;
12983 offset_reloc[2] = BFD_RELOC_UNUSED;
12984 }
12985 if (strcmp (s, "lw") == 0 && op[0] == breg)
12986 {
12987 ep->X_add_number += 4;
12988 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
12989 offset_reloc[1], offset_reloc[2], breg);
12990 ep->X_add_number -= 4;
12991 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
12992 offset_reloc[1], offset_reloc[2], breg);
12993 }
12994 else
12995 {
12996 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
12997 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12998 breg);
12999 ep->X_add_number += 4;
13000 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
13001 offset_reloc[0], offset_reloc[1], offset_reloc[2],
13002 breg);
13003 }
13004 break;
13005 }
13006
13007 if (offset_expr.X_op != O_symbol
13008 && offset_expr.X_op != O_constant)
13009 {
13010 as_bad (_("expression too complex"));
13011 offset_expr.X_op = O_constant;
13012 }
13013
13014 if (HAVE_32BIT_ADDRESSES
13015 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
13016 {
13017 char value [32];
13018
13019 sprintf_vma (value, offset_expr.X_add_number);
13020 as_bad (_("number (0x%s) larger than 32 bits"), value);
13021 }
13022
13023 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
13024 {
13025 /* If this is a reference to a GP relative symbol, we want
13026 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
13027 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
13028 If we have a base register, we use this
13029 addu $at,$breg,$gp
13030 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
13031 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
13032 If this is not a GP relative symbol, we want
13033 lui $at,<sym> (BFD_RELOC_HI16_S)
13034 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13035 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
13036 If there is a base register, we add it to $at after the
13037 lui instruction. If there is a constant, we always use
13038 the last case. */
13039 if (offset_expr.X_op == O_symbol
13040 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
13041 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
13042 {
13043 relax_start (offset_expr.X_add_symbol);
13044 if (breg == 0)
13045 {
13046 tempreg = mips_gp_register;
13047 }
13048 else
13049 {
13050 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13051 AT, breg, mips_gp_register);
13052 tempreg = AT;
13053 used_at = 1;
13054 }
13055
13056 /* Itbl support may require additional care here. */
13057 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13058 BFD_RELOC_GPREL16, tempreg);
13059 offset_expr.X_add_number += 4;
13060
13061 /* Set mips_optimize to 2 to avoid inserting an
13062 undesired nop. */
13063 hold_mips_optimize = mips_optimize;
13064 mips_optimize = 2;
13065 /* Itbl support may require additional care here. */
13066 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13067 BFD_RELOC_GPREL16, tempreg);
13068 mips_optimize = hold_mips_optimize;
13069
13070 relax_switch ();
13071
13072 offset_expr.X_add_number -= 4;
13073 }
13074 used_at = 1;
13075 if (offset_high_part (offset_expr.X_add_number, 16)
13076 != offset_high_part (offset_expr.X_add_number + 4, 16))
13077 {
13078 load_address (AT, &offset_expr, &used_at);
13079 offset_expr.X_op = O_constant;
13080 offset_expr.X_add_number = 0;
13081 }
13082 else
13083 macro_build_lui (&offset_expr, AT);
13084 if (breg != 0)
13085 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13086 /* Itbl support may require additional care here. */
13087 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13088 BFD_RELOC_LO16, AT);
13089 /* FIXME: How do we handle overflow here? */
13090 offset_expr.X_add_number += 4;
13091 /* Itbl support may require additional care here. */
13092 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13093 BFD_RELOC_LO16, AT);
13094 if (mips_relax.sequence)
13095 relax_end ();
13096 }
13097 else if (!mips_big_got)
13098 {
13099 /* If this is a reference to an external symbol, we want
13100 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13101 nop
13102 <op> op[0],0($at)
13103 <op> op[0]+1,4($at)
13104 Otherwise we want
13105 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13106 nop
13107 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13108 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
13109 If there is a base register we add it to $at before the
13110 lwc1 instructions. If there is a constant we include it
13111 in the lwc1 instructions. */
13112 used_at = 1;
13113 expr1.X_add_number = offset_expr.X_add_number;
13114 if (expr1.X_add_number < -0x8000
13115 || expr1.X_add_number >= 0x8000 - 4)
13116 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
13117 load_got_offset (AT, &offset_expr);
13118 load_delay_nop ();
13119 if (breg != 0)
13120 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13121
13122 /* Set mips_optimize to 2 to avoid inserting an undesired
13123 nop. */
13124 hold_mips_optimize = mips_optimize;
13125 mips_optimize = 2;
13126
13127 /* Itbl support may require additional care here. */
13128 relax_start (offset_expr.X_add_symbol);
13129 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
13130 BFD_RELOC_LO16, AT);
13131 expr1.X_add_number += 4;
13132 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
13133 BFD_RELOC_LO16, AT);
13134 relax_switch ();
13135 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13136 BFD_RELOC_LO16, AT);
13137 offset_expr.X_add_number += 4;
13138 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13139 BFD_RELOC_LO16, AT);
13140 relax_end ();
13141
13142 mips_optimize = hold_mips_optimize;
13143 }
13144 else if (mips_big_got)
13145 {
13146 int gpdelay;
13147
13148 /* If this is a reference to an external symbol, we want
13149 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
13150 addu $at,$at,$gp
13151 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
13152 nop
13153 <op> op[0],0($at)
13154 <op> op[0]+1,4($at)
13155 Otherwise we want
13156 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13157 nop
13158 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13159 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
13160 If there is a base register we add it to $at before the
13161 lwc1 instructions. If there is a constant we include it
13162 in the lwc1 instructions. */
13163 used_at = 1;
13164 expr1.X_add_number = offset_expr.X_add_number;
13165 offset_expr.X_add_number = 0;
13166 if (expr1.X_add_number < -0x8000
13167 || expr1.X_add_number >= 0x8000 - 4)
13168 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
13169 gpdelay = reg_needs_delay (mips_gp_register);
13170 relax_start (offset_expr.X_add_symbol);
13171 macro_build (&offset_expr, "lui", LUI_FMT,
13172 AT, BFD_RELOC_MIPS_GOT_HI16);
13173 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13174 AT, AT, mips_gp_register);
13175 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
13176 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
13177 load_delay_nop ();
13178 if (breg != 0)
13179 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13180 /* Itbl support may require additional care here. */
13181 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
13182 BFD_RELOC_LO16, AT);
13183 expr1.X_add_number += 4;
13184
13185 /* Set mips_optimize to 2 to avoid inserting an undesired
13186 nop. */
13187 hold_mips_optimize = mips_optimize;
13188 mips_optimize = 2;
13189 /* Itbl support may require additional care here. */
13190 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
13191 BFD_RELOC_LO16, AT);
13192 mips_optimize = hold_mips_optimize;
13193 expr1.X_add_number -= 4;
13194
13195 relax_switch ();
13196 offset_expr.X_add_number = expr1.X_add_number;
13197 if (gpdelay)
13198 macro_build (NULL, "nop", "");
13199 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
13200 BFD_RELOC_MIPS_GOT16, mips_gp_register);
13201 load_delay_nop ();
13202 if (breg != 0)
13203 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13204 /* Itbl support may require additional care here. */
13205 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13206 BFD_RELOC_LO16, AT);
13207 offset_expr.X_add_number += 4;
13208
13209 /* Set mips_optimize to 2 to avoid inserting an undesired
13210 nop. */
13211 hold_mips_optimize = mips_optimize;
13212 mips_optimize = 2;
13213 /* Itbl support may require additional care here. */
13214 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13215 BFD_RELOC_LO16, AT);
13216 mips_optimize = hold_mips_optimize;
13217 relax_end ();
13218 }
13219 else
13220 abort ();
13221
13222 break;
13223
13224 case M_SAA_AB:
13225 s = "saa";
13226 goto saa_saad;
13227 case M_SAAD_AB:
13228 s = "saad";
13229 saa_saad:
13230 gas_assert (!mips_opts.micromips);
13231 offbits = 0;
13232 fmt = "t,(b)";
13233 goto ld_st;
13234
13235 /* New code added to support COPZ instructions.
13236 This code builds table entries out of the macros in mip_opcodes.
13237 R4000 uses interlocks to handle coproc delays.
13238 Other chips (like the R3000) require nops to be inserted for delays.
13239
13240 FIXME: Currently, we require that the user handle delays.
13241 In order to fill delay slots for non-interlocked chips,
13242 we must have a way to specify delays based on the coprocessor.
13243 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
13244 What are the side-effects of the cop instruction?
13245 What cache support might we have and what are its effects?
13246 Both coprocessor & memory require delays. how long???
13247 What registers are read/set/modified?
13248
13249 If an itbl is provided to interpret cop instructions,
13250 this knowledge can be encoded in the itbl spec. */
13251
13252 case M_COP0:
13253 s = "c0";
13254 goto copz;
13255 case M_COP1:
13256 s = "c1";
13257 goto copz;
13258 case M_COP2:
13259 s = "c2";
13260 goto copz;
13261 case M_COP3:
13262 s = "c3";
13263 copz:
13264 gas_assert (!mips_opts.micromips);
13265 /* For now we just do C (same as Cz). The parameter will be
13266 stored in insn_opcode by mips_ip. */
13267 macro_build (NULL, s, "C", (int) ip->insn_opcode);
13268 break;
13269
13270 case M_MOVE:
13271 move_register (op[0], op[1]);
13272 break;
13273
13274 case M_MOVEP:
13275 gas_assert (mips_opts.micromips);
13276 gas_assert (mips_opts.insn32);
13277 move_register (micromips_to_32_reg_h_map1[op[0]],
13278 micromips_to_32_reg_m_map[op[1]]);
13279 move_register (micromips_to_32_reg_h_map2[op[0]],
13280 micromips_to_32_reg_n_map[op[2]]);
13281 break;
13282
13283 case M_DMUL:
13284 dbl = 1;
13285 /* Fall through. */
13286 case M_MUL:
13287 if (mips_opts.arch == CPU_R5900)
13288 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13289 op[2]);
13290 else
13291 {
13292 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13293 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13294 }
13295 break;
13296
13297 case M_DMUL_I:
13298 dbl = 1;
13299 /* Fall through. */
13300 case M_MUL_I:
13301 /* The MIPS assembler some times generates shifts and adds. I'm
13302 not trying to be that fancy. GCC should do this for us
13303 anyway. */
13304 used_at = 1;
13305 load_register (AT, &imm_expr, dbl);
13306 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13307 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13308 break;
13309
13310 case M_DMULO_I:
13311 dbl = 1;
13312 /* Fall through. */
13313 case M_MULO_I:
13314 imm = 1;
13315 goto do_mulo;
13316
13317 case M_DMULO:
13318 dbl = 1;
13319 /* Fall through. */
13320 case M_MULO:
13321 do_mulo:
13322 start_noreorder ();
13323 used_at = 1;
13324 if (imm)
13325 load_register (AT, &imm_expr, dbl);
13326 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13327 op[1], imm ? AT : op[2]);
13328 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13329 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
13330 macro_build (NULL, "mfhi", MFHL_FMT, AT);
13331 if (mips_trap)
13332 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
13333 else
13334 {
13335 if (mips_opts.micromips)
13336 micromips_label_expr (&label_expr);
13337 else
13338 label_expr.X_add_number = 8;
13339 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
13340 macro_build (NULL, "nop", "");
13341 macro_build (NULL, "break", BRK_FMT, 6);
13342 if (mips_opts.micromips)
13343 micromips_add_label ();
13344 }
13345 end_noreorder ();
13346 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13347 break;
13348
13349 case M_DMULOU_I:
13350 dbl = 1;
13351 /* Fall through. */
13352 case M_MULOU_I:
13353 imm = 1;
13354 goto do_mulou;
13355
13356 case M_DMULOU:
13357 dbl = 1;
13358 /* Fall through. */
13359 case M_MULOU:
13360 do_mulou:
13361 start_noreorder ();
13362 used_at = 1;
13363 if (imm)
13364 load_register (AT, &imm_expr, dbl);
13365 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
13366 op[1], imm ? AT : op[2]);
13367 macro_build (NULL, "mfhi", MFHL_FMT, AT);
13368 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13369 if (mips_trap)
13370 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
13371 else
13372 {
13373 if (mips_opts.micromips)
13374 micromips_label_expr (&label_expr);
13375 else
13376 label_expr.X_add_number = 8;
13377 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
13378 macro_build (NULL, "nop", "");
13379 macro_build (NULL, "break", BRK_FMT, 6);
13380 if (mips_opts.micromips)
13381 micromips_add_label ();
13382 }
13383 end_noreorder ();
13384 break;
13385
13386 case M_DROL:
13387 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13388 {
13389 if (op[0] == op[1])
13390 {
13391 tempreg = AT;
13392 used_at = 1;
13393 }
13394 else
13395 tempreg = op[0];
13396 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13397 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
13398 break;
13399 }
13400 used_at = 1;
13401 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13402 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13403 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13404 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13405 break;
13406
13407 case M_ROL:
13408 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13409 {
13410 if (op[0] == op[1])
13411 {
13412 tempreg = AT;
13413 used_at = 1;
13414 }
13415 else
13416 tempreg = op[0];
13417 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13418 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
13419 break;
13420 }
13421 used_at = 1;
13422 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13423 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13424 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13425 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13426 break;
13427
13428 case M_DROL_I:
13429 {
13430 unsigned int rot;
13431 const char *l;
13432 const char *rr;
13433
13434 rot = imm_expr.X_add_number & 0x3f;
13435 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13436 {
13437 rot = (64 - rot) & 0x3f;
13438 if (rot >= 32)
13439 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13440 else
13441 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13442 break;
13443 }
13444 if (rot == 0)
13445 {
13446 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13447 break;
13448 }
13449 l = (rot < 0x20) ? "dsll" : "dsll32";
13450 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
13451 rot &= 0x1f;
13452 used_at = 1;
13453 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13454 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13455 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13456 }
13457 break;
13458
13459 case M_ROL_I:
13460 {
13461 unsigned int rot;
13462
13463 rot = imm_expr.X_add_number & 0x1f;
13464 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13465 {
13466 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13467 (32 - rot) & 0x1f);
13468 break;
13469 }
13470 if (rot == 0)
13471 {
13472 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13473 break;
13474 }
13475 used_at = 1;
13476 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13477 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13478 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13479 }
13480 break;
13481
13482 case M_DROR:
13483 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13484 {
13485 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
13486 break;
13487 }
13488 used_at = 1;
13489 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13490 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13491 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13492 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13493 break;
13494
13495 case M_ROR:
13496 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13497 {
13498 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
13499 break;
13500 }
13501 used_at = 1;
13502 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13503 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13504 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13505 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13506 break;
13507
13508 case M_DROR_I:
13509 {
13510 unsigned int rot;
13511 const char *l;
13512 const char *rr;
13513
13514 rot = imm_expr.X_add_number & 0x3f;
13515 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13516 {
13517 if (rot >= 32)
13518 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13519 else
13520 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13521 break;
13522 }
13523 if (rot == 0)
13524 {
13525 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13526 break;
13527 }
13528 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
13529 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13530 rot &= 0x1f;
13531 used_at = 1;
13532 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13533 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13534 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13535 }
13536 break;
13537
13538 case M_ROR_I:
13539 {
13540 unsigned int rot;
13541
13542 rot = imm_expr.X_add_number & 0x1f;
13543 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13544 {
13545 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
13546 break;
13547 }
13548 if (rot == 0)
13549 {
13550 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13551 break;
13552 }
13553 used_at = 1;
13554 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13555 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13556 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13557 }
13558 break;
13559
13560 case M_SEQ:
13561 if (op[1] == 0)
13562 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13563 else if (op[2] == 0)
13564 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13565 else
13566 {
13567 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13568 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13569 }
13570 break;
13571
13572 case M_SEQ_I:
13573 if (imm_expr.X_add_number == 0)
13574 {
13575 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13576 break;
13577 }
13578 if (op[1] == 0)
13579 {
13580 as_warn (_("instruction %s: result is always false"),
13581 ip->insn_mo->name);
13582 move_register (op[0], 0);
13583 break;
13584 }
13585 if (CPU_HAS_SEQ (mips_opts.arch)
13586 && -512 <= imm_expr.X_add_number
13587 && imm_expr.X_add_number < 512)
13588 {
13589 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
13590 (int) imm_expr.X_add_number);
13591 break;
13592 }
13593 if (imm_expr.X_add_number >= 0
13594 && imm_expr.X_add_number < 0x10000)
13595 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
13596 else if (imm_expr.X_add_number > -0x8000
13597 && imm_expr.X_add_number < 0)
13598 {
13599 imm_expr.X_add_number = -imm_expr.X_add_number;
13600 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13601 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13602 }
13603 else if (CPU_HAS_SEQ (mips_opts.arch))
13604 {
13605 used_at = 1;
13606 load_register (AT, &imm_expr, GPR_SIZE == 64);
13607 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
13608 break;
13609 }
13610 else
13611 {
13612 load_register (AT, &imm_expr, GPR_SIZE == 64);
13613 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13614 used_at = 1;
13615 }
13616 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13617 break;
13618
13619 case M_SGE: /* X >= Y <==> not (X < Y) */
13620 s = "slt";
13621 goto sge;
13622 case M_SGEU:
13623 s = "sltu";
13624 sge:
13625 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13626 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13627 break;
13628
13629 case M_SGE_I: /* X >= I <==> not (X < I). */
13630 case M_SGEU_I:
13631 if (imm_expr.X_add_number >= -0x8000
13632 && imm_expr.X_add_number < 0x8000)
13633 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13634 op[0], op[1], BFD_RELOC_LO16);
13635 else
13636 {
13637 load_register (AT, &imm_expr, GPR_SIZE == 64);
13638 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
13639 op[0], op[1], AT);
13640 used_at = 1;
13641 }
13642 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13643 break;
13644
13645 case M_SGT: /* X > Y <==> Y < X. */
13646 s = "slt";
13647 goto sgt;
13648 case M_SGTU:
13649 s = "sltu";
13650 sgt:
13651 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13652 break;
13653
13654 case M_SGT_I: /* X > I <==> I < X. */
13655 s = "slt";
13656 goto sgti;
13657 case M_SGTU_I:
13658 s = "sltu";
13659 sgti:
13660 used_at = 1;
13661 load_register (AT, &imm_expr, GPR_SIZE == 64);
13662 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13663 break;
13664
13665 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X). */
13666 s = "slt";
13667 goto sle;
13668 case M_SLEU:
13669 s = "sltu";
13670 sle:
13671 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13672 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13673 break;
13674
13675 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
13676 s = "slt";
13677 goto slei;
13678 case M_SLEU_I:
13679 s = "sltu";
13680 slei:
13681 used_at = 1;
13682 load_register (AT, &imm_expr, GPR_SIZE == 64);
13683 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13684 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13685 break;
13686
13687 case M_SLT_I:
13688 if (imm_expr.X_add_number >= -0x8000
13689 && imm_expr.X_add_number < 0x8000)
13690 {
13691 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13692 BFD_RELOC_LO16);
13693 break;
13694 }
13695 used_at = 1;
13696 load_register (AT, &imm_expr, GPR_SIZE == 64);
13697 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
13698 break;
13699
13700 case M_SLTU_I:
13701 if (imm_expr.X_add_number >= -0x8000
13702 && imm_expr.X_add_number < 0x8000)
13703 {
13704 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
13705 BFD_RELOC_LO16);
13706 break;
13707 }
13708 used_at = 1;
13709 load_register (AT, &imm_expr, GPR_SIZE == 64);
13710 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
13711 break;
13712
13713 case M_SNE:
13714 if (op[1] == 0)
13715 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13716 else if (op[2] == 0)
13717 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13718 else
13719 {
13720 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13721 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13722 }
13723 break;
13724
13725 case M_SNE_I:
13726 if (imm_expr.X_add_number == 0)
13727 {
13728 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13729 break;
13730 }
13731 if (op[1] == 0)
13732 {
13733 as_warn (_("instruction %s: result is always true"),
13734 ip->insn_mo->name);
13735 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
13736 op[0], 0, BFD_RELOC_LO16);
13737 break;
13738 }
13739 if (CPU_HAS_SEQ (mips_opts.arch)
13740 && -512 <= imm_expr.X_add_number
13741 && imm_expr.X_add_number < 512)
13742 {
13743 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
13744 (int) imm_expr.X_add_number);
13745 break;
13746 }
13747 if (imm_expr.X_add_number >= 0
13748 && imm_expr.X_add_number < 0x10000)
13749 {
13750 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13751 BFD_RELOC_LO16);
13752 }
13753 else if (imm_expr.X_add_number > -0x8000
13754 && imm_expr.X_add_number < 0)
13755 {
13756 imm_expr.X_add_number = -imm_expr.X_add_number;
13757 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13758 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13759 }
13760 else if (CPU_HAS_SEQ (mips_opts.arch))
13761 {
13762 used_at = 1;
13763 load_register (AT, &imm_expr, GPR_SIZE == 64);
13764 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
13765 break;
13766 }
13767 else
13768 {
13769 load_register (AT, &imm_expr, GPR_SIZE == 64);
13770 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13771 used_at = 1;
13772 }
13773 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13774 break;
13775
13776 case M_SUB_I:
13777 s = "addi";
13778 s2 = "sub";
13779 if (ISA_IS_R6 (mips_opts.isa))
13780 goto do_subi_i;
13781 else
13782 goto do_subi;
13783 case M_SUBU_I:
13784 s = "addiu";
13785 s2 = "subu";
13786 goto do_subi;
13787 case M_DSUB_I:
13788 dbl = 1;
13789 s = "daddi";
13790 s2 = "dsub";
13791 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
13792 goto do_subi;
13793 if (imm_expr.X_add_number > -0x200
13794 && imm_expr.X_add_number <= 0x200
13795 && !ISA_IS_R6 (mips_opts.isa))
13796 {
13797 macro_build (NULL, s, "t,r,.", op[0], op[1],
13798 (int) -imm_expr.X_add_number);
13799 break;
13800 }
13801 goto do_subi_i;
13802 case M_DSUBU_I:
13803 dbl = 1;
13804 s = "daddiu";
13805 s2 = "dsubu";
13806 do_subi:
13807 if (imm_expr.X_add_number > -0x8000
13808 && imm_expr.X_add_number <= 0x8000)
13809 {
13810 imm_expr.X_add_number = -imm_expr.X_add_number;
13811 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13812 break;
13813 }
13814 do_subi_i:
13815 used_at = 1;
13816 load_register (AT, &imm_expr, dbl);
13817 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
13818 break;
13819
13820 case M_TEQ_I:
13821 s = "teq";
13822 goto trap;
13823 case M_TGE_I:
13824 s = "tge";
13825 goto trap;
13826 case M_TGEU_I:
13827 s = "tgeu";
13828 goto trap;
13829 case M_TLT_I:
13830 s = "tlt";
13831 goto trap;
13832 case M_TLTU_I:
13833 s = "tltu";
13834 goto trap;
13835 case M_TNE_I:
13836 s = "tne";
13837 trap:
13838 used_at = 1;
13839 load_register (AT, &imm_expr, GPR_SIZE == 64);
13840 macro_build (NULL, s, "s,t", op[0], AT);
13841 break;
13842
13843 case M_TRUNCWS:
13844 case M_TRUNCWD:
13845 gas_assert (!mips_opts.micromips);
13846 gas_assert (mips_opts.isa == ISA_MIPS1);
13847 used_at = 1;
13848
13849 /*
13850 * Is the double cfc1 instruction a bug in the mips assembler;
13851 * or is there a reason for it?
13852 */
13853 start_noreorder ();
13854 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13855 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13856 macro_build (NULL, "nop", "");
13857 expr1.X_add_number = 3;
13858 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
13859 expr1.X_add_number = 2;
13860 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13861 macro_build (NULL, "ctc1", "t,G", AT, RA);
13862 macro_build (NULL, "nop", "");
13863 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
13864 op[0], op[1]);
13865 macro_build (NULL, "ctc1", "t,G", op[2], RA);
13866 macro_build (NULL, "nop", "");
13867 end_noreorder ();
13868 break;
13869
13870 case M_ULH_AB:
13871 s = "lb";
13872 s2 = "lbu";
13873 off = 1;
13874 goto uld_st;
13875 case M_ULHU_AB:
13876 s = "lbu";
13877 s2 = "lbu";
13878 off = 1;
13879 goto uld_st;
13880 case M_ULW_AB:
13881 s = "lwl";
13882 s2 = "lwr";
13883 offbits = (mips_opts.micromips ? 12 : 16);
13884 off = 3;
13885 goto uld_st;
13886 case M_ULD_AB:
13887 s = "ldl";
13888 s2 = "ldr";
13889 offbits = (mips_opts.micromips ? 12 : 16);
13890 off = 7;
13891 goto uld_st;
13892 case M_USH_AB:
13893 s = "sb";
13894 s2 = "sb";
13895 off = 1;
13896 ust = 1;
13897 goto uld_st;
13898 case M_USW_AB:
13899 s = "swl";
13900 s2 = "swr";
13901 offbits = (mips_opts.micromips ? 12 : 16);
13902 off = 3;
13903 ust = 1;
13904 goto uld_st;
13905 case M_USD_AB:
13906 s = "sdl";
13907 s2 = "sdr";
13908 offbits = (mips_opts.micromips ? 12 : 16);
13909 off = 7;
13910 ust = 1;
13911
13912 uld_st:
13913 breg = op[2];
13914 large_offset = !small_offset_p (off, align, offbits);
13915 ep = &offset_expr;
13916 expr1.X_add_number = 0;
13917 if (large_offset)
13918 {
13919 used_at = 1;
13920 tempreg = AT;
13921 if (small_offset_p (0, align, 16))
13922 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13923 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13924 else
13925 {
13926 load_address (tempreg, ep, &used_at);
13927 if (breg != 0)
13928 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13929 tempreg, tempreg, breg);
13930 }
13931 offset_reloc[0] = BFD_RELOC_LO16;
13932 offset_reloc[1] = BFD_RELOC_UNUSED;
13933 offset_reloc[2] = BFD_RELOC_UNUSED;
13934 breg = tempreg;
13935 tempreg = op[0];
13936 ep = &expr1;
13937 }
13938 else if (!ust && op[0] == breg)
13939 {
13940 used_at = 1;
13941 tempreg = AT;
13942 }
13943 else
13944 tempreg = op[0];
13945
13946 if (off == 1)
13947 goto ulh_sh;
13948
13949 if (!target_big_endian)
13950 ep->X_add_number += off;
13951 if (offbits == 12)
13952 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
13953 else
13954 macro_build (ep, s, "t,o(b)", tempreg, -1,
13955 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13956
13957 if (!target_big_endian)
13958 ep->X_add_number -= off;
13959 else
13960 ep->X_add_number += off;
13961 if (offbits == 12)
13962 macro_build (NULL, s2, "t,~(b)",
13963 tempreg, (int) ep->X_add_number, breg);
13964 else
13965 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13966 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13967
13968 /* If necessary, move the result in tempreg to the final destination. */
13969 if (!ust && op[0] != tempreg)
13970 {
13971 /* Protect second load's delay slot. */
13972 load_delay_nop ();
13973 move_register (op[0], tempreg);
13974 }
13975 break;
13976
13977 ulh_sh:
13978 used_at = 1;
13979 if (target_big_endian == ust)
13980 ep->X_add_number += off;
13981 tempreg = ust || large_offset ? op[0] : AT;
13982 macro_build (ep, s, "t,o(b)", tempreg, -1,
13983 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13984
13985 /* For halfword transfers we need a temporary register to shuffle
13986 bytes. Unfortunately for M_USH_A we have none available before
13987 the next store as AT holds the base address. We deal with this
13988 case by clobbering TREG and then restoring it as with ULH. */
13989 tempreg = ust == large_offset ? op[0] : AT;
13990 if (ust)
13991 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
13992
13993 if (target_big_endian == ust)
13994 ep->X_add_number -= off;
13995 else
13996 ep->X_add_number += off;
13997 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13998 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13999
14000 /* For M_USH_A re-retrieve the LSB. */
14001 if (ust && large_offset)
14002 {
14003 if (target_big_endian)
14004 ep->X_add_number += off;
14005 else
14006 ep->X_add_number -= off;
14007 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
14008 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
14009 }
14010 /* For ULH and M_USH_A OR the LSB in. */
14011 if (!ust || large_offset)
14012 {
14013 tempreg = !large_offset ? AT : op[0];
14014 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
14015 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
14016 }
14017 break;
14018
14019 default:
14020 /* FIXME: Check if this is one of the itbl macros, since they
14021 are added dynamically. */
14022 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
14023 break;
14024 }
14025 if (!mips_opts.at && used_at)
14026 as_bad (_("macro used $at after \".set noat\""));
14027 }
14028
14029 /* Implement macros in mips16 mode. */
14030
14031 static void
14032 mips16_macro (struct mips_cl_insn *ip)
14033 {
14034 const struct mips_operand_array *operands;
14035 int mask;
14036 int tmp;
14037 expressionS expr1;
14038 int dbl;
14039 const char *s, *s2, *s3;
14040 unsigned int op[MAX_OPERANDS];
14041 unsigned int i;
14042
14043 mask = ip->insn_mo->mask;
14044
14045 operands = insn_operands (ip);
14046 for (i = 0; i < MAX_OPERANDS; i++)
14047 if (operands->operand[i])
14048 op[i] = insn_extract_operand (ip, operands->operand[i]);
14049 else
14050 op[i] = -1;
14051
14052 expr1.X_op = O_constant;
14053 expr1.X_op_symbol = NULL;
14054 expr1.X_add_symbol = NULL;
14055 expr1.X_add_number = 1;
14056
14057 dbl = 0;
14058
14059 switch (mask)
14060 {
14061 default:
14062 abort ();
14063
14064 case M_DDIV_3:
14065 dbl = 1;
14066 /* Fall through. */
14067 case M_DIV_3:
14068 s = "mflo";
14069 goto do_div3;
14070 case M_DREM_3:
14071 dbl = 1;
14072 /* Fall through. */
14073 case M_REM_3:
14074 s = "mfhi";
14075 do_div3:
14076 start_noreorder ();
14077 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
14078 expr1.X_add_number = 2;
14079 macro_build (&expr1, "bnez", "x,p", op[2]);
14080 macro_build (NULL, "break", "6", 7);
14081
14082 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
14083 since that causes an overflow. We should do that as well,
14084 but I don't see how to do the comparisons without a temporary
14085 register. */
14086 end_noreorder ();
14087 macro_build (NULL, s, "x", op[0]);
14088 break;
14089
14090 case M_DIVU_3:
14091 s = "divu";
14092 s2 = "mflo";
14093 goto do_divu3;
14094 case M_REMU_3:
14095 s = "divu";
14096 s2 = "mfhi";
14097 goto do_divu3;
14098 case M_DDIVU_3:
14099 s = "ddivu";
14100 s2 = "mflo";
14101 goto do_divu3;
14102 case M_DREMU_3:
14103 s = "ddivu";
14104 s2 = "mfhi";
14105 do_divu3:
14106 start_noreorder ();
14107 macro_build (NULL, s, ".,x,y", op[1], op[2]);
14108 expr1.X_add_number = 2;
14109 macro_build (&expr1, "bnez", "x,p", op[2]);
14110 macro_build (NULL, "break", "6", 7);
14111 end_noreorder ();
14112 macro_build (NULL, s2, "x", op[0]);
14113 break;
14114
14115 case M_DMUL:
14116 dbl = 1;
14117 /* Fall through. */
14118 case M_MUL:
14119 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
14120 macro_build (NULL, "mflo", "x", op[0]);
14121 break;
14122
14123 case M_DSUBU_I:
14124 dbl = 1;
14125 goto do_subu;
14126 case M_SUBU_I:
14127 do_subu:
14128 imm_expr.X_add_number = -imm_expr.X_add_number;
14129 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
14130 break;
14131
14132 case M_SUBU_I_2:
14133 imm_expr.X_add_number = -imm_expr.X_add_number;
14134 macro_build (&imm_expr, "addiu", "x,k", op[0]);
14135 break;
14136
14137 case M_DSUBU_I_2:
14138 imm_expr.X_add_number = -imm_expr.X_add_number;
14139 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
14140 break;
14141
14142 case M_BEQ:
14143 s = "cmp";
14144 s2 = "bteqz";
14145 goto do_branch;
14146 case M_BNE:
14147 s = "cmp";
14148 s2 = "btnez";
14149 goto do_branch;
14150 case M_BLT:
14151 s = "slt";
14152 s2 = "btnez";
14153 goto do_branch;
14154 case M_BLTU:
14155 s = "sltu";
14156 s2 = "btnez";
14157 goto do_branch;
14158 case M_BLE:
14159 s = "slt";
14160 s2 = "bteqz";
14161 goto do_reverse_branch;
14162 case M_BLEU:
14163 s = "sltu";
14164 s2 = "bteqz";
14165 goto do_reverse_branch;
14166 case M_BGE:
14167 s = "slt";
14168 s2 = "bteqz";
14169 goto do_branch;
14170 case M_BGEU:
14171 s = "sltu";
14172 s2 = "bteqz";
14173 goto do_branch;
14174 case M_BGT:
14175 s = "slt";
14176 s2 = "btnez";
14177 goto do_reverse_branch;
14178 case M_BGTU:
14179 s = "sltu";
14180 s2 = "btnez";
14181
14182 do_reverse_branch:
14183 tmp = op[1];
14184 op[1] = op[0];
14185 op[0] = tmp;
14186
14187 do_branch:
14188 macro_build (NULL, s, "x,y", op[0], op[1]);
14189 macro_build (&offset_expr, s2, "p");
14190 break;
14191
14192 case M_BEQ_I:
14193 s = "cmpi";
14194 s2 = "bteqz";
14195 s3 = "x,U";
14196 goto do_branch_i;
14197 case M_BNE_I:
14198 s = "cmpi";
14199 s2 = "btnez";
14200 s3 = "x,U";
14201 goto do_branch_i;
14202 case M_BLT_I:
14203 s = "slti";
14204 s2 = "btnez";
14205 s3 = "x,8";
14206 goto do_branch_i;
14207 case M_BLTU_I:
14208 s = "sltiu";
14209 s2 = "btnez";
14210 s3 = "x,8";
14211 goto do_branch_i;
14212 case M_BLE_I:
14213 s = "slti";
14214 s2 = "btnez";
14215 s3 = "x,8";
14216 goto do_addone_branch_i;
14217 case M_BLEU_I:
14218 s = "sltiu";
14219 s2 = "btnez";
14220 s3 = "x,8";
14221 goto do_addone_branch_i;
14222 case M_BGE_I:
14223 s = "slti";
14224 s2 = "bteqz";
14225 s3 = "x,8";
14226 goto do_branch_i;
14227 case M_BGEU_I:
14228 s = "sltiu";
14229 s2 = "bteqz";
14230 s3 = "x,8";
14231 goto do_branch_i;
14232 case M_BGT_I:
14233 s = "slti";
14234 s2 = "bteqz";
14235 s3 = "x,8";
14236 goto do_addone_branch_i;
14237 case M_BGTU_I:
14238 s = "sltiu";
14239 s2 = "bteqz";
14240 s3 = "x,8";
14241
14242 do_addone_branch_i:
14243 ++imm_expr.X_add_number;
14244
14245 do_branch_i:
14246 macro_build (&imm_expr, s, s3, op[0]);
14247 macro_build (&offset_expr, s2, "p");
14248 break;
14249
14250 case M_ABS:
14251 expr1.X_add_number = 0;
14252 macro_build (&expr1, "slti", "x,8", op[1]);
14253 if (op[0] != op[1])
14254 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
14255 expr1.X_add_number = 2;
14256 macro_build (&expr1, "bteqz", "p");
14257 macro_build (NULL, "neg", "x,w", op[0], op[0]);
14258 break;
14259 }
14260 }
14261
14262 /* Look up instruction [START, START + LENGTH) in HASH. Record any extra
14263 opcode bits in *OPCODE_EXTRA. */
14264
14265 static struct mips_opcode *
14266 mips_lookup_insn (struct hash_control *hash, const char *start,
14267 ssize_t length, unsigned int *opcode_extra)
14268 {
14269 char *name, *dot, *p;
14270 unsigned int mask, suffix;
14271 ssize_t opend;
14272 struct mips_opcode *insn;
14273
14274 /* Make a copy of the instruction so that we can fiddle with it. */
14275 name = xstrndup (start, length);
14276
14277 /* Look up the instruction as-is. */
14278 insn = (struct mips_opcode *) hash_find (hash, name);
14279 if (insn)
14280 goto end;
14281
14282 dot = strchr (name, '.');
14283 if (dot && dot[1])
14284 {
14285 /* Try to interpret the text after the dot as a VU0 channel suffix. */
14286 p = mips_parse_vu0_channels (dot + 1, &mask);
14287 if (*p == 0 && mask != 0)
14288 {
14289 *dot = 0;
14290 insn = (struct mips_opcode *) hash_find (hash, name);
14291 *dot = '.';
14292 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14293 {
14294 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
14295 goto end;
14296 }
14297 }
14298 }
14299
14300 if (mips_opts.micromips)
14301 {
14302 /* See if there's an instruction size override suffix,
14303 either `16' or `32', at the end of the mnemonic proper,
14304 that defines the operation, i.e. before the first `.'
14305 character if any. Strip it and retry. */
14306 opend = dot != NULL ? dot - name : length;
14307 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14308 suffix = 2;
14309 else if (opend >= 2 && name[opend - 2] == '3' && name[opend - 1] == '2')
14310 suffix = 4;
14311 else
14312 suffix = 0;
14313 if (suffix)
14314 {
14315 memmove (name + opend - 2, name + opend, length - opend + 1);
14316 insn = (struct mips_opcode *) hash_find (hash, name);
14317 if (insn)
14318 {
14319 forced_insn_length = suffix;
14320 goto end;
14321 }
14322 }
14323 }
14324
14325 insn = NULL;
14326 end:
14327 free (name);
14328 return insn;
14329 }
14330
14331 /* Assemble an instruction into its binary format. If the instruction
14332 is a macro, set imm_expr and offset_expr to the values associated
14333 with "I" and "A" operands respectively. Otherwise store the value
14334 of the relocatable field (if any) in offset_expr. In both cases
14335 set offset_reloc to the relocation operators applied to offset_expr. */
14336
14337 static void
14338 mips_ip (char *str, struct mips_cl_insn *insn)
14339 {
14340 const struct mips_opcode *first, *past;
14341 struct hash_control *hash;
14342 char format;
14343 size_t end;
14344 struct mips_operand_token *tokens;
14345 unsigned int opcode_extra;
14346
14347 if (mips_opts.micromips)
14348 {
14349 hash = micromips_op_hash;
14350 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14351 }
14352 else
14353 {
14354 hash = op_hash;
14355 past = &mips_opcodes[NUMOPCODES];
14356 }
14357 forced_insn_length = 0;
14358 opcode_extra = 0;
14359
14360 /* We first try to match an instruction up to a space or to the end. */
14361 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14362 continue;
14363
14364 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14365 if (first == NULL)
14366 {
14367 set_insn_error (0, _("unrecognized opcode"));
14368 return;
14369 }
14370
14371 if (strcmp (first->name, "li.s") == 0)
14372 format = 'f';
14373 else if (strcmp (first->name, "li.d") == 0)
14374 format = 'd';
14375 else
14376 format = 0;
14377 tokens = mips_parse_arguments (str + end, format);
14378 if (!tokens)
14379 return;
14380
14381 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14382 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
14383 set_insn_error (0, _("invalid operands"));
14384
14385 obstack_free (&mips_operand_tokens, tokens);
14386 }
14387
14388 /* As for mips_ip, but used when assembling MIPS16 code.
14389 Also set forced_insn_length to the resulting instruction size in
14390 bytes if the user explicitly requested a small or extended instruction. */
14391
14392 static void
14393 mips16_ip (char *str, struct mips_cl_insn *insn)
14394 {
14395 char *end, *s, c;
14396 struct mips_opcode *first;
14397 struct mips_operand_token *tokens;
14398 unsigned int l;
14399
14400 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
14401 ;
14402 end = s;
14403 c = *end;
14404
14405 l = 0;
14406 switch (c)
14407 {
14408 case '\0':
14409 break;
14410
14411 case ' ':
14412 s++;
14413 break;
14414
14415 case '.':
14416 s++;
14417 if (*s == 't')
14418 {
14419 l = 2;
14420 s++;
14421 }
14422 else if (*s == 'e')
14423 {
14424 l = 4;
14425 s++;
14426 }
14427 if (*s == '\0')
14428 break;
14429 else if (*s++ == ' ')
14430 break;
14431 set_insn_error (0, _("unrecognized opcode"));
14432 return;
14433 }
14434 forced_insn_length = l;
14435
14436 *end = 0;
14437 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
14438 *end = c;
14439
14440 if (!first)
14441 {
14442 set_insn_error (0, _("unrecognized opcode"));
14443 return;
14444 }
14445
14446 tokens = mips_parse_arguments (s, 0);
14447 if (!tokens)
14448 return;
14449
14450 if (!match_mips16_insns (insn, first, tokens))
14451 set_insn_error (0, _("invalid operands"));
14452
14453 obstack_free (&mips_operand_tokens, tokens);
14454 }
14455
14456 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14457 NBITS is the number of significant bits in VAL. */
14458
14459 static unsigned long
14460 mips16_immed_extend (offsetT val, unsigned int nbits)
14461 {
14462 int extval;
14463
14464 extval = 0;
14465 val &= (1U << nbits) - 1;
14466 if (nbits == 16 || nbits == 9)
14467 {
14468 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14469 val &= 0x1f;
14470 }
14471 else if (nbits == 15)
14472 {
14473 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14474 val &= 0xf;
14475 }
14476 else if (nbits == 6)
14477 {
14478 extval = ((val & 0x1f) << 6) | (val & 0x20);
14479 val = 0;
14480 }
14481 return (extval << 16) | val;
14482 }
14483
14484 /* Like decode_mips16_operand, but require the operand to be defined and
14485 require it to be an integer. */
14486
14487 static const struct mips_int_operand *
14488 mips16_immed_operand (int type, bfd_boolean extended_p)
14489 {
14490 const struct mips_operand *operand;
14491
14492 operand = decode_mips16_operand (type, extended_p);
14493 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14494 abort ();
14495 return (const struct mips_int_operand *) operand;
14496 }
14497
14498 /* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14499
14500 static bfd_boolean
14501 mips16_immed_in_range_p (const struct mips_int_operand *operand,
14502 bfd_reloc_code_real_type reloc, offsetT sval)
14503 {
14504 int min_val, max_val;
14505
14506 min_val = mips_int_operand_min (operand);
14507 max_val = mips_int_operand_max (operand);
14508 if (reloc != BFD_RELOC_UNUSED)
14509 {
14510 if (min_val < 0)
14511 sval = SEXT_16BIT (sval);
14512 else
14513 sval &= 0xffff;
14514 }
14515
14516 return (sval >= min_val
14517 && sval <= max_val
14518 && (sval & ((1 << operand->shift) - 1)) == 0);
14519 }
14520
14521 /* Install immediate value VAL into MIPS16 instruction *INSN,
14522 extending it if necessary. The instruction in *INSN may
14523 already be extended.
14524
14525 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14526 if none. In the former case, VAL is a 16-bit number with no
14527 defined signedness.
14528
14529 TYPE is the type of the immediate field. USER_INSN_LENGTH
14530 is the length that the user requested, or 0 if none. */
14531
14532 static void
14533 mips16_immed (const char *file, unsigned int line, int type,
14534 bfd_reloc_code_real_type reloc, offsetT val,
14535 unsigned int user_insn_length, unsigned long *insn)
14536 {
14537 const struct mips_int_operand *operand;
14538 unsigned int uval, length;
14539
14540 operand = mips16_immed_operand (type, FALSE);
14541 if (!mips16_immed_in_range_p (operand, reloc, val))
14542 {
14543 /* We need an extended instruction. */
14544 if (user_insn_length == 2)
14545 as_bad_where (file, line, _("invalid unextended operand value"));
14546 else
14547 *insn |= MIPS16_EXTEND;
14548 }
14549 else if (user_insn_length == 4)
14550 {
14551 /* The operand doesn't force an unextended instruction to be extended.
14552 Warn if the user wanted an extended instruction anyway. */
14553 *insn |= MIPS16_EXTEND;
14554 as_warn_where (file, line,
14555 _("extended operand requested but not required"));
14556 }
14557
14558 length = mips16_opcode_length (*insn);
14559 if (length == 4)
14560 {
14561 operand = mips16_immed_operand (type, TRUE);
14562 if (!mips16_immed_in_range_p (operand, reloc, val))
14563 as_bad_where (file, line,
14564 _("operand value out of range for instruction"));
14565 }
14566 uval = ((unsigned int) val >> operand->shift) - operand->bias;
14567 if (length == 2 || operand->root.lsb != 0)
14568 *insn = mips_insert_operand (&operand->root, *insn, uval);
14569 else
14570 *insn |= mips16_immed_extend (uval, operand->root.size);
14571 }
14572 \f
14573 struct percent_op_match
14574 {
14575 const char *str;
14576 bfd_reloc_code_real_type reloc;
14577 };
14578
14579 static const struct percent_op_match mips_percent_op[] =
14580 {
14581 {"%lo", BFD_RELOC_LO16},
14582 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14583 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14584 {"%call16", BFD_RELOC_MIPS_CALL16},
14585 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14586 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14587 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14588 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14589 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14590 {"%got", BFD_RELOC_MIPS_GOT16},
14591 {"%gp_rel", BFD_RELOC_GPREL16},
14592 {"%gprel", BFD_RELOC_GPREL16},
14593 {"%half", BFD_RELOC_16},
14594 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14595 {"%higher", BFD_RELOC_MIPS_HIGHER},
14596 {"%neg", BFD_RELOC_MIPS_SUB},
14597 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14598 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14599 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14600 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14601 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14602 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14603 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14604 {"%hi", BFD_RELOC_HI16_S},
14605 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14606 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
14607 };
14608
14609 static const struct percent_op_match mips16_percent_op[] =
14610 {
14611 {"%lo", BFD_RELOC_MIPS16_LO16},
14612 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
14613 {"%gprel", BFD_RELOC_MIPS16_GPREL},
14614 {"%got", BFD_RELOC_MIPS16_GOT16},
14615 {"%call16", BFD_RELOC_MIPS16_CALL16},
14616 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14617 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14618 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14619 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14620 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14621 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14622 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14623 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14624 };
14625
14626
14627 /* Return true if *STR points to a relocation operator. When returning true,
14628 move *STR over the operator and store its relocation code in *RELOC.
14629 Leave both *STR and *RELOC alone when returning false. */
14630
14631 static bfd_boolean
14632 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14633 {
14634 const struct percent_op_match *percent_op;
14635 size_t limit, i;
14636
14637 if (mips_opts.mips16)
14638 {
14639 percent_op = mips16_percent_op;
14640 limit = ARRAY_SIZE (mips16_percent_op);
14641 }
14642 else
14643 {
14644 percent_op = mips_percent_op;
14645 limit = ARRAY_SIZE (mips_percent_op);
14646 }
14647
14648 for (i = 0; i < limit; i++)
14649 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14650 {
14651 int len = strlen (percent_op[i].str);
14652
14653 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14654 continue;
14655
14656 *str += strlen (percent_op[i].str);
14657 *reloc = percent_op[i].reloc;
14658
14659 /* Check whether the output BFD supports this relocation.
14660 If not, issue an error and fall back on something safe. */
14661 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14662 {
14663 as_bad (_("relocation %s isn't supported by the current ABI"),
14664 percent_op[i].str);
14665 *reloc = BFD_RELOC_UNUSED;
14666 }
14667 return TRUE;
14668 }
14669 return FALSE;
14670 }
14671
14672
14673 /* Parse string STR as a 16-bit relocatable operand. Store the
14674 expression in *EP and the relocations in the array starting
14675 at RELOC. Return the number of relocation operators used.
14676
14677 On exit, EXPR_END points to the first character after the expression. */
14678
14679 static size_t
14680 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14681 char *str)
14682 {
14683 bfd_reloc_code_real_type reversed_reloc[3];
14684 size_t reloc_index, i;
14685 int crux_depth, str_depth;
14686 char *crux;
14687
14688 /* Search for the start of the main expression, recoding relocations
14689 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14690 of the main expression and with CRUX_DEPTH containing the number
14691 of open brackets at that point. */
14692 reloc_index = -1;
14693 str_depth = 0;
14694 do
14695 {
14696 reloc_index++;
14697 crux = str;
14698 crux_depth = str_depth;
14699
14700 /* Skip over whitespace and brackets, keeping count of the number
14701 of brackets. */
14702 while (*str == ' ' || *str == '\t' || *str == '(')
14703 if (*str++ == '(')
14704 str_depth++;
14705 }
14706 while (*str == '%'
14707 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14708 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14709
14710 my_getExpression (ep, crux);
14711 str = expr_end;
14712
14713 /* Match every open bracket. */
14714 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14715 if (*str++ == ')')
14716 crux_depth--;
14717
14718 if (crux_depth > 0)
14719 as_bad (_("unclosed '('"));
14720
14721 expr_end = str;
14722
14723 if (reloc_index != 0)
14724 {
14725 prev_reloc_op_frag = frag_now;
14726 for (i = 0; i < reloc_index; i++)
14727 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14728 }
14729
14730 return reloc_index;
14731 }
14732
14733 static void
14734 my_getExpression (expressionS *ep, char *str)
14735 {
14736 char *save_in;
14737
14738 save_in = input_line_pointer;
14739 input_line_pointer = str;
14740 expression (ep);
14741 expr_end = input_line_pointer;
14742 input_line_pointer = save_in;
14743 }
14744
14745 const char *
14746 md_atof (int type, char *litP, int *sizeP)
14747 {
14748 return ieee_md_atof (type, litP, sizeP, target_big_endian);
14749 }
14750
14751 void
14752 md_number_to_chars (char *buf, valueT val, int n)
14753 {
14754 if (target_big_endian)
14755 number_to_chars_bigendian (buf, val, n);
14756 else
14757 number_to_chars_littleendian (buf, val, n);
14758 }
14759 \f
14760 static int support_64bit_objects(void)
14761 {
14762 const char **list, **l;
14763 int yes;
14764
14765 list = bfd_target_list ();
14766 for (l = list; *l != NULL; l++)
14767 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14768 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14769 break;
14770 yes = (*l != NULL);
14771 free (list);
14772 return yes;
14773 }
14774
14775 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14776 NEW_VALUE. Warn if another value was already specified. Note:
14777 we have to defer parsing the -march and -mtune arguments in order
14778 to handle 'from-abi' correctly, since the ABI might be specified
14779 in a later argument. */
14780
14781 static void
14782 mips_set_option_string (const char **string_ptr, const char *new_value)
14783 {
14784 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14785 as_warn (_("a different %s was already specified, is now %s"),
14786 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14787 new_value);
14788
14789 *string_ptr = new_value;
14790 }
14791
14792 int
14793 md_parse_option (int c, const char *arg)
14794 {
14795 unsigned int i;
14796
14797 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14798 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14799 {
14800 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
14801 c == mips_ases[i].option_on);
14802 return 1;
14803 }
14804
14805 switch (c)
14806 {
14807 case OPTION_CONSTRUCT_FLOATS:
14808 mips_disable_float_construction = 0;
14809 break;
14810
14811 case OPTION_NO_CONSTRUCT_FLOATS:
14812 mips_disable_float_construction = 1;
14813 break;
14814
14815 case OPTION_TRAP:
14816 mips_trap = 1;
14817 break;
14818
14819 case OPTION_BREAK:
14820 mips_trap = 0;
14821 break;
14822
14823 case OPTION_EB:
14824 target_big_endian = 1;
14825 break;
14826
14827 case OPTION_EL:
14828 target_big_endian = 0;
14829 break;
14830
14831 case 'O':
14832 if (arg == NULL)
14833 mips_optimize = 1;
14834 else if (arg[0] == '0')
14835 mips_optimize = 0;
14836 else if (arg[0] == '1')
14837 mips_optimize = 1;
14838 else
14839 mips_optimize = 2;
14840 break;
14841
14842 case 'g':
14843 if (arg == NULL)
14844 mips_debug = 2;
14845 else
14846 mips_debug = atoi (arg);
14847 break;
14848
14849 case OPTION_MIPS1:
14850 file_mips_opts.isa = ISA_MIPS1;
14851 break;
14852
14853 case OPTION_MIPS2:
14854 file_mips_opts.isa = ISA_MIPS2;
14855 break;
14856
14857 case OPTION_MIPS3:
14858 file_mips_opts.isa = ISA_MIPS3;
14859 break;
14860
14861 case OPTION_MIPS4:
14862 file_mips_opts.isa = ISA_MIPS4;
14863 break;
14864
14865 case OPTION_MIPS5:
14866 file_mips_opts.isa = ISA_MIPS5;
14867 break;
14868
14869 case OPTION_MIPS32:
14870 file_mips_opts.isa = ISA_MIPS32;
14871 break;
14872
14873 case OPTION_MIPS32R2:
14874 file_mips_opts.isa = ISA_MIPS32R2;
14875 break;
14876
14877 case OPTION_MIPS32R3:
14878 file_mips_opts.isa = ISA_MIPS32R3;
14879 break;
14880
14881 case OPTION_MIPS32R5:
14882 file_mips_opts.isa = ISA_MIPS32R5;
14883 break;
14884
14885 case OPTION_MIPS32R6:
14886 file_mips_opts.isa = ISA_MIPS32R6;
14887 break;
14888
14889 case OPTION_MIPS64R2:
14890 file_mips_opts.isa = ISA_MIPS64R2;
14891 break;
14892
14893 case OPTION_MIPS64R3:
14894 file_mips_opts.isa = ISA_MIPS64R3;
14895 break;
14896
14897 case OPTION_MIPS64R5:
14898 file_mips_opts.isa = ISA_MIPS64R5;
14899 break;
14900
14901 case OPTION_MIPS64R6:
14902 file_mips_opts.isa = ISA_MIPS64R6;
14903 break;
14904
14905 case OPTION_MIPS64:
14906 file_mips_opts.isa = ISA_MIPS64;
14907 break;
14908
14909 case OPTION_MTUNE:
14910 mips_set_option_string (&mips_tune_string, arg);
14911 break;
14912
14913 case OPTION_MARCH:
14914 mips_set_option_string (&mips_arch_string, arg);
14915 break;
14916
14917 case OPTION_M4650:
14918 mips_set_option_string (&mips_arch_string, "4650");
14919 mips_set_option_string (&mips_tune_string, "4650");
14920 break;
14921
14922 case OPTION_NO_M4650:
14923 break;
14924
14925 case OPTION_M4010:
14926 mips_set_option_string (&mips_arch_string, "4010");
14927 mips_set_option_string (&mips_tune_string, "4010");
14928 break;
14929
14930 case OPTION_NO_M4010:
14931 break;
14932
14933 case OPTION_M4100:
14934 mips_set_option_string (&mips_arch_string, "4100");
14935 mips_set_option_string (&mips_tune_string, "4100");
14936 break;
14937
14938 case OPTION_NO_M4100:
14939 break;
14940
14941 case OPTION_M3900:
14942 mips_set_option_string (&mips_arch_string, "3900");
14943 mips_set_option_string (&mips_tune_string, "3900");
14944 break;
14945
14946 case OPTION_NO_M3900:
14947 break;
14948
14949 case OPTION_MICROMIPS:
14950 if (file_mips_opts.mips16 == 1)
14951 {
14952 as_bad (_("-mmicromips cannot be used with -mips16"));
14953 return 0;
14954 }
14955 file_mips_opts.micromips = 1;
14956 mips_no_prev_insn ();
14957 break;
14958
14959 case OPTION_NO_MICROMIPS:
14960 file_mips_opts.micromips = 0;
14961 mips_no_prev_insn ();
14962 break;
14963
14964 case OPTION_MIPS16:
14965 if (file_mips_opts.micromips == 1)
14966 {
14967 as_bad (_("-mips16 cannot be used with -micromips"));
14968 return 0;
14969 }
14970 file_mips_opts.mips16 = 1;
14971 mips_no_prev_insn ();
14972 break;
14973
14974 case OPTION_NO_MIPS16:
14975 file_mips_opts.mips16 = 0;
14976 mips_no_prev_insn ();
14977 break;
14978
14979 case OPTION_FIX_24K:
14980 mips_fix_24k = 1;
14981 break;
14982
14983 case OPTION_NO_FIX_24K:
14984 mips_fix_24k = 0;
14985 break;
14986
14987 case OPTION_FIX_RM7000:
14988 mips_fix_rm7000 = 1;
14989 break;
14990
14991 case OPTION_NO_FIX_RM7000:
14992 mips_fix_rm7000 = 0;
14993 break;
14994
14995 case OPTION_FIX_LOONGSON3_LLSC:
14996 mips_fix_loongson3_llsc = TRUE;
14997 break;
14998
14999 case OPTION_NO_FIX_LOONGSON3_LLSC:
15000 mips_fix_loongson3_llsc = FALSE;
15001 break;
15002
15003 case OPTION_FIX_LOONGSON2F_JUMP:
15004 mips_fix_loongson2f_jump = TRUE;
15005 break;
15006
15007 case OPTION_NO_FIX_LOONGSON2F_JUMP:
15008 mips_fix_loongson2f_jump = FALSE;
15009 break;
15010
15011 case OPTION_FIX_LOONGSON2F_NOP:
15012 mips_fix_loongson2f_nop = TRUE;
15013 break;
15014
15015 case OPTION_NO_FIX_LOONGSON2F_NOP:
15016 mips_fix_loongson2f_nop = FALSE;
15017 break;
15018
15019 case OPTION_FIX_VR4120:
15020 mips_fix_vr4120 = 1;
15021 break;
15022
15023 case OPTION_NO_FIX_VR4120:
15024 mips_fix_vr4120 = 0;
15025 break;
15026
15027 case OPTION_FIX_VR4130:
15028 mips_fix_vr4130 = 1;
15029 break;
15030
15031 case OPTION_NO_FIX_VR4130:
15032 mips_fix_vr4130 = 0;
15033 break;
15034
15035 case OPTION_FIX_CN63XXP1:
15036 mips_fix_cn63xxp1 = TRUE;
15037 break;
15038
15039 case OPTION_NO_FIX_CN63XXP1:
15040 mips_fix_cn63xxp1 = FALSE;
15041 break;
15042
15043 case OPTION_FIX_R5900:
15044 mips_fix_r5900 = TRUE;
15045 mips_fix_r5900_explicit = TRUE;
15046 break;
15047
15048 case OPTION_NO_FIX_R5900:
15049 mips_fix_r5900 = FALSE;
15050 mips_fix_r5900_explicit = TRUE;
15051 break;
15052
15053 case OPTION_RELAX_BRANCH:
15054 mips_relax_branch = 1;
15055 break;
15056
15057 case OPTION_NO_RELAX_BRANCH:
15058 mips_relax_branch = 0;
15059 break;
15060
15061 case OPTION_IGNORE_BRANCH_ISA:
15062 mips_ignore_branch_isa = TRUE;
15063 break;
15064
15065 case OPTION_NO_IGNORE_BRANCH_ISA:
15066 mips_ignore_branch_isa = FALSE;
15067 break;
15068
15069 case OPTION_INSN32:
15070 file_mips_opts.insn32 = TRUE;
15071 break;
15072
15073 case OPTION_NO_INSN32:
15074 file_mips_opts.insn32 = FALSE;
15075 break;
15076
15077 case OPTION_MSHARED:
15078 mips_in_shared = TRUE;
15079 break;
15080
15081 case OPTION_MNO_SHARED:
15082 mips_in_shared = FALSE;
15083 break;
15084
15085 case OPTION_MSYM32:
15086 file_mips_opts.sym32 = TRUE;
15087 break;
15088
15089 case OPTION_MNO_SYM32:
15090 file_mips_opts.sym32 = FALSE;
15091 break;
15092
15093 /* When generating ELF code, we permit -KPIC and -call_shared to
15094 select SVR4_PIC, and -non_shared to select no PIC. This is
15095 intended to be compatible with Irix 5. */
15096 case OPTION_CALL_SHARED:
15097 mips_pic = SVR4_PIC;
15098 mips_abicalls = TRUE;
15099 break;
15100
15101 case OPTION_CALL_NONPIC:
15102 mips_pic = NO_PIC;
15103 mips_abicalls = TRUE;
15104 break;
15105
15106 case OPTION_NON_SHARED:
15107 mips_pic = NO_PIC;
15108 mips_abicalls = FALSE;
15109 break;
15110
15111 /* The -xgot option tells the assembler to use 32 bit offsets
15112 when accessing the got in SVR4_PIC mode. It is for Irix
15113 compatibility. */
15114 case OPTION_XGOT:
15115 mips_big_got = 1;
15116 break;
15117
15118 case 'G':
15119 g_switch_value = atoi (arg);
15120 g_switch_seen = 1;
15121 break;
15122
15123 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15124 and -mabi=64. */
15125 case OPTION_32:
15126 mips_abi = O32_ABI;
15127 break;
15128
15129 case OPTION_N32:
15130 mips_abi = N32_ABI;
15131 break;
15132
15133 case OPTION_64:
15134 mips_abi = N64_ABI;
15135 if (!support_64bit_objects())
15136 as_fatal (_("no compiled in support for 64 bit object file format"));
15137 break;
15138
15139 case OPTION_GP32:
15140 file_mips_opts.gp = 32;
15141 break;
15142
15143 case OPTION_GP64:
15144 file_mips_opts.gp = 64;
15145 break;
15146
15147 case OPTION_FP32:
15148 file_mips_opts.fp = 32;
15149 break;
15150
15151 case OPTION_FPXX:
15152 file_mips_opts.fp = 0;
15153 break;
15154
15155 case OPTION_FP64:
15156 file_mips_opts.fp = 64;
15157 break;
15158
15159 case OPTION_ODD_SPREG:
15160 file_mips_opts.oddspreg = 1;
15161 break;
15162
15163 case OPTION_NO_ODD_SPREG:
15164 file_mips_opts.oddspreg = 0;
15165 break;
15166
15167 case OPTION_SINGLE_FLOAT:
15168 file_mips_opts.single_float = 1;
15169 break;
15170
15171 case OPTION_DOUBLE_FLOAT:
15172 file_mips_opts.single_float = 0;
15173 break;
15174
15175 case OPTION_SOFT_FLOAT:
15176 file_mips_opts.soft_float = 1;
15177 break;
15178
15179 case OPTION_HARD_FLOAT:
15180 file_mips_opts.soft_float = 0;
15181 break;
15182
15183 case OPTION_MABI:
15184 if (strcmp (arg, "32") == 0)
15185 mips_abi = O32_ABI;
15186 else if (strcmp (arg, "o64") == 0)
15187 mips_abi = O64_ABI;
15188 else if (strcmp (arg, "n32") == 0)
15189 mips_abi = N32_ABI;
15190 else if (strcmp (arg, "64") == 0)
15191 {
15192 mips_abi = N64_ABI;
15193 if (! support_64bit_objects())
15194 as_fatal (_("no compiled in support for 64 bit object file "
15195 "format"));
15196 }
15197 else if (strcmp (arg, "eabi") == 0)
15198 mips_abi = EABI_ABI;
15199 else
15200 {
15201 as_fatal (_("invalid abi -mabi=%s"), arg);
15202 return 0;
15203 }
15204 break;
15205
15206 case OPTION_M7000_HILO_FIX:
15207 mips_7000_hilo_fix = TRUE;
15208 break;
15209
15210 case OPTION_MNO_7000_HILO_FIX:
15211 mips_7000_hilo_fix = FALSE;
15212 break;
15213
15214 case OPTION_MDEBUG:
15215 mips_flag_mdebug = TRUE;
15216 break;
15217
15218 case OPTION_NO_MDEBUG:
15219 mips_flag_mdebug = FALSE;
15220 break;
15221
15222 case OPTION_PDR:
15223 mips_flag_pdr = TRUE;
15224 break;
15225
15226 case OPTION_NO_PDR:
15227 mips_flag_pdr = FALSE;
15228 break;
15229
15230 case OPTION_MVXWORKS_PIC:
15231 mips_pic = VXWORKS_PIC;
15232 break;
15233
15234 case OPTION_NAN:
15235 if (strcmp (arg, "2008") == 0)
15236 mips_nan2008 = 1;
15237 else if (strcmp (arg, "legacy") == 0)
15238 mips_nan2008 = 0;
15239 else
15240 {
15241 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
15242 return 0;
15243 }
15244 break;
15245
15246 default:
15247 return 0;
15248 }
15249
15250 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15251
15252 return 1;
15253 }
15254 \f
15255 /* Set up globals to tune for the ISA or processor described by INFO. */
15256
15257 static void
15258 mips_set_tune (const struct mips_cpu_info *info)
15259 {
15260 if (info != 0)
15261 mips_tune = info->cpu;
15262 }
15263
15264
15265 void
15266 mips_after_parse_args (void)
15267 {
15268 const struct mips_cpu_info *arch_info = 0;
15269 const struct mips_cpu_info *tune_info = 0;
15270
15271 /* GP relative stuff not working for PE. */
15272 if (strncmp (TARGET_OS, "pe", 2) == 0)
15273 {
15274 if (g_switch_seen && g_switch_value != 0)
15275 as_bad (_("-G not supported in this configuration"));
15276 g_switch_value = 0;
15277 }
15278
15279 if (mips_abi == NO_ABI)
15280 mips_abi = MIPS_DEFAULT_ABI;
15281
15282 /* The following code determines the architecture.
15283 Similar code was added to GCC 3.3 (see override_options() in
15284 config/mips/mips.c). The GAS and GCC code should be kept in sync
15285 as much as possible. */
15286
15287 if (mips_arch_string != 0)
15288 arch_info = mips_parse_cpu ("-march", mips_arch_string);
15289
15290 if (file_mips_opts.isa != ISA_UNKNOWN)
15291 {
15292 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
15293 ISA level specified by -mipsN, while arch_info->isa contains
15294 the -march selection (if any). */
15295 if (arch_info != 0)
15296 {
15297 /* -march takes precedence over -mipsN, since it is more descriptive.
15298 There's no harm in specifying both as long as the ISA levels
15299 are the same. */
15300 if (file_mips_opts.isa != arch_info->isa)
15301 as_bad (_("-%s conflicts with the other architecture options,"
15302 " which imply -%s"),
15303 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
15304 mips_cpu_info_from_isa (arch_info->isa)->name);
15305 }
15306 else
15307 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
15308 }
15309
15310 if (arch_info == 0)
15311 {
15312 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15313 gas_assert (arch_info);
15314 }
15315
15316 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15317 as_bad (_("-march=%s is not compatible with the selected ABI"),
15318 arch_info->name);
15319
15320 file_mips_opts.arch = arch_info->cpu;
15321 file_mips_opts.isa = arch_info->isa;
15322 file_mips_opts.init_ase = arch_info->ase;
15323
15324 /* The EVA Extension has instructions which are only valid when the R6 ISA
15325 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
15326 present. */
15327 if (((file_mips_opts.ase & ASE_EVA) != 0) && ISA_IS_R6 (file_mips_opts.isa))
15328 file_mips_opts.ase |= ASE_EVA_R6;
15329
15330 /* Set up initial mips_opts state. */
15331 mips_opts = file_mips_opts;
15332
15333 /* For the R5900 default to `-mfix-r5900' unless the user told otherwise. */
15334 if (!mips_fix_r5900_explicit)
15335 mips_fix_r5900 = file_mips_opts.arch == CPU_R5900;
15336
15337 /* The register size inference code is now placed in
15338 file_mips_check_options. */
15339
15340 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15341 processor. */
15342 if (mips_tune_string != 0)
15343 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15344
15345 if (tune_info == 0)
15346 mips_set_tune (arch_info);
15347 else
15348 mips_set_tune (tune_info);
15349
15350 if (mips_flag_mdebug < 0)
15351 mips_flag_mdebug = 0;
15352 }
15353 \f
15354 void
15355 mips_init_after_args (void)
15356 {
15357 /* Initialize opcodes. */
15358 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15359 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15360 }
15361
15362 long
15363 md_pcrel_from (fixS *fixP)
15364 {
15365 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15366
15367 switch (fixP->fx_r_type)
15368 {
15369 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15370 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15371 /* Return the address of the delay slot. */
15372 return addr + 2;
15373
15374 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15375 case BFD_RELOC_MICROMIPS_JMP:
15376 case BFD_RELOC_MIPS16_16_PCREL_S1:
15377 case BFD_RELOC_16_PCREL_S2:
15378 case BFD_RELOC_MIPS_21_PCREL_S2:
15379 case BFD_RELOC_MIPS_26_PCREL_S2:
15380 case BFD_RELOC_MIPS_JMP:
15381 /* Return the address of the delay slot. */
15382 return addr + 4;
15383
15384 case BFD_RELOC_MIPS_18_PCREL_S3:
15385 /* Return the aligned address of the doubleword containing
15386 the instruction. */
15387 return addr & ~7;
15388
15389 default:
15390 return addr;
15391 }
15392 }
15393
15394 /* This is called before the symbol table is processed. In order to
15395 work with gcc when using mips-tfile, we must keep all local labels.
15396 However, in other cases, we want to discard them. If we were
15397 called with -g, but we didn't see any debugging information, it may
15398 mean that gcc is smuggling debugging information through to
15399 mips-tfile, in which case we must generate all local labels. */
15400
15401 void
15402 mips_frob_file_before_adjust (void)
15403 {
15404 #ifndef NO_ECOFF_DEBUGGING
15405 if (ECOFF_DEBUGGING
15406 && mips_debug != 0
15407 && ! ecoff_debugging_seen)
15408 flag_keep_locals = 1;
15409 #endif
15410 }
15411
15412 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15413 the corresponding LO16 reloc. This is called before md_apply_fix and
15414 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15415 relocation operators.
15416
15417 For our purposes, a %lo() expression matches a %got() or %hi()
15418 expression if:
15419
15420 (a) it refers to the same symbol; and
15421 (b) the offset applied in the %lo() expression is no lower than
15422 the offset applied in the %got() or %hi().
15423
15424 (b) allows us to cope with code like:
15425
15426 lui $4,%hi(foo)
15427 lh $4,%lo(foo+2)($4)
15428
15429 ...which is legal on RELA targets, and has a well-defined behaviour
15430 if the user knows that adding 2 to "foo" will not induce a carry to
15431 the high 16 bits.
15432
15433 When several %lo()s match a particular %got() or %hi(), we use the
15434 following rules to distinguish them:
15435
15436 (1) %lo()s with smaller offsets are a better match than %lo()s with
15437 higher offsets.
15438
15439 (2) %lo()s with no matching %got() or %hi() are better than those
15440 that already have a matching %got() or %hi().
15441
15442 (3) later %lo()s are better than earlier %lo()s.
15443
15444 These rules are applied in order.
15445
15446 (1) means, among other things, that %lo()s with identical offsets are
15447 chosen if they exist.
15448
15449 (2) means that we won't associate several high-part relocations with
15450 the same low-part relocation unless there's no alternative. Having
15451 several high parts for the same low part is a GNU extension; this rule
15452 allows careful users to avoid it.
15453
15454 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15455 with the last high-part relocation being at the front of the list.
15456 It therefore makes sense to choose the last matching low-part
15457 relocation, all other things being equal. It's also easier
15458 to code that way. */
15459
15460 void
15461 mips_frob_file (void)
15462 {
15463 struct mips_hi_fixup *l;
15464 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15465
15466 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15467 {
15468 segment_info_type *seginfo;
15469 bfd_boolean matched_lo_p;
15470 fixS **hi_pos, **lo_pos, **pos;
15471
15472 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15473
15474 /* If a GOT16 relocation turns out to be against a global symbol,
15475 there isn't supposed to be a matching LO. Ignore %gots against
15476 constants; we'll report an error for those later. */
15477 if (got16_reloc_p (l->fixp->fx_r_type)
15478 && !(l->fixp->fx_addsy
15479 && pic_need_relax (l->fixp->fx_addsy)))
15480 continue;
15481
15482 /* Check quickly whether the next fixup happens to be a matching %lo. */
15483 if (fixup_has_matching_lo_p (l->fixp))
15484 continue;
15485
15486 seginfo = seg_info (l->seg);
15487
15488 /* Set HI_POS to the position of this relocation in the chain.
15489 Set LO_POS to the position of the chosen low-part relocation.
15490 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15491 relocation that matches an immediately-preceding high-part
15492 relocation. */
15493 hi_pos = NULL;
15494 lo_pos = NULL;
15495 matched_lo_p = FALSE;
15496 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15497
15498 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15499 {
15500 if (*pos == l->fixp)
15501 hi_pos = pos;
15502
15503 if ((*pos)->fx_r_type == looking_for_rtype
15504 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15505 && (*pos)->fx_offset >= l->fixp->fx_offset
15506 && (lo_pos == NULL
15507 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15508 || (!matched_lo_p
15509 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15510 lo_pos = pos;
15511
15512 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15513 && fixup_has_matching_lo_p (*pos));
15514 }
15515
15516 /* If we found a match, remove the high-part relocation from its
15517 current position and insert it before the low-part relocation.
15518 Make the offsets match so that fixup_has_matching_lo_p()
15519 will return true.
15520
15521 We don't warn about unmatched high-part relocations since some
15522 versions of gcc have been known to emit dead "lui ...%hi(...)"
15523 instructions. */
15524 if (lo_pos != NULL)
15525 {
15526 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15527 if (l->fixp->fx_next != *lo_pos)
15528 {
15529 *hi_pos = l->fixp->fx_next;
15530 l->fixp->fx_next = *lo_pos;
15531 *lo_pos = l->fixp;
15532 }
15533 }
15534 }
15535 }
15536
15537 int
15538 mips_force_relocation (fixS *fixp)
15539 {
15540 if (generic_force_reloc (fixp))
15541 return 1;
15542
15543 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15544 so that the linker relaxation can update targets. */
15545 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15546 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15547 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15548 return 1;
15549
15550 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15551 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15552 microMIPS symbols so that we can do cross-mode branch diagnostics
15553 and BAL to JALX conversion by the linker. */
15554 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15555 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15556 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15557 && fixp->fx_addsy
15558 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15559 return 1;
15560
15561 /* We want all PC-relative relocations to be kept for R6 relaxation. */
15562 if (ISA_IS_R6 (file_mips_opts.isa)
15563 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15564 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15565 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15566 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15567 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15568 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15569 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15570 return 1;
15571
15572 return 0;
15573 }
15574
15575 /* Implement TC_FORCE_RELOCATION_ABS. */
15576
15577 bfd_boolean
15578 mips_force_relocation_abs (fixS *fixp)
15579 {
15580 if (generic_force_reloc (fixp))
15581 return TRUE;
15582
15583 /* These relocations do not have enough bits in the in-place addend
15584 to hold an arbitrary absolute section's offset. */
15585 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15586 return TRUE;
15587
15588 return FALSE;
15589 }
15590
15591 /* Read the instruction associated with RELOC from BUF. */
15592
15593 static unsigned int
15594 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15595 {
15596 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15597 return read_compressed_insn (buf, 4);
15598 else
15599 return read_insn (buf);
15600 }
15601
15602 /* Write instruction INSN to BUF, given that it has been relocated
15603 by RELOC. */
15604
15605 static void
15606 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15607 unsigned long insn)
15608 {
15609 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15610 write_compressed_insn (buf, insn, 4);
15611 else
15612 write_insn (buf, insn);
15613 }
15614
15615 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15616 to a symbol in another ISA mode, which cannot be converted to JALX. */
15617
15618 static bfd_boolean
15619 fix_bad_cross_mode_jump_p (fixS *fixP)
15620 {
15621 unsigned long opcode;
15622 int other;
15623 char *buf;
15624
15625 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15626 return FALSE;
15627
15628 other = S_GET_OTHER (fixP->fx_addsy);
15629 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15630 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15631 switch (fixP->fx_r_type)
15632 {
15633 case BFD_RELOC_MIPS_JMP:
15634 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15635 case BFD_RELOC_MICROMIPS_JMP:
15636 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15637 default:
15638 return FALSE;
15639 }
15640 }
15641
15642 /* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15643 jump to a symbol in the same ISA mode. */
15644
15645 static bfd_boolean
15646 fix_bad_same_mode_jalx_p (fixS *fixP)
15647 {
15648 unsigned long opcode;
15649 int other;
15650 char *buf;
15651
15652 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15653 return FALSE;
15654
15655 other = S_GET_OTHER (fixP->fx_addsy);
15656 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15657 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15658 switch (fixP->fx_r_type)
15659 {
15660 case BFD_RELOC_MIPS_JMP:
15661 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15662 case BFD_RELOC_MIPS16_JMP:
15663 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15664 case BFD_RELOC_MICROMIPS_JMP:
15665 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15666 default:
15667 return FALSE;
15668 }
15669 }
15670
15671 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15672 to a symbol whose value plus addend is not aligned according to the
15673 ultimate (after linker relaxation) jump instruction's immediate field
15674 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15675 regular MIPS code, to (1 << 2). */
15676
15677 static bfd_boolean
15678 fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15679 {
15680 bfd_boolean micro_to_mips_p;
15681 valueT val;
15682 int other;
15683
15684 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15685 return FALSE;
15686
15687 other = S_GET_OTHER (fixP->fx_addsy);
15688 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15689 val += fixP->fx_offset;
15690 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15691 && !ELF_ST_IS_MICROMIPS (other));
15692 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15693 != ELF_ST_IS_COMPRESSED (other));
15694 }
15695
15696 /* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15697 to a symbol whose annotation indicates another ISA mode. For absolute
15698 symbols check the ISA bit instead.
15699
15700 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15701 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15702 MIPS symbols and associated with BAL instructions as these instructions
15703 may be converted to JALX by the linker. */
15704
15705 static bfd_boolean
15706 fix_bad_cross_mode_branch_p (fixS *fixP)
15707 {
15708 bfd_boolean absolute_p;
15709 unsigned long opcode;
15710 asection *symsec;
15711 valueT val;
15712 int other;
15713 char *buf;
15714
15715 if (mips_ignore_branch_isa)
15716 return FALSE;
15717
15718 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15719 return FALSE;
15720
15721 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15722 absolute_p = bfd_is_abs_section (symsec);
15723
15724 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15725 other = S_GET_OTHER (fixP->fx_addsy);
15726
15727 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15728 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15729 switch (fixP->fx_r_type)
15730 {
15731 case BFD_RELOC_16_PCREL_S2:
15732 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15733 && opcode != 0x0411);
15734 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15735 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15736 && opcode != 0x4060);
15737 case BFD_RELOC_MIPS_21_PCREL_S2:
15738 case BFD_RELOC_MIPS_26_PCREL_S2:
15739 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15740 case BFD_RELOC_MIPS16_16_PCREL_S1:
15741 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15742 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15743 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15744 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15745 default:
15746 abort ();
15747 }
15748 }
15749
15750 /* Return TRUE if the symbol plus addend associated with a regular MIPS
15751 branch instruction pointed to by FIXP is not aligned according to the
15752 branch instruction's immediate field requirement. We need the addend
15753 to preserve the ISA bit and also the sum must not have bit 2 set. We
15754 must explicitly OR in the ISA bit from symbol annotation as the bit
15755 won't be set in the symbol's value then. */
15756
15757 static bfd_boolean
15758 fix_bad_misaligned_branch_p (fixS *fixP)
15759 {
15760 bfd_boolean absolute_p;
15761 asection *symsec;
15762 valueT isa_bit;
15763 valueT val;
15764 valueT off;
15765 int other;
15766
15767 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15768 return FALSE;
15769
15770 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15771 absolute_p = bfd_is_abs_section (symsec);
15772
15773 val = S_GET_VALUE (fixP->fx_addsy);
15774 other = S_GET_OTHER (fixP->fx_addsy);
15775 off = fixP->fx_offset;
15776
15777 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15778 val |= ELF_ST_IS_COMPRESSED (other);
15779 val += off;
15780 return (val & 0x3) != isa_bit;
15781 }
15782
15783 /* Calculate the relocation target by masking off ISA mode bit before
15784 combining symbol and addend. */
15785
15786 static valueT
15787 fix_bad_misaligned_address (fixS *fixP)
15788 {
15789 valueT val;
15790 valueT off;
15791 unsigned isa_mode;
15792 gas_assert (fixP != NULL && fixP->fx_addsy != NULL);
15793 val = S_GET_VALUE (fixP->fx_addsy);
15794 off = fixP->fx_offset;
15795 isa_mode = (ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixP->fx_addsy))
15796 ? 1 : 0);
15797
15798 return ((val & ~isa_mode) + off);
15799 }
15800
15801 /* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15802 and its calculated value VAL. */
15803
15804 static void
15805 fix_validate_branch (fixS *fixP, valueT val)
15806 {
15807 if (fixP->fx_done && (val & 0x3) != 0)
15808 as_bad_where (fixP->fx_file, fixP->fx_line,
15809 _("branch to misaligned address (0x%lx)"),
15810 (long) (val + md_pcrel_from (fixP)));
15811 else if (fix_bad_cross_mode_branch_p (fixP))
15812 as_bad_where (fixP->fx_file, fixP->fx_line,
15813 _("branch to a symbol in another ISA mode"));
15814 else if (fix_bad_misaligned_branch_p (fixP))
15815 as_bad_where (fixP->fx_file, fixP->fx_line,
15816 _("branch to misaligned address (0x%lx)"),
15817 (long) fix_bad_misaligned_address (fixP));
15818 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15819 as_bad_where (fixP->fx_file, fixP->fx_line,
15820 _("cannot encode misaligned addend "
15821 "in the relocatable field (0x%lx)"),
15822 (long) fixP->fx_offset);
15823 }
15824
15825 /* Apply a fixup to the object file. */
15826
15827 void
15828 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15829 {
15830 char *buf;
15831 unsigned long insn;
15832 reloc_howto_type *howto;
15833
15834 if (fixP->fx_pcrel)
15835 switch (fixP->fx_r_type)
15836 {
15837 case BFD_RELOC_16_PCREL_S2:
15838 case BFD_RELOC_MIPS16_16_PCREL_S1:
15839 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15840 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15841 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15842 case BFD_RELOC_32_PCREL:
15843 case BFD_RELOC_MIPS_21_PCREL_S2:
15844 case BFD_RELOC_MIPS_26_PCREL_S2:
15845 case BFD_RELOC_MIPS_18_PCREL_S3:
15846 case BFD_RELOC_MIPS_19_PCREL_S2:
15847 case BFD_RELOC_HI16_S_PCREL:
15848 case BFD_RELOC_LO16_PCREL:
15849 break;
15850
15851 case BFD_RELOC_32:
15852 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15853 break;
15854
15855 default:
15856 as_bad_where (fixP->fx_file, fixP->fx_line,
15857 _("PC-relative reference to a different section"));
15858 break;
15859 }
15860
15861 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15862 that have no MIPS ELF equivalent. */
15863 if (fixP->fx_r_type != BFD_RELOC_8)
15864 {
15865 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15866 if (!howto)
15867 return;
15868 }
15869
15870 gas_assert (fixP->fx_size == 2
15871 || fixP->fx_size == 4
15872 || fixP->fx_r_type == BFD_RELOC_8
15873 || fixP->fx_r_type == BFD_RELOC_16
15874 || fixP->fx_r_type == BFD_RELOC_64
15875 || fixP->fx_r_type == BFD_RELOC_CTOR
15876 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15877 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15878 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15879 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15880 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15881 || fixP->fx_r_type == BFD_RELOC_NONE);
15882
15883 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15884
15885 /* Don't treat parts of a composite relocation as done. There are two
15886 reasons for this:
15887
15888 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15889 should nevertheless be emitted if the first part is.
15890
15891 (2) In normal usage, composite relocations are never assembly-time
15892 constants. The easiest way of dealing with the pathological
15893 exceptions is to generate a relocation against STN_UNDEF and
15894 leave everything up to the linker. */
15895 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15896 fixP->fx_done = 1;
15897
15898 switch (fixP->fx_r_type)
15899 {
15900 case BFD_RELOC_MIPS_TLS_GD:
15901 case BFD_RELOC_MIPS_TLS_LDM:
15902 case BFD_RELOC_MIPS_TLS_DTPREL32:
15903 case BFD_RELOC_MIPS_TLS_DTPREL64:
15904 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15905 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15906 case BFD_RELOC_MIPS_TLS_GOTTPREL:
15907 case BFD_RELOC_MIPS_TLS_TPREL32:
15908 case BFD_RELOC_MIPS_TLS_TPREL64:
15909 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15910 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15911 case BFD_RELOC_MICROMIPS_TLS_GD:
15912 case BFD_RELOC_MICROMIPS_TLS_LDM:
15913 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15914 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15915 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15916 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15917 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15918 case BFD_RELOC_MIPS16_TLS_GD:
15919 case BFD_RELOC_MIPS16_TLS_LDM:
15920 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15921 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15922 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15923 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15924 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15925 if (fixP->fx_addsy)
15926 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15927 else
15928 as_bad_where (fixP->fx_file, fixP->fx_line,
15929 _("TLS relocation against a constant"));
15930 break;
15931
15932 case BFD_RELOC_MIPS_JMP:
15933 case BFD_RELOC_MIPS16_JMP:
15934 case BFD_RELOC_MICROMIPS_JMP:
15935 {
15936 int shift;
15937
15938 gas_assert (!fixP->fx_done);
15939
15940 /* Shift is 2, unusually, for microMIPS JALX. */
15941 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15942 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15943 shift = 1;
15944 else
15945 shift = 2;
15946
15947 if (fix_bad_cross_mode_jump_p (fixP))
15948 as_bad_where (fixP->fx_file, fixP->fx_line,
15949 _("jump to a symbol in another ISA mode"));
15950 else if (fix_bad_same_mode_jalx_p (fixP))
15951 as_bad_where (fixP->fx_file, fixP->fx_line,
15952 _("JALX to a symbol in the same ISA mode"));
15953 else if (fix_bad_misaligned_jump_p (fixP, shift))
15954 as_bad_where (fixP->fx_file, fixP->fx_line,
15955 _("jump to misaligned address (0x%lx)"),
15956 (long) fix_bad_misaligned_address (fixP));
15957 else if (HAVE_IN_PLACE_ADDENDS
15958 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15959 as_bad_where (fixP->fx_file, fixP->fx_line,
15960 _("cannot encode misaligned addend "
15961 "in the relocatable field (0x%lx)"),
15962 (long) fixP->fx_offset);
15963 }
15964 /* Fall through. */
15965
15966 case BFD_RELOC_MIPS_SHIFT5:
15967 case BFD_RELOC_MIPS_SHIFT6:
15968 case BFD_RELOC_MIPS_GOT_DISP:
15969 case BFD_RELOC_MIPS_GOT_PAGE:
15970 case BFD_RELOC_MIPS_GOT_OFST:
15971 case BFD_RELOC_MIPS_SUB:
15972 case BFD_RELOC_MIPS_INSERT_A:
15973 case BFD_RELOC_MIPS_INSERT_B:
15974 case BFD_RELOC_MIPS_DELETE:
15975 case BFD_RELOC_MIPS_HIGHEST:
15976 case BFD_RELOC_MIPS_HIGHER:
15977 case BFD_RELOC_MIPS_SCN_DISP:
15978 case BFD_RELOC_MIPS_REL16:
15979 case BFD_RELOC_MIPS_RELGOT:
15980 case BFD_RELOC_MIPS_JALR:
15981 case BFD_RELOC_HI16:
15982 case BFD_RELOC_HI16_S:
15983 case BFD_RELOC_LO16:
15984 case BFD_RELOC_GPREL16:
15985 case BFD_RELOC_MIPS_LITERAL:
15986 case BFD_RELOC_MIPS_CALL16:
15987 case BFD_RELOC_MIPS_GOT16:
15988 case BFD_RELOC_GPREL32:
15989 case BFD_RELOC_MIPS_GOT_HI16:
15990 case BFD_RELOC_MIPS_GOT_LO16:
15991 case BFD_RELOC_MIPS_CALL_HI16:
15992 case BFD_RELOC_MIPS_CALL_LO16:
15993 case BFD_RELOC_HI16_S_PCREL:
15994 case BFD_RELOC_LO16_PCREL:
15995 case BFD_RELOC_MIPS16_GPREL:
15996 case BFD_RELOC_MIPS16_GOT16:
15997 case BFD_RELOC_MIPS16_CALL16:
15998 case BFD_RELOC_MIPS16_HI16:
15999 case BFD_RELOC_MIPS16_HI16_S:
16000 case BFD_RELOC_MIPS16_LO16:
16001 case BFD_RELOC_MICROMIPS_GOT_DISP:
16002 case BFD_RELOC_MICROMIPS_GOT_PAGE:
16003 case BFD_RELOC_MICROMIPS_GOT_OFST:
16004 case BFD_RELOC_MICROMIPS_SUB:
16005 case BFD_RELOC_MICROMIPS_HIGHEST:
16006 case BFD_RELOC_MICROMIPS_HIGHER:
16007 case BFD_RELOC_MICROMIPS_SCN_DISP:
16008 case BFD_RELOC_MICROMIPS_JALR:
16009 case BFD_RELOC_MICROMIPS_HI16:
16010 case BFD_RELOC_MICROMIPS_HI16_S:
16011 case BFD_RELOC_MICROMIPS_LO16:
16012 case BFD_RELOC_MICROMIPS_GPREL16:
16013 case BFD_RELOC_MICROMIPS_LITERAL:
16014 case BFD_RELOC_MICROMIPS_CALL16:
16015 case BFD_RELOC_MICROMIPS_GOT16:
16016 case BFD_RELOC_MICROMIPS_GOT_HI16:
16017 case BFD_RELOC_MICROMIPS_GOT_LO16:
16018 case BFD_RELOC_MICROMIPS_CALL_HI16:
16019 case BFD_RELOC_MICROMIPS_CALL_LO16:
16020 case BFD_RELOC_MIPS_EH:
16021 if (fixP->fx_done)
16022 {
16023 offsetT value;
16024
16025 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
16026 {
16027 insn = read_reloc_insn (buf, fixP->fx_r_type);
16028 if (mips16_reloc_p (fixP->fx_r_type))
16029 insn |= mips16_immed_extend (value, 16);
16030 else
16031 insn |= (value & 0xffff);
16032 write_reloc_insn (buf, fixP->fx_r_type, insn);
16033 }
16034 else
16035 as_bad_where (fixP->fx_file, fixP->fx_line,
16036 _("unsupported constant in relocation"));
16037 }
16038 break;
16039
16040 case BFD_RELOC_64:
16041 /* This is handled like BFD_RELOC_32, but we output a sign
16042 extended value if we are only 32 bits. */
16043 if (fixP->fx_done)
16044 {
16045 if (8 <= sizeof (valueT))
16046 md_number_to_chars (buf, *valP, 8);
16047 else
16048 {
16049 valueT hiv;
16050
16051 if ((*valP & 0x80000000) != 0)
16052 hiv = 0xffffffff;
16053 else
16054 hiv = 0;
16055 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
16056 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
16057 }
16058 }
16059 break;
16060
16061 case BFD_RELOC_RVA:
16062 case BFD_RELOC_32:
16063 case BFD_RELOC_32_PCREL:
16064 case BFD_RELOC_16:
16065 case BFD_RELOC_8:
16066 /* If we are deleting this reloc entry, we must fill in the
16067 value now. This can happen if we have a .word which is not
16068 resolved when it appears but is later defined. */
16069 if (fixP->fx_done)
16070 md_number_to_chars (buf, *valP, fixP->fx_size);
16071 break;
16072
16073 case BFD_RELOC_MIPS_21_PCREL_S2:
16074 fix_validate_branch (fixP, *valP);
16075 if (!fixP->fx_done)
16076 break;
16077
16078 if (*valP + 0x400000 <= 0x7fffff)
16079 {
16080 insn = read_insn (buf);
16081 insn |= (*valP >> 2) & 0x1fffff;
16082 write_insn (buf, insn);
16083 }
16084 else
16085 as_bad_where (fixP->fx_file, fixP->fx_line,
16086 _("branch out of range"));
16087 break;
16088
16089 case BFD_RELOC_MIPS_26_PCREL_S2:
16090 fix_validate_branch (fixP, *valP);
16091 if (!fixP->fx_done)
16092 break;
16093
16094 if (*valP + 0x8000000 <= 0xfffffff)
16095 {
16096 insn = read_insn (buf);
16097 insn |= (*valP >> 2) & 0x3ffffff;
16098 write_insn (buf, insn);
16099 }
16100 else
16101 as_bad_where (fixP->fx_file, fixP->fx_line,
16102 _("branch out of range"));
16103 break;
16104
16105 case BFD_RELOC_MIPS_18_PCREL_S3:
16106 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
16107 as_bad_where (fixP->fx_file, fixP->fx_line,
16108 _("PC-relative access using misaligned symbol (%lx)"),
16109 (long) S_GET_VALUE (fixP->fx_addsy));
16110 if ((fixP->fx_offset & 0x7) != 0)
16111 as_bad_where (fixP->fx_file, fixP->fx_line,
16112 _("PC-relative access using misaligned offset (%lx)"),
16113 (long) fixP->fx_offset);
16114 if (!fixP->fx_done)
16115 break;
16116
16117 if (*valP + 0x100000 <= 0x1fffff)
16118 {
16119 insn = read_insn (buf);
16120 insn |= (*valP >> 3) & 0x3ffff;
16121 write_insn (buf, insn);
16122 }
16123 else
16124 as_bad_where (fixP->fx_file, fixP->fx_line,
16125 _("PC-relative access out of range"));
16126 break;
16127
16128 case BFD_RELOC_MIPS_19_PCREL_S2:
16129 if ((*valP & 0x3) != 0)
16130 as_bad_where (fixP->fx_file, fixP->fx_line,
16131 _("PC-relative access to misaligned address (%lx)"),
16132 (long) *valP);
16133 if (!fixP->fx_done)
16134 break;
16135
16136 if (*valP + 0x100000 <= 0x1fffff)
16137 {
16138 insn = read_insn (buf);
16139 insn |= (*valP >> 2) & 0x7ffff;
16140 write_insn (buf, insn);
16141 }
16142 else
16143 as_bad_where (fixP->fx_file, fixP->fx_line,
16144 _("PC-relative access out of range"));
16145 break;
16146
16147 case BFD_RELOC_16_PCREL_S2:
16148 fix_validate_branch (fixP, *valP);
16149
16150 /* We need to save the bits in the instruction since fixup_segment()
16151 might be deleting the relocation entry (i.e., a branch within
16152 the current segment). */
16153 if (! fixP->fx_done)
16154 break;
16155
16156 /* Update old instruction data. */
16157 insn = read_insn (buf);
16158
16159 if (*valP + 0x20000 <= 0x3ffff)
16160 {
16161 insn |= (*valP >> 2) & 0xffff;
16162 write_insn (buf, insn);
16163 }
16164 else if (fixP->fx_tcbit2
16165 && fixP->fx_done
16166 && fixP->fx_frag->fr_address >= text_section->vma
16167 && (fixP->fx_frag->fr_address
16168 < text_section->vma + bfd_section_size (text_section))
16169 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
16170 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
16171 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
16172 {
16173 /* The branch offset is too large. If this is an
16174 unconditional branch, and we are not generating PIC code,
16175 we can convert it to an absolute jump instruction. */
16176 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
16177 insn = 0x0c000000; /* jal */
16178 else
16179 insn = 0x08000000; /* j */
16180 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
16181 fixP->fx_done = 0;
16182 fixP->fx_addsy = section_symbol (text_section);
16183 *valP += md_pcrel_from (fixP);
16184 write_insn (buf, insn);
16185 }
16186 else
16187 {
16188 /* If we got here, we have branch-relaxation disabled,
16189 and there's nothing we can do to fix this instruction
16190 without turning it into a longer sequence. */
16191 as_bad_where (fixP->fx_file, fixP->fx_line,
16192 _("branch out of range"));
16193 }
16194 break;
16195
16196 case BFD_RELOC_MIPS16_16_PCREL_S1:
16197 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16198 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16199 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
16200 gas_assert (!fixP->fx_done);
16201 if (fix_bad_cross_mode_branch_p (fixP))
16202 as_bad_where (fixP->fx_file, fixP->fx_line,
16203 _("branch to a symbol in another ISA mode"));
16204 else if (fixP->fx_addsy
16205 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
16206 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
16207 && (fixP->fx_offset & 0x1) != 0)
16208 as_bad_where (fixP->fx_file, fixP->fx_line,
16209 _("branch to misaligned address (0x%lx)"),
16210 (long) fix_bad_misaligned_address (fixP));
16211 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
16212 as_bad_where (fixP->fx_file, fixP->fx_line,
16213 _("cannot encode misaligned addend "
16214 "in the relocatable field (0x%lx)"),
16215 (long) fixP->fx_offset);
16216 break;
16217
16218 case BFD_RELOC_VTABLE_INHERIT:
16219 fixP->fx_done = 0;
16220 if (fixP->fx_addsy
16221 && !S_IS_DEFINED (fixP->fx_addsy)
16222 && !S_IS_WEAK (fixP->fx_addsy))
16223 S_SET_WEAK (fixP->fx_addsy);
16224 break;
16225
16226 case BFD_RELOC_NONE:
16227 case BFD_RELOC_VTABLE_ENTRY:
16228 fixP->fx_done = 0;
16229 break;
16230
16231 default:
16232 abort ();
16233 }
16234
16235 /* Remember value for tc_gen_reloc. */
16236 fixP->fx_addnumber = *valP;
16237 }
16238
16239 static symbolS *
16240 get_symbol (void)
16241 {
16242 int c;
16243 char *name;
16244 symbolS *p;
16245
16246 c = get_symbol_name (&name);
16247 p = (symbolS *) symbol_find_or_make (name);
16248 (void) restore_line_pointer (c);
16249 return p;
16250 }
16251
16252 /* Align the current frag to a given power of two. If a particular
16253 fill byte should be used, FILL points to an integer that contains
16254 that byte, otherwise FILL is null.
16255
16256 This function used to have the comment:
16257
16258 The MIPS assembler also automatically adjusts any preceding label.
16259
16260 The implementation therefore applied the adjustment to a maximum of
16261 one label. However, other label adjustments are applied to batches
16262 of labels, and adjusting just one caused problems when new labels
16263 were added for the sake of debugging or unwind information.
16264 We therefore adjust all preceding labels (given as LABELS) instead. */
16265
16266 static void
16267 mips_align (int to, int *fill, struct insn_label_list *labels)
16268 {
16269 mips_emit_delays ();
16270 mips_record_compressed_mode ();
16271 if (fill == NULL && subseg_text_p (now_seg))
16272 frag_align_code (to, 0);
16273 else
16274 frag_align (to, fill ? *fill : 0, 0);
16275 record_alignment (now_seg, to);
16276 mips_move_labels (labels, subseg_text_p (now_seg));
16277 }
16278
16279 /* Align to a given power of two. .align 0 turns off the automatic
16280 alignment used by the data creating pseudo-ops. */
16281
16282 static void
16283 s_align (int x ATTRIBUTE_UNUSED)
16284 {
16285 int temp, fill_value, *fill_ptr;
16286 long max_alignment = 28;
16287
16288 /* o Note that the assembler pulls down any immediately preceding label
16289 to the aligned address.
16290 o It's not documented but auto alignment is reinstated by
16291 a .align pseudo instruction.
16292 o Note also that after auto alignment is turned off the mips assembler
16293 issues an error on attempt to assemble an improperly aligned data item.
16294 We don't. */
16295
16296 temp = get_absolute_expression ();
16297 if (temp > max_alignment)
16298 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
16299 else if (temp < 0)
16300 {
16301 as_warn (_("alignment negative, 0 assumed"));
16302 temp = 0;
16303 }
16304 if (*input_line_pointer == ',')
16305 {
16306 ++input_line_pointer;
16307 fill_value = get_absolute_expression ();
16308 fill_ptr = &fill_value;
16309 }
16310 else
16311 fill_ptr = 0;
16312 if (temp)
16313 {
16314 segment_info_type *si = seg_info (now_seg);
16315 struct insn_label_list *l = si->label_list;
16316 /* Auto alignment should be switched on by next section change. */
16317 auto_align = 1;
16318 mips_align (temp, fill_ptr, l);
16319 }
16320 else
16321 {
16322 auto_align = 0;
16323 }
16324
16325 demand_empty_rest_of_line ();
16326 }
16327
16328 static void
16329 s_change_sec (int sec)
16330 {
16331 segT seg;
16332
16333 /* The ELF backend needs to know that we are changing sections, so
16334 that .previous works correctly. We could do something like check
16335 for an obj_section_change_hook macro, but that might be confusing
16336 as it would not be appropriate to use it in the section changing
16337 functions in read.c, since obj-elf.c intercepts those. FIXME:
16338 This should be cleaner, somehow. */
16339 obj_elf_section_change_hook ();
16340
16341 mips_emit_delays ();
16342
16343 switch (sec)
16344 {
16345 case 't':
16346 s_text (0);
16347 break;
16348 case 'd':
16349 s_data (0);
16350 break;
16351 case 'b':
16352 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16353 demand_empty_rest_of_line ();
16354 break;
16355
16356 case 'r':
16357 seg = subseg_new (RDATA_SECTION_NAME,
16358 (subsegT) get_absolute_expression ());
16359 bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_READONLY
16360 | SEC_RELOC | SEC_DATA));
16361 if (strncmp (TARGET_OS, "elf", 3) != 0)
16362 record_alignment (seg, 4);
16363 demand_empty_rest_of_line ();
16364 break;
16365
16366 case 's':
16367 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
16368 bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_RELOC
16369 | SEC_DATA | SEC_SMALL_DATA));
16370 if (strncmp (TARGET_OS, "elf", 3) != 0)
16371 record_alignment (seg, 4);
16372 demand_empty_rest_of_line ();
16373 break;
16374
16375 case 'B':
16376 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16377 bfd_set_section_flags (seg, SEC_ALLOC | SEC_SMALL_DATA);
16378 if (strncmp (TARGET_OS, "elf", 3) != 0)
16379 record_alignment (seg, 4);
16380 demand_empty_rest_of_line ();
16381 break;
16382 }
16383
16384 auto_align = 1;
16385 }
16386
16387 void
16388 s_change_section (int ignore ATTRIBUTE_UNUSED)
16389 {
16390 char *saved_ilp;
16391 char *section_name;
16392 char c, endc;
16393 char next_c = 0;
16394 int section_type;
16395 int section_flag;
16396 int section_entry_size;
16397 int section_alignment;
16398
16399 saved_ilp = input_line_pointer;
16400 endc = get_symbol_name (&section_name);
16401 c = (endc == '"' ? input_line_pointer[1] : endc);
16402 if (c)
16403 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
16404
16405 /* Do we have .section Name<,"flags">? */
16406 if (c != ',' || (c == ',' && next_c == '"'))
16407 {
16408 /* Just after name is now '\0'. */
16409 (void) restore_line_pointer (endc);
16410 input_line_pointer = saved_ilp;
16411 obj_elf_section (ignore);
16412 return;
16413 }
16414
16415 section_name = xstrdup (section_name);
16416 c = restore_line_pointer (endc);
16417
16418 input_line_pointer++;
16419
16420 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16421 if (c == ',')
16422 section_type = get_absolute_expression ();
16423 else
16424 section_type = 0;
16425
16426 if (*input_line_pointer++ == ',')
16427 section_flag = get_absolute_expression ();
16428 else
16429 section_flag = 0;
16430
16431 if (*input_line_pointer++ == ',')
16432 section_entry_size = get_absolute_expression ();
16433 else
16434 section_entry_size = 0;
16435
16436 if (*input_line_pointer++ == ',')
16437 section_alignment = get_absolute_expression ();
16438 else
16439 section_alignment = 0;
16440
16441 /* FIXME: really ignore? */
16442 (void) section_alignment;
16443
16444 /* When using the generic form of .section (as implemented by obj-elf.c),
16445 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16446 traditionally had to fall back on the more common @progbits instead.
16447
16448 There's nothing really harmful in this, since bfd will correct
16449 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
16450 means that, for backwards compatibility, the special_section entries
16451 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16452
16453 Even so, we shouldn't force users of the MIPS .section syntax to
16454 incorrectly label the sections as SHT_PROGBITS. The best compromise
16455 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16456 generic type-checking code. */
16457 if (section_type == SHT_MIPS_DWARF)
16458 section_type = SHT_PROGBITS;
16459
16460 obj_elf_change_section (section_name, section_type, section_flag,
16461 section_entry_size, 0, 0, 0);
16462
16463 if (now_seg->name != section_name)
16464 free (section_name);
16465 }
16466
16467 void
16468 mips_enable_auto_align (void)
16469 {
16470 auto_align = 1;
16471 }
16472
16473 static void
16474 s_cons (int log_size)
16475 {
16476 segment_info_type *si = seg_info (now_seg);
16477 struct insn_label_list *l = si->label_list;
16478
16479 mips_emit_delays ();
16480 if (log_size > 0 && auto_align)
16481 mips_align (log_size, 0, l);
16482 cons (1 << log_size);
16483 mips_clear_insn_labels ();
16484 }
16485
16486 static void
16487 s_float_cons (int type)
16488 {
16489 segment_info_type *si = seg_info (now_seg);
16490 struct insn_label_list *l = si->label_list;
16491
16492 mips_emit_delays ();
16493
16494 if (auto_align)
16495 {
16496 if (type == 'd')
16497 mips_align (3, 0, l);
16498 else
16499 mips_align (2, 0, l);
16500 }
16501
16502 float_cons (type);
16503 mips_clear_insn_labels ();
16504 }
16505
16506 /* Handle .globl. We need to override it because on Irix 5 you are
16507 permitted to say
16508 .globl foo .text
16509 where foo is an undefined symbol, to mean that foo should be
16510 considered to be the address of a function. */
16511
16512 static void
16513 s_mips_globl (int x ATTRIBUTE_UNUSED)
16514 {
16515 char *name;
16516 int c;
16517 symbolS *symbolP;
16518
16519 do
16520 {
16521 c = get_symbol_name (&name);
16522 symbolP = symbol_find_or_make (name);
16523 S_SET_EXTERNAL (symbolP);
16524
16525 *input_line_pointer = c;
16526 SKIP_WHITESPACE_AFTER_NAME ();
16527
16528 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16529 && (*input_line_pointer != ','))
16530 {
16531 char *secname;
16532 asection *sec;
16533
16534 c = get_symbol_name (&secname);
16535 sec = bfd_get_section_by_name (stdoutput, secname);
16536 if (sec == NULL)
16537 as_bad (_("%s: no such section"), secname);
16538 (void) restore_line_pointer (c);
16539
16540 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16541 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
16542 }
16543
16544 c = *input_line_pointer;
16545 if (c == ',')
16546 {
16547 input_line_pointer++;
16548 SKIP_WHITESPACE ();
16549 if (is_end_of_line[(unsigned char) *input_line_pointer])
16550 c = '\n';
16551 }
16552 }
16553 while (c == ',');
16554
16555 demand_empty_rest_of_line ();
16556 }
16557
16558 #ifdef TE_IRIX
16559 /* The Irix 5 and 6 assemblers set the type of any common symbol and
16560 any undefined non-function symbol to STT_OBJECT. We try to be
16561 compatible, since newer Irix 5 and 6 linkers care. */
16562
16563 void
16564 mips_frob_symbol (symbolS *symp ATTRIBUTE_UNUSED)
16565 {
16566 /* This late in assembly we can set BSF_OBJECT indiscriminately
16567 and let elf.c:swap_out_syms sort out the symbol type. */
16568 flagword *flags = &symbol_get_bfdsym (symp)->flags;
16569 if ((*flags & (BSF_GLOBAL | BSF_WEAK)) != 0
16570 || !S_IS_DEFINED (symp))
16571 *flags |= BSF_OBJECT;
16572 }
16573 #endif
16574
16575 static void
16576 s_option (int x ATTRIBUTE_UNUSED)
16577 {
16578 char *opt;
16579 char c;
16580
16581 c = get_symbol_name (&opt);
16582
16583 if (*opt == 'O')
16584 {
16585 /* FIXME: What does this mean? */
16586 }
16587 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
16588 {
16589 int i;
16590
16591 i = atoi (opt + 3);
16592 if (i != 0 && i != 2)
16593 as_bad (_(".option pic%d not supported"), i);
16594 else if (mips_pic == VXWORKS_PIC)
16595 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16596 else if (i == 0)
16597 mips_pic = NO_PIC;
16598 else if (i == 2)
16599 {
16600 mips_pic = SVR4_PIC;
16601 mips_abicalls = TRUE;
16602 }
16603
16604 if (mips_pic == SVR4_PIC)
16605 {
16606 if (g_switch_seen && g_switch_value != 0)
16607 as_warn (_("-G may not be used with SVR4 PIC code"));
16608 g_switch_value = 0;
16609 bfd_set_gp_size (stdoutput, 0);
16610 }
16611 }
16612 else
16613 as_warn (_("unrecognized option \"%s\""), opt);
16614
16615 (void) restore_line_pointer (c);
16616 demand_empty_rest_of_line ();
16617 }
16618
16619 /* This structure is used to hold a stack of .set values. */
16620
16621 struct mips_option_stack
16622 {
16623 struct mips_option_stack *next;
16624 struct mips_set_options options;
16625 };
16626
16627 static struct mips_option_stack *mips_opts_stack;
16628
16629 /* Return status for .set/.module option handling. */
16630
16631 enum code_option_type
16632 {
16633 /* Unrecognized option. */
16634 OPTION_TYPE_BAD = -1,
16635
16636 /* Ordinary option. */
16637 OPTION_TYPE_NORMAL,
16638
16639 /* ISA changing option. */
16640 OPTION_TYPE_ISA
16641 };
16642
16643 /* Handle common .set/.module options. Return status indicating option
16644 type. */
16645
16646 static enum code_option_type
16647 parse_code_option (char * name)
16648 {
16649 bfd_boolean isa_set = FALSE;
16650 const struct mips_ase *ase;
16651
16652 if (strncmp (name, "at=", 3) == 0)
16653 {
16654 char *s = name + 3;
16655
16656 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16657 as_bad (_("unrecognized register name `%s'"), s);
16658 }
16659 else if (strcmp (name, "at") == 0)
16660 mips_opts.at = ATREG;
16661 else if (strcmp (name, "noat") == 0)
16662 mips_opts.at = ZERO;
16663 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16664 mips_opts.nomove = 0;
16665 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16666 mips_opts.nomove = 1;
16667 else if (strcmp (name, "bopt") == 0)
16668 mips_opts.nobopt = 0;
16669 else if (strcmp (name, "nobopt") == 0)
16670 mips_opts.nobopt = 1;
16671 else if (strcmp (name, "gp=32") == 0)
16672 mips_opts.gp = 32;
16673 else if (strcmp (name, "gp=64") == 0)
16674 mips_opts.gp = 64;
16675 else if (strcmp (name, "fp=32") == 0)
16676 mips_opts.fp = 32;
16677 else if (strcmp (name, "fp=xx") == 0)
16678 mips_opts.fp = 0;
16679 else if (strcmp (name, "fp=64") == 0)
16680 mips_opts.fp = 64;
16681 else if (strcmp (name, "softfloat") == 0)
16682 mips_opts.soft_float = 1;
16683 else if (strcmp (name, "hardfloat") == 0)
16684 mips_opts.soft_float = 0;
16685 else if (strcmp (name, "singlefloat") == 0)
16686 mips_opts.single_float = 1;
16687 else if (strcmp (name, "doublefloat") == 0)
16688 mips_opts.single_float = 0;
16689 else if (strcmp (name, "nooddspreg") == 0)
16690 mips_opts.oddspreg = 0;
16691 else if (strcmp (name, "oddspreg") == 0)
16692 mips_opts.oddspreg = 1;
16693 else if (strcmp (name, "mips16") == 0
16694 || strcmp (name, "MIPS-16") == 0)
16695 mips_opts.mips16 = 1;
16696 else if (strcmp (name, "nomips16") == 0
16697 || strcmp (name, "noMIPS-16") == 0)
16698 mips_opts.mips16 = 0;
16699 else if (strcmp (name, "micromips") == 0)
16700 mips_opts.micromips = 1;
16701 else if (strcmp (name, "nomicromips") == 0)
16702 mips_opts.micromips = 0;
16703 else if (name[0] == 'n'
16704 && name[1] == 'o'
16705 && (ase = mips_lookup_ase (name + 2)))
16706 mips_set_ase (ase, &mips_opts, FALSE);
16707 else if ((ase = mips_lookup_ase (name)))
16708 mips_set_ase (ase, &mips_opts, TRUE);
16709 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16710 {
16711 /* Permit the user to change the ISA and architecture on the fly.
16712 Needless to say, misuse can cause serious problems. */
16713 if (strncmp (name, "arch=", 5) == 0)
16714 {
16715 const struct mips_cpu_info *p;
16716
16717 p = mips_parse_cpu ("internal use", name + 5);
16718 if (!p)
16719 as_bad (_("unknown architecture %s"), name + 5);
16720 else
16721 {
16722 mips_opts.arch = p->cpu;
16723 mips_opts.isa = p->isa;
16724 isa_set = TRUE;
16725 mips_opts.init_ase = p->ase;
16726 }
16727 }
16728 else if (strncmp (name, "mips", 4) == 0)
16729 {
16730 const struct mips_cpu_info *p;
16731
16732 p = mips_parse_cpu ("internal use", name);
16733 if (!p)
16734 as_bad (_("unknown ISA level %s"), name + 4);
16735 else
16736 {
16737 mips_opts.arch = p->cpu;
16738 mips_opts.isa = p->isa;
16739 isa_set = TRUE;
16740 mips_opts.init_ase = p->ase;
16741 }
16742 }
16743 else
16744 as_bad (_("unknown ISA or architecture %s"), name);
16745 }
16746 else if (strcmp (name, "autoextend") == 0)
16747 mips_opts.noautoextend = 0;
16748 else if (strcmp (name, "noautoextend") == 0)
16749 mips_opts.noautoextend = 1;
16750 else if (strcmp (name, "insn32") == 0)
16751 mips_opts.insn32 = TRUE;
16752 else if (strcmp (name, "noinsn32") == 0)
16753 mips_opts.insn32 = FALSE;
16754 else if (strcmp (name, "sym32") == 0)
16755 mips_opts.sym32 = TRUE;
16756 else if (strcmp (name, "nosym32") == 0)
16757 mips_opts.sym32 = FALSE;
16758 else
16759 return OPTION_TYPE_BAD;
16760
16761 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
16762 }
16763
16764 /* Handle the .set pseudo-op. */
16765
16766 static void
16767 s_mipsset (int x ATTRIBUTE_UNUSED)
16768 {
16769 enum code_option_type type = OPTION_TYPE_NORMAL;
16770 char *name = input_line_pointer, ch;
16771
16772 file_mips_check_options ();
16773
16774 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16775 ++input_line_pointer;
16776 ch = *input_line_pointer;
16777 *input_line_pointer = '\0';
16778
16779 if (strchr (name, ','))
16780 {
16781 /* Generic ".set" directive; use the generic handler. */
16782 *input_line_pointer = ch;
16783 input_line_pointer = name;
16784 s_set (0);
16785 return;
16786 }
16787
16788 if (strcmp (name, "reorder") == 0)
16789 {
16790 if (mips_opts.noreorder)
16791 end_noreorder ();
16792 }
16793 else if (strcmp (name, "noreorder") == 0)
16794 {
16795 if (!mips_opts.noreorder)
16796 start_noreorder ();
16797 }
16798 else if (strcmp (name, "macro") == 0)
16799 mips_opts.warn_about_macros = 0;
16800 else if (strcmp (name, "nomacro") == 0)
16801 {
16802 if (mips_opts.noreorder == 0)
16803 as_bad (_("`noreorder' must be set before `nomacro'"));
16804 mips_opts.warn_about_macros = 1;
16805 }
16806 else if (strcmp (name, "gp=default") == 0)
16807 mips_opts.gp = file_mips_opts.gp;
16808 else if (strcmp (name, "fp=default") == 0)
16809 mips_opts.fp = file_mips_opts.fp;
16810 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16811 {
16812 mips_opts.isa = file_mips_opts.isa;
16813 mips_opts.arch = file_mips_opts.arch;
16814 mips_opts.init_ase = file_mips_opts.init_ase;
16815 mips_opts.gp = file_mips_opts.gp;
16816 mips_opts.fp = file_mips_opts.fp;
16817 }
16818 else if (strcmp (name, "push") == 0)
16819 {
16820 struct mips_option_stack *s;
16821
16822 s = XNEW (struct mips_option_stack);
16823 s->next = mips_opts_stack;
16824 s->options = mips_opts;
16825 mips_opts_stack = s;
16826 }
16827 else if (strcmp (name, "pop") == 0)
16828 {
16829 struct mips_option_stack *s;
16830
16831 s = mips_opts_stack;
16832 if (s == NULL)
16833 as_bad (_(".set pop with no .set push"));
16834 else
16835 {
16836 /* If we're changing the reorder mode we need to handle
16837 delay slots correctly. */
16838 if (s->options.noreorder && ! mips_opts.noreorder)
16839 start_noreorder ();
16840 else if (! s->options.noreorder && mips_opts.noreorder)
16841 end_noreorder ();
16842
16843 mips_opts = s->options;
16844 mips_opts_stack = s->next;
16845 free (s);
16846 }
16847 }
16848 else
16849 {
16850 type = parse_code_option (name);
16851 if (type == OPTION_TYPE_BAD)
16852 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16853 }
16854
16855 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16856 registers based on what is supported by the arch/cpu. */
16857 if (type == OPTION_TYPE_ISA)
16858 {
16859 switch (mips_opts.isa)
16860 {
16861 case 0:
16862 break;
16863 case ISA_MIPS1:
16864 /* MIPS I cannot support FPXX. */
16865 mips_opts.fp = 32;
16866 /* fall-through. */
16867 case ISA_MIPS2:
16868 case ISA_MIPS32:
16869 case ISA_MIPS32R2:
16870 case ISA_MIPS32R3:
16871 case ISA_MIPS32R5:
16872 mips_opts.gp = 32;
16873 if (mips_opts.fp != 0)
16874 mips_opts.fp = 32;
16875 break;
16876 case ISA_MIPS32R6:
16877 mips_opts.gp = 32;
16878 mips_opts.fp = 64;
16879 break;
16880 case ISA_MIPS3:
16881 case ISA_MIPS4:
16882 case ISA_MIPS5:
16883 case ISA_MIPS64:
16884 case ISA_MIPS64R2:
16885 case ISA_MIPS64R3:
16886 case ISA_MIPS64R5:
16887 case ISA_MIPS64R6:
16888 mips_opts.gp = 64;
16889 if (mips_opts.fp != 0)
16890 {
16891 if (mips_opts.arch == CPU_R5900)
16892 mips_opts.fp = 32;
16893 else
16894 mips_opts.fp = 64;
16895 }
16896 break;
16897 default:
16898 as_bad (_("unknown ISA level %s"), name + 4);
16899 break;
16900 }
16901 }
16902
16903 mips_check_options (&mips_opts, FALSE);
16904
16905 mips_check_isa_supports_ases ();
16906 *input_line_pointer = ch;
16907 demand_empty_rest_of_line ();
16908 }
16909
16910 /* Handle the .module pseudo-op. */
16911
16912 static void
16913 s_module (int ignore ATTRIBUTE_UNUSED)
16914 {
16915 char *name = input_line_pointer, ch;
16916
16917 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16918 ++input_line_pointer;
16919 ch = *input_line_pointer;
16920 *input_line_pointer = '\0';
16921
16922 if (!file_mips_opts_checked)
16923 {
16924 if (parse_code_option (name) == OPTION_TYPE_BAD)
16925 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16926
16927 /* Update module level settings from mips_opts. */
16928 file_mips_opts = mips_opts;
16929 }
16930 else
16931 as_bad (_(".module is not permitted after generating code"));
16932
16933 *input_line_pointer = ch;
16934 demand_empty_rest_of_line ();
16935 }
16936
16937 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
16938 .option pic2. It means to generate SVR4 PIC calls. */
16939
16940 static void
16941 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16942 {
16943 mips_pic = SVR4_PIC;
16944 mips_abicalls = TRUE;
16945
16946 if (g_switch_seen && g_switch_value != 0)
16947 as_warn (_("-G may not be used with SVR4 PIC code"));
16948 g_switch_value = 0;
16949
16950 bfd_set_gp_size (stdoutput, 0);
16951 demand_empty_rest_of_line ();
16952 }
16953
16954 /* Handle the .cpload pseudo-op. This is used when generating SVR4
16955 PIC code. It sets the $gp register for the function based on the
16956 function address, which is in the register named in the argument.
16957 This uses a relocation against _gp_disp, which is handled specially
16958 by the linker. The result is:
16959 lui $gp,%hi(_gp_disp)
16960 addiu $gp,$gp,%lo(_gp_disp)
16961 addu $gp,$gp,.cpload argument
16962 The .cpload argument is normally $25 == $t9.
16963
16964 The -mno-shared option changes this to:
16965 lui $gp,%hi(__gnu_local_gp)
16966 addiu $gp,$gp,%lo(__gnu_local_gp)
16967 and the argument is ignored. This saves an instruction, but the
16968 resulting code is not position independent; it uses an absolute
16969 address for __gnu_local_gp. Thus code assembled with -mno-shared
16970 can go into an ordinary executable, but not into a shared library. */
16971
16972 static void
16973 s_cpload (int ignore ATTRIBUTE_UNUSED)
16974 {
16975 expressionS ex;
16976 int reg;
16977 int in_shared;
16978
16979 file_mips_check_options ();
16980
16981 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16982 .cpload is ignored. */
16983 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16984 {
16985 s_ignore (0);
16986 return;
16987 }
16988
16989 if (mips_opts.mips16)
16990 {
16991 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16992 ignore_rest_of_line ();
16993 return;
16994 }
16995
16996 /* .cpload should be in a .set noreorder section. */
16997 if (mips_opts.noreorder == 0)
16998 as_warn (_(".cpload not in noreorder section"));
16999
17000 reg = tc_get_register (0);
17001
17002 /* If we need to produce a 64-bit address, we are better off using
17003 the default instruction sequence. */
17004 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
17005
17006 ex.X_op = O_symbol;
17007 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
17008 "__gnu_local_gp");
17009 ex.X_op_symbol = NULL;
17010 ex.X_add_number = 0;
17011
17012 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
17013 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
17014
17015 mips_mark_labels ();
17016 mips_assembling_insn = TRUE;
17017
17018 macro_start ();
17019 macro_build_lui (&ex, mips_gp_register);
17020 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17021 mips_gp_register, BFD_RELOC_LO16);
17022 if (in_shared)
17023 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
17024 mips_gp_register, reg);
17025 macro_end ();
17026
17027 mips_assembling_insn = FALSE;
17028 demand_empty_rest_of_line ();
17029 }
17030
17031 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
17032 .cpsetup $reg1, offset|$reg2, label
17033
17034 If offset is given, this results in:
17035 sd $gp, offset($sp)
17036 lui $gp, %hi(%neg(%gp_rel(label)))
17037 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17038 daddu $gp, $gp, $reg1
17039
17040 If $reg2 is given, this results in:
17041 or $reg2, $gp, $0
17042 lui $gp, %hi(%neg(%gp_rel(label)))
17043 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17044 daddu $gp, $gp, $reg1
17045 $reg1 is normally $25 == $t9.
17046
17047 The -mno-shared option replaces the last three instructions with
17048 lui $gp,%hi(_gp)
17049 addiu $gp,$gp,%lo(_gp) */
17050
17051 static void
17052 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
17053 {
17054 expressionS ex_off;
17055 expressionS ex_sym;
17056 int reg1;
17057
17058 file_mips_check_options ();
17059
17060 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
17061 We also need NewABI support. */
17062 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17063 {
17064 s_ignore (0);
17065 return;
17066 }
17067
17068 if (mips_opts.mips16)
17069 {
17070 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
17071 ignore_rest_of_line ();
17072 return;
17073 }
17074
17075 reg1 = tc_get_register (0);
17076 SKIP_WHITESPACE ();
17077 if (*input_line_pointer != ',')
17078 {
17079 as_bad (_("missing argument separator ',' for .cpsetup"));
17080 return;
17081 }
17082 else
17083 ++input_line_pointer;
17084 SKIP_WHITESPACE ();
17085 if (*input_line_pointer == '$')
17086 {
17087 mips_cpreturn_register = tc_get_register (0);
17088 mips_cpreturn_offset = -1;
17089 }
17090 else
17091 {
17092 mips_cpreturn_offset = get_absolute_expression ();
17093 mips_cpreturn_register = -1;
17094 }
17095 SKIP_WHITESPACE ();
17096 if (*input_line_pointer != ',')
17097 {
17098 as_bad (_("missing argument separator ',' for .cpsetup"));
17099 return;
17100 }
17101 else
17102 ++input_line_pointer;
17103 SKIP_WHITESPACE ();
17104 expression (&ex_sym);
17105
17106 mips_mark_labels ();
17107 mips_assembling_insn = TRUE;
17108
17109 macro_start ();
17110 if (mips_cpreturn_register == -1)
17111 {
17112 ex_off.X_op = O_constant;
17113 ex_off.X_add_symbol = NULL;
17114 ex_off.X_op_symbol = NULL;
17115 ex_off.X_add_number = mips_cpreturn_offset;
17116
17117 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17118 BFD_RELOC_LO16, SP);
17119 }
17120 else
17121 move_register (mips_cpreturn_register, mips_gp_register);
17122
17123 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
17124 {
17125 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
17126 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
17127 BFD_RELOC_HI16_S);
17128
17129 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
17130 mips_gp_register, -1, BFD_RELOC_GPREL16,
17131 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
17132
17133 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
17134 mips_gp_register, reg1);
17135 }
17136 else
17137 {
17138 expressionS ex;
17139
17140 ex.X_op = O_symbol;
17141 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
17142 ex.X_op_symbol = NULL;
17143 ex.X_add_number = 0;
17144
17145 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
17146 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
17147
17148 macro_build_lui (&ex, mips_gp_register);
17149 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17150 mips_gp_register, BFD_RELOC_LO16);
17151 }
17152
17153 macro_end ();
17154
17155 mips_assembling_insn = FALSE;
17156 demand_empty_rest_of_line ();
17157 }
17158
17159 static void
17160 s_cplocal (int ignore ATTRIBUTE_UNUSED)
17161 {
17162 file_mips_check_options ();
17163
17164 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
17165 .cplocal is ignored. */
17166 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17167 {
17168 s_ignore (0);
17169 return;
17170 }
17171
17172 if (mips_opts.mips16)
17173 {
17174 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
17175 ignore_rest_of_line ();
17176 return;
17177 }
17178
17179 mips_gp_register = tc_get_register (0);
17180 demand_empty_rest_of_line ();
17181 }
17182
17183 /* Handle the .cprestore pseudo-op. This stores $gp into a given
17184 offset from $sp. The offset is remembered, and after making a PIC
17185 call $gp is restored from that location. */
17186
17187 static void
17188 s_cprestore (int ignore ATTRIBUTE_UNUSED)
17189 {
17190 expressionS ex;
17191
17192 file_mips_check_options ();
17193
17194 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
17195 .cprestore is ignored. */
17196 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
17197 {
17198 s_ignore (0);
17199 return;
17200 }
17201
17202 if (mips_opts.mips16)
17203 {
17204 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
17205 ignore_rest_of_line ();
17206 return;
17207 }
17208
17209 mips_cprestore_offset = get_absolute_expression ();
17210 mips_cprestore_valid = 1;
17211
17212 ex.X_op = O_constant;
17213 ex.X_add_symbol = NULL;
17214 ex.X_op_symbol = NULL;
17215 ex.X_add_number = mips_cprestore_offset;
17216
17217 mips_mark_labels ();
17218 mips_assembling_insn = TRUE;
17219
17220 macro_start ();
17221 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
17222 SP, HAVE_64BIT_ADDRESSES);
17223 macro_end ();
17224
17225 mips_assembling_insn = FALSE;
17226 demand_empty_rest_of_line ();
17227 }
17228
17229 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
17230 was given in the preceding .cpsetup, it results in:
17231 ld $gp, offset($sp)
17232
17233 If a register $reg2 was given there, it results in:
17234 or $gp, $reg2, $0 */
17235
17236 static void
17237 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
17238 {
17239 expressionS ex;
17240
17241 file_mips_check_options ();
17242
17243 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
17244 We also need NewABI support. */
17245 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17246 {
17247 s_ignore (0);
17248 return;
17249 }
17250
17251 if (mips_opts.mips16)
17252 {
17253 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
17254 ignore_rest_of_line ();
17255 return;
17256 }
17257
17258 mips_mark_labels ();
17259 mips_assembling_insn = TRUE;
17260
17261 macro_start ();
17262 if (mips_cpreturn_register == -1)
17263 {
17264 ex.X_op = O_constant;
17265 ex.X_add_symbol = NULL;
17266 ex.X_op_symbol = NULL;
17267 ex.X_add_number = mips_cpreturn_offset;
17268
17269 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
17270 }
17271 else
17272 move_register (mips_gp_register, mips_cpreturn_register);
17273
17274 macro_end ();
17275
17276 mips_assembling_insn = FALSE;
17277 demand_empty_rest_of_line ();
17278 }
17279
17280 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17281 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17282 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17283 debug information or MIPS16 TLS. */
17284
17285 static void
17286 s_tls_rel_directive (const size_t bytes, const char *dirstr,
17287 bfd_reloc_code_real_type rtype)
17288 {
17289 expressionS ex;
17290 char *p;
17291
17292 expression (&ex);
17293
17294 if (ex.X_op != O_symbol)
17295 {
17296 as_bad (_("unsupported use of %s"), dirstr);
17297 ignore_rest_of_line ();
17298 }
17299
17300 p = frag_more (bytes);
17301 md_number_to_chars (p, 0, bytes);
17302 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
17303 demand_empty_rest_of_line ();
17304 mips_clear_insn_labels ();
17305 }
17306
17307 /* Handle .dtprelword. */
17308
17309 static void
17310 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17311 {
17312 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
17313 }
17314
17315 /* Handle .dtpreldword. */
17316
17317 static void
17318 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17319 {
17320 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17321 }
17322
17323 /* Handle .tprelword. */
17324
17325 static void
17326 s_tprelword (int ignore ATTRIBUTE_UNUSED)
17327 {
17328 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17329 }
17330
17331 /* Handle .tpreldword. */
17332
17333 static void
17334 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17335 {
17336 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
17337 }
17338
17339 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17340 code. It sets the offset to use in gp_rel relocations. */
17341
17342 static void
17343 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
17344 {
17345 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17346 We also need NewABI support. */
17347 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17348 {
17349 s_ignore (0);
17350 return;
17351 }
17352
17353 mips_gprel_offset = get_absolute_expression ();
17354
17355 demand_empty_rest_of_line ();
17356 }
17357
17358 /* Handle the .gpword pseudo-op. This is used when generating PIC
17359 code. It generates a 32 bit GP relative reloc. */
17360
17361 static void
17362 s_gpword (int ignore ATTRIBUTE_UNUSED)
17363 {
17364 segment_info_type *si;
17365 struct insn_label_list *l;
17366 expressionS ex;
17367 char *p;
17368
17369 /* When not generating PIC code, this is treated as .word. */
17370 if (mips_pic != SVR4_PIC)
17371 {
17372 s_cons (2);
17373 return;
17374 }
17375
17376 si = seg_info (now_seg);
17377 l = si->label_list;
17378 mips_emit_delays ();
17379 if (auto_align)
17380 mips_align (2, 0, l);
17381
17382 expression (&ex);
17383 mips_clear_insn_labels ();
17384
17385 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17386 {
17387 as_bad (_("unsupported use of .gpword"));
17388 ignore_rest_of_line ();
17389 }
17390
17391 p = frag_more (4);
17392 md_number_to_chars (p, 0, 4);
17393 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17394 BFD_RELOC_GPREL32);
17395
17396 demand_empty_rest_of_line ();
17397 }
17398
17399 static void
17400 s_gpdword (int ignore ATTRIBUTE_UNUSED)
17401 {
17402 segment_info_type *si;
17403 struct insn_label_list *l;
17404 expressionS ex;
17405 char *p;
17406
17407 /* When not generating PIC code, this is treated as .dword. */
17408 if (mips_pic != SVR4_PIC)
17409 {
17410 s_cons (3);
17411 return;
17412 }
17413
17414 si = seg_info (now_seg);
17415 l = si->label_list;
17416 mips_emit_delays ();
17417 if (auto_align)
17418 mips_align (3, 0, l);
17419
17420 expression (&ex);
17421 mips_clear_insn_labels ();
17422
17423 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17424 {
17425 as_bad (_("unsupported use of .gpdword"));
17426 ignore_rest_of_line ();
17427 }
17428
17429 p = frag_more (8);
17430 md_number_to_chars (p, 0, 8);
17431 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17432 BFD_RELOC_GPREL32)->fx_tcbit = 1;
17433
17434 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
17435 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17436 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
17437
17438 demand_empty_rest_of_line ();
17439 }
17440
17441 /* Handle the .ehword pseudo-op. This is used when generating unwinding
17442 tables. It generates a R_MIPS_EH reloc. */
17443
17444 static void
17445 s_ehword (int ignore ATTRIBUTE_UNUSED)
17446 {
17447 expressionS ex;
17448 char *p;
17449
17450 mips_emit_delays ();
17451
17452 expression (&ex);
17453 mips_clear_insn_labels ();
17454
17455 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17456 {
17457 as_bad (_("unsupported use of .ehword"));
17458 ignore_rest_of_line ();
17459 }
17460
17461 p = frag_more (4);
17462 md_number_to_chars (p, 0, 4);
17463 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17464 BFD_RELOC_32_PCREL);
17465
17466 demand_empty_rest_of_line ();
17467 }
17468
17469 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
17470 tables in SVR4 PIC code. */
17471
17472 static void
17473 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17474 {
17475 int reg;
17476
17477 file_mips_check_options ();
17478
17479 /* This is ignored when not generating SVR4 PIC code. */
17480 if (mips_pic != SVR4_PIC)
17481 {
17482 s_ignore (0);
17483 return;
17484 }
17485
17486 mips_mark_labels ();
17487 mips_assembling_insn = TRUE;
17488
17489 /* Add $gp to the register named as an argument. */
17490 macro_start ();
17491 reg = tc_get_register (0);
17492 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17493 macro_end ();
17494
17495 mips_assembling_insn = FALSE;
17496 demand_empty_rest_of_line ();
17497 }
17498
17499 /* Handle the .insn pseudo-op. This marks instruction labels in
17500 mips16/micromips mode. This permits the linker to handle them specially,
17501 such as generating jalx instructions when needed. We also make
17502 them odd for the duration of the assembly, in order to generate the
17503 right sort of code. We will make them even in the adjust_symtab
17504 routine, while leaving them marked. This is convenient for the
17505 debugger and the disassembler. The linker knows to make them odd
17506 again. */
17507
17508 static void
17509 s_insn (int ignore ATTRIBUTE_UNUSED)
17510 {
17511 file_mips_check_options ();
17512 file_ase_mips16 |= mips_opts.mips16;
17513 file_ase_micromips |= mips_opts.micromips;
17514
17515 mips_mark_labels ();
17516
17517 demand_empty_rest_of_line ();
17518 }
17519
17520 /* Handle the .nan pseudo-op. */
17521
17522 static void
17523 s_nan (int ignore ATTRIBUTE_UNUSED)
17524 {
17525 static const char str_legacy[] = "legacy";
17526 static const char str_2008[] = "2008";
17527 size_t i;
17528
17529 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17530
17531 if (i == sizeof (str_2008) - 1
17532 && memcmp (input_line_pointer, str_2008, i) == 0)
17533 mips_nan2008 = 1;
17534 else if (i == sizeof (str_legacy) - 1
17535 && memcmp (input_line_pointer, str_legacy, i) == 0)
17536 {
17537 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17538 mips_nan2008 = 0;
17539 else
17540 as_bad (_("`%s' does not support legacy NaN"),
17541 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17542 }
17543 else
17544 as_bad (_("bad .nan directive"));
17545
17546 input_line_pointer += i;
17547 demand_empty_rest_of_line ();
17548 }
17549
17550 /* Handle a .stab[snd] directive. Ideally these directives would be
17551 implemented in a transparent way, so that removing them would not
17552 have any effect on the generated instructions. However, s_stab
17553 internally changes the section, so in practice we need to decide
17554 now whether the preceding label marks compressed code. We do not
17555 support changing the compression mode of a label after a .stab*
17556 directive, such as in:
17557
17558 foo:
17559 .stabs ...
17560 .set mips16
17561
17562 so the current mode wins. */
17563
17564 static void
17565 s_mips_stab (int type)
17566 {
17567 file_mips_check_options ();
17568 mips_mark_labels ();
17569 s_stab (type);
17570 }
17571
17572 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
17573
17574 static void
17575 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17576 {
17577 char *name;
17578 int c;
17579 symbolS *symbolP;
17580 expressionS exp;
17581
17582 c = get_symbol_name (&name);
17583 symbolP = symbol_find_or_make (name);
17584 S_SET_WEAK (symbolP);
17585 *input_line_pointer = c;
17586
17587 SKIP_WHITESPACE_AFTER_NAME ();
17588
17589 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17590 {
17591 if (S_IS_DEFINED (symbolP))
17592 {
17593 as_bad (_("ignoring attempt to redefine symbol %s"),
17594 S_GET_NAME (symbolP));
17595 ignore_rest_of_line ();
17596 return;
17597 }
17598
17599 if (*input_line_pointer == ',')
17600 {
17601 ++input_line_pointer;
17602 SKIP_WHITESPACE ();
17603 }
17604
17605 expression (&exp);
17606 if (exp.X_op != O_symbol)
17607 {
17608 as_bad (_("bad .weakext directive"));
17609 ignore_rest_of_line ();
17610 return;
17611 }
17612 symbol_set_value_expression (symbolP, &exp);
17613 }
17614
17615 demand_empty_rest_of_line ();
17616 }
17617
17618 /* Parse a register string into a number. Called from the ECOFF code
17619 to parse .frame. The argument is non-zero if this is the frame
17620 register, so that we can record it in mips_frame_reg. */
17621
17622 int
17623 tc_get_register (int frame)
17624 {
17625 unsigned int reg;
17626
17627 SKIP_WHITESPACE ();
17628 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17629 reg = 0;
17630 if (frame)
17631 {
17632 mips_frame_reg = reg != 0 ? reg : SP;
17633 mips_frame_reg_valid = 1;
17634 mips_cprestore_valid = 0;
17635 }
17636 return reg;
17637 }
17638
17639 valueT
17640 md_section_align (asection *seg, valueT addr)
17641 {
17642 int align = bfd_section_alignment (seg);
17643
17644 /* We don't need to align ELF sections to the full alignment.
17645 However, Irix 5 may prefer that we align them at least to a 16
17646 byte boundary. We don't bother to align the sections if we
17647 are targeted for an embedded system. */
17648 if (strncmp (TARGET_OS, "elf", 3) == 0)
17649 return addr;
17650 if (align > 4)
17651 align = 4;
17652
17653 return ((addr + (1 << align) - 1) & -(1 << align));
17654 }
17655
17656 /* Utility routine, called from above as well. If called while the
17657 input file is still being read, it's only an approximation. (For
17658 example, a symbol may later become defined which appeared to be
17659 undefined earlier.) */
17660
17661 static int
17662 nopic_need_relax (symbolS *sym, int before_relaxing)
17663 {
17664 if (sym == 0)
17665 return 0;
17666
17667 if (g_switch_value > 0)
17668 {
17669 const char *symname;
17670 int change;
17671
17672 /* Find out whether this symbol can be referenced off the $gp
17673 register. It can be if it is smaller than the -G size or if
17674 it is in the .sdata or .sbss section. Certain symbols can
17675 not be referenced off the $gp, although it appears as though
17676 they can. */
17677 symname = S_GET_NAME (sym);
17678 if (symname != (const char *) NULL
17679 && (strcmp (symname, "eprol") == 0
17680 || strcmp (symname, "etext") == 0
17681 || strcmp (symname, "_gp") == 0
17682 || strcmp (symname, "edata") == 0
17683 || strcmp (symname, "_fbss") == 0
17684 || strcmp (symname, "_fdata") == 0
17685 || strcmp (symname, "_ftext") == 0
17686 || strcmp (symname, "end") == 0
17687 || strcmp (symname, "_gp_disp") == 0))
17688 change = 1;
17689 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17690 && (0
17691 #ifndef NO_ECOFF_DEBUGGING
17692 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17693 && (symbol_get_obj (sym)->ecoff_extern_size
17694 <= g_switch_value))
17695 #endif
17696 /* We must defer this decision until after the whole
17697 file has been read, since there might be a .extern
17698 after the first use of this symbol. */
17699 || (before_relaxing
17700 #ifndef NO_ECOFF_DEBUGGING
17701 && symbol_get_obj (sym)->ecoff_extern_size == 0
17702 #endif
17703 && S_GET_VALUE (sym) == 0)
17704 || (S_GET_VALUE (sym) != 0
17705 && S_GET_VALUE (sym) <= g_switch_value)))
17706 change = 0;
17707 else
17708 {
17709 const char *segname;
17710
17711 segname = segment_name (S_GET_SEGMENT (sym));
17712 gas_assert (strcmp (segname, ".lit8") != 0
17713 && strcmp (segname, ".lit4") != 0);
17714 change = (strcmp (segname, ".sdata") != 0
17715 && strcmp (segname, ".sbss") != 0
17716 && strncmp (segname, ".sdata.", 7) != 0
17717 && strncmp (segname, ".sbss.", 6) != 0
17718 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17719 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17720 }
17721 return change;
17722 }
17723 else
17724 /* We are not optimizing for the $gp register. */
17725 return 1;
17726 }
17727
17728
17729 /* Return true if the given symbol should be considered local for SVR4 PIC. */
17730
17731 static bfd_boolean
17732 pic_need_relax (symbolS *sym)
17733 {
17734 asection *symsec;
17735
17736 /* Handle the case of a symbol equated to another symbol. */
17737 while (symbol_equated_reloc_p (sym))
17738 {
17739 symbolS *n;
17740
17741 /* It's possible to get a loop here in a badly written program. */
17742 n = symbol_get_value_expression (sym)->X_add_symbol;
17743 if (n == sym)
17744 break;
17745 sym = n;
17746 }
17747
17748 if (symbol_section_p (sym))
17749 return TRUE;
17750
17751 symsec = S_GET_SEGMENT (sym);
17752
17753 /* This must duplicate the test in adjust_reloc_syms. */
17754 return (!bfd_is_und_section (symsec)
17755 && !bfd_is_abs_section (symsec)
17756 && !bfd_is_com_section (symsec)
17757 /* A global or weak symbol is treated as external. */
17758 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
17759 }
17760 \f
17761 /* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17762 convert a section-relative value VAL to the equivalent PC-relative
17763 value. */
17764
17765 static offsetT
17766 mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17767 offsetT val, long stretch)
17768 {
17769 fragS *sym_frag;
17770 addressT addr;
17771
17772 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17773
17774 sym_frag = symbol_get_frag (fragp->fr_symbol);
17775
17776 /* If the relax_marker of the symbol fragment differs from the
17777 relax_marker of this fragment, we have not yet adjusted the
17778 symbol fragment fr_address. We want to add in STRETCH in
17779 order to get a better estimate of the address. This
17780 particularly matters because of the shift bits. */
17781 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17782 {
17783 fragS *f;
17784
17785 /* Adjust stretch for any alignment frag. Note that if have
17786 been expanding the earlier code, the symbol may be
17787 defined in what appears to be an earlier frag. FIXME:
17788 This doesn't handle the fr_subtype field, which specifies
17789 a maximum number of bytes to skip when doing an
17790 alignment. */
17791 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17792 {
17793 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17794 {
17795 if (stretch < 0)
17796 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17797 else
17798 stretch &= ~((1 << (int) f->fr_offset) - 1);
17799 if (stretch == 0)
17800 break;
17801 }
17802 }
17803 if (f != NULL)
17804 val += stretch;
17805 }
17806
17807 addr = fragp->fr_address + fragp->fr_fix;
17808
17809 /* The base address rules are complicated. The base address of
17810 a branch is the following instruction. The base address of a
17811 PC relative load or add is the instruction itself, but if it
17812 is in a delay slot (in which case it can not be extended) use
17813 the address of the instruction whose delay slot it is in. */
17814 if (pcrel_op->include_isa_bit)
17815 {
17816 addr += 2;
17817
17818 /* If we are currently assuming that this frag should be
17819 extended, then the current address is two bytes higher. */
17820 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17821 addr += 2;
17822
17823 /* Ignore the low bit in the target, since it will be set
17824 for a text label. */
17825 val &= -2;
17826 }
17827 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17828 addr -= 4;
17829 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17830 addr -= 2;
17831
17832 val -= addr & -(1 << pcrel_op->align_log2);
17833
17834 return val;
17835 }
17836
17837 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17838 extended opcode. SEC is the section the frag is in. */
17839
17840 static int
17841 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17842 {
17843 const struct mips_int_operand *operand;
17844 offsetT val;
17845 segT symsec;
17846 int type;
17847
17848 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17849 return 0;
17850 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17851 return 1;
17852
17853 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17854 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17855 operand = mips16_immed_operand (type, FALSE);
17856 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17857 || (operand->root.type == OP_PCREL
17858 ? sec != symsec
17859 : !bfd_is_abs_section (symsec)))
17860 return 1;
17861
17862 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17863
17864 if (operand->root.type == OP_PCREL)
17865 {
17866 const struct mips_pcrel_operand *pcrel_op;
17867 offsetT maxtiny;
17868
17869 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
17870 return 1;
17871
17872 pcrel_op = (const struct mips_pcrel_operand *) operand;
17873 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17874
17875 /* If any of the shifted bits are set, we must use an extended
17876 opcode. If the address depends on the size of this
17877 instruction, this can lead to a loop, so we arrange to always
17878 use an extended opcode. */
17879 if ((val & ((1 << operand->shift) - 1)) != 0)
17880 {
17881 fragp->fr_subtype =
17882 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17883 return 1;
17884 }
17885
17886 /* If we are about to mark a frag as extended because the value
17887 is precisely the next value above maxtiny, then there is a
17888 chance of an infinite loop as in the following code:
17889 la $4,foo
17890 .skip 1020
17891 .align 2
17892 foo:
17893 In this case when the la is extended, foo is 0x3fc bytes
17894 away, so the la can be shrunk, but then foo is 0x400 away, so
17895 the la must be extended. To avoid this loop, we mark the
17896 frag as extended if it was small, and is about to become
17897 extended with the next value above maxtiny. */
17898 maxtiny = mips_int_operand_max (operand);
17899 if (val == maxtiny + (1 << operand->shift)
17900 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17901 {
17902 fragp->fr_subtype =
17903 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17904 return 1;
17905 }
17906 }
17907
17908 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17909 }
17910
17911 /* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17912 macro expansion. SEC is the section the frag is in. We only
17913 support PC-relative instructions (LA, DLA, LW, LD) here, in
17914 non-PIC code using 32-bit addressing. */
17915
17916 static int
17917 mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17918 {
17919 const struct mips_pcrel_operand *pcrel_op;
17920 const struct mips_int_operand *operand;
17921 offsetT val;
17922 segT symsec;
17923 int type;
17924
17925 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17926
17927 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17928 return 0;
17929 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17930 return 0;
17931
17932 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17933 switch (type)
17934 {
17935 case 'A':
17936 case 'B':
17937 case 'E':
17938 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17939 if (bfd_is_abs_section (symsec))
17940 return 1;
17941 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17942 return 0;
17943 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17944 return 1;
17945
17946 operand = mips16_immed_operand (type, TRUE);
17947 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17948 pcrel_op = (const struct mips_pcrel_operand *) operand;
17949 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17950
17951 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17952
17953 default:
17954 return 0;
17955 }
17956 }
17957
17958 /* Compute the length of a branch sequence, and adjust the
17959 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17960 worst-case length is computed, with UPDATE being used to indicate
17961 whether an unconditional (-1), branch-likely (+1) or regular (0)
17962 branch is to be computed. */
17963 static int
17964 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17965 {
17966 bfd_boolean toofar;
17967 int length;
17968
17969 if (fragp
17970 && S_IS_DEFINED (fragp->fr_symbol)
17971 && !S_IS_WEAK (fragp->fr_symbol)
17972 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17973 {
17974 addressT addr;
17975 offsetT val;
17976
17977 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17978
17979 addr = fragp->fr_address + fragp->fr_fix + 4;
17980
17981 val -= addr;
17982
17983 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17984 }
17985 else
17986 /* If the symbol is not defined or it's in a different segment,
17987 we emit the long sequence. */
17988 toofar = TRUE;
17989
17990 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17991 fragp->fr_subtype
17992 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17993 RELAX_BRANCH_PIC (fragp->fr_subtype),
17994 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17995 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17996 RELAX_BRANCH_LINK (fragp->fr_subtype),
17997 toofar);
17998
17999 length = 4;
18000 if (toofar)
18001 {
18002 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
18003 length += 8;
18004
18005 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
18006 {
18007 /* Additional space for PIC loading of target address. */
18008 length += 8;
18009 if (mips_opts.isa == ISA_MIPS1)
18010 /* Additional space for $at-stabilizing nop. */
18011 length += 4;
18012 }
18013
18014 /* If branch is conditional. */
18015 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
18016 length += 8;
18017 }
18018
18019 return length;
18020 }
18021
18022 /* Get a FRAG's branch instruction delay slot size, either from the
18023 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
18024 or SHORT_INSN_SIZE otherwise. */
18025
18026 static int
18027 frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
18028 {
18029 char *buf = fragp->fr_literal + fragp->fr_fix;
18030
18031 if (al)
18032 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
18033 else
18034 return short_insn_size;
18035 }
18036
18037 /* Compute the length of a branch sequence, and adjust the
18038 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
18039 worst-case length is computed, with UPDATE being used to indicate
18040 whether an unconditional (-1), or regular (0) branch is to be
18041 computed. */
18042
18043 static int
18044 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
18045 {
18046 bfd_boolean insn32 = TRUE;
18047 bfd_boolean nods = TRUE;
18048 bfd_boolean pic = TRUE;
18049 bfd_boolean al = TRUE;
18050 int short_insn_size;
18051 bfd_boolean toofar;
18052 int length;
18053
18054 if (fragp)
18055 {
18056 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18057 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18058 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18059 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18060 }
18061 short_insn_size = insn32 ? 4 : 2;
18062
18063 if (fragp
18064 && S_IS_DEFINED (fragp->fr_symbol)
18065 && !S_IS_WEAK (fragp->fr_symbol)
18066 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18067 {
18068 addressT addr;
18069 offsetT val;
18070
18071 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18072 /* Ignore the low bit in the target, since it will be set
18073 for a text label. */
18074 if ((val & 1) != 0)
18075 --val;
18076
18077 addr = fragp->fr_address + fragp->fr_fix + 4;
18078
18079 val -= addr;
18080
18081 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
18082 }
18083 else
18084 /* If the symbol is not defined or it's in a different segment,
18085 we emit the long sequence. */
18086 toofar = TRUE;
18087
18088 if (fragp && update
18089 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18090 fragp->fr_subtype = (toofar
18091 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
18092 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
18093
18094 length = 4;
18095 if (toofar)
18096 {
18097 bfd_boolean compact_known = fragp != NULL;
18098 bfd_boolean compact = FALSE;
18099 bfd_boolean uncond;
18100
18101 if (fragp)
18102 {
18103 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18104 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
18105 }
18106 else
18107 uncond = update < 0;
18108
18109 /* If label is out of range, we turn branch <br>:
18110
18111 <br> label # 4 bytes
18112 0:
18113
18114 into:
18115
18116 j label # 4 bytes
18117 nop # 2/4 bytes if
18118 # compact && (!PIC || insn32)
18119 0:
18120 */
18121 if ((!pic || insn32) && (!compact_known || compact))
18122 length += short_insn_size;
18123
18124 /* If assembling PIC code, we further turn:
18125
18126 j label # 4 bytes
18127
18128 into:
18129
18130 lw/ld at, %got(label)(gp) # 4 bytes
18131 d/addiu at, %lo(label) # 4 bytes
18132 jr/c at # 2/4 bytes
18133 */
18134 if (pic)
18135 length += 4 + short_insn_size;
18136
18137 /* Add an extra nop if the jump has no compact form and we need
18138 to fill the delay slot. */
18139 if ((!pic || al) && nods)
18140 length += (fragp
18141 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
18142 : short_insn_size);
18143
18144 /* If branch <br> is conditional, we prepend negated branch <brneg>:
18145
18146 <brneg> 0f # 4 bytes
18147 nop # 2/4 bytes if !compact
18148 */
18149 if (!uncond)
18150 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
18151 }
18152 else if (nods)
18153 {
18154 /* Add an extra nop to fill the delay slot. */
18155 gas_assert (fragp);
18156 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
18157 }
18158
18159 return length;
18160 }
18161
18162 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
18163 bit accordingly. */
18164
18165 static int
18166 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
18167 {
18168 bfd_boolean toofar;
18169
18170 if (fragp
18171 && S_IS_DEFINED (fragp->fr_symbol)
18172 && !S_IS_WEAK (fragp->fr_symbol)
18173 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18174 {
18175 addressT addr;
18176 offsetT val;
18177 int type;
18178
18179 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18180 /* Ignore the low bit in the target, since it will be set
18181 for a text label. */
18182 if ((val & 1) != 0)
18183 --val;
18184
18185 /* Assume this is a 2-byte branch. */
18186 addr = fragp->fr_address + fragp->fr_fix + 2;
18187
18188 /* We try to avoid the infinite loop by not adding 2 more bytes for
18189 long branches. */
18190
18191 val -= addr;
18192
18193 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18194 if (type == 'D')
18195 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
18196 else if (type == 'E')
18197 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
18198 else
18199 abort ();
18200 }
18201 else
18202 /* If the symbol is not defined or it's in a different segment,
18203 we emit a normal 32-bit branch. */
18204 toofar = TRUE;
18205
18206 if (fragp && update
18207 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18208 fragp->fr_subtype
18209 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
18210 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
18211
18212 if (toofar)
18213 return 4;
18214
18215 return 2;
18216 }
18217
18218 /* Estimate the size of a frag before relaxing. Unless this is the
18219 mips16, we are not really relaxing here, and the final size is
18220 encoded in the subtype information. For the mips16, we have to
18221 decide whether we are using an extended opcode or not. */
18222
18223 int
18224 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
18225 {
18226 int change;
18227
18228 if (RELAX_BRANCH_P (fragp->fr_subtype))
18229 {
18230
18231 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
18232
18233 return fragp->fr_var;
18234 }
18235
18236 if (RELAX_MIPS16_P (fragp->fr_subtype))
18237 {
18238 /* We don't want to modify the EXTENDED bit here; it might get us
18239 into infinite loops. We change it only in mips_relax_frag(). */
18240 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18241 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
18242 else
18243 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
18244 }
18245
18246 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18247 {
18248 int length = 4;
18249
18250 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18251 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
18252 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18253 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
18254 fragp->fr_var = length;
18255
18256 return length;
18257 }
18258
18259 if (mips_pic == VXWORKS_PIC)
18260 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
18261 change = 0;
18262 else if (RELAX_PIC (fragp->fr_subtype))
18263 change = pic_need_relax (fragp->fr_symbol);
18264 else
18265 change = nopic_need_relax (fragp->fr_symbol, 0);
18266
18267 if (change)
18268 {
18269 fragp->fr_subtype |= RELAX_USE_SECOND;
18270 return -RELAX_FIRST (fragp->fr_subtype);
18271 }
18272 else
18273 return -RELAX_SECOND (fragp->fr_subtype);
18274 }
18275
18276 /* This is called to see whether a reloc against a defined symbol
18277 should be converted into a reloc against a section. */
18278
18279 int
18280 mips_fix_adjustable (fixS *fixp)
18281 {
18282 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18283 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18284 return 0;
18285
18286 if (fixp->fx_addsy == NULL)
18287 return 1;
18288
18289 /* Allow relocs used for EH tables. */
18290 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18291 return 1;
18292
18293 /* If symbol SYM is in a mergeable section, relocations of the form
18294 SYM + 0 can usually be made section-relative. The mergeable data
18295 is then identified by the section offset rather than by the symbol.
18296
18297 However, if we're generating REL LO16 relocations, the offset is split
18298 between the LO16 and partnering high part relocation. The linker will
18299 need to recalculate the complete offset in order to correctly identify
18300 the merge data.
18301
18302 The linker has traditionally not looked for the partnering high part
18303 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
18304 placed anywhere. Rather than break backwards compatibility by changing
18305 this, it seems better not to force the issue, and instead keep the
18306 original symbol. This will work with either linker behavior. */
18307 if ((lo16_reloc_p (fixp->fx_r_type)
18308 || reloc_needs_lo_p (fixp->fx_r_type))
18309 && HAVE_IN_PLACE_ADDENDS
18310 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
18311 return 0;
18312
18313 /* There is no place to store an in-place offset for JALR relocations. */
18314 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
18315 return 0;
18316
18317 /* Likewise an in-range offset of limited PC-relative relocations may
18318 overflow the in-place relocatable field if recalculated against the
18319 start address of the symbol's containing section.
18320
18321 Also, PC relative relocations for MIPS R6 need to be symbol rather than
18322 section relative to allow linker relaxations to be performed later on. */
18323 if (limited_pcrel_reloc_p (fixp->fx_r_type)
18324 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
18325 return 0;
18326
18327 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18328 to a floating-point stub. The same is true for non-R_MIPS16_26
18329 relocations against MIPS16 functions; in this case, the stub becomes
18330 the function's canonical address.
18331
18332 Floating-point stubs are stored in unique .mips16.call.* or
18333 .mips16.fn.* sections. If a stub T for function F is in section S,
18334 the first relocation in section S must be against F; this is how the
18335 linker determines the target function. All relocations that might
18336 resolve to T must also be against F. We therefore have the following
18337 restrictions, which are given in an intentionally-redundant way:
18338
18339 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18340 symbols.
18341
18342 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18343 if that stub might be used.
18344
18345 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18346 symbols.
18347
18348 4. We cannot reduce a stub's relocations against MIPS16 symbols if
18349 that stub might be used.
18350
18351 There is a further restriction:
18352
18353 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
18354 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
18355 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18356 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18357 against MIPS16 or microMIPS symbols because we need to keep the
18358 MIPS16 or microMIPS symbol for the purpose of mode mismatch
18359 detection and JAL or BAL to JALX instruction conversion in the
18360 linker.
18361
18362 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
18363 against a MIPS16 symbol. We deal with (5) by additionally leaving
18364 alone any jump and branch relocations against a microMIPS symbol.
18365
18366 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18367 relocation against some symbol R, no relocation against R may be
18368 reduced. (Note that this deals with (2) as well as (1) because
18369 relocations against global symbols will never be reduced on ELF
18370 targets.) This approach is a little simpler than trying to detect
18371 stub sections, and gives the "all or nothing" per-symbol consistency
18372 that we have for MIPS16 symbols. */
18373 if (fixp->fx_subsy == NULL
18374 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
18375 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
18376 && (jmp_reloc_p (fixp->fx_r_type)
18377 || b_reloc_p (fixp->fx_r_type)))
18378 || *symbol_get_tc (fixp->fx_addsy)))
18379 return 0;
18380
18381 return 1;
18382 }
18383
18384 /* Translate internal representation of relocation info to BFD target
18385 format. */
18386
18387 arelent **
18388 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
18389 {
18390 static arelent *retval[4];
18391 arelent *reloc;
18392 bfd_reloc_code_real_type code;
18393
18394 memset (retval, 0, sizeof(retval));
18395 reloc = retval[0] = XCNEW (arelent);
18396 reloc->sym_ptr_ptr = XNEW (asymbol *);
18397 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18398 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18399
18400 if (fixp->fx_pcrel)
18401 {
18402 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18403 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
18404 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18405 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
18406 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18407 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18408 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18409 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18410 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18411 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18412 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18413 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
18414
18415 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18416 Relocations want only the symbol offset. */
18417 switch (fixp->fx_r_type)
18418 {
18419 case BFD_RELOC_MIPS_18_PCREL_S3:
18420 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18421 break;
18422 default:
18423 reloc->addend = fixp->fx_addnumber + reloc->address;
18424 break;
18425 }
18426 }
18427 else if (HAVE_IN_PLACE_ADDENDS
18428 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18429 && (read_compressed_insn (fixp->fx_frag->fr_literal
18430 + fixp->fx_where, 4) >> 26) == 0x3c)
18431 {
18432 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18433 addend accordingly. */
18434 reloc->addend = fixp->fx_addnumber >> 1;
18435 }
18436 else
18437 reloc->addend = fixp->fx_addnumber;
18438
18439 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18440 entry to be used in the relocation's section offset. */
18441 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18442 {
18443 reloc->address = reloc->addend;
18444 reloc->addend = 0;
18445 }
18446
18447 code = fixp->fx_r_type;
18448
18449 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
18450 if (reloc->howto == NULL)
18451 {
18452 as_bad_where (fixp->fx_file, fixp->fx_line,
18453 _("cannot represent %s relocation in this object file"
18454 " format"),
18455 bfd_get_reloc_code_name (code));
18456 retval[0] = NULL;
18457 }
18458
18459 return retval;
18460 }
18461
18462 /* Relax a machine dependent frag. This returns the amount by which
18463 the current size of the frag should change. */
18464
18465 int
18466 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
18467 {
18468 if (RELAX_BRANCH_P (fragp->fr_subtype))
18469 {
18470 offsetT old_var = fragp->fr_var;
18471
18472 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
18473
18474 return fragp->fr_var - old_var;
18475 }
18476
18477 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18478 {
18479 offsetT old_var = fragp->fr_var;
18480 offsetT new_var = 4;
18481
18482 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18483 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18484 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18485 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18486 fragp->fr_var = new_var;
18487
18488 return new_var - old_var;
18489 }
18490
18491 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18492 return 0;
18493
18494 if (!mips16_extended_frag (fragp, sec, stretch))
18495 {
18496 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18497 {
18498 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18499 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
18500 }
18501 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18502 {
18503 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18504 return -2;
18505 }
18506 else
18507 return 0;
18508 }
18509 else if (!mips16_macro_frag (fragp, sec, stretch))
18510 {
18511 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18512 {
18513 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18514 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18515 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
18516 }
18517 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18518 {
18519 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18520 return 2;
18521 }
18522 else
18523 return 0;
18524 }
18525 else
18526 {
18527 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18528 return 0;
18529 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18530 {
18531 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18532 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18533 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
18534 }
18535 else
18536 {
18537 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18538 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
18539 }
18540 }
18541
18542 return 0;
18543 }
18544
18545 /* Convert a machine dependent frag. */
18546
18547 void
18548 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
18549 {
18550 if (RELAX_BRANCH_P (fragp->fr_subtype))
18551 {
18552 char *buf;
18553 unsigned long insn;
18554 fixS *fixp;
18555
18556 buf = fragp->fr_literal + fragp->fr_fix;
18557 insn = read_insn (buf);
18558
18559 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18560 {
18561 /* We generate a fixup instead of applying it right now
18562 because, if there are linker relaxations, we're going to
18563 need the relocations. */
18564 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18565 fragp->fr_symbol, fragp->fr_offset,
18566 TRUE, BFD_RELOC_16_PCREL_S2);
18567 fixp->fx_file = fragp->fr_file;
18568 fixp->fx_line = fragp->fr_line;
18569
18570 buf = write_insn (buf, insn);
18571 }
18572 else
18573 {
18574 int i;
18575
18576 as_warn_where (fragp->fr_file, fragp->fr_line,
18577 _("relaxed out-of-range branch into a jump"));
18578
18579 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18580 goto uncond;
18581
18582 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18583 {
18584 /* Reverse the branch. */
18585 switch ((insn >> 28) & 0xf)
18586 {
18587 case 4:
18588 if ((insn & 0xff000000) == 0x47000000
18589 || (insn & 0xff600000) == 0x45600000)
18590 {
18591 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18592 reversed by tweaking bit 23. */
18593 insn ^= 0x00800000;
18594 }
18595 else
18596 {
18597 /* bc[0-3][tf]l? instructions can have the condition
18598 reversed by tweaking a single TF bit, and their
18599 opcodes all have 0x4???????. */
18600 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18601 insn ^= 0x00010000;
18602 }
18603 break;
18604
18605 case 0:
18606 /* bltz 0x04000000 bgez 0x04010000
18607 bltzal 0x04100000 bgezal 0x04110000 */
18608 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18609 insn ^= 0x00010000;
18610 break;
18611
18612 case 1:
18613 /* beq 0x10000000 bne 0x14000000
18614 blez 0x18000000 bgtz 0x1c000000 */
18615 insn ^= 0x04000000;
18616 break;
18617
18618 default:
18619 abort ();
18620 }
18621 }
18622
18623 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18624 {
18625 /* Clear the and-link bit. */
18626 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18627
18628 /* bltzal 0x04100000 bgezal 0x04110000
18629 bltzall 0x04120000 bgezall 0x04130000 */
18630 insn &= ~0x00100000;
18631 }
18632
18633 /* Branch over the branch (if the branch was likely) or the
18634 full jump (not likely case). Compute the offset from the
18635 current instruction to branch to. */
18636 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18637 i = 16;
18638 else
18639 {
18640 /* How many bytes in instructions we've already emitted? */
18641 i = buf - fragp->fr_literal - fragp->fr_fix;
18642 /* How many bytes in instructions from here to the end? */
18643 i = fragp->fr_var - i;
18644 }
18645 /* Convert to instruction count. */
18646 i >>= 2;
18647 /* Branch counts from the next instruction. */
18648 i--;
18649 insn |= i;
18650 /* Branch over the jump. */
18651 buf = write_insn (buf, insn);
18652
18653 /* nop */
18654 buf = write_insn (buf, 0);
18655
18656 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18657 {
18658 /* beql $0, $0, 2f */
18659 insn = 0x50000000;
18660 /* Compute the PC offset from the current instruction to
18661 the end of the variable frag. */
18662 /* How many bytes in instructions we've already emitted? */
18663 i = buf - fragp->fr_literal - fragp->fr_fix;
18664 /* How many bytes in instructions from here to the end? */
18665 i = fragp->fr_var - i;
18666 /* Convert to instruction count. */
18667 i >>= 2;
18668 /* Don't decrement i, because we want to branch over the
18669 delay slot. */
18670 insn |= i;
18671
18672 buf = write_insn (buf, insn);
18673 buf = write_insn (buf, 0);
18674 }
18675
18676 uncond:
18677 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
18678 {
18679 /* j or jal. */
18680 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18681 ? 0x0c000000 : 0x08000000);
18682
18683 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18684 fragp->fr_symbol, fragp->fr_offset,
18685 FALSE, BFD_RELOC_MIPS_JMP);
18686 fixp->fx_file = fragp->fr_file;
18687 fixp->fx_line = fragp->fr_line;
18688
18689 buf = write_insn (buf, insn);
18690 }
18691 else
18692 {
18693 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18694
18695 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
18696 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18697 insn |= at << OP_SH_RT;
18698
18699 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18700 fragp->fr_symbol, fragp->fr_offset,
18701 FALSE, BFD_RELOC_MIPS_GOT16);
18702 fixp->fx_file = fragp->fr_file;
18703 fixp->fx_line = fragp->fr_line;
18704
18705 buf = write_insn (buf, insn);
18706
18707 if (mips_opts.isa == ISA_MIPS1)
18708 /* nop */
18709 buf = write_insn (buf, 0);
18710
18711 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
18712 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18713 insn |= at << OP_SH_RS | at << OP_SH_RT;
18714
18715 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18716 fragp->fr_symbol, fragp->fr_offset,
18717 FALSE, BFD_RELOC_LO16);
18718 fixp->fx_file = fragp->fr_file;
18719 fixp->fx_line = fragp->fr_line;
18720
18721 buf = write_insn (buf, insn);
18722
18723 /* j(al)r $at. */
18724 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18725 insn = 0x0000f809;
18726 else
18727 insn = 0x00000008;
18728 insn |= at << OP_SH_RS;
18729
18730 buf = write_insn (buf, insn);
18731 }
18732 }
18733
18734 fragp->fr_fix += fragp->fr_var;
18735 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18736 return;
18737 }
18738
18739 /* Relax microMIPS branches. */
18740 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18741 {
18742 char *buf = fragp->fr_literal + fragp->fr_fix;
18743 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18744 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18745 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18746 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18747 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18748 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18749 bfd_boolean short_ds;
18750 unsigned long insn;
18751 fixS *fixp;
18752
18753 fragp->fr_fix += fragp->fr_var;
18754
18755 /* Handle 16-bit branches that fit or are forced to fit. */
18756 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18757 {
18758 /* We generate a fixup instead of applying it right now,
18759 because if there is linker relaxation, we're going to
18760 need the relocations. */
18761 switch (type)
18762 {
18763 case 'D':
18764 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18765 fragp->fr_symbol, fragp->fr_offset,
18766 TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18767 break;
18768 case 'E':
18769 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18770 fragp->fr_symbol, fragp->fr_offset,
18771 TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18772 break;
18773 default:
18774 abort ();
18775 }
18776
18777 fixp->fx_file = fragp->fr_file;
18778 fixp->fx_line = fragp->fr_line;
18779
18780 /* These relocations can have an addend that won't fit in
18781 2 octets. */
18782 fixp->fx_no_overflow = 1;
18783
18784 return;
18785 }
18786
18787 /* Handle 32-bit branches that fit or are forced to fit. */
18788 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18789 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18790 {
18791 /* We generate a fixup instead of applying it right now,
18792 because if there is linker relaxation, we're going to
18793 need the relocations. */
18794 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18795 fragp->fr_symbol, fragp->fr_offset,
18796 TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18797 fixp->fx_file = fragp->fr_file;
18798 fixp->fx_line = fragp->fr_line;
18799
18800 if (type == 0)
18801 {
18802 insn = read_compressed_insn (buf, 4);
18803 buf += 4;
18804
18805 if (nods)
18806 {
18807 /* Check the short-delay-slot bit. */
18808 if (!al || (insn & 0x02000000) != 0)
18809 buf = write_compressed_insn (buf, 0x0c00, 2);
18810 else
18811 buf = write_compressed_insn (buf, 0x00000000, 4);
18812 }
18813
18814 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18815 return;
18816 }
18817 }
18818
18819 /* Relax 16-bit branches to 32-bit branches. */
18820 if (type != 0)
18821 {
18822 insn = read_compressed_insn (buf, 2);
18823
18824 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18825 insn = 0x94000000; /* beq */
18826 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18827 {
18828 unsigned long regno;
18829
18830 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18831 regno = micromips_to_32_reg_d_map [regno];
18832 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18833 insn |= regno << MICROMIPSOP_SH_RS;
18834 }
18835 else
18836 abort ();
18837
18838 /* Nothing else to do, just write it out. */
18839 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18840 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18841 {
18842 buf = write_compressed_insn (buf, insn, 4);
18843 if (nods)
18844 buf = write_compressed_insn (buf, 0x0c00, 2);
18845 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18846 return;
18847 }
18848 }
18849 else
18850 insn = read_compressed_insn (buf, 4);
18851
18852 /* Relax 32-bit branches to a sequence of instructions. */
18853 as_warn_where (fragp->fr_file, fragp->fr_line,
18854 _("relaxed out-of-range branch into a jump"));
18855
18856 /* Set the short-delay-slot bit. */
18857 short_ds = !al || (insn & 0x02000000) != 0;
18858
18859 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18860 {
18861 symbolS *l;
18862
18863 /* Reverse the branch. */
18864 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18865 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18866 insn ^= 0x20000000;
18867 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18868 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18869 || (insn & 0xffe00000) == 0x40800000 /* blez */
18870 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18871 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18872 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18873 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18874 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18875 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18876 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18877 insn ^= 0x00400000;
18878 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18879 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18880 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18881 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18882 insn ^= 0x00200000;
18883 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18884 BNZ.df */
18885 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18886 BNZ.V */
18887 insn ^= 0x00800000;
18888 else
18889 abort ();
18890
18891 if (al)
18892 {
18893 /* Clear the and-link and short-delay-slot bits. */
18894 gas_assert ((insn & 0xfda00000) == 0x40200000);
18895
18896 /* bltzal 0x40200000 bgezal 0x40600000 */
18897 /* bltzals 0x42200000 bgezals 0x42600000 */
18898 insn &= ~0x02200000;
18899 }
18900
18901 /* Make a label at the end for use with the branch. */
18902 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18903 micromips_label_inc ();
18904 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18905
18906 /* Refer to it. */
18907 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18908 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18909 fixp->fx_file = fragp->fr_file;
18910 fixp->fx_line = fragp->fr_line;
18911
18912 /* Branch over the jump. */
18913 buf = write_compressed_insn (buf, insn, 4);
18914
18915 if (!compact)
18916 {
18917 /* nop */
18918 if (insn32)
18919 buf = write_compressed_insn (buf, 0x00000000, 4);
18920 else
18921 buf = write_compressed_insn (buf, 0x0c00, 2);
18922 }
18923 }
18924
18925 if (!pic)
18926 {
18927 unsigned long jal = (short_ds || nods
18928 ? 0x74000000 : 0xf4000000); /* jal/s */
18929
18930 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18931 insn = al ? jal : 0xd4000000;
18932
18933 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18934 fragp->fr_symbol, fragp->fr_offset,
18935 FALSE, BFD_RELOC_MICROMIPS_JMP);
18936 fixp->fx_file = fragp->fr_file;
18937 fixp->fx_line = fragp->fr_line;
18938
18939 buf = write_compressed_insn (buf, insn, 4);
18940
18941 if (compact || nods)
18942 {
18943 /* nop */
18944 if (insn32)
18945 buf = write_compressed_insn (buf, 0x00000000, 4);
18946 else
18947 buf = write_compressed_insn (buf, 0x0c00, 2);
18948 }
18949 }
18950 else
18951 {
18952 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18953
18954 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18955 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18956 insn |= at << MICROMIPSOP_SH_RT;
18957
18958 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18959 fragp->fr_symbol, fragp->fr_offset,
18960 FALSE, BFD_RELOC_MICROMIPS_GOT16);
18961 fixp->fx_file = fragp->fr_file;
18962 fixp->fx_line = fragp->fr_line;
18963
18964 buf = write_compressed_insn (buf, insn, 4);
18965
18966 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18967 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18968 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18969
18970 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18971 fragp->fr_symbol, fragp->fr_offset,
18972 FALSE, BFD_RELOC_MICROMIPS_LO16);
18973 fixp->fx_file = fragp->fr_file;
18974 fixp->fx_line = fragp->fr_line;
18975
18976 buf = write_compressed_insn (buf, insn, 4);
18977
18978 if (insn32)
18979 {
18980 /* jr/jalr $at */
18981 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18982 insn |= at << MICROMIPSOP_SH_RS;
18983
18984 buf = write_compressed_insn (buf, insn, 4);
18985
18986 if (compact || nods)
18987 /* nop */
18988 buf = write_compressed_insn (buf, 0x00000000, 4);
18989 }
18990 else
18991 {
18992 /* jr/jrc/jalr/jalrs $at */
18993 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18994 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
18995
18996 insn = al ? jalr : jr;
18997 insn |= at << MICROMIPSOP_SH_MJ;
18998
18999 buf = write_compressed_insn (buf, insn, 2);
19000 if (al && nods)
19001 {
19002 /* nop */
19003 if (short_ds)
19004 buf = write_compressed_insn (buf, 0x0c00, 2);
19005 else
19006 buf = write_compressed_insn (buf, 0x00000000, 4);
19007 }
19008 }
19009 }
19010
19011 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
19012 return;
19013 }
19014
19015 if (RELAX_MIPS16_P (fragp->fr_subtype))
19016 {
19017 int type;
19018 const struct mips_int_operand *operand;
19019 offsetT val;
19020 char *buf;
19021 unsigned int user_length;
19022 bfd_boolean need_reloc;
19023 unsigned long insn;
19024 bfd_boolean mac;
19025 bfd_boolean ext;
19026 segT symsec;
19027
19028 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
19029 operand = mips16_immed_operand (type, FALSE);
19030
19031 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
19032 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
19033 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
19034
19035 symsec = S_GET_SEGMENT (fragp->fr_symbol);
19036 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
19037 || (operand->root.type == OP_PCREL && !mac
19038 ? asec != symsec
19039 : !bfd_is_abs_section (symsec)));
19040
19041 if (operand->root.type == OP_PCREL && !mac)
19042 {
19043 const struct mips_pcrel_operand *pcrel_op;
19044
19045 pcrel_op = (const struct mips_pcrel_operand *) operand;
19046
19047 if (pcrel_op->include_isa_bit && !need_reloc)
19048 {
19049 if (!mips_ignore_branch_isa
19050 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
19051 as_bad_where (fragp->fr_file, fragp->fr_line,
19052 _("branch to a symbol in another ISA mode"));
19053 else if ((fragp->fr_offset & 0x1) != 0)
19054 as_bad_where (fragp->fr_file, fragp->fr_line,
19055 _("branch to misaligned address (0x%lx)"),
19056 (long) (resolve_symbol_value (fragp->fr_symbol)
19057 + (fragp->fr_offset & ~1)));
19058 }
19059
19060 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
19061
19062 /* Make sure the section winds up with the alignment we have
19063 assumed. */
19064 if (operand->shift > 0)
19065 record_alignment (asec, operand->shift);
19066 }
19067
19068 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
19069 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
19070 {
19071 if (mac)
19072 as_warn_where (fragp->fr_file, fragp->fr_line,
19073 _("macro instruction expanded into multiple "
19074 "instructions in a branch delay slot"));
19075 else if (ext)
19076 as_warn_where (fragp->fr_file, fragp->fr_line,
19077 _("extended instruction in a branch delay slot"));
19078 }
19079 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
19080 as_warn_where (fragp->fr_file, fragp->fr_line,
19081 _("macro instruction expanded into multiple "
19082 "instructions"));
19083
19084 buf = fragp->fr_literal + fragp->fr_fix;
19085
19086 insn = read_compressed_insn (buf, 2);
19087 if (ext)
19088 insn |= MIPS16_EXTEND;
19089
19090 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
19091 user_length = 4;
19092 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
19093 user_length = 2;
19094 else
19095 user_length = 0;
19096
19097 if (mac)
19098 {
19099 unsigned long reg;
19100 unsigned long new;
19101 unsigned long op;
19102 bfd_boolean e2;
19103
19104 gas_assert (type == 'A' || type == 'B' || type == 'E');
19105 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
19106
19107 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
19108
19109 if (need_reloc)
19110 {
19111 fixS *fixp;
19112
19113 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
19114
19115 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19116 fragp->fr_symbol, fragp->fr_offset,
19117 FALSE, BFD_RELOC_MIPS16_HI16_S);
19118 fixp->fx_file = fragp->fr_file;
19119 fixp->fx_line = fragp->fr_line;
19120
19121 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
19122 fragp->fr_symbol, fragp->fr_offset,
19123 FALSE, BFD_RELOC_MIPS16_LO16);
19124 fixp->fx_file = fragp->fr_file;
19125 fixp->fx_line = fragp->fr_line;
19126
19127 val = 0;
19128 }
19129
19130 switch (insn & 0xf800)
19131 {
19132 case 0x0800: /* ADDIU */
19133 reg = (insn >> 8) & 0x7;
19134 op = 0xf0004800 | (reg << 8);
19135 break;
19136 case 0xb000: /* LW */
19137 reg = (insn >> 8) & 0x7;
19138 op = 0xf0009800 | (reg << 8) | (reg << 5);
19139 break;
19140 case 0xf800: /* I64 */
19141 reg = (insn >> 5) & 0x7;
19142 switch (insn & 0x0700)
19143 {
19144 case 0x0400: /* LD */
19145 op = 0xf0003800 | (reg << 8) | (reg << 5);
19146 break;
19147 case 0x0600: /* DADDIU */
19148 op = 0xf000fd00 | (reg << 5);
19149 break;
19150 default:
19151 abort ();
19152 }
19153 break;
19154 default:
19155 abort ();
19156 }
19157
19158 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
19159 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
19160 buf = write_compressed_insn (buf, new, 4);
19161 if (!e2)
19162 {
19163 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
19164 buf = write_compressed_insn (buf, new, 4);
19165 }
19166 op |= mips16_immed_extend (val, 16);
19167 buf = write_compressed_insn (buf, op, 4);
19168
19169 fragp->fr_fix += e2 ? 8 : 12;
19170 }
19171 else
19172 {
19173 unsigned int length = ext ? 4 : 2;
19174
19175 if (need_reloc)
19176 {
19177 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
19178 fixS *fixp;
19179
19180 switch (type)
19181 {
19182 case 'p':
19183 case 'q':
19184 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
19185 break;
19186 default:
19187 break;
19188 }
19189 if (mac || reloc == BFD_RELOC_NONE)
19190 as_bad_where (fragp->fr_file, fragp->fr_line,
19191 _("unsupported relocation"));
19192 else if (ext)
19193 {
19194 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19195 fragp->fr_symbol, fragp->fr_offset,
19196 TRUE, reloc);
19197 fixp->fx_file = fragp->fr_file;
19198 fixp->fx_line = fragp->fr_line;
19199 }
19200 else
19201 as_bad_where (fragp->fr_file, fragp->fr_line,
19202 _("invalid unextended operand value"));
19203 }
19204 else
19205 mips16_immed (fragp->fr_file, fragp->fr_line, type,
19206 BFD_RELOC_UNUSED, val, user_length, &insn);
19207
19208 gas_assert (mips16_opcode_length (insn) == length);
19209 write_compressed_insn (buf, insn, length);
19210 fragp->fr_fix += length;
19211 }
19212 }
19213 else
19214 {
19215 relax_substateT subtype = fragp->fr_subtype;
19216 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
19217 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
19218 unsigned int first, second;
19219 fixS *fixp;
19220
19221 first = RELAX_FIRST (subtype);
19222 second = RELAX_SECOND (subtype);
19223 fixp = (fixS *) fragp->fr_opcode;
19224
19225 /* If the delay slot chosen does not match the size of the instruction,
19226 then emit a warning. */
19227 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
19228 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
19229 {
19230 relax_substateT s;
19231 const char *msg;
19232
19233 s = subtype & (RELAX_DELAY_SLOT_16BIT
19234 | RELAX_DELAY_SLOT_SIZE_FIRST
19235 | RELAX_DELAY_SLOT_SIZE_SECOND);
19236 msg = macro_warning (s);
19237 if (msg != NULL)
19238 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
19239 subtype &= ~s;
19240 }
19241
19242 /* Possibly emit a warning if we've chosen the longer option. */
19243 if (use_second == second_longer)
19244 {
19245 relax_substateT s;
19246 const char *msg;
19247
19248 s = (subtype
19249 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
19250 msg = macro_warning (s);
19251 if (msg != NULL)
19252 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
19253 subtype &= ~s;
19254 }
19255
19256 /* Go through all the fixups for the first sequence. Disable them
19257 (by marking them as done) if we're going to use the second
19258 sequence instead. */
19259 while (fixp
19260 && fixp->fx_frag == fragp
19261 && fixp->fx_where + second < fragp->fr_fix)
19262 {
19263 if (subtype & RELAX_USE_SECOND)
19264 fixp->fx_done = 1;
19265 fixp = fixp->fx_next;
19266 }
19267
19268 /* Go through the fixups for the second sequence. Disable them if
19269 we're going to use the first sequence, otherwise adjust their
19270 addresses to account for the relaxation. */
19271 while (fixp && fixp->fx_frag == fragp)
19272 {
19273 if (subtype & RELAX_USE_SECOND)
19274 fixp->fx_where -= first;
19275 else
19276 fixp->fx_done = 1;
19277 fixp = fixp->fx_next;
19278 }
19279
19280 /* Now modify the frag contents. */
19281 if (subtype & RELAX_USE_SECOND)
19282 {
19283 char *start;
19284
19285 start = fragp->fr_literal + fragp->fr_fix - first - second;
19286 memmove (start, start + first, second);
19287 fragp->fr_fix -= first;
19288 }
19289 else
19290 fragp->fr_fix -= second;
19291 }
19292 }
19293
19294 /* This function is called after the relocs have been generated.
19295 We've been storing mips16 text labels as odd. Here we convert them
19296 back to even for the convenience of the debugger. */
19297
19298 void
19299 mips_frob_file_after_relocs (void)
19300 {
19301 asymbol **syms;
19302 unsigned int count, i;
19303
19304 syms = bfd_get_outsymbols (stdoutput);
19305 count = bfd_get_symcount (stdoutput);
19306 for (i = 0; i < count; i++, syms++)
19307 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
19308 && ((*syms)->value & 1) != 0)
19309 {
19310 (*syms)->value &= ~1;
19311 /* If the symbol has an odd size, it was probably computed
19312 incorrectly, so adjust that as well. */
19313 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
19314 ++elf_symbol (*syms)->internal_elf_sym.st_size;
19315 }
19316 }
19317
19318 /* This function is called whenever a label is defined, including fake
19319 labels instantiated off the dot special symbol. It is used when
19320 handling branch delays; if a branch has a label, we assume we cannot
19321 move it. This also bumps the value of the symbol by 1 in compressed
19322 code. */
19323
19324 static void
19325 mips_record_label (symbolS *sym)
19326 {
19327 segment_info_type *si = seg_info (now_seg);
19328 struct insn_label_list *l;
19329
19330 if (free_insn_labels == NULL)
19331 l = XNEW (struct insn_label_list);
19332 else
19333 {
19334 l = free_insn_labels;
19335 free_insn_labels = l->next;
19336 }
19337
19338 l->label = sym;
19339 l->next = si->label_list;
19340 si->label_list = l;
19341 }
19342
19343 /* This function is called as tc_frob_label() whenever a label is defined
19344 and adds a DWARF-2 record we only want for true labels. */
19345
19346 void
19347 mips_define_label (symbolS *sym)
19348 {
19349 mips_record_label (sym);
19350 dwarf2_emit_label (sym);
19351 }
19352
19353 /* This function is called by tc_new_dot_label whenever a new dot symbol
19354 is defined. */
19355
19356 void
19357 mips_add_dot_label (symbolS *sym)
19358 {
19359 mips_record_label (sym);
19360 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19361 mips_compressed_mark_label (sym);
19362 }
19363 \f
19364 /* Converting ASE flags from internal to .MIPS.abiflags values. */
19365 static unsigned int
19366 mips_convert_ase_flags (int ase)
19367 {
19368 unsigned int ext_ases = 0;
19369
19370 if (ase & ASE_DSP)
19371 ext_ases |= AFL_ASE_DSP;
19372 if (ase & ASE_DSPR2)
19373 ext_ases |= AFL_ASE_DSPR2;
19374 if (ase & ASE_DSPR3)
19375 ext_ases |= AFL_ASE_DSPR3;
19376 if (ase & ASE_EVA)
19377 ext_ases |= AFL_ASE_EVA;
19378 if (ase & ASE_MCU)
19379 ext_ases |= AFL_ASE_MCU;
19380 if (ase & ASE_MDMX)
19381 ext_ases |= AFL_ASE_MDMX;
19382 if (ase & ASE_MIPS3D)
19383 ext_ases |= AFL_ASE_MIPS3D;
19384 if (ase & ASE_MT)
19385 ext_ases |= AFL_ASE_MT;
19386 if (ase & ASE_SMARTMIPS)
19387 ext_ases |= AFL_ASE_SMARTMIPS;
19388 if (ase & ASE_VIRT)
19389 ext_ases |= AFL_ASE_VIRT;
19390 if (ase & ASE_MSA)
19391 ext_ases |= AFL_ASE_MSA;
19392 if (ase & ASE_XPA)
19393 ext_ases |= AFL_ASE_XPA;
19394 if (ase & ASE_MIPS16E2)
19395 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
19396 if (ase & ASE_CRC)
19397 ext_ases |= AFL_ASE_CRC;
19398 if (ase & ASE_GINV)
19399 ext_ases |= AFL_ASE_GINV;
19400 if (ase & ASE_LOONGSON_MMI)
19401 ext_ases |= AFL_ASE_LOONGSON_MMI;
19402 if (ase & ASE_LOONGSON_CAM)
19403 ext_ases |= AFL_ASE_LOONGSON_CAM;
19404 if (ase & ASE_LOONGSON_EXT)
19405 ext_ases |= AFL_ASE_LOONGSON_EXT;
19406 if (ase & ASE_LOONGSON_EXT2)
19407 ext_ases |= AFL_ASE_LOONGSON_EXT2;
19408
19409 return ext_ases;
19410 }
19411 /* Some special processing for a MIPS ELF file. */
19412
19413 void
19414 mips_elf_final_processing (void)
19415 {
19416 int fpabi;
19417 Elf_Internal_ABIFlags_v0 flags;
19418
19419 flags.version = 0;
19420 flags.isa_rev = 0;
19421 switch (file_mips_opts.isa)
19422 {
19423 case INSN_ISA1:
19424 flags.isa_level = 1;
19425 break;
19426 case INSN_ISA2:
19427 flags.isa_level = 2;
19428 break;
19429 case INSN_ISA3:
19430 flags.isa_level = 3;
19431 break;
19432 case INSN_ISA4:
19433 flags.isa_level = 4;
19434 break;
19435 case INSN_ISA5:
19436 flags.isa_level = 5;
19437 break;
19438 case INSN_ISA32:
19439 flags.isa_level = 32;
19440 flags.isa_rev = 1;
19441 break;
19442 case INSN_ISA32R2:
19443 flags.isa_level = 32;
19444 flags.isa_rev = 2;
19445 break;
19446 case INSN_ISA32R3:
19447 flags.isa_level = 32;
19448 flags.isa_rev = 3;
19449 break;
19450 case INSN_ISA32R5:
19451 flags.isa_level = 32;
19452 flags.isa_rev = 5;
19453 break;
19454 case INSN_ISA32R6:
19455 flags.isa_level = 32;
19456 flags.isa_rev = 6;
19457 break;
19458 case INSN_ISA64:
19459 flags.isa_level = 64;
19460 flags.isa_rev = 1;
19461 break;
19462 case INSN_ISA64R2:
19463 flags.isa_level = 64;
19464 flags.isa_rev = 2;
19465 break;
19466 case INSN_ISA64R3:
19467 flags.isa_level = 64;
19468 flags.isa_rev = 3;
19469 break;
19470 case INSN_ISA64R5:
19471 flags.isa_level = 64;
19472 flags.isa_rev = 5;
19473 break;
19474 case INSN_ISA64R6:
19475 flags.isa_level = 64;
19476 flags.isa_rev = 6;
19477 break;
19478 }
19479
19480 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19481 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19482 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19483 : (file_mips_opts.fp == 64) ? AFL_REG_64
19484 : AFL_REG_32;
19485 flags.cpr2_size = AFL_REG_NONE;
19486 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19487 Tag_GNU_MIPS_ABI_FP);
19488 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19489 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19490 if (file_ase_mips16)
19491 flags.ases |= AFL_ASE_MIPS16;
19492 if (file_ase_micromips)
19493 flags.ases |= AFL_ASE_MICROMIPS;
19494 flags.flags1 = 0;
19495 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19496 || file_mips_opts.fp == 64)
19497 && file_mips_opts.oddspreg)
19498 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19499 flags.flags2 = 0;
19500
19501 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19502 ((Elf_External_ABIFlags_v0 *)
19503 mips_flags_frag));
19504
19505 /* Write out the register information. */
19506 if (mips_abi != N64_ABI)
19507 {
19508 Elf32_RegInfo s;
19509
19510 s.ri_gprmask = mips_gprmask;
19511 s.ri_cprmask[0] = mips_cprmask[0];
19512 s.ri_cprmask[1] = mips_cprmask[1];
19513 s.ri_cprmask[2] = mips_cprmask[2];
19514 s.ri_cprmask[3] = mips_cprmask[3];
19515 /* The gp_value field is set by the MIPS ELF backend. */
19516
19517 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19518 ((Elf32_External_RegInfo *)
19519 mips_regmask_frag));
19520 }
19521 else
19522 {
19523 Elf64_Internal_RegInfo s;
19524
19525 s.ri_gprmask = mips_gprmask;
19526 s.ri_pad = 0;
19527 s.ri_cprmask[0] = mips_cprmask[0];
19528 s.ri_cprmask[1] = mips_cprmask[1];
19529 s.ri_cprmask[2] = mips_cprmask[2];
19530 s.ri_cprmask[3] = mips_cprmask[3];
19531 /* The gp_value field is set by the MIPS ELF backend. */
19532
19533 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19534 ((Elf64_External_RegInfo *)
19535 mips_regmask_frag));
19536 }
19537
19538 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19539 sort of BFD interface for this. */
19540 if (mips_any_noreorder)
19541 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19542 if (mips_pic != NO_PIC)
19543 {
19544 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
19545 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19546 }
19547 if (mips_abicalls)
19548 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19549
19550 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19551 defined at present; this might need to change in future. */
19552 if (file_ase_mips16)
19553 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
19554 if (file_ase_micromips)
19555 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
19556 if (file_mips_opts.ase & ASE_MDMX)
19557 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
19558
19559 /* Set the MIPS ELF ABI flags. */
19560 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
19561 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
19562 else if (mips_abi == O64_ABI)
19563 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
19564 else if (mips_abi == EABI_ABI)
19565 {
19566 if (file_mips_opts.gp == 64)
19567 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19568 else
19569 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19570 }
19571
19572 /* Nothing to do for N32_ABI or N64_ABI. */
19573
19574 if (mips_32bitmode)
19575 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
19576
19577 if (mips_nan2008 == 1)
19578 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19579
19580 /* 32 bit code with 64 bit FP registers. */
19581 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19582 Tag_GNU_MIPS_ABI_FP);
19583 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
19584 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
19585 }
19586 \f
19587 typedef struct proc {
19588 symbolS *func_sym;
19589 symbolS *func_end_sym;
19590 unsigned long reg_mask;
19591 unsigned long reg_offset;
19592 unsigned long fpreg_mask;
19593 unsigned long fpreg_offset;
19594 unsigned long frame_offset;
19595 unsigned long frame_reg;
19596 unsigned long pc_reg;
19597 } procS;
19598
19599 static procS cur_proc;
19600 static procS *cur_proc_ptr;
19601 static int numprocs;
19602
19603 /* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19604 as "2", and a normal nop as "0". */
19605
19606 #define NOP_OPCODE_MIPS 0
19607 #define NOP_OPCODE_MIPS16 1
19608 #define NOP_OPCODE_MICROMIPS 2
19609
19610 char
19611 mips_nop_opcode (void)
19612 {
19613 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19614 return NOP_OPCODE_MICROMIPS;
19615 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19616 return NOP_OPCODE_MIPS16;
19617 else
19618 return NOP_OPCODE_MIPS;
19619 }
19620
19621 /* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19622 32-bit microMIPS NOPs here (if applicable). */
19623
19624 void
19625 mips_handle_align (fragS *fragp)
19626 {
19627 char nop_opcode;
19628 char *p;
19629 int bytes, size, excess;
19630 valueT opcode;
19631
19632 if (fragp->fr_type != rs_align_code)
19633 return;
19634
19635 p = fragp->fr_literal + fragp->fr_fix;
19636 nop_opcode = *p;
19637 switch (nop_opcode)
19638 {
19639 case NOP_OPCODE_MICROMIPS:
19640 opcode = micromips_nop32_insn.insn_opcode;
19641 size = 4;
19642 break;
19643 case NOP_OPCODE_MIPS16:
19644 opcode = mips16_nop_insn.insn_opcode;
19645 size = 2;
19646 break;
19647 case NOP_OPCODE_MIPS:
19648 default:
19649 opcode = nop_insn.insn_opcode;
19650 size = 4;
19651 break;
19652 }
19653
19654 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19655 excess = bytes % size;
19656
19657 /* Handle the leading part if we're not inserting a whole number of
19658 instructions, and make it the end of the fixed part of the frag.
19659 Try to fit in a short microMIPS NOP if applicable and possible,
19660 and use zeroes otherwise. */
19661 gas_assert (excess < 4);
19662 fragp->fr_fix += excess;
19663 switch (excess)
19664 {
19665 case 3:
19666 *p++ = '\0';
19667 /* Fall through. */
19668 case 2:
19669 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
19670 {
19671 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
19672 break;
19673 }
19674 *p++ = '\0';
19675 /* Fall through. */
19676 case 1:
19677 *p++ = '\0';
19678 /* Fall through. */
19679 case 0:
19680 break;
19681 }
19682
19683 md_number_to_chars (p, opcode, size);
19684 fragp->fr_var = size;
19685 }
19686
19687 static long
19688 get_number (void)
19689 {
19690 int negative = 0;
19691 long val = 0;
19692
19693 if (*input_line_pointer == '-')
19694 {
19695 ++input_line_pointer;
19696 negative = 1;
19697 }
19698 if (!ISDIGIT (*input_line_pointer))
19699 as_bad (_("expected simple number"));
19700 if (input_line_pointer[0] == '0')
19701 {
19702 if (input_line_pointer[1] == 'x')
19703 {
19704 input_line_pointer += 2;
19705 while (ISXDIGIT (*input_line_pointer))
19706 {
19707 val <<= 4;
19708 val |= hex_value (*input_line_pointer++);
19709 }
19710 return negative ? -val : val;
19711 }
19712 else
19713 {
19714 ++input_line_pointer;
19715 while (ISDIGIT (*input_line_pointer))
19716 {
19717 val <<= 3;
19718 val |= *input_line_pointer++ - '0';
19719 }
19720 return negative ? -val : val;
19721 }
19722 }
19723 if (!ISDIGIT (*input_line_pointer))
19724 {
19725 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19726 *input_line_pointer, *input_line_pointer);
19727 as_warn (_("invalid number"));
19728 return -1;
19729 }
19730 while (ISDIGIT (*input_line_pointer))
19731 {
19732 val *= 10;
19733 val += *input_line_pointer++ - '0';
19734 }
19735 return negative ? -val : val;
19736 }
19737
19738 /* The .file directive; just like the usual .file directive, but there
19739 is an initial number which is the ECOFF file index. In the non-ECOFF
19740 case .file implies DWARF-2. */
19741
19742 static void
19743 s_mips_file (int x ATTRIBUTE_UNUSED)
19744 {
19745 static int first_file_directive = 0;
19746
19747 if (ECOFF_DEBUGGING)
19748 {
19749 get_number ();
19750 s_app_file (0);
19751 }
19752 else
19753 {
19754 char *filename;
19755
19756 filename = dwarf2_directive_filename ();
19757
19758 /* Versions of GCC up to 3.1 start files with a ".file"
19759 directive even for stabs output. Make sure that this
19760 ".file" is handled. Note that you need a version of GCC
19761 after 3.1 in order to support DWARF-2 on MIPS. */
19762 if (filename != NULL && ! first_file_directive)
19763 {
19764 (void) new_logical_line (filename, -1);
19765 s_app_file_string (filename, 0);
19766 }
19767 first_file_directive = 1;
19768 }
19769 }
19770
19771 /* The .loc directive, implying DWARF-2. */
19772
19773 static void
19774 s_mips_loc (int x ATTRIBUTE_UNUSED)
19775 {
19776 if (!ECOFF_DEBUGGING)
19777 dwarf2_directive_loc (0);
19778 }
19779
19780 /* The .end directive. */
19781
19782 static void
19783 s_mips_end (int x ATTRIBUTE_UNUSED)
19784 {
19785 symbolS *p;
19786
19787 /* Following functions need their own .frame and .cprestore directives. */
19788 mips_frame_reg_valid = 0;
19789 mips_cprestore_valid = 0;
19790
19791 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19792 {
19793 p = get_symbol ();
19794 demand_empty_rest_of_line ();
19795 }
19796 else
19797 p = NULL;
19798
19799 if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
19800 as_warn (_(".end not in text section"));
19801
19802 if (!cur_proc_ptr)
19803 {
19804 as_warn (_(".end directive without a preceding .ent directive"));
19805 demand_empty_rest_of_line ();
19806 return;
19807 }
19808
19809 if (p != NULL)
19810 {
19811 gas_assert (S_GET_NAME (p));
19812 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
19813 as_warn (_(".end symbol does not match .ent symbol"));
19814
19815 if (debug_type == DEBUG_STABS)
19816 stabs_generate_asm_endfunc (S_GET_NAME (p),
19817 S_GET_NAME (p));
19818 }
19819 else
19820 as_warn (_(".end directive missing or unknown symbol"));
19821
19822 /* Create an expression to calculate the size of the function. */
19823 if (p && cur_proc_ptr)
19824 {
19825 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19826 expressionS *exp = XNEW (expressionS);
19827
19828 obj->size = exp;
19829 exp->X_op = O_subtract;
19830 exp->X_add_symbol = symbol_temp_new_now ();
19831 exp->X_op_symbol = p;
19832 exp->X_add_number = 0;
19833
19834 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19835 }
19836
19837 #ifdef md_flush_pending_output
19838 md_flush_pending_output ();
19839 #endif
19840
19841 /* Generate a .pdr section. */
19842 if (!ECOFF_DEBUGGING && mips_flag_pdr)
19843 {
19844 segT saved_seg = now_seg;
19845 subsegT saved_subseg = now_subseg;
19846 expressionS exp;
19847 char *fragp;
19848
19849 gas_assert (pdr_seg);
19850 subseg_set (pdr_seg, 0);
19851
19852 /* Write the symbol. */
19853 exp.X_op = O_symbol;
19854 exp.X_add_symbol = p;
19855 exp.X_add_number = 0;
19856 emit_expr (&exp, 4);
19857
19858 fragp = frag_more (7 * 4);
19859
19860 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19861 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19862 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19863 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19864 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19865 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19866 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19867
19868 subseg_set (saved_seg, saved_subseg);
19869 }
19870
19871 cur_proc_ptr = NULL;
19872 }
19873
19874 /* The .aent and .ent directives. */
19875
19876 static void
19877 s_mips_ent (int aent)
19878 {
19879 symbolS *symbolP;
19880
19881 symbolP = get_symbol ();
19882 if (*input_line_pointer == ',')
19883 ++input_line_pointer;
19884 SKIP_WHITESPACE ();
19885 if (ISDIGIT (*input_line_pointer)
19886 || *input_line_pointer == '-')
19887 get_number ();
19888
19889 if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
19890 as_warn (_(".ent or .aent not in text section"));
19891
19892 if (!aent && cur_proc_ptr)
19893 as_warn (_("missing .end"));
19894
19895 if (!aent)
19896 {
19897 /* This function needs its own .frame and .cprestore directives. */
19898 mips_frame_reg_valid = 0;
19899 mips_cprestore_valid = 0;
19900
19901 cur_proc_ptr = &cur_proc;
19902 memset (cur_proc_ptr, '\0', sizeof (procS));
19903
19904 cur_proc_ptr->func_sym = symbolP;
19905
19906 ++numprocs;
19907
19908 if (debug_type == DEBUG_STABS)
19909 stabs_generate_asm_func (S_GET_NAME (symbolP),
19910 S_GET_NAME (symbolP));
19911 }
19912
19913 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19914
19915 demand_empty_rest_of_line ();
19916 }
19917
19918 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19919 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19920 s_mips_frame is used so that we can set the PDR information correctly.
19921 We can't use the ecoff routines because they make reference to the ecoff
19922 symbol table (in the mdebug section). */
19923
19924 static void
19925 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19926 {
19927 if (ECOFF_DEBUGGING)
19928 s_ignore (ignore);
19929 else
19930 {
19931 long val;
19932
19933 if (cur_proc_ptr == (procS *) NULL)
19934 {
19935 as_warn (_(".frame outside of .ent"));
19936 demand_empty_rest_of_line ();
19937 return;
19938 }
19939
19940 cur_proc_ptr->frame_reg = tc_get_register (1);
19941
19942 SKIP_WHITESPACE ();
19943 if (*input_line_pointer++ != ','
19944 || get_absolute_expression_and_terminator (&val) != ',')
19945 {
19946 as_warn (_("bad .frame directive"));
19947 --input_line_pointer;
19948 demand_empty_rest_of_line ();
19949 return;
19950 }
19951
19952 cur_proc_ptr->frame_offset = val;
19953 cur_proc_ptr->pc_reg = tc_get_register (0);
19954
19955 demand_empty_rest_of_line ();
19956 }
19957 }
19958
19959 /* The .fmask and .mask directives. If the mdebug section is present
19960 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19961 embedded targets, s_mips_mask is used so that we can set the PDR
19962 information correctly. We can't use the ecoff routines because they
19963 make reference to the ecoff symbol table (in the mdebug section). */
19964
19965 static void
19966 s_mips_mask (int reg_type)
19967 {
19968 if (ECOFF_DEBUGGING)
19969 s_ignore (reg_type);
19970 else
19971 {
19972 long mask, off;
19973
19974 if (cur_proc_ptr == (procS *) NULL)
19975 {
19976 as_warn (_(".mask/.fmask outside of .ent"));
19977 demand_empty_rest_of_line ();
19978 return;
19979 }
19980
19981 if (get_absolute_expression_and_terminator (&mask) != ',')
19982 {
19983 as_warn (_("bad .mask/.fmask directive"));
19984 --input_line_pointer;
19985 demand_empty_rest_of_line ();
19986 return;
19987 }
19988
19989 off = get_absolute_expression ();
19990
19991 if (reg_type == 'F')
19992 {
19993 cur_proc_ptr->fpreg_mask = mask;
19994 cur_proc_ptr->fpreg_offset = off;
19995 }
19996 else
19997 {
19998 cur_proc_ptr->reg_mask = mask;
19999 cur_proc_ptr->reg_offset = off;
20000 }
20001
20002 demand_empty_rest_of_line ();
20003 }
20004 }
20005
20006 /* A table describing all the processors gas knows about. Names are
20007 matched in the order listed.
20008
20009 To ease comparison, please keep this table in the same order as
20010 gcc's mips_cpu_info_table[]. */
20011 static const struct mips_cpu_info mips_cpu_info_table[] =
20012 {
20013 /* Entries for generic ISAs. */
20014 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
20015 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
20016 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
20017 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
20018 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
20019 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
20020 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20021 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
20022 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
20023 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
20024 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
20025 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
20026 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
20027 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
20028 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
20029
20030 /* MIPS I */
20031 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
20032 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
20033 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
20034
20035 /* MIPS II */
20036 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
20037
20038 /* MIPS III */
20039 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
20040 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
20041 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
20042 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
20043 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
20044 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
20045 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
20046 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
20047 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
20048 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
20049 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
20050 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
20051 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
20052 /* ST Microelectronics Loongson 2E and 2F cores. */
20053 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
20054 { "loongson2f", 0, ASE_LOONGSON_MMI, ISA_MIPS3, CPU_LOONGSON_2F },
20055
20056 /* MIPS IV */
20057 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
20058 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
20059 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
20060 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
20061 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
20062 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
20063 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
20064 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
20065 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
20066 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
20067 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
20068 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
20069 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
20070 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
20071 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
20072
20073 /* MIPS 32 */
20074 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20075 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20076 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20077 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
20078
20079 /* MIPS 32 Release 2 */
20080 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20081 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20082 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20083 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
20084 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20085 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20086 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
20087 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
20088 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20089 ISA_MIPS32R2, CPU_MIPS32R2 },
20090 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20091 ISA_MIPS32R2, CPU_MIPS32R2 },
20092 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20093 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20094 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20095 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20096 /* Deprecated forms of the above. */
20097 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20098 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20099 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
20100 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20101 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20102 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20103 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20104 /* Deprecated forms of the above. */
20105 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20106 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20107 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
20108 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20109 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20110 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20111 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20112 /* Deprecated forms of the above. */
20113 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20114 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20115 /* 34Kn is a 34kc without DSP. */
20116 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20117 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
20118 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20119 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20120 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20121 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20122 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20123 /* Deprecated forms of the above. */
20124 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20125 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20126 /* 1004K cores are multiprocessor versions of the 34K. */
20127 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20128 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20129 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20130 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20131 /* interaptiv is the new name for 1004kf. */
20132 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20133 { "interaptiv-mr2", 0,
20134 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
20135 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
20136 /* M5100 family. */
20137 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
20138 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
20139 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
20140 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
20141
20142 /* MIPS 64 */
20143 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20144 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20145 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
20146 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
20147
20148 /* Broadcom SB-1 CPU core. */
20149 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
20150 /* Broadcom SB-1A CPU core. */
20151 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
20152
20153 /* MIPS 64 Release 2. */
20154 /* Loongson CPU core. */
20155 /* -march=loongson3a is an alias of -march=gs464 for compatibility. */
20156 { "loongson3a", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
20157 ISA_MIPS64R2, CPU_GS464 },
20158 { "gs464", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
20159 ISA_MIPS64R2, CPU_GS464 },
20160 { "gs464e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20161 | ASE_LOONGSON_EXT2, ISA_MIPS64R2, CPU_GS464E },
20162 { "gs264e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20163 | ASE_LOONGSON_EXT2 | ASE_MSA | ASE_MSA64, ISA_MIPS64R2, CPU_GS264E },
20164
20165 /* Cavium Networks Octeon CPU core. */
20166 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
20167 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
20168 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
20169 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
20170
20171 /* RMI Xlr */
20172 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
20173
20174 /* Broadcom XLP.
20175 XLP is mostly like XLR, with the prominent exception that it is
20176 MIPS64R2 rather than MIPS64. */
20177 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
20178
20179 /* MIPS 64 Release 6. */
20180 { "i6400", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
20181 { "i6500", 0, ASE_VIRT | ASE_MSA | ASE_CRC | ASE_GINV,
20182 ISA_MIPS64R6, CPU_MIPS64R6},
20183 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
20184
20185 /* End marker. */
20186 { NULL, 0, 0, 0, 0 }
20187 };
20188
20189
20190 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
20191 with a final "000" replaced by "k". Ignore case.
20192
20193 Note: this function is shared between GCC and GAS. */
20194
20195 static bfd_boolean
20196 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
20197 {
20198 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
20199 given++, canonical++;
20200
20201 return ((*given == 0 && *canonical == 0)
20202 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
20203 }
20204
20205
20206 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
20207 CPU name. We've traditionally allowed a lot of variation here.
20208
20209 Note: this function is shared between GCC and GAS. */
20210
20211 static bfd_boolean
20212 mips_matching_cpu_name_p (const char *canonical, const char *given)
20213 {
20214 /* First see if the name matches exactly, or with a final "000"
20215 turned into "k". */
20216 if (mips_strict_matching_cpu_name_p (canonical, given))
20217 return TRUE;
20218
20219 /* If not, try comparing based on numerical designation alone.
20220 See if GIVEN is an unadorned number, or 'r' followed by a number. */
20221 if (TOLOWER (*given) == 'r')
20222 given++;
20223 if (!ISDIGIT (*given))
20224 return FALSE;
20225
20226 /* Skip over some well-known prefixes in the canonical name,
20227 hoping to find a number there too. */
20228 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
20229 canonical += 2;
20230 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
20231 canonical += 2;
20232 else if (TOLOWER (canonical[0]) == 'r')
20233 canonical += 1;
20234
20235 return mips_strict_matching_cpu_name_p (canonical, given);
20236 }
20237
20238
20239 /* Parse an option that takes the name of a processor as its argument.
20240 OPTION is the name of the option and CPU_STRING is the argument.
20241 Return the corresponding processor enumeration if the CPU_STRING is
20242 recognized, otherwise report an error and return null.
20243
20244 A similar function exists in GCC. */
20245
20246 static const struct mips_cpu_info *
20247 mips_parse_cpu (const char *option, const char *cpu_string)
20248 {
20249 const struct mips_cpu_info *p;
20250
20251 /* 'from-abi' selects the most compatible architecture for the given
20252 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
20253 EABIs, we have to decide whether we're using the 32-bit or 64-bit
20254 version. Look first at the -mgp options, if given, otherwise base
20255 the choice on MIPS_DEFAULT_64BIT.
20256
20257 Treat NO_ABI like the EABIs. One reason to do this is that the
20258 plain 'mips' and 'mips64' configs have 'from-abi' as their default
20259 architecture. This code picks MIPS I for 'mips' and MIPS III for
20260 'mips64', just as we did in the days before 'from-abi'. */
20261 if (strcasecmp (cpu_string, "from-abi") == 0)
20262 {
20263 if (ABI_NEEDS_32BIT_REGS (mips_abi))
20264 return mips_cpu_info_from_isa (ISA_MIPS1);
20265
20266 if (ABI_NEEDS_64BIT_REGS (mips_abi))
20267 return mips_cpu_info_from_isa (ISA_MIPS3);
20268
20269 if (file_mips_opts.gp >= 0)
20270 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
20271 ? ISA_MIPS1 : ISA_MIPS3);
20272
20273 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
20274 ? ISA_MIPS3
20275 : ISA_MIPS1);
20276 }
20277
20278 /* 'default' has traditionally been a no-op. Probably not very useful. */
20279 if (strcasecmp (cpu_string, "default") == 0)
20280 return 0;
20281
20282 for (p = mips_cpu_info_table; p->name != 0; p++)
20283 if (mips_matching_cpu_name_p (p->name, cpu_string))
20284 return p;
20285
20286 as_bad (_("bad value (%s) for %s"), cpu_string, option);
20287 return 0;
20288 }
20289
20290 /* Return the canonical processor information for ISA (a member of the
20291 ISA_MIPS* enumeration). */
20292
20293 static const struct mips_cpu_info *
20294 mips_cpu_info_from_isa (int isa)
20295 {
20296 int i;
20297
20298 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20299 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
20300 && isa == mips_cpu_info_table[i].isa)
20301 return (&mips_cpu_info_table[i]);
20302
20303 return NULL;
20304 }
20305
20306 static const struct mips_cpu_info *
20307 mips_cpu_info_from_arch (int arch)
20308 {
20309 int i;
20310
20311 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20312 if (arch == mips_cpu_info_table[i].cpu)
20313 return (&mips_cpu_info_table[i]);
20314
20315 return NULL;
20316 }
20317 \f
20318 static void
20319 show (FILE *stream, const char *string, int *col_p, int *first_p)
20320 {
20321 if (*first_p)
20322 {
20323 fprintf (stream, "%24s", "");
20324 *col_p = 24;
20325 }
20326 else
20327 {
20328 fprintf (stream, ", ");
20329 *col_p += 2;
20330 }
20331
20332 if (*col_p + strlen (string) > 72)
20333 {
20334 fprintf (stream, "\n%24s", "");
20335 *col_p = 24;
20336 }
20337
20338 fprintf (stream, "%s", string);
20339 *col_p += strlen (string);
20340
20341 *first_p = 0;
20342 }
20343
20344 void
20345 md_show_usage (FILE *stream)
20346 {
20347 int column, first;
20348 size_t i;
20349
20350 fprintf (stream, _("\
20351 MIPS options:\n\
20352 -EB generate big endian output\n\
20353 -EL generate little endian output\n\
20354 -g, -g2 do not remove unneeded NOPs or swap branches\n\
20355 -G NUM allow referencing objects up to NUM bytes\n\
20356 implicitly with the gp register [default 8]\n"));
20357 fprintf (stream, _("\
20358 -mips1 generate MIPS ISA I instructions\n\
20359 -mips2 generate MIPS ISA II instructions\n\
20360 -mips3 generate MIPS ISA III instructions\n\
20361 -mips4 generate MIPS ISA IV instructions\n\
20362 -mips5 generate MIPS ISA V instructions\n\
20363 -mips32 generate MIPS32 ISA instructions\n\
20364 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
20365 -mips32r3 generate MIPS32 release 3 ISA instructions\n\
20366 -mips32r5 generate MIPS32 release 5 ISA instructions\n\
20367 -mips32r6 generate MIPS32 release 6 ISA instructions\n\
20368 -mips64 generate MIPS64 ISA instructions\n\
20369 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
20370 -mips64r3 generate MIPS64 release 3 ISA instructions\n\
20371 -mips64r5 generate MIPS64 release 5 ISA instructions\n\
20372 -mips64r6 generate MIPS64 release 6 ISA instructions\n\
20373 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
20374
20375 first = 1;
20376
20377 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20378 show (stream, mips_cpu_info_table[i].name, &column, &first);
20379 show (stream, "from-abi", &column, &first);
20380 fputc ('\n', stream);
20381
20382 fprintf (stream, _("\
20383 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20384 -no-mCPU don't generate code specific to CPU.\n\
20385 For -mCPU and -no-mCPU, CPU must be one of:\n"));
20386
20387 first = 1;
20388
20389 show (stream, "3900", &column, &first);
20390 show (stream, "4010", &column, &first);
20391 show (stream, "4100", &column, &first);
20392 show (stream, "4650", &column, &first);
20393 fputc ('\n', stream);
20394
20395 fprintf (stream, _("\
20396 -mips16 generate mips16 instructions\n\
20397 -no-mips16 do not generate mips16 instructions\n"));
20398 fprintf (stream, _("\
20399 -mmips16e2 generate MIPS16e2 instructions\n\
20400 -mno-mips16e2 do not generate MIPS16e2 instructions\n"));
20401 fprintf (stream, _("\
20402 -mmicromips generate microMIPS instructions\n\
20403 -mno-micromips do not generate microMIPS instructions\n"));
20404 fprintf (stream, _("\
20405 -msmartmips generate smartmips instructions\n\
20406 -mno-smartmips do not generate smartmips instructions\n"));
20407 fprintf (stream, _("\
20408 -mdsp generate DSP instructions\n\
20409 -mno-dsp do not generate DSP instructions\n"));
20410 fprintf (stream, _("\
20411 -mdspr2 generate DSP R2 instructions\n\
20412 -mno-dspr2 do not generate DSP R2 instructions\n"));
20413 fprintf (stream, _("\
20414 -mdspr3 generate DSP R3 instructions\n\
20415 -mno-dspr3 do not generate DSP R3 instructions\n"));
20416 fprintf (stream, _("\
20417 -mmt generate MT instructions\n\
20418 -mno-mt do not generate MT instructions\n"));
20419 fprintf (stream, _("\
20420 -mmcu generate MCU instructions\n\
20421 -mno-mcu do not generate MCU instructions\n"));
20422 fprintf (stream, _("\
20423 -mmsa generate MSA instructions\n\
20424 -mno-msa do not generate MSA instructions\n"));
20425 fprintf (stream, _("\
20426 -mxpa generate eXtended Physical Address (XPA) instructions\n\
20427 -mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20428 fprintf (stream, _("\
20429 -mvirt generate Virtualization instructions\n\
20430 -mno-virt do not generate Virtualization instructions\n"));
20431 fprintf (stream, _("\
20432 -mcrc generate CRC instructions\n\
20433 -mno-crc do not generate CRC instructions\n"));
20434 fprintf (stream, _("\
20435 -mginv generate Global INValidate (GINV) instructions\n\
20436 -mno-ginv do not generate Global INValidate instructions\n"));
20437 fprintf (stream, _("\
20438 -mloongson-mmi generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20439 -mno-loongson-mmi do not generate Loongson MultiMedia extensions Instructions\n"));
20440 fprintf (stream, _("\
20441 -mloongson-cam generate Loongson Content Address Memory (CAM) instructions\n\
20442 -mno-loongson-cam do not generate Loongson Content Address Memory Instructions\n"));
20443 fprintf (stream, _("\
20444 -mloongson-ext generate Loongson EXTensions (EXT) instructions\n\
20445 -mno-loongson-ext do not generate Loongson EXTensions Instructions\n"));
20446 fprintf (stream, _("\
20447 -mloongson-ext2 generate Loongson EXTensions R2 (EXT2) instructions\n\
20448 -mno-loongson-ext2 do not generate Loongson EXTensions R2 Instructions\n"));
20449 fprintf (stream, _("\
20450 -minsn32 only generate 32-bit microMIPS instructions\n\
20451 -mno-insn32 generate all microMIPS instructions\n"));
20452 #if DEFAULT_MIPS_FIX_LOONGSON3_LLSC
20453 fprintf (stream, _("\
20454 -mfix-loongson3-llsc work around Loongson3 LL/SC errata, default\n\
20455 -mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n"));
20456 #else
20457 fprintf (stream, _("\
20458 -mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20459 -mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata, default\n"));
20460 #endif
20461 fprintf (stream, _("\
20462 -mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20463 -mfix-loongson2f-nop work around Loongson2F NOP errata\n\
20464 -mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20465 -mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n\
20466 -mfix-vr4120 work around certain VR4120 errata\n\
20467 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
20468 -mfix-24k insert a nop after ERET and DERET instructions\n\
20469 -mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
20470 -mfix-r5900 work around R5900 short loop errata\n\
20471 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20472 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
20473 -msym32 assume all symbols have 32-bit values\n\
20474 -O0 do not remove unneeded NOPs, do not swap branches\n\
20475 -O, -O1 remove unneeded NOPs, do not swap branches\n\
20476 -O2 remove unneeded NOPs and swap branches\n\
20477 --trap, --no-break trap exception on div by 0 and mult overflow\n\
20478 --break, --no-trap break exception on div by 0 and mult overflow\n"));
20479 fprintf (stream, _("\
20480 -mhard-float allow floating-point instructions\n\
20481 -msoft-float do not allow floating-point instructions\n\
20482 -msingle-float only allow 32-bit floating-point operations\n\
20483 -mdouble-float allow 32-bit and 64-bit floating-point operations\n\
20484 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
20485 --[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
20486 -mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20487 -mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
20488 -mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20489
20490 first = 1;
20491
20492 show (stream, "legacy", &column, &first);
20493 show (stream, "2008", &column, &first);
20494
20495 fputc ('\n', stream);
20496
20497 fprintf (stream, _("\
20498 -KPIC, -call_shared generate SVR4 position independent code\n\
20499 -call_nonpic generate non-PIC code that can operate with DSOs\n\
20500 -mvxworks-pic generate VxWorks position independent code\n\
20501 -non_shared do not generate code that can operate with DSOs\n\
20502 -xgot assume a 32 bit GOT\n\
20503 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
20504 -mshared, -mno-shared disable/enable .cpload optimization for\n\
20505 position dependent (non shared) code\n\
20506 -mabi=ABI create ABI conformant object file for:\n"));
20507
20508 first = 1;
20509
20510 show (stream, "32", &column, &first);
20511 show (stream, "o64", &column, &first);
20512 show (stream, "n32", &column, &first);
20513 show (stream, "64", &column, &first);
20514 show (stream, "eabi", &column, &first);
20515
20516 fputc ('\n', stream);
20517
20518 fprintf (stream, _("\
20519 -32 create o32 ABI object file%s\n"),
20520 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20521 fprintf (stream, _("\
20522 -n32 create n32 ABI object file%s\n"),
20523 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20524 fprintf (stream, _("\
20525 -64 create 64 ABI object file%s\n"),
20526 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
20527 }
20528
20529 #ifdef TE_IRIX
20530 enum dwarf2_format
20531 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
20532 {
20533 if (HAVE_64BIT_SYMBOLS)
20534 return dwarf2_format_64bit_irix;
20535 else
20536 return dwarf2_format_32bit;
20537 }
20538 #endif
20539
20540 int
20541 mips_dwarf2_addr_size (void)
20542 {
20543 if (HAVE_64BIT_OBJECTS)
20544 return 8;
20545 else
20546 return 4;
20547 }
20548
20549 /* Standard calling conventions leave the CFA at SP on entry. */
20550 void
20551 mips_cfi_frame_initial_instructions (void)
20552 {
20553 cfi_add_CFA_def_cfa_register (SP);
20554 }
20555
20556 int
20557 tc_mips_regname_to_dw2regnum (char *regname)
20558 {
20559 unsigned int regnum = -1;
20560 unsigned int reg;
20561
20562 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20563 regnum = reg;
20564
20565 return regnum;
20566 }
20567
20568 /* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20569 Given a symbolic attribute NAME, return the proper integer value.
20570 Returns -1 if the attribute is not known. */
20571
20572 int
20573 mips_convert_symbolic_attribute (const char *name)
20574 {
20575 static const struct
20576 {
20577 const char * name;
20578 const int tag;
20579 }
20580 attribute_table[] =
20581 {
20582 #define T(tag) {#tag, tag}
20583 T (Tag_GNU_MIPS_ABI_FP),
20584 T (Tag_GNU_MIPS_ABI_MSA),
20585 #undef T
20586 };
20587 unsigned int i;
20588
20589 if (name == NULL)
20590 return -1;
20591
20592 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20593 if (streq (name, attribute_table[i].name))
20594 return attribute_table[i].tag;
20595
20596 return -1;
20597 }
20598
20599 void
20600 md_mips_end (void)
20601 {
20602 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20603
20604 mips_emit_delays ();
20605 if (cur_proc_ptr)
20606 as_warn (_("missing .end at end of assembly"));
20607
20608 /* Just in case no code was emitted, do the consistency check. */
20609 file_mips_check_options ();
20610
20611 /* Set a floating-point ABI if the user did not. */
20612 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20613 {
20614 /* Perform consistency checks on the floating-point ABI. */
20615 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20616 Tag_GNU_MIPS_ABI_FP);
20617 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20618 check_fpabi (fpabi);
20619 }
20620 else
20621 {
20622 /* Soft-float gets precedence over single-float, the two options should
20623 not be used together so this should not matter. */
20624 if (file_mips_opts.soft_float == 1)
20625 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20626 /* Single-float gets precedence over all double_float cases. */
20627 else if (file_mips_opts.single_float == 1)
20628 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20629 else
20630 {
20631 switch (file_mips_opts.fp)
20632 {
20633 case 32:
20634 if (file_mips_opts.gp == 32)
20635 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20636 break;
20637 case 0:
20638 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20639 break;
20640 case 64:
20641 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20642 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20643 else if (file_mips_opts.gp == 32)
20644 fpabi = Val_GNU_MIPS_ABI_FP_64;
20645 else
20646 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20647 break;
20648 }
20649 }
20650
20651 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20652 Tag_GNU_MIPS_ABI_FP, fpabi);
20653 }
20654 }
20655
20656 /* Returns the relocation type required for a particular CFI encoding. */
20657
20658 bfd_reloc_code_real_type
20659 mips_cfi_reloc_for_encoding (int encoding)
20660 {
20661 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20662 return BFD_RELOC_32_PCREL;
20663 else return BFD_RELOC_NONE;
20664 }
This page took 0.494607 seconds and 4 git commands to generate.